Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion node/external/transactionAPI/apiTransactionProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (atp *apiTransactionProcessor) populateComputedFieldInitiallyPaidFee(tx *tr
isFeeFixActive := atp.enableEpochsHandler.IsFlagEnabledInEpoch(common.FixRelayedBaseCostFlag, tx.Epoch)
isRelayedAfterFix := tx.IsRelayed && isFeeFixActive
if isRelayedAfterFix {
fee, _ = atp.gasUsedAndFeeProcessor.getFeeOfRelayed(tx)
_, fee, _ = atp.gasUsedAndFeeProcessor.getFeeOfRelayed(tx)
tx.InitiallyPaidFee = fee.String()
}
}
Expand Down
80 changes: 55 additions & 25 deletions node/external/transactionAPI/gasUsedAndFeeProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math/big"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-go/common"
Expand Down Expand Up @@ -49,7 +50,7 @@ func (gfp *gasUsedAndFeeProcessor) computeAndAttachGasUsedAndFee(tx *transaction
tx.Fee = tx.InitiallyPaidFee
}

initialTotalFee, isRelayed := gfp.getFeeOfRelayed(tx)
userTx, initialTotalFee, isRelayed := gfp.getFeeOfRelayed(tx)
isRelayedAfterFix := isRelayed && isFeeFixActive
if isRelayedAfterFix {
tx.InitiallyPaidFee = initialTotalFee.String()
Expand All @@ -63,26 +64,26 @@ func (gfp *gasUsedAndFeeProcessor) computeAndAttachGasUsedAndFee(tx *transaction
continue
}

gfp.setGasUsedAndFeeBaseOnRefundValue(tx, scr.Value)
gfp.setGasUsedAndFeeBaseOnRefundValue(tx, userTx, scr.Value)
hasRefundForSender = true
break
}

gfp.prepareTxWithResultsBasedOnLogs(tx, hasRefundForSender)
gfp.prepareTxWithResultsBasedOnLogs(tx, userTx, hasRefundForSender)
}

