Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
shotasilagadzetaal committed Jan 6, 2025
1 parent 4e48fdc commit 970d2c1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 32 deletions.
6 changes: 3 additions & 3 deletions erigon-lib/commitment/hex_patricia_hashed.go
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,7 @@ func (hph *HexPatriciaHashed) GenerateProofTrie(ctx context.Context, updates *Up
logEvery = time.NewTicker(20 * time.Second)
)
defer logEvery.Stop()
var tries []*trie.Trie = make([]*trie.Trie, 0, len(updates.keys)) // slice of tries, i.e the witness for each key, these will be all merged into single trie
var tries []*trie.Trie = make([]*trie.Trie, 0, len(updates.keys)) // slice of tries, i.e tree for for each key/update, these will be all merged into single trie
err = updates.HashSort(ctx, func(hashedKey, plainKey []byte, stateUpdate *Update) error {
select {
case <-logEvery.C:
Expand Down Expand Up @@ -1936,7 +1936,7 @@ func (hph *HexPatriciaHashed) GenerateProofTrie(ctx context.Context, updates *Up
hph.PrintGrid()

// convert grid to trie.Trie
tr, err = hph.ToTrie(hashedKey, nil) // build witness trie for this key, based on the current state of the grid
tr, err = hph.ToTrie(hashedKey, nil) // build trie for this key, based on the current state of the grid
if err != nil {
return err
}
Expand Down Expand Up @@ -1982,7 +1982,7 @@ func (hph *HexPatriciaHashed) GenerateProofTrie(ctx context.Context, updates *Up
fmt.Printf("mergedTrieRootHash = %x\n", proofTrieRootHash)

if !bytes.Equal(proofTrieRootHash, expectedRootHash) {
return nil, nil, fmt.Errorf("root hash mismatch witnessTrieRootHash(%x)!=expectedRootHash(%x)", proofTrieRootHash, expectedRootHash)
return nil, nil, fmt.Errorf("root hash mismatch keyTrieRootHash(%x)!=expectedRootHash(%x)", proofTrieRootHash, expectedRootHash)
}

return proofTrie, rootHash, nil
Expand Down
24 changes: 15 additions & 9 deletions erigon-lib/trie/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,21 @@ func VerifyStorageProofByHash(storageRoot libcommon.Hash, keyHash libcommon.Hash
if proof.Value.ToInt().Sign() != 0 {
return errors.New("empty storage root cannot have non-zero values")
}
// The spec here is a bit unclear. The yellow paper makes it clear that the
// EmptyRoot hash is a special case where the trie is empty. Since the trie
// is empty there are no proof elements to collect. But, EIP-1186 also
// clearly states that the proof must be "starting with the
// storageHash-Node", which could imply an RLP encoded `[]byte(nil)` (the
// pre-image of the EmptyRoot) should be included. This implementation
// chooses to require the proof be empty.
if len(proof.Proof) > 0 {
return errors.New("empty storage root should not have proof nodes")
// if storage root is zero (0000000) then we should have an empty proof
// if it corresponds to empty storage tree, having value EmptyRoot above
// then proof should be RLP encoding of empty proof (0x80)
if storageRoot == EmptyRoot {
for i, _ := range proof.Proof {
if len(proof.Proof[i]) != 1 || proof.Proof[i][0] != 0x80 {
return errors.New("empty storage root should have RLP encoding of empty proof")
}
}
} else {
for i, _ := range proof.Proof {
if len(proof.Proof[i]) != 0 {
return errors.New("zero storage root should have empty proof")
}
}
}
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions turbo/jsonrpc/eth_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,10 @@ func (api *APIImpl) getProof(ctx context.Context, address libcommon.Address, sto
// set key proof
proof.StorageProof[i].Key = keyHash
proof.StorageProof[i].Value = (*hexutil.Big)(unsafe.Pointer(n))
if proof.StorageHash.Cmp(libcommon.Hash{}) == 0 {
proof.StorageProof[i].Proof = nil
continue
}
proof.StorageProof[i].Proof = []hexutility.Bytes{[]byte{0x80}}
if storageProof != nil && len(storageProof) != 0 {
proof.StorageProof[i].Proof = *(*[]hexutility.Bytes)(unsafe.Pointer(&storageProof))
Expand Down
20 changes: 0 additions & 20 deletions turbo/jsonrpc/eth_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,26 +162,6 @@ func TestGetProof(t *testing.T) {
blockNum: 3,
stateVal: 0,
},
{
name: "currentBlockNoAccountMissingState",
addr: libcommon.HexToAddress("0xdeaddeaddeaddeaddeaddeaddeaddeaddeaddead0"),
storageKeys: []libcommon.Hash{libcommon.HexToHash("0xdeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddeaddead")},
blockNum: 3,
stateVal: 0,
},
{
name: "olderBlockWithState",
addr: contractAddr,
blockNum: 2,
storageKeys: []libcommon.Hash{key(1), key(5), key(9), key(13)},
stateVal: 1,
},
{
name: "tooOldBlock",
addr: contractAddr,
blockNum: 1,
expectedErr: "requested block is too old, block must be within 1 blocks of the head block number (currently 3)",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 970d2c1

Please sign in to comment.