This repository has been archived by the owner on Feb 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert.go
148 lines (130 loc) · 4.58 KB
/
convert.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
package main
import (
"bytes"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/common/debug"
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
"github.com/ledgerwatch/turbo-geth/crypto"
"github.com/ledgerwatch/turbo-geth/ethdb"
)
var preimagePrefix = []byte("secure-key-")
var emptyCodeHash = crypto.Keccak256Hash(nil)
var emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421").Bytes()
func ConvertSnapshot(from EthereumDatabase, to TurboDatabase, start []byte, maxOperationsPerTransaction uint, trieDB *trie.Database, stateDB *state.StateDB, t *trie.Trie, mut ethdb.DbWithPendingMutations) (uint, []byte, error) {
iterator := trie.NewIterator(t.NodeIterator(start))
var counter uint
for iterator.Next() {
if counter > maxOperationsPerTransaction {
_, err := mut.Commit()
log.Info("entries has just been written", "entries", counter)
return counter, iterator.Key, err
}
gethAccount := state.Account{} // go-ethereum account
var tAccount accounts.Account // turbo-geth account
// setup account
tAccount.Nonce = gethAccount.Nonce
if gethAccount.Balance != nil {
tAccount.Balance = *gethAccount.Balance
}
// Decode geth account
err := rlp.Decode(bytes.NewBuffer(iterator.Value), &gethAccount)
if err != nil {
_, _ = mut.Commit()
return counter, nil, err
}
// Storage Bucket
storageTrie, err := trie.New(gethAccount.Root, trieDB)
if err != nil {
_, _ = mut.Commit()
return counter, nil, err
}
err, isContract := makeStorage(mut, storageTrie, iterator.Key, &counter)
if err != nil {
_, _ = mut.Commit()
return counter, nil, err
}
if isContract {
tAccount.Root.SetBytes(gethAccount.Root.Bytes())
tAccount.CodeHash.SetBytes(gethAccount.CodeHash)
tAccount.Incarnation = 1
err := makeCode(mut, from, stateDB, iterator.Key)
counter++
if err != nil {
_, _ = mut.Commit()
return counter, nil, err
}
if debug.IsThinHistory() {
err := makeContractCode(mut, from, tAccount, stateDB, iterator.Key)
counter++
if err != nil {
_, _ = mut.Commit()
return counter, nil, err
}
}
} else {
tAccount.Incarnation = 0
tAccount.Root.SetBytes(emptyRoot)
tAccount.CodeHash = emptyCodeHash
}
// Account Bucket
bytesAccount := make([]byte, tAccount.EncodingLengthForStorage())
tAccount.EncodeForStorage(bytesAccount)
counter++
err = mut.Put(dbutils.AccountsBucket, iterator.Key, bytesAccount)
if err != nil {
_, _ = mut.Commit()
return counter, nil, err
}
}
_, err := mut.Commit()
log.Info("entries has just been written", "entries", counter)
return counter, iterator.Key, err
}
func makeStorage(mut ethdb.DbWithPendingMutations, t *trie.Trie, accountKey []byte, counter *uint) (error, bool) {
iterator := trie.NewIterator(t.NodeIterator(nil))
var isContract bool
for iterator.Next() {
buffer := bytes.NewBuffer(iterator.Value)
storageValue := []byte{}
rlp.Decode(buffer, &storageValue)
err := mut.Put(dbutils.StorageBucket, append(accountKey, iterator.Key...), storageValue)
if err != nil {
return err, true
}
isContract = true
*counter++
}
return nil, isContract
}
func makeCode(mut ethdb.DbWithPendingMutations, from EthereumDatabase, stateDB *state.StateDB, accountKey []byte) error {
address, err := getAddress(from, accountKey)
if err != nil {
return err
}
return mut.Put(dbutils.CodeBucket, accountKey, stateDB.GetCode(address))
}
func makeContractCode(mut ethdb.DbWithPendingMutations, from EthereumDatabase, tAccount accounts.Account, stateDB *state.StateDB, accountKey []byte) error {
addressHash, err := getAddressHash(from, accountKey)
if err != nil {
return err
}
return mut.Put(dbutils.ContractCodeBucket, append(addressHash.Bytes(), byte(tAccount.Incarnation)), tAccount.CodeHash.Bytes())
}
func getAddress(from EthereumDatabase, preimage []byte) (common.Address, error) {
addressBytes, err := from.db.Get(append(preimagePrefix, preimage...))
return common.BytesToAddress(addressBytes), err
}
func getAddressHash(from EthereumDatabase, preimage []byte) (common.Hash, error) {
addressBytes, err := from.db.Get(append(preimagePrefix, preimage...))
return common.BytesToHash(addressBytes), err
}
func getBlockNumber(db EthereumDatabase) uint64 {
hash := rawdb.ReadHeadBlockHash(db.db)
return *rawdb.ReadHeaderNumber(db.db, hash)
}