-
Notifications
You must be signed in to change notification settings - Fork 2.6k
babe: make secondary slot randomness available on-chain #7053
Conversation
frame/babe/src/lib.rs
Outdated
Initialized::put(maybe_randomness); | ||
let maybe_primary_randomness = maybe_randomness.filter(|_| is_primary); | ||
|
||
Initialized::put(maybe_primary_randomness); |
There was a problem hiding this comment.
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) }
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 });
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
substrate/frame/babe/src/lib.rs
Line 464 in e65a60c
debug_assert!(Self::initialized().is_some()); |
frame/babe/src/lib.rs
Outdated
digest | ||
.vrf_output() | ||
.and_then(|vrf_output| { | ||
// place the VRF output into the `Initialized` storage item |
There was a problem hiding this comment.
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.
There was a problem hiding this 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!
There was a problem hiding this 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.
frame/babe/src/lib.rs
Outdated
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Place the both primary and secondary VRF output into the | |
// Place either the primary or secondary VRF output into the |
frame/babe/src/lib.rs
Outdated
/// Temporary value (cleared at block finalization) which is `Some` if we | ||
/// have generated VRF randomness. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// 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). |
frame/babe/src/lib.rs
Outdated
.map(|inout| { | ||
inout.make_bytes(&sp_consensus_babe::BABE_VRF_INOUT_CONTEXT) | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
}) | |
}) |
frame/babe/src/lib.rs
Outdated
@@ -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>; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
substrate/frame/babe/src/lib.rs
Line 464 in e65a60c
debug_assert!(Self::initialized().is_some()); |
However for AuthorVrfRandomness
yes probably the outer Option
can be removed
There was a problem hiding this comment.
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
.
frame/babe/src/tests.rs
Outdated
@@ -126,6 +128,99 @@ fn first_block_epoch_zero_start() { | |||
}) | |||
} | |||
|
|||
fn make_vrf_output( |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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.
There was a problem hiding this 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 👍
bot merge |
Waiting for commit status. |
bot merge cancel |
Merge cancelled. |
The PR has been reviewed and looks good but should not be merged until it is audited. |
@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 |
Conflicts: bin/node/runtime/src/lib.rs
bot merge |
Trying merge. |
Make secondary slot randomness available on-chain: #7046
TODO