-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathassembly.go
More file actions
330 lines (294 loc) · 10 KB
/
Copy pathassembly.go
File metadata and controls
330 lines (294 loc) · 10 KB
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
321
322
323
324
325
326
327
328
329
330
package integration
import (
"crypto/ecdsa"
"errors"
"fmt"
"path"
"github.com/Fantom-foundation/lachesis-base/abft"
"github.com/Fantom-foundation/lachesis-base/hash"
"github.com/Fantom-foundation/lachesis-base/inter/idx"
"github.com/Fantom-foundation/lachesis-base/kvdb"
"github.com/Fantom-foundation/lachesis-base/kvdb/multidb"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/keycard-go/hexutils"
"github.com/Fantom-foundation/go-opera/gossip"
"github.com/Fantom-foundation/go-opera/opera/genesis"
"github.com/Fantom-foundation/go-opera/utils/adapters/vecmt2dagidx"
"github.com/Fantom-foundation/go-opera/utils/compactdb"
"github.com/Fantom-foundation/go-opera/vecmt"
)
var (
MetadataPrefix = hexutils.HexToBytes("0068c2927bf842c3e9e2f1364494a33a752db334b9a819534bc9f17d2c3b4e5970008ff519d35a86f29fcaa5aae706b75dee871f65f174fcea1747f2915fc92158f6bfbf5eb79f65d16225738594bffb")
FlushIDKey = append(common.CopyBytes(MetadataPrefix), 0x0c)
TablesKey = append(common.CopyBytes(MetadataPrefix), 0x0d)
)
// GenesisMismatchError is raised when trying to overwrite an existing
// genesis block with an incompatible one.
type GenesisMismatchError struct {
Stored, New hash.Hash
}
// Error implements error interface.
func (e *GenesisMismatchError) Error() string {
return fmt.Sprintf("database contains incompatible genesis (have %s, new %s)", e.Stored.String(), e.New.String())
}
type Configs struct {
Opera gossip.Config
OperaStore gossip.StoreConfig
Lachesis abft.Config
LachesisStore abft.StoreConfig
VectorClock vecmt.IndexConfig
DBs DBsConfig
}
func panics(name string) func(error) {
return func(err error) {
log.Crit(fmt.Sprintf("%s error", name), "err", err)
}
}
func mustOpenDB(producer kvdb.DBProducer, name string) kvdb.Store {
db, err := producer.OpenDB(name)
if err != nil {
utils.Fatalf("Failed to open '%s' database: %v", name, err)
}
return db
}
func getStores(producer kvdb.FlushableDBProducer, cfg Configs) (*gossip.Store, *abft.Store) {
gdb := gossip.NewStore(producer, cfg.OperaStore)
cMainDb := mustOpenDB(producer, "lachesis")
cGetEpochDB := func(epoch idx.Epoch) kvdb.Store {
return mustOpenDB(producer, fmt.Sprintf("lachesis-%d", epoch))
}
cdb := abft.NewStore(cMainDb, cGetEpochDB, panics("Lachesis store"), cfg.LachesisStore)
return gdb, cdb
}
func rawApplyGenesis(gdb *gossip.Store, cdb *abft.Store, g genesis.Genesis, cfg Configs) error {
_, _, _, err := rawMakeEngine(gdb, cdb, &g, cfg)
return err
}
func rawMakeEngine(gdb *gossip.Store, cdb *abft.Store, g *genesis.Genesis, cfg Configs) (*abft.Lachesis, *vecmt.Index, gossip.BlockProc, error) {
blockProc := gossip.DefaultBlockProc()
if g != nil {
_, err := gdb.ApplyGenesis(*g)
if err != nil {
return nil, nil, blockProc, fmt.Errorf("failed to write Gossip genesis state: %v", err)
}
err = cdb.ApplyGenesis(&abft.Genesis{
Epoch: gdb.GetEpoch(),
Validators: gdb.GetValidators(),
})
if err != nil {
return nil, nil, blockProc, fmt.Errorf("failed to write Lachesis genesis state: %v", err)
}
}
// create consensus
vecClock := vecmt.NewIndex(panics("Vector clock"), cfg.VectorClock)
engine := abft.NewLachesis(cdb, &GossipStoreAdapter{gdb}, vecmt2dagidx.Wrap(vecClock), panics("Lachesis"), cfg.Lachesis)
return engine, vecClock, blockProc, nil
}
func applyGenesis(dbs kvdb.FlushableDBProducer, g genesis.Genesis, cfg Configs) error {
gdb, cdb := getStores(dbs, cfg)
defer gdb.Close()
defer cdb.Close()
log.Info("Applying genesis state")
err := rawApplyGenesis(gdb, cdb, g, cfg)
if err != nil {
return err
}
err = gdb.Commit()
if err != nil {
return err
}
return nil
}
func migrate(dbs kvdb.FlushableDBProducer, cfg Configs) error {
gdb, cdb := getStores(dbs, cfg)
defer gdb.Close()
defer cdb.Close()
err := gdb.Commit()
if err != nil {
return err
}
return nil
}
func CheckStateInitialized(chaindataDir string, cfg DBsConfig) error {
if isInterrupted(chaindataDir) {
return errors.New("genesis processing isn't finished")
}
runtimeProducers, runtimeScopedProducers := SupportedDBs(chaindataDir, cfg.RuntimeCache)
dbs, err := MakeMultiProducer(runtimeProducers, runtimeScopedProducers, cfg.Routing)
if err != nil {
return err
}
return dbs.Close()
}
func compactDB(typ multidb.TypeName, name string, producer kvdb.DBProducer) error {
humanName := path.Join(string(typ), name)
db, err := producer.OpenDB(name)
defer db.Close()
if err != nil {
return err
}
return compactdb.Compact(db, humanName)
}
func makeEngine(chaindataDir string, g *genesis.Genesis, genesisProc bool, cfg Configs) (*abft.Lachesis, *vecmt.Index, *gossip.Store, *abft.Store, gossip.BlockProc, func() error, error) {
// Genesis processing
if genesisProc {
setGenesisProcessing(chaindataDir)
// use increased DB cache for genesis processing
genesisProducers, _ := SupportedDBs(chaindataDir, cfg.DBs.GenesisCache)
if g == nil {
return nil, nil, nil, nil, gossip.BlockProc{}, nil, fmt.Errorf("missing --genesis flag for an empty datadir")
}
dbs, err := MakeDirectMultiProducer(genesisProducers, cfg.DBs.Routing)
if err != nil {
return nil, nil, nil, nil, gossip.BlockProc{}, nil, fmt.Errorf("failed to make DB multi-producer: %v", err)
}
err = applyGenesis(dbs, *g, cfg)
if err != nil {
_ = dbs.Close()
return nil, nil, nil, nil, gossip.BlockProc{}, nil, fmt.Errorf("failed to apply genesis state: %v", err)
}
_ = dbs.Close()
setGenesisComplete(chaindataDir)
}
// Compact DBs after first launch
if genesisProc {
genesisProducers, _ := SupportedDBs(chaindataDir, cfg.DBs.GenesisCache)
for typ, p := range genesisProducers {
for _, name := range p.Names() {
if err := compactDB(typ, name, p); err != nil {
return nil, nil, nil, nil, gossip.BlockProc{}, nil, err
}
}
}
}
// Check DBs are synced
{
err := CheckStateInitialized(chaindataDir, cfg.DBs)
if err != nil {
return nil, nil, nil, nil, gossip.BlockProc{}, nil, err
}
}
// Migration
{
runtimeProducers, _ := SupportedDBs(chaindataDir, cfg.DBs.RuntimeCache)
dbs, err := MakeDirectMultiProducer(runtimeProducers, cfg.DBs.Routing)
if err != nil {
return nil, nil, nil, nil, gossip.BlockProc{}, nil, err
}
err = migrate(dbs, cfg)
_ = dbs.Close()
if err != nil {
return nil, nil, nil, nil, gossip.BlockProc{}, nil, fmt.Errorf("failed to migrate state: %v", err)
}
}
// Live setup
runtimeProducers, runtimeScopedProducers := SupportedDBs(chaindataDir, cfg.DBs.RuntimeCache)
// open flushable DBs
dbs, err := MakeMultiProducer(runtimeProducers, runtimeScopedProducers, cfg.DBs.Routing)
if err != nil {
return nil, nil, nil, nil, gossip.BlockProc{}, nil, err
}
gdb, cdb := getStores(dbs, cfg)
defer func() {
if err != nil {
gdb.Close()
cdb.Close()
dbs.Close()
}
}()
// compare genesis with the input
genesisID := gdb.GetGenesisID()
if genesisID == nil {
err = errors.New("malformed chainstore: genesis ID is not written")
return nil, nil, nil, nil, gossip.BlockProc{}, dbs.Close, err
}
if g != nil {
if *genesisID != g.GenesisID {
err = &GenesisMismatchError{*genesisID, g.GenesisID}
return nil, nil, nil, nil, gossip.BlockProc{}, dbs.Close, err
}
}
engine, vecClock, blockProc, err := rawMakeEngine(gdb, cdb, nil, cfg)
if err != nil {
err = fmt.Errorf("failed to make engine: %v", err)
return nil, nil, nil, nil, gossip.BlockProc{}, dbs.Close, err
}
if genesisProc {
err = gdb.Commit()
if err != nil {
err = fmt.Errorf("failed to commit DBs: %v", err)
return nil, nil, nil, nil, gossip.BlockProc{}, dbs.Close, err
}
}
return engine, vecClock, gdb, cdb, blockProc, dbs.Close, nil
}
// MakeEngine makes consensus engine from config.
func MakeEngine(chaindataDir string, g *genesis.Genesis, cfg Configs) (*abft.Lachesis, *vecmt.Index, *gossip.Store, *abft.Store, gossip.BlockProc, func() error) {
// Legacy DBs migrate
if cfg.DBs.MigrationMode != "reformat" && cfg.DBs.MigrationMode != "rebuild" && cfg.DBs.MigrationMode != "" {
utils.Fatalf("MigrationMode must be 'reformat' or 'rebuild'")
}
if !isEmpty(path.Join(chaindataDir, "gossip")) {
MakeDBDirs(chaindataDir)
genesisProducers, _ := SupportedDBs(chaindataDir, cfg.DBs.GenesisCache)
dbs, err := MakeDirectMultiProducer(genesisProducers, cfg.DBs.Routing)
if err != nil {
utils.Fatalf("Failed to make engine: %v", err)
}
err = migrateLegacyDBs(chaindataDir, dbs, cfg.DBs.MigrationMode, cfg.DBs.Routing)
_ = dbs.Close()
if err != nil {
utils.Fatalf("Failed to migrate state: %v", err)
}
}
dropAllDBsIfInterrupted(chaindataDir)
firstLaunch := isEmpty(chaindataDir)
MakeDBDirs(chaindataDir)
engine, vecClock, gdb, cdb, blockProc, closeDBs, err := makeEngine(chaindataDir, g, firstLaunch, cfg)
if err != nil {
if firstLaunch {
dropAllDBs(chaindataDir)
}
utils.Fatalf("Failed to make engine: %v", err)
}
rules := gdb.GetRules()
genesisID := gdb.GetGenesisID()
if firstLaunch {
log.Info("Applied genesis state", "name", rules.Name, "id", rules.NetworkID, "genesis", genesisID.String())
} else {
log.Info("Genesis is already written", "name", rules.Name, "id", rules.NetworkID, "genesis", genesisID.String())
}
return engine, vecClock, gdb, cdb, blockProc, closeDBs
}
// SetAccountKey sets key into accounts manager and unlocks it with pswd.
func SetAccountKey(
am *accounts.Manager, key *ecdsa.PrivateKey, pswd string,
) (
acc accounts.Account,
) {
kss := am.Backends(keystore.KeyStoreType)
if len(kss) < 1 {
log.Crit("Keystore is not found")
return
}
ks := kss[0].(*keystore.KeyStore)
acc = accounts.Account{
Address: crypto.PubkeyToAddress(key.PublicKey),
}
imported, err := ks.ImportECDSA(key, pswd)
if err == nil {
acc = imported
} else if err.Error() != "account already exists" {
log.Crit("Failed to import key", "err", err)
}
err = ks.Unlock(acc, pswd)
if err != nil {
log.Crit("failed to unlock key", "err", err)
}
return
}