-
Notifications
You must be signed in to change notification settings - Fork 0
/
querier.go
449 lines (383 loc) · 14 KB
/
querier.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package keeper
import (
"encoding/json"
"fmt"
"strconv"
apptypes "github.com/brc20-collab/brczero/app/types"
"github.com/brc20-collab/brczero/app/utils"
"github.com/brc20-collab/brczero/libs/cosmos-sdk/codec"
"github.com/brc20-collab/brczero/libs/cosmos-sdk/store/mpt"
sdk "github.com/brc20-collab/brczero/libs/cosmos-sdk/types"
sdkerrors "github.com/brc20-collab/brczero/libs/cosmos-sdk/types/errors"
abci "github.com/brc20-collab/brczero/libs/tendermint/abci/types"
"github.com/brc20-collab/brczero/x/evm/types"
ethcmn "github.com/ethereum/go-ethereum/common"
)
// NewQuerier is the module level router for state queries
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
if len(path) < 1 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 1 parameter is required")
}
switch path[0] {
case types.QueryBalance:
return queryBalance(ctx, path, keeper)
case types.QueryBlockNumber:
return queryBlockNumber(ctx, keeper)
case types.QueryStorage:
return queryStorage(ctx, path, keeper)
case types.QueryStorageProof:
return queryStorageProof(ctx, path, keeper)
case types.QueryStorageRoot:
return queryStorageRootHash(ctx, path, keeper, req.Height)
case types.QueryStorageByKey:
return queryStorageByKey(ctx, path, keeper)
case types.QueryCode:
return queryCode(ctx, path, keeper)
case types.QueryCodeByHash:
return queryCodeByHash(ctx, path, keeper)
case types.QueryHashToHeight:
return queryHashToHeight(ctx, path, keeper)
case types.QueryBloom:
return queryBlockBloom(ctx, path, keeper)
case types.QueryAccount:
return queryAccount(ctx, path, keeper)
case types.QueryExportAccount:
return queryExportAccount(ctx, path, keeper)
case types.QueryParameters:
return queryParams(ctx, keeper)
case types.QueryHeightToHash:
return queryHeightToHash(ctx, path, keeper)
case types.QuerySection:
return querySection(ctx, path, keeper)
case types.QueryContractDeploymentWhitelist:
return queryContractDeploymentWhitelist(ctx, keeper)
case types.QueryContractBlockedList:
return queryContractBlockedList(ctx, keeper)
case types.QueryContractMethodBlockedList:
return queryContractMethodBlockedList(ctx, keeper)
case types.QuerySysContractAddress:
return querySysContractAddress(ctx, keeper)
case types.QueryEthBlockByHeight:
return queryEthBlockByHeight(ctx, path, keeper)
case types.QueryEthBlockByHash:
return queryEthBlockByHash(ctx, path, keeper)
default:
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown query endpoint")
}
}
}
func queryContractMethodBlockedList(ctx sdk.Context, keeper Keeper) (res []byte, err sdk.Error) {
blockedList := types.CreateEmptyCommitStateDB(keeper.GeneratePureCSDBParams(), ctx).GetContractMethodBlockedList()
res, errUnmarshal := codec.MarshalJSONIndent(types.ModuleCdc, blockedList)
if errUnmarshal != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", errUnmarshal.Error()))
}
return res, nil
}
func queryContractBlockedList(ctx sdk.Context, keeper Keeper) (res []byte, err sdk.Error) {
blockedList := types.CreateEmptyCommitStateDB(keeper.GeneratePureCSDBParams(), ctx).GetContractBlockedList()
res, errUnmarshal := codec.MarshalJSONIndent(types.ModuleCdc, blockedList)
if errUnmarshal != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", errUnmarshal.Error()))
}
return res, nil
}
func queryContractDeploymentWhitelist(ctx sdk.Context, keeper Keeper) (res []byte, err sdk.Error) {
whitelist := types.CreateEmptyCommitStateDB(keeper.GeneratePureCSDBParams(), ctx).GetContractDeploymentWhitelist()
res, errUnmarshal := codec.MarshalJSONIndent(types.ModuleCdc, whitelist)
if errUnmarshal != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", errUnmarshal.Error()))
}
return res, nil
}
func queryBalance(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}
addr := ethcmn.HexToAddress(path[1])
balance := keeper.GetBalance(ctx, addr)
balanceStr, err := utils.MarshalBigInt(balance)
if err != nil {
return nil, err
}
res := types.QueryResBalance{Balance: balanceStr}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryBlockNumber(ctx sdk.Context, keeper Keeper) ([]byte, error) {
num := ctx.BlockHeight()
bnRes := types.QueryResBlockNumber{Number: num}
bz, err := codec.MarshalJSONIndent(keeper.cdc, bnRes)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryStorage(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 3 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 3 parameters is required")
}
addr := ethcmn.HexToAddress(path[1])
key := ethcmn.HexToHash(path[2])
val := keeper.GetState(ctx, addr, key)
res := types.QueryResStorage{Value: val.Bytes()}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryStorageProof(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 3 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 3 parameters is required")
}
addr := ethcmn.HexToAddress(path[1])
key := ethcmn.HexToHash(path[2])
val := keeper.GetState(ctx, addr, key)
proofList, err := keeper.GetStorageProof(ctx, addr, key)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInternal, err.Error())
}
res := types.QueryResStorageProof{Value: val.Bytes(), Proof: proofList}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryStorageByKey(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 3 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 3 parameters is required")
}
addr := ethcmn.HexToAddress(path[1])
key := ethcmn.HexToHash(path[2])
val := keeper.GetStateByKey(ctx, addr, key)
res := types.QueryResStorage{Value: val}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryStorageRootHash(ctx sdk.Context, path []string, keeper Keeper, height int64) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 1 parameters is required")
}
addr := ethcmn.HexToAddress(path[1])
acc := keeper.accountKeeper.GetAccount(ctx, addr.Bytes())
if acc == nil {
return nil, fmt.Errorf("get %s storage root hash failed: acc is not exist", addr)
}
return acc.GetStateRoot().Bytes(), nil
}
func queryCode(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}
addr := ethcmn.HexToAddress(path[1])
code := keeper.GetCode(ctx, addr)
res := types.QueryResCode{Code: code}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryCodeByHash(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}
hash := ethcmn.HexToHash(path[1])
code := keeper.GetCodeByHash(ctx, hash)
res := types.QueryResCode{Code: code}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryHashToHeight(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}
blockHash := ethcmn.HexToHash(path[1])
blockNumber, found := keeper.GetBlockHeight(ctx, blockHash)
if !found {
return []byte{}, sdkerrors.Wrap(types.ErrKeyNotFound, fmt.Sprintf("block height not found for hash %s", path[1]))
}
res := types.QueryResBlockNumber{Number: blockNumber}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryBlockBloom(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}
num, err := strconv.ParseInt(path[1], 10, 64)
if err != nil {
return nil, sdkerrors.Wrap(types.ErrStrConvertFailed, fmt.Sprintf("could not unmarshal block height: %s", err))
}
copyCtx := ctx
copyCtx.SetBlockHeight(num)
bloom := keeper.GetBlockBloom(copyCtx, num)
res := types.QueryBloomFilter{Bloom: bloom}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryAccount(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}
addr := ethcmn.HexToAddress(path[1])
res, err := resolveEthAccount(ctx, keeper, addr)
if err != nil {
return nil, err
}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func resolveEthAccount(ctx sdk.Context, k Keeper, addr ethcmn.Address) (*types.QueryResAccount, error) {
codeHash := mpt.EmptyCodeHashBytes
account := k.accountKeeper.GetAccount(ctx, addr.Bytes())
if account == nil {
return &types.QueryResAccount{Nonce: uint64(0), CodeHash: codeHash, Balance: "0x0"}, nil
}
ethAccount := account.(*apptypes.EthAccount)
if ethAccount == nil {
return &types.QueryResAccount{Nonce: uint64(0), CodeHash: codeHash, Balance: "0x0"}, nil
}
// get balance
balance := ethAccount.Balance(sdk.DefaultBondDenom).BigInt()
if balance == nil {
balance = sdk.ZeroInt().BigInt()
}
balanceStr, err := utils.MarshalBigInt(balance)
if err != nil {
return nil, err
}
// get codeHash
if ethAccount.CodeHash != nil {
codeHash = ethAccount.CodeHash
}
//return
return &types.QueryResAccount{
Balance: balanceStr,
CodeHash: codeHash,
Nonce: ethAccount.Sequence,
}, nil
}
func queryExportAccount(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}
hexAddress := path[1]
addr := ethcmn.HexToAddress(hexAddress)
var storage types.Storage
err := keeper.ForEachStorage(ctx, addr, func(key, value ethcmn.Hash) bool {
storage = append(storage, types.NewState(key, value))
return false
})
if err != nil {
return nil, err
}
res := types.GenesisAccount{
Address: hexAddress,
Code: keeper.GetCode(ctx, addr),
Storage: storage,
}
// TODO: codec.MarshalJSONIndent doesn't call the String() method of types properly
bz, err := json.MarshalIndent(res, "", "\t")
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryParams(ctx sdk.Context, keeper Keeper) (res []byte, err sdk.Error) {
params := keeper.GetParams(ctx)
res, errUnmarshal := codec.MarshalJSONIndent(types.ModuleCdc, params)
if errUnmarshal != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal result to JSON", errUnmarshal.Error()))
}
return res, nil
}
func queryHeightToHash(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}
height, err := strconv.Atoi(path[1])
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, params[1] convert to int failed")
}
hash := keeper.GetHeightHash(ctx, uint64(height))
return hash.Bytes(), nil
}
func querySection(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if !types.GetEnableBloomFilter() {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"disable bloom filter")
}
if len(path) != 1 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"wrong parameters, need no parameters")
}
res, err := json.Marshal(types.GetIndexer().StoredSection())
if err != nil {
return nil, err
}
return res, nil
}
func queryEthBlockByHeight(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}
height, err := strconv.Atoi(path[1])
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, params[1] convert to int failed")
}
res, found := keeper.GetEthBlockBytesByHeight(ctx, uint64(height))
if !found {
return nil, fmt.Errorf("not found block by heith(%d)", height)
}
return res, nil
}
func queryEthBlockByHash(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
if len(path) < 2 {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
"Insufficient parameters, at least 2 parameters is required")
}
blockHash := ethcmn.HexToHash(path[1])
res, found := keeper.GetEthBlockBytesByHash(ctx, blockHash.Bytes())
if !found {
return nil, fmt.Errorf("not found block by hash(%s)", blockHash.Hex())
}
return res, nil
}