From 3025b42c65298e2d27b854aed949baba263d00cb Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 17 Nov 2020 15:49:54 +0300 Subject: [PATCH] consensus: assume non-nil message in `decodeData` `nil` message is occuring only in tests to check that no unnecessary decoding is done on errors. --- pkg/consensus/payload.go | 2 +- pkg/consensus/payload_test.go | 5 +++-- pkg/consensus/recovery_message_test.go | 5 ----- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/pkg/consensus/payload.go b/pkg/consensus/payload.go index 3b00979ad..33c29161b 100644 --- a/pkg/consensus/payload.go +++ b/pkg/consensus/payload.go @@ -340,7 +340,7 @@ func (t messageType) String() string { // decode data of payload into it's message func (p *Payload) decodeData() error { - m := new(message) + m := p.message br := io.NewBinReaderFromBuf(p.data) m.DecodeBinary(br) if br.Err != nil { diff --git a/pkg/consensus/payload_test.go b/pkg/consensus/payload_test.go index 1724715c8..e74803c57 100644 --- a/pkg/consensus/payload_test.go +++ b/pkg/consensus/payload_test.go @@ -102,6 +102,7 @@ func TestConsensusPayload_Serializable(t *testing.T) { require.NoError(t, testserdes.DecodeBinary(data, actual)) // message is nil after decoding as we didn't yet call decodeData require.Nil(t, actual.message) + actual.message = new(message) // message should now be decoded from actual.data byte array assert.NoError(t, actual.decodeData()) assert.NotNil(t, actual.MarshalUnsigned()) @@ -152,7 +153,7 @@ func TestConsensusPayload_DecodeBinaryInvalid(t *testing.T) { buf[delimeterIndex] = 1 buf[lenIndex] = 34 buf[typeIndex] = byte(prepareResponseType) - p := new(Payload) + p := &Payload{message: new(message)} require.NoError(t, testserdes.DecodeBinary(buf, p)) // decode `data` into `message` _ = p.Hash() @@ -161,7 +162,7 @@ func TestConsensusPayload_DecodeBinaryInvalid(t *testing.T) { // invalid type buf[typeIndex] = 0xFF - actual := new(Payload) + actual := &Payload{message: new(message)} require.NoError(t, testserdes.DecodeBinary(buf, actual)) require.Error(t, actual.decodeData()) diff --git a/pkg/consensus/recovery_message_test.go b/pkg/consensus/recovery_message_test.go index b5ef23ceb..47471a0e6 100644 --- a/pkg/consensus/recovery_message_test.go +++ b/pkg/consensus/recovery_message_test.go @@ -23,7 +23,6 @@ func TestRecoveryMessage_Setters(t *testing.T) { r := &recoveryMessage{} p := NewPayload(netmode.UnitTestNet) - p.message = &message{} p.SetType(payload.RecoveryMessageType) p.SetPayload(r) // sign payload to have verification script @@ -35,7 +34,6 @@ func TestRecoveryMessage_Setters(t *testing.T) { transactionHashes: []util.Uint256{{1}}, } p1 := NewPayload(netmode.UnitTestNet) - p1.message = &message{} p1.SetType(payload.PrepareRequestType) p1.SetPayload(req) p1.SetValidatorIndex(0) @@ -43,7 +41,6 @@ func TestRecoveryMessage_Setters(t *testing.T) { t.Run("prepare response is added", func(t *testing.T) { p2 := NewPayload(netmode.UnitTestNet) - p2.message = &message{} p2.SetType(payload.PrepareResponseType) p2.SetPayload(&prepareResponse{ preparationHash: p1.Hash(), @@ -80,7 +77,6 @@ func TestRecoveryMessage_Setters(t *testing.T) { t.Run("change view is added", func(t *testing.T) { p3 := NewPayload(netmode.UnitTestNet) - p3.message = &message{} p3.SetType(payload.ChangeViewType) p3.SetPayload(&changeView{ newViewNumber: 1, @@ -103,7 +99,6 @@ func TestRecoveryMessage_Setters(t *testing.T) { t.Run("commit is added", func(t *testing.T) { p4 := NewPayload(netmode.UnitTestNet) - p4.message = &message{} p4.SetType(payload.CommitType) p4.SetPayload(randomMessage(t, commitType)) p4.SetValidatorIndex(3)