func (gfp *gasUsedAndFeeProcessor) getFeeOfRelayed(tx *transaction.ApiTransactionResult) (*big.Int, bool) {
func (gfp *gasUsedAndFeeProcessor) getFeeOfRelayed(tx *transaction.ApiTransactionResult) (*transaction.ApiTransactionResult, *big.Int, bool) {
if !tx.IsRelayed {
return nil, false
return nil, nil, false
}

if len(tx.Data) == 0 {
return nil, false
return nil, nil, false
}

funcName, args, err := gfp.argsParser.ParseCallData(string(tx.Data))
if err != nil {
return nil, false
return nil, nil, false
}

if funcName == core.RelayedTransaction {
Expand All @@ -93,32 +94,33 @@ func (gfp *gasUsedAndFeeProcessor) getFeeOfRelayed(tx *transaction.ApiTransactio
return gfp.handleRelayedV2(args, tx)
}

return nil, false
return nil, nil, false
}

func (gfp *gasUsedAndFeeProcessor) handleRelayedV1(args [][]byte, tx *transaction.ApiTransactionResult) (*big.Int, bool) {
func (gfp *gasUsedAndFeeProcessor) handleRelayedV1(args [][]byte, tx *transaction.ApiTransactionResult) (*transaction.ApiTransactionResult, *big.Int, bool) {
if len(args) != 1 {
return nil, false
return nil, nil, false
}

innerTx := &transaction.Transaction{}
err := gfp.marshaller.Unmarshal(innerTx, args[0])
if err != nil {
return nil, false
return nil, nil, false
}

gasUsed := gfp.feeComputer.ComputeGasLimit(tx)
fee := gfp.feeComputer.ComputeTxFeeBasedOnGasUsed(tx, gasUsed)

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

return big.NewInt(0).Add(fee, innerFee), true
return innerTxApiResult, big.NewInt(0).Add(fee, innerFee), true
}

func (gfp *gasUsedAndFeeProcessor) handleRelayedV2(args [][]byte, tx *transaction.ApiTransactionResult) (*big.Int, bool) {
func (gfp *gasUsedAndFeeProcessor) handleRelayedV2(args [][]byte, tx *transaction.ApiTransactionResult) (*transaction.ApiTransactionResult, *big.Int, bool) {
innerTx := &transaction.Transaction{}
innerTx.RcvAddr = args[0]
innerTx.Nonce = big.NewInt(0).SetBytes(args[1]).Uint64()
Expand All @@ -132,32 +134,45 @@ func (gfp *gasUsedAndFeeProcessor) handleRelayedV2(args [][]byte, tx *transactio
gasUsed := gfp.feeComputer.ComputeGasLimit(tx)
fee := gfp.feeComputer.ComputeTxFeeBasedOnGasUsed(tx, gasUsed)

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

return big.NewInt(0).Add(fee, innerFee), true
return innerTxApiResult, big.NewInt(0).Add(fee, innerFee), true
}

func (gfp *gasUsedAndFeeProcessor) prepareTxWithResultsBasedOnLogs(
tx *transaction.ApiTransactionResult,
userTx *transaction.ApiTransactionResult,
hasRefund bool,
) {
if tx.Logs == nil || (tx.Function == "" && tx.Operation == datafield.OperationTransfer) {
return
}

for _, event := range tx.Logs.Events {
gfp.setGasUsedAndFeeBaseOnLogEvent(tx, hasRefund, event)
gfp.setGasUsedAndFeeBaseOnLogEvent(tx, userTx, hasRefund, event)
}
}

func (gfp *gasUsedAndFeeProcessor) setGasUsedAndFeeBaseOnLogEvent(tx *transaction.ApiTransactionResult, hasRefund bool, event *transaction.Events) {
func (gfp *gasUsedAndFeeProcessor) setGasUsedAndFeeBaseOnLogEvent(tx *transaction.ApiTransactionResult, userTx *transaction.ApiTransactionResult, hasRefund bool, event *transaction.Events) {
if core.WriteLogIdentifier == event.Identifier && !hasRefund {
gasUsed, fee := gfp.feeComputer.ComputeGasUsedAndFeeBasedOnRefundValue(tx, big.NewInt(0))
tx.GasUsed = gasUsed
tx.Fee = fee.String()
if !check.IfNilReflect(userTx) && gfp.enableEpochsHandler.IsFlagEnabledInEpoch(common.FixRelayedBaseCostFlag, tx.Epoch) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check.IfNil

gasUsed, fee := gfp.feeComputer.ComputeGasUsedAndFeeBasedOnRefundValue(userTx, big.NewInt(0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code block L162-L174 is very similar to L189-L206, can you extract it in a function and call it in both places?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in #6546

gasUsedRelayedTx := gfp.feeComputer.ComputeGasLimit(tx)
feeRelayedTx := gfp.feeComputer.ComputeTxFeeBasedOnGasUsed(tx, gasUsedRelayedTx)

tx.GasUsed = gasUsed + gasUsedRelayedTx

fee.Add(fee, feeRelayedTx)
tx.Fee = fee.String()
} else {
gasUsed, fee := gfp.feeComputer.ComputeGasUsedAndFeeBasedOnRefundValue(tx, big.NewInt(0))
tx.GasUsed = gasUsed
tx.Fee = fee.String()
}
}
if core.SignalErrorOperation == event.Identifier {
fee := gfp.feeComputer.ComputeTxFeeBasedOnGasUsed(tx, tx.GasLimit)
Expand All @@ -166,14 +181,29 @@ func (gfp *gasUsedAndFeeProcessor) setGasUsedAndFeeBaseOnLogEvent(tx *transactio
}
}

func (gfp *gasUsedAndFeeProcessor) setGasUsedAndFeeBaseOnRefundValue(tx *transaction.ApiTransactionResult, refund *big.Int) {
func (gfp *gasUsedAndFeeProcessor) setGasUsedAndFeeBaseOnRefundValue(
tx *transaction.ApiTransactionResult,
userTx *transaction.ApiTransactionResult,
refund *big.Int,
) {
if !check.IfNilReflect(userTx) && gfp.enableEpochsHandler.IsFlagEnabledInEpoch(common.FixRelayedBaseCostFlag, tx.Epoch) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check.IfNil

gasUsed, fee := gfp.feeComputer.ComputeGasUsedAndFeeBasedOnRefundValue(userTx, refund)
gasUsedRelayedTx := gfp.feeComputer.ComputeGasLimit(tx)
feeRelayedTx := gfp.feeComputer.ComputeTxFeeBasedOnGasUsed(tx, gasUsedRelayedTx)

tx.GasUsed = gasUsed + gasUsedRelayedTx

fee.Add(fee, feeRelayedTx)
tx.Fee = fee.String()

return
}

gasUsed, fee := gfp.feeComputer.ComputeGasUsedAndFeeBasedOnRefundValue(tx, refund)

tx.GasUsed = gasUsed

tx.Fee = fee.String()

}

func (gfp *gasUsedAndFeeProcessor) isESDTOperationWithSCCall(tx *transaction.ApiTransactionResult) bool {
Expand Down
48 changes: 48 additions & 0 deletions node/external/transactionAPI/gasUsedAndFeeProcessor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,51 @@ func TestComputeAndAttachGasUsedAndFeeFailedRelayedV1(t *testing.T) {
require.Equal(t, "1198000000000000", txWithSRefundSCR.Fee)
require.Equal(t, "1274230000000000", txWithSRefundSCR.InitiallyPaidFee)
}

func TestComputeAndAttachGasUsedAndFeeRelayedV1CreateNewDelegationContractWithRefund(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/relayedV1CreateNewDelegationContract.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(56328500), txWithSRefundSCR.GasUsed)
require.Equal(t, "1878500000000000", txWithSRefundSCR.Fee)
require.Equal(t, "2177505000000000", txWithSRefundSCR.InitiallyPaidFee)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
{
"type": "normal",
"processingTypeOnSource": "RelayedTx",
"processingTypeOnDestination": "RelayedTx",
"hash": "94cb3bd3e2dca9920115f05549c9eee4dfc1d33e4ac3edd0741eb51165148b52",
"nonce": 0,
"round": 7,
"epoch": 1,
"value": "0",
"receiver": "erd1s89rm6mv6xyct38r3vqadj74rmqunamhwyz7c84a6u9thedj2wus5nlchg",
"sender": "erd1tp66n2lkhs2fm7elvh9lmzfajpg480v55sd8lf2lvu4fw92zsrasvn2wze",
"gasPrice": 1000000000,
"gasLimit": 86229000,
"data": "cmVsYXllZFR4QDdiMjI2ZTZmNmU2MzY1MjIzYTMwMmMyMjczNjU2ZTY0NjU3MjIyM2EyMjY3NjM2ZjM5MzYzMjdhNTI2OTU5NTg0NTM0MzQ3MzQyMzE3Mzc2NTY0ODczNDg0YTM5MzM2NDc4NDI2NTc3NjU3NjY0NjM0Yjc1MmI1Nzc5NTUzNzZiM2QyMjJjMjI3MjY1NjM2NTY5NzY2NTcyMjIzYTIyNDE0MTQxNDE0MTQxNDE0MTQxNDE0MTQxNDE1MTQxNDE0MTQxNDE0MTQxNDE0MTQxNDE0MTQxNDE0MTQxNDE0MTQxNDE0MTQxNDE0MTQxNDUyZjJmMzgzZDIyMmMyMjc2NjE2Yzc1NjUyMjNhMzIzNTMwMzEzMDMwMzAzMDMwMzAzMDMwMzAzMDMwMzAzMDMwMzAzMDMwMzAyYzIyNjc2MTczNTA3MjY5NjM2NTIyM2EzMTMwMzAzMDMwMzAzMDMwMzAzMDJjMjI2NzYxNzM0YzY5NmQ2OTc0MjIzYTM4MzUzMDMwMzAzMDMwMzAyYzIyNjQ2MTc0NjEyMjNhMjI1OTMzNGE2YzU5NTg1MjZjNTQ2ZDU2MzM1MjQ3NTY3MzVhNTc2NDY4NjQ0NzZjNzY2MjZiNGU3NjYyNmU1Mjc5NTk1NzRlMzA1MTQ0NDE3NzUxNDQ0MTc3MjIyYzIyNzM2OTY3NmU2MTc0NzU3MjY1MjIzYTIyNTk1MTUzNTQ0MjMwMzE0ZjUwNGY0NDU5MzM0ZDU2NDQ1NDRkNWE3NzRkNzY2NjZiNzI1MDUwMzk2ZTM1NjQ1MzU4NjI3MDQ5NWE2MjcwNDM1MTQ5MzMzNDRkNTY2NzZiNzYzMzc0Njk2MTRmNGMzNjQ0NWE2NjM4NTU2NTJmNjg2Zjc5MzkzNTRiNGI2NTVhNDI2YjcwNzAzMTU1NzY3NzU5MzY0NzU3NDI3NzNkM2QyMjJjMjI2MzY4NjE2OTZlNDk0NDIyM2EyMjU5MzI2ODY4NjE1NzM0M2QyMjJjMjI3NjY1NzI3MzY5NmY2ZTIyM2EzMjdk",
"signature": "bf5d14d237b951d23247e99113fa15566ebbd0dccd215b743ae9629f226ab3f9ba333622567c5606e2ed06104df52f81f05fad2488be9cb50a83e1356e0d070e",
"sourceShard": 1,
"destinationShard": 1,
"blockNonce": 7,
"blockHash": "5f2a597d07b4b5ae84195178eb6f83493bb1230c7f316b40a0d9b6efbe1a4da5",
"notarizedAtSourceInMetaNonce": 9,
"NotarizedAtSourceInMetaHash": "34cd7dc91fc9773ddd2e09d900087b96cfecf6280a6dc25a1894c2db161cded1",
"notarizedAtDestinationInMetaNonce": 9,
"notarizedAtDestinationInMetaHash": "34cd7dc91fc9773ddd2e09d900087b96cfecf6280a6dc25a1894c2db161cded1",
"miniblockType": "TxBlock",
"miniblockHash": "03ef037eb1fd03f89a9b5ece12aa422223b440cb1fd02c4a6b82d65268121d28",
"hyperblockNonce": 9,
"hyperblockHash": "34cd7dc91fc9773ddd2e09d900087b96cfecf6280a6dc25a1894c2db161cded1",
"timestamp": 1729691671,
"smartContractResults": [
{
"hash": "6e71137c75ad162d97ce45502d38f0af96362c06f03378f03a4e50bec6ce31ea",
"nonce": 1,
"value": 299005000000000,
"receiver": "erd1tp66n2lkhs2fm7elvh9lmzfajpg480v55sd8lf2lvu4fw92zsrasvn2wze",
"sender": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqylllslmq6y6",
"prevTxHash": "c825db2f9e6d17e41ed12e1aac86eecd7a828eeea0caf63a6e6a979f02a74f70",
"originalTxHash": "94cb3bd3e2dca9920115f05549c9eee4dfc1d33e4ac3edd0741eb51165148b52",
"gasLimit": 0,
"gasPrice": 1000000000,
"callType": 0,
"returnMessage": "gas refund for relayer",
"logs": {
"address": "erd1tp66n2lkhs2fm7elvh9lmzfajpg480v55sd8lf2lvu4fw92zsrasvn2wze",
"events": [
{
"address": "erd1tp66n2lkhs2fm7elvh9lmzfajpg480v55sd8lf2lvu4fw92zsrasvn2wze",
"identifier": "completedTxEvent",
"topics": [
"yCXbL55tF+Qe0S4arIbuzXqCju6gyvY6bmqXnwKnT3A="
],
"data": null,
"additionalData": null
}
]
},
"operation": "transfer",
"isRefund": true
},
{
"hash": "c825db2f9e6d17e41ed12e1aac86eecd7a828eeea0caf63a6e6a979f02a74f70",
"nonce": 0,
"value": 2501000000000000000000,
"receiver": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqylllslmq6y6",
"sender": "erd1s89rm6mv6xyct38r3vqadj74rmqunamhwyz7c84a6u9thedj2wus5nlchg",
"relayerAddress": "erd1tp66n2lkhs2fm7elvh9lmzfajpg480v55sd8lf2lvu4fw92zsrasvn2wze",
"relayedValue": 0,
"data": "createNewDelegationContract@00@00",
"prevTxHash": "94cb3bd3e2dca9920115f05549c9eee4dfc1d33e4ac3edd0741eb51165148b52",
"originalTxHash": "94cb3bd3e2dca9920115f05549c9eee4dfc1d33e4ac3edd0741eb51165148b52",
"gasLimit": 84900500,
"gasPrice": 1000000000,
"callType": 0,
"logs": {
"address": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqylllslmq6y6",
"events": [
{
"address": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqylllslmq6y6",
"identifier": "transferValueOnly",
"topics": [
"h5RY6SJT9AAA",
"AAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAL///8="
],
"data": "RGVwbG95U21hcnRDb250cmFjdA==",
"additionalData": [
"RGVwbG95U21hcnRDb250cmFjdA==",
"X2luaXQ=",
"AA==",
"AA=="
]
},
{
"address": "erd1s89rm6mv6xyct38r3vqadj74rmqunamhwyz7c84a6u9thedj2wus5nlchg",
"identifier": "delegate",
"topics": [
"h5RY6SJT9AAA",
"h5RY6SJT9AAA",
"AQ==",
"h5RY6SJT9AAA",
"AAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAL///8="
],
"data": null,
"additionalData": null
},
{
"address": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqhllllsajxzat",
"identifier": "transferValueOnly",
"topics": [
"h5RY6SJT9AAA",
"AAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAB//8="
],
"data": "RXhlY3V0ZU9uRGVzdENvbnRleHQ=",
"additionalData": [
"RXhlY3V0ZU9uRGVzdENvbnRleHQ=",
"c3Rha2U="
]
},
{
"address": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqhllllsajxzat",
"identifier": "SCDeploy",
"topics": [
"AAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAL///8=",
"gco962zRiYXE44sB1svVHsHJ93dxBewevdcKu+WyU7k=",
"/uYNe6O98aIOSpF57HocNxS4JQ7FILx6+N7MEN3oAQY="
],
"data": null,
"additionalData": null
},
{
"address": "erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqylllslmq6y6",
"identifier": "writeLog",
"topics": [
"gco962zRiYXE44sB1svVHsHJ93dxBewevdcKu+WyU7k="
],
"data": "QDZmNmJAMDAwMDAwMDAwMDAwMDAwMDAwMDEwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMmZmZmZmZg==",
"additionalData": [
"QDZmNmJAMDAwMDAwMDAwMDAwMDAwMDAwMDEwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMmZmZmZmZg=="
]
}
]
},
"operation": "transfer",
"function": "createNewDelegationContract"
}
],
"status": "success",
"receivers": [
"erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqylllslmq6y6"
],
"receiversShardIDs": [
4294967295
],
"operation": "transfer",
"function": "createNewDelegationContract",
"isRelayed": true,
"chainID": "chain",
"version": 2,
"options": 0
}
Loading
Loading