[#360] Run basic income collection in inner ring

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-02-01 19:20:33 +03:00 committed by Alex Vanin
parent 8c4bf81351
commit be2ed6bf4c
4 changed files with 98 additions and 8 deletions

View file

@ -78,6 +78,8 @@ func defaultConfiguration(cfg *viper.Viper) {
cfg.SetDefault("timers.emit", "0") cfg.SetDefault("timers.emit", "0")
cfg.SetDefault("timers.stop_estimation.mul", 1) cfg.SetDefault("timers.stop_estimation.mul", 1)
cfg.SetDefault("timers.stop_estimation.div", 1) 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("workers.netmap", "10") cfg.SetDefault("workers.netmap", "10")
cfg.SetDefault("workers.balance", "10") cfg.SetDefault("workers.balance", "10")

View file

@ -3,8 +3,10 @@ package innerring
import ( import (
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/alphabet" "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/alphabet"
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/netmap" "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/netmap"
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement"
"github.com/nspcc-dev/neofs-node/pkg/innerring/timers" "github.com/nspcc-dev/neofs-node/pkg/innerring/timers"
container "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" container "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -24,6 +26,10 @@ type (
epochDuration uint32 // in blocks epochDuration uint32 // in blocks
stopEstimationDMul uint32 // X: X/Y of epoch in blocks stopEstimationDMul uint32 // X: X/Y of epoch in blocks
stopEstimationDDiv uint32 // Y: 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
} }
emitTimerArgs struct { emitTimerArgs struct {
@ -80,6 +86,18 @@ func newEpochTimer(args *epochTimerArgs) *timers.BlockTimer {
} }
}) })
epochTimer.OnDelta(
args.collectBasicIncomeDMul,
args.collectBasicIncomeDDiv,
func() {
epochN := args.epoch.EpochCounter()
if epochN == 0 { // estimates are invalid in genesis epoch
return
}
args.collectBasicIncome(settlement.NewBasicIncomeCollectEvent(epochN - 1))
})
return epochTimer return epochTimer
} }

View file

@ -297,6 +297,11 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
settlementDeps: settlementDeps, settlementDeps: settlementDeps,
} }
basicSettlementDeps := &basicIncomeSettlementDeps{
settlementDeps: settlementDeps,
cnrClient: cnrClient,
}
auditSettlementCalc := auditSettlement.NewCalculator( auditSettlementCalc := auditSettlement.NewCalculator(
&auditSettlement.CalculatorPrm{ &auditSettlement.CalculatorPrm{
ResultStorage: auditCalcDeps, ResultStorage: auditCalcDeps,
@ -313,6 +318,8 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
settlementProcessor := settlement.New( settlementProcessor := settlement.New(
settlement.Prm{ settlement.Prm{
AuditProcessor: (*auditSettlementCalculator)(auditSettlementCalc), AuditProcessor: (*auditSettlementCalculator)(auditSettlementCalc),
BasicIncome: &basicSettlementConstructor{dep: basicSettlementDeps},
State: server,
}, },
settlement.WithLogger(server.log), settlement.WithLogger(server.log),
) )
@ -437,6 +444,9 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
epochDuration: cfg.GetUint32("timers.epoch"), epochDuration: cfg.GetUint32("timers.epoch"),
stopEstimationDMul: cfg.GetUint32("timers.stop_estimation.mul"), stopEstimationDMul: cfg.GetUint32("timers.stop_estimation.mul"),
stopEstimationDDiv: cfg.GetUint32("timers.stop_estimation.div"), 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"),
}) })
server.addBlockTimer(server.epochTimer) server.addBlockTimer(server.epochTimer)

View file

@ -2,6 +2,7 @@ package innerring
import ( import (
"context" "context"
"encoding/hex"
"math/big" "math/big"
auditAPI "github.com/nspcc-dev/neofs-api-go/pkg/audit" auditAPI "github.com/nspcc-dev/neofs-api-go/pkg/audit"
@ -14,9 +15,12 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/core/container" "github.com/nspcc-dev/neofs-node/pkg/core/container"
"github.com/nspcc-dev/neofs-node/pkg/core/netmap" "github.com/nspcc-dev/neofs-node/pkg/core/netmap"
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/audit" "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/audit"
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/basic"
"github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/common" "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/common"
auditClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit/wrapper" auditClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit/wrapper"
balanceClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance/wrapper" balanceClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance/wrapper"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
containerClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper"
"github.com/nspcc-dev/neofs-node/pkg/util/logger" "github.com/nspcc-dev/neofs-node/pkg/util/logger"
"github.com/pkg/errors" "github.com/pkg/errors"
"go.uber.org/zap" "go.uber.org/zap"
@ -40,6 +44,15 @@ type auditSettlementDeps struct {
*settlementDeps *settlementDeps
} }
type basicIncomeSettlementDeps struct {
*settlementDeps
cnrClient *containerClient.Wrapper
}
type basicSettlementConstructor struct {
dep *basicIncomeSettlementDeps
}
type auditSettlementCalculator audit.Calculator type auditSettlementCalculator audit.Calculator
type containerWrapper containerAPI.Container type containerWrapper containerAPI.Container
@ -171,7 +184,10 @@ func (s settlementDeps) ResolveKey(ni common.NodeInfo) (*owner.ID, error) {
return id, nil return id, nil
} }
var transferAuditDetails = []byte("settlement-audit") var (
transferAuditDetails = []byte("settlement-audit")
basicIncomeAuditDetails = []byte("settlement-basic-income")
)
func (s settlementDeps) transfer(sender, recipient *owner.ID, amount *big.Int, details []byte) { func (s settlementDeps) transfer(sender, recipient *owner.ID, amount *big.Int, details []byte) {
log := s.log.With( log := s.log.With(
@ -206,8 +222,52 @@ func (a auditSettlementDeps) Transfer(sender, recipient *owner.ID, amount *big.I
a.transfer(sender, recipient, amount, transferAuditDetails) a.transfer(sender, recipient, amount, transferAuditDetails)
} }
func (b basicIncomeSettlementDeps) Transfer(sender, recipient *owner.ID, amount *big.Int) {
b.transfer(sender, recipient, amount, basicIncomeAuditDetails)
}
func (b basicIncomeSettlementDeps) BasicRate() uint64 {
return 1_0000_0000 // fixme: read from config and from chain
}
func (b basicIncomeSettlementDeps) Estimations(epoch uint64) ([]*wrapper.Estimations, error) {
estimationIDs, err := b.cnrClient.ListLoadEstimationsByEpoch(epoch)
if err != nil {
return nil, err
}
result := make([]*wrapper.Estimations, 0, len(estimationIDs))
for i := range estimationIDs {
estimation, err := b.cnrClient.GetUsedSpaceEstimations(estimationIDs[i])
if err != nil {
b.log.Warn("can't get used space estimation",
zap.String("estimation_id", hex.EncodeToString(estimationIDs[i])),
zap.String("error", err.Error()))
continue
}
result = append(result, estimation)
}
return result, nil
}
func (s *auditSettlementCalculator) ProcessAuditSettlements(epoch uint64) { func (s *auditSettlementCalculator) ProcessAuditSettlements(epoch uint64) {
(*audit.Calculator)(s).Calculate(&audit.CalculatePrm{ (*audit.Calculator)(s).Calculate(&audit.CalculatePrm{
Epoch: epoch, Epoch: epoch,
}) })
} }
func (b *basicSettlementConstructor) CreateContext(epoch uint64) (*basic.IncomeSettlementContext, error) {
return basic.NewIncomeSettlementContext(&basic.IncomeSettlementContextPrms{
Log: b.dep.log,
Epoch: epoch,
Rate: b.dep,
Estimations: b.dep,
Container: b.dep,
Placement: b.dep,
Exchange: b.dep,
})
}