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 {
|
if len(blob) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return crypto.HashData(blob) == hash // exists but not match
|
return crypto.Keccak256Hash(blob) == hash // exists but not match
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("Unknown scheme %v", scheme))
|
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 {
|
if len(blob) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if crypto.HashData(blob) != hash {
|
if crypto.Keccak256Hash(blob) != hash {
|
||||||
return nil // exists but not match
|
return nil // exists but not match
|
||||||
}
|
}
|
||||||
return blob
|
return blob
|
||||||
|
|
|
||||||
|
|
@ -408,8 +408,6 @@ func (s *stateObject) updateRoot() {
|
||||||
// fulfills the storage diffs into the given accountUpdate struct.
|
// fulfills the storage diffs into the given accountUpdate struct.
|
||||||
func (s *stateObject) commitStorage(op *accountUpdate) {
|
func (s *stateObject) commitStorage(op *accountUpdate) {
|
||||||
var (
|
var (
|
||||||
hash common.Hash
|
|
||||||
hasher = crypto.NewKeccakState()
|
|
||||||
encode = func(val common.Hash) []byte {
|
encode = func(val common.Hash) []byte {
|
||||||
if val == (common.Hash{}) {
|
if val == (common.Hash{}) {
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -426,9 +424,7 @@ func (s *stateObject) commitStorage(op *accountUpdate) {
|
||||||
if val == s.originStorage[key] {
|
if val == s.originStorage[key] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
hasher.Reset()
|
hash := crypto.Keccak256Hash(key[:])
|
||||||
hasher.Write(key[:])
|
|
||||||
hasher.Read(hash[:])
|
|
||||||
if op.storages == nil {
|
if op.storages == nil {
|
||||||
op.storages = make(map[common.Hash][]byte)
|
op.storages = make(map[common.Hash][]byte)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -580,7 +580,7 @@ func (s *StateDB) getStateObject(addr common.Address) *stateObject {
|
||||||
var data *types.StateAccount
|
var data *types.StateAccount
|
||||||
if s.snap != nil {
|
if s.snap != nil {
|
||||||
start := time.Now()
|
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)
|
s.SnapshotAccountReads += time.Since(start)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
@ -1069,7 +1069,7 @@ func (s *StateDB) handleDestruction() (map[common.Hash]*accountDelete, []*trieno
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// The account was existent, it can be either case (c) or (d).
|
// The account was existent, it can be either case (c) or (d).
|
||||||
addrHash := crypto.HashData(addr.Bytes())
|
addrHash := crypto.Keccak256Hash(addr.Bytes())
|
||||||
op := &accountDelete{
|
op := &accountDelete{
|
||||||
address: addr,
|
address: addr,
|
||||||
origin: types.SlimAccountRLP(*prev),
|
origin: types.SlimAccountRLP(*prev),
|
||||||
|
|
|
||||||
|
|
@ -74,39 +74,41 @@ func NewKeccakState() KeccakState {
|
||||||
return sha3.NewLegacyKeccak256().(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
|
// HashData hashes the provided data using the KeccakState and returns a 32 byte hash
|
||||||
func HashData(data []byte) (h common.Hash) {
|
func HashData(kh KeccakState, data []byte) (h common.Hash) {
|
||||||
kh := hasherPool.Get().(KeccakState)
|
|
||||||
kh.Reset()
|
kh.Reset()
|
||||||
kh.Write(data)
|
kh.Write(data)
|
||||||
kh.Read(h[:])
|
kh.Read(h[:])
|
||||||
hasherPool.Put(kh)
|
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var hasherPool = sync.Pool{
|
||||||
|
New: func() interface{} { return NewKeccakState() },
|
||||||
|
}
|
||||||
|
|
||||||
// Keccak256 calculates and returns the Keccak256 hash of the input data.
|
// Keccak256 calculates and returns the Keccak256 hash of the input data.
|
||||||
func Keccak256(data ...[]byte) []byte {
|
func Keccak256(data ...[]byte) []byte {
|
||||||
b := make([]byte, 32)
|
b := make([]byte, 32)
|
||||||
d := NewKeccakState()
|
d := hasherPool.Get().(KeccakState)
|
||||||
|
d.Reset()
|
||||||
for _, b := range data {
|
for _, b := range data {
|
||||||
d.Write(b)
|
d.Write(b)
|
||||||
}
|
}
|
||||||
d.Read(b)
|
d.Read(b)
|
||||||
|
hasherPool.Put(d)
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
|
// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
|
||||||
// converting it to an internal Hash data structure.
|
// converting it to an internal Hash data structure.
|
||||||
func Keccak256Hash(data ...[]byte) (h common.Hash) {
|
func Keccak256Hash(data ...[]byte) (h common.Hash) {
|
||||||
d := NewKeccakState()
|
d := hasherPool.Get().(KeccakState)
|
||||||
|
d.Reset()
|
||||||
for _, b := range data {
|
for _, b := range data {
|
||||||
d.Write(b)
|
d.Write(b)
|
||||||
}
|
}
|
||||||
d.Read(h[:])
|
d.Read(h[:])
|
||||||
|
hasherPool.Put(d)
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ func TestKeccak256Hash(t *testing.T) {
|
||||||
func TestKeccak256Hasher(t *testing.T) {
|
func TestKeccak256Hasher(t *testing.T) {
|
||||||
msg := []byte("abc")
|
msg := []byte("abc")
|
||||||
exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
|
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) {
|
func TestToECDSAErrors(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -729,7 +729,7 @@ func (s *Sync) hasNode(owner common.Hash, path []byte, hash common.Hash) (exists
|
||||||
} else {
|
} else {
|
||||||
blob = rawdb.ReadStorageTrieNode(s.database, owner, path)
|
blob = rawdb.ReadStorageTrieNode(s.database, owner, path)
|
||||||
}
|
}
|
||||||
exists = hash == crypto.HashData(blob)
|
exists = hash == crypto.Keccak256Hash(blob)
|
||||||
inconsistent = !exists && len(blob) != 0
|
inconsistent = !exists && len(blob) != 0
|
||||||
return exists, inconsistent
|
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
|
// existent in post-state. Apply the reverse diff and verify if the storage
|
||||||
// root matches the one in prev-state account.
|
// root matches the one in prev-state account.
|
||||||
func updateAccount(ctx *context, loader TrieLoader, addr common.Address) error {
|
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])
|
prev, err := types.FullAccount(ctx.accounts[addr])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -195,7 +195,7 @@ func updateAccount(ctx *context, loader TrieLoader, addr common.Address) error {
|
||||||
// account and storage is wiped out correctly.
|
// account and storage is wiped out correctly.
|
||||||
func deleteAccount(ctx *context, loader TrieLoader, addr common.Address) error {
|
func deleteAccount(ctx *context, loader TrieLoader, addr common.Address) error {
|
||||||
// The account must be existent in post-state, load the account.
|
// 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())
|
blob, err := ctx.accountTrie.Get(addrHash.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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 {
|
if blob := dl.cleans.Get(nil, key); len(blob) > 0 {
|
||||||
cleanHitMeter.Mark(1)
|
cleanHitMeter.Mark(1)
|
||||||
cleanReadMeter.Mark(int64(len(blob)))
|
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)
|
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)))
|
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
|
// update implements the layer interface, returning a new diff layer on top
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue