diff --git a/process/transaction/transactionCostEstimator.go b/process/transaction/transactionCostEstimator.go index 678f721af31..1d355010b75 100644 --- a/process/transaction/transactionCostEstimator.go +++ b/process/transaction/transactionCostEstimator.go @@ -67,11 +67,14 @@ func (tce *transactionCostEstimator) ComputeTransactionGasLimit(tx *transaction. tce.mutExecution.RLock() defer tce.mutExecution.RUnlock() - txType, _ := tce.txTypeHandler.ComputeTransactionType(tx) + txTypeOnSender, txTypeOnDestination := tce.txTypeHandler.ComputeTransactionType(tx) + if txTypeOnSender == process.MoveBalance && txTypeOnDestination == process.MoveBalance { + return tce.computeMoveBalanceCost(tx), nil + } - switch txType { + switch txTypeOnSender { case process.SCDeployment, process.SCInvoking, process.BuiltInFunctionCall, process.MoveBalance: - return tce.simulateTransactionCost(tx, txType) + return tce.simulateTransactionCost(tx, txTypeOnSender) case process.RelayedTx, process.RelayedTxV2: // TODO implement in the next PR return &transaction.CostResponse{ @@ -86,6 +89,15 @@ func (tce *transactionCostEstimator) ComputeTransactionGasLimit(tx *transaction. } } +func (tce *transactionCostEstimator) computeMoveBalanceCost(tx *transaction.Transaction) *transaction.CostResponse { + gasUnits := tce.feeHandler.ComputeGasLimit(tx) + + return &transaction.CostResponse{ + GasUnits: gasUnits, + ReturnMessage: "", + } +} + func (tce *transactionCostEstimator) simulateTransactionCost(tx *transaction.Transaction, txType process.TransactionType) (*transaction.CostResponse, error) { err := tce.addMissingFieldsIfNeeded(tx) if err != nil { diff --git a/process/transaction/transactionCostEstimator_test.go b/process/transaction/transactionCostEstimator_test.go index 7634cd05d63..b4b5d31de9d 100644 --- a/process/transaction/transactionCostEstimator_test.go +++ b/process/transaction/transactionCostEstimator_test.go @@ -87,6 +87,38 @@ func TestComputeTransactionGasLimit_MoveBalance(t *testing.T) { require.Equal(t, consumedGasUnits, cost.GasUnits) } +func TestComputeTransactionGasLimit_MoveBalanceInvalidNonceShouldStillComputeCost(t *testing.T) { + t.Parallel() + + simulationErr := errors.New("invalid nonce") + consumedGasUnits := uint64(1000) + tce, _ := NewTransactionCostEstimator(&testscommon.TxTypeHandlerMock{ + ComputeTransactionTypeCalled: func(tx data.TransactionHandler) (process.TransactionType, process.TransactionType) { + return process.MoveBalance, process.MoveBalance + }, + }, &mock.FeeHandlerStub{ + MaxGasLimitPerBlockCalled: func() uint64 { + return math.MaxUint64 + }, + ComputeGasLimitCalled: func(tx data.TransactionWithFeeHandler) uint64 { + return consumedGasUnits + }, + }, &mock.TransactionSimulatorStub{ + ProcessTxCalled: func(tx *transaction.Transaction) (*txSimData.SimulationResults, error) { + return nil, simulationErr + }, + }, &stateMock.AccountsStub{ + LoadAccountCalled: func(address []byte) (vmcommon.AccountHandler, error) { + return &stateMock.UserAccountStub{Balance: big.NewInt(100000)}, nil + }, + }, &mock.ShardCoordinatorStub{}) + + tx := &transaction.Transaction{} + cost, err := tce.ComputeTransactionGasLimit(tx) + require.Nil(t, err) + require.Equal(t, consumedGasUnits, cost.GasUnits) +} + func TestComputeTransactionGasLimit_BuiltInFunction(t *testing.T) { consumedGasUnits := uint64(4000) tce, _ := NewTransactionCostEstimator(&testscommon.TxTypeHandlerMock{