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

Commit

Permalink
Bring in types from #1242
Browse files Browse the repository at this point in the history
  • Loading branch information
montekki committed Jun 16, 2020
1 parent 5b5a8b4 commit 67e4e46
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 37 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion node/messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion node/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
34 changes: 0 additions & 34 deletions node/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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<bitvec::order::Lsb0, u8>,
/// 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<SignedAvailabilityBitfield>);
89 changes: 89 additions & 0 deletions primitives/src/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bitvec::order::Lsb0, u8>);

impl From<BitVec<bitvec::order::Lsb0, u8>> for AvailabilityBitfield {
fn from(inner: BitVec<bitvec::order::Lsb0, u8>) -> 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<u8>,
) {
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<u8> {
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<H: Encode>(
bitfield: &AvailabilityBitfield,
validator: &ValidatorId,
signature: &ValidatorSignature,
signing_context: &SigningContext,
payload_encode_buf: Option<&mut Vec<u8>>,
) -> 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<SignedAvailabilityBitfield>);

/// 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<ValidityAttestation>,
/// The indices of the validators within the group, expressed as a bitfield.
pub validator_indices: BitVec<bitvec::order::Lsb0, u8>,
}

sp_api::decl_runtime_apis! {
/// The API for querying the state of parachains on-chain.
#[api_version(3)]
Expand Down

0 comments on commit 67e4e46

Please sign in to comment.