From 44334486456c6881e86680997d40bf39319d783b Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 2 Feb 2021 16:45:13 +0300 Subject: [PATCH] [#365] settlement/basic: Check amount of collected assets Some transfers from container owners into bank account may fail due to lack of assets, so we have to deal with remaining amount of assets in banking account. Signed-off-by: Alex Vanin --- .../processors/settlement/basic/collect.go | 2 -- .../processors/settlement/basic/context.go | 9 +++++++++ .../processors/settlement/basic/distribute.go | 14 ++++++++++++++ pkg/innerring/settlement.go | 5 +++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pkg/innerring/processors/settlement/basic/collect.go b/pkg/innerring/processors/settlement/basic/collect.go index ad205afde..2c978c9c2 100644 --- a/pkg/innerring/processors/settlement/basic/collect.go +++ b/pkg/innerring/processors/settlement/basic/collect.go @@ -18,8 +18,6 @@ func (inc *IncomeSettlementContext) Collect() { inc.mu.Lock() defer inc.mu.Unlock() - // todo: save state of bank wallet - cachedRate, err := inc.rate.BasicRate() if err != nil { inc.log.Error("can't get basic income rate", diff --git a/pkg/innerring/processors/settlement/basic/context.go b/pkg/innerring/processors/settlement/basic/context.go index 1b46fbe44..6528fed3c 100644 --- a/pkg/innerring/processors/settlement/basic/context.go +++ b/pkg/innerring/processors/settlement/basic/context.go @@ -1,6 +1,7 @@ package basic import ( + "math/big" "sync" "github.com/mr-tron/base58" @@ -21,6 +22,11 @@ type ( BasicRate() (uint64, error) } + // BalanceFetcher uses NEP-17 compatible balance contract + BalanceFetcher interface { + Balance(id *owner.ID) (*big.Int, error) + } + IncomeSettlementContext struct { mu sync.Mutex // lock to prevent collection and distribution in the same time @@ -29,6 +35,7 @@ type ( rate RateFetcher estimations EstimationFetcher + balances BalanceFetcher container common.ContainerStorage placement common.PlacementCalculator exchange common.Exchanger @@ -42,6 +49,7 @@ type ( Epoch uint64 Rate RateFetcher Estimations EstimationFetcher + Balances BalanceFetcher Container common.ContainerStorage Placement common.PlacementCalculator Exchange common.Exchanger @@ -59,6 +67,7 @@ func NewIncomeSettlementContext(p *IncomeSettlementContextPrms) (*IncomeSettleme epoch: p.Epoch, rate: p.Rate, estimations: p.Estimations, + balances: p.Balances, container: p.Container, placement: p.Placement, exchange: p.Exchange, diff --git a/pkg/innerring/processors/settlement/basic/distribute.go b/pkg/innerring/processors/settlement/basic/distribute.go index 3ec5e9797..70eb13d2f 100644 --- a/pkg/innerring/processors/settlement/basic/distribute.go +++ b/pkg/innerring/processors/settlement/basic/distribute.go @@ -1,6 +1,20 @@ package basic +import ( + "go.uber.org/zap" +) + func (inc *IncomeSettlementContext) Distribute() { inc.mu.Lock() defer inc.mu.Unlock() + + bankBalance, err := inc.balances.Balance(inc.bankOwner) + if err != nil { + inc.log.Error("can't fetch balance of banking account", + zap.String("error", err.Error())) + + return + } + + _ = bankBalance } diff --git a/pkg/innerring/settlement.go b/pkg/innerring/settlement.go index 598fadeeb..080c5c4f4 100644 --- a/pkg/innerring/settlement.go +++ b/pkg/innerring/settlement.go @@ -259,6 +259,10 @@ func (b basicIncomeSettlementDeps) Estimations(epoch uint64) ([]*wrapper.Estimat return result, nil } +func (b basicIncomeSettlementDeps) Balance(id *owner.ID) (*big.Int, error) { + return b.balanceClient.BalanceOf(id) +} + func (s *auditSettlementCalculator) ProcessAuditSettlements(epoch uint64) { (*audit.Calculator)(s).Calculate(&audit.CalculatePrm{ Epoch: epoch, @@ -271,6 +275,7 @@ func (b *basicSettlementConstructor) CreateContext(epoch uint64) (*basic.IncomeS Epoch: epoch, Rate: b.dep, Estimations: b.dep, + Balances: b.dep, Container: b.dep, Placement: b.dep, Exchange: b.dep,