github.com/MetalBlockchain/metalgo@v1.11.9/snow/engine/snowman/syncer/config.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 "fmt" 8 9 "github.com/MetalBlockchain/metalgo/ids" 10 "github.com/MetalBlockchain/metalgo/snow" 11 "github.com/MetalBlockchain/metalgo/snow/engine/common" 12 "github.com/MetalBlockchain/metalgo/snow/engine/common/tracker" 13 "github.com/MetalBlockchain/metalgo/snow/engine/snowman/block" 14 "github.com/MetalBlockchain/metalgo/snow/validators" 15 ) 16 17 type Config struct { 18 common.AllGetsServer 19 20 Ctx *snow.ConsensusContext 21 22 StartupTracker tracker.Startup 23 Sender common.Sender 24 25 // SampleK determines the number of nodes to attempt to fetch the latest 26 // state sync summary from. In order for a round of voting to succeed, there 27 // must be at least one correct node sampled. 28 SampleK int 29 30 // Alpha specifies the amount of weight that validators must put behind a 31 // state summary to consider it valid to sync to. 32 Alpha uint64 33 34 // StateSyncBeacons are the nodes that will be used to sample and vote over 35 // state summaries. 36 StateSyncBeacons validators.Manager 37 38 VM block.ChainVM 39 } 40 41 func NewConfig( 42 snowGetHandler common.AllGetsServer, 43 ctx *snow.ConsensusContext, 44 startupTracker tracker.Startup, 45 sender common.Sender, 46 beacons validators.Manager, 47 sampleK int, 48 alpha uint64, 49 stateSyncerIDs []ids.NodeID, 50 vm block.ChainVM, 51 ) (Config, error) { 52 // Initialize the beacons that will be used if stateSyncerIDs is empty. 53 stateSyncBeacons := beacons 54 55 // If the user has manually provided state syncer IDs, then override the 56 // state sync beacons to them. 57 if len(stateSyncerIDs) != 0 { 58 stateSyncBeacons = validators.NewManager() 59 for _, peerID := range stateSyncerIDs { 60 // Invariant: We never use the TxID or BLS keys populated here. 61 if err := stateSyncBeacons.AddStaker(ctx.SubnetID, peerID, nil, ids.Empty, 1); err != nil { 62 return Config{}, err 63 } 64 } 65 stateSyncingWeight, err := stateSyncBeacons.TotalWeight(ctx.SubnetID) 66 if err != nil { 67 return Config{}, fmt.Errorf("failed to calculate total weight of state sync beacons for subnet %s: %w", ctx.SubnetID, err) 68 } 69 sampleK = int(min(uint64(sampleK), stateSyncingWeight)) 70 alpha = stateSyncingWeight/2 + 1 // must be > 50% 71 } 72 return Config{ 73 AllGetsServer: snowGetHandler, 74 Ctx: ctx, 75 StartupTracker: startupTracker, 76 Sender: sender, 77 SampleK: sampleK, 78 Alpha: alpha, 79 StateSyncBeacons: stateSyncBeacons, 80 VM: vm, 81 }, nil 82 }