diff --git a/account/priv_account.go b/account/priv_account.go index 92a7cb36..77351e53 100644 --- a/account/priv_account.go +++ b/account/priv_account.go @@ -28,7 +28,7 @@ func GenPrivAccount() *PrivAccount { // Generates a new account with private key from SHA256 hash of a secret func GenPrivAccountFromSecret(secret []byte) *PrivAccount { - privKey32 := binary.BinarySha256(secret) + privKey32 := binary.BinarySha256(secret) // Not Ripemd160 because we want 32 bytes. privKeyBytes := new([64]byte) copy(privKeyBytes[:32], privKey32) pubKeyBytes := ed25519.MakePublicKey(privKeyBytes) diff --git a/binary/util.go b/binary/util.go index 29aa57cd..d03464bf 100644 --- a/binary/util.go +++ b/binary/util.go @@ -40,6 +40,7 @@ func BinaryCompare(a, b interface{}) int { return bytes.Compare(aBytes, bBytes) } +// NOTE: only use this if you need 32 bytes. func BinarySha256(o interface{}) []byte { hasher, n, err := sha256.New(), new(int64), new(error) WriteBinary(o, hasher, n, err) @@ -49,6 +50,7 @@ func BinarySha256(o interface{}) []byte { return hasher.Sum(nil) } +// NOTE: The default hash function is Ripemd160. func BinaryRipemd160(o interface{}) []byte { hasher, n, err := ripemd160.New(), new(int64), new(error) WriteBinary(o, hasher, n, err) diff --git a/mempool/mempool.go b/mempool/mempool.go index 51cfab13..a08d9bc6 100644 --- a/mempool/mempool.go +++ b/mempool/mempool.go @@ -11,7 +11,6 @@ package mempool import ( "sync" - "github.com/tendermint/tendermint/binary" sm "github.com/tendermint/tendermint/state" "github.com/tendermint/tendermint/types" ) @@ -73,19 +72,18 @@ func (mem *Mempool) ResetForBlockAndState(block *types.Block, state *sm.State) { // First, create a lookup map of txns in new block. blockTxsMap := make(map[string]struct{}) for _, tx := range block.Data.Txs { - txHash := binary.BinarySha256(tx) - blockTxsMap[string(txHash)] = struct{}{} + blockTxsMap[string(types.TxID(state.ChainID, tx))] = struct{}{} } // Next, filter all txs from mem.txs that are in blockTxsMap txs := []types.Tx{} for _, tx := range mem.txs { - txHash := binary.BinarySha256(tx) - if _, ok := blockTxsMap[string(txHash)]; ok { - log.Debug("Filter out, already committed", "tx", tx, "txHash", txHash) + txID := types.TxID(state.ChainID, tx) + if _, ok := blockTxsMap[string(txID)]; ok { + log.Debug("Filter out, already committed", "tx", tx, "txID", txID) continue } else { - log.Debug("Filter in, still new", "tx", tx, "txHash", txHash) + log.Debug("Filter in, still new", "tx", tx, "txID", txID) txs = append(txs, tx) } } diff --git a/merkle/iavl_node.go b/merkle/iavl_node.go index 6786e9d2..0055f671 100644 --- a/merkle/iavl_node.go +++ b/merkle/iavl_node.go @@ -2,7 +2,7 @@ package merkle import ( "bytes" - "crypto/sha256" + "github.com/tendermint/tendermint/Godeps/_workspace/src/code.google.com/p/go.crypto/ripemd160" "io" "github.com/tendermint/tendermint/binary" @@ -132,7 +132,7 @@ func (node *IAVLNode) hashWithCount(t *IAVLTree) ([]byte, int) { return node.hash, 0 } - hasher := sha256.New() + hasher := ripemd160.New() buf := new(bytes.Buffer) _, hashCount, err := node.writeHashBytes(t, buf) if err != nil { diff --git a/merkle/iavl_proof.go b/merkle/iavl_proof.go index 0541aa73..4216c6c1 100644 --- a/merkle/iavl_proof.go +++ b/merkle/iavl_proof.go @@ -2,7 +2,9 @@ package merkle import ( "bytes" - "crypto/sha256" + + "github.com/tendermint/tendermint/Godeps/_workspace/src/code.google.com/p/go.crypto/ripemd160" + "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" ) @@ -41,7 +43,7 @@ type IAVLProofInnerNode struct { } func (branch IAVLProofInnerNode) Hash(childHash []byte) []byte { - hasher := sha256.New() + hasher := ripemd160.New() buf := new(bytes.Buffer) n, err := int64(0), error(nil) binary.WriteInt8(branch.Height, buf, &n, &err) @@ -67,7 +69,7 @@ type IAVLProofLeafNode struct { } func (leaf IAVLProofLeafNode) Hash() []byte { - hasher := sha256.New() + hasher := ripemd160.New() buf := new(bytes.Buffer) n, err := int64(0), error(nil) binary.WriteInt8(0, buf, &n, &err) diff --git a/merkle/simple_tree.go b/merkle/simple_tree.go index 941ddb5c..ad75393c 100644 --- a/merkle/simple_tree.go +++ b/merkle/simple_tree.go @@ -26,16 +26,17 @@ package merkle import ( "bytes" - "crypto/sha256" "fmt" + "github.com/tendermint/tendermint/Godeps/_workspace/src/code.google.com/p/go.crypto/ripemd160" + "github.com/tendermint/tendermint/binary" ) func SimpleHashFromTwoHashes(left []byte, right []byte) []byte { var n int64 var err error - var hasher = sha256.New() + var hasher = ripemd160.New() binary.WriteByteSlice(left, hasher, &n, &err) binary.WriteByteSlice(right, hasher, &n, &err) if err != nil { @@ -69,7 +70,7 @@ func SimpleHashFromBinaries(items []interface{}) []byte { // General Convenience func SimpleHashFromBinary(item interface{}) []byte { - hasher, n, err := sha256.New(), new(int64), new(error) + hasher, n, err := ripemd160.New(), new(int64), new(error) binary.WriteBinary(item, hasher, n, err) if *err != nil { panic(err) diff --git a/state/validator.go b/state/validator.go index 20f12532..1519ea68 100644 --- a/state/validator.go +++ b/state/validator.go @@ -98,7 +98,7 @@ func (v *Validator) String() string { } func (v *Validator) Hash() []byte { - return binary.BinarySha256(v) + return binary.BinaryRipemd160(v) } //------------------------------------- diff --git a/types/block.go b/types/block.go index c34387a8..c67674ab 100644 --- a/types/block.go +++ b/types/block.go @@ -2,7 +2,6 @@ package types import ( "bytes" - "crypto/sha256" "errors" "fmt" "strings" @@ -138,15 +137,7 @@ func (h *Header) Hash() []byte { return nil } - buf := new(bytes.Buffer) - hasher, n, err := sha256.New(), new(int64), new(error) - binary.WriteBinary(h, buf, n, err) - if *err != nil { - panic(err) - } - hasher.Write(buf.Bytes()) - hash := hasher.Sum(nil) - return hash + return binary.BinaryRipemd160(h) } func (h *Header) StringIndented(indent string) string { @@ -321,7 +312,7 @@ func (data *Data) Hash() []byte { for i, tx := range data.Txs { bs[i] = account.SignBytes(config.GetString("chain_id"), tx) } - data.hash = merkle.SimpleHashFromBinaries(bs) + data.hash = merkle.SimpleHashFromBinaries(bs) // NOTE: leaves are TxIDs. } return data.hash } diff --git a/types/part_set.go b/types/part_set.go index 8d4ba320..32803fb2 100644 --- a/types/part_set.go +++ b/types/part_set.go @@ -2,12 +2,13 @@ package types import ( "bytes" - "crypto/sha256" "errors" "fmt" "io" "sync" + "github.com/tendermint/tendermint/Godeps/_workspace/src/code.google.com/p/go.crypto/ripemd160" + "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" "github.com/tendermint/tendermint/merkle" @@ -34,7 +35,7 @@ func (part *Part) Hash() []byte { if part.hash != nil { return part.hash } else { - hasher := sha256.New() + hasher := ripemd160.New() _, err := hasher.Write(part.Bytes) if err != nil { panic(err) diff --git a/types/tx.go b/types/tx.go index 36c91082..552485a9 100644 --- a/types/tx.go +++ b/types/tx.go @@ -314,8 +314,7 @@ func (tx *DupeoutTx) String() string { //----------------------------------------------------------------------------- -// NOTE: the tx merkle tree uses sha256, so this TxID is really just for -// reference when using the rpc and catching events +// This should match the leaf hashes of Block.Data.Hash()'s SimpleMerkleTree. func TxID(chainID string, tx Tx) []byte { signBytes := account.SignBytes(chainID, tx) return binary.BinaryRipemd160(signBytes)