Skip to content

Commit

Permalink
Merge pull request #3472 from ElrondNetwork/tx-cost-improvements
Browse files Browse the repository at this point in the history
improve tx cost computation
  • Loading branch information
LucianMincu authored Sep 28, 2021
2 parents 54b931d + 83e74d3 commit 6922167
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
18 changes: 15 additions & 3 deletions process/transaction/transactionCostEstimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions process/transaction/transactionCostEstimator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 6922167

Please sign in to comment.