@lodestar/builder 1.50.0-dev.b4eaa62b30 → 1.50.0-dev.be50566b7e

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.
Files changed (40) hide show
  1. package/lib/builder.d.ts +2 -0
  2. package/lib/builder.d.ts.map +1 -1
  3. package/lib/builder.js +56 -4
  4. package/lib/builder.js.map +1 -1
  5. package/lib/services/bidLedger.d.ts +7 -0
  6. package/lib/services/bidLedger.d.ts.map +1 -1
  7. package/lib/services/bidLedger.js +15 -10
  8. package/lib/services/bidLedger.js.map +1 -1
  9. package/lib/services/bidPublisher.d.ts +43 -0
  10. package/lib/services/bidPublisher.d.ts.map +1 -0
  11. package/lib/services/bidPublisher.js +50 -0
  12. package/lib/services/bidPublisher.js.map +1 -0
  13. package/lib/services/bidSelector.d.ts +67 -0
  14. package/lib/services/bidSelector.d.ts.map +1 -0
  15. package/lib/services/bidSelector.js +59 -0
  16. package/lib/services/bidSelector.js.map +1 -0
  17. package/lib/services/blockObserver.d.ts +1 -2
  18. package/lib/services/blockObserver.d.ts.map +1 -1
  19. package/lib/services/blockObserver.js +3 -35
  20. package/lib/services/blockObserver.js.map +1 -1
  21. package/lib/services/builderSigner.d.ts +2 -1
  22. package/lib/services/builderSigner.d.ts.map +1 -1
  23. package/lib/services/builderSigner.js.map +1 -1
  24. package/lib/services/executionPayloadBid.d.ts +57 -0
  25. package/lib/services/executionPayloadBid.d.ts.map +1 -0
  26. package/lib/services/executionPayloadBid.js +69 -0
  27. package/lib/services/executionPayloadBid.js.map +1 -0
  28. package/lib/services/proposerPreferencesTracker.d.ts +0 -6
  29. package/lib/services/proposerPreferencesTracker.d.ts.map +1 -1
  30. package/lib/services/proposerPreferencesTracker.js +0 -38
  31. package/lib/services/proposerPreferencesTracker.js.map +1 -1
  32. package/package.json +10 -10
  33. package/src/builder.ts +63 -5
  34. package/src/services/bidLedger.ts +28 -14
  35. package/src/services/bidPublisher.ts +95 -0
  36. package/src/services/bidSelector.ts +138 -0
  37. package/src/services/blockObserver.ts +3 -41
  38. package/src/services/builderSigner.ts +3 -1
  39. package/src/services/executionPayloadBid.ts +125 -0
  40. package/src/services/proposerPreferencesTracker.ts +1 -36
