From 08685aa9494a26fe8cd78bcefcd818d2532594f0 Mon Sep 17 00:00:00 2001 From: andyzhang2023 Date: Wed, 30 Oct 2024 17:36:58 +0800 Subject: [PATCH 1/7] fix:staled transactions will lead to oom if the pool is always not-empty --- core/txpool/legacypool/legacypool.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 7953eda76e..3934a0ea77 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -1892,6 +1892,7 @@ func (pool *LegacyPool) demoteUnexecutables(demoteAddrs []common.Address) { } demoteTxMeter.Mark(int64(len(demoteAddrs))) + var removed = 0 // Iterate over all accounts and demote any non-executable transactions gasLimit := txpool.EffectiveGasLimit(pool.chainconfig, pool.currentHead.Load().GasLimit, pool.config.EffectiveGasCeil) for _, addr := range demoteAddrs { @@ -1955,7 +1956,9 @@ func (pool *LegacyPool) demoteUnexecutables(demoteAddrs []common.Address) { } } pool.pendingCache.del(dropPendingCache, pool.signer) + removed += len(dropPendingCache) } + pool.priced.Removed(removed) } // addressByHeartbeat is an account address tagged with its last activity timestamp. From a648aafb6176d95c3efb03854a2be60d0c9ac353 Mon Sep 17 00:00:00 2001 From: andyzhang2023 Date: Wed, 30 Oct 2024 17:38:27 +0800 Subject: [PATCH 2/7] fix: memory grows too fast if the pool copy all transactions 5 times per second --- core/txpool/legacypool/legacypool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 3934a0ea77..99d661cf2f 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -391,7 +391,7 @@ func (pool *LegacyPool) Init(gasTip uint64, head *types.Header, reserve txpool.A } func (pool *LegacyPool) loopOfSync() { - ticker := time.NewTicker(200 * time.Millisecond) + ticker := time.NewTicker(200 * time.Second) for { select { case <-pool.reorgShutdownCh: From 8582cd0f3b259324b25685b5d9c9e6f7d3f81cba Mon Sep 17 00:00:00 2001 From: andyzhang2023 Date: Mon, 25 Nov 2024 12:19:20 +0800 Subject: [PATCH 3/7] adjust sync2cache from 200s to 1.5s, for passing the Unit test cases --- accounts/abi/bind/util_test.go | 6 +++--- core/txpool/legacypool/legacypool.go | 2 +- eth/catalyst/api_test.go | 14 +++++++------- miner/payload_building_test.go | 2 +- miner/worker_test.go | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/accounts/abi/bind/util_test.go b/accounts/abi/bind/util_test.go index 87917d43fa..70468fd87d 100644 --- a/accounts/abi/bind/util_test.go +++ b/accounts/abi/bind/util_test.go @@ -83,7 +83,7 @@ func TestWaitDeployed(t *testing.T) { // Send and mine the transaction. backend.Client().SendTransaction(ctx, tx) - time.Sleep(500 * time.Millisecond) //wait for the tx to be mined + time.Sleep(2 * time.Second) //wait for the tx to be mined backend.Commit() select { @@ -118,7 +118,7 @@ func TestWaitDeployedCornerCases(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() backend.Client().SendTransaction(ctx, tx) - time.Sleep(500 * time.Millisecond) //wait for the tx to be mined + time.Sleep(2 * time.Second) //wait for the tx to be mined backend.Commit() notContractCreation := errors.New("tx is not contract creation") if _, err := bind.WaitDeployed(ctx, backend.Client(), tx); err.Error() != notContractCreation.Error() { @@ -137,6 +137,6 @@ func TestWaitDeployedCornerCases(t *testing.T) { }() backend.Client().SendTransaction(ctx, tx) - time.Sleep(500 * time.Millisecond) //wait for the tx to be mined + time.Sleep(2 * time.Second) //wait for the tx to be mined cancel() } diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 99d661cf2f..72d2a0478e 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -391,7 +391,7 @@ func (pool *LegacyPool) Init(gasTip uint64, head *types.Header, reserve txpool.A } func (pool *LegacyPool) loopOfSync() { - ticker := time.NewTicker(200 * time.Second) + ticker := time.NewTicker(1500 * time.Millisecond) for { select { case <-pool.reorgShutdownCh: diff --git a/eth/catalyst/api_test.go b/eth/catalyst/api_test.go index 64907080af..4e41fa67b8 100644 --- a/eth/catalyst/api_test.go +++ b/eth/catalyst/api_test.go @@ -118,7 +118,7 @@ func TestEth2AssembleBlock(t *testing.T) { } ethservice.TxPool().Add([]*types.Transaction{tx}, true, false) // we wait for the tx to be promoted into pending list in the txpool - time.Sleep(500 * time.Millisecond) + time.Sleep(2 * time.Second) blockParams := engine.PayloadAttributes{ Timestamp: blocks[9].Time() + 5, } @@ -157,7 +157,7 @@ func TestEth2AssembleBlockWithAnotherBlocksTxs(t *testing.T) { txs := blocks[9].Transactions() api.eth.TxPool().Add(txs, false, true) // we wait for the tx to be promoted into pending list in the txpool - time.Sleep(500 * time.Millisecond) + time.Sleep(2 * time.Second) blockParams := engine.PayloadAttributes{ Timestamp: blocks[8].Time() + 5, } @@ -199,7 +199,7 @@ func TestEth2PrepareAndGetPayload(t *testing.T) { txs := blocks[9].Transactions() ethservice.TxPool().Add(txs, true, false) // we wait for the txs to be promoted into pending list in the txpool - time.Sleep(500 * time.Millisecond) + time.Sleep(2 * time.Second) blockParams := engine.PayloadAttributes{ Timestamp: blocks[8].Time() + 5, } @@ -323,7 +323,7 @@ func TestEth2NewBlock(t *testing.T) { ethservice.TxPool().Add([]*types.Transaction{tx}, true, false) //we wait for the tx to be promoted into pending list in the txpool - time.Sleep(500 * time.Millisecond) + time.Sleep(2 * time.Second) execData, err := assembleWithTransactions(api, parent.Hash(), &engine.PayloadAttributes{ Timestamp: parent.Time() + 5, @@ -499,7 +499,7 @@ func TestFullAPI(t *testing.T) { tx, _ := types.SignTx(types.NewContractCreation(nonce, new(big.Int), 1000000, big.NewInt(2*params.InitialBaseFee), logCode), types.LatestSigner(ethservice.BlockChain().Config()), testKey) ethservice.TxPool().Add([]*types.Transaction{tx}, true, false) // we wait for the tx to be promoted into pending list in the txpool - time.Sleep(500 * time.Millisecond) + time.Sleep(2 * time.Second) } setupBlocks(t, ethservice, 10, parent, callback, nil) @@ -627,7 +627,7 @@ func TestNewPayloadOnInvalidChain(t *testing.T) { }) ethservice.TxPool().Add([]*types.Transaction{tx}, false, true) // we wait for the tx to be promoted into pending list in the txpool - time.Sleep(500 * time.Millisecond) + time.Sleep(2 * time.Second) var ( params = engine.PayloadAttributes{ Timestamp: parent.Time + 1, @@ -1333,7 +1333,7 @@ func setupBodies(t *testing.T) (*node.Node, *eth.Ethereum, []*types.Block) { tx, _ := types.SignTx(types.NewContractCreation(nonce, new(big.Int), 1000000, big.NewInt(2*params.InitialBaseFee), logCode), types.LatestSigner(ethservice.BlockChain().Config()), testKey) ethservice.TxPool().Add([]*types.Transaction{tx}, false, false) // we wait for the tx to be promoted into pending list in the txpool - time.Sleep(500 * time.Millisecond) + time.Sleep(2 * time.Second) } withdrawals := make([][]*types.Withdrawal, 10) diff --git a/miner/payload_building_test.go b/miner/payload_building_test.go index cb1220fbfa..d147f3cf71 100644 --- a/miner/payload_building_test.go +++ b/miner/payload_building_test.go @@ -54,7 +54,7 @@ func testBuildPayload(t *testing.T, noTxPool, interrupt bool) { txs := genTxs(1, numInterruptTxs) b.txPool.Add(txs, true, false) // we wait for the txs to be promoted - time.Sleep(500 * time.Millisecond) + time.Sleep(2 * time.Second) } timestamp := uint64(time.Now().Unix()) diff --git a/miner/worker_test.go b/miner/worker_test.go index 7a78b6898f..4aad512ee5 100644 --- a/miner/worker_test.go +++ b/miner/worker_test.go @@ -162,7 +162,7 @@ func (b *testWorkerBackend) newRandomTx(creation bool) *types.Transaction { func newTestWorker(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, db ethdb.Database, blocks int) (*worker, *testWorkerBackend) { backend := newTestWorkerBackend(t, chainConfig, engine, db, blocks) backend.txPool.Add(pendingTxs, true, false) - time.Sleep(500 * time.Millisecond) // Wait for txs to be promoted + time.Sleep(2 * time.Second) // Wait for txs to be promoted w := newWorker(testConfig, chainConfig, engine, backend, new(event.TypeMux), nil, false) w.setEtherbase(testBankAddress) return w, backend From 28fb1896bb21012f75a0c1d180f11e29d45d734e Mon Sep 17 00:00:00 2001 From: andyzhang2023 Date: Mon, 25 Nov 2024 14:21:29 +0800 Subject: [PATCH 4/7] adjust sync2cache from 200s to 1.5s, for passing the Unit test cases --- miner/worker_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miner/worker_test.go b/miner/worker_test.go index de89d0ffe8..7a6ec698ca 100644 --- a/miner/worker_test.go +++ b/miner/worker_test.go @@ -200,7 +200,7 @@ func TestGenerateAndImportBlock(t *testing.T) { for i := 0; i < 5; i++ { b.txPool.Add([]*types.Transaction{b.newRandomTx(true)}, true, false) b.txPool.Add([]*types.Transaction{b.newRandomTx(false)}, true, false) - time.Sleep(1 * time.Second) // Wait for txs to be promoted + time.Sleep(2 * time.Second) // Wait for txs to be promoted select { case ev := <-sub.Chan(): From 4d600de64fa09a930bb93e92ce326a40a9f4dbe2 Mon Sep 17 00:00:00 2001 From: andyzhang2023 Date: Mon, 25 Nov 2024 16:10:08 +0800 Subject: [PATCH 5/7] Revert "adjust sync2cache from 200s to 1.5s, for passing the Unit test cases" This reverts commit 28fb1896bb21012f75a0c1d180f11e29d45d734e. --- miner/worker_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miner/worker_test.go b/miner/worker_test.go index 7a6ec698ca..de89d0ffe8 100644 --- a/miner/worker_test.go +++ b/miner/worker_test.go @@ -200,7 +200,7 @@ func TestGenerateAndImportBlock(t *testing.T) { for i := 0; i < 5; i++ { b.txPool.Add([]*types.Transaction{b.newRandomTx(true)}, true, false) b.txPool.Add([]*types.Transaction{b.newRandomTx(false)}, true, false) - time.Sleep(2 * time.Second) // Wait for txs to be promoted + time.Sleep(1 * time.Second) // Wait for txs to be promoted select { case ev := <-sub.Chan(): From afabfd5e2c9da10b464db56f05e2200a5b578478 Mon Sep 17 00:00:00 2001 From: andyzhang2023 Date: Mon, 25 Nov 2024 16:10:10 +0800 Subject: [PATCH 6/7] Revert "adjust sync2cache from 200s to 1.5s, for passing the Unit test cases" This reverts commit 8582cd0f3b259324b25685b5d9c9e6f7d3f81cba. --- accounts/abi/bind/util_test.go | 6 +++--- core/txpool/legacypool/legacypool.go | 2 +- eth/catalyst/api_test.go | 14 +++++++------- miner/payload_building_test.go | 2 +- miner/worker_test.go | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/accounts/abi/bind/util_test.go b/accounts/abi/bind/util_test.go index 70468fd87d..87917d43fa 100644 --- a/accounts/abi/bind/util_test.go +++ b/accounts/abi/bind/util_test.go @@ -83,7 +83,7 @@ func TestWaitDeployed(t *testing.T) { // Send and mine the transaction. backend.Client().SendTransaction(ctx, tx) - time.Sleep(2 * time.Second) //wait for the tx to be mined + time.Sleep(500 * time.Millisecond) //wait for the tx to be mined backend.Commit() select { @@ -118,7 +118,7 @@ func TestWaitDeployedCornerCases(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() backend.Client().SendTransaction(ctx, tx) - time.Sleep(2 * time.Second) //wait for the tx to be mined + time.Sleep(500 * time.Millisecond) //wait for the tx to be mined backend.Commit() notContractCreation := errors.New("tx is not contract creation") if _, err := bind.WaitDeployed(ctx, backend.Client(), tx); err.Error() != notContractCreation.Error() { @@ -137,6 +137,6 @@ func TestWaitDeployedCornerCases(t *testing.T) { }() backend.Client().SendTransaction(ctx, tx) - time.Sleep(2 * time.Second) //wait for the tx to be mined + time.Sleep(500 * time.Millisecond) //wait for the tx to be mined cancel() } diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 72d2a0478e..99d661cf2f 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -391,7 +391,7 @@ func (pool *LegacyPool) Init(gasTip uint64, head *types.Header, reserve txpool.A } func (pool *LegacyPool) loopOfSync() { - ticker := time.NewTicker(1500 * time.Millisecond) + ticker := time.NewTicker(200 * time.Second) for { select { case <-pool.reorgShutdownCh: diff --git a/eth/catalyst/api_test.go b/eth/catalyst/api_test.go index 4e41fa67b8..64907080af 100644 --- a/eth/catalyst/api_test.go +++ b/eth/catalyst/api_test.go @@ -118,7 +118,7 @@ func TestEth2AssembleBlock(t *testing.T) { } ethservice.TxPool().Add([]*types.Transaction{tx}, true, false) // we wait for the tx to be promoted into pending list in the txpool - time.Sleep(2 * time.Second) + time.Sleep(500 * time.Millisecond) blockParams := engine.PayloadAttributes{ Timestamp: blocks[9].Time() + 5, } @@ -157,7 +157,7 @@ func TestEth2AssembleBlockWithAnotherBlocksTxs(t *testing.T) { txs := blocks[9].Transactions() api.eth.TxPool().Add(txs, false, true) // we wait for the tx to be promoted into pending list in the txpool - time.Sleep(2 * time.Second) + time.Sleep(500 * time.Millisecond) blockParams := engine.PayloadAttributes{ Timestamp: blocks[8].Time() + 5, } @@ -199,7 +199,7 @@ func TestEth2PrepareAndGetPayload(t *testing.T) { txs := blocks[9].Transactions() ethservice.TxPool().Add(txs, true, false) // we wait for the txs to be promoted into pending list in the txpool - time.Sleep(2 * time.Second) + time.Sleep(500 * time.Millisecond) blockParams := engine.PayloadAttributes{ Timestamp: blocks[8].Time() + 5, } @@ -323,7 +323,7 @@ func TestEth2NewBlock(t *testing.T) { ethservice.TxPool().Add([]*types.Transaction{tx}, true, false) //we wait for the tx to be promoted into pending list in the txpool - time.Sleep(2 * time.Second) + time.Sleep(500 * time.Millisecond) execData, err := assembleWithTransactions(api, parent.Hash(), &engine.PayloadAttributes{ Timestamp: parent.Time() + 5, @@ -499,7 +499,7 @@ func TestFullAPI(t *testing.T) { tx, _ := types.SignTx(types.NewContractCreation(nonce, new(big.Int), 1000000, big.NewInt(2*params.InitialBaseFee), logCode), types.LatestSigner(ethservice.BlockChain().Config()), testKey) ethservice.TxPool().Add([]*types.Transaction{tx}, true, false) // we wait for the tx to be promoted into pending list in the txpool - time.Sleep(2 * time.Second) + time.Sleep(500 * time.Millisecond) } setupBlocks(t, ethservice, 10, parent, callback, nil) @@ -627,7 +627,7 @@ func TestNewPayloadOnInvalidChain(t *testing.T) { }) ethservice.TxPool().Add([]*types.Transaction{tx}, false, true) // we wait for the tx to be promoted into pending list in the txpool - time.Sleep(2 * time.Second) + time.Sleep(500 * time.Millisecond) var ( params = engine.PayloadAttributes{ Timestamp: parent.Time + 1, @@ -1333,7 +1333,7 @@ func setupBodies(t *testing.T) (*node.Node, *eth.Ethereum, []*types.Block) { tx, _ := types.SignTx(types.NewContractCreation(nonce, new(big.Int), 1000000, big.NewInt(2*params.InitialBaseFee), logCode), types.LatestSigner(ethservice.BlockChain().Config()), testKey) ethservice.TxPool().Add([]*types.Transaction{tx}, false, false) // we wait for the tx to be promoted into pending list in the txpool - time.Sleep(2 * time.Second) + time.Sleep(500 * time.Millisecond) } withdrawals := make([][]*types.Withdrawal, 10) diff --git a/miner/payload_building_test.go b/miner/payload_building_test.go index d147f3cf71..cb1220fbfa 100644 --- a/miner/payload_building_test.go +++ b/miner/payload_building_test.go @@ -54,7 +54,7 @@ func testBuildPayload(t *testing.T, noTxPool, interrupt bool) { txs := genTxs(1, numInterruptTxs) b.txPool.Add(txs, true, false) // we wait for the txs to be promoted - time.Sleep(2 * time.Second) + time.Sleep(500 * time.Millisecond) } timestamp := uint64(time.Now().Unix()) diff --git a/miner/worker_test.go b/miner/worker_test.go index de89d0ffe8..1c19e60de9 100644 --- a/miner/worker_test.go +++ b/miner/worker_test.go @@ -163,7 +163,7 @@ func (b *testWorkerBackend) newRandomTx(creation bool) *types.Transaction { func newTestWorker(t *testing.T, chainConfig *params.ChainConfig, engine consensus.Engine, db ethdb.Database, blocks int) (*worker, *testWorkerBackend) { backend := newTestWorkerBackend(t, chainConfig, engine, db, blocks) backend.txPool.Add(pendingTxs, true, false) - time.Sleep(2 * time.Second) // Wait for txs to be promoted + time.Sleep(500 * time.Millisecond) // Wait for txs to be promoted w := newWorker(testConfig, chainConfig, engine, backend, new(event.TypeMux), nil, false) w.setEtherbase(testBankAddress) return w, backend From 7893787f0d0d61a299a4c1605f46792caaa01fc3 Mon Sep 17 00:00:00 2001 From: andyzhang2023 Date: Mon, 25 Nov 2024 16:11:52 +0800 Subject: [PATCH 7/7] set sync2cache frequency from 200s to 400ms, to pass the unit cases --- core/txpool/legacypool/legacypool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 99d661cf2f..202f495dc6 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -391,7 +391,7 @@ func (pool *LegacyPool) Init(gasTip uint64, head *types.Header, reserve txpool.A } func (pool *LegacyPool) loopOfSync() { - ticker := time.NewTicker(200 * time.Second) + ticker := time.NewTicker(400 * time.Millisecond) for { select { case <-pool.reorgShutdownCh: