Break dependency of ethcfg package to txpool. Create txpoolcfg package (#958)

This commit is contained in:
Alex Sharov 2023-03-29 14:24:37 +07:00 committed by GitHub
parent a21ab4ea5c
commit b29550fd20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 43 deletions

View File

@ -38,6 +38,7 @@ import (
"github.com/google/btree"
"github.com/hashicorp/golang-lru/v2/simplelru"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/txpool/txpoolcfg"
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/erigon-lib/chain"
@ -71,38 +72,6 @@ var (
basefeeSubCounter = metrics.GetOrCreateCounter(`txpool_basefee`)
)
type Config struct {
DBDir string
TracedSenders []string // List of senders for which tx pool should print out debugging info
SyncToNewPeersEvery time.Duration
ProcessRemoteTxsEvery time.Duration
CommitEvery time.Duration
LogEvery time.Duration
PendingSubPoolLimit int
BaseFeeSubPoolLimit int
QueuedSubPoolLimit int
MinFeeCap uint64
AccountSlots uint64 // Number of executable transaction slots guaranteed per account
PriceBump uint64 // Price bump percentage to replace an already existing transaction
OverrideShanghaiTime *big.Int
}
var DefaultConfig = Config{
SyncToNewPeersEvery: 2 * time.Minute,
ProcessRemoteTxsEvery: 100 * time.Millisecond,
CommitEvery: 15 * time.Second,
LogEvery: 30 * time.Second,
PendingSubPoolLimit: 10_000,
BaseFeeSubPoolLimit: 10_000,
QueuedSubPoolLimit: 10_000,
MinFeeCap: 1,
AccountSlots: 16, //TODO: to choose right value (16 to be compatible with Geth)
PriceBump: 10, // Price bump percentage to replace an already existing transaction
OverrideShanghaiTime: nil,
}
// Pool is interface for the transaction pool
// This interface exists for the convenience of testing, and not yet because
// there are multiple implementations
@ -320,7 +289,7 @@ type TxPool struct {
all *BySenderAndNonce // senderID => (sorted map of tx nonce => *metaTx)
deletedTxs []*metaTx // list of discarded txs since last db commit
promoted types.Announcements
cfg Config
cfg txpoolcfg.Config
chainID uint256.Int
lastSeenBlock atomic.Uint64
started atomic.Bool
@ -330,7 +299,7 @@ type TxPool struct {
isPostShanghai atomic.Bool
}
func New(newTxs chan types.Announcements, coreDB kv.RoDB, cfg Config, cache kvcache.Cache, chainID uint256.Int, shanghaiTime *big.Int) (*TxPool, error) {
func New(newTxs chan types.Announcements, coreDB kv.RoDB, cfg txpoolcfg.Config, cache kvcache.Cache, chainID uint256.Int, shanghaiTime *big.Int) (*TxPool, error) {
var err error
localsHistory, err := simplelru.NewLRU[string, struct{}](10_000, nil)
if err != nil {

View File

@ -6,9 +6,11 @@ import (
"bytes"
"context"
"encoding/binary"
"github.com/ledgerwatch/erigon-lib/common"
"testing"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/txpool/txpoolcfg"
"github.com/holiman/uint256"
"github.com/ledgerwatch/log/v3"
"github.com/stretchr/testify/assert"
@ -310,7 +312,7 @@ func FuzzOnNewBlocks(f *testing.F) {
ch := make(chan types.Announcements, 100)
db, coreDB := memdb.NewTestPoolDB(t), memdb.NewTestDB(t)
cfg := DefaultConfig
cfg := txpoolcfg.DefaultConfig
sendersCache := kvcache.New(kvcache.DefaultCoherentConfig)
pool, err := New(ch, coreDB, cfg, sendersCache, *u256.N1, nil)
assert.NoError(err)
@ -543,7 +545,7 @@ func FuzzOnNewBlocks(f *testing.F) {
check(p2pReceived, types.TxSlots{}, "after_flush")
checkNotify(p2pReceived, types.TxSlots{}, "after_flush")
p2, err := New(ch, coreDB, DefaultConfig, sendersCache, *u256.N1, nil)
p2, err := New(ch, coreDB, txpoolcfg.DefaultConfig, sendersCache, *u256.N1, nil)
assert.NoError(err)
p2.senders = pool.senders // senders are not persisted
err = coreDB.View(ctx, func(coreTx kv.Tx) error { return p2.fromDB(ctx, tx, coreTx) })

View File

@ -25,6 +25,7 @@ import (
"testing"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/txpool/txpoolcfg"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -44,7 +45,7 @@ func TestNonceFromAddress(t *testing.T) {
ch := make(chan types.Announcements, 100)
db, coreDB := memdb.NewTestPoolDB(t), memdb.NewTestDB(t)
cfg := DefaultConfig
cfg := txpoolcfg.DefaultConfig
sendersCache := kvcache.New(kvcache.DefaultCoherentConfig)
pool, err := New(ch, coreDB, cfg, sendersCache, *u256.N1, nil)
assert.NoError(err)
@ -164,7 +165,7 @@ func TestReplaceWithHigherFee(t *testing.T) {
ch := make(chan types.Announcements, 100)
db, coreDB := memdb.NewTestPoolDB(t), memdb.NewTestDB(t)
cfg := DefaultConfig
cfg := txpoolcfg.DefaultConfig
sendersCache := kvcache.New(kvcache.DefaultCoherentConfig)
pool, err := New(ch, coreDB, cfg, sendersCache, *u256.N1, nil)
assert.NoError(err)
@ -281,7 +282,7 @@ func TestReverseNonces(t *testing.T) {
ch := make(chan types.Announcements, 100)
db, coreDB := memdb.NewTestPoolDB(t), memdb.NewTestDB(t)
cfg := DefaultConfig
cfg := txpoolcfg.DefaultConfig
sendersCache := kvcache.New(kvcache.DefaultCoherentConfig)
pool, err := New(ch, coreDB, cfg, sendersCache, *u256.N1, nil)
assert.NoError(err)
@ -408,7 +409,7 @@ func TestTxPoke(t *testing.T) {
ch := make(chan types.Announcements, 100)
db, coreDB := memdb.NewTestPoolDB(t), memdb.NewTestDB(t)
cfg := DefaultConfig
cfg := txpoolcfg.DefaultConfig
sendersCache := kvcache.New(kvcache.DefaultCoherentConfig)
pool, err := New(ch, coreDB, cfg, sendersCache, *u256.N1, nil)
assert.NoError(err)
@ -665,7 +666,7 @@ func TestShanghaiValidateTx(t *testing.T) {
t.Run(name, func(t *testing.T) {
ch := make(chan types.Announcements, 100)
_, coreDB := memdb.NewTestPoolDB(t), memdb.NewTestDB(t)
cfg := DefaultConfig
cfg := txpoolcfg.DefaultConfig
var shanghaiTime *big.Int
if test.isShanghai {

View File

@ -0,0 +1,38 @@
package txpoolcfg
import (
"math/big"
"time"
)
type Config struct {
DBDir string
TracedSenders []string // List of senders for which tx pool should print out debugging info
SyncToNewPeersEvery time.Duration
ProcessRemoteTxsEvery time.Duration
CommitEvery time.Duration
LogEvery time.Duration
PendingSubPoolLimit int
BaseFeeSubPoolLimit int
QueuedSubPoolLimit int
MinFeeCap uint64
AccountSlots uint64 // Number of executable transaction slots guaranteed per account
PriceBump uint64 // Price bump percentage to replace an already existing transaction
OverrideShanghaiTime *big.Int
}
var DefaultConfig = Config{
SyncToNewPeersEvery: 2 * time.Minute,
ProcessRemoteTxsEvery: 100 * time.Millisecond,
CommitEvery: 15 * time.Second,
LogEvery: 30 * time.Second,
PendingSubPoolLimit: 10_000,
BaseFeeSubPoolLimit: 10_000,
QueuedSubPoolLimit: 10_000,
MinFeeCap: 1,
AccountSlots: 16, //TODO: to choose right value (16 to be compatible with Geth)
PriceBump: 10, // Price bump percentage to replace an already existing transaction
OverrideShanghaiTime: nil,
}

View File

@ -23,6 +23,7 @@ import (
"github.com/c2h5oh/datasize"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/txpool/txpoolcfg"
"github.com/ledgerwatch/log/v3"
mdbx2 "github.com/torquem-ch/mdbx-go/mdbx"
@ -99,7 +100,7 @@ func SaveChainConfigIfNeed(ctx context.Context, coreDB kv.RoDB, txPoolDB kv.RwDB
return cc, blockNum, nil
}
func AllComponents(ctx context.Context, cfg txpool.Config, cache kvcache.Cache, newTxs chan types.Announcements, chainDB kv.RoDB, sentryClients []direct.SentryClient, stateChangesClient txpool.StateChangesClient) (kv.RwDB, *txpool.TxPool, *txpool.Fetch, *txpool.Send, *txpool.GrpcServer, error) {
func AllComponents(ctx context.Context, cfg txpoolcfg.Config, cache kvcache.Cache, newTxs chan types.Announcements, chainDB kv.RoDB, sentryClients []direct.SentryClient, stateChangesClient txpool.StateChangesClient) (kv.RwDB, *txpool.TxPool, *txpool.Fetch, *txpool.Send, *txpool.GrpcServer, error) {
txPoolDB, err := mdbx.NewMDBX(log.New()).Label(kv.TxPoolDB).Path(cfg.DBDir).
WithTableCfg(func(defaultBuckets kv.TableCfg) kv.TableCfg { return kv.TxpoolTablesCfg }).
Flags(func(f uint) uint { return f ^ mdbx2.Durable | mdbx2.SafeNoSync }).