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

Companion for Substrate #13889 #7063

Merged
merged 13 commits into from
Apr 19, 2023
389 changes: 184 additions & 205 deletions Cargo.lock

Large diffs are not rendered by default.

41 changes: 16 additions & 25 deletions node/core/approval-voting/src/criteria.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,10 @@ fn compute_relay_vrf_modulo_assignments(
// has been executed.
let cert = AssignmentCert {
kind: AssignmentCertKind::RelayVRFModulo { sample: rvm_sample },
vrf: (
approval_types::VRFOutput(vrf_in_out.to_output()),
approval_types::VRFProof(vrf_proof),
),
vrf: approval_types::VrfSignature {
output: approval_types::VrfOutput(vrf_in_out.to_output()),
proof: approval_types::VrfProof(vrf_proof),
},
};

// All assignments of type RelayVRFModulo have tranche 0.
Expand Down Expand Up @@ -393,10 +393,10 @@ fn compute_relay_vrf_delay_assignments(

let cert = AssignmentCert {
kind: AssignmentCertKind::RelayVRFDelay { core_index: core },
vrf: (
approval_types::VRFOutput(vrf_in_out.to_output()),
approval_types::VRFProof(vrf_proof),
),
vrf: approval_types::VrfSignature {
output: approval_types::VrfOutput(vrf_in_out.to_output()),
proof: approval_types::VrfProof(vrf_proof),
},
};

let our_assignment = OurAssignment { cert, tranche, validator_index, triggered: false };
Expand Down Expand Up @@ -496,7 +496,7 @@ pub(crate) fn check_assignment_cert(
return Err(InvalidAssignment(Reason::IsInBackingGroup))
}

let (vrf_output, vrf_proof) = &assignment.vrf;
let vrf_signature = &assignment.vrf;
match assignment.kind {
AssignmentCertKind::RelayVRFModulo { sample } => {
if sample >= config.relay_vrf_modulo_samples {
Expand All @@ -506,8 +506,8 @@ pub(crate) fn check_assignment_cert(
let (vrf_in_out, _) = public
.vrf_verify_extra(
relay_vrf_modulo_transcript(relay_vrf_story, sample),
&vrf_output.0,
&vrf_proof.0,
&vrf_signature.output.0,
&vrf_signature.proof.0,
assigned_core_transcript(claimed_core_index),
)
.map_err(|_| InvalidAssignment(Reason::VRFModuloOutputMismatch))?;
Expand All @@ -527,8 +527,8 @@ pub(crate) fn check_assignment_cert(
let (vrf_in_out, _) = public
.vrf_verify(
relay_vrf_delay_transcript(relay_vrf_story, core_index),
&vrf_output.0,
&vrf_proof.0,
&vrf_signature.output.0,
&vrf_signature.proof.0,
)
.map_err(|_| InvalidAssignment(Reason::VRFDelayOutputMismatch))?;

Expand All @@ -552,7 +552,7 @@ fn is_in_backing_group(
#[cfg(test)]
mod tests {
use super::*;
use polkadot_node_primitives::approval::{VRFOutput, VRFProof};
use crate::import::tests::garbage_vrf_signature;
use polkadot_primitives::{Hash, ASSIGNMENT_KEY_TYPE_ID};
use sp_application_crypto::sr25519;
use sp_core::crypto::Pair as PairT;
Expand Down Expand Up @@ -606,15 +606,6 @@ mod tests {
.collect()
}

// used for generating assignments where the validity of the VRF doesn't matter.
fn garbage_vrf() -> (VRFOutput, VRFProof) {
let key = Sr25519Keyring::Alice.pair();
let key: &schnorrkel::Keypair = key.as_ref();

let (o, p, _) = key.vrf_sign(Transcript::new(b"test-garbage"));
(VRFOutput(o.to_output()), VRFProof(p))
}

#[test]
fn assignments_produced_for_non_backing() {
let keystore = make_keystore(&[Sr25519Keyring::Alice]);
Expand Down Expand Up @@ -825,7 +816,7 @@ mod tests {
check_mutated_assignments(40, 10, 8, |m| {
match m.cert.kind.clone() {
AssignmentCertKind::RelayVRFDelay { .. } => {
m.cert.vrf = garbage_vrf();
m.cert.vrf = garbage_vrf_signature();
Some(false)
},
_ => None, // skip everything else.
Expand All @@ -838,7 +829,7 @@ mod tests {
check_mutated_assignments(200, 100, 25, |m| {
match m.cert.kind.clone() {
AssignmentCertKind::RelayVRFModulo { .. } => {
m.cert.vrf = garbage_vrf();
m.cert.vrf = garbage_vrf_signature();
Some(false)
},
_ => None, // skip everything else.
Expand Down
26 changes: 11 additions & 15 deletions node/core/approval-voting/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,7 @@ pub(crate) mod tests {
use crate::approval_db::v1::DbBackend;
use ::test_helpers::{dummy_candidate_receipt, dummy_hash};
use assert_matches::assert_matches;
use merlin::Transcript;
use polkadot_node_primitives::approval::{VRFOutput, VRFProof};
use polkadot_node_primitives::approval::{VrfSignature, VrfTranscript};
use polkadot_node_subsystem::messages::{AllMessages, ApprovalVotingMessage};
use polkadot_node_subsystem_test_helpers::make_subsystem_context;
use polkadot_node_subsystem_util::database::Database;
Expand All @@ -622,7 +621,7 @@ pub(crate) mod tests {
digests::{CompatibleDigestItem, PreDigest, SecondaryVRFPreDigest},
AllowedSlots, BabeEpochConfiguration, Epoch as BabeEpoch,
};
use sp_core::testing::TaskExecutor;
use sp_core::{crypto::VrfSigner, testing::TaskExecutor};
use sp_keyring::sr25519::Keyring as Sr25519Keyring;
pub(crate) use sp_runtime::{Digest, DigestItem};
use std::{pin::Pin, sync::Arc};
Expand Down Expand Up @@ -703,12 +702,9 @@ pub(crate) mod tests {
}

// used for generating assignments where the validity of the VRF doesn't matter.
pub(crate) fn garbage_vrf() -> (VRFOutput, VRFProof) {
let key = Sr25519Keyring::Alice.pair();
let key: &schnorrkel::Keypair = key.as_ref();

let (o, p, _) = key.vrf_sign(Transcript::new(b"test-garbage"));
(VRFOutput(o.to_output()), VRFProof(p))
pub(crate) fn garbage_vrf_signature() -> VrfSignature {
let transcript = VrfTranscript::new(b"test-garbage", &[]);
Sr25519Keyring::Alice.pair().vrf_sign(&transcript)
}

fn dummy_session_info(index: SessionIndex) -> SessionInfo {
Expand Down Expand Up @@ -743,9 +739,9 @@ pub(crate) mod tests {
let header = Header {
digest: {
let mut d = Digest::default();
let (vrf_output, vrf_proof) = garbage_vrf();
let vrf_signature = garbage_vrf_signature();
d.push(DigestItem::babe_pre_digest(PreDigest::SecondaryVRF(
SecondaryVRFPreDigest { authority_index: 0, slot, vrf_output, vrf_proof },
SecondaryVRFPreDigest { authority_index: 0, slot, vrf_signature },
)));

d
Expand Down Expand Up @@ -1042,9 +1038,9 @@ pub(crate) mod tests {
let header = Header {
digest: {
let mut d = Digest::default();
let (vrf_output, vrf_proof) = garbage_vrf();
let vrf_signature = garbage_vrf_signature();
d.push(DigestItem::babe_pre_digest(PreDigest::SecondaryVRF(
SecondaryVRFPreDigest { authority_index: 0, slot, vrf_output, vrf_proof },
SecondaryVRFPreDigest { authority_index: 0, slot, vrf_signature },
)));

d.push(ConsensusLog::ForceApprove(3).into());
Expand Down Expand Up @@ -1194,9 +1190,9 @@ pub(crate) mod tests {
let header = Header {
digest: {
let mut d = Digest::default();
let (vrf_output, vrf_proof) = garbage_vrf();
let vrf_signature = garbage_vrf_signature();
d.push(DigestItem::babe_pre_digest(PreDigest::SecondaryVRF(
SecondaryVRFPreDigest { authority_index: 0, slot, vrf_output, vrf_proof },
SecondaryVRFPreDigest { authority_index: 0, slot, vrf_signature },
)));

d
Expand Down
12 changes: 6 additions & 6 deletions node/core/approval-voting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::tests::test_constants::TEST_CONFIG;
use super::*;
use polkadot_node_primitives::{
approval::{
AssignmentCert, AssignmentCertKind, DelayTranche, VRFOutput, VRFProof,
AssignmentCert, AssignmentCertKind, DelayTranche, VrfOutput, VrfProof, VrfSignature,
RELAY_VRF_MODULO_CONTEXT,
},
AvailableData, BlockData, PoV,
Expand Down Expand Up @@ -56,8 +56,8 @@ use super::{
approval_db::v1::StoredBlockRange,
backend::BackendWriteOp,
import::tests::{
garbage_vrf, AllowedSlots, BabeEpoch, BabeEpochConfiguration, CompatibleDigestItem, Digest,
DigestItem, PreDigest, SecondaryVRFPreDigest,
garbage_vrf_signature, AllowedSlots, BabeEpoch, BabeEpochConfiguration,
CompatibleDigestItem, Digest, DigestItem, PreDigest, SecondaryVRFPreDigest,
},
};

Expand Down Expand Up @@ -392,7 +392,7 @@ fn garbage_assignment_cert(kind: AssignmentCertKind) -> AssignmentCert {
let (inout, proof, _) = keypair.vrf_sign(ctx.bytes(msg));
let out = inout.to_output();

AssignmentCert { kind, vrf: (VRFOutput(out), VRFProof(proof)) }
AssignmentCert { kind, vrf: VrfSignature { output: VrfOutput(out), proof: VrfProof(proof) } }
}

fn sign_approval(
Expand Down Expand Up @@ -721,9 +721,9 @@ impl ChainBuilder {
fn make_header(parent_hash: Hash, slot: Slot, number: u32) -> Header {
let digest = {
let mut digest = Digest::default();
let (vrf_output, vrf_proof) = garbage_vrf();
let vrf_signature = garbage_vrf_signature();
digest.push(DigestItem::babe_pre_digest(PreDigest::SecondaryVRF(
SecondaryVRFPreDigest { authority_index: 0, slot, vrf_output, vrf_proof },
SecondaryVRFPreDigest { authority_index: 0, slot, vrf_signature },
)));
digest
};
Expand Down
4 changes: 2 additions & 2 deletions node/network/approval-distribution/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use polkadot_node_network_protocol::{
view, ObservedRole,
};
use polkadot_node_primitives::approval::{
AssignmentCertKind, VRFOutput, VRFProof, RELAY_VRF_MODULO_CONTEXT,
AssignmentCertKind, VrfOutput, VrfProof, VrfSignature, RELAY_VRF_MODULO_CONTEXT,
};
use polkadot_node_subsystem::messages::{network_bridge_event, AllMessages, ApprovalCheckError};
use polkadot_node_subsystem_test_helpers as test_helpers;
Expand Down Expand Up @@ -265,7 +265,7 @@ fn fake_assignment_cert(block_hash: Hash, validator: ValidatorIndex) -> Indirect
validator,
cert: AssignmentCert {
kind: AssignmentCertKind::RelayVRFModulo { sample: 1 },
vrf: (VRFOutput(out), VRFProof(proof)),
vrf: VrfSignature { output: VrfOutput(out), proof: VrfProof(proof) },
},
}
}
Expand Down
1 change: 0 additions & 1 deletion node/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ polkadot-primitives = { path = "../../primitives" }
parity-scale-codec = { version = "3.4.0", default-features = false, features = ["derive"] }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-application-crypto = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-consensus-vrf = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-consensus-babe = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-keystore = { git = "https://github.com/paritytech/substrate", branch = "master" }
sp-maybe-compressed-blob = { git = "https://github.com/paritytech/substrate", branch = "master" }
Expand Down
28 changes: 12 additions & 16 deletions node/primitives/src/approval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

//! Types relevant for approval.

pub use sp_consensus_babe::Slot;
pub use sp_consensus_vrf::schnorrkel::{Randomness, VRFOutput, VRFProof};
pub use sp_consensus_babe::{Randomness, Slot, VrfOutput, VrfProof, VrfSignature, VrfTranscript};

use parity_scale_codec::{Decode, Encode};
use polkadot_primitives::{
Expand Down Expand Up @@ -82,8 +81,8 @@ pub enum AssignmentCertKind {
pub struct AssignmentCert {
/// The criterion which is claimed to be met by this cert.
pub kind: AssignmentCertKind,
/// The VRF showing the criterion is met.
pub vrf: (VRFOutput, VRFProof),
/// The VRF signature showing the criterion is met.
pub vrf: VrfSignature,
}

/// An assignment criterion which refers to the candidate under which the assignment is
Expand Down Expand Up @@ -144,7 +143,7 @@ pub enum ApprovalError {

/// An unsafe VRF output. Provide BABE Epoch info to create a `RelayVRFStory`.
pub struct UnsafeVRFOutput {
vrf_output: VRFOutput,
vrf_output: VrfOutput,
slot: Slot,
authority_index: u32,
}
Expand All @@ -170,12 +169,12 @@ impl UnsafeVRFOutput {
let pubkey = schnorrkel::PublicKey::from_bytes(author.as_slice())
.map_err(ApprovalError::SchnorrkelSignature)?;

let transcript = babe_primitives::make_transcript(randomness, self.slot, epoch_index);
let transcript = sp_consensus_babe::make_transcript(randomness, self.slot, epoch_index);

let inout = self
.vrf_output
.0
.attach_input_hash(&pubkey, transcript)
.attach_input_hash(&pubkey, transcript.0)
.map_err(ApprovalError::SchnorrkelSignature)?;
Ok(RelayVRFStory(inout.make_bytes(RELAY_VRF_STORY_CONTEXT)))
}
Expand All @@ -187,21 +186,18 @@ impl UnsafeVRFOutput {
/// the digest has type `SecondaryPlain`, which Substrate nodes do
/// not produce or accept anymore.
pub fn babe_unsafe_vrf_info(header: &Header) -> Option<UnsafeVRFOutput> {
use babe_primitives::digests::{CompatibleDigestItem, PreDigest};
use babe_primitives::digests::CompatibleDigestItem;

for digest in &header.digest.logs {
if let Some(pre) = digest.as_babe_pre_digest() {
let slot = pre.slot();
let authority_index = pre.authority_index();

// exhaustive match to defend against upstream variant changes.
let vrf_output = match pre {
PreDigest::Primary(primary) => primary.vrf_output,
PreDigest::SecondaryVRF(secondary) => secondary.vrf_output,
PreDigest::SecondaryPlain(_) => return None,
};

return Some(UnsafeVRFOutput { vrf_output, slot, authority_index })
return pre.vrf_signature().map(|sig| UnsafeVRFOutput {
vrf_output: sig.output.clone(),
slot,
authority_index,
})
}
}

Expand Down
21 changes: 9 additions & 12 deletions node/service/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
use super::{relay_chain_selection::*, *};

use futures::channel::oneshot::Receiver;
use polkadot_node_primitives::approval::{VRFOutput, VRFProof};
use polkadot_node_primitives::approval::VrfSignature;
use polkadot_node_subsystem::messages::{AllMessages, BlockDescription};
use polkadot_node_subsystem_test_helpers as test_helpers;
use polkadot_node_subsystem_util::TimeoutExt;
use polkadot_test_client::Sr25519Keyring;
use sp_consensus_babe::{
digests::{CompatibleDigestItem, PreDigest, SecondaryVRFPreDigest},
Transcript,
VrfTranscript,
};
use sp_core::{crypto::VrfSigner, testing::TaskExecutor};
use sp_runtime::{testing::*, DigestItem};
use std::{
collections::{BTreeMap, HashMap, HashSet},
Expand Down Expand Up @@ -74,7 +75,7 @@ fn test_harness<T: Future<Output = VirtualOverseer>>(
.filter_level(log::LevelFilter::Trace)
.try_init();

let pool = sp_core::testing::TaskExecutor::new();
let pool = TaskExecutor::new();
let (mut context, virtual_overseer) = test_helpers::make_subsystem_context(pool);

let (finality_target_tx, finality_target_rx) = oneshot::channel::<Option<Hash>>();
Expand Down Expand Up @@ -129,12 +130,9 @@ async fn overseer_recv_with_timeout(
const TIMEOUT: Duration = Duration::from_millis(2000);

// used for generating assignments where the validity of the VRF doesn't matter.
fn garbage_vrf() -> (VRFOutput, VRFProof) {
let key = Sr25519Keyring::Alice.pair();
let key = key.as_ref();

let (o, p, _) = key.vrf_sign(Transcript::new(b"test-garbage"));
(VRFOutput(o.to_output()), VRFProof(p))
fn garbage_vrf_signature() -> VrfSignature {
let transcript = VrfTranscript::new(b"test-garbage", &[]);
Sr25519Keyring::Alice.pair().vrf_sign(&transcript)
}

/// Representation of a local representation
Expand Down Expand Up @@ -316,13 +314,12 @@ impl ChainBuilder {
fn make_header(parent_hash: Hash, number: u32) -> Header {
let digest = {
let mut digest = Digest::default();
let (vrf_output, vrf_proof) = garbage_vrf();
let vrf_signature = garbage_vrf_signature();
digest.push(DigestItem::babe_pre_digest(PreDigest::SecondaryVRF(
SecondaryVRFPreDigest {
authority_index: 0,
slot: 1.into(), // slot, unused
vrf_output,
vrf_proof,
vrf_signature,
},
)));
digest
Expand Down