core: better block production and usage in fixture - #218
Merged
Conversation
Collaborator
Author
|
@varun-doshi What do you think about this? We can rework this again in follow up PRs to cleanup the forkchoice store a bit but I think it solves the problems you had in #213 no? |
KolbyML
reviewed
Dec 4, 2025
varun-doshi
reviewed
Dec 4, 2025
Contributor
There was a problem hiding this comment.
what if we reverse the duties of the 2 functions to something like this:
def build_block(
self,
slot: Slot,
proposer_index: ValidatorIndex,
parent_root: Bytes32,
attestations: list[Attestation] | None = None,
) -> tuple[Block, "State", list[Attestation], list[Signature]]:
attestations = list(attestations or [])
signatures: list[Signature] = []
head_state = self
while True:
temp_block = Block(
slot=slot,
proposer_index=proposer_index,
parent_root=parent_root,
state_root=Bytes32.zero(),
body=BlockBody(attestations=Attestations(data=attestations)),
)
post_state = head_state.process_slots(slot).process_block(temp_block)
new_attestations = []
new_signatures = []
for target in self.available_attestations:
data = target.data
if data.head.root not in self.blocks:
continue
if data.source != post_state.latest_justified:
continue
if data in (a.data for a in attestations):
continue
new_attestations.append(target)
new_signatures.append(target.signature)
if not new_attestations:
break
attestations.extend(new_attestations)
signatures.extend(new_signatures)
final_state_root = hash_tree_root(post_state)
final_block = Block(
slot=slot,
proposer_index=proposer_index,
parent_root=parent_root,
state_root=final_state_root,
body=BlockBody(attestations=Attestations(data=attestations)),
)
return final_block, post_state, attestations, signaturesand
def produce_block_with_signatures(
self,
slot: Slot,
validator_index: ValidatorIndex,
) -> tuple["Store", Block, list[Signature]]:
store = self
head_root = store.head
head_state = store.states[head_root]
final_block, final_post_state, _, signatures = head_state.build_block(
slot=slot,
proposer_index=validator_index,
parent_root=head_root,
)
block_hash = hash_tree_root(final_block)
store = store.model_copy(
update={
"blocks": {**store.blocks, block_hash: final_block},
"states": {**store.states, block_hash: final_post_state},
}
)
return store, final_block, signaturesthis way we are separating block production/storage and also build_block would be usable separately in test fixtures. We are also not doing the extra state computation. The naming of functions can be changed if needed
3 tasks
Collaborator
Author
|
@varun-doshi should be solved, let me know what you think |
varun-doshi
approved these changes
Dec 4, 2025
varun-doshi
left a comment
Contributor
There was a problem hiding this comment.
looks very neat now
lgtm
Contributor
|
Ah yeah this is much nicer 👌🏼 |
3 tasks
3 tasks
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🗒️ Description
Now we can use the same block building function in the test fixture that we are using in the spec, making the logic more consistent and more concise.
🔗 Related Issues or PRs
Should close #167
✅ Checklist
toxchecks to avoid unnecessary CI fails:uvx tox