diff --git a/eth/rpc/backend/backend_suite_test.go b/eth/rpc/backend/backend_suite_test.go index be973330c..3cd7618b6 100644 --- a/eth/rpc/backend/backend_suite_test.go +++ b/eth/rpc/backend/backend_suite_test.go @@ -173,7 +173,7 @@ func SendTransaction(s *BackendSuite, tx *gethcore.LegacyTx, waitForNextBlock bo // WaitForReceipt waits for a transaction to be included in a block, returns block number, block hash and receipt func WaitForReceipt(s *BackendSuite, txHash gethcommon.Hash) (*big.Int, *gethcommon.Hash, *backend.TransactionReceipt) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) defer cancel() for { diff --git a/eth/rpc/backend/gas_used_test.go b/eth/rpc/backend/gas_used_test.go index 2a655f771..116ea6249 100644 --- a/eth/rpc/backend/gas_used_test.go +++ b/eth/rpc/backend/gas_used_test.go @@ -154,10 +154,6 @@ func (s *BackendSuite) TestGasUsedFunTokens() { s.Require().NotNil(blockNumber2) s.Require().NotNil(blockNumber3) - // TXs should have been included in the same block - s.Require().Equal(blockNumber1, blockNumber2) - s.Require().Equal(blockNumber2, blockNumber3) - // 1 and 3 should pass and 2 should fail s.Require().Equal(gethcore.ReceiptStatusSuccessful, receipt1.Status) s.Require().Equal(gethcore.ReceiptStatusFailed, receipt2.Status) diff --git a/x/evm/keeper/funtoken_from_coin.go b/x/evm/keeper/funtoken_from_coin.go index 6fe8d17fa..b62523d48 100644 --- a/x/evm/keeper/funtoken_from_coin.go +++ b/x/evm/keeper/funtoken_from_coin.go @@ -99,7 +99,6 @@ func (k *Keeper) deployERC20ForBankCoin( stateDB = k.NewStateDB(ctx, txConfig) } evmObj := k.NewEVM(ctx, evmMsg, evmCfg, nil /*tracer*/, stateDB) - evmResp, err := k.CallContractWithInput( ctx, evmObj, evm.EVM_MODULE_ADDRESS, nil, true /*commit*/, input, Erc20GasLimitDeploy, ) @@ -107,6 +106,14 @@ func (k *Keeper) deployERC20ForBankCoin( k.ResetGasMeterAndConsumeGas(ctx, ctx.GasMeter().Limit()) return gethcommon.Address{}, errors.Wrap(err, "failed to deploy ERC20 contract") } + + err = stateDB.Commit() + if err != nil { + return gethcommon.Address{}, errors.Wrap(err, "failed to commit stateDB") + } + // Don't need the StateDB anymore because it's not usable after committing + k.Bank.StateDB = nil + blockGasUsed, errBlockGasUsed := k.AddToBlockGasUsed(ctx, evmResp.GasUsed) if errBlockGasUsed != nil { return gethcommon.Address{}, errors.Wrap(errBlockGasUsed, "error adding transient gas used") diff --git a/x/evm/keeper/funtoken_from_erc20.go b/x/evm/keeper/funtoken_from_erc20.go index 387aa7f0b..b393811f9 100644 --- a/x/evm/keeper/funtoken_from_erc20.go +++ b/x/evm/keeper/funtoken_from_erc20.go @@ -5,6 +5,7 @@ import ( "fmt" "math/big" + "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" bank "github.com/cosmos/cosmos-sdk/x/bank/types" gethcommon "github.com/ethereum/go-ethereum/common" @@ -114,7 +115,7 @@ func (k *Keeper) createFunTokenFromERC20( ) (funtoken *evm.FunToken, err error) { // 1 | ERC20 already registered with FunToken? if funtokens := k.FunTokens.Collect(ctx, k.FunTokens.Indexes.ERC20Addr.ExactMatch(ctx, erc20)); len(funtokens) > 0 { - return funtoken, fmt.Errorf("funtoken mapping already created for ERC20 \"%s\"", erc20) + return nil, fmt.Errorf("funtoken mapping already created for ERC20 \"%s\"", erc20) } // 2 | Get existing ERC20 metadata @@ -139,7 +140,7 @@ func (k *Keeper) createFunTokenFromERC20( evmObj := k.NewEVM(ctx, evmMsg, k.GetEVMConfig(ctx), evm.NewNoOpTracer(), stateDB) erc20Info, err := k.FindERC20Metadata(ctx, evmObj, erc20) if err != nil { - return funtoken, err + return nil, err } bankDenom := fmt.Sprintf("erc20/%s", erc20.String()) @@ -147,10 +148,10 @@ func (k *Keeper) createFunTokenFromERC20( // 3 | Coin already registered with FunToken? _, isFound := k.Bank.GetDenomMetaData(ctx, bankDenom) if isFound { - return funtoken, fmt.Errorf("bank coin denom already registered with denom \"%s\"", bankDenom) + return nil, fmt.Errorf("bank coin denom already registered with denom \"%s\"", bankDenom) } if funtokens := k.FunTokens.Collect(ctx, k.FunTokens.Indexes.BankDenom.ExactMatch(ctx, bankDenom)); len(funtokens) > 0 { - return funtoken, fmt.Errorf("funtoken mapping already created for bank denom \"%s\"", bankDenom) + return nil, fmt.Errorf("funtoken mapping already created for bank denom \"%s\"", bankDenom) } // 4 | Set bank coin denom metadata in state @@ -158,7 +159,7 @@ func (k *Keeper) createFunTokenFromERC20( err = bankMetadata.Validate() if err != nil { - return funtoken, fmt.Errorf("failed to validate bank metadata: %w", err) + return nil, fmt.Errorf("failed to validate bank metadata: %w", err) } k.Bank.SetDenomMetaData(ctx, bankMetadata) @@ -171,6 +172,13 @@ func (k *Keeper) createFunTokenFromERC20( IsMadeFromCoin: false, } + err = stateDB.Commit() + if err != nil { + return nil, errors.Wrap(err, "failed to commit stateDB") + } + // Don't need the StateDB anymore because it's not usable after committing + k.Bank.StateDB = nil + return funtoken, k.FunTokens.SafeInsert( ctx, erc20, bankDenom, false, )