-
Notifications
You must be signed in to change notification settings - Fork 4
/
keeper.go
188 lines (160 loc) · 6.44 KB
/
keeper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package keeper
import (
"encoding/binary"
"fmt"
"math/big"
"github.com/cosmos/cosmos-sdk/x/bank"
ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/zenchainprotocol/zenchain-node/x/params"
"github.com/zenchainprotocol/zenchain-node/x/evm/types"
"github.com/ethereum/go-ethereum/common"
ethtypes "github.com/ethereum/go-ethereum/core/types"
)
// Keeper wraps the CommitStateDB, allowing us to pass in SDK context while adhering
// to the StateDB interface.
type Keeper struct {
// Amino codec
cdc *codec.Codec
// 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.
storeKey sdk.StoreKey
// Account Keeper for fetching accounts
accountKeeper types.AccountKeeper
paramSpace params.Subspace
supplyKeeper types.SupplyKeeper
bankKeeper bank.Keeper
// 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
Bhash ethcmn.Hash
LogSize uint
}
// NewKeeper generates new evm module keeper
func NewKeeper(
cdc *codec.Codec, storeKey sdk.StoreKey, paramSpace params.Subspace, ak types.AccountKeeper, sk types.SupplyKeeper, bk bank.Keeper,
) *Keeper {
// 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
return &Keeper{
cdc: cdc,
storeKey: storeKey,
accountKeeper: ak,
paramSpace: paramSpace,
supplyKeeper: sk,
bankKeeper: bk,
TxCount: 0,
Bloom: big.NewInt(0),
LogSize: 0,
}
}
// Logger returns a module-specific logger.
func (k Keeper) GenerateCSDBParams() types.CommitStateDBParams {
return types.CommitStateDBParams{
StoreKey: k.storeKey,
ParamSpace: k.paramSpace,
AccountKeeper: k.accountKeeper,
SupplyKeeper: k.supplyKeeper,
BankKeeper: k.bankKeeper,
}
}
// 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))
}
// ----------------------------------------------------------------------------
// Block hash mapping functions
// Required by Web3 API.
// TODO: remove once tendermint support block queries by hash.
// ----------------------------------------------------------------------------
// 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)
bz := store.Get(hash)
if len(bz) == 0 {
return 0, false
}
height := binary.BigEndian.Uint64(bz)
return int64(height), true
}
// 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)
bz := sdk.Uint64ToBigEndian(uint64(height))
store.Set(hash, bz)
}
// ----------------------------------------------------------------------------
// Epoch Height -> hash mapping functions
// Required by EVM context's GetHashFunc
// ----------------------------------------------------------------------------
// GetHeightHash returns the block header hash associated with a given block height and chain epoch number.
func (k Keeper) GetHeightHash(ctx sdk.Context, height uint64) common.Hash {
return types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx).GetHeightHash(height)
}
// SetHeightHash sets the block header hash associated with a given height.
func (k Keeper) SetHeightHash(ctx sdk.Context, height uint64, hash common.Hash) {
types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx).SetHeightHash(height, hash)
}
// ----------------------------------------------------------------------------
// Block bloom bits mapping functions
// Required by Web3 API.
// ----------------------------------------------------------------------------
// GetBlockBloom gets bloombits from block height
func (k Keeper) GetBlockBloom(ctx sdk.Context, height int64) ethtypes.Bloom {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixBloom)
has := store.Has(types.BloomKey(height))
if !has {
return ethtypes.Bloom{}
}
bz := store.Get(types.BloomKey(height))
return ethtypes.BytesToBloom(bz)
}
// 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())
}
// GetAccountStorage return state storage associated with an account
func (k Keeper) GetAccountStorage(ctx sdk.Context, address common.Address) (types.Storage, error) {
storage := types.Storage{}
csdb := types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx)
err := csdb.ForEachStorage(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
}
// 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)
}