2020-05-18 19:21:12 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
2021-05-25 12:56:36 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2020-05-18 19:21:12 +00:00
|
|
|
"math/big"
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
2020-07-02 15:19:48 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-01-05 14:08:31 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2022-01-05 07:28:27 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2022-06-19 09:43:41 +00:00
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
|
|
|
"github.com/evmos/ethermint/x/evm/statedb"
|
|
|
|
"github.com/evmos/ethermint/x/evm/types"
|
2020-05-18 19:21:12 +00:00
|
|
|
)
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
var _ statedb.Keeper = &Keeper{}
|
2021-05-25 12:56:36 +00:00
|
|
|
|
2020-05-18 19:21:12 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2022-01-05 18:18:02 +00:00
|
|
|
// StateDB Keeper implementation
|
2020-05-18 19:21:12 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2022-01-05 18:18:02 +00:00
|
|
|
// GetAccount returns nil if account is not exist, returns error if it's not `EthAccountI`
|
|
|
|
func (k *Keeper) GetAccount(ctx sdk.Context, addr common.Address) *statedb.Account {
|
|
|
|
acct := k.GetAccountWithoutBalance(ctx, addr)
|
|
|
|
if acct == nil {
|
|
|
|
return nil
|
2021-05-25 12:56:36 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
acct.Balance = k.GetBalance(ctx, addr)
|
2022-01-05 18:18:02 +00:00
|
|
|
return acct
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// GetState loads contract state from database, implements `statedb.Keeper` interface.
|
|
|
|
func (k *Keeper) GetState(ctx sdk.Context, addr common.Address, key common.Hash) common.Hash {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.AddressStoragePrefix(addr))
|
2021-05-25 12:56:36 +00:00
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
value := store.Get(key.Bytes())
|
|
|
|
if len(value) == 0 {
|
|
|
|
return common.Hash{}
|
2021-06-29 17:02:21 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
return common.BytesToHash(value)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// GetCode loads contract code from database, implements `statedb.Keeper` interface.
|
|
|
|
func (k *Keeper) GetCode(ctx sdk.Context, codeHash common.Hash) []byte {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixCode)
|
|
|
|
return store.Get(codeHash.Bytes())
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// ForEachStorage iterate contract storage, callback return false to break early
|
|
|
|
func (k *Keeper) ForEachStorage(ctx sdk.Context, addr common.Address, cb func(key, value common.Hash) bool) {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
prefix := types.AddressStoragePrefix(addr)
|
2021-05-25 12:56:36 +00:00
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
iterator := sdk.KVStorePrefixIterator(store, prefix)
|
|
|
|
defer iterator.Close()
|
2021-09-17 06:22:52 +00:00
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
|
|
key := common.BytesToHash(iterator.Key())
|
|
|
|
value := common.BytesToHash(iterator.Value())
|
2021-08-31 12:50:31 +00:00
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// check if iteration stops
|
|
|
|
if !cb(key, value) {
|
|
|
|
return
|
|
|
|
}
|
2021-05-25 12:56:36 +00:00
|
|
|
}
|
2020-06-04 10:40:21 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// SetBalance update account's balance, compare with current balance first, then decide to mint or burn.
|
|
|
|
func (k *Keeper) SetBalance(ctx sdk.Context, addr common.Address, amount *big.Int) error {
|
2021-05-25 12:56:36 +00:00
|
|
|
cosmosAddr := sdk.AccAddress(addr.Bytes())
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
params := k.GetParams(ctx)
|
|
|
|
coin := k.bankKeeper.GetBalance(ctx, cosmosAddr, params.EvmDenom)
|
|
|
|
balance := coin.Amount.BigInt()
|
|
|
|
delta := new(big.Int).Sub(amount, balance)
|
|
|
|
switch delta.Sign() {
|
|
|
|
case 1:
|
|
|
|
// mint
|
|
|
|
coins := sdk.NewCoins(sdk.NewCoin(params.EvmDenom, sdk.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:
|
|
|
|
// burn
|
|
|
|
coins := sdk.NewCoins(sdk.NewCoin(params.EvmDenom, sdk.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
|
2021-05-25 12:56:36 +00:00
|
|
|
}
|
2022-01-05 07:28:27 +00:00
|
|
|
return nil
|
2020-08-23 21:41:54 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// SetAccount updates nonce/balance/codeHash together.
|
|
|
|
func (k *Keeper) SetAccount(ctx sdk.Context, addr common.Address, account statedb.Account) error {
|
|
|
|
// update account
|
2021-05-25 12:56:36 +00:00
|
|
|
cosmosAddr := sdk.AccAddress(addr.Bytes())
|
2022-01-05 07:28:27 +00:00
|
|
|
acct := k.accountKeeper.GetAccount(ctx, cosmosAddr)
|
|
|
|
if acct == nil {
|
|
|
|
acct = k.accountKeeper.NewAccountWithAddress(ctx, cosmosAddr)
|
2021-09-17 06:22:52 +00:00
|
|
|
}
|
2022-01-05 18:18:02 +00:00
|
|
|
|
|
|
|
if err := acct.SetSequence(account.Nonce); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
codeHash := common.BytesToHash(account.CodeHash)
|
|
|
|
|
|
|
|
if ethAcct, ok := acct.(ethermint.EthAccountI); ok {
|
|
|
|
if err := ethAcct.SetCodeHash(codeHash); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-25 12:56:36 +00:00
|
|
|
}
|
2022-01-05 18:18:02 +00:00
|
|
|
|
|
|
|
k.accountKeeper.SetAccount(ctx, acct)
|
|
|
|
|
|
|
|
if err := k.SetBalance(ctx, addr, account.Balance); err != nil {
|
2022-01-05 07:28:27 +00:00
|
|
|
return err
|
2021-05-25 12:56:36 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug(
|
2022-01-05 07:28:27 +00:00
|
|
|
"account updated",
|
2021-05-25 12:56:36 +00:00
|
|
|
"ethereum-address", addr.Hex(),
|
2022-01-05 07:28:27 +00:00
|
|
|
"nonce", account.Nonce,
|
2022-01-05 18:18:02 +00:00
|
|
|
"codeHash", codeHash.Hex(),
|
2022-01-05 07:28:27 +00:00
|
|
|
"balance", account.Balance,
|
2021-05-25 12:56:36 +00:00
|
|
|
)
|
2022-01-05 18:18:02 +00:00
|
|
|
return nil
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// SetState update contract storage, delete if value is empty.
|
|
|
|
func (k *Keeper) SetState(ctx sdk.Context, addr common.Address, key common.Hash, value []byte) {
|
2021-08-31 12:50:31 +00:00
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.AddressStoragePrefix(addr))
|
2021-05-25 12:56:36 +00:00
|
|
|
action := "updated"
|
2022-01-05 07:28:27 +00:00
|
|
|
if len(value) == 0 {
|
2021-05-25 12:56:36 +00:00
|
|
|
store.Delete(key.Bytes())
|
|
|
|
action = "deleted"
|
|
|
|
} else {
|
2022-01-05 07:28:27 +00:00
|
|
|
store.Set(key.Bytes(), value)
|
2021-05-25 12:56:36 +00:00
|
|
|
}
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug(
|
2021-05-25 12:56:36 +00:00
|
|
|
fmt.Sprintf("state %s", action),
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"key", key.Hex(),
|
|
|
|
)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// SetCode set contract code, delete if code is empty.
|
|
|
|
func (k *Keeper) SetCode(ctx sdk.Context, codeHash, code []byte) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixCode)
|
2021-05-25 12:56:36 +00:00
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// store or delete code
|
|
|
|
action := "updated"
|
|
|
|
if len(code) == 0 {
|
|
|
|
store.Delete(codeHash)
|
|
|
|
action = "deleted"
|
|
|
|
} else {
|
|
|
|
store.Set(codeHash, code)
|
2021-05-25 12:56:36 +00:00
|
|
|
}
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug(
|
2022-01-05 07:28:27 +00:00
|
|
|
fmt.Sprintf("code %s", action),
|
|
|
|
"code-hash", common.BytesToHash(codeHash).Hex(),
|
2021-05-25 12:56:36 +00:00
|
|
|
)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// DeleteAccount handles contract's suicide call:
|
|
|
|
// - clear balance
|
|
|
|
// - remove code
|
|
|
|
// - remove states
|
|
|
|
// - remove auth account
|
|
|
|
func (k *Keeper) DeleteAccount(ctx sdk.Context, addr common.Address) error {
|
2021-05-25 12:56:36 +00:00
|
|
|
cosmosAddr := sdk.AccAddress(addr.Bytes())
|
2022-01-05 07:28:27 +00:00
|
|
|
acct := k.accountKeeper.GetAccount(ctx, cosmosAddr)
|
|
|
|
if acct == nil {
|
|
|
|
return nil
|
2021-11-30 11:36:10 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 18:18:02 +00:00
|
|
|
// NOTE: only Ethereum accounts (contracts) can be selfdestructed
|
|
|
|
ethAcct, ok := acct.(ethermint.EthAccountI)
|
2022-01-05 07:28:27 +00:00
|
|
|
if !ok {
|
2022-01-05 14:08:31 +00:00
|
|
|
return sdkerrors.Wrapf(types.ErrInvalidAccount, "type %T, address %s", acct, addr)
|
2021-09-17 06:22:52 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// clear balance
|
|
|
|
if err := k.SetBalance(ctx, addr, new(big.Int)); err != nil {
|
|
|
|
return err
|
2021-09-17 06:22:52 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// remove code
|
2022-01-05 18:18:02 +00:00
|
|
|
codeHashBz := ethAcct.GetCodeHash().Bytes()
|
|
|
|
if !bytes.Equal(codeHashBz, types.EmptyCodeHash) {
|
|
|
|
k.SetCode(ctx, codeHashBz, nil)
|
2021-09-17 06:22:52 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// clear storage
|
2022-01-05 14:08:31 +00:00
|
|
|
k.ForEachStorage(ctx, addr, func(key, _ common.Hash) bool {
|
2022-01-05 07:28:27 +00:00
|
|
|
k.SetState(ctx, addr, key, nil)
|
|
|
|
return true
|
|
|
|
})
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// remove auth account
|
|
|
|
k.accountKeeper.RemoveAccount(ctx, acct)
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug(
|
2022-01-05 07:28:27 +00:00
|
|
|
"account suicided",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"cosmos-address", cosmosAddr.String(),
|
2021-05-25 12:56:36 +00:00
|
|
|
)
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
return nil
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|