Skip to content

Commit

Permalink
Merge pull request #6454 from multiversx/proper_fee_fix_for_relayed_a…
Browse files Browse the repository at this point in the history
…fter_flag

Proper fix for fee/initially paid fee for relayed after fix
  • Loading branch information
sstanculeanu authored Sep 13, 2024
2 parents afacd46 + 7becc67 commit 50114c8
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 12 deletions.
13 changes: 10 additions & 3 deletions node/external/transactionAPI/gasUsedAndFeeProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ func (gfp *gasUsedAndFeeProcessor) computeAndAttachGasUsedAndFee(tx *transaction
}

initialTotalFee, isRelayed := gfp.getFeeOfRelayed(tx)
if isRelayed && isFeeFixActive {
isRelayedAfterFix := isRelayed && isFeeFixActive
if isRelayedAfterFix {
tx.InitiallyPaidFee = initialTotalFee.String()
tx.Fee = initialTotalFee.String()
tx.GasUsed = big.NewInt(0).Div(initialTotalFee, big.NewInt(0).SetUint64(tx.GasPrice)).Uint64()
Expand All @@ -73,6 +74,10 @@ func (gfp *gasUsedAndFeeProcessor) computeAndAttachGasUsedAndFee(tx *transaction
tx.Fee = fee.String()
}

if isRelayedAfterFix {
return
}

gfp.prepareTxWithResultsBasedOnLogs(tx, hasRefundForSender)
}

Expand Down Expand Up @@ -120,7 +125,8 @@ func (gfp *gasUsedAndFeeProcessor) handleRelayedV1(args [][]byte, tx *transactio
fee := gfp.feeComputer.ComputeTxFeeBasedOnGasUsed(tx, gasUsed)

innerFee := gfp.feeComputer.ComputeTransactionFee(&transaction.ApiTransactionResult{
Tx: innerTx,
Tx: innerTx,
Epoch: tx.Epoch,
})

return big.NewInt(0).Add(fee, innerFee), true
Expand All @@ -141,7 +147,8 @@ func (gfp *gasUsedAndFeeProcessor) handleRelayedV2(args [][]byte, tx *transactio
fee := gfp.feeComputer.ComputeTxFeeBasedOnGasUsed(tx, gasUsed)

innerFee := gfp.feeComputer.ComputeTransactionFee(&transaction.ApiTransactionResult{
Tx: innerTx,
Tx: innerTx,
Epoch: tx.Epoch,
})

return big.NewInt(0).Add(fee, innerFee), true
Expand Down
50 changes: 50 additions & 0 deletions node/external/transactionAPI/gasUsedAndFeeProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/pubkeyConverter"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/node/external/timemachine/fee"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/economics"
"github.com/multiversx/mx-chain-go/process/smartContract"
"github.com/multiversx/mx-chain-go/testscommon"
"github.com/multiversx/mx-chain-go/testscommon/enableEpochsHandlerMock"
"github.com/multiversx/mx-chain-go/testscommon/epochNotifier"
Expand Down Expand Up @@ -359,3 +361,51 @@ func TestComputeAndAttachGasUsedAndFeeRelayedV3WithRefund(t *testing.T) {
require.Equal(t, uint64(55149500), txWithSRefundSCR.GasUsed)
require.Equal(t, "699500000000000", txWithSRefundSCR.Fee)
}

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

enableEpochsHandler := &enableEpochsHandlerMock.EnableEpochsHandlerStub{
IsFlagEnabledInEpochCalled: func(flag core.EnableEpochFlag, epoch uint32) bool {
return flag == common.GasPriceModifierFlag ||
flag == common.PenalizedTooMuchGasFlag ||
flag == common.FixRelayedBaseCostFlag
},
}
feeComp, _ := fee.NewFeeComputer(createEconomicsData(enableEpochsHandler))
computer := fee.NewTestFeeComputer(feeComp)

gasUsedAndFeeProc := newGasUsedAndFeeProcessor(
computer,
pubKeyConverter,
smartContract.NewArgumentParser(),
&marshal.JsonMarshalizer{},
enableEpochsHandler,
)

txWithSRefundSCR := &transaction.ApiTransactionResult{}
err := core.LoadJsonFile(txWithSRefundSCR, "testData/failedRelayedV1.json")
require.NoError(t, err)

snd, _ := pubKeyConverter.Decode(txWithSRefundSCR.Sender)
rcv, _ := pubKeyConverter.Decode(txWithSRefundSCR.Receiver)
val, _ := big.NewInt(0).SetString(txWithSRefundSCR.Value, 10)
txWithSRefundSCR.Tx = &transaction.Transaction{
Nonce: txWithSRefundSCR.Nonce,
Value: val,
RcvAddr: rcv,
SndAddr: snd,
GasPrice: txWithSRefundSCR.GasPrice,
GasLimit: txWithSRefundSCR.GasLimit,
Data: txWithSRefundSCR.Data,
}

txWithSRefundSCR.InitiallyPaidFee = ""
txWithSRefundSCR.Fee = ""
txWithSRefundSCR.GasUsed = 0

gasUsedAndFeeProc.computeAndAttachGasUsedAndFee(txWithSRefundSCR)
require.Equal(t, uint64(1274230), txWithSRefundSCR.GasUsed)
require.Equal(t, "1274230000000000", txWithSRefundSCR.Fee)
require.Equal(t, "1274230000000000", txWithSRefundSCR.InitiallyPaidFee)
}
60 changes: 60 additions & 0 deletions node/external/transactionAPI/testData/failedRelayedV1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"type": "normal",
"processingTypeOnSource": "RelayedTx",
"processingTypeOnDestination": "RelayedTx",
"hash": "efe3e8fa273fc2db4d559185e9729f7bbf17f617e28424b6a6533fb193caf561",
"nonce": 125,
"value": "0",
"receiver": "erd1x9hax7mdqux0nak9ahxrc2g75h5ckpfs4ssr8l7tkxscaa293x6q8ffd4e",
"sender": "erd1tn62hjp72rznp8vq0lplva5csav6rccpqqdungpxtqz0g2hcq6uq9k4cc6",
"gasPrice": 1000000000,
"gasLimit": 6148000,
"data": "cmVsYXllZFR4QDdiMjI2ZTZmNmU2MzY1MjIzYTM0MzcyYzIyNzM2NTZlNjQ2NTcyMjIzYTIyNGQ1NzJmNTQ2NTMyMzA0ODQ0NTA2ZTMyNzg2NTMzNGQ1MDQzNmI2NTcwNjU2ZDRjNDI1NDQzNzM0OTQ0NTAyZjc5Mzc0NzY4NmE3NjU2NDY2OTYyNTEzZDIyMmMyMjcyNjU2MzY1Njk3NjY1N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gZG9lcyBub3QgYWNjZXB0IEVHTEQgcGF5bWVudA=="
],
"data": "QDY1Nzg2NTYzNzU3NDY5NmY2ZTIwNjY2MTY5NmM2NTY0",
"additionalData": [
"QDY1Nzg2NTYzNzU3NDY5NmY2ZTIwNjY2MTY5NmM2NTY0"
]
},
{
"address": "erd1qqqqqqqqqqqqqpgq8efw6ak0e9q2as9zzr38aa2r9gy4lxcnq6uqpefzvu",
"identifier": "signalError",
"topics": [
"XPSryD5QxTCdgH/D9naYh1mh4wEAG8mgJlgE9Cr4Brg=",
"ZnVuY3Rpb24gZG9lcyBub3QgYWNjZXB0IEVHTEQgcGF5bWVudA=="
],
"data": null,
"additionalData": [
""
]
},
{
"address": "erd1x9hax7mdqux0nak9ahxrc2g75h5ckpfs4ssr8l7tkxscaa293x6q8ffd4e",
"identifier": "internalVMErrors",
"topics": [
"AAAAAAAAAAAFAD5S7XbPyUCuwKIQ4n71QyoJX5sTBrg=",
"YWRk"
],
"data": "CglydW50aW1lLmdvOjg1NiBbZXhlY3V0aW9uIGZhaWxlZF0gW2FkZF0KCXJ1bnRpbWUuZ286ODU2IFtleGVjdXRpb24gZmFpbGVkXSBbYWRkXQoJcnVudGltZS5nbzo4NTMgW2Z1bmN0aW9uIGRvZXMgbm90IGFjY2VwdCBFR0xEIHBheW1lbnRd",
"additionalData": [
"CglydW50aW1lLmdvOjg1NiBbZXhlY3V0aW9uIGZhaWxlZF0gW2FkZF0KCXJ1bnRpbWUuZ286ODU2IFtleGVjdXRpb24gZmFpbGVkXSBbYWRkXQoJcnVudGltZS5nbzo4NTMgW2Z1bmN0aW9uIGRvZXMgbm90IGFjY2VwdCBFR0xEIHBheW1lbnRd"
]
}
]
},
"status": "success",
"operation": "transfer",
"function": "add",
"isRelayed": true
}
22 changes: 13 additions & 9 deletions outport/process/transactionsfee/transactionsFeeProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,20 @@ func (tep *transactionsFeeProcessor) prepareNormalTxs(transactionsAndScrs *trans
continue
}

