From 6fb5d3b4c996b019c534abab7c6fcd80a4d60dbc Mon Sep 17 00:00:00 2001 From: Raneet Debnath Date: Mon, 3 Jul 2023 21:10:46 +0530 Subject: [PATCH] core,eth,miner: fix initial test cases --- core/state_transition.go | 2 +- core/txpool/txpool.go | 30 ++++++++++++++++++++---------- eth/downloader/downloader_test.go | 2 -- eth/protocols/eth/handler_test.go | 1 + eth/tracers/api_test.go | 3 ++- miner/test_backend.go | 12 ++++++------ 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/core/state_transition.go b/core/state_transition.go index 95e668abe0..e541fe3f53 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -458,7 +458,7 @@ func (st *StateTransition) TransitionDb(interruptCtx context.Context) (*Executio var burntContractAddress common.Address if rules.IsLondon { - burntContractAddress := common.HexToAddress(st.evm.ChainConfig().Bor.CalculateBurntContract(st.evm.Context.BlockNumber.Uint64())) + burntContractAddress = common.HexToAddress(st.evm.ChainConfig().Bor.CalculateBurntContract(st.evm.Context.BlockNumber.Uint64())) burnAmount = new(big.Int).Mul(new(big.Int).SetUint64(st.gasUsed()), st.evm.Context.BaseFee) if !st.noFeeBurnAndTip { diff --git a/core/txpool/txpool.go b/core/txpool/txpool.go index c5b9d3c992..0085ab5905 100644 --- a/core/txpool/txpool.go +++ b/core/txpool/txpool.go @@ -822,9 +822,9 @@ func (pool *TxPool) validateTxBasics(tx *types.Transaction, local bool) error { // Transactor should have enough funds to cover the costs // cost == V + GP * GL balance := pool.currentState.GetBalance(from) - if balance.Cmp(tx.Cost()) < 0 { - return core.ErrInsufficientFunds - } + // if balance.Cmp(tx.Cost()) < 0 { + // return core.ErrInsufficientFunds + // } // Verify that replacing transactions will not result in overdraft list := pool.pending[from] if list != nil { // Sender already has pending txs @@ -1200,31 +1200,33 @@ func (pool *TxPool) AddRemoteSync(txs *types.Transaction) error { // This is like AddRemotes with a single transaction, but waits for pool reorganization. Tests use this method. func (pool *TxPool) addRemoteSync(tx *types.Transaction) error { - return pool.AddRemoteSync(tx) + errs := pool.AddRemotesSync([]*types.Transaction{tx}) + return errs[0] } // AddRemote enqueues a single transaction into the pool if it is valid. This is a convenience // wrapper around AddRemotes. func (pool *TxPool) AddRemote(tx *types.Transaction) error { - return pool.addTx(tx, false, false) + errs := pool.AddRemotes([]*types.Transaction{tx}) + return errs[0] } // addTxs attempts to queue a batch of transactions if they are valid. func (pool *TxPool) addTxs(txs []*types.Transaction, local, sync bool) []error { // Filter out known ones without obtaining the pool lock or recovering signatures var ( - errs []error + errs = make([]error, len(txs)) news = make([]*types.Transaction, 0, len(txs)) hash common.Hash ) - for _, tx := range txs { + for i, tx := range txs { // If the transaction is known, pre-set the error slot hash = tx.Hash() if pool.all.Get(hash) != nil { - errs = append(errs, ErrAlreadyKnown) + errs[i] = ErrAlreadyKnown knownTxMeter.Mark(1) @@ -1236,7 +1238,7 @@ func (pool *TxPool) addTxs(txs []*types.Transaction, local, sync bool) []error { // in transactions before obtaining lock if err := pool.validateTxBasics(tx, local); err != nil { - errs = append(errs, ErrAlreadyKnown) + errs[i] = err invalidTxMeter.Mark(1) @@ -1257,9 +1259,17 @@ func (pool *TxPool) addTxs(txs []*types.Transaction, local, sync bool) []error { // Process all the new transaction and merge any errors into the original slice pool.mu.Lock() - errs, dirtyAddrs := pool.addTxsLocked(news, local) + newErrs, dirtyAddrs := pool.addTxsLocked(news, local) pool.mu.Unlock() + var nilSlot = 0 + for _, err := range newErrs { + for errs[nilSlot] != nil { + nilSlot++ + } + errs[nilSlot] = err + nilSlot++ + } // Reorg the pool internals if needed and return done := pool.requestPromoteExecutables(dirtyAddrs) if sync { diff --git a/eth/downloader/downloader_test.go b/eth/downloader/downloader_test.go index ddf2bf77d3..d4b15516e0 100644 --- a/eth/downloader/downloader_test.go +++ b/eth/downloader/downloader_test.go @@ -1860,8 +1860,6 @@ func TestCheckpointEnforcement67Light(t *testing.T) { } func testCheckpointEnforcement(t *testing.T, protocol uint, mode SyncMode) { - t.Parallel() - // Create a new tester with a particular hard coded checkpoint block tester := newTester(t) defer tester.terminate() diff --git a/eth/protocols/eth/handler_test.go b/eth/protocols/eth/handler_test.go index 663b3096e2..29e71a5d5a 100644 --- a/eth/protocols/eth/handler_test.go +++ b/eth/protocols/eth/handler_test.go @@ -98,6 +98,7 @@ func newTestBackendWithGenerator(blocks int, shanghai bool, generator func(int, TerminalTotalDifficulty: big.NewInt(0), TerminalTotalDifficultyPassed: true, Ethash: new(params.EthashConfig), + Bor: params.TestChainConfig.Bor, } engine = beacon.NewFaker() } diff --git a/eth/tracers/api_test.go b/eth/tracers/api_test.go index 1a8b3c2af3..a89e9089f9 100644 --- a/eth/tracers/api_test.go +++ b/eth/tracers/api_test.go @@ -66,11 +66,12 @@ type testBackend struct { // invoked in order to release associated resources. func newTestBackend(t *testing.T, n int, gspec *core.Genesis, generator func(i int, b *core.BlockGen)) *testBackend { backend := &testBackend{ - chainConfig: gspec.Config, + chainConfig: params.TestChainConfig, engine: ethash.NewFaker(), chaindb: rawdb.NewMemoryDatabase(), } // Generate blocks for testing + gspec.Config = backend.chainConfig _, blocks, _ := core.GenerateChainWithGenesis(gspec, backend.engine, n, generator) // Import the canonical chain diff --git a/miner/test_backend.go b/miner/test_backend.go index 5d0e8a09c5..e8acf42edf 100644 --- a/miner/test_backend.go +++ b/miner/test_backend.go @@ -5,19 +5,19 @@ import ( "crypto/rand" "errors" "fmt" + "math/big" + "os" + "sync/atomic" + "time" + "github.com/ethereum/go-ethereum/accounts" // nolint:typecheck "github.com/ethereum/go-ethereum/consensus/bor" "github.com/ethereum/go-ethereum/consensus/clique" "github.com/ethereum/go-ethereum/consensus/ethash" - "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/txpool" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" - "math/big" - "os" - "sync/atomic" - "time" "github.com/ethereum/go-ethereum/common" cmath "github.com/ethereum/go-ethereum/common/math" @@ -122,7 +122,7 @@ func newTestWorkerBackend(t TensingObject, chainConfig *params.ChainConfig, engi genesis := gspec.MustCommit(db) - chain, _ := core.NewBlockChain(rawdb.NewMemoryDatabase(), &core.CacheConfig{TrieDirtyDisabled: true}, &gspec, nil, engine, vm.Config{}, nil, nil, nil) + chain, _ := core.NewBlockChain(db, &core.CacheConfig{TrieDirtyDisabled: true}, &gspec, nil, engine, vm.Config{}, nil, nil, nil) txpool := txpool.NewTxPool(testTxPoolConfig, chainConfig, chain) // Generate a small n-block chain and an uncle block for it