Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3517a80
babe: secondary blocks with VRF
sorpaas Apr 2, 2020
ae38673
Fix node runtime compile
sorpaas Apr 2, 2020
13aa495
Fix test-utils runtime interface
sorpaas Apr 2, 2020
c264025
Fix babe tests
sorpaas Apr 2, 2020
936253c
typo: v == 2
sorpaas Apr 3, 2020
5383f4d
babe: support online configuration upgrades
sorpaas Apr 3, 2020
2c91a14
Fix rpc tests
sorpaas Apr 3, 2020
06adbb9
Fix runtime version tests
sorpaas Apr 3, 2020
c1b502c
Merge branch 'master' of https://github.com/paritytech/substrate into…
sorpaas Apr 3, 2020
8185b91
Switch to use NextConfigDescriptor instead of changing runtime interface
sorpaas Apr 7, 2020
e9c57dd
Merge branch 'master' of https://github.com/paritytech/substrate into…
sorpaas Apr 7, 2020
559b895
Fix tests
sorpaas Apr 7, 2020
9402233
Merge branch 'master' into sp-epoch-config
sorpaas Apr 8, 2020
682e3b7
epoch-changes: map function that allows converting with different epo…
sorpaas Apr 8, 2020
fc8d9fe
Add migration script for the epoch config change
sorpaas Apr 8, 2020
7a65f26
Merge branch 'sp-epoch-config' of https://github.com/paritytech/subst…
sorpaas Apr 8, 2020
9a23a79
Merge branch 'master' of https://github.com/paritytech/substrate into…
sorpaas Apr 19, 2020
0c52d85
Fix docs for PrimaryAndSecondaryVRFSlots
sorpaas Apr 19, 2020
6a032b8
Merge branch 'master' of https://github.com/paritytech/substrate into…
sorpaas Apr 20, 2020
d978b67
Merge branch 'master' into sp-epoch-config
sorpaas Apr 20, 2020
d825d45
Add docs of `SecondaryVRF` in babe crate
sorpaas Apr 20, 2020
df540a4
babe-primitives: Secondary -> SecondaryPlain
sorpaas Apr 20, 2020
42360ec
babe-client: Secondary -> SecondaryPlain
sorpaas Apr 20, 2020
5144e80
Fix migration tests
sorpaas Apr 20, 2020
bd102bc
Merge branch 'sp-epoch-config' of https://github.com/paritytech/subst…
sorpaas Apr 20, 2020
e049b81
test-utils-runtime: Secondary -> SecondaryPlain
sorpaas Apr 20, 2020
904df04
Fix missing name change in test-utils-runtime
sorpaas Apr 20, 2020
abba6a5
Fix migration: Epoch should be EpochV0
sorpaas Apr 20, 2020
ee72297
Update client/consensus/babe/src/lib.rs
sorpaas Apr 20, 2020
52bc6f9
Fix new epochChanges version
sorpaas Apr 20, 2020
60f4f80
Merge branch 'sp-epoch-config' of https://github.com/paritytech/subst…
sorpaas Apr 20, 2020
4e9490d
Merge branch 'sp-epoch-config' into sp-aura-vrf
sorpaas Apr 20, 2020
3b889a3
Fix babe-primitives naming changes
sorpaas Apr 20, 2020
6cf220a
Fix merge issues in babe-client
sorpaas Apr 20, 2020
2d1295e
Merge branch 'master' of https://github.com/paritytech/substrate into…
sorpaas Apr 23, 2020
4c12ea8
Merge branch 'master' into sp-aura-vrf
andresilva Apr 24, 2020
b2e9eba
Merge branch 'master' into sp-aura-vrf
andresilva Apr 24, 2020
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
6 changes: 3 additions & 3 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 240,
impl_version: 1,
spec_version: 241,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
};

Expand Down Expand Up @@ -794,7 +794,7 @@ impl_runtime_apis! {
c: PRIMARY_PROBABILITY,
genesis_authorities: Babe::authorities(),
randomness: Babe::randomness(),
secondary_slots: true,
allowed_slots: sp_consensus_babe::AllowedSlots::PrimaryAndSecondarySlots,
}
}

