Merge pull request #2738 from filecoin-project/feat/better-gas-used
Add gas guesstimation
This commit is contained in:
commit
a8e12f94e9
74
miner/guessgas.go
Normal file
74
miner/guessgas.go
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
package miner
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/ipfs/go-cid"
|
||||||
|
"golang.org/x/xerrors"
|
||||||
|
|
||||||
|
"github.com/filecoin-project/lotus/chain/types"
|
||||||
|
|
||||||
|
"github.com/filecoin-project/go-address"
|
||||||
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
||||||
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
||||||
|
)
|
||||||
|
|
||||||
|
const failedGasGuessRatio = 0.5
|
||||||
|
const failedGasGuessMax = 25_000_000
|
||||||
|
|
||||||
|
type costKey struct {
|
||||||
|
code cid.Cid
|
||||||
|
m abi.MethodNum
|
||||||
|
}
|
||||||
|
|
||||||
|
var costs = map[costKey]int64{
|
||||||
|
{builtin.InitActorCodeID, 2}: 8916753,
|
||||||
|
{builtin.StorageMarketActorCodeID, 2}: 6955002,
|
||||||
|
{builtin.StorageMarketActorCodeID, 4}: 245436108,
|
||||||
|
{builtin.StorageMinerActorCodeID, 4}: 2315133,
|
||||||
|
{builtin.StorageMinerActorCodeID, 5}: 1600271356,
|
||||||
|
{builtin.StorageMinerActorCodeID, 6}: 22864493,
|
||||||
|
{builtin.StorageMinerActorCodeID, 7}: 142002419,
|
||||||
|
{builtin.StorageMinerActorCodeID, 10}: 23008274,
|
||||||
|
{builtin.StorageMinerActorCodeID, 11}: 19303178,
|
||||||
|
{builtin.StorageMinerActorCodeID, 14}: 566356835,
|
||||||
|
{builtin.StorageMinerActorCodeID, 16}: 5325185,
|
||||||
|
{builtin.StorageMinerActorCodeID, 18}: 2328637,
|
||||||
|
{builtin.StoragePowerActorCodeID, 2}: 23600956,
|
||||||
|
}
|
||||||
|
|
||||||
|
func failedGuess(msg *types.SignedMessage) int64 {
|
||||||
|
guess := int64(float64(msg.Message.GasLimit) * failedGasGuessRatio)
|
||||||
|
if guess > failedGasGuessMax {
|
||||||
|
guess = failedGasGuessMax
|
||||||
|
}
|
||||||
|
return guess
|
||||||
|
}
|
||||||
|
|
||||||
|
func GuessGasUsed(ctx context.Context, tsk types.TipSetKey, msg *types.SignedMessage, al ActorLookup) (int64, error) {
|
||||||
|
if msg.Message.Method == builtin.MethodSend {
|
||||||
|
switch msg.Message.From.Protocol() {
|
||||||
|
case address.BLS:
|
||||||
|
return 1298450, nil
|
||||||
|
case address.SECP256K1:
|
||||||
|
return 1385999, nil
|
||||||
|
default:
|
||||||
|
// who knows?
|
||||||
|
return 1298450, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
to, err := al(ctx, msg.Message.To, tsk)
|
||||||
|
if err != nil {
|
||||||
|
return failedGuess(msg), xerrors.Errorf("could not lookup actor: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
guess, ok := costs[costKey{to.Code, msg.Message.Method}]
|
||||||
|
if !ok {
|
||||||
|
return failedGuess(msg), xerrors.Errorf("unknown code-method combo")
|
||||||
|
}
|
||||||
|
if guess > msg.Message.GasLimit {
|
||||||
|
guess = msg.Message.GasLimit
|
||||||
|
}
|
||||||
|
return guess, nil
|
||||||
|
}
|
198
miner/miner.go
198
miner/miner.go
@ -6,14 +6,11 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
big2 "math/big"
|
|
||||||
"sort"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/filecoin-project/go-address"
|
"github.com/filecoin-project/go-address"
|
||||||
"github.com/filecoin-project/specs-actors/actors/abi"
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
||||||
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
|
||||||
"github.com/filecoin-project/specs-actors/actors/crypto"
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
||||||
lru "github.com/hashicorp/golang-lru"
|
lru "github.com/hashicorp/golang-lru"
|
||||||
|
|
||||||
@ -22,7 +19,6 @@ import (
|
|||||||
"github.com/filecoin-project/lotus/chain/gen"
|
"github.com/filecoin-project/lotus/chain/gen"
|
||||||
"github.com/filecoin-project/lotus/chain/store"
|
"github.com/filecoin-project/lotus/chain/store"
|
||||||
"github.com/filecoin-project/lotus/chain/types"
|
"github.com/filecoin-project/lotus/chain/types"
|
||||||
"github.com/filecoin-project/lotus/chain/vm"
|
|
||||||
|
|
||||||
logging "github.com/ipfs/go-log/v2"
|
logging "github.com/ipfs/go-log/v2"
|
||||||
"go.opencensus.io/trace"
|
"go.opencensus.io/trace"
|
||||||
@ -482,197 +478,3 @@ func countFrom(msgs []*types.SignedMessage, from address.Address) (out int) {
|
|||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func SelectMessages(ctx context.Context, al ActorLookup, ts *types.TipSet, msgs []*types.SignedMessage) ([]*types.SignedMessage, error) {
|
|
||||||
al = (&cachedActorLookup{
|
|
||||||
tsk: ts.Key(),
|
|
||||||
cache: map[address.Address]actCacheEntry{},
|
|
||||||
fallback: al,
|
|
||||||
}).StateGetActor
|
|
||||||
|
|
||||||
type senderMeta struct {
|
|
||||||
lastReward abi.TokenAmount
|
|
||||||
lastGasLimit int64
|
|
||||||
|
|
||||||
gasReward []abi.TokenAmount
|
|
||||||
gasLimit []int64
|
|
||||||
|
|
||||||
msgs []*types.SignedMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
inclNonces := make(map[address.Address]uint64)
|
|
||||||
inclBalances := make(map[address.Address]big.Int)
|
|
||||||
outBySender := make(map[address.Address]*senderMeta)
|
|
||||||
|
|
||||||
tooLowFundMsgs := 0
|
|
||||||
tooHighNonceMsgs := 0
|
|
||||||
|
|
||||||
start := build.Clock.Now()
|
|
||||||
vmValid := time.Duration(0)
|
|
||||||
getbal := time.Duration(0)
|
|
||||||
|
|
||||||
sort.Slice(msgs, func(i, j int) bool {
|
|
||||||
return msgs[i].Message.Nonce < msgs[j].Message.Nonce
|
|
||||||
})
|
|
||||||
|
|
||||||
for _, msg := range msgs {
|
|
||||||
vmstart := build.Clock.Now()
|
|
||||||
|
|
||||||
minGas := vm.PricelistByEpoch(ts.Height()).OnChainMessage(msg.ChainLength()) // TODO: really should be doing just msg.ChainLength() but the sync side of this code doesnt seem to have access to that
|
|
||||||
if err := msg.VMMessage().ValidForBlockInclusion(minGas.Total()); err != nil {
|
|
||||||
log.Warnf("invalid message in message pool: %s", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
vmValid += build.Clock.Since(vmstart)
|
|
||||||
|
|
||||||
// TODO: this should be in some more general 'validate message' call
|
|
||||||
if msg.Message.GasLimit > build.BlockGasLimit {
|
|
||||||
log.Warnf("message in mempool had too high of a gas limit (%d)", msg.Message.GasLimit)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if msg.Message.To == address.Undef {
|
|
||||||
log.Warnf("message in mempool had bad 'To' address")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
from := msg.Message.From
|
|
||||||
|
|
||||||
getBalStart := build.Clock.Now()
|
|
||||||
if _, ok := inclNonces[from]; !ok {
|
|
||||||
act, err := al(ctx, from, ts.Key())
|
|
||||||
if err != nil {
|
|
||||||
log.Warnf("failed to check message sender balance, skipping message: %+v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
inclNonces[from] = act.Nonce
|
|
||||||
inclBalances[from] = act.Balance
|
|
||||||
}
|
|
||||||
getbal += build.Clock.Since(getBalStart)
|
|
||||||
|
|
||||||
if inclBalances[from].LessThan(msg.Message.RequiredFunds()) {
|
|
||||||
tooLowFundMsgs++
|
|
||||||
// todo: drop from mpool
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if msg.Message.Nonce > inclNonces[from] {
|
|
||||||
tooHighNonceMsgs++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if msg.Message.Nonce < inclNonces[from] {
|
|
||||||
log.Warnf("message in mempool has already used nonce (%d < %d), from %s, to %s, %s (%d pending for)", msg.Message.Nonce, inclNonces[from], msg.Message.From, msg.Message.To, msg.Cid(), countFrom(msgs, from))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
inclNonces[from] = msg.Message.Nonce + 1
|
|
||||||
inclBalances[from] = types.BigSub(inclBalances[from], msg.Message.RequiredFunds())
|
|
||||||
sm := outBySender[from]
|
|
||||||
if sm == nil {
|
|
||||||
sm = &senderMeta{
|
|
||||||
lastReward: big.Zero(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sm.gasLimit = append(sm.gasLimit, sm.lastGasLimit+msg.Message.GasLimit)
|
|
||||||
sm.lastGasLimit = sm.gasLimit[len(sm.gasLimit)-1]
|
|
||||||
|
|
||||||
estimatedReward := big.Mul(types.NewInt(uint64(msg.Message.GasLimit)), msg.Message.GasPrice)
|
|
||||||
// TODO: estimatedReward = estimatedReward * (guessActualGasUse(msg) / msg.GasLimit)
|
|
||||||
|
|
||||||
sm.gasReward = append(sm.gasReward, big.Add(sm.lastReward, estimatedReward))
|
|
||||||
sm.lastReward = sm.gasReward[len(sm.gasReward)-1]
|
|
||||||
|
|
||||||
sm.msgs = append(sm.msgs, msg)
|
|
||||||
|
|
||||||
outBySender[from] = sm
|
|
||||||
}
|
|
||||||
|
|
||||||
gasLimitLeft := int64(build.BlockGasLimit)
|
|
||||||
|
|
||||||
orderedSenders := make([]address.Address, 0, len(outBySender))
|
|
||||||
for k := range outBySender {
|
|
||||||
orderedSenders = append(orderedSenders, k)
|
|
||||||
}
|
|
||||||
sort.Slice(orderedSenders, func(i, j int) bool {
|
|
||||||
return bytes.Compare(orderedSenders[i].Bytes(), orderedSenders[j].Bytes()) == -1
|
|
||||||
})
|
|
||||||
|
|
||||||
out := make([]*types.SignedMessage, 0, build.BlockMessageLimit)
|
|
||||||
{
|
|
||||||
for {
|
|
||||||
var bestSender address.Address
|
|
||||||
var nBest int
|
|
||||||
var bestGasToReward float64
|
|
||||||
|
|
||||||
// TODO: This is O(n^2)-ish, could use something like container/heap to cache this math
|
|
||||||
for _, sender := range orderedSenders {
|
|
||||||
meta, ok := outBySender[sender]
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for n := range meta.msgs {
|
|
||||||
if meta.gasLimit[n] > gasLimitLeft {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if n+len(out) > build.BlockMessageLimit {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
gasToReward, _ := new(big2.Float).SetInt(meta.gasReward[n].Int).Float64()
|
|
||||||
gasToReward /= float64(meta.gasLimit[n])
|
|
||||||
|
|
||||||
if gasToReward >= bestGasToReward {
|
|
||||||
bestSender = sender
|
|
||||||
nBest = n + 1
|
|
||||||
bestGasToReward = gasToReward
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if nBest == 0 {
|
|
||||||
break // block gas limit reached
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
out = append(out, outBySender[bestSender].msgs[:nBest]...)
|
|
||||||
gasLimitLeft -= outBySender[bestSender].gasLimit[nBest-1]
|
|
||||||
|
|
||||||
outBySender[bestSender].msgs = outBySender[bestSender].msgs[nBest:]
|
|
||||||
outBySender[bestSender].gasLimit = outBySender[bestSender].gasLimit[nBest:]
|
|
||||||
outBySender[bestSender].gasReward = outBySender[bestSender].gasReward[nBest:]
|
|
||||||
|
|
||||||
if len(outBySender[bestSender].msgs) == 0 {
|
|
||||||
delete(outBySender, bestSender)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(out) >= build.BlockMessageLimit {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if tooLowFundMsgs > 0 {
|
|
||||||
log.Warnf("%d messages in mempool does not have enough funds", tooLowFundMsgs)
|
|
||||||
}
|
|
||||||
|
|
||||||
if tooHighNonceMsgs > 0 {
|
|
||||||
log.Warnf("%d messages in mempool had too high nonce", tooHighNonceMsgs)
|
|
||||||
}
|
|
||||||
|
|
||||||
sm := build.Clock.Now()
|
|
||||||
if sm.Sub(start) > time.Second {
|
|
||||||
log.Warnw("SelectMessages took a long time",
|
|
||||||
"duration", sm.Sub(start),
|
|
||||||
"vmvalidate", vmValid,
|
|
||||||
"getbalance", getbal,
|
|
||||||
"msgs", len(msgs))
|
|
||||||
}
|
|
||||||
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
@ -5,7 +5,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/filecoin-project/go-address"
|
"github.com/filecoin-project/go-address"
|
||||||
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
||||||
|
|
||||||
|
"github.com/filecoin-project/lotus/build"
|
||||||
"github.com/filecoin-project/lotus/chain/types"
|
"github.com/filecoin-project/lotus/chain/types"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
@ -19,6 +21,71 @@ func mustIDAddr(i uint64) address.Address {
|
|||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSelectNotOverLimited(t *testing.T) {
|
||||||
|
ctx := context.TODO()
|
||||||
|
a1 := mustIDAddr(1)
|
||||||
|
a2 := mustIDAddr(2)
|
||||||
|
a3 := mustIDAddr(3)
|
||||||
|
|
||||||
|
actors := map[address.Address]*types.Actor{
|
||||||
|
a1: {
|
||||||
|
Code: builtin.AccountActorCodeID,
|
||||||
|
Nonce: 1,
|
||||||
|
Balance: types.FromFil(1200),
|
||||||
|
},
|
||||||
|
a2: {
|
||||||
|
Code: builtin.AccountActorCodeID,
|
||||||
|
Nonce: 1,
|
||||||
|
Balance: types.FromFil(1200),
|
||||||
|
},
|
||||||
|
a3: {
|
||||||
|
Code: builtin.StorageMinerActorCodeID,
|
||||||
|
Nonce: 0,
|
||||||
|
Balance: types.FromFil(1000),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
af := func(ctx context.Context, addr address.Address, tsk types.TipSetKey) (*types.Actor, error) {
|
||||||
|
return actors[addr], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
gasUsed := costs[costKey{builtin.StorageMinerActorCodeID, 4}]
|
||||||
|
|
||||||
|
var goodMsgs []types.Message
|
||||||
|
for i := int64(0); i < build.BlockGasLimit/gasUsed+10; i++ {
|
||||||
|
goodMsgs = append(goodMsgs, types.Message{
|
||||||
|
From: a1,
|
||||||
|
To: a3,
|
||||||
|
Method: 4,
|
||||||
|
Nonce: uint64(1 + i),
|
||||||
|
Value: types.FromFil(0),
|
||||||
|
GasLimit: gasUsed + 1000,
|
||||||
|
GasPrice: types.NewInt(1),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
var badMsgs []types.Message
|
||||||
|
for i := int64(0); i < build.BlockGasLimit/gasUsed+10; i++ {
|
||||||
|
badMsgs = append(badMsgs, types.Message{
|
||||||
|
From: a2,
|
||||||
|
To: a3,
|
||||||
|
Method: 4,
|
||||||
|
Nonce: uint64(1 + i),
|
||||||
|
Value: types.FromFil(0),
|
||||||
|
GasLimit: 10 * gasUsed,
|
||||||
|
GasPrice: types.NewInt(9),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
outmsgs, err := SelectMessages(ctx, af, &types.TipSet{}, wrapMsgs(append(goodMsgs, badMsgs...)))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, v := range outmsgs {
|
||||||
|
if v.Message.From == a2 {
|
||||||
|
t.Errorf("included bad actor message: %v", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMessageFiltering(t *testing.T) {
|
func TestMessageFiltering(t *testing.T) {
|
||||||
ctx := context.TODO()
|
ctx := context.TODO()
|
||||||
a1 := mustIDAddr(1)
|
a1 := mustIDAddr(1)
|
||||||
|
218
miner/selectmessages.go
Normal file
218
miner/selectmessages.go
Normal file
@ -0,0 +1,218 @@
|
|||||||
|
package miner
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
big2 "math/big"
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/filecoin-project/go-address"
|
||||||
|
"github.com/filecoin-project/lotus/build"
|
||||||
|
"github.com/filecoin-project/lotus/chain/types"
|
||||||
|
"github.com/filecoin-project/lotus/chain/vm"
|
||||||
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
||||||
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
func SelectMessages(ctx context.Context, al ActorLookup, ts *types.TipSet, msgs []*types.SignedMessage) ([]*types.SignedMessage, error) {
|
||||||
|
al = (&cachedActorLookup{
|
||||||
|
tsk: ts.Key(),
|
||||||
|
cache: map[address.Address]actCacheEntry{},
|
||||||
|
fallback: al,
|
||||||
|
}).StateGetActor
|
||||||
|
|
||||||
|
type senderMeta struct {
|
||||||
|
lastReward abi.TokenAmount
|
||||||
|
lastGasLimit int64
|
||||||
|
|
||||||
|
gasReward []abi.TokenAmount
|
||||||
|
gasLimit []int64
|
||||||
|
|
||||||
|
msgs []*types.SignedMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
inclNonces := make(map[address.Address]uint64)
|
||||||
|
inclBalances := make(map[address.Address]big.Int)
|
||||||
|
outBySender := make(map[address.Address]*senderMeta)
|
||||||
|
|
||||||
|
tooLowFundMsgs := 0
|
||||||
|
tooHighNonceMsgs := 0
|
||||||
|
|
||||||
|
start := build.Clock.Now()
|
||||||
|
vmValid := time.Duration(0)
|
||||||
|
getbal := time.Duration(0)
|
||||||
|
guessGasDur := time.Duration(0)
|
||||||
|
|
||||||
|
sort.Slice(msgs, func(i, j int) bool {
|
||||||
|
return msgs[i].Message.Nonce < msgs[j].Message.Nonce
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, msg := range msgs {
|
||||||
|
vmstart := build.Clock.Now()
|
||||||
|
|
||||||
|
minGas := vm.PricelistByEpoch(ts.Height()).OnChainMessage(msg.ChainLength()) // TODO: really should be doing just msg.ChainLength() but the sync side of this code doesnt seem to have access to that
|
||||||
|
if err := msg.VMMessage().ValidForBlockInclusion(minGas.Total()); err != nil {
|
||||||
|
log.Warnf("invalid message in message pool: %s", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
vmValid += build.Clock.Since(vmstart)
|
||||||
|
|
||||||
|
// TODO: this should be in some more general 'validate message' call
|
||||||
|
if msg.Message.GasLimit > build.BlockGasLimit {
|
||||||
|
log.Warnf("message in mempool had too high of a gas limit (%d)", msg.Message.GasLimit)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg.Message.To == address.Undef {
|
||||||
|
log.Warnf("message in mempool had bad 'To' address")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
from := msg.Message.From
|
||||||
|
|
||||||
|
getBalStart := build.Clock.Now()
|
||||||
|
if _, ok := inclNonces[from]; !ok {
|
||||||
|
act, err := al(ctx, from, ts.Key())
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("failed to check message sender balance, skipping message: %+v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
inclNonces[from] = act.Nonce
|
||||||
|
inclBalances[from] = act.Balance
|
||||||
|
}
|
||||||
|
getbal += build.Clock.Since(getBalStart)
|
||||||
|
|
||||||
|
if inclBalances[from].LessThan(msg.Message.RequiredFunds()) {
|
||||||
|
tooLowFundMsgs++
|
||||||
|
// todo: drop from mpool
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg.Message.Nonce > inclNonces[from] {
|
||||||
|
tooHighNonceMsgs++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg.Message.Nonce < inclNonces[from] {
|
||||||
|
log.Warnf("message in mempool has already used nonce (%d < %d), from %s, to %s, %s (%d pending for)", msg.Message.Nonce, inclNonces[from], msg.Message.From, msg.Message.To, msg.Cid(), countFrom(msgs, from))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
inclNonces[from] = msg.Message.Nonce + 1
|
||||||
|
inclBalances[from] = types.BigSub(inclBalances[from], msg.Message.RequiredFunds())
|
||||||
|
sm := outBySender[from]
|
||||||
|
if sm == nil {
|
||||||
|
sm = &senderMeta{
|
||||||
|
lastReward: big.Zero(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sm.gasLimit = append(sm.gasLimit, sm.lastGasLimit+msg.Message.GasLimit)
|
||||||
|
sm.lastGasLimit = sm.gasLimit[len(sm.gasLimit)-1]
|
||||||
|
|
||||||
|
guessGasStart := build.Clock.Now()
|
||||||
|
guessedGas, err := GuessGasUsed(ctx, ts.Key(), msg, al)
|
||||||
|
guessGasDur += build.Clock.Since(guessGasStart)
|
||||||
|
if err != nil {
|
||||||
|
log.Infow("failed to guess gas", "to", msg.Message.To, "method", msg.Message.Method, "err", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
estimatedReward := big.Mul(types.NewInt(uint64(guessedGas)), msg.Message.GasPrice)
|
||||||
|
|
||||||
|
sm.gasReward = append(sm.gasReward, big.Add(sm.lastReward, estimatedReward))
|
||||||
|
sm.lastReward = sm.gasReward[len(sm.gasReward)-1]
|
||||||
|
|
||||||
|
sm.msgs = append(sm.msgs, msg)
|
||||||
|
|
||||||
|
outBySender[from] = sm
|
||||||
|
}
|
||||||
|
|
||||||
|
gasLimitLeft := int64(build.BlockGasLimit)
|
||||||
|
|
||||||
|
orderedSenders := make([]address.Address, 0, len(outBySender))
|
||||||
|
for k := range outBySender {
|
||||||
|
orderedSenders = append(orderedSenders, k)
|
||||||
|
}
|
||||||
|
sort.Slice(orderedSenders, func(i, j int) bool {
|
||||||
|
return bytes.Compare(orderedSenders[i].Bytes(), orderedSenders[j].Bytes()) == -1
|
||||||
|
})
|
||||||
|
|
||||||
|
out := make([]*types.SignedMessage, 0, build.BlockMessageLimit)
|
||||||
|
{
|
||||||
|
for {
|
||||||
|
var bestSender address.Address
|
||||||
|
var nBest int
|
||||||
|
var bestGasToReward float64
|
||||||
|
|
||||||
|
// TODO: This is O(n^2)-ish, could use something like container/heap to cache this math
|
||||||
|
for _, sender := range orderedSenders {
|
||||||
|
meta, ok := outBySender[sender]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for n := range meta.msgs {
|
||||||
|
if meta.gasLimit[n] > gasLimitLeft {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if n+len(out) > build.BlockMessageLimit {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
gasToReward, _ := new(big2.Float).SetInt(meta.gasReward[n].Int).Float64()
|
||||||
|
gasToReward /= float64(meta.gasLimit[n])
|
||||||
|
|
||||||
|
if gasToReward >= bestGasToReward {
|
||||||
|
bestSender = sender
|
||||||
|
nBest = n + 1
|
||||||
|
bestGasToReward = gasToReward
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if nBest == 0 {
|
||||||
|
break // block gas limit reached
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
out = append(out, outBySender[bestSender].msgs[:nBest]...)
|
||||||
|
gasLimitLeft -= outBySender[bestSender].gasLimit[nBest-1]
|
||||||
|
|
||||||
|
outBySender[bestSender].msgs = outBySender[bestSender].msgs[nBest:]
|
||||||
|
outBySender[bestSender].gasLimit = outBySender[bestSender].gasLimit[nBest:]
|
||||||
|
outBySender[bestSender].gasReward = outBySender[bestSender].gasReward[nBest:]
|
||||||
|
|
||||||
|
if len(outBySender[bestSender].msgs) == 0 {
|
||||||
|
delete(outBySender, bestSender)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(out) >= build.BlockMessageLimit {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if tooLowFundMsgs > 0 {
|
||||||
|
log.Warnf("%d messages in mempool does not have enough funds", tooLowFundMsgs)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tooHighNonceMsgs > 0 {
|
||||||
|
log.Warnf("%d messages in mempool had too high nonce", tooHighNonceMsgs)
|
||||||
|
}
|
||||||
|
|
||||||
|
sm := build.Clock.Now()
|
||||||
|
if sm.Sub(start) > time.Second {
|
||||||
|
log.Warnw("SelectMessages took a long time",
|
||||||
|
"duration", sm.Sub(start),
|
||||||
|
"vmvalidate", vmValid,
|
||||||
|
"getbalance", getbal,
|
||||||
|
"guessgas", guessGasDur,
|
||||||
|
"msgs", len(msgs))
|
||||||
|
}
|
||||||
|
|
||||||
|
return out, nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user