cargo +nightly fmt (#3540)

* cargo +nightly fmt

* add cargo-fmt check to ci

* update ci

* fmt

* fmt

* skip macro

* ignore bridges
This commit is contained in:
Shawn Tabrizi
2021-08-02 12:47:33 +02:00
committed by GitHub
parent 30e3012270
commit ff5d56fb76
350 changed files with 20617 additions and 21266 deletions
-1
View File
@@ -17,7 +17,6 @@
//! Polkadot types shared between the runtime and the Node-side code.
#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
pub mod v0;
+37 -45
View File
@@ -17,28 +17,26 @@
//! Primitives which are necessary for parachain execution from a relay-chain
//! perspective.
use sp_std::prelude::*;
use sp_std::cmp::Ordering;
use sp_std::{cmp::Ordering, prelude::*};
use parity_scale_codec::{Encode, Decode};
use bitvec::vec::BitVec;
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
use parity_scale_codec::{Decode, Encode};
#[cfg(feature = "std")]
use parity_util_mem::{MallocSizeOf, MallocSizeOfOps};
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use application_crypto::KeyTypeId;
use inherents::InherentIdentifier;
use primitives::RuntimeDebug;
use runtime_primitives::traits::{AppVerify, Block as BlockT};
use inherents::InherentIdentifier;
use application_crypto::KeyTypeId;
pub use runtime_primitives::traits::{BlakeTwo256, Hash as HashT, Verify, IdentifyAccount};
pub use polkadot_core_primitives::*;
pub use parity_scale_codec::Compact;
pub use polkadot_core_primitives::*;
pub use runtime_primitives::traits::{BlakeTwo256, Hash as HashT, IdentifyAccount, Verify};
pub use polkadot_parachain::primitives::{
Id, LOWEST_USER_ID, UpwardMessage, HeadData, BlockData,
ValidationCode,
BlockData, HeadData, Id, UpwardMessage, ValidationCode, LOWEST_USER_ID,
};
/// The key type ID for a collator key.
@@ -180,9 +178,7 @@ pub struct Info {
}
/// An `Info` value for a standard leased parachain.
pub const PARACHAIN_INFO: Info = Info {
scheduling: Scheduling::Always,
};
pub const PARACHAIN_INFO: Info = Info { scheduling: Scheduling::Always };
/// Auxiliary for when there's an attempt to swap two parachains/parathreads.
pub trait SwapAux {
@@ -200,8 +196,12 @@ pub trait SwapAux {
}
impl SwapAux for () {
fn ensure_can_swap(_: Id, _: Id) -> Result<(), &'static str> { Err("Swapping disabled") }
fn on_swap(_: Id, _: Id) -> Result<(), &'static str> { Err("Swapping disabled") }
fn ensure_can_swap(_: Id, _: Id) -> Result<(), &'static str> {
Err("Swapping disabled")
}
fn on_swap(_: Id, _: Id) -> Result<(), &'static str> {
Err("Swapping disabled")
}
}
/// Identifier for a chain, either one of a number of parachains or the relay chain.
@@ -300,7 +300,7 @@ fn check_collator_signature<H: AsRef<[u8]>>(
pov_block_hash: &H,
collator: &CollatorId,
signature: &CollatorSignature,
) -> Result<(),()> {
) -> Result<(), ()> {
let payload = collator_signature_payload(relay_parent, parachain_index, pov_block_hash);
if signature.verify(&payload[..], collator) {
Ok(())
@@ -371,10 +371,7 @@ impl<H: AsRef<[u8]>, N> CandidateReceipt<H, N> {
commitments,
};
let omitted = OmittedValidationData {
global_validation,
local_validation,
};
let omitted = OmittedValidationData { global_validation, local_validation };
(abridged, omitted)
}
@@ -390,7 +387,8 @@ impl Ord for CandidateReceipt {
fn cmp(&self, other: &Self) -> Ordering {
// TODO: compare signatures or something more sane
// https://github.com/paritytech/polkadot/issues/222
self.parachain_index.cmp(&other.parachain_index)
self.parachain_index
.cmp(&other.parachain_index)
.then_with(|| self.head_data.cmp(&other.head_data))
}
}
@@ -439,7 +437,7 @@ pub struct CommitedCandidateReceipt<H = Hash> {
pub descriptor: CandidateDescriptor,
/// The commitments of the candidate receipt.
pub commitments: CandidateCommitments<H>
pub commitments: CandidateCommitments<H>,
}
impl<H: AsRef<[u8]> + Encode> AbridgedCandidateReceipt<H> {
@@ -480,10 +478,7 @@ impl AbridgedCandidateReceipt {
commitments,
} = self;
let OmittedValidationData {
global_validation,
local_validation,
} = omitted;
let OmittedValidationData { global_validation, local_validation } = omitted;
CandidateReceipt {
parachain_index,
@@ -542,7 +537,8 @@ impl Ord for AbridgedCandidateReceipt {
fn cmp(&self, other: &Self) -> Ordering {
// TODO: compare signatures or something more sane
// https://github.com/paritytech/polkadot/issues/222
self.parachain_index.cmp(&other.parachain_index)
self.parachain_index
.cmp(&other.parachain_index)
.then_with(|| self.head_data.cmp(&other.head_data))
}
}
@@ -711,10 +707,12 @@ impl parity_scale_codec::Encode for CompactStatement {
}
impl parity_scale_codec::Decode for CompactStatement {
fn decode<I: parity_scale_codec::Input>(input: &mut I) -> Result<Self, parity_scale_codec::Error> {
fn decode<I: parity_scale_codec::Input>(
input: &mut I,
) -> Result<Self, parity_scale_codec::Error> {
let maybe_magic = <[u8; 4]>::decode(input)?;
if maybe_magic != BACKING_STATEMENT_MAGIC {
return Err(parity_scale_codec::Error::from("invalid magic string"));
return Err(parity_scale_codec::Error::from("invalid magic string"))
}
Ok(match CompactStatementInner::decode(input)? {
@@ -764,14 +762,10 @@ impl ValidityAttestation {
signing_context: &SigningContext<H>,
) -> Vec<u8> {
match *self {
ValidityAttestation::Implicit(_) => (
CompactStatement::Seconded(candidate_hash),
signing_context,
).encode(),
ValidityAttestation::Explicit(_) => (
CompactStatement::Valid(candidate_hash),
signing_context,
).encode(),
ValidityAttestation::Implicit(_) =>
(CompactStatement::Seconded(candidate_hash), signing_context).encode(),
ValidityAttestation::Explicit(_) =>
(CompactStatement::Valid(candidate_hash), signing_context).encode(),
}
}
}
@@ -906,14 +900,15 @@ pub mod fisherman {
/// An `AppCrypto` type to allow submitting signed transactions using the fisherman
/// application key as signer.
pub struct FishermanAppCrypto;
impl frame_system::offchain::AppCrypto<<Signature as Verify>::Signer, Signature> for FishermanAppCrypto {
impl frame_system::offchain::AppCrypto<<Signature as Verify>::Signer, Signature>
for FishermanAppCrypto
{
type RuntimeAppPublic = FishermanId;
type GenericSignature = primitives::sr25519::Signature;
type GenericPublic = primitives::sr25519::Public;
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -932,10 +927,7 @@ mod tests {
let h = Hash::default();
assert_eq!(h.as_ref().len(), 32);
let _payload = collator_signature_payload(
&Hash::repeat_byte(1),
&5u32.into(),
&Hash::repeat_byte(2),
);
let _payload =
collator_signature_payload(&Hash::repeat_byte(1), &5u32.into(), &Hash::repeat_byte(2));
}
}
+92 -86
View File
@@ -16,36 +16,35 @@
//! `V1` Primitives.
use sp_std::prelude::*;
use sp_std::collections::btree_map::BTreeMap;
use parity_scale_codec::{Encode, Decode};
use bitvec::vec::BitVec;
use parity_scale_codec::{Decode, Encode};
use sp_std::{collections::btree_map::BTreeMap, prelude::*};
use application_crypto::KeyTypeId;
use inherents::InherentIdentifier;
use primitives::RuntimeDebug;
use runtime_primitives::traits::{AppVerify, Header as HeaderT};
use inherents::InherentIdentifier;
use sp_arithmetic::traits::{BaseArithmetic, Saturating};
use application_crypto::KeyTypeId;
pub use runtime_primitives::traits::{BlakeTwo256, Hash as HashT};
// Export some core primitives.
pub use polkadot_core_primitives::v1::{
BlockNumber, Moment, Signature, AccountPublic, AccountId, AccountIndex, ChainId, Hash, Nonce,
Balance, Header, Block, BlockId, UncheckedExtrinsic, Remark, DownwardMessage,
InboundDownwardMessage, CandidateHash, InboundHrmpMessage, OutboundHrmpMessage,
AccountId, AccountIndex, AccountPublic, Balance, Block, BlockId, BlockNumber, CandidateHash,
ChainId, DownwardMessage, Hash, Header, InboundDownwardMessage, InboundHrmpMessage, Moment,
Nonce, OutboundHrmpMessage, Remark, Signature, UncheckedExtrinsic,
};
// Export some polkadot-parachain primitives
pub use polkadot_parachain::primitives::{
Id, LOWEST_USER_ID, LOWEST_PUBLIC_ID, HrmpChannelId, UpwardMessage, HeadData, ValidationCode, ValidationCodeHash,
HeadData, HrmpChannelId, Id, UpwardMessage, ValidationCode, ValidationCodeHash,
LOWEST_PUBLIC_ID, LOWEST_USER_ID,
};
// Export some basic parachain primitives from v0.
pub use crate::v0::{
CollatorId, CollatorSignature, PARACHAIN_KEY_TYPE_ID, ValidatorId, ValidatorIndex,
ValidatorSignature, SigningContext, ValidityAttestation,
CompactStatement,
CollatorId, CollatorSignature, CompactStatement, SigningContext, ValidatorId, ValidatorIndex,
ValidatorSignature, ValidityAttestation, PARACHAIN_KEY_TYPE_ID,
};
#[cfg(feature = "std")]
@@ -53,23 +52,23 @@ use parity_util_mem::{MallocSizeOf, MallocSizeOfOps};
// More exports from v0 for std.
#[cfg(feature = "std")]
pub use crate::v0::{ValidatorPair, CollatorPair};
pub use crate::v0::{CollatorPair, ValidatorPair};
pub use sp_staking::SessionIndex;
pub use sp_authority_discovery::AuthorityId as AuthorityDiscoveryId;
pub use sp_consensus_slots::Slot;
pub use sp_staking::SessionIndex;
/// Signed data.
mod signed;
pub use signed::{Signed, UncheckedSigned, EncodeAs};
pub use signed::{EncodeAs, Signed, UncheckedSigned};
/// A declarations of storage keys where an external observer can find some interesting data.
pub mod well_known_keys {
use super::{Id, HrmpChannelId};
use super::{HrmpChannelId, Id};
use hex_literal::hex;
use parity_scale_codec::Encode as _;
use sp_io::hashing::twox_64;
use sp_std::prelude::*;
use parity_scale_codec::Encode as _;
// A note on generating these magic values below:
//
@@ -105,7 +104,8 @@ pub mod well_known_keys {
let prefix = hex!["f5207f03cfdce586301014700e2c2593fad157e461d71fd4c1f936839a5f1f3e"];
para_id.using_encoded(|para_id: &[u8]| {
prefix.as_ref()
prefix
.as_ref()
.iter()
.chain(twox_64(para_id).iter())
.chain(para_id.iter())
@@ -121,7 +121,8 @@ pub mod well_known_keys {
let prefix = hex!["6a0da05ca59913bc38a8630590f2627cb6604cff828a6e3f579ca6c59ace013d"];
channel.using_encoded(|channel: &[u8]| {
prefix.as_ref()
prefix
.as_ref()
.iter()
.chain(twox_64(channel).iter())
.chain(channel.iter())
@@ -137,7 +138,8 @@ pub mod well_known_keys {
let prefix = hex!["6a0da05ca59913bc38a8630590f2627c1d3719f5b0b12c7105c073c507445948"];
para_id.using_encoded(|para_id: &[u8]| {
prefix.as_ref()
prefix
.as_ref()
.iter()
.chain(twox_64(para_id).iter())
.chain(para_id.iter())
@@ -153,7 +155,8 @@ pub mod well_known_keys {
let prefix = hex!["6a0da05ca59913bc38a8630590f2627cf12b746dcf32e843354583c9702cc020"];
para_id.using_encoded(|para_id: &[u8]| {
prefix.as_ref()
prefix
.as_ref()
.iter()
.chain(twox_64(para_id).iter())
.chain(para_id.iter())
@@ -170,7 +173,8 @@ pub mod well_known_keys {
let prefix = hex!["63f78c98723ddc9073523ef3beefda0c4d7fefc408aac59dbfe80a72ac8e3ce5"];
para_id.using_encoded(|para_id: &[u8]| {
prefix.as_ref()
prefix
.as_ref()
.iter()
.chain(twox_64(para_id).iter())
.chain(para_id.iter())
@@ -180,7 +184,6 @@ pub mod well_known_keys {
}
}
/// Unique identifier for the Parachains Inherent
pub const PARACHAINS_INHERENT_IDENTIFIER: InherentIdentifier = *b"parachn0";
@@ -338,7 +341,10 @@ impl<H> CandidateReceipt<H> {
}
/// Computes the blake2-256 hash of the receipt.
pub fn hash(&self) -> CandidateHash where H: Encode {
pub fn hash(&self) -> CandidateHash
where
H: Encode,
{
CandidateHash(BlakeTwo256::hash_of(self))
}
}
@@ -386,12 +392,18 @@ impl<H: Clone> CommittedCandidateReceipt<H> {
///
/// This computes the canonical hash, not the hash of the directly encoded data.
/// Thus this is a shortcut for `candidate.to_plain().hash()`.
pub fn hash(&self) -> CandidateHash where H: Encode {
pub fn hash(&self) -> CandidateHash
where
H: Encode,
{
self.to_plain().hash()
}
/// Does this committed candidate receipt corresponds to the given [`CandidateReceipt`]?
pub fn corresponds_to(&self, receipt: &CandidateReceipt<H>) -> bool where H: PartialEq {
pub fn corresponds_to(&self, receipt: &CandidateReceipt<H>) -> bool
where
H: PartialEq,
{
receipt.descriptor == self.descriptor && receipt.commitments_hash == self.commitments.hash()
}
}
@@ -406,7 +418,9 @@ impl Ord for CommittedCandidateReceipt {
fn cmp(&self, other: &Self) -> sp_std::cmp::Ordering {
// TODO: compare signatures or something more sane
// https://github.com/paritytech/polkadot/issues/222
self.descriptor().para_id.cmp(&other.descriptor().para_id)
self.descriptor()
.para_id
.cmp(&other.descriptor().para_id)
.then_with(|| self.commitments.head_data.cmp(&other.commitments.head_data))
}
}
@@ -475,7 +489,6 @@ impl CandidateCommitments {
}
}
/// A bitfield concerning availability of backed candidates.
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
pub struct AvailabilityBitfield(pub BitVec<bitvec::order::Lsb0, u8>);
@@ -486,7 +499,6 @@ impl From<BitVec<bitvec::order::Lsb0, u8>> for AvailabilityBitfield {
}
}
/// A signed compact statement, suitable to be sent to the chain.
pub type SignedStatement = Signed<CompactStatement>;
@@ -519,12 +531,18 @@ impl<H> BackedCandidate<H> {
}
/// Compute this candidate's hash.
pub fn hash(&self) -> CandidateHash where H: Clone + Encode {
pub fn hash(&self) -> CandidateHash
where
H: Clone + Encode,
{
self.candidate.hash()
}
/// Get this candidate's receipt.
pub fn receipt(&self) -> CandidateReceipt<H> where H: Clone {
pub fn receipt(&self) -> CandidateReceipt<H>
where
H: Clone,
{
self.candidate.to_plain()
}
}
@@ -557,7 +575,10 @@ pub fn check_candidate_backing<H: AsRef<[u8]> + Clone + Encode>(
let hash = backed.candidate.hash();
let mut signed = 0;
for ((val_in_group_idx, _), attestation) in backed.validator_indices.iter().enumerate()
for ((val_in_group_idx, _), attestation) in backed
.validator_indices
.iter()
.enumerate()
.filter(|(_, signed)| **signed)
.zip(backed.validity_votes.iter())
{
@@ -644,8 +665,12 @@ impl GroupRotationInfo {
///
/// `core_index` should be less than `cores`, which is capped at `u32::max()`.
pub fn group_for_core(&self, core_index: CoreIndex, cores: usize) -> GroupIndex {
if self.group_rotation_frequency == 0 { return GroupIndex(core_index.0) }
if cores == 0 { return GroupIndex(0) }
if self.group_rotation_frequency == 0 {
return GroupIndex(core_index.0)
}
if cores == 0 {
return GroupIndex(0)
}
let cores = sp_std::cmp::min(cores, u32::MAX as usize);
let blocks_since_start = self.now.saturating_sub(self.session_start_block);
@@ -662,8 +687,12 @@ impl GroupRotationInfo {
///
/// `core_index` should be less than `cores`, which is capped at `u32::max()`.
pub fn core_for_group(&self, group_index: GroupIndex, cores: usize) -> CoreIndex {
if self.group_rotation_frequency == 0 { return CoreIndex(group_index.0) }
if cores == 0 { return CoreIndex(0) }
if self.group_rotation_frequency == 0 {
return CoreIndex(group_index.0)
}
if cores == 0 {
return CoreIndex(0)
}
let cores = sp_std::cmp::min(cores, u32::MAX as usize);
let blocks_since_start = self.now.saturating_sub(self.session_start_block);
@@ -694,17 +723,15 @@ impl<N: Saturating + BaseArithmetic + Copy> GroupRotationInfo<N> {
/// is 10 and the rotation frequency is 5, this should return 15.
pub fn next_rotation_at(&self) -> N {
let cycle_once = self.now + self.group_rotation_frequency;
cycle_once - (
cycle_once.saturating_sub(self.session_start_block) % self.group_rotation_frequency
)
cycle_once -
(cycle_once.saturating_sub(self.session_start_block) % self.group_rotation_frequency)
}
/// Returns the block number of the last rotation before or including the current block. If the
/// current block is 10 and the rotation frequency is 5, this should return 10.
pub fn last_rotation_at(&self) -> N {
self.now - (
self.now.saturating_sub(self.session_start_block) % self.group_rotation_frequency
)
self.now -
(self.now.saturating_sub(self.session_start_block) % self.group_rotation_frequency)
}
}
@@ -713,7 +740,6 @@ impl<N: Saturating + BaseArithmetic + Copy> GroupRotationInfo<N> {
#[cfg_attr(feature = "std", derive(Debug, PartialEq, MallocSizeOf))]
pub struct OccupiedCore<H = Hash, N = BlockNumber> {
// NOTE: this has no ParaId as it can be deduced from the candidate descriptor.
/// If this core is freed by availability, this is the assignment that is next up on this
/// core, if any. None if there is nothing queued for this core.
pub next_up_on_available: Option<ScheduledCore>,
@@ -861,10 +887,7 @@ 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> {
pub fn signing_payload(&self, session_index: SessionIndex) -> Vec<u8> {
const MAGIC: [u8; 4] = *b"APPR";
(MAGIC, &self.0, session_index).encode()
@@ -1037,14 +1060,14 @@ pub enum ConsensusLog {
/// In practice, these are issued when on-chain logic has detected an
/// invalid parachain block within its own chain, due to a dispute.
#[codec(index = 4)]
Revert(BlockNumber)
Revert(BlockNumber),
}
impl ConsensusLog {
/// Attempt to convert a reference to a generic digest item into a consensus log.
pub fn from_digest_item<H>(digest_item: &runtime_primitives::DigestItem<H>)
-> Result<Option<Self>, parity_scale_codec::Error>
{
pub fn from_digest_item<H>(
digest_item: &runtime_primitives::DigestItem<H>,
) -> Result<Option<Self>, parity_scale_codec::Error> {
match digest_item {
runtime_primitives::DigestItem::Consensus(id, encoded) if id == &POLKADOT_ENGINE_ID =>
Ok(Some(Self::decode(&mut &encoded[..])?)),
@@ -1075,36 +1098,24 @@ pub enum DisputeStatement {
impl DisputeStatement {
/// Get the payload data for this type of dispute statement.
pub fn payload_data(&self, candidate_hash: CandidateHash, session: SessionIndex) -> Vec<u8> {
match *self {
DisputeStatement::Valid(ValidDisputeStatementKind::Explicit) => {
ExplicitDisputeStatement {
valid: true,
candidate_hash,
session,
}.signing_payload()
},
DisputeStatement::Valid(ValidDisputeStatementKind::BackingSeconded(inclusion_parent)) => {
CompactStatement::Seconded(candidate_hash).signing_payload(&SigningContext {
session_index: session,
parent_hash: inclusion_parent,
})
},
DisputeStatement::Valid(ValidDisputeStatementKind::BackingValid(inclusion_parent)) => {
match *self {
DisputeStatement::Valid(ValidDisputeStatementKind::Explicit) =>
ExplicitDisputeStatement { valid: true, candidate_hash, session }.signing_payload(),
DisputeStatement::Valid(ValidDisputeStatementKind::BackingSeconded(
inclusion_parent,
)) => CompactStatement::Seconded(candidate_hash).signing_payload(&SigningContext {
session_index: session,
parent_hash: inclusion_parent,
}),
DisputeStatement::Valid(ValidDisputeStatementKind::BackingValid(inclusion_parent)) =>
CompactStatement::Valid(candidate_hash).signing_payload(&SigningContext {
session_index: session,
parent_hash: inclusion_parent,
})
},
DisputeStatement::Valid(ValidDisputeStatementKind::ApprovalChecking) => {
ApprovalVote(candidate_hash).signing_payload(session)
},
DisputeStatement::Invalid(InvalidDisputeStatementKind::Explicit) => {
ExplicitDisputeStatement {
valid: false,
candidate_hash,
session,
}.signing_payload()
},
}),
DisputeStatement::Valid(ValidDisputeStatementKind::ApprovalChecking) =>
ApprovalVote(candidate_hash).signing_payload(session),
DisputeStatement::Invalid(InvalidDisputeStatementKind::Explicit) =>
ExplicitDisputeStatement { valid: false, candidate_hash, session }.signing_payload(),
}
}
@@ -1118,7 +1129,7 @@ impl DisputeStatement {
) -> Result<(), ()> {
let payload = self.payload_data(candidate_hash, session);
if validator_signature.verify(&payload[..] , &validator_public) {
if validator_signature.verify(&payload[..], &validator_public) {
Ok(())
} else {
Err(())
@@ -1246,11 +1257,8 @@ mod tests {
#[test]
fn group_rotation_info_calculations() {
let info = GroupRotationInfo {
session_start_block: 10u32,
now: 15,
group_rotation_frequency: 5,
};
let info =
GroupRotationInfo { session_start_block: 10u32, now: 15, group_rotation_frequency: 5 };
assert_eq!(info.next_rotation_at(), 20);
assert_eq!(info.last_rotation_at(), 15);
@@ -1258,7 +1266,6 @@ mod tests {
#[test]
fn group_for_core_is_core_for_group() {
for cores in 1..=256 {
for rotations in 0..(cores * 2) {
let info = GroupRotationInfo {
@@ -1273,7 +1280,6 @@ mod tests {
}
}
}
}
#[test]
+34 -27
View File
@@ -14,20 +14,20 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use parity_scale_codec::{Encode, Decode};
use parity_scale_codec::{Decode, Encode};
use sp_std::prelude::Vec;
#[cfg(feature = "std")]
use sp_std::convert::TryInto;
#[cfg(feature = "std")]
use application_crypto::AppKey;
#[cfg(feature = "std")]
use sp_keystore::{CryptoStore, SyncCryptoStorePtr, Error as KeystoreError};
use sp_keystore::{CryptoStore, Error as KeystoreError, SyncCryptoStorePtr};
#[cfg(feature = "std")]
use sp_std::convert::TryInto;
use sp_std::prelude::Vec;
use primitives::RuntimeDebug;
use runtime_primitives::traits::AppVerify;
use crate::v0::{SigningContext, ValidatorId, ValidatorSignature, ValidatorIndex};
use crate::v0::{SigningContext, ValidatorId, ValidatorIndex, ValidatorSignature};
/// Signed data with signature already verified.
///
@@ -95,7 +95,7 @@ impl<Payload: EncodeAs<RealPayload>, RealPayload: Encode> Signed<Payload, RealPa
pub fn try_from_unchecked<H: Encode>(
unchecked: UncheckedSigned<Payload, RealPayload>,
context: &SigningContext<H>,
key: &ValidatorId
key: &ValidatorId,
) -> Result<Self, UncheckedSigned<Payload, RealPayload>> {
if unchecked.check_signature(context, key).is_ok() {
Ok(Self(unchecked))
@@ -134,7 +134,10 @@ impl<Payload: EncodeAs<RealPayload>, RealPayload: Encode> Signed<Payload, RealPa
}
/// Convert `Payload` into `RealPayload`.
pub fn convert_payload(&self) -> Signed<RealPayload> where for<'a> &'a Payload: Into<RealPayload> {
pub fn convert_payload(&self) -> Signed<RealPayload>
where
for<'a> &'a Payload: Into<RealPayload>,
{
Signed(self.0.unchecked_convert_payload())
}
}
@@ -153,19 +156,14 @@ impl<Payload: EncodeAs<RealPayload>, RealPayload: Encode> UncheckedSigned<Payloa
validator_index: ValidatorIndex,
signature: ValidatorSignature,
) -> Self {
Self {
payload,
validator_index,
signature,
real_payload: std::marker::PhantomData,
}
Self { payload, validator_index, signature, real_payload: std::marker::PhantomData }
}
/// Check signature and convert to `Signed` if successful.
pub fn try_into_checked<H: Encode>(
self,
context: &SigningContext<H>,
key: &ValidatorId
key: &ValidatorId,
) -> Result<Signed<Payload, RealPayload>, Self> {
Signed::try_from_unchecked(self, context, key)
}
@@ -195,7 +193,10 @@ impl<Payload: EncodeAs<RealPayload>, RealPayload: Encode> UncheckedSigned<Payloa
}
/// Convert `Payload` into `RealPayload`.
pub fn unchecked_convert_payload(&self) -> UncheckedSigned<RealPayload> where for<'a> &'a Payload: Into<RealPayload> {
pub fn unchecked_convert_payload(&self) -> UncheckedSigned<RealPayload>
where
for<'a> &'a Payload: Into<RealPayload>,
{
UncheckedSigned {
signature: self.signature.clone(),
validator_index: self.validator_index,
@@ -221,15 +222,12 @@ impl<Payload: EncodeAs<RealPayload>, RealPayload: Encode> UncheckedSigned<Payloa
key: &ValidatorId,
) -> Result<Option<Self>, KeystoreError> {
let data = Self::payload_data(&payload, context);
let signature = CryptoStore::sign_with(
&**keystore,
ValidatorId::ID,
&key.into(),
&data,
).await?;
let signature =
CryptoStore::sign_with(&**keystore, ValidatorId::ID, &key.into(), &data).await?;
let signature = match signature {
Some(sig) => sig.try_into().map_err(|_| KeystoreError::KeyNotSupported(ValidatorId::ID))?,
Some(sig) =>
sig.try_into().map_err(|_| KeystoreError::KeyNotSupported(ValidatorId::ID))?,
None => return Ok(None),
};
@@ -242,14 +240,23 @@ impl<Payload: EncodeAs<RealPayload>, RealPayload: Encode> UncheckedSigned<Payloa
}
/// Validate the payload given the context and public key.
fn check_signature<H: Encode>(&self, context: &SigningContext<H>, key: &ValidatorId) -> Result<(), ()> {
fn check_signature<H: Encode>(
&self,
context: &SigningContext<H>,
key: &ValidatorId,
) -> Result<(), ()> {
let data = Self::payload_data(&self.payload, context);
if self.signature.verify(data.as_slice(), key) { Ok(()) } else { Err(()) }
if self.signature.verify(data.as_slice(), key) {
Ok(())
} else {
Err(())
}
}
}
impl<Payload, RealPayload> From<Signed<Payload, RealPayload>> for UncheckedSigned<Payload, RealPayload> {
impl<Payload, RealPayload> From<Signed<Payload, RealPayload>>
for UncheckedSigned<Payload, RealPayload>
{
fn from(signed: Signed<Payload, RealPayload>) -> Self {
signed.0
}