Skip to content
This repository was archived by the owner on Jul 4, 2022. It is now read-only.

Commit dcad810

Browse files
committed
Merge branch 'chore/upstream-cherry-picks' of ssh://github.com/plugblockchain/plug-blockchain into chore/upstream-cherry-picks
2 parents b2c01dd + 636c2be commit dcad810

File tree

12 files changed

+139
-10
lines changed

12 files changed

+139
-10
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frame/support/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,12 @@ pub enum Never {}
102102
///
103103
/// # Examples
104104
///
105-
/// ```
105+
/// ```no_compile
106106
/// # use frame_support::traits::Get;
107107
/// # use frame_support::parameter_types;
108108
/// // This function cannot be used in a const context.
109109
/// fn non_const_expression() -> u64 { 99 }
110110
///
111-
/// ```no_compile
112111
/// parameter_types! {
113112
/// pub const Argument: u64 = 42 + FIXED_VALUE;
114113
/// /// Visibility of the type is optional

frame/support/test/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ frame-support = { version = "2.0.0-alpha.5", default-features = false, path = ".
1717
sp-inherents = { version = "2.0.0-alpha.5", default-features = false, path = "../../../primitives/inherents" }
1818
sp-runtime = { version = "2.0.0-alpha.5", default-features = false, path = "../../../primitives/runtime" }
1919
sp-core = { version = "2.0.0-alpha.5", default-features = false, path = "../../../primitives/core" }
20-
trybuild = "1.0.17"
20+
trybuild = "1.0.24"
2121
pretty_assertions = "0.6.1"
22+
# pin ed25519-dalek to 1.0.0-pre.3
23+
# otherwise cargo will resolve 1.0.0-pre.4 which is breaking.
24+
ed25519-dalek = "=1.0.0-pre.3"
2225

2326
[features]
2427
default = ["std"]

frame/system/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1380,7 +1380,7 @@ mod tests {
13801380
pub const MaximumBlockWeight: Weight = 1024;
13811381
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
13821382
pub const MaximumBlockLength: u32 = 1024;
1383-
pub const Version: RuntimeVersion = RuntimeVersion {
1383+
pub Version: RuntimeVersion = RuntimeVersion {
13841384
spec_name: sp_version::create_runtime_str!("test"),
13851385
impl_name: sp_version::create_runtime_str!("system-test"),
13861386
authoring_version: 1,

primitives/arithmetic/src/per_things.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,28 @@ macro_rules! implement_per_thing {
121121
///
122122
#[doc = $title]
123123
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
124-
#[derive(Encode, Decode, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, RuntimeDebug, CompactAs)]
124+
#[derive(Encode, Decode, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, RuntimeDebug)]
125125
pub struct $name($type);
126126

127+
/// Implementation makes any compact encoding of `PerThing::Inner` valid,
128+
/// when decoding it will saturate up to `PerThing::ACCURACY`.
129+
impl CompactAs for $name {
130+
type As = $type;
131+
fn encode_as(&self) -> &Self::As {
132+
&self.0
133+
}
134+
fn decode_from(x: Self::As) -> Self {
135+
// Saturates if `x` is more than `$max` internally.
136+
Self::from_parts(x)
137+
}
138+
}
139+
140+
impl From<codec::Compact<$name>> for $name {
141+
fn from(x: codec::Compact<$name>) -> $name {
142+
x.0
143+
}
144+
}
145+
127146
impl PerThing for $name {
128147
type Inner = $type;
129148
type Upper = $upper_type;
@@ -261,8 +280,8 @@ macro_rules! implement_per_thing {
261280
}
262281

263282
/// See [`PerThing::one`].
264-
pub fn one() -> Self {
265-
<Self as PerThing>::one()
283+
pub const fn one() -> Self {
284+
Self::from_parts($max)
266285
}
267286

268287
/// See [`PerThing::zero`].
@@ -714,6 +733,17 @@ macro_rules! implement_per_thing {
714733
);
715734
}
716735

736+
#[test]
737+
fn compact_decoding_saturate_when_beyond_accuracy() {
738+
use num_traits::Bounded;
739+
use codec::Compact;
740+
741+
let p = Compact::<$name>::decode(&mut &Compact(<$type>::max_value()).encode()[..])
742+
.unwrap();
743+
assert_eq!((p.0).0, $max);
744+
assert_eq!($name::from(p), $name::max_value());
745+
}
746+
717747
#[test]
718748
fn per_things_div_works() {
719749
// normal

primitives/phragmen/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ pub fn elect<AccountId, Balance, C, R>(
192192
voters.extend(initial_voters.into_iter().map(|(who, voter_stake, votes)| {
193193
let mut edges: Vec<Edge<AccountId>> = Vec::with_capacity(votes.len());
194194
for v in votes {
195+
if edges.iter().any(|e| e.who == v) {
196+
// duplicate edge.
197+
continue;
198+
}
195199
if let Some(idx) = c_idx_cache.get(&v) {
196200
// This candidate is valid + already cached.
197201
candidates[*idx].approval_stake = candidates[*idx].approval_stake

primitives/phragmen/src/tests.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,63 @@ fn self_votes_should_be_kept() {
486486
&Support { total: 20u128, voters: vec![(20u64, 20u128)] },
487487
);
488488
}
489+
490+
#[test]
491+
fn duplicate_target_is_ignored() {
492+
let candidates = vec![1, 2, 3];
493+
let voters = vec![
494+
(10, 100, vec![1, 1, 2, 3]),
495+
(20, 100, vec![2, 3]),
496+
(30, 50, vec![1, 1, 2]),
497+
];
498+
499+
let PhragmenResult { winners, assignments } = elect::<_, _, TestCurrencyToVote, Output>(
500+
2,
501+
2,
502+
candidates,
503+
voters,
504+
).unwrap();
505+
let (winners, _): (Vec<i32>, Vec<_>) = winners.into_iter().unzip();
506+
507+
assert_eq!(winners, vec![(2), (3)]);
508+
assert_eq!(
509+
assignments
510+
.into_iter()
511+
.map(|x| (x.0, x.1.into_iter().map(|(w, _)| w).collect::<Vec<_>>()))
512+
.collect::<Vec<_>>(),
513+
vec![
514+
(10, vec![2, 3]),
515+
(20, vec![2, 3]),
516+
(30, vec![2]),
517+
],
518+
);
519+
}
520+
521+
#[test]
522+
fn duplicate_target_is_ignored_when_winner() {
523+
let candidates = vec![1, 2, 3];
524+
let voters = vec![
525+
(10, 100, vec![1, 1, 2, 3]),
526+
(20, 100, vec![1, 2]),
527+
];
528+
529+
let PhragmenResult { winners, assignments } = elect::<_, _, TestCurrencyToVote, Output>(
530+
2,
531+
2,
532+
candidates,
533+
voters,
534+
).unwrap();
535+
let (winners, _): (Vec<i32>, Vec<_>) = winners.into_iter().unzip();
536+
537+
assert_eq!(winners, vec![1, 2]);
538+
assert_eq!(
539+
assignments
540+
.into_iter()
541+
.map(|x| (x.0, x.1.into_iter().map(|(w, _)| w).collect::<Vec<_>>()))
542+
.collect::<Vec<_>>(),
543+
vec![
544+
(10, vec![1, 2]),
545+
(20, vec![1, 2]),
546+
],
547+
);
548+
}

primitives/trie/src/lib.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod storage_proof;
2525
mod trie_stream;
2626

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

259260
let mut db = KeySpacedDBMut::new(&mut *db, keyspace);
260-
delta_trie_root::<L, _, _, _, _, _>(
261+
delta_trie_root::<L, _, _, _, _>(
261262
&mut db,
262263
root,
263264
delta,
@@ -464,7 +465,7 @@ mod trie_constants {
464465
#[cfg(test)]
465466
mod tests {
466467
use super::*;
467-
use codec::{Encode, Compact};
468+
use codec::{Encode, Decode, Compact};
468469
use sp_core::Blake2Hasher;
469470
use hash_db::{HashDB, Hasher};
470471
use trie_db::{DBValue, TrieMut, Trie, NodeCodec as NodeCodecT};
@@ -852,4 +853,34 @@ mod tests {
852853
).is_err()
853854
);
854855
}
856+
857+
#[test]
858+
fn generate_storage_root_with_proof_works_independently_from_the_delta_order() {
859+
let proof = StorageProof::decode(&mut &include_bytes!("../test-res/proof")[..]).unwrap();
860+
let storage_root = sp_core::H256::decode(
861+
&mut &include_bytes!("../test-res/storage_root")[..],
862+
).unwrap();
863+
// Delta order that is "invalid" so that it would require a different proof.
864+
let invalid_delta = Vec::<(Vec<u8>, Option<Vec<u8>>)>::decode(
865+
&mut &include_bytes!("../test-res/invalid-delta-order")[..],
866+
).unwrap();
867+
// Delta order that is "valid"
868+
let valid_delta = Vec::<(Vec<u8>, Option<Vec<u8>>)>::decode(
869+
&mut &include_bytes!("../test-res/valid-delta-order")[..],
870+
).unwrap();
871+
872+
let proof_db = proof.into_memory_db::<Blake2Hasher>();
873+
let first_storage_root = delta_trie_root::<Layout, _, _, _, _>(
874+
&mut proof_db.clone(),
875+
storage_root,
876+
valid_delta,
877+
).unwrap();
878+
let second_storage_root = delta_trie_root::<Layout, _, _, _, _>(
879+
&mut proof_db.clone(),
880+
storage_root,
881+
invalid_delta,
882+
).unwrap();
883+
884+
assert_eq!(first_storage_root, second_storage_root);
885+
}
855886
}
3.39 KB
Binary file not shown.

primitives/trie/test-res/proof

4.57 KB
Binary file not shown.

0 commit comments

Comments
 (0)