github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/block/proposal_block_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package block
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/MetalBlockchain/metalgo/ids"
    13  	"github.com/MetalBlockchain/metalgo/vms/platformvm/txs"
    14  )
    15  
    16  func TestNewBanffProposalBlock(t *testing.T) {
    17  	timestamp := time.Now().Truncate(time.Second)
    18  	parentID := ids.GenerateTestID()
    19  	height := uint64(1337)
    20  	proposalTx, err := testProposalTx()
    21  	require.NoError(t, err)
    22  	decisionTxs, err := testDecisionTxs()
    23  	require.NoError(t, err)
    24  
    25  	type test struct {
    26  		name        string
    27  		proposalTx  *txs.Tx
    28  		decisionTxs []*txs.Tx
    29  	}
    30  
    31  	tests := []test{
    32  		{
    33  			name:        "no decision txs",
    34  			proposalTx:  proposalTx,
    35  			decisionTxs: []*txs.Tx{},
    36  		},
    37  		{
    38  			name:        "decision txs",
    39  			proposalTx:  proposalTx,
    40  			decisionTxs: decisionTxs,
    41  		},
    42  	}
    43  
    44  	for _, test := range tests {
    45  		t.Run(test.name, func(t *testing.T) {
    46  			require := require.New(t)
    47  
    48  			blk, err := NewBanffProposalBlock(
    49  				timestamp,
    50  				parentID,
    51  				height,
    52  				test.proposalTx,
    53  				test.decisionTxs,
    54  			)
    55  			require.NoError(err)
    56  
    57  			require.NotEmpty(blk.Bytes())
    58  			require.Equal(parentID, blk.Parent())
    59  			require.Equal(height, blk.Height())
    60  			require.Equal(timestamp, blk.Timestamp())
    61  
    62  			l := len(test.decisionTxs)
    63  			expectedTxs := make([]*txs.Tx, l+1)
    64  			copy(expectedTxs, test.decisionTxs)
    65  			expectedTxs[l] = test.proposalTx
    66  
    67  			blkTxs := blk.Txs()
    68  			require.Equal(expectedTxs, blkTxs)
    69  			for i, blkTx := range blkTxs {
    70  				expectedTx := expectedTxs[i]
    71  				require.NotEmpty(blkTx.Bytes())
    72  				require.NotEqual(ids.Empty, blkTx.ID())
    73  				require.Equal(expectedTx.Bytes(), blkTx.Bytes())
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  func TestNewApricotProposalBlock(t *testing.T) {
    80  	require := require.New(t)
    81  
    82  	parentID := ids.GenerateTestID()
    83  	height := uint64(1337)
    84  	proposalTx, err := testProposalTx()
    85  	require.NoError(err)
    86  
    87  	blk, err := NewApricotProposalBlock(
    88  		parentID,
    89  		height,
    90  		proposalTx,
    91  	)
    92  	require.NoError(err)
    93  
    94  	require.NotEmpty(blk.Bytes())
    95  	require.Equal(parentID, blk.Parent())
    96  	require.Equal(height, blk.Height())
    97  
    98  	expectedTxs := []*txs.Tx{proposalTx}
    99  
   100  	blkTxs := blk.Txs()
   101  	require.Equal(expectedTxs, blkTxs)
   102  	for i, blkTx := range blkTxs {
   103  		expectedTx := expectedTxs[i]
   104  		require.NotEmpty(blkTx.Bytes())
   105  		require.NotEqual(ids.Empty, blkTx.ID())
   106  		require.Equal(expectedTx.Bytes(), blkTx.Bytes())
   107  	}
   108  }