/
keeper.go
244 lines (206 loc) · 7.14 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package keeper
import (
"encoding/binary"
"fmt"
"path/filepath"
"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"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
wasmvm "github.com/CosmWasm/wasmvm"
"github.com/terra-money/core/x/wasm/config"
"github.com/terra-money/core/x/wasm/types"
)
// Keeper will have a reference to Wasmer with it's own data directory.
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.BinaryCodec
paramSpace paramstypes.Subspace
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
treasuryKeeper types.TreasuryKeeper
serviceRouter types.MsgServiceRouter
queryRouter types.GRPCQueryRouter
wasmVM types.WasmerEngine
querier types.Querier
msgParser types.MsgParser
// WASM config values
wasmConfig *config.Config
}
// NewKeeper creates a new contract Keeper instance
func NewKeeper(
cdc codec.BinaryCodec,
storeKey sdk.StoreKey,
paramspace paramstypes.Subspace,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
treasuryKeeper types.TreasuryKeeper,
serviceRouter types.MsgServiceRouter,
queryRouter types.GRPCQueryRouter,
supportedFeatures string,
homePath string,
wasmConfig *config.Config) Keeper {
// set KeyTable if it has not already been set
if !paramspace.HasKeyTable() {
paramspace = paramspace.WithKeyTable(types.ParamKeyTable())
}
// prevent zero write vm cache
if wasmConfig.ContractMemoryCacheSize == 0 {
wasmConfig.ContractMemoryCacheSize = config.DefaultContractMemoryCacheSize
}
vm, err := wasmvm.NewVM(
filepath.Join(homePath, config.DBDir),
supportedFeatures,
types.ContractMemoryLimit,
wasmConfig.ContractDebugMode,
wasmConfig.ContractMemoryCacheSize,
)
if err != nil {
panic(err)
}
return Keeper{
storeKey: storeKey,
cdc: cdc,
paramSpace: paramspace,
wasmVM: vm,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
treasuryKeeper: treasuryKeeper,
serviceRouter: serviceRouter,
queryRouter: queryRouter,
wasmConfig: wasmConfig,
msgParser: types.NewWasmMsgParser(),
querier: types.NewWasmQuerier(),
}
}
// 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))
}
// GetLastCodeID return last code ID
func (k Keeper) GetLastCodeID(ctx sdk.Context) (uint64, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.LastCodeIDKey)
if bz == nil {
return 0, sdkerrors.Wrap(types.ErrInvalidGenesis, "initial code ID hasn't been set")
}
return binary.BigEndian.Uint64(bz), nil
}
// SetLastCodeID set last code id
func (k Keeper) SetLastCodeID(ctx sdk.Context, id uint64) {
store := ctx.KVStore(k.storeKey)
bz := sdk.Uint64ToBigEndian(id)
store.Set(types.LastCodeIDKey, bz)
}
// GetLastInstanceID return last instance ID
func (k Keeper) GetLastInstanceID(ctx sdk.Context) (uint64, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.LastInstanceIDKey)
if bz == nil {
return 0, sdkerrors.Wrap(types.ErrInvalidGenesis, "initial instance ID hasn't been set")
}
return binary.BigEndian.Uint64(bz), nil
}
// SetLastInstanceID set last instance id
func (k Keeper) SetLastInstanceID(ctx sdk.Context, id uint64) {
store := ctx.KVStore(k.storeKey)
bz := sdk.Uint64ToBigEndian(id)
store.Set(types.LastInstanceIDKey, bz)
}
// GetCodeInfo returns CodeInfo for the given codeID
func (k Keeper) GetCodeInfo(ctx sdk.Context, codeID uint64) (codeInfo types.CodeInfo, err error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetCodeInfoKey(codeID))
if bz == nil {
return types.CodeInfo{}, sdkerrors.Wrapf(types.ErrNotFound, "codeID %d", codeID)
}
k.cdc.MustUnmarshal(bz, &codeInfo)
return
}
// SetCodeInfo stores CodeInfo for the given codeID
func (k Keeper) SetCodeInfo(ctx sdk.Context, codeID uint64, codeInfo types.CodeInfo) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&codeInfo)
store.Set(types.GetCodeInfoKey(codeID), bz)
}
// GetContractInfo returns contract info of the given address
func (k Keeper) GetContractInfo(ctx sdk.Context, contractAddress sdk.AccAddress) (contractInfo types.ContractInfo, err error) {
store := ctx.KVStore(k.storeKey)
contractBz := store.Get(types.GetContractInfoKey(contractAddress))
if contractBz == nil {
return types.ContractInfo{}, sdkerrors.Wrapf(types.ErrNotFound, "constractInfo %s", contractAddress.String())
}
k.cdc.MustUnmarshal(contractBz, &contractInfo)
return contractInfo, nil
}
// SetContractInfo stores ContractInfo for the given contractAddress
func (k Keeper) SetContractInfo(ctx sdk.Context, contractAddress sdk.AccAddress, codeInfo types.ContractInfo) {
store := ctx.KVStore(k.storeKey)
b := k.cdc.MustMarshal(&codeInfo)
store.Set(types.GetContractInfoKey(contractAddress), b)
}
// IterateContractInfo iterates all contract infos
func (k Keeper) IterateContractInfo(ctx sdk.Context, cb func(types.ContractInfo) bool) {
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.ContractInfoKey)
iter := prefixStore.Iterator(nil, nil)
for ; iter.Valid(); iter.Next() {
var contract types.ContractInfo
k.cdc.MustUnmarshal(iter.Value(), &contract)
// cb returns true to stop early
if cb(contract) {
break
}
}
}
// GetContractStoreIterator returns iterator for a contract store
func (k Keeper) GetContractStoreIterator(ctx sdk.Context, contractAddress sdk.AccAddress) sdk.Iterator {
prefixStoreKey := types.GetContractStoreKey(contractAddress)
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
return prefixStore.Iterator(nil, nil)
}
// SetContractStore records all the Models on the contract store
func (k Keeper) SetContractStore(ctx sdk.Context, contractAddress sdk.AccAddress, models []types.Model) {
prefixStoreKey := types.GetContractStoreKey(contractAddress)
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), prefixStoreKey)
for _, model := range models {
prefixStore.Set(model.Key, model.Value)
}
}
// GetByteCode returns ByteCode of the given CodeHash
func (k Keeper) GetByteCode(ctx sdk.Context, codeID uint64) ([]byte, error) {
codeInfo, sdkErr := k.GetCodeInfo(ctx, codeID)
if sdkErr != nil {
return nil, sdkErr
}
byteCode, err := k.wasmVM.GetCode(codeInfo.CodeHash)
if err != nil {
return nil, err
}
return byteCode, nil
}
// RegisterMsgParsers register module msg parsers
func (k *Keeper) RegisterMsgParsers(
parsers map[string]types.WasmMsgParserInterface,
stargateWasmMsgParser types.StargateWasmMsgParserInterface,
) {
for route, parser := range parsers {
k.msgParser.Parsers[route] = parser
}
if stargateWasmMsgParser != nil {
k.msgParser.StargateParser = stargateWasmMsgParser
}
}
// RegisterQueriers register module queriers
func (k *Keeper) RegisterQueriers(
queriers map[string]types.WasmQuerierInterface,
stargateWasmQuerier types.StargateWasmQuerierInterface,
) {
for route, querier := range queriers {
k.querier.Queriers[route] = querier
}
if stargateWasmQuerier != nil {
k.querier.StargateQuerier = stargateWasmQuerier
}
}