[#365] innerring: Produce distribute income events

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-02-02 17:17:51 +03:00 committed by Alex Vanin
parent d77d49bd2a
commit fd461bdb65
3 changed files with 46 additions and 16 deletions

View file

@ -80,6 +80,8 @@ func defaultConfiguration(cfg *viper.Viper) {
cfg.SetDefault("timers.stop_estimation.div", 1)
cfg.SetDefault("timers.collect_basic_income.mul", 1)
cfg.SetDefault("timers.collect_basic_income.div", 1)
cfg.SetDefault("timers.distribute_basic_income.mul", 1)
cfg.SetDefault("timers.distribute_basic_income.div", 1)
cfg.SetDefault("workers.netmap", "10")
cfg.SetDefault("workers.balance", "10")

View file

@ -15,6 +15,12 @@ type (
EpochCounter() uint64
}
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
}
epochTimerArgs struct {
l *zap.Logger
@ -27,9 +33,8 @@ type (
stopEstimationDMul uint32 // X: X/Y of epoch in blocks
stopEstimationDDiv uint32 // Y: X/Y of epoch in blocks
collectBasicIncome event.Handler // handle collect basic income
collectBasicIncomeDMul uint32 // X: X/Y of epoch in blocks
collectBasicIncomeDDiv uint32 // Y: X/Y of epoch in blocks
collectBasicIncome subEpochEventHandler
distributeBasicIncome subEpochEventHandler
}
emitTimerArgs struct {
@ -87,15 +92,31 @@ func newEpochTimer(args *epochTimerArgs) *timers.BlockTimer {
})
epochTimer.OnDelta(
args.collectBasicIncomeDMul,
args.collectBasicIncomeDDiv,
args.collectBasicIncome.durationMul,
args.collectBasicIncome.durationDiv,
func() {
epochN := args.epoch.EpochCounter()
if epochN == 0 { // estimates are invalid in genesis epoch
return
}
args.collectBasicIncome(settlement.NewBasicIncomeCollectEvent(epochN - 1))
args.collectBasicIncome.handler(
settlement.NewBasicIncomeCollectEvent(epochN - 1),
)
})
epochTimer.OnDelta(
args.distributeBasicIncome.durationMul,
args.distributeBasicIncome.durationDiv,
func() {
epochN := args.epoch.EpochCounter()
if epochN == 0 { // estimates are invalid in genesis epoch
return
}
args.distributeBasicIncome.handler(
settlement.NewBasicIncomeDistributeEvent(epochN - 1),
)
})
return epochTimer

View file

@ -442,16 +442,23 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
// initialize epoch timers
server.epochTimer = newEpochTimer(&epochTimerArgs{
l: server.log,
nm: netmapProcessor,
cnrWrapper: cnrClient,
epoch: server,
epochDuration: cfg.GetUint32("timers.epoch"),
stopEstimationDMul: cfg.GetUint32("timers.stop_estimation.mul"),
stopEstimationDDiv: cfg.GetUint32("timers.stop_estimation.div"),
collectBasicIncome: settlementProcessor.HandleIncomeCollectionEvent,
collectBasicIncomeDMul: cfg.GetUint32("timers.collect_basic_income.mul"),
collectBasicIncomeDDiv: cfg.GetUint32("timers.collect_basic_income.div"),
l: server.log,
nm: netmapProcessor,
cnrWrapper: cnrClient,
epoch: server,
epochDuration: cfg.GetUint32("timers.epoch"),
stopEstimationDMul: cfg.GetUint32("timers.stop_estimation.mul"),
stopEstimationDDiv: cfg.GetUint32("timers.stop_estimation.div"),
collectBasicIncome: subEpochEventHandler{
handler: settlementProcessor.HandleIncomeCollectionEvent,
durationMul: cfg.GetUint32("timers.collect_basic_income.mul"),
durationDiv: cfg.GetUint32("timers.collect_basic_income.div"),
},
distributeBasicIncome: subEpochEventHandler{
handler: settlementProcessor.HandleIncomeDistributionEvent,
durationMul: cfg.GetUint32("timers.distribute_basic_income.mul"),
durationDiv: cfg.GetUint32("timers.distribute_basic_income.div"),
},
})
server.addBlockTimer(server.epochTimer)