-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathexecutionPayloadBid.ts
More file actions
141 lines (126 loc) · 5.51 KB
/
executionPayloadBid.ts
File metadata and controls
141 lines (126 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import {PublicKey} from "@chainsafe/lodestar-z/blst";
import {
CachedBeaconStateGloas,
canBuilderCoverBid,
createSingleSignatureSetFromComponents,
getExecutionPayloadBidSigningRoot,
isActiveBuilder,
} from "@lodestar/state-transition";
import {gloas} from "@lodestar/types";
import {toRootHex} from "@lodestar/utils";
import {ExecutionPayloadBidError, ExecutionPayloadBidErrorCode, GossipAction} from "../errors/index.js";
import {IBeaconChain} from "../index.js";
import {RegenCaller} from "../regen/index.js";
export async function validateApiExecutionPayloadBid(
chain: IBeaconChain,
signedExecutionPayloadBid: gloas.SignedExecutionPayloadBid
): Promise<void> {
return validateExecutionPayloadBid(chain, signedExecutionPayloadBid);
}
export async function validateGossipExecutionPayloadBid(
chain: IBeaconChain,
signedExecutionPayloadBid: gloas.SignedExecutionPayloadBid
): Promise<void> {
return validateExecutionPayloadBid(chain, signedExecutionPayloadBid);
}
async function validateExecutionPayloadBid(
chain: IBeaconChain,
signedExecutionPayloadBid: gloas.SignedExecutionPayloadBid
): Promise<void> {
const bid = signedExecutionPayloadBid.message;
const parentBlockRootHex = toRootHex(bid.parentBlockRoot);
const parentBlockHashHex = toRootHex(bid.parentBlockHash);
const state = (await chain.getHeadStateAtCurrentEpoch(
RegenCaller.validateGossipExecutionPayloadBid
)) as CachedBeaconStateGloas;
// [IGNORE] `bid.slot` is the current slot or the next slot.
const currentSlot = chain.clock.currentSlot;
if (bid.slot !== currentSlot && bid.slot !== currentSlot + 1) {
throw new ExecutionPayloadBidError(GossipAction.IGNORE, {
code: ExecutionPayloadBidErrorCode.INVALID_SLOT,
builderIndex: bid.builderIndex,
slot: bid.slot,
});
}
// [IGNORE] the `SignedProposerPreferences` where `preferences.proposal_slot`
// is equal to `bid.slot` has been seen.
// TODO GLOAS: Implement this along with proposer preference
// [REJECT] `bid.builder_index` is a valid/active builder index -- i.e.
// `is_active_builder(state, bid.builder_index)` returns `True`.
const builder = state.builders.getReadonly(bid.builderIndex);
if (!isActiveBuilder(builder, state.finalizedCheckpoint.epoch)) {
throw new ExecutionPayloadBidError(GossipAction.REJECT, {
code: ExecutionPayloadBidErrorCode.BUILDER_NOT_ELIGIBLE,
builderIndex: bid.builderIndex,
});
}
// [REJECT] `bid.execution_payment` is zero.
if (bid.executionPayment !== 0) {
throw new ExecutionPayloadBidError(GossipAction.REJECT, {
code: ExecutionPayloadBidErrorCode.NON_ZERO_EXECUTION_PAYMENT,
builderIndex: bid.builderIndex,
executionPayment: bid.executionPayment,
});
}
// [REJECT] `bid.fee_recipient` matches the `fee_recipient` from the proposer's
// `SignedProposerPreferences` associated with `bid.slot`.
// [REJECT] `bid.gas_limit` matches the `gas_limit` from the proposer's
// `SignedProposerPreferences` associated with `bid.slot`.
// TODO GLOAS: Implement this along with proposer preference
// [IGNORE] this is the first signed bid seen with a valid signature from the given builder for this slot.
if (chain.seenExecutionPayloadBids.isKnown(bid.slot, bid.builderIndex)) {
throw new ExecutionPayloadBidError(GossipAction.IGNORE, {
code: ExecutionPayloadBidErrorCode.BID_ALREADY_KNOWN,
builderIndex: bid.builderIndex,
slot: bid.slot,
parentBlockRoot: parentBlockRootHex,
parentBlockHash: parentBlockHashHex,
});
}
// [IGNORE] this bid is the highest value bid seen for the corresponding slot
// and the given parent block hash.
const bestBid = chain.executionPayloadBidPool.getBestBid(parentBlockRootHex, parentBlockHashHex, bid.slot);
if (bestBid !== null && bestBid.value >= bid.value) {
throw new ExecutionPayloadBidError(GossipAction.IGNORE, {
code: ExecutionPayloadBidErrorCode.BID_TOO_LOW,
bidValue: bid.value,
currentHighestBid: bestBid.value,
});
}
// [IGNORE] `bid.value` is less or equal than the builder's excess balance --
// i.e. `can_builder_cover_bid(state, builder_index, amount)` returns `True`.
if (!canBuilderCoverBid(state, bid.builderIndex, bid.value)) {
throw new ExecutionPayloadBidError(GossipAction.IGNORE, {
code: ExecutionPayloadBidErrorCode.BID_TOO_HIGH,
bidValue: bid.value,
builderBalance: builder.balance,
});
}
// [IGNORE] `bid.parent_block_hash` is the block hash of a known execution
// payload in fork choice.
// TODO GLOAS: implement this
// [IGNORE] `bid.parent_block_root` is the hash tree root of a known beacon
// block in fork choice.
const block = chain.forkChoice.getBlock(bid.parentBlockRoot);
if (block === null) {
throw new ExecutionPayloadBidError(GossipAction.IGNORE, {
code: ExecutionPayloadBidErrorCode.UNKNOWN_BLOCK_ROOT,
parentBlockRoot: parentBlockRootHex,
});
}
// [REJECT] `signed_execution_payload_bid.signature` is valid with respect to the `bid.builder_index`.
const signatureSet = createSingleSignatureSetFromComponents(
PublicKey.fromBytes(builder.pubkey),
getExecutionPayloadBidSigningRoot(chain.config, state.slot, bid),
signedExecutionPayloadBid.signature
);
if (!(await chain.bls.verifySignatureSets([signatureSet]))) {
throw new ExecutionPayloadBidError(GossipAction.REJECT, {
code: ExecutionPayloadBidErrorCode.INVALID_SIGNATURE,
builderIndex: bid.builderIndex,
slot: bid.slot,
});
}
// Valid
chain.seenExecutionPayloadBids.add(bid.slot, bid.builderIndex);
}