-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
http.ts
125 lines (113 loc) Β· 4.54 KB
/
http.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
import {byteArrayEquals, toHexString} from "@chainsafe/ssz";
import {allForks, bellatrix, Slot, Root, BLSPubkey, ssz, deneb, Wei} from "@lodestar/types";
import {ChainForkConfig} from "@lodestar/config";
import {getClient, Api as BuilderApi} from "@lodestar/api/builder";
import {SLOTS_PER_EPOCH} from "@lodestar/params";
import {ApiError} from "@lodestar/api";
import {Metrics} from "../../metrics/metrics.js";
import {IExecutionBuilder} from "./interface.js";
export type ExecutionBuilderHttpOpts = {
enabled: boolean;
urls: string[];
timeout?: number;
faultInspectionWindow?: number;
allowedFaults?: number;
// Only required for merge-mock runs, no need to expose it to cli
issueLocalFcUWithFeeRecipient?: string;
// Add User-Agent header to all requests
userAgent?: string;
};
export const defaultExecutionBuilderHttpOpts: ExecutionBuilderHttpOpts = {
enabled: false,
urls: ["http://localhost:8661"],
timeout: 12000,
};
export class ExecutionBuilderHttp implements IExecutionBuilder {
readonly api: BuilderApi;
readonly config: ChainForkConfig;
readonly issueLocalFcUWithFeeRecipient?: string;
// Builder needs to be explicity enabled using updateStatus
status = false;
faultInspectionWindow: number;
allowedFaults: number;
constructor(opts: ExecutionBuilderHttpOpts, config: ChainForkConfig, metrics: Metrics | null = null) {
const baseUrl = opts.urls[0];
if (!baseUrl) throw Error("No Url provided for executionBuilder");
this.api = getClient(
{
baseUrl,
timeoutMs: opts.timeout,
extraHeaders: opts.userAgent ? {"User-Agent": opts.userAgent} : undefined,
},
{config, metrics: metrics?.builderHttpClient}
);
this.config = config;
this.issueLocalFcUWithFeeRecipient = opts.issueLocalFcUWithFeeRecipient;
/**
* Beacon clients select randomized values from the following ranges when initializing
* the circuit breaker (so at boot time and once for each unique boot).
*
* ALLOWED_FAULTS: between 1 and SLOTS_PER_EPOCH // 2
* FAULT_INSPECTION_WINDOW: between SLOTS_PER_EPOCH and 2 * SLOTS_PER_EPOCH
*
*/
this.faultInspectionWindow = Math.max(
opts.faultInspectionWindow ?? SLOTS_PER_EPOCH + Math.floor(Math.random() * SLOTS_PER_EPOCH),
SLOTS_PER_EPOCH
);
// allowedFaults should be < faultInspectionWindow, limiting them to faultInspectionWindow/2
this.allowedFaults = Math.min(
opts.allowedFaults ?? Math.floor(this.faultInspectionWindow / 2),
Math.floor(this.faultInspectionWindow / 2)
);
}
updateStatus(shouldEnable: boolean): void {
this.status = shouldEnable;
}
async checkStatus(): Promise<void> {
try {
await this.api.status();
} catch (e) {
// Disable if the status was enabled
this.status = false;
throw e;
}
}
async registerValidator(registrations: bellatrix.SignedValidatorRegistrationV1[]): Promise<void> {
ApiError.assert(await this.api.registerValidator(registrations));
}
async getHeader(
slot: Slot,
parentHash: Root,
proposerPubKey: BLSPubkey
): Promise<{
header: allForks.ExecutionPayloadHeader;
blockValue: Wei;
blobKzgCommitments?: deneb.BlobKzgCommitments;
}> {
const res = await this.api.getHeader(slot, parentHash, proposerPubKey);
ApiError.assert(res, "execution.builder.getheader");
const {header, value: blockValue} = res.response.data.message;
const {blobKzgCommitments} = res.response.data.message as {blobKzgCommitments?: deneb.BlobKzgCommitments};
return {header, blockValue, blobKzgCommitments};
}
async submitBlindedBlock(signedBlock: allForks.SignedBlindedBeaconBlock): Promise<allForks.SignedBeaconBlock> {
const res = await this.api.submitBlindedBlock(signedBlock);
ApiError.assert(res, "execution.builder.submitBlindedBlock");
const executionPayload = res.response.data;
const expectedTransactionsRoot = signedBlock.message.body.executionPayloadHeader.transactionsRoot;
const actualTransactionsRoot = ssz.bellatrix.Transactions.hashTreeRoot(res.response.data.transactions);
if (!byteArrayEquals(expectedTransactionsRoot, actualTransactionsRoot)) {
throw Error(
`Invalid transactionsRoot of the builder payload, expected=${toHexString(
expectedTransactionsRoot
)}, actual=${toHexString(actualTransactionsRoot)}`
);
}
const fullySignedBlock: bellatrix.SignedBeaconBlock = {
...signedBlock,
message: {...signedBlock.message, body: {...signedBlock.message.body, executionPayload}},
};
return fullySignedBlock;
}
}