|
| 1 | +package txpool |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/ecdsa" |
| 6 | + "math/big" |
| 7 | + "testing" |
| 8 | + "unsafe" |
| 9 | + |
| 10 | + "github.com/ethereum/go-ethereum/common" |
| 11 | + "github.com/ethereum/go-ethereum/core/types" |
| 12 | + "github.com/ethereum/go-ethereum/crypto" |
| 13 | + "github.com/ethereum/go-ethereum/eth/ethconfig" |
| 14 | + "github.com/ethereum/go-ethereum/ethclient" |
| 15 | + "github.com/ethereum/go-ethereum/ethclient/simulated" |
| 16 | + "github.com/ethereum/go-ethereum/node" |
| 17 | +) |
| 18 | + |
| 19 | +func makeGenesisAlloc(t *testing.T, n int) ([]common.Address, types.GenesisAlloc) { |
| 20 | + var addrs []common.Address |
| 21 | + alloc := make(types.GenesisAlloc) |
| 22 | + for i := 0; i < n; i++ { |
| 23 | + key, err := crypto.GenerateKey() |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("failed to generate a private key: err=%v", err) |
| 26 | + } |
| 27 | + |
| 28 | + addr := crypto.PubkeyToAddress(key.PublicKey) |
| 29 | + |
| 30 | + addrs = append(addrs, addr) |
| 31 | + alloc[addr] = types.Account{ |
| 32 | + Balance: new(big.Int).Lsh(common.Big1, 250), |
| 33 | + PrivateKey: crypto.FromECDSA(key), |
| 34 | + } |
| 35 | + } |
| 36 | + return addrs, alloc |
| 37 | +} |
| 38 | + |
| 39 | +func getPrivateKey(t *testing.T, account types.Account) *ecdsa.PrivateKey { |
| 40 | + key, err := crypto.ToECDSA(account.PrivateKey) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("failed to unmarshal the private key: err=%v", err) |
| 43 | + } |
| 44 | + return key |
| 45 | +} |
| 46 | + |
| 47 | +func getEthClient(sim *simulated.Backend) *ethclient.Client { |
| 48 | + type simClient struct { |
| 49 | + *ethclient.Client |
| 50 | + } |
| 51 | + ifceCli := sim.Client() |
| 52 | + ptrCli := unsafe.Add(unsafe.Pointer(&ifceCli), unsafe.Sizeof(uintptr(0))) |
| 53 | + return (*simClient)(ptrCli).Client |
| 54 | +} |
| 55 | + |
| 56 | +func transfer(t *testing.T, ctx context.Context, client *ethclient.Client, signer types.Signer, key *ecdsa.PrivateKey, nonce uint64, gasTipCap, gasFeeCap *big.Int, to common.Address, amount *big.Int) { |
| 57 | + tx := types.NewTx(&types.DynamicFeeTx{ |
| 58 | + Nonce: nonce, |
| 59 | + GasTipCap: gasTipCap, |
| 60 | + GasFeeCap: gasFeeCap, |
| 61 | + Gas: 21000, |
| 62 | + To: &to, |
| 63 | + Value: amount, |
| 64 | + }) |
| 65 | + |
| 66 | + tx, err := types.SignTx(tx, signer, key) |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("failed to sign tx: err=%v", err) |
| 69 | + } |
| 70 | + |
| 71 | + if err := client.SendTransaction(ctx, tx); err != nil { |
| 72 | + t.Fatalf("failed to send tx: err=%v", err) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func replace(t *testing.T, ctx context.Context, client *ethclient.Client, signer types.Signer, key *ecdsa.PrivateKey, priceBump uint64, nonce uint64, gasTipCap, gasFeeCap *big.Int, to common.Address, amount *big.Int) { |
| 77 | + addr := crypto.PubkeyToAddress(key.PublicKey) |
| 78 | + |
| 79 | + if minFeeCap, minTipCap, err := GetMinimumRequiredFee(ctx, client, addr, nonce, priceBump); err != nil { |
| 80 | + t.Fatalf("failed to get the minimum fee required to replace tx: err=%v", err) |
| 81 | + } else if minFeeCap.Cmp(common.Big0) == 0 { |
| 82 | + t.Fatalf("tx to replace not found") |
| 83 | + } else { |
| 84 | + t.Logf("minimum required fees: feeCap=%v, tipCap=%v", minFeeCap, minTipCap) |
| 85 | + if gasFeeCap.Cmp(minFeeCap) < 0 { |
| 86 | + t.Logf("gasFeeCap updated: %v => %v", gasFeeCap, minFeeCap) |
| 87 | + gasFeeCap = minFeeCap |
| 88 | + } |
| 89 | + if gasTipCap.Cmp(minTipCap) < 0 { |
| 90 | + t.Logf("gasTipCap updated: %v => %v", gasTipCap, minTipCap) |
| 91 | + gasTipCap = minTipCap |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + transfer(t, ctx, client, signer, key, nonce, gasTipCap, gasFeeCap, to, amount) |
| 96 | +} |
| 97 | + |
| 98 | +func TestContentFrom(t *testing.T) { |
| 99 | + ctx := context.Background() |
| 100 | + |
| 101 | + addrs, alloc := makeGenesisAlloc(t, 2) |
| 102 | + sender, receiver := addrs[0], addrs[1] |
| 103 | + senderKey := getPrivateKey(t, alloc[sender]) |
| 104 | + |
| 105 | + priceBump := uint64(25) |
| 106 | + sim := simulated.NewBackend(alloc, func(nodeConf *node.Config, ethConf *ethconfig.Config) { |
| 107 | + t.Logf("original price bump is %v", ethConf.TxPool.PriceBump) |
| 108 | + ethConf.TxPool.PriceBump = priceBump |
| 109 | + }) |
| 110 | + defer sim.Close() |
| 111 | + |
| 112 | + cli := getEthClient(sim) |
| 113 | + |
| 114 | + // make signer |
| 115 | + chainID, err := cli.ChainID(ctx) |
| 116 | + if err != nil { |
| 117 | + t.Fatalf("failed to get chain ID: err=%v", err) |
| 118 | + } |
| 119 | + signer := types.LatestSignerForChainID(chainID) |
| 120 | + |
| 121 | + // we use small fee cap to prepend txs from being included |
| 122 | + feeCap := big.NewInt(1_000_000) |
| 123 | + tipCap := big.NewInt(1_000_000) |
| 124 | + amount := big.NewInt(1_000_000_000) // 1 GWei |
| 125 | + |
| 126 | + transfer(t, ctx, cli, signer, senderKey, 0, tipCap, feeCap, receiver, amount) |
| 127 | + for i := 0; i < 10; i++ { |
| 128 | + replace(t, ctx, cli, signer, senderKey, priceBump, 0, tipCap, feeCap, receiver, amount) |
| 129 | + } |
| 130 | + |
| 131 | + // check block info |
| 132 | + block, err := cli.BlockByNumber(ctx, nil) |
| 133 | + if err != nil { |
| 134 | + t.Fatalf("failed to get block by number: err=%v", err) |
| 135 | + } else { |
| 136 | + t.Logf("bn=%v, basefee=%v", block.Number(), block.BaseFee()) |
| 137 | + } |
| 138 | + |
| 139 | + // commit block |
| 140 | + sim.Commit() |
| 141 | + |
| 142 | + // check block info |
| 143 | + block, err = cli.BlockByNumber(ctx, nil) |
| 144 | + if err != nil { |
| 145 | + t.Fatalf("failed to get block by number: err=%v", err) |
| 146 | + } else { |
| 147 | + t.Logf("bn=%v, basefee=%v", block.Number(), block.BaseFee()) |
| 148 | + } |
| 149 | + |
| 150 | + // check that len(pendingTxs) == 1 |
| 151 | + if pendingTxs, err := PendingTransactions(ctx, cli, sender); err != nil { |
| 152 | + t.Fatalf("failed to get pending transactions: err=%v", err) |
| 153 | + } else if len(pendingTxs) != 1 { |
| 154 | + t.Fatalf("unexpected pending txs: pendingTxs=%v", pendingTxs) |
| 155 | + } |
| 156 | + |
| 157 | + // update fee cap to allow the tx to be included |
| 158 | + feeCap = block.BaseFee() |
| 159 | + |
| 160 | + replace(t, ctx, cli, signer, senderKey, priceBump, 0, tipCap, feeCap, receiver, amount) |
| 161 | + |
| 162 | + // commit block |
| 163 | + sim.Commit() |
| 164 | + |
| 165 | + // check block info |
| 166 | + block, err = cli.BlockByNumber(ctx, nil) |
| 167 | + if err != nil { |
| 168 | + t.Fatalf("failed to get block by number: err=%v", err) |
| 169 | + } else { |
| 170 | + t.Logf("bn=%v, basefee=%v", block.Number(), block.BaseFee()) |
| 171 | + } |
| 172 | + |
| 173 | + // check that len(pendingTxs) == 0 |
| 174 | + if pendingTxs, err := PendingTransactions(ctx, cli, sender); err != nil { |
| 175 | + t.Fatalf("failed to get pending transactions: err=%v", err) |
| 176 | + } else if len(pendingTxs) != 0 { |
| 177 | + t.Fatalf("unexpected pending txs: pendingTxs=%v", pendingTxs) |
| 178 | + } |
| 179 | +} |
0 commit comments