Skip to content

Commit c6e1702

Browse files
committed
fix: ban arithmetic side effects
1 parent 1f22d1b commit c6e1702

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2106
-1676
lines changed

Cargo.lock

Lines changed: 1836 additions & 1569 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ members = [
44
"runtime",
55
"pallets/*",
66
"pallets/*/api",
7+
"pallets/*/rpc",
78
"test-utils",
89
"xtask",
910
]
@@ -13,6 +14,15 @@ resolver = "2"
1314
authors = ["Renlabs <[email protected]>"]
1415
edition = "2021"
1516

17+
[workspace.lints.clippy]
18+
all = "deny"
19+
arithmetic_side_effects = "deny"
20+
indexing_slicing = "deny"
21+
panicking_unwrap = "deny"
22+
out_of_bounds_indexing = "deny"
23+
match_bool = "deny"
24+
infinite_loop = "deny"
25+
1626
[workspace.dependencies]
1727

1828
# Local
@@ -27,6 +37,7 @@ pallet-torus0 = { path = "./pallets/torus0", default-features = false }
2737
pallet-torus0-api = { path = "./pallets/torus0/api", default-features = false }
2838
pallet-permission0 = { path = "./pallets/permission0", default-features = false }
2939
pallet-permission0-api = { path = "./pallets/permission0/api", default-features = false }
40+
pallet-permission0-rpc = { path = "./pallets/permission0/rpc", default-features = false }
3041
test-utils.path = "./test-utils"
3142

