github.com/MetalBlockchain/metalgo@v1.11.9/vms/rpcchainvm/with_context_vm_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 rpcchainvm
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/require"
    12  	"go.uber.org/mock/gomock"
    13  
    14  	"github.com/MetalBlockchain/metalgo/database/memdb"
    15  	"github.com/MetalBlockchain/metalgo/ids"
    16  	"github.com/MetalBlockchain/metalgo/snow/consensus/snowman"
    17  	"github.com/MetalBlockchain/metalgo/snow/consensus/snowman/snowmantest"
    18  	"github.com/MetalBlockchain/metalgo/snow/engine/snowman/block"
    19  	"github.com/MetalBlockchain/metalgo/snow/snowtest"
    20  )
    21  
    22  var (
    23  	_ block.ChainVM                      = ContextEnabledVMMock{}
    24  	_ block.BuildBlockWithContextChainVM = ContextEnabledVMMock{}
    25  
    26  	_ snowman.Block           = ContextEnabledBlockMock{}
    27  	_ block.WithVerifyContext = ContextEnabledBlockMock{}
    28  
    29  	blockContext = &block.Context{
    30  		PChainHeight: 1,
    31  	}
    32  
    33  	blkID    = ids.ID{1}
    34  	parentID = ids.ID{0}
    35  	blkBytes = []byte{0}
    36  )
    37  
    38  type ContextEnabledVMMock struct {
    39  	*block.MockChainVM
    40  	*block.MockBuildBlockWithContextChainVM
    41  }
    42  
    43  type ContextEnabledBlockMock struct {
    44  	*snowmantest.MockBlock
    45  	*block.MockWithVerifyContext
    46  }
    47  
    48  func contextEnabledTestPlugin(t *testing.T, loadExpectations bool) block.ChainVM {
    49  	// test key is "contextTestKey"
    50  
    51  	// create mock
    52  	ctrl := gomock.NewController(t)
    53  	ctxVM := ContextEnabledVMMock{
    54  		MockChainVM:                      block.NewMockChainVM(ctrl),
    55  		MockBuildBlockWithContextChainVM: block.NewMockBuildBlockWithContextChainVM(ctrl),
    56  	}
    57  
    58  	if loadExpectations {
    59  		ctxBlock := ContextEnabledBlockMock{
    60  			MockBlock:             snowmantest.NewMockBlock(ctrl),
    61  			MockWithVerifyContext: block.NewMockWithVerifyContext(ctrl),
    62  		}
    63  		gomock.InOrder(
    64  			// Initialize
    65  			ctxVM.MockChainVM.EXPECT().Initialize(
    66  				gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(),
    67  				gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(),
    68  				gomock.Any(),
    69  			).Return(nil).Times(1),
    70  			ctxVM.MockChainVM.EXPECT().LastAccepted(gomock.Any()).Return(preSummaryBlk.ID(), nil).Times(1),
    71  			ctxVM.MockChainVM.EXPECT().GetBlock(gomock.Any(), gomock.Any()).Return(preSummaryBlk, nil).Times(1),
    72  
    73  			// BuildBlockWithContext
    74  			ctxVM.MockBuildBlockWithContextChainVM.EXPECT().BuildBlockWithContext(gomock.Any(), blockContext).Return(ctxBlock, nil).Times(1),
    75  			ctxBlock.MockWithVerifyContext.EXPECT().ShouldVerifyWithContext(gomock.Any()).Return(true, nil).Times(1),
    76  			ctxBlock.MockBlock.EXPECT().ID().Return(blkID).Times(1),
    77  			ctxBlock.MockBlock.EXPECT().Parent().Return(parentID).Times(1),
    78  			ctxBlock.MockBlock.EXPECT().Bytes().Return(blkBytes).Times(1),
    79  			ctxBlock.MockBlock.EXPECT().Height().Return(uint64(1)).Times(1),
    80  			ctxBlock.MockBlock.EXPECT().Timestamp().Return(time.Now()).Times(1),
    81  
    82  			// VerifyWithContext
    83  			ctxVM.MockChainVM.EXPECT().ParseBlock(gomock.Any(), blkBytes).Return(ctxBlock, nil).Times(1),
    84  			ctxBlock.MockWithVerifyContext.EXPECT().VerifyWithContext(gomock.Any(), blockContext).Return(nil).Times(1),
    85  			ctxBlock.MockBlock.EXPECT().Timestamp().Return(time.Now()).Times(1),
    86  		)
    87  	}
    88  
    89  	return ctxVM
    90  }
    91  
    92  func TestContextVMSummary(t *testing.T) {
    93  	require := require.New(t)
    94  	testKey := contextTestKey
    95  
    96  	// Create and start the plugin
    97  	vm, stopper := buildClientHelper(require, testKey)
    98  	defer stopper.Stop(context.Background())
    99  
   100  	ctx := snowtest.Context(t, snowtest.CChainID)
   101  
   102  	require.NoError(vm.Initialize(context.Background(), ctx, memdb.New(), nil, nil, nil, nil, nil, nil))
   103  
   104  	blkIntf, err := vm.BuildBlockWithContext(context.Background(), blockContext)
   105  	require.NoError(err)
   106  
   107  	blk, ok := blkIntf.(block.WithVerifyContext)
   108  	require.True(ok)
   109  
   110  	shouldVerify, err := blk.ShouldVerifyWithContext(context.Background())
   111  	require.NoError(err)
   112  	require.True(shouldVerify)
   113  
   114  	require.NoError(blk.VerifyWithContext(context.Background(), blockContext))
   115  }