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
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 48 additions & 30 deletions frame/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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).

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.


/// How late the current block is compared to its parent.
///
/// This entry is populated as part of block execution and is cleaned up
Expand Down Expand Up @@ -248,6 +252,9 @@ decl_module! {
Self::deposit_randomness(&randomness);
}

// The stored author generated VRF randomness is ephemeral.
AuthorVrfRandomness::take();

// remove temporary "environment" entry from storage
Lateness::<T>::kill();
}
Expand Down Expand Up @@ -542,6 +549,8 @@ impl<T: Trait> Module<T> {
})
.next();

let is_primary = matches!(maybe_pre_digest, Some(PreDigest::Primary(..)));

let maybe_randomness: Option<schnorrkel::Randomness> = maybe_pre_digest.and_then(|digest| {
// on the first non-zero block (i.e. block #1)
// this is where the first epoch (epoch #0) actually starts.
Expand Down Expand Up @@ -571,38 +580,47 @@ impl<T: Trait> Module<T> {
Lateness::<T>::put(lateness);
CurrentSlot::put(current_slot);

if let PreDigest::Primary(primary) = digest {
// place the VRF output into the `Initialized` storage item
// and it'll be put onto the under-construction randomness
// later, once we've decided which epoch this block is in.
//
// Reconstruct the bytes of VRFInOut using the authority id.
Authorities::get()
.get(primary.authority_index as usize)
.and_then(|author| {
schnorrkel::PublicKey::from_bytes(author.0.as_slice()).ok()
})
.and_then(|pubkey| {
let transcript = sp_consensus_babe::make_transcript(
&Self::randomness(),
current_slot,
EpochIndex::get(),
);

primary.vrf_output.0.attach_input_hash(
&pubkey,
transcript
).ok()
})
.map(|inout| {
inout.make_bytes(&sp_consensus_babe::BABE_VRF_INOUT_CONTEXT)
})
} else {
None
}
let authority_index = digest.authority_index();

// Extract out the VRF output if we have it
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.

// and it'll be put onto the under-construction randomness
// later, once we've decided which epoch this block is in.
//
// Place the both primary and secondary VRF output into the
// `AuthorVrfRandomness` storage item.
//
// Reconstruct the bytes of VRFInOut using the authority id.
Authorities::get()
.get(authority_index as usize)
.and_then(|author| {
schnorrkel::PublicKey::from_bytes(author.0.as_slice()).ok()
})
.and_then(|pubkey| {
let transcript = sp_consensus_babe::make_transcript(
&Self::randomness(),
current_slot,
EpochIndex::get(),
);

vrf_output.0.attach_input_hash(
&pubkey,
transcript
).ok()
})
.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
})
})

});

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

AuthorVrfRandomness::put(maybe_randomness);

// enact epoch change, if necessary.
T::EpochChangeTrigger::trigger::<T>(now)
Expand Down
18 changes: 18 additions & 0 deletions frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,24 @@ pub fn make_secondary_plain_pre_digest(
Digest { logs: vec![log] }
}

pub fn make_secondary_vrf_pre_digest(
authority_index: sp_consensus_babe::AuthorityIndex,
slot_number: sp_consensus_babe::SlotNumber,
vrf_output: VRFOutput,
vrf_proof: VRFProof,
) -> Digest {
let digest_data = sp_consensus_babe::digests::PreDigest::SecondaryVRF(
sp_consensus_babe::digests::SecondaryVRFPreDigest {
authority_index,
slot_number,
vrf_output,
vrf_proof,
}
);
let log = DigestItem::PreRuntime(sp_consensus_babe::BABE_ENGINE_ID, digest_data.encode());
Digest { logs: vec![log] }
}

