-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbootstrapper.ts
More file actions
164 lines (121 loc) · 5.13 KB
/
Copy pathbootstrapper.ts
File metadata and controls
164 lines (121 loc) · 5.13 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
import type { Contracts } from "@mainsail/contracts";
import { Identifiers } from "@mainsail/constants";
import { inject, injectable, optional } from "@mainsail/container";
@injectable()
export class Bootstrapper {
@inject(Identifiers.Application.Instance)
private readonly app!: Contracts.Kernel.Application;
@inject(Identifiers.Consensus.Service)
private readonly consensus!: Contracts.Consensus.Service;
@inject(Identifiers.Consensus.Bootstrapper)
private readonly consensusBootstrapper!: Contracts.Consensus.Bootstrapper;
@inject(Identifiers.State.Store)
private stateStore!: Contracts.State.Store;
@inject(Identifiers.State.State)
private readonly state!: Contracts.State.State;
@inject(Identifiers.Cryptography.Configuration)
private readonly configuration!: Contracts.Crypto.Configuration;
@inject(Identifiers.Validator.Repository)
private readonly validatorRepository!: Contracts.Validator.ValidatorRepository;
@inject(Identifiers.P2P.Server)
private readonly p2pServer!: Contracts.P2P.Server;
@inject(Identifiers.P2P.Service)
private readonly p2pService!: Contracts.P2P.Service;
@inject(Identifiers.Cryptography.Commit.Factory)
private readonly commitFactory!: Contracts.Crypto.CommitFactory;
@inject(Identifiers.Database.Service)
private readonly databaseService!: Contracts.Database.DatabaseService;
@inject(Identifiers.ValidatorSet.Service)
private readonly validatorSet!: Contracts.ValidatorSet.Service;
@inject(Identifiers.Processor.BlockProcessor)
private readonly blockProcessor!: Contracts.Processor.BlockProcessor;
@inject(Identifiers.Consensus.CommitState.Factory)
private readonly commitStateFactory!: Contracts.Consensus.CommitStateFactory;
@inject(Identifiers.ApiSync.Service)
@optional()
private readonly apiSync?: Contracts.ApiSync.Service;
@inject(Identifiers.Snapshot.Legacy.Importer)
private readonly snapshotImporter!: Contracts.Snapshot.LegacyImporter;
@inject(Identifiers.TransactionPool.Worker)
private readonly txPoolWorker!: Contracts.TransactionPool.Worker;
@inject(Identifiers.Evm.Worker)
private readonly evmWorker!: Contracts.Evm.Worker;
public async bootstrap(): Promise<void> {
await this.#setGenesisCommit();
await this.#checkStoredGenesisCommit();
if (await this.databaseService.isEmpty()) {
await this.#initGenesisState();
} else {
await this.#initPostGenesisState();
}
await this.validatorSet.restore();
// After genesis commit to restore all data
await this.#initApiSync();
this.snapshotImporter.dispose();
this.state.setBootstrap(false);
this.validatorRepository.printLoadedValidators();
void this.txPoolWorker
.start(this.stateStore.getBlockNumber())
.catch((error) => this.app.terminate("tx-pool worker failed to start", error));
void this.evmWorker
.start(this.stateStore.getBlockNumber())
.catch((error) => this.app.terminate("evm-api worker failed to start", error));
await this.consensus.run(await this.consensusBootstrapper.bootstrap());
await this.p2pServer.boot();
await this.p2pService.boot();
}
async #setGenesisCommit(): Promise<void> {
const genesisCommitJson = this.configuration.getGenesisCommit();
const genesisCommit = await this.commitFactory.fromJson(genesisCommitJson);
this.stateStore.setGenesisCommit(genesisCommit);
}
async #checkStoredGenesisCommit(): Promise<void> {
const genesisBlock = await this.databaseService.getBlock(this.configuration.getGenesisHeight());
if (!genesisBlock) {
return;
}
if (this.stateStore.getGenesisCommit().block.hash !== genesisBlock.hash) {
throw new Error("Block from crypto.json doesn't match stored genesis block");
}
}
async #initApiSync(): Promise<void> {
if (this.apiSync) {
await this.apiSync.bootstrap();
}
}
async #initGenesisState(): Promise<void> {
await this.#tryImportSnapshot();
await this.#processGenesisBlock();
}
async #initPostGenesisState(): Promise<void> {
const commit = await this.databaseService.getLastCommit();
this.stateStore.setLastBlock(commit.block);
this.stateStore.setTotalRound(this.databaseService.getState().totalRound);
}
async #processGenesisBlock(): Promise<void> {
const genesisBlock = this.stateStore.getGenesisCommit();
await this.#processCommit(genesisBlock);
}
async #processCommit(commit: Contracts.Crypto.Commit): Promise<void> {
const commitState = this.commitStateFactory(commit);
const result = await this.blockProcessor.process(commitState);
if (!result.success) {
throw new Error(`Block is not processed.`);
}
commitState.setProcessorResult(result);
await this.blockProcessor.commit(commitState);
}
async #tryImportSnapshot(): Promise<void> {
// TODO: Move this check to snapshot importer
const genesisBlock = this.stateStore.getGenesisCommit();
const milestone = this.configuration.getMilestone();
// assume snapshot is present if the previous block points to a non-zero hash
if (genesisBlock.block.parentHash === "0000000000000000000000000000000000000000000000000000000000000000") {
if (milestone.snapshot) {
throw new Error("Previous block is set to snapshot, but there is no snapshot defined in milestones");
}
return;
}
await this.snapshotImporter.run(genesisBlock);
}
}