2021-01-29 07:42:40 +00:00
|
|
|
package innerring
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/alphabet"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/netmap"
|
2021-02-01 16:20:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement"
|
2021-01-29 07:42:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/innerring/timers"
|
2021-01-29 07:48:47 +00:00
|
|
|
container "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
|
2021-02-01 16:20:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
2021-01-29 07:48:47 +00:00
|
|
|
"go.uber.org/zap"
|
2021-01-29 07:42:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2021-01-29 07:48:47 +00:00
|
|
|
epochState interface {
|
|
|
|
EpochCounter() uint64
|
|
|
|
}
|
|
|
|
|
2021-02-02 14:17:51 +00:00
|
|
|
subEpochEventHandler struct {
|
|
|
|
handler event.Handler // handle to execute
|
|
|
|
durationMul uint32 // X: X/Y of epoch in blocks
|
|
|
|
durationDiv uint32 // Y: X/Y of epoch in blocks
|
|
|
|
}
|
|
|
|
|
2021-01-29 07:42:40 +00:00
|
|
|
epochTimerArgs struct {
|
2021-01-29 07:48:47 +00:00
|
|
|
l *zap.Logger
|
|
|
|
|
2021-01-29 07:42:40 +00:00
|
|
|
nm *netmap.Processor // to handle new epoch tick
|
|
|
|
|
2021-01-29 07:48:47 +00:00
|
|
|
cnrWrapper *container.Wrapper // to invoke stop container estimation
|
|
|
|
epoch epochState // to specify which epoch to stop
|
|
|
|
|
|
|
|
epochDuration uint32 // in blocks
|
|
|
|
stopEstimationDMul uint32 // X: X/Y of epoch in blocks
|
|
|
|
stopEstimationDDiv uint32 // Y: X/Y of epoch in blocks
|
2021-02-01 16:20:33 +00:00
|
|
|
|
2021-02-02 14:17:51 +00:00
|
|
|
collectBasicIncome subEpochEventHandler
|
|
|
|
distributeBasicIncome subEpochEventHandler
|
2021-01-29 07:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emitTimerArgs struct {
|
|
|
|
ap *alphabet.Processor // to handle new emission tick
|
|
|
|
|
|
|
|
emitDuration uint32 // in blocks
|
|
|
|
}
|
2021-02-24 15:46:01 +00:00
|
|
|
|
|
|
|
notaryDepositArgs struct {
|
|
|
|
l *zap.Logger
|
|
|
|
|
|
|
|
depositor func() error
|
|
|
|
|
|
|
|
notaryDuration uint32 // in blocks
|
|
|
|
}
|
2021-01-29 07:42:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) addBlockTimer(t *timers.BlockTimer) {
|
|
|
|
s.blockTimers = append(s.blockTimers, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) startBlockTimers() error {
|
|
|
|
for i := range s.blockTimers {
|
|
|
|
if err := s.blockTimers[i].Reset(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) tickTimers() {
|
|
|
|
for i := range s.blockTimers {
|
|
|
|
s.blockTimers[i].Tick()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEpochTimer(args *epochTimerArgs) *timers.BlockTimer {
|
|
|
|
epochTimer := timers.NewBlockTimer(
|
|
|
|
timers.StaticBlockMeter(args.epochDuration),
|
|
|
|
func() {
|
|
|
|
args.nm.HandleNewEpochTick(timers.NewEpochTick{})
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-01-29 07:48:47 +00:00
|
|
|
// sub-timer for epoch timer to tick stop container estimation events at
|
|
|
|
// some block in epoch
|
|
|
|
epochTimer.OnDelta(
|
|
|
|
args.stopEstimationDMul,
|
|
|
|
args.stopEstimationDDiv,
|
|
|
|
func() {
|
|
|
|
epochN := args.epoch.EpochCounter()
|
|
|
|
if epochN == 0 { // estimates are invalid in genesis epoch
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := args.cnrWrapper.StopEstimation(epochN - 1)
|
|
|
|
if err != nil {
|
|
|
|
args.l.Warn("can't stop epoch estimation",
|
|
|
|
zap.Uint64("epoch", epochN),
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-02-01 16:20:33 +00:00
|
|
|
epochTimer.OnDelta(
|
2021-02-02 14:17:51 +00:00
|
|
|
args.collectBasicIncome.durationMul,
|
|
|
|
args.collectBasicIncome.durationDiv,
|
|
|
|
func() {
|
|
|
|
epochN := args.epoch.EpochCounter()
|
|
|
|
if epochN == 0 { // estimates are invalid in genesis epoch
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
args.collectBasicIncome.handler(
|
|
|
|
settlement.NewBasicIncomeCollectEvent(epochN - 1),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
epochTimer.OnDelta(
|
|
|
|
args.distributeBasicIncome.durationMul,
|
|
|
|
args.distributeBasicIncome.durationDiv,
|
2021-02-01 16:20:33 +00:00
|
|
|
func() {
|
|
|
|
epochN := args.epoch.EpochCounter()
|
|
|
|
if epochN == 0 { // estimates are invalid in genesis epoch
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-02 14:17:51 +00:00
|
|
|
args.distributeBasicIncome.handler(
|
|
|
|
settlement.NewBasicIncomeDistributeEvent(epochN - 1),
|
|
|
|
)
|
2021-02-01 16:20:33 +00:00
|
|
|
})
|
|
|
|
|
2021-01-29 07:42:40 +00:00
|
|
|
return epochTimer
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEmissionTimer(args *emitTimerArgs) *timers.BlockTimer {
|
|
|
|
return timers.NewBlockTimer(
|
|
|
|
timers.StaticBlockMeter(args.emitDuration),
|
|
|
|
func() {
|
|
|
|
args.ap.HandleGasEmission(timers.NewAlphabetEmitTick{})
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2021-02-24 15:46:01 +00:00
|
|
|
|
|
|
|
func newNotaryDepositTimer(args *notaryDepositArgs) *timers.BlockTimer {
|
|
|
|
return timers.NewBlockTimer(
|
|
|
|
timers.StaticBlockMeter(args.notaryDuration),
|
|
|
|
func() {
|
|
|
|
err := args.depositor()
|
|
|
|
if err != nil {
|
|
|
|
args.l.Warn("can't deposit notary contract",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|