mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
use crypto.Keccak256
This commit is contained in:
parent
8af918d7ad
commit
2543c2ece1
1 changed files with 10 additions and 22 deletions
|
|
@ -19,6 +19,7 @@ package trie
|
||||||
import (
|
import (
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
"github.com/ethereum/go-ethereum/trie/trienode"
|
"github.com/ethereum/go-ethereum/trie/trienode"
|
||||||
"github.com/ethereum/go-ethereum/triedb/database"
|
"github.com/ethereum/go-ethereum/triedb/database"
|
||||||
|
|
@ -63,7 +64,6 @@ type StateTrie struct {
|
||||||
trie Trie
|
trie Trie
|
||||||
db database.NodeDatabase
|
db database.NodeDatabase
|
||||||
preimages preimageStore
|
preimages preimageStore
|
||||||
hashKeyBuf [common.HashLength]byte
|
|
||||||
secKeyCache map[common.Hash][]byte
|
secKeyCache map[common.Hash][]byte
|
||||||
secKeyCacheOwner *StateTrie // Pointer to self, replace the key cache on mismatch
|
secKeyCacheOwner *StateTrie // Pointer to self, replace the key cache on mismatch
|
||||||
}
|
}
|
||||||
|
|
@ -97,7 +97,7 @@ func NewStateTrie(id *ID, db database.NodeDatabase) (*StateTrie, error) {
|
||||||
// This function will omit any encountered error but just
|
// This function will omit any encountered error but just
|
||||||
// print out an error message.
|
// print out an error message.
|
||||||
func (t *StateTrie) MustGet(key []byte) []byte {
|
func (t *StateTrie) MustGet(key []byte) []byte {
|
||||||
return t.trie.MustGet(t.hashKey(key))
|
return t.trie.MustGet(crypto.Keccak256(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetStorage attempts to retrieve a storage slot with provided account address
|
// GetStorage attempts to retrieve a storage slot with provided account address
|
||||||
|
|
@ -105,7 +105,7 @@ func (t *StateTrie) MustGet(key []byte) []byte {
|
||||||
// If the specified storage slot is not in the trie, nil will be returned.
|
// If the specified storage slot is not in the trie, nil will be returned.
|
||||||
// If a trie node is not found in the database, a MissingNodeError is returned.
|
// If a trie node is not found in the database, a MissingNodeError is returned.
|
||||||
func (t *StateTrie) GetStorage(_ common.Address, key []byte) ([]byte, error) {
|
func (t *StateTrie) GetStorage(_ common.Address, key []byte) ([]byte, error) {
|
||||||
enc, err := t.trie.Get(t.hashKey(key))
|
enc, err := t.trie.Get(crypto.Keccak256(key))
|
||||||
if err != nil || len(enc) == 0 {
|
if err != nil || len(enc) == 0 {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -117,7 +117,7 @@ func (t *StateTrie) GetStorage(_ common.Address, key []byte) ([]byte, error) {
|
||||||
// If the specified account is not in the trie, nil will be returned.
|
// If the specified account is not in the trie, nil will be returned.
|
||||||
// If a trie node is not found in the database, a MissingNodeError is returned.
|
// If a trie node is not found in the database, a MissingNodeError is returned.
|
||||||
func (t *StateTrie) GetAccount(address common.Address) (*types.StateAccount, error) {
|
func (t *StateTrie) GetAccount(address common.Address) (*types.StateAccount, error) {
|
||||||
res, err := t.trie.Get(t.hashKey(address.Bytes()))
|
res, err := t.trie.Get(crypto.Keccak256(address.Bytes()))
|
||||||
if res == nil || err != nil {
|
if res == nil || err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -157,7 +157,7 @@ func (t *StateTrie) GetNode(path []byte) ([]byte, int, error) {
|
||||||
// This function will omit any encountered error but just print out an
|
// This function will omit any encountered error but just print out an
|
||||||
// error message.
|
// error message.
|
||||||
func (t *StateTrie) MustUpdate(key, value []byte) {
|
func (t *StateTrie) MustUpdate(key, value []byte) {
|
||||||
hk := t.hashKey(key)
|
hk := crypto.Keccak256(key)
|
||||||
t.trie.MustUpdate(hk, value)
|
t.trie.MustUpdate(hk, value)
|
||||||
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
|
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
|
||||||
}
|
}
|
||||||
|
|
@ -171,7 +171,7 @@ func (t *StateTrie) MustUpdate(key, value []byte) {
|
||||||
//
|
//
|
||||||
// If a node is not found in the database, a MissingNodeError is returned.
|
// If a node is not found in the database, a MissingNodeError is returned.
|
||||||
func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error {
|
func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error {
|
||||||
hk := t.hashKey(key)
|
hk := crypto.Keccak256(key)
|
||||||
v, _ := rlp.EncodeToBytes(value)
|
v, _ := rlp.EncodeToBytes(value)
|
||||||
err := t.trie.Update(hk, v)
|
err := t.trie.Update(hk, v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -183,7 +183,7 @@ func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error {
|
||||||
|
|
||||||
// UpdateAccount will abstract the write of an account to the secure trie.
|
// UpdateAccount will abstract the write of an account to the secure trie.
|
||||||
func (t *StateTrie) UpdateAccount(address common.Address, acc *types.StateAccount, _ int) error {
|
func (t *StateTrie) UpdateAccount(address common.Address, acc *types.StateAccount, _ int) error {
|
||||||
hk := t.hashKey(address.Bytes())
|
hk := crypto.Keccak256(address.Bytes())
|
||||||
data, err := rlp.EncodeToBytes(acc)
|
data, err := rlp.EncodeToBytes(acc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -202,7 +202,7 @@ func (t *StateTrie) UpdateContractCode(_ common.Address, _ common.Hash, _ []byte
|
||||||
// MustDelete removes any existing value for key from the trie. This function
|
// MustDelete removes any existing value for key from the trie. This function
|
||||||
// will omit any encountered error but just print out an error message.
|
// will omit any encountered error but just print out an error message.
|
||||||
func (t *StateTrie) MustDelete(key []byte) {
|
func (t *StateTrie) MustDelete(key []byte) {
|
||||||
hk := t.hashKey(key)
|
hk := crypto.Keccak256(key)
|
||||||
delete(t.getSecKeyCache(), common.Hash(hk))
|
delete(t.getSecKeyCache(), common.Hash(hk))
|
||||||
t.trie.MustDelete(hk)
|
t.trie.MustDelete(hk)
|
||||||
}
|
}
|
||||||
|
|
@ -211,14 +211,14 @@ func (t *StateTrie) MustDelete(key []byte) {
|
||||||
// If the specified trie node is not in the trie, nothing will be changed.
|
// If the specified trie node is not in the trie, nothing will be changed.
|
||||||
// If a node is not found in the database, a MissingNodeError is returned.
|
// If a node is not found in the database, a MissingNodeError is returned.
|
||||||
func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error {
|
func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error {
|
||||||
hk := t.hashKey(key)
|
hk := crypto.Keccak256(key)
|
||||||
delete(t.getSecKeyCache(), common.Hash(hk))
|
delete(t.getSecKeyCache(), common.Hash(hk))
|
||||||
return t.trie.Delete(hk)
|
return t.trie.Delete(hk)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAccount abstracts an account deletion from the trie.
|
// DeleteAccount abstracts an account deletion from the trie.
|
||||||
func (t *StateTrie) DeleteAccount(address common.Address) error {
|
func (t *StateTrie) DeleteAccount(address common.Address) error {
|
||||||
hk := t.hashKey(address.Bytes())
|
hk := crypto.Keccak256(address.Bytes())
|
||||||
delete(t.getSecKeyCache(), common.Hash(hk))
|
delete(t.getSecKeyCache(), common.Hash(hk))
|
||||||
return t.trie.Delete(hk)
|
return t.trie.Delete(hk)
|
||||||
}
|
}
|
||||||
|
|
@ -287,18 +287,6 @@ func (t *StateTrie) MustNodeIterator(start []byte) NodeIterator {
|
||||||
return t.trie.MustNodeIterator(start)
|
return t.trie.MustNodeIterator(start)
|
||||||
}
|
}
|
||||||
|
|
||||||
// hashKey returns the hash of key as an ephemeral buffer.
|
|
||||||
// The caller must not hold onto the return value because it will become
|
|
||||||
// invalid on the next call to hashKey or secKey.
|
|
||||||
func (t *StateTrie) hashKey(key []byte) []byte {
|
|
||||||
h := newHasher(false)
|
|
||||||
h.sha.Reset()
|
|
||||||
h.sha.Write(key)
|
|
||||||
h.sha.Read(t.hashKeyBuf[:])
|
|
||||||
returnHasherToPool(h)
|
|
||||||
return t.hashKeyBuf[:]
|
|
||||||
}
|
|
||||||
|
|
||||||
// getSecKeyCache returns the current secure key cache, creating a new one if
|
// getSecKeyCache returns the current secure key cache, creating a new one if
|
||||||
// ownership changed (i.e. the current secure trie is a copy of another owning
|
// ownership changed (i.e. the current secure trie is a copy of another owning
|
||||||
// the actual cache).
|
// the actual cache).
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue