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

Commit

Permalink
PreventHashing split into storage/account PreventHashing
Browse files Browse the repository at this point in the history
  • Loading branch information
miha-stopar committed Mar 13, 2024
1 parent 06fb0ca commit 3195338
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
3 changes: 2 additions & 1 deletion geth-utils/gethutil/mpt/oracle/prefetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ var RemoteUrl = "https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161"
var LocalUrl = "http://localhost:8545"

// For generating special tests for MPT circuit:
var PreventHashingInSecureTrie = false
var PreventHashingInSecureTrie = false // storage
var AccountPreventHashingInSecureTrie = false

func toFilename(key string) string {
return fmt.Sprintf("/tmp/eth/json_%s", key)
Expand Down
4 changes: 2 additions & 2 deletions geth-utils/gethutil/mpt/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (db *Database) CopyTrie(t Trie) Trie {

// OpenTrie opens the main account trie at a specific root hash.
func (db *Database) OpenTrie(root common.Hash) (Trie, error) {
tr, err := trie.NewSecure(root, db.db)
tr, err := trie.NewSecure(root, db.db, false)
if err != nil {
return nil, err
}
Expand All @@ -62,7 +62,7 @@ func (db *Database) OpenTrie(root common.Hash) (Trie, error) {
// OpenStorageTrie opens the storage trie of an account.
func (db *Database) OpenStorageTrie(addrHash, root common.Hash) (Trie, error) {
//return SimpleTrie{db.BlockNumber, root, true, addrHash}, nil
tr, err := trie.NewSecure(root, db.db)
tr, err := trie.NewSecure(root, db.db, true)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions geth-utils/gethutil/mpt/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ func (s *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash {

// GetProof returns the Merkle proof for a given account.
func (s *StateDB) GetProof(addr common.Address) ([][]byte, []byte, [][]byte, bool, bool, error) {
var newAddr common.Hash
if oracle.PreventHashingInSecureTrie {
newAddr := crypto.Keccak256Hash(addr.Bytes())
if oracle.AccountPreventHashingInSecureTrie {
bytes := append(addr.Bytes(), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}...)
newAddr = common.BytesToHash(bytes)
}
Expand All @@ -317,7 +317,7 @@ func (s *StateDB) GetProofByHash(addrHash common.Hash) ([][]byte, []byte, [][]by
func (s *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, []byte, [][]byte, bool, bool, error) {
var proof proofList
newAddr := a
if oracle.PreventHashingInSecureTrie {
if oracle.AccountPreventHashingInSecureTrie {
bytes := append(a.Bytes(), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}...)
newAddr = common.BytesToAddress(bytes)
}
Expand Down Expand Up @@ -545,7 +545,7 @@ func (s *StateDB) updateStateObject(obj *stateObject) {
panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err))
}

if !oracle.PreventHashingInSecureTrie {
if !oracle.AccountPreventHashingInSecureTrie {
if err = s.trie.TryUpdateAlwaysHash(addr[:], data); err != nil {
s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err))
}
Expand Down
8 changes: 5 additions & 3 deletions geth-utils/gethutil/mpt/trie/secure_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type SecureTrie struct {
hashKeyBuf [common.HashLength]byte
secKeyCache map[string][]byte
secKeyCacheOwner *SecureTrie // Pointer to self, replace the key cache on mismatch
isStorageTrie bool
}

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

// Get returns the value for key stored in the trie.
Expand Down Expand Up @@ -202,7 +203,8 @@ func (t *SecureTrie) NodeIterator(start []byte) NodeIterator {
// The caller must not hold onto the return value because it will become
// invalid on the next call to hashKey or secKey.
func (t *SecureTrie) hashKey(key []byte) []byte {
if !oracle.PreventHashingInSecureTrie {
preventHashing := (oracle.PreventHashingInSecureTrie && t.isStorageTrie) || (oracle.AccountPreventHashingInSecureTrie && !t.isStorageTrie)
if !preventHashing {
h := NewHasher(false)
h.sha.Reset()
h.sha.Write(key)
Expand Down
2 changes: 1 addition & 1 deletion geth-utils/gethutil/mpt/witness/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ type Node struct {

func GetStartNode(proofType string, sRoot, cRoot common.Hash, specialTest byte) Node {
s := StartNode{
DisablePreimageCheck: oracle.PreventHashingInSecureTrie || specialTest == 5,
DisablePreimageCheck: oracle.PreventHashingInSecureTrie || oracle.AccountPreventHashingInSecureTrie || specialTest == 5,
ProofType: proofType,
}
var values [][]byte
Expand Down
4 changes: 2 additions & 2 deletions geth-utils/gethutil/mpt/witness/prepare_witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func obtainAccountProofAndConvertToWitness(tMod TrieModification, statedb *state

addr := tMod.Address
addrh := crypto.Keccak256(addr.Bytes())
if oracle.PreventHashingInSecureTrie {
if oracle.AccountPreventHashingInSecureTrie {
addrh = addr.Bytes()
addrh = append(addrh, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}...)
addr = common.BytesToAddress(addrh)
Expand Down Expand Up @@ -180,7 +180,7 @@ func obtainTwoProofsAndConvertToWitness(trieModifications []TrieModification, st

addr := tMod.Address
addrh := crypto.Keccak256(addr.Bytes())
if oracle.PreventHashingInSecureTrie {
if oracle.AccountPreventHashingInSecureTrie {
addrh = addr.Bytes()
addrh = append(addrh, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}...)
addr = common.BytesToAddress(addrh)
Expand Down

0 comments on commit 3195338

Please sign in to comment.