Skip to content
This repository was archived by the owner on Jul 4, 2022. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Merge/test fix
  • Loading branch information
jordy25519 committed Sep 25, 2020
commit e9ed13eebb744cab04fb850af05843568c864078
3 changes: 2 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion client/transaction-pool/src/testing/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ fn pruning_a_transaction_should_remove_it_from_best_transaction() {
let xt1 = Extrinsic::IncludeData(Vec::new());

block_on(pool.submit_one(&BlockId::number(0), SOURCE, xt1.clone())).expect("1. Imported");
let header = pool.api.push_block(1, vec![xt1.clone()]);
let header = pool.api.push_block(1, vec![xt1.clone()], true);

// This will prune `xt1`.
block_on(pool.maintain(block_event(header)));
Expand Down
3 changes: 1 addition & 2 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,12 @@ pub enum Never {}
///
/// # Examples
///
/// ```
/// ```no_compile
/// # use frame_support::traits::Get;
/// # use frame_support::parameter_types;
/// // This function cannot be used in a const context.
/// fn non_const_expression() -> u64 { 99 }
///
/// ```no_compile
/// parameter_types! {
/// pub const Argument: u64 = 42 + FIXED_VALUE;
/// /// Visibility of the type is optional
Expand Down
5 changes: 4 additions & 1 deletion frame/support/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ frame-support = { version = "2.0.0-alpha.5", default-features = false, path = ".
sp-inherents = { version = "2.0.0-alpha.5", default-features = false, path = "../../../primitives/inherents" }
sp-runtime = { version = "2.0.0-alpha.5", default-features = false, path = "../../../primitives/runtime" }
sp-core = { version = "2.0.0-alpha.5", default-features = false, path = "../../../primitives/core" }
trybuild = "1.0.17"
trybuild = "1.0.24"
pretty_assertions = "0.6.1"
# pin ed25519-dalek to 1.0.0-pre.3
# otherwise cargo will resolve 1.0.0-pre.4 which is breaking.
ed25519-dalek = "=1.0.0-pre.3"

[features]
default = ["std"]
Expand Down
2 changes: 1 addition & 1 deletion frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ mod tests {
pub const MaximumBlockWeight: Weight = 1024;
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
pub const MaximumBlockLength: u32 = 1024;
pub const Version: RuntimeVersion = RuntimeVersion {
pub Version: RuntimeVersion = RuntimeVersion {
spec_name: sp_version::create_runtime_str!("test"),
impl_name: sp_version::create_runtime_str!("system-test"),
authoring_version: 1,
Expand Down
36 changes: 33 additions & 3 deletions primitives/arithmetic/src/per_things.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,28 @@ macro_rules! implement_per_thing {
///
#[doc = $title]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Encode, Decode, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, RuntimeDebug, CompactAs)]
#[derive(Encode, Decode, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, RuntimeDebug)]
pub struct $name($type);

/// Implementation makes any compact encoding of `PerThing::Inner` valid,
/// when decoding it will saturate up to `PerThing::ACCURACY`.
impl CompactAs for $name {
type As = $type;
fn encode_as(&self) -> &Self::As {
&self.0
}
fn decode_from(x: Self::As) -> Self {
// Saturates if `x` is more than `$max` internally.
Self::from_parts(x)
}
}

impl From<codec::Compact<$name>> for $name {
fn from(x: codec::Compact<$name>) -> $name {
x.0
}
}

impl PerThing for $name {
type Inner = $type;
type Upper = $upper_type;
Expand Down Expand Up @@ -261,8 +280,8 @@ macro_rules! implement_per_thing {
}

/// See [`PerThing::one`].
pub fn one() -> Self {
<Self as PerThing>::one()
pub const fn one() -> Self {
Self::from_parts($max)
}

/// See [`PerThing::zero`].
Expand Down Expand Up @@ -714,6 +733,17 @@ macro_rules! implement_per_thing {
);
}

#[test]
fn compact_decoding_saturate_when_beyond_accuracy() {
use num_traits::Bounded;
use codec::Compact;

let p = Compact::<$name>::decode(&mut &Compact(<$type>::max_value()).encode()[..])
.unwrap();
assert_eq!((p.0).0, $max);
assert_eq!($name::from(p), $name::max_value());
}

#[test]
fn per_things_div_works() {
// normal
Expand Down
4 changes: 4 additions & 0 deletions primitives/phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ pub fn elect<AccountId, Balance, C, R>(
voters.extend(initial_voters.into_iter().map(|(who, voter_stake, votes)| {
let mut edges: Vec<Edge<AccountId>> = Vec::with_capacity(votes.len());
for v in votes {
if edges.iter().any(|e| e.who == v) {
// duplicate edge.
continue;
}
if let Some(idx) = c_idx_cache.get(&v) {
// This candidate is valid + already cached.
candidates[*idx].approval_stake = candidates[*idx].approval_stake
Expand Down
60 changes: 60 additions & 0 deletions primitives/phragmen/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,63 @@ fn self_votes_should_be_kept() {
&Support { total: 20u128, voters: vec![(20u64, 20u128)] },
);
}

#[test]
fn duplicate_target_is_ignored() {
let candidates = vec![1, 2, 3];
let voters = vec![
(10, 100, vec![1, 1, 2, 3]),
(20, 100, vec![2, 3]),
(30, 50, vec![1, 1, 2]),
];

let PhragmenResult { winners, assignments } = elect::<_, _, TestCurrencyToVote, Output>(
2,
2,
candidates,
voters,
).unwrap();
let (winners, _): (Vec<i32>, Vec<_>) = winners.into_iter().unzip();

assert_eq!(winners, vec![(2), (3)]);
assert_eq!(
assignments
.into_iter()
.map(|x| (x.0, x.1.into_iter().map(|(w, _)| w).collect::<Vec<_>>()))
.collect::<Vec<_>>(),
vec![
(10, vec![2, 3]),
(20, vec![2, 3]),
(30, vec![2]),
],
);
}

#[test]
fn duplicate_target_is_ignored_when_winner() {
let candidates = vec![1, 2, 3];
let voters = vec![
(10, 100, vec![1, 1, 2, 3]),
(20, 100, vec![1, 2]),
];

let PhragmenResult { winners, assignments } = elect::<_, _, TestCurrencyToVote, Output>(
2,
2,
candidates,
voters,
).unwrap();
let (winners, _): (Vec<i32>, Vec<_>) = winners.into_iter().unzip();

assert_eq!(winners, vec![1, 2]);
assert_eq!(
assignments
.into_iter()
.map(|x| (x.0, x.1.into_iter().map(|(w, _)| w).collect::<Vec<_>>()))
.collect::<Vec<_>>(),
vec![
(10, vec![1, 2]),
(20, vec![1, 2]),
],
);
}
35 changes: 33 additions & 2 deletions primitives/trie/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod storage_proof;
mod trie_stream;

use sp_std::boxed::Box;
use sp_std::borrow::Borrow;
use sp_std::marker::PhantomData;
use sp_std::vec::Vec;
use hash_db::{Hasher, Prefix};
Expand Down Expand Up @@ -257,7 +258,7 @@ pub fn child_delta_trie_root<L: TrieConfiguration, I, A, B, DB, RD>(
root.as_mut().copy_from_slice(root_data.as_ref());

let mut db = KeySpacedDBMut::new(&mut *db, keyspace);
delta_trie_root::<L, _, _, _, _, _>(
delta_trie_root::<L, _, _, _, _>(
&mut db,
root,
delta,
Expand Down Expand Up @@ -464,7 +465,7 @@ mod trie_constants {
#[cfg(test)]
mod tests {
use super::*;
use codec::{Encode, Compact};
use codec::{Encode, Decode, Compact};
use sp_core::Blake2Hasher;
use hash_db::{HashDB, Hasher};
use trie_db::{DBValue, TrieMut, Trie, NodeCodec as NodeCodecT};
Expand Down Expand Up @@ -852,4 +853,34 @@ mod tests {
).is_err()
);
}

#[test]
fn generate_storage_root_with_proof_works_independently_from_the_delta_order() {
let proof = StorageProof::decode(&mut &include_bytes!("../test-res/proof")[..]).unwrap();
let storage_root = sp_core::H256::decode(
&mut &include_bytes!("../test-res/storage_root")[..],
).unwrap();
// Delta order that is "invalid" so that it would require a different proof.
let invalid_delta = Vec::<(Vec<u8>, Option<Vec<u8>>)>::decode(
&mut &include_bytes!("../test-res/invalid-delta-order")[..],
).unwrap();
// Delta order that is "valid"
let valid_delta = Vec::<(Vec<u8>, Option<Vec<u8>>)>::decode(
&mut &include_bytes!("../test-res/valid-delta-order")[..],
).unwrap();

let proof_db = proof.into_memory_db::<Blake2Hasher>();
let first_storage_root = delta_trie_root::<Layout, _, _, _, _>(
&mut proof_db.clone(),
storage_root,
valid_delta,
).unwrap();
let second_storage_root = delta_trie_root::<Layout, _, _, _, _>(
&mut proof_db.clone(),
storage_root,
invalid_delta,
).unwrap();

assert_eq!(first_storage_root, second_storage_root);
}
}
Binary file added primitives/trie/test-res/invalid-delta-order
Binary file not shown.
Binary file added primitives/trie/test-res/proof
Binary file not shown.
1 change: 1 addition & 0 deletions primitives/trie/test-res/storage_root
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�=�[�42%�JP ��h�Kw�)R� 0�ԭu��
Binary file added primitives/trie/test-res/valid-delta-order
Binary file not shown.