-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(evm): proper nonce management in statedb (#2130)
* fix(evm): proper nonce management in statedb * chore: changelog update * test: fixed statedb journal test * fix: text comment --------- Co-authored-by: Unique Divine <[email protected]>
- Loading branch information
1 parent
20531e7
commit 2c4c224
Showing
6 changed files
with
167 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package backend_test | ||
|
||
import ( | ||
sdkmath "cosmossdk.io/math" | ||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" | ||
gethcore "github.com/ethereum/go-ethereum/core/types" | ||
|
||
"github.com/NibiruChain/nibiru/v2/x/evm" | ||
) | ||
|
||
// TestNonceIncrementWithMultipleMsgsTx tests that the nonce is incremented correctly | ||
// when multiple messages are included in a single transaction. | ||
func (s *BackendSuite) TestNonceIncrementWithMultipleMsgsTx() { | ||
nonce := s.getCurrentNonce(s.fundedAccEthAddr) | ||
|
||
// Create series of 3 tx messages. Expecting nonce to be incremented by 3 | ||
creationTx := s.buildContractCreationTx(nonce) | ||
firstTransferTx := s.buildContractCallTx(nonce+1, testContractAddress) | ||
secondTransferTx := s.buildContractCallTx(nonce+2, testContractAddress) | ||
|
||
// Create and broadcast SDK transaction | ||
sdkTx := s.buildSDKTxWithEVMMessages( | ||
creationTx, | ||
firstTransferTx, | ||
secondTransferTx, | ||
) | ||
|
||
// Broadcast transaction | ||
rsp := s.broadcastSDKTx(sdkTx) | ||
s.Assert().NotEqual(rsp.Code, 0) | ||
s.Require().NoError(s.network.WaitForNextBlock()) | ||
|
||
// Expected nonce should be incremented by 3 | ||
currentNonce := s.getCurrentNonce(s.fundedAccEthAddr) | ||
s.Assert().Equal(nonce+3, currentNonce) | ||
|
||
// Assert all transactions included in block | ||
for _, tx := range []gethcore.Transaction{creationTx, firstTransferTx, secondTransferTx} { | ||
blockNum, blockHash := WaitForReceipt(s, tx.Hash()) | ||
s.Require().NotNil(blockNum) | ||
s.Require().NotNil(blockHash) | ||
} | ||
} | ||
|
||
// buildSDKTxWithEVMMessages creates an SDK transaction with EVM messages | ||
func (s *BackendSuite) buildSDKTxWithEVMMessages(txs ...gethcore.Transaction) sdk.Tx { | ||
msgs := make([]sdk.Msg, len(txs)) | ||
for i, tx := range txs { | ||
msg := &evm.MsgEthereumTx{} | ||
err := msg.FromEthereumTx(&tx) | ||
s.Require().NoError(err) | ||
msgs[i] = msg | ||
} | ||
|
||
option, err := codectypes.NewAnyWithValue(&evm.ExtensionOptionsEthereumTx{}) | ||
s.Require().NoError(err) | ||
|
||
txBuilder, _ := s.backend.ClientCtx().TxConfig.NewTxBuilder().(authtx.ExtensionOptionsTxBuilder) | ||
txBuilder.SetExtensionOptions(option) | ||
err = txBuilder.SetMsgs(msgs...) | ||
s.Require().NoError(err) | ||
|
||
// Set fees for all messages | ||
totalGas := uint64(0) | ||
for _, tx := range txs { | ||
totalGas += tx.Gas() | ||
} | ||
fees := sdk.NewCoins(sdk.NewCoin("unibi", sdkmath.NewIntFromUint64(totalGas))) | ||
txBuilder.SetFeeAmount(fees) | ||
txBuilder.SetGasLimit(totalGas) | ||
|
||
return txBuilder.GetTx() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters