2020-03-09 13:17:23 +00:00
|
|
|
package keeper
|
2019-07-24 22:14:12 +00:00
|
|
|
|
|
|
|
import (
|
2020-04-01 18:49:21 +00:00
|
|
|
"encoding/binary"
|
2019-09-27 14:08:45 +00:00
|
|
|
"fmt"
|
2020-06-04 10:40:21 +00:00
|
|
|
"math/big"
|
2019-09-27 14:08:45 +00:00
|
|
|
|
2020-04-30 03:36:30 +00:00
|
|
|
"github.com/tendermint/tendermint/libs/log"
|
|
|
|
|
2019-07-25 20:38:55 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-06-04 10:40:21 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
2019-07-24 22:14:12 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-09-02 19:41:05 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/params"
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2020-03-09 13:17:23 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
2020-05-18 19:21:12 +00:00
|
|
|
|
2020-08-23 21:41:54 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-07-24 22:14:12 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Keeper wraps the CommitStateDB, allowing us to pass in SDK context while adhering
|
2020-03-09 13:17:23 +00:00
|
|
|
// to the StateDB interface.
|
2019-07-24 22:14:12 +00:00
|
|
|
type Keeper struct {
|
2020-03-09 13:17:23 +00:00
|
|
|
// Amino codec
|
|
|
|
cdc *codec.Codec
|
2020-06-04 10:40:21 +00:00
|
|
|
// Store key required for the EVM Prefix KVStore. It is required by:
|
|
|
|
// - storing Account's Storage State
|
|
|
|
// - storing Account's Code
|
|
|
|
// - storing transaction Logs
|
|
|
|
// - storing block height -> bloom filter map. Needed for the Web3 API.
|
|
|
|
// - storing block hash -> block height map. Needed for the Web3 API.
|
2020-09-02 19:41:05 +00:00
|
|
|
storeKey sdk.StoreKey
|
|
|
|
// Ethermint concrete implementation on the EVM StateDB interface
|
2020-03-09 13:17:23 +00:00
|
|
|
CommitStateDB *types.CommitStateDB
|
2020-05-18 19:21:12 +00:00
|
|
|
// Transaction counter in a block. Used on StateSB's Prepare function.
|
|
|
|
// It is reset to 0 every block on BeginBlock so there's no point in storing the counter
|
|
|
|
// on the KVStore or adding it as a field on the EVM genesis state.
|
|
|
|
TxCount int
|
|
|
|
Bloom *big.Int
|
2019-10-03 16:46:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 14:08:45 +00:00
|
|
|
// NewKeeper generates new evm module keeper
|
2020-04-01 18:49:21 +00:00
|
|
|
func NewKeeper(
|
2020-09-02 19:41:05 +00:00
|
|
|
cdc *codec.Codec, storeKey sdk.StoreKey, paramSpace params.Subspace, ak types.AccountKeeper,
|
2020-04-01 18:49:21 +00:00
|
|
|
) Keeper {
|
2020-09-02 19:41:05 +00:00
|
|
|
// set KeyTable if it has not already been set
|
|
|
|
if !paramSpace.HasKeyTable() {
|
|
|
|
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: we pass in the parameter space to the CommitStateDB in order to use custom denominations for the EVM operations
|
2019-07-24 22:14:12 +00:00
|
|
|
return Keeper{
|
2020-03-09 13:17:23 +00:00
|
|
|
cdc: cdc,
|
2020-06-04 10:40:21 +00:00
|
|
|
storeKey: storeKey,
|
2020-09-02 19:41:05 +00:00
|
|
|
CommitStateDB: types.NewCommitStateDB(sdk.Context{}, storeKey, paramSpace, ak),
|
2020-04-01 18:49:21 +00:00
|
|
|
TxCount: 0,
|
2020-03-09 13:17:23 +00:00
|
|
|
Bloom: big.NewInt(0),
|
2019-09-27 14:08:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 03:36:30 +00:00
|
|
|
// Logger returns a module-specific logger.
|
|
|
|
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
|
|
|
|
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
|
|
|
|
}
|
|
|
|
|
2019-09-27 14:08:45 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Block hash mapping functions
|
2020-06-04 10:40:21 +00:00
|
|
|
// Required by Web3 API.
|
|
|
|
// TODO: remove once tendermint support block queries by hash.
|
2019-09-27 14:08:45 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
// GetBlockHash gets block height from block consensus hash
|
|
|
|
func (k Keeper) GetBlockHash(ctx sdk.Context, hash []byte) (int64, bool) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixBlockHash)
|
2019-09-27 14:08:45 +00:00
|
|
|
bz := store.Get(hash)
|
2020-04-23 15:49:25 +00:00
|
|
|
if len(bz) == 0 {
|
2020-06-04 10:40:21 +00:00
|
|
|
return 0, false
|
2019-07-24 22:14:12 +00:00
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
height := binary.BigEndian.Uint64(bz)
|
2020-06-04 10:40:21 +00:00
|
|
|
return int64(height), true
|
2020-04-23 15:49:25 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
// SetBlockHash sets the mapping from block consensus hash to block height
|
|
|
|
func (k Keeper) SetBlockHash(ctx sdk.Context, hash []byte, height int64) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixBlockHash)
|
2020-04-23 15:49:25 +00:00
|
|
|
bz := sdk.Uint64ToBigEndian(uint64(height))
|
|
|
|
store.Set(hash, bz)
|
2019-07-24 22:14:12 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 19:32:56 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Block bloom bits mapping functions
|
2020-06-04 10:40:21 +00:00
|
|
|
// Required by Web3 API.
|
2019-10-04 19:32:56 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
// GetBlockBloom gets bloombits from block height
|
|
|
|
func (k Keeper) GetBlockBloom(ctx sdk.Context, height int64) (ethtypes.Bloom, bool) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixBloom)
|
|
|
|
bz := store.Get(types.BloomKey(height))
|
2020-04-01 18:49:21 +00:00
|
|
|
if len(bz) == 0 {
|
2020-06-04 10:40:21 +00:00
|
|
|
return ethtypes.Bloom{}, false
|
2019-10-04 19:32:56 +00:00
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
return ethtypes.BytesToBloom(bz), true
|
2019-10-04 19:32:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
// SetBlockBloom sets the mapping from block height to bloom bits
|
|
|
|
func (k Keeper) SetBlockBloom(ctx sdk.Context, height int64, bloom ethtypes.Bloom) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixBloom)
|
|
|
|
store.Set(types.BloomKey(height), bloom.Bytes())
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
// GetAllTxLogs return all the transaction logs from the store.
|
|
|
|
func (k Keeper) GetAllTxLogs(ctx sdk.Context) []types.TransactionLogs {
|
|
|
|
store := ctx.KVStore(k.storeKey)
|
|
|
|
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixLogs)
|
|
|
|
defer iterator.Close()
|
|
|
|
|
|
|
|
txsLogs := []types.TransactionLogs{}
|
|
|
|
for ; iterator.Valid(); iterator.Next() {
|
2020-08-23 21:41:54 +00:00
|
|
|
hash := common.BytesToHash(iterator.Key())
|
2020-06-04 10:40:21 +00:00
|
|
|
var logs []*ethtypes.Log
|
|
|
|
k.cdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &logs)
|
|
|
|
|
|
|
|
// add a new entry
|
|
|
|
txLog := types.NewTransactionLogs(hash, logs)
|
|
|
|
txsLogs = append(txsLogs, txLog)
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
2020-06-04 10:40:21 +00:00
|
|
|
return txsLogs
|
2019-07-24 22:14:12 +00:00
|
|
|
}
|
2020-08-23 21:41:54 +00:00
|
|
|
|
|
|
|
// GetAccountStorage return state storage associated with an account
|
|
|
|
func (k Keeper) GetAccountStorage(ctx sdk.Context, address common.Address) (types.Storage, error) {
|
|
|
|
storage := types.Storage{}
|
|
|
|
err := k.ForEachStorage(ctx, address, func(key, value common.Hash) bool {
|
|
|
|
storage = append(storage, types.NewState(key, value))
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return types.Storage{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return storage, nil
|
|
|
|
}
|
2020-09-02 19:41:05 +00:00
|
|
|
|
|
|
|
// GetChainConfig gets block height from block consensus hash
|
|
|
|
func (k Keeper) GetChainConfig(ctx sdk.Context) (types.ChainConfig, bool) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixChainConfig)
|
|
|
|
// get from an empty key that's already prefixed by KeyPrefixChainConfig
|
|
|
|
bz := store.Get([]byte{})
|
|
|
|
if len(bz) == 0 {
|
|
|
|
return types.ChainConfig{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
var config types.ChainConfig
|
|
|
|
k.cdc.MustUnmarshalBinaryBare(bz, &config)
|
|
|
|
return config, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetChainConfig sets the mapping from block consensus hash to block height
|
|
|
|
func (k Keeper) SetChainConfig(ctx sdk.Context, config types.ChainConfig) {
|
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixChainConfig)
|
|
|
|
bz := k.cdc.MustMarshalBinaryBare(config)
|
|
|
|
// get to an empty key that's already prefixed by KeyPrefixChainConfig
|
|
|
|
store.Set([]byte{}, bz)
|
|
|
|
}
|