From 200787ede2e7bde972b33234c6436cd626cdb722 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Sun, 14 Jan 2018 19:05:22 -0500 Subject: [PATCH] types: update for new go-wire. WriteSignBytes -> SignBytes --- types/block.go | 26 ++++++++++++++------------ types/evidence.go | 4 ++-- types/heartbeat.go | 13 ++++++++----- types/part_set.go | 5 ----- types/priv_validator.go | 9 +++++---- types/proposal.go | 13 ++++++++----- types/results.go | 8 ++++++-- types/signable.go | 22 +++++++++------------- types/test_util.go | 2 +- types/validator.go | 29 ++++------------------------- types/validator_set.go | 4 ++-- types/vote.go | 13 ++++++++----- 12 files changed, 67 insertions(+), 81 deletions(-) diff --git a/types/block.go b/types/block.go index 3e800e72..9aa7b0a0 100644 --- a/types/block.go +++ b/types/block.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "fmt" - "io" "strings" "time" @@ -96,7 +95,11 @@ func (b *Block) Hash() cmn.HexBytes { // MakePartSet returns a PartSet containing parts of a serialized block. // This is the form in which the block is gossipped to peers. func (b *Block) MakePartSet(partSize int) *PartSet { - return NewPartSetFromData(wire.BinaryBytes(b), partSize) + bz, err := wire.MarshalBinary(b) + if err != nil { + panic(err) + } + return NewPartSetFromData(bz, partSize) } // HashesTo is a convenience function that checks if a block hashes to the given argument. @@ -493,17 +496,11 @@ func (blockID BlockID) Equals(other BlockID) bool { // Key returns a machine-readable string representation of the BlockID func (blockID BlockID) Key() string { - return string(blockID.Hash) + string(wire.BinaryBytes(blockID.PartsHeader)) -} - -// WriteSignBytes writes the canonical bytes of the BlockID to the given writer for digital signing -func (blockID BlockID) WriteSignBytes(w io.Writer, n *int, err *error) { - if blockID.IsZero() { - wire.WriteTo([]byte("null"), w, n, err) - } else { - wire.WriteJSON(CanonicalBlockID(blockID), w, n, err) + bz, err := wire.MarshalBinary(blockID.PartsHeader) + if err != nil { + panic(err) } - + return string(blockID.Hash) + string(bz) } // String returns a human readable string representation of the BlockID @@ -527,6 +524,11 @@ func (h hasher) Hash() []byte { } +func tmHash(item interface{}) []byte { + h := hasher{item} + return h.Hash() +} + func wireHasher(item interface{}) merkle.Hasher { return hasher{item} } diff --git a/types/evidence.go b/types/evidence.go index 9973c62e..b793f7d8 100644 --- a/types/evidence.go +++ b/types/evidence.go @@ -148,10 +148,10 @@ func (dve *DuplicateVoteEvidence) Verify(chainID string) error { } // Signatures must be valid - if !dve.PubKey.VerifyBytes(SignBytes(chainID, dve.VoteA), dve.VoteA.Signature) { + if !dve.PubKey.VerifyBytes(dve.VoteA.SignBytes(chainID), dve.VoteA.Signature) { return fmt.Errorf("DuplicateVoteEvidence Error verifying VoteA: %v", ErrVoteInvalidSignature) } - if !dve.PubKey.VerifyBytes(SignBytes(chainID, dve.VoteB), dve.VoteB.Signature) { + if !dve.PubKey.VerifyBytes(dve.VoteB.SignBytes(chainID), dve.VoteB.Signature) { return fmt.Errorf("DuplicateVoteEvidence Error verifying VoteB: %v", ErrVoteInvalidSignature) } diff --git a/types/heartbeat.go b/types/heartbeat.go index a4ff0d21..81a497a4 100644 --- a/types/heartbeat.go +++ b/types/heartbeat.go @@ -2,7 +2,6 @@ package types import ( "fmt" - "io" "github.com/tendermint/go-crypto" "github.com/tendermint/go-wire" @@ -23,13 +22,17 @@ type Heartbeat struct { Signature crypto.Signature `json:"signature"` } -// WriteSignBytes writes the Heartbeat for signing. +// SignBytes returns the Heartbeat bytes for signing. // It panics if the Heartbeat is nil. -func (heartbeat *Heartbeat) WriteSignBytes(chainID string, w io.Writer, n *int, err *error) { - wire.WriteJSON(CanonicalJSONOnceHeartbeat{ +func (heartbeat *Heartbeat) SignBytes(chainID string) []byte { + bz, err := wire.MarshalJSON(CanonicalJSONOnceHeartbeat{ chainID, CanonicalHeartbeat(heartbeat), - }, w, n, err) + }) + if err != nil { + panic(err) + } + return bz } // Copy makes a copy of the Heartbeat. diff --git a/types/part_set.go b/types/part_set.go index ff11f7d3..5c43b151 100644 --- a/types/part_set.go +++ b/types/part_set.go @@ -9,7 +9,6 @@ import ( "golang.org/x/crypto/ripemd160" - "github.com/tendermint/go-wire" cmn "github.com/tendermint/tmlibs/common" "github.com/tendermint/tmlibs/merkle" ) @@ -73,10 +72,6 @@ func (psh PartSetHeader) Equals(other PartSetHeader) bool { return psh.Total == other.Total && bytes.Equal(psh.Hash, other.Hash) } -func (psh PartSetHeader) WriteSignBytes(w io.Writer, n *int, err *error) { - wire.WriteJSON(CanonicalPartSetHeader(psh), w, n, err) -} - //------------------------------------- type PartSet struct { diff --git a/types/priv_validator.go b/types/priv_validator.go index bf370a88..fe1a5d1c 100644 --- a/types/priv_validator.go +++ b/types/priv_validator.go @@ -234,10 +234,11 @@ func (privVal *PrivValidatorFS) save() { // Reset resets all fields in the PrivValidatorFS. // NOTE: Unsafe! func (privVal *PrivValidatorFS) Reset() { + var sig crypto.Signature privVal.LastHeight = 0 privVal.LastRound = 0 privVal.LastStep = 0 - privVal.LastSignature = crypto.Signature{} + privVal.LastSignature = sig privVal.LastSignBytes = nil privVal.Save() } @@ -297,7 +298,7 @@ func (privVal *PrivValidatorFS) checkHRS(height int64, round int, step int8) (bo // a previously signed vote (ie. we crashed after signing but before the vote hit the WAL). func (privVal *PrivValidatorFS) signVote(chainID string, vote *Vote) error { height, round, step := vote.Height, vote.Round, voteToStep(vote) - signBytes := SignBytes(chainID, vote) + signBytes := vote.SignBytes(chainID) sameHRS, err := privVal.checkHRS(height, round, step) if err != nil { @@ -336,7 +337,7 @@ func (privVal *PrivValidatorFS) signVote(chainID string, vote *Vote) error { // a previously signed proposal ie. we crashed after signing but before the proposal hit the WAL). func (privVal *PrivValidatorFS) signProposal(chainID string, proposal *Proposal) error { height, round, step := proposal.Height, proposal.Round, stepPropose - signBytes := SignBytes(chainID, proposal) + signBytes := proposal.SignBytes(chainID) sameHRS, err := privVal.checkHRS(height, round, step) if err != nil { @@ -388,7 +389,7 @@ func (privVal *PrivValidatorFS) SignHeartbeat(chainID string, heartbeat *Heartbe privVal.mtx.Lock() defer privVal.mtx.Unlock() var err error - heartbeat.Signature, err = privVal.Sign(SignBytes(chainID, heartbeat)) + heartbeat.Signature, err = privVal.Sign(heartbeat.SignBytes(chainID)) return err } diff --git a/types/proposal.go b/types/proposal.go index 98600681..cd928ea4 100644 --- a/types/proposal.go +++ b/types/proposal.go @@ -3,7 +3,6 @@ package types import ( "errors" "fmt" - "io" "time" "github.com/tendermint/go-crypto" @@ -50,10 +49,14 @@ func (p *Proposal) String() string { p.POLBlockID, p.Signature, CanonicalTime(p.Timestamp)) } -// WriteSignBytes writes the Proposal bytes for signing -func (p *Proposal) WriteSignBytes(chainID string, w io.Writer, n *int, err *error) { - wire.WriteJSON(CanonicalJSONOnceProposal{ +// SignBytes returns the Proposal bytes for signing +func (p *Proposal) SignBytes(chainID string) []byte { + bz, err := wire.MarshalJSON(CanonicalJSONOnceProposal{ ChainID: chainID, Proposal: CanonicalProposal(p), - }, w, n, err) + }) + if err != nil { + panic(err) + } + return bz } diff --git a/types/results.go b/types/results.go index 4a02f285..27486c98 100644 --- a/types/results.go +++ b/types/results.go @@ -18,7 +18,7 @@ type ABCIResult struct { // Hash returns the canonical hash of the ABCIResult func (a ABCIResult) Hash() []byte { - return wire.BinaryRipemd160(a) + return merkle.SimpleHashFromBinary(a) } // ABCIResults wraps the deliver tx results to return a proof @@ -42,7 +42,11 @@ func NewResultFromResponse(response *abci.ResponseDeliverTx) ABCIResult { // Bytes serializes the ABCIResponse using go-wire func (a ABCIResults) Bytes() []byte { - return wire.BinaryBytes(a) + bz, err := wire.MarshalBinary(a) + if err != nil { + panic(err) + } + return bz } // Hash returns a merkle hash of all results diff --git a/types/signable.go b/types/signable.go index bfdf9faa..1edaf020 100644 --- a/types/signable.go +++ b/types/signable.go @@ -1,24 +1,20 @@ package types import ( - "bytes" - "io" - - cmn "github.com/tendermint/tmlibs/common" + "github.com/tendermint/tmlibs/merkle" ) // Signable is an interface for all signable things. // It typically removes signatures before serializing. +// SignBytes returns the bytes to be signed +// NOTE: chainIDs are part of the SignBytes but not +// necessarily the object themselves. +// NOTE: Expected to panic if there is an error marshalling. type Signable interface { - WriteSignBytes(chainID string, w io.Writer, n *int, err *error) + SignBytes(chainID string) []byte } -// SignBytes is a convenience method for getting the bytes to sign of a Signable. -func SignBytes(chainID string, o Signable) []byte { - buf, n, err := new(bytes.Buffer), new(int), new(error) - o.WriteSignBytes(chainID, buf, n, err) - if *err != nil { - cmn.PanicCrisis(err) - } - 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(o.SignBytes(chainID)) } diff --git a/types/test_util.go b/types/test_util.go index d13de04e..73e53eb1 100644 --- a/types/test_util.go +++ b/types/test_util.go @@ -29,7 +29,7 @@ func MakeCommit(blockID BlockID, height int64, round int, } func signAddVote(privVal *PrivValidatorFS, vote *Vote, voteSet *VoteSet) (signed bool, err error) { - vote.Signature, err = privVal.Signer.Sign(SignBytes(voteSet.ChainID(), vote)) + vote.Signature, err = privVal.Signer.Sign(vote.SignBytes(voteSet.ChainID())) if err != nil { return false, err } diff --git a/types/validator.go b/types/validator.go index dfe57551..027f7dc3 100644 --- a/types/validator.go +++ b/types/validator.go @@ -3,10 +3,8 @@ package types import ( "bytes" "fmt" - "io" "github.com/tendermint/go-crypto" - "github.com/tendermint/go-wire" cmn "github.com/tendermint/tmlibs/common" ) @@ -14,9 +12,9 @@ import ( // NOTE: The Accum is not included in Validator.Hash(); // make sure to update that method if changes are made here type Validator struct { - Address Address `json:"address"` - PubKey crypto.PubKey `json:"pub_key"` - VotingPower int64 `json:"voting_power"` + Address Address `json:"address"` + PubKey crypto.PubKey `json:"pub_key"` + VotingPower int64 `json:"voting_power"` Accum int64 `json:"accum"` } @@ -72,7 +70,7 @@ func (v *Validator) String() string { // Hash computes the unique ID of a validator with a given voting power. // It excludes the Accum value, which changes with every round. func (v *Validator) Hash() []byte { - return wire.BinaryRipemd160(struct { + return tmHash(struct { Address Address PubKey crypto.PubKey VotingPower int64 @@ -83,25 +81,6 @@ func (v *Validator) Hash() []byte { }) } -//------------------------------------- - -var ValidatorCodec = validatorCodec{} - -type validatorCodec struct{} - -func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int, err *error) { - wire.WriteBinary(o.(*Validator), w, n, err) -} - -func (vc validatorCodec) Decode(r io.Reader, n *int, err *error) interface{} { - return wire.ReadBinary(&Validator{}, r, 0, n, err) -} - -func (vc validatorCodec) Compare(o1 interface{}, o2 interface{}) int { - cmn.PanicSanity("ValidatorCodec.Compare not implemented") - return 0 -} - //-------------------------------------------------------------------------------- // For testing... diff --git a/types/validator_set.go b/types/validator_set.go index 0d6c3249..83d066ec 100644 --- a/types/validator_set.go +++ b/types/validator_set.go @@ -253,7 +253,7 @@ func (valSet *ValidatorSet) VerifyCommit(chainID string, blockID BlockID, height } _, val := valSet.GetByIndex(idx) // Validate signature - precommitSignBytes := SignBytes(chainID, precommit) + precommitSignBytes := precommit.SignBytes(chainID) if !val.PubKey.VerifyBytes(precommitSignBytes, precommit.Signature) { return fmt.Errorf("Invalid commit -- invalid signature: %v", precommit) } @@ -327,7 +327,7 @@ func (valSet *ValidatorSet) VerifyCommitAny(newSet *ValidatorSet, chainID string seen[vi] = true // Validate signature old school - precommitSignBytes := SignBytes(chainID, precommit) + precommitSignBytes := precommit.SignBytes(chainID) if !ov.PubKey.VerifyBytes(precommitSignBytes, precommit.Signature) { return errors.Errorf("Invalid commit -- invalid signature: %v", precommit) } diff --git a/types/vote.go b/types/vote.go index 7b069f2f..b013d1ce 100644 --- a/types/vote.go +++ b/types/vote.go @@ -4,7 +4,6 @@ import ( "bytes" "errors" "fmt" - "io" "time" "github.com/tendermint/go-crypto" @@ -73,11 +72,15 @@ type Vote struct { Signature crypto.Signature `json:"signature"` } -func (vote *Vote) WriteSignBytes(chainID string, w io.Writer, n *int, err *error) { - wire.WriteJSON(CanonicalJSONOnceVote{ +func (vote *Vote) SignBytes(chainID string) []byte { + bz, err := wire.MarshalJSON(CanonicalJSONOnceVote{ chainID, CanonicalVote(vote), - }, w, n, err) + }) + if err != nil { + panic(err) + } + return bz } func (vote *Vote) Copy() *Vote { @@ -111,7 +114,7 @@ func (vote *Vote) Verify(chainID string, pubKey crypto.PubKey) error { return ErrVoteInvalidValidatorAddress } - if !pubKey.VerifyBytes(SignBytes(chainID, vote), vote.Signature) { + if !pubKey.VerifyBytes(vote.SignBytes(chainID), vote.Signature) { return ErrVoteInvalidSignature } return nil