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
36 changes: 18 additions & 18 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,
&vrf_signature.proof,
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,
&vrf_signature.proof,
)
.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 polkadot_node_primitives::approval::{VrfOutput, VrfProof, VrfSignature};
use polkadot_primitives::{Hash, ASSIGNMENT_KEY_TYPE_ID};
use sp_application_crypto::sr25519;
use sp_core::crypto::Pair as PairT;
Expand Down Expand Up @@ -607,12 +607,12 @@ mod tests {
}

// used for generating assignments where the validity of the VRF doesn't matter.
fn garbage_vrf() -> (VRFOutput, VRFProof) {
fn garbage_vrf_signature() -> VrfSignature {
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))
VrfSignature { output: VrfOutput(o.to_output()), proof: VrfProof(p) }
}
davxy marked this conversation as resolved.
Show resolved Hide resolved

#[test]
Expand Down Expand Up @@ -825,7 +825,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 +838,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
18 changes: 9 additions & 9 deletions node/core/approval-voting/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ pub(crate) mod tests {
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::{VrfOutput, VrfProof, VrfSignature};
use polkadot_node_subsystem::messages::{AllMessages, ApprovalVotingMessage};
use polkadot_node_subsystem_test_helpers::make_subsystem_context;
use polkadot_node_subsystem_util::database::Database;
Expand Down Expand Up @@ -703,12 +703,12 @@ pub(crate) mod tests {
}

// used for generating assignments where the validity of the VRF doesn't matter.
pub(crate) fn garbage_vrf() -> (VRFOutput, VRFProof) {
pub(crate) fn garbage_vrf_signature() -> VrfSignature {
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))
VrfSignature { output: VrfOutput(o.to_output()), proof: VrfProof(p) }
}

fn dummy_session_info(index: SessionIndex) -> SessionInfo {
Expand Down Expand Up @@ -743,9 +743,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 +1042,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 +1194,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
29 changes: 12 additions & 17 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};

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,11 @@ 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.clone())
.map_err(ApprovalError::SchnorrkelSignature)?;
Ok(RelayVRFStory(inout.make_bytes(RELAY_VRF_STORY_CONTEXT)))
}
Expand All @@ -187,21 +185,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
16 changes: 8 additions & 8 deletions node/service/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
use super::{relay_chain_selection::*, *};

use futures::channel::oneshot::Receiver;
use polkadot_node_primitives::approval::{VRFOutput, VRFProof};
use polkadot_node_primitives::approval::{VrfOutput, VrfProof, 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_runtime::{testing::*, DigestItem};
use std::{
Expand Down Expand Up @@ -129,12 +129,13 @@ 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) {
fn garbage_vrf_signature() -> VrfSignature {
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))
let transcript = VrfTranscript::new(b"test-garbage", &[]);
let (o, p, _) = key.vrf_sign(transcript.clone());
VrfSignature { output: VrfOutput(o.to_output()), proof: VrfProof(p) }
}

/// Representation of a local representation
Expand Down Expand Up @@ -316,13 +317,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