github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/network/payload/merkleblock_test.go (about)

     1  package payload
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/nspcc-dev/neo-go/internal/testserdes"
     9  	"github.com/nspcc-dev/neo-go/pkg/core/block"
    10  	"github.com/nspcc-dev/neo-go/pkg/core/transaction"
    11  	"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
    12  	"github.com/nspcc-dev/neo-go/pkg/util"
    13  )
    14  
    15  func newDumbBlock() *block.Header {
    16  	return &block.Header{
    17  		Version:       0,
    18  		PrevHash:      hash.Sha256([]byte("a")),
    19  		MerkleRoot:    hash.Sha256([]byte("b")),
    20  		Timestamp:     100500,
    21  		Index:         1,
    22  		NextConsensus: hash.Hash160([]byte("a")),
    23  		Script: transaction.Witness{
    24  			VerificationScript: []byte{0x51}, // PUSH1
    25  			InvocationScript:   []byte{0x61}, // NOP
    26  		},
    27  	}
    28  }
    29  
    30  func TestMerkleBlock_EncodeDecodeBinary(t *testing.T) {
    31  	t.Run("positive", func(t *testing.T) {
    32  		b := newDumbBlock()
    33  		_ = b.Hash()
    34  		expected := &MerkleBlock{
    35  			Header:  b,
    36  			TxCount: 0,
    37  			Hashes:  []util.Uint256{},
    38  			Flags:   []byte{},
    39  		}
    40  		testserdes.EncodeDecodeBinary(t, expected, new(MerkleBlock))
    41  	})
    42  
    43  	t.Run("bad contents count", func(t *testing.T) {
    44  		b := newDumbBlock()
    45  		_ = b.Hash()
    46  		expected := &MerkleBlock{
    47  			Header:  b,
    48  			TxCount: block.MaxTransactionsPerBlock + 1,
    49  			Hashes:  make([]util.Uint256, block.MaxTransactionsPerBlock),
    50  			Flags:   []byte{},
    51  		}
    52  		data, err := testserdes.EncodeBinary(expected)
    53  		require.NoError(t, err)
    54  		require.ErrorIs(t, testserdes.DecodeBinary(data, new(MerkleBlock)), block.ErrMaxContentsPerBlock)
    55  	})
    56  
    57  	t.Run("bad flags size", func(t *testing.T) {
    58  		b := newDumbBlock()
    59  		_ = b.Hash()
    60  		expected := &MerkleBlock{
    61  			Header:  b,
    62  			TxCount: 0,
    63  			Hashes:  []util.Uint256{},
    64  			Flags:   []byte{1, 2, 3, 4, 5},
    65  		}
    66  		data, err := testserdes.EncodeBinary(expected)
    67  		require.NoError(t, err)
    68  		require.Error(t, testserdes.DecodeBinary(data, new(MerkleBlock)))
    69  	})
    70  }