Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ignite
GitHub Repository: ignite/cli
Path: blob/main/integration/msgs.go
1007 views
1
package envtest
2
3
import (
4
"bytes"
5
"context"
6
"encoding/json"
7
"fmt"
8
"io"
9
"net/http"
10
"net/url"
11
"strings"
12
"time"
13
14
"github.com/stretchr/testify/require"
15
16
"github.com/ignite/cli/v29/ignite/pkg/cmdrunner/step"
17
"github.com/ignite/cli/v29/ignite/pkg/errors"
18
"github.com/ignite/cli/v29/ignite/pkg/gomodulepath"
19
"github.com/ignite/cli/v29/ignite/pkg/xurl"
20
)
21
22
const defaultRequestTimeout = 90 * time.Second
23
24
type TxResponse struct {
25
Code int `json:"code"`
26
Codespace string `json:"codespace"`
27
RawLog string `json:"raw_log"`
28
TxHash string `json:"txhash"`
29
Height string `json:"height"`
30
Data string `json:"data"`
31
Info string `json:"info"`
32
GasWanted string `json:"gas_wanted"`
33
GasUsed string `json:"gas_used"`
34
Timestamp string `json:"timestamp"`
35
}
36
37
func (a App) CLITx(chainRPC, module, method string, args ...string) TxResponse {
38
nodeAddr, err := xurl.TCP(chainRPC)
39
require.NoErrorf(a.env.T(), err, "cant read nodeAddr from host.RPC %v", chainRPC)
40
41
args = append(args,
42
"--node", nodeAddr,
43
"--home", a.homePath,
44
"--from", "alice",
45
"--output", "json",
46
"--log_format", "json",
47
"--keyring-backend", "test",
48
"--yes",
49
)
50
var (
51
output = &bytes.Buffer{}
52
outErr = &bytes.Buffer{}
53
txResponse = TxResponse{}
54
)
55
stepsTx := step.NewSteps(
56
step.New(
57
step.Stdout(output),
58
step.Stderr(outErr),
59
step.PreExec(func() error {
60
output.Reset()
61
outErr.Reset()
62
return nil
63
}),
64
step.Exec(
65
a.Binary(),
66
append([]string{"tx", module, method}, args...)...,
67
),
68
step.PostExec(func(execErr error) error {
69
if execErr != nil {
70
return execErr
71
}
72
if outErr.Len() > 0 {
73
return errors.Errorf("error executing request: %s", outErr.String())
74
}
75
output := output.Bytes()
76
if err := json.Unmarshal(output, &txResponse); err != nil {
77
return errors.Errorf("unmarshalling tx response error: %w, response: %s", err, string(output))
78
}
79
return nil
80
}),
81
))
82
83
ctx, cancel := context.WithTimeout(a.env.Ctx(), defaultRequestTimeout)
84
defer cancel()
85
86
if !a.env.Exec("sending chain request "+args[0], stepsTx, ExecRetry(), ExecCtx(ctx)) {
87
cancel()
88
a.env.t.FailNow()
89
}
90
91
return txResponse
92
}
93
94
func (a App) CLIQueryTx(chainRPC, txHash string) (txResponse TxResponse) {
95
output := a.query(chainRPC, "tx", txHash)
96
err := json.Unmarshal(output, &txResponse)
97
require.NoError(a.env.T(), err, "unmarshalling tx response: %s", string(output))
98
return txResponse
99
}
100
101
func (a App) CLIQuery(chainRPC, module, method string, args ...string) []byte {
102
return a.query(chainRPC, module, method, args...)
103
}
104
105
func (a App) query(chainRPC, module, method string, args ...string) []byte {
106
nodeAddr, err := xurl.TCP(chainRPC)
107
require.NoErrorf(a.env.T(), err, "cant read nodeAddr from host.RPC %v", chainRPC)
108
109
var (
110
output = &bytes.Buffer{}
111
outErr = &bytes.Buffer{}
112
)
113
114
cmd := append([]string{"query", module, method}, args...)
115
cmd = append(cmd,
116
"--node", nodeAddr,
117
"--home", a.homePath,
118
"--output", "json",
119
"--log_format", "json",
120
)
121
steps := step.NewSteps(
122
step.New(
123
step.Stdout(output),
124
step.Stderr(outErr),
125
step.PreExec(func() error {
126
output.Reset()
127
outErr.Reset()
128
return nil
129
}),
130
step.Exec(a.Binary(), cmd...),
131
step.PostExec(func(execErr error) error {
132
if execErr != nil {
133
return execErr
134
}
135
if outErr.Len() > 0 {
136
return errors.Errorf("error executing request: %s", outErr.String())
137
}
138
return nil
139
}),
140
))
141
142
if !a.env.Exec(fmt.Sprintf("fetching query data %s => %s", module, method), steps, ExecRetry()) {
143
a.env.t.FailNow()
144
}
145
146
return output.Bytes()
147
}
148
149
func (a App) APIQuery(ctx context.Context, chainAPI, namespace, module, method string, args ...string) []byte {
150
ctx, cancel := context.WithTimeout(ctx, defaultRequestTimeout)
151
defer cancel()
152
153
chainAPI, err := xurl.HTTP(chainAPI)
154
require.NoErrorf(a.env.T(), err, "failed to convert chain API %s to HTTP", chainAPI)
155
156
modulePath := gomodulepath.ExtractAppPath(namespace)
157
apiURL, err := url.JoinPath(chainAPI, modulePath, module, "v1", method, strings.Join(args, "/"))
158
require.NoErrorf(a.env.T(), err, "failed to create API URL")
159
160
req, err := http.NewRequestWithContext(ctx, "GET", apiURL, nil)
161
require.NoErrorf(a.env.T(), err, "failed to create HTTP request")
162
163
req.Header.Set("Accept", "application/json")
164
165
resp, err := http.DefaultClient.Do(req)
166
require.NoErrorf(a.env.T(), err, "failed to execute HTTP request")
167
defer resp.Body.Close()
168
169
if resp.StatusCode != http.StatusOK {
170
require.Failf(a.env.T(), "unexpected status code", "expected 200 OK, got %d", resp.StatusCode)
171
}
172
173
body, err := io.ReadAll(resp.Body)
174
require.NoErrorf(a.env.T(), err, "failed to read response body")
175
176
return body
177
}
178
179