@lodestar/builder 1.50.0-dev.814fe8c461 → 1.50.0-dev.82f05c43ff

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,42 @@
1
+ import { LodestarError } from "@lodestar/utils";
2
+ import { type BuildRequest, type BuiltPayload, type PayloadSource } from "./payloadSource.js";
3
+ export type PayloadBuildJob = {
4
+ /** Jobs with the same ID share one build and its result. */
5
+ id: string;
6
+ request: BuildRequest;
7
+ /** Unix timestamp in milliseconds at which the prepared payload should be retrieved. */
8
+ getPayloadAt: number;
9
+ };
10
+ export type PayloadOrchestratorOptions = {
11
+ /** Maximum time in milliseconds to wait for payload retrieval. */
12
+ getPayloadTimeout: number;
13
+ };
14
+ export declare enum PayloadOrchestratorErrorCode {
15
+ PREPARE_DEADLINE_REACHED = "PAYLOAD_ORCHESTRATOR_ERROR_PREPARE_DEADLINE_REACHED",
16
+ PREPARE_TIMEOUT = "PAYLOAD_ORCHESTRATOR_ERROR_PREPARE_TIMEOUT",
17
+ GET_PAYLOAD_TIMEOUT = "PAYLOAD_ORCHESTRATOR_ERROR_GET_PAYLOAD_TIMEOUT"
18
+ }
19
+ export type PayloadOrchestratorErrorType = {
20
+ code: PayloadOrchestratorErrorCode.PREPARE_DEADLINE_REACHED;
21
+ jobId: string;
22
+ getPayloadAt: number;
23
+ } | {
24
+ code: PayloadOrchestratorErrorCode.PREPARE_TIMEOUT | PayloadOrchestratorErrorCode.GET_PAYLOAD_TIMEOUT;
25
+ jobId: string;
26
+ };
27
+ export declare class PayloadOrchestratorError extends LodestarError<PayloadOrchestratorErrorType> {
28
+ }
29
+ /**
30
+ * Prepares a payload and retrieves it at the requested time. Preparation retries missing payload IDs until the
31
+ * retrieval time. Transport-level retries remain owned by the source.
32
+ */
33
+ export declare class PayloadOrchestrator {
34
+ private readonly source;
35
+ private readonly options;
36
+ private readonly signal;
37
+ private readonly jobs;
38
+ constructor(source: PayloadSource, options: PayloadOrchestratorOptions, signal: AbortSignal);
39
+ run(job: PayloadBuildJob): Promise<BuiltPayload>;
40
+ private runJob;
41
+ }
42
+ //# sourceMappingURL=payloadOrchestrator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"payloadOrchestrator.d.ts","sourceRoot":"","sources":["../../src/services/payloadOrchestrator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAA0C,MAAM,iBAAiB,CAAC;AACvF,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,aAAa,EAGnB,MAAM,oBAAoB,CAAC;AAI5B,MAAM,MAAM,eAAe,GAAG;IAC5B,4DAA4D;IAC5D,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,YAAY,CAAC;IACtB,wFAAwF;IACxF,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,0BAA0B,GAAG;IACvC,kEAAkE;IAClE,iBAAiB,EAAE,MAAM,CAAC;CAC3B,CAAC;AAEF,oBAAY,4BAA4B;IACtC,wBAAwB,wDAAwD;IAChF,eAAe,+CAA+C;IAC9D,mBAAmB,mDAAmD;CACvE;AAED,MAAM,MAAM,4BAA4B,GACpC;IACE,IAAI,EAAE,4BAA4B,CAAC,wBAAwB,CAAC;IAC5D,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,MAAM,CAAC;CACtB,GACD;IACE,IAAI,EAAE,4BAA4B,CAAC,eAAe,GAAG,4BAA4B,CAAC,mBAAmB,CAAC;IACtG,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEN,qBAAa,wBAAyB,SAAQ,aAAa,CAAC,4BAA4B,CAAC;CAAG;AAE5F;;;GAGG;AACH,qBAAa,mBAAmB;IAI5B,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM;IALzB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA4C;IAEjE,YACmB,MAAM,EAAE,aAAa,EACrB,OAAO,EAAE,0BAA0B,EACnC,MAAM,EAAE,WAAW,EAClC;IAEJ,GAAG,CAAC,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,YAAY,CAAC,CAQ/C;YAEa,MAAM;CAwCrB"}
@@ -0,0 +1,60 @@
1
+ import { LodestarError, TimeoutError, retry, sleep, withTimeout } from "@lodestar/utils";
2
+ import { PayloadSourceError, PayloadSourceErrorCode, } from "./payloadSource.js";
3
+ const PREPARE_RETRY_DELAY = 100;
4
+ export var PayloadOrchestratorErrorCode;
5
+ (function (PayloadOrchestratorErrorCode) {
6
+ PayloadOrchestratorErrorCode["PREPARE_DEADLINE_REACHED"] = "PAYLOAD_ORCHESTRATOR_ERROR_PREPARE_DEADLINE_REACHED";
7
+ PayloadOrchestratorErrorCode["PREPARE_TIMEOUT"] = "PAYLOAD_ORCHESTRATOR_ERROR_PREPARE_TIMEOUT";
8
+ PayloadOrchestratorErrorCode["GET_PAYLOAD_TIMEOUT"] = "PAYLOAD_ORCHESTRATOR_ERROR_GET_PAYLOAD_TIMEOUT";
9
+ })(PayloadOrchestratorErrorCode || (PayloadOrchestratorErrorCode = {}));
10
+ export class PayloadOrchestratorError extends LodestarError {
11
+ }
12
+ /**
13
+ * Prepares a payload and retrieves it at the requested time. Preparation retries missing payload IDs until the
14
+ * retrieval time. Transport-level retries remain owned by the source.
15
+ */
16
+ export class PayloadOrchestrator {
17
+ source;
18
+ options;
19
+ signal;
20
+ jobs = new Map();
21
+ constructor(source, options, signal) {
22
+ this.source = source;
23
+ this.options = options;
24
+ this.signal = signal;
25
+ }
26
+ run(job) {
27
+ const { id } = job;
28
+ let promise = this.jobs.get(id);
29
+ if (promise === undefined) {
30
+ promise = this.runJob(job).finally(() => this.jobs.delete(id));
31
+ this.jobs.set(id, promise);
32
+ }
33
+ return promise;
34
+ }
35
+ async runJob({ id, request, getPayloadAt }) {
36
+ const prepareTimeout = getPayloadAt - Date.now();
37
+ if (!(prepareTimeout > 0)) {
38
+ throw new PayloadOrchestratorError({ code: PayloadOrchestratorErrorCode.PREPARE_DEADLINE_REACHED, jobId: id, getPayloadAt }, `Payload preparation deadline reached jobId=${id} getPayloadAt=${getPayloadAt}`);
39
+ }
40
+ const handle = await withTimeout((signal = this.signal) => retry(() => this.source.prepare(request, signal), {
41
+ retries: Infinity,
42
+ retryDelay: PREPARE_RETRY_DELAY,
43
+ shouldRetry: (error) => error instanceof PayloadSourceError && error.type.code === PayloadSourceErrorCode.NO_PAYLOAD_ID,
44
+ signal,
45
+ }), prepareTimeout, this.signal).catch((error) => {
46
+ if (error instanceof TimeoutError) {
47
+ throw new PayloadOrchestratorError({ code: PayloadOrchestratorErrorCode.PREPARE_TIMEOUT, jobId: id });
48
+ }
49
+ throw error;
50
+ });
51
+ await sleep(getPayloadAt - Date.now(), this.signal);
52
+ return withTimeout((signal = this.signal) => this.source.getPayload(handle, signal), this.options.getPayloadTimeout, this.signal).catch((error) => {
53
+ if (error instanceof TimeoutError) {
54
+ throw new PayloadOrchestratorError({ code: PayloadOrchestratorErrorCode.GET_PAYLOAD_TIMEOUT, jobId: id });
55
+ }
56
+ throw error;
57
+ });
58
+ }
59
+ }
60
+ //# sourceMappingURL=payloadOrchestrator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"payloadOrchestrator.js","sourceRoot":"","sources":["../../src/services/payloadOrchestrator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,YAAY,EAAE,KAAK,EAAE,KAAK,EAAE,WAAW,EAAC,MAAM,iBAAiB,CAAC;AACvF,OAAO,EAIL,kBAAkB,EAClB,sBAAsB,GACvB,MAAM,oBAAoB,CAAC;AAE5B,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAehC,MAAM,CAAN,IAAY,4BAIX;AAJD,WAAY,4BAA4B;IACtC,gHAAgF,CAAA;IAChF,8FAA8D,CAAA;IAC9D,sGAAsE,CAAA;AACxE,CAAC,EAJW,4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
2
  "name": "@lodestar/builder",
