lotus/chain/messagepool/selection.go

577 lines
16 KiB
Go
Raw Normal View History

2020-08-04 06:40:02 +00:00
package messagepool
import (
"context"
2020-08-05 06:46:48 +00:00
"math/big"
"sort"
2020-08-04 06:40:02 +00:00
"time"
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-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-05 06:46:48 +00:00
abig "github.com/filecoin-project/specs-actors/actors/abi/big"
"github.com/ipfs/go-cid"
2020-08-04 06:40:02 +00:00
)
var bigBlockGasLimit = big.NewInt(build.BlockGasLimit)
2020-08-04 06:40:02 +00:00
type msgChain struct {
msgs []*types.SignedMessage
2020-08-05 06:46:48 +00:00
gasReward *big.Int
gasLimit int64
2020-08-04 06:40:02 +00:00
gasPerf float64
valid bool
next *msgChain
}
func (mp *MessagePool) SelectMessages(ts *types.TipSet) ([]*types.SignedMessage, error) {
2020-08-04 06:40:02 +00:00
mp.curTsLk.Lock()
defer mp.curTsLk.Unlock()
2020-08-04 06:40:02 +00:00
mp.lk.Lock()
defer mp.lk.Unlock()
return mp.selectMessages(mp.curTs, ts)
2020-08-04 06:40:02 +00:00
}
func (mp *MessagePool) selectMessages(curTs, ts *types.TipSet) ([]*types.SignedMessage, error) {
start := time.Now()
baseFee, err := mp.api.ChainComputeBaseFee(context.TODO(), ts)
if err != nil {
return nil, xerrors.Errorf("computing basefee: %w", err)
}
// 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
pending, err := mp.getPendingMessages(curTs, ts)
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() {
2020-08-08 13:59:08 +00:00
log.Infow("message selection done", "took", time.Since(start))
}()
2020-08-07 15:24:36 +00:00
// 0b. Select all priority messages that fit in the block
minGas := int64(gasguess.MinGas)
result, gasLimit := mp.selectPriorityMessages(pending, baseFee, ts)
// have we filled the block?
if gasLimit < minGas {
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
for actor, mset := range pending {
next := mp.createMessageChains(actor, mset, baseFee, ts)
2020-08-04 06:40:02 +00:00
chains = append(chains, next...)
}
2020-08-08 14:07:37 +00:00
log.Infow("create message chains done", "took", time.Since(startChains))
2020-08-04 06:40:02 +00:00
// 2. Sort the chains
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 12:43:32 +00:00
if len(chains) != 0 && chains[0].gasPerf < 0 {
log.Warnw("all messages in mpool have non-positive gas performance", "bestGasPerf", chains[0].gasPerf)
return nil, nil
}
2020-08-04 06:40:02 +00:00
// 3. Merge the head chains to produce the list of messages selected for inclusion, subject to
// the block gas limit.
startMerge := time.Now()
2020-08-04 06:40:02 +00:00
last := len(chains)
for i, chain := range chains {
2020-08-04 17:24:23 +00:00
// does it fit in the block?
2020-08-07 12:43:32 +00:00
if chain.gasLimit <= gasLimit && chain.gasPerf >= 0 {
2020-08-04 06:40:02 +00:00
gasLimit -= chain.gasLimit
result = append(result, chain.msgs...)
continue
}
// did we run out of performing chains?
2020-08-07 12:43:32 +00:00
if chain.gasPerf < 0 {
break
}
2020-08-04 06:40:02 +00:00
// we can't fit this chain because of block gasLimit -- we are at the edge
last = i
break
}
2020-08-08 13:59:08 +00:00
log.Infow("merge message chains done", "took", time.Since(startMerge))
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.
startTail := time.Now()
2020-08-04 06:40:02 +00:00
tailLoop:
for gasLimit >= minGas && last < len(chains) {
// trim
2020-08-07 15:24:36 +00:00
chains[last].Trim(gasLimit, mp, baseFee, ts, false)
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:] {
// has the chain been invalidated
if !chain.valid {
continue
}
// does it fit in the bock?
2020-08-07 12:43:32 +00:00
if chain.gasLimit <= gasLimit && chain.gasPerf >= 0 {
2020-08-04 06:40:02 +00:00
gasLimit -= chain.gasLimit
result = append(result, chain.msgs...)
continue
}
2020-08-07 12:43:32 +00:00
// if gasPerf < 0 we have no more profitable chains
if chain.gasPerf < 0 {
break tailLoop
}
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-04 19:01:32 +00:00
// the merge loop ended after processing all the chains and we probably still have gas to spare
// -- mark the end.
2020-08-04 06:40:02 +00:00
last = len(chains)
}
2020-08-08 13:59:08 +00:00
log.Infow("pack tail chains done", "took", time.Since(startTail))
2020-08-04 06:40:02 +00:00
return result, nil
}
2020-08-07 15:24:36 +00:00
func (mp *MessagePool) selectPriorityMessages(pending map[address.Address]map[uint64]*types.SignedMessage, baseFee types.BigInt, ts *types.TipSet) ([]*types.SignedMessage, int64) {
start := time.Now()
defer func() {
2020-08-08 13:59:08 +00:00
log.Infow("select priority messages done", "took", time.Since(start))
}()
2020-08-07 15:24:36 +00:00
result := make([]*types.SignedMessage, 0, mp.cfg.SizeLimitLow)
gasLimit := int64(build.BlockGasLimit)
minGas := int64(gasguess.MinGas)
// 1. Get priority actor chains
var chains []*msgChain
priority := mp.cfg.PriorityAddrs
for _, actor := range priority {
mset, ok := pending[actor]
if ok {
// remove actor from pending set as we are already processed these messages
delete(pending, actor)
// create chains for the priority actor
next := mp.createMessageChains(actor, mset, baseFee, ts)
chains = append(chains, next...)
}
}
// 2. Sort the chains
sort.Slice(chains, func(i, j int) bool {
return chains[i].Before(chains[j])
})
// 3. Merge chains until the block limit; we are willing to include negative performing chains
// as these are messages from our own miners
last := len(chains)
for i, chain := range chains {
if chain.gasLimit <= gasLimit {
gasLimit -= chain.gasLimit
result = append(result, chain.msgs...)
continue
}
// we can't fit this chain because of block gasLimit -- we are at the edge
last = i
break
}
tailLoop:
for gasLimit >= minGas && last < len(chains) {
// trim, without discarding negative performing messages
chains[last].Trim(gasLimit, mp, baseFee, ts, true)
// 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
}
// does it fit in the bock?
if chain.gasLimit <= gasLimit {
gasLimit -= chain.gasLimit
result = append(result, chain.msgs...)
continue
}
// this chain needs to be trimmed
last += i
continue tailLoop
}
// the merge loop ended after processing all the chains and we probably still have gas to spare
// -- mark the end.
last = len(chains)
}
return result, gasLimit
}
func (mp *MessagePool) getPendingMessages(curTs, ts *types.TipSet) (map[address.Address]map[uint64]*types.SignedMessage, error) {
start := time.Now()
defer func() {
2020-08-08 13:59:08 +00:00
log.Infow("get pending messages done", "took", time.Since(start))
}()
result := make(map[address.Address]map[uint64]*types.SignedMessage)
haveCids := make(map[cid.Cid]struct{})
// are we in sync?
inSync := false
if curTs.Height() == ts.Height() && curTs.Equals(ts) {
inSync = true
}
// first add our current pending messages
for a, mset := range mp.pending {
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
// mark the messages as seen
for _, m := range mset.msgs {
haveCids[m.Cid()] = struct{}{}
}
}
}
// we are in sync, that's the happy path
if inSync {
return result, nil
}
// nope, we need to sync the tipsets
for {
if curTs.Height() == ts.Height() {
if curTs.Equals(ts) {
return result, nil
}
// different blocks in tipsets -- we mark them as seen so that they are not included in
// in the message set we return, but *neither me (vyzo) nor why understand why*
// this code is also probably completely untested in production, so I am adding a big fat
// warning to revisit this case and sanity check this decision.
log.Warnf("mpool tipset has same height as target tipset but it's not equal; beware of dragons!")
have, err := mp.MessagesForBlocks(ts.Blocks())
if err != nil {
return nil, xerrors.Errorf("error retrieving messages for tipset: %w", err)
}
for _, m := range have {
haveCids[m.Cid()] = struct{}{}
}
}
msgs, err := mp.MessagesForBlocks(ts.Blocks())
if err != nil {
return nil, xerrors.Errorf("error retrieving messages for tipset: %w", err)
}
for _, m := range msgs {
if _, have := haveCids[m.Cid()]; have {
continue
}
haveCids[m.Cid()] = struct{}{}
mset, ok := result[m.Message.From]
if !ok {
mset = make(map[uint64]*types.SignedMessage)
result[m.Message.From] = mset
}
other, dupNonce := mset[m.Message.Nonce]
if dupNonce {
// duplicate nonce, selfishly keep the message with the highest GasPrice
// if the gas prices are the same, keep the one with the highest GasLimit
switch m.Message.GasPremium.Int.Cmp(other.Message.GasPremium.Int) {
case 0:
if m.Message.GasLimit > other.Message.GasLimit {
mset[m.Message.Nonce] = m
}
case 1:
mset[m.Message.Nonce] = m
}
} else {
mset[m.Message.Nonce] = m
}
}
if curTs.Height() >= ts.Height() {
return result, nil
}
ts, err = mp.api.LoadTipSet(ts.Parents())
if err != nil {
return nil, xerrors.Errorf("error loading parent tipset: %w", err)
}
}
2020-08-04 06:40:02 +00:00
}
func (mp *MessagePool) getGasReward(msg *types.SignedMessage, baseFee types.BigInt, ts *types.TipSet) *big.Int {
gasReward := abig.Mul(msg.Message.GasPremium, types.NewInt(uint64(msg.Message.GasLimit)))
maxReward := types.BigSub(msg.Message.GasFeeCap, baseFee)
if types.BigCmp(maxReward, gasReward) < 0 {
gasReward = maxReward
}
2020-08-05 06:46:48 +00:00
return gasReward.Int
}
func (mp *MessagePool) getGasPerf(gasReward *big.Int, gasLimit int64) float64 {
// gasPerf = gasReward * build.BlockGasLimit / gasLimit
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
}
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
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
a, _ := mp.api.StateGetActor(actor, ts)
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)
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]
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)
skip++
continue
}
2020-08-05 06:46:48 +00:00
if m.Message.Nonce != curNonce {
2020-08-04 19:01:32 +00:00
break
}
curNonce++
2020-08-05 06:46:48 +00:00
minGas := vm.PricelistByEpoch(ts.Height()).OnChainMessage(m.ChainLength()).Total()
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
}
required := m.Message.RequiredFunds().Int
if balance.Cmp(required) < 0 {
2020-08-04 19:01:32 +00:00
break
}
balance = new(big.Int).Sub(balance, required)
value := m.Message.Value.Int
if balance.Cmp(value) >= 0 {
// Note: we only account for the value if the balance doesn't drop below 0
// otherwise the message will fail and the miner can reap the gas rewards
balance = new(big.Int).Sub(balance, value)
}
gasReward := mp.getGasReward(m, baseFee, ts)
2020-08-04 19:01:32 +00:00
rewards = append(rewards, gasReward)
}
// check we have a sane set of messages to construct the chains
if i > 0 {
msgs = msgs[skip:i]
2020-08-04 19:01:32 +00:00
} else {
return nil
}
// 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:
// - in the first pass we create chains that aggreagate messages with non-decreasing gasPerf
// - 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
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
// try to add the message to the current chain -- if it decreases the gasPerf, then make a
// 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-- {
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]
}
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
}
2020-08-07 15:24:36 +00:00
func (mc *msgChain) Trim(gasLimit int64, mp *MessagePool, baseFee types.BigInt, ts *types.TipSet, priority bool) {
2020-08-04 17:24:23 +00:00
i := len(mc.msgs) - 1
2020-08-07 15:24:36 +00:00
for i >= 0 && (mc.gasLimit > gasLimit || (!priority && mc.gasPerf < 0)) {
gasReward := mp.getGasReward(mc.msgs[i], baseFee, ts)
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-04 17:24:23 +00:00
} else {
mc.gasPerf = 0
}
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
}
if mc.next != nil {
mc.next.invalidate()
mc.next = nil
}
}
func (mc *msgChain) invalidate() {
mc.valid = false
mc.msgs = nil
if mc.next != nil {
mc.next.invalidate()
mc.next = nil
}
2020-08-04 06:40:02 +00:00
}