Skip to content

Commit

Permalink
node: init blockchain state on startup (#3069)
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-khimov authored Dec 25, 2024
2 parents 9019493 + b0b1270 commit 7ac9947
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
9 changes: 8 additions & 1 deletion cmd/neofs-node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,10 @@ func initBasics(c *cfg, key *keys.PrivateKey, stateStorage *state.PersistentStor

lookupScriptHashesInNNS(cli, c.applicationConfiguration, &b)

nState := newNetworkState()
nState := newNetworkState(c.log)
currBlock, err := cli.BlockCount()
fatalOnErr(err)
nState.block.Store(currBlock)

cnrWrap, err := cntClient.NewFromMorph(cli, b.containerSH, 0)
fatalOnErr(err)
Expand All @@ -738,6 +741,10 @@ func initBasics(c *cfg, key *keys.PrivateKey, stateStorage *state.PersistentStor
nmWrap, err := nmClient.NewFromMorph(cli, b.netmapSH, 0)
fatalOnErr(err)

eDuration, err := nmWrap.EpochDuration()
fatalOnErr(err)
nState.updateEpochDuration(eDuration)

ttl := c.applicationConfiguration.fsChain.cacheTTL
if ttl == 0 {
msPerBlock, err := cli.MsPerBlock()
Expand Down
18 changes: 17 additions & 1 deletion cmd/neofs-node/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ import (
"go.uber.org/zap"
)

// defaultEpochDuration is a default epoch duration to replace zero from FS chain.
const defaultEpochDuration = 240

// primary solution of local network state dump.
type networkState struct {
l *zap.Logger

epoch atomic.Uint64
block atomic.Uint32
epochDuration atomic.Uint64
Expand All @@ -35,11 +40,12 @@ type networkState struct {
metrics *metrics.NodeMetrics
}

func newNetworkState() *networkState {
func newNetworkState(l *zap.Logger) *networkState {
var nmStatus atomic.Value
nmStatus.Store(control.NetmapStatus_STATUS_UNDEFINED)

return &networkState{
l: l,
controlNetStatus: nmStatus,
}
}
Expand All @@ -62,6 +68,16 @@ func (s *networkState) setCurrentEpoch(v uint64) {
s.metrics.SetEpoch(v)
}

func (s *networkState) updateEpochDuration(v uint64) {
if v != 0 {
s.epochDuration.Store(v)
return
}

s.l.Warn("zero epoch duration received, fallback to default value", zap.Uint64("applied default value", defaultEpochDuration))
s.epochDuration.Store(defaultEpochDuration)
}

func (s *networkState) setNodeInfo(ni *netmapSDK.NodeInfo) {
ctrlNetSt := control.NetmapStatus_STATUS_UNDEFINED

Expand Down
3 changes: 1 addition & 2 deletions cmd/neofs-node/reputation.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ func initReputationService(c *cfg) {
log.Debug("could not fetch epoch duration", zap.Error(err))
return
}

c.networkState.epochDuration.Store(duration)
c.networkState.updateEpochDuration(duration)

iterations, err := c.cfgNetmap.wrapper.EigenTrustIterations()
if err != nil {
Expand Down

0 comments on commit 7ac9947

Please sign in to comment.