-
Notifications
You must be signed in to change notification settings - Fork 175
/
storage_used_update_migration.go
217 lines (187 loc) · 5.22 KB
/
storage_used_update_migration.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
package migrations
import (
"bufio"
"fmt"
"os"
"path"
"runtime"
"sync"
"time"
fvm "github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/ledger/common/utils"
"github.com/onflow/flow-go/model/flow"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type accountPayloadSize struct {
Address string
StorageUsed uint64
}
type indexedPayload struct {
Index int
Payload ledger.Payload
}
type accountStorageUsedPayload struct {
Address string
Index int
}
type StorageUsedUpdateMigration struct {
Log zerolog.Logger
OutputDir string
}
func (m *StorageUsedUpdateMigration) filename() string {
return path.Join(m.OutputDir, fmt.Sprintf("storage_used_update_%d.csv", int32(time.Now().Unix())))
}
// iterates through registers keeping a map of register sizes
// after it has reached the end it add storage used and storage capacity for each address
func (m *StorageUsedUpdateMigration) Migrate(payload []ledger.Payload) ([]ledger.Payload, error) {
fn := m.filename()
m.Log.Info().Msgf("Running Storage Used update. Saving output to %s.", fn)
f, err := os.Create(fn)
if err != nil {
return nil, err
}
defer func() {
err = f.Close()
if err != nil {
panic(err)
}
}()
writer := bufio.NewWriter(f)
defer func() {
err = writer.Flush()
if err != nil {
panic(err)
}
}()
workerCount := runtime.NumCPU()
storageUsed := make(map[string]uint64)
storageUsedChan := make(chan accountPayloadSize, workerCount)
payloadChan := make(chan indexedPayload)
storageUsedPayloadChan := make(chan accountStorageUsedPayload, workerCount)
storageUsedPayload := make(map[string]int)
inputWG := &sync.WaitGroup{}
outputWG := &sync.WaitGroup{}
outputWG.Add(1)
go func() {
for payloadSize := range storageUsedChan {
if _, ok := storageUsed[payloadSize.Address]; !ok {
storageUsed[payloadSize.Address] = 0
}
storageUsed[payloadSize.Address] = storageUsed[payloadSize.Address] + payloadSize.StorageUsed
}
outputWG.Done()
}()
outputWG.Add(1)
go func() {
for su := range storageUsedPayloadChan {
if _, ok := storageUsedPayload[su.Address]; ok {
m.Log.Error().
Str("address", flow.BytesToAddress([]byte(su.Address)).Hex()).
Msg("Already found a storage used payload for this address. Is this a duplicate?")
}
storageUsedPayload[su.Address] = su.Index
}
outputWG.Done()
}()
for i := 0; i < workerCount; i++ {
inputWG.Add(1)
go func() {
for p := range payloadChan {
var id flow.RegisterID
id, err = keyToRegisterID(p.Payload.Key)
if err != nil {
log.Error().Err(err).Msg("error converting key to register ID")
}
if len([]byte(id.Owner)) != flow.AddressLength {
// not an address
continue
}
if id.Key == fvm.KeyStorageUsed {
storageUsedPayloadChan <- accountStorageUsedPayload{
Address: id.Owner,
Index: p.Index,
}
}
storageUsedChan <- accountPayloadSize{
Address: id.Owner,
StorageUsed: uint64(registerSize(id, p.Payload)),
}
}
inputWG.Done()
}()
}
for i, p := range payload {
payloadChan <- indexedPayload{
Index: i,
Payload: p,
}
}
close(payloadChan)
inputWG.Wait()
close(storageUsedChan)
close(storageUsedPayloadChan)
outputWG.Wait()
if err != nil {
return nil, err
}
if len(storageUsedPayload) != len(storageUsed) {
errStr := "number off accounts and number of storage_used registers don't match"
log.Error().Int("accounts", len(storageUsed)).Int("storageUsedPayloads", len(storageUsedPayload)).Msg(errStr)
return nil, fmt.Errorf(errStr)
}
var change int64
storageIncreaseCount := 0
storageDecreaseCount := 0
storageNoChangeCount := 0
for a, pIndex := range storageUsedPayload {
p := payload[pIndex]
used, ok := storageUsed[a]
if !ok {
errStr := "address has a storage used payload but is not using any storage"
log.Error().Str("address", flow.BytesToAddress([]byte(a)).Hex()).Msg(errStr)
return nil, fmt.Errorf(errStr)
}
id, err := keyToRegisterID(p.Key)
if err != nil {
log.Error().Err(err).Msg("error converting key to register ID")
return nil, err
}
if id.Key != fvm.KeyStorageUsed {
return nil, fmt.Errorf("this is not a storage used register")
}
oldUsed, _, err := utils.ReadUint64(p.Value)
if err != nil {
errStr := "cannot decode storage used by address"
log.Error().
Str("address", flow.BytesToAddress([]byte(a)).Hex()).
Hex("storageUsed", p.Value).
Hex("storageUsedKey", p.Key.CanonicalForm()).
Err(err).
Msg(errStr)
return nil, fmt.Errorf(errStr)
}
if oldUsed > used {
storageDecreaseCount += 1
change = -int64(oldUsed - used)
} else if oldUsed == used {
storageNoChangeCount += 1
change = 0
} else {
storageIncreaseCount += 1
change = int64(oldUsed - used)
}
_, err = writer.WriteString(fmt.Sprintf("%s,%d,%d,%d\n", flow.BytesToAddress([]byte(a)).Hex(), oldUsed, used, change))
if err != nil {
return nil, err
}
payload[pIndex].Value = utils.Uint64ToBinary(used)
}
m.Log.Info().
Int("accountsStorageIncreased", storageIncreaseCount).
Int("accountsStorageDecreased", storageDecreaseCount).
Int("accountsStorageNoChangeCount", storageNoChangeCount).
Msg("Storage used update complete.")
return payload, nil
}