|
1 | 1 | const { ethers } = require('hardhat'); |
2 | 2 | const { expect } = require('chai'); |
3 | 3 | const { spawn } = require('child_process'); |
4 | | -const { MerklePatriciaTrie, createMerkleProof } = require('@ethereumjs/mpt'); |
5 | 4 |
|
6 | 5 | const { Enum } = require('../../helpers/enums'); |
7 | 6 | const { zip } = require('../../helpers/iterate'); |
8 | 7 | const { generators } = require('../../helpers/random'); |
| 8 | +const { BlockTries } = require('../../helpers/trie'); |
9 | 9 | const { batchInBlock } = require('../../helpers/txpool'); |
10 | 10 |
|
11 | 11 | const ProofError = Enum( |
@@ -82,44 +82,25 @@ describe('TrieProof', function () { |
82 | 82 | false, |
83 | 83 | ]); |
84 | 84 |
|
85 | | - // Rebuild tries |
86 | | - const transactionTrie = new MerklePatriciaTrie(); |
87 | | - const receiptTrie = new MerklePatriciaTrie(); |
88 | | - |
89 | | - for (const tx of txs) { |
90 | | - const key = ethers.encodeRlp(ethers.stripZerosLeft(ethers.toBeHex(tx.index))); |
91 | | - |
92 | | - // Transaction |
93 | | - const encodedTransaction = await tx.getTransaction().then(tx => ethers.Transaction.from(tx).serialized); |
94 | | - await transactionTrie.put(ethers.getBytes(key), encodedTransaction); |
95 | | - |
96 | | - // Receipt |
97 | | - const encodedReceipt = ethers.concat([ |
98 | | - tx.type === 0 ? '0x' : ethers.toBeHex(tx.type), |
99 | | - ethers.encodeRlp([ |
100 | | - tx.status === 0 ? '0x' : '0x01', |
101 | | - ethers.toBeHex(tx.cumulativeGasUsed), |
102 | | - tx.logsBloom, |
103 | | - tx.logs.map(log => [log.address, log.topics, log.data]), |
104 | | - ]), |
105 | | - ]); |
106 | | - await receiptTrie.put(ethers.getBytes(key), encodedReceipt); |
107 | | - |
108 | | - Object.assign(tx, { key, encodedTransaction, encodedReceipt }); |
109 | | - } |
| 85 | + const blockTries = await this.provider.getBlock(txs.at(0).blockNumber).then(BlockTries.from); |
110 | 86 |
|
111 | 87 | // Sanity check trie roots |
112 | | - expect(ethers.hexlify(transactionTrie.root())).to.equal(transactionsRoot); |
113 | | - expect(ethers.hexlify(receiptTrie.root())).to.equal(receiptsRoot); |
| 88 | + expect(blockTries.transactionTrieRoot).to.equal(transactionsRoot); |
| 89 | + expect(blockTries.receiptTrieRoot).to.equal(receiptsRoot); |
114 | 90 |
|
115 | | - // Verify transaction inclusion in the block's transaction trie |
116 | | - for (const { key, encodedTransaction, encodedReceipt } of txs) { |
117 | | - const transactionProof = await createMerkleProof(transactionTrie, ethers.getBytes(key)); |
118 | | - await expect(this.mock.$verify(encodedTransaction, transactionsRoot, key, transactionProof)).to.eventually.be |
119 | | - .true; |
120 | | - |
121 | | - const receiptProof = await createMerkleProof(receiptTrie, ethers.getBytes(key)); |
122 | | - await expect(this.mock.$verify(encodedReceipt, receiptsRoot, key, receiptProof)).to.eventually.be.true; |
| 91 | + for (const tx of txs) { |
| 92 | + // verify transaction inclusion in the block's transaction trie |
| 93 | + const transaction = await tx.getTransaction().then(BlockTries.serializeTransaction); |
| 94 | + const transactionProof = await blockTries.getTransactionProof(tx.index); |
| 95 | + await expect( |
| 96 | + this.mock.$verify(transaction, transactionsRoot, BlockTries.indexToKey(tx.index), transactionProof), |
| 97 | + ).to.eventually.be.true; |
| 98 | + |
| 99 | + // verify receipt inclusion in the block's receipt trie |
| 100 | + const receipt = BlockTries.serializeReceipt(tx); |
| 101 | + const receiptProof = await blockTries.getReceiptProof(tx.index); |
| 102 | + await expect(this.mock.$verify(receipt, receiptsRoot, BlockTries.indexToKey(tx.index), receiptProof)).to |
| 103 | + .eventually.be.true; |
123 | 104 | } |
124 | 105 | }); |
125 | 106 |
|
|
0 commit comments