From 83fc38ae3a3e63c8836b68b800e503c1b0a58c07 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 10 Sep 2020 15:35:19 +0300 Subject: [PATCH] mempool: don't create new big.Int in tryAddSendersFee() if possible Do a little less allocations. --- pkg/core/mempool/mem_pool.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/pkg/core/mempool/mem_pool.go b/pkg/core/mempool/mem_pool.go index 31e34f4fb..a0800ccf3 100644 --- a/pkg/core/mempool/mem_pool.go +++ b/pkg/core/mempool/mem_pool.go @@ -117,25 +117,30 @@ func (mp *Pool) tryAddSendersFee(tx *transaction.Transaction, feer Feer, needChe senderFee.feeSum = big.NewInt(0) mp.fees[tx.Sender()] = senderFee } - if needCheck && checkBalance(tx, senderFee) != nil { - return false + if needCheck { + newFeeSum, err := checkBalance(tx, senderFee) + if err != nil { + return false + } + senderFee.feeSum.Set(newFeeSum) + } else { + senderFee.feeSum.Add(senderFee.feeSum, big.NewInt(tx.SystemFee+tx.NetworkFee)) } - senderFee.feeSum.Add(senderFee.feeSum, big.NewInt(tx.SystemFee+tx.NetworkFee)) return true } -// checkBalance returns nil in case when sender has enough GAS to pay for the -// transaction -func checkBalance(tx *transaction.Transaction, balance utilityBalanceAndFees) error { +// checkBalance returns new cumulative fee balance for account or an error in +// case sender doesn't have enough GAS to pay for the transaction. +func checkBalance(tx *transaction.Transaction, balance utilityBalanceAndFees) (*big.Int, error) { txFee := big.NewInt(tx.SystemFee + tx.NetworkFee) if balance.balance.Cmp(txFee) < 0 { - return ErrInsufficientFunds + return nil, ErrInsufficientFunds } - needFee := txFee.Add(txFee, balance.feeSum) - if balance.balance.Cmp(needFee) < 0 { - return ErrConflict + txFee.Add(txFee, balance.feeSum) + if balance.balance.Cmp(txFee) < 0 { + return nil, ErrConflict } - return nil + return txFee, nil } // Add tries to add given transaction to the Pool. @@ -299,7 +304,8 @@ func (mp *Pool) checkTxConflicts(tx *transaction.Transaction, fee Feer) error { senderFee.balance = fee.GetUtilityTokenBalance(tx.Sender()) senderFee.feeSum = big.NewInt(0) } - return checkBalance(tx, senderFee) + _, err := checkBalance(tx, senderFee) + return err } // Verify checks if a Sender of tx is able to pay for it (and all the other