-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proper fix for gasUsed and fee in case of relayedV3 #6440
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,16 +49,16 @@ func (gfp *gasUsedAndFeeProcessor) computeAndAttachGasUsedAndFee(tx *transaction | |
tx.Fee = tx.InitiallyPaidFee | ||
} | ||
|
||
if tx.IsRelayed && isFeeFixActive { | ||
totalFee, isRelayed := gfp.getFeeOfRelayed(tx) | ||
if isRelayed { | ||
tx.Fee = totalFee.String() | ||
tx.InitiallyPaidFee = totalFee.String() | ||
tx.GasUsed = big.NewInt(0).Div(totalFee, big.NewInt(0).SetUint64(tx.GasPrice)).Uint64() | ||
} | ||
initialTotalFee, isRelayed := gfp.getFeeOfRelayed(tx) | ||
if isRelayed && isFeeFixActive { | ||
tx.InitiallyPaidFee = initialTotalFee.String() | ||
tx.Fee = initialTotalFee.String() | ||
tx.GasUsed = big.NewInt(0).Div(initialTotalFee, big.NewInt(0).SetUint64(tx.GasPrice)).Uint64() | ||
} | ||
|
||
hasRefundForSender := false | ||
totalRefunds := big.NewInt(0) | ||
isRelayedV3 := len(tx.InnerTransactions) > 0 | ||
for _, scr := range tx.SmartContractResults { | ||
if !scr.IsRefund || scr.RcvAddr != tx.Sender { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think line 67 is dead code right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true, removed it |
||
continue | ||
|
@@ -69,7 +69,17 @@ func (gfp *gasUsedAndFeeProcessor) computeAndAttachGasUsedAndFee(tx *transaction | |
|
||
gfp.setGasUsedAndFeeBaseOnRefundValue(tx, scr.Value) | ||
hasRefundForSender = true | ||
break | ||
totalRefunds.Add(totalRefunds, scr.Value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't this affect regular transactions with multiple refund scrs from other shards as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see.. it means this was a bug also for that transactions.. updated to iterate through all of them |
||
if !isRelayedV3 { | ||
break | ||
} | ||
} | ||
|
||
if isRelayedV3 { | ||
gasUsed, fee = gfp.feeComputer.ComputeGasUsedAndFeeBasedOnRefundValue(tx, totalRefunds) | ||
tx.GasUsed = gasUsed | ||
tx.Fee = fee.String() | ||
return | ||
} | ||
|
||
gfp.prepareTxWithResultsBasedOnLogs(tx, hasRefundForSender) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,6 +155,8 @@ func (tep *transactionsFeeProcessor) prepareNormalTxs(transactionsAndScrs *trans | |
|
||
func (tep *transactionsFeeProcessor) prepareTxWithResults(txHashHex string, txWithResults *transactionWithResults) { | ||
hasRefund := false | ||
totalRefunds := big.NewInt(0) | ||
isRelayedV3 := len(txWithResults.GetTxHandler().GetUserTransactions()) > 0 | ||
for _, scrHandler := range txWithResults.scrs { | ||
scr, ok := scrHandler.GetTxHandler().(*smartContractResult.SmartContractResult) | ||
if !ok { | ||
|
@@ -167,12 +169,21 @@ func (tep *transactionsFeeProcessor) prepareTxWithResults(txHashHex string, txWi | |
txWithResults.GetFeeInfo().SetGasUsed(gasUsed) | ||
txWithResults.GetFeeInfo().SetFee(fee) | ||
hasRefund = true | ||
break | ||
totalRefunds.Add(totalRefunds, scr.Value) | ||
if !isRelayedV3 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will affect other transactions with multiple scrs in different shards as well, so I think it should iterate over all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see.. it means this was a bug also for that transactions.. updated to iterate through all of them |
||
break | ||
} | ||
} | ||
} | ||
|
||
tep.prepareTxWithResultsBasedOnLogs(txHashHex, txWithResults, hasRefund) | ||
if isRelayedV3 { | ||
gasUsed, fee := tep.txFeeCalculator.ComputeGasUsedAndFeeBasedOnRefundValue(txWithResults.GetTxHandler(), totalRefunds) | ||
txWithResults.GetFeeInfo().SetGasUsed(gasUsed) | ||
txWithResults.GetFeeInfo().SetFee(fee) | ||
return | ||
} | ||
|
||
tep.prepareTxWithResultsBasedOnLogs(txHashHex, txWithResults, hasRefund) | ||
} | ||
|
||
func (tep *transactionsFeeProcessor) getFeeOfRelayed(tx *transactionWithResults) (*big.Int, bool) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -591,8 +591,15 @@ func (ed *economicsData) ComputeGasUsedAndFeeBasedOnRefundValueInEpoch(tx data.T | |
txFee := ed.ComputeTxFeeInEpoch(tx, epoch) | ||
|
||
if len(tx.GetUserTransactions()) > 0 { | ||
gasUnitsUsed := big.NewInt(0).Div(txFee, big.NewInt(0).SetUint64(tx.GetGasPrice())) | ||
return gasUnitsUsed.Uint64(), txFee | ||
txFeeAfterRefund := txFee.Sub(txFee, refundValue) | ||
|
||
gasPriceForProcessing := ed.GasPriceForProcessingInEpoch(tx, epoch) | ||
gasUnitsRefunded := refundValue.Uint64() / gasPriceForProcessing | ||
|
||
gasUnitsConsideredForInitialFee := ed.computeRelayedTxV3MinGasLimit(tx) | ||
gasUnitsUsed := gasUnitsConsideredForInitialFee - gasUnitsRefunded | ||
|
||
return gasUnitsUsed, txFeeAfterRefund | ||
} | ||
|
||
isPenalizedTooMuchGasFlagEnabled := ed.enableEpochsHandler.IsFlagEnabledInEpoch(common.PenalizedTooMuchGasFlag, epoch) | ||
|
@@ -682,6 +689,20 @@ func (ed *economicsData) ComputeGasLimitBasedOnBalanceInEpoch(tx data.Transactio | |
return totalGasLimit, nil | ||
} | ||
|
||
func (ed *economicsData) computeRelayedTxV3MinGasLimit(tx data.TransactionWithFeeHandler) uint64 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-trivial function, should also be unit-tested (separately from "ComputeGasUsedAndFeeBasedOnRefundValueInEpoch"). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right, will add tests in a separate PR |
||
relayedTxGasLimit := ed.ComputeGasLimit(tx) | ||
relayedTxMinGasLimit := ed.MinGasLimit() | ||
relayedTxGasLimitDiff := relayedTxGasLimit - relayedTxMinGasLimit // this may be positive if the relayed tx is guarded | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the case mentioned in comment tested using "TestEconomicsData_ComputeGasUsedAndFeeBasedOnRefundValueRelayedV3"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed not tested here, covered by testing scenarios 100% will add tests in a separate PR |
||
|
||
innerTxs := tx.GetUserTransactions() | ||
totalGasLimit := relayedTxGasLimitDiff + relayedTxMinGasLimit*uint64(len(innerTxs)) | ||
for _, innerTx := range innerTxs { | ||
totalGasLimit += innerTx.GetGasLimit() | ||
} | ||
|
||
return totalGasLimit | ||
} | ||
|
||
// IsInterfaceNil returns true if there is no value under the interface | ||
func (ed *economicsData) IsInterfaceNil() bool { | ||
return ed == nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is basically the same as before right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exactly, only removed one if