if isRelayedTx(txWithResult) && isFeeFixActive {
totalFee, isRelayed := tep.getFeeOfRelayed(txWithResult)
if isRelayed {
feeInfo.SetFee(totalFee)
feeInfo.SetInitialPaidFee(totalFee)
feeInfo.SetGasUsed(big.NewInt(0).Div(totalFee, big.NewInt(0).SetUint64(txHandler.GetGasPrice())).Uint64())
}
totalFee, isRelayed := tep.getFeeOfRelayed(txWithResult)
isRelayedAfterFix := isRelayed && isFeeFixActive
if isRelayedAfterFix {
feeInfo.SetFee(totalFee)
feeInfo.SetInitialPaidFee(totalFee)
feeInfo.SetGasUsed(big.NewInt(0).Div(totalFee, big.NewInt(0).SetUint64(txHandler.GetGasPrice())).Uint64())

}

tep.prepareTxWithResults(txHashHex, txWithResult)
tep.prepareTxWithResults(txHashHex, txWithResult, isRelayedAfterFix)
}
}

func (tep *transactionsFeeProcessor) prepareTxWithResults(txHashHex string, txWithResults *transactionWithResults) {
func (tep *transactionsFeeProcessor) prepareTxWithResults(txHashHex string, txWithResults *transactionWithResults, isRelayedAfterFix bool) {
hasRefund := false
totalRefunds := big.NewInt(0)
for _, scrHandler := range txWithResults.scrs {
Expand All @@ -174,6 +174,10 @@ func (tep *transactionsFeeProcessor) prepareTxWithResults(txHashHex string, txWi
txWithResults.GetFeeInfo().SetFee(fee)
}

if isRelayedAfterFix {
return
}

tep.prepareTxWithResultsBasedOnLogs(txHashHex, txWithResults, hasRefund)
}

Expand Down

0 comments on commit 50114c8

Please sign in to comment.