From b2630732523665755e475bcf190b587ea78da514 Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Sat, 31 Aug 2019 17:31:47 -0700 Subject: [PATCH] Move (core/)types.rlpHash -> (crypto/)hash.FromRLP This is because one of its consumers (types.Header) will move out of the types package. --- core/types/block.go | 14 +++----------- core/types/transaction.go | 4 +++- core/types/transaction_signing.go | 6 ++++-- crypto/hash/rlp.go | 15 +++++++++++++++ 4 files changed, 25 insertions(+), 14 deletions(-) create mode 100644 crypto/hash/rlp.go diff --git a/core/types/block.go b/core/types/block.go index 87d2c2be2..7f31a9caa 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -31,8 +31,8 @@ import ( ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rlp" "github.com/rs/zerolog" - "golang.org/x/crypto/sha3" + "github.com/harmony-one/harmony/crypto/hash" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/shard" ) @@ -113,7 +113,7 @@ type headerMarshaling struct { // Hash returns the block hash of the header, which is simply the keccak256 hash of its // RLP encoding. func (h *Header) Hash() common.Hash { - return RLPHash(h) + return hash.FromRLP(h) } // Size returns the approximate memory used by all internal contents. It is used @@ -145,14 +145,6 @@ func (h *Header) GetShardState() (shard.ShardState, error) { return shardState, nil } -// RLPHash hashes the RLP representation of the given object. -func RLPHash(x interface{}) (h common.Hash) { - hw := sha3.NewLegacyKeccak256() - rlp.Encode(hw, x) - hw.Sum(h[:0]) - return h -} - // Body is a simple (mutable, non-safe) data container for storing and moving // a block's data contents (transactions and uncles) together. type Body struct { @@ -454,7 +446,7 @@ func (c *writeCounter) Write(b []byte) (int, error) { // CalcUncleHash returns rlp hash of uncles. func CalcUncleHash(uncles []*Header) common.Hash { - return RLPHash(uncles) + return hash.FromRLP(uncles) } // WithSeal returns a new block with the data from b but the header replaced with diff --git a/core/types/transaction.go b/core/types/transaction.go index 9fd3c6449..4fc954f80 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -28,6 +28,8 @@ import ( "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/rlp" + + "github.com/harmony-one/harmony/crypto/hash" common2 "github.com/harmony-one/harmony/internal/common" ) @@ -292,7 +294,7 @@ func (tx *Transaction) Hash() common.Hash { if hash := tx.hash.Load(); hash != nil { return hash.(common.Hash) } - v := RLPHash(tx) + v := hash.FromRLP(tx) tx.hash.Store(v) return v } diff --git a/core/types/transaction_signing.go b/core/types/transaction_signing.go index b510437b6..fbde4d676 100644 --- a/core/types/transaction_signing.go +++ b/core/types/transaction_signing.go @@ -24,6 +24,8 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" + + "github.com/harmony-one/harmony/crypto/hash" "github.com/harmony-one/harmony/internal/params" ) @@ -157,7 +159,7 @@ func (s EIP155Signer) SignatureValues(tx *Transaction, sig []byte) (R, S, V *big // Hash returns the hash to be signed by the sender. // It does not uniquely identify the transaction. func (s EIP155Signer) Hash(tx *Transaction) common.Hash { - return RLPHash([]interface{}{ + return hash.FromRLP([]interface{}{ tx.data.AccountNonce, tx.data.Price, tx.data.GasLimit, @@ -215,7 +217,7 @@ func (fs FrontierSigner) SignatureValues(tx *Transaction, sig []byte) (r, s, v * // Hash returns the hash to be signed by the sender. // It does not uniquely identify the transaction. func (fs FrontierSigner) Hash(tx *Transaction) common.Hash { - return RLPHash([]interface{}{ + return hash.FromRLP([]interface{}{ tx.data.AccountNonce, tx.data.Price, tx.data.GasLimit, diff --git a/crypto/hash/rlp.go b/crypto/hash/rlp.go new file mode 100644 index 000000000..d0ba7052f --- /dev/null +++ b/crypto/hash/rlp.go @@ -0,0 +1,15 @@ +package hash + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/rlp" + "golang.org/x/crypto/sha3" +) + +// FromRLP hashes the RLP representation of the given object. +func FromRLP(x interface{}) (h common.Hash) { + hw := sha3.NewLegacyKeccak256() + rlp.Encode(hw, x) + hw.Sum(h[:0]) + return h +}