From 67e4e46fb0a1afb9f114004f1ce6c899b1da17f9 Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Tue, 16 Jun 2020 20:17:18 +0300 Subject: [PATCH] Bring in types from #1242 --- Cargo.lock | 1 - node/messages/src/lib.rs | 3 +- node/primitives/Cargo.toml | 1 - node/primitives/src/lib.rs | 34 -------------- primitives/src/parachain.rs | 89 +++++++++++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 296dee77ccbe..156f115d7794 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4230,7 +4230,6 @@ dependencies = [ name = "polkadot-node-primitives" version = "0.1.0" dependencies = [ - "bitvec", "parity-scale-codec", "polkadot-primitives", "polkadot-statement-table", diff --git a/node/messages/src/lib.rs b/node/messages/src/lib.rs index 2929b560c4a8..8bb83557994c 100644 --- a/node/messages/src/lib.rs +++ b/node/messages/src/lib.rs @@ -27,9 +27,10 @@ use futures::channel::{mpsc, oneshot}; use polkadot_primitives::{Hash, Signature}; use polkadot_primitives::parachain::{ AbridgedCandidateReceipt, PoVBlock, ErasureChunk, AttestedCandidate as BackedCandidate, + SignedAvailabilityBitfield }; use polkadot_node_primitives::{ - MisbehaviorReport, SignedStatement, SignedAvailabilityBitfield, + MisbehaviorReport, SignedStatement, }; /// Signals sent by an overseer to a subsystem. diff --git a/node/primitives/Cargo.toml b/node/primitives/Cargo.toml index e17d3dd4b4a7..f317565b2e99 100644 --- a/node/primitives/Cargo.toml +++ b/node/primitives/Cargo.toml @@ -10,4 +10,3 @@ polkadot-primitives = { path = "../../primitives" } polkadot-statement-table = { path = "../../statement-table" } parity-scale-codec = { version = "1.3.0", default-features = false, features = ["derive"] } runtime_primitives = { package = "sp-runtime", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false } -bitvec = { version = "0.17.4", default-features = false, features = ["alloc"] } diff --git a/node/primitives/src/lib.rs b/node/primitives/src/lib.rs index 80fe87071e99..78a87cda39a9 100644 --- a/node/primitives/src/lib.rs +++ b/node/primitives/src/lib.rs @@ -20,8 +20,6 @@ //! not shared between the node and the runtime. This crate builds on top of the primitives defined //! there. -use bitvec::vec::BitVec; - use runtime_primitives::traits::AppVerify; use polkadot_primitives::Hash; use polkadot_primitives::parachain::{ @@ -109,35 +107,3 @@ pub enum MisbehaviorReport { /// This peer has seconded more than one parachain candidate for this relay parent head DoubleVote(CandidateReceipt, SignedStatement, SignedStatement), } - -/// A bitfield signed by a particular validator about the availability of pending candidates. -pub struct SignedAvailabilityBitfield { - /// The index of the validator that signed this bitfield - pub validator_index: ValidatorIndex, - /// Bitfield itself. - pub bitfield: BitVec, - /// Signature. - pub signature: ValidatorSignature, // signature is on payload: bitfield ++ relay_parent ++ validator index -} - -impl SignedAvailabilityBitfield { - /// Check the signature on an availability bitfield. Provide a list of validators to index into. - /// - /// Returns an `Err` if out of bounds or the signature is invalid. Otherwise, returns `Ok`. - pub fn check_signature( - &self, - validators: &[ValidatorId], - ) -> Result<(), ()> { - let validator = validators.get(self.validator_index as usize).ok_or(())?; - let payload = self.bitfield.as_slice(); - - if self.signature.verify(payload, validator) { - Ok(()) - } else { - Err(()) - } - } -} - -/// A bitfield signed by a particular validator about the availability of pending candidates. -pub struct Bitfields(pub Vec); diff --git a/primitives/src/parachain.rs b/primitives/src/parachain.rs index 7d4bd7dca677..195c986a23be 100644 --- a/primitives/src/parachain.rs +++ b/primitives/src/parachain.rs @@ -670,6 +670,95 @@ impl FeeSchedule { } } +/// A bitfield concerning availability of backed candidates. +#[derive(PartialEq, Eq, Clone, Encode, Decode)] +#[cfg_attr(feature = "std", derive(Debug))] +pub struct AvailabilityBitfield(pub BitVec); + +impl From> for AvailabilityBitfield { + fn from(inner: BitVec) -> Self { + AvailabilityBitfield(inner) + } +} + +impl AvailabilityBitfield { + /// Encodes the signing payload into the given buffer. + pub fn encode_signing_payload_into( + &self, + signing_context: &SigningContext, + buf: &mut Vec, + ) { + self.0.encode_to(buf); + signing_context.encode_to(buf); + } + + /// Encodes the signing payload into a fresh byte-vector. + pub fn encode_signing_payload( + &self, + signing_context: + &SigningContext, + ) -> Vec { + let mut v = Vec::new(); + self.encode_signing_payload_into(signing_context, &mut v); + v + } +} + +/// A bitfield signed by a particular validator about the availability of pending candidates. +#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)] +pub struct SignedAvailabilityBitfield { + /// The index of the validator in the current set. + pub validator_index: ValidatorIndex, + /// The bitfield itself, with one bit per core. Only occupied cores may have the `1` bit set. + pub bitfield: AvailabilityBitfield, + /// The signature by the validator on the bitfield's signing payload. The context of the signature + /// should be apparent when checking the signature. + pub signature: ValidatorSignature, +} + +/// Check a signature on an availability bitfield. Provide the bitfield, the validator who signed it, +/// the signature, the signing context, and an optional buffer in which to encode. +/// +/// If the buffer is provided, it is assumed to be empty. +pub fn check_availability_bitfield_signature( + bitfield: &AvailabilityBitfield, + validator: &ValidatorId, + signature: &ValidatorSignature, + signing_context: &SigningContext, + payload_encode_buf: Option<&mut Vec>, +) -> Result<(),()> { + use runtime_primitives::traits::AppVerify; + + let mut v = Vec::new(); + let payload_encode_buf = payload_encode_buf.unwrap_or(&mut v); + + bitfield.encode_signing_payload_into(signing_context, payload_encode_buf); + + if signature.verify(&payload_encode_buf[..], validator) { + Ok(()) + } else { + Err(()) + } +} + +/// A set of signed availability bitfields. Should be sorted by validator index, ascending. +#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)] +pub struct SignedAvailabilityBitfields(pub Vec); + +/// A backed (or backable, depending on context) candidate. +// TODO: yes, this is roughly the same as AttestedCandidate. +// After https://github.com/paritytech/polkadot/issues/1250 +// they should be unified to this type. +#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)] +pub struct BackedCandidate { + /// The candidate referred to. + pub candidate: AbridgedCandidateReceipt, + /// The validity votes themselves, expressed as signatures. + pub validity_votes: Vec, + /// The indices of the validators within the group, expressed as a bitfield. + pub validator_indices: BitVec, +} + sp_api::decl_runtime_apis! { /// The API for querying the state of parachains on-chain. #[api_version(3)]