Skip to content
Snippets Groups Projects
Commit ffbe6283 authored by Gav's avatar Gav
Browse files

Fix overflow bug.

parent 3b7e4e02
No related merge requests found
...@@ -49,6 +49,8 @@ impl<S: codec::Slicable + Default> StorageVec for AuthorityStorageVec<S> { ...@@ -49,6 +49,8 @@ impl<S: codec::Slicable + Default> StorageVec for AuthorityStorageVec<S> {
pub const CODE: &'static [u8] = b":code"; pub const CODE: &'static [u8] = b":code";
pub type KeyValue = (Vec<u8>, Vec<u8>);
pub trait Trait: system::Trait { pub trait Trait: system::Trait {
type PublicAux: RefInto<Self::AccountId>; type PublicAux: RefInto<Self::AccountId>;
type SessionKey: Parameter + Default; type SessionKey: Parameter + Default;
...@@ -61,6 +63,7 @@ decl_module! { ...@@ -61,6 +63,7 @@ decl_module! {
} }
pub enum PrivCall { pub enum PrivCall {
fn set_code(new: Vec<u8>) = 0; fn set_code(new: Vec<u8>) = 0;
fn set_storage(items: Vec<KeyValue>) = 1;
} }
} }
...@@ -75,6 +78,13 @@ impl<T: Trait> Module<T> { ...@@ -75,6 +78,13 @@ impl<T: Trait> Module<T> {
storage::unhashed::put_raw(CODE, &new); storage::unhashed::put_raw(CODE, &new);
} }
/// Set some items of storage.
fn set_storage(items: Vec<KeyValue>) {
for i in &items {
storage::unhashed::put_raw(&i.0, &i.1);
}
}
/// Report some misbehaviour. /// Report some misbehaviour.
fn report_misbehavior(_aux: &T::PublicAux, _report: MisbehaviorReport) { fn report_misbehavior(_aux: &T::PublicAux, _report: MisbehaviorReport) {
// TODO. // TODO.
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
//! Voting thresholds. //! Voting thresholds.
use primitives::traits::IntegerSquareRoot; use primitives::traits::{Zero, IntegerSquareRoot};
use codec::{Input, Slicable}; use codec::{Input, Slicable};
use rstd::ops::{Add, Mul, Div}; use rstd::ops::{Add, Mul, Div};
...@@ -58,17 +58,19 @@ pub trait Approved<Balance> { ...@@ -58,17 +58,19 @@ pub trait Approved<Balance> {
fn approved(&self, approve: Balance, against: Balance, electorate: Balance) -> bool; fn approved(&self, approve: Balance, against: Balance, electorate: Balance) -> bool;
} }
impl<Balance: IntegerSquareRoot + Ord + Add<Balance, Output = Balance> + Mul<Balance, Output = Balance> + Div<Balance, Output = Balance> + Copy> Approved<Balance> for VoteThreshold { impl<Balance: IntegerSquareRoot + Zero + Ord + Add<Balance, Output = Balance> + Mul<Balance, Output = Balance> + Div<Balance, Output = Balance> + Copy> Approved<Balance> for VoteThreshold {
/// Given `approve` votes for and `against` votes against from a total electorate size of /// Given `approve` votes for and `against` votes against from a total electorate size of
/// `electorate` (`electorate - (approve + against)` are abstainers), then returns true if the /// `electorate` (`electorate - (approve + against)` are abstainers), then returns true if the
/// overall outcome is in favour of approval. /// overall outcome is in favour of approval.
fn approved(&self, approve: Balance, against: Balance, electorate: Balance) -> bool { fn approved(&self, approve: Balance, against: Balance, electorate: Balance) -> bool {
let voters = approve + against; let voters = approve + against;
let sqrt_voters = voters.integer_sqrt();
if sqrt_voters.is_zero() { return false; }
match *self { match *self {
VoteThreshold::SuperMajorityApprove => VoteThreshold::SuperMajorityApprove =>
voters.integer_sqrt() * approve / electorate.integer_sqrt() > against, approve / electorate.integer_sqrt() > against / sqrt_voters,
VoteThreshold::SuperMajorityAgainst => VoteThreshold::SuperMajorityAgainst =>
approve > voters.integer_sqrt() * against / electorate.integer_sqrt(), approve / sqrt_voters > against / electorate.integer_sqrt(),
VoteThreshold::SimpleMajority => approve > against, VoteThreshold::SimpleMajority => approve > against,
} }
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment