Skip to content

Commit

Permalink
Merge pull request #5059 from multiversx/bug-fix-gas-used-and-fee-scr…
Browse files Browse the repository at this point in the history
…-with-refund

Bug-fix `gasUsed` and `fee` transasctionsFeeProcessor
  • Loading branch information
gabi-vuls authored Mar 7, 2023
2 parents 88a5873 + 7553536 commit ed4cdbe
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions node/external/transactionAPI/gasUsedAndFeeProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func (gfp *gasUsedAndFeeProcessor) computeAndAttachGasUsedAndFee(tx *transaction
if !scr.IsRefund {
continue
}
if scr.RcvAddr != tx.Sender {
continue
}

gfp.setGasUsedAndFeeBaseOnRefundValue(tx, scr.Value)
hasRefund = true
Expand Down
3 changes: 3 additions & 0 deletions node/external/transactionAPI/gasUsedAndFeeProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,14 @@ func TestComputeTransactionGasUsedAndFeeTransactionWithScrWithRefund(t *testing.
RcvAddr: silentDecodeAddress(receiver),
Data: []byte("relayedTx@"),
},
Sender: sender,
Receiver: receiver,
GasLimit: 10_000_000,
SmartContractResults: []*transaction.ApiSmartContractResult{
{
Value: big.NewInt(66350000000000),
IsRefund: true,
RcvAddr: sender,
},
},
Logs: &transaction.ApiLogs{
Expand Down
6 changes: 6 additions & 0 deletions outport/process/transactionsfee/transactionsFeeProcessor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package transactionsfee

import (
"bytes"
"math/big"

"github.com/multiversx/mx-chain-core-go/core"
Expand Down Expand Up @@ -184,6 +185,11 @@ func (tep *transactionsFeeProcessor) prepareScrsNoTx(transactionsAndScrs *transa
continue
}

isForInitialTxSender := bytes.Equal(scr.RcvAddr, txFromStorage.SndAddr)
if !isForInitialTxSender {
continue
}

gasUsed, fee := tep.txFeeCalculator.ComputeGasUsedAndFeeBasedOnRefundValue(txFromStorage, scr.Value)

scrHandler.SetGasUsed(gasUsed)
Expand Down
47 changes: 47 additions & 0 deletions outport/process/transactionsfee/transactionsFeeProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func TestPutFeeAndGasUsedScrNoTx(t *testing.T) {
initialTx := &transaction.Transaction{
GasLimit: 30000000,
GasPrice: 1000000000,
SndAddr: []byte("erd1k7j6ewjsla4zsgv8v6f6fe3dvrkgv3d0d9jerczw45hzedhyed8sh2u34u"),
}
txBytes, _ := arg.Marshaller.Marshal(initialTx)

Expand Down Expand Up @@ -388,3 +389,49 @@ func TestPutFeeAndGasUsedScrWithRefundNoTx(t *testing.T) {
require.Equal(t, uint64(0), scr.GetGasUsed())
require.True(t, wasCalled)
}

func TestPutFeeAndGasUsedScrWithRefundNotForInitialSender(t *testing.T) {
t.Parallel()

txHash := []byte("tx")
scrWithRefund := []byte("scrWithRefund")

refundValueBig, _ := big.NewInt(0).SetString("226498540000000", 10)

scr := outportcore.NewTransactionHandlerWithGasAndFee(&smartContractResult.SmartContractResult{
Nonce: 3,
SndAddr: []byte("erd1qqqqqqqqqqqqqpgq3dswlnnlkfd3gqrcv3dhzgnvh8ryf27g5rfsecnn2s"),
RcvAddr: []byte("erd1k7j6ewjsla4zsgv8v6f6fe3dvrkgv3d0d9jerczw45hzedhyed8sh2u34u"),
PrevTxHash: []byte("f639cb7a0231191e04ec19dcb1359bd93a03fe8dc4a28a80d00835c5d1c988f8"),
OriginalTxHash: txHash,
Value: refundValueBig,
Data: []byte(""),
}, 0, big.NewInt(0))

pool := &outportcore.Pool{
Scrs: map[string]coreData.TransactionHandlerWithGasUsedAndFee{
"wrong": outportcore.NewTransactionHandlerWithGasAndFee(&transaction.Transaction{}, 0, big.NewInt(0)),
string(scrWithRefund): scr,
},
}

arg := prepareMockArg()

initialTx := &transaction.Transaction{
GasLimit: 30_000_000,
GasPrice: 1000000000,
SndAddr: []byte("erd1dglncxk6sl9a3xumj78n6z2xux4ghp5c92cstv5zsn56tjgtdwpsk46qrs"),
}
txBytes, _ := arg.Marshaller.Marshal(initialTx)

_ = arg.TransactionsStorer.Put(txHash, txBytes)

txsFeeProc, err := NewTransactionsFeeProcessor(arg)
require.NotNil(t, txsFeeProc)
require.Nil(t, err)

err = txsFeeProc.PutFeeAndGasUsed(pool)
require.Nil(t, err)
require.Equal(t, big.NewInt(0), scr.GetFee())
require.Equal(t, uint64(0), scr.GetGasUsed())
}

0 comments on commit ed4cdbe

Please sign in to comment.