diff --git a/types/evidence_test.go b/types/evidence_test.go index 876b68ad..84811514 100644 --- a/types/evidence_test.go +++ b/types/evidence_test.go @@ -22,7 +22,7 @@ func makeVote(val *PrivValidatorFS, chainID string, valIndex int, height int64, Type: byte(step), BlockID: blockID, } - sig := val.PrivKey.Sign(SignBytes(chainID, v)) + sig := val.PrivKey.Sign(v.SignBytes(chainID)) v.Signature = sig return v @@ -41,7 +41,7 @@ func TestEvidence(t *testing.T) { vote1 := makeVote(val, chainID, 0, 10, 2, 1, blockID) badVote := makeVote(val, chainID, 0, 10, 2, 1, blockID) - badVote.Signature = val2.PrivKey.Sign(SignBytes(chainID, badVote)) + badVote.Signature = val2.PrivKey.Sign(badVote.SignBytes(chainID)) cases := []voteData{ {vote1, makeVote(val, chainID, 0, 10, 2, 1, blockID2), true}, // different block ids diff --git a/types/heartbeat_test.go b/types/heartbeat_test.go index 660ccd0f..20663616 100644 --- a/types/heartbeat_test.go +++ b/types/heartbeat_test.go @@ -1,7 +1,6 @@ package types import ( - "bytes" "testing" "github.com/stretchr/testify/require" @@ -34,23 +33,18 @@ func TestHeartbeatString(t *testing.T) { } func TestHeartbeatWriteSignBytes(t *testing.T) { - var n int - var err error - buf := new(bytes.Buffer) hb := &Heartbeat{ValidatorIndex: 1, Height: 10, Round: 1} - hb.WriteSignBytes("0xdeadbeef", buf, &n, &err) - require.Equal(t, buf.String(), `{"chain_id":"0xdeadbeef","heartbeat":{"height":10,"round":1,"sequence":0,"validator_address":"","validator_index":1}}`) + bz := hb.SignBytes("0xdeadbeef") + require.Equal(t, string(bz), `{"chain_id":"0xdeadbeef","heartbeat":{"height":10,"round":1,"sequence":0,"validator_address":"","validator_index":1}}`) - buf.Reset() plainHb := &Heartbeat{} - plainHb.WriteSignBytes("0xdeadbeef", buf, &n, &err) - require.Equal(t, buf.String(), `{"chain_id":"0xdeadbeef","heartbeat":{"height":0,"round":0,"sequence":0,"validator_address":"","validator_index":0}}`) + bz = plainHb.SignBytes("0xdeadbeef") + require.Equal(t, string(bz), `{"chain_id":"0xdeadbeef","heartbeat":{"height":0,"round":0,"sequence":0,"validator_address":"","validator_index":0}}`) require.Panics(t, func() { - buf.Reset() var nilHb *Heartbeat - nilHb.WriteSignBytes("0xdeadbeef", buf, &n, &err) - require.Equal(t, buf.String(), "null") + bz := nilHb.SignBytes("0xdeadbeef") + require.Equal(t, string(bz), "null") }) } diff --git a/types/priv_validator_test.go b/types/priv_validator_test.go index 08b58273..edfcdf58 100644 --- a/types/priv_validator_test.go +++ b/types/priv_validator_test.go @@ -185,18 +185,19 @@ func TestDifferByTimestamp(t *testing.T) { proposal := newProposal(height, round, block1) err := privVal.SignProposal(chainID, proposal) assert.NoError(t, err, "expected no error signing proposal") - signBytes := SignBytes(chainID, proposal) + signBytes := proposal.SignBytes(chainID) sig := proposal.Signature timeStamp := clipToMS(proposal.Timestamp) // manipulate the timestamp. should get changed back proposal.Timestamp = proposal.Timestamp.Add(time.Millisecond) - proposal.Signature = crypto.Signature{} + var emptySig crypto.Signature + proposal.Signature = emptySig err = privVal.SignProposal("mychainid", proposal) assert.NoError(t, err, "expected no error on signing same proposal") assert.Equal(t, timeStamp, proposal.Timestamp) - assert.Equal(t, signBytes, SignBytes(chainID, proposal)) + assert.Equal(t, signBytes, proposal.SignBytes(chainID)) assert.Equal(t, sig, proposal.Signature) } @@ -208,18 +209,19 @@ func TestDifferByTimestamp(t *testing.T) { err := privVal.SignVote("mychainid", vote) assert.NoError(t, err, "expected no error signing vote") - signBytes := SignBytes(chainID, vote) + signBytes := vote.SignBytes(chainID) sig := vote.Signature timeStamp := clipToMS(vote.Timestamp) // manipulate the timestamp. should get changed back vote.Timestamp = vote.Timestamp.Add(time.Millisecond) - vote.Signature = crypto.Signature{} + var emptySig crypto.Signature + vote.Signature = emptySig err = privVal.SignVote("mychainid", vote) assert.NoError(t, err, "expected no error on signing same vote") assert.Equal(t, timeStamp, vote.Timestamp) - assert.Equal(t, signBytes, SignBytes(chainID, vote)) + assert.Equal(t, signBytes, vote.SignBytes(chainID)) assert.Equal(t, sig, vote.Signature) } } diff --git a/types/proposal_test.go b/types/proposal_test.go index 6fbfbba0..bebf37ca 100644 --- a/types/proposal_test.go +++ b/types/proposal_test.go @@ -26,7 +26,7 @@ func init() { } func TestProposalSignable(t *testing.T) { - signBytes := SignBytes("test_chain_id", testProposal) + signBytes := testProposal.SignBytes("test_chain_id") signStr := string(signBytes) expected := `{"chain_id":"test_chain_id","proposal":{"block_parts_header":{"hash":"626C6F636B7061727473","total":111},"height":12345,"pol_block_id":{},"pol_round":-1,"round":23456,"timestamp":"2018-02-11T07:09:22.765Z"}}` @@ -48,24 +48,25 @@ func TestProposalVerifySignature(t *testing.T) { pubKey := privVal.GetPubKey() prop := NewProposal(4, 2, PartSetHeader{777, []byte("proper")}, 2, BlockID{}) - signBytes := SignBytes("test_chain_id", prop) + signBytes := prop.SignBytes("test_chain_id") // sign it signature, err := privVal.Signer.Sign(signBytes) require.NoError(t, err) // verify the same proposal - valid := pubKey.VerifyBytes(SignBytes("test_chain_id", prop), signature) + valid := pubKey.VerifyBytes(prop.SignBytes("test_chain_id"), signature) require.True(t, valid) // serialize, deserialize and verify again.... newProp := new(Proposal) - bs := wire.BinaryBytes(prop) - err = wire.ReadBinaryBytes(bs, &newProp) + bs, err := wire.MarshalBinary(prop) + require.NoError(t, err) + err = wire.UnmarshalBinary(bs, &newProp) require.NoError(t, err) // verify the transmitted proposal - newSignBytes := SignBytes("test_chain_id", newProp) + newSignBytes := newProp.SignBytes("test_chain_id") require.Equal(t, string(signBytes), string(newSignBytes)) valid = pubKey.VerifyBytes(newSignBytes, signature) require.True(t, valid) @@ -73,14 +74,14 @@ func TestProposalVerifySignature(t *testing.T) { func BenchmarkProposalWriteSignBytes(b *testing.B) { for i := 0; i < b.N; i++ { - SignBytes("test_chain_id", testProposal) + testProposal.SignBytes("test_chain_id") } } func BenchmarkProposalSign(b *testing.B) { privVal := GenPrivValidatorFS("") for i := 0; i < b.N; i++ { - _, err := privVal.Signer.Sign(SignBytes("test_chain_id", testProposal)) + _, err := privVal.Signer.Sign(testProposal.SignBytes("test_chain_id")) if err != nil { b.Error(err) } @@ -88,12 +89,12 @@ func BenchmarkProposalSign(b *testing.B) { } func BenchmarkProposalVerifySignature(b *testing.B) { - signBytes := SignBytes("test_chain_id", testProposal) + signBytes := testProposal.SignBytes("test_chain_id") privVal := GenPrivValidatorFS("") signature, _ := privVal.Signer.Sign(signBytes) pubKey := privVal.GetPubKey() for i := 0; i < b.N; i++ { - pubKey.VerifyBytes(SignBytes("test_chain_id", testProposal), signature) + pubKey.VerifyBytes(testProposal.SignBytes("test_chain_id"), signature) } } diff --git a/types/tx_test.go b/types/tx_test.go index dc99509d..2f51a612 100644 --- a/types/tx_test.go +++ b/types/tx_test.go @@ -69,8 +69,9 @@ func TestValidTxProof(t *testing.T) { // read-write must also work var p2 TxProof - bin := wire.BinaryBytes(proof) - err := wire.ReadBinaryBytes(bin, &p2) + bin, err := wire.MarshalBinary(proof) + assert.Nil(err) + err = wire.UnmarshalBinary(bin, &p2) if assert.Nil(err, "%d: %d: %+v", h, i, err) { assert.Nil(p2.Validate(root), "%d: %d", h, i) } @@ -96,7 +97,8 @@ func testTxProofUnchangable(t *testing.T) { // make sure it is valid to start with assert.Nil(proof.Validate(root)) - bin := wire.BinaryBytes(proof) + bin, err := wire.MarshalBinary(proof) + assert.Nil(err) // try mutating the data and make sure nothing breaks for j := 0; j < 500; j++ { @@ -110,7 +112,7 @@ func testTxProofUnchangable(t *testing.T) { // this make sure the proof doesn't deserialize into something valid func assertBadProof(t *testing.T, root []byte, bad []byte, good TxProof) { var proof TxProof - err := wire.ReadBinaryBytes(bad, &proof) + err := wire.UnmarshalBinary(bad, &proof) if err == nil { err = proof.Validate(root) if err == nil { diff --git a/types/validator_set_test.go b/types/validator_set_test.go index 9c751237..3a8649ef 100644 --- a/types/validator_set_test.go +++ b/types/validator_set_test.go @@ -16,7 +16,7 @@ import ( func randPubKey() crypto.PubKey { var pubKey [32]byte copy(pubKey[:], cmn.RandBytes(32)) - return crypto.PubKeyEd25519(pubKey).Wrap() + return crypto.PubKeyEd25519(pubKey) } func randValidator_() *Validator { @@ -291,19 +291,17 @@ func BenchmarkValidatorSetCopy(b *testing.B) { } func (valSet *ValidatorSet) toBytes() []byte { - buf, n, err := new(bytes.Buffer), new(int), new(error) - wire.WriteBinary(valSet, buf, n, err) - if *err != nil { - cmn.PanicCrisis(*err) + bz, err := wire.MarshalBinary(valSet) + if err != nil { + panic(err) } - return buf.Bytes() + return bz } func (valSet *ValidatorSet) fromBytes(b []byte) { - r, n, err := bytes.NewReader(b), new(int), new(error) - wire.ReadBinary(valSet, r, 0, n, err) - if *err != nil { + err := wire.UnmarshalBinary(b, valSet) + if err != nil { // DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED - cmn.PanicCrisis(*err) + panic(err) } } diff --git a/types/vote_test.go b/types/vote_test.go index 5e2d5c0d..1da1ec7f 100644 --- a/types/vote_test.go +++ b/types/vote_test.go @@ -42,7 +42,7 @@ func exampleVote(t byte) *Vote { func TestVoteSignable(t *testing.T) { vote := examplePrecommit() - signBytes := SignBytes("test_chain_id", vote) + signBytes := vote.SignBytes("test_chain_id") signStr := string(signBytes) expected := `{"chain_id":"test_chain_id","vote":{"block_id":{"hash":"68617368","parts":{"hash":"70617274735F68617368","total":1000000}},"height":12345,"round":2,"timestamp":"2017-12-25T03:00:01.234Z","type":2}}` @@ -77,24 +77,25 @@ func TestVoteVerifySignature(t *testing.T) { pubKey := privVal.GetPubKey() vote := examplePrecommit() - signBytes := SignBytes("test_chain_id", vote) + signBytes := vote.SignBytes("test_chain_id") // sign it signature, err := privVal.Signer.Sign(signBytes) require.NoError(t, err) // verify the same vote - valid := pubKey.VerifyBytes(SignBytes("test_chain_id", vote), signature) + valid := pubKey.VerifyBytes(vote.SignBytes("test_chain_id"), signature) require.True(t, valid) // serialize, deserialize and verify again.... precommit := new(Vote) - bs := wire.BinaryBytes(vote) - err = wire.ReadBinaryBytes(bs, &precommit) + bs, err := wire.MarshalBinary(vote) + require.NoError(t, err) + err = wire.UnmarshalBinary(bs, &precommit) require.NoError(t, err) // verify the transmitted vote - newSignBytes := SignBytes("test_chain_id", precommit) + newSignBytes := precommit.SignBytes("test_chain_id") require.Equal(t, string(signBytes), string(newSignBytes)) valid = pubKey.VerifyBytes(newSignBytes, signature) require.True(t, valid)