Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.

Commit 3195338

Browse files
committed
PreventHashing split into storage/account PreventHashing
1 parent 06fb0ca commit 3195338

File tree

6 files changed

+16
-13
lines changed

6 files changed

+16
-13
lines changed

geth-utils/gethutil/mpt/oracle/prefetch.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ var RemoteUrl = "https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161"
8181
var LocalUrl = "http://localhost:8545"
8282

8383
// For generating special tests for MPT circuit:
84-
var PreventHashingInSecureTrie = false
84+
var PreventHashingInSecureTrie = false // storage
85+
var AccountPreventHashingInSecureTrie = false
8586

8687
func toFilename(key string) string {
8788
return fmt.Sprintf("/tmp/eth/json_%s", key)

geth-utils/gethutil/mpt/state/database.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (db *Database) CopyTrie(t Trie) Trie {
5252

5353
// OpenTrie opens the main account trie at a specific root hash.
5454
func (db *Database) OpenTrie(root common.Hash) (Trie, error) {
55-
tr, err := trie.NewSecure(root, db.db)
55+
tr, err := trie.NewSecure(root, db.db, false)
5656
if err != nil {
5757
return nil, err
5858
}
@@ -62,7 +62,7 @@ func (db *Database) OpenTrie(root common.Hash) (Trie, error) {
6262
// OpenStorageTrie opens the storage trie of an account.
6363
func (db *Database) OpenStorageTrie(addrHash, root common.Hash) (Trie, error) {
6464
//return SimpleTrie{db.BlockNumber, root, true, addrHash}, nil
65-
tr, err := trie.NewSecure(root, db.db)
65+
tr, err := trie.NewSecure(root, db.db, true)
6666
if err != nil {
6767
return nil, err
6868
}

geth-utils/gethutil/mpt/state/statedb.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ func (s *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash {
298298

299299
// GetProof returns the Merkle proof for a given account.
300300
func (s *StateDB) GetProof(addr common.Address) ([][]byte, []byte, [][]byte, bool, bool, error) {
301-
var newAddr common.Hash
302-
if oracle.PreventHashingInSecureTrie {
301+
newAddr := crypto.Keccak256Hash(addr.Bytes())
302+
if oracle.AccountPreventHashingInSecureTrie {
303303
bytes := append(addr.Bytes(), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}...)
304304
newAddr = common.BytesToHash(bytes)
305305
}
@@ -317,7 +317,7 @@ func (s *StateDB) GetProofByHash(addrHash common.Hash) ([][]byte, []byte, [][]by
317317
func (s *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, []byte, [][]byte, bool, bool, error) {
318318
var proof proofList
319319
newAddr := a
320-
if oracle.PreventHashingInSecureTrie {
320+
if oracle.AccountPreventHashingInSecureTrie {
321321
bytes := append(a.Bytes(), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}...)
322322
newAddr = common.BytesToAddress(bytes)
323323
}
@@ -545,7 +545,7 @@ func (s *StateDB) updateStateObject(obj *stateObject) {
545545
panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err))
546546
}
547547

548-
if !oracle.PreventHashingInSecureTrie {
548+
if !oracle.AccountPreventHashingInSecureTrie {
549549
if err = s.trie.TryUpdateAlwaysHash(addr[:], data); err != nil {
550550
s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err))
551551
}

geth-utils/gethutil/mpt/trie/secure_trie.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type SecureTrie struct {
4040
hashKeyBuf [common.HashLength]byte
4141
secKeyCache map[string][]byte
4242
secKeyCacheOwner *SecureTrie // Pointer to self, replace the key cache on mismatch
43+
isStorageTrie bool
4344
}
4445

4546
// NewSecure creates a trie with an existing root node from a backing database
@@ -53,15 +54,15 @@ type SecureTrie struct {
5354
// Loaded nodes are kept around until their 'cache generation' expires.
5455
// A new cache generation is created by each call to Commit.
5556
// cachelimit sets the number of past cache generations to keep.
56-
func NewSecure(root common.Hash, db *Database) (*SecureTrie, error) {
57+
func NewSecure(root common.Hash, db *Database, isStorageTrie bool) (*SecureTrie, error) {
5758
if db == nil {
5859
panic("trie.NewSecure called without a database")
5960
}
6061
trie, err := New(root, db)
6162
if err != nil {
6263
return nil, err
6364
}
64-
return &SecureTrie{trie: *trie}, nil
65+
return &SecureTrie{trie: *trie, isStorageTrie: isStorageTrie}, nil
6566
}
6667

6768
// Get returns the value for key stored in the trie.
@@ -202,7 +203,8 @@ func (t *SecureTrie) NodeIterator(start []byte) NodeIterator {
202203
// The caller must not hold onto the return value because it will become
203204
// invalid on the next call to hashKey or secKey.
204205
func (t *SecureTrie) hashKey(key []byte) []byte {
205-
if !oracle.PreventHashingInSecureTrie {
206+
preventHashing := (oracle.PreventHashingInSecureTrie && t.isStorageTrie) || (oracle.AccountPreventHashingInSecureTrie && !t.isStorageTrie)
207+
if !preventHashing {
206208
h := NewHasher(false)
207209
h.sha.Reset()
208210
h.sha.Write(key)

geth-utils/gethutil/mpt/witness/nodes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ type Node struct {
180180

181181
func GetStartNode(proofType string, sRoot, cRoot common.Hash, specialTest byte) Node {
182182
s := StartNode{
183-
DisablePreimageCheck: oracle.PreventHashingInSecureTrie || specialTest == 5,
183+
DisablePreimageCheck: oracle.PreventHashingInSecureTrie || oracle.AccountPreventHashingInSecureTrie || specialTest == 5,
184184
ProofType: proofType,
185185
}
186186
var values [][]byte

geth-utils/gethutil/mpt/witness/prepare_witness.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func obtainAccountProofAndConvertToWitness(tMod TrieModification, statedb *state
7070

7171
addr := tMod.Address
7272
addrh := crypto.Keccak256(addr.Bytes())
73-
if oracle.PreventHashingInSecureTrie {
73+
if oracle.AccountPreventHashingInSecureTrie {
7474
addrh = addr.Bytes()
7575
addrh = append(addrh, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}...)
7676
addr = common.BytesToAddress(addrh)
@@ -180,7 +180,7 @@ func obtainTwoProofsAndConvertToWitness(trieModifications []TrieModification, st
180180

181181
addr := tMod.Address
182182
addrh := crypto.Keccak256(addr.Bytes())
183-
if oracle.PreventHashingInSecureTrie {
183+
if oracle.AccountPreventHashingInSecureTrie {
184184
addrh = addr.Bytes()
185185
addrh = append(addrh, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}...)
186186
addr = common.BytesToAddress(addrh)

0 commit comments

Comments
 (0)