[#122] Add converter interface in balance and neofs processors

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-10-27 13:49:09 +03:00 committed by Alex Vanin
parent b6012977bc
commit 90984ee219
4 changed files with 24 additions and 4 deletions

View file

@ -17,7 +17,7 @@ func (bp *Processor) processLock(lock *balanceEvent.Lock) {
err := invoke.CashOutCheque(bp.mainnetClient, bp.neofsContract, err := invoke.CashOutCheque(bp.mainnetClient, bp.neofsContract,
&invoke.ChequeParams{ &invoke.ChequeParams{
ID: lock.ID(), ID: lock.ID(),
Amount: lock.Amount() / 1_0000_0000, // Fixed16 to Fixed8 Amount: bp.converter.ToFixed8(lock.Amount()),
User: lock.User(), User: lock.User(),
LockAccount: lock.LockAccount(), LockAccount: lock.LockAccount(),
}) })

View file

@ -16,6 +16,11 @@ type (
IsActive() bool IsActive() bool
} }
// PrecisionConverter converts balance amount values.
PrecisionConverter interface {
ToFixed8(int64) int64
}
// Processor of events produced by balance contract in morph chain. // Processor of events produced by balance contract in morph chain.
Processor struct { Processor struct {
log *zap.Logger log *zap.Logger
@ -24,6 +29,7 @@ type (
balanceContract util.Uint160 balanceContract util.Uint160
mainnetClient *client.Client mainnetClient *client.Client
activeState ActiveState activeState ActiveState
converter PrecisionConverter
} }
// Params of the processor constructor. // Params of the processor constructor.
@ -34,6 +40,7 @@ type (
BalanceContract util.Uint160 BalanceContract util.Uint160
MainnetClient *client.Client MainnetClient *client.Client
ActiveState ActiveState 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") return nil, errors.New("ir/balance: neo:mainnet client is not set")
case p.ActiveState == nil: case p.ActiveState == nil:
return nil, errors.New("ir/balance: global state is not set") 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)) p.Log.Debug("balance worker pool", zap.Int("size", p.PoolSize))
@ -66,6 +75,7 @@ func New(p *Params) (*Processor, error) {
balanceContract: p.BalanceContract, balanceContract: p.BalanceContract,
mainnetClient: p.MainnetClient, mainnetClient: p.MainnetClient,
activeState: p.ActiveState, activeState: p.ActiveState,
converter: p.Converter,
}, nil }, nil
} }

View file

@ -27,7 +27,7 @@ func (np *Processor) processDeposit(deposit *neofsEvent.Deposit) {
err := invoke.Mint(np.morphClient, np.balanceContract, err := invoke.Mint(np.morphClient, np.balanceContract,
&invoke.MintBurnParams{ &invoke.MintBurnParams{
ScriptHash: deposit.To().BytesBE(), 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()...), Comment: append([]byte(txLogPrefix), deposit.ID()...),
}) })
if err != nil { if err != nil {
@ -69,7 +69,7 @@ func (np *Processor) processWithdraw(withdraw *neofsEvent.Withdraw) {
ID: withdraw.ID(), ID: withdraw.ID(),
User: withdraw.User(), User: withdraw.User(),
LockAccount: lock, LockAccount: lock,
Amount: withdraw.Amount() * 1_0000_0000, // from Fixed8 to Fixed16 Amount: np.converter.ToBalancePrecision(withdraw.Amount()),
Until: curEpoch + lockAccountLifetime, Until: curEpoch + lockAccountLifetime,
}) })
if err != nil { if err != nil {
@ -88,7 +88,7 @@ func (np *Processor) processCheque(cheque *neofsEvent.Cheque) {
err := invoke.Burn(np.morphClient, np.balanceContract, err := invoke.Burn(np.morphClient, np.balanceContract,
&invoke.MintBurnParams{ &invoke.MintBurnParams{
ScriptHash: cheque.LockAccount().BytesBE(), 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()...), Comment: append([]byte(txLogPrefix), cheque.ID()...),
}) })
if err != nil { if err != nil {

View file

@ -21,6 +21,11 @@ type (
IsActive() bool IsActive() bool
} }
// PrecisionConverter converts balance amount values.
PrecisionConverter interface {
ToBalancePrecision(int64) int64
}
// Processor of events produced by neofs contract in main net. // Processor of events produced by neofs contract in main net.
Processor struct { Processor struct {
log *zap.Logger log *zap.Logger
@ -31,6 +36,7 @@ type (
morphClient *client.Client morphClient *client.Client
epochState EpochState epochState EpochState
activeState ActiveState activeState ActiveState
converter PrecisionConverter
} }
// Params of the processor constructor. // Params of the processor constructor.
@ -43,6 +49,7 @@ type (
MorphClient *client.Client MorphClient *client.Client
EpochState EpochState EpochState EpochState
ActiveState ActiveState 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") return nil, errors.New("ir/neofs: global state is not set")
case p.ActiveState == nil: case p.ActiveState == nil:
return nil, errors.New("ir/neofs: global state is not set") 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)) p.Log.Debug("neofs worker pool", zap.Int("size", p.PoolSize))
@ -83,6 +92,7 @@ func New(p *Params) (*Processor, error) {
morphClient: p.MorphClient, morphClient: p.MorphClient,
epochState: p.EpochState, epochState: p.EpochState,
activeState: p.ActiveState, activeState: p.ActiveState,
converter: p.Converter,
}, nil }, nil
} }