Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

babe: make secondary slot randomness available on-chain #7053

Merged
17 commits merged into from
Oct 15, 2020

Conversation

octol
Copy link
Contributor

@octol octol commented Sep 8, 2020

Make secondary slot randomness available on-chain: #7046

TODO

  • Make sure that the implementation satisfies the requirements in the issue
  • Tests

@octol octol added A3-in_progress Pull request is in progress. No review needed at this stage. B3-apinoteworthy C1-low PR touches the given topic and has a low impact on builders. labels Sep 8, 2020
@octol octol marked this pull request as ready for review September 14, 2020 11:12
@github-actions github-actions bot added A0-please_review Pull request needs code review. and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Sep 14, 2020
Initialized::put(maybe_randomness);
let maybe_primary_randomness = maybe_randomness.filter(|_| is_primary);

Initialized::put(maybe_primary_randomness);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would find this cleaner to read just as a single if is_primary { Initializer:put(maybe_randomness) }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is not exactly the same as what the existing version does, which puts a None when there is no randomness

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could do

Initialized::put(if is_primary { maybe_randomness } else { None });

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The base value of Initialized is None, so it logically doesn't make a difference. But yeah, the second option is more exact

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried the suggested approach of if is_primary { Initialized::put(maybe_randomness) } but got assertion failures here

debug_assert!(Self::initialized().is_some());
in a lot of the frame-babe tests

digest
.vrf_output()
.and_then(|vrf_output| {
// place the VRF output into the `Initialized` storage item
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is now outdated. Seems like it should move to below.

Copy link
Contributor

@rphmeier rphmeier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks fine except for nits. thanks!

@rphmeier rphmeier added A6-mustntgrumble and removed A0-please_review Pull request needs code review. labels Sep 15, 2020
Copy link
Contributor

@andresilva andresilva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. just minor nits.

// once we've decided which epoch this block is in.
Initialized::put(if is_primary { maybe_randomness } else { None });

// Place the both primary and secondary VRF output into the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Place the both primary and secondary VRF output into the
// Place either the primary or secondary VRF output into the

Comment on lines 206 to 207
/// Temporary value (cleared at block finalization) which is `Some` if we
/// have generated VRF randomness.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Temporary value (cleared at block finalization) which is `Some` if we
/// have generated VRF randomness.
/// Temporary value (cleared at block finalization) that includes the VRF randomness generated at this block.
/// This field should always be populated during block processing unless secondary plain slots are enabled
/// (which don't contain a VRF proof).

.map(|inout| {
inout.make_bytes(&sp_consensus_babe::BABE_VRF_INOUT_CONTEXT)
})
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
})
})

@@ -203,6 +203,10 @@ decl_storage! {
/// if per-block initialization has already been called for current block.
Initialized get(fn initialized): Option<MaybeRandomness>;

/// Temporary value (cleared at block finalization) which is `Some` if we
/// have generated VRF randomness.
AuthorVrfRandomness get(fn author_vrf_randomness): Option<MaybeRandomness>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that this is not related to this PR, but we're essentially declaring this as Option<Option<Randomness>> (same for Initialized). Do we really need the double option wrapping or was it just an oversight? Both keys are supposed to be cleared before block execution finishes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well for Initialized the outer Option seems to have a specific meaning? See for example

debug_assert!(Self::initialized().is_some());

However for AuthorVrfRandomness yes probably the outer Option can be removed

Copy link
Contributor

@andresilva andresilva Sep 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah right the semantics for Initialized require both options since the outer signals that we have done the initialization procedure (regardless of whether there was a primary VRF to extract or not). For AuthorVrfRandomness I think we could do with removing it and just use MaybeRandomness.

@@ -126,6 +128,99 @@ fn first_block_epoch_zero_start() {
})
}

fn make_vrf_output(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ocd: maybe move to mock.rs next to the other utility functions?

(vrf_output, vrf_proof, vrf_randomness)
}

#[test]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the duplication in the tests below could be factored out, but won't block merge on this.

@andresilva andresilva added the D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited label Sep 18, 2020
Copy link
Contributor

@andresilva andresilva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made some final changes for minor nits. lgtm 👍

@andresilva
Copy link
Contributor

bot merge

@ghost
Copy link

ghost commented Sep 18, 2020

Waiting for commit status.

@andresilva
Copy link
Contributor

bot merge cancel

@ghost
Copy link

ghost commented Sep 18, 2020

Merge cancelled.

@andresilva
Copy link
Contributor

The PR has been reviewed and looks good but should not be merged until it is audited.

@andresilva andresilva added D1-audited 👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited and removed D9-needsaudit 👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited labels Oct 14, 2020
@andresilva
Copy link
Contributor

@octol The audit is done and no issues were found. Can you merge master to fix the conflicts (I believe it's only about the increased spec_version)?

@andresilva
Copy link
Contributor

bot merge

@ghost
Copy link

ghost commented Oct 15, 2020

Trying merge.

@ghost ghost merged commit d86a98c into master Oct 15, 2020
@ghost ghost deleted the jon/make-secondary-slot-randomness-available branch October 15, 2020 08:32
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C1-low PR touches the given topic and has a low impact on builders. D1-audited 👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants