diff --git a/consensus/types/proposal.go b/consensus/types/proposal.go index 79a3f001..c9dbad1a 100644 --- a/consensus/types/proposal.go +++ b/consensus/types/proposal.go @@ -7,6 +7,7 @@ import ( "github.com/tendermint/tendermint/account" "github.com/tendermint/tendermint/binary" + . "github.com/tendermint/tendermint/common" "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/types" ) @@ -39,9 +40,11 @@ func (p *Proposal) String() string { } func (p *Proposal) WriteSignBytes(w io.Writer, n *int64, err *error) { - binary.WriteString(config.App().GetString("Network"), w, n, err) - binary.WriteUvarint(p.Height, w, n, err) - binary.WriteUvarint(p.Round, w, n, err) - binary.WriteBinary(p.BlockParts, w, n, err) - binary.WriteBinary(p.POLParts, w, n, err) + // We hex encode the network name so we don't deal with escaping issues. + binary.WriteTo([]byte(Fmt(`{"Network":"%X"`, config.App().GetString("Network"))), w, n, err) + binary.WriteTo([]byte(`,"Proprosal":{"BlockParts":`), w, n, err) + p.BlockParts.WriteSignBytes(w, n, err) + binary.WriteTo([]byte(Fmt(`,"Height":%v,"POLParts":`, p.Height)), w, n, err) + p.POLParts.WriteSignBytes(w, n, err) + binary.WriteTo([]byte(Fmt(`,"Round":%v}}`, p.Round)), w, n, err) } diff --git a/consensus/types/proposal_test.go b/consensus/types/proposal_test.go new file mode 100644 index 00000000..6db16474 --- /dev/null +++ b/consensus/types/proposal_test.go @@ -0,0 +1,27 @@ +package consensus + +import ( + "testing" + + "github.com/tendermint/tendermint/account" + . "github.com/tendermint/tendermint/common" + "github.com/tendermint/tendermint/config" + "github.com/tendermint/tendermint/types" +) + +func TestProposalSignable(t *testing.T) { + proposal := &Proposal{ + Height: 12345, + Round: 23456, + BlockParts: types.PartSetHeader{111, []byte("blockparts")}, + POLParts: types.PartSetHeader{222, []byte("polparts")}, + Signature: nil, + } + signBytes := account.SignBytes(proposal) + signStr := string(signBytes) + expected := Fmt(`{"Network":"%X","Proprosal":{"BlockParts":{"Hash":"626C6F636B7061727473","Total":111},"Height":12345,"POLParts":{"Hash":"706F6C7061727473","Total":222},"Round":23456}}`, + config.App().GetString("Network")) + if signStr != expected { + t.Errorf("Got unexpected sign string for SendTx. Expected:\n%v\nGot:\n%v", expected, signStr) + } +} diff --git a/types/part_set.go b/types/part_set.go index d403809d..1251ea5d 100644 --- a/types/part_set.go +++ b/types/part_set.go @@ -9,6 +9,7 @@ import ( "strings" "sync" + "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" "github.com/tendermint/tendermint/merkle" ) @@ -84,6 +85,10 @@ 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 *int64, err *error) { + binary.WriteTo([]byte(Fmt(`{"Hash":"%X","Total":%v}`, psh.Hash, psh.Total)), w, n, err) +} + //------------------------------------- type PartSet struct {