2020-08-04 06:40:02 +00:00
|
|
|
package messagepool
|
|
|
|
|
|
|
|
import (
|
2020-08-06 22:40:25 +00:00
|
|
|
"context"
|
2020-08-05 06:46:48 +00:00
|
|
|
"math/big"
|
2020-09-09 09:37:03 +00:00
|
|
|
"math/rand"
|
2020-08-05 06:46:48 +00:00
|
|
|
"sort"
|
2020-08-04 06:40:02 +00:00
|
|
|
"time"
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2020-08-06 08:17:11 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-08-04 06:40:02 +00:00
|
|
|
|
2020-08-06 08:17:11 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-09-07 03:49:10 +00:00
|
|
|
tbig "github.com/filecoin-project/go-state-types/big"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
2020-09-30 17:32:19 +00:00
|
|
|
|
2020-08-04 06:40:02 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/messagepool/gasguess"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-08-04 19:12:15 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
2020-08-04 06:40:02 +00:00
|
|
|
)
|
|
|
|
|
2020-08-05 07:22:40 +00:00
|
|
|
var bigBlockGasLimit = big.NewInt(build.BlockGasLimit)
|
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
const MaxBlocks = 15
|
|
|
|
|
2020-08-04 06:40:02 +00:00
|
|
|
type msgChain struct {
|
2020-08-13 00:23:52 +00:00
|
|
|
msgs []*types.SignedMessage
|
|
|
|
gasReward *big.Int
|
|
|
|
gasLimit int64
|
|
|
|
gasPerf float64
|
|
|
|
effPerf float64
|
|
|
|
bp float64
|
|
|
|
parentOffset float64
|
|
|
|
valid bool
|
|
|
|
merged bool
|
|
|
|
next *msgChain
|
|
|
|
prev *msgChain
|
2021-09-11 00:24:12 +00:00
|
|
|
sigType crypto.SigType
|
2020-08-04 06:40:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
func (mp *MessagePool) SelectMessages(ctx context.Context, ts *types.TipSet, tq float64) ([]*types.SignedMessage, error) {
|
2020-08-04 06:40:02 +00:00
|
|
|
mp.curTsLk.Lock()
|
2020-08-07 00:48:53 +00:00
|
|
|
defer mp.curTsLk.Unlock()
|
2020-08-04 06:40:02 +00:00
|
|
|
|
2023-03-25 01:49:02 +00:00
|
|
|
//TODO confirm if we can switch to RLock here for performance
|
2020-08-04 06:40:02 +00:00
|
|
|
mp.lk.Lock()
|
|
|
|
defer mp.lk.Unlock()
|
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
// if the ticket quality is high enough that the first block has higher probability
|
|
|
|
// than any other block, then we don't bother with optimal selection because the
|
|
|
|
// first block will always have higher effective performance
|
2021-09-11 00:24:12 +00:00
|
|
|
var sm *selectedMessages
|
|
|
|
var err error
|
2020-08-11 09:33:17 +00:00
|
|
|
if tq > 0.84 {
|
2021-09-11 00:24:12 +00:00
|
|
|
sm, err = mp.selectMessagesGreedy(ctx, mp.curTs, ts)
|
2020-11-18 10:25:26 +00:00
|
|
|
} else {
|
2021-09-11 00:24:12 +00:00
|
|
|
sm, err = mp.selectMessagesOptimal(ctx, mp.curTs, ts, tq)
|
2020-11-18 10:25:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-12 01:23:49 +00:00
|
|
|
if sm == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
// one last sanity check
|
|
|
|
if len(sm.msgs) > build.BlockMessageLimit {
|
2021-12-13 23:31:31 +00:00
|
|
|
log.Errorf("message selection chose too many messages %d > %d", len(sm.msgs), build.BlockMessageLimit)
|
2021-09-11 00:24:12 +00:00
|
|
|
sm.msgs = sm.msgs[:build.BlockMessageLimit]
|
|
|
|
}
|
|
|
|
|
|
|
|
return sm.msgs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type selectedMessages struct {
|
|
|
|
msgs []*types.SignedMessage
|
|
|
|
gasLimit int64
|
|
|
|
secpLimit int
|
|
|
|
blsLimit int
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns false if chain can't be added due to block constraints
|
|
|
|
func (sm *selectedMessages) tryToAdd(mc *msgChain) bool {
|
|
|
|
l := len(mc.msgs)
|
|
|
|
|
|
|
|
if build.BlockMessageLimit < l+len(sm.msgs) || sm.gasLimit < mc.gasLimit {
|
|
|
|
return false
|
2020-08-11 09:33:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
if mc.sigType == crypto.SigTypeBLS {
|
|
|
|
if sm.blsLimit < l {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sm.msgs = append(sm.msgs, mc.msgs...)
|
|
|
|
sm.blsLimit -= l
|
|
|
|
sm.gasLimit -= mc.gasLimit
|
2023-01-13 19:11:13 +00:00
|
|
|
} else if mc.sigType == crypto.SigTypeSecp256k1 || mc.sigType == crypto.SigTypeDelegated {
|
2021-09-11 00:24:12 +00:00
|
|
|
if sm.secpLimit < l {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sm.msgs = append(sm.msgs, mc.msgs...)
|
|
|
|
sm.secpLimit -= l
|
|
|
|
sm.gasLimit -= mc.gasLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't add the weird sigType msg, but otherwise proceed
|
|
|
|
return true
|
2020-08-11 09:33:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
// returns false if messages can't be added due to block constraints
|
|
|
|
// will trim / invalidate chain as appropriate
|
|
|
|
func (sm *selectedMessages) tryToAddWithDeps(mc *msgChain, mp *MessagePool, baseFee types.BigInt) bool {
|
|
|
|
// compute the dependencies that must be merged and the gas limit including deps
|
|
|
|
chainGasLimit := mc.gasLimit
|
|
|
|
chainMsgLimit := len(mc.msgs)
|
|
|
|
depGasLimit := int64(0)
|
|
|
|
depMsgLimit := 0
|
|
|
|
smMsgLimit := 0
|
|
|
|
|
|
|
|
if mc.sigType == crypto.SigTypeBLS {
|
|
|
|
smMsgLimit = sm.blsLimit
|
2023-01-13 19:11:13 +00:00
|
|
|
} else if mc.sigType == crypto.SigTypeSecp256k1 || mc.sigType == crypto.SigTypeDelegated {
|
2021-09-11 00:24:12 +00:00
|
|
|
smMsgLimit = sm.secpLimit
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if smMsgLimit > build.BlockMessageLimit-len(sm.msgs) {
|
|
|
|
smMsgLimit = build.BlockMessageLimit - len(sm.msgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
var chainDeps []*msgChain
|
|
|
|
for curChain := mc.prev; curChain != nil && !curChain.merged; curChain = curChain.prev {
|
|
|
|
chainDeps = append(chainDeps, curChain)
|
|
|
|
chainGasLimit += curChain.gasLimit
|
|
|
|
chainMsgLimit += len(curChain.msgs)
|
|
|
|
depGasLimit += curChain.gasLimit
|
|
|
|
depMsgLimit += len(curChain.msgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// the chain doesn't fit as-is, so trim / invalidate it and return false
|
|
|
|
if chainGasLimit > sm.gasLimit || chainMsgLimit > smMsgLimit {
|
|
|
|
|
|
|
|
// it doesn't all fit; now we have to take into account the dependent chains before
|
|
|
|
// making a decision about trimming or invalidating.
|
|
|
|
// if the dependencies exceed the block limits, then we must invalidate the chain
|
|
|
|
// as it can never be included.
|
|
|
|
// Otherwise we can just trim and continue
|
|
|
|
if depGasLimit > sm.gasLimit || depMsgLimit >= smMsgLimit {
|
|
|
|
mc.Invalidate()
|
|
|
|
} else {
|
|
|
|
// dependencies fit, just trim it
|
|
|
|
mc.Trim(sm.gasLimit-depGasLimit, smMsgLimit-depMsgLimit, mp, baseFee)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// the chain fits! include it together with all dependencies
|
|
|
|
for i := len(chainDeps) - 1; i >= 0; i-- {
|
|
|
|
curChain := chainDeps[i]
|
|
|
|
curChain.merged = true
|
|
|
|
sm.msgs = append(sm.msgs, curChain.msgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
mc.merged = true
|
|
|
|
|
|
|
|
sm.msgs = append(sm.msgs, mc.msgs...)
|
|
|
|
sm.gasLimit -= chainGasLimit
|
|
|
|
|
|
|
|
if mc.sigType == crypto.SigTypeBLS {
|
|
|
|
sm.blsLimit -= chainMsgLimit
|
2023-01-13 19:11:13 +00:00
|
|
|
} else if mc.sigType == crypto.SigTypeSecp256k1 || mc.sigType == crypto.SigTypeDelegated {
|
2021-09-11 00:24:12 +00:00
|
|
|
sm.secpLimit -= chainMsgLimit
|
2020-08-11 09:33:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
return true
|
2020-08-11 09:33:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
func (sm *selectedMessages) trimChain(mc *msgChain, mp *MessagePool, baseFee types.BigInt) {
|
|
|
|
msgLimit := build.BlockMessageLimit - len(sm.msgs)
|
|
|
|
if mc.sigType == crypto.SigTypeBLS {
|
|
|
|
if msgLimit > sm.blsLimit {
|
|
|
|
msgLimit = sm.blsLimit
|
|
|
|
}
|
2023-01-13 19:11:13 +00:00
|
|
|
} else if mc.sigType == crypto.SigTypeSecp256k1 || mc.sigType == crypto.SigTypeDelegated {
|
2021-09-11 00:24:12 +00:00
|
|
|
if msgLimit > sm.secpLimit {
|
|
|
|
msgLimit = sm.secpLimit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if mc.gasLimit > sm.gasLimit || len(mc.msgs) > msgLimit {
|
|
|
|
mc.Trim(sm.gasLimit, msgLimit, mp, baseFee)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mp *MessagePool) selectMessagesOptimal(ctx context.Context, curTs, ts *types.TipSet, tq float64) (*selectedMessages, error) {
|
2020-08-11 09:33:17 +00:00
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
baseFee, err := mp.api.ChainComputeBaseFee(context.TODO(), ts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("computing basefee: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 17:20:59 +00:00
|
|
|
// 0. Load messages from the target tipset; if it is the same as the current tipset in
|
|
|
|
// the mpool, then this is just the pending messages
|
2021-12-11 21:03:00 +00:00
|
|
|
pending, err := mp.getPendingMessages(ctx, curTs, ts)
|
2020-08-11 09:33:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pending) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// defer only here so if we have no pending messages we don't spam
|
|
|
|
defer func() {
|
|
|
|
log.Infow("message selection done", "took", time.Since(start))
|
|
|
|
}()
|
|
|
|
|
|
|
|
// 0b. Select all priority messages that fit in the block
|
|
|
|
minGas := int64(gasguess.MinGas)
|
2021-09-11 00:24:12 +00:00
|
|
|
result := mp.selectPriorityMessages(ctx, pending, baseFee, ts)
|
2020-08-11 09:33:17 +00:00
|
|
|
|
|
|
|
// have we filled the block?
|
2021-09-11 00:24:12 +00:00
|
|
|
if result.gasLimit < minGas || len(result.msgs) >= build.BlockMessageLimit {
|
2020-08-11 09:33:17 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. Create a list of dependent message chains with maximal gas reward per limit consumed
|
|
|
|
startChains := time.Now()
|
|
|
|
var chains []*msgChain
|
|
|
|
for actor, mset := range pending {
|
|
|
|
next := mp.createMessageChains(actor, mset, baseFee, ts)
|
|
|
|
chains = append(chains, next...)
|
|
|
|
}
|
2020-08-11 17:00:51 +00:00
|
|
|
if dt := time.Since(startChains); dt > time.Millisecond {
|
|
|
|
log.Infow("create message chains done", "took", dt)
|
|
|
|
}
|
2020-08-11 09:33:17 +00:00
|
|
|
|
|
|
|
// 2. Sort the chains
|
|
|
|
sort.Slice(chains, func(i, j int) bool {
|
|
|
|
return chains[i].Before(chains[j])
|
|
|
|
})
|
|
|
|
|
2020-12-04 03:54:46 +00:00
|
|
|
if len(chains) != 0 && chains[0].gasPerf < 0 {
|
2020-08-11 09:33:17 +00:00
|
|
|
log.Warnw("all messages in mpool have non-positive gas performance", "bestGasPerf", chains[0].gasPerf)
|
2020-09-05 18:30:02 +00:00
|
|
|
return result, nil
|
2020-08-11 09:33:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 19:22:41 +00:00
|
|
|
// 3. Partition chains into blocks (without trimming)
|
2020-08-11 09:43:44 +00:00
|
|
|
// we use the full blockGasLimit (as opposed to the residual gas limit from the
|
2021-09-08 19:22:41 +00:00
|
|
|
// priority message selection) as we have to account for what other block providers are doing
|
2020-08-11 09:33:17 +00:00
|
|
|
nextChain := 0
|
|
|
|
partitions := make([][]*msgChain, MaxBlocks)
|
|
|
|
for i := 0; i < MaxBlocks && nextChain < len(chains); i++ {
|
2022-07-18 16:36:51 +00:00
|
|
|
gasLimit := build.BlockGasLimit
|
2021-09-08 19:22:41 +00:00
|
|
|
msgLimit := build.BlockMessageLimit
|
2020-08-11 09:33:17 +00:00
|
|
|
for nextChain < len(chains) {
|
|
|
|
chain := chains[nextChain]
|
|
|
|
nextChain++
|
2020-08-11 15:18:46 +00:00
|
|
|
partitions[i] = append(partitions[i], chain)
|
2020-08-11 09:33:17 +00:00
|
|
|
gasLimit -= chain.gasLimit
|
2021-09-08 19:22:41 +00:00
|
|
|
msgLimit -= len(chain.msgs)
|
|
|
|
if gasLimit < minGas || msgLimit <= 0 {
|
2020-08-11 09:33:17 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 15:18:46 +00:00
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 4. Compute effective performance for each chain, based on the partition they fall into
|
|
|
|
// The effective performance is the gasPerf of the chain * block probability
|
|
|
|
blockProb := mp.blockProbabilities(tq)
|
2020-08-11 09:50:10 +00:00
|
|
|
effChains := 0
|
2020-08-11 09:33:17 +00:00
|
|
|
for i := 0; i < MaxBlocks; i++ {
|
|
|
|
for _, chain := range partitions[i] {
|
|
|
|
chain.SetEffectivePerf(blockProb[i])
|
|
|
|
}
|
2020-08-11 09:50:10 +00:00
|
|
|
effChains += len(partitions[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// nullify the effective performance of chains that don't fit in any partition
|
|
|
|
for _, chain := range chains[effChains:] {
|
|
|
|
chain.SetNullEffectivePerf()
|
2020-08-11 09:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 5. Resort the chains based on effective performance
|
|
|
|
sort.Slice(chains, func(i, j int) bool {
|
|
|
|
return chains[i].BeforeEffective(chains[j])
|
|
|
|
})
|
|
|
|
|
|
|
|
// 6. Merge the head chains to produce the list of messages selected for inclusion
|
2021-09-08 19:22:41 +00:00
|
|
|
// subject to the residual block limits
|
2020-08-11 09:33:17 +00:00
|
|
|
// When a chain is merged in, all its previous dependent chains *must* also be
|
|
|
|
// merged in or we'll have a broken block
|
|
|
|
startMerge := time.Now()
|
|
|
|
last := len(chains)
|
|
|
|
for i, chain := range chains {
|
|
|
|
// did we run out of performing chains?
|
2020-12-04 03:54:46 +00:00
|
|
|
if chain.gasPerf < 0 {
|
2020-08-11 09:33:17 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// has it already been merged?
|
|
|
|
if chain.merged {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
if result.tryToAddWithDeps(chain, mp, baseFee) {
|
2021-09-08 19:22:41 +00:00
|
|
|
// adjust the effective performance for all subsequent chains
|
2020-08-13 07:33:20 +00:00
|
|
|
if next := chain.next; next != nil && next.effPerf > 0 {
|
2020-08-13 00:23:52 +00:00
|
|
|
next.effPerf += next.parentOffset
|
2020-08-13 16:49:07 +00:00
|
|
|
for next = next.next; next != nil && next.effPerf > 0; next = next.next {
|
|
|
|
next.setEffPerf()
|
|
|
|
}
|
2020-08-13 00:23:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
// re-sort to account for already merged chains and effective performance adjustments
|
2020-09-09 17:45:24 +00:00
|
|
|
// the sort *must* be stable or we end up getting negative gasPerfs pushed up.
|
|
|
|
sort.SliceStable(chains[i+1:], func(i, j int) bool {
|
2020-08-13 00:23:52 +00:00
|
|
|
return chains[i].BeforeEffective(chains[j])
|
|
|
|
})
|
2020-09-09 17:45:24 +00:00
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
// we can't fit this chain and its dependencies because of block limits -- we are
|
2020-08-11 09:33:17 +00:00
|
|
|
// at the edge
|
|
|
|
last = i
|
|
|
|
break
|
|
|
|
}
|
2020-08-11 17:00:51 +00:00
|
|
|
if dt := time.Since(startMerge); dt > time.Millisecond {
|
|
|
|
log.Infow("merge message chains done", "took", dt)
|
|
|
|
}
|
2020-08-11 09:33:17 +00:00
|
|
|
|
|
|
|
// 7. We have reached the edge of what can fit wholesale; if we still hae available
|
|
|
|
// gasLimit to pack some more chains, then trim the last chain and push it down.
|
2021-09-08 19:22:41 +00:00
|
|
|
// Trimming invalidates subsequent dependent chains so that they can't be selected
|
2020-08-11 09:33:17 +00:00
|
|
|
// as their dependency cannot be (fully) included.
|
|
|
|
// We do this in a loop because the blocker might have been inordinately large and
|
|
|
|
// we might have to do it multiple times to satisfy tail packing
|
|
|
|
startTail := time.Now()
|
|
|
|
tailLoop:
|
2021-09-11 00:24:12 +00:00
|
|
|
for result.gasLimit >= minGas && last < len(chains) {
|
|
|
|
|
|
|
|
if !chains[last].valid {
|
|
|
|
last++
|
|
|
|
continue tailLoop
|
2020-08-11 09:33:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
// trim if necessary
|
|
|
|
result.trimChain(chains[last], mp, baseFee)
|
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
// push down if it hasn't been invalidated
|
|
|
|
if chains[last].valid {
|
|
|
|
for i := last; i < len(chains)-1; i++ {
|
|
|
|
if chains[i].BeforeEffective(chains[i+1]) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
chains[i], chains[i+1] = chains[i+1], chains[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// select the next (valid and fitting) chain and its dependencies for inclusion
|
2021-09-11 00:24:12 +00:00
|
|
|
for _, chain := range chains[last:] {
|
2020-08-11 09:33:17 +00:00
|
|
|
// has the chain been invalidated?
|
|
|
|
if !chain.valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// has it already been merged?
|
|
|
|
if chain.merged {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// if gasPerf < 0 we have no more profitable chains
|
2020-12-04 03:54:46 +00:00
|
|
|
if chain.gasPerf < 0 {
|
2020-08-11 09:33:17 +00:00
|
|
|
break tailLoop
|
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
if result.tryToAddWithDeps(chain, mp, baseFee) {
|
2020-08-11 09:33:17 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
continue tailLoop
|
|
|
|
}
|
|
|
|
|
|
|
|
// the merge loop ended after processing all the chains and we we probably have still
|
|
|
|
// gas to spare; end the loop.
|
|
|
|
break
|
|
|
|
}
|
2020-08-11 17:00:51 +00:00
|
|
|
if dt := time.Since(startTail); dt > time.Millisecond {
|
|
|
|
log.Infow("pack tail chains done", "took", dt)
|
|
|
|
}
|
2020-08-11 09:33:17 +00:00
|
|
|
|
2021-09-08 19:22:41 +00:00
|
|
|
// if we have room to spare, pick some random (non-negative) chains to fill the block
|
|
|
|
// we pick randomly so that we minimize the probability of duplication among all block producers
|
2021-09-11 00:24:12 +00:00
|
|
|
if result.gasLimit >= minGas && len(result.msgs) <= build.BlockMessageLimit {
|
|
|
|
preRandomLength := len(result.msgs)
|
2020-09-09 20:17:09 +00:00
|
|
|
|
|
|
|
startRandom := time.Now()
|
2020-09-09 09:37:03 +00:00
|
|
|
shuffleChains(chains)
|
|
|
|
|
|
|
|
for _, chain := range chains {
|
|
|
|
// have we filled the block
|
2021-09-11 00:24:12 +00:00
|
|
|
if result.gasLimit < minGas || len(result.msgs) >= build.BlockMessageLimit {
|
2020-09-09 09:37:03 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// has it been merged or invalidated?
|
|
|
|
if chain.merged || !chain.valid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// is it negative?
|
2020-12-04 03:54:46 +00:00
|
|
|
if chain.gasPerf < 0 {
|
2020-09-09 09:37:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
if result.tryToAddWithDeps(chain, mp, baseFee) {
|
2020-09-09 09:37:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
if chain.valid {
|
|
|
|
// chain got trimmed on the previous call to tryToAddWithDeps, can now be included
|
|
|
|
result.tryToAddWithDeps(chain, mp, baseFee)
|
|
|
|
continue
|
2020-09-09 09:37:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-09 20:17:09 +00:00
|
|
|
|
|
|
|
if dt := time.Since(startRandom); dt > time.Millisecond {
|
|
|
|
log.Infow("pack random tail chains done", "took", dt)
|
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
if len(result.msgs) != preRandomLength {
|
2020-09-09 20:17:09 +00:00
|
|
|
log.Warnf("optimal selection failed to pack a block; picked %d messages with random selection",
|
2021-09-11 00:24:12 +00:00
|
|
|
len(result.msgs)-preRandomLength)
|
2020-09-09 20:17:09 +00:00
|
|
|
}
|
2020-09-09 09:37:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
return result, nil
|
2020-08-04 06:40:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
func (mp *MessagePool) selectMessagesGreedy(ctx context.Context, curTs, ts *types.TipSet) (*selectedMessages, error) {
|
2020-08-05 10:28:14 +00:00
|
|
|
start := time.Now()
|
|
|
|
|
2020-08-06 22:40:25 +00:00
|
|
|
baseFee, err := mp.api.ChainComputeBaseFee(context.TODO(), ts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("computing basefee: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-08-05 20:16:09 +00:00
|
|
|
// 0. Load messages for the target tipset; if it is the same as the current tipset in the mpool
|
|
|
|
// then this is just the pending messages
|
2021-12-11 21:03:00 +00:00
|
|
|
pending, err := mp.getPendingMessages(ctx, curTs, ts)
|
2020-08-05 20:16:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-07 07:33:00 +00:00
|
|
|
|
2020-08-06 22:54:58 +00:00
|
|
|
if len(pending) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// defer only here so if we have no pending messages we don't spam
|
|
|
|
defer func() {
|
2020-08-08 13:59:08 +00:00
|
|
|
log.Infow("message selection done", "took", time.Since(start))
|
2020-08-06 22:54:58 +00:00
|
|
|
}()
|
2020-08-05 20:16:09 +00:00
|
|
|
|
2020-08-07 15:24:36 +00:00
|
|
|
// 0b. Select all priority messages that fit in the block
|
|
|
|
minGas := int64(gasguess.MinGas)
|
2021-09-11 00:24:12 +00:00
|
|
|
result := mp.selectPriorityMessages(ctx, pending, baseFee, ts)
|
2020-08-07 15:24:36 +00:00
|
|
|
|
|
|
|
// have we filled the block?
|
2021-09-11 00:24:12 +00:00
|
|
|
if result.gasLimit < minGas || len(result.msgs) > build.BlockMessageLimit {
|
2020-08-07 15:24:36 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-08-04 06:40:02 +00:00
|
|
|
// 1. Create a list of dependent message chains with maximal gas reward per limit consumed
|
2020-08-08 14:07:37 +00:00
|
|
|
startChains := time.Now()
|
2020-08-04 06:40:02 +00:00
|
|
|
var chains []*msgChain
|
2020-08-05 20:16:09 +00:00
|
|
|
for actor, mset := range pending {
|
2020-08-06 22:40:25 +00:00
|
|
|
next := mp.createMessageChains(actor, mset, baseFee, ts)
|
2020-08-04 06:40:02 +00:00
|
|
|
chains = append(chains, next...)
|
|
|
|
}
|
2020-08-11 17:00:51 +00:00
|
|
|
if dt := time.Since(startChains); dt > time.Millisecond {
|
|
|
|
log.Infow("create message chains done", "took", dt)
|
|
|
|
}
|
2020-08-04 06:40:02 +00:00
|
|
|
|
|
|
|
// 2. Sort the chains
|
2020-08-06 15:37:28 +00:00
|
|
|
sort.Slice(chains, func(i, j int) bool {
|
2020-08-04 06:40:02 +00:00
|
|
|
return chains[i].Before(chains[j])
|
|
|
|
})
|
2020-08-07 07:33:00 +00:00
|
|
|
|
2020-12-04 03:54:46 +00:00
|
|
|
if len(chains) != 0 && chains[0].gasPerf < 0 {
|
2020-08-07 07:55:43 +00:00
|
|
|
log.Warnw("all messages in mpool have non-positive gas performance", "bestGasPerf", chains[0].gasPerf)
|
2020-09-05 18:30:02 +00:00
|
|
|
return result, nil
|
2020-08-06 22:40:25 +00:00
|
|
|
}
|
2020-08-04 06:40:02 +00:00
|
|
|
|
|
|
|
// 3. Merge the head chains to produce the list of messages selected for inclusion, subject to
|
2021-09-08 19:22:41 +00:00
|
|
|
// the block gas and message limits.
|
2020-08-08 13:51:30 +00:00
|
|
|
startMerge := time.Now()
|
2020-08-04 06:40:02 +00:00
|
|
|
last := len(chains)
|
|
|
|
for i, chain := range chains {
|
2020-08-11 09:33:17 +00:00
|
|
|
// did we run out of performing chains?
|
2020-12-04 03:54:46 +00:00
|
|
|
if chain.gasPerf < 0 {
|
2020-08-11 09:33:17 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-08-04 17:24:23 +00:00
|
|
|
// does it fit in the block?
|
2021-09-11 00:24:12 +00:00
|
|
|
if result.tryToAdd(chain) {
|
|
|
|
// there was room, we added the chain, keep going
|
2020-08-04 06:40:02 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:22:41 +00:00
|
|
|
// we can't fit this chain because of block limits -- we are at the edge
|
2020-08-04 06:40:02 +00:00
|
|
|
last = i
|
|
|
|
break
|
|
|
|
}
|
2020-08-11 17:00:51 +00:00
|
|
|
if dt := time.Since(startMerge); dt > time.Millisecond {
|
|
|
|
log.Infow("merge message chains done", "took", dt)
|
|
|
|
}
|
2020-08-04 06:40:02 +00:00
|
|
|
|
|
|
|
// 4. We have reached the edge of what we can fit wholesale; if we still have available gasLimit
|
|
|
|
// to pack some more chains, then trim the last chain and push it down.
|
|
|
|
// Trimming invalidates subsequent dependent chains so that they can't be selected as their
|
|
|
|
// dependency cannot be (fully) included.
|
|
|
|
// We do this in a loop because the blocker might have been inordinately large and we might
|
|
|
|
// have to do it multiple times to satisfy tail packing.
|
2020-08-08 13:51:30 +00:00
|
|
|
startTail := time.Now()
|
2020-08-04 06:40:02 +00:00
|
|
|
tailLoop:
|
2021-09-11 00:24:12 +00:00
|
|
|
for result.gasLimit >= minGas && last < len(chains) {
|
2020-08-04 06:40:02 +00:00
|
|
|
// trim
|
2021-09-11 00:24:12 +00:00
|
|
|
result.trimChain(chains[last], mp, baseFee)
|
2020-08-04 06:40:02 +00:00
|
|
|
|
2020-08-04 19:01:32 +00:00
|
|
|
// push down if it hasn't been invalidated
|
2020-08-05 06:46:48 +00:00
|
|
|
if chains[last].valid {
|
2020-08-04 17:24:23 +00:00
|
|
|
for i := last; i < len(chains)-1; i++ {
|
|
|
|
if chains[i].Before(chains[i+1]) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
chains[i], chains[i+1] = chains[i+1], chains[i]
|
2020-08-04 06:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// select the next (valid and fitting) chain for inclusion
|
|
|
|
for i, chain := range chains[last:] {
|
2020-08-11 09:33:17 +00:00
|
|
|
// has the chain been invalidated?
|
2020-08-04 06:40:02 +00:00
|
|
|
if !chain.valid {
|
|
|
|
continue
|
|
|
|
}
|
2020-08-07 07:33:00 +00:00
|
|
|
|
2020-08-07 12:43:32 +00:00
|
|
|
// if gasPerf < 0 we have no more profitable chains
|
2020-12-04 03:54:46 +00:00
|
|
|
if chain.gasPerf < 0 {
|
2020-08-07 07:33:00 +00:00
|
|
|
break tailLoop
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
// does it fit in the bock?
|
2021-09-11 00:24:12 +00:00
|
|
|
if result.tryToAdd(chain) {
|
|
|
|
// there was room, we added the chain, keep going
|
2020-08-11 09:33:17 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-04 06:40:02 +00:00
|
|
|
// this chain needs to be trimmed
|
2020-08-06 23:01:45 +00:00
|
|
|
last += i
|
2020-08-04 06:40:02 +00:00
|
|
|
continue tailLoop
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
// the merge loop ended after processing all the chains and we probably still have
|
|
|
|
// gas to spare; end the loop
|
|
|
|
break
|
2020-08-04 06:40:02 +00:00
|
|
|
}
|
2020-08-11 17:00:51 +00:00
|
|
|
if dt := time.Since(startTail); dt > time.Millisecond {
|
|
|
|
log.Infow("pack tail chains done", "took", dt)
|
|
|
|
}
|
2020-08-04 06:40:02 +00:00
|
|
|
|
2020-08-05 20:16:09 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
func (mp *MessagePool) selectPriorityMessages(ctx context.Context, pending map[address.Address]map[uint64]*types.SignedMessage, baseFee types.BigInt, ts *types.TipSet) *selectedMessages {
|
2020-08-08 13:51:30 +00:00
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
2020-08-11 17:00:51 +00:00
|
|
|
if dt := time.Since(start); dt > time.Millisecond {
|
|
|
|
log.Infow("select priority messages done", "took", dt)
|
|
|
|
}
|
2020-08-08 13:51:30 +00:00
|
|
|
}()
|
2021-01-14 02:55:49 +00:00
|
|
|
mpCfg := mp.getConfig()
|
2021-09-11 00:24:12 +00:00
|
|
|
result := &selectedMessages{
|
|
|
|
msgs: make([]*types.SignedMessage, 0, mpCfg.SizeLimitLow),
|
2022-07-18 16:36:51 +00:00
|
|
|
gasLimit: build.BlockGasLimit,
|
2021-09-11 00:24:12 +00:00
|
|
|
blsLimit: cbg.MaxLength,
|
|
|
|
secpLimit: cbg.MaxLength,
|
|
|
|
}
|
2020-08-07 15:24:36 +00:00
|
|
|
minGas := int64(gasguess.MinGas)
|
|
|
|
|
|
|
|
// 1. Get priority actor chains
|
|
|
|
var chains []*msgChain
|
2021-01-14 02:55:49 +00:00
|
|
|
priority := mpCfg.PriorityAddrs
|
2020-08-07 15:24:36 +00:00
|
|
|
for _, actor := range priority {
|
2021-05-31 22:12:42 +00:00
|
|
|
pk, err := mp.resolveToKey(ctx, actor)
|
2021-05-07 03:51:42 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debugf("mpooladdlocal failed to resolve sender: %s", err)
|
2021-09-11 00:24:12 +00:00
|
|
|
return result
|
2021-05-07 03:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mset, ok := pending[pk]
|
2020-08-07 15:24:36 +00:00
|
|
|
if ok {
|
|
|
|
// remove actor from pending set as we are already processed these messages
|
2021-05-07 03:51:42 +00:00
|
|
|
delete(pending, pk)
|
2020-08-07 15:24:36 +00:00
|
|
|
// create chains for the priority actor
|
|
|
|
next := mp.createMessageChains(actor, mset, baseFee, ts)
|
|
|
|
chains = append(chains, next...)
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 12:55:19 +00:00
|
|
|
if len(chains) == 0 {
|
2021-09-11 00:24:12 +00:00
|
|
|
return result
|
2020-08-29 12:55:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 15:24:36 +00:00
|
|
|
// 2. Sort the chains
|
|
|
|
sort.Slice(chains, func(i, j int) bool {
|
|
|
|
return chains[i].Before(chains[j])
|
|
|
|
})
|
|
|
|
|
2020-12-04 03:54:46 +00:00
|
|
|
if len(chains) != 0 && chains[0].gasPerf < 0 {
|
2020-08-29 12:55:19 +00:00
|
|
|
log.Warnw("all priority messages in mpool have negative gas performance", "bestGasPerf", chains[0].gasPerf)
|
2021-09-11 00:24:12 +00:00
|
|
|
return result
|
2020-08-29 12:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Merge chains until the block limit, as long as they have non-negative gas performance
|
2020-08-07 15:24:36 +00:00
|
|
|
last := len(chains)
|
|
|
|
for i, chain := range chains {
|
2020-12-04 03:54:46 +00:00
|
|
|
if chain.gasPerf < 0 {
|
2020-08-29 12:55:19 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
if result.tryToAdd(chain) {
|
|
|
|
// there was room, we added the chain, keep going
|
2020-08-07 15:24:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can't fit this chain because of block gasLimit -- we are at the edge
|
|
|
|
last = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
tailLoop:
|
2021-09-11 00:24:12 +00:00
|
|
|
for result.gasLimit >= minGas && last < len(chains) {
|
2020-08-29 12:55:19 +00:00
|
|
|
// trim, discarding negative performing messages
|
2021-09-11 00:24:12 +00:00
|
|
|
|
|
|
|
result.trimChain(chains[last], mp, baseFee)
|
2020-08-07 15:24:36 +00:00
|
|
|
|
|
|
|
// push down if it hasn't been invalidated
|
|
|
|
if chains[last].valid {
|
|
|
|
for i := last; i < len(chains)-1; i++ {
|
|
|
|
if chains[i].Before(chains[i+1]) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
chains[i], chains[i+1] = chains[i+1], chains[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// select the next (valid and fitting) chain for inclusion
|
|
|
|
for i, chain := range chains[last:] {
|
|
|
|
// has the chain been invalidated
|
|
|
|
if !chain.valid {
|
|
|
|
continue
|
|
|
|
}
|
2020-08-29 12:55:19 +00:00
|
|
|
|
|
|
|
// if gasPerf < 0 we have no more profitable chains
|
2020-12-04 03:54:46 +00:00
|
|
|
if chain.gasPerf < 0 {
|
2020-08-29 12:55:19 +00:00
|
|
|
break tailLoop
|
|
|
|
}
|
|
|
|
|
2020-08-07 15:24:36 +00:00
|
|
|
// does it fit in the bock?
|
2021-09-11 00:24:12 +00:00
|
|
|
if result.tryToAdd(chain) {
|
|
|
|
// there was room, we added the chain, keep going
|
2020-08-07 15:24:36 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// this chain needs to be trimmed
|
|
|
|
last += i
|
|
|
|
continue tailLoop
|
|
|
|
}
|
|
|
|
|
2020-08-29 12:55:19 +00:00
|
|
|
// the merge loop ended after processing all the chains and we probably still have gas to spare;
|
|
|
|
// end the loop
|
|
|
|
break
|
2020-08-07 15:24:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
return result
|
2020-08-07 15:24:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
func (mp *MessagePool) getPendingMessages(ctx context.Context, curTs, ts *types.TipSet) (map[address.Address]map[uint64]*types.SignedMessage, error) {
|
2020-08-08 13:51:30 +00:00
|
|
|
start := time.Now()
|
|
|
|
|
2020-08-05 20:16:09 +00:00
|
|
|
result := make(map[address.Address]map[uint64]*types.SignedMessage)
|
2020-08-08 20:51:24 +00:00
|
|
|
defer func() {
|
2020-08-11 17:00:51 +00:00
|
|
|
if dt := time.Since(start); dt > time.Millisecond {
|
|
|
|
log.Infow("get pending messages done", "took", dt)
|
2020-08-08 20:51:24 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-08-05 20:16:09 +00:00
|
|
|
|
|
|
|
// are we in sync?
|
|
|
|
inSync := false
|
|
|
|
if curTs.Height() == ts.Height() && curTs.Equals(ts) {
|
|
|
|
inSync = true
|
|
|
|
}
|
|
|
|
|
2021-05-29 00:35:50 +00:00
|
|
|
mp.forEachPending(func(a address.Address, mset *msgSet) {
|
2020-08-05 20:16:09 +00:00
|
|
|
if inSync {
|
|
|
|
// no need to copy the map
|
|
|
|
result[a] = mset.msgs
|
|
|
|
} else {
|
|
|
|
// we need to copy the map to avoid clobbering it as we load more messages
|
|
|
|
msetCopy := make(map[uint64]*types.SignedMessage, len(mset.msgs))
|
|
|
|
for nonce, m := range mset.msgs {
|
|
|
|
msetCopy[nonce] = m
|
|
|
|
}
|
|
|
|
result[a] = msetCopy
|
|
|
|
|
|
|
|
}
|
2021-05-29 00:35:50 +00:00
|
|
|
})
|
2020-08-05 20:16:09 +00:00
|
|
|
|
|
|
|
// we are in sync, that's the happy path
|
|
|
|
if inSync {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
if err := mp.runHeadChange(ctx, curTs, ts, result); err != nil {
|
2020-08-25 00:21:03 +00:00
|
|
|
return nil, xerrors.Errorf("failed to process difference between mpool head and given head: %w", err)
|
2020-08-05 20:16:09 +00:00
|
|
|
}
|
2020-08-25 00:21:03 +00:00
|
|
|
|
|
|
|
return result, nil
|
2020-08-04 06:40:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 17:53:49 +00:00
|
|
|
func (*MessagePool) getGasReward(msg *types.SignedMessage, baseFee types.BigInt) *big.Int {
|
2020-08-11 15:18:46 +00:00
|
|
|
maxPremium := types.BigSub(msg.Message.GasFeeCap, baseFee)
|
2020-09-03 17:36:03 +00:00
|
|
|
|
|
|
|
if types.BigCmp(maxPremium, msg.Message.GasPremium) > 0 {
|
2020-08-11 15:18:46 +00:00
|
|
|
maxPremium = msg.Message.GasPremium
|
2020-08-06 22:40:25 +00:00
|
|
|
}
|
2020-09-03 17:36:03 +00:00
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
gasReward := tbig.Mul(maxPremium, types.NewInt(uint64(msg.Message.GasLimit)))
|
2020-12-04 03:54:46 +00:00
|
|
|
if gasReward.Sign() == -1 {
|
|
|
|
// penalty multiplier
|
|
|
|
gasReward = tbig.Mul(gasReward, types.NewInt(3))
|
|
|
|
}
|
2020-08-05 06:46:48 +00:00
|
|
|
return gasReward.Int
|
|
|
|
}
|
|
|
|
|
2020-09-03 17:53:49 +00:00
|
|
|
func (*MessagePool) getGasPerf(gasReward *big.Int, gasLimit int64) float64 {
|
2020-08-05 06:46:48 +00:00
|
|
|
// gasPerf = gasReward * build.BlockGasLimit / gasLimit
|
2020-08-05 07:22:40 +00:00
|
|
|
a := new(big.Rat).SetInt(new(big.Int).Mul(gasReward, bigBlockGasLimit))
|
2020-08-05 06:46:48 +00:00
|
|
|
b := big.NewRat(1, gasLimit)
|
|
|
|
c := new(big.Rat).Mul(a, b)
|
|
|
|
r, _ := c.Float64()
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-08-06 22:40:25 +00:00
|
|
|
func (mp *MessagePool) createMessageChains(actor address.Address, mset map[uint64]*types.SignedMessage, baseFee types.BigInt, ts *types.TipSet) []*msgChain {
|
2020-08-04 19:01:32 +00:00
|
|
|
// collect all messages
|
2020-08-05 20:16:09 +00:00
|
|
|
msgs := make([]*types.SignedMessage, 0, len(mset))
|
|
|
|
for _, m := range mset {
|
2020-08-04 19:01:32 +00:00
|
|
|
msgs = append(msgs, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort by nonce
|
2020-08-05 06:46:48 +00:00
|
|
|
sort.Slice(msgs, func(i, j int) bool {
|
|
|
|
return msgs[i].Message.Nonce < msgs[j].Message.Nonce
|
2020-08-04 19:01:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// sanity checks:
|
|
|
|
// - there can be no gaps in nonces, starting from the current actor nonce
|
|
|
|
// if there is a gap, drop messages after the gap, we can't include them
|
|
|
|
// - all messages must have minimum gas and the total gas for the candidate messages
|
|
|
|
// cannot exceed the block limit; drop all messages that exceed the limit
|
|
|
|
// - the total gasReward cannot exceed the actor's balance; drop all messages that exceed
|
|
|
|
// the balance
|
2020-08-25 02:02:06 +00:00
|
|
|
a, err := mp.api.GetActorAfter(actor, ts)
|
2020-08-25 00:21:03 +00:00
|
|
|
if err != nil {
|
2021-02-11 11:00:26 +00:00
|
|
|
log.Errorf("failed to load actor state, not building chain for %s: %v", actor, err)
|
2020-08-25 00:40:12 +00:00
|
|
|
return nil
|
2020-08-25 00:21:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 19:01:32 +00:00
|
|
|
curNonce := a.Nonce
|
2020-08-05 06:46:48 +00:00
|
|
|
balance := a.Balance.Int
|
|
|
|
gasLimit := int64(0)
|
2020-08-07 23:16:56 +00:00
|
|
|
skip := 0
|
2020-08-04 19:01:32 +00:00
|
|
|
i := 0
|
2020-08-05 06:46:48 +00:00
|
|
|
rewards := make([]*big.Int, 0, len(msgs))
|
2020-08-04 19:01:32 +00:00
|
|
|
for i = 0; i < len(msgs); i++ {
|
|
|
|
m := msgs[i]
|
2020-08-05 20:16:09 +00:00
|
|
|
|
|
|
|
if m.Message.Nonce < curNonce {
|
|
|
|
log.Warnf("encountered message from actor %s with nonce (%d) less than the current nonce (%d)",
|
|
|
|
actor, m.Message.Nonce, curNonce)
|
2020-08-07 23:16:56 +00:00
|
|
|
skip++
|
2020-08-05 20:16:09 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-05 06:46:48 +00:00
|
|
|
if m.Message.Nonce != curNonce {
|
2020-08-04 19:01:32 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
curNonce++
|
|
|
|
|
2022-05-30 19:19:51 +00:00
|
|
|
minGas := vm.PricelistByEpoch(ts.Height()).OnChainMessage(m.ChainLength()).Total()
|
2020-08-05 06:46:48 +00:00
|
|
|
if m.Message.GasLimit < minGas {
|
2020-08-04 19:01:32 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-08-05 06:46:48 +00:00
|
|
|
gasLimit += m.Message.GasLimit
|
2020-08-04 19:01:32 +00:00
|
|
|
if gasLimit > build.BlockGasLimit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-08-05 10:08:07 +00:00
|
|
|
required := m.Message.RequiredFunds().Int
|
|
|
|
if balance.Cmp(required) < 0 {
|
2020-08-04 19:01:32 +00:00
|
|
|
break
|
|
|
|
}
|
2021-04-23 12:29:46 +00:00
|
|
|
|
2020-08-05 10:08:07 +00:00
|
|
|
balance = new(big.Int).Sub(balance, required)
|
2020-10-30 18:56:33 +00:00
|
|
|
|
|
|
|
value := m.Message.Value.Int
|
|
|
|
balance = new(big.Int).Sub(balance, value)
|
2020-08-06 17:49:31 +00:00
|
|
|
|
2020-09-03 17:50:35 +00:00
|
|
|
gasReward := mp.getGasReward(m, baseFee)
|
2020-08-04 19:01:32 +00:00
|
|
|
rewards = append(rewards, gasReward)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check we have a sane set of messages to construct the chains
|
2020-08-11 16:32:28 +00:00
|
|
|
if i > skip {
|
2020-08-07 23:16:56 +00:00
|
|
|
msgs = msgs[skip:i]
|
2020-08-04 19:01:32 +00:00
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:22:41 +00:00
|
|
|
// if we have more messages from this sender than can fit in a block, drop the extra ones
|
|
|
|
if len(msgs) > build.BlockMessageLimit {
|
|
|
|
msgs = msgs[:build.BlockMessageLimit]
|
|
|
|
}
|
|
|
|
|
2020-08-04 19:01:32 +00:00
|
|
|
// ok, now we can construct the chains using the messages we have
|
|
|
|
// invariant: each chain has a bigger gasPerf than the next -- otherwise they can be merged
|
|
|
|
// and increase the gasPerf of the first chain
|
|
|
|
// We do this in two passes:
|
2021-09-08 19:22:41 +00:00
|
|
|
// - in the first pass we create chains that aggregate messages with non-decreasing gasPerf
|
2020-08-04 19:01:32 +00:00
|
|
|
// - in the second pass we merge chains to maintain the invariant.
|
|
|
|
var chains []*msgChain
|
|
|
|
var curChain *msgChain
|
|
|
|
|
|
|
|
newChain := func(m *types.SignedMessage, i int) *msgChain {
|
|
|
|
chain := new(msgChain)
|
|
|
|
chain.msgs = []*types.SignedMessage{m}
|
|
|
|
chain.gasReward = rewards[i]
|
2020-08-05 06:46:48 +00:00
|
|
|
chain.gasLimit = m.Message.GasLimit
|
2020-08-05 15:28:04 +00:00
|
|
|
chain.gasPerf = mp.getGasPerf(chain.gasReward, chain.gasLimit)
|
2020-08-04 19:01:32 +00:00
|
|
|
chain.valid = true
|
2021-09-11 00:24:12 +00:00
|
|
|
chain.sigType = m.Signature.Type
|
2020-08-04 19:01:32 +00:00
|
|
|
return chain
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the individual chains
|
|
|
|
for i, m := range msgs {
|
|
|
|
if curChain == nil {
|
|
|
|
curChain = newChain(m, i)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-05 06:46:48 +00:00
|
|
|
gasReward := new(big.Int).Add(curChain.gasReward, rewards[i])
|
|
|
|
gasLimit := curChain.gasLimit + m.Message.GasLimit
|
|
|
|
gasPerf := mp.getGasPerf(gasReward, gasLimit)
|
2020-08-04 19:01:32 +00:00
|
|
|
|
2021-09-08 19:22:41 +00:00
|
|
|
// try to add the message to the current chain -- if it decreases the gasPerf, or then make a
|
2020-08-04 19:01:32 +00:00
|
|
|
// new chain
|
|
|
|
if gasPerf < curChain.gasPerf {
|
|
|
|
chains = append(chains, curChain)
|
|
|
|
curChain = newChain(m, i)
|
|
|
|
} else {
|
|
|
|
curChain.msgs = append(curChain.msgs, m)
|
|
|
|
curChain.gasReward = gasReward
|
|
|
|
curChain.gasLimit = gasLimit
|
|
|
|
curChain.gasPerf = gasPerf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chains = append(chains, curChain)
|
|
|
|
|
|
|
|
// merge chains to maintain the invariant
|
|
|
|
for {
|
|
|
|
merged := 0
|
|
|
|
|
|
|
|
for i := len(chains) - 1; i > 0; i-- {
|
2020-08-06 15:37:28 +00:00
|
|
|
if chains[i].gasPerf >= chains[i-1].gasPerf {
|
2020-08-04 19:01:32 +00:00
|
|
|
chains[i-1].msgs = append(chains[i-1].msgs, chains[i].msgs...)
|
2020-08-05 06:46:48 +00:00
|
|
|
chains[i-1].gasReward = new(big.Int).Add(chains[i-1].gasReward, chains[i].gasReward)
|
2020-08-04 19:01:32 +00:00
|
|
|
chains[i-1].gasLimit += chains[i].gasLimit
|
2020-08-05 06:46:48 +00:00
|
|
|
chains[i-1].gasPerf = mp.getGasPerf(chains[i-1].gasReward, chains[i-1].gasLimit)
|
2020-08-04 19:01:32 +00:00
|
|
|
chains[i].valid = false
|
|
|
|
merged++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if merged == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// drop invalidated chains
|
2020-08-05 06:46:48 +00:00
|
|
|
newChains := make([]*msgChain, 0, len(chains)-merged)
|
2020-08-04 19:01:32 +00:00
|
|
|
for _, c := range chains {
|
|
|
|
if c.valid {
|
|
|
|
newChains = append(newChains, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chains = newChains
|
|
|
|
}
|
|
|
|
|
|
|
|
// link dependent chains
|
|
|
|
for i := 0; i < len(chains)-1; i++ {
|
|
|
|
chains[i].next = chains[i+1]
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
for i := len(chains) - 1; i > 0; i-- {
|
|
|
|
chains[i].prev = chains[i-1]
|
|
|
|
}
|
|
|
|
|
2020-08-04 19:01:32 +00:00
|
|
|
return chains
|
2020-08-04 06:40:02 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 07:34:47 +00:00
|
|
|
func (mc *msgChain) Before(other *msgChain) bool {
|
|
|
|
return mc.gasPerf > other.gasPerf ||
|
|
|
|
(mc.gasPerf == other.gasPerf && mc.gasReward.Cmp(other.gasReward) > 0)
|
2020-08-04 06:40:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 19:22:41 +00:00
|
|
|
func (mc *msgChain) Trim(gasLimit int64, msgLimit int, mp *MessagePool, baseFee types.BigInt) {
|
2020-08-04 17:24:23 +00:00
|
|
|
i := len(mc.msgs) - 1
|
2021-09-08 19:22:41 +00:00
|
|
|
for i >= 0 && (mc.gasLimit > gasLimit || mc.gasPerf < 0 || i >= msgLimit) {
|
2020-09-03 17:50:35 +00:00
|
|
|
gasReward := mp.getGasReward(mc.msgs[i], baseFee)
|
2020-08-05 06:46:48 +00:00
|
|
|
mc.gasReward = new(big.Int).Sub(mc.gasReward, gasReward)
|
|
|
|
mc.gasLimit -= mc.msgs[i].Message.GasLimit
|
2020-08-04 17:24:23 +00:00
|
|
|
if mc.gasLimit > 0 {
|
2020-08-05 06:46:48 +00:00
|
|
|
mc.gasPerf = mp.getGasPerf(mc.gasReward, mc.gasLimit)
|
2020-08-12 16:50:35 +00:00
|
|
|
if mc.bp != 0 {
|
|
|
|
mc.setEffPerf()
|
2020-08-11 21:55:06 +00:00
|
|
|
}
|
2020-08-04 17:24:23 +00:00
|
|
|
} else {
|
|
|
|
mc.gasPerf = 0
|
2020-08-12 16:50:35 +00:00
|
|
|
mc.effPerf = 0
|
2020-08-04 17:24:23 +00:00
|
|
|
}
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
|
|
|
if i < 0 {
|
|
|
|
mc.msgs = nil
|
|
|
|
mc.valid = false
|
|
|
|
} else {
|
2020-08-05 07:31:42 +00:00
|
|
|
mc.msgs = mc.msgs[:i+1]
|
2020-08-04 17:24:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 00:24:12 +00:00
|
|
|
// TODO: if the trim above is a no-op, this (may) needlessly invalidates the next chain
|
2020-08-04 17:24:23 +00:00
|
|
|
if mc.next != nil {
|
2020-08-11 09:33:17 +00:00
|
|
|
mc.next.Invalidate()
|
2020-08-04 17:24:23 +00:00
|
|
|
mc.next = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
func (mc *msgChain) Invalidate() {
|
2020-08-04 17:24:23 +00:00
|
|
|
mc.valid = false
|
|
|
|
mc.msgs = nil
|
|
|
|
if mc.next != nil {
|
2020-08-11 09:33:17 +00:00
|
|
|
mc.next.Invalidate()
|
2020-08-04 17:24:23 +00:00
|
|
|
mc.next = nil
|
|
|
|
}
|
2020-08-04 06:40:02 +00:00
|
|
|
}
|
2020-08-11 09:33:17 +00:00
|
|
|
|
|
|
|
func (mc *msgChain) SetEffectivePerf(bp float64) {
|
2020-08-12 16:50:35 +00:00
|
|
|
mc.bp = bp
|
|
|
|
mc.setEffPerf()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *msgChain) setEffPerf() {
|
|
|
|
effPerf := mc.gasPerf * mc.bp
|
2020-08-13 07:28:56 +00:00
|
|
|
if effPerf > 0 && mc.prev != nil {
|
2020-08-13 00:23:52 +00:00
|
|
|
effPerfWithParent := (effPerf*float64(mc.gasLimit) + mc.prev.effPerf*float64(mc.prev.gasLimit)) / float64(mc.gasLimit+mc.prev.gasLimit)
|
|
|
|
mc.parentOffset = effPerf - effPerfWithParent
|
|
|
|
effPerf = effPerfWithParent
|
2020-08-12 16:50:35 +00:00
|
|
|
}
|
|
|
|
mc.effPerf = effPerf
|
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 09:50:10 +00:00
|
|
|
func (mc *msgChain) SetNullEffectivePerf() {
|
|
|
|
if mc.gasPerf < 0 {
|
|
|
|
mc.effPerf = mc.gasPerf
|
|
|
|
} else {
|
|
|
|
mc.effPerf = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 09:33:17 +00:00
|
|
|
func (mc *msgChain) BeforeEffective(other *msgChain) bool {
|
2020-08-13 07:28:56 +00:00
|
|
|
// move merged chains to the front so we can discard them earlier
|
2020-09-09 17:45:24 +00:00
|
|
|
return (mc.merged && !other.merged) ||
|
|
|
|
(mc.gasPerf >= 0 && other.gasPerf < 0) ||
|
|
|
|
mc.effPerf > other.effPerf ||
|
2020-08-11 09:33:17 +00:00
|
|
|
(mc.effPerf == other.effPerf && mc.gasPerf > other.gasPerf) ||
|
|
|
|
(mc.effPerf == other.effPerf && mc.gasPerf == other.gasPerf && mc.gasReward.Cmp(other.gasReward) > 0)
|
|
|
|
}
|
2020-09-09 09:37:03 +00:00
|
|
|
|
|
|
|
func shuffleChains(lst []*msgChain) {
|
|
|
|
for i := range lst {
|
|
|
|
j := rand.Intn(i + 1)
|
|
|
|
lst[i], lst[j] = lst[j], lst[i]
|
|
|
|
}
|
|
|
|
}
|