forked from goat-systems/go-tezos
/
independent.go
276 lines (231 loc) · 7.38 KB
/
independent.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
package rpc
import (
"encoding/json"
"time"
validator "github.com/go-playground/validator/v10"
"github.com/go-resty/resty/v2"
"github.com/pkg/errors"
)
/*
InjectionOperationInput is the input for the InjectionOperation function.
RPC:
https://tezos.gitlab.io/shell/rpc.html#post-injection-operation
*/
type InjectionOperationInput struct {
// The operation string.
Operation string `validate:"required"`
// If ?async is true, the function returns immediately.
Async bool
// Specify the ChainID.
ChainID string
}
func (i *InjectionOperationInput) contructRPCOptions() []rpcOptions {
var opts []rpcOptions
if i.Async {
opts = append(opts, rpcOptions{
"async",
"true",
})
}
if i.ChainID != "" {
opts = append(opts, rpcOptions{
"chain_id",
i.ChainID,
})
}
return opts
}
/*
InjectionOperation injects an operation in node and broadcast it. Returns the ID of the operation.
The `signedOperationContents` should be constructed using a contextual RPCs from the latest block
and signed by the client. By default, the RPC will wait for the operation to be (pre-)validated
before answering. See RPCs under /blocks/prevalidation for more details on the prevalidation context.
If ?async is true, the function returns immediately. Otherwise, the operation will be validated before
the result is returned. An optional ?chain parameter can be used to specify whether to inject on the
test chain or the main chain.
Path:
/injection/operation (POST)
RPC:
https://tezos.gitlab.io/shell/rpc.html#post-injection-operation
*/
func (c *Client) InjectionOperation(input InjectionOperationInput) (*resty.Response, string, error) {
err := validator.New().Struct(input)
if err != nil {
return nil, "", errors.Wrap(err, "failed to inject operation: invalid input")
}
v, err := json.Marshal(input.Operation)
if err != nil {
return nil, "", errors.Wrap(err, "failed to inject operation")
}
resp, err := c.post("/injection/operation", v, input.contructRPCOptions()...)
if err != nil {
return resp, "", errors.Wrap(err, "failed to inject operation")
}
var opstring string
err = json.Unmarshal(resp.Body(), &opstring)
if err != nil {
return resp, "", errors.Wrap(err, "failed to inject operation: failed to parse json")
}
return resp, opstring, nil
}
func (c *Client) SimulationOperation(input InjectionOperationInput) (*resty.Response, string, error) {
err := validator.New().Struct(input)
if err != nil {
return nil, "", errors.Wrap(err, "failed to simulate operation: invalid input")
}
v, err := json.Marshal(input.Operation)
if err != nil {
return nil, "", errors.Wrap(err, "failed to simulate operation")
}
resp, err := c.post("/chains/main/blocks/head/helpers/scripts/simulate_operation", v, input.contructRPCOptions()...)
if err != nil {
return resp, "", errors.Wrap(err, "failed to simulate operation")
}
var opstring string
err = json.Unmarshal(resp.Body(), &opstring)
if err != nil {
return resp, "", errors.Wrap(err, "failed to simulate operation: failed to parse json")
}
return resp, opstring, nil
}
/*
InjectionBlockInput is the input for the InjectionBlock function.
RPC:
https://tezos.gitlab.io/shell/rpc.html#post-injection-block
*/
type InjectionBlockInput struct {
// Block to inject
Block *Block `validate:"required"`
// If ?async is true, the function returns immediately.
Async bool
// If ?force is true, it will be injected even on non strictly increasing fitness.
Force bool
// Specify the ChainID.
ChainID string
}
func (i *InjectionBlockInput) contructRPCOptions() []rpcOptions {
var opts []rpcOptions
if i.Async {
opts = append(opts, rpcOptions{
"async",
"true",
})
}
if i.Force {
opts = append(opts, rpcOptions{
"force",
"true",
})
}
if i.ChainID != "" {
opts = append(opts, rpcOptions{
"chain_id",
i.ChainID,
})
}
return opts
}
/*
InjectionBlock inject a block in the node and broadcast it. The `operations`
embedded in `blockHeader` might be pre-validated using a contextual RPCs
from the latest block (e.g. '/blocks/head/context/preapply'). Returns the
ID of the block. By default, the RPC will wait for the block to be validated
before answering. If ?async is true, the function returns immediately. Otherwise,
the block will be validated before the result is returned. If ?force is true, it
will be injected even on non strictly increasing fitness. An optional ?chain parameter
can be used to specify whether to inject on the test chain or the main chain.
Path:
/injection/operation (POST)
RPC:
https://tezos.gitlab.io/shell/rpc.html#post-injection-block
*/
func (c *Client) InjectionBlock(input InjectionBlockInput) (*resty.Response, error) {
err := validator.New().Struct(input)
if err != nil {
return nil, errors.Wrap(err, "failed to inject block: invalid input")
}
resp, err := c.post("/injection/block", *input.Block, input.contructRPCOptions()...)
if err != nil {
return resp, errors.Wrap(err, "failed to inject block")
}
return resp, nil
}
/*
Connections is the network connections of a tezos node.
Path:
/network/connections (GET)
RPC:
https://tezos.gitlab.io/shell/rpc.html#get-network-connections
*/
type Connections []struct {
Incoming bool `json:"incoming"`
PeerID string `json:"peer_id"`
IDPoint struct {
Addr string `json:"addr"`
Port int `json:"port"`
} `json:"id_point"`
RemoteSocketPort int `json:"remote_socket_port"`
Versions []struct {
Name string `json:"name"`
Major int `json:"major"`
Minor int `json:"minor"`
} `json:"versions"`
Private bool `json:"private"`
LocalMetadata struct {
DisableMempool bool `json:"disable_mempool"`
PrivateNode bool `json:"private_node"`
} `json:"local_metadata"`
RemoteMetadata struct {
DisableMempool bool `json:"disable_mempool"`
PrivateNode bool `json:"private_node"`
} `json:"remote_metadata"`
}
/*
Connections lists the running P2P connection.
Path:
/network/connections (GET)
RPC:
https://tezos.gitlab.io/shell/rpc.html#get-network-connections
*/
func (c *Client) Connections() (*resty.Response, Connections, error) {
resp, err := c.get("/network/connections")
if err != nil {
return resp, Connections{}, errors.Wrapf(err, "failed to get network connections")
}
var connections Connections
err = json.Unmarshal(resp.Body(), &connections)
if err != nil {
return resp, Connections{}, errors.Wrapf(err, "failed to get network connections: failed to parse json")
}
return resp, connections, nil
}
/*
ActiveChains is the active chains on the tezos network.
RPC:
https://tezos.gitlab.io/shell/rpc.html#get-monitor-active-chains
*/
type ActiveChains []struct {
ChainID string `json:"chain_id"`
TestProtocol string `json:"test_protocol"`
ExpirationDate time.Time `json:"expiration_date"`
Stopping string `json:"stopping"`
}
/*
ActiveChains monitor every chain creation and destruction. Currently active chains will be given as first elements.
Path:
/monitor/active_chains (GET)
RPC:
https://tezos.gitlab.io/shell/rpc.html#get-monitor-active-chains
*/
func (c *Client) ActiveChains() (*resty.Response, ActiveChains, error) {
resp, err := c.get("/monitor/active_chains")
if err != nil {
return nil, ActiveChains{}, errors.Wrap(err, "failed to get active chains")
}
var activeChains ActiveChains
err = json.Unmarshal(resp.Body(), &activeChains)
if err != nil {
return resp, ActiveChains{}, errors.Wrap(err, "failed to get active chains: failed to parse json")
}
return resp, activeChains, nil
}