Skip to content

Commit

Permalink
Feature/update extrinsic to the latest (paritytech#269)
Browse files Browse the repository at this point in the history
* Update extrinsic to the latest

* Update tests accordingly

* Update wasm
  • Loading branch information
liuchengxu authored Feb 12, 2019
1 parent 4624736 commit 69cc909
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 54 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions runtime/wasm/Cargo.lock

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

Binary file not shown.
2 changes: 2 additions & 0 deletions xr-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ integer-sqrt = "0.1.2"
num-traits = { version = "0.2", default-features = false }
parity-codec = { version = "3.0", default-features = false }

sr-io = { git = "https://github.com/chainpool/substrate", default_features = false }
sr-std = { git = "https://github.com/chainpool/substrate", default_features = false }
sr-primitives = { git = "https://github.com/chainpool/substrate", default_features = false }
srml-support = { git = "https://github.com/chainpool/substrate", default_features = false }
Expand All @@ -23,6 +24,7 @@ std = [
"serde",
"num-traits/std",
"parity-codec/std",
"sr-io/std",
"sr-std/std",
"sr-primitives/std",
"srml-support/std"
Expand Down
60 changes: 33 additions & 27 deletions xr-primitives/src/generic/unchecked_mortal_compact_extrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ use std::fmt;
use parity_codec::{Compact, Decode, Encode, Input};

use rstd::prelude::*;
use runtime_io::blake2_256;
use runtime_primitives::{
generic::Era,
traits::{
self, BlockNumberToHash, Checkable, CurrentHeight, Extrinsic, Lookup, MaybeDisplay, Member,
SimpleArithmetic,
},
verify_encoded_lazy,
};

use super::checked_extrinsic::CheckedExtrinsic;
Expand Down Expand Up @@ -117,14 +117,20 @@ where
era.birth(context.current_height().as_()),
))
.ok_or("transaction birth block ancient")?;
let payload = (index, self.function, era, h, acceleration);
let raw_payload = (index, self.function, era, h, acceleration);
let signed = context.lookup(signed)?;
if !verify_encoded_lazy(&signature, &payload, &signed) {
if !raw_payload.using_encoded(|payload| {
if payload.len() > 256 {
signature.verify(&blake2_256(payload)[..], &signed)
} else {
signature.verify(payload, &signed)
}
}) {
return Err("bad signature in extrinsic");
}
CheckedExtrinsic {
signed: Some((signed, (payload.0).0, (payload.4).0)),
function: payload.1,
signed: Some((signed, (raw_payload.0).0, (raw_payload.4).0)),
function: raw_payload.1,
}
}
None => CheckedExtrinsic {
Expand Down Expand Up @@ -236,6 +242,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use runtime_io::blake2_256;

struct TestContext;
impl Lookup for TestContext {
Expand Down Expand Up @@ -268,15 +275,14 @@ mod tests {
}
}

const DUMMY_FUNCTION: u64 = 0;
const DUMMY_ACCOUNTID: u64 = 0;

type Ex = UncheckedMortalCompactExtrinsic<u64, u64, u64, TestSig, u32>;
type CEx = CheckedExtrinsic<u64, u64, u64, u32>;
type Ex = UncheckedMortalCompactExtrinsic<u64, u64, Vec<u8>, TestSig, u32>;
type CEx = CheckedExtrinsic<u64, u64, Vec<u8>, u32>;

#[test]
fn unsigned_codec_should_work() {
let ux = Ex::new_unsigned(DUMMY_FUNCTION);
let ux = Ex::new_unsigned(vec![0u8; 0]);
let encoded = ux.encode();
assert_eq!(Ex::decode(&mut &encoded[..]), Some(ux));
}
Expand All @@ -285,13 +291,13 @@ mod tests {
fn signed_codec_should_work() {
let ux = Ex::new_signed(
0,
DUMMY_FUNCTION,
vec![0u8; 0],
DUMMY_ACCOUNTID,
TestSig(
DUMMY_ACCOUNTID,
(
DUMMY_ACCOUNTID,
DUMMY_FUNCTION,
vec![0u8; 0],
Era::immortal(),
0u64,
Compact::<u32>::from(1),
Expand All @@ -307,7 +313,7 @@ mod tests {

#[test]
fn unsigned_check_should_work() {
let ux = Ex::new_unsigned(DUMMY_FUNCTION);
let ux = Ex::new_unsigned(vec![0u8; 0]);
assert!(!ux.is_signed().unwrap_or(false));
assert!(<Ex as Checkable<TestContext>>::check(ux, &TestContext).is_ok());
}
Expand All @@ -316,7 +322,7 @@ mod tests {
fn badly_signed_check_should_fail() {
let ux = Ex::new_signed(
0,
DUMMY_FUNCTION,
vec![0u8; 0],
DUMMY_ACCOUNTID,
TestSig(DUMMY_ACCOUNTID, vec![0u8]),
Era::immortal(),
Expand All @@ -333,13 +339,13 @@ mod tests {
fn immortal_signed_check_should_work() {
let ux = Ex::new_signed(
0,
DUMMY_FUNCTION,
vec![0u8; 0],
DUMMY_ACCOUNTID,
TestSig(
DUMMY_ACCOUNTID,
(
Compact::from(DUMMY_ACCOUNTID),
DUMMY_FUNCTION,
vec![0u8; 0],
Era::immortal(),
0u64,
Compact::<u32>::from(1),
Expand All @@ -354,7 +360,7 @@ mod tests {
<Ex as Checkable<TestContext>>::check(ux, &TestContext),
Ok(CEx {
signed: Some((DUMMY_ACCOUNTID, 0, 1)),
function: DUMMY_FUNCTION
function: vec![0u8; 0]
})
);
}
Expand All @@ -363,13 +369,13 @@ mod tests {
fn mortal_signed_check_should_work() {
let ux = Ex::new_signed(
0,
DUMMY_FUNCTION,
vec![0u8; 0],
DUMMY_ACCOUNTID,
TestSig(
DUMMY_ACCOUNTID,
(
Compact::from(DUMMY_ACCOUNTID),
DUMMY_FUNCTION,
vec![0u8; 0],
Era::mortal(32, 42),
42u64,
Compact::<u32>::from(1),
Expand All @@ -384,7 +390,7 @@ mod tests {
<Ex as Checkable<TestContext>>::check(ux, &TestContext),
Ok(CEx {
signed: Some((DUMMY_ACCOUNTID, 0, 1)),
function: DUMMY_FUNCTION
function: vec![0u8; 0]
})
);
}
Expand All @@ -393,13 +399,13 @@ mod tests {
fn later_mortal_signed_check_should_work() {
let ux = Ex::new_signed(
0,
DUMMY_FUNCTION,
vec![0u8; 0],
DUMMY_ACCOUNTID,
TestSig(
DUMMY_ACCOUNTID,
(
Compact::from(DUMMY_ACCOUNTID),
DUMMY_FUNCTION,
vec![0u8; 0],
Era::mortal(32, 11),
11u64,
Compact::<u32>::from(1),
Expand All @@ -414,7 +420,7 @@ mod tests {
<Ex as Checkable<TestContext>>::check(ux, &TestContext),
Ok(CEx {
signed: Some((DUMMY_ACCOUNTID, 0, 1)),
function: DUMMY_FUNCTION
function: vec![0u8; 0]
})
);
}
Expand All @@ -423,13 +429,13 @@ mod tests {
fn too_late_mortal_signed_check_should_fail() {
let ux = Ex::new_signed(
0,
DUMMY_FUNCTION,
vec![0u8; 0],
DUMMY_ACCOUNTID,
TestSig(
DUMMY_ACCOUNTID,
(
DUMMY_ACCOUNTID,
DUMMY_FUNCTION,
vec![0u8; 0],
Era::mortal(32, 10),
10u64,
Compact::<u32>::from(1),
Expand All @@ -450,13 +456,13 @@ mod tests {
fn too_early_mortal_signed_check_should_fail() {
let ux = Ex::new_signed(
0,
DUMMY_FUNCTION,
vec![0u8; 0],
DUMMY_ACCOUNTID,
TestSig(
DUMMY_ACCOUNTID,
(
DUMMY_ACCOUNTID,
DUMMY_FUNCTION,
vec![0u8; 0],
Era::mortal(32, 43),
43u64,
Compact::<u32>::from(1),
Expand All @@ -475,7 +481,7 @@ mod tests {

#[test]
fn encoding_matches_vec() {
let ex = Ex::new_unsigned(DUMMY_FUNCTION);
let ex = Ex::new_unsigned(vec![0u8; 0]);
let encoded = ex.encode();
let decoded = Ex::decode(&mut encoded.as_slice()).unwrap();
assert_eq!(decoded, ex);
Expand Down
Loading

0 comments on commit 69cc909

Please sign in to comment.