use set rather than slice for tracking yielded when mining (#6488)

make use of sets when tracking yielded transactions during mining
This commit is contained in:
hexoscott 2023-01-02 13:00:08 +00:00 committed by GitHub
parent 17e0afbe44
commit 1a09dcbdb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 14 deletions

View File

@ -9,6 +9,7 @@ import (
"sync/atomic"
"time"
mapset "github.com/deckarep/golang-set/v2"
"github.com/holiman/uint256"
"github.com/ledgerwatch/log/v3"
"golang.org/x/net/context"
@ -18,6 +19,7 @@ import (
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon-lib/txpool"
types2 "github.com/ledgerwatch/erigon-lib/types"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/rlp"
@ -121,7 +123,7 @@ func SpawnMiningExecStage(s *StageState, tx kv.RwTx, cfg MiningExecCfg, quit <-c
NotifyPendingLogs(logPrefix, cfg.notifier, logs)
} else {
yielded := make([][32]byte, 0)
yielded := mapset.NewSet[[32]byte]()
simulationTx := memdb.NewMemoryBatch(tx, cfg.tmpdir)
defer simulationTx.Rollback()
executionAt, err := s.ExecutionAt(tx)
@ -134,7 +136,6 @@ func SpawnMiningExecStage(s *StageState, tx kv.RwTx, cfg MiningExecCfg, quit <-c
if err != nil {
return err
}
yielded = append(yielded, y...)
if !txs.Empty() {
logs, stop, err := addTransactionsToMiningBlock(logPrefix, current, cfg.chainConfig, cfg.vmConfig, getHeader, cfg.engine, txs, cfg.miningState.MiningConfig.Etherbase, ibs, quit, cfg.interrupt, cfg.payloadId)
@ -150,7 +151,7 @@ func SpawnMiningExecStage(s *StageState, tx kv.RwTx, cfg MiningExecCfg, quit <-c
}
// if we yielded less than the count we wanted, assume the txpool has run dry now and stop to save another loop
if len(y) < 50 {
if y < 50 {
break
}
}
@ -190,17 +191,17 @@ func getNextTransactions(
amount uint16,
executionAt uint64,
simulationTx *memdb.MemoryMutation,
alreadyYielded [][32]byte,
) (types.TransactionsStream, [][32]byte, error) {
alreadyYielded mapset.Set[[32]byte],
) (types.TransactionsStream, int, error) {
txSlots := types2.TxsRlp{}
var onTime bool
var yielded [][32]byte
count := 0
if err := cfg.txPool2DB.View(context.Background(), func(poolTx kv.Tx) error {
var err error
counter := 0
for !onTime && counter < 1000 {
remainingGas := header.GasLimit - header.GasUsed
if onTime, yielded, err = cfg.txPool2.YieldBest(amount, &txSlots, poolTx, executionAt, remainingGas, alreadyYielded); err != nil {
if onTime, count, err = cfg.txPool2.YieldBest(amount, &txSlots, poolTx, executionAt, remainingGas, alreadyYielded); err != nil {
return err
}
time.Sleep(1 * time.Millisecond)
@ -208,7 +209,7 @@ func getNextTransactions(
}
return nil
}); err != nil {
return nil, nil, err
return nil, 0, err
}
var txs []types.Transaction //nolint:prealloc
@ -223,7 +224,7 @@ func getNextTransactions(
continue
}
if err != nil {
return nil, nil, err
return nil, 0, err
}
if !transaction.GetChainID().IsZero() && transaction.GetChainID().Cmp(chainID) != 0 {
continue
@ -240,10 +241,10 @@ func getNextTransactions(
blockNum := executionAt + 1
txs, err := filterBadTransactions(txs, cfg.chainConfig, blockNum, header.BaseFee, simulationTx)
if err != nil {
return nil, nil, err
return nil, 0, err
}
return types.NewTransactionsFixedOrder(txs), yielded, nil
return types.NewTransactionsFixedOrder(txs), count, nil
}
func filterBadTransactions(transactions []types.Transaction, config params.ChainConfig, blockNumber uint64, baseFee *big.Int, simulationTx *memdb.MemoryMutation) ([]types.Transaction, error) {

3
go.mod
View File

@ -3,7 +3,7 @@ module github.com/ledgerwatch/erigon
go 1.18
require (
github.com/ledgerwatch/erigon-lib v0.0.0-20230102030350-e98139900454
github.com/ledgerwatch/erigon-lib v0.0.0-20230102122455-3c7c00677de7
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20221223003841-487873d31492
github.com/ledgerwatch/log/v3 v3.7.0
github.com/ledgerwatch/secp256k1 v1.0.0
@ -26,6 +26,7 @@ require (
github.com/crate-crypto/go-ipa v0.0.0-20221111143132-9aa5d42120bc
github.com/davecgh/go-spew v1.1.1
github.com/deckarep/golang-set v1.8.0
github.com/deckarep/golang-set/v2 v2.1.0
github.com/docker/docker v20.10.17+incompatible
github.com/dop251/goja v0.0.0-20220405120441-9037c2b61cbf
github.com/edsrzf/mmap-go v1.1.0

6
go.sum
View File

@ -234,6 +234,8 @@ github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c h1:pFUpOrbxDR
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c/go.mod h1:6UhI8N9EjYm1c2odKpFpAYeR8dsBeM7PtzQhRgxRr9U=
github.com/deckarep/golang-set v1.8.0 h1:sk9/l/KqpunDwP7pSjUg0keiOOLEnOBHzykLrsPppp4=
github.com/deckarep/golang-set v1.8.0/go.mod h1:5nI87KwE7wgsBU1F4GKAw2Qod7p5kyS383rP6+o6qqo=
github.com/deckarep/golang-set/v2 v2.1.0 h1:g47V4Or+DUdzbs8FxCCmgb6VYd+ptPAngjM6dtGktsI=
github.com/deckarep/golang-set/v2 v2.1.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 h1:HbphB4TFFXpv7MNrT52FGrrgVXF1owhMVTHFZIlnvd4=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0/go.mod h1:DZGJHZMqrU4JJqFAWUS2UO1+lbSKsdiOoYi9Zzey7Fc=
@ -563,8 +565,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3PYPwICLl+/9oulQauOuETfgFvhBDffs0=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/ledgerwatch/erigon-lib v0.0.0-20230102030350-e98139900454 h1:Q7HDFrWmQZafwQI4C7duP2eS5wGQSzUsr804LhzzMmQ=
github.com/ledgerwatch/erigon-lib v0.0.0-20230102030350-e98139900454/go.mod h1:HaO/Rz8JnrwEhJWEZxRyy3H3DAImgoK05l9ZhULXU2k=
github.com/ledgerwatch/erigon-lib v0.0.0-20230102122455-3c7c00677de7 h1:2g77r9NnEJxeGeQZBlUkUjOAstnVgeL2ZNBQ9Cpurts=
github.com/ledgerwatch/erigon-lib v0.0.0-20230102122455-3c7c00677de7/go.mod h1:6/5QIpW4N5whHwz8wFG6Q4WFd962KoaWvTxQ95Snfd8=
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20221223003841-487873d31492 h1:SSYvbAzdreVrXdy8z8A92ug36c7zsGQLzXFrSiw92Zc=
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20221223003841-487873d31492/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo=
github.com/ledgerwatch/log/v3 v3.7.0 h1:aFPEZdwZx4jzA3+/Pf8wNDN5tCI0cIolq/kfvgcM+og=