Fix subtle indices bug (#2128)

* Fix subtle indices bug

* Also fix balances divide by zero.
This commit is contained in:
Gav Wood
2019-03-27 17:46:30 +01:00
committed by GitHub
parent 823abc2fc0
commit 039acb9518
5 changed files with 32 additions and 4 deletions
+2 -2
View File
@@ -58,8 +58,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("node"),
impl_name: create_runtime_str!("substrate-node"),
authoring_version: 10,
spec_version: 45,
impl_version: 45,
spec_version: 46,
impl_version: 46,
apis: RUNTIME_API_VERSIONS,
};
+1 -1
View File
@@ -312,7 +312,7 @@ decl_storage! {
// <= begin it should be >= balance
// >= begin+length it should be <= 0
let per_block = balance / length;
let per_block = balance / length.max(primitives::traits::One::one());
let offset = begin * per_block + balance;
(who.clone(), VestingSchedule { offset, per_block })
+29 -1
View File
@@ -54,7 +54,7 @@ impl<AccountId, AccountIndex> From<AccountId> for Address<AccountId, AccountInde
}
fn need_more_than<T: PartialOrd>(a: T, b: T) -> Option<T> {
if a < b { Some(a) } else { None }
if a < b { Some(b) } else { None }
}
impl<AccountId, AccountIndex> Decode for Address<AccountId, AccountIndex> where
@@ -108,3 +108,31 @@ impl<AccountId, AccountIndex> Default for Address<AccountId, AccountIndex> where
Address::Id(Default::default())
}
}
#[cfg(test)]
mod tests {
use crate::{Encode, Decode};
type Address = super::Address<[u8; 8], u32>;
fn index(i: u32) -> Address { super::Address::Index(i) }
fn id(i: [u8; 8]) -> Address { super::Address::Id(i) }
fn compare(a: Option<Address>, d: &[u8]) {
if let Some(ref a) = a {
assert_eq!(d, &a.encode()[..]);
}
assert_eq!(Address::decode(&mut &d[..]), a);
}
#[test]
fn it_should_work() {
compare(Some(index(2)), &[2][..]);
compare(None, &[240][..]);
compare(None, &[252, 239, 0][..]);
compare(Some(index(240)), &[252, 240, 0][..]);
compare(Some(index(304)), &[252, 48, 1][..]);
compare(None, &[253, 255, 255, 0, 0][..]);
compare(Some(index(0x10000)), &[253, 0, 0, 1, 0][..]);
compare(Some(id([42, 69, 42, 69, 42, 69, 42, 69])), &[255, 42, 69, 42, 69, 42, 69, 42, 69][..]);
}
}