Check signatures as "Compact" in statement distribution (#5071)

* allow converting payloads _up_

* convert to superpayload in statement-distribution

* Update primitives/src/v2/signed.rs

Co-authored-by: Andronik <write@reusable.software>

Co-authored-by: Andronik <write@reusable.software>
This commit is contained in:
Robert Habermeier
2022-03-10 14:49:35 -06:00
committed by GitHub
parent 22a7fad75f
commit 8a17c614f0
5 changed files with 109 additions and 27 deletions
+2
View File
@@ -660,6 +660,8 @@ impl From<BitVec<u8, bitvec::order::Lsb0>> for AvailabilityBitfield {
/// A signed compact statement, suitable to be sent to the chain.
pub type SignedStatement = Signed<CompactStatement>;
/// A signed compact statement, with signature not yet checked.
pub type UncheckedSignedStatement = UncheckedSigned<CompactStatement>;
/// A bitfield signed by a particular validator about the availability of pending candidates.
pub type SignedAvailabilityBitfield = Signed<AvailabilityBitfield>;
+24
View File
@@ -148,6 +148,30 @@ impl<Payload: EncodeAs<RealPayload>, RealPayload: Encode> Signed<Payload, RealPa
{
Signed(self.0.unchecked_convert_payload())
}
/// Convert `Payload` into some claimed `SuperPayload` if the encoding matches.
///
/// Succeeds if and only if the super-payload provided actually encodes as
/// the expected payload.
pub fn convert_to_superpayload<SuperPayload>(
self,
claimed: SuperPayload,
) -> Result<Signed<SuperPayload, RealPayload>, (Self, SuperPayload)>
where
SuperPayload: EncodeAs<RealPayload>,
Payload: Encode,
{
if claimed.encode_as() == self.0.payload.encode_as() {
Ok(Signed(UncheckedSigned {
payload: claimed,
validator_index: self.0.validator_index,
signature: self.0.signature,
real_payload: sp_std::marker::PhantomData,
}))
} else {
Err((self, claimed))
}
}
}
// We can't bound this on `Payload: Into<RealPayload>` because that conversion consumes