|
| 1 | +// Copyright 2020 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 gasprice |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "math/big" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/scroll-tech/go-ethereum/common" |
| 25 | + "github.com/scroll-tech/go-ethereum/core" |
| 26 | + "github.com/scroll-tech/go-ethereum/core/state" |
| 27 | + "github.com/scroll-tech/go-ethereum/core/types" |
| 28 | + "github.com/scroll-tech/go-ethereum/crypto" |
| 29 | + "github.com/scroll-tech/go-ethereum/event" |
| 30 | + "github.com/scroll-tech/go-ethereum/params" |
| 31 | + "github.com/scroll-tech/go-ethereum/rpc" |
| 32 | + "github.com/scroll-tech/go-ethereum/trie" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + blockGasLimit = params.TxGas * 3 |
| 37 | +) |
| 38 | + |
| 39 | +type testTxData struct { |
| 40 | + priorityFee int64 |
| 41 | + gasLimit uint64 |
| 42 | +} |
| 43 | + |
| 44 | +type opTestBackend struct { |
| 45 | + block *types.Block |
| 46 | + receipts []*types.Receipt |
| 47 | +} |
| 48 | + |
| 49 | +func (b *opTestBackend) HeaderByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Header, error) { |
| 50 | + panic("not implemented") |
| 51 | +} |
| 52 | + |
| 53 | +func (b *opTestBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { |
| 54 | + return b.block, nil |
| 55 | +} |
| 56 | + |
| 57 | +func (b *opTestBackend) GetReceipts(ctx context.Context, hash common.Hash) (types.Receipts, error) { |
| 58 | + return b.receipts, nil |
| 59 | +} |
| 60 | + |
| 61 | +func (b *opTestBackend) Pending() (*types.Block, types.Receipts, *state.StateDB) { |
| 62 | + panic("not implemented") |
| 63 | +} |
| 64 | + |
| 65 | +func (b *opTestBackend) ChainConfig() *params.ChainConfig { |
| 66 | + return b.chain.Config() |
| 67 | +} |
| 68 | + |
| 69 | +func (b *opTestBackend) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +var _ OracleBackend = (*opTestBackend)(nil) |
| 74 | + |
| 75 | +func newOpTestBackend(t *testing.T, txs []testTxData) *opTestBackend { |
| 76 | + var ( |
| 77 | + key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") |
| 78 | + signer = types.LatestSigner(params.TestChainConfig) |
| 79 | + ) |
| 80 | + // only the most recent block is considered for optimism priority fee suggestions, so this is |
| 81 | + // where we add the test transactions |
| 82 | + ts := []*types.Transaction{} |
| 83 | + rs := []*types.Receipt{} |
| 84 | + header := types.Header{} |
| 85 | + header.GasLimit = blockGasLimit |
| 86 | + var nonce uint64 |
| 87 | + for _, tx := range txs { |
| 88 | + txdata := &types.DynamicFeeTx{ |
| 89 | + ChainID: params.TestChainConfig.ChainID, |
| 90 | + Nonce: nonce, |
| 91 | + To: &common.Address{}, |
| 92 | + Gas: params.TxGas, |
| 93 | + GasFeeCap: big.NewInt(100 * params.GWei), |
| 94 | + GasTipCap: big.NewInt(tx.priorityFee), |
| 95 | + Data: []byte{}, |
| 96 | + } |
| 97 | + t := types.MustSignNewTx(key, signer, txdata) |
| 98 | + ts = append(ts, t) |
| 99 | + r := types.Receipt{} |
| 100 | + r.GasUsed = tx.gasLimit |
| 101 | + header.GasUsed += r.GasUsed |
| 102 | + rs = append(rs, &r) |
| 103 | + nonce++ |
| 104 | + } |
| 105 | + hasher := trie.NewStackTrie(nil) |
| 106 | + b := types.NewBlock(&header, &types.Body{Transactions: ts}, nil, hasher, types.DefaultBlockConfig) |
| 107 | + return &opTestBackend{block: b, receipts: rs} |
| 108 | +} |
| 109 | + |
| 110 | +func TestSuggestOptimismPriorityFee(t *testing.T) { |
| 111 | + minSuggestion := new(big.Int).SetUint64(1e8 * params.Wei) |
| 112 | + cases := []struct { |
| 113 | + txdata []testTxData |
| 114 | + want *big.Int |
| 115 | + }{ |
| 116 | + { |
| 117 | + // block well under capacity, expect min priority fee suggestion |
| 118 | + txdata: []testTxData{{params.GWei, 21000}}, |
| 119 | + want: minSuggestion, |
| 120 | + }, |
| 121 | + { |
| 122 | + // 2 txs, still under capacity, expect min priority fee suggestion |
| 123 | + txdata: []testTxData{{params.GWei, 21000}, {params.GWei, 21000}}, |
| 124 | + want: minSuggestion, |
| 125 | + }, |
| 126 | + { |
| 127 | + // 2 txs w same priority fee (1 gwei), but second tx puts it right over capacity |
| 128 | + txdata: []testTxData{{params.GWei, 21000}, {params.GWei, 21001}}, |
| 129 | + want: big.NewInt(1100000000), // 10 percent over 1 gwei, the median |
| 130 | + }, |
| 131 | + { |
| 132 | + // 3 txs, full block. return 10% over the median tx (10 gwei * 10% == 11 gwei) |
| 133 | + txdata: []testTxData{{10 * params.GWei, 21000}, {1 * params.GWei, 21000}, {100 * params.GWei, 21000}}, |
| 134 | + want: big.NewInt(11 * params.GWei), |
| 135 | + }, |
| 136 | + } |
| 137 | + for i, c := range cases { |
| 138 | + backend := newOpTestBackend(t, c.txdata) |
| 139 | + oracle := NewOracle(backend, Config{MinSuggestedPriorityFee: minSuggestion}, big.NewInt(params.GWei)) |
| 140 | + got := oracle.SuggestOptimismPriorityFee(context.Background(), backend.block.Header(), backend.block.Hash()) |
| 141 | + if got.Cmp(c.want) != 0 { |
| 142 | + t.Errorf("Gas price mismatch for test case %d: want %d, got %d", i, c.want, got) |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments