2021-01-28 19:43:32 +00:00
|
|
|
package settlement
|
|
|
|
|
|
|
|
import (
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
2021-01-28 19:43:32 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HandleAuditEvent catches a new AuditEvent and
|
|
|
|
// adds AuditProcessor call to the execution queue.
|
|
|
|
func (p *Processor) HandleAuditEvent(e event.Event) {
|
|
|
|
ev := e.(AuditEvent)
|
|
|
|
|
|
|
|
epoch := ev.Epoch()
|
|
|
|
|
2023-03-30 15:24:37 +00:00
|
|
|
if !p.state.IsAlphabet() {
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Info(logs.SettlementNonAlphabetModeIgnoreAuditPayments)
|
2023-03-30 15:24:37 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-28 07:41:01 +00:00
|
|
|
log := &logger.Logger{Logger: p.log.With(
|
2021-01-28 19:43:32 +00:00
|
|
|
zap.Uint64("epoch", epoch),
|
2022-09-28 07:41:01 +00:00
|
|
|
)}
|
2021-01-28 19:43:32 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
log.Info(logs.SettlementNewAuditSettlementEvent)
|
2021-01-28 19:43:32 +00:00
|
|
|
|
|
|
|
if epoch == 0 {
|
2023-04-12 14:35:10 +00:00
|
|
|
log.Debug(logs.SettlementIgnoreGenesisEpoch)
|
2021-01-28 19:43:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := &auditEventHandler{
|
|
|
|
log: log,
|
|
|
|
epoch: epoch,
|
|
|
|
proc: p.auditProc,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := p.pool.Submit(handler.handle)
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
log.Warn(logs.SettlementCouldNotAddHandlerOfAuditEventToQueue,
|
2021-01-28 19:43:32 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
log.Debug(logs.SettlementAuditEventHandlingSuccessfullyScheduled)
|
2021-01-28 19:43:32 +00:00
|
|
|
}
|
2021-02-01 16:18:34 +00:00
|
|
|
|
|
|
|
func (p *Processor) HandleIncomeCollectionEvent(e event.Event) {
|
|
|
|
ev := e.(BasicIncomeCollectEvent)
|
|
|
|
epoch := ev.Epoch()
|
|
|
|
|
2021-03-23 15:20:44 +00:00
|
|
|
if !p.state.IsAlphabet() {
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Info(logs.SettlementNonAlphabetModeIgnoreIncomeCollectionEvent)
|
2021-02-01 16:18:34 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Info(logs.SettlementStartBasicIncomeCollection,
|
2021-02-01 16:18:34 +00:00
|
|
|
zap.Uint64("epoch", epoch))
|
|
|
|
|
|
|
|
p.contextMu.Lock()
|
|
|
|
defer p.contextMu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := p.incomeContexts[epoch]; ok {
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Error(logs.SettlementIncomeContextAlreadyExists,
|
2021-02-01 16:18:34 +00:00
|
|
|
zap.Uint64("epoch", epoch))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
incomeCtx, err := p.basicIncome.CreateContext(epoch)
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Error(logs.SettlementCantCreateIncomeContext,
|
2021-02-01 16:18:34 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.incomeContexts[epoch] = incomeCtx
|
|
|
|
|
|
|
|
err = p.pool.Submit(func() {
|
|
|
|
incomeCtx.Collect()
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Warn(logs.SettlementCouldNotAddHandlerOfBasicIncomeCollectionToQueue,
|
2021-02-01 16:18:34 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 14:17:38 +00:00
|
|
|
|
|
|
|
func (p *Processor) HandleIncomeDistributionEvent(e event.Event) {
|
|
|
|
ev := e.(BasicIncomeDistributeEvent)
|
|
|
|
epoch := ev.Epoch()
|
|
|
|
|
2021-03-23 15:20:44 +00:00
|
|
|
if !p.state.IsAlphabet() {
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Info(logs.SettlementNonAlphabetModeIgnoreIncomeDistributionEvent)
|
2021-02-02 14:17:38 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Info(logs.SettlementStartBasicIncomeDistribution,
|
2021-02-02 14:17:38 +00:00
|
|
|
zap.Uint64("epoch", epoch))
|
|
|
|
|
|
|
|
p.contextMu.Lock()
|
|
|
|
defer p.contextMu.Unlock()
|
|
|
|
|
|
|
|
incomeCtx, ok := p.incomeContexts[epoch]
|
|
|
|
delete(p.incomeContexts, epoch)
|
|
|
|
|
|
|
|
if !ok {
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Warn(logs.SettlementIncomeContextDistributionDoesNotExists,
|
2021-02-02 14:17:38 +00:00
|
|
|
zap.Uint64("epoch", epoch))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := p.pool.Submit(func() {
|
|
|
|
incomeCtx.Distribute()
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
p.log.Warn(logs.SettlementCouldNotAddHandlerOfBasicIncomeDistributionToQueue,
|
2021-02-02 14:17:38 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|