mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-11 20:31:20 +00:00
Make Ripemd160 the default
This commit is contained in:
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -98,7 +98,7 @@ func (v *Validator) String() string {
|
||||
}
|
||||
|
||||
func (v *Validator) Hash() []byte {
|
||||
return binary.BinarySha256(v)
|
||||
return binary.BinaryRipemd160(v)
|
||||
}
|
||||
|
||||
//-------------------------------------
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user