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 pathscript_result_handler.go
More file actions
180 lines (141 loc) · 4.26 KB
/
script_result_handler.go
File metadata and controls
180 lines (141 loc) · 4.26 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package flow_storage_tracker
import (
"context"
"github.com/onflow/cadence"
"github.com/onflow/flow-go-sdk"
"github.com/rs/zerolog"
scan "github.com/onflow/flow-batch-scan"
)
type DBTX interface {
Get(address []flow.Address) (map[flow.Address]StoredAccountData, error)
Set(blockHeight uint64, address map[flow.Address]StoredAccountData) error
GetTotalAccounts() (uint64, error)
GatTotalStorageUsed() (uint64, error)
}
type StoredAccountData struct {
StorageUsed uint64
}
type ScriptResultHandler struct {
*scan.ComponentBase
db DB[DBTX]
limiterChan chan struct{}
reporter Reporter
notFirstReport bool
}
func NewScriptResultHandler(
db DB[DBTX],
reporter Reporter,
logger zerolog.Logger,
) *ScriptResultHandler {
h := &ScriptResultHandler{
db: db,
limiterChan: make(chan struct{}, 5), // concurrency limiter
reporter: reporter,
}
h.ComponentBase = scan.NewComponentWithStart("script_result_handler", h.start, logger)
return h
}
func (r *ScriptResultHandler) start(ctx context.Context) {
go func() {
<-ctx.Done()
r.Finish(ctx.Err())
}()
}
func (r *ScriptResultHandler) Handle(out scan.ProcessedAddressBatch) error {
r.limiterChan <- struct{}{}
defer func() { <-r.limiterChan }()
if !r.notFirstReport {
r.notFirstReport = true
err := r.firstReport()
if err != nil {
return err
}
}
blockHeight, batch := ParseProcessedAddressBatch(out)
updateResults, err := r.UpdateState(blockHeight, batch)
if err != nil {
return err
}
err = r.HandleUpdateResults(updateResults)
return err
}
type dataChanges struct {
newAddresses uint64
storageUsedChanged int64
}
func (r *ScriptResultHandler) UpdateState(blockHeight uint64, batch ParsedProcessedAddressBatch) (dataChanges, error) {
changes := dataChanges{}
err := r.db.TX(
func(txn DBTX) error {
allAddresses := make([]flow.Address, 0, len(batch))
for address := range batch {
allAddresses = append(allAddresses, address)
}
allAccounts, err := txn.Get(allAddresses)
if err != nil {
return err
}
// new addresses
changes.newAddresses = uint64(len(allAddresses) - len(allAccounts))
newStorageUsed := uint64(0)
for _, newAccountData := range batch {
newStorageUsed += newAccountData.StorageUsed
}
oldStorageUsed := uint64(0)
for _, oldAccountData := range allAccounts {
oldStorageUsed += oldAccountData.StorageUsed
}
if newStorageUsed < oldStorageUsed {
changes.storageUsedChanged = -int64(oldStorageUsed - newStorageUsed)
} else {
changes.storageUsedChanged = int64(newStorageUsed - oldStorageUsed)
}
return txn.Set(blockHeight, batch)
})
return changes, err
}
func (r *ScriptResultHandler) HandleUpdateResults(results dataChanges) error {
r.reporter.ReportTotalAccountsIncreased(results.newAddresses)
r.reporter.ReportStorageUsedChanged(results.storageUsedChanged)
return nil
}
func (r *ScriptResultHandler) firstReport() error {
totalAccounts := uint64(0)
totalStorageUsed := uint64(0)
err := r.db.TX(func(txn DBTX) error {
var err error
totalAccounts, err = txn.GetTotalAccounts()
if err != nil {
return err
}
totalStorageUsed, err = txn.GatTotalStorageUsed()
if err != nil {
return err
}
return nil
})
if err != nil {
r.Logger.Error().Err(err).Msg("failed to get total accounts and storage used from db")
return err
}
r.reporter.ReportTotalAccountsIncreased(totalAccounts)
r.reporter.ReportStorageUsedChanged(int64(totalStorageUsed))
return nil
}
type ParsedProcessedAddressBatch = map[flow.Address]StoredAccountData
func ParseProcessedAddressBatch(batch scan.ProcessedAddressBatch) (uint64, ParsedProcessedAddressBatch) {
parsedBatch := make(ParsedProcessedAddressBatch, len(batch.Addresses))
for _, account := range batch.Result.(cadence.Array).Values {
address, accountData := ParseProcessedSingleAddressBatchResult(account)
parsedBatch[address] = accountData
}
return batch.BlockHeight, parsedBatch
}
func ParseProcessedSingleAddressBatchResult(value cadence.Value) (flow.Address, StoredAccountData) {
accountStruct := value.(cadence.Struct)
address := flow.BytesToAddress(accountStruct.Fields[0].(cadence.Address).Bytes())
data := StoredAccountData{
StorageUsed: accountStruct.Fields[1].(cadence.UInt64).ToGoValue().(uint64),
}
return address, data
}