@@ -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 {@link start}, because
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,125 @@
1
+ import {ForkName, type ForkPostGloas, INCLUSION_LIST_COMMITTEE_SIZE, isForkPostHeze} from "@lodestar/params";
2
+ import type {
3
+ BuilderIndex,
4
+ Bytes32,
5
+ ExecutionAddress,
6
+ ExecutionPayloadBid,
7
+ Root,
8
+ RootHex,
9
+ Slot,
10
+ gloas,
11
+ heze,
12
+ } from "@lodestar/types";
13
+ import {sszTypesFor} from "@lodestar/types";
14
+ import {LodestarError, byteArrayEquals, toRootHex} from "@lodestar/utils";
15
+ import type {BuiltPayload} from "./payloadSource.js";
16
+
17
+ type CommonBidInput<F extends ForkPostGloas> = {
18
+ slot: Slot;
19
+ parentBlockRoot: Root;
20
+ /** Expected value from the BN payload attributes used for this build. */
21
+ prevRandao: Bytes32;
22
+ builderIndex: BuilderIndex;
23
+ feeRecipient: ExecutionAddress;
24
+ /** Bid value in gwei */
25
+ value: number;
26
+ payload: BuiltPayload & {fork: F};
27
+ };
28
+
29
+ export type GloasBidInput = CommonBidInput<ForkName.gloas>;
30
+
31
+ export type HezeBidInput = CommonBidInput<ForkName.heze> & {
32
+ inclusionListBits: heze.ExecutionPayloadBid["inclusionListBits"];
33
+ };
34
+
35
+ export type ExecutionPayloadBidInput = GloasBidInput | HezeBidInput;
36
+
37
+ export enum ExecutionPayloadBidErrorCode {
38
+ INVALID_VALUE = "EXECUTION_PAYLOAD_BID_ERROR_INVALID_VALUE",
39
+ INVALID_GAS_LIMIT = "EXECUTION_PAYLOAD_BID_ERROR_INVALID_GAS_LIMIT",
40
+ SLOT_MISMATCH = "EXECUTION_PAYLOAD_BID_ERROR_SLOT_MISMATCH",
41
+ BLOCK_HASH_EQUALS_PARENT = "EXECUTION_PAYLOAD_BID_ERROR_BLOCK_HASH_EQUALS_PARENT",
42
+ INVALID_INCLUSION_LIST_BITS = "EXECUTION_PAYLOAD_BID_ERROR_INVALID_INCLUSION_LIST_BITS",
43
+ PREV_RANDAO_MISMATCH = "EXECUTION_PAYLOAD_BID_ERROR_PREV_RANDAO_MISMATCH",
44
+ }
45
+
46
+ export type ExecutionPayloadBidErrorType =
47
+ | {code: ExecutionPayloadBidErrorCode.INVALID_GAS_LIMIT; gasLimit: number}
48
+ | {code: ExecutionPayloadBidErrorCode.SLOT_MISMATCH; slot: Slot; payloadSlot: Slot}
49
+ | {code: ExecutionPayloadBidErrorCode.BLOCK_HASH_EQUALS_PARENT}
50
+ | {code: ExecutionPayloadBidErrorCode.INVALID_INCLUSION_LIST_BITS; bitLen: number | undefined}
51
+ | {
52
+ code: ExecutionPayloadBidErrorCode.PREV_RANDAO_MISMATCH;
53
+ expectedPrevRandao: RootHex;
54
+ payloadPrevRandao: RootHex;
55
+ }
56
+ | {
57
+ code: ExecutionPayloadBidErrorCode.INVALID_VALUE;
58
+ value: number;
59
+ };
60
+
61
+ export class ExecutionPayloadBidError extends LodestarError<ExecutionPayloadBidErrorType> {}
62
+
63
+ export function createExecutionPayloadBid(input: GloasBidInput): gloas.ExecutionPayloadBid;
64
+ export function createExecutionPayloadBid(input: HezeBidInput): heze.ExecutionPayloadBid;
65
+ export function createExecutionPayloadBid(input: ExecutionPayloadBidInput): ExecutionPayloadBid;
66
+ export function createExecutionPayloadBid(input: ExecutionPayloadBidInput): ExecutionPayloadBid {
67
+ if (!Number.isSafeInteger(input.value) || input.value < 0) {
68
+ throw new ExecutionPayloadBidError(
69
+ {code: ExecutionPayloadBidErrorCode.INVALID_VALUE, value: input.value},
70
+ `Invalid bid value value=${input.value}`
71
+ );
72
+ }
73
+
74
+ const {fork, executionPayload, executionRequests, blobsBundle} = input.payload;
75
+ if (executionPayload.gasLimit > Number.MAX_SAFE_INTEGER) {
76
+ throw new ExecutionPayloadBidError({
77
+ code: ExecutionPayloadBidErrorCode.INVALID_GAS_LIMIT,
78
+ gasLimit: executionPayload.gasLimit,
79
+ });
80
+ }
81
+ if (executionPayload.slotNumber !== input.slot) {
82
+ throw new ExecutionPayloadBidError({
83
+ code: ExecutionPayloadBidErrorCode.SLOT_MISMATCH,
84
+ slot: input.slot,
85
+ payloadSlot: executionPayload.slotNumber,
86
+ });
87
+ }
88
+ if (byteArrayEquals(executionPayload.blockHash, executionPayload.parentHash)) {
89
+ throw new ExecutionPayloadBidError({code: ExecutionPayloadBidErrorCode.BLOCK_HASH_EQUALS_PARENT});
90
+ }
91
+ if (!byteArrayEquals(executionPayload.prevRandao, input.prevRandao)) {
92
+ throw new ExecutionPayloadBidError({
93
+ code: ExecutionPayloadBidErrorCode.PREV_RANDAO_MISMATCH,
94
+ expectedPrevRandao: toRootHex(input.prevRandao),
95
+ payloadPrevRandao: toRootHex(executionPayload.prevRandao),
96
+ });
97
+ }
98
+ const bid: gloas.ExecutionPayloadBid = {
99
+ parentBlockHash: executionPayload.parentHash,
100
+ parentBlockRoot: input.parentBlockRoot,
101
+ blockHash: executionPayload.blockHash,
102
+ prevRandao: input.prevRandao,
103
+ feeRecipient: input.feeRecipient,
104
+ gasLimit: BigInt(executionPayload.gasLimit),
105
+ builderIndex: input.builderIndex,
106
+ slot: input.slot,
107
+ value: input.value,
108
+ executionPayment: 0n,
109
+ blobKzgCommitments: blobsBundle.commitments,
110
+ executionRequestsRoot: sszTypesFor(fork).ExecutionRequests.hashTreeRoot(executionRequests),
111
+ };
112
+
113
+ if (isForkPostHeze(fork)) {
114
+ const inclusionListBits = "inclusionListBits" in input ? input.inclusionListBits : undefined;
115
+ if (inclusionListBits?.bitLen !== INCLUSION_LIST_COMMITTEE_SIZE) {
116
+ throw new ExecutionPayloadBidError({
117
+ code: ExecutionPayloadBidErrorCode.INVALID_INCLUSION_LIST_BITS,
118
+ bitLen: inclusionListBits?.bitLen,
119
+ });
120
+ }
121
+ return {...bid, inclusionListBits};
122
+ }
123
+
124
+ return bid;
125
+ }
@@ -1,6 +1,5 @@
1
- import {ApiClient, routes} from "@lodestar/api";
2
1
  import type {RootHex, Slot, gloas} from "@lodestar/types";
3
- import {Logger, MapDef, toRootHex} from "@lodestar/utils";
2
+ import {MapDef, toRootHex} from "@lodestar/utils";
4
3
 
5
4
  /** Retains validated proposer preferences by the branch-specific identity used for bid validation. */
6
5
  export class ProposerPreferencesTracker {
@@ -8,40 +7,6 @@ export class ProposerPreferencesTracker {
8
7
  () => new Map()
9
8
  );
10
9
 
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
10
  onProposerPreferences(signedProposerPreferences: gloas.SignedProposerPreferences): boolean {
46
11
  const {proposalSlot, dependentRoot} = signedProposerPreferences.message;
47
12
  const dependentRootHex = toRootHex(dependentRoot);