github.com/MetalBlockchain/metalgo@v1.11.9/vms/components/verify/subnet_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 verify
     5  
     6  import (
     7  	"context"
     8  	"errors"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  	"go.uber.org/mock/gomock"
    13  
    14  	"github.com/MetalBlockchain/metalgo/ids"
    15  	"github.com/MetalBlockchain/metalgo/snow"
    16  	"github.com/MetalBlockchain/metalgo/snow/validators"
    17  )
    18  
    19  var errMissing = errors.New("missing")
    20  
    21  func TestSameSubnet(t *testing.T) {
    22  	subnetID0 := ids.GenerateTestID()
    23  	subnetID1 := ids.GenerateTestID()
    24  	chainID0 := ids.GenerateTestID()
    25  	chainID1 := ids.GenerateTestID()
    26  
    27  	tests := []struct {
    28  		name    string
    29  		ctxF    func(*gomock.Controller) *snow.Context
    30  		chainID ids.ID
    31  		result  error
    32  	}{
    33  		{
    34  			name: "same chain",
    35  			ctxF: func(ctrl *gomock.Controller) *snow.Context {
    36  				state := validators.NewMockState(ctrl)
    37  				return &snow.Context{
    38  					SubnetID:       subnetID0,
    39  					ChainID:        chainID0,
    40  					ValidatorState: state,
    41  				}
    42  			},
    43  			chainID: chainID0,
    44  			result:  ErrSameChainID,
    45  		},
    46  		{
    47  			name: "unknown chain",
    48  			ctxF: func(ctrl *gomock.Controller) *snow.Context {
    49  				state := validators.NewMockState(ctrl)
    50  				state.EXPECT().GetSubnetID(gomock.Any(), chainID1).Return(subnetID1, errMissing)
    51  				return &snow.Context{
    52  					SubnetID:       subnetID0,
    53  					ChainID:        chainID0,
    54  					ValidatorState: state,
    55  				}
    56  			},
    57  			chainID: chainID1,
    58  			result:  errMissing,
    59  		},
    60  		{
    61  			name: "wrong subnet",
    62  			ctxF: func(ctrl *gomock.Controller) *snow.Context {
    63  				state := validators.NewMockState(ctrl)
    64  				state.EXPECT().GetSubnetID(gomock.Any(), chainID1).Return(subnetID1, nil)
    65  				return &snow.Context{
    66  					SubnetID:       subnetID0,
    67  					ChainID:        chainID0,
    68  					ValidatorState: state,
    69  				}
    70  			},
    71  			chainID: chainID1,
    72  			result:  ErrMismatchedSubnetIDs,
    73  		},
    74  		{
    75  			name: "same subnet",
    76  			ctxF: func(ctrl *gomock.Controller) *snow.Context {
    77  				state := validators.NewMockState(ctrl)
    78  				state.EXPECT().GetSubnetID(gomock.Any(), chainID1).Return(subnetID0, nil)
    79  				return &snow.Context{
    80  					SubnetID:       subnetID0,
    81  					ChainID:        chainID0,
    82  					ValidatorState: state,
    83  				}
    84  			},
    85  			chainID: chainID1,
    86  			result:  nil,
    87  		},
    88  	}
    89  	for _, test := range tests {
    90  		t.Run(test.name, func(t *testing.T) {
    91  			ctrl := gomock.NewController(t)
    92  			ctx := test.ctxF(ctrl)
    93  
    94  			result := SameSubnet(context.Background(), ctx, test.chainID)
    95  			require.ErrorIs(t, result, test.result)
    96  		})
    97  	}
    98  }