Skip to content

Commit

Permalink
feat: Introduce Unit for constant unit values
Browse files Browse the repository at this point in the history
  • Loading branch information
conr2d committed Dec 25, 2024
1 parent b02cd8d commit 1888fc8
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 60 deletions.
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ version = "0.5.0"
edition = "2021"
repository = "https://github.com/noirhq/noir.git"

[workspace.lints.clippy]
erasing-op = { level = "allow", priority = 2 }
identity-op = { level = "allow", priority = 2 }

[workspace]
resolver = "2"
members = [
Expand Down
3 changes: 0 additions & 3 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ edition = { workspace = true }
repository = { workspace = true }
publish = false

[lints]
workspace = true

[dependencies]
frame-support = { workspace = true }
frame-system = { workspace = true }
Expand Down
19 changes: 10 additions & 9 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
#![cfg_attr(not(feature = "std"), no_std)]

mod unit;

pub use noir_core_primitives::*;
pub mod units;
pub use unit::Unit;

use crate::units::{MiB, CENTS, MILLICENTS};
use frame_support::{
dispatch::DispatchClass,
parameter_types,
Expand All @@ -33,7 +34,7 @@ use frame_support::{
Weight, WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
},
};
use frame_system::limits;
use frame_system::limits::{BlockLength as TBlockLength, BlockWeights as TBlockWeights};
use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment};
use smallvec::smallvec;
use static_assertions::const_assert;
Expand Down Expand Up @@ -61,9 +62,9 @@ parameter_types! {
/// Maximum amount of the multiplier.
pub MaximumMultiplier: Multiplier = Bounded::max_value();
/// 5 MiB block size limit.
pub BlockLength: limits::BlockLength =
limits::BlockLength::max_with_normal_ratio(5 * MiB, NORMAL_DISPATCH_RATIO);
pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder()
pub BlockLength: TBlockLength =
TBlockLength::max_with_normal_ratio(Unit(5).mebibytes(), NORMAL_DISPATCH_RATIO);
pub BlockWeights: TBlockWeights = TBlockWeights::builder()
.base_block(BlockExecutionWeight::get())
.for_class(DispatchClass::all(), |weights| {
weights.base_extrinsic = ExtrinsicBaseWeight::get();
Expand All @@ -81,8 +82,8 @@ parameter_types! {
})
.avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO)
.build_or_panic();

pub TransactionByteFee: Balance = 10 * MILLICENTS;
/// Transaction fees per byte.
pub TransactionByteFee: Balance = Unit(10).millicents();
}

/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
Expand All @@ -100,7 +101,7 @@ pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
let p = 1 * CENTS;
let p = Unit(1).cent();
let q = 10 * Balance::from(ExtrinsicBaseWeight::get().ref_time());
smallvec![WeightToFeeCoefficient {
degree: 1,
Expand Down
136 changes: 136 additions & 0 deletions runtime/common/src/unit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// This file is part of Noir.

// Copyright (c) Haderech Pte. Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use crate::{Balance, Moment};

/// A number of units.
pub struct Unit(pub u32);

impl Unit {
/// A unit of base currency.
pub const fn dollars(&self) -> Balance {
self.0 as Balance * 1_000_000_000_000_000_000
}
#[doc(hidden)]
pub const fn dollar(&self) -> Balance {
self.dollars()
}
/// One hundredth of a dollar.
pub const fn cents(&self) -> Balance {
self.dollars() / 100
}
#[doc(hidden)]
pub const fn cent(&self) -> Balance {
self.cents()
}
/// One thousandth of a cent.
pub const fn millicents(&self) -> Balance {
self.cents() / 1_000
}
#[doc(hidden)]
pub const fn millicent(&self) -> Balance {
self.millicents()
}
/// Kibibytes.
pub const fn kibibytes(&self) -> u32 {
self.0 * 1024
}
#[doc(hidden)]
pub const fn kibibyte(&self) -> u32 {
self.kibibytes()
}
/// Mebibytes.
pub const fn mebibytes(&self) -> u32 {
self.kibibytes() * 1024
}
#[doc(hidden)]
pub const fn mebibyte(&self) -> u32 {
self.mebibytes()
}
/// A day in milliseconds.
pub const fn days(&self) -> Moment {
self.hours() * 24
}
#[doc(hidden)]
pub const fn day(&self) -> Moment {
self.days()
}
/// An hour in milliseconds.
pub const fn hours(&self) -> Moment {
self.minutes() * 60
}
#[doc(hidden)]
pub const fn hour(&self) -> Moment {
self.hours()
}
/// A minute in milliseconds.
pub const fn minutes(&self) -> Moment {
self.seconds() * 60
}
#[doc(hidden)]
pub const fn minute(&self) -> Moment {
self.minutes()
}
/// A second in milliseconds.
pub const fn seconds(&self) -> Moment {
self.milliseconds() * 1000
}
#[doc(hidden)]
pub const fn second(&self) -> Moment {
self.seconds()
}
/// A millisecond.
pub const fn milliseconds(&self) -> Moment {
self.0 as Moment
}
#[doc(hidden)]
pub const fn millisecond(&self) -> Moment {
self.milliseconds()
}
}

#[cfg(test)]
mod tests {
#![allow(non_upper_case_globals)]
use super::*;

pub const DOLLARS: Balance = 1_000_000_000_000_000_000;
pub const CENTS: Balance = DOLLARS / 100;
pub const MILLICENTS: Balance = CENTS / 1_000;
pub const KiB: u32 = 1024;
pub const MiB: u32 = 1024 * KiB;
pub const DAYS: Moment = 24 * HOURS;
pub const HOURS: Moment = 60 * MINUTES;
pub const MINUTES: Moment = 60 * SECONDS;
pub const SECONDS: Moment = 1000;
pub const MILLISECONDS: Moment = 1;

#[test]
fn test_units() {
assert_eq!(Unit(42).dollars(), 42 * DOLLARS);
assert_eq!(Unit(42).cents(), 42 * CENTS);
assert_eq!(Unit(42).millicents(), 42 * MILLICENTS);
assert_eq!(Unit(42).kibibytes(), 42 * KiB);
assert_eq!(Unit(42).mebibytes(), 42 * MiB);
assert_eq!(Unit(42).days(), 42 * DAYS);
assert_eq!(Unit(42).hours(), 42 * HOURS);
assert_eq!(Unit(42).minutes(), 42 * MINUTES);
assert_eq!(Unit(42).seconds(), 42 * SECONDS);
assert_eq!(Unit(42).milliseconds(), 42 * MILLISECONDS);
}
}
44 changes: 0 additions & 44 deletions runtime/common/src/units.rs

This file was deleted.

0 comments on commit 1888fc8

Please sign in to comment.