3
- "version": "1.50.0-dev.814fe8c461",
3
+ "version": "1.50.0-dev.82f05c43ff",
4
4
  "description": "A Typescript implementation of the builder client",
5
5
  "author": "ChainSafe Systems",
6
6
  "license": "Apache-2.0",
@@ -47,17 +47,17 @@
47
47
  ],
48
48
  "dependencies": {
49
49
  "@chainsafe/lodestar-z": "catalog:",
50
- "@chainsafe/ssz": "^1.8.0",
51
- "@lodestar/api": "^1.50.0-dev.814fe8c461",
52
- "@lodestar/config": "^1.50.0-dev.814fe8c461",
53
- "@lodestar/params": "^1.50.0-dev.814fe8c461",
54
- "@lodestar/state-transition": "^1.50.0-dev.814fe8c461",
55
- "@lodestar/types": "^1.50.0-dev.814fe8c461",
56
- "@lodestar/utils": "^1.50.0-dev.814fe8c461"
50
+ "@chainsafe/ssz": "^1.8.1",
51
+ "@lodestar/api": "^1.50.0-dev.82f05c43ff",
52
+ "@lodestar/config": "^1.50.0-dev.82f05c43ff",
53
+ "@lodestar/params": "^1.50.0-dev.82f05c43ff",
54
+ "@lodestar/state-transition": "^1.50.0-dev.82f05c43ff",
55
+ "@lodestar/types": "^1.50.0-dev.82f05c43ff",
56
+ "@lodestar/utils": "^1.50.0-dev.82f05c43ff"
57
57
  },
