Skip to content
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

Fix how gasUsed is calculated on move balance transaction #2349

Merged

Conversation

miiu96
Copy link
Contributor

@miiu96 miiu96 commented Oct 8, 2020

Fix how gas used is calculated for move balance transactions.
Now gasUsed for a move balance transaction is calculated with formula minGasLimit + len(data)*gasPerDataByte

Testing procedure:

  1. run a testnet with this branch and make sure that external.toml has ElasticSearchConnector enabled

  2. in epoch 0:generate an invalid transaction with gasLimit= 500 000 (to generate an invalid transaction value from transaction
    should be greater than sender account balance.

  3. check in elasticsearch database that gasUsed of generated transaction is equals with 50 000

  4. wait 2 epochs to pass (when you move to next step epoch number should be greater of equal with 2)

  5. generate again an invalid transaction with gasLimit= 500 000

  6. check in elasticsearch database that gasUsed of generated transaction is equals with 500 000

@miiu96 miiu96 self-assigned this Oct 8, 2020
Copy link
Collaborator

@andreibancioiu andreibancioiu left a comment

Choose a reason for hiding this comment

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

Add a description in the PR on how to test this (e.g. send both valid and invalid transactions, look in Explorer etc.).

@@ -98,6 +100,8 @@ func (cm *commonProcessor) buildTransaction(
header data.HeaderHandler,
txStatus string,
) *Transaction {
gasUsed := cm.minGasLimit + uint64(len(tx.Data))*cm.gasPerDataByte
Copy link
Collaborator

Choose a reason for hiding this comment

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

For invalid transactions, after the Mainnet upgrade, this will not be correct anymore. Perhaps use the soft fork swtich here as well (PenalizedTooMuchGasEnableEpoch - @sasurobert, @SebastianMarian is this the one?).

After the Mainnet upgrade, for invalid transactions, gasUsed := gasLimit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for invalid transactions gasUsed will be deduced from the receipt.

@@ -57,6 +59,10 @@ func createMockElasticProcessorArgs() ArgElasticProcessor {
blockIndex: {}, txIndex: {}, miniblocksIndex: {}, tpsIndex: {}, validatorsIndex: {}, roundIndex: {}, accountsIndex: {}, ratingIndex: {}, accountsHistoryIndex: {},
},
AccountsDB: &mock.AccountsStub{},
FeeConfig: &config.FeeSettings{
MinGasLimit: "10",
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can use "real" values in tests as well (optional).

vmcommon "github.com/ElrondNetwork/elrond-vm-common"
"github.com/stretchr/testify/require"
)

func createCommonProcessor() commonProcessor {
func createCommonProcessor(minGasLimit, gasPerDataByte uint64) commonProcessor {
Copy link
Contributor

Choose a reason for hiding this comment

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

minGasLimit uint64, gasPerDataByte uint64 keep our standard

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

@@ -140,6 +136,22 @@ func (tdp *txDatabaseProcessor) prepareTransactionsForDatabase(
return append(convertMapTxsToSlice(transactions), rewardsTxs...), alteredAddresses
}

func getGasUsedFromReceipt(rec *receipt.Receipt, tx *Transaction) uint64 {
if rec.Data != nil && string(rec.Data) == transactionProcess.RefundGasMessage {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need rec.Data != nil here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer to have this check just to be sure the cast to string never panics.

@@ -19,6 +19,7 @@ import (
"github.com/ElrondNetwork/elrond-go/hashing"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/ElrondNetwork/elrond-go/process"
transactionProcess "github.com/ElrondNetwork/elrond-go/process/transaction"
Copy link
Collaborator

Choose a reason for hiding this comment

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

processTransaction?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

return gasUsed.Uint64()
}

gasUsed := big.NewInt(0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe add a comment, that in this case the receipt contains the cost (the fee).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

@@ -13,6 +13,7 @@ import (
"github.com/ElrondNetwork/elrond-go/data/rewardTx"
"github.com/ElrondNetwork/elrond-go/data/smartContractResult"
"github.com/ElrondNetwork/elrond-go/data/transaction"
transactionProcess "github.com/ElrondNetwork/elrond-go/process/transaction"
Copy link
Collaborator

Choose a reason for hiding this comment

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

processTransaction.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

GasLimit: gasLimit,
}

expectedGasUsed := big.NewInt(0).SetUint64(gasPrice)
Copy link
Collaborator

Choose a reason for hiding this comment

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

expectedGasUsed as a constant / a big int initialized with the expected value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

GasLimit: gasLimit,
}

expectedGasUsed := big.NewInt(0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

expectedGasUsed as a constant / a big int initialized with the expected value - e.g. from string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

@@ -24,6 +24,10 @@ import (
var log = logger.GetOrCreate("process/transaction")
var _ process.TransactionProcessor = (*txProcessor)(nil)

// RefundGasMessage is message that is returned in data field of created receipt
Copy link
Collaborator

Choose a reason for hiding this comment

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

// RefundGasMessage is the message returned in the data field of a receipt,
// for move balance transactions that provide more gas than needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

bogdan-rosianu
bogdan-rosianu previously approved these changes Oct 8, 2020
Base automatically changed from es-imp-v2 to feat/elastic-search-new-impl October 8, 2020 14:05
@miiu96 miiu96 dismissed bogdan-rosianu’s stale review October 8, 2020 14:05

The base branch was changed.

@miiu96 miiu96 merged commit acee62d into feat/elastic-search-new-impl Oct 12, 2020
@miiu96 miiu96 deleted the EN-7886-fix-how-gas-used-is-calculated branch October 12, 2020 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants