This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
rights.go
321 lines (295 loc) · 9.99 KB
/
rights.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
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package rpc
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strconv"
"time"
"blockwatch.cc/tzgo/tezos"
)
// BakingRight holds information about the right to bake a specific Tezos block.
type BakingRight struct {
Delegate tezos.Address `json:"delegate"`
Level int64 `json:"level"`
Priority int `json:"priority"` // until v011
Round int `json:"round"` // v012+
EstimatedTime time.Time `json:"estimated_time"`
}
func (r BakingRight) Address() tezos.Address {
return r.Delegate
}
// EndorsingRight holds information about the right to endorse a specific Tezos block.
type EndorsingRight struct {
Delegate tezos.Address `json:"delegate"`
Level int64 `json:"level"`
EstimatedTime time.Time `json:"estimated_time"`
Slots []int `json:"slots,omitempty"` // until v011
FirstSlot int `json:"first_slot"` // v012+
EndorsingPower int `json:"endorsing_power"` // v012+
}
func (r EndorsingRight) Address() tezos.Address {
return r.Delegate
}
func (r EndorsingRight) Power() int {
return r.EndorsingPower + len(r.Slots)
}
type RollSnapshotInfo struct {
LastRoll []string `json:"last_roll"`
Nonces []string `json:"nonces"`
RandomSeed string `json:"random_seed"`
RollSnapshot int `json:"roll_snapshot"`
}
type StakeInfo struct {
ActiveStake int64 `json:"active_stake,string"`
Baker tezos.Address `json:"baker"`
}
// v012+
type StakingSnapshotInfo struct {
Nonces []string `json:"nonces"`
RandomSeed string `json:"random_seed"`
BakerStake []StakeInfo `json:"selected_stake_distribution,omitempty"`
TotalActiveStake int64 `json:"total_active_stake,string"`
// SlashedDeposits []?? `json:"slashed_deposits"`
}
type SnapshotIndex struct {
Cycle int64 // the requested cycle that contains rights from the snapshot
Base int64 // the cycle where the snapshot happened
Index int // the index inside base where snapshot happened
}
type SnapshotRoll struct {
RollId int64
OwnerKey tezos.Key
}
func (r *SnapshotRoll) UnmarshalJSON(data []byte) error {
if len(data) == 0 || bytes.Equal(data, null) {
return nil
}
if len(data) == 2 {
return nil
}
if data[0] != '[' || data[len(data)-1] != ']' {
return fmt.Errorf("SnapshotRoll: invalid json array '%s'", string(data))
}
dec := json.NewDecoder(bytes.NewReader(data))
dec.UseNumber()
unpacked := make([]interface{}, 0)
err := dec.Decode(&unpacked)
if err != nil {
return err
}
return r.decode(unpacked)
}
func (r SnapshotRoll) MarshalJSON() ([]byte, error) {
buf := make([]byte, 0, 2048)
buf = append(buf, '[')
buf = strconv.AppendInt(buf, r.RollId, 10)
buf = append(buf, ',')
buf = strconv.AppendQuote(buf, r.OwnerKey.String())
buf = append(buf, ']')
return buf, nil
}
func (r *SnapshotRoll) decode(unpacked []interface{}) error {
if l := len(unpacked); l != 2 {
return fmt.Errorf("SnapshotRoll: invalid json array len %d", l)
}
id, err := strconv.ParseInt(unpacked[0].(json.Number).String(), 10, 64)
if err != nil {
return fmt.Errorf("SnapshotRoll: invalid roll id: %v", err)
}
if err = r.OwnerKey.UnmarshalText([]byte(unpacked[1].(string))); err != nil {
return err
}
r.RollId = id
return nil
}
type SnapshotOwners struct {
Cycle int64 `json:"cycle"`
Index int64 `json:"index"`
Rolls []SnapshotRoll `json:"rolls"`
}
// ListBakingRights returns information about baking rights at block id.
// Use max to set a max block priority (before Ithaca) or a max round (after Ithaca).
func (c *Client) ListBakingRights(ctx context.Context, id BlockID, max int) ([]BakingRight, error) {
p, err := c.GetParams(ctx, id)
if err != nil {
return nil, err
}
maxSelector := "max_priority=%d"
if p.Version >= 12 {
maxSelector = "max_round=%d"
}
if p.Version < 6 {
max++
}
rights := make([]BakingRight, 0)
u := fmt.Sprintf("chains/main/blocks/%s/helpers/baking_rights?all=true&"+maxSelector, id, max)
if err := c.Get(ctx, u, &rights); err != nil {
return nil, err
}
return rights, nil
}
// ListBakingRightsCycle returns information about baking rights for an entire cycle
// as seen from block id. Note block and cycle must be no further than preserved cycles
// away from each other. Use max to set a max block priority (before Ithaca) or a max
// round (after Ithaca).
func (c *Client) ListBakingRightsCycle(ctx context.Context, id BlockID, cycle int64, max int) ([]BakingRight, error) {
p, err := c.GetParams(ctx, id)
if err != nil {
return nil, err
}
maxSelector := "max_priority=%d"
if p.Version >= 12 {
maxSelector = "max_round=%d"
}
if p.Version < 6 {
max++
}
rights := make([]BakingRight, 0)
u := fmt.Sprintf("chains/main/blocks/%s/helpers/baking_rights?all=true&cycle=%d&"+maxSelector, id, cycle, max)
if err := c.Get(ctx, u, &rights); err != nil {
return nil, err
}
return rights, nil
}
// ListEndorsingRights returns information about block endorsing rights.
func (c *Client) ListEndorsingRights(ctx context.Context, id BlockID) ([]EndorsingRight, error) {
p, err := c.GetParams(ctx, id)
if err != nil {
return nil, err
}
u := fmt.Sprintf("chains/main/blocks/%s/helpers/endorsing_rights?all=true", id)
rights := make([]EndorsingRight, 0)
// Note: future cycles are seen from current protocol (!)
if p.Version >= 12 {
type V12Rights struct {
Level int64 `json:"level"`
Delegates []EndorsingRight `json:"delegates"`
EstimatedTime time.Time `json:"estimated_time"`
}
v12rights := make([]V12Rights, 0, 1)
if err := c.Get(ctx, u, &v12rights); err != nil {
return nil, err
}
for _, v := range v12rights {
for _, r := range v.Delegates {
r.Level = v.Level
r.EstimatedTime = v.EstimatedTime
rights = append(rights, r)
}
}
} else {
if err := c.Get(ctx, u, &rights); err != nil {
return nil, err
}
}
return rights, nil
}
// ListEndorsingRightsCycle returns information about endorsing rights for an entire cycle
// as seen from block id. Note block and cycle must be no further than preserved cycles
// away.
func (c *Client) ListEndorsingRightsCycle(ctx context.Context, id BlockID, cycle int64) ([]EndorsingRight, error) {
p, err := c.GetParams(ctx, id)
if err != nil {
return nil, err
}
u := fmt.Sprintf("chains/main/blocks/%s/helpers/endorsing_rights?all=true&cycle=%d", id, cycle)
rights := make([]EndorsingRight, 0)
// Note: future cycles are seen from current protocol (!)
if p.Version >= 12 && p.StartHeight <= id.Int64() {
type V12Rights struct {
Level int64 `json:"level"`
Delegates []EndorsingRight `json:"delegates"`
EstimatedTime time.Time `json:"estimated_time"`
}
v12rights := make([]V12Rights, 0, p.BlocksPerCycle)
if err := c.Get(ctx, u, &v12rights); err != nil {
return nil, err
}
for _, v := range v12rights {
for _, r := range v.Delegates {
r.Level = v.Level
r.EstimatedTime = v.EstimatedTime
rights = append(rights, r)
}
}
} else {
if err := c.Get(ctx, u, &rights); err != nil {
return nil, err
}
}
return rights, nil
}
// GetRollSnapshotInfoCycle returns information about a roll snapshot as seen from block id.
// Note block and cycle must be no further than preserved cycles away.
func (c *Client) GetRollSnapshotInfoCycle(ctx context.Context, id BlockID, cycle int64) (*RollSnapshotInfo, error) {
idx := &RollSnapshotInfo{}
u := fmt.Sprintf("chains/main/blocks/%s/context/raw/json/cycle/%d", id, cycle)
if err := c.Get(ctx, u, idx); err != nil {
return nil, err
}
if idx.RandomSeed == "" {
return nil, fmt.Errorf("missing snapshot for cycle %d at block %s", cycle, id)
}
return idx, nil
}
// GetStakingSnapshotInfoCycle returns information about a roll snapshot as seen from block id.
// Note block and cycle must be no further than preserved cycles away.
func (c *Client) GetStakingSnapshotInfoCycle(ctx context.Context, id BlockID, cycle int64) (*StakingSnapshotInfo, error) {
idx := &StakingSnapshotInfo{}
u := fmt.Sprintf("chains/main/blocks/%s/context/raw/json/cycle/%d", id, cycle)
if err := c.Get(ctx, u, idx); err != nil {
return nil, err
}
return idx, nil
}
// GetSnapshotIndexCycle returns information about a roll or staking snapshot that
// produced rights at cycle.
// Note block and cycle must be no further than preserved cycles away.
func (c *Client) GetSnapshotIndexCycle(ctx context.Context, id BlockID, cycle int64) (*SnapshotIndex, error) {
p, err := c.GetParams(ctx, id)
if err != nil {
return nil, err
}
idx := &SnapshotIndex{}
if p.Version >= 12 {
idx.Cycle = cycle
idx.Base = p.SnapshotBaseCycle(cycle)
idx.Index = -1
if cycle >= p.PreservedCycles+1 {
u := fmt.Sprintf("chains/main/blocks/%s/context/selected_snapshot?cycle=%d", id, cycle)
if err := c.Get(ctx, u, &idx.Index); err != nil {
return nil, err
}
} else {
log.Warnf("No snapshot for cycle %d", cycle)
}
} else {
// pre-Ithaca we can at most look PRESERVED_CYCLES into the future since
// the snapshot happened 2 cycles back from the block we're looking from.
var info RollSnapshotInfo
u := fmt.Sprintf("chains/main/blocks/%s/context/raw/json/cycle/%d", id, cycle)
if err := c.Get(ctx, u, &info); err != nil {
return nil, err
}
if info.RandomSeed == "" {
return nil, fmt.Errorf("missing snapshot for cycle %d at block %s", cycle, id)
}
idx.Cycle = cycle
idx.Base = p.SnapshotBaseCycle(cycle)
idx.Index = info.RollSnapshot
}
return idx, nil
}
// ListSnapshotRollOwners returns information about a roll snapshot ownership.
// Response is a nested array `[[roll_id, pubkey]]`. Deprecated in Ithaca.
func (c *Client) ListSnapshotRollOwners(ctx context.Context, id BlockID, cycle, index int64) (*SnapshotOwners, error) {
owners := &SnapshotOwners{Cycle: cycle, Index: index}
u := fmt.Sprintf("chains/main/blocks/%s/context/raw/json/rolls/owner/snapshot/%d/%d?depth=1", id, cycle, index)
if err := c.Get(ctx, u, &owners.Rolls); err != nil {
return nil, err
}
return owners, nil
}