-
Notifications
You must be signed in to change notification settings - Fork 178
/
storage_used_migration.go
151 lines (123 loc) · 3.67 KB
/
storage_used_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
package migrations
import (
"context"
"fmt"
"github.com/rs/zerolog"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/fvm/environment"
fvm "github.com/onflow/flow-go/fvm/environment"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/ledger/common/convert"
"github.com/onflow/flow-go/model/flow"
)
// AccountUsageMigrator iterates through each payload, and calculate the storage usage
// and update the accounts status with the updated storage usage
type AccountUsageMigrator struct {
log zerolog.Logger
}
var _ AccountBasedMigration = &AccountUsageMigrator{}
func (m *AccountUsageMigrator) InitMigration(
log zerolog.Logger,
_ []*ledger.Payload,
_ int,
) error {
m.log = log.With().Str("component", "AccountUsageMigrator").Logger()
return nil
}
const oldAccountStatusSize = 25
func (m *AccountUsageMigrator) Close() error {
return nil
}
func (m *AccountUsageMigrator) MigrateAccount(
_ context.Context,
address common.Address,
payloads []*ledger.Payload,
) ([]*ledger.Payload, error) {
var status *environment.AccountStatus
var statusIndex int
actualSize := uint64(0)
for i, payload := range payloads {
key, err := payload.Key()
if err != nil {
return nil, err
}
if isAccountKey(key) {
statusIndex = i
status, err = environment.AccountStatusFromBytes(payload.Value())
if err != nil {
return nil, fmt.Errorf("could not parse account status: %w", err)
}
}
size, err := payloadSize(key, payload)
if err != nil {
return nil, err
}
actualSize += size
}
if status == nil {
return nil, fmt.Errorf("could not find account status for account %v", address.Hex())
}
isOldVersionOfStatusRegister := len(payloads[statusIndex].Value()) == oldAccountStatusSize
same := m.compareUsage(isOldVersionOfStatusRegister, status, actualSize)
if same {
// there is no problem with the usage, return
return payloads, nil
}
if isOldVersionOfStatusRegister {
// size will grow by 8 bytes because of the on-the-fly
// migration of account status in AccountStatusFromBytes
actualSize += 8
}
// update storage used
status.SetStorageUsed(actualSize)
newValue := status.ToBytes()
newPayload, err := newPayloadWithValue(payloads[statusIndex], newValue)
if err != nil {
return nil, fmt.Errorf("cannot create new payload with value: %w", err)
}
payloads[statusIndex] = newPayload
return payloads, nil
}
func (m *AccountUsageMigrator) compareUsage(
isOldVersionOfStatusRegister bool,
status *environment.AccountStatus,
actualSize uint64,
) bool {
oldSize := status.StorageUsed()
if isOldVersionOfStatusRegister {
// size will be reported as 8 bytes larger than the actual size due to on-the-fly
// migration of account status in AccountStatusFromBytes
oldSize -= 8
}
if oldSize != actualSize {
m.log.Warn().
Uint64("old_size", oldSize).
Uint64("new_size", actualSize).
Msg("account storage used usage mismatch")
return false
}
return true
}
// newPayloadWithValue returns a new payload with the key from the given payload, and
// the value from the argument
func newPayloadWithValue(payload *ledger.Payload, value ledger.Value) (*ledger.Payload, error) {
key, err := payload.Key()
if err != nil {
return nil, err
}
newPayload := ledger.NewPayload(key, value)
return newPayload, nil
}
func registerSize(id flow.RegisterID, p *ledger.Payload) int {
return fvm.RegisterSize(id, p.Value())
}
func payloadSize(key ledger.Key, payload *ledger.Payload) (uint64, error) {
id, err := convert.LedgerKeyToRegisterID(key)
if err != nil {
return 0, err
}
return uint64(registerSize(id, payload)), nil
}
func isAccountKey(key ledger.Key) bool {
return string(key.KeyParts[1].Value) == flow.AccountStatusKey
}