58
58
  "devDependencies": {
59
- "@lodestar/logger": "^1.50.0-dev.814fe8c461",
60
- "@lodestar/test-utils": "^1.50.0-dev.814fe8c461"
59
+ "@lodestar/logger": "^1.50.0-dev.82f05c43ff",
60
+ "@lodestar/test-utils": "^1.50.0-dev.82f05c43ff"
61
61
  },
62
- "gitHead": "f8cfef9ffe414aba7e36b26e5fcfdf7c1135e7e8"
62
+ "gitHead": "aa535ba9e1da82967b49356035d948d5b35fedae"
63
63
  }
package/src/builder.ts CHANGED
@@ -147,6 +147,9 @@ export class Builder {
147
147
  onEvent: (event) => {
148
148
  void this.onEvent(event);
149
149
  },
150
+ onOpen: () => {
151
+ void this.fetchProposerPreferences(api);
152
+ },
150
153
  onError: (error) => {
151
154
  if (!signal.aborted) this.logger.error("Failed to receive builder event", {topics: topics.join(",")}, error);
152
155
  },
@@ -169,6 +172,31 @@ export class Builder {
169
172
  });
170
173
  }
171
174
 
175
+ /**
176
+ * Proposer preferences are only broadcast once per proposal slot, fetch the ones the beacon node
177
+ * already knows to cover those missed while not connected, e.g. after a restart
178
+ */
179
+ private async fetchProposerPreferences(api: ApiClient): Promise<void> {
180
+ const signal = this.controller.signal;
181
+
182
+ try {
183
+ const preferences = (await api.beacon.getProposerPreferences({}, {signal})).value();
184
+ for (const signedProposerPreferences of preferences) {
185
+ // Does not replace preferences already received from the event stream
186
+ this.proposerPreferencesTracker.onProposerPreferences(signedProposerPreferences);
187
+ }
188
+ this.logger.verbose("Fetched proposer preferences", {count: preferences.length});
189
+ } catch (error) {
190
+ if (!signal.aborted && !isErrorAborted(error)) {
191
+ this.logger.warn(
192
+ "Failed to fetch proposer preferences",
193
+ {},
194
+ error instanceof Error ? error : Error(String(error))
195
+ );
196
+ }
197
+ }
198
+ }
199
+
172
200
  private async onEvent(event: routes.events.BeaconEvent): Promise<void> {
173
201
  const signal = this.controller.signal;
174
202
  if (signal.aborted) return;
@@ -128,6 +128,11 @@ export class BidLedger {
128
128
  return toRecord(record);
129
129
  }
130
130
 
131
+ hasWon(identity: BidIdentity, blockRoot: RootHex): boolean {
132
+ const record = this.getBid(identity.slot, identity.parentBlockHash, identity.parentBlockRoot);
133
+ return record !== null && record.blockHash === identity.blockHash && record.wonBlockRoots.has(blockRoot);
134
+ }
135
+
131
136
  /** Release a winning bid liability only after its payment is known to be reflected in Builder balance. */
132
137
  recordPaymentSettled(identity: BidIdentity): BidLedgerRecord | null {
133
138
  const record = this.getBid(identity.slot, identity.parentBlockHash, identity.parentBlockRoot);
@@ -0,0 +1,138 @@
1
+ import type {ChainForkConfig} from "@lodestar/config";
2
+ import type {ForkPostGloas} from "@lodestar/params";
3
+ import type {BuilderIndex, RootHex, SignedBeaconBlock, Slot} from "@lodestar/types";
4
+ import {sszTypesFor} from "@lodestar/types";
5
+ import {LodestarError, toRootHex} from "@lodestar/utils";
6
+ import type {BidIdentity, BidLedger, BidLedgerRecord} from "./bidLedger.js";
7
+
8
+ export type ObservedPostGloasBlock = {
9
+ blockRoot: RootHex;
10
+ slot: Slot;
11
+ version: ForkPostGloas;
12
+ block: SignedBeaconBlock<ForkPostGloas>;
13
+ };
14
+
15
+ export type BidSelectorModules = {
16
+ config: ChainForkConfig;
17
+ ledger: BidLedger;
18
+ builderIndex: BuilderIndex;
19
+ };
20
+
21
+ export enum BidSelectionIgnoreReason {
22
+ FOREIGN_BUILDER = "foreign_builder",
23
+ }
24
+
25
+ export type BidSelectionResult =
26
+ | {
27
+ status: "selected";
28
+ blockRoot: RootHex;
29
+ bid: BidLedgerRecord;
30
+ }
31
+ | {
32
+ status: "ignored";
33
+ reason: BidSelectionIgnoreReason;
34
+ };
35
+
36
+ export enum BidSelectorErrorCode {
37
+ BLOCK_ROOT_MISMATCH = "BID_SELECTOR_ERROR_BLOCK_ROOT_MISMATCH",
38
+ BLOCK_SLOT_MISMATCH = "BID_SELECTOR_ERROR_BLOCK_SLOT_MISMATCH",
39
+ BLOCK_FORK_MISMATCH = "BID_SELECTOR_ERROR_BLOCK_FORK_MISMATCH",
40
+ BID_SLOT_MISMATCH = "BID_SELECTOR_ERROR_BID_SLOT_MISMATCH",
41
+ UNKNOWN_BID = "BID_SELECTOR_ERROR_UNKNOWN_BID",
42
+ }
43
+
44
+ export type BidSelectorErrorType =
45
+ | {
46
+ code: BidSelectorErrorCode.BLOCK_ROOT_MISMATCH;
47
+ blockRoot: RootHex;
48
+ computedBlockRoot: RootHex;
49
+ }
50
+ | {
51
+ code: BidSelectorErrorCode.BLOCK_SLOT_MISMATCH;
52
+ slot: Slot;
53
+ blockSlot: Slot;
54
+ }
55
+ | {
56
+ code: BidSelectorErrorCode.BLOCK_FORK_MISMATCH;
57
+ version: ForkPostGloas;
58
+ blockFork: string;
59
+ }
60
+ | {
61
+ code: BidSelectorErrorCode.BID_SLOT_MISMATCH;
62
+ slot: Slot;
63
+ bidSlot: Slot;
64
+ }
65
+ | {
66
+ code: BidSelectorErrorCode.UNKNOWN_BID;
67
+ slot: Slot;
68
+ parentBlockHash: RootHex;
69
+ parentBlockRoot: RootHex;
70
+ blockHash: RootHex;
71
+ signedBidRoot: RootHex;
72
+ };
73
+
74
+ export class BidSelectorError extends LodestarError<BidSelectorErrorType> {}
75
+
76
+ /** Matches an imported post-Gloas block to an exact local bid and records the win. */
77
+ export class BidSelector {
78
+ constructor(private readonly modules: BidSelectorModules) {}
79
+
80
+ match(observed: ObservedPostGloasBlock): BidSelectionResult {
81
+ const {block, blockRoot, slot, version} = observed;
82
+ const {builderIndex, config, ledger} = this.modules;
83
+ const blockSlot = block.message.slot;
84
+
85
+ if (blockSlot !== slot) {
86
+ throw new BidSelectorError(
87
+ {code: BidSelectorErrorCode.BLOCK_SLOT_MISMATCH, slot, blockSlot},
88
+ `Observed block slot does not match fetched block slot=${slot} blockSlot=${blockSlot}`
89
+ );
90
+ }
91
+
92
+ const blockFork = config.getForkName(slot);
93
+ if (blockFork !== version) {
94
+ throw new BidSelectorError(
95
+ {code: BidSelectorErrorCode.BLOCK_FORK_MISMATCH, version, blockFork},
96
+ `Observed block fork does not match configured fork version=${version} blockFork=${blockFork}`
97
+ );
98
+ }
99
+
100
+ const computedBlockRoot = toRootHex(config.getForkTypes(slot).BeaconBlock.hashTreeRoot(block.message));
101
+ if (computedBlockRoot !== blockRoot) {
102
+ throw new BidSelectorError(
103
+ {code: BidSelectorErrorCode.BLOCK_ROOT_MISMATCH, blockRoot, computedBlockRoot},
104
+ `Observed block root does not match fetched block blockRoot=${blockRoot} computedBlockRoot=${computedBlockRoot}`
105
+ );
106
+ }
107
+
108
+ const bid = block.message.body.signedExecutionPayloadBid.message;
109
+ if (bid.slot !== slot) {
110
+ throw new BidSelectorError(
111
+ {code: BidSelectorErrorCode.BID_SLOT_MISMATCH, slot, bidSlot: bid.slot},
112
+ `Selected bid slot does not match block slot=${slot} bidSlot=${bid.slot}`
113
+ );
114
+ }
115
+ if (bid.builderIndex !== builderIndex) {
116
+ return {status: "ignored", reason: BidSelectionIgnoreReason.FOREIGN_BUILDER};
117
+ }
118
+
119
+ const identity: BidIdentity = {
120
+ slot,
121
+ parentBlockHash: toRootHex(bid.parentBlockHash),
122
+ parentBlockRoot: toRootHex(bid.parentBlockRoot),
123
+ blockHash: toRootHex(bid.blockHash),
124
+ };
125
+ const signedBidRoot = toRootHex(
126
+ sszTypesFor(version, "SignedExecutionPayloadBid").hashTreeRoot(block.message.body.signedExecutionPayloadBid)
127
+ );
128
+ const localBid = ledger.recordWin({...identity, signedBidRoot}, blockRoot);
129
+ if (localBid === null) {
130
+ throw new BidSelectorError(
131
+ {code: BidSelectorErrorCode.UNKNOWN_BID, ...identity, signedBidRoot},
132
+ `Selected bid is not in the local ledger slot=${slot} blockHash=${identity.blockHash} signedBidRoot=${signedBidRoot}`
133
+ );
134
+ }
135
+
136
+ return {status: "selected", blockRoot, bid: localBid};
137
+ }
138
+ }
@@ -0,0 +1,154 @@
1
+ import {type ApiClient, routes} from "@lodestar/api";
2
+ import {type BuilderIndex, type RootHex, type gloas, ssz} from "@lodestar/types";
3
+ import {LodestarError, defer, toRootHex} from "@lodestar/utils";
4
+ import type {BidIdentity, BidLedger} from "./bidLedger.js";
5
+ import type {BuilderSigner} from "./builderSigner.js";
6
+ import type {ExecutionPayloadEnvelopeContents} from "./executionPayloadEnvelope.js";
7
+
8
+ export type EnvelopeSelectionIdentity = BidIdentity & {blockRoot: RootHex};
9
+
10
+ export type EnvelopePublisherModules = {
11
+ api: ApiClient;
12
+ signer: BuilderSigner;
13
+ ledger: BidLedger;
14
+ builderIndex: BuilderIndex;
15
+ };
16
+
17
+ export enum EnvelopePublisherErrorCode {
18
+ BUILDER_INDEX_MISMATCH = "ENVELOPE_PUBLISHER_ERROR_BUILDER_INDEX_MISMATCH",
19
+ SELECTION_NOT_RECORDED = "ENVELOPE_PUBLISHER_ERROR_SELECTION_NOT_RECORDED",
20
+ }
21
+
22
+ export type EnvelopePublisherErrorType =
23
+ | {
24
+ code: EnvelopePublisherErrorCode.BUILDER_INDEX_MISMATCH;
25
+ builderIndex: BuilderIndex;
26
+ envelopeBuilderIndex: BuilderIndex;
27
+ }
28
+ | ({code: EnvelopePublisherErrorCode.SELECTION_NOT_RECORDED} & EnvelopeSelectionIdentity);
29
+
30
+ export class EnvelopePublisherError extends LodestarError<EnvelopePublisherErrorType> {}
31
+
32
+ export type EnvelopePublicationResult =
33
+ | {status: "published"; signedEnvelope: gloas.SignedExecutionPayloadEnvelope}
34
+ | {status: "duplicate"};
35
+
36
+ type ActivePublication = {
37
+ controller: AbortController;
38
+ promise: Promise<EnvelopePublicationResult>;
39
+ waiters: number;
40
+ };
41
+
42
+ /** Signs and submits an envelope for a recorded local selection. */
43
+ export class EnvelopePublisher {
44
+ private readonly activePublications = new Map<RootHex, ActivePublication>();
45
+
46
+ constructor(private readonly modules: EnvelopePublisherModules) {}
47
+
48
+ async publish(contents: ExecutionPayloadEnvelopeContents, signal: AbortSignal): Promise<EnvelopePublicationResult> {
49
+ signal.throwIfAborted();
50
+
51
+ const {api, builderIndex, ledger, signer} = this.modules;
52
+ const {envelope} = contents;
53
+ if (envelope.builderIndex !== builderIndex) {
54
+ throw new EnvelopePublisherError(
55
+ {
56
+ code: EnvelopePublisherErrorCode.BUILDER_INDEX_MISMATCH,
57
+ builderIndex,
58
+ envelopeBuilderIndex: envelope.builderIndex,
59
+ },
60
+ `Envelope builder index does not match local builder index builderIndex=${builderIndex} envelopeBuilderIndex=${envelope.builderIndex}`
61
+ );
62
+ }
63
+
64
+ const identity: EnvelopeSelectionIdentity = {
65
+ slot: envelope.payload.slotNumber,
66
+ parentBlockHash: toRootHex(envelope.payload.parentHash),
67
+ parentBlockRoot: toRootHex(envelope.parentBeaconBlockRoot),
68
+ blockHash: toRootHex(envelope.payload.blockHash),
69
+ blockRoot: toRootHex(envelope.beaconBlockRoot),
70
+ };
71
+ if (!ledger.hasWon(identity, identity.blockRoot)) {
72
+ throw new EnvelopePublisherError(
73
+ {code: EnvelopePublisherErrorCode.SELECTION_NOT_RECORDED, ...identity},
74
+ `Envelope selection is not recorded slot=${identity.slot} blockRoot=${identity.blockRoot} blockHash=${identity.blockHash}`
75
+ );
76
+ }
77
+
78
+ const envelopeRoot = toRootHex(ssz.gloas.ExecutionPayloadEnvelope.hashTreeRoot(envelope));
79
+ ledger.recordReveal(identity.slot, identity.blockRoot, identity.blockHash, envelopeRoot);
80
+ if (ledger.hasPublishedReveal(identity.blockRoot)) {
81
+ return {status: "duplicate"};
82
+ }
83
+
84
+ let publication = this.activePublications.get(identity.blockRoot);
85
+ if (publication === undefined) {
86
+ const controller = new AbortController();
87
+ publication = {
88
+ controller,
89
+ waiters: 0,
90
+ promise: this.publishEnvelope(contents, identity, envelopeRoot, api, ledger, signer, controller.signal).finally(
91
+ () => {
92
+ if (this.activePublications.get(identity.blockRoot)?.controller === controller) {
93
+ this.activePublications.delete(identity.blockRoot);
94
+ }
95
+ }
96
+ ),
97
+ };
98
+ this.activePublications.set(identity.blockRoot, publication);
99
+ }
100
+ return this.waitForPublication(identity.blockRoot, publication, signal);
101
+ }
102
+
103
+ private async waitForPublication(
104
+ blockRoot: RootHex,
105
+ publication: ActivePublication,
106
+ signal: AbortSignal
107
+ ): Promise<EnvelopePublicationResult> {
108
+ const aborted = defer<never>();
109
+ const onAbort = (): void => aborted.reject(signal.reason);
110
+ publication.waiters++;
111
+ signal.addEventListener("abort", onAbort, {once: true});
112
+ if (signal.aborted) onAbort();
113
+
114
+ try {
115
+ return await Promise.race([publication.promise, aborted.promise]);
116
+ } finally {
117
+ signal.removeEventListener("abort", onAbort);
118
+ publication.waiters--;
119
+ if (publication.waiters === 0 && signal.aborted) {
120
+ if (this.activePublications.get(blockRoot) === publication) {
121
+ this.activePublications.delete(blockRoot);
122
+ }
123
+ publication.controller.abort(signal.reason);
124
+ }
125
+ }
126
+ }
127
+
128
+ private async publishEnvelope(
129
+ contents: ExecutionPayloadEnvelopeContents,
130
+ identity: EnvelopeSelectionIdentity,
131
+ envelopeRoot: RootHex,
132
+ api: ApiClient,
133
+ ledger: BidLedger,
134
+ signer: BuilderSigner,
135
+ signal: AbortSignal
136
+ ): Promise<EnvelopePublicationResult> {
137
+ const signedEnvelope = signer.signExecutionPayloadEnvelope(contents.envelope);
138
+
139
+ const response = await api.beacon.publishExecutionPayloadEnvelope(
140
+ {
141
+ signedEnvelopeOrContents: {
142
+ signedExecutionPayloadEnvelope: signedEnvelope,
143
+ kzgProofs: contents.kzgProofs,
144
+ blobs: contents.blobs,
145
+ },
146
+ broadcastValidation: routes.beacon.BroadcastValidation.gossip,
147
+ },
148
+ {signal}
149
+ );
150
+ response.assertOk();
151
+ ledger.recordRevealPublished(identity.slot, identity.blockRoot, identity.blockHash, envelopeRoot);
152
+ return {status: "published", signedEnvelope};
153
+ }
154
+ }
@@ -0,0 +1,156 @@
1
+ import type {BuilderIndex, RootHex, Slot, deneb, gloas} from "@lodestar/types";
2
+ import {ssz} from "@lodestar/types";
3
+ import {LodestarError, byteArrayEquals, fromHex, toRootHex} from "@lodestar/utils";
4
+ import type {StoredPayload} from "./payloadStore.js";
5
+
6
+ export type ExecutionPayloadEnvelopeInput = {
7
+ blockRoot: RootHex;
8
+ builderIndex: BuilderIndex;
9
+ selectedBid: gloas.ExecutionPayloadBid;
10
+ storedPayload: StoredPayload;
11
+ };
12
+
13
+ export type ExecutionPayloadEnvelopeContents = {
14
+ envelope: gloas.ExecutionPayloadEnvelope;
15
+ kzgProofs: deneb.KZGProofs;
16
+ blobs: deneb.Blobs;
17
+ };
18
+
19
+ export enum ExecutionPayloadEnvelopeErrorCode {
20
+ SLOT_MISMATCH = "EXECUTION_PAYLOAD_ENVELOPE_ERROR_SLOT_MISMATCH",
21
+ PARENT_BLOCK_ROOT_MISMATCH = "EXECUTION_PAYLOAD_ENVELOPE_ERROR_PARENT_BLOCK_ROOT_MISMATCH",
22
+ PARENT_BLOCK_HASH_MISMATCH = "EXECUTION_PAYLOAD_ENVELOPE_ERROR_PARENT_BLOCK_HASH_MISMATCH",
23
+ BLOCK_HASH_MISMATCH = "EXECUTION_PAYLOAD_ENVELOPE_ERROR_BLOCK_HASH_MISMATCH",
24
+ BUILDER_INDEX_MISMATCH = "EXECUTION_PAYLOAD_ENVELOPE_ERROR_BUILDER_INDEX_MISMATCH",
25
+ BLOB_KZG_COMMITMENTS_MISMATCH = "EXECUTION_PAYLOAD_ENVELOPE_ERROR_BLOB_KZG_COMMITMENTS_MISMATCH",
26
+ EXECUTION_REQUESTS_ROOT_MISMATCH = "EXECUTION_PAYLOAD_ENVELOPE_ERROR_EXECUTION_REQUESTS_ROOT_MISMATCH",
27
+ }
28
+
29
+ export type ExecutionPayloadEnvelopeErrorType =
30
+ | {
31
+ code: ExecutionPayloadEnvelopeErrorCode.BUILDER_INDEX_MISMATCH;
32
+ builderIndex: BuilderIndex;
33
+ bidBuilderIndex: BuilderIndex;
34
+ }
35
+ | {code: ExecutionPayloadEnvelopeErrorCode.BLOB_KZG_COMMITMENTS_MISMATCH}
36
+ | {
37
+ code: ExecutionPayloadEnvelopeErrorCode.EXECUTION_REQUESTS_ROOT_MISMATCH;
38
+ bidExecutionRequestsRoot: RootHex;
39
+ payloadExecutionRequestsRoot: RootHex;
40
+ }
41
+ | {
42
+ code: ExecutionPayloadEnvelopeErrorCode.PARENT_BLOCK_ROOT_MISMATCH;
43
+ bidParentBlockRoot: RootHex;
44
+ storedParentBlockRoot: RootHex;
45
+ }
46
+ | {
47
+ code: ExecutionPayloadEnvelopeErrorCode.SLOT_MISMATCH;
48
+ bidSlot: Slot;
49
+ payloadSlot: Slot;
50
+ }
51
+ | {
52
+ code: ExecutionPayloadEnvelopeErrorCode.PARENT_BLOCK_HASH_MISMATCH;
53
+ bidParentBlockHash: RootHex;
54
+ payloadParentBlockHash: RootHex;
55
+ }
56
+ | {
57
+ code: ExecutionPayloadEnvelopeErrorCode.BLOCK_HASH_MISMATCH;
58
+ bidBlockHash: RootHex;
59
+ payloadBlockHash: RootHex;
60
+ };
61
+
62
+ export class ExecutionPayloadEnvelopeError extends LodestarError<ExecutionPayloadEnvelopeErrorType> {}
63
+
64
+ /** The caller must have matched the selected bid to its local ledger record. */
65
+ export function createExecutionPayloadEnvelopeContents({
66
+ blockRoot,
67
+ builderIndex,
68
+ selectedBid,
69
+ storedPayload,
70
+ }: ExecutionPayloadEnvelopeInput): ExecutionPayloadEnvelopeContents {
71
+ if (builderIndex !== selectedBid.builderIndex) {
72
+ throw new ExecutionPayloadEnvelopeError({
73
+ code: ExecutionPayloadEnvelopeErrorCode.BUILDER_INDEX_MISMATCH,
74
+ builderIndex,
75
+ bidBuilderIndex: selectedBid.builderIndex,
76
+ });
77
+ }
78
+
79
+ const bidParentBlockRoot = toRootHex(selectedBid.parentBlockRoot);
80
+ const storedParentBlockRoot = toRootHex(storedPayload.parentBlockRoot);
81
+ if (storedParentBlockRoot !== bidParentBlockRoot) {
82
+ throw new ExecutionPayloadEnvelopeError(
83
+ {
84
+ code: ExecutionPayloadEnvelopeErrorCode.PARENT_BLOCK_ROOT_MISMATCH,
85
+ bidParentBlockRoot,
86
+ storedParentBlockRoot,
87
+ },
88
+ `Selected bid beacon parent does not match retained payload bidParentBlockRoot=${bidParentBlockRoot} storedParentBlockRoot=${storedParentBlockRoot}`
89
+ );
90
+ }
91
+
92
+ const {payload} = storedPayload;
93
+ const payloadSlot = payload.executionPayload.slotNumber;
94
+ if (payloadSlot !== selectedBid.slot) {
95
+ throw new ExecutionPayloadEnvelopeError(
96
+ {code: ExecutionPayloadEnvelopeErrorCode.SLOT_MISMATCH, bidSlot: selectedBid.slot, payloadSlot},
97
+ `Selected bid slot does not match payload slot bidSlot=${selectedBid.slot} payloadSlot=${payloadSlot}`
98
+ );
99
+ }
100
+
101
+ const payloadParentBlockHash = toRootHex(payload.executionPayload.parentHash);
102
+ const bidParentBlockHash = toRootHex(selectedBid.parentBlockHash);
103
+ if (payloadParentBlockHash !== bidParentBlockHash) {
104
+ throw new ExecutionPayloadEnvelopeError(
105
+ {
106
+ code: ExecutionPayloadEnvelopeErrorCode.PARENT_BLOCK_HASH_MISMATCH,
107
+ bidParentBlockHash,
108
+ payloadParentBlockHash,
109
+ },
110
+ `Selected bid parent does not match payload parent bidParentBlockHash=${bidParentBlockHash} payloadParentBlockHash=${payloadParentBlockHash}`
111
+ );
112
+ }
113
+
114
+ const payloadBlockHash = toRootHex(payload.executionPayload.blockHash);
115
+ const bidBlockHash = toRootHex(selectedBid.blockHash);
116
+ if (payloadBlockHash !== bidBlockHash) {
117
+ throw new ExecutionPayloadEnvelopeError(
118
+ {
119
+ code: ExecutionPayloadEnvelopeErrorCode.BLOCK_HASH_MISMATCH,
120
+ bidBlockHash,
121
+ payloadBlockHash,
122
+ },
123
+ `Selected bid block hash does not match payload bidBlockHash=${bidBlockHash} payloadBlockHash=${payloadBlockHash}`
124
+ );
125
+ }
126
+
127
+ if (
128
+ !ssz.gloas.ExecutionPayloadBid.fields.blobKzgCommitments.equals(
129
+ selectedBid.blobKzgCommitments,
130
+ payload.blobsBundle.commitments
131
+ )
132
+ ) {
133
+ throw new ExecutionPayloadEnvelopeError({code: ExecutionPayloadEnvelopeErrorCode.BLOB_KZG_COMMITMENTS_MISMATCH});
134
+ }
135
+
136
+ const executionRequestsRoot = ssz.gloas.ExecutionRequests.hashTreeRoot(payload.executionRequests);
137
+ if (!byteArrayEquals(executionRequestsRoot, selectedBid.executionRequestsRoot)) {
138
+ throw new ExecutionPayloadEnvelopeError({
139
+ code: ExecutionPayloadEnvelopeErrorCode.EXECUTION_REQUESTS_ROOT_MISMATCH,
140
+ bidExecutionRequestsRoot: toRootHex(selectedBid.executionRequestsRoot),
141
+ payloadExecutionRequestsRoot: toRootHex(executionRequestsRoot),
142
+ });
143
+ }
144
+
145
+ return {
146
+ envelope: {
147
+ payload: payload.executionPayload,
148
+ executionRequests: payload.executionRequests,
149
+ builderIndex,
150
+ beaconBlockRoot: fromHex(blockRoot),
151
+ parentBeaconBlockRoot: selectedBid.parentBlockRoot,
152
+ },
153
+ kzgProofs: payload.blobsBundle.proofs,
154
+ blobs: payload.blobsBundle.blobs,
155
+ };
156
+ }