-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathiricalls.go
356 lines (325 loc) · 12.3 KB
/
iricalls.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
package api
import (
"github.com/iotaledger/iota.go/checksum"
. "github.com/iotaledger/iota.go/consts"
. "github.com/iotaledger/iota.go/guards"
. "github.com/iotaledger/iota.go/guards/validators"
"github.com/iotaledger/iota.go/pow"
. "github.com/iotaledger/iota.go/trinary"
"strconv"
)
// AddNeighbors adds a list of neighbors to the connected IRI node.
// Assumes addNeighbors command is available on the node.
// AddNeighbors has only a temporary effect until the node relaunches.
func (api *API) AddNeighbors(uris ...string) (int64, error) {
if err := Validate(ValidateURIs(uris...), ValidateNonEmptyStrings(ErrInvalidURI, uris...)); err != nil {
return 0, err
}
cmd := &AddNeighborsCommand{Command: Command{AddNeighborsCmd}, URIs: uris}
rsp := &AddNeighborsResponse{}
if err := api.provider.Send(cmd, rsp); err != nil {
return 0, err
}
return rsp.AddedNeighbors, nil
}
// AttachToTangle performs the Proof-of-Work required to attach a transaction to the Tangle by
// calling the attachToTangle IRI API command. Returns a list of transaction trytes and overwrites the following
// fields: Hash, Nonce, AttachmentTimestamp, AttachmentTimestampLowerBound, AttachmentTimestampUpperBound.
//
// If a Proof-of-Work function is supplied when composing the API, then that function is used
// instead of using the connected node.
func (api *API) AttachToTangle(trunkTxHash Hash, branchTxHash Hash, mwm uint64, trytes []Trytes) ([]Trytes, error) {
if api.localProofOfWorkFunc != nil {
return pow.DoPoW(trunkTxHash, branchTxHash, trytes, mwm, api.localProofOfWorkFunc)
}
if err := Validate(ValidateTransactionTrytes(trytes...)); err != nil {
return nil, err
}
if !IsTransactionHash(trunkTxHash) {
return nil, ErrInvalidTrunkTransaction
}
if !IsTransactionHash(branchTxHash) {
return nil, ErrInvalidBranchTransaction
}
cmd := &AttachToTangleCommand{
TrunkTransaction: trunkTxHash, BranchTransaction: branchTxHash,
Command: Command{AttachToTangleCmd}, Trytes: trytes, MinWeightMagnitude: mwm,
}
rsp := &AttachToTangleResponse{}
if err := api.provider.Send(cmd, rsp); err != nil {
return nil, err
}
return rsp.Trytes, nil
}
// BroadcastTransactions broadcasts a list of attached transaction trytes to the network.
// Tip-selection and Proof-of-Work must be done first by calling
// GetTransactionsToApprove and AttachToTangle or an equivalent attach method.
//
// You may use this method to increase odds of effective transaction propagation.
//
// Persist the transaction trytes in local storage before calling this command for first time, to ensure
// that reattachment is possible, until your bundle has been included.
func (api *API) BroadcastTransactions(trytes ...Trytes) ([]Trytes, error) {
if err := Validate(ValidateAttachedTransactionTrytes(trytes...)); err != nil {
return nil, err
}
cmd := &BroadcastTransactionsCommand{Trytes: trytes, Command: Command{BroadcastTransactionsCmd}}
if err := api.provider.Send(cmd, nil); err != nil {
return nil, err
}
return trytes, nil
}
// CheckConsistency checks if a transaction is consistent or a set of transactions are co-consistent.
//
// Co-consistent transactions and the transactions that they approve (directly or indirectly),
// are not conflicting with each other and the rest of the ledger.
//
// As long as a transaction is consistent, it might be accepted by the network.
// In case a transaction is inconsistent, it will not be accepted and a reattachment
// is required by calling ReplayBundle().
func (api *API) CheckConsistency(hashes ...Hash) (bool, string, error) {
if err := Validate(
ValidateTransactionHashes(hashes...),
ValidateNonEmptyStrings(ErrInvalidTransactionHash, hashes...),
); err != nil {
return false, "", err
}
cmd := &CheckConsistencyCommand{Tails: hashes, Command: Command{CheckConsistencyCmd}}
rsp := &CheckConsistencyResponse{}
if err := api.provider.Send(cmd, rsp); err != nil {
return false, "", err
}
return rsp.State, rsp.Info, nil
}
func validateFindTransactions(query *FindTransactionsQuery) error {
if err := Validate(
ValidateAddresses(false, query.Addresses...),
ValidateHashes(query.Bundles...),
ValidateTransactionHashes(query.Approvees...),
ValidateTags(query.Tags...),
); err != nil {
return err
}
if query.Addresses != nil {
if err := Validate(ValidateNonEmptyStrings(ErrInvalidAddress, query.Addresses...)); err != nil {
return err
}
}
if query.Bundles != nil {
if err := Validate(ValidateNonEmptyStrings(ErrInvalidBundleHash, query.Bundles...)); err != nil {
return err
}
}
if query.Approvees != nil {
if err := Validate(ValidateNonEmptyStrings(ErrInvalidTransactionHash, query.Approvees...)); err != nil {
return err
}
}
if query.Tags != nil {
if err := Validate(ValidateNonEmptyStrings(ErrInvalidTag, query.Tags...)); err != nil {
return err
}
}
return nil
}
// FindTransactions searches for transaction hashes.
// It allows to search for transactions by passing a query object with addresses, bundle hashes, tags and/or approvees fields.
// Multiple query fields are supported and FindTransactions returns the intersection of the results.
func (api *API) FindTransactions(query FindTransactionsQuery) (Hashes, error) {
if err := validateFindTransactions(&query); err != nil {
return nil, err
}
if len(query.Addresses) > 0 {
cleanedAddrs, err := checksum.RemoveChecksums(query.Addresses)
if err != nil {
return nil, err
}
query.Addresses = cleanedAddrs
}
cmd := &FindTransactionsCommand{FindTransactionsQuery: query, Command: Command{FindTransactionsCmd}}
rsp := &FindTransactionsResponse{}
if err := api.provider.Send(cmd, rsp); err != nil {
return nil, err
}
return rsp.Hashes, nil
}
// GetBalances fetches confirmed balances of the given addresses at the latest solid milestone.
func (api *API) GetBalances(addresses Hashes, threshold uint64, tips ...Hash) (*Balances, error) {
if err := Validate(ValidateAddresses(false, addresses...)); err != nil {
return nil, err
}
if threshold > 100 {
return nil, ErrInvalidThreshold
}
cleanedAddrs, err := checksum.RemoveChecksums(addresses)
if err != nil {
return nil, err
}
cmd := &GetBalancesCommand{
Addresses: cleanedAddrs,
Threshold: threshold,
Command: Command{GetBalancesCmd},
Tips: tips,
}
rsp := &GetBalancesResponse{}
if err := api.provider.Send(cmd, rsp); err != nil {
return nil, err
}
balances := &Balances{
Balances: make([]uint64, len(rsp.Balances)),
Milestone: rsp.Milestone, MilestoneIndex: rsp.MilestoneIndex,
}
for i, s := range rsp.Balances {
num, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return nil, err
}
balances.Balances[i] = num
}
return balances, err
}
// GetInclusionStates fetches inclusion states of a given list of transactions.
func (api *API) GetInclusionStates(txHashes Hashes, tips ...Hash) ([]bool, error) {
if err := Validate(
ValidateTransactionHashes(txHashes...),
ValidateNonEmptyStrings(ErrInvalidTransactionHash, txHashes...),
ValidateTransactionHashes(tips...),
ValidateNonEmptyStrings(ErrInvalidTransactionHash, tips...),
); err != nil {
return nil, err
}
cmd := &GetInclusionStatesCommand{Transactions: txHashes, Tips: tips, Command: Command{GetInclusionStatesCmd}}
rsp := &GetInclusionStatesResponse{}
if err := api.provider.Send(cmd, rsp); err != nil {
return nil, err
}
return rsp.States, nil
}
// GetNeighbors returns the list of connected neighbors of the connected node.
func (api *API) GetNeighbors() (Neighbors, error) {
cmd := &GetNeighborsCommand{Command: Command{GetNeighborsCmd}}
rsp := &GetNeighborsResponse{}
err := api.provider.Send(cmd, rsp)
if err != nil {
return nil, err
}
return rsp.Neighbors, nil
}
// GetNodeInfo returns information about the connected node.
func (api *API) GetNodeInfo() (*GetNodeInfoResponse, error) {
cmd := &GetNodeInfoCommand{Command: Command{GetNodeInfoCmd}}
rsp := &GetNodeInfoResponse{}
if err := api.provider.Send(cmd, rsp); err != nil {
return nil, err
}
return rsp, nil
}
// GetTips returns a list of tips (transactions not referenced by other transactions) as seen by the connected node.
func (api *API) GetTips() (Hashes, error) {
cmd := &GetTipsCommand{Command: Command{GetTipsCmd}}
rsp := &GetTipsResponse{}
if err := api.provider.Send(cmd, rsp); err != nil {
return nil, err
}
return rsp.Hashes, nil
}
// GetTransactionsToApprove does the tip selection via the connected node.
//
// Returns a pair of approved transactions which are chosen randomly after validating the transaction trytes,
// the signatures and cross-checking for conflicting transactions.
//
// Tip selection is executed by a Random Walk (RW) starting at random point in the given depth,
// ending up to the pair of selected tips. For more information about tip selection please refer to the
// whitepaper (http://iotatoken.com/IOTA_Whitepaper.pdf).
//
// The reference option allows to select tips in a way that the reference transaction is being approved too.
// This is useful for promoting transactions, for example with PromoteTransaction().
func (api *API) GetTransactionsToApprove(depth uint64, reference ...Hash) (*TransactionsToApprove, error) {
cmd := &GetTransactionsToApproveCommand{Command: Command{GetTransactionsToApproveCmd}, Depth: depth}
if len(reference) > 0 {
if err := Validate(ValidateTransactionHashes(reference...)); err != nil {
return nil, ErrInvalidReferenceHash
}
cmd.Reference = reference[0]
}
rsp := &GetTransactionsToApproveResponse{}
if err := api.provider.Send(cmd, rsp); err != nil {
return nil, err
}
return &rsp.TransactionsToApprove, nil
}
// GetTrytes fetches the transaction trytes given a list of transaction hashes.
func (api *API) GetTrytes(hashes ...Hash) ([]Trytes, error) {
if err := Validate(
ValidateNonEmptyStrings(ErrInvalidTransactionHash, hashes...),
ValidateTransactionHashes(hashes...),
); err != nil {
return nil, err
}
cmd := &GetTrytesCommand{Hashes: hashes, Command: Command{GetTrytesCmd}}
rsp := &GetTrytesResponse{}
if err := api.provider.Send(cmd, rsp); err != nil {
return nil, err
}
return rsp.Trytes, nil
}
// InterruptAttachToTangle interrupts the currently ongoing Proof-of-Work on the connected node.
func (api *API) InterruptAttachToTangle() error {
cmd := &InterruptAttachToTangleCommand{Command: Command{InterruptAttachToTangleCmd}}
return api.provider.Send(cmd, nil)
}
// RemoveNeighbors removes a list of neighbors from the connected IRI node.
// This method has a temporary effect until the IRI node relaunches.
func (api *API) RemoveNeighbors(uris ...string) (int64, error) {
if err := Validate(
ValidateNonEmptyStrings(ErrInvalidURI, uris...),
ValidateURIs(uris...),
); err != nil {
return 0, err
}
cmd := &RemoveNeighborsCommand{Command: Command{RemoveNeighborsCmd}, URIs: uris}
rsp := &RemoveNeighborsResponse{}
if err := api.provider.Send(cmd, rsp); err != nil {
return 0, err
}
return rsp.RemovedNeighbors, nil
}
// StoreTransactions persists a list of attached transaction trytes in the store of the connected node.
// Tip-selection and Proof-of-Work must be done first by calling GetTransactionsToApprove and
// AttachToTangle or an equivalent attach method.
//
// Persist the transaction trytes in local storage before calling this command, to ensure
// reattachment is possible, until your bundle has been included.
//
// Any transactions stored with this command will eventually be erased as a result of a snapshot.
func (api *API) StoreTransactions(trytes ...Trytes) ([]Trytes, error) {
if err := Validate(
ValidateNonEmptyStrings(ErrInvalidTrytes, trytes...),
ValidateAttachedTransactionTrytes(trytes...),
); err != nil {
return nil, err
}
cmd := &StoreTransactionsCommand{Trytes: trytes, Command: Command{StoreTransactionsCmd}}
if err := api.provider.Send(cmd, nil); err != nil {
return nil, err
}
return trytes, nil
}
// WereAddressesSpentFrom checks whether the given addresses were already spent.
func (api *API) WereAddressesSpentFrom(addresses ...Hash) ([]bool, error) {
if err := Validate(
ValidateAddresses(false, addresses...),
); err != nil {
return nil, err
}
cleanedAddrs, err := checksum.RemoveChecksums(addresses)
if err != nil {
return nil, err
}
cmd := &WereAddressesSpentFromCommand{Addresses: cleanedAddrs, Command: Command{WereAddressesSpentFromCmd}}
rsp := &WereAddressesSpentFromResponse{}
if err := api.provider.Send(cmd, rsp); err != nil {
return nil, err
}
return rsp.States, nil
}