@lodestar/builder 1.50.0-dev.f2d839a25c → 1.50.0-dev.f2fb26d453
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.
- package/lib/builder.d.ts +2 -0
- package/lib/builder.d.ts.map +1 -1
- package/lib/builder.js +56 -4
- package/lib/builder.js.map +1 -1
- package/lib/services/bidLedger.d.ts +8 -0
- package/lib/services/bidLedger.d.ts.map +1 -1
- package/lib/services/bidLedger.js +19 -10
- package/lib/services/bidLedger.js.map +1 -1
- package/lib/services/bidPublisher.d.ts +43 -0
- package/lib/services/bidPublisher.d.ts.map +1 -0
- package/lib/services/bidPublisher.js +50 -0
- package/lib/services/bidPublisher.js.map +1 -0
- package/lib/services/bidSelector.d.ts +67 -0
- package/lib/services/bidSelector.d.ts.map +1 -0
- package/lib/services/bidSelector.js +59 -0
- package/lib/services/bidSelector.js.map +1 -0
- package/lib/services/blockObserver.d.ts +1 -2
- package/lib/services/blockObserver.d.ts.map +1 -1
- package/lib/services/blockObserver.js +3 -35
- package/lib/services/blockObserver.js.map +1 -1
- package/lib/services/builderSigner.d.ts +2 -1
- package/lib/services/builderSigner.d.ts.map +1 -1
- package/lib/services/builderSigner.js.map +1 -1
- package/lib/services/envelopePublisher.d.ts +44 -0
- package/lib/services/envelopePublisher.d.ts.map +1 -0
- package/lib/services/envelopePublisher.js +96 -0
- package/lib/services/envelopePublisher.js.map +1 -0
- package/lib/services/executionPayloadBid.d.ts +57 -0
- package/lib/services/executionPayloadBid.d.ts.map +1 -0
- package/lib/services/executionPayloadBid.js +69 -0
- package/lib/services/executionPayloadBid.js.map +1 -0
- package/lib/services/executionPayloadEnvelope.d.ts +55 -0
- package/lib/services/executionPayloadEnvelope.d.ts.map +1 -0
- package/lib/services/executionPayloadEnvelope.js +79 -0
- package/lib/services/executionPayloadEnvelope.js.map +1 -0
- package/lib/services/proposerPreferencesTracker.d.ts +0 -6
- package/lib/services/proposerPreferencesTracker.d.ts.map +1 -1
- package/lib/services/proposerPreferencesTracker.js +0 -38
- package/lib/services/proposerPreferencesTracker.js.map +1 -1
- package/package.json +10 -10
- package/src/builder.ts +63 -5
- package/src/services/bidLedger.ts +33 -14
- package/src/services/bidPublisher.ts +95 -0
- package/src/services/bidSelector.ts +138 -0
- package/src/services/blockObserver.ts +3 -41
- package/src/services/builderSigner.ts +3 -1
- package/src/services/envelopePublisher.ts +154 -0
- package/src/services/executionPayloadBid.ts +125 -0
- package/src/services/executionPayloadEnvelope.ts +156 -0
- package/src/services/proposerPreferencesTracker.ts +1 -36
package/src/builder.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {ApiClient} from "@lodestar/api";
|
|
1
|
+
import {ApiClient, routes} from "@lodestar/api";
|
|
2
2
|
import {ChainForkConfig, assertEqualParams, createBeaconConfig} from "@lodestar/config";
|
|
3
3
|
import {Clock, ClockOptions, IClock} from "@lodestar/state-transition";
|
|
4
4
|
import {BuilderIndex, ExecutionAddress} from "@lodestar/types";
|
|
5
|
-
import {Logger, toHex, toRootHex} from "@lodestar/utils";
|
|
5
|
+
import {Logger, isErrorAborted, toHex, toRootHex} from "@lodestar/utils";
|
|
6
6
|
import {waitForGenesis} from "./genesis.js";
|
|
7
7
|
import {resolveBuilderIdentity} from "./identity.js";
|
|
8
8
|
import {Metrics} from "./metrics.js";
|
|
@@ -75,8 +75,7 @@ export class Builder {
|
|
|
75
75
|
this.clock.runEverySlot(async (slot) => this.onSlot(slot));
|
|
76
76
|
this.clock.runEveryEpoch((epoch) => this.builderStatusTracker.poll(epoch));
|
|
77
77
|
this.clock.start(this.controller.signal);
|
|
78
|
-
this.
|
|
79
|
-
this.proposerPreferencesTracker.start(this.controller.signal);
|
|
78
|
+
this.subscribeToEvents(opts.api);
|
|
80
79
|
|
|
81
80
|
this.logger.info("Builder client initialized", {
|
|
82
81
|
index: this.index,
|
|
@@ -114,7 +113,7 @@ export class Builder {
|
|
|
114
113
|
|
|
115
114
|
const builderStatusTracker = new BuilderStatusTracker(api, logger, index, opts.metrics);
|
|
116
115
|
const blockObserver = new BlockObserver(config, logger, api);
|
|
117
|
-
const proposerPreferencesTracker = new ProposerPreferencesTracker(
|
|
116
|
+
const proposerPreferencesTracker = new ProposerPreferencesTracker();
|
|
118
117
|
|
|
119
118
|
const payloadStore = new PayloadStore();
|
|
120
119
|
|
|
@@ -135,6 +134,65 @@ export class Builder {
|
|
|
135
134
|
this.proposerPreferencesTracker.prune(slot);
|
|
136
135
|
}
|
|
137
136
|
|
|
137
|
+
private subscribeToEvents(api: ApiClient): void {
|
|
138
|
+
const signal = this.controller.signal;
|
|
139
|
+
if (signal.aborted) return;
|
|
140
|
+
|
|
141
|
+
const topics = [routes.events.EventType.block, routes.events.EventType.proposerPreferences];
|
|
142
|
+
this.logger.verbose("Subscribing to builder events", {topics: topics.join(",")});
|
|
143
|
+
api.events
|
|
144
|
+
.eventstream({
|
|
145
|
+
topics,
|
|
146
|
+
signal,
|
|
147
|
+
onEvent: (event) => {
|
|
148
|
+
void this.onEvent(event);
|
|
149
|
+
},
|
|
150
|
+
onError: (error) => {
|
|
151
|
+
if (!signal.aborted) this.logger.error("Failed to receive builder event", {topics: topics.join(",")}, error);
|
|
152
|
+
},
|
|
153
|
+
onClose: () => {
|
|
154
|
+
if (signal.aborted) {
|
|
155
|
+
this.logger.verbose("Closed builder event stream", {topics: topics.join(",")});
|
|
156
|
+
} else {
|
|
157
|
+
this.logger.error("Builder event stream closed unexpectedly", {topics: topics.join(",")});
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
})
|
|
161
|
+
.catch((error: unknown) => {
|
|
162
|
+
if (!signal.aborted && !isErrorAborted(error)) {
|
|
163
|
+
this.logger.error(
|
|
164
|
+
"Failed to subscribe to builder events",
|
|
165
|
+
{topics: topics.join(",")},
|
|
166
|
+
error instanceof Error ? error : Error(String(error))
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private async onEvent(event: routes.events.BeaconEvent): Promise<void> {
|
|
173
|
+
const signal = this.controller.signal;
|
|
174
|
+
if (signal.aborted) return;
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
switch (event.type) {
|
|
178
|
+
case routes.events.EventType.block:
|
|
179
|
+
await this.blockObserver.processBlockEvent(event.message, signal);
|
|
180
|
+
break;
|
|
181
|
+
case routes.events.EventType.proposerPreferences:
|
|
182
|
+
this.proposerPreferencesTracker.onProposerPreferences(event.message.data);
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if (!signal.aborted && !isErrorAborted(error)) {
|
|
187
|
+
this.logger.warn(
|
|
188
|
+
"Failed to process builder event",
|
|
189
|
+
{eventType: event.type},
|
|
190
|
+
error instanceof Error ? error : Error(String(error))
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
138
196
|
async close(): Promise<void> {
|
|
139
197
|
this.controller.abort();
|
|
140
198
|
}
|
|
@@ -32,6 +32,7 @@ type MutableBidLedgerRecord = SubmittedBid & {
|
|
|
32
32
|
export enum BidLedgerErrorCode {
|
|
33
33
|
INVALID_BID_VALUE = "BID_LEDGER_ERROR_INVALID_BID_VALUE",
|
|
34
34
|
DUPLICATE_BID = "BID_LEDGER_ERROR_DUPLICATE_BID",
|
|
35
|
+
BID_TOO_OLD = "BID_LEDGER_ERROR_BID_TOO_OLD",
|
|
35
36
|
REVEAL_CONFLICT = "BID_LEDGER_ERROR_REVEAL_CONFLICT",
|
|
36
37
|
UNSETTLED_VALUE_OVERFLOW = "BID_LEDGER_ERROR_UNSETTLED_VALUE_OVERFLOW",
|
|
37
38
|
}
|
|
@@ -47,6 +48,11 @@ export type BidLedgerErrorType =
|
|
|
47
48
|
parentBlockHash: RootHex;
|
|
48
49
|
parentBlockRoot: RootHex;
|
|
49
50
|
}
|
|
51
|
+
| {
|
|
52
|
+
code: BidLedgerErrorCode.BID_TOO_OLD;
|
|
53
|
+
slot: Slot;
|
|
54
|
+
pruneCutoffSlot: Slot;
|
|
55
|
+
}
|
|
50
56
|
| {
|
|
51
57
|
code: BidLedgerErrorCode.REVEAL_CONFLICT;
|
|
52
58
|
blockRoot: RootHex;
|
|
@@ -69,11 +75,28 @@ const KEEP_SLOTS = RECORD_RETENTION_EPOCHS * SLOTS_PER_EPOCH;
|
|
|
69
75
|
export class BidLedger {
|
|
70
76
|
private readonly bidsBySlot = new Map<Slot, Map<string, MutableBidLedgerRecord>>();
|
|
71
77
|
private readonly revealedPayloadByBlockRoot = new Map<RootHex, RevealedPayload>();
|
|
78
|
+
private pruneCutoffSlot = 0;
|
|
72
79
|
|
|
73
80
|
hasSubmitted(slot: Slot, parentBlockHash: RootHex, parentBlockRoot: RootHex): boolean {
|
|
74
81
|
return this.bidsBySlot.get(slot)?.has(tupleKey(parentBlockHash, parentBlockRoot)) ?? false;
|
|
75
82
|
}
|
|
76
83
|
|
|
84
|
+
assertCanRecordBid({
|
|
85
|
+
slot,
|
|
86
|
+
parentBlockHash,
|
|
87
|
+
parentBlockRoot,
|
|
88
|
+
}: Pick<SubmittedBid, "slot" | "parentBlockHash" | "parentBlockRoot">): void {
|
|
89
|
+
if (slot < this.pruneCutoffSlot) {
|
|
90
|
+
throw new BidLedgerError({code: BidLedgerErrorCode.BID_TOO_OLD, slot, pruneCutoffSlot: this.pruneCutoffSlot});
|
|
91
|
+
}
|
|
92
|
+
if (this.hasSubmitted(slot, parentBlockHash, parentBlockRoot)) {
|
|
93
|
+
throw new BidLedgerError(
|
|
94
|
+
{code: BidLedgerErrorCode.DUPLICATE_BID, slot, parentBlockHash, parentBlockRoot},
|
|
95
|
+
`Bid already recorded slot=${slot} parentBlockHash=${parentBlockHash} parentBlockRoot=${parentBlockRoot}`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
77
100
|
recordBid(bid: SubmittedBid): BidLedgerRecord {
|
|
78
101
|
if (!Number.isSafeInteger(bid.valueGwei) || bid.valueGwei < 0) {
|
|
79
102
|
throw new BidLedgerError(
|
|
@@ -82,6 +105,7 @@ export class BidLedger {
|
|
|
82
105
|
);
|
|
83
106
|
}
|
|
84
107
|
|
|
108
|
+
this.assertCanRecordBid(bid);
|
|
85
109
|
let bidsForSlot = this.bidsBySlot.get(bid.slot);
|
|
86
110
|
if (bidsForSlot === undefined) {
|
|
87
111
|
bidsForSlot = new Map();
|
|
@@ -89,18 +113,6 @@ export class BidLedger {
|
|
|
89
113
|
}
|
|
90
114
|
|
|
91
115
|
const key = tupleKey(bid.parentBlockHash, bid.parentBlockRoot);
|
|
92
|
-
if (bidsForSlot.has(key)) {
|
|
93
|
-
throw new BidLedgerError(
|
|
94
|
-
{
|
|
95
|
-
code: BidLedgerErrorCode.DUPLICATE_BID,
|
|
96
|
-
slot: bid.slot,
|
|
97
|
-
parentBlockHash: bid.parentBlockHash,
|
|
98
|
-
parentBlockRoot: bid.parentBlockRoot,
|
|
99
|
-
},
|
|
100
|
-
`Bid already recorded slot=${bid.slot} parentBlockHash=${bid.parentBlockHash} parentBlockRoot=${bid.parentBlockRoot}`
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
116
|
const record = {...bid, wonBlockRoots: new Set<RootHex>(), paymentSettled: false};
|
|
105
117
|
bidsForSlot.set(key, record);
|
|
106
118
|
return toRecord(record);
|
|
@@ -116,6 +128,11 @@ export class BidLedger {
|
|
|
116
128
|
return toRecord(record);
|
|
117
129
|
}
|
|
118
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
|
+
|
|
119
136
|
/** Release a winning bid liability only after its payment is known to be reflected in Builder balance. */
|
|
120
137
|
recordPaymentSettled(identity: BidIdentity): BidLedgerRecord | null {
|
|
121
138
|
const record = this.getBid(identity.slot, identity.parentBlockHash, identity.parentBlockRoot);
|
|
@@ -198,9 +215,11 @@ export class BidLedger {
|
|
|
198
215
|
}
|
|
199
216
|
|
|
200
217
|
prune(currentSlot: Slot): number {
|
|
218
|
+
// Pruning must not make an expired slot eligible for another submission.
|
|
219
|
+
this.pruneCutoffSlot = Math.max(this.pruneCutoffSlot, currentSlot - KEEP_SLOTS);
|
|
201
220
|
let removed = 0;
|
|
202
221
|
for (const [slot, bidsForSlot] of this.bidsBySlot) {
|
|
203
|
-
if (slot >=
|
|
222
|
+
if (slot >= this.pruneCutoffSlot) {
|
|
204
223
|
continue;
|
|
205
224
|
}
|
|
206
225
|
|
|
@@ -216,7 +235,7 @@ export class BidLedger {
|
|
|
216
235
|
}
|
|
217
236
|
|
|
218
237
|
for (const [blockRoot, revealedPayload] of this.revealedPayloadByBlockRoot) {
|
|
219
|
-
if (revealedPayload.slot <
|
|
238
|
+
if (revealedPayload.slot < this.pruneCutoffSlot) {
|
|
220
239
|
this.revealedPayloadByBlockRoot.delete(blockRoot);
|
|
221
240
|
}
|
|
222
241
|
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type {ApiClient} from "@lodestar/api";
|
|
2
|
+
import type {ChainForkConfig} from "@lodestar/config";
|
|
3
|
+
import {isForkPostGloas} from "@lodestar/params";
|
|
4
|
+
import type {BuilderIndex, RootHex, Slot, gloas, heze} from "@lodestar/types";
|
|
5
|
+
import {sszTypesFor} from "@lodestar/types";
|
|
6
|
+
import {LodestarError, toRootHex} from "@lodestar/utils";
|
|
7
|
+
import type {BidIdentity, BidLedger} from "./bidLedger.js";
|
|
8
|
+
import type {BuilderSigner} from "./builderSigner.js";
|
|
9
|
+
|
|
10
|
+
export type BidPublisherModules = {
|
|
11
|
+
api: ApiClient;
|
|
12
|
+
config: ChainForkConfig;
|
|
13
|
+
signer: BuilderSigner;
|
|
14
|
+
ledger: BidLedger;
|
|
15
|
+
builderIndex: BuilderIndex;
|
|
16
|
+
hasPayload: (identity: BidIdentity) => boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export enum BidPublisherErrorCode {
|
|
20
|
+
BUILDER_INDEX_MISMATCH = "BID_PUBLISHER_ERROR_BUILDER_INDEX_MISMATCH",
|
|
21
|
+
PAYLOAD_NOT_RETAINED = "BID_PUBLISHER_ERROR_PAYLOAD_NOT_RETAINED",
|
|
22
|
+
PRE_GLOAS_BID = "BID_PUBLISHER_ERROR_PRE_GLOAS_BID",
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type BidPublisherErrorType =
|
|
26
|
+
| {
|
|
27
|
+
code: BidPublisherErrorCode.PRE_GLOAS_BID;
|
|
28
|
+
slot: Slot;
|
|
29
|
+
}
|
|
30
|
+
| {
|
|
31
|
+
code: BidPublisherErrorCode.BUILDER_INDEX_MISMATCH;
|
|
32
|
+
builderIndex: BuilderIndex;
|
|
33
|
+
bidBuilderIndex: BuilderIndex;
|
|
34
|
+
}
|
|
35
|
+
| {
|
|
36
|
+
code: BidPublisherErrorCode.PAYLOAD_NOT_RETAINED;
|
|
37
|
+
slot: Slot;
|
|
38
|
+
parentBlockHash: RootHex;
|
|
39
|
+
parentBlockRoot: RootHex;
|
|
40
|
+
blockHash: RootHex;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export class BidPublisherError extends LodestarError<BidPublisherErrorType> {}
|
|
44
|
+
|
|
45
|
+
/** Signs and submits a bid only if its payload is retained locally. */
|
|
46
|
+
export class BidPublisher {
|
|
47
|
+
constructor(private readonly modules: BidPublisherModules) {}
|
|
48
|
+
|
|
49
|
+
publish(bid: heze.ExecutionPayloadBid, signal: AbortSignal): Promise<heze.SignedExecutionPayloadBid>;
|
|
50
|
+
publish(bid: gloas.ExecutionPayloadBid, signal: AbortSignal): Promise<gloas.SignedExecutionPayloadBid>;
|
|
51
|
+
async publish(bid: gloas.ExecutionPayloadBid, signal: AbortSignal): Promise<gloas.SignedExecutionPayloadBid> {
|
|
52
|
+
signal.throwIfAborted();
|
|
53
|
+
|
|
54
|
+
const {api, config, builderIndex, hasPayload, ledger, signer} = this.modules;
|
|
55
|
+
const fork = config.getForkName(bid.slot);
|
|
56
|
+
if (!isForkPostGloas(fork)) {
|
|
57
|
+
throw new BidPublisherError({code: BidPublisherErrorCode.PRE_GLOAS_BID, slot: bid.slot});
|
|
58
|
+
}
|
|
59
|
+
if (bid.builderIndex !== builderIndex) {
|
|
60
|
+
throw new BidPublisherError(
|
|
61
|
+
{
|
|
62
|
+
code: BidPublisherErrorCode.BUILDER_INDEX_MISMATCH,
|
|
63
|
+
builderIndex,
|
|
64
|
+
bidBuilderIndex: bid.builderIndex,
|
|
65
|
+
},
|
|
66
|
+
`Bid builder index does not match local builder index builderIndex=${builderIndex} bidBuilderIndex=${bid.builderIndex}`
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const identity: BidIdentity = {
|
|
71
|
+
slot: bid.slot,
|
|
72
|
+
parentBlockHash: toRootHex(bid.parentBlockHash),
|
|
73
|
+
parentBlockRoot: toRootHex(bid.parentBlockRoot),
|
|
74
|
+
blockHash: toRootHex(bid.blockHash),
|
|
75
|
+
};
|
|
76
|
+
if (!hasPayload(identity)) {
|
|
77
|
+
throw new BidPublisherError(
|
|
78
|
+
{code: BidPublisherErrorCode.PAYLOAD_NOT_RETAINED, ...identity},
|
|
79
|
+
`Bid payload is not retained slot=${identity.slot} parentBlockHash=${identity.parentBlockHash} parentBlockRoot=${identity.parentBlockRoot} blockHash=${identity.blockHash}`
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
ledger.assertCanRecordBid(identity);
|
|
84
|
+
|
|
85
|
+
const signedExecutionPayloadBid = signer.signExecutionPayloadBid(bid);
|
|
86
|
+
const signedBidRoot = toRootHex(
|
|
87
|
+
sszTypesFor(fork, "SignedExecutionPayloadBid").hashTreeRoot(signedExecutionPayloadBid)
|
|
88
|
+
);
|
|
89
|
+
ledger.recordBid({...identity, valueGwei: bid.value, signedBidRoot});
|
|
90
|
+
|
|
91
|
+
const response = await api.beacon.publishExecutionPayloadBid({signedExecutionPayloadBid}, {signal});
|
|
92
|
+
response.assertOk();
|
|
93
|
+
return signedExecutionPayloadBid;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -47,55 +47,17 @@ export class BlockObserver {
|
|
|
47
47
|
this.retries = retries;
|
|
48
48
|
this.retryDelay = retryDelay;
|
|
49
49
|
this.maxSeenBlockRoots = maxSeenBlockRoots;
|
|
50
|
+
this.logger.info("Block observer initialized", {retries, retryDelay, maxSeenBlockRoots});
|
|
50
51
|
}
|
|
51
52
|
|
|
52
53
|
/**
|
|
53
|
-
* Register a block consumer. Consumers that require complete observation must register before
|
|
54
|
+
* Register a block consumer. Consumers that require complete observation must register before event delivery, because
|
|
54
55
|
* roots observed before registration remain consumed until normal FIFO eviction.
|
|
55
56
|
*/
|
|
56
57
|
runOnBlock(fn: RunOnBlockFn): void {
|
|
57
58
|
this.fns.push(fn);
|
|
58
59
|
}
|
|
59
60
|
|
|
60
|
-
start(signal: AbortSignal): void {
|
|
61
|
-
this.logger.info("Subscribing to block events", {
|
|
62
|
-
retries: this.retries,
|
|
63
|
-
retryDelay: this.retryDelay,
|
|
64
|
-
maxSeenBlockRoots: this.maxSeenBlockRoots,
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
this.api.events
|
|
68
|
-
.eventstream({
|
|
69
|
-
topics: [EventType.block],
|
|
70
|
-
signal,
|
|
71
|
-
onEvent: (event) => {
|
|
72
|
-
if (event.type !== EventType.block) {
|
|
73
|
-
this.logger.debug("Ignoring unexpected beacon event", {eventType: event.type});
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
void this.processBlockEvent(event.message, signal);
|
|
78
|
-
},
|
|
79
|
-
onError: (error) => {
|
|
80
|
-
this.logger.error("Failed to receive block event", {}, error);
|
|
81
|
-
},
|
|
82
|
-
onClose: () => {
|
|
83
|
-
if (signal.aborted) {
|
|
84
|
-
this.logger.debug("Closed stream for block events");
|
|
85
|
-
} else {
|
|
86
|
-
this.logger.error("Block event stream closed unexpectedly", {});
|
|
87
|
-
}
|
|
88
|
-
},
|
|
89
|
-
})
|
|
90
|
-
.catch((error: unknown) => {
|
|
91
|
-
this.logger.error(
|
|
92
|
-
"Failed to subscribe to block events",
|
|
93
|
-
{},
|
|
94
|
-
error instanceof Error ? error : Error(String(error))
|
|
95
|
-
);
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
61
|
async processBlockEvent(event: BlockEvent, signal: AbortSignal): Promise<void> {
|
|
100
62
|
try {
|
|
101
63
|
const {slot, block: blockRoot, executionOptimistic} = event;
|
|
@@ -145,7 +107,7 @@ export class BlockObserver {
|
|
|
145
107
|
return null;
|
|
146
108
|
});
|
|
147
109
|
|
|
148
|
-
if (response === null) {
|
|
110
|
+
if (response === null || signal.aborted) {
|
|
149
111
|
return;
|
|
150
112
|
}
|
|
151
113
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {PublicKey, SecretKey} from "@chainsafe/lodestar-z/blst";
|
|
2
2
|
import {BeaconConfig} from "@lodestar/config";
|
|
3
3
|
import {getExecutionPayloadBidSigningRoot, getExecutionPayloadEnvelopeSigningRoot} from "@lodestar/state-transition";
|
|
4
|
-
import {gloas} from "@lodestar/types";
|
|
4
|
+
import {gloas, heze} from "@lodestar/types";
|
|
5
5
|
|
|
6
6
|
export type Keypair = {publicKey: PublicKey; secretKey: SecretKey};
|
|
7
7
|
|
|
@@ -23,6 +23,8 @@ export class BuilderSigner {
|
|
|
23
23
|
};
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
signExecutionPayloadBid(bid: heze.ExecutionPayloadBid): heze.SignedExecutionPayloadBid;
|
|
27
|
+
signExecutionPayloadBid(bid: gloas.ExecutionPayloadBid): gloas.SignedExecutionPayloadBid;
|
|
26
28
|
signExecutionPayloadBid(bid: gloas.ExecutionPayloadBid): gloas.SignedExecutionPayloadBid {
|
|
27
29
|
const signingRoot = getExecutionPayloadBidSigningRoot(this.config, bid);
|
|
28
30
|
|
|
@@ -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
|
+
}
|