Expand Down
7 changes: 6 additions & 1 deletion client/consensus/babe/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ impl<B, C, SC> BabeApi for BabeRPCHandler<B, C, SC>
PreDigest::Secondary { .. } => {
claims.entry(key.public()).or_default().secondary.push(slot_number);
}
PreDigest::SecondaryVRF { .. } => {
claims.entry(key.public()).or_default().secondary_vrf.push(slot_number);
},
};
}
}
Expand All @@ -144,6 +147,8 @@ pub struct EpochAuthorship {
primary: Vec<u64>,
/// the array of secondary slots that can be claimed
secondary: Vec<u64>,
/// The array of secondary VRF slots that can be claimed.
secondary_vrf: Vec<u64>,
}

/// Errors encountered by the RPC
Expand Down Expand Up @@ -236,7 +241,7 @@ mod tests {

io.extend_with(BabeApi::to_delegate(handler));
let request = r#"{"jsonrpc":"2.0","method":"babe_epochAuthorship","params": [],"id":1}"#;
let response = r#"{"jsonrpc":"2.0","result":{"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY":{"primary":[0],"secondary":[1,2,4]}},"id":1}"#;
let response = r#"{"jsonrpc":"2.0","result":{"5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY":{"primary":[0],"secondary":[1,2,4],"secondary_vrf":[]}},"id":1}"#;

assert_eq!(Some(response.into()), io.handle_request_sync(request));
}
Expand Down
45 changes: 34 additions & 11 deletions client/consensus/babe/src/authorship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use sp_consensus_babe::{
AuthorityId, BabeAuthorityWeight, BABE_ENGINE_ID, BABE_VRF_PREFIX,
SlotNumber, AuthorityPair, BabeConfiguration
};
use sp_consensus_babe::digests::{PreDigest, PrimaryPreDigest, SecondaryPreDigest};
use sp_consensus_babe::digests::{
PreDigest, PrimaryPreDigest, SecondaryPreDigest, SecondaryVRFPreDigest,
};
use sp_consensus_vrf::schnorrkel::{VRFOutput, VRFProof};
use sp_core::{U256, blake2_256};
use codec::Encode;
Expand Down Expand Up @@ -105,18 +107,20 @@ pub(super) fn make_transcript(
/// to propose.
fn claim_secondary_slot(
slot_number: SlotNumber,
authorities: &[(AuthorityId, BabeAuthorityWeight)],
epoch: &Epoch,
keystore: &KeyStorePtr,
randomness: [u8; 32],
author_secondary_vrf: bool,
) -> Option<(PreDigest, AuthorityPair)> {
let Epoch { authorities, randomness, epoch_index, .. } = epoch;

if authorities.is_empty() {
return None;
}

let expected_author = super::authorship::secondary_slot_author(
slot_number,
authorities,
randomness,
*randomness,
)?;

let keystore = keystore.read();
Expand All @@ -128,10 +132,27 @@ fn claim_secondary_slot(
})
{
if pair.public() == *expected_author {
let pre_digest = PreDigest::Secondary(SecondaryPreDigest {
slot_number,
authority_index: authority_index as u32,
});
let pre_digest = if author_secondary_vrf {
let transcript = super::authorship::make_transcript(
randomness,
slot_number,
*epoch_index,
);

let s = get_keypair(&pair).vrf_sign(transcript);

PreDigest::SecondaryVRF(SecondaryVRFPreDigest {
slot_number,
vrf_output: VRFOutput(s.0.to_output()),
vrf_proof: VRFProof(s.1),
authority_index: authority_index as u32,
})
} else {
PreDigest::Secondary(SecondaryPreDigest {
slot_number,
authority_index: authority_index as u32,
})
};

return Some((pre_digest, pair));
}
Expand All @@ -152,12 +173,14 @@ pub fn claim_slot(
) -> Option<(PreDigest, AuthorityPair)> {
claim_primary_slot(slot_number, epoch, config.c, keystore)
.or_else(|| {
if config.secondary_slots {
if config.allowed_slots.is_secondary_slots_allowed() ||
config.allowed_slots.is_secondary_vrf_slots_allowed()
{
claim_secondary_slot(
slot_number,
&epoch.authorities,
&epoch,
keystore,
epoch.randomness,
config.allowed_slots.is_secondary_vrf_slots_allowed(),
)
} else {
None
Expand Down
21 changes: 20 additions & 1 deletion client/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,26 @@ impl Config {
C: AuxStore + ProvideRuntimeApi<B>, C::Api: BabeApi<B, Error = sp_blockchain::Error>,
{
trace!(target: "babe", "Getting slot duration");
match sc_consensus_slots::SlotDuration::get_or_compute(client, |a, b| a.configuration(b)).map(Self) {
match sc_consensus_slots::SlotDuration::get_or_compute(client, |a, b| {
let has_api_v1 = a.has_api_with::<dyn BabeApi<B, Error = sp_blockchain::Error>, _>(
&b, |v| v == 1,
)?;
let has_api_v2 = a.has_api_with::<dyn BabeApi<B, Error = sp_blockchain::Error>, _>(
&b, |v| v == 2,
)?;

if has_api_v1 {
#[allow(deprecated)] {
Ok(a.configuration_before_version_2(b)?.into())
}
} else if has_api_v2 {
a.configuration(b)
} else {
Err(sp_blockchain::Error::VersionInvalid(
"Unsupported or invalid BabeApi version".to_string()
))
}
}).map(Self) {
Ok(s) => Ok(s),
Err(s) => {
warn!(target: "babe", "Failed to get slot duration");
Expand Down
6 changes: 3 additions & 3 deletions client/consensus/babe/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use super::*;
use authorship::claim_slot;

use sp_consensus_babe::{AuthorityPair, SlotNumber};
use sp_consensus_babe::{AuthorityPair, SlotNumber, AllowedSlots};
use sc_block_builder::{BlockBuilder, BlockBuilderProvider};
use sp_consensus::{
NoNetwork as DummyOracle, Proposal, RecordProof,
Expand Down Expand Up @@ -513,7 +513,7 @@ fn can_author_block() {
c: (3, 10),
genesis_authorities: Vec::new(),
randomness: [0; 32],
secondary_slots: true,
allowed_slots: AllowedSlots::PrimaryAndSecondarySlots,
};

// with secondary slots enabled it should never be empty
Expand All @@ -524,7 +524,7 @@ fn can_author_block() {

// otherwise with only vrf-based primary slots we might need to try a couple
// of times.
config.secondary_slots = false;
config.allowed_slots = AllowedSlots::PrimarySlots;
loop {
match claim_slot(i, &epoch, &config, &keystore) {
None => i += 1,
Expand Down
65 changes: 58 additions & 7 deletions client/consensus/babe/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use sp_runtime::{traits::Header, traits::DigestItemFor};
use sp_core::{Pair, Public};
use sp_consensus_babe::{AuthoritySignature, SlotNumber, AuthorityPair, AuthorityId};
use sp_consensus_babe::digests::{
PreDigest, PrimaryPreDigest, SecondaryPreDigest, CompatibleDigestItem
PreDigest, PrimaryPreDigest, SecondaryPreDigest, SecondaryVRFPreDigest,
CompatibleDigestItem
};
use sc_consensus_slots::CheckedHeader;
use log::{debug, trace};
Expand All @@ -28,17 +29,17 @@ use super::authorship::{make_transcript, calculate_primary_threshold, check_prim

/// BABE verification parameters
pub(super) struct VerificationParams<'a, B: 'a + BlockT> {
/// the header being verified.
/// The header being verified.
pub(super) header: B::Header,
/// the pre-digest of the header being verified. this is optional - if prior
/// The pre-digest of the header being verified. this is optional - if prior
/// verification code had to read it, it can be included here to avoid duplicate
/// work.
pub(super) pre_digest: Option<PreDigest>,
/// the slot number of the current time.
/// The slot number of the current time.
pub(super) slot_now: SlotNumber,
/// epoch descriptor of the epoch this block _should_ be under, if it's valid.
/// Epoch descriptor of the epoch this block _should_ be under, if it's valid.
pub(super) epoch: &'a Epoch,
/// genesis config of this BABE chain.
/// Genesis config of this BABE chain.
pub(super) config: &'a super::Config,
}

Expand Down Expand Up @@ -105,7 +106,7 @@ pub(super) fn check_header<B: BlockT + Sized>(
config.c,
)?;
},
PreDigest::Secondary(secondary) if config.secondary_slots => {
PreDigest::Secondary(secondary) if config.allowed_slots.is_secondary_slots_allowed() => {
debug!(target: "babe", "Verifying Secondary block");

check_secondary_header::<B>(
Expand All @@ -115,6 +116,16 @@ pub(super) fn check_header<B: BlockT + Sized>(
&epoch,
)?;
},
PreDigest::SecondaryVRF(secondary) if config.allowed_slots.is_secondary_vrf_slots_allowed() => {
debug!(target: "babe", "Verifying Secondary VRF block");

check_secondary_vrf_header::<B>(
pre_hash,
secondary,
sig,
&epoch,
)?;
},
_ => {
return Err(babe_err(Error::SecondarySlotAssignmentsDisabled));
}
Expand Down Expand Up @@ -208,3 +219,43 @@ fn check_secondary_header<B: BlockT>(
Err(Error::BadSignature(pre_hash))
}
}

/// Check a secondary VRF slot proposal header.
fn check_secondary_vrf_header<B: BlockT>(
pre_hash: B::Hash,
pre_digest: &SecondaryVRFPreDigest,
signature: AuthoritySignature,
epoch: &Epoch,
) -> Result<(), Error<B>> {
// check the signature is valid under the expected authority and
// chain state.
let expected_author = secondary_slot_author(
pre_digest.slot_number,
&epoch.authorities,
epoch.randomness,
).ok_or_else(|| Error::NoSecondaryAuthorExpected)?;

let author = &epoch.authorities[pre_digest.authority_index as usize].0;

if expected_author != author {
return Err(Error::InvalidAuthor(expected_author.clone(), author.clone()));
}

if AuthorityPair::verify(&signature, pre_hash.as_ref(), author) {
let transcript = make_transcript(
&epoch.randomness,
pre_digest.slot_number,
epoch.epoch_index,
);

schnorrkel::PublicKey::from_bytes(author.as_slice()).and_then(|p| {
p.vrf_verify(transcript, &pre_digest.vrf_output, &pre_digest.vrf_proof)
}).map_err(|s| {
babe_err(Error::VRFVerificationFailed(s))
})?;

Ok(())
} else {
Err(Error::BadSignature(pre_hash))
}
}
2 changes: 1 addition & 1 deletion client/rpc/src/state/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ fn should_return_runtime_version() {
let result = "{\"specName\":\"test\",\"implName\":\"parity-test\",\"authoringVersion\":1,\
\"specVersion\":2,\"implVersion\":2,\"apis\":[[\"0xdf6acb689907609b\",2],\
[\"0x37e397fc7c91f5e4\",1],[\"0xd2bc9897eed08f15\",2],[\"0x40fe3ad401f8959a\",4],\
[\"0xc6e9a76309f39b09\",1],[\"0xdd718d5cc53262d4\",1],[\"0xcbca25e39f142387\",1],\
[\"0xc6e9a76309f39b09\",1],[\"0xdd718d5cc53262d4\",1],[\"0xcbca25e39f142387\",2],\
[\"0xf78b278be53f454c\",2],[\"0xab3c0572291feb8b\",1],[\"0xbc9d89904f5b923f\",1]]}";

let runtime_version = api.runtime_version(None.into()).wait().unwrap();
Expand Down
39 changes: 38 additions & 1 deletion primitives/consensus/babe/src/digests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,37 @@ pub struct SecondaryPreDigest {
pub slot_number: SlotNumber,
}

/// BABE secondary deterministic slot assignment with VRF outputs.
#[derive(Clone, RuntimeDebug, Encode, Decode)]
pub struct RawSecondaryVRFPreDigest<VRFOutput=schnorrkel::RawVRFOutput, VRFProof=schnorrkel::RawVRFProof> {
/// Authority index
pub authority_index: super::AuthorityIndex,
/// Slot number
pub slot_number: SlotNumber,
/// VRF output
pub vrf_output: VRFOutput,
/// VRF proof
pub vrf_proof: VRFProof,
}

#[cfg(feature = "std")]
/// BABE secondary slot assignment with VRF outputs pre-digest, for std environment.
pub type SecondaryVRFPreDigest = RawSecondaryVRFPreDigest<schnorrkel::VRFOutput, schnorrkel::VRFProof>;

#[cfg(feature = "std")]
impl TryFrom<RawSecondaryVRFPreDigest> for SecondaryVRFPreDigest {
type Error = SignatureError;

fn try_from(raw: RawSecondaryVRFPreDigest) -> Result<SecondaryVRFPreDigest, SignatureError> {
Ok(SecondaryVRFPreDigest {
authority_index: raw.authority_index,
slot_number: raw.slot_number,
vrf_output: raw.vrf_output.try_into()?,
vrf_proof: raw.vrf_proof.try_into()?,
})
}
}

/// A BABE pre-runtime digest. This contains all data required to validate a
/// block and for the BABE runtime module. Slots can be assigned to a primary
/// (VRF based) and to a secondary (slot number based).
Expand All @@ -88,6 +119,9 @@ pub enum RawPreDigest<VRFOutput=schnorrkel::RawVRFOutput, VRFProof=schnorrkel::R
/// A secondary deterministic slot assignment.
#[codec(index = "2")]
Secondary(SecondaryPreDigest),
/// A secondary deterministic slot assignment with VRF outputs.
#[codec(index = "3")]
SecondaryVRF(RawSecondaryVRFPreDigest<VRFOutput, VRFProof>),
Comment thread
andresilva marked this conversation as resolved.
}

#[cfg(feature = "std")]
Expand All @@ -100,6 +134,7 @@ impl<VRFOutput, VRFProof> RawPreDigest<VRFOutput, VRFProof> {
match self {
RawPreDigest::Primary(primary) => primary.authority_index,
RawPreDigest::Secondary(secondary) => secondary.authority_index,
RawPreDigest::SecondaryVRF(secondary) => secondary.authority_index,
}
}

Expand All @@ -108,6 +143,7 @@ impl<VRFOutput, VRFProof> RawPreDigest<VRFOutput, VRFProof> {
match self {
RawPreDigest::Primary(primary) => primary.slot_number,
RawPreDigest::Secondary(secondary) => secondary.slot_number,
RawPreDigest::SecondaryVRF(secondary) => secondary.slot_number,
}
}

Expand All @@ -116,7 +152,7 @@ impl<VRFOutput, VRFProof> RawPreDigest<VRFOutput, VRFProof> {
pub fn added_weight(&self) -> crate::BabeBlockWeight {
match self {
RawPreDigest::Primary(_) => 1,
RawPreDigest::Secondary(_) => 0,
RawPreDigest::Secondary(_) | RawPreDigest::SecondaryVRF(_) => 0,
}
}
}
Expand All @@ -129,6 +165,7 @@ impl TryFrom<RawPreDigest> for PreDigest {
Ok(match raw {
RawPreDigest::Primary(primary) => PreDigest::Primary(primary.try_into()?),
RawPreDigest::Secondary(secondary) => PreDigest::Secondary(secondary),
RawPreDigest::SecondaryVRF(secondary) => PreDigest::SecondaryVRF(secondary.try_into()?),
})
}
}
Expand Down
Loading