3243
# Substrate
@@ -36,10 +47,11 @@ polkadot-sdk = { git = "https://github.com/paritytech/polkadot-sdk.git", branch
3647
substrate-wasm-builder = { git = "https://github.com/paritytech/polkadot-sdk.git", branch = "stable2409", default-features = false }
3748

3849
# Utils
50+
async-trait = "0.1.88"
3951
num-traits = { version = "0.2.19", default-features = false, features = [
4052
"libm",
4153
] }
42-
bitflags = { version = "2.5.0", default-features = false }
54+
bitflags = { version = "2.9.1", default-features = false }
4355

4456
# Frontier / EVM
4557
fc-api = { git = "https://github.com/paritytech/frontier.git", rev = "b9b1c620c8b418bdeeadc79725f9cfa4703c0333" }

flake.lock

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

flake.nix

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description = "Torus Substrate development environment";
33

44
inputs = {
5-
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
66

77
flake-utils.url = "github:numtide/flake-utils";
88

@@ -32,14 +32,21 @@
3232
else
3333
generalBuildInputs
3434
++ [ pkgs.darwin.apple_sdk.frameworks.SystemConfiguration ];
35-
nativeBuildInputs = with pkgs; [ git rust clang protobuf sccache ];
35+
nativeBuildInputs = with pkgs; [
36+
git
37+
llvmPackages_17.stdenv
38+
llvmPackages_17.libcxx
39+
llvmPackages_17.libcxxStdenv
40+
llvmPackages_17.clang-unwrapped
41+
rust
42+
protobuf
43+
sccache
44+
];
3645

3746
shellPkgs = [
3847
pkgs.bashInteractive
3948
# Run project-specific commands
4049
pkgs.just
41-
# Run Github actions locally
42-
pkgs.act
4350
# Python
4451
pkgs.python310
4552
# # Code coverage tool
@@ -55,13 +62,26 @@
5562
};
5663

5764
devShells.default = pkgs.mkShell {
58-
inherit (self.checks.${system}.pre-commit-check) shellHook;
5965
buildInputs = buildInputs;
6066
nativeBuildInputs = nativeBuildInputs;
6167
packages = shellPkgs;
6268

69+
shellHook = ''
70+
${self.checks.${system}.pre-commit-check.shellHook}
71+
72+
# Correct paths to build RocksDB and WASM-OPT.
73+
# Don't touch it unless you know what you are doing.
74+
export C_INCLUDE_PATH="${pkgs.llvmPackages_17.clang-unwrapped.lib}/lib/clang/17/include"
75+
export CPLUS_INCLUDE_PATH="${pkgs.llvmPackages_17.libcxx.dev}/include/c++/v1:${pkgs.llvmPackages_17.clang-unwrapped.lib}/lib/clang/17/include"
76+
77+
# Ensure clang-17 is prioritized in PATH. Oxalica's Rust Overlay
78+
# also ships clang-19 but current polkadot dependencies require 17.
79+
export PATH="${pkgs.llvmPackages_17.clang-unwrapped}/bin:$PATH"
80+
echo "Using clang version: $(clang --version | head -n1)"
81+
'';
82+
6383
env = {
64-
LIBCLANG_PATH = "${pkgs.libclang.lib}/lib";
84+
LIBCLANG_PATH = "${pkgs.llvmPackages_17.clang-unwrapped.lib}/lib";
6585
ROCKSDB_LIB_DIR = "${pkgs.rocksdb}/lib";
6686
ZSTD_SYS_USE_PKG_CONFIG = "true";
6787
OPENSSL_NO_VENDOR = "1";

node/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ runtime-benchmarks = [
1515
]
1616
testnet = ["torus-runtime/testnet"]
1717

18-
1918
[dependencies]
2019

2120
clap = { workspace = true, features = ["derive"] }
@@ -107,7 +106,7 @@ fp-rpc = { workspace = true, features = ["default"] }
107106

108107
# Local
109108
torus-runtime.workspace = true
110-
109+
pallet-permission0-rpc.workspace = true
111110

112111
[build-dependencies]
113112
polkadot-sdk = { workspace = true, features = ["substrate-build-script-utils"] }

node/src/rpc.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use std::sync::Arc;
2727
use eth::EthDeps;
2828
use futures::channel::mpsc;
2929
use jsonrpsee::RpcModule;
30+
use pallet_permission0_rpc::{Permission0Rpc, Permission0StreamApiServer};
3031
use polkadot_sdk::{
3132
pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApiServer},
3233
sc_consensus_manual_seal::{
@@ -84,7 +85,8 @@ where
8485
} = deps;
8586

8687
io.merge(System::new(client.clone(), pool).into_rpc())?;
87-
io.merge(TransactionPayment::new(client).into_rpc())?;
88+
io.merge(TransactionPayment::new(client.clone()).into_rpc())?;
89+
io.merge(Permission0Rpc::new(client).into_rpc())?;
8890

8991
if let Some(command_sink) = command_sink {
9092
io.merge(

node/src/service.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,19 @@ type InherentDataProviders = (
153153
fp_dynamic_fee::InherentDataProvider,
154154
);
155155

156-
fn aura_data_provider(
157-
slot_duration: sp_consensus_aura::SlotDuration,
158-
eth_config: &EthConfiguration,
159-
) -> impl Fn(
160-
sp_core::H256,
161-
(),
162-
) -> Pin<
156+
type AuraData = Pin<
163157
Box<
164158
dyn std::future::Future<
165159
Output = Result<InherentDataProviders, Box<dyn std::error::Error + Send + Sync>>,
166160
> + Send
167161
+ Sync,
168162
>,
169-
> {
163+
>;
164+
165+
fn aura_data_provider(
166+
slot_duration: sp_consensus_aura::SlotDuration,
167+
eth_config: &EthConfiguration,
168+
) -> impl Fn(sp_core::H256, ()) -> AuraData {
170169
let target_gas_price = eth_config.target_gas_price;
171170
move |_, ()| {
172171
Box::pin(async move {

pallets/emission0/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ license = "MIT-0"
66
authors.workspace = true
77
edition.workspace = true
88

9+
[lints]
10+
workspace = true
11+
912
[features]
1013
default = ["std"]
1114
std = [

pallets/emission0/api/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ license = "MIT-0"
66
authors.workspace = true
77
edition.workspace = true
88

9+
[lints]
10+
workspace = true
11+
912
[features]
1013
default = ["std"]
1114
std = ["polkadot-sdk/std"]

pallets/emission0/src/distribute.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@ pub fn get_total_emission_per_block<T: Config>() -> BalanceOf<T> {
7474
}
7575

7676
let interval = T::HalvingInterval::get();
77-
let halving_count = total_issuance.saturating_div(interval.get());
77+
let halving_count = total_issuance
78+
.checked_div(interval.get())
79+
.unwrap_or_default();
7880
let emission = T::BlockEmission::get() >> halving_count;
7981

80-
let not_recycled = Percent::one() - crate::EmissionRecyclingPercentage::<T>::get();
82+
let not_recycled =
83+
Percent::one().saturating_sub(crate::EmissionRecyclingPercentage::<T>::get());
8184
not_recycled.mul_floor(emission)
8285
}
8386

@@ -168,8 +171,9 @@ impl<T: Config> ConsensusMemberInput<T> {
168171
}
169172

170173
if total_network_stake != 0.into() {
171-
input.normalized_stake =
172-
FixedU128::from_inner(input.total_stake) / total_network_stake;
174+
input.normalized_stake = FixedU128::from_inner(input.total_stake)
175+
.const_checked_div(total_network_stake)
176+
.unwrap_or_default();
173177
}
174178
}
175179

@@ -184,7 +188,8 @@ impl<T: Config> ConsensusMemberInput<T> {
184188
weights: Weights<T>,
185189
min_validator_stake: u128,
186190
) -> ConsensusMemberInput<T> {
187-
let weight_factor = Percent::one() - <T::Torus>::weight_penalty_factor(&agent_id);
191+
let weight_factor =
192+
Percent::one().saturating_sub(<T::Torus>::weight_penalty_factor(&agent_id));
188193

189194
let mut total_stake = 0;
190195
let stakes = <T::Torus>::staked_by(&agent_id)
@@ -239,7 +244,7 @@ impl<T: Config> ConsensusMemberInput<T> {
239244

240245
if weights_sum > 0.into() {
241246
for (_, weight) in weights.iter_mut() {
242-
*weight = *weight / weights_sum;
247+
*weight = weight.const_checked_div(weights_sum).unwrap_or_default();
243248
}
244249
}
245250

@@ -429,7 +434,7 @@ fn compute_emissions<T: Config>(
429434
.collect();
430435
let emission_sum = combined_emission
431436
.iter()
432-
.fold(FixedU128::default(), |acc, &e| acc + e);
437+
.fold(FixedU128::default(), |acc, &e| acc.saturating_add(e));
433438

434439
let normalized_incentives = math::normalize_with_sum(incentives, emission_sum);
435440
let normalized_dividends = if emission_sum == 0.into() {
@@ -457,12 +462,12 @@ fn compute_emissions<T: Config>(
457462
let dividends_to_be_emitted;
458463

459464
if let Some(incentives_ratio) = incentives_ratio.checked_sub(50) {
460-
let incentives_percentage = Percent::from_parts(incentives_ratio * 2);
465+
let incentives_percentage = Percent::from_parts(incentives_ratio.saturating_mul(2));
461466
let incentives = incentives_percentage.mul_floor(to_be_emitted);
462467
incentives_to_be_emitted = to_be_emitted.saturating_add(incentives);
463468
dividends_to_be_emitted = to_be_emitted.saturating_sub(incentives);
464469
} else if let Some(dividends_ratio) = 50u8.checked_sub(incentives_ratio) {
465-
let dividends_percentage = Percent::from_parts(dividends_ratio * 2);
470+
let dividends_percentage = Percent::from_parts(dividends_ratio.saturating_mul(2));
466471
let dividends = dividends_percentage.mul_floor(to_be_emitted);
467472
dividends_to_be_emitted = to_be_emitted.saturating_add(dividends);
468473
incentives_to_be_emitted = to_be_emitted.saturating_sub(dividends);

0 commit comments

Comments
 (0)