pub fn new_test_ext(authorities_len: usize) -> sp_io::TestExternalities {
new_test_ext_with_pairs(authorities_len).1
}
Expand Down
95 changes: 95 additions & 0 deletions frame/babe/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ fn first_block_epoch_zero_start() {
assert_eq!(Babe::genesis_slot(), genesis_slot);
assert_eq!(Babe::current_slot(), genesis_slot);
assert_eq!(Babe::epoch_index(), 0);
assert_eq!(Babe::author_vrf_randomness(), Some(Some(vrf_randomness)));

Babe::on_finalize(1);
let header = System::finalize();

assert_eq!(SegmentIndex::get(), 0);
assert_eq!(UnderConstruction::get(0), vec![vrf_randomness]);
assert_eq!(Babe::randomness(), [0; 32]);
assert_eq!(Babe::author_vrf_randomness(), None);
assert_eq!(NextRandomness::get(), [0; 32]);

assert_eq!(header.digest.logs.len(), 2);
Expand All @@ -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?

slot_number: u64,
pair: &sp_consensus_babe::AuthorityPair
) -> (VRFOutput, VRFProof, [u8; 32]) {
let pair = sp_core::sr25519::Pair::from_ref(pair).as_ref();
let transcript = sp_consensus_babe::make_transcript(&Babe::randomness(), slot_number, 0);
let vrf_inout = pair.vrf_sign(transcript);
let vrf_randomness: sp_consensus_vrf::schnorrkel::Randomness = vrf_inout.0
.make_bytes::<[u8; 32]>(&sp_consensus_babe::BABE_VRF_INOUT_CONTEXT);
let vrf_output = VRFOutput(vrf_inout.0.to_output());
let vrf_proof = VRFProof(vrf_inout.1);

(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.

fn author_vrf_output_for_primary() {
let (pairs, mut ext) = new_test_ext_with_pairs(1);

ext.execute_with(|| {
let genesis_slot = 10;
let (vrf_output, vrf_proof, vrf_randomness) = make_vrf_output(genesis_slot, &pairs[0]);
let primary_pre_digest = make_pre_digest(0, genesis_slot, vrf_output, vrf_proof);

System::initialize(
&1,
&Default::default(),
&Default::default(),
&primary_pre_digest,
Default::default(),
);
assert_eq!(Babe::author_vrf_randomness(), None);

Babe::do_initialize(1);
assert_eq!(Babe::author_vrf_randomness(), Some(Some(vrf_randomness)));

Babe::on_finalize(1);
System::finalize();
assert_eq!(Babe::author_vrf_randomness(), None);
})
}

#[test]
fn author_vrf_output_for_secondary_vrf() {
let (pairs, mut ext) = new_test_ext_with_pairs(1);

ext.execute_with(|| {
let genesis_slot = 10;
let (vrf_output, vrf_proof, vrf_randomness) = make_vrf_output(genesis_slot, &pairs[0]);
let secondary_vrf_pre_digest = make_secondary_vrf_pre_digest(0, genesis_slot, vrf_output, vrf_proof);

System::initialize(
&1,
&Default::default(),
&Default::default(),
&secondary_vrf_pre_digest,
Default::default(),
);
assert_eq!(Babe::author_vrf_randomness(), None);

Babe::do_initialize(1);
assert_eq!(Babe::author_vrf_randomness(), Some(Some(vrf_randomness)));

Babe::on_finalize(1);
System::finalize();
assert_eq!(Babe::author_vrf_randomness(), None);
})
}

#[test]
fn no_author_vrf_output_for_secondary_plain() {
new_test_ext(1).execute_with(|| {
let genesis_slot = 10;
let secondary_plain_pre_digest = make_secondary_plain_pre_digest(0, genesis_slot);

System::initialize(
&1,
&Default::default(),
&Default::default(),
&secondary_plain_pre_digest,
Default::default(),
);
assert_eq!(Babe::author_vrf_randomness(), None);

Babe::do_initialize(1);
assert_eq!(Babe::author_vrf_randomness(), Some(None));

Babe::on_finalize(1);
System::finalize();
assert_eq!(Babe::author_vrf_randomness(), None);
})
}

#[test]
fn authority_index() {
new_test_ext(4).execute_with(|| {
Expand Down
9 changes: 9 additions & 0 deletions primitives/consensus/babe/src/digests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ impl PreDigest {
PreDigest::SecondaryPlain(_) | PreDigest::SecondaryVRF(_) => 0,
}
}

/// Returns the VRF output, if it exists.
pub fn vrf_output(&self) -> Option<&VRFOutput> {
match self {
PreDigest::Primary(primary) => Some(&primary.vrf_output),
PreDigest::SecondaryVRF(secondary) => Some(&secondary.vrf_output),
PreDigest::SecondaryPlain(_) => None,
}
}
}

/// Information about the next epoch. This is broadcast in the first block
Expand Down