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"
|
|
|
|
"errors"
|
2019-09-27 14:08:45 +00:00
|
|
|
"fmt"
|
|
|
|
|
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"
|
2019-07-24 22:14:12 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
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
|
|
|
|
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2019-07-24 22:14:12 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
|
|
|
|
"math/big"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
// Store key required to update the block bloom filter mappings needed for the
|
|
|
|
// Web3 API
|
2020-04-01 18:49:21 +00:00
|
|
|
blockKey sdk.StoreKey
|
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(
|
|
|
|
cdc *codec.Codec, blockKey, codeKey, storeKey sdk.StoreKey,
|
2020-04-22 19:26:01 +00:00
|
|
|
ak types.AccountKeeper, bk types.BankKeeper,
|
2020-04-01 18:49:21 +00:00
|
|
|
) Keeper {
|
2019-07-24 22:14:12 +00:00
|
|
|
return Keeper{
|
2020-03-09 13:17:23 +00:00
|
|
|
cdc: cdc,
|
2020-04-01 18:49:21 +00:00
|
|
|
blockKey: blockKey,
|
2020-04-22 19:26:01 +00:00
|
|
|
CommitStateDB: types.NewCommitStateDB(sdk.Context{}, codeKey, storeKey, ak, bk),
|
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
|
|
|
|
// May be removed when using only as module (only required by rpc api)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// GetBlockHashMapping gets block height from block consensus hash
|
2020-04-23 15:49:25 +00:00
|
|
|
func (k Keeper) GetBlockHashMapping(ctx sdk.Context, hash []byte) (int64, error) {
|
2020-04-01 18:49:21 +00:00
|
|
|
store := ctx.KVStore(k.blockKey)
|
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-05-15 02:08:13 +00:00
|
|
|
return 0, fmt.Errorf("block with hash '%s' not found", ethcmn.BytesToHash(hash).Hex())
|
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)
|
|
|
|
return int64(height), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBlockHashMapping sets the mapping from block consensus hash to block height
|
|
|
|
func (k Keeper) SetBlockHashMapping(ctx sdk.Context, hash []byte, height int64) {
|
|
|
|
store := ctx.KVStore(k.blockKey)
|
|
|
|
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
|
|
|
|
// May be removed when using only as module (only required by rpc api)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
// GetBlockBloomMapping gets bloombits from block height
|
|
|
|
func (k Keeper) GetBlockBloomMapping(ctx sdk.Context, height int64) (ethtypes.Bloom, error) {
|
2020-04-01 18:49:21 +00:00
|
|
|
store := ctx.KVStore(k.blockKey)
|
2020-04-23 15:49:25 +00:00
|
|
|
heightBz := sdk.Uint64ToBigEndian(uint64(height))
|
|
|
|
bz := store.Get(types.BloomKey(heightBz))
|
2020-04-01 18:49:21 +00:00
|
|
|
if len(bz) == 0 {
|
2020-04-23 15:49:25 +00:00
|
|
|
return ethtypes.Bloom{}, fmt.Errorf("block at height %d not found", height)
|
2019-10-04 19:32:56 +00:00
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
return ethtypes.BytesToBloom(bz), nil
|
2019-10-04 19:32:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
// SetBlockBloomMapping sets the mapping from block height to bloom bits
|
|
|
|
func (k Keeper) SetBlockBloomMapping(ctx sdk.Context, bloom ethtypes.Bloom, height int64) {
|
2020-04-01 18:49:21 +00:00
|
|
|
store := ctx.KVStore(k.blockKey)
|
2020-04-23 15:49:25 +00:00
|
|
|
heightBz := sdk.Uint64ToBigEndian(uint64(height))
|
|
|
|
store.Set(types.BloomKey(heightBz), bloom.Bytes())
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:47:39 +00:00
|
|
|
// SetTransactionLogs sets the transaction's logs in the KVStore
|
2020-05-18 19:21:12 +00:00
|
|
|
func (k *Keeper) SetTransactionLogs(ctx sdk.Context, hash []byte, logs []*ethtypes.Log) {
|
2020-04-01 18:49:21 +00:00
|
|
|
store := ctx.KVStore(k.blockKey)
|
2020-05-18 19:21:12 +00:00
|
|
|
bz := k.cdc.MustMarshalBinaryLengthPrefixed(logs)
|
|
|
|
store.Set(types.LogsKey(hash), bz)
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:47:39 +00:00
|
|
|
// GetTransactionLogs gets the logs for a transaction from the KVStore
|
2020-04-01 18:49:21 +00:00
|
|
|
func (k *Keeper) GetTransactionLogs(ctx sdk.Context, hash []byte) ([]*ethtypes.Log, error) {
|
|
|
|
store := ctx.KVStore(k.blockKey)
|
2020-05-18 19:21:12 +00:00
|
|
|
bz := store.Get(types.LogsKey(hash))
|
|
|
|
if len(bz) == 0 {
|
2020-04-01 18:49:21 +00:00
|
|
|
return nil, errors.New("cannot get transaction logs")
|
|
|
|
}
|
|
|
|
|
2020-05-18 19:21:12 +00:00
|
|
|
var logs []*ethtypes.Log
|
|
|
|
k.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &logs)
|
|
|
|
return logs, nil
|
2019-07-24 22:14:12 +00:00
|
|
|
}
|