From 0e3adbbb2a81955a82e87f83f10d0705e3f83916 Mon Sep 17 00:00:00 2001 From: Sorin Stanculeanu Date: Wed, 11 Sep 2024 10:41:06 +0300 Subject: [PATCH 1/4] proper fix for fee/initially paid fee for relayed after fix --- .../transactionAPI/gasUsedAndFeeProcessor.go | 7 +++++- .../transactionsFeeProcessor.go | 22 +++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/node/external/transactionAPI/gasUsedAndFeeProcessor.go b/node/external/transactionAPI/gasUsedAndFeeProcessor.go index 5f4e2626fca..7f957f50ece 100644 --- a/node/external/transactionAPI/gasUsedAndFeeProcessor.go +++ b/node/external/transactionAPI/gasUsedAndFeeProcessor.go @@ -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() @@ -73,6 +74,10 @@ func (gfp *gasUsedAndFeeProcessor) computeAndAttachGasUsedAndFee(tx *transaction tx.Fee = fee.String() } + if isRelayedAfterFix { + return + } + gfp.prepareTxWithResultsBasedOnLogs(tx, hasRefundForSender) } diff --git a/outport/process/transactionsfee/transactionsFeeProcessor.go b/outport/process/transactionsfee/transactionsFeeProcessor.go index ac92ce09790..dccf0366b23 100644 --- a/outport/process/transactionsfee/transactionsFeeProcessor.go +++ b/outport/process/transactionsfee/transactionsFeeProcessor.go @@ -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 { @@ -174,6 +174,10 @@ func (tep *transactionsFeeProcessor) prepareTxWithResults(txHashHex string, txWi txWithResults.GetFeeInfo().SetFee(fee) } + if isRelayedAfterFix { + return + } + tep.prepareTxWithResultsBasedOnLogs(txHashHex, txWithResults, hasRefund) } From cfc8c78746bc60c8ed919c3f7b08788714dc5040 Mon Sep 17 00:00:00 2001 From: Sorin Stanculeanu Date: Wed, 11 Sep 2024 13:11:10 +0300 Subject: [PATCH 2/4] added unittest --- .../gasUsedAndFeeProcessor_test.go | 50 +++++++++++++++ .../testData/failedRelayedV1.json | 61 +++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 node/external/transactionAPI/testData/failedRelayedV1.json diff --git a/node/external/transactionAPI/gasUsedAndFeeProcessor_test.go b/node/external/transactionAPI/gasUsedAndFeeProcessor_test.go index 3c8bb6d05cd..7e4011e0f2c 100644 --- a/node/external/transactionAPI/gasUsedAndFeeProcessor_test.go +++ b/node/external/transactionAPI/gasUsedAndFeeProcessor_test.go @@ -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" @@ -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) +} diff --git a/node/external/transactionAPI/testData/failedRelayedV1.json b/node/external/transactionAPI/testData/failedRelayedV1.json new file mode 100644 index 00000000000..5f1a72c8c33 --- /dev/null +++ b/node/external/transactionAPI/testData/failedRelayedV1.json @@ -0,0 +1,61 @@ +{ + "type": "normal", + "processingTypeOnSource": "RelayedTx", + "processingTypeOnDestination": "RelayedTx", + "hash": "efe3e8fa273fc2db4d559185e9729f7bbf17f617e28424b6a6533fb193caf561", + "nonce": 125, + "value": "0", + "receiver": "erd1x9hax7mdqux0nak9ahxrc2g75h5ckpfs4ssr8l7tkxscaa293x6q8ffd4e", + "sender": "erd1tn62hjp72rznp8vq0lplva5csav6rccpqqdungpxtqz0g2hcq6uq9k4cc6", + "gasPrice": 1000000000, + "gasLimit": 6148000, + "gasUsed": 6148000, + "data": "cmVsYXllZFR4QDdiMjI2ZTZmNmU2MzY1MjIzYTM0MzcyYzIyNzM2NTZlNjQ2NTcyMjIzYTIyNGQ1NzJmNTQ2NTMyMzA0ODQ0NTA2ZTMyNzg2NTMzNGQ1MDQzNmI2NTcwNjU2ZDRjNDI1NDQzNzM0OTQ0NTAyZjc5Mzc0NzY4NmE3NjU2NDY2OTYyNTEzZDIyMmMyMjcyNjU2MzY1Njk3NjY1NzIyMjNhMjI0MTQxNDE0MTQxNDE0MTQxNDE0MTQxNDY0MTQ0MzU1MzM3NTg2MjUwNzk1NTQzNzU3NzRiND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2MTY5NmM2NTY0", + "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 +} \ No newline at end of file From 1f2d5930888ca7c98ff7ad052d9c765ad382c9ca Mon Sep 17 00:00:00 2001 From: Sorin Stanculeanu Date: Wed, 11 Sep 2024 13:13:17 +0300 Subject: [PATCH 3/4] new line at the end of newly added test json --- node/external/transactionAPI/testData/failedRelayedV1.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/node/external/transactionAPI/testData/failedRelayedV1.json b/node/external/transactionAPI/testData/failedRelayedV1.json index 5f1a72c8c33..e2b1be88bee 100644 --- a/node/external/transactionAPI/testData/failedRelayedV1.json +++ b/node/external/transactionAPI/testData/failedRelayedV1.json @@ -9,7 +9,6 @@ "sender": "erd1tn62hjp72rznp8vq0lplva5csav6rccpqqdungpxtqz0g2hcq6uq9k4cc6", "gasPrice": 1000000000, "gasLimit": 6148000, - "gasUsed": 6148000, "data": "cmVsYXllZFR4QDdiMjI2ZTZmNmU2MzY1MjIzYTM0MzcyYzIyNzM2NTZlNjQ2NTcyMjIzYTIyNGQ1NzJmNTQ2NTMyMzA0ODQ0NTA2ZTMyNzg2NTMzNGQ1MDQzNmI2NTcwNjU2ZDRjNDI1NDQzNzM0OTQ0NTAyZjc5Mzc0NzY4NmE3NjU2NDY2OTYyNTEzZDIyMmMyMjcyNjU2MzY1Njk3NjY1NzIyMjNhMjI0MTQxNDE0MTQxNDE0MTQxNDE0MTQxNDY0MTQ0MzU1MzM3NTg2MjUwNzk1NTQzNzU3NzRiNDk1MTM0NmUzNzMxNTE3OTZmNGE1ODM1NzM1NDQyNzI2NzNkMjIyYzIyNzY2MTZjNzU2NTIyM2EzMTMwMzAzMDMwMzAzMDMwMzAzMDMwMzAzMDMwMzAzMDMwMzAzMDJjMjI2NzYxNzM1MDcyNjk2MzY1MjIzYTMxMzAzMDMwMzAzMDMwMzAzMDMwMmMyMjY3NjE3MzRjNjk2ZDY5NzQyMjNhMzUzMDMwMzAzMDMwMzAyYzIyNjQ2MTc0NjEyMjNhMjI1OTU3NTI2YjUxNDQ0MTc4NTE0NDQxNzk1MTQ0NDE3YTUxNDQ0MTMwNTE0NDQxMzEyMjJjMjI3MzY5Njc2ZTYxNzQ3NTcyNjUyMjNhMjI3NjRjNjQ2ZDMyMmY0ZTQ0NTQ0NDc3MzgzMTMxNTEzMDYyNzgyYjU4NGI1MTc4NTA0NTJmNzY0OTU3NDM2ODRlNzI2OTU1NmE3ODcyNzAyZjU5Mzg1MDRlNzI0ZDQ1NmM2NTQ3NmUzNTMxNTM3NjUyNTk2NDQ3MzQ2ZjQ3NTc3MjQ5Njk0MzQ4Mzk2YTMxNzY0ZjRiNmY1OTJmNDg1NTUyNjE0ZTQxNDg0MjUxM2QzZDIyMmMyMjYzNjg2MTY5NmU0OTQ0MjIzYTIyNTY0MTNkM2QyMjJjMjI3NjY1NzI3MzY5NmY2ZTIyM2EzMjdk", "sourceShard": 0, "destinationShard": 0, @@ -58,4 +57,4 @@ "operation": "transfer", "function": "add", "isRelayed": true -} \ No newline at end of file +} From 7becc67db2e307548cb0ebaf144d2141758b22fe Mon Sep 17 00:00:00 2001 From: Sorin Stanculeanu Date: Wed, 11 Sep 2024 13:40:56 +0300 Subject: [PATCH 4/4] added missing epoch for inner tx --- node/external/transactionAPI/gasUsedAndFeeProcessor.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/node/external/transactionAPI/gasUsedAndFeeProcessor.go b/node/external/transactionAPI/gasUsedAndFeeProcessor.go index 7f957f50ece..530b6b81b3d 100644 --- a/node/external/transactionAPI/gasUsedAndFeeProcessor.go +++ b/node/external/transactionAPI/gasUsedAndFeeProcessor.go @@ -125,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 @@ -146,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