2020-07-16 22:28:35 +00:00
|
|
|
package messagepool
|
|
|
|
|
|
|
|
import (
|
2020-08-01 22:54:21 +00:00
|
|
|
"context"
|
2020-08-01 21:56:43 +00:00
|
|
|
"sort"
|
2020-07-16 22:28:35 +00:00
|
|
|
"time"
|
|
|
|
|
2020-08-01 22:54:21 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-08-06 22:40:25 +00:00
|
|
|
"golang.org/x/xerrors"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
2023-11-14 00:06:11 +00:00
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-07-16 22:28:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (mp *MessagePool) pruneExcessMessages() error {
|
2020-08-01 23:25:13 +00:00
|
|
|
mp.curTsLk.Lock()
|
|
|
|
ts := mp.curTs
|
|
|
|
mp.curTsLk.Unlock()
|
|
|
|
|
2020-07-16 22:28:35 +00:00
|
|
|
mp.lk.Lock()
|
|
|
|
defer mp.lk.Unlock()
|
|
|
|
|
2021-01-14 02:55:49 +00:00
|
|
|
mpCfg := mp.getConfig()
|
|
|
|
if mp.currentSize < mpCfg.SizeLimitHigh {
|
2020-07-16 22:28:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-07 16:50:10 +00:00
|
|
|
select {
|
|
|
|
case <-mp.pruneCooldown:
|
|
|
|
err := mp.pruneMessages(context.TODO(), ts)
|
|
|
|
go func() {
|
2021-01-14 02:55:49 +00:00
|
|
|
time.Sleep(mpCfg.PruneCooldown)
|
2020-08-07 16:50:10 +00:00
|
|
|
mp.pruneCooldown <- struct{}{}
|
|
|
|
}()
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return xerrors.New("cannot prune before cooldown")
|
|
|
|
}
|
2020-08-01 21:56:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-01 23:25:13 +00:00
|
|
|
func (mp *MessagePool) pruneMessages(ctx context.Context, ts *types.TipSet) error {
|
2020-08-05 10:28:14 +00:00
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
|
|
|
log.Infof("message pruning took %s", time.Since(start))
|
|
|
|
}()
|
2020-08-01 22:54:21 +00:00
|
|
|
|
2020-08-06 22:40:25 +00:00
|
|
|
baseFee, err := mp.api.ChainComputeBaseFee(ctx, ts)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("computing basefee: %w", err)
|
|
|
|
}
|
2020-09-11 14:11:57 +00:00
|
|
|
baseFeeLowerBound := getBaseFeeLowerBound(baseFee, baseFeeLowerBoundFactor)
|
2020-08-06 22:40:25 +00:00
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
pending, _ := mp.getPendingMessages(ctx, ts, ts)
|
2020-08-05 20:16:09 +00:00
|
|
|
|
2020-09-11 18:12:11 +00:00
|
|
|
// protected actors -- not pruned
|
|
|
|
protected := make(map[address.Address]struct{})
|
|
|
|
|
2021-01-14 02:55:49 +00:00
|
|
|
mpCfg := mp.getConfig()
|
2020-09-11 18:12:11 +00:00
|
|
|
// we never prune priority addresses
|
2021-01-14 02:55:49 +00:00
|
|
|
for _, actor := range mpCfg.PriorityAddrs {
|
2021-05-31 22:12:42 +00:00
|
|
|
pk, err := mp.resolveToKey(ctx, actor)
|
2021-05-30 19:20:47 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debugf("pruneMessages failed to resolve priority address: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
protected[pk] = struct{}{}
|
2020-08-07 15:32:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 17:32:52 +00:00
|
|
|
// we also never prune locally published messages
|
2021-05-29 00:35:50 +00:00
|
|
|
mp.forEachLocal(ctx, func(ctx context.Context, actor address.Address) {
|
2020-09-11 18:12:11 +00:00
|
|
|
protected[actor] = struct{}{}
|
2021-05-29 00:35:50 +00:00
|
|
|
})
|
2020-09-11 17:32:52 +00:00
|
|
|
|
2020-08-05 10:28:14 +00:00
|
|
|
// Collect all messages to track which ones to remove and create chains for block inclusion
|
|
|
|
pruneMsgs := make(map[cid.Cid]*types.SignedMessage, mp.currentSize)
|
2020-08-07 15:32:05 +00:00
|
|
|
keepCount := 0
|
|
|
|
|
2020-08-05 10:28:14 +00:00
|
|
|
var chains []*msgChain
|
2020-08-05 20:16:09 +00:00
|
|
|
for actor, mset := range pending {
|
2020-09-11 18:12:11 +00:00
|
|
|
// we never prune protected actors
|
|
|
|
_, keep := protected[actor]
|
2020-08-07 15:32:05 +00:00
|
|
|
if keep {
|
|
|
|
keepCount += len(mset)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-09-11 18:12:11 +00:00
|
|
|
// not a protected actor, track the messages and create chains
|
2020-08-05 20:16:09 +00:00
|
|
|
for _, m := range mset {
|
2020-08-05 10:28:14 +00:00
|
|
|
pruneMsgs[m.Message.Cid()] = m
|
2020-08-01 22:54:21 +00:00
|
|
|
}
|
2020-09-11 09:15:03 +00:00
|
|
|
actorChains := mp.createMessageChains(actor, mset, baseFeeLowerBound, ts)
|
2020-08-05 10:28:14 +00:00
|
|
|
chains = append(chains, actorChains...)
|
2020-08-01 22:54:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 10:28:14 +00:00
|
|
|
// Sort the chains
|
2020-08-06 15:37:28 +00:00
|
|
|
sort.Slice(chains, func(i, j int) bool {
|
2020-08-05 10:28:14 +00:00
|
|
|
return chains[i].Before(chains[j])
|
2020-08-01 22:54:21 +00:00
|
|
|
})
|
|
|
|
|
2020-08-05 10:28:14 +00:00
|
|
|
// Keep messages (remove them from pruneMsgs) from chains while we are under the low water mark
|
2021-01-14 02:55:49 +00:00
|
|
|
loWaterMark := mpCfg.SizeLimitLow
|
2020-08-05 10:28:14 +00:00
|
|
|
keepLoop:
|
|
|
|
for _, chain := range chains {
|
|
|
|
for _, m := range chain.msgs {
|
2020-08-07 14:33:55 +00:00
|
|
|
if keepCount < loWaterMark {
|
2020-08-05 10:28:14 +00:00
|
|
|
delete(pruneMsgs, m.Message.Cid())
|
|
|
|
keepCount++
|
|
|
|
} else {
|
|
|
|
break keepLoop
|
2020-08-01 22:54:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 10:28:14 +00:00
|
|
|
// and remove all messages that are still in pruneMsgs after processing the chains
|
|
|
|
log.Infof("Pruning %d messages", len(pruneMsgs))
|
|
|
|
for _, m := range pruneMsgs {
|
2021-05-18 18:56:42 +00:00
|
|
|
mp.remove(ctx, m.Message.From, m.Message.Nonce, false)
|
2020-08-01 22:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|