github.com/MetalBlockchain/metalgo@v1.11.9/snow/engine/snowman/syncer/utils_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 syncer
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/MetalBlockchain/metalgo/database"
    14  	"github.com/MetalBlockchain/metalgo/ids"
    15  	"github.com/MetalBlockchain/metalgo/snow"
    16  	"github.com/MetalBlockchain/metalgo/snow/engine/common"
    17  	"github.com/MetalBlockchain/metalgo/snow/engine/common/tracker"
    18  	"github.com/MetalBlockchain/metalgo/snow/engine/snowman/block"
    19  	"github.com/MetalBlockchain/metalgo/snow/engine/snowman/getter"
    20  	"github.com/MetalBlockchain/metalgo/snow/validators"
    21  	"github.com/MetalBlockchain/metalgo/utils/hashing"
    22  )
    23  
    24  const (
    25  	key         uint64 = 2022
    26  	minorityKey uint64 = 2000
    27  )
    28  
    29  var (
    30  	_ block.ChainVM         = fullVM{}
    31  	_ block.StateSyncableVM = fullVM{}
    32  
    33  	unknownSummaryID = ids.ID{'g', 'a', 'r', 'b', 'a', 'g', 'e'}
    34  
    35  	summaryBytes = []byte{'s', 'u', 'm', 'm', 'a', 'r', 'y'}
    36  	summaryID    ids.ID
    37  
    38  	minoritySummaryBytes = []byte{'m', 'i', 'n', 'o', 'r', 'i', 't', 'y'}
    39  	minoritySummaryID    ids.ID
    40  )
    41  
    42  func init() {
    43  	var err error
    44  	summaryID, err = ids.ToID(hashing.ComputeHash256(summaryBytes))
    45  	if err != nil {
    46  		panic(err)
    47  	}
    48  
    49  	minoritySummaryID, err = ids.ToID(hashing.ComputeHash256(minoritySummaryBytes))
    50  	if err != nil {
    51  		panic(err)
    52  	}
    53  }
    54  
    55  type fullVM struct {
    56  	*block.TestVM
    57  	*block.TestStateSyncableVM
    58  }
    59  
    60  func buildTestPeers(t *testing.T, subnetID ids.ID) validators.Manager {
    61  	// We consider more than maxOutstandingBroadcastRequests peers to test
    62  	// capping the number of requests sent out.
    63  	vdrs := validators.NewManager()
    64  	for idx := 0; idx < 2*maxOutstandingBroadcastRequests; idx++ {
    65  		beaconID := ids.GenerateTestNodeID()
    66  		require.NoError(t, vdrs.AddStaker(subnetID, beaconID, nil, ids.Empty, 1))
    67  	}
    68  	return vdrs
    69  }
    70  
    71  func buildTestsObjects(
    72  	t *testing.T,
    73  	ctx *snow.ConsensusContext,
    74  	startupTracker tracker.Startup,
    75  	beacons validators.Manager,
    76  	alpha uint64,
    77  ) (
    78  	*stateSyncer,
    79  	*fullVM,
    80  	*common.SenderTest,
    81  ) {
    82  	require := require.New(t)
    83  
    84  	fullVM := &fullVM{
    85  		TestVM: &block.TestVM{
    86  			TestVM: common.TestVM{T: t},
    87  		},
    88  		TestStateSyncableVM: &block.TestStateSyncableVM{
    89  			T: t,
    90  		},
    91  	}
    92  	sender := &common.SenderTest{T: t}
    93  	dummyGetter, err := getter.New(
    94  		fullVM,
    95  		sender,
    96  		ctx.Log,
    97  		time.Second,
    98  		2000,
    99  		ctx.Registerer,
   100  	)
   101  	require.NoError(err)
   102  
   103  	cfg, err := NewConfig(
   104  		dummyGetter,
   105  		ctx,
   106  		startupTracker,
   107  		sender,
   108  		beacons,
   109  		beacons.Count(ctx.SubnetID),
   110  		alpha,
   111  		nil,
   112  		fullVM,
   113  	)
   114  	require.NoError(err)
   115  	commonSyncer := New(cfg, func(context.Context, uint32) error {
   116  		return nil
   117  	})
   118  	require.IsType(&stateSyncer{}, commonSyncer)
   119  	syncer := commonSyncer.(*stateSyncer)
   120  	require.NotNil(syncer.stateSyncVM)
   121  
   122  	fullVM.GetOngoingSyncStateSummaryF = func(context.Context) (block.StateSummary, error) {
   123  		return nil, database.ErrNotFound
   124  	}
   125  
   126  	return syncer, fullVM, sender
   127  }
   128  
   129  func pickRandomFrom(nodes map[ids.NodeID]uint32) ids.NodeID {
   130  	for node := range nodes {
   131  		return node
   132  	}
   133  	return ids.EmptyNodeID
   134  }