Skip to content

Commit bbe7954

Browse files
committed
Secure trie shakey / key matching
1 parent d6da533 commit bbe7954

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

core/state/dump.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ func (self *StateDB) RawDump() World {
2828

2929
it := self.trie.Iterator()
3030
for it.Next() {
31-
stateObject := NewStateObjectFromBytes(common.BytesToAddress(it.Key), it.Value, self.db)
31+
addr := self.trie.GetKey(it.Key)
32+
stateObject := NewStateObjectFromBytes(common.BytesToAddress(addr), it.Value, self.db)
3233

3334
account := Account{Balance: stateObject.balance.String(), Nonce: stateObject.nonce, Root: common.Bytes2Hex(stateObject.Root()), CodeHash: common.Bytes2Hex(stateObject.codeHash)}
3435
account.Storage = make(map[string]string)
3536

3637
storageIt := stateObject.State.trie.Iterator()
3738
for storageIt.Next() {
38-
fmt.Println("value", storageIt.Value)
39-
account.Storage[common.Bytes2Hex(storageIt.Key)] = common.Bytes2Hex(storageIt.Value)
39+
account.Storage[common.Bytes2Hex(self.trie.GetKey(storageIt.Key))] = common.Bytes2Hex(storageIt.Value)
4040
}
41-
world.Accounts[common.Bytes2Hex(it.Key)] = account
41+
world.Accounts[common.Bytes2Hex(addr)] = account
4242
}
4343
return world
4444
}

trie/secure_trie.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package trie
22

33
import "github.com/ethereum/go-ethereum/crypto"
44

5+
var keyPrefix = []byte("secure-key-")
6+
57
type SecureTrie struct {
68
*Trie
79
}
@@ -11,7 +13,10 @@ func NewSecure(root []byte, backend Backend) *SecureTrie {
1113
}
1214

1315
func (self *SecureTrie) Update(key, value []byte) Node {
14-
return self.Trie.Update(crypto.Sha3(key), value)
16+
shaKey := crypto.Sha3(key)
17+
self.Trie.cache.Put(append(keyPrefix, shaKey...), key)
18+
19+
return self.Trie.Update(shaKey, value)
1520
}
1621
func (self *SecureTrie) UpdateString(key, value string) Node {
1722
return self.Update([]byte(key), []byte(value))
@@ -34,3 +39,7 @@ func (self *SecureTrie) DeleteString(key string) Node {
3439
func (self *SecureTrie) Copy() *SecureTrie {
3540
return &SecureTrie{self.Trie.Copy()}
3641
}
42+
43+
func (self *SecureTrie) GetKey(shaKey []byte) []byte {
44+
return self.Trie.cache.Get(append(keyPrefix, shaKey...))
45+
}

0 commit comments

Comments
 (0)