Build block without checking signatures (#4916)

* in executive

* in other places

* to UnsafeResult

* move doc comment

* apply suggestions

* allow validity mocking for TestXt

* add test

* augment checkable instead of another trait

* fix im online test

* blockbuilder dihotomy

* review suggestions

* update test

* Update client/block-builder/src/lib.rs

* updae spec_version

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Nikolay Volf
2020-02-19 01:34:31 +03:00
committed by GitHub
parent ba2362dadd
commit e50f610907
15 changed files with 247 additions and 68 deletions
@@ -39,6 +39,15 @@ pub use self::digest::{
use crate::codec::Encode;
use sp_std::prelude::*;
/// Perform singature check.
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum CheckSignature {
/// Perform.
Yes,
/// Don't perform.
No,
}
fn encode_with_vec_prefix<T: Encode, F: Fn(&mut Vec<u8>)>(encoder: F) -> Vec<u8> {
let size = ::sp_std::mem::size_of::<T>();
let reserve = match size {
@@ -24,7 +24,8 @@ use crate::{
self, Member, MaybeDisplay, SignedExtension, Checkable, Extrinsic, ExtrinsicMetadata,
IdentifyAccount,
},
generic::CheckedExtrinsic, transaction_validity::{TransactionValidityError, InvalidTransaction},
generic::{CheckSignature, CheckedExtrinsic},
transaction_validity::{TransactionValidityError, InvalidTransaction},
};
const TRANSACTION_VERSION: u8 = 4;
@@ -120,18 +121,26 @@ where
{
type Checked = CheckedExtrinsic<AccountId, Call, Extra>;
fn check(self, lookup: &Lookup) -> Result<Self::Checked, TransactionValidityError> {
fn check(self, check_signature: CheckSignature, lookup: &Lookup) -> Result<Self::Checked, TransactionValidityError> {
Ok(match self.signature {
Some((signed, signature, extra)) => {
let signed = lookup.lookup(signed)?;
let raw_payload = SignedPayload::new(self.function, extra)?;
if !raw_payload.using_encoded(|payload| {
signature.verify(payload, &signed)
}) {
return Err(InvalidTransaction::BadProof.into())
}
let (function, extra, _) = raw_payload.deconstruct();
let (function, extra) = if let CheckSignature::No = check_signature {
(self.function, extra)
} else {
let raw_payload = SignedPayload::new(self.function, extra)?;
if !raw_payload.using_encoded(|payload| {
signature.verify(payload, &signed)
}) {
return Err(InvalidTransaction::BadProof.into())
}
let (function, extra, _) = raw_payload.deconstruct();
(function, extra)
};
CheckedExtrinsic {
signed: Some((signed, extra)),
function,
@@ -322,6 +331,7 @@ mod tests {
use sp_io::hashing::blake2_256;
use crate::codec::{Encode, Decode};
use crate::traits::{SignedExtension, IdentifyAccount, IdentityLookup};
use crate::generic::CheckSignature;
use serde::{Serialize, Deserialize};
type TestContext = IdentityLookup<u64>;
@@ -402,7 +412,7 @@ mod tests {
fn unsigned_check_should_work() {
let ux = Ex::new_unsigned(vec![0u8; 0]);
assert!(!ux.is_signed().unwrap_or(false));
assert!(<Ex as Checkable<TestContext>>::check(ux, &Default::default()).is_ok());
assert!(<Ex as Checkable<TestContext>>::check(ux, CheckSignature::Yes, &Default::default()).is_ok());
}
#[test]
@@ -415,7 +425,7 @@ mod tests {
);
assert!(ux.is_signed().unwrap_or(false));
assert_eq!(
<Ex as Checkable<TestContext>>::check(ux, &Default::default()),
<Ex as Checkable<TestContext>>::check(ux, CheckSignature::Yes, &Default::default()),
Err(InvalidTransaction::BadProof.into()),
);
}
@@ -430,7 +440,7 @@ mod tests {
);
assert!(ux.is_signed().unwrap_or(false));
assert_eq!(
<Ex as Checkable<TestContext>>::check(ux, &Default::default()),
<Ex as Checkable<TestContext>>::check(ux, CheckSignature::Yes, &Default::default()),
Ok(CEx { signed: Some((TEST_ACCOUNT, TestExtra)), function: vec![0u8; 0] }),
);
}