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

Commit

Permalink
move ApprovalVote to shared primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
rphmeier committed May 5, 2021
1 parent f70f6e2 commit 0451139
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
26 changes: 5 additions & 21 deletions node/core/approval-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,13 @@ use polkadot_primitives::v1::{
ValidatorIndex, Hash, SessionIndex, SessionInfo, CandidateHash,
CandidateReceipt, BlockNumber, PersistedValidationData,
ValidationCode, CandidateDescriptor, ValidatorPair, ValidatorSignature, ValidatorId,
CandidateIndex, GroupIndex,
CandidateIndex, GroupIndex, ApprovalVote,
};
use polkadot_node_primitives::{ValidationResult, PoV};
use polkadot_node_primitives::approval::{
IndirectAssignmentCert, IndirectSignedApprovalVote, ApprovalVote, DelayTranche,
BlockApprovalMeta,
IndirectAssignmentCert, IndirectSignedApprovalVote, DelayTranche, BlockApprovalMeta,
};
use polkadot_node_jaeger as jaeger;
use parity_scale_codec::Encode;
use sc_keystore::LocalKeystore;
use sp_consensus::SyncOracle;
use sp_consensus_slots::Slot;
Expand Down Expand Up @@ -1226,15 +1224,6 @@ async fn handle_approved_ancestor(
Ok(all_approved_max)
}

fn approval_signing_payload(
approval_vote: ApprovalVote,
session_index: SessionIndex,
) -> Vec<u8> {
const MAGIC: [u8; 4] = *b"APPR";

(MAGIC, approval_vote, session_index).encode()
}

// `Option::cmp` treats `None` as less than `Some`.
fn min_prefer_some<T: std::cmp::Ord>(
a: Option<T>,
Expand Down Expand Up @@ -1466,10 +1455,8 @@ fn check_and_import_approval<T>(
None => respond_early!(ApprovalCheckResult::Bad)
};

let approval_payload = approval_signing_payload(
ApprovalVote(approved_candidate_hash),
block_entry.session(),
);
let approval_payload = ApprovalVote(approved_candidate_hash)
.signing_payload(block_entry.session());

let pubkey = match session_info.validators.get(approval.validator.0 as usize) {
Some(k) => k,
Expand Down Expand Up @@ -2157,10 +2144,7 @@ fn sign_approval(
) -> Option<ValidatorSignature> {
let key = keystore.key_pair::<ValidatorPair>(public).ok().flatten()?;

let payload = approval_signing_payload(
ApprovalVote(candidate_hash),
session_index,
);
let payload = ApprovalVote(candidate_hash).signing_payload(session_index);

Some(key.sign(&payload[..]))
}
2 changes: 1 addition & 1 deletion node/core/approval-voting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ fn sign_approval(
candidate_hash: CandidateHash,
session_index: SessionIndex,
) -> ValidatorSignature {
key.sign(&super::approval_signing_payload(ApprovalVote(candidate_hash), session_index)).into()
key.sign(&ApprovalVote(candidate_hash).signing_payload(session_index)).into()
}

#[derive(Clone)]
Expand Down
4 changes: 0 additions & 4 deletions node/primitives/src/approval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ pub struct IndirectAssignmentCert {
pub cert: AssignmentCert,
}

/// A vote of approval on a candidate.
#[derive(Debug, Clone, Encode, Decode)]
pub struct ApprovalVote(pub CandidateHash);

/// A signed approval vote which references the candidate indirectly via the block.
///
/// In practice, we have a look-up from block hash and candidate index to candidate hash,
Expand Down
16 changes: 16 additions & 0 deletions primitives/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,22 @@ pub struct SessionInfo {
pub needed_approvals: u32,
}

/// A vote of approval on a candidate.
#[derive(Clone, RuntimeDebug)]
pub struct ApprovalVote(pub CandidateHash);

impl ApprovalVote {
/// Yields the signing payload for this approval vote.
pub fn signing_payload(
&self,
session_index: SessionIndex,
) -> Vec<u8> {
const MAGIC: [u8; 4] = *b"APPR";

(MAGIC, &self.0, session_index).encode()
}
}

sp_api::decl_runtime_apis! {
/// The API for querying the state of parachains on-chain.
pub trait ParachainHost<H: Decode = Hash, N: Encode + Decode = BlockNumber> {
Expand Down

0 comments on commit 0451139

Please sign in to comment.