github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/block/standard_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/components/avax"
    14  	"github.com/MetalBlockchain/metalgo/vms/components/verify"
    15  	"github.com/MetalBlockchain/metalgo/vms/platformvm/txs"
    16  	"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
    17  )
    18  
    19  func TestNewBanffStandardBlock(t *testing.T) {
    20  	require := require.New(t)
    21  
    22  	timestamp := time.Now().Truncate(time.Second)
    23  	parentID := ids.GenerateTestID()
    24  	height := uint64(1337)
    25  
    26  	tx := &txs.Tx{
    27  		Unsigned: &txs.AddValidatorTx{
    28  			BaseTx: txs.BaseTx{
    29  				BaseTx: avax.BaseTx{
    30  					Ins:  []*avax.TransferableInput{},
    31  					Outs: []*avax.TransferableOutput{},
    32  				},
    33  			},
    34  			StakeOuts: []*avax.TransferableOutput{},
    35  			Validator: txs.Validator{},
    36  			RewardsOwner: &secp256k1fx.OutputOwners{
    37  				Addrs: []ids.ShortID{},
    38  			},
    39  		},
    40  		Creds: []verify.Verifiable{},
    41  	}
    42  	require.NoError(tx.Initialize(txs.Codec))
    43  
    44  	blk, err := NewBanffStandardBlock(
    45  		timestamp,
    46  		parentID,
    47  		height,
    48  		[]*txs.Tx{tx},
    49  	)
    50  	require.NoError(err)
    51  
    52  	// Make sure the block and tx are initialized
    53  	require.NotEmpty(blk.Bytes())
    54  	require.NotEmpty(blk.Transactions[0].Bytes())
    55  	require.NotEqual(ids.Empty, blk.Transactions[0].ID())
    56  	require.Equal(tx.Bytes(), blk.Transactions[0].Bytes())
    57  	require.Equal(timestamp, blk.Timestamp())
    58  	require.Equal(parentID, blk.Parent())
    59  	require.Equal(height, blk.Height())
    60  }
    61  
    62  func TestNewApricotStandardBlock(t *testing.T) {
    63  	require := require.New(t)
    64  
    65  	parentID := ids.GenerateTestID()
    66  	height := uint64(1337)
    67  
    68  	tx := &txs.Tx{
    69  		Unsigned: &txs.AddValidatorTx{
    70  			BaseTx: txs.BaseTx{
    71  				BaseTx: avax.BaseTx{
    72  					Ins:  []*avax.TransferableInput{},
    73  					Outs: []*avax.TransferableOutput{},
    74  				},
    75  			},
    76  			StakeOuts: []*avax.TransferableOutput{},
    77  			Validator: txs.Validator{},
    78  			RewardsOwner: &secp256k1fx.OutputOwners{
    79  				Addrs: []ids.ShortID{},
    80  			},
    81  		},
    82  		Creds: []verify.Verifiable{},
    83  	}
    84  	require.NoError(tx.Initialize(txs.Codec))
    85  
    86  	blk, err := NewApricotStandardBlock(
    87  		parentID,
    88  		height,
    89  		[]*txs.Tx{tx},
    90  	)
    91  	require.NoError(err)
    92  
    93  	// Make sure the block and tx are initialized
    94  	require.NotEmpty(blk.Bytes())
    95  	require.NotEmpty(blk.Transactions[0].Bytes())
    96  	require.NotEqual(ids.Empty, blk.Transactions[0].ID())
    97  	require.Equal(tx.Bytes(), blk.Transactions[0].Bytes())
    98  	require.Equal(parentID, blk.Parent())
    99  	require.Equal(height, blk.Height())
   100  }