Move (core/)types.rlpHash -> (crypto/)hash.FromRLP

This is because one of its consumers (types.Header) will move out of the
types package.
pull/1498/head
Eugene Kim 5 years ago
parent cefa1d99cd
commit b263073252
  1. 14
      core/types/block.go
  2. 4
      core/types/transaction.go
  3. 6
      core/types/transaction_signing.go
  4. 15
      crypto/hash/rlp.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

@ -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
}

@ -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,

@ -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
}
Loading…
Cancel
Save