Skip to content

Commit 03527fa

Browse files
authored
Merge pull request #984 from opentensor/fix-evm
fix EVM <=> TAO balance transfer precision
2 parents 5bcb978 + ed8288d commit 03527fa

File tree

8 files changed

+120
-92
lines changed

8 files changed

+120
-92
lines changed

.github/workflows/cargo-audit.yml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,11 @@ jobs:
2323
sudo apt-get update &&
2424
sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler
2525
26-
- name: Install Rust Stable
27-
uses: actions-rs/[email protected]
28-
with:
29-
toolchain: stable
30-
components: rustfmt, clippy
31-
profile: minimal
32-
33-
- name: Utilize Shared Rust Cache
34-
uses: Swatinem/[email protected]
35-
with:
36-
key: ubuntu-latest-${{ env.RUST_BIN_DIR }}
37-
3826
- name: Install cargo-audit
39-
run: cargo install --version 0.20.1 cargo-audit
27+
run: cargo install --version 0.20.1 --force cargo-audit
28+
29+
- name: Display cargo-audit --version
30+
run: cargo audit --version
4031

4132
- name: cargo audit
4233
run: cargo audit --ignore RUSTSEC-2024-0336 # rustls issue; wait for upstream to resolve this

.github/workflows/check-devnet.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ jobs:
2020
sudo apt-get install -y curl clang curl libssl-dev llvm \
2121
libudev-dev protobuf-compiler
2222
23-
- name: Set up Rust Toolchain
24-
run: curl https://sh.rustup.rs -sSf | sh -s -- -y
25-
2623
- name: Install substrate-spec-version
2724
run: cargo install substrate-spec-version
2825

.github/workflows/check-finney.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ jobs:
2020
sudo apt-get install -y curl clang curl libssl-dev llvm \
2121
libudev-dev protobuf-compiler
2222
23-
- name: Set up Rust Toolchain
24-
run: curl https://sh.rustup.rs -sSf | sh -s -- -y
25-
2623
- name: Install substrate-spec-version
2724
run: cargo install substrate-spec-version
2825

.github/workflows/check-rust.yml

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ jobs:
4343
env:
4444
RELEASE_NAME: development
4545
# RUSTFLAGS: -A warnings
46-
RUSTV: ${{ matrix.rust-branch }}
4746
RUST_BACKTRACE: full
48-
RUST_BIN_DIR: target/${{ matrix.rust-target }}
4947
SKIP_WASM_BUILD: 1
5048
TARGET: ${{ matrix.rust-target }}
5149
steps:
@@ -55,10 +53,10 @@ jobs:
5553
- name: Install dependencies
5654
run: sudo apt-get update && sudo apt-get install -y build-essential
5755

58-
- name: Install Rust ${{ matrix.rust-branch }}
56+
- name: Install Rust Nightly
5957
uses: actions-rs/[email protected]
6058
with:
61-
toolchain: ${{ matrix.rust-branch }}
59+
toolchain: nightly
6260
components: rustfmt
6361
profile: minimal
6462

@@ -84,11 +82,10 @@ jobs:
8482
env:
8583
RELEASE_NAME: development
8684
# RUSTFLAGS: -A warnings
87-
RUSTV: ${{ matrix.rust-branch }}
8885
RUST_BACKTRACE: full
89-
RUST_BIN_DIR: target/${{ matrix.rust-target }}
9086
SKIP_WASM_BUILD: 1
9187
TARGET: ${{ matrix.rust-target }}
88+
RUST_BIN_DIR: target/${{ matrix.rust-target }}
9289
steps:
9390
- name: Check-out repository under $GITHUB_WORKSPACE
9491
uses: actions/checkout@v4
@@ -98,13 +95,6 @@ jobs:
9895
sudo apt-get update &&
9996
sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler
10097
101-
- name: Install Rust ${{ matrix.rust-branch }}
102-
uses: actions-rs/[email protected]
103-
with:
104-
toolchain: ${{ matrix.rust-branch }}
105-
components: rustfmt, clippy
106-
profile: minimal
107-
10898
- name: Utilize Shared Rust Cache
10999
uses: Swatinem/[email protected]
110100
with:
@@ -128,12 +118,11 @@ jobs:
128118
# - macos-latest
129119
env:
130120
RELEASE_NAME: development
131-
RUSTV: ${{ matrix.rust-branch }}
132121
RUSTFLAGS: -D warnings
133122
RUST_BACKTRACE: full
134-
RUST_BIN_DIR: target/${{ matrix.rust-target }}
135123
SKIP_WASM_BUILD: 1
136124
TARGET: ${{ matrix.rust-target }}
125+
RUST_BIN_DIR: target/${{ matrix.rust-target }}
137126
steps:
138127
- name: Check-out repository under $GITHUB_WORKSPACE
139128
uses: actions/checkout@v4
@@ -166,8 +155,6 @@ jobs:
166155
runs-on: SubtensorCI
167156
strategy:
168157
matrix:
169-
rust-branch:
170-
- stable
171158
rust-target:
172159
- x86_64-unknown-linux-gnu
173160
# - x86_64-apple-darwin
@@ -180,7 +167,6 @@ jobs:
180167
env:
181168
RELEASE_NAME: development
182169
# RUSTFLAGS: -A warnings
183-
RUSTV: ${{ matrix.rust-branch }}
184170
RUST_BACKTRACE: full
185171
RUST_BIN_DIR: target/${{ matrix.rust-target }}
186172
SKIP_WASM_BUILD: 1
@@ -194,13 +180,6 @@ jobs:
194180
sudo apt-get update &&
195181
sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler
196182
197-
- name: Install Rust ${{ matrix.rust-branch }}
198-
uses: actions-rs/[email protected]
199-
with:
200-
toolchain: ${{ matrix.rust-branch }}
201-
components: rustfmt, clippy
202-
profile: minimal
203-
204183
- name: Utilize Shared Rust Cache
205184
uses: Swatinem/[email protected]
206185
with:
@@ -215,8 +194,6 @@ jobs:
215194
runs-on: SubtensorCI
216195
strategy:
217196
matrix:
218-
rust-branch:
219-
- stable
220197
rust-target:
221198
- x86_64-unknown-linux-gnu
222199
# - x86_64-apple-darwin
@@ -229,7 +206,6 @@ jobs:
229206
env:
230207
RELEASE_NAME: development
231208
# RUSTFLAGS: -A warnings
232-
RUSTV: ${{ matrix.rust-branch }}
233209
RUST_BACKTRACE: full
234210
RUST_BIN_DIR: target/${{ matrix.rust-target }}
235211
SKIP_WASM_BUILD: 1
@@ -243,13 +219,6 @@ jobs:
243219
sudo apt-get update &&
244220
sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler
245221
246-
- name: Install Rust ${{ matrix.rust-branch }}
247-
uses: actions-rs/[email protected]
248-
with:
249-
toolchain: ${{ matrix.rust-branch }}
250-
components: rustfmt, clippy
251-
profile: minimal
252-
253222
- name: Utilize Rust shared cached
254223
uses: Swatinem/[email protected]
255224
with:
@@ -278,7 +247,6 @@ jobs:
278247
env:
279248
RELEASE_NAME: development
280249
# RUSTFLAGS: -A warnings
281-
RUSTV: ${{ matrix.rust-branch }}
282250
RUST_BACKTRACE: full
283251
RUST_BIN_DIR: target/${{ matrix.rust-target }}
284252
SKIP_WASM_BUILD: 1
@@ -292,13 +260,6 @@ jobs:
292260
sudo apt-get update &&
293261
sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler
294262
295-
- name: Install Rust ${{ matrix.rust-branch }}
296-
uses: actions-rs/[email protected]
297-
with:
298-
toolchain: ${{ matrix.rust-branch }}
299-
components: rustfmt, clippy
300-
profile: minimal
301-
302263
- name: Utilize Rust shared cached
303264
uses: Swatinem/[email protected]
304265
with:
@@ -322,12 +283,6 @@ jobs:
322283
runs-on: SubtensorCI
323284

324285
steps:
325-
- name: Install stable Rust
326-
uses: actions-rs/toolchain@v1
327-
with:
328-
profile: minimal
329-
toolchain: stable
330-
331286
- name: Install Zepter
332287
run: cargo install --locked -q zepter && zepter --version
333288

.github/workflows/check-testnet.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ jobs:
2020
sudo apt-get install -y curl clang curl libssl-dev llvm \
2121
libudev-dev protobuf-compiler
2222
23-
- name: Set up Rust Toolchain
24-
run: curl https://sh.rustup.rs -sSf | sh -s -- -y
25-
2623
- name: Install substrate-spec-version
2724
run: cargo install substrate-spec-version
2825

.github/workflows/try-runtime.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: SubtensorCI
1515
steps:
1616
- name: Checkout sources
17-
uses: actions/checkout@v3
17+
uses: actions/checkout@v4
1818

1919
- name: Run Try Runtime Checks
2020
uses: "paritytech/[email protected]"
@@ -29,7 +29,7 @@ jobs:
2929
runs-on: SubtensorCI
3030
steps:
3131
- name: Checkout sources
32-
uses: actions/checkout@v3
32+
uses: actions/checkout@v4
3333

3434
- name: Run Try Runtime Checks
3535
uses: "paritytech/[email protected]"

runtime/src/lib.rs

Lines changed: 91 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,18 +1119,34 @@ parameter_types! {
11191119
const EVM_DECIMALS_FACTOR: u64 = 1_000_000_000_u64;
11201120

11211121
pub struct SubtensorEvmBalanceConverter;
1122+
11221123
impl BalanceConverter for SubtensorEvmBalanceConverter {
1124+
/// Convert from Substrate balance (u64) to EVM balance (U256)
11231125
fn into_evm_balance(value: U256) -> Option<U256> {
1124-
U256::from(UniqueSaturatedInto::<u128>::unique_saturated_into(value))
1126+
value
11251127
.checked_mul(U256::from(EVM_DECIMALS_FACTOR))
1128+
.and_then(|evm_value| {
1129+
// Ensure the result fits within the maximum U256 value
1130+
if evm_value <= U256::MAX {
1131+
Some(evm_value)
1132+
} else {
1133+
None
1134+
}
1135+
})
11261136
}
11271137

1138+
/// Convert from EVM balance (U256) to Substrate balance (u64)
11281139
fn into_substrate_balance(value: U256) -> Option<U256> {
1129-
if value <= U256::from(u64::MAX) {
1130-
value.checked_div(U256::from(EVM_DECIMALS_FACTOR))
1131-
} else {
1132-
None
1133-
}
1140+
value
1141+
.checked_div(U256::from(EVM_DECIMALS_FACTOR))
1142+
.and_then(|substrate_value| {
1143+
// Ensure the result fits within the TAO balance type (u64)
1144+
if substrate_value <= U256::from(u64::MAX) {
1145+
Some(substrate_value)
1146+
} else {
1147+
None
1148+
}
1149+
})
11341150
}
11351151
}
11361152

@@ -2003,9 +2019,6 @@ impl_runtime_apis! {
20032019
}
20042020
}
20052021

2006-
// #[cfg(test)]
2007-
// mod tests {
2008-
20092022
#[test]
20102023
fn check_whitelist() {
20112024
use crate::*;
@@ -2028,4 +2041,72 @@ fn check_whitelist() {
20282041
// System Events
20292042
assert!(whitelist.contains("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7"));
20302043
}
2031-
// }
2044+
2045+
#[test]
2046+
fn test_into_substrate_balance_valid() {
2047+
// Valid conversion within u64 range
2048+
let evm_balance = U256::from(1_000_000_000_000_000_000u128); // 1 TAO in EVM
2049+
let expected_substrate_balance = U256::from(1_000_000_000u128); // 1 TAO in Substrate
2050+
2051+
let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
2052+
assert_eq!(result, Some(expected_substrate_balance));
2053+
}
2054+
2055+
#[test]
2056+
fn test_into_substrate_balance_large_value() {
2057+
// Maximum valid balance for u64
2058+
let evm_balance = U256::from(u64::MAX) * U256::from(EVM_DECIMALS_FACTOR); // Max u64 TAO in EVM
2059+
let expected_substrate_balance = U256::from(u64::MAX);
2060+
2061+
let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
2062+
assert_eq!(result, Some(expected_substrate_balance));
2063+
}
2064+
2065+
#[test]
2066+
fn test_into_substrate_balance_exceeds_u64() {
2067+
// EVM balance that exceeds u64 after conversion
2068+
let evm_balance = (U256::from(u64::MAX) + U256::from(1)) * U256::from(EVM_DECIMALS_FACTOR);
2069+
2070+
let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
2071+
assert_eq!(result, None); // Exceeds u64, should return None
2072+
}
2073+
2074+
#[test]
2075+
fn test_into_substrate_balance_precision_loss() {
2076+
// EVM balance with precision loss
2077+
let evm_balance = U256::from(1_000_000_000_123_456_789u128); // 1 TAO + extra precision in EVM
2078+
let expected_substrate_balance = U256::from(1_000_000_000u128); // Truncated to 1 TAO in Substrate
2079+
2080+
let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
2081+
assert_eq!(result, Some(expected_substrate_balance));
2082+
}
2083+
2084+
#[test]
2085+
fn test_into_substrate_balance_zero_value() {
2086+
// Zero balance should convert to zero
2087+
let evm_balance = U256::from(0);
2088+
let expected_substrate_balance = U256::from(0);
2089+
2090+
let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
2091+
assert_eq!(result, Some(expected_substrate_balance));
2092+
}
2093+
2094+
#[test]
2095+
fn test_into_evm_balance_valid() {
2096+
// Valid conversion from Substrate to EVM
2097+
let substrate_balance = U256::from(1_000_000_000u128); // 1 TAO in Substrate
2098+
let expected_evm_balance = U256::from(1_000_000_000_000_000_000u128); // 1 TAO in EVM
2099+
2100+
let result = SubtensorEvmBalanceConverter::into_evm_balance(substrate_balance);
2101+
assert_eq!(result, Some(expected_evm_balance));
2102+
}
2103+
2104+
#[test]
2105+
fn test_into_evm_balance_overflow() {
2106+
// Substrate balance larger than u64::MAX but valid within U256
2107+
let substrate_balance = U256::from(u64::MAX) + U256::from(1); // Large balance
2108+
let expected_evm_balance = substrate_balance * U256::from(EVM_DECIMALS_FACTOR);
2109+
2110+
let result = SubtensorEvmBalanceConverter::into_evm_balance(substrate_balance);
2111+
assert_eq!(result, Some(expected_evm_balance)); // Should return the scaled value
2112+
}

0 commit comments

Comments
 (0)