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/ethereum/go-ethereum/common"
|
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-05-18 19:21:12 +00:00
|
|
|
|
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"
|
2021-06-24 16:05:45 +00:00
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
ethermint "github.com/tharsis/ethermint/types"
|
|
|
|
"github.com/tharsis/ethermint/x/evm/types"
|
2020-05-18 19:21:12 +00:00
|
|
|
)
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
var _ vm.StateDB = &Keeper{}
|
|
|
|
|
2020-05-18 19:21:12 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2021-05-25 12:56:36 +00:00
|
|
|
// Account
|
2020-05-18 19:21:12 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// CreateAccount creates a new EthAccount instance from the provided address and
|
2021-08-05 16:24:06 +00:00
|
|
|
// sets the value to store. If an account with the given address already exists,
|
|
|
|
// this function also resets any preexisting code and storage associated with that
|
|
|
|
// address.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) CreateAccount(addr common.Address) {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
cosmosAddr := sdk.AccAddress(addr.Bytes())
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
account := k.accountKeeper.GetAccount(ctx, cosmosAddr)
|
2021-05-25 12:56:36 +00:00
|
|
|
log := ""
|
|
|
|
if account == nil {
|
|
|
|
log = "account created"
|
|
|
|
} else {
|
|
|
|
log = "account overwritten"
|
|
|
|
k.ResetAccount(addr)
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
account = k.accountKeeper.NewAccountWithAddress(ctx, cosmosAddr)
|
|
|
|
k.accountKeeper.SetAccount(ctx, account)
|
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
|
|
|
log,
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"cosmos-address", cosmosAddr.String(),
|
|
|
|
)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Balance
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
// AddBalance adds the given amount to the address balance coin by minting new
|
|
|
|
// coins and transferring them to the address. The coin denomination is obtained
|
|
|
|
// from the module parameters.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) AddBalance(addr common.Address, amount *big.Int) {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
|
2021-06-02 08:06:12 +00:00
|
|
|
if amount.Sign() != 1 {
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug(
|
2021-06-02 08:06:12 +00:00
|
|
|
"ignored non-positive amount addition",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"amount", amount.Int64(),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
cosmosAddr := sdk.AccAddress(addr.Bytes())
|
2021-08-31 12:50:31 +00:00
|
|
|
params := k.GetParams(ctx)
|
2021-05-25 12:56:36 +00:00
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
// Coin denom and amount already validated
|
|
|
|
coins := sdk.Coins{
|
|
|
|
{
|
|
|
|
Denom: params.EvmDenom,
|
|
|
|
Amount: sdk.NewIntFromBigInt(amount),
|
|
|
|
},
|
|
|
|
}
|
2021-05-25 12:56:36 +00:00
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, coins); err != nil {
|
|
|
|
k.Logger(ctx).Error(
|
2021-06-29 17:02:21 +00:00
|
|
|
"failed to mint coins when adding balance",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"cosmos-address", cosmosAddr.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
2021-09-17 06:22:52 +00:00
|
|
|
k.stateErr = err
|
2021-06-29 17:02:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, cosmosAddr, coins); err != nil {
|
|
|
|
k.Logger(ctx).Error(
|
2021-06-29 17:02:21 +00:00
|
|
|
"failed to send from module to account when adding balance",
|
2021-05-25 12:56:36 +00:00
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"cosmos-address", cosmosAddr.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
2021-09-17 06:22:52 +00:00
|
|
|
k.stateErr = err
|
2021-05-25 12:56:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug(
|
2021-05-25 12:56:36 +00:00
|
|
|
"balance addition",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"cosmos-address", cosmosAddr.String(),
|
|
|
|
)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
// SubBalance subtracts the given amount from the address balance by transferring the
|
|
|
|
// coins to an escrow account and then burning them. The coin denomination is obtained
|
|
|
|
// from the module parameters. This function performs a no-op if the amount is negative
|
|
|
|
// or the user doesn't have enough funds for the transfer.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) SubBalance(addr common.Address, amount *big.Int) {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
|
2021-06-02 08:06:12 +00:00
|
|
|
if amount.Sign() != 1 {
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug(
|
2021-06-02 08:06:12 +00:00
|
|
|
"ignored non-positive amount addition",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"amount", amount.Int64(),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
cosmosAddr := sdk.AccAddress(addr.Bytes())
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
params := k.GetParams(ctx)
|
|
|
|
|
|
|
|
// Coin denom and amount already validated
|
|
|
|
coins := sdk.Coins{
|
|
|
|
{
|
|
|
|
Denom: params.EvmDenom,
|
|
|
|
Amount: sdk.NewIntFromBigInt(amount),
|
|
|
|
},
|
|
|
|
}
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, cosmosAddr, types.ModuleName, coins); err != nil {
|
|
|
|
k.Logger(ctx).Debug(
|
2021-06-29 17:02:21 +00:00
|
|
|
"failed to send from account to module when subtracting balance",
|
2021-05-25 12:56:36 +00:00
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"cosmos-address", cosmosAddr.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, coins); err != nil {
|
|
|
|
k.Logger(ctx).Error(
|
2021-06-29 17:02:21 +00:00
|
|
|
"failed to burn coins when subtracting balance",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"cosmos-address", cosmosAddr.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
2021-09-17 06:22:52 +00:00
|
|
|
k.stateErr = err
|
2021-06-29 17:02:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug(
|
2021-05-25 12:56:36 +00:00
|
|
|
"balance subtraction",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"cosmos-address", cosmosAddr.String(),
|
|
|
|
)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
// GetBalance returns the EVM denomination balance of the provided address. The
|
|
|
|
// denomination is obtained from the module parameters.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) GetBalance(addr common.Address) *big.Int {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return big.NewInt(0)
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
cosmosAddr := sdk.AccAddress(addr.Bytes())
|
2021-08-31 12:50:31 +00:00
|
|
|
params := k.GetParams(ctx)
|
|
|
|
balance := k.bankKeeper.GetBalance(ctx, cosmosAddr, params.EvmDenom)
|
2021-05-25 12:56:36 +00:00
|
|
|
|
|
|
|
return balance.Amount.BigInt()
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Nonce
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
// GetNonce retrieves the account with the given address and returns the tx
|
|
|
|
// sequence (i.e nonce). The function performs a no-op if the account is not found.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) GetNonce(addr common.Address) uint64 {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
cosmosAddr := sdk.AccAddress(addr.Bytes())
|
2021-08-31 12:50:31 +00:00
|
|
|
nonce, err := k.accountKeeper.GetSequence(ctx, cosmosAddr)
|
2021-05-25 12:56:36 +00:00
|
|
|
if err != nil {
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Error(
|
2021-05-25 12:56:36 +00:00
|
|
|
"account not found",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"cosmos-address", cosmosAddr.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
2021-09-17 06:22:52 +00:00
|
|
|
// since adding state error here will break some logic in the go-ethereum (unwanted panic), no
|
|
|
|
// state error will be store here
|
|
|
|
// Refer panic: https://github.com/ethereum/go-ethereum/blob/991384a7f6719e1125ca0be7fb27d0c4d1c5d2d3/core/vm/operations_acl.go#L66
|
2021-05-25 12:56:36 +00:00
|
|
|
}
|
2021-04-18 15:54:18 +00:00
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
return nonce
|
2020-06-04 10:40:21 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 08:06:12 +00:00
|
|
|
// SetNonce sets the given nonce as the sequence of the address' account. If the
|
|
|
|
// account doesn't exist, a new one will be created from the address.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) SetNonce(addr common.Address, nonce uint64) {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
cosmosAddr := sdk.AccAddress(addr.Bytes())
|
2021-08-31 12:50:31 +00:00
|
|
|
account := k.accountKeeper.GetAccount(ctx, cosmosAddr)
|
2021-05-25 12:56:36 +00:00
|
|
|
if account == nil {
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug(
|
2021-05-25 12:56:36 +00:00
|
|
|
"account not found",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"cosmos-address", cosmosAddr.String(),
|
|
|
|
)
|
|
|
|
|
|
|
|
// create address if it doesn't exist
|
2021-08-31 12:50:31 +00:00
|
|
|
account = k.accountKeeper.NewAccountWithAddress(ctx, cosmosAddr)
|
2021-05-25 12:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := account.SetSequence(nonce); err != nil {
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Error(
|
2021-05-25 12:56:36 +00:00
|
|
|
"failed to set nonce",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"cosmos-address", cosmosAddr.String(),
|
|
|
|
"nonce", nonce,
|
|
|
|
"error", err,
|
|
|
|
)
|
2021-09-17 06:22:52 +00:00
|
|
|
k.stateErr = err
|
2021-05-25 12:56:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
k.accountKeeper.SetAccount(ctx, account)
|
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
|
|
|
"nonce set",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"cosmos-address", cosmosAddr.String(),
|
|
|
|
"nonce", nonce,
|
|
|
|
)
|
2020-08-23 21:41:54 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Code
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
// GetCodeHash fetches the account from the store and returns its code hash. If the account doesn't
|
|
|
|
// exist or is not an EthAccount type, GetCodeHash returns the empty code hash value.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) GetCodeHash(addr common.Address) common.Hash {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
2021-05-25 12:56:36 +00:00
|
|
|
cosmosAddr := sdk.AccAddress(addr.Bytes())
|
2021-08-31 12:50:31 +00:00
|
|
|
|
|
|
|
account := k.accountKeeper.GetAccount(ctx, cosmosAddr)
|
2021-05-25 12:56:36 +00:00
|
|
|
if account == nil {
|
|
|
|
return common.BytesToHash(types.EmptyCodeHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
ethAccount, isEthAccount := account.(*ethermint.EthAccount)
|
|
|
|
if !isEthAccount {
|
|
|
|
return common.BytesToHash(types.EmptyCodeHash)
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
return common.HexToHash(ethAccount.CodeHash)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
// GetCode returns the code byte array associated with the given address.
|
|
|
|
// If the code hash from the account is empty, this function returns nil.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) GetCode(addr common.Address) []byte {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
2021-05-25 12:56:36 +00:00
|
|
|
hash := k.GetCodeHash(addr)
|
|
|
|
|
|
|
|
if bytes.Equal(hash.Bytes(), common.BytesToHash(types.EmptyCodeHash).Bytes()) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixCode)
|
2021-05-25 12:56:36 +00:00
|
|
|
code := store.Get(hash.Bytes())
|
|
|
|
|
|
|
|
if len(code) == 0 {
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug(
|
2021-05-25 12:56:36 +00:00
|
|
|
"code not found",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"code-hash", hash.Hex(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return code
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
// SetCode stores the code byte array to the application KVStore and sets the
|
|
|
|
// code hash to the given account. The code is deleted from the store if it is empty.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) SetCode(addr common.Address, code []byte) {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
|
2021-06-15 17:39:41 +00:00
|
|
|
if bytes.Equal(code, types.EmptyCodeHash) {
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug("passed in EmptyCodeHash, but expected empty code")
|
2021-06-15 17:39:41 +00:00
|
|
|
}
|
2021-05-25 12:56:36 +00:00
|
|
|
hash := crypto.Keccak256Hash(code)
|
|
|
|
|
|
|
|
// update account code hash
|
2021-08-31 12:50:31 +00:00
|
|
|
account := k.accountKeeper.GetAccount(ctx, addr.Bytes())
|
2021-05-25 12:56:36 +00:00
|
|
|
if account == nil {
|
2021-08-31 12:50:31 +00:00
|
|
|
account = k.accountKeeper.NewAccountWithAddress(ctx, addr.Bytes())
|
|
|
|
k.accountKeeper.SetAccount(ctx, account)
|
2021-05-25 12:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ethAccount, isEthAccount := account.(*ethermint.EthAccount)
|
|
|
|
if !isEthAccount {
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Error(
|
2021-05-25 12:56:36 +00:00
|
|
|
"invalid account type",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"code-hash", hash.Hex(),
|
|
|
|
)
|
2021-09-17 06:22:52 +00:00
|
|
|
k.stateErr = fmt.Errorf("invalid account type, ethereum-address %v, code-hash %v", addr.Hex(), hash.Hex())
|
2021-05-25 12:56:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
ethAccount.CodeHash = hash.Hex()
|
2021-08-31 12:50:31 +00:00
|
|
|
k.accountKeeper.SetAccount(ctx, ethAccount)
|
2021-05-25 12:56:36 +00:00
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixCode)
|
2021-05-25 12:56:36 +00:00
|
|
|
|
|
|
|
action := "updated"
|
|
|
|
|
|
|
|
// store or delete code
|
|
|
|
if len(code) == 0 {
|
|
|
|
store.Delete(hash.Bytes())
|
|
|
|
action = "deleted"
|
|
|
|
} else {
|
|
|
|
store.Set(hash.Bytes(), code)
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug(
|
2021-05-25 12:56:36 +00:00
|
|
|
fmt.Sprintf("code %s", action),
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"code-hash", hash.Hex(),
|
|
|
|
)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 17:39:41 +00:00
|
|
|
// GetCodeSize returns the size of the contract code associated with this object,
|
|
|
|
// or zero if none.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) GetCodeSize(addr common.Address) int {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-06-15 17:39:41 +00:00
|
|
|
code := k.GetCode(addr)
|
|
|
|
return len(code)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2021-05-25 12:56:36 +00:00
|
|
|
// Refund
|
2020-05-18 19:21:12 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// NOTE: gas refunded needs to be tracked and stored in a separate variable in
|
|
|
|
// order to add it subtract/add it from/to the gas used value after the EVM
|
2021-06-24 16:05:45 +00:00
|
|
|
// execution has finalized. The refund value is cleared on every transaction and
|
2021-05-25 12:56:36 +00:00
|
|
|
// at the end of every block.
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
// AddRefund adds the given amount of gas to the refund transient value.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) AddRefund(gas uint64) {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
2021-05-25 12:56:36 +00:00
|
|
|
refund := k.GetRefund()
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
refund += gas
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
store := ctx.TransientStore(k.transientKey)
|
2021-05-25 12:56:36 +00:00
|
|
|
store.Set(types.KeyPrefixTransientRefund, sdk.Uint64ToBigEndian(refund))
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
// SubRefund subtracts the given amount of gas from the transient refund value. This function
|
2021-05-25 12:56:36 +00:00
|
|
|
// will panic if gas amount is greater than the stored refund.
|
|
|
|
func (k *Keeper) SubRefund(gas uint64) {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
2021-05-25 12:56:36 +00:00
|
|
|
refund := k.GetRefund()
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
if gas > refund {
|
|
|
|
// TODO: (@fedekunze) set to 0?? Geth panics here
|
|
|
|
panic("refund counter below zero")
|
|
|
|
}
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
refund -= gas
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
store := ctx.TransientStore(k.transientKey)
|
2021-05-25 12:56:36 +00:00
|
|
|
store.Set(types.KeyPrefixTransientRefund, sdk.Uint64ToBigEndian(refund))
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// GetRefund returns the amount of gas available for return after the tx execution
|
2021-08-05 16:24:06 +00:00
|
|
|
// finalizes. This value is reset to 0 on every transaction.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) GetRefund() uint64 {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
store := ctx.TransientStore(k.transientKey)
|
2021-05-25 12:56:36 +00:00
|
|
|
|
|
|
|
bz := store.Get(types.KeyPrefixTransientRefund)
|
|
|
|
if len(bz) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return sdk.BigEndianToUint64(bz)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// State
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-09-27 10:26:45 +00:00
|
|
|
func doGetState(ctx sdk.Context, storeKey sdk.StoreKey, addr common.Address, key common.Hash) common.Hash {
|
2021-08-04 07:49:02 +00:00
|
|
|
store := prefix.NewStore(ctx.KVStore(storeKey), types.AddressStoragePrefix(addr))
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
value := store.Get(key.Bytes())
|
|
|
|
if len(value) == 0 {
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
return common.BytesToHash(value)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 07:49:02 +00:00
|
|
|
// GetCommittedState returns the value set in store for the given key hash. If the key is not registered
|
|
|
|
// this function returns the empty hash.
|
|
|
|
func (k *Keeper) GetCommittedState(addr common.Address, hash common.Hash) common.Hash {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
|
|
|
|
2021-08-10 07:22:46 +00:00
|
|
|
return doGetState(k.ctxStack.initialCtx, k.storeKey, addr, hash)
|
2021-08-04 07:49:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 07:07:11 +00:00
|
|
|
// GetState returns the committed state for the given key hash, as all changes are committed directly
|
2021-06-02 08:06:12 +00:00
|
|
|
// to the KVStore.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) GetState(addr common.Address, hash common.Hash) common.Hash {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
return doGetState(ctx, k.storeKey, addr, hash)
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 08:06:12 +00:00
|
|
|
// SetState sets the given hashes (key, value) to the KVStore. If the value hash is empty, this
|
|
|
|
// function deletes the key from the store.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) SetState(addr common.Address, key, value common.Hash) {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.AddressStoragePrefix(addr))
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
action := "updated"
|
|
|
|
if ethermint.IsEmptyHash(value.Hex()) {
|
|
|
|
store.Delete(key.Bytes())
|
|
|
|
action = "deleted"
|
|
|
|
} else {
|
|
|
|
store.Set(key.Bytes(), value.Bytes())
|
|
|
|
}
|
2020-05-18 19:21:12 +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
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2021-05-25 12:56:36 +00:00
|
|
|
// Suicide
|
2020-05-18 19:21:12 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// Suicide marks the given account as suicided and clears the account balance of
|
|
|
|
// the EVM tokens.
|
|
|
|
func (k *Keeper) Suicide(addr common.Address) bool {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
prev := k.HasSuicided(addr)
|
|
|
|
if prev {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
cosmosAddr := sdk.AccAddress(addr.Bytes())
|
|
|
|
|
|
|
|
_, err := k.ClearBalance(cosmosAddr)
|
|
|
|
if err != nil {
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Error(
|
2021-05-25 12:56:36 +00:00
|
|
|
"failed to subtract balance on suicide",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"cosmos-address", cosmosAddr.String(),
|
|
|
|
"error", err,
|
|
|
|
)
|
2021-09-17 06:22:52 +00:00
|
|
|
k.stateErr = err
|
2021-05-25 12:56:36 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: (@fedekunze) do we also need to delete the storage state and the code?
|
2021-08-31 12:50:31 +00:00
|
|
|
k.setSuicided(ctx, addr)
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug(
|
2021-05-25 12:56:36 +00:00
|
|
|
"account suicided",
|
|
|
|
"ethereum-address", addr.Hex(),
|
|
|
|
"cosmos-address", cosmosAddr.String(),
|
|
|
|
)
|
|
|
|
|
|
|
|
return true
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
// setSuicided sets a single byte to the transient store and marks the address as suicided
|
|
|
|
func (k Keeper) setSuicided(ctx sdk.Context, addr common.Address) {
|
|
|
|
store := prefix.NewStore(ctx.TransientStore(k.transientKey), types.KeyPrefixTransientSuicided)
|
|
|
|
store.Set(addr.Bytes(), []byte{1})
|
|
|
|
}
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// HasSuicided queries the transient store to check if the account has been marked as suicided in the
|
|
|
|
// current block. Accounts that are suicided will be returned as non-nil during queries and "cleared"
|
|
|
|
// after the block has been committed.
|
|
|
|
func (k *Keeper) HasSuicided(addr common.Address) bool {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
store := prefix.NewStore(ctx.TransientStore(k.transientKey), types.KeyPrefixTransientSuicided)
|
2021-05-25 12:56:36 +00:00
|
|
|
return store.Has(addr.Bytes())
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2021-05-25 12:56:36 +00:00
|
|
|
// Account Exist / Empty
|
2020-05-18 19:21:12 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// Exist returns true if the given account exists in store or if it has been
|
|
|
|
// marked as suicided in the transient store.
|
|
|
|
func (k *Keeper) Exist(addr common.Address) bool {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
2021-05-25 12:56:36 +00:00
|
|
|
// return true if the account has suicided
|
|
|
|
if k.HasSuicided(addr) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
cosmosAddr := sdk.AccAddress(addr.Bytes())
|
2021-08-31 12:50:31 +00:00
|
|
|
account := k.accountKeeper.GetAccount(ctx, cosmosAddr)
|
2021-05-25 12:56:36 +00:00
|
|
|
return account != nil
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// Empty returns true if the address meets the following conditions:
|
|
|
|
// - nonce is 0
|
|
|
|
// - balance amount for evm denom is 0
|
|
|
|
// - account code hash is empty
|
2021-06-02 08:06:12 +00:00
|
|
|
//
|
|
|
|
// Non-ethereum accounts are considered not empty
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) Empty(addr common.Address) bool {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
2021-05-25 12:56:36 +00:00
|
|
|
nonce := uint64(0)
|
|
|
|
codeHash := types.EmptyCodeHash
|
|
|
|
|
|
|
|
cosmosAddr := sdk.AccAddress(addr.Bytes())
|
2021-08-31 12:50:31 +00:00
|
|
|
account := k.accountKeeper.GetAccount(ctx, cosmosAddr)
|
2021-05-25 12:56:36 +00:00
|
|
|
|
|
|
|
if account != nil {
|
|
|
|
nonce = account.GetSequence()
|
|
|
|
ethAccount, isEthAccount := account.(*ethermint.EthAccount)
|
|
|
|
if !isEthAccount {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-06-29 17:02:21 +00:00
|
|
|
codeHash = common.HexToHash(ethAccount.CodeHash).Bytes()
|
2021-05-25 12:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
balance := k.GetBalance(addr)
|
|
|
|
hasZeroBalance := balance.Sign() == 0
|
|
|
|
hasEmptyCodeHash := bytes.Equal(codeHash, types.EmptyCodeHash)
|
|
|
|
|
|
|
|
return hasZeroBalance && nonce == 0 && hasEmptyCodeHash
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2021-05-25 12:56:36 +00:00
|
|
|
// Access List
|
2020-05-18 19:21:12 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// PrepareAccessList handles the preparatory steps for executing a state transition with
|
|
|
|
// regards to both EIP-2929 and EIP-2930:
|
|
|
|
//
|
|
|
|
// - Add sender to access list (2929)
|
|
|
|
// - Add destination to access list (2929)
|
|
|
|
// - Add precompiles to access list (2929)
|
|
|
|
// - Add the contents of the optional tx access list (2930)
|
|
|
|
//
|
|
|
|
// This method should only be called if Yolov3/Berlin/2929+2930 is applicable at the current number.
|
|
|
|
func (k *Keeper) PrepareAccessList(sender common.Address, dest *common.Address, precompiles []common.Address, txAccesses ethtypes.AccessList) {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
k.AddAddressToAccessList(sender)
|
|
|
|
if dest != nil {
|
|
|
|
k.AddAddressToAccessList(*dest)
|
|
|
|
// If it's a create-tx, the destination will be added inside evm.create
|
|
|
|
}
|
|
|
|
for _, addr := range precompiles {
|
|
|
|
k.AddAddressToAccessList(addr)
|
|
|
|
}
|
|
|
|
for _, tuple := range txAccesses {
|
|
|
|
k.AddAddressToAccessList(tuple.Address)
|
|
|
|
for _, key := range tuple.StorageKeys {
|
|
|
|
k.AddSlotToAccessList(tuple.Address, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 10:05:49 +00:00
|
|
|
// AddressInAccessList returns true if the address is registered on the transient store.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) AddressInAccessList(addr common.Address) bool {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
ts := prefix.NewStore(ctx.TransientStore(k.transientKey), types.KeyPrefixTransientAccessListAddress)
|
2021-06-07 10:05:49 +00:00
|
|
|
return ts.Has(addr.Bytes())
|
2021-05-25 12:56:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 10:05:49 +00:00
|
|
|
// SlotInAccessList checks if the address and the slots are registered in the transient store
|
2021-09-05 11:03:06 +00:00
|
|
|
func (k *Keeper) SlotInAccessList(addr common.Address, slot common.Hash) (addressOk, slotOk bool) {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
|
2021-06-07 10:05:49 +00:00
|
|
|
addressOk = k.AddressInAccessList(addr)
|
|
|
|
slotOk = k.addressSlotInAccessList(addr, slot)
|
|
|
|
return addressOk, slotOk
|
2021-05-25 12:56:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 10:05:49 +00:00
|
|
|
// addressSlotInAccessList returns true if the address's slot is registered on the transient store.
|
|
|
|
func (k *Keeper) addressSlotInAccessList(addr common.Address, slot common.Hash) bool {
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
ts := prefix.NewStore(ctx.TransientStore(k.transientKey), types.KeyPrefixTransientAccessListSlot)
|
2021-06-07 10:05:49 +00:00
|
|
|
key := append(addr.Bytes(), slot.Bytes()...)
|
|
|
|
return ts.Has(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddAddressToAccessList adds the given address to the access list. If the address is already
|
|
|
|
// in the access list, this function performs a no-op.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) AddAddressToAccessList(addr common.Address) {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-07 10:05:49 +00:00
|
|
|
if k.AddressInAccessList(addr) {
|
2021-05-25 12:56:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
ts := prefix.NewStore(ctx.TransientStore(k.transientKey), types.KeyPrefixTransientAccessListAddress)
|
2021-06-07 10:05:49 +00:00
|
|
|
ts.Set(addr.Bytes(), []byte{0x1})
|
2021-05-25 12:56:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 10:05:49 +00:00
|
|
|
// AddSlotToAccessList adds the given (address, slot) to the access list. If the address and slot are
|
|
|
|
// already in the access list, this function performs a no-op.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) AddSlotToAccessList(addr common.Address, slot common.Hash) {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-07 10:05:49 +00:00
|
|
|
k.AddAddressToAccessList(addr)
|
|
|
|
if k.addressSlotInAccessList(addr, slot) {
|
2021-05-25 12:56:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
ts := prefix.NewStore(ctx.TransientStore(k.transientKey), types.KeyPrefixTransientAccessListSlot)
|
2021-06-07 10:05:49 +00:00
|
|
|
key := append(addr.Bytes(), slot.Bytes()...)
|
|
|
|
ts.Set(key, []byte{0x1})
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Snapshotting
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-08-10 07:22:46 +00:00
|
|
|
// Snapshot return the index in the cached context stack
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) Snapshot() int {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-08-10 07:22:46 +00:00
|
|
|
return k.ctxStack.Snapshot()
|
2020-05-18 19:21:12 +00:00
|
|
|
}
|
|
|
|
|
2021-08-10 07:22:46 +00:00
|
|
|
// RevertToSnapshot pop all the cached contexts after(including) the snapshot
|
|
|
|
func (k *Keeper) RevertToSnapshot(target int) {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-10 07:22:46 +00:00
|
|
|
k.ctxStack.RevertToSnapshot(target)
|
|
|
|
}
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Log
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// AddLog appends the given ethereum Log to the list of Logs associated with the transaction hash kept in the current
|
|
|
|
// context. This function also fills in the tx hash, block hash, tx index and log index fields before setting the log
|
|
|
|
// to store.
|
|
|
|
func (k *Keeper) AddLog(log *ethtypes.Log) {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
|
|
|
|
log.BlockHash = common.BytesToHash(ctx.HeaderHash())
|
2021-05-25 12:56:36 +00:00
|
|
|
log.TxIndex = uint(k.GetTxIndexTransient())
|
2021-06-30 09:35:11 +00:00
|
|
|
log.TxHash = k.GetTxHashTransient()
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-07-26 08:40:59 +00:00
|
|
|
log.Index = uint(k.GetLogSizeTransient())
|
|
|
|
k.IncreaseLogSizeTransient()
|
2021-09-15 09:45:03 +00:00
|
|
|
k.AddLogTransient(log)
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
k.Logger(ctx).Debug(
|
2021-05-25 12:56:36 +00:00
|
|
|
"log added",
|
2021-06-24 16:05:45 +00:00
|
|
|
"tx-hash-ethereum", log.TxHash.Hex(),
|
2021-05-25 12:56:36 +00:00
|
|
|
"log-index", int(log.Index),
|
|
|
|
)
|
2020-06-22 16:07:35 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Trie
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-06-22 16:07:35 +00:00
|
|
|
|
2021-05-25 12:56:36 +00:00
|
|
|
// AddPreimage performs a no-op since the EnablePreimageRecording flag is disabled
|
|
|
|
// on the vm.Config during state transitions. No store trie preimages are written
|
|
|
|
// to the database.
|
|
|
|
func (k *Keeper) AddPreimage(_ common.Hash, _ []byte) {}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Iterator
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2021-08-05 16:24:06 +00:00
|
|
|
// ForEachStorage uses the store iterator to iterate over all the state keys and perform a callback
|
|
|
|
// function on each of them.
|
2021-05-25 12:56:36 +00:00
|
|
|
func (k *Keeper) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error {
|
2021-09-17 06:22:52 +00:00
|
|
|
if k.HasStateError() {
|
|
|
|
return k.stateErr
|
|
|
|
}
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
ctx := k.Ctx()
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
2021-05-25 12:56:36 +00:00
|
|
|
prefix := types.AddressStoragePrefix(addr)
|
|
|
|
|
|
|
|
iterator := sdk.KVStorePrefixIterator(store, prefix)
|
|
|
|
defer iterator.Close()
|
|
|
|
|
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
|
|
|
|
|
|
|
// TODO: check if the key prefix needs to be trimmed
|
|
|
|
key := common.BytesToHash(iterator.Key())
|
|
|
|
value := common.BytesToHash(iterator.Value())
|
|
|
|
|
|
|
|
// check if iteration stops
|
|
|
|
if cb(key, value) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
2021-09-17 06:22:52 +00:00
|
|
|
|
|
|
|
// HasStateError return the previous error for any state operations
|
|
|
|
func (k *Keeper) HasStateError() bool {
|
|
|
|
return k.stateErr != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClearStateError reset the previous state operation error to nil
|
|
|
|
func (k *Keeper) ClearStateError() {
|
|
|
|
k.stateErr = nil
|
|
|
|
}
|