forked from goat-systems/go-tezos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructs.go
561 lines (493 loc) · 18.8 KB
/
structs.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
package gotezos
import (
"encoding/json"
"log"
"math/rand"
"sync"
"time"
gocache "github.com/patrickmn/go-cache"
)
// MUTEZ is a helper for balance devision
const MUTEZ = 1000000
// ResponseRaw represents a raw RPC/HTTP response
type ResponseRaw struct {
Bytes []byte
}
// NetworkVersion represents the network version returned by the Tezos network.
type NetworkVersion struct {
Name string `json:"name"`
Major int `json:"major"`
Minor int `json:"minor"`
Network string // Human readable network name
}
// NetworkVersions is an array of NetworkVersion
type NetworkVersions []NetworkVersion
// NetworkConstants represents the network constants returned by the Tezos network.
type NetworkConstants struct {
ProofOfWorkNonceSize int `json:"proof_of_work_nonce_size"`
NonceLength int `json:"nonce_length"`
MaxRevelationsPerBlock int `json:"max_revelations_per_block"`
MaxOperationDataLength int `json:"max_operation_data_length"`
MaxProposalsPerDelegate int `json:"max_proposals_per_delegate"`
PreservedCycles int `json:"preserved_cycles"`
BlocksPerCycle int `json:"blocks_per_cycle"`
BlocksPerCommitment int `json:"blocks_per_commitment"`
BlocksPerRollSnapshot int `json:"blocks_per_roll_snapshot"`
BlocksPerVotingPeriod int `json:"blocks_per_voting_period"`
TimeBetweenBlocks []string `json:"time_between_blocks"`
EndorsersPerBlock int `json:"endorsers_per_block"`
HardGasLimitPerOperation string `json:"hard_gas_limit_per_operation"`
HardGasLimitPerBlock string `json:"hard_gas_limit_per_block"`
ProofOfWorkThreshold string `json:"proof_of_work_threshold"`
TokensPerRoll string `json:"tokens_per_roll"`
MichelsonMaximumTypeSize int `json:"michelson_maximum_type_size"`
SeedNonceRevelationTip string `json:"seed_nonce_revelation_tip"`
OriginationSize int `json:"origination_size"`
BlockSecurityDeposit string `json:"block_security_deposit"`
EndorsementSecurityDeposit string `json:"endorsement_security_deposit"`
BlockReward string `json:"block_reward"`
EndorsementReward string `json:"endorsement_reward"`
CostPerByte string `json:"cost_per_byte"`
HardStorageLimitPerOperation string `json:"hard_storage_limit_per_operation"`
}
// Block is a block returned by the Tezos RPC API.
type Block struct {
Protocol string `json:"protocol"`
ChainID string `json:"chain_id"`
Hash string `json:"hash"`
Header StructHeader `json:"header"`
Metadata StructMetadata `json:"metadata"`
Operations [][]StructOperations `json:"operations"`
}
// StructHeader is a header in a block returned by the Tezos RPC API.
type StructHeader struct {
Level int `json:"level"`
Proto int `json:"proto"`
Predecessor string `json:"Predecessor"`
Timestamp time.Time `json:"timestamp"`
ValidationPass int `json:"validation_pass"`
OperationsHash string `json:"operations_hash"`
Fitness []string `json:"fitness"`
Context string `json:"context"`
Priority int `json:"priority"`
ProofOfWorkNonce string `json:"proof_of_work_nonce"`
Signature string `json:"signature"`
}
// StructMetadata is the Metadata in a block returned by the Tezos RPC API.
type StructMetadata struct {
Protocol string `json:"protocol"`
NextProtocol string `json:"next_protocol"`
TestChainStatus StructTestChainStatus `json:"test_chain_status"`
MaxOperationsTTL int `json:"max_operations_ttl"`
MaxOperationDataLength int `json:"max_operation_data_length"`
MaxBlockHeaderLength int `json:"max_block_header_length"`
MaxOperationListLength []StructMaxOperationListLength `json:"max_operation_list_length"`
Baker string `json:"baker"`
Level StructLevel `json:"level"`
VotingPeriodKind string `json:"voting_period_kind"`
NonceHash interface{} `json:"nonce_hash"`
ConsumedGas string `json:"consumed_gas"`
Deactivated []string `json:"deactivated"`
BalanceUpdates []StructBalanceUpdates `json:"balance_updates"`
}
// StructTestChainStatus is the TestChainStatus found in the Metadata of a block returned by the Tezos RPC API.
type StructTestChainStatus struct {
Status string `json:"status"`
}
// StructMaxOperationListLength is the MaxOperationListLength found in the Metadata of a block returned by the Tezos RPC API.
type StructMaxOperationListLength struct {
MaxSize int `json:"max_size"`
MaxOp int `json:"max_op,omitempty"`
}
// StructLevel the Level found in the Metadata of a block returned by the Tezos RPC API.
type StructLevel struct {
Level int `json:"level"`
LevelPosition int `json:"level_position"`
Cycle int `json:"cycle"`
CyclePosition int `json:"cycle_position"`
VotingPeriod int `json:"voting_period"`
VotingPeriodPosition int `json:"voting_period_position"`
ExpectedCommitment bool `json:"expected_commitment"`
}
// StructBalanceUpdates is the BalanceUpdates found in the Metadata of a block returned by the Tezos RPC API.
type StructBalanceUpdates struct {
Kind string `json:"kind"`
Contract string `json:"contract,omitempty"`
Change string `json:"change"`
Category string `json:"category,omitempty"`
Delegate string `json:"delegate,omitempty"`
Level int `json:"level,omitempty"`
}
// StructOperations is the Operations found in a block returned by the Tezos RPC API.
type StructOperations struct {
Protocol string `json:"protocol"`
ChainID string `json:"chain_id"`
Hash string `json:"hash"`
Branch string `json:"branch"`
Contents []StructContents `json:"contents"`
Signature string `json:"signature"`
}
// StructContents is the Contents found in a operation of a block returned by the Tezos RPC API.
type StructContents struct {
Kind string `json:"kind"`
Source string `json:"source"`
Fee string `json:"fee"`
Counter string `json:"counter"`
GasLimit string `json:"gas_limit"`
StorageLimit string `json:"storage_limit"`
Amount string `json:"amount"`
Destination string `json:"destination"`
Delegate string `json:"delegate"`
Phk string `json:"phk"`
Secret string `json:"secret"`
Level int `json:"level"`
ManagerPublicKey string `json:"managerPubkey"`
Balance string `json:"balance"`
Metadata ContentsMetadata `json:"metadata"`
}
// ContentsMetadata is the Metadata found in the Contents in a operation of a block returned by the Tezos RPC API.
type ContentsMetadata struct {
BalanceUpdates []StructBalanceUpdates `json:"balance_updates"`
Slots []int `json:"slots"`
OperationResult OperationResult `json:"operation_result"`
}
// OperationResult is the metadata OperationResult found in the Contents in a operation of a block returned by the Tezos RPC API.
type OperationResult struct {
Status string `json:"status"`
}
// SnapShot is a SnapShot on the Tezos Network.
type SnapShot struct {
Cycle int
Number int
AssociatedHash string
AssociatedBlock int
}
// SnapShotQuery is a SnapShot returned by the Tezos RPC API.
type SnapShotQuery struct {
RandomSeed string `json:"random_seed"`
RollSnapShot int `json:"roll_snapshot"`
}
// FrozenBalanceRewards is a FrozenBalanceRewards query returned by the Tezos RPC API.
type FrozenBalanceRewards struct {
Deposits string `json:"deposits"`
Fees string `json:"fees"`
Rewards string `json:"rewards"`
}
// TransOp is a helper structure to build out a transfer operation to post to the Tezos RPC
type TransOp struct {
Kind string `json:"kind"`
Amount string `json:"amount"`
Source string `json:"source"`
Destination string `json:"destination"`
StorageLimit string `json:"storage_limit"`
GasLimit string `json:"gas_limit"`
Fee string `json:"fee"`
Counter string `json:"counter"`
Parameters MichelineExpr `json:"parameters"`
}
// Conts is helper structure to build out the contents of a a transfer operation to post to the Tezos RPC
type Conts struct {
Contents []TransOp `json:"contents"`
Branch string `json:"branch"`
}
// Transfer a complete transfer request
type Transfer struct {
Conts
Protocol string `json:"protocol"`
Signature string `json:"signature"`
}
// Delegate is representation of a delegate on the Tezos Network
type Delegate struct {
Balance string `json:"balance"`
FrozenBalance string `json:"frozen_balance"`
FrozenBalanceByCycle []FrozenBalanceByCycle `json:"frozen_balance_by_cycle"`
StakingBalance string `json:"staking_balance"`
DelegateContracts []string `json:"delegated_contracts"`
DelegatedBalance string `json:"delegated_balance"`
Deactivated bool `json:"deactivated"`
GracePeriod int `json:"grace_period"`
}
// FrozenBalanceByCycle a representation of frozen balance by cycle on the Tezos network
type FrozenBalanceByCycle struct {
Cycle int `json:"cycle"`
Deposit string `json:"deposit"`
Fees string `json:"fees"`
Rewards string `json:"rewards"`
}
// FrozenBalance is representation of frozen balance on the Tezos network
type FrozenBalance struct {
Deposits string `json:"deposits"`
Fees string `json:"fees"`
Rewards string `json:"rewards"`
}
// BakingRights a representation of baking rights on the Tezos network
type BakingRights []struct {
Level int `json:"level"`
Delegate string `json:"delegate"`
Priority int `json:"priority"`
EstimatedTime time.Time `json:"estimated_time"`
}
// EndorsingRights is a representation of endorsing rights on the Tezos network
type EndorsingRights []struct {
Level int `json:"level"`
Delegate string `json:"delegate"`
Slots []int `json:"slots"`
EstimatedTime time.Time `json:"estimated_time"`
}
type DelegateReport struct {
DelegatePhk string
Cycle int
Delegations []DelegationReport
CycleRewards string
TotalFeeRewards string
SelfBakedRewards string
TotalRewards string
}
type DelegationReport struct {
DelegationPhk string
Share float64
GrossRewards string
Fee string
NetRewards string
}
// BRights is a structure representing baking rights for a specific delegate between cycles
type BRights struct {
Delegate string `json:"delegate"`
Cycles []BCycles `json:"cycles"`
}
// ERights is a structure representing endorsing rights for a specific delegate between cycles
type ERights struct {
Delegate string `json:"delegate"`
Cycles []ECycles `json:"cycles"`
}
// BCycles is a structure representing the baking rights in a specific cycle
type BCycles struct {
Cycle int `json:"cycle"`
BakingRights BakingRights `json:"baking_rights"`
}
// ECycles is a structure representing the endorsing rights in a specific cycle
type ECycles struct {
Cycle int `json:"cycle"`
EndorsingRights EndorsingRights `json:"endorsing_rights"`
}
// Payment is a helper struct for transfers
type Payment struct {
Address string
Amount float64
Parameters MichelineExpr
}
// MichelineExpr gather Michelson expressions
type MichelineExpr map[string]interface{}
// TezClientWrapper is a wrapper for the TezosRPCClient
type TezClientWrapper struct {
healthy bool // isHealthy
client *TezosRPCClient
}
// RPCGenericError is an Error helper for the RPC
type RPCGenericError struct {
Kind string `json:"kind"`
Error string `json:"error"`
}
// RPCGenericErrors and array of RPCGenericErrors
type RPCGenericErrors []RPCGenericError
// OperationHashes slice
type OperationHashes []string
// slice of OperationHashes slice, as returned by GET /chains/<chain_id>/blocks/:block_ih/operation_hashes
type RawOperationHashes [][]string
// slice of RawBlockHashes slice, as returned by GET /chains/<chain_id>/blocks
type RawBlockHashes [][]string
// GoTezos manages multiple Clients
// each Client represents a Connection to a Tezos Node
// GoTezos manages failover if one Node is down, there
// are 2 Strategies:
// failover: always use the same unless it is down -> go to the next - default
// random: send to each Node equally
type GoTezos struct {
clientLock sync.Mutex
RPCClients []*TezClientWrapper
ActiveRPCCient *TezClientWrapper
Constants NetworkConstants
Versions []NetworkVersion
balancerStrategy string
rand *rand.Rand
logger *log.Logger
cache *gocache.Cache
debug bool
}
//Wallet needed for signing operations
type Wallet struct {
Address string
Mnemonic string
Seed []byte
Kp KeyPair
Sk string
Pk string
}
// Key Pair Storage
type KeyPair struct {
PrivKey []byte
PubKey []byte
}
// UnmarshalJSON unmarshals the bytes received as a parameter, into the type NetworkVersion.
func (nvs *NetworkVersions) UnmarshalJSON(v []byte) (NetworkVersions, error) {
networkVersions := NetworkVersions{}
err := json.Unmarshal(v, &networkVersions)
if err != nil {
return networkVersions, err
}
return networkVersions, nil
}
// UnmarshalJSON unmarshals bytes received as a parameter, into the type NetworkConstants.
func (nc *NetworkConstants) UnmarshalJSON(v []byte) (NetworkConstants, error) {
networkConstants := NetworkConstants{}
err := json.Unmarshal(v, &networkConstants)
if err != nil {
log.Println("Could not get unMarshal bytes into NetworkConstants: " + err.Error())
return networkConstants, err
}
return networkConstants, nil
}
// UnmarshalJSON unmarshals the bytes received as a parameter, into the type Block.
func (b *Block) UnmarshalJSON(v []byte) (Block, error) {
block := Block{}
err := json.Unmarshal(v, &block)
if err != nil {
log.Println("Could not get unMarshal bytes into block: " + err.Error())
return block, err
}
return block, nil
}
// UnmarshalJSON unmarshals the bytes received as a parameter, into the type SnapShotQuery.
func (sq *SnapShotQuery) UnmarshalJSON(v []byte) (SnapShotQuery, error) {
snapShotQuery := SnapShotQuery{}
err := json.Unmarshal(v, &snapShotQuery)
if err != nil {
log.Println("Could not unmarhel SnapShotQuery: " + err.Error())
return snapShotQuery, err
}
return snapShotQuery, nil
}
// UnmarshalJSON unmarshals the bytes received as a parameter, into the type SnapShotQuery.
func (fb *FrozenBalanceRewards) UnmarshalJSON(v []byte) (FrozenBalanceRewards, error) {
frozenBalance := FrozenBalanceRewards{}
err := json.Unmarshal(v, &frozenBalance)
if err != nil {
log.Println("Could not unmarhel frozenBalanceRewards: " + err.Error())
return frozenBalance, err
}
return frozenBalance, nil
}
// unmarshalString unmarshals the bytes received as a parameter, into the type string.
func unmarshalString(v []byte) (string, error) {
var str string
err := json.Unmarshal(v, &str)
if err != nil {
log.Println("Could not unMarshal to string " + err.Error())
return str, err
}
return str, nil
}
// UnmarshalJSON unmarshals the bytes received as a parameter, into the type an array of strings.
func unMarshalStringArray(v []byte) ([]string, error) {
var strs []string
err := json.Unmarshal(v, &strs)
if err != nil {
log.Println("Could not unMarshal to strings " + err.Error())
return strs, err
}
return strs, nil
}
// UnmarshalJSON unmarshalls bytes into StructDelegate
func (d *Delegate) UnmarshalJSON(v []byte) (Delegate, error) {
delegate := Delegate{}
err := json.Unmarshal(v, &delegate)
if err != nil {
return delegate, err
}
return delegate, nil
}
// UnmarshalJSON unmarshalls bytes into frozen balance
func (fb *FrozenBalance) UnmarshalJSON(v []byte) (FrozenBalance, error) {
frozenBalance := FrozenBalance{}
err := json.Unmarshal(v, &frozenBalance)
if err != nil {
return frozenBalance, err
}
return frozenBalance, nil
}
// UnmarshalJSON unmarhsels bytes into Baking_Rights
func (br *BakingRights) UnmarshalJSON(v []byte) (BakingRights, error) {
bakingRights := BakingRights{}
err := json.Unmarshal(v, &bakingRights)
if err != nil {
return bakingRights, err
}
return bakingRights, nil
}
// UnmarshalJSON unmarhsels bytes into Endorsing_Rights
func (er *EndorsingRights) UnmarshalJSON(v []byte) (EndorsingRights, error) {
endorsingRights := EndorsingRights{}
err := json.Unmarshal(v, &endorsingRights)
if err != nil {
return endorsingRights, err
}
return endorsingRights, nil
}
func (c Conts) String() string {
res, _ := json.Marshal(c)
return string(res)
}
// UnmarshalJSON unmarhsels bytes into OperationHashes
func (oh *OperationHashes) UnmarshalJSON(v []byte) (OperationHashes, error) {
// RPC returns slice of slice
// Will flatten to single slice of ops for easy use
ops := [][]string{}
operationHashes := OperationHashes{}
err := json.Unmarshal(v, &ops)
if err != nil {
return operationHashes, err
}
// flatten
for _, i := range ops {
for _, j := range i {
operationHashes = append(*oh, j)
}
}
return operationHashes, nil
}
// UnmarshalJSON unmarshals bytes into RawOperationHashes
func (bh *RawOperationHashes) UnmarshalJSON(v []byte) (RawOperationHashes, error) {
operationHashes := RawOperationHashes{}
err := json.Unmarshal(v, &operationHashes)
if err != nil {
return operationHashes, err
}
return operationHashes, nil
}
// UnmarshalJSON unmarshals bytes into RawBlockHashes
func (bh *RawBlockHashes) UnmarshalJSON(v []byte) (RawBlockHashes, error) {
blockHashes := RawBlockHashes{}
err := json.Unmarshal(v, &blockHashes)
if err != nil {
return blockHashes, err
}
return blockHashes, nil
}
// UnmarshalJSON unmarshals bytes into StructOperations
func (op *StructOperations) UnmarshalJSON(v []byte) (StructOperations, error) {
structOperations := StructOperations{}
err := json.Unmarshal(v, &structOperations)
if err != nil {
return structOperations, err
}
return structOperations, nil
}
// UnmarshalJSON unmarhsels bytes into RPCGenericErrors
func (ge *RPCGenericErrors) UnmarshalJSON(v []byte) (RPCGenericErrors, error) {
r := RPCGenericErrors{}
err := json.Unmarshal(v, &r)
if err != nil {
return r, err
}
return r, nil
}