|
| 1 | +// Copyright 2025 The go-ethereum Authors |
| 2 | +// This file is part of the go-ethereum library. |
| 3 | +// |
| 4 | +// The go-ethereum library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// The go-ethereum library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package locals |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "math/big" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/ethereum/go-ethereum/common" |
| 26 | + "github.com/ethereum/go-ethereum/consensus/ethash" |
| 27 | + "github.com/ethereum/go-ethereum/core" |
| 28 | + "github.com/ethereum/go-ethereum/core/rawdb" |
| 29 | + "github.com/ethereum/go-ethereum/core/txpool" |
| 30 | + "github.com/ethereum/go-ethereum/core/txpool/legacypool" |
| 31 | + "github.com/ethereum/go-ethereum/core/types" |
| 32 | + "github.com/ethereum/go-ethereum/core/vm" |
| 33 | + "github.com/ethereum/go-ethereum/crypto" |
| 34 | + "github.com/ethereum/go-ethereum/ethdb" |
| 35 | + "github.com/ethereum/go-ethereum/params" |
| 36 | +) |
| 37 | + |
| 38 | +var ( |
| 39 | + key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") |
| 40 | + address = crypto.PubkeyToAddress(key.PublicKey) |
| 41 | + funds = big.NewInt(1000000000000000) |
| 42 | + gspec = &core.Genesis{ |
| 43 | + Config: params.TestChainConfig, |
| 44 | + Alloc: types.GenesisAlloc{ |
| 45 | + address: {Balance: funds}, |
| 46 | + }, |
| 47 | + BaseFee: big.NewInt(params.InitialBaseFee), |
| 48 | + } |
| 49 | + signer = types.LatestSigner(gspec.Config) |
| 50 | +) |
| 51 | + |
| 52 | +type testEnv struct { |
| 53 | + chain *core.BlockChain |
| 54 | + pool *txpool.TxPool |
| 55 | + tracker *TxTracker |
| 56 | + genDb ethdb.Database |
| 57 | +} |
| 58 | + |
| 59 | +func newTestEnv(t *testing.T, n int, gasTip uint64, journal string) *testEnv { |
| 60 | + genDb, blocks, _ := core.GenerateChainWithGenesis(gspec, ethash.NewFaker(), n, func(i int, gen *core.BlockGen) { |
| 61 | + tx, err := types.SignTx(types.NewTransaction(gen.TxNonce(address), common.Address{0x00}, big.NewInt(1000), params.TxGas, gen.BaseFee(), nil), signer, key) |
| 62 | + if err != nil { |
| 63 | + panic(err) |
| 64 | + } |
| 65 | + gen.AddTx(tx) |
| 66 | + }) |
| 67 | + |
| 68 | + db := rawdb.NewMemoryDatabase() |
| 69 | + chain, _ := core.NewBlockChain(db, nil, gspec, nil, ethash.NewFaker(), vm.Config{}, nil) |
| 70 | + if n, err := chain.InsertChain(blocks); err != nil { |
| 71 | + t.Fatalf("failed to process block %d: %v", n, err) |
| 72 | + } |
| 73 | + legacyPool := legacypool.New(legacypool.DefaultConfig, chain) |
| 74 | + pool, err := txpool.New(gasTip, chain, []txpool.SubPool{legacyPool}) |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("Failed to create tx pool: %v", err) |
| 77 | + } |
| 78 | + time.Sleep(time.Second) // hack, make sure txpool initialization is done |
| 79 | + |
| 80 | + return &testEnv{ |
| 81 | + chain: chain, |
| 82 | + pool: pool, |
| 83 | + tracker: New(journal, time.Minute, gspec.Config, pool), |
| 84 | + genDb: genDb, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (env *testEnv) close() { |
| 89 | + env.chain.Stop() |
| 90 | +} |
| 91 | + |
| 92 | +func (env *testEnv) setGasTip(gasTip uint64) { |
| 93 | + env.pool.SetGasTip(new(big.Int).SetUint64(gasTip)) |
| 94 | +} |
| 95 | + |
| 96 | +func (env *testEnv) makeTx(nonce uint64, gasPrice *big.Int) *types.Transaction { |
| 97 | + if nonce == 0 { |
| 98 | + head := env.chain.CurrentHeader() |
| 99 | + state, _ := env.chain.StateAt(head.Root) |
| 100 | + nonce = state.GetNonce(address) |
| 101 | + } |
| 102 | + if gasPrice == nil { |
| 103 | + gasPrice = big.NewInt(params.GWei) |
| 104 | + } |
| 105 | + tx, _ := types.SignTx(types.NewTransaction(nonce, common.Address{0x00}, big.NewInt(1000), params.TxGas, gasPrice, nil), signer, key) |
| 106 | + return tx |
| 107 | +} |
| 108 | + |
| 109 | +func (env *testEnv) commit() { |
| 110 | + head := env.chain.CurrentBlock() |
| 111 | + block := env.chain.GetBlock(head.Hash(), head.Number.Uint64()) |
| 112 | + blocks, _ := core.GenerateChain(env.chain.Config(), block, ethash.NewFaker(), env.genDb, 1, func(i int, gen *core.BlockGen) { |
| 113 | + tx, err := types.SignTx(types.NewTransaction(gen.TxNonce(address), common.Address{0x00}, big.NewInt(1000), params.TxGas, gen.BaseFee(), nil), signer, key) |
| 114 | + if err != nil { |
| 115 | + panic(err) |
| 116 | + } |
| 117 | + gen.AddTx(tx) |
| 118 | + }) |
| 119 | + env.chain.InsertChain(blocks) |
| 120 | +} |
| 121 | + |
| 122 | +func TestRejectInvalids(t *testing.T) { |
| 123 | + env := newTestEnv(t, 10, 0, "") |
| 124 | + defer env.close() |
| 125 | + |
| 126 | + var cases = []struct { |
| 127 | + gasTip uint64 |
| 128 | + tx *types.Transaction |
| 129 | + expErr error |
| 130 | + op func() |
| 131 | + }{ |
| 132 | + { |
| 133 | + tx: env.makeTx(5, nil), // stale |
| 134 | + expErr: core.ErrNonceTooLow, |
| 135 | + }, |
| 136 | + { |
| 137 | + tx: env.makeTx(11, nil), // future transaction |
| 138 | + expErr: nil, |
| 139 | + }, |
| 140 | + { |
| 141 | + gasTip: params.GWei, |
| 142 | + tx: env.makeTx(0, new(big.Int).SetUint64(params.GWei/2)), // low price |
| 143 | + expErr: txpool.ErrUnderpriced, |
| 144 | + }, |
| 145 | + { |
| 146 | + tx: types.NewTransaction(10, common.Address{0x00}, big.NewInt(1000), params.TxGas, big.NewInt(params.GWei), nil), // invalid signature |
| 147 | + expErr: types.ErrInvalidSig, |
| 148 | + }, |
| 149 | + { |
| 150 | + op: func() { |
| 151 | + env.commit() |
| 152 | + }, |
| 153 | + tx: env.makeTx(10, nil), // stale |
| 154 | + expErr: core.ErrNonceTooLow, |
| 155 | + }, |
| 156 | + { |
| 157 | + tx: env.makeTx(11, nil), |
| 158 | + expErr: nil, |
| 159 | + }, |
| 160 | + } |
| 161 | + for _, c := range cases { |
| 162 | + if c.gasTip != 0 { |
| 163 | + env.setGasTip(c.gasTip) |
| 164 | + } |
| 165 | + if c.op != nil { |
| 166 | + c.op() |
| 167 | + } |
| 168 | + gotErr := env.tracker.Track(c.tx) |
| 169 | + if c.expErr == nil && gotErr != nil { |
| 170 | + t.Fatalf("Unexpected error: %v", gotErr) |
| 171 | + } |
| 172 | + if c.expErr != nil && !errors.Is(gotErr, c.expErr) { |
| 173 | + t.Fatalf("Unexpected error, want: %v, got: %v", c.expErr, gotErr) |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments