@lodestar/builder 1.48.0-dev.e9c9b8835d → 1.48.0-dev.fd70e5fc15
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 +1 -11
- package/lib/builder.d.ts.map +1 -1
- package/lib/builder.js +2 -30
- package/lib/builder.js.map +1 -1
- package/lib/identity.d.ts +1 -0
- package/lib/identity.d.ts.map +1 -1
- package/lib/identity.js +4 -11
- package/lib/identity.js.map +1 -1
- package/package.json +10 -10
- package/src/builder.ts +3 -45
- package/src/identity.ts +6 -13
- package/lib/services/bidLedger.d.ts +0 -61
- package/lib/services/bidLedger.d.ts.map +0 -1
- package/lib/services/bidLedger.js +0 -154
- package/lib/services/bidLedger.js.map +0 -1
- package/lib/services/bidPolicy.d.ts +0 -30
- package/lib/services/bidPolicy.d.ts.map +0 -1
- package/lib/services/bidPolicy.js +0 -37
- package/lib/services/bidPolicy.js.map +0 -1
- package/lib/services/blockObserver.d.ts +0 -48
- package/lib/services/blockObserver.d.ts.map +0 -1
- package/lib/services/blockObserver.js +0 -172
- package/lib/services/blockObserver.js.map +0 -1
- package/lib/services/payloadStore.d.ts +0 -25
- package/lib/services/payloadStore.d.ts.map +0 -1
- package/lib/services/payloadStore.js +0 -25
- package/lib/services/payloadStore.js.map +0 -1
- package/lib/services/proposerPreferencesTracker.d.ts +0 -15
- package/lib/services/proposerPreferencesTracker.d.ts.map +0 -1
- package/lib/services/proposerPreferencesTracker.js +0 -68
- package/lib/services/proposerPreferencesTracker.js.map +0 -1
- package/src/services/bidLedger.ts +0 -245
- package/src/services/bidPolicy.ts +0 -65
- package/src/services/blockObserver.ts +0 -231
- package/src/services/payloadStore.ts +0 -51
- package/src/services/proposerPreferencesTracker.ts +0 -74
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
import {SLOTS_PER_EPOCH} from "@lodestar/params";
|
|
2
|
-
import type {Epoch, RootHex, Slot} from "@lodestar/types";
|
|
3
|
-
import {LodestarError} from "@lodestar/utils";
|
|
4
|
-
|
|
5
|
-
export type SubmittedBid = {
|
|
6
|
-
slot: Slot;
|
|
7
|
-
parentBlockHash: RootHex;
|
|
8
|
-
parentBlockRoot: RootHex;
|
|
9
|
-
blockHash: RootHex;
|
|
10
|
-
valueGwei: number;
|
|
11
|
-
signedBidRoot: RootHex;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export type BidIdentity = Pick<SubmittedBid, "slot" | "parentBlockHash" | "parentBlockRoot" | "blockHash">;
|
|
15
|
-
|
|
16
|
-
export type BidLedgerRecord = SubmittedBid & {
|
|
17
|
-
wonBlockRoots: RootHex[];
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
type RevealedPayload = {
|
|
21
|
-
slot: Slot;
|
|
22
|
-
blockHash: RootHex;
|
|
23
|
-
envelopeRoot: RootHex;
|
|
24
|
-
published: boolean;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
type MutableBidLedgerRecord = SubmittedBid & {
|
|
28
|
-
wonBlockRoots: Set<RootHex>;
|
|
29
|
-
paymentSettled: boolean;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export enum BidLedgerErrorCode {
|
|
33
|
-
INVALID_BID_VALUE = "BID_LEDGER_ERROR_INVALID_BID_VALUE",
|
|
34
|
-
DUPLICATE_BID = "BID_LEDGER_ERROR_DUPLICATE_BID",
|
|
35
|
-
REVEAL_CONFLICT = "BID_LEDGER_ERROR_REVEAL_CONFLICT",
|
|
36
|
-
UNSETTLED_VALUE_OVERFLOW = "BID_LEDGER_ERROR_UNSETTLED_VALUE_OVERFLOW",
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export type BidLedgerErrorType =
|
|
40
|
-
| {
|
|
41
|
-
code: BidLedgerErrorCode.INVALID_BID_VALUE;
|
|
42
|
-
valueGwei: number;
|
|
43
|
-
}
|
|
44
|
-
| {
|
|
45
|
-
code: BidLedgerErrorCode.DUPLICATE_BID;
|
|
46
|
-
slot: Slot;
|
|
47
|
-
parentBlockHash: RootHex;
|
|
48
|
-
parentBlockRoot: RootHex;
|
|
49
|
-
}
|
|
50
|
-
| {
|
|
51
|
-
code: BidLedgerErrorCode.REVEAL_CONFLICT;
|
|
52
|
-
blockRoot: RootHex;
|
|
53
|
-
blockHash: RootHex;
|
|
54
|
-
revealedBlockHash: RootHex;
|
|
55
|
-
envelopeRoot: RootHex;
|
|
56
|
-
revealedEnvelopeRoot: RootHex;
|
|
57
|
-
}
|
|
58
|
-
| {
|
|
59
|
-
code: BidLedgerErrorCode.UNSETTLED_VALUE_OVERFLOW;
|
|
60
|
-
currentEpoch: Epoch;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export class BidLedgerError extends LodestarError<BidLedgerErrorType> {}
|
|
64
|
-
|
|
65
|
-
const RECORD_RETENTION_EPOCHS = 3;
|
|
66
|
-
const KEEP_SLOTS = RECORD_RETENTION_EPOCHS * SLOTS_PER_EPOCH;
|
|
67
|
-
|
|
68
|
-
/** Tracks at most one bid per (slot, parentBlockHash, parentBlockRoot) and its reveal obligations. */
|
|
69
|
-
export class BidLedger {
|
|
70
|
-
private readonly bidsBySlot = new Map<Slot, Map<string, MutableBidLedgerRecord>>();
|
|
71
|
-
private readonly revealedPayloadByBlockRoot = new Map<RootHex, RevealedPayload>();
|
|
72
|
-
|
|
73
|
-
hasSubmitted(slot: Slot, parentBlockHash: RootHex, parentBlockRoot: RootHex): boolean {
|
|
74
|
-
return this.bidsBySlot.get(slot)?.has(tupleKey(parentBlockHash, parentBlockRoot)) ?? false;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
recordBid(bid: SubmittedBid): BidLedgerRecord {
|
|
78
|
-
if (!Number.isSafeInteger(bid.valueGwei) || bid.valueGwei < 0) {
|
|
79
|
-
throw new BidLedgerError(
|
|
80
|
-
{code: BidLedgerErrorCode.INVALID_BID_VALUE, valueGwei: bid.valueGwei},
|
|
81
|
-
`Invalid bid value valueGwei=${bid.valueGwei}`
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
let bidsForSlot = this.bidsBySlot.get(bid.slot);
|
|
86
|
-
if (bidsForSlot === undefined) {
|
|
87
|
-
bidsForSlot = new Map();
|
|
88
|
-
this.bidsBySlot.set(bid.slot, bidsForSlot);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
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
|
-
const record = {...bid, wonBlockRoots: new Set<RootHex>(), paymentSettled: false};
|
|
105
|
-
bidsForSlot.set(key, record);
|
|
106
|
-
return toRecord(record);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
recordWin(identity: BidIdentity & Pick<SubmittedBid, "signedBidRoot">, blockRoot: RootHex): BidLedgerRecord | null {
|
|
110
|
-
const record = this.getBid(identity.slot, identity.parentBlockHash, identity.parentBlockRoot);
|
|
111
|
-
if (record === null || record.blockHash !== identity.blockHash || record.signedBidRoot !== identity.signedBidRoot) {
|
|
112
|
-
return null;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
record.wonBlockRoots.add(blockRoot);
|
|
116
|
-
return toRecord(record);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/** Release a winning bid liability only after its payment is known to be reflected in Builder balance. */
|
|
120
|
-
recordPaymentSettled(identity: BidIdentity): BidLedgerRecord | null {
|
|
121
|
-
const record = this.getBid(identity.slot, identity.parentBlockHash, identity.parentBlockRoot);
|
|
122
|
-
if (record === null || record.blockHash !== identity.blockHash || record.wonBlockRoots.size === 0) {
|
|
123
|
-
return null;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
record.paymentSettled = true;
|
|
127
|
-
return toRecord(record);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
canReveal(blockRoot: RootHex, blockHash: RootHex, envelopeRoot: RootHex): boolean {
|
|
131
|
-
const revealedPayload = this.revealedPayloadByBlockRoot.get(blockRoot);
|
|
132
|
-
return (
|
|
133
|
-
revealedPayload === undefined ||
|
|
134
|
-
(revealedPayload.blockHash === blockHash && revealedPayload.envelopeRoot === envelopeRoot)
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
hasRevealed(blockRoot: RootHex): boolean {
|
|
139
|
-
return this.revealedPayloadByBlockRoot.has(blockRoot);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
hasPublishedReveal(blockRoot: RootHex): boolean {
|
|
143
|
-
return this.revealedPayloadByBlockRoot.get(blockRoot)?.published ?? false;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
recordReveal(slot: Slot, blockRoot: RootHex, blockHash: RootHex, envelopeRoot: RootHex): void {
|
|
147
|
-
const revealedPayload = this.revealedPayloadByBlockRoot.get(blockRoot);
|
|
148
|
-
if (revealedPayload !== undefined) {
|
|
149
|
-
if (revealedPayload.blockHash !== blockHash || revealedPayload.envelopeRoot !== envelopeRoot) {
|
|
150
|
-
throw new BidLedgerError(
|
|
151
|
-
{
|
|
152
|
-
code: BidLedgerErrorCode.REVEAL_CONFLICT,
|
|
153
|
-
blockRoot,
|
|
154
|
-
blockHash,
|
|
155
|
-
revealedBlockHash: revealedPayload.blockHash,
|
|
156
|
-
envelopeRoot,
|
|
157
|
-
revealedEnvelopeRoot: revealedPayload.envelopeRoot,
|
|
158
|
-
},
|
|
159
|
-
`Different envelope already recorded blockRoot=${blockRoot} envelopeRoot=${revealedPayload.envelopeRoot}`
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
this.revealedPayloadByBlockRoot.set(blockRoot, {slot, blockHash, envelopeRoot, published: false});
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
recordRevealPublished(slot: Slot, blockRoot: RootHex, blockHash: RootHex, envelopeRoot: RootHex): void {
|
|
169
|
-
this.recordReveal(slot, blockRoot, blockHash, envelopeRoot);
|
|
170
|
-
const revealedPayload = this.revealedPayloadByBlockRoot.get(blockRoot);
|
|
171
|
-
if (revealedPayload !== undefined) {
|
|
172
|
-
revealedPayload.published = true;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
getUnsettledValueGwei(currentEpoch: Epoch): number {
|
|
177
|
-
let total = 0;
|
|
178
|
-
for (const bidsForSlot of this.bidsBySlot.values()) {
|
|
179
|
-
for (const record of bidsForSlot.values()) {
|
|
180
|
-
if (record.wonBlockRoots.size === 0 || record.paymentSettled) {
|
|
181
|
-
continue;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
total += record.valueGwei;
|
|
185
|
-
if (!Number.isSafeInteger(total)) {
|
|
186
|
-
throw new BidLedgerError(
|
|
187
|
-
{code: BidLedgerErrorCode.UNSETTLED_VALUE_OVERFLOW, currentEpoch},
|
|
188
|
-
`Unsettled bid value exceeds the safe integer range currentEpoch=${currentEpoch}`
|
|
189
|
-
);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
return total;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
getBidsForSlot(slot: Slot): BidLedgerRecord[] {
|
|
197
|
-
return Array.from(this.bidsBySlot.get(slot)?.values() ?? [], toRecord);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
prune(currentSlot: Slot): number {
|
|
201
|
-
let removed = 0;
|
|
202
|
-
for (const [slot, bidsForSlot] of this.bidsBySlot) {
|
|
203
|
-
if (slot >= currentSlot - KEEP_SLOTS) {
|
|
204
|
-
continue;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
for (const [key, record] of bidsForSlot) {
|
|
208
|
-
if (record.wonBlockRoots.size === 0 || record.paymentSettled) {
|
|
209
|
-
bidsForSlot.delete(key);
|
|
210
|
-
removed++;
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
if (bidsForSlot.size === 0) {
|
|
214
|
-
this.bidsBySlot.delete(slot);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
for (const [blockRoot, revealedPayload] of this.revealedPayloadByBlockRoot) {
|
|
219
|
-
if (revealedPayload.slot < currentSlot - KEEP_SLOTS) {
|
|
220
|
-
this.revealedPayloadByBlockRoot.delete(blockRoot);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
return removed;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
private getBid(slot: Slot, parentBlockHash: RootHex, parentBlockRoot: RootHex): MutableBidLedgerRecord | null {
|
|
227
|
-
return this.bidsBySlot.get(slot)?.get(tupleKey(parentBlockHash, parentBlockRoot)) ?? null;
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
function tupleKey(parentBlockHash: RootHex, parentBlockRoot: RootHex): string {
|
|
232
|
-
return `${parentBlockHash}:${parentBlockRoot}`;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
function toRecord(record: MutableBidLedgerRecord): BidLedgerRecord {
|
|
236
|
-
return {
|
|
237
|
-
slot: record.slot,
|
|
238
|
-
parentBlockHash: record.parentBlockHash,
|
|
239
|
-
parentBlockRoot: record.parentBlockRoot,
|
|
240
|
-
blockHash: record.blockHash,
|
|
241
|
-
valueGwei: record.valueGwei,
|
|
242
|
-
signedBidRoot: record.signedBidRoot,
|
|
243
|
-
wonBlockRoots: Array.from(record.wonBlockRoots),
|
|
244
|
-
};
|
|
245
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
export type BidContext = {
|
|
2
|
-
/** Value of the payload to the builder's fee recipient, as reported by the execution client */
|
|
3
|
-
payloadValueGwei: number;
|
|
4
|
-
/** Builder balance that can back a bid, excess over the minimum and unsettled payments */
|
|
5
|
-
coverableGwei: number;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
/** Decides how much to pay the proposer for a payload, null means do not bid */
|
|
9
|
-
export interface BidPolicy {
|
|
10
|
-
computeValue(ctx: BidContext): number | null;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export type ProportionalBidPolicyOpts = {
|
|
14
|
-
/** Share of the payload value offered to the proposer, in basis points */
|
|
15
|
-
shareBps: number;
|
|
16
|
-
/** Fixed amount deducted from the share, e.g. to cover operating cost */
|
|
17
|
-
fixedCostGwei: number;
|
|
18
|
-
/** Never bid below this value */
|
|
19
|
-
minValueGwei: number;
|
|
20
|
-
/** Never bid above this value */
|
|
21
|
-
maxValueGwei?: number;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Offers a fixed share of the payload value, bounded by the configured limits and the
|
|
26
|
-
* builder's coverable balance. Independent of competing bids.
|
|
27
|
-
*/
|
|
28
|
-
export class ProportionalBidPolicy implements BidPolicy {
|
|
29
|
-
constructor(private readonly opts: ProportionalBidPolicyOpts) {
|
|
30
|
-
if (!Number.isSafeInteger(opts.shareBps) || opts.shareBps < 0 || opts.shareBps > 10_000) {
|
|
31
|
-
throw Error(`Invalid shareBps=${opts.shareBps}, must be an integer within [0, 10000]`);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (!Number.isSafeInteger(opts.fixedCostGwei) || opts.fixedCostGwei < 0) {
|
|
35
|
-
throw Error(`Invalid fixedCostGwei=${opts.fixedCostGwei}, must be a non-negative safe integer`);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (!Number.isSafeInteger(opts.minValueGwei) || opts.minValueGwei < 0) {
|
|
39
|
-
throw Error(`Invalid minValueGwei=${opts.minValueGwei}, must be a non-negative safe integer`);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (
|
|
43
|
-
opts.maxValueGwei !== undefined &&
|
|
44
|
-
(!Number.isSafeInteger(opts.maxValueGwei) || opts.maxValueGwei < opts.minValueGwei)
|
|
45
|
-
) {
|
|
46
|
-
throw Error(
|
|
47
|
-
`Invalid maxValueGwei=${opts.maxValueGwei}, must be a safe integer greater than or equal to minValueGwei=${opts.minValueGwei}`
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
computeValue({payloadValueGwei, coverableGwei}: BidContext): number | null {
|
|
53
|
-
const proportionalValue = Number((BigInt(payloadValueGwei) * BigInt(this.opts.shareBps)) / 10_000n);
|
|
54
|
-
const share = proportionalValue - this.opts.fixedCostGwei;
|
|
55
|
-
// This will override `fixedCostGwei` for the sake of fulfilling `minValueGwei`
|
|
56
|
-
let value = Math.max(this.opts.minValueGwei, share);
|
|
57
|
-
if (this.opts.maxValueGwei !== undefined) {
|
|
58
|
-
value = Math.min(value, this.opts.maxValueGwei);
|
|
59
|
-
}
|
|
60
|
-
if (value > coverableGwei) {
|
|
61
|
-
return null;
|
|
62
|
-
}
|
|
63
|
-
return value;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
@@ -1,231 +0,0 @@
|
|
|
1
|
-
import {ApiClient, ApiError, routes} from "@lodestar/api";
|
|
2
|
-
import {ChainForkConfig} from "@lodestar/config";
|
|
3
|
-
import {ForkPostGloas, isForkPostGloas} from "@lodestar/params";
|
|
4
|
-
import {RootHex, SignedBeaconBlock, Slot, isGloasBeaconBlock} from "@lodestar/types";
|
|
5
|
-
import {Logger, TimeoutError, isErrorAborted, isFetchError, pruneSetToMax, retry, toRootHex} from "@lodestar/utils";
|
|
6
|
-
|
|
7
|
-
const {EventType} = routes.events;
|
|
8
|
-
|
|
9
|
-
type BlockEvent = routes.events.EventData[typeof EventType.block];
|
|
10
|
-
|
|
11
|
-
type BlockObserverOptions = {
|
|
12
|
-
retries?: number;
|
|
13
|
-
retryDelay?: number;
|
|
14
|
-
maxSeenBlockRoots?: number;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export type ObservedBlock = {
|
|
18
|
-
blockRoot: RootHex;
|
|
19
|
-
slot: Slot;
|
|
20
|
-
executionOptimistic: boolean;
|
|
21
|
-
version: ForkPostGloas;
|
|
22
|
-
block: SignedBeaconBlock<ForkPostGloas>;
|
|
23
|
-
signedBid: SignedBeaconBlock<ForkPostGloas>["message"]["body"]["signedExecutionPayloadBid"];
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
type RunOnBlockFn = (block: ObservedBlock) => Promise<void>;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Observes imported beacon blocks through the source beacon node API.
|
|
30
|
-
*
|
|
31
|
-
* Each block root is evaluated at most once while retained in the bounded seen set. Terminal failures remain consumed
|
|
32
|
-
* until normal FIFO eviction; reconnect, replay, and recovery policies are handled separately.
|
|
33
|
-
*/
|
|
34
|
-
export class BlockObserver {
|
|
35
|
-
private readonly fns: RunOnBlockFn[] = [];
|
|
36
|
-
private readonly seenBlockRoots = new Set<RootHex>();
|
|
37
|
-
private readonly retries: number;
|
|
38
|
-
private readonly retryDelay: number;
|
|
39
|
-
private readonly maxSeenBlockRoots: number;
|
|
40
|
-
|
|
41
|
-
constructor(
|
|
42
|
-
private readonly config: ChainForkConfig,
|
|
43
|
-
private readonly logger: Logger,
|
|
44
|
-
private readonly api: ApiClient,
|
|
45
|
-
{retries = 5, retryDelay = 200, maxSeenBlockRoots = 256}: BlockObserverOptions = {}
|
|
46
|
-
) {
|
|
47
|
-
this.retries = retries;
|
|
48
|
-
this.retryDelay = retryDelay;
|
|
49
|
-
this.maxSeenBlockRoots = maxSeenBlockRoots;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Register a block consumer. Consumers that require complete observation must register before {@link start}, because
|
|
54
|
-
* roots observed before registration remain consumed until normal FIFO eviction.
|
|
55
|
-
*/
|
|
56
|
-
runOnBlock(fn: RunOnBlockFn): void {
|
|
57
|
-
this.fns.push(fn);
|
|
58
|
-
}
|
|
59
|
-
|
|
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
|
-
async processBlockEvent(event: BlockEvent, signal: AbortSignal): Promise<void> {
|
|
100
|
-
try {
|
|
101
|
-
const {slot, block: blockRoot, executionOptimistic} = event;
|
|
102
|
-
|
|
103
|
-
if (this.seenBlockRoots.has(blockRoot)) {
|
|
104
|
-
this.logger.debug("Ignoring duplicate block event", {slot, blockRoot});
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
this.seenBlockRoots.add(blockRoot);
|
|
109
|
-
pruneSetToMax(this.seenBlockRoots, this.maxSeenBlockRoots);
|
|
110
|
-
|
|
111
|
-
if (!isForkPostGloas(this.config.getForkName(slot))) {
|
|
112
|
-
this.logger.debug("Ignoring pre-Gloas block event", {slot, blockRoot});
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const response = await retry(
|
|
117
|
-
async () => {
|
|
118
|
-
// TODO GLOAS: Remove this lookup once bid-selection notifications provide the required data.
|
|
119
|
-
const result = await this.api.beacon.getBlockV2({blockId: blockRoot}, {signal});
|
|
120
|
-
result.assertOk();
|
|
121
|
-
return result;
|
|
122
|
-
},
|
|
123
|
-
{
|
|
124
|
-
retries: this.retries,
|
|
125
|
-
retryDelay: this.retryDelay,
|
|
126
|
-
signal,
|
|
127
|
-
shouldRetry: isRetryableBlockRetrievalError,
|
|
128
|
-
onRetry: (error, attempt) => {
|
|
129
|
-
this.logger.debug("Retrying block retrieval", {
|
|
130
|
-
slot,
|
|
131
|
-
blockRoot,
|
|
132
|
-
attempt,
|
|
133
|
-
error: error.message,
|
|
134
|
-
});
|
|
135
|
-
},
|
|
136
|
-
}
|
|
137
|
-
).catch((error: unknown) => {
|
|
138
|
-
if (!isErrorAborted(error)) {
|
|
139
|
-
this.logger.error(
|
|
140
|
-
"Failed to retrieve block referenced by block event",
|
|
141
|
-
{slot, blockRoot},
|
|
142
|
-
error instanceof Error ? error : Error(String(error))
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
return null;
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
if (response === null) {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const {version} = response.meta();
|
|
153
|
-
if (!isForkPostGloas(version)) {
|
|
154
|
-
this.logger.warn("Ignoring block with unsupported fork version", {slot, blockRoot, fork: version});
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const block = response.value();
|
|
159
|
-
if (!isGloasBeaconBlock(block.message)) {
|
|
160
|
-
this.logger.warn("Block response version and body do not agree", {slot, blockRoot, fork: version});
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (block.message.slot !== slot) {
|
|
165
|
-
this.logger.warn("Block response slot does not match block event", {
|
|
166
|
-
slot,
|
|
167
|
-
blockRoot,
|
|
168
|
-
blockSlot: block.message.slot,
|
|
169
|
-
});
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const postGloasBlock = block as SignedBeaconBlock<ForkPostGloas>;
|
|
174
|
-
const signedBid = postGloasBlock.message.body.signedExecutionPayloadBid;
|
|
175
|
-
const observedBlock: ObservedBlock = {
|
|
176
|
-
blockRoot,
|
|
177
|
-
slot,
|
|
178
|
-
executionOptimistic,
|
|
179
|
-
version,
|
|
180
|
-
block: postGloasBlock,
|
|
181
|
-
signedBid,
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
this.logger.debug("Observed post-Gloas block", {
|
|
185
|
-
slot,
|
|
186
|
-
blockRoot,
|
|
187
|
-
fork: version,
|
|
188
|
-
builderIndex: signedBid.message.builderIndex,
|
|
189
|
-
value: signedBid.message.value,
|
|
190
|
-
blockHash: toRootHex(signedBid.message.blockHash),
|
|
191
|
-
parentBlockHash: toRootHex(signedBid.message.parentBlockHash),
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
await Promise.all(
|
|
195
|
-
this.fns.map(async (fn) => {
|
|
196
|
-
try {
|
|
197
|
-
await fn(observedBlock);
|
|
198
|
-
} catch (error) {
|
|
199
|
-
if (isErrorAborted(error)) {
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
this.logger.error(
|
|
204
|
-
"Failed to process observed block",
|
|
205
|
-
{slot, blockRoot},
|
|
206
|
-
error instanceof Error ? error : Error(String(error))
|
|
207
|
-
);
|
|
208
|
-
}
|
|
209
|
-
})
|
|
210
|
-
);
|
|
211
|
-
} catch (error) {
|
|
212
|
-
if (isErrorAborted(error)) {
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
this.logger.error(
|
|
217
|
-
"Failed to process block event",
|
|
218
|
-
{slot: event.slot, blockRoot: event.block},
|
|
219
|
-
error instanceof Error ? error : Error(String(error))
|
|
220
|
-
);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
export function isRetryableBlockRetrievalError(error: Error): boolean {
|
|
226
|
-
if (error instanceof ApiError) {
|
|
227
|
-
return error.status === 404 || error.status >= 500;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return error instanceof TimeoutError || (isFetchError(error) && error.type !== "input");
|
|
231
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import {ForkPostGloas} from "@lodestar/params";
|
|
2
|
-
import {BlobsBundle, ExecutionPayload, Root, RootHex, Slot, gloas} from "@lodestar/types";
|
|
3
|
-
|
|
4
|
-
// NOTE: BuiltPayload will migrate to payloadSource.ts when the payload source lands; the store
|
|
5
|
-
// holds it here for now.
|
|
6
|
-
export type BuiltPayload = {
|
|
7
|
-
sourceId: string;
|
|
8
|
-
executionPayload: ExecutionPayload<ForkPostGloas>;
|
|
9
|
-
executionRequests: gloas.ExecutionRequests;
|
|
10
|
-
blobsBundle: BlobsBundle<ForkPostGloas>;
|
|
11
|
-
/** Value of the payload to the fee recipient in wei, as reported by the execution client */
|
|
12
|
-
executionPayloadValue: bigint;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export type StoredPayload = {
|
|
16
|
-
slot: Slot;
|
|
17
|
-
parentBlockRoot: Root;
|
|
18
|
-
blockHash: RootHex;
|
|
19
|
-
payload: BuiltPayload;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/** Keep payloads for this many slots after their target slot, late blocks may still commit to them */
|
|
23
|
-
const KEEP_SLOTS = 2;
|
|
24
|
-
|
|
25
|
-
export class PayloadStore {
|
|
26
|
-
private readonly byBlockHash = new Map<RootHex, StoredPayload>();
|
|
27
|
-
|
|
28
|
-
add(payload: StoredPayload): void {
|
|
29
|
-
this.byBlockHash.set(payload.blockHash, payload);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
get(blockHash: RootHex): StoredPayload | null {
|
|
33
|
-
return this.byBlockHash.get(blockHash) ?? null;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
has(blockHash: RootHex): boolean {
|
|
37
|
-
return this.byBlockHash.has(blockHash);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
prune(currentSlot: Slot): void {
|
|
41
|
-
for (const [blockHash, {slot}] of this.byBlockHash) {
|
|
42
|
-
if (slot + KEEP_SLOTS < currentSlot) {
|
|
43
|
-
this.byBlockHash.delete(blockHash);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
get size(): number {
|
|
49
|
-
return this.byBlockHash.size;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import {ApiClient, routes} from "@lodestar/api";
|
|
2
|
-
import type {RootHex, Slot, gloas} from "@lodestar/types";
|
|
3
|
-
import {Logger, MapDef, toRootHex} from "@lodestar/utils";
|
|
4
|
-
|
|
5
|
-
/** Retains validated proposer preferences by the branch-specific identity used for bid validation. */
|
|
6
|
-
export class ProposerPreferencesTracker {
|
|
7
|
-
private readonly byDependentRootBySlot = new MapDef<Slot, Map<RootHex, gloas.SignedProposerPreferences>>(
|
|
8
|
-
() => new Map()
|
|
9
|
-
);
|
|
10
|
-
|
|
11
|
-
constructor(
|
|
12
|
-
private readonly api: ApiClient,
|
|
13
|
-
private readonly logger: Logger
|
|
14
|
-
) {}
|
|
15
|
-
|
|
16
|
-
start(signal: AbortSignal): void {
|
|
17
|
-
if (signal.aborted) return;
|
|
18
|
-
|
|
19
|
-
this.logger.verbose("Subscribing to proposer preferences");
|
|
20
|
-
this.api.events
|
|
21
|
-
.eventstream({
|
|
22
|
-
topics: [routes.events.EventType.proposerPreferences],
|
|
23
|
-
signal,
|
|
24
|
-
onEvent: (event) => {
|
|
25
|
-
if (!signal.aborted && event.type === routes.events.EventType.proposerPreferences) {
|
|
26
|
-
this.onProposerPreferences(event.message.data);
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
onError: (error) => {
|
|
30
|
-
if (!signal.aborted) this.logger.error("Failed to receive proposer preferences", {}, error);
|
|
31
|
-
},
|
|
32
|
-
onClose: () => {
|
|
33
|
-
if (signal.aborted) {
|
|
34
|
-
this.logger.verbose("Closed proposer preferences stream");
|
|
35
|
-
} else {
|
|
36
|
-
this.logger.error("Proposer preferences stream closed unexpectedly", {});
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
})
|
|
40
|
-
.catch((error: Error) => {
|
|
41
|
-
if (!signal.aborted) this.logger.error("Failed to subscribe to proposer preferences", {}, error);
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
onProposerPreferences(signedProposerPreferences: gloas.SignedProposerPreferences): boolean {
|
|
46
|
-
const {proposalSlot, dependentRoot} = signedProposerPreferences.message;
|
|
47
|
-
const dependentRootHex = toRootHex(dependentRoot);
|
|
48
|
-
const byDependentRoot = this.byDependentRootBySlot.getOrDefault(proposalSlot);
|
|
49
|
-
|
|
50
|
-
if (byDependentRoot.has(dependentRootHex)) {
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
byDependentRoot.set(dependentRootHex, signedProposerPreferences);
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
get(slot: Slot, dependentRoot: RootHex): gloas.SignedProposerPreferences | null {
|
|
59
|
-
return this.byDependentRootBySlot.get(slot)?.get(dependentRoot) ?? null;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
prune(currentSlot: Slot): number {
|
|
63
|
-
let removed = 0;
|
|
64
|
-
for (const [slot, byDependentRoot] of this.byDependentRootBySlot) {
|
|
65
|
-
if (slot >= currentSlot) {
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
removed += byDependentRoot.size;
|
|
70
|
-
this.byDependentRootBySlot.delete(slot);
|
|
71
|
-
}
|
|
72
|
-
return removed;
|
|
73
|
-
}
|
|
74
|
-
}
|