[#1900] node: Refactor bootstrap methods

In previous implementation bootstrapping state was chosen according to
bool flag which was not convenient.

Create separate method to boostrap with "online" and the current state.

Signed-off-by: Leonard Lyubich <ctulhurider@gmail.com>
This commit is contained in:
Leonard Lyubich 2022-10-28 11:02:55 +04:00 committed by fyrchik
parent e8c5f03c30
commit db92e96e40
2 changed files with 31 additions and 22 deletions

View file

@ -836,26 +836,12 @@ func (c *cfg) handleLocalNodeInfo(ni *netmap.NodeInfo) {
c.cfgNetmap.state.setNodeInfo(ni)
}
// bootstrap sets local node's netmap status to "online".
// If current netmap status is MAINTENANCE and this function wasn't called thorough a control service,
// the status is untouched.
func (c *cfg) bootstrap(manual bool) error {
// bootstrapWithState calls "addPeer" method of the Sidechain Netmap contract
// with the binary-encoded information from the current node's configuration.
// The state is set using the provided setter which MUST NOT be nil.
func (c *cfg) bootstrapWithState(stateSetter func(*netmap.NodeInfo)) error {
ni := c.cfgNodeInfo.localInfo
// switch to online except when under maintenance
if st := c.cfgNetmap.state.controlNetmapStatus(); st == control.NetmapStatus_MAINTENANCE && !manual {
ni.SetMaintenance()
c.log.Info("bootstrap with untouched node state",
zap.Stringer("state", st),
)
} else {
ni.SetOnline()
c.log.Info("bootstrapping with online state",
zap.Stringer("previous", st),
)
}
stateSetter(&ni)
prm := nmClient.AddPeerPrm{}
prm.SetNodeInfo(ni)
@ -863,6 +849,29 @@ func (c *cfg) bootstrap(manual bool) error {
return c.cfgNetmap.wrapper.AddPeer(prm)
}
// bootstrapOnline calls cfg.bootstrapWithState with "online" state.
func bootstrapOnline(c *cfg) error {
return c.bootstrapWithState((*netmap.NodeInfo).SetOnline)
}
// bootstrap calls bootstrapWithState with:
// - "maintenance" state if maintenance is in progress on the current node
// - "online", otherwise
func (c *cfg) bootstrap() error {
// switch to online except when under maintenance
st := c.cfgNetmap.state.controlNetmapStatus()
if st == control.NetmapStatus_MAINTENANCE {
c.log.Info("bootstrapping with the maintenance state")
return c.bootstrapWithState((*netmap.NodeInfo).SetMaintenance)
}
c.log.Info("bootstrapping with online state",
zap.Stringer("previous", st),
)
return bootstrapOnline(c)
}
// needBootstrap checks if local node should be registered in network on bootup.
func (c *cfg) needBootstrap() bool {
return c.cfgNetmap.needBootstrap

View file

@ -179,7 +179,7 @@ func initNetmapService(c *cfg) {
const reBootstrapInterval = 2
if (n-c.cfgNetmap.startEpoch)%reBootstrapInterval == 0 {
err := c.bootstrap(false)
err := c.bootstrap()
if err != nil {
c.log.Warn("can't send re-bootstrap tx", zap.Error(err))
}
@ -241,7 +241,7 @@ func readSubnetCfg(c *cfg) {
// Must be called after initNetmapService.
func bootstrapNode(c *cfg) {
if c.needBootstrap() {
err := c.bootstrap(false)
err := c.bootstrap()
fatalOnErrDetails("bootstrap error", err)
}
}
@ -352,7 +352,7 @@ func (c *cfg) SetNetmapStatus(st control.NetmapStatus) error {
if st == control.NetmapStatus_ONLINE {
c.cfgNetmap.reBoostrapTurnedOff.Store(false)
return c.bootstrap(true)
return bootstrapOnline(c)
}
c.cfgNetmap.reBoostrapTurnedOff.Store(true)