-
Notifications
You must be signed in to change notification settings - Fork 182
/
iavl2mpt.go
320 lines (274 loc) · 11.1 KB
/
iavl2mpt.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package mpt
import (
"bytes"
"fmt"
"log"
ethcmn "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
ethstate "github.com/ethereum/go-ethereum/core/state"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
"github.com/okex/exchain/app"
apptypes "github.com/okex/exchain/app/types"
"github.com/okex/exchain/libs/cosmos-sdk/server"
"github.com/okex/exchain/libs/cosmos-sdk/store/mpt"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
authexported "github.com/okex/exchain/libs/cosmos-sdk/x/auth/exported"
authtypes "github.com/okex/exchain/libs/cosmos-sdk/x/auth/types"
"github.com/okex/exchain/libs/iavl"
evmtypes "github.com/okex/exchain/x/evm/types"
"github.com/spf13/cobra"
)
func iavl2mptCmd(ctx *server.Context) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "iavl2mpt acc/evm",
Short: "migrate data from iavl to mpt",
PreRunE: func(cmd *cobra.Command, args []string) error {
return checkValidKey(args[0])
},
Run: func(cmd *cobra.Command, args []string) {
log.Printf("--------- migrate %s start ---------\n", args[0])
switch args[0] {
case accStoreKey:
migrateAccFromIavlToMpt(ctx)
case evmStoreKey:
migrateEvmFromIavlToMpt(ctx)
case legacyStoreKey:
migrateEvmLegacyFromIavlToIavl(ctx)
}
log.Printf("--------- migrate %s end ---------\n", args[0])
},
}
return cmd
}
// migrateAccFromIavlToMpt migrate acc data from iavl to mpt
func migrateAccFromIavlToMpt(ctx *server.Context) {
// 0.1 initialize App and context
migrationApp := newMigrationApp(ctx)
cmCtx := migrationApp.MockContext()
committedHeight := cmCtx.BlockHeight() - 1
// 0.1 initialize database of acc mpt
accMptDb := mpt.InstanceOfMptStore()
accTrie, err := accMptDb.OpenTrie(mpt.NilHash)
panicError(err)
// 0.2 initialize database of evm mpt
evmMptDb := mpt.InstanceOfMptStore()
evmTrie, err := evmMptDb.OpenTrie(mpt.NilHash)
panicError(err)
// 1.1 update GlobalNumber to mpt
accountNumber := migrationApp.AccountKeeper.GetNextAccountNumber(cmCtx)
err = accTrie.TryUpdate(authtypes.GlobalAccountNumberKey, migrationApp.Codec().MustMarshalBinaryLengthPrefixed(accountNumber))
panicError(err)
fmt.Println("GlobalNumber", accountNumber)
// 1.2 update every account to mpt
count, contractCount := 0, 0
batch := evmMptDb.TrieDB().DiskDB().NewBatch()
migrationApp.AccountKeeper.MigrateAccounts(cmCtx, func(account authexported.Account, key, value []byte) (stop bool) {
count++
if len(value) == 0 {
log.Printf("[warning] %s has nil value\n", account.GetAddress().String())
}
// update acc mpt for every account
panicError(accTrie.TryUpdate(key, value))
if count%100 == 0 {
pushData2Database(accMptDb, accTrie, committedHeight, false)
log.Println(count)
}
// check if the account is a contract account
if ethAcc, ok := account.(*apptypes.EthAccount); ok {
if !bytes.Equal(ethAcc.CodeHash, mpt.EmptyCodeHashBytes) {
contractCount++
// update evm mpt. Key is the address of the contract; Value is the empty root hash
panicError(evmTrie.TryUpdate(ethAcc.EthAddress().Bytes(), mpt.EmptyRootHashBytes))
if contractCount%100 == 0 {
pushData2Database(evmMptDb, evmTrie, committedHeight, true)
}
// write code to evm.db in direct
codeHash := ethcmn.BytesToHash(ethAcc.CodeHash)
rawdb.WriteCode(batch, codeHash, migrationApp.EvmKeeper.GetCodeByHash(cmCtx, codeHash))
writeDataToRawdb(batch)
}
}
return false
})
// 1.3 make sure the last data is committed to the database
pushData2Database(accMptDb, accTrie, committedHeight, false)
pushData2Database(evmMptDb, evmTrie, committedHeight, true)
fmt.Println(fmt.Sprintf("Successfully migrate %d account (include %d contract account) at version %d", count, contractCount, committedHeight))
}
// migrateEvmFromIavlToMpt migrate evm data from iavl to mpt
func migrateEvmFromIavlToMpt(ctx *server.Context) {
// 0.1 initialize App and context
migrationApp := newMigrationApp(ctx)
cmCtx := migrationApp.MockContext()
// 0.1 initialize database of evm mpt, and open trie based on the latest root hash
evmMptDb := mpt.InstanceOfMptStore()
rootHash := migrationApp.EvmKeeper.GetMptRootHash(uint64(cmCtx.BlockHeight() - 1))
evmTrie, err := evmMptDb.OpenTrie(rootHash)
panicError(err)
/* Here are prefix keys from evm module:
KeyPrefixBlockHash
KeyPrefixBloom
KeyPrefixCode
KeyPrefixStorage
KeyPrefixChainConfig
KeyPrefixHeightHash
KeyPrefixContractDeploymentWhitelist
KeyPrefixContractBlockedList
So, here are data list about the migration process:
1. Accounts -> accMpt
Code -> rawdb (note: done in iavl2mpt acc cmd)
Storage -> evmMpt
2. BlockHash、HeightHash -> rawdb
3. Bloom -> rawdb
4. ChainConfig -> iavl // do it in abci
5. ContractDeploymentWhitelist、ContractBlockedList -> iavl // do it in abci
*/
// 1. Migratess Accounts、Storage -> mpt
migrateContractToMpt(migrationApp, cmCtx, evmMptDb, evmTrie)
// 2. Migrates BlockHash、HeightHash -> rawdb
batch := evmMptDb.TrieDB().DiskDB().NewBatch()
miragteBlockHashesToDb(migrationApp, cmCtx, batch)
// 3. Migrates Bloom -> rawdb
miragteBloomsToDb(migrationApp, cmCtx, batch)
/*
// 4. save an empty evmlegacy iavl tree in mirgate height
upgradedPrefixDb := dbm.NewPrefixDB(migrationApp.GetDB(), []byte(iavlEvmLegacyKey))
upgradedTree, err := iavl.NewMutableTreeWithOpts(upgradedPrefixDb, iavlstore.IavlCacheSize, nil)
panicError(err)
_, version, err := upgradedTree.SaveVersionSync(cmCtx.BlockHeight()-1, false)
panicError(err)
fmt.Printf("Successfully save an empty evmlegacy iavl tree in %d\n", version)
*/
}
// 1. migrateContractToMpt Migrates Accounts、Code、Storage
func migrateContractToMpt(migrationApp *app.OKExChainApp, cmCtx sdk.Context, evmMptDb ethstate.Database, evmTrie ethstate.Trie) {
committedHeight := cmCtx.BlockHeight() - 1
count := 0
itr := trie.NewIterator(evmTrie.NodeIterator(nil))
for itr.Next() {
count++
addr := ethcmn.BytesToAddress(evmTrie.GetKey(itr.Key))
// 1.1 get solo contract mpt
contractTrie := getStorageTrie(evmMptDb, ethcrypto.Keccak256Hash(addr[:]), mpt.NilHash)
_ = migrationApp.EvmKeeper.ForEachStorage(cmCtx, addr, func(key, value ethcmn.Hash) bool {
// Encoding []byte cannot fail, ok to ignore the error.
v, _ := rlp.EncodeToBytes(ethcmn.TrimLeftZeroes(value[:]))
if len(v) == 0 {
log.Printf("[warning] %s in %s has nil value\n", addr.String(), key.String())
}
// 1.2 set every storage into solo
panicError(contractTrie.TryUpdate(key.Bytes(), v))
return false
})
// 1.3 calculate rootHash of contract mpt
rootHash, err := contractTrie.Commit(nil)
panicError(err)
// 1.4 set the rootHash of contract mpt into evm mpt
panicError(evmTrie.TryUpdate(addr[:], rootHash.Bytes()))
if count%100 == 0 {
pushData2Database(evmMptDb, evmTrie, committedHeight, true)
log.Println(count)
}
}
pushData2Database(evmMptDb, evmTrie, committedHeight, true)
fmt.Printf("Successfully migrate %d contract stroage at version %d\n", count, committedHeight)
}
// 2. miragteBlockHashesToDb Migrates BlockHash/HeightHash
func miragteBlockHashesToDb(migrationApp *app.OKExChainApp, cmCtx sdk.Context, batch ethdb.Batch) {
count := 0
migrationApp.EvmKeeper.IterateBlockHash(cmCtx, func(key []byte, value []byte) bool {
count++
panicError(batch.Put(key, value))
panicError(batch.Put(append(evmtypes.KeyPrefixHeightHash, value...), key[1:]))
if count%1000 == 0 {
writeDataToRawdb(batch)
log.Printf("write block hash between %d~%d\n", count-1000, count)
}
return false
})
writeDataToRawdb(batch)
fmt.Printf("Successfully migrate %d block-hashes\n", count)
}
// 3. miragteBloomsToDb Migrates Bloom
func miragteBloomsToDb(migrationApp *app.OKExChainApp, cmCtx sdk.Context, batch ethdb.Batch) {
count := 0
migrationApp.EvmKeeper.IterateBlockBloom(cmCtx, func(key []byte, value []byte) bool {
count++
panicError(batch.Put(key, value))
if count%1000 == 0 {
writeDataToRawdb(batch)
log.Printf("write bloom between %d~%d\n", count-1000, count)
}
return false
})
writeDataToRawdb(batch)
fmt.Printf("Successfully migrate %d blooms\n", count)
}
// migrateEvmLegacyFromIavlToIavl only used for test!
func migrateEvmLegacyFromIavlToIavl(ctx *server.Context) {
// 0.1 initialize App and context
migrationApp := newMigrationApp(ctx)
cmCtx := migrationApp.MockContext()
allParams := readAllParams(migrationApp)
upgradedTree := getUpgradedTree(migrationApp.GetDB(), []byte(KeyParams), true)
// 0. migrate latest params to pre-latest version
for key, value := range allParams {
upgradedTree.Set([]byte(key), value)
}
fmt.Printf("Successfully update all module params\n")
// 1. Migrates ChainConfig -> evmlegacy iavl
config, _ := migrationApp.EvmKeeper.GetChainConfig(cmCtx)
upgradedTree.Set(generateKeyForCustomParamStore(evmStoreKey, evmtypes.KeyPrefixChainConfig),
migrationApp.Codec().MustMarshalBinaryBare(config))
fmt.Printf("Successfully migrate chain config\n")
// 2. Migrates ContractDeploymentWhitelist、ContractBlockedList
csdb := evmtypes.CreateEmptyCommitStateDB(migrationApp.EvmKeeper.GenerateCSDBParams(), cmCtx)
// 5.1、deploy white list
whiteList := csdb.GetContractDeploymentWhitelist()
for i := 0; i < len(whiteList); i++ {
upgradedTree.Set(generateKeyForCustomParamStore(evmStoreKey, evmtypes.GetContractDeploymentWhitelistMemberKey(whiteList[i])),
[]byte(""))
}
// 5.2、deploy blocked list
blockedList := csdb.GetContractBlockedList()
for i := 0; i < len(blockedList); i++ {
upgradedTree.Set(generateKeyForCustomParamStore(evmStoreKey, evmtypes.GetContractBlockedListMemberKey(blockedList[i])),
[]byte(""))
}
// 5.3、deploy blocked method list
bcml := csdb.GetContractMethodBlockedList()
count := 0
for i := 0; i < len(bcml); i++ {
if !bcml[i].IsAllMethodBlocked() {
count++
evmtypes.SortContractMethods(bcml[i].BlockMethods)
value := migrationApp.Codec().MustMarshalJSON(bcml[i].BlockMethods)
sortedValue := sdk.MustSortJSON(value)
upgradedTree.Set(generateKeyForCustomParamStore(evmStoreKey, evmtypes.GetContractBlockedListMemberKey(bcml[i].Address)),
sortedValue)
}
}
fmt.Printf("Successfully migrate %d addresses in white list, %d addresses in blocked list, %d addresses in method block list\n",
len(whiteList), len(blockedList), count)
iavl.SetIgnoreVersionCheck(true)
hash, version, _, err := upgradedTree.SaveVersion(false)
panicError(err)
fmt.Printf("Successfully save evmlegacy, version: %d, hash: %s\n", version, ethcmn.BytesToHash(hash))
}
func readAllParams(app *app.OKExChainApp) map[string][]byte{
tree := getUpgradedTree(app.GetDB(), []byte(KeyParams), false)
paramsMap := make(map[string][]byte)
tree.IterateRange(nil, nil, true, func(key, value []byte) bool{
paramsMap[string(key)] = value
return false
})
return paramsMap
}
func generateKeyForCustomParamStore(storeKey string, key []byte) []byte {
prefix := []byte("custom/" + storeKey + "/")
return append(prefix, key...)
}