-
Notifications
You must be signed in to change notification settings - Fork 178
/
accounts.go
254 lines (207 loc) · 6.25 KB
/
accounts.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package migrations
import (
"encoding/hex"
"fmt"
"strings"
"github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/ledger"
"github.com/onflow/flow-go/model/flow"
)
func AddMissingKeysMigration(payloads []ledger.Payload) ([]ledger.Payload, error) {
l := newView(payloads)
st := state.NewState(l)
sth := state.NewStateHolder(st)
a := state.NewAccounts(sth)
//// TestNet
// coreContractEncodedKey := "f847b8402b0bf247520770a4bad19e07f6d6b1e8f0542da564154087e2681b175b4432ec2c7b09a52d34dabe0a887ea0f96b067e52c6a0792dcff730fe78a6c5fbbf0a9c02038203e8"
// // Testnet FungibleToken
// err := appendKeyForAccount(a, "9a0766d93b6608b7", coreContractEncodedKey)
// if err != nil {
// return nil, err
// }
// // Testnet NonFungibleToken
// err = appendKeyForAccount(a, "631e88ae7f1d7c20", coreContractEncodedKey)
// if err != nil {
// return nil, err
// }
// // Testnet FlowToken
// err = appendKeyForAccount(a, "7e60df042a9c0868", coreContractEncodedKey)
// if err != nil {
// return nil, err
// }
// // Testnet FlowFees
// err = appendKeyForAccount(a, "912d5440f7e3769e", coreContractEncodedKey)
// if err != nil {
// return nil, err
// }
coreContractEncodedKey := "f847b8403588eb28b60e28d24c1e8b03f9a00f73ebd3f6707ee813e27d58ecb6439b8dde1413d7a74a7cc7e8939cbef2e0aa6acc51d5c7010afdb4c6dba55d4cc2ca8bed02018203e8"
// Mainnet FungibleToken
err := appendKeyForAccount(a, "f233dcee88fe0abe", coreContractEncodedKey)
if err != nil {
return nil, err
}
// Mainnet NonFungibleToken
err = appendKeyForAccount(a, "1d7e57aa55817448", coreContractEncodedKey)
if err != nil {
return nil, err
}
// Mainnet FlowToken
err = appendKeyForAccount(a, "1654653399040a61", coreContractEncodedKey)
if err != nil {
return nil, err
}
// Mainnet FlowFees
err = appendKeyForAccount(a, "f919ee77447b7497", coreContractEncodedKey)
if err != nil {
return nil, err
}
//// TestNet
// nonCoreContractEncodedKey := "f847b840a272d78cfa14eb248d95c12da8c6a24db9fda5ceddc07444080b49ef6cd15a06e88223af8acd235e2ff7a627adb81cf37d0a1384d9985de4bc7a7fc7eb86848402038203e8"
// // Testnet StakingProxy
// err = appendKeyForAccount(a, "7aad92e5a0715d21", nonCoreContractEncodedKey)
// if err != nil {
// return nil, err
// }
// // Testnet LockedTokens
// err = appendKeyForAccount(a, "95e019a17d0e23d7", nonCoreContractEncodedKey)
// if err != nil {
// return nil, err
// }
// Mainnet
nonCoreContractEncodedKey := "f847b8406e4f43f79d3c1d8cacb3d5f3e7aeedb29feaeb4559fdb71a97e2fd0438565310e87670035d83bc10fe67fe314dba5363c81654595d64884b1ecad1512a64e65e02018203e8"
// Mainnet StakingProxy
err = appendKeyForAccount(a, "62430cf28c26d095", nonCoreContractEncodedKey)
if err != nil {
return nil, err
}
// Mainnet LockedTokens
err = appendKeyForAccount(a, "8d0e87b65159ae63", nonCoreContractEncodedKey)
if err != nil {
return nil, err
}
return l.Payloads(), nil
}
func appendKeyForAccount(accounts *state.Accounts, addressInHex string, encodedKeyInHex string) error {
address := flow.HexToAddress(addressInHex)
ok, err := accounts.Exists(address)
if err != nil {
return err
}
if ok {
accountKeyBytes, err := hex.DecodeString(encodedKeyInHex)
if err != nil {
return err
}
accountKey, err := flow.DecodeRuntimeAccountPublicKey(accountKeyBytes, 0)
if err != nil {
return err
}
err = accounts.AppendPublicKey(address, accountKey)
if err != nil {
return err
}
} else {
// if not exist log and return gracefully
fmt.Println("warning account does not exist: ", addressInHex)
}
return nil
}
type view struct {
Parent *view
Ledger *led
}
func newView(payloads []ledger.Payload) *view {
return &view{
Ledger: newLed(payloads),
}
}
func (v *view) NewChild() state.View {
payload := make([]ledger.Payload, 0)
ch := newView(payload)
ch.Parent = v
return ch
}
func (v *view) DropDelta() {
v.Ledger.payloads = make(map[string]ledger.Payload)
}
func (v *view) MergeView(o state.View) error {
var other *view
var ok bool
if other, ok = o.(*view); !ok {
return fmt.Errorf("view type mismatch (given: %T, expected:Delta.View)", o)
}
for key, value := range other.Ledger.payloads {
v.Ledger.payloads[key] = value
}
return nil
}
func (v *view) Set(owner, controller, key string, value flow.RegisterValue) error {
return v.Ledger.Set(owner, controller, key, value)
}
func (v *view) Get(owner, controller, key string) (flow.RegisterValue, error) {
value, err := v.Ledger.Get(owner, controller, key)
if err != nil {
return nil, err
}
if len(value) > 0 {
return value, nil
}
if v.Parent != nil {
return v.Parent.Get(owner, controller, key)
}
return nil, nil
}
func (v *view) Touch(owner, controller, key string) error {
return v.Ledger.Touch(owner, controller, key)
}
func (v *view) Delete(owner, controller, key string) error {
return v.Ledger.Delete(owner, controller, key)
}
func (v *view) Payloads() []ledger.Payload {
return v.Ledger.Payloads()
}
type led struct {
payloads map[string]ledger.Payload
}
func (l *led) Set(owner, controller, key string, value flow.RegisterValue) error {
keyparts := []ledger.KeyPart{ledger.NewKeyPart(0, []byte(owner)),
ledger.NewKeyPart(1, []byte(controller)),
ledger.NewKeyPart(2, []byte(key))}
fk := fullKey(owner, controller, key)
l.payloads[fk] = ledger.Payload{Key: ledger.NewKey(keyparts), Value: ledger.Value(value)}
return nil
}
func (l *led) Get(owner, controller, key string) (flow.RegisterValue, error) {
fk := fullKey(owner, controller, key)
return flow.RegisterValue(l.payloads[fk].Value), nil
}
func (l *led) Delete(owner, controller, key string) error {
fk := fullKey(owner, controller, key)
delete(l.payloads, fk)
return nil
}
func (l *led) Touch(owner, controller, key string) error {
return nil
}
func (l *led) Payloads() []ledger.Payload {
ret := make([]ledger.Payload, 0, len(l.payloads))
for _, v := range l.payloads {
ret = append(ret, v)
}
return ret
}
func newLed(payloads []ledger.Payload) *led {
mapping := make(map[string]ledger.Payload)
for _, p := range payloads {
fk := fullKey(string(p.Key.KeyParts[0].Value),
string(p.Key.KeyParts[1].Value),
string(p.Key.KeyParts[2].Value))
mapping[fk] = p
}
return &led{
payloads: mapping,
}
}
func fullKey(owner, controller, key string) string {
return strings.Join([]string{owner, controller, key}, "\x1F")
}