2021-01-29 07:42:40 +00:00
|
|
|
package innerring
|
|
|
|
|
|
|
|
import (
|
2021-05-04 08:50:13 +00:00
|
|
|
"context"
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/innerring/processors/alphabet"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/innerring/processors/settlement"
|
|
|
|
timerEvent "github.com/TrueCloudLab/frostfs-node/pkg/innerring/timers"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/client/container"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/event"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/timer"
|
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2021-04-27 11:07:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
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-10-14 11:04:09 +00:00
|
|
|
EpochDuration() uint64
|
2021-01-29 07:48:47 +00:00
|
|
|
}
|
|
|
|
|
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-10-11 17:08:31 +00:00
|
|
|
newEpochHandler func()
|
|
|
|
|
2021-01-29 07:42:40 +00:00
|
|
|
epochTimerArgs struct {
|
2022-09-28 07:41:01 +00:00
|
|
|
l *logger.Logger
|
2021-01-29 07:48:47 +00:00
|
|
|
|
2021-10-11 17:08:31 +00:00
|
|
|
newEpochHandlers []newEpochHandler
|
2021-01-29 07:42:40 +00:00
|
|
|
|
2022-01-31 13:34:01 +00:00
|
|
|
cnrWrapper *container.Client // to invoke stop container estimation
|
|
|
|
epoch epochState // to specify which epoch to stop, and epoch duration
|
2021-01-29 07:48:47 +00:00
|
|
|
|
2021-10-14 11:04:09 +00:00
|
|
|
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
|
|
|
|
2021-05-04 08:50:13 +00:00
|
|
|
depositor func() (util.Uint256, error)
|
|
|
|
awaiter func(context.Context, util.Uint256) error
|
2021-01-29 07:42:40 +00:00
|
|
|
)
|
|
|
|
|
2021-04-15 13:50:34 +00:00
|
|
|
func (s *Server) addBlockTimer(t *timer.BlockTimer) {
|
2021-01-29 07:42:40 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-03-30 10:16:41 +00:00
|
|
|
func (s *Server) tickTimers(h uint32) {
|
2021-01-29 07:42:40 +00:00
|
|
|
for i := range s.blockTimers {
|
2022-03-30 10:16:41 +00:00
|
|
|
s.blockTimers[i].Tick(h)
|
2021-01-29 07:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 13:50:34 +00:00
|
|
|
func newEpochTimer(args *epochTimerArgs) *timer.BlockTimer {
|
|
|
|
epochTimer := timer.NewBlockTimer(
|
2021-10-14 11:04:09 +00:00
|
|
|
func() (uint32, error) {
|
|
|
|
return uint32(args.epoch.EpochDuration()), nil
|
|
|
|
},
|
2021-01-29 07:42:40 +00:00
|
|
|
func() {
|
2021-10-11 17:08:31 +00:00
|
|
|
for _, handler := range args.newEpochHandlers {
|
|
|
|
handler()
|
|
|
|
}
|
2021-01-29 07:42:40 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-11-12 15:19:05 +00:00
|
|
|
prm := container.StopEstimationPrm{}
|
|
|
|
prm.SetEpoch(epochN - 1)
|
|
|
|
|
|
|
|
err := args.cnrWrapper.StopEstimation(prm)
|
2021-01-29 07:48:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-15 13:50:34 +00:00
|
|
|
func newEmissionTimer(args *emitTimerArgs) *timer.BlockTimer {
|
|
|
|
return timer.NewBlockTimer(
|
|
|
|
timer.StaticBlockMeter(args.emitDuration),
|
2021-01-29 07:42:40 +00:00
|
|
|
func() {
|
2021-04-15 13:50:34 +00:00
|
|
|
args.ap.HandleGasEmission(timerEvent.NewAlphabetEmitTick{})
|
2021-01-29 07:42:40 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|