consensus: assume non-nil message in decodeData

`nil` message is occuring only in tests to check that
no unnecessary decoding is done on errors.
This commit is contained in:
Evgenii Stratonikov 2020-11-17 15:49:54 +03:00
parent 445354012a
commit 3025b42c65
3 changed files with 4 additions and 8 deletions

View file

@ -340,7 +340,7 @@ func (t messageType) String() string {
// decode data of payload into it's message // decode data of payload into it's message
func (p *Payload) decodeData() error { func (p *Payload) decodeData() error {
m := new(message) m := p.message
br := io.NewBinReaderFromBuf(p.data) br := io.NewBinReaderFromBuf(p.data)
m.DecodeBinary(br) m.DecodeBinary(br)
if br.Err != nil { if br.Err != nil {

View file

@ -102,6 +102,7 @@ func TestConsensusPayload_Serializable(t *testing.T) {
require.NoError(t, testserdes.DecodeBinary(data, actual)) require.NoError(t, testserdes.DecodeBinary(data, actual))
// message is nil after decoding as we didn't yet call decodeData // message is nil after decoding as we didn't yet call decodeData
require.Nil(t, actual.message) require.Nil(t, actual.message)
actual.message = new(message)
// message should now be decoded from actual.data byte array // message should now be decoded from actual.data byte array
assert.NoError(t, actual.decodeData()) assert.NoError(t, actual.decodeData())
assert.NotNil(t, actual.MarshalUnsigned()) assert.NotNil(t, actual.MarshalUnsigned())
@ -152,7 +153,7 @@ func TestConsensusPayload_DecodeBinaryInvalid(t *testing.T) {
buf[delimeterIndex] = 1 buf[delimeterIndex] = 1
buf[lenIndex] = 34 buf[lenIndex] = 34
buf[typeIndex] = byte(prepareResponseType) buf[typeIndex] = byte(prepareResponseType)
p := new(Payload) p := &Payload{message: new(message)}
require.NoError(t, testserdes.DecodeBinary(buf, p)) require.NoError(t, testserdes.DecodeBinary(buf, p))
// decode `data` into `message` // decode `data` into `message`
_ = p.Hash() _ = p.Hash()
@ -161,7 +162,7 @@ func TestConsensusPayload_DecodeBinaryInvalid(t *testing.T) {
// invalid type // invalid type
buf[typeIndex] = 0xFF buf[typeIndex] = 0xFF
actual := new(Payload) actual := &Payload{message: new(message)}
require.NoError(t, testserdes.DecodeBinary(buf, actual)) require.NoError(t, testserdes.DecodeBinary(buf, actual))
require.Error(t, actual.decodeData()) require.Error(t, actual.decodeData())

View file

@ -23,7 +23,6 @@ func TestRecoveryMessage_Setters(t *testing.T) {
r := &recoveryMessage{} r := &recoveryMessage{}
p := NewPayload(netmode.UnitTestNet) p := NewPayload(netmode.UnitTestNet)
p.message = &message{}
p.SetType(payload.RecoveryMessageType) p.SetType(payload.RecoveryMessageType)
p.SetPayload(r) p.SetPayload(r)
// sign payload to have verification script // sign payload to have verification script
@ -35,7 +34,6 @@ func TestRecoveryMessage_Setters(t *testing.T) {
transactionHashes: []util.Uint256{{1}}, transactionHashes: []util.Uint256{{1}},
} }
p1 := NewPayload(netmode.UnitTestNet) p1 := NewPayload(netmode.UnitTestNet)
p1.message = &message{}
p1.SetType(payload.PrepareRequestType) p1.SetType(payload.PrepareRequestType)
p1.SetPayload(req) p1.SetPayload(req)
p1.SetValidatorIndex(0) p1.SetValidatorIndex(0)
@ -43,7 +41,6 @@ func TestRecoveryMessage_Setters(t *testing.T) {
t.Run("prepare response is added", func(t *testing.T) { t.Run("prepare response is added", func(t *testing.T) {
p2 := NewPayload(netmode.UnitTestNet) p2 := NewPayload(netmode.UnitTestNet)
p2.message = &message{}
p2.SetType(payload.PrepareResponseType) p2.SetType(payload.PrepareResponseType)
p2.SetPayload(&prepareResponse{ p2.SetPayload(&prepareResponse{
preparationHash: p1.Hash(), preparationHash: p1.Hash(),
@ -80,7 +77,6 @@ func TestRecoveryMessage_Setters(t *testing.T) {
t.Run("change view is added", func(t *testing.T) { t.Run("change view is added", func(t *testing.T) {
p3 := NewPayload(netmode.UnitTestNet) p3 := NewPayload(netmode.UnitTestNet)
p3.message = &message{}
p3.SetType(payload.ChangeViewType) p3.SetType(payload.ChangeViewType)
p3.SetPayload(&changeView{ p3.SetPayload(&changeView{
newViewNumber: 1, newViewNumber: 1,
@ -103,7 +99,6 @@ func TestRecoveryMessage_Setters(t *testing.T) {
t.Run("commit is added", func(t *testing.T) { t.Run("commit is added", func(t *testing.T) {
p4 := NewPayload(netmode.UnitTestNet) p4 := NewPayload(netmode.UnitTestNet)
p4.message = &message{}
p4.SetType(payload.CommitType) p4.SetType(payload.CommitType)
p4.SetPayload(randomMessage(t, commitType)) p4.SetPayload(randomMessage(t, commitType))
p4.SetValidatorIndex(3) p4.SetValidatorIndex(3)