update types

This commit is contained in:
Ethan Buchman
2018-02-03 03:23:10 -05:00
parent 061ad355bb
commit 4e3488c677
10 changed files with 71 additions and 61 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/tendermint/go-wire/data" "github.com/tendermint/go-wire/data"
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/merkle" "github.com/tendermint/tmlibs/merkle"
"golang.org/x/crypto/ripemd160"
) )
// Block defines the atomic unit of a Tendermint blockchain. // Block defines the atomic unit of a Tendermint blockchain.
@ -179,20 +180,20 @@ func (h *Header) Hash() data.Bytes {
if len(h.ValidatorsHash) == 0 { if len(h.ValidatorsHash) == 0 {
return nil return nil
} }
return merkle.SimpleHashFromMap(map[string]interface{}{ return merkle.SimpleHashFromMap(map[string]merkle.Hasher{
"ChainID": h.ChainID, "ChainID": wireHasher(h.ChainID),
"Height": h.Height, "Height": wireHasher(h.Height),
"Time": h.Time, "Time": wireHasher(h.Time),
"NumTxs": h.NumTxs, "NumTxs": wireHasher(h.NumTxs),
"TotalTxs": h.TotalTxs, "TotalTxs": wireHasher(h.TotalTxs),
"LastBlockID": h.LastBlockID, "LastBlockID": wireHasher(h.LastBlockID),
"LastCommit": h.LastCommitHash, "LastCommit": wireHasher(h.LastCommitHash),
"Data": h.DataHash, "Data": wireHasher(h.DataHash),
"Validators": h.ValidatorsHash, "Validators": wireHasher(h.ValidatorsHash),
"App": h.AppHash, "App": wireHasher(h.AppHash),
"Consensus": h.ConsensusHash, "Consensus": wireHasher(h.ConsensusHash),
"Results": h.LastResultsHash, "Results": wireHasher(h.LastResultsHash),
"Evidence": h.EvidenceHash, "Evidence": wireHasher(h.EvidenceHash),
}) })
} }
@ -356,11 +357,11 @@ func (commit *Commit) ValidateBasic() error {
// Hash returns the hash of the commit // Hash returns the hash of the commit
func (commit *Commit) Hash() data.Bytes { func (commit *Commit) Hash() data.Bytes {
if commit.hash == nil { if commit.hash == nil {
bs := make([]interface{}, len(commit.Precommits)) bs := make([]merkle.Hasher, len(commit.Precommits))
for i, precommit := range commit.Precommits { for i, precommit := range commit.Precommits {
bs[i] = precommit bs[i] = wireHasher(precommit)
} }
commit.hash = merkle.SimpleHashFromBinaries(bs) commit.hash = merkle.SimpleHashFromHashers(bs)
} }
return commit.hash return commit.hash
} }
@ -510,3 +511,23 @@ func (blockID BlockID) WriteSignBytes(w io.Writer, n *int, err *error) {
func (blockID BlockID) String() string { func (blockID BlockID) String() string {
return fmt.Sprintf(`%v:%v`, blockID.Hash, blockID.PartsHeader) return fmt.Sprintf(`%v:%v`, blockID.Hash, blockID.PartsHeader)
} }
//-------------------------------------------------------
type hasher struct {
item interface{}
}
func (h hasher) Hash() []byte {
hasher, n, err := ripemd160.New(), new(int), new(error)
wire.WriteBinary(h.item, hasher, n, err)
if *err != nil {
panic(err)
}
return hasher.Sum(nil)
}
func wireHasher(item interface{}) merkle.Hasher {
return hasher{item}
}

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
abci "github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log" "github.com/tendermint/tmlibs/log"
tmpubsub "github.com/tendermint/tmlibs/pubsub" tmpubsub "github.com/tendermint/tmlibs/pubsub"
@ -98,17 +97,11 @@ func (b *EventBus) PublishEventTx(event EventDataTx) error {
// validate and fill tags from tx result // validate and fill tags from tx result
for _, tag := range event.Result.Tags { for _, tag := range event.Result.Tags {
// basic validation // basic validation
if tag.Key == "" { if len(tag.Key) == 0 {
b.Logger.Info("Got tag with an empty key (skipping)", "tag", tag, "tx", event.Tx) b.Logger.Info("Got tag with an empty key (skipping)", "tag", tag, "tx", event.Tx)
continue continue
} }
tags[string(tag.Key)] = tag.Value
switch tag.ValueType {
case abci.KVPair_STRING:
tags[tag.Key] = tag.ValueString
case abci.KVPair_INT:
tags[tag.Key] = tag.ValueInt
}
} }
// add predefined tags // add predefined tags

View File

@ -120,7 +120,7 @@ func (dve *DuplicateVoteEvidence) Index() int {
// Hash returns the hash of the evidence. // Hash returns the hash of the evidence.
func (dve *DuplicateVoteEvidence) Hash() []byte { func (dve *DuplicateVoteEvidence) Hash() []byte {
return merkle.SimpleHashFromBinary(dve) return wireHasher(dve).Hash()
} }
// Verify returns an error if the two votes aren't conflicting. // Verify returns an error if the two votes aren't conflicting.
@ -165,7 +165,9 @@ func (dve *DuplicateVoteEvidence) Equal(ev Evidence) bool {
} }
// just check their hashes // just check their hashes
return bytes.Equal(merkle.SimpleHashFromBinary(dve), merkle.SimpleHashFromBinary(ev)) dveHash := wireHasher(dve).Hash()
evHash := wireHasher(ev).Hash()
return bytes.Equal(dveHash, evHash)
} }
//----------------------------------------------------------------- //-----------------------------------------------------------------

View File

@ -106,13 +106,13 @@ func (params *ConsensusParams) Validate() error {
// Hash returns a merkle hash of the parameters to store // Hash returns a merkle hash of the parameters to store
// in the block header // in the block header
func (params *ConsensusParams) Hash() []byte { func (params *ConsensusParams) Hash() []byte {
return merkle.SimpleHashFromMap(map[string]interface{}{ return merkle.SimpleHashFromMap(map[string]merkle.Hasher{
"block_gossip_part_size_bytes": params.BlockGossip.BlockPartSizeBytes, "block_gossip_part_size_bytes": wireHasher(params.BlockGossip.BlockPartSizeBytes),
"block_size_max_bytes": params.BlockSize.MaxBytes, "block_size_max_bytes": wireHasher(params.BlockSize.MaxBytes),
"block_size_max_gas": params.BlockSize.MaxGas, "block_size_max_gas": wireHasher(params.BlockSize.MaxGas),
"block_size_max_txs": params.BlockSize.MaxTxs, "block_size_max_txs": wireHasher(params.BlockSize.MaxTxs),
"tx_size_max_bytes": params.TxSize.MaxBytes, "tx_size_max_bytes": wireHasher(params.TxSize.MaxBytes),
"tx_size_max_gas": params.TxSize.MaxGas, "tx_size_max_gas": wireHasher(params.TxSize.MaxGas),
}) })
} }

View File

@ -96,7 +96,7 @@ func NewPartSetFromData(data []byte, partSize int) *PartSet {
// divide data into 4kb parts. // divide data into 4kb parts.
total := (len(data) + partSize - 1) / partSize total := (len(data) + partSize - 1) / partSize
parts := make([]*Part, total) parts := make([]*Part, total)
parts_ := make([]merkle.Hashable, total) parts_ := make([]merkle.Hasher, total)
partsBitArray := cmn.NewBitArray(total) partsBitArray := cmn.NewBitArray(total)
for i := 0; i < total; i++ { for i := 0; i < total; i++ {
part := &Part{ part := &Part{
@ -108,7 +108,7 @@ func NewPartSetFromData(data []byte, partSize int) *PartSet {
partsBitArray.SetIndex(i, true) partsBitArray.SetIndex(i, true)
} }
// Compute merkle proofs // Compute merkle proofs
root, proofs := merkle.SimpleProofsFromHashables(parts_) root, proofs := merkle.SimpleProofsFromHashers(parts_)
for i := 0; i < total; i++ { for i := 0; i < total; i++ {
parts[i].Proof = *proofs[i] parts[i].Proof = *proofs[i]
} }

View File

@ -23,15 +23,15 @@ func (tm2pb) Header(header *Header) *types.Header {
} }
} }
func (tm2pb) BlockID(blockID BlockID) *types.BlockID { func (tm2pb) BlockID(blockID BlockID) types.BlockID {
return &types.BlockID{ return types.BlockID{
Hash: blockID.Hash, Hash: blockID.Hash,
Parts: TM2PB.PartSetHeader(blockID.PartsHeader), Parts: TM2PB.PartSetHeader(blockID.PartsHeader),
} }
} }
func (tm2pb) PartSetHeader(partSetHeader PartSetHeader) *types.PartSetHeader { func (tm2pb) PartSetHeader(partSetHeader PartSetHeader) types.PartSetHeader {
return &types.PartSetHeader{ return types.PartSetHeader{
Total: int32(partSetHeader.Total), // XXX: overflow Total: int32(partSetHeader.Total), // XXX: overflow
Hash: partSetHeader.Hash, Hash: partSetHeader.Hash,
} }

View File

@ -47,20 +47,20 @@ func (a ABCIResults) Bytes() []byte {
// Hash returns a merkle hash of all results // Hash returns a merkle hash of all results
func (a ABCIResults) Hash() []byte { func (a ABCIResults) Hash() []byte {
return merkle.SimpleHashFromHashables(a.toHashables()) return merkle.SimpleHashFromHashers(a.toHashers())
} }
// ProveResult returns a merkle proof of one result from the set // ProveResult returns a merkle proof of one result from the set
func (a ABCIResults) ProveResult(i int) merkle.SimpleProof { func (a ABCIResults) ProveResult(i int) merkle.SimpleProof {
_, proofs := merkle.SimpleProofsFromHashables(a.toHashables()) _, proofs := merkle.SimpleProofsFromHashers(a.toHashers())
return *proofs[i] return *proofs[i]
} }
func (a ABCIResults) toHashables() []merkle.Hashable { func (a ABCIResults) toHashers() []merkle.Hasher {
l := len(a) l := len(a)
hashables := make([]merkle.Hashable, l) hashers := make([]merkle.Hasher, l)
for i := 0; i < l; i++ { for i := 0; i < l; i++ {
hashables[i] = a[i] hashers[i] = a[i]
} }
return hashables return hashers
} }

View File

@ -5,7 +5,6 @@ import (
"io" "io"
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/merkle"
) )
// Signable is an interface for all signable things. // Signable is an interface for all signable things.
@ -23,8 +22,3 @@ func SignBytes(chainID string, o Signable) []byte {
} }
return buf.Bytes() return buf.Bytes()
} }
// HashSignBytes is a convenience method for getting the hash of the bytes of a signable
func HashSignBytes(chainID string, o Signable) []byte {
return merkle.SimpleHashFromBinary(SignBytes(chainID, o))
}

View File

@ -18,7 +18,7 @@ type Tx []byte
// Hash computes the RIPEMD160 hash of the go-wire encoded transaction. // Hash computes the RIPEMD160 hash of the go-wire encoded transaction.
func (tx Tx) Hash() []byte { func (tx Tx) Hash() []byte {
return merkle.SimpleHashFromBinary(tx) return wireHasher(tx).Hash()
} }
// String returns the hex-encoded transaction as a string. // String returns the hex-encoded transaction as a string.
@ -72,11 +72,11 @@ func (txs Txs) IndexByHash(hash []byte) int {
// TODO: optimize this! // TODO: optimize this!
func (txs Txs) Proof(i int) TxProof { func (txs Txs) Proof(i int) TxProof {
l := len(txs) l := len(txs)
hashables := make([]merkle.Hashable, l) hashers := make([]merkle.Hasher, l)
for i := 0; i < l; i++ { for i := 0; i < l; i++ {
hashables[i] = txs[i] hashers[i] = txs[i]
} }
root, proofs := merkle.SimpleProofsFromHashables(hashables) root, proofs := merkle.SimpleProofsFromHashers(hashers)
return TxProof{ return TxProof{
Index: i, Index: i,

View File

@ -54,7 +54,7 @@ func (valSet *ValidatorSet) IncrementAccum(times int) {
for _, val := range valSet.Validators { for _, val := range valSet.Validators {
// check for overflow both multiplication and sum // check for overflow both multiplication and sum
val.Accum = safeAddClip(val.Accum, safeMulClip(val.VotingPower, int64(times))) val.Accum = safeAddClip(val.Accum, safeMulClip(val.VotingPower, int64(times)))
validatorsHeap.Push(val, accumComparable{val}) validatorsHeap.PushComparable(val, accumComparable{val})
} }
// Decrement the validator with most accum times times // Decrement the validator with most accum times times
@ -150,11 +150,11 @@ func (valSet *ValidatorSet) Hash() []byte {
if len(valSet.Validators) == 0 { if len(valSet.Validators) == 0 {
return nil return nil
} }
hashables := make([]merkle.Hashable, len(valSet.Validators)) hashers := make([]merkle.Hasher, len(valSet.Validators))
for i, val := range valSet.Validators { for i, val := range valSet.Validators {
hashables[i] = val hashers[i] = val
} }
return merkle.SimpleHashFromHashables(hashables) return merkle.SimpleHashFromHashers(hashers)
} }
func (valSet *ValidatorSet) Add(val *Validator) (added bool) { func (valSet *ValidatorSet) Add(val *Validator) (added bool) {