This repository was archived by the owner on Feb 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcandidate_scanner.go
More file actions
165 lines (140 loc) · 3.87 KB
/
candidate_scanner.go
File metadata and controls
165 lines (140 loc) · 3.87 KB
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
package flow_storage_tracker
import (
"context"
"fmt"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/engine/common/rpc/convert"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow/protobuf/go/flow/entities"
"github.com/onflow/flow-go-sdk"
flowModel "github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-batch-scan/candidates"
"github.com/onflow/flow-batch-scan/client"
execution "github.com/onflow/flow/protobuf/go/flow/executiondata"
)
type CandidateScanner struct {
chain flowModel.Chain
exeDataClient execution.ExecutionDataAPIClient
logger zerolog.Logger
}
var _ candidates.CandidateScanner = CandidateScanner{}
func NewCandidateScanner(
chain flowModel.Chain,
exeDataClient execution.ExecutionDataAPIClient,
logger zerolog.Logger,
) CandidateScanner {
return CandidateScanner{
chain: chain,
exeDataClient: exeDataClient,
logger: logger.With().Str("component", "candidate_scanner").Logger(),
}
}
func (s CandidateScanner) Scan(
ctx context.Context,
client client.Client,
blocks candidates.BlockRange,
) candidates.CandidatesResult {
candidatesChan := make(chan candidates.CandidatesResult, blocks.End-blocks.Start+1)
defer close(candidatesChan)
blockHeight := blocks.Start
for blockHeight <= blocks.End {
go func(blockHeight uint64) {
candidatesChan <- s.scanBlock(
ctx,
client,
blockHeight,
)
}(blockHeight)
blockHeight++
}
return candidates.WaitForCandidateResults(candidatesChan, int(blocks.End-blocks.Start+1))
}
func (s CandidateScanner) scanBlock(
ctx context.Context,
client client.Client,
blockHeight uint64,
) candidates.CandidatesResult {
blockID, err := s.getBlockID(ctx, client, blockHeight)
if err != nil {
return candidates.NewCandidatesResultError(
fmt.Errorf("could not get block ID: %w", err),
)
}
executionData, err := s.getExecutionData(ctx, blockID)
if err != nil {
return candidates.NewCandidatesResultError(
fmt.Errorf("could not get execution data: %w", err),
)
}
updates, err := s.extractTrieUpdates(executionData)
if err != nil {
return candidates.NewCandidatesResultError(
fmt.Errorf("could not convert execution data: %w", err),
)
}
addresses := map[flow.Address]struct{}{}
for _, update := range updates {
for _, payload := range update.Payloads {
key, err := payload.Key()
if err != nil {
return candidates.NewCandidatesResultError(
fmt.Errorf("could not get payload key: %w", err),
)
}
if len(key.KeyParts[0].Value) != flow.AddressLength {
continue
}
address := flow.BytesToAddress(key.KeyParts[0].Value)
addresses[address] = struct{}{}
}
}
return candidates.NewCandidatesResult(addresses)
}
func (s CandidateScanner) getBlockID(
ctx context.Context,
c client.Client,
height uint64,
) (flow.Identifier, error) {
block, err := c.GetBlockByHeight(ctx, height)
if err != nil {
return flow.EmptyID, err
}
return block.ID, nil
}
func (s CandidateScanner) getExecutionData(
ctx context.Context,
blockID flow.Identifier,
) (*entities.BlockExecutionData, error) {
resp, err := s.exeDataClient.GetExecutionDataByBlockID(
ctx,
&execution.GetExecutionDataByBlockIDRequest{
BlockId: blockID.Bytes(),
})
if err != nil {
s.logger.
Error().
Err(err).
Str("block_id", blockID.String()).
Msg("could not get execution data")
return nil, err
}
return resp.BlockExecutionData, nil
}
func (s CandidateScanner) extractTrieUpdates(
m *entities.BlockExecutionData,
) ([]*ledger.TrieUpdate, error) {
if m == nil {
return nil, convert.ErrEmptyMessage
}
var updates []*ledger.TrieUpdate
for i, c := range m.GetChunkExecutionData() {
chunk, err := convert.MessageToChunkExecutionData(c, s.chain)
if err != nil {
return nil, fmt.Errorf("could not convert chunk %d: %w", i, err)
}
if chunk.TrieUpdate != nil {
updates = append(updates, chunk.TrieUpdate)
}
}
return updates, nil
}