From 90984ee219ded21f6d8a747aeb0aa9bc29bcb60d Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 27 Oct 2020 13:49:09 +0300 Subject: [PATCH] [#122] Add converter interface in balance and neofs processors Signed-off-by: Alex Vanin --- pkg/innerring/processors/balance/process_assets.go | 2 +- pkg/innerring/processors/balance/processor.go | 10 ++++++++++ pkg/innerring/processors/neofs/process_assets.go | 6 +++--- pkg/innerring/processors/neofs/processor.go | 10 ++++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/pkg/innerring/processors/balance/process_assets.go b/pkg/innerring/processors/balance/process_assets.go index dc5a125d79..062616e584 100644 --- a/pkg/innerring/processors/balance/process_assets.go +++ b/pkg/innerring/processors/balance/process_assets.go @@ -17,7 +17,7 @@ func (bp *Processor) processLock(lock *balanceEvent.Lock) { err := invoke.CashOutCheque(bp.mainnetClient, bp.neofsContract, &invoke.ChequeParams{ ID: lock.ID(), - Amount: lock.Amount() / 1_0000_0000, // Fixed16 to Fixed8 + Amount: bp.converter.ToFixed8(lock.Amount()), User: lock.User(), LockAccount: lock.LockAccount(), }) diff --git a/pkg/innerring/processors/balance/processor.go b/pkg/innerring/processors/balance/processor.go index d317aff640..329e31a106 100644 --- a/pkg/innerring/processors/balance/processor.go +++ b/pkg/innerring/processors/balance/processor.go @@ -16,6 +16,11 @@ type ( IsActive() bool } + // PrecisionConverter converts balance amount values. + PrecisionConverter interface { + ToFixed8(int64) int64 + } + // Processor of events produced by balance contract in morph chain. Processor struct { log *zap.Logger @@ -24,6 +29,7 @@ type ( balanceContract util.Uint160 mainnetClient *client.Client activeState ActiveState + converter PrecisionConverter } // Params of the processor constructor. @@ -34,6 +40,7 @@ type ( BalanceContract util.Uint160 MainnetClient *client.Client ActiveState ActiveState + Converter PrecisionConverter } ) @@ -50,6 +57,8 @@ func New(p *Params) (*Processor, error) { return nil, errors.New("ir/balance: neo:mainnet client is not set") case p.ActiveState == nil: return nil, errors.New("ir/balance: global state is not set") + case p.Converter == nil: + return nil, errors.New("ir/balance: balance precision converter is not set") } p.Log.Debug("balance worker pool", zap.Int("size", p.PoolSize)) @@ -66,6 +75,7 @@ func New(p *Params) (*Processor, error) { balanceContract: p.BalanceContract, mainnetClient: p.MainnetClient, activeState: p.ActiveState, + converter: p.Converter, }, nil } diff --git a/pkg/innerring/processors/neofs/process_assets.go b/pkg/innerring/processors/neofs/process_assets.go index b35d6c7df3..0fe74b0129 100644 --- a/pkg/innerring/processors/neofs/process_assets.go +++ b/pkg/innerring/processors/neofs/process_assets.go @@ -27,7 +27,7 @@ func (np *Processor) processDeposit(deposit *neofsEvent.Deposit) { err := invoke.Mint(np.morphClient, np.balanceContract, &invoke.MintBurnParams{ ScriptHash: deposit.To().BytesBE(), - Amount: deposit.Amount() * 1_0000_0000, // from Fixed8 to Fixed16 + Amount: np.converter.ToBalancePrecision(deposit.Amount()), Comment: append([]byte(txLogPrefix), deposit.ID()...), }) if err != nil { @@ -69,7 +69,7 @@ func (np *Processor) processWithdraw(withdraw *neofsEvent.Withdraw) { ID: withdraw.ID(), User: withdraw.User(), LockAccount: lock, - Amount: withdraw.Amount() * 1_0000_0000, // from Fixed8 to Fixed16 + Amount: np.converter.ToBalancePrecision(withdraw.Amount()), Until: curEpoch + lockAccountLifetime, }) if err != nil { @@ -88,7 +88,7 @@ func (np *Processor) processCheque(cheque *neofsEvent.Cheque) { err := invoke.Burn(np.morphClient, np.balanceContract, &invoke.MintBurnParams{ ScriptHash: cheque.LockAccount().BytesBE(), - Amount: cheque.Amount() * 1_0000_0000, // from Fixed8 to Fixed16 + Amount: np.converter.ToBalancePrecision(cheque.Amount()), Comment: append([]byte(txLogPrefix), cheque.ID()...), }) if err != nil { diff --git a/pkg/innerring/processors/neofs/processor.go b/pkg/innerring/processors/neofs/processor.go index 5d2d088188..50e6457251 100644 --- a/pkg/innerring/processors/neofs/processor.go +++ b/pkg/innerring/processors/neofs/processor.go @@ -21,6 +21,11 @@ type ( IsActive() bool } + // PrecisionConverter converts balance amount values. + PrecisionConverter interface { + ToBalancePrecision(int64) int64 + } + // Processor of events produced by neofs contract in main net. Processor struct { log *zap.Logger @@ -31,6 +36,7 @@ type ( morphClient *client.Client epochState EpochState activeState ActiveState + converter PrecisionConverter } // Params of the processor constructor. @@ -43,6 +49,7 @@ type ( MorphClient *client.Client EpochState EpochState ActiveState ActiveState + Converter PrecisionConverter } ) @@ -65,6 +72,8 @@ func New(p *Params) (*Processor, error) { return nil, errors.New("ir/neofs: global state is not set") case p.ActiveState == nil: return nil, errors.New("ir/neofs: global state is not set") + case p.Converter == nil: + return nil, errors.New("ir/neofs: balance precision converter is not set") } p.Log.Debug("neofs worker pool", zap.Int("size", p.PoolSize)) @@ -83,6 +92,7 @@ func New(p *Params) (*Processor, error) { morphClient: p.MorphClient, epochState: p.EpochState, activeState: p.ActiveState, + converter: p.Converter, }, nil }