-
Notifications
You must be signed in to change notification settings - Fork 1
/
flow.go
238 lines (211 loc) · 7.15 KB
/
flow.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
package main
import (
"context"
"math"
"strings"
"sync"
"time"
"github.com/rs/zerolog/log"
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/client"
"google.golang.org/grpc"
)
type FlowAdapter struct {
Client *client.Client
Context context.Context
URL string
}
func NewFlowClient(url string) *FlowAdapter {
adapter := FlowAdapter{}
adapter.Context = context.Background()
// any reason to pass this as an arg instead?
adapter.URL = url
// create flow client
FlowClient, err := client.New(strings.TrimSpace(adapter.URL), grpc.WithInsecure())
if err != nil {
log.Panic().Msgf("failed to connect to %s", adapter.URL)
}
adapter.Client = FlowClient
return &adapter
}
func (fa *FlowAdapter) GetAccountAtBlockHeight(addr string, blockheight uint64) (*flow.Account, error) {
hexAddr := flow.HexToAddress(addr)
return fa.Client.GetAccountAtBlockHeight(fa.Context, hexAddr, blockheight)
}
func (fa *FlowAdapter) GetCurrentBlockHeight() (uint64, error) {
block, err := fa.Client.GetLatestBlock(fa.Context, true)
if err != nil {
return 0, err
}
return block.Height, nil
}
func (fa *FlowAdapter) GetAddressesFromBlockEvents(flowUrls []string, startBlockheight uint64, maxBlockRange int, waitNumBlocks int) ([]PublicKeyActions, uint64, bool, error) {
itemsPerRequest := 245 // access node can only handel 250
eventTypes := []string{"flow.AccountKeyAdded", "flow.AccountKeyRemoved"}
var addrActions []PublicKeyActions
restartBulkLoad := true
currentHeight, err := fa.GetCurrentBlockHeight()
if err != nil {
log.Error().Err(err).Msg("Could not get current block height")
return addrActions, currentHeight, restartBulkLoad, err
}
// backing off a few blocks to allow for buffer
BlockHeight := currentHeight //- 10 // backoff current
// create chunks to make sure query limit is not exceeded
var chunksEvents []client.EventRangeQuery
for _, eventType := range eventTypes {
events := ChunkEventRangeQuery(itemsPerRequest, startBlockheight, BlockHeight, eventType)
chunksEvents = append(chunksEvents, events...)
}
addrs, restartDataLoader := fa.GetEventAddresses(flowUrls, chunksEvents)
return addrs, BlockHeight, restartDataLoader, nil
}
func ChunkEventRangeQuery(itemsPerRequest int, lowBlockHeight, highBlockHeight uint64, eventName string) []client.EventRangeQuery {
maxServerBlockRange := float64(itemsPerRequest) // less than server's max of 250
var chunks []client.EventRangeQuery
totalRange := float64(highBlockHeight - lowBlockHeight)
numChunks := math.Ceil(totalRange / maxServerBlockRange)
lowHeight := lowBlockHeight
highHeight := uint64(lowHeight + uint64(maxServerBlockRange))
for i := 0; i < int(numChunks); i++ {
query := client.EventRangeQuery{
Type: eventName,
StartHeight: lowHeight,
EndHeight: highHeight,
}
chunks = append(chunks, query)
lowHeight = lowHeight + uint64(maxServerBlockRange) + 1
highHeight = lowHeight + uint64(maxServerBlockRange)
if highHeight > highBlockHeight {
highHeight = highBlockHeight
}
}
return chunks
}
func RunAddressQuery(client *client.Client, context context.Context, query client.EventRangeQuery) (PublicKeyActions, bool) {
var publicKeyActions PublicKeyActions
restartBulkLoad := false
events, err := client.GetEventsForHeightRange(context, query)
if err != nil {
log.Warn().Err(err).Msgf("retrying get events in block range %d %d", query.StartHeight, query.EndHeight)
// break up query into smaller chunks
for _, q := range splitQuery(query) {
eventsRetry, errRetry := client.GetEventsForHeightRange(context, q)
if errRetry != nil {
log.Error().Err(errRetry).Msg("retrying get events failed")
return publicKeyActions, true
}
events = append(events, eventsRetry...)
}
}
for _, event := range events {
for _, evt := range event.Events {
var pkAddr PublicKeyEvent
payload, err := jsoncdc.Decode(evt.Payload)
if err != nil {
log.Warn().Msgf("Could not decode payload %v %v", evt.Type, err.Error())
continue
}
addEvent, ok := payload.(cadence.Event)
if !ok {
log.Warn().Msgf("could not decode event payload")
continue
}
// field 0 is account address
address := addEvent.Fields[0].String()
// field 1 is public key
pkValues, isOld := addEvent.Fields[1].(cadence.Array)
if isOld {
// old add account key format
var dError error
pkAddr, dError = CreatePublicKeyFromEvent(pkValues, address)
if dError != nil {
continue
}
if evt.Type == "flow.AccountKeyRemoved" {
publicKeyActions.removes = append(publicKeyActions.removes, pkAddr)
}
}
publicKeyActions.addresses = append(publicKeyActions.addresses, address)
}
}
log.Debug().Msgf("total addrs %v, remove addrs %v", len(publicKeyActions.addresses), len(publicKeyActions.removes))
return publicKeyActions, restartBulkLoad
}
func CreatePublicKeyFromEvent(cadenceArr cadence.Array, address string) (PublicKeyEvent, error) {
pkBytes := ByteArrayValueToByteSlice(cadenceArr)
publicKeyValue, decodeErr := flow.DecodeAccountKey(pkBytes)
if decodeErr != nil {
log.Warn().Msgf("could not decode public key %v", decodeErr.Error())
return PublicKeyEvent{}, decodeErr
}
pk := Trim0x(publicKeyValue.PublicKey.String())
pkAddr := PublicKeyEvent{pk, address}
return pkAddr, nil
}
func ByteArrayValueToByteSlice(array cadence.Array) (result []byte) {
result = make([]byte, 0, len(array.Values))
for _, value := range array.Values {
result = append(result, byte(value.(cadence.UInt8)))
}
return result
}
func Trim0x(hexString string) string {
prefix := hexString[:2]
if prefix == "0x" {
return hexString[2:]
}
return hexString
}
func (fa *FlowAdapter) GetEventAddresses(flowUrls []string, queries []client.EventRangeQuery) ([]PublicKeyActions, bool) {
var allPkAddrs []PublicKeyActions
restartBulkLoad := false
var wg sync.WaitGroup
eventRangeChan := make(chan client.EventRangeQuery)
publicKeyChan := make(chan PublicKeyActions)
go func() {
for actions := range publicKeyChan {
allPkAddrs = append(allPkAddrs, actions)
}
}()
wg.Add(len(queries))
for i := 0; i < len(flowUrls); i++ {
flowUrl := flowUrls[i]
go func() {
// Each worker has a separate Flow client
client := getFlowClient(flowUrl)
defer func() {
err := client.Close()
if err != nil {
log.Warn().
Err(err).
Msg("error closing client")
}
}()
for query := range eventRangeChan {
log.Debug().Msgf("Query %v event blocks: %d %d", query.Type, query.StartHeight, query.EndHeight)
addrs, restart := RunAddressQuery(client, fa.Context, query)
if !restart {
restartBulkLoad = restart
}
publicKeyChan <- addrs
time.Sleep(500 * time.Millisecond) // give time for channel to process
wg.Done()
}
}()
}
for _, query := range queries {
eventRangeChan <- query
}
wg.Wait()
close(publicKeyChan)
close(eventRangeChan)
return allPkAddrs, restartBulkLoad
}
func splitQuery(query client.EventRangeQuery) []client.EventRangeQuery {
rangef := float64(query.EndHeight - query.StartHeight)
itemsPerRequest := math.Ceil(rangef / 2)
return ChunkEventRangeQuery(int(itemsPerRequest), query.StartHeight, query.EndHeight, query.Type)
}