-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
261 lines (242 loc) · 8.82 KB
/
query.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
package rest
import (
"encoding/json"
"fmt"
sdk "github.com/ci123chain/ci123chain/pkg/abci/types"
sdkerrors "github.com/ci123chain/ci123chain/pkg/abci/types/errors"
"github.com/ci123chain/ci123chain/pkg/abci/types/rest"
"github.com/ci123chain/ci123chain/pkg/account/exported"
"github.com/ci123chain/ci123chain/pkg/account/keeper"
types2 "github.com/ci123chain/ci123chain/pkg/account/types"
types3 "github.com/ci123chain/ci123chain/pkg/app/types"
"github.com/ci123chain/ci123chain/pkg/client"
"github.com/ci123chain/ci123chain/pkg/client/context"
evm "github.com/ci123chain/ci123chain/pkg/vm/evmtypes"
"github.com/ci123chain/ci123chain/pkg/vm/moduletypes"
"github.com/ci123chain/ci123chain/pkg/vm/moduletypes/utils"
"github.com/ci123chain/ci123chain/pkg/vm/wasmtypes"
ethcmn "github.com/ethereum/go-ethereum/common"
"math/big"
"net/http"
"strconv"
)
const DefaultRPCGasLimit = 10000000
func queryCodeHandlerFn(cliCtx context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
height := r.FormValue("height")
prove := r.FormValue("prove")
codeHash := r.FormValue("code_hash")
cliCtx, ok, Err := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r, "")
if !ok || Err != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrInternal, "get clictx failed").Error())
return
}
if !rest.CheckHeightAndProve(w, height, prove, moduletypes.DefaultCodespace) {
return
}
params := types.NewQueryCodeInfoParams(codeHash)
bz, Er := cliCtx.Cdc.MarshalJSON(params)
if Er != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrInternal, fmt.Sprintf("cdc marshal failed: %v", Er.Error())).Error())
return
}
isProve := false
if prove == "true" {
isProve = true
}
res, _, proof, Err := cliCtx.Query("/custom/" + moduletypes.ModuleName + "/" + types.QueryCodeInfo, bz, isProve)
if Err != nil {
rest.WriteErrorRes(w, Err.Error())
return
}
if len(res) < 1 {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrResponse, fmt.Sprintf("unexpected res: %v", res)).Error())
return
}
var codeInfo types.CodeInfo
cliCtx.Cdc.MustUnmarshalBinaryBare(res, &codeInfo)
value := codeInfo
resp := rest.BuildQueryRes(height, isProve, value, proof)
rest.PostProcessResponseBare(w, cliCtx, resp)
}
}
func listContractsByCodeHandlerFn(cliCtx context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
height := r.FormValue("height")
prove := r.FormValue("prove")
accountAddr := r.FormValue("account_address")
accountAddress := sdk.HexToAddress(accountAddr)
params := types.NewContractListParams(accountAddress)
bz, Er := cliCtx.Cdc.MarshalJSON(params)
if Er != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrInternal, "cdc marshal failed").Error())
return
}
if !rest.CheckHeightAndProve(w, height, prove, sdkerrors.RootCodespace) {
return
}
isProve := false
if prove == "true" {
isProve = true
}
res, _, _, err := cliCtx.Query("/custom/" + moduletypes.ModuleName + "/" + types.QueryContractList, bz, isProve)
if err != nil {
rest.WriteErrorRes(w, err.Error())
return
}
if len(res) < 1 {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrResponse, fmt.Sprintf("unexpected res: %v", res)).Error())
return
}
var contractList types.ContractListResponse
cliCtx.Cdc.MustUnmarshalBinaryBare(res, &contractList)
rest.PostProcessResponseBare(w, cliCtx, contractList)
}
}
func queryContractHandlerFn(cliCtx context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
height := r.FormValue("height")
prove := r.FormValue("prove")
contractAddr := r.FormValue("contract_address")
contractAddress := sdk.HexToAddress(contractAddr)
cliCtx, ok, err := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r, "")
if !ok || err != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrInternal, "get clictx failed").Error())
return
}
if !rest.CheckHeightAndProve(w, height, prove, sdkerrors.RootCodespace) {
return
}
params := types.NewQueryContractInfoParams(contractAddress)
bz, Er := cliCtx.Cdc.MarshalJSON(params)
if Er != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrInternal, "cdc marshal failed").Error())
return
}
isProve := false
if prove == "true" {
isProve = true
}
res, _, proof, err := cliCtx.Query("/custom/" + moduletypes.ModuleName + "/" + types.QueryContractInfo, bz, isProve)
if err != nil {
rest.WriteErrorRes(w, err.Error())
return
}
if len(res) < 1 {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrResponse, fmt.Sprintf("unexpected res: %v", res)).Error())
return
}
var contractInfo types.ContractInfo
cliCtx.Cdc.MustUnmarshalBinaryBare(res, &contractInfo)
value := contractInfo
resp := rest.BuildQueryRes(height, isProve, value, proof)
rest.PostProcessResponseBare(w, cliCtx, resp)
}
}
func queryContractStateAllHandlerFn(cliCtx context.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
contractAddr := r.FormValue("contract_address")
contractAddress := sdk.HexToAddress(contractAddr)
qparams := keeper.NewQueryAccountParams(contractAddress, -1)
bz, err := cliCtx.Cdc.MarshalJSON(qparams)
if err != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrInternal, "cdc marshal failed").Error())
return
}
ctx, err2 := client.NewClientContextFromViper(cdc)
if err2 != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrParams, fmt.Sprintf("new context failed: %v", err2.Error())).Error())
return
}
var fromAddr sdk.AccAddress
var nonce uint64
from := r.FormValue("from")
if from != "" {
fromAddr = sdk.HexToAddress(from)
nonce, _, err = ctx.GetNonceByAddress(fromAddr, false)
if err != nil {
return
}
}
var gas uint64
gasStr := r.FormValue("gas")
if gasStr != "" {
gas, err = strconv.ParseUint(gasStr, 10, 64)
if err != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrParams, fmt.Sprintf("invalid gas: %v", err.Error())).Error())
return
}
} else {
gas = uint64(DefaultRPCGasLimit)
}
var msg sdk.Msg
var args utils.CallData
args_str := r.FormValue("calldata")
if args_str == "" {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrParams, "invalid calldata").Error())
return
}else {
err := json.Unmarshal([]byte(args_str), &args)
if err != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, fmt.Sprintf("json unmarshal calldata failed: %v", err.Error())).Error())
return
}
}
queryRes, _, _, err := cliCtx.Query("/custom/" + types2.ModuleName + "/" + types2.QueryAccount, bz, false)
if err != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrInternal, fmt.Sprintf("query contract account failed:%v", err.Error())).Error())
return
}
if queryRes == nil{
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrInternal, "contract account does not exist").Error())
return
}
var acc exported.Account
err2 = cliCtx.Cdc.UnmarshalBinaryLengthPrefixed(queryRes, &acc)
if err2 != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrInternal, fmt.Sprintf("unmarshal query response to account failed:%v", err2.Error())).Error())
return
}
if acc.GetContractType() == types2.WasmContractType {
msg = types.NewMsgExecuteContract(fromAddr, contractAddress, args)
} else if acc.GetContractType() == types2.EvmContractType {
var to *ethcmn.Address
to_addr := ethcmn.HexToAddress(contractAddr)
to = &to_addr
amount_str := r.FormValue("amount")
amount := new(big.Int)
if len(amount_str) < 2 {
amount.SetString(amount_str, 10)
} else if amount_str[:2] == "0x" {
amount.SetString(amount_str[2:], 16)
} else {
amount.SetString(amount_str, 10)
}
payload, err := evm.EVMEncode(args)
if err != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrParams, fmt.Sprintf("encode evm callData failed: %v", err.Error())).Error())
return
}
msg = evm.NewMsgEvmTx(fromAddr, nonce, to, amount, gas, big.NewInt(1), payload)
} else {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrParams, "not contract account").Error())
return
}
txBytes, err := cdc.MarshalBinaryBare(types3.NewCommonTx(fromAddr, nonce, gas, []sdk.Msg{msg}))
if err != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrInternal, fmt.Sprintf("cdc marshal failed: %v", err.Error())).Error())
return
}
res, _, _, err := cliCtx.Query("app/simulate", txBytes, false)
if err != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrInternal, fmt.Sprintf("unexpected res: %v", err.Error())).Error())
return
}
var simResponse sdk.QureyAppResponse
if err := cdc.UnmarshalBinaryBare(res, &simResponse); err != nil {
rest.WriteErrorRes(w, sdkerrors.Wrap(sdkerrors.ErrInternal, fmt.Sprintf("cdc unmarshal failed: %v", err.Error())).Error())
return
}
rest.PostProcessResponseBare(w, cliCtx, simResponse)
}
}