-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
199 lines (183 loc) · 5.45 KB
/
client.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
package rpc_client
import (
"context"
"fmt"
"strconv"
"strings"
"time"
tzblock "github.com/bullblock-io/go-tezos/v2/block"
tzc "github.com/bullblock-io/go-tezos/v2/client"
"github.com/bullblock-io/tezTracker/models"
"github.com/bullblock-io/tezTracker/services/rpc_client/client"
"github.com/bullblock-io/tezTracker/services/rpc_client/client/baking_rights"
"github.com/bullblock-io/tezTracker/services/rpc_client/client/snapshots"
genmodels "github.com/bullblock-io/tezTracker/services/rpc_client/models"
)
const headBlock = "head"
const BlocksInCycle = 4096
type Tezos struct {
client *client.Tezosrpc
network string
isTestNetwork bool //we have to use a separate flag due to stupid nodes configs...
tzcClient *tzblock.BlockService
}
func (t *Tezos) BlocksInCycle() int64 {
if t.isTestNetwork {
return BlocksInCycle / 2
}
return BlocksInCycle
}
func New(cfg client.TransportConfig, network string, isTestNetwork bool) *Tezos {
cli := client.NewHTTPClientWithConfig(nil, &cfg)
return &Tezos{
client: cli,
network: network,
tzcClient: tzblock.NewBlockService(tzc.NewClient(cfg.Host)),
isTestNetwork: isTestNetwork,
}
}
func (t *Tezos) RightsFor(ctx context.Context, blockFrom, blockTo, currentHead int64) ([]models.FutureBakingRight, error) {
all := true
blockToUse := headBlock
if currentHead >= blockFrom {
blockToUse = strconv.FormatInt(blockFrom, 10)
}
params := baking_rights.NewGetBakingRightsParamsWithContext(ctx).
WithNetwork(t.network).
WithBlock(blockToUse).
WithAll(&all)
levels := []string{}
for b := blockFrom; b <= blockTo; b++ {
levels = append(levels, strconv.FormatInt(b, 10))
}
params.SetLevel(levels)
resp, err := t.client.BakingRights.GetBakingRights(params)
if err != nil {
return nil, err
}
rights := make([]models.FutureBakingRight, len(resp.Payload))
for i := range resp.Payload {
if resp.Payload[i] != nil {
rights[i] = genRightToModel(*resp.Payload[i])
}
}
return rights, nil
}
func genRightToModel(m genmodels.BakingRight) models.FutureBakingRight {
return models.FutureBakingRight{
Level: m.Level,
Priority: int(m.Priority),
Delegate: m.Delegate,
EstimatedTime: time.Time(m.EstimatedTime),
}
}
func (t *Tezos) SnapshotForCycle(ctx context.Context, cycle int64, useHead bool) (snap models.Snapshot, err error) {
blockToUse := headBlock
if !useHead {
level := cycle*t.BlocksInCycle() + 1
blockToUse = strconv.FormatInt(level, 10)
}
params := snapshots.NewGetRollSnapshotParamsWithContext(ctx).
WithCycle(cycle).
WithNetwork(t.network).
WithBlock(blockToUse)
resp, err := t.client.Snapshots.GetRollSnapshot(params)
if err != nil {
return snap, err
}
snapshot := resp.Payload
snap.Cycle = cycle
snap.BlockLevel = ((cycle-7)*t.BlocksInCycle() + 1) + (snapshot+1)*256 - 1
rollParams := snapshots.NewGetRollsParamsWithContext(ctx).
WithCycle(cycle).
WithNetwork(t.network).
WithSnap(snapshot).
WithBlock(blockToUse)
rollsResp, err := t.client.Snapshots.GetRolls(rollParams)
if err != nil {
return snap, err
}
if rollsResp == nil {
return snap, fmt.Errorf("nil resp")
}
snap.Rolls = int64(len(rollsResp.Payload))
return snap, nil
}
func (t *Tezos) DoubleBakingEvidence(ctx context.Context, blockLevel int, operationHash string) (dee models.DoubleBakingEvidence, err error) {
block, err := t.tzcClient.Get(blockLevel)
if err != nil {
return dee, err
}
for i := range block.Operations {
for _, op := range block.Operations[i] {
if strings.EqualFold(op.Hash, operationHash) {
dee, err := ToDoubleBakingEvidence(op)
if err != nil {
return dee, err
}
dee.BlockLevel = int64(block.Header.Level)
dee.BlockHash = block.Hash
return dee, err
}
}
}
return dee, fmt.Errorf("not found")
}
func ToDoubleBakingEvidence(op tzblock.Operations) (dee models.DoubleBakingEvidence, err error) {
for i := range op.Contents {
if op.Contents[i].Bh1 != nil {
dee.DenouncedLevel = int64(op.Contents[i].Bh1.Level)
dee.Priority = op.Contents[i].Bh1.Priority
if meta := op.Contents[i].Metadata; meta != nil {
for _, bu := range meta.BalanceUpdates {
if strings.EqualFold(bu.Kind, "freezer") {
switch strings.ToLower(bu.Category) {
case "deposits":
dee.Offender = bu.Delegate
change, err := strconv.ParseInt(bu.Change, 10, 64)
if err != nil {
return dee, err
}
dee.LostDeposits = -change
case "rewards":
change, err := strconv.ParseInt(bu.Change, 10, 64)
if err != nil {
return dee, err
}
if change < 0 {
dee.LostRewards = -change
} else {
dee.BakerReward = change
dee.EvidenceBaker = bu.Delegate
}
}
}
}
}
return dee, nil
}
}
return dee, fmt.Errorf("not a double baking evidence")
}
func (t *Tezos) DoubleEndrsementEvidenceLevel(ctx context.Context, blockLevel int, operationHash string) (int64, error) {
block, err := t.tzcClient.Get(blockLevel)
if err != nil {
return 0, err
}
for i := range block.Operations {
for _, op := range block.Operations[i] {
if strings.EqualFold(op.Hash, operationHash) {
return GetDoubleEndrsementEvidenceLevel(op)
}
}
}
return 0, fmt.Errorf("not found")
}
func GetDoubleEndrsementEvidenceLevel(op tzblock.Operations) (int64, error) {
for i := range op.Contents {
if op.Contents[i].Op1 != nil {
return int64(op.Contents[i].Op1.Operations.Level), nil
}
}
return 0, fmt.Errorf("not a double endorsement evidence")
}