mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
reuse Keccak256Hash and Keccak256
This commit is contained in:
parent
277aa5b31a
commit
63c481259c
8 changed files with 22 additions and 24 deletions
|
|
@ -150,7 +150,7 @@ func HasTrieNode(db ethdb.KeyValueReader, owner common.Hash, path []byte, hash c
|
|||
if len(blob) == 0 {
|
||||
return false
|
||||
}
|
||||
return crypto.HashData(blob) == hash // exists but not match
|
||||
return crypto.Keccak256Hash(blob) == hash // exists but not match
|
||||
default:
|
||||
panic(fmt.Sprintf("Unknown scheme %v", scheme))
|
||||
}
|
||||
|
|
@ -172,7 +172,7 @@ func ReadTrieNode(db ethdb.KeyValueReader, owner common.Hash, path []byte, hash
|
|||
if len(blob) == 0 {
|
||||
return nil
|
||||
}
|
||||
if crypto.HashData(blob) != hash {
|
||||
if crypto.Keccak256Hash(blob) != hash {
|
||||
return nil // exists but not match
|
||||
}
|
||||
return blob
|
||||
|
|
|
|||
|
|
@ -408,8 +408,6 @@ func (s *stateObject) updateRoot() {
|
|||
// fulfills the storage diffs into the given accountUpdate struct.
|
||||
func (s *stateObject) commitStorage(op *accountUpdate) {
|
||||
var (
|
||||
hash common.Hash
|
||||
hasher = crypto.NewKeccakState()
|
||||
encode = func(val common.Hash) []byte {
|
||||
if val == (common.Hash{}) {
|
||||
return nil
|
||||
|
|
@ -426,9 +424,7 @@ func (s *stateObject) commitStorage(op *accountUpdate) {
|
|||
if val == s.originStorage[key] {
|
||||
continue
|
||||
}
|
||||
hasher.Reset()
|
||||
hasher.Write(key[:])
|
||||
hasher.Read(hash[:])
|
||||
hash := crypto.Keccak256Hash(key[:])
|
||||
if op.storages == nil {
|
||||
op.storages = make(map[common.Hash][]byte)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -580,7 +580,7 @@ func (s *StateDB) getStateObject(addr common.Address) *stateObject {
|
|||
var data *types.StateAccount
|
||||
if s.snap != nil {
|
||||
start := time.Now()
|
||||
acc, err := s.snap.Account(crypto.HashData(addr.Bytes()))
|
||||
acc, err := s.snap.Account(crypto.Keccak256Hash(addr.Bytes()))
|
||||
s.SnapshotAccountReads += time.Since(start)
|
||||
|
||||
if err == nil {
|
||||
|
|
@ -1069,7 +1069,7 @@ func (s *StateDB) handleDestruction() (map[common.Hash]*accountDelete, []*trieno
|
|||
continue
|
||||
}
|
||||
// The account was existent, it can be either case (c) or (d).
|
||||
addrHash := crypto.HashData(addr.Bytes())
|
||||
addrHash := crypto.Keccak256Hash(addr.Bytes())
|
||||
op := &accountDelete{
|
||||
address: addr,
|
||||
origin: types.SlimAccountRLP(*prev),
|
||||
|
|
|
|||
|
|
@ -74,39 +74,41 @@ func NewKeccakState() KeccakState {
|
|||
return sha3.NewLegacyKeccak256().(KeccakState)
|
||||
}
|
||||
|
||||
var hasherPool = sync.Pool{
|
||||
New: func() interface{} { return NewKeccakState() },
|
||||
}
|
||||
|
||||
// HashData hashes the provided data using the KeccakState and returns a 32 byte hash
|
||||
func HashData(data []byte) (h common.Hash) {
|
||||
kh := hasherPool.Get().(KeccakState)
|
||||
func HashData(kh KeccakState, data []byte) (h common.Hash) {
|
||||
kh.Reset()
|
||||
kh.Write(data)
|
||||
kh.Read(h[:])
|
||||
hasherPool.Put(kh)
|
||||
return h
|
||||
}
|
||||
|
||||
var hasherPool = sync.Pool{
|
||||
New: func() interface{} { return NewKeccakState() },
|
||||
}
|
||||
|
||||
// Keccak256 calculates and returns the Keccak256 hash of the input data.
|
||||
func Keccak256(data ...[]byte) []byte {
|
||||
b := make([]byte, 32)
|
||||
d := NewKeccakState()
|
||||
d := hasherPool.Get().(KeccakState)
|
||||
d.Reset()
|
||||
for _, b := range data {
|
||||
d.Write(b)
|
||||
}
|
||||
d.Read(b)
|
||||
hasherPool.Put(d)
|
||||
return b
|
||||
}
|
||||
|
||||
// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
|
||||
// converting it to an internal Hash data structure.
|
||||
func Keccak256Hash(data ...[]byte) (h common.Hash) {
|
||||
d := NewKeccakState()
|
||||
d := hasherPool.Get().(KeccakState)
|
||||
d.Reset()
|
||||
for _, b := range data {
|
||||
d.Write(b)
|
||||
}
|
||||
d.Read(h[:])
|
||||
hasherPool.Put(d)
|
||||
return h
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ func TestKeccak256Hash(t *testing.T) {
|
|||
func TestKeccak256Hasher(t *testing.T) {
|
||||
msg := []byte("abc")
|
||||
exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
|
||||
checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := HashData(in); return h[:] }, msg, exp)
|
||||
checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := Keccak256Hash(in); return h[:] }, msg, exp)
|
||||
}
|
||||
|
||||
func TestToECDSAErrors(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -729,7 +729,7 @@ func (s *Sync) hasNode(owner common.Hash, path []byte, hash common.Hash) (exists
|
|||
} else {
|
||||
blob = rawdb.ReadStorageTrieNode(s.database, owner, path)
|
||||
}
|
||||
exists = hash == crypto.HashData(blob)
|
||||
exists = hash == crypto.Keccak256Hash(blob)
|
||||
inconsistent = !exists && len(blob) != 0
|
||||
return exists, inconsistent
|
||||
}
|
||||
|
|
|
|||
|
|
@ -138,7 +138,7 @@ func Apply(prevRoot common.Hash, postRoot common.Hash, accounts map[common.Addre
|
|||
// existent in post-state. Apply the reverse diff and verify if the storage
|
||||
// root matches the one in prev-state account.
|
||||
func updateAccount(ctx *context, loader TrieLoader, addr common.Address) error {
|
||||
addrHash := crypto.HashData(addr.Bytes())
|
||||
addrHash := crypto.Keccak256Hash(addr.Bytes())
|
||||
prev, err := types.FullAccount(ctx.accounts[addr])
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -195,7 +195,7 @@ func updateAccount(ctx *context, loader TrieLoader, addr common.Address) error {
|
|||
// account and storage is wiped out correctly.
|
||||
func deleteAccount(ctx *context, loader TrieLoader, addr common.Address) error {
|
||||
// The account must be existent in post-state, load the account.
|
||||
addrHash := crypto.HashData(addr.Bytes())
|
||||
addrHash := crypto.Keccak256Hash(addr.Bytes())
|
||||
blob, err := ctx.accountTrie.Get(addrHash.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -120,7 +120,7 @@ func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co
|
|||
if blob := dl.cleans.Get(nil, key); len(blob) > 0 {
|
||||
cleanHitMeter.Mark(1)
|
||||
cleanReadMeter.Mark(int64(len(blob)))
|
||||
return blob, crypto.HashData(blob), &nodeLoc{loc: locCleanCache, depth: depth}, nil
|
||||
return blob, crypto.Keccak256Hash(blob), &nodeLoc{loc: locCleanCache, depth: depth}, nil
|
||||
}
|
||||
cleanMissMeter.Mark(1)
|
||||
}
|
||||
|
|
@ -136,7 +136,7 @@ func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co
|
|||
cleanWriteMeter.Mark(int64(len(blob)))
|
||||
}
|
||||
|
||||
return blob, crypto.HashData(blob), &nodeLoc{loc: locDiskLayer, depth: depth}, nil
|
||||
return blob, crypto.Keccak256Hash(blob), &nodeLoc{loc: locDiskLayer, depth: depth}, nil
|
||||
}
|
||||
|
||||
// update implements the layer interface, returning a new diff layer on top
|
||||
|
|
|
|||
Loading…
Reference in a new issue