Skip to content

Commit

Permalink
revert statedb changes
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Jan 7, 2025
1 parent 5f4e528 commit a6a6400
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 32 deletions.
20 changes: 16 additions & 4 deletions x/evm/keeper/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,23 @@ func (k *Keeper) SetBalance(ctx sdk.Context, addr common.Address, amount *big.In
delta := new(big.Int).Sub(amount, balance)
switch delta.Sign() {
case 1:
coins := sdk.NewCoins(sdk.NewCoin(evmDenom, sdkmath.NewIntFromBigInt(delta)))
return k.AddBalance(ctx, cosmosAddr, coins)
// mint
coins := sdk.NewCoins(sdk.NewCoin(params.EvmDenom, sdkmath.NewIntFromBigInt(delta)))
if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, coins); err != nil {
return err
}
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, cosmosAddr, coins); err != nil {
return err
}
case -1:
coins := sdk.NewCoins(sdk.NewCoin(evmDenom, sdkmath.NewIntFromBigInt(new(big.Int).Abs(delta))))
return k.SubBalance(ctx, cosmosAddr, coins)
// burn
coins := sdk.NewCoins(sdk.NewCoin(params.EvmDenom, sdkmath.NewIntFromBigInt(new(big.Int).Neg(delta))))
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, cosmosAddr, types.ModuleName, coins); err != nil {
return err
}
if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, coins); err != nil {
return err
}
default:
// not changed
}
Expand Down
36 changes: 8 additions & 28 deletions x/evm/statedb/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ package statedb

import (
"fmt"
"math/big"
"sort"

errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
storetypes "cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -375,36 +373,18 @@ func (s *StateDB) CacheContext() sdk.Context {
*/

// AddBalance adds amount to the account associated with addr.
func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) {
if amount.Sign() <= 0 {
return
}
coins := sdk.Coins{sdk.NewCoin(s.evmDenom, sdkmath.NewIntFromBigInt(amount))}
if err := s.ExecuteNativeAction(common.Address{}, nil, func(ctx sdk.Context) error {
return s.keeper.AddBalance(ctx, sdk.AccAddress(addr.Bytes()), coins)
}); err != nil {
s.err = err
func (s *StateDB) AddBalance(addr common.Address, amount *uint256.Int) {
stateObject := s.getOrNewStateObject(addr)
if stateObject != nil {
stateObject.AddBalance(amount)
}
}

// SubBalance subtracts amount from the account associated with addr.
func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) {
if amount.Sign() <= 0 {
return
}
coins := sdk.Coins{sdk.NewCoin(s.evmDenom, sdkmath.NewIntFromBigInt(amount))}
if err := s.ExecuteNativeAction(common.Address{}, nil, func(ctx sdk.Context) error {
return s.keeper.SubBalance(ctx, sdk.AccAddress(addr.Bytes()), coins)
}); err != nil {
s.err = err
}
}

func (s *StateDB) SetBalance(addr common.Address, amount *big.Int) {
if err := s.ExecuteNativeAction(common.Address{}, nil, func(ctx sdk.Context) error {
return s.keeper.SetBalance(ctx, addr, amount, s.evmDenom)
}); err != nil {
s.err = err
func (s *StateDB) SubBalance(addr common.Address, amount *uint256.Int) {
stateObject := s.getOrNewStateObject(addr)
if stateObject != nil {
stateObject.SubBalance(amount)
}
}

Expand Down

0 comments on commit a6a6400

Please sign in to comment.