Skip to content

Commit

Permalink
fix: clear StateDB between txs
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Jan 16, 2025
1 parent e94abc7 commit e95f809
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion eth/rpc/backend/backend_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions eth/rpc/backend/gas_used_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion x/evm/keeper/funtoken_from_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,21 @@ 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,
)
if err != nil {
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")
Expand Down
18 changes: 13 additions & 5 deletions x/evm/keeper/funtoken_from_erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -139,26 +140,26 @@ 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())

// 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
bankMetadata := erc20Info.ToBankMetadata(bankDenom, erc20)

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)

Expand All @@ -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,
)
Expand Down

0 comments on commit e95f809

Please sign in to comment.