|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | + "github.com/ethereum/go-ethereum/core/types" |
| 13 | + "github.com/ethereum/go-ethereum/crypto" |
| 14 | + "github.com/ethereum/go-ethereum/trie" |
| 15 | + |
| 16 | + "github.com/offchainlabs/nitro/arbutil" |
| 17 | +) |
| 18 | + |
| 19 | +func TestFetchTransactionsForBlockHeader_DynamicFeeTxs(t *testing.T) { |
| 20 | + ctx := context.Background() |
| 21 | + total := uint64(42) |
| 22 | + txes := make([]*types.Transaction, total) |
| 23 | + for i := uint64(0); i < total; i++ { |
| 24 | + txData := types.DynamicFeeTx{ |
| 25 | + Nonce: i, |
| 26 | + To: nil, |
| 27 | + Gas: 21000, |
| 28 | + GasTipCap: big.NewInt(1), |
| 29 | + GasFeeCap: big.NewInt(1), |
| 30 | + } |
| 31 | + txes[i] = types.NewTx(&txData) |
| 32 | + } |
| 33 | + hasher := newRecordingHasher() |
| 34 | + txsRoot := types.DeriveSha(types.Transactions(txes), hasher) |
| 35 | + header := &types.Header{ |
| 36 | + TxHash: txsRoot, |
| 37 | + } |
| 38 | + preimages := hasher.GetPreimages() |
| 39 | + mockPreimageResolver := &mockPreimageResolver{ |
| 40 | + preimages: preimages, |
| 41 | + } |
| 42 | + txsFetcher := &txsFetcherForBlock{ |
| 43 | + header: header, |
| 44 | + preimageResolver: mockPreimageResolver, |
| 45 | + } |
| 46 | + fetched, err := txsFetcher.TransactionsByHeader(ctx, header.Hash()) |
| 47 | + require.NoError(t, err) |
| 48 | + require.True(t, uint64(len(fetched)) == total) // #nosec G115 |
| 49 | + for i, tx := range fetched { |
| 50 | + require.Equal(t, txes[i].Hash(), tx.Hash()) |
| 51 | + require.Equal(t, uint64(i), tx.Nonce()) // #nosec G115 |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestFetchTransactionsForBlockHeader_LegacyTxs(t *testing.T) { |
| 56 | + ctx := context.Background() |
| 57 | + total := uint64(42) |
| 58 | + txes := make([]*types.Transaction, total) |
| 59 | + for i := uint64(0); i < total; i++ { |
| 60 | + txes[i] = types.NewTransaction(i, common.Address{}, big.NewInt(0), 21000, big.NewInt(1), nil) |
| 61 | + } |
| 62 | + hasher := newRecordingHasher() |
| 63 | + txsRoot := types.DeriveSha(types.Transactions(txes), hasher) |
| 64 | + header := &types.Header{ |
| 65 | + TxHash: txsRoot, |
| 66 | + } |
| 67 | + preimages := hasher.GetPreimages() |
| 68 | + mockPreimageResolver := &mockPreimageResolver{ |
| 69 | + preimages: preimages, |
| 70 | + } |
| 71 | + txsFetcher := &txsFetcherForBlock{ |
| 72 | + header: header, |
| 73 | + preimageResolver: mockPreimageResolver, |
| 74 | + } |
| 75 | + fetched, err := txsFetcher.TransactionsByHeader(ctx, header.Hash()) |
| 76 | + require.NoError(t, err) |
| 77 | + require.True(t, uint64(len(fetched)) == total) // #nosec G115 |
| 78 | + for i, tx := range fetched { |
| 79 | + require.Equal(t, txes[i].Hash(), tx.Hash()) |
| 80 | + require.Equal(t, uint64(i), tx.Nonce()) // #nosec G115 |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +type mockPreimageResolver struct { |
| 85 | + preimages map[common.Hash][]byte |
| 86 | +} |
| 87 | + |
| 88 | +func (m *mockPreimageResolver) ResolveTypedPreimage(preimageType arbutil.PreimageType, hash common.Hash) ([]byte, error) { |
| 89 | + if preimage, exists := m.preimages[hash]; exists { |
| 90 | + return preimage, nil |
| 91 | + } |
| 92 | + return nil, fmt.Errorf("preimage not found for hash: %s", hash.Hex()) |
| 93 | +} |
| 94 | + |
| 95 | +// Implements a hasher that captures preimages of hashes as it computes them. |
| 96 | +type preimageRecordingHasher struct { |
| 97 | + trie *trie.StackTrie |
| 98 | + preimages map[common.Hash][]byte |
| 99 | +} |
| 100 | + |
| 101 | +func newRecordingHasher() *preimageRecordingHasher { |
| 102 | + h := &preimageRecordingHasher{ |
| 103 | + preimages: make(map[common.Hash][]byte), |
| 104 | + } |
| 105 | + // OnTrieNode callback captures all trie nodes. |
| 106 | + onTrieNode := func(path []byte, hash common.Hash, blob []byte) { |
| 107 | + // Deep copy the blob since the callback warns contents may change, so this is required. |
| 108 | + h.preimages[hash] = common.CopyBytes(blob) |
| 109 | + } |
| 110 | + |
| 111 | + h.trie = trie.NewStackTrie(onTrieNode) |
| 112 | + return h |
| 113 | +} |
| 114 | + |
| 115 | +func (h *preimageRecordingHasher) Reset() { |
| 116 | + onTrieNode := func(path []byte, hash common.Hash, blob []byte) { |
| 117 | + h.preimages[hash] = common.CopyBytes(blob) |
| 118 | + } |
| 119 | + h.trie = trie.NewStackTrie(onTrieNode) |
| 120 | +} |
| 121 | + |
| 122 | +func (h *preimageRecordingHasher) Update(key, value []byte) error { |
| 123 | + valueHash := crypto.Keccak256Hash(value) |
| 124 | + h.preimages[valueHash] = common.CopyBytes(value) |
| 125 | + return h.trie.Update(key, value) |
| 126 | +} |
| 127 | + |
| 128 | +func (h *preimageRecordingHasher) Hash() common.Hash { |
| 129 | + return h.trie.Hash() |
| 130 | +} |
| 131 | + |
| 132 | +func (h *preimageRecordingHasher) GetPreimages() map[common.Hash][]byte { |
| 133 | + return h.preimages |
| 134 | +} |
0 commit comments