Skip to content

Commit 0066b48

Browse files
committed
Merge branch 'devnet-ready' into hybrid-node
2 parents 868268f + dfb1448 commit 0066b48

File tree

11 files changed

+435
-85
lines changed

11 files changed

+435
-85
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LABEL ai.opentensor.image.authors="operations@opentensor.ai" \
1818

1919
# Rust targets
2020
RUN rustup update stable && \
21-
rustup target add wasm32-unknown-unknown --toolchain stable
21+
rustup target add wasm32v1-none --toolchain stable
2222

2323
# Build prerequisites
2424
ENV RUST_BACKTRACE=1

Dockerfile-localnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ WORKDIR /build
2727
RUN set -o pipefail && curl https://sh.rustup.rs -sSf | sh -s -- -y
2828
ENV PATH="/root/.cargo/bin:${PATH}"
2929
RUN rustup toolchain install
30-
RUN rustup target add wasm32-unknown-unknown
30+
RUN rustup target add wasm32v1-none
3131

3232
## Build fast-blocks node
3333
RUN ./scripts/localnet.sh --build-only

docs/consensus.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,10 @@ ssh -L 8888:localhost:8888 root@<your_vps_ip_address> -p <your_vps_port> -i ~/.s
297297
```bash
298298
sudo apt-get update && sudo apt install -y build-essential clang curl git make libssl-dev llvm libudev-dev protobuf-compiler python3 python3-pip \
299299
&& curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y \
300-
&& source ~/.cargo/env && rustup default stable && rustup update \
301-
&& rustup target add wasm32-unknown-unknown \
302-
&& rustup toolchain install nightly \
303-
&& rustup target add --toolchain nightly wasm32-unknown-unknown
304-
300+
&& source ~/.cargo/env \
301+
&& rustup default stable \
302+
&& rustup update \
303+
&& rustup target add wasm32v1-none
305304
```
306305

307306
3. **Clone the Subtensor repository and checkout the relevant branch:**

docs/rust-setup.md

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Installation
33
---
44
This guide is for reference only, please check the latest information on getting starting with Substrate
5-
[here](https://docs.polkadot.com/main-docs/install/).
5+
[here](https://docs.polkadot.com/develop/parachains/install-polkadot-sdk/).
66

77
This page will guide you through the **2 steps** needed to prepare a computer for **Substrate** development.
88
Since Substrate is built with [the Rust programming language](https://www.rust-lang.org/), the first
@@ -65,18 +65,15 @@ Open the Terminal application and execute the following commands:
6565
# Install Homebrew if necessary https://brew.sh/
6666
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
6767

68-
# Make sure Homebrew is up-to-date, install openssl
68+
# Make sure Homebrew is up-to-date, install protobuf and openssl
6969
brew update
70-
brew install openssl
70+
brew install protobuf openssl
7171
```
7272

7373
### Windows
7474

7575
**_PLEASE NOTE:_** Native Windows development of Substrate is _not_ very well supported! It is _highly_
76-
recommend to use [Windows Subsystem Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10)
77-
(WSL) and follow the instructions for [Ubuntu/Debian](#ubuntudebian).
78-
Please refer to the separate
79-
[guide for native Windows development](https://docs.polkadot.com/main-docs/install/windows/).
76+
recommend to use [Windows Subsystem Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10) (WSL) and follow the following [instructions](https://docs.polkadot.com/develop/parachains/install-polkadot-sdk/#windows-wsl).
8077

8178
## Rust developer environment
8279

@@ -90,13 +87,12 @@ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
9087
source ~/.cargo/env
9188
```
9289

93-
Configure the Rust toolchain to default to the latest stable version, add nightly and the nightly wasm target:
90+
Configure the Rust toolchain to default to the latest stable version:
9491

9592
```bash
9693
rustup default stable
9794
rustup update
98-
rustup update nightly
99-
rustup target add wasm32-unknown-unknown --toolchain nightly
95+
rustup target add wasm32v1-none
10096
```
10197

10298
## Test your set-up
@@ -125,38 +121,26 @@ rustup home: /home/user/.rustup
125121
126122
installed toolchains
127123
--------------------
128-
129124
stable-x86_64-unknown-linux-gnu (default)
130-
nightly-2020-10-06-x86_64-unknown-linux-gnu
131125
nightly-x86_64-unknown-linux-gnu
132126
133-
installed targets for active toolchain
134-
--------------------------------------
135-
136-
wasm32-unknown-unknown
137-
x86_64-unknown-linux-gnu
138-
139127
active toolchain
140128
----------------
141-
142-
stable-x86_64-unknown-linux-gnu (default)
143-
rustc 1.50.0 (cb75ad5db 2021-02-10)
129+
name: stable-x86_64-unknown-linux-gnu
130+
active because: it's the default toolchain
131+
installed targets:
132+
x86_64-unknown-linux-gnu
133+
wasm32v1-none
144134
```
145135

146136
As you can see above, the default toolchain is stable, and the
147-
`nightly-x86_64-unknown-linux-gnu` toolchain as well as its `wasm32-unknown-unknown` target is installed.
148-
You also see that `nightly-2020-10-06-x86_64-unknown-linux-gnu` is installed, but is not used unless explicitly defined as illustrated in the [specify your nightly version](#specifying-nightly-version)
137+
`stable-x86_64-unknown-linux-gnu` toolchain as well as its `wasm32v1-none` target is installed.
138+
You also see that `nightly-x86_64-unknown-linux-gnu` is installed, but is not used unless explicitly defined as illustrated in the [specify your nightly version](#specifying-nightly-version)
149139
section.
150140

151141
### WebAssembly compilation
152142

153-
Substrate uses [WebAssembly](https://webassembly.org) (Wasm) to produce portable blockchain
154-
runtimes. You will need to configure your Rust compiler to use
155-
[`nightly` builds](https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) to allow you to
156-
compile Substrate runtime code to the Wasm target.
157-
158-
> There are upstream issues in Rust that need to be resolved before all of Substrate can use the stable Rust toolchain.
159-
> [This is our tracking issue](https://github.com/paritytech/substrate/issues/1252) if you're curious as to why and how this will be resolved.
143+
Substrate uses [WebAssembly](https://webassembly.org) (Wasm) to produce portable blockchain runtimes.
160144

161145
#### Latest nightly for Substrate `master`
162146

@@ -168,7 +152,7 @@ To ensure your Rust compiler is always up to date, you should run:
168152
```bash
169153
rustup update
170154
rustup update nightly
171-
rustup target add wasm32-unknown-unknown --toolchain nightly
155+
rustup target add wasm32v1-none --toolchain nightly
172156
```
173157

174158
> NOTE: It may be necessary to occasionally rerun `rustup update` if a change in the upstream Substrate
@@ -197,7 +181,7 @@ rustup install nightly-<yyyy-MM-dd>
197181
Now, configure the nightly version to work with the Wasm compilation target:
198182

199183
```bash
200-
rustup target add wasm32-unknown-unknown --toolchain nightly-<yyyy-MM-dd>
184+
rustup target add wasm32v1-none --toolchain nightly-<yyyy-MM-dd>
201185
```
202186

203187
### Specifying nightly version
@@ -220,6 +204,6 @@ specific nightly version, follow these steps:
220204
```bash
221205
rustup uninstall nightly
222206
rustup install nightly-<yyyy-MM-dd>
223-
rustup target add wasm32-unknown-unknown --toolchain nightly-<yyyy-MM-dd>
207+
rustup target add wasm32v1-none --toolchain nightly-<yyyy-MM-dd>
224208
```
225209

pallets/subtensor/src/lib.rs

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,9 @@ pub enum CustomTransactionError {
19181918
BadRequest,
19191919
ZeroMaxAmount,
19201920
InvalidRevealRound,
1921+
CommitNotFound,
1922+
CommitBlockNotInRevealRange,
1923+
InputLengthsUnequal,
19211924
}
19221925

19231926
impl From<CustomTransactionError> for u8 {
@@ -1940,6 +1943,9 @@ impl From<CustomTransactionError> for u8 {
19401943
CustomTransactionError::BadRequest => 255,
19411944
CustomTransactionError::ZeroMaxAmount => 14,
19421945
CustomTransactionError::InvalidRevealRound => 15,
1946+
CustomTransactionError::CommitNotFound => 16,
1947+
CustomTransactionError::CommitBlockNotInRevealRange => 17,
1948+
CustomTransactionError::InputLengthsUnequal => 18,
19431949
}
19441950
}
19451951
}
@@ -2081,20 +2087,84 @@ where
20812087
Err(CustomTransactionError::StakeAmountTooLow.into())
20822088
}
20832089
}
2084-
Some(Call::reveal_weights { netuid, .. }) => {
2090+
Some(Call::reveal_weights {
2091+
netuid,
2092+
uids,
2093+
values,
2094+
salt,
2095+
version_key,
2096+
}) => {
20852097
if Self::check_weights_min_stake(who, *netuid) {
2086-
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
2087-
let validity = Self::validity_ok(priority);
2088-
Ok((validity, Some(who.clone()), origin))
2098+
let provided_hash = Pallet::<T>::get_commit_hash(
2099+
who,
2100+
*netuid,
2101+
uids,
2102+
values,
2103+
salt,
2104+
*version_key,
2105+
);
2106+
match Pallet::<T>::find_commit_block_via_hash(provided_hash) {
2107+
Some(commit_block) => {
2108+
if Pallet::<T>::is_reveal_block_range(*netuid, commit_block) {
2109+
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
2110+
let validity = Self::validity_ok(priority);
2111+
Ok((validity, Some(who.clone()), origin))
2112+
} else {
2113+
Err(CustomTransactionError::CommitBlockNotInRevealRange.into())
2114+
}
2115+
}
2116+
None => Err(CustomTransactionError::CommitNotFound.into()),
2117+
}
20892118
} else {
20902119
Err(CustomTransactionError::StakeAmountTooLow.into())
20912120
}
20922121
}
2093-
Some(Call::batch_reveal_weights { netuid, .. }) => {
2122+
Some(Call::batch_reveal_weights {
2123+
netuid,
2124+
uids_list,
2125+
values_list,
2126+
salts_list,
2127+
version_keys,
2128+
}) => {
20942129
if Self::check_weights_min_stake(who, *netuid) {
2095-
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
2096-
let validity = Self::validity_ok(priority);
2097-
Ok((validity, Some(who.clone()), origin))
2130+
let num_reveals = uids_list.len();
2131+
if num_reveals == values_list.len()
2132+
&& num_reveals == salts_list.len()
2133+
&& num_reveals == version_keys.len()
2134+
{
2135+
let provided_hashs = (0..num_reveals)
2136+
.map(|i| {
2137+
Pallet::<T>::get_commit_hash(
2138+
who,
2139+
*netuid,
2140+
uids_list.get(i).unwrap_or(&Vec::new()),
2141+
values_list.get(i).unwrap_or(&Vec::new()),
2142+
salts_list.get(i).unwrap_or(&Vec::new()),
2143+
*version_keys.get(i).unwrap_or(&0_u64),
2144+
)
2145+
})
2146+
.collect::<Vec<_>>();
2147+
2148+
let batch_reveal_block = provided_hashs
2149+
.iter()
2150+
.filter_map(|hash| Pallet::<T>::find_commit_block_via_hash(*hash))
2151+
.collect::<Vec<_>>();
2152+
2153+
if provided_hashs.len() == batch_reveal_block.len() {
2154+
if Pallet::<T>::is_batch_reveal_block_range(*netuid, batch_reveal_block)
2155+
{
2156+
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
2157+
let validity = Self::validity_ok(priority);
2158+
Ok((validity, Some(who.clone()), origin))
2159+
} else {
2160+
Err(CustomTransactionError::CommitBlockNotInRevealRange.into())
2161+
}
2162+
} else {
2163+
Err(CustomTransactionError::CommitNotFound.into())
2164+
}
2165+
} else {
2166+
Err(CustomTransactionError::InputLengthsUnequal.into())
2167+
}
20982168
} else {
20992169
Err(CustomTransactionError::StakeAmountTooLow.into())
21002170
}

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ mod dispatches {
279279
/// - Attempting to commit when the user has more than the allowed limit of unrevealed commits.
280280
///
281281
#[pallet::call_index(99)]
282-
#[pallet::weight((Weight::from_parts(50_980_000, 0)
282+
#[pallet::weight((Weight::from_parts(62_300_000, 0)
283283
.saturating_add(T::DbWeight::get().reads(7_u64))
284284
.saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Normal, Pays::No))]
285285
pub fn commit_crv3_weights(
@@ -827,7 +827,7 @@ mod dispatches {
827827
/// - The ip type v4 or v6.
828828
///
829829
#[pallet::call_index(5)]
830-
#[pallet::weight((Weight::from_parts(23_180_000, 0)
830+
#[pallet::weight((Weight::from_parts(22_640_000, 0)
831831
.saturating_add(T::DbWeight::get().reads(4))
832832
.saturating_add(T::DbWeight::get().writes(1)), DispatchClass::Normal, Pays::No))]
833833
pub fn serve_prometheus(
@@ -1045,7 +1045,7 @@ mod dispatches {
10451045
///
10461046
#[pallet::call_index(69)]
10471047
#[pallet::weight((
1048-
Weight::from_parts(3_630_000, 0)
1048+
Weight::from_parts(4_690_000, 0)
10491049
.saturating_add(T::DbWeight::get().reads(0))
10501050
.saturating_add(T::DbWeight::get().writes(1)),
10511051
DispatchClass::Operational,
@@ -1327,7 +1327,7 @@ mod dispatches {
13271327
/// - Consider adding checks to prevent scheduling too far into the future.
13281328
/// TODO: Benchmark this call
13291329
#[pallet::call_index(73)]
1330-
#[pallet::weight((Weight::from_parts(28_770_000, 0)
1330+
#[pallet::weight((Weight::from_parts(47_820_000, 0)
13311331
.saturating_add(T::DbWeight::get().reads(4))
13321332
.saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Operational, Pays::Yes))]
13331333
pub fn schedule_swap_coldkey(

pallets/subtensor/src/subnets/weights.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,14 +377,8 @@ impl<T: Config> Pallet<T> {
377377
}
378378

379379
// --- 5. Hash the provided data.
380-
let provided_hash: H256 = BlakeTwo256::hash_of(&(
381-
who.clone(),
382-
netuid,
383-
uids.clone(),
384-
values.clone(),
385-
salt.clone(),
386-
version_key,
387-
));
380+
let provided_hash: H256 =
381+
Self::get_commit_hash(&who, netuid, &uids, &values, &salt, version_key);
388382

389383
// --- 6. After removing expired commits, check if any commits are left.
390384
if commits.is_empty() {
@@ -1083,4 +1077,30 @@ impl<T: Config> Pallet<T> {
10831077
.saturating_mul(tempo_plus_one)
10841078
.saturating_sub(netuid_plus_one)
10851079
}
1080+
1081+
pub fn get_commit_hash(
1082+
who: &T::AccountId,
1083+
netuid: NetUid,
1084+
uids: &[u16],
1085+
values: &[u16],
1086+
salt: &[u16],
1087+
version_key: u64,
1088+
) -> H256 {
1089+
BlakeTwo256::hash_of(&(who.clone(), netuid, uids, values, salt, version_key))
1090+
}
1091+
1092+
pub fn find_commit_block_via_hash(hash: H256) -> Option<u64> {
1093+
WeightCommits::<T>::iter().find_map(|(_, _, commits)| {
1094+
commits
1095+
.iter()
1096+
.find(|(h, _, _, _)| *h == hash)
1097+
.map(|(_, commit_block, _, _)| *commit_block)
1098+
})
1099+
}
1100+
1101+
pub fn is_batch_reveal_block_range(netuid: NetUid, commit_block: Vec<u64>) -> bool {
1102+
commit_block
1103+
.iter()
1104+
.all(|block| Self::is_reveal_block_range(netuid, *block))
1105+
}
10861106
}

0 commit comments

Comments
 (0)