Skip to content

Commit

Permalink
Remove common.Config from syncer.Config (#2330)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Nov 17, 2023
1 parent dbc209c commit a803f38
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 250 deletions.
36 changes: 20 additions & 16 deletions chains/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1223,22 +1223,21 @@ func (m *manager) createSnowmanChain(
engine = smeng.TraceEngine(engine, m.Tracer)
}

commonCfg := common.Config{
Ctx: ctx,
Beacons: beacons,
SampleK: sampleK,
StartupTracker: startupTracker,
Alpha: bootstrapWeight/2 + 1, // must be > 50%
Sender: messageSender,
BootstrapTracker: sb,
Timer: h,
AncestorsMaxContainersReceived: m.BootstrapAncestorsMaxContainersReceived,
SharedCfg: &common.SharedConfig{},
}

// create bootstrap gear
alpha := bootstrapWeight/2 + 1 // must be > 50%
bootstrapCfg := smbootstrap.Config{
Config: commonCfg,
Config: common.Config{
Ctx: ctx,
Beacons: beacons,
SampleK: sampleK,
StartupTracker: startupTracker,
Alpha: alpha,
Sender: messageSender,
BootstrapTracker: sb,
Timer: h,
AncestorsMaxContainersReceived: m.BootstrapAncestorsMaxContainersReceived,
SharedCfg: &common.SharedConfig{},
},
AllGetsServer: snowGetHandler,
Blocked: blocked,
VM: vm,
Expand All @@ -1259,9 +1258,14 @@ func (m *manager) createSnowmanChain(

// create state sync gear
stateSyncCfg, err := syncer.NewConfig(
commonCfg,
m.StateSyncBeacons,
snowGetHandler,
ctx,
startupTracker,
messageSender,
beacons,
sampleK,
alpha,
m.StateSyncBeacons,
vm,
)
if err != nil {
Expand Down
47 changes: 27 additions & 20 deletions snow/engine/snowman/syncer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ import (
"fmt"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/snow/engine/common/tracker"
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/math"
)

type Config struct {
common.Config
common.AllGetsServer

Ctx *snow.ConsensusContext

StartupTracker tracker.Startup
Sender common.Sender

// SampleK determines the number of nodes to attempt to fetch the latest
// state sync summary from. In order for a round of voting to succeed, there
// must be at least one correct node sampled.
Expand All @@ -33,43 +40,43 @@ type Config struct {
}

func NewConfig(
commonCfg common.Config,
stateSyncerIDs []ids.NodeID,
snowGetHandler common.AllGetsServer,
ctx *snow.ConsensusContext,
startupTracker tracker.Startup,
sender common.Sender,
beacons validators.Manager,
sampleK int,
alpha uint64,
stateSyncerIDs []ids.NodeID,
vm block.ChainVM,
) (Config, error) {
// Initialize the default values that will be used if stateSyncerIDs is
// empty.
var (
stateSyncBeacons = commonCfg.Beacons
syncAlpha = commonCfg.Alpha
syncSampleK = commonCfg.SampleK
)
// Initialize the beacons that will be used if stateSyncerIDs is empty.
stateSyncBeacons := beacons

// If the user has manually provided state syncer IDs, then override the
// state sync beacons to them.
if len(stateSyncerIDs) != 0 {
stateSyncBeacons = validators.NewManager()
for _, peerID := range stateSyncerIDs {
// Invariant: We never use the TxID or BLS keys populated here.
if err := stateSyncBeacons.AddStaker(commonCfg.Ctx.SubnetID, peerID, nil, ids.Empty, 1); err != nil {
if err := stateSyncBeacons.AddStaker(ctx.SubnetID, peerID, nil, ids.Empty, 1); err != nil {
return Config{}, err
}
}
stateSyncingWeight, err := stateSyncBeacons.TotalWeight(commonCfg.Ctx.SubnetID)
stateSyncingWeight, err := stateSyncBeacons.TotalWeight(ctx.SubnetID)
if err != nil {
return Config{}, fmt.Errorf("failed to calculate total weight of state sync beacons for subnet %s: %w", commonCfg.Ctx.SubnetID, err)
}
if uint64(syncSampleK) > stateSyncingWeight {
syncSampleK = int(stateSyncingWeight)
return Config{}, fmt.Errorf("failed to calculate total weight of state sync beacons for subnet %s: %w", ctx.SubnetID, err)
}
syncAlpha = stateSyncingWeight/2 + 1 // must be > 50%
sampleK = int(math.Min(uint64(sampleK), stateSyncingWeight))
alpha = stateSyncingWeight/2 + 1 // must be > 50%
}
return Config{
Config: commonCfg,
AllGetsServer: snowGetHandler,
SampleK: syncSampleK,
Alpha: syncAlpha,
Ctx: ctx,
StartupTracker: startupTracker,
Sender: sender,
SampleK: sampleK,
Alpha: alpha,
StateSyncBeacons: stateSyncBeacons,
VM: vm,
}, nil
Expand Down
Loading

0 comments on commit a803f38

Please sign in to comment.