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

Commit 06fb0ca

Browse files
committed
Fix in account address hashing prevention
1 parent 87724d3 commit 06fb0ca

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ func (t *SecureTrie) Update(key, value []byte) {
109109
// If a node was not found in the database, a MissingNodeError is returned.
110110
func (t *SecureTrie) TryUpdate(key, value []byte) error {
111111
hk := t.hashKey(key)
112-
if oracle.PreventHashingInSecureTrie {
113-
hk = append(hk, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}...)
114-
}
115112
err := t.trie.TryUpdate(hk, value)
116113
if err != nil {
117114
return err
@@ -214,6 +211,9 @@ func (t *SecureTrie) hashKey(key []byte) []byte {
214211
return t.hashKeyBuf[:]
215212
} else {
216213
// For generating special tests for MPT circuit.
214+
if len(key) < 32 { // accounts
215+
key = append(key, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}...)
216+
}
217217
return key
218218
}
219219
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,7 @@ func TestStorageWrongExtensionNode(t *testing.T) {
23472347
oracle.PreventHashingInSecureTrie = true // to store the unchanged key
23482348

23492349
statedb.CreateAccount(addr)
2350+
statedb.IntermediateRoot(false)
23502351

23512352
key1 := common.HexToHash("0x1230000000000000000000000000000000000000000000000000000000000000")
23522353
key2 := common.HexToHash("0x1231000000000000000000000000000000000000000000000000000000000000")

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ func GetWitness(nodeUrl string, blockNum int, trieModifications []TrieModificati
6565
return obtainTwoProofsAndConvertToWitness(trieModifications, statedb, 0)
6666
}
6767

68-
func obtainAccountProofAndConvertToWitness(i int, tMod TrieModification, tModsLen int, statedb *state.StateDB, specialTest byte) []Node {
68+
func obtainAccountProofAndConvertToWitness(tMod TrieModification, statedb *state.StateDB, specialTest byte) []Node {
6969
statedb.IntermediateRoot(false)
7070

7171
addr := tMod.Address
7272
addrh := crypto.Keccak256(addr.Bytes())
7373
if oracle.PreventHashingInSecureTrie {
7474
addrh = addr.Bytes()
75-
addrh = append(addrh, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}...)
75+
addrh = append(addrh, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}...)
76+
addr = common.BytesToAddress(addrh)
7677
}
7778
accountAddr := trie.KeybytesToHex(addrh)
7879

@@ -82,7 +83,7 @@ func obtainAccountProofAndConvertToWitness(i int, tMod TrieModification, tModsLe
8283
// for cases when statedb.loadRemoteAccountsIntoStateObjects = false.
8384
statedb.SetStateObjectIfExists(tMod.Address)
8485

85-
oracle.PrefetchAccount(statedb.Db.BlockNumber, tMod.Address, nil)
86+
oracle.PrefetchAccount(statedb.Db.BlockNumber, addr, nil)
8687
accountProof, aNeighbourNode1, aExtNibbles1, isLastLeaf1, aIsNeighbourNodeHashed1, err := statedb.GetProof(addr)
8788
check(err)
8889

@@ -182,10 +183,11 @@ func obtainTwoProofsAndConvertToWitness(trieModifications []TrieModification, st
182183
if oracle.PreventHashingInSecureTrie {
183184
addrh = addr.Bytes()
184185
addrh = append(addrh, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}...)
186+
addr = common.BytesToAddress(addrh)
185187
}
186188
accountAddr := trie.KeybytesToHex(addrh)
187189

188-
oracle.PrefetchAccount(statedb.Db.BlockNumber, tMod.Address, nil)
190+
oracle.PrefetchAccount(statedb.Db.BlockNumber, addr, nil)
189191
oracle.PrefetchStorage(statedb.Db.BlockNumber, addr, tMod.Key, nil)
190192

191193
if specialTest == 1 {
@@ -282,7 +284,7 @@ func obtainTwoProofsAndConvertToWitness(trieModifications []TrieModification, st
282284
nodes = append(nodes, nodesStorage...)
283285
nodes = append(nodes, GetEndNode())
284286
} else {
285-
accountNodes := obtainAccountProofAndConvertToWitness(i, tMod, len(trieModifications), statedb, specialTest)
287+
accountNodes := obtainAccountProofAndConvertToWitness(tMod, statedb, specialTest)
286288
nodes = append(nodes, accountNodes...)
287289
}
288290
}

0 commit comments

Comments
 (0)