-
Notifications
You must be signed in to change notification settings - Fork 404
/
legacy_querier.go
153 lines (142 loc) · 4.96 KB
/
legacy_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
package keeper
import (
"encoding/json"
"reflect"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/CosmWasm/wasmd/x/wasm/types"
)
const (
QueryListContractByCode = "list-contracts-by-code"
QueryGetContract = "contract-info"
QueryGetContractState = "contract-state"
QueryGetCode = "code"
QueryListCode = "list-code"
QueryContractHistory = "contract-history"
)
const (
QueryMethodContractStateSmart = "smart"
QueryMethodContractStateAll = "all"
QueryMethodContractStateRaw = "raw"
)
// NewLegacyQuerier creates a new querier
func NewLegacyQuerier(keeper types.ViewKeeper, gasLimit sdk.Gas) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
var (
rsp interface{}
err error
)
switch path[0] {
case QueryGetContract:
addr, addrErr := sdk.AccAddressFromBech32(path[1])
if addrErr != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, addrErr.Error())
}
rsp, err = queryContractInfo(ctx, addr, keeper)
case QueryListContractByCode:
codeID, parseErr := strconv.ParseUint(path[1], 10, 64)
if parseErr != nil {
return nil, sdkerrors.Wrapf(types.ErrInvalid, "code id: %s", parseErr.Error())
}
rsp = queryContractListByCode(ctx, codeID, keeper)
case QueryGetContractState:
if len(path) < 3 {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown data query endpoint")
}
return queryContractState(ctx, path[1], path[2], req.Data, gasLimit, keeper)
case QueryGetCode:
codeID, parseErr := strconv.ParseUint(path[1], 10, 64)
if parseErr != nil {
return nil, sdkerrors.Wrapf(types.ErrInvalid, "code id: %s", parseErr.Error())
}
rsp, err = queryCode(ctx, codeID, keeper)
case QueryListCode:
rsp, err = queryCodeList(ctx, keeper)
case QueryContractHistory:
contractAddr, addrErr := sdk.AccAddressFromBech32(path[1])
if addrErr != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, addrErr.Error())
}
rsp, err = queryContractHistory(ctx, contractAddr, keeper)
default:
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown data query endpoint")
}
if err != nil {
return nil, err
}
if rsp == nil || reflect.ValueOf(rsp).IsNil() {
return nil, nil
}
bz, err := json.MarshalIndent(rsp, "", " ")
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
}
func queryContractState(ctx sdk.Context, bech, queryMethod string, data []byte, gasLimit sdk.Gas, keeper types.ViewKeeper) (json.RawMessage, error) {
contractAddr, err := sdk.AccAddressFromBech32(bech)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, bech)
}
switch queryMethod {
case QueryMethodContractStateAll:
resultData := make([]types.Model, 0)
// this returns a serialized json object (which internally encoded binary fields properly)
keeper.IterateContractState(ctx, contractAddr, func(key, value []byte) bool {
resultData = append(resultData, types.Model{Key: key, Value: value})
return false
})
bz, err := json.Marshal(resultData)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
case QueryMethodContractStateRaw:
// this returns the raw data from the state, base64-encoded
return keeper.QueryRaw(ctx, contractAddr, data), nil
case QueryMethodContractStateSmart:
// we enforce a subjective gas limit on all queries to avoid infinite loops
ctx = ctx.WithGasMeter(sdk.NewGasMeter(gasLimit))
msg := types.RawContractMessage(data)
if err := msg.ValidateBasic(); err != nil {
return nil, sdkerrors.Wrap(err, "json msg")
}
// this returns raw bytes (must be base64-encoded)
bz, err := keeper.QuerySmart(ctx, contractAddr, msg)
return bz, err
default:
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, queryMethod)
}
}
func queryCodeList(ctx sdk.Context, keeper types.ViewKeeper) ([]types.CodeInfoResponse, error) {
var info []types.CodeInfoResponse
keeper.IterateCodeInfos(ctx, func(i uint64, res types.CodeInfo) bool {
info = append(info, types.CodeInfoResponse{
CodeID: i,
Creator: res.Creator,
DataHash: res.CodeHash,
InstantiatePermission: res.InstantiateConfig,
})
return false
})
return info, nil
}
func queryContractHistory(ctx sdk.Context, contractAddr sdk.AccAddress, keeper types.ViewKeeper) ([]types.ContractCodeHistoryEntry, error) {
history := keeper.GetContractHistory(ctx, contractAddr)
// redact response
for i := range history {
history[i].Updated = nil
}
return history, nil
}
func queryContractListByCode(ctx sdk.Context, codeID uint64, keeper types.ViewKeeper) []string {
var contracts []string
keeper.IterateContractsByCode(ctx, codeID, func(addr sdk.AccAddress) bool {
contracts = append(contracts, addr.String())
return false
})
return contracts
}