-
Notifications
You must be signed in to change notification settings - Fork 272
/
besu-test-ledger.ts
388 lines (346 loc) · 12.1 KB
/
besu-test-ledger.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import { v4 as uuidv4 } from "uuid";
import Docker, { Container, ContainerInfo } from "dockerode";
import axios from "axios";
import Joi from "joi";
import tar from "tar-stream";
import { EventEmitter } from "events";
import Web3 from "web3";
import { Account } from "web3-core";
import {
LogLevelDesc,
Logger,
LoggerProvider,
} from "@hyperledger/cactus-common";
import { ITestLedger } from "../i-test-ledger";
import { Streams } from "../common/streams";
import { IKeyPair } from "../i-key-pair";
export interface IBesuTestLedgerConstructorOptions {
containerImageVersion?: string;
containerImageName?: string;
rpcApiHttpPort?: number;
envVars?: string[];
logLevel?: LogLevelDesc;
}
export const BESU_TEST_LEDGER_DEFAULT_OPTIONS = Object.freeze({
containerImageVersion: "2021-01-08-7a055c3",
containerImageName: "hyperledger/cactus-besu-all-in-one",
rpcApiHttpPort: 8545,
envVars: ["BESU_NETWORK=dev"],
});
export const BESU_TEST_LEDGER_OPTIONS_JOI_SCHEMA: Joi.Schema = Joi.object().keys(
{
containerImageVersion: Joi.string().min(5).required(),
containerImageName: Joi.string().min(1).required(),
rpcApiHttpPort: Joi.number()
.integer()
.positive()
.min(1024)
.max(65535)
.required(),
envVars: Joi.array().allow(null).required(),
},
);
export class BesuTestLedger implements ITestLedger {
public readonly containerImageVersion: string;
public readonly containerImageName: string;
public readonly rpcApiHttpPort: number;
public readonly envVars: string[];
private readonly log: Logger;
private container: Container | undefined;
private containerId: string | undefined;
constructor(public readonly options: IBesuTestLedgerConstructorOptions = {}) {
if (!options) {
throw new TypeError(`BesuTestLedger#ctor options was falsy.`);
}
this.containerImageVersion =
options.containerImageVersion ||
BESU_TEST_LEDGER_DEFAULT_OPTIONS.containerImageVersion;
this.containerImageName =
options.containerImageName ||
BESU_TEST_LEDGER_DEFAULT_OPTIONS.containerImageName;
this.rpcApiHttpPort =
options.rpcApiHttpPort || BESU_TEST_LEDGER_DEFAULT_OPTIONS.rpcApiHttpPort;
this.envVars = options.envVars || BESU_TEST_LEDGER_DEFAULT_OPTIONS.envVars;
this.validateConstructorOptions();
const label = "besu-test-ledger";
const level = options.logLevel || "INFO";
this.log = LoggerProvider.getOrCreate({ level, label });
}
public getContainer(): Container {
const fnTag = "BesuTestLedger#getContainer()";
if (!this.container) {
throw new Error(`${fnTag} container not yet started by this instance.`);
} else {
return this.container;
}
}
public getContainerImageName(): string {
return `${this.containerImageName}:${this.containerImageVersion}`;
}
public async getRpcApiHttpHost(): Promise<string> {
const ipAddress = "127.0.0.1";
const hostPort: number = await this.getRpcApiPublicPort();
return `http://${ipAddress}:${hostPort}`;
}
public async getFileContents(filePath: string): Promise<string> {
const response: any = await this.getContainer().getArchive({
path: filePath,
});
const extract: tar.Extract = tar.extract({ autoDestroy: true });
return new Promise((resolve, reject) => {
let fileContents = "";
extract.on("entry", async (header: any, stream, next) => {
stream.on("error", (err: Error) => {
reject(err);
});
const chunks: string[] = await Streams.aggregate<string>(stream);
fileContents += chunks.join("");
stream.resume();
next();
});
extract.on("finish", () => {
resolve(fileContents);
});
response.pipe(extract);
});
}
/**
* Output is based on the standard 'dev' Besu genesis.json contents.
*
* @see https://github.com/hyperledger/besu/blob/1.5.1/config/src/main/resources/dev.json
*/
public getGenesisAccountPubKey(): string {
return "627306090abaB3A6e1400e9345bC60c78a8BEf57";
}
/**
* Output is based on the standard 'dev' Besu genesis.json contents.
*
* @see https://github.com/hyperledger/besu/blob/1.5.1/config/src/main/resources/dev.json
*/
public getGenesisAccountPrivKey(): string {
return "c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3";
}
/**
* Creates a new ETH account from scratch on the ledger and then sends it a
* little seed money to get things started.
*
* @param [seedMoney=10e8] The amount of money to seed the new test account with.
*/
public async createEthTestAccount(seedMoney = 10e8): Promise<Account> {
const fnTag = `BesuTestLedger#getEthTestAccount()`;
const rpcApiHttpHost = await this.getRpcApiHttpHost();
const web3 = new Web3(rpcApiHttpHost);
const ethTestAccount = web3.eth.accounts.create(uuidv4());
const tx = await web3.eth.accounts.signTransaction(
{
from: this.getGenesisAccountPubKey(),
to: ethTestAccount.address,
value: seedMoney,
gas: 1000000,
},
this.getGenesisAccountPrivKey(),
);
if (!tx.rawTransaction) {
throw new Error(`${fnTag} Signing transaction failed, reason unknown.`);
}
const receipt = await web3.eth.sendSignedTransaction(tx.rawTransaction);
if (receipt instanceof Error) {
throw receipt;
} else {
return ethTestAccount;
}
}
public async getBesuKeyPair(): Promise<IKeyPair> {
const publicKey = await this.getFileContents("/opt/besu/keys/key.pub");
const privateKey = await this.getFileContents("/opt/besu/keys/key");
return { publicKey, privateKey };
}
public async getOrionKeyPair(): Promise<IKeyPair> {
const publicKey = await this.getFileContents("/config/orion/nodeKey.pub");
const privateKey = await this.getFileContents("/config/orion/nodeKey.key");
return { publicKey, privateKey };
}
public async start(): Promise<Container> {
const imageFqn = this.getContainerImageName();
if (this.container) {
await this.container.stop();
await this.container.remove();
}
const docker = new Docker();
this.log.debug(`Pulling container image ${imageFqn} ...`);
await this.pullContainerImage(imageFqn);
this.log.debug(`Pulled ${imageFqn} OK. Starting container...`);
return new Promise<Container>((resolve, reject) => {
const eventEmitter: EventEmitter = docker.run(
imageFqn,
[],
[],
{
ExposedPorts: {
[`${this.rpcApiHttpPort}/tcp`]: {}, // besu RPC - HTTP
"8546/tcp": {}, // besu RPC - WebSocket
"8888/tcp": {}, // orion Client Port - HTTP
"8080/tcp": {}, // orion Node Port - HTTP
"9001/tcp": {}, // supervisord - HTTP
"9545/tcp": {}, // besu metrics
},
// This is a workaround needed for macOS which has issues with routing
// to docker container's IP addresses directly...
// https://stackoverflow.com/a/39217691
PublishAllPorts: true,
Env: this.envVars,
},
{},
(err: any) => {
if (err) {
reject(err);
}
},
);
eventEmitter.once("start", async (container: Container) => {
this.log.debug(`Started container OK. Waiting for healthcheck...`);
this.container = container;
this.containerId = container.id;
try {
await this.waitForHealthCheck();
this.log.debug(`Healthcheck passing OK.`);
resolve(container);
} catch (ex) {
reject(ex);
}
});
});
}
public async waitForHealthCheck(timeoutMs = 120000): Promise<void> {
const fnTag = "BesuTestLedger#waitForHealthCheck()";
const httpUrl = await this.getRpcApiHttpHost();
const startedAt = Date.now();
let reachable = false;
do {
try {
const res = await axios.get(httpUrl);
reachable = res.status > 199 && res.status < 300;
} catch (ex) {
reachable = false;
if (Date.now() >= startedAt + timeoutMs) {
throw new Error(`${fnTag} timed out (${timeoutMs}ms) -> ${ex.stack}`);
}
}
await new Promise((resolve2) => setTimeout(resolve2, 100));
} while (!reachable);
}
public stop(): Promise<any> {
const fnTag = "BesuTestLedger#stop()";
return new Promise((resolve, reject) => {
if (this.container) {
this.container.stop({}, (err: any, result: any) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
} else {
return reject(new Error(`${fnTag} Container was not running.`));
}
});
}
public destroy(): Promise<any> {
const fnTag = "BesuTestLedger#destroy()";
if (this.container) {
return this.container.remove();
} else {
const ex = new Error(`${fnTag} Container not found, nothing to destroy.`);
return Promise.reject(ex);
}
}
protected async getContainerInfo(): Promise<ContainerInfo> {
const docker = new Docker();
const image = this.getContainerImageName();
const containerInfos = await docker.listContainers({});
let aContainerInfo;
if (this.containerId !== undefined) {
aContainerInfo = containerInfos.find((ci) => ci.Id === this.containerId);
}
if (aContainerInfo) {
return aContainerInfo;
} else {
throw new Error(`BesuTestLedger#getContainerInfo() no image "${image}"`);
}
}
public async getRpcApiPublicPort(): Promise<number> {
const fnTag = "BesuTestLedger#getRpcApiPublicPort()";
const aContainerInfo = await this.getContainerInfo();
const { rpcApiHttpPort: thePort } = this;
const { Ports: ports } = aContainerInfo;
if (ports.length < 1) {
throw new Error(`${fnTag} no ports exposed or mapped at all`);
}
const mapping = ports.find((x) => x.PrivatePort === thePort);
if (mapping) {
if (!mapping.PublicPort) {
throw new Error(`${fnTag} port ${thePort} mapped but not public`);
} else if (mapping.IP !== "0.0.0.0") {
throw new Error(`${fnTag} port ${thePort} mapped to localhost`);
} else {
return mapping.PublicPort;
}
} else {
throw new Error(`${fnTag} no mapping found for ${thePort}`);
}
}
public async getContainerIpAddress(): Promise<string> {
const fnTag = "BesuTestLedger#getContainerIpAddress()";
const aContainerInfo = await this.getContainerInfo();
if (aContainerInfo) {
const { NetworkSettings } = aContainerInfo;
const networkNames: string[] = Object.keys(NetworkSettings.Networks);
if (networkNames.length < 1) {
throw new Error(`${fnTag} container not connected to any networks`);
} else {
// return IP address of container on the first network that we found
// it connected to. Make this configurable?
return NetworkSettings.Networks[networkNames[0]].IPAddress;
}
} else {
throw new Error(`${fnTag} cannot find image: ${this.containerImageName}`);
}
}
private pullContainerImage(containerNameAndTag: string): Promise<any[]> {
return new Promise((resolve, reject) => {
const docker = new Docker();
docker.pull(containerNameAndTag, (pullError: any, stream: any) => {
if (pullError) {
reject(pullError);
} else {
docker.modem.followProgress(
stream,
(progressError: any, output: any[]) => {
if (progressError) {
reject(progressError);
} else {
resolve(output);
}
},
);
}
});
});
}
private validateConstructorOptions(): void {
const validationResult = Joi.validate<IBesuTestLedgerConstructorOptions>(
{
containerImageVersion: this.containerImageVersion,
containerImageName: this.containerImageName,
rpcApiHttpPort: this.rpcApiHttpPort,
envVars: this.envVars,
},
BESU_TEST_LEDGER_OPTIONS_JOI_SCHEMA,
);
if (validationResult.error) {
throw new Error(
`BesuTestLedger#ctor ${validationResult.error.annotate()}`,
);
}
}
}