This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
node.ts
192 lines (175 loc) · 6.3 KB
/
node.ts
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
import { Provider } from "@ethersproject/providers";
import { BigNumber, ethers } from "ethers";
import { EventEmitter } from "events";
import { FastifyInstance } from "fastify";
import { storageManagerFactory } from "../factory";
import * as mcl from "../../ts/mcl";
import { Bidder } from "./services/bidder";
import { SyncerService, SyncMode } from "./services/syncer";
import { Packer } from "./services/packer";
import { TransferPool } from "./features/transfer";
import { BurnAuctionWrapper } from "../burnAuction";
import { RPC } from "./services/rpc";
import { CoreAPI } from "./coreAPI";
import { SyncCompleteEvent } from "./constants";
import { ClientConfig } from "./config";
import { Genesis } from "../genesis";
import { EmptyConfigPropError, MissingConfigPropError } from "../exceptions";
import { close as closeDB } from "./database/connection";
export type NodeModes = {
isProposer: boolean;
isWatcher: boolean;
};
export class HubbleNode {
constructor(
private readonly modes: NodeModes,
private readonly provider: Provider,
private readonly eventEmitter: EventEmitter,
private readonly syncer: SyncerService,
private readonly packer?: Packer,
private readonly bidder?: Bidder,
private readonly rpc?: RPC
) {}
public static async init(config: ClientConfig, fast: FastifyInstance) {
await mcl.init();
const genesis = await Genesis.fromConfig(config.genesisPath);
const provider = new ethers.providers.JsonRpcProvider(
config.providerUrl,
genesis.auxiliary.chainid
);
provider.on("error", err => {
console.error(err);
});
const { MAX_DEPTH } = genesis.parameters;
const storageManager = await storageManagerFactory({
stateTreeDepth: MAX_DEPTH,
pubkeyTreeDepth: MAX_DEPTH
});
const signer = provider.getSigner();
const api = CoreAPI.new(storageManager, genesis, provider, signer);
const syncer = new SyncerService(api);
let transferPool;
let packer;
let bidder;
const modes = this.getNodeModes(config);
if (modes.isProposer) {
const { feeReceivers, willingnessToBid, maxPendingTransactions } =
config.proposer || {};
if (!feeReceivers) {
throw new MissingConfigPropError("proposer.feeRecievers");
}
if (!feeReceivers.length) {
throw new EmptyConfigPropError("proposer.feeRecievers");
}
if (!willingnessToBid) {
throw new MissingConfigPropError("proposer.willingnessToBid");
}
transferPool = new TransferPool(
storageManager.state,
feeReceivers,
maxPendingTransactions
);
packer = new Packer(api, transferPool);
bidder = await Bidder.new(
BigNumber.from(willingnessToBid),
api.contracts.burnAuction
);
}
if (modes.isWatcher) {
throw new Error("watcher is currently not supported");
}
fast.addHook("onRequest", async (_request, reply) => {
if (syncer.getMode() === SyncMode.INITIAL_SYNCING) {
return reply.status(503).send({
message: "Initial sync incomplete",
error: "RPC unavailable",
statusCode: 503
});
}
});
const rpc = new RPC(api, fast, transferPool);
return new this(
modes,
provider,
api.eventEmitter,
syncer,
packer,
bidder,
rpc
);
}
private static getNodeModes({
proposer,
watcher
}: ClientConfig): NodeModes {
return {
isProposer: proposer ? proposer.enabled : false,
isWatcher: watcher ? watcher.enabled : false
};
}
public async start() {
this.eventEmitter.once(SyncCompleteEvent, this.onSyncComplete);
await this.syncer.start();
}
public async close() {
console.log("Node start closing");
this.syncer.stop();
this.packer?.stop();
this.bidder?.stop();
console.log("closing leveldb connection");
await closeDB();
}
onSyncComplete = async () => {
console.info("Initial Sync complete");
if (!this.modes.isProposer) {
console.info("Not a proposer, nothing to do");
return;
}
const burnAuction = this.bidder?.burnAuction as BurnAuctionWrapper;
this.bidder?.start();
const onSlotBoundary = async (blockNumber: number) => {
console.log("On boundary");
const isProposingThisSlot = await burnAuction.checkAmIProposerNow(
blockNumber
);
const willProposeNextSlot = await burnAuction.checkAmIProposerNext(
blockNumber
);
console.log(
"Is proposing this slot?",
isProposingThisSlot,
"Will propose next slot?",
willProposeNextSlot
);
if (isProposingThisSlot && !willProposeNextSlot) {
this.packer?.stop();
}
};
const onNewSlot = async (blockNumber: number) => {
const isProposer = await burnAuction.checkAmIProposerNow(
blockNumber
);
if (!isProposer) {
this.syncer.start();
return;
}
this.syncer.stop();
if (this.packer?.isStopped) {
this.packer?.start();
}
};
this.provider.on("block", async (blockNumber: number) => {
if (blockNumber < burnAuction.genesisBlock) return;
const slotLength = burnAuction.blocksPerSlot;
const blockModSlot = burnAuction.slotProgress(blockNumber);
console.log(
`block ${blockNumber}\tSlot progress\t${blockModSlot}/${slotLength}`
);
if (blockModSlot === slotLength - 3) {
onSlotBoundary(blockNumber);
} else if (blockModSlot === 0) {
onNewSlot(blockNumber);
}
});
};
}