From b20f8f1edff8ab8cbaa74475737b3757f51f67a3 Mon Sep 17 00:00:00 2001 From: Jacky Wang Date: Fri, 9 Apr 2021 11:53:25 -0700 Subject: [PATCH] [txPool] added gas balance check when validating delegate transaction --- core/tx_pool.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/core/tx_pool.go b/core/tx_pool.go index 93426e8c3..9c35a668d 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -839,9 +839,19 @@ func (pool *TxPool) validateStakingTx(tx *staking.StakingTransaction) error { return err } pendingEpoch := pool.pendingEpoch() - _, _, _, err = VerifyAndDelegateFromMsg( + _, delegateAmt, _, err := VerifyAndDelegateFromMsg( pool.currentState, pendingEpoch, stkMsg, delegations, pool.chainconfig.IsRedelegation(pendingEpoch)) - return err + if err != nil { + return err + } + // We need to deduct gas price and verify balance since txn.Cost() is not accurate for delegate + // staking transaction because of re-delegation. + gasAmt := new(big.Int).Mul(tx.GasPrice(), new(big.Int).SetUint64(tx.GasLimit()))) + totalAmt := new(big.Int).Add(delegateAmt, gasAmt) + if bal := pool.currentState.GetBalance(from); bal.Cmp(totalAmt) < 0 { + return fmt.Errorf("not enough balance for delegation: %v < %v", bal, delegateAmt) + } + return nil case staking.DirectiveUndelegate: msg, err := staking.RLPDecodeStakeMsg(tx.Data(), staking.DirectiveUndelegate) if err != nil {