From 3e60771175525a06621292dcd8422cda9a01f3e2 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 11 Aug 2021 22:36:26 +0300 Subject: [PATCH] core: deduplicate and simplify processNEP17Transfer a bit Just refactoring, no functional changes. --- pkg/core/blockchain.go | 54 ++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index f9e9f3072..f2b08c83a 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -1066,46 +1066,34 @@ func (bc *Blockchain) processNEP17Transfer(cache *dao.Cached, h util.Uint256, b Tx: h, } if !fromAddr.Equals(util.Uint160{}) { - balances, err := cache.GetNEP17TransferInfo(fromAddr) - if err != nil { - return - } - balances.LastUpdated[id] = b.Index - transfer.Amount = *new(big.Int).Sub(&transfer.Amount, amount) - balances.NewBatch, err = cache.AppendNEP17Transfer(fromAddr, - balances.NextTransferBatch, balances.NewBatch, transfer) - if err != nil { - return - } - if balances.NewBatch { - balances.NextTransferBatch++ - } - if err := cache.PutNEP17TransferInfo(fromAddr, balances); err != nil { + _ = transfer.Amount.Neg(amount) // We already have the Int. + if appendNEP17Transfer(cache, fromAddr, transfer) != nil { return } } if !toAddr.Equals(util.Uint160{}) { - balances, err := cache.GetNEP17TransferInfo(toAddr) - if err != nil { - return - } - balances.LastUpdated[id] = b.Index - - transfer.Amount = *amount - balances.NewBatch, err = cache.AppendNEP17Transfer(toAddr, - balances.NextTransferBatch, balances.NewBatch, transfer) - if err != nil { - return - } - if balances.NewBatch { - balances.NextTransferBatch++ - } - if err := cache.PutNEP17TransferInfo(toAddr, balances); err != nil { - return - } + _ = transfer.Amount.Set(amount) // We already have the Int. + _ = appendNEP17Transfer(cache, toAddr, transfer) // Nothing useful we can do. } } +func appendNEP17Transfer(cache *dao.Cached, addr util.Uint160, transfer *state.NEP17Transfer) error { + balances, err := cache.GetNEP17TransferInfo(addr) + if err != nil { + return err + } + balances.LastUpdated[transfer.Asset] = transfer.Block + balances.NewBatch, err = cache.AppendNEP17Transfer(addr, + balances.NextTransferBatch, balances.NewBatch, transfer) + if err != nil { + return err + } + if balances.NewBatch { + balances.NextTransferBatch++ + } + return cache.PutNEP17TransferInfo(addr, balances) +} + // ForEachNEP17Transfer executes f for each nep17 transfer in log. func (bc *Blockchain) ForEachNEP17Transfer(acc util.Uint160, f func(*state.NEP17Transfer) (bool, error)) error { balances, err := bc.dao.GetNEP17TransferInfo(acc)