|
| 1 | +const { ethers } = require('hardhat'); |
| 2 | +const { expect } = require('chai'); |
| 3 | +const { spawn } = require('child_process'); |
| 4 | + |
| 5 | +const anvilPort = 8546; |
| 6 | +const ProofError = { |
| 7 | + NO_ERROR: 0, |
| 8 | + EMPTY_KEY: 1, |
| 9 | + INDEX_OUT_OF_BOUNDS: 2, |
| 10 | + INVALID_ROOT_HASH: 3, |
| 11 | + INVALID_LARGE_INTERNAL_HASH: 4, |
| 12 | + INVALID_INTERNAL_NODE_HASH: 5, |
| 13 | + EMPTY_VALUE: 6, |
| 14 | + INVALID_EXTRA_PROOF_ELEMENT: 7, |
| 15 | + INVALID_PATH_REMAINDER: 8, |
| 16 | + INVALID_KEY_REMAINDER: 9, |
| 17 | + UNKNOWN_NODE_PREFIX: 10, |
| 18 | + UNPARSEABLE_NODE: 11, |
| 19 | + INVALID_PROOF: 12, |
| 20 | +}; |
| 21 | + |
| 22 | +async function fixture() { |
| 23 | + const anvil = spawn('anvil', ['--port', anvilPort], { |
| 24 | + timeout: 30000, |
| 25 | + }); // Method eth_getProof is not supported with default Hardhat Network |
| 26 | + await new Promise(resolve => { |
| 27 | + anvil.stdout.once('data', resolve); |
| 28 | + }); |
| 29 | + if (process.env.ANVIL_LOGS === 'true') { |
| 30 | + anvil.stdout.on('data', function (data) { |
| 31 | + console.log(data.toString()); |
| 32 | + }); |
| 33 | + } |
| 34 | + const provider = new ethers.JsonRpcProvider(`http://localhost:${anvilPort}`); |
| 35 | + const account = await provider.getSigner(0); |
| 36 | + const mock = (await ethers.deployContract('$TrieProof', account)).connect(account); |
| 37 | + const storage = (await ethers.deployContract('StorageSlotMock', account)).connect(account); |
| 38 | + return { |
| 39 | + anvil, |
| 40 | + provider, |
| 41 | + mock, |
| 42 | + storage, |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +describe('TrieProof', function () { |
| 47 | + beforeEach(async function () { |
| 48 | + Object.assign(this, await fixture()); |
| 49 | + }); |
| 50 | + |
| 51 | + afterEach(async function () { |
| 52 | + this.anvil.kill(); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('verify', function () { |
| 56 | + it('returns true for a valid proof with leaf', async function () { |
| 57 | + const slot = ethers.ZeroHash; |
| 58 | + const tx = await this.storage.setUint256Slot(slot, 42); |
| 59 | + const response = await this.provider.send('eth_getProof', [ |
| 60 | + this.storage.target, |
| 61 | + [slot], |
| 62 | + ethers.toBeHex(tx.blockNumber), |
| 63 | + ]); |
| 64 | + const { storageHash, storageProof } = response; |
| 65 | + const { key, value, proof } = storageProof[0]; |
| 66 | + const result = await this.mock.$verify(key, value, proof, storageHash); |
| 67 | + expect(result).is.true; |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns true for a valid proof with extension', async function () { |
| 71 | + const slot0 = ethers.ZeroHash; |
| 72 | + const slot1 = '0x0000000000000000000000000000000000000000000000000000000000000001'; |
| 73 | + await this.storage.setUint256Slot(slot0, 42); |
| 74 | + const tx = await this.storage.setUint256Slot(slot1, 43); |
| 75 | + const response = await this.provider.send('eth_getProof', [ |
| 76 | + this.storage.target, |
| 77 | + [slot1], |
| 78 | + ethers.toBeHex(tx.blockNumber), |
| 79 | + ]); |
| 80 | + const { storageHash, storageProof } = response; |
| 81 | + const { key, value, proof } = storageProof[0]; |
| 82 | + const result = await this.mock.$verify(key, value, proof, storageHash); |
| 83 | + expect(result).is.true; |
| 84 | + }); |
| 85 | + |
| 86 | + it('fails to process proof with empty key', async function () { |
| 87 | + const [value, error] = await this.mock.$processProof('0x', [], ethers.ZeroHash); |
| 88 | + expect(value).to.equal('0x'); |
| 89 | + expect(error).to.equal(ProofError.EMPTY_KEY); |
| 90 | + }); |
| 91 | + |
| 92 | + it.skip('fails to process proof with key index out of bounds', async function () {}); // TODO: INDEX_OUT_OF_BOUNDS |
| 93 | + |
| 94 | + it('fails to process proof with invalid root hash', async function () { |
| 95 | + const slot = ethers.ZeroHash; |
| 96 | + const tx = await this.storage.setUint256Slot(slot, 42); |
| 97 | + const { storageHash, storageProof } = await this.provider.send('eth_getProof', [ |
| 98 | + this.storage.target, |
| 99 | + [slot], |
| 100 | + ethers.toBeHex(tx.blockNumber), |
| 101 | + ]); |
| 102 | + const { key, proof } = storageProof[0]; |
| 103 | + const [processedValue, error] = await this.mock.$processProof(key, proof, ethers.keccak256(storageHash)); // Corrupt root hash |
| 104 | + expect(processedValue).to.equal('0x'); |
| 105 | + expect(error).to.equal(ProofError.INVALID_ROOT_HASH); |
| 106 | + }); |
| 107 | + |
| 108 | + it('fails to process proof with invalid internal large hash', async function () { |
| 109 | + const slot0 = ethers.ZeroHash; |
| 110 | + const slot1 = '0x0000000000000000000000000000000000000000000000000000000000000001'; |
| 111 | + await this.storage.setUint256Slot(slot0, 42); |
| 112 | + const tx = await this.storage.setUint256Slot(slot1, 43); |
| 113 | + const { storageHash, storageProof } = await this.provider.send('eth_getProof', [ |
| 114 | + this.storage.target, |
| 115 | + [slot1], |
| 116 | + ethers.toBeHex(tx.blockNumber), |
| 117 | + ]); |
| 118 | + const { key, proof } = storageProof[0]; |
| 119 | + proof[1] = ethers.toBeHex(BigInt(proof[1]) + 1n); // Corrupt internal large node hash |
| 120 | + const [processedValue, error] = await this.mock.$processProof(key, proof, storageHash); |
| 121 | + expect(processedValue).to.equal('0x'); |
| 122 | + expect(error).to.equal(ProofError.INVALID_LARGE_INTERNAL_HASH); |
| 123 | + }); |
| 124 | + |
| 125 | + it.skip('fails to process proof with invalid internal short node', async function () {}); // TODO: INVALID_INTERNAL_NODE_HASH |
| 126 | + |
| 127 | + it('fails to process proof with empty value', async function () { |
| 128 | + const proof = [ethers.encodeRlp(['0x20', '0x'])]; // Corrupt proof to yield empty value |
| 129 | + const [processedValue, error] = await this.mock.$processProof('0x00', proof, ethers.keccak256(proof[0])); |
| 130 | + expect(processedValue).to.equal('0x'); |
| 131 | + expect(error).to.equal(ProofError.EMPTY_VALUE); |
| 132 | + }); |
| 133 | + |
| 134 | + it('fails to process proof with invalid extra proof', async function () { |
| 135 | + const slot0 = ethers.ZeroHash; |
| 136 | + const tx = await this.storage.setUint256Slot(slot0, 42); |
| 137 | + const { storageHash, storageProof } = await this.provider.send('eth_getProof', [ |
| 138 | + this.storage.target, |
| 139 | + [slot0], |
| 140 | + ethers.toBeHex(tx.blockNumber), |
| 141 | + ]); |
| 142 | + const { key, proof } = storageProof[0]; |
| 143 | + proof[1] = ethers.encodeRlp([]); // extra proof element |
| 144 | + const [processedValue, error] = await this.mock.$processProof(key, proof, storageHash); |
| 145 | + expect(processedValue).to.equal('0x'); |
| 146 | + expect(error).to.equal(ProofError.INVALID_EXTRA_PROOF_ELEMENT); |
| 147 | + }); |
| 148 | + |
| 149 | + it('fails to process proof with invalid path remainder', async function () { |
| 150 | + const proof = [ethers.encodeRlp(['0x0011', '0x'])]; // Corrupt proof to yield invalid path remainder |
| 151 | + const [processedValue, error] = await this.mock.$processProof(ethers.ZeroHash, proof, ethers.keccak256(proof[0])); |
| 152 | + expect(processedValue).to.equal('0x'); |
| 153 | + expect(error).to.equal(ProofError.INVALID_PATH_REMAINDER); |
| 154 | + }); |
| 155 | + |
| 156 | + it.skip('fails to process proof with invalid key remainder', async function () {}); // TODO: INVALID_KEY_REMAINDER |
| 157 | + |
| 158 | + it('fails to process proof with unknown node prefix', async function () { |
| 159 | + const proof = [ethers.encodeRlp(['0x40', '0x'])]; |
| 160 | + const [processedValue, error] = await this.mock.$processProof('0x00', proof, ethers.keccak256(proof[0])); |
| 161 | + expect(processedValue).to.equal('0x'); |
| 162 | + expect(error).to.equal(ProofError.UNKNOWN_NODE_PREFIX); |
| 163 | + }); |
| 164 | + |
| 165 | + it('fails to process proof with unparsable node', async function () { |
| 166 | + const proof = [ethers.encodeRlp(['0x00', '0x00', '0x00'])]; |
| 167 | + const [processedValue, error] = await this.mock.$processProof('0x00', proof, ethers.keccak256(proof[0])); |
| 168 | + expect(processedValue).to.equal('0x'); |
| 169 | + expect(error).to.equal(ProofError.UNPARSEABLE_NODE); |
| 170 | + }); |
| 171 | + |
| 172 | + it('fails to process proof with invalid proof', async function () { |
| 173 | + const [processedValue, error] = await this.mock.$processProof('0x00', [], ethers.ZeroHash); |
| 174 | + expect(processedValue).to.equal('0x'); |
| 175 | + expect(error).to.equal(ProofError.INVALID_PROOF); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments