Skip to content

Commit

Permalink
Merge branch 'main' into fix/chainspecs
Browse files Browse the repository at this point in the history
  • Loading branch information
orriin committed Jul 2, 2024
2 parents 15878cb + f62d824 commit 0ce8638
Show file tree
Hide file tree
Showing 56 changed files with 562 additions and 302 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ members = [
resolver = "2"

[workspace.lints.clippy]
indexing-slicing = "deny"
arithmetic-side-effects = "deny"
type_complexity = "allow"
unwrap-used = "deny"

[workspace.dependencies]
cargo-husky = { version = "1", default-features = false }
Expand Down
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ COPY Cargo.lock Cargo.toml /subtensor/

# Specs
COPY ./snapshot.json /subtensor/snapshot.json
COPY ./raw_spec_testfinney.json /subtensor/raw_spec_testfinney.json
COPY ./raw_spec_finney.json /subtensor/raw_spec_finney.json

# Copy our sources
COPY ./node /subtensor/node
Expand All @@ -50,11 +52,13 @@ RUN /subtensor/scripts/init.sh

# Cargo build
WORKDIR /subtensor
RUN cargo build --release --features runtime-benchmarks --locked
RUN cargo build --profile production --features runtime-benchmarks --locked
EXPOSE 30333 9933 9944


FROM $BASE_IMAGE AS subtensor

COPY --from=builder /subtensor/snapshot.json /
COPY --from=builder /subtensor/target/release/node-subtensor /usr/local/bin
COPY --from=builder /subtensor/raw_spec_testfinney.json /
COPY --from=builder /subtensor/raw_spec_finney.json /
COPY --from=builder /subtensor/target/production/node-subtensor /usr/local/bin
10 changes: 5 additions & 5 deletions docs/running-subtensor-locally.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ rm -rf /tmp/blockchain
Install subtensor by compiling with `cargo`:

```bash
cargo build --release --features=runtime-benchmarks
cargo build --profile production --features=runtime-benchmarks
```

## Run the subtensor node
Expand All @@ -174,31 +174,31 @@ You can now run the public subtensor node either as a lite node or as an archive
To run a lite node connected to the mainchain, execute the below command (note the `--sync=warp` flag which runs the subtensor node in lite mode):

```bash title="With --sync=warp setting, for lite node"
./target/release/node-subtensor --chain raw_spec_finney.json --base-path /tmp/blockchain --sync=warp --execution wasm --wasm-execution compiled --port 30333 --max-runtime-instances 32 --rpc-max-response-size 2048 --rpc-cors all --rpc-port 9944 --bootnodes /ip4/13.58.175.193/tcp/30333/p2p/12D3KooWDe7g2JbNETiKypcKT1KsCEZJbTzEHCn8hpd4PHZ6pdz5 --no-mdns --in-peers 8000 --out-peers 8000 --prometheus-external --rpc-external
./target/production/node-subtensor --chain raw_spec.json --base-path /tmp/blockchain --sync=warp --port 30333 --max-runtime-instances 32 --rpc-max-response-size 2048 --rpc-cors all --rpc-port 9944 --bootnodes /ip4/13.58.175.193/tcp/30333/p2p/12D3KooWDe7g2JbNETiKypcKT1KsCEZJbTzEHCn8hpd4PHZ6pdz5 --no-mdns --in-peers 8000 --out-peers 8000 --prometheus-external --rpc-external
```

### Archive node on mainchain

To run an archive node connected to the mainchain, execute the below command (note the `--sync=full` which syncs the node to the full chain and `--pruning archive` flags, which disables the node's automatic pruning of older historical data):

```bash title="With --sync=full and --pruning archive setting, for archive node"
./target/release/node-subtensor --chain raw_spec_finney.json --base-path /tmp/blockchain --sync=full --pruning archive --execution wasm --wasm-execution compiled --port 30333 --max-runtime-instances 32 --rpc-max-response-size 2048 --rpc-cors all --rpc-port 9944 --bootnodes /ip4/13.58.175.193/tcp/30333/p2p/12D3KooWDe7g2JbNETiKypcKT1KsCEZJbTzEHCn8hpd4PHZ6pdz5 --no-mdns --in-peers 8000 --out-peers 8000 --prometheus-external --rpc-external
./target/production/node-subtensor --chain raw_spec.json --base-path /tmp/blockchain --sync=full --pruning archive --port 30333 --max-runtime-instances 32 --rpc-max-response-size 2048 --rpc-cors all --rpc-port 9944 --bootnodes /ip4/13.58.175.193/tcp/30333/p2p/12D3KooWDe7g2JbNETiKypcKT1KsCEZJbTzEHCn8hpd4PHZ6pdz5 --no-mdns --in-peers 8000 --out-peers 8000 --prometheus-external --rpc-external
```

### Lite node on testchain

To run a lite node connected to the testchain, execute the below command:

```bash title="With bootnodes set to testnet and --sync=warp setting, for lite node."
./target/release/node-subtensor --chain raw_spec_testfinney.json --base-path /tmp/blockchain --sync=warp --execution wasm --wasm-execution compiled --port 30333 --max-runtime-instances 32 --rpc-max-response-size 2048 --rpc-cors all --rpc-port 9944 --bootnodes /dns/bootnode.test.finney.opentensor.ai/tcp/30333/p2p/12D3KooWPM4mLcKJGtyVtkggqdG84zWrd7Rij6PGQDoijh1X86Vr --no-mdns --in-peers 8000 --out-peers 8000 --prometheus-external --rpc-external
./target/production/node-subtensor --chain raw_testspec.json --base-path /tmp/blockchain --sync=warp --port 30333 --max-runtime-instances 32 --rpc-max-response-size 2048 --rpc-cors all --rpc-port 9944 --bootnodes /dns/bootnode.test.finney.opentensor.ai/tcp/30333/p2p/12D3KooWPM4mLcKJGtyVtkggqdG84zWrd7Rij6PGQDoijh1X86Vr --no-mdns --in-peers 8000 --out-peers 8000 --prometheus-external --rpc-external
```

### Archive node on testchain

To run an archive node connected to the testchain, execute the below command:

```bash title="With bootnodes set to testnet and --sync=full and --pruning archive setting, for archive node"
./target/release/node-subtensor --chain raw_spec_testfinney.json --base-path /tmp/blockchain --sync=full --pruning archive --execution wasm --wasm-execution compiled --port 30333 --max-runtime-instances 32 --rpc-max-response-size 2048 --rpc-cors all --rpc-port 9944 --bootnodes /dns/bootnode.test.finney.opentensor.ai/tcp/30333/p2p/12D3KooWPM4mLcKJGtyVtkggqdG84zWrd7Rij6PGQDoijh1X86Vr --no-mdns --in-peers 8000 --out-peers 8000 --prometheus-external --rpc-external
./target/production/node-subtensor --chain raw_testspec.json --base-path /tmp/blockchain --sync=full --pruning archive --port 30333 --max-runtime-instances 32 --rpc-max-response-size 2048 --rpc-cors all --rpc-port 9944 --bootnodes /dns/bootnode.test.finney.opentensor.ai/tcp/30333/p2p/12D3KooWPM4mLcKJGtyVtkggqdG84zWrd7Rij6PGQDoijh1X86Vr --no-mdns --in-peers 8000 --out-peers 8000 --prometheus-external --rpc-external
```

## Running on cloud
Expand Down
6 changes: 3 additions & 3 deletions docs/rust-setup.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
---
title: Installation
---

This guide is for reference only, please check the latest information on getting starting with Substrate
This guide is for reference only, please check the latest information on getting starting with Substrate
[here](https://docs.substrate.io/main-docs/install/).

This page will guide you through the **2 steps** needed to prepare a computer for **Substrate** development.
Expand Down Expand Up @@ -207,7 +206,7 @@ Use the `WASM_BUILD_TOOLCHAIN` environment variable to specify the Rust nightly
project should use for Wasm compilation:

```bash
WASM_BUILD_TOOLCHAIN=nightly-<yyyy-MM-dd> cargo build --release
WASM_BUILD_TOOLCHAIN=nightly-<yyyy-MM-dd> cargo build --profile production
```

> Note that this only builds _the runtime_ with the specified nightly. The rest of project will be
Expand All @@ -223,3 +222,4 @@ rustup uninstall nightly
rustup install nightly-<yyyy-MM-dd>
rustup target add wasm32-unknown-unknown --toolchain nightly-<yyyy-MM-dd>
```

6 changes: 3 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export RUST_BACKTRACE := "full"
export SKIP_WASM_BUILD := "1"
export RUST_BIN_DIR := "target/x86_64-unknown-linux-gnu"
export TARGET := "x86_64-unknown-linux-gnu"
export RUSTV := "nightly-2024-03-05"
export RUSTV := "stable"
export RELEASE_NAME := "development"

fmt:
Expand All @@ -25,13 +25,13 @@ benchmarks:

clippy:
@echo "Running cargo clippy..."
cargo +{{RUSTV}} clippy -- -D clippy::panic \
cargo +{{RUSTV}} clippy --workspace --all-targets -- -D \
-D clippy::todo \
-D clippy::unimplemented

clippy-fix:
@echo "Running cargo clippy with automatic fixes on potentially dirty code..."
cargo +{{RUSTV}} clippy --fix --allow-dirty -- -A clippy::panic \
cargo +{{RUSTV}} clippy --fix --allow-dirty --workspace --all-targets -- -A \
-A clippy::todo \
-A clippy::unimplemented
fix:
Expand Down
6 changes: 4 additions & 2 deletions node/src/chain_spec/finney.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::*;

pub fn finney_mainnet_config() -> Result<ChainSpec, String> {
let path: PathBuf = std::path::PathBuf::from("./snapshot.json");
let wasm_binary = WASM_BINARY.ok_or_else(|| "Development wasm not available".to_string())?;
let wasm_binary = WASM_BINARY.ok_or("Development wasm not available".to_string())?;

// We mmap the file into memory first, as this is *a lot* faster than using
// `serde_json::from_reader`. See https://github.com/serde-rs/json/issues/160
Expand Down Expand Up @@ -53,7 +53,9 @@ pub fn finney_mainnet_config() -> Result<ChainSpec, String> {
let key_account = sp_runtime::AccountId32::from(key);

processed_balances.push((key_account, *amount));
balances_issuance += *amount;
balances_issuance = balances_issuance
.checked_add(*amount)
.ok_or("Balances issuance overflowed".to_string())?;
}

// Give front-ends necessary data to present to users
Expand Down
6 changes: 4 additions & 2 deletions node/src/chain_spec/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn finney_testnet_config() -> Result<ChainSpec, String> {
};

let old_state: ColdkeyHotkeys =
json::from_slice(&bytes).map_err(|e| format!("Error parsing genesis file: {}", e))?;
json::from_slice(&bytes).map_err(|e| format!("Error parsing genesis file: {e}"))?;

let mut processed_stakes: Vec<(
sp_runtime::AccountId32,
Expand Down Expand Up @@ -53,7 +53,9 @@ pub fn finney_testnet_config() -> Result<ChainSpec, String> {
let key_account = sp_runtime::AccountId32::from(key);

processed_balances.push((key_account, *amount));
balances_issuance += *amount;
balances_issuance = balances_issuance
.checked_add(*amount)
.ok_or("Balances issuance overflowed".to_string())?;
}

// Give front-ends necessary data to present to users
Expand Down
50 changes: 49 additions & 1 deletion node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use sp_runtime::traits::HashingFor;

use node_subtensor_runtime::Block;
use sc_cli::SubstrateCli;
use sc_service::PartialComponents;
use sc_service::{Configuration, PartialComponents};

impl SubstrateCli for Cli {
fn impl_name() -> String {
Expand Down Expand Up @@ -209,8 +209,56 @@ pub fn run() -> sc_cli::Result<()> {
None => {
let runner = cli.create_runner(&cli.run)?;
runner.run_node_until_exit(|config| async move {
let config = override_default_heap_pages(config, 60_000);
service::new_full(config).map_err(sc_cli::Error::Service)
})
}
}
}

/// Override default heap pages
fn override_default_heap_pages(config: Configuration, pages: u64) -> Configuration {
Configuration {
default_heap_pages: Some(pages),
impl_name: config.impl_name,
impl_version: config.impl_version,
role: config.role,
tokio_handle: config.tokio_handle,
transaction_pool: config.transaction_pool,
network: config.network,
keystore: config.keystore,
database: config.database,
trie_cache_maximum_size: config.trie_cache_maximum_size,
state_pruning: config.state_pruning,
blocks_pruning: config.blocks_pruning,
chain_spec: config.chain_spec,
wasm_method: config.wasm_method,
wasm_runtime_overrides: config.wasm_runtime_overrides,
rpc_addr: config.rpc_addr,
rpc_max_connections: config.rpc_max_connections,
rpc_cors: config.rpc_cors,
rpc_methods: config.rpc_methods,
rpc_max_request_size: config.rpc_max_request_size,
rpc_max_response_size: config.rpc_max_response_size,
rpc_id_provider: config.rpc_id_provider,
rpc_max_subs_per_conn: config.rpc_max_subs_per_conn,
rpc_port: config.rpc_port,
rpc_message_buffer_capacity: config.rpc_message_buffer_capacity,
rpc_batch_config: config.rpc_batch_config,
rpc_rate_limit: config.rpc_rate_limit,
prometheus_config: config.prometheus_config,
telemetry_endpoints: config.telemetry_endpoints,
offchain_worker: config.offchain_worker,
force_authoring: config.force_authoring,
disable_grandpa: config.disable_grandpa,
dev_key_seed: config.dev_key_seed,
tracing_targets: config.tracing_targets,
tracing_receiver: config.tracing_receiver,
max_runtime_instances: config.max_runtime_instances,
announce_block: config.announce_block,
data_path: config.data_path,
base_path: config.base_path,
informant_output_format: config.informant_output_format,
runtime_cache_size: config.runtime_cache_size,
}
}
20 changes: 9 additions & 11 deletions pallets/admin-utils/scripts/benchmark.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
cargo build --release --features runtime-benchmarks
./target/release/node-subtensor benchmark pallet \
--chain=local \
--execution=wasm \
--wasm-execution=compiled \
--pallet=pallet_admin_utils \
--extrinsic="*" \
--steps 50 \
--repeat 20 \
--output=pallets/admin-utils/src/weights.rs \
--template=./.maintain/frame-weight-template.hbs
cargo build --profile production --features runtime-benchmarks
./target/production/node-subtensor benchmark pallet \
--chain=local \
--pallet=pallet_admin_utils \
--extrinsic="*" \
--steps 50 \
--repeat 20 \
--output=pallets/admin-utils/src/weights.rs \
--template=./.maintain/frame-weight-template.hbs
1 change: 1 addition & 0 deletions pallets/admin-utils/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Benchmarking setup
#![cfg(feature = "runtime-benchmarks")]
#![allow(clippy::arithmetic_side_effects)]
use super::*;

#[allow(unused)]
Expand Down
2 changes: 2 additions & 0 deletions pallets/admin-utils/tests/mock.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::arithmetic_side_effects, clippy::unwrap_used)]

use frame_support::{
assert_ok, derive_impl, parameter_types,
traits::{Everything, Hooks},
Expand Down
3 changes: 2 additions & 1 deletion pallets/collective/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.

//! Staking pallet benchmarking.
#![allow(clippy::arithmetic_side_effects, clippy::indexing_slicing)]

use super::*;
use crate::Pallet as Collective;
Expand Down Expand Up @@ -70,7 +71,7 @@ benchmarks_instance_pallet! {
// Proposals should be different so that different proposal hashes are generated
let proposal: T::Proposal = SystemCall::<T>::remark { remark: id_to_remark_data(i, length) }.into();
Collective::<T, I>::propose(
SystemOrigin::Signed(old_members.last().unwrap().clone()).into(),
SystemOrigin::Signed(old_members.last().expect("m is greater than 0; old_members must have at least 1 element; qed").clone()).into(),
Box::new(proposal.clone()),
MAX_BYTES,
TryInto::<BlockNumberFor<T>>::try_into(3u64).ok().expect("convert u64 to block number.")
Expand Down
29 changes: 18 additions & 11 deletions pallets/collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use frame_support::{
use scale_info::TypeInfo;
use sp_io::storage;
use sp_runtime::traits::Dispatchable;
use sp_runtime::{traits::Hash, RuntimeDebug};
use sp_runtime::{traits::Hash, RuntimeDebug, Saturating};
use sp_std::{marker::PhantomData, prelude::*, result};

#[cfg(test)]
Expand Down Expand Up @@ -119,7 +119,7 @@ impl DefaultVote for MoreThanMajorityThenPrimeDefaultVote {
_no_votes: MemberCount,
len: MemberCount,
) -> bool {
let more_than_majority = yes_votes * 2 > len;
let more_than_majority = yes_votes.saturating_mul(2) > len;
more_than_majority || prime_vote.unwrap_or(false)
}
}
Expand Down Expand Up @@ -545,7 +545,9 @@ pub mod pallet {
Error::<T, I>::DurationLowerThanConfiguredMotionDuration
);

let threshold = (T::GetVotingMembers::get_count() / 2) + 1;
let threshold = T::GetVotingMembers::get_count()
.saturating_div(2)
.saturating_add(1);

let members = Self::members();
let (proposal_len, active_proposals) =
Expand Down Expand Up @@ -716,10 +718,15 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
})?;

let index = Self::proposal_count();
<ProposalCount<T, I>>::mutate(|i| *i += 1);
<ProposalCount<T, I>>::try_mutate(|i| {
*i = i
.checked_add(1)
.ok_or(Error::<T, I>::TooManyActiveProposals)?;
Ok::<(), Error<T, I>>(())
})?;
<ProposalOf<T, I>>::insert(proposal_hash, proposal);
let votes = {
let end = frame_system::Pallet::<T>::block_number() + duration;
let end = frame_system::Pallet::<T>::block_number().saturating_add(duration);
Votes {
index,
threshold,
Expand Down Expand Up @@ -862,10 +869,10 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
// default voting strategy.
let default = T::DefaultVote::default_vote(prime_vote, yes_votes, no_votes, seats);

let abstentions = seats - (yes_votes + no_votes);
let abstentions = seats.saturating_sub(yes_votes.saturating_add(no_votes));
match default {
true => yes_votes += abstentions,
false => no_votes += abstentions,
true => yes_votes = yes_votes.saturating_add(abstentions),
false => no_votes = no_votes.saturating_add(abstentions),
}
let approved = yes_votes >= voting.threshold;

Expand Down Expand Up @@ -981,7 +988,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Voting::<T, I>::remove(proposal_hash);
let num_proposals = Proposals::<T, I>::mutate(|proposals| {
proposals.retain(|h| h != &proposal_hash);
proposals.len() + 1 // calculate weight based on original length
proposals.len().saturating_add(1) // calculate weight based on original length
});
num_proposals as u32
}
Expand Down Expand Up @@ -1154,7 +1161,7 @@ impl<
type Success = ();
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| match o {
RawOrigin::Members(n, m) if n * D > N * m => Ok(()),
RawOrigin::Members(n, m) if n.saturating_mul(D) > N.saturating_mul(m) => Ok(()),
r => Err(O::from(r)),
})
}
Expand All @@ -1179,7 +1186,7 @@ impl<
type Success = ();
fn try_origin(o: O) -> Result<Self::Success, O> {
o.into().and_then(|o| match o {
RawOrigin::Members(n, m) if n * D >= N * m => Ok(()),
RawOrigin::Members(n, m) if n.saturating_mul(D) >= N.saturating_mul(m) => Ok(()),
r => Err(O::from(r)),
})
}
Expand Down
2 changes: 1 addition & 1 deletion pallets/collective/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![allow(non_camel_case_types)]
#![allow(non_camel_case_types, clippy::indexing_slicing, clippy::unwrap_used)]

use super::{Event as CollectiveEvent, *};
use crate as pallet_collective;
Expand Down
Loading

0 comments on commit 0ce8638

Please sign in to comment.