Skip to content
Snippets Groups Projects
Commit 21fae718 authored by Kian Paimani's avatar Kian Paimani Committed by GitHub
Browse files

Multi-limb arithmetic for runtime (#3743)


* First working version of all operations.

* New and improved version of everything.

* Minor cleanup.

* Fix build

* Finalize nignum

* Some final works on refactors and tests.

* fix build

* Some review comments

* Bench, better try into and nits

* mutify the API

* rename to big_uint

* unmutify.

* Remove resize

* Apply suggestions from code review

* Update core/sr-primitives/src/sr_arithmetic.rs

Co-Authored-By: default avatarthiolliere <gui.thiolliere@gmail.com>

* BEtter proof

* Fix panic doc.

* Bump.
parent 4da48dd9
No related merge requests found
......@@ -34,8 +34,8 @@
#![cfg_attr(not(feature = "std"), no_std)]
use rstd::{prelude::*, collections::btree_map::BTreeMap};
use sr_primitives::{helpers_128bit::multiply_by_rational_best_effort, Perbill, Rational128};
use sr_primitives::traits::{Zero, Convert, Member, SimpleArithmetic, Saturating};
use sr_primitives::{helpers_128bit::multiply_by_rational, Perbill, Rational128};
use sr_primitives::traits::{Zero, Convert, Member, SimpleArithmetic, Saturating, Bounded};
mod mock;
mod tests;
......@@ -253,11 +253,11 @@ pub fn elect<AccountId, Balance, FS, C>(
for e in &n.edges {
let c = &mut candidates[e.candidate_index];
if !c.elected && !c.approval_stake.is_zero() {
let temp_n = multiply_by_rational_best_effort(
let temp_n = multiply_by_rational(
n.load.n(),
n.budget,
c.approval_stake,
);
).unwrap_or(Bounded::max_value());
let temp_d = n.load.d();
let temp = Rational128::from(temp_n, temp_d);
c.score = c.score.lazy_saturating_add(temp);
......@@ -303,11 +303,11 @@ pub fn elect<AccountId, Balance, FS, C>(
if e.load.d() == n.load.d() {
// return e.load / n.load.
let desired_scale: u128 = Perbill::accuracy().into();
multiply_by_rational_best_effort(
multiply_by_rational(
desired_scale,
e.load.n(),
n.load.n(),
)
).unwrap_or(Bounded::max_value())
} else {
// defensive only. Both edge and nominator loads are built from
// scores, hence MUST have the same denominator.
......
......@@ -26,6 +26,7 @@ rand = "0.7.2"
substrate-offchain = { path = "../offchain" }
[features]
bench = []
default = ["std"]
std = [
"num-traits/std",
......
......@@ -19,6 +19,10 @@
#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
// to allow benchmarking
#![cfg_attr(feature = "bench", feature(test))]
#[cfg(feature = "bench")] extern crate test;
#[doc(hidden)]
pub use codec;
#[cfg(feature = "std")]
......@@ -59,13 +63,15 @@ pub use generic::{DigestItem, Digest};
pub use primitives::{TypeId, crypto::{key_types, KeyTypeId, CryptoType}};
pub use app_crypto::RuntimeAppPublic;
/// Re-export arithmetic stuff.
/// Re-export top-level arithmetic stuff.
pub use sr_arithmetic::{
Perquintill, Perbill, Permill, Percent,
Rational128, Fixed64
};
/// Re-export 128 bit helpers from sr_arithmetic
/// Re-export 128 bit helpers.
pub use sr_arithmetic::helpers_128bit;
/// Re-export big_uint stiff.
pub use sr_arithmetic::biguint;
#[cfg(feature = "std")]
pub use externalities::set_and_run_with_externalities;
......
This diff is collapsed.
......@@ -84,8 +84,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to equal spec_version. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 173,
impl_version: 173,
spec_version: 174,
impl_version: 174,
apis: RUNTIME_API_VERSIONS,
};
......
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