@@ -76,25 +76,63 @@ describe('TrieProof', function () {
7676 }
7777 } ) ;
7878
79- describe ( 'verify' , function ( ) {
80- it ( 'returns true for a valid proof with leaf' , async function ( ) {
79+ describe ( 'verify proof ' , function ( ) {
80+ it ( 'returns true with proof size 1 (even leaf [0x20]) ' , async function ( ) {
8181 const slot = ethers . ZeroHash ;
82- const tx = await call ( this . storage , 'setUint256Slot' , [ slot , 42 ] ) ;
83- const { key, value, proof, storageHash } = await this . getProof ( this . storage , slot , tx ) ;
82+ await call ( this . storage , 'setUint256Slot' , [ slot , 42 ] ) ;
83+ const { key, value, proof, storageHash } = await this . getProof ( this . storage , slot ) ;
8484 const result = await this . mock . $verify ( ethers . keccak256 ( key ) , value , proof , storageHash ) ;
8585 expect ( result ) . is . true ;
8686 } ) ;
8787
88- it ( 'returns true for a valid proof with extension ' , async function ( ) {
88+ it ( 'returns true with proof size 2 (branch then odd leaf [0x3]) ' , async function ( ) {
8989 const slot0 = ethers . ZeroHash ;
9090 const slot1 = '0x0000000000000000000000000000000000000000000000000000000000000001' ;
9191 await call ( this . storage , 'setUint256Slot' , [ slot0 , 42 ] ) ;
92- const tx = await call ( this . storage , 'setUint256Slot' , [ slot1 , 43 ] ) ;
93- const { key, value, proof, storageHash } = await this . getProof ( this . storage , slot1 , tx ) ;
92+ await call ( this . storage , 'setUint256Slot' , [ slot1 , 43 ] ) ;
93+ const { key, value, proof, storageHash } = await this . getProof ( this . storage , slot1 ) ;
9494 const result = await this . mock . $verify ( ethers . keccak256 ( key ) , value , proof , storageHash ) ;
9595 expect ( result ) . is . true ;
9696 } ) ;
9797
98+ it ( 'returns true with proof size 3 (even extension [0x00], branch then leaf)' , async function ( ) {
99+ const slots = [
100+ '0x0000000000000000000000000000000000000000000000000000000000001889' , // 0xabc4243e220df4927f4d7b432d2d718dadbba652f6cee6a45bb90c077fa4e158
101+ '0x0000000000000000000000000000000000000000000000000000000000008b23' , // 0xabd5ef9a39144905d28bd8554745ebae050359cf7e89079f49b66a6c06bd2bf9
102+ '0x0000000000000000000000000000000000000000000000000000000000002383' , // 0xabe87cb73c1e15a89cfb0daa7fd0cc3eb1a762345fe15d668f5061a4900b22fa
103+ ] ;
104+ await call ( this . storage , 'setUint256Slot' , [ slots [ 0 ] , 42 ] ) ;
105+ await call ( this . storage , 'setUint256Slot' , [ slots [ 1 ] , 43 ] ) ;
106+ await call ( this . storage , 'setUint256Slot' , [ slots [ 2 ] , 44 ] ) ;
107+ for ( let slot of slots ) {
108+ const { key, value, proof, storageHash } = await this . getProof ( this . storage , slot ) ;
109+ const result = await this . mock . $verify ( ethers . keccak256 ( key ) , value , proof , storageHash ) ;
110+ expect ( result ) . is . true ;
111+ }
112+ } ) ;
113+
114+ it ( 'returns true with proof size 3 (odd extension [0x1], branch then leaf)' , async function ( ) {
115+ const slots = [
116+ '0x0000000000000000000000000000000000000000000000000000000000004616' , // 0xabcd2ce29d227a0aaaa2ea425df9d5c96a569b416fd0bb7e018b8c9ce9b9d15d
117+ '0x0000000000000000000000000000000000000000000000000000000000012dd3' , // 0xabce7718834e2932319fc4642268a27405261f7d3826b19811d044bf2b56ebb1
118+ '0x000000000000000000000000000000000000000000000000000000000000ce8f' , // 0xabcf8b375ce20d03da20a3f5efeb8f3666810beca66f729f995953f51559a4ff
119+ ] ;
120+ await call ( this . storage , 'setUint256Slot' , [ slots [ 0 ] , 42 ] ) ;
121+ await call ( this . storage , 'setUint256Slot' , [ slots [ 1 ] , 43 ] ) ;
122+ await call ( this . storage , 'setUint256Slot' , [ slots [ 2 ] , 44 ] ) ;
123+ for ( let slot of slots ) {
124+ const { key, value, proof, storageHash } = await this . getProof ( this . storage , slot ) ;
125+ const result = await this . mock . $verify ( ethers . keccak256 ( key ) , value , proof , storageHash ) ;
126+ expect ( result ) . is . true ;
127+ }
128+ } ) ;
129+
130+ it ( 'returns false for invalid proof' , async function ( ) {
131+ await expect ( this . mock . $verify ( '0x' , '0x' , [ ] , ethers . ZeroHash ) ) . to . eventually . be . false ;
132+ } ) ;
133+ } ) ;
134+
135+ describe ( 'process invalid proof' , function ( ) {
98136 it ( 'fails to process proof with empty key' , async function ( ) {
99137 const [ value , error ] = await this . mock . $processProof ( '0x' , [ ] , ethers . ZeroHash ) ;
100138 expect ( value ) . to . equal ( '0x' ) ;
@@ -116,8 +154,8 @@ describe('TrieProof', function () {
116154 const slot0 = ethers . ZeroHash ;
117155 const slot1 = '0x0000000000000000000000000000000000000000000000000000000000000001' ;
118156 await call ( this . storage , 'setUint256Slot' , [ slot0 , 42 ] ) ;
119- const tx = await call ( this . storage , 'setUint256Slot' , [ slot1 , 43 ] ) ;
120- const { key, proof, storageHash } = await this . getProof ( this . storage , slot1 , tx ) ;
157+ await call ( this . storage , 'setUint256Slot' , [ slot1 , 43 ] ) ;
158+ const { key, proof, storageHash } = await this . getProof ( this . storage , slot1 ) ;
121159 proof [ 1 ] = ethers . toBeHex ( BigInt ( proof [ 1 ] ) + 1n ) ; // Corrupt internal large node hash
122160 const [ processedValue , error ] = await this . mock . $processProof ( key , proof , storageHash ) ;
123161 expect ( processedValue ) . to . equal ( '0x' ) ;
@@ -127,7 +165,7 @@ describe('TrieProof', function () {
127165 it . skip ( 'fails to process proof with invalid internal short node' , async function ( ) { } ) ; // TODO: INVALID_INTERNAL_NODE_HASH
128166
129167 it ( 'fails to process proof with empty value' , async function ( ) {
130- const proof = [ ethers . encodeRlp ( [ '0x2000' , '0x' ] ) ] ; // Corrupt proof to yield empty value
168+ const proof = [ ethers . encodeRlp ( [ '0x2000' , '0x' ] ) ] ;
131169 const [ processedValue , error ] = await this . mock . $processProof ( '0x00' , proof , ethers . keccak256 ( proof [ 0 ] ) ) ;
132170 expect ( processedValue ) . to . equal ( '0x' ) ;
133171 expect ( error ) . to . equal ( ProofError . EMPTY_VALUE ) ;
@@ -159,7 +197,7 @@ describe('TrieProof', function () {
159197 } ) ;
160198
161199 it ( 'fails to process proof with invalid path remainder' , async function ( ) {
162- const proof = [ ethers . encodeRlp ( [ '0x0011' , '0x' ] ) ] ; // Corrupt proof to yield invalid path remainder
200+ const proof = [ ethers . encodeRlp ( [ '0x0011' , '0x' ] ) ] ;
163201 const [ processedValue , error ] = await this . mock . $processProof ( ethers . ZeroHash , proof , ethers . keccak256 ( proof [ 0 ] ) ) ;
164202 expect ( processedValue ) . to . equal ( '0x' ) ;
165203 expect ( error ) . to . equal ( ProofError . INVALID_PATH_REMAINDER ) ;
0 commit comments