Skip to content

Commit

Permalink
get blinded execution payload headers
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec committed Jul 19, 2022
1 parent 7792224 commit f9d05a8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion beacon_chain/spec/mev/bellatrix_mev.nim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type
signature*: ValidatorSig

# https://github.com/ethereum/builder-specs/blob/v0.2.0/specs/builder.md#builderbid
BuilderBid = object
BuilderBid* = object
header*: ExecutionPayloadHeader
value*: UInt256
pubkey*: ValidatorPubKey
Expand Down
9 changes: 7 additions & 2 deletions beacon_chain/spec/signatures.nim
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ proc get_contribution_and_proof_signature*(

blsSign(privkey, signing_root.data)


# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.1/specs/altair/validator.md#aggregation-selection
func is_sync_committee_aggregator*(signature: ValidatorSig): bool =
let
Expand All @@ -338,7 +337,7 @@ from stew/byteutils import fromHex

# https://github.com/ethereum/builder-specs/blob/v0.2.0/specs/builder.md#signing
func compute_builder_signing_root*(
fork: Fork, msg: ValidatorRegistrationV1): Eth2Digest =
fork: Fork, msg: BuilderBid | ValidatorRegistrationV1): Eth2Digest =
# Uses genesis fork version regardless
doAssert fork.current_version == fork.previous_version

Expand All @@ -362,3 +361,9 @@ proc get_builder_signature*(
CookedSig =
let signing_root = compute_builder_signing_root(fork, msg)
blsSign(privkey, signing_root.data)

proc verify_builder_signature*(
fork: Fork, msg: BuilderBid,
pubkey: ValidatorPubKey | CookedPubKey, signature: SomeSig): bool =
let signing_root = compute_builder_signing_root(fork, msg)
blsVerify(pubkey, signing_root.data, signature)
57 changes: 57 additions & 0 deletions beacon_chain/validators/validator_duties.nim
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,59 @@ proc makeBeaconBlockForHeadAndSlot*(node: BeaconNode,
head = shortLog(head),
slot

proc getBlindedExecutionPayload(
node: BeaconNode, slot: Slot, executionBlockRoot: Eth2Digest,
pubkey: ValidatorPubKey):
Future[Result[ExecutionPayloadHeader, cstring]] {.async.} =
doAssert not node.restClient.isNil

let blindedHeader = await node.restClient.getHeader(
slot, executionBlockRoot, pubkey)

const httpOk = 200
if blindedHeader.status != httpOk:
return err("getBlindedExecutionPayload: non-200 HTTP response")
else:
#info "getBlindedExecutionPayload: succeeded",
# slot, pubkey = shortLog(pubkey), execution_root = executionBlockRoot
if blindedHeader.data.data.message.pubkey != pubkey:
return err("getBlindedExecutionPayload: incorrect pubkey")

if not verify_builder_signature(
node.dag.cfg.genesisFork, blindedHeader.data.data.message, pubkey,
blindedHeader.data.data.signature):
return err("getBlindedExecutionPayload: signature verification failed")

return ok blindedHeader.data.data.message.header

proc proposeBlockMEV(
node: BeaconNode, slot: Slot, head: BlockRef, pubkey: ValidatorPubKey)
{.async.} =
try:
if node.restClient.isNil:
debug "proposeBlockMEV: nil REST client",
slot, pubkey, head = shortLog(head)
return

let
executionBlockRoot = node.dag.loadExecutionBlockRoot(head)
executionPayloadHeader =
await node.getBlindedExecutionPayload(slot, executionBlockRoot, pubkey)

if executionPayloadHeader.isErr:
info "proposeBlockMEV: failed to get blinded execution payload header",
head = shortLog(head),
slot, pubkey, executionBlockRoot
return

debug "proposeBlockMEV: got blinded execution payload header",
slot, pubkey, executionBlockRoot,
head = shortLog(head),
payloadHeader = executionPayloadHeader.get
except CatchableError as exc:
debug "proposeBlockMEV: error",
slot, error = exc.msg

proc proposeBlock(node: BeaconNode,
validator: AttachedValidator,
validator_index: ValidatorIndex,
Expand Down Expand Up @@ -515,6 +568,10 @@ proc proposeBlock(node: BeaconNode,

let forkedBlck = newBlock.get()

# Currently, it doesn't fully propose a block, so continue with control flow
if node.config.payloadBuilder.isSome:
asyncSpawn node.proposeBlockMEV(slot, head, validator.pubkey)

withBlck(forkedBlck):
let
blockRoot = hash_tree_root(blck)
Expand Down

0 comments on commit f9d05a8

Please sign in to comment.