Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix EVM <=> TAO balance transfer precision #984

Merged
merged 20 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions .github/workflows/cargo-audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,11 @@ jobs:
sudo apt-get update &&
sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler

- name: Install Rust Stable
uses: actions-rs/[email protected]
with:
toolchain: stable
components: rustfmt, clippy
profile: minimal

- name: Utilize Shared Rust Cache
uses: Swatinem/[email protected]
with:
key: ubuntu-latest-${{ env.RUST_BIN_DIR }}

- name: Install cargo-audit
run: cargo install --version 0.20.1 cargo-audit
run: cargo install --version 0.20.1 --force cargo-audit

- name: Display cargo-audit --version
run: cargo audit --version

- name: cargo audit
run: cargo audit --ignore RUSTSEC-2024-0336 # rustls issue; wait for upstream to resolve this
3 changes: 0 additions & 3 deletions .github/workflows/check-devnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ jobs:
sudo apt-get install -y curl clang curl libssl-dev llvm \
libudev-dev protobuf-compiler

- name: Set up Rust Toolchain
run: curl https://sh.rustup.rs -sSf | sh -s -- -y

- name: Install substrate-spec-version
run: cargo install substrate-spec-version

Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/check-finney.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ jobs:
sudo apt-get install -y curl clang curl libssl-dev llvm \
libudev-dev protobuf-compiler

- name: Set up Rust Toolchain
run: curl https://sh.rustup.rs -sSf | sh -s -- -y

- name: Install substrate-spec-version
run: cargo install substrate-spec-version

Expand Down
53 changes: 4 additions & 49 deletions .github/workflows/check-rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ jobs:
env:
RELEASE_NAME: development
# RUSTFLAGS: -A warnings
RUSTV: ${{ matrix.rust-branch }}
RUST_BACKTRACE: full
RUST_BIN_DIR: target/${{ matrix.rust-target }}
SKIP_WASM_BUILD: 1
TARGET: ${{ matrix.rust-target }}
steps:
Expand All @@ -55,10 +53,10 @@ jobs:
- name: Install dependencies
run: sudo apt-get update && sudo apt-get install -y build-essential

- name: Install Rust ${{ matrix.rust-branch }}
- name: Install Rust Nightly
uses: actions-rs/[email protected]
with:
toolchain: ${{ matrix.rust-branch }}
toolchain: nightly
components: rustfmt
profile: minimal

Expand All @@ -84,11 +82,10 @@ jobs:
env:
RELEASE_NAME: development
# RUSTFLAGS: -A warnings
RUSTV: ${{ matrix.rust-branch }}
RUST_BACKTRACE: full
RUST_BIN_DIR: target/${{ matrix.rust-target }}
SKIP_WASM_BUILD: 1
TARGET: ${{ matrix.rust-target }}
RUST_BIN_DIR: target/${{ matrix.rust-target }}
steps:
- name: Check-out repository under $GITHUB_WORKSPACE
uses: actions/checkout@v4
Expand All @@ -98,13 +95,6 @@ jobs:
sudo apt-get update &&
sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler

- name: Install Rust ${{ matrix.rust-branch }}
uses: actions-rs/[email protected]
with:
toolchain: ${{ matrix.rust-branch }}
components: rustfmt, clippy
profile: minimal

- name: Utilize Shared Rust Cache
uses: Swatinem/[email protected]
with:
Expand All @@ -128,12 +118,11 @@ jobs:
# - macos-latest
env:
RELEASE_NAME: development
RUSTV: ${{ matrix.rust-branch }}
RUSTFLAGS: -D warnings
RUST_BACKTRACE: full
RUST_BIN_DIR: target/${{ matrix.rust-target }}
SKIP_WASM_BUILD: 1
TARGET: ${{ matrix.rust-target }}
RUST_BIN_DIR: target/${{ matrix.rust-target }}
steps:
- name: Check-out repository under $GITHUB_WORKSPACE
uses: actions/checkout@v4
Expand Down Expand Up @@ -166,8 +155,6 @@ jobs:
runs-on: SubtensorCI
strategy:
matrix:
rust-branch:
- stable
rust-target:
- x86_64-unknown-linux-gnu
# - x86_64-apple-darwin
Expand All @@ -180,7 +167,6 @@ jobs:
env:
RELEASE_NAME: development
# RUSTFLAGS: -A warnings
RUSTV: ${{ matrix.rust-branch }}
RUST_BACKTRACE: full
RUST_BIN_DIR: target/${{ matrix.rust-target }}
SKIP_WASM_BUILD: 1
Expand All @@ -194,13 +180,6 @@ jobs:
sudo apt-get update &&
sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler

- name: Install Rust ${{ matrix.rust-branch }}
uses: actions-rs/[email protected]
with:
toolchain: ${{ matrix.rust-branch }}
components: rustfmt, clippy
profile: minimal

- name: Utilize Shared Rust Cache
uses: Swatinem/[email protected]
with:
Expand All @@ -215,8 +194,6 @@ jobs:
runs-on: SubtensorCI
strategy:
matrix:
rust-branch:
- stable
rust-target:
- x86_64-unknown-linux-gnu
# - x86_64-apple-darwin
Expand All @@ -229,7 +206,6 @@ jobs:
env:
RELEASE_NAME: development
# RUSTFLAGS: -A warnings
RUSTV: ${{ matrix.rust-branch }}
RUST_BACKTRACE: full
RUST_BIN_DIR: target/${{ matrix.rust-target }}
SKIP_WASM_BUILD: 1
Expand All @@ -243,13 +219,6 @@ jobs:
sudo apt-get update &&
sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler

- name: Install Rust ${{ matrix.rust-branch }}
uses: actions-rs/[email protected]
with:
toolchain: ${{ matrix.rust-branch }}
components: rustfmt, clippy
profile: minimal

- name: Utilize Rust shared cached
uses: Swatinem/[email protected]
with:
Expand Down Expand Up @@ -278,7 +247,6 @@ jobs:
env:
RELEASE_NAME: development
# RUSTFLAGS: -A warnings
RUSTV: ${{ matrix.rust-branch }}
RUST_BACKTRACE: full
RUST_BIN_DIR: target/${{ matrix.rust-target }}
SKIP_WASM_BUILD: 1
Expand All @@ -292,13 +260,6 @@ jobs:
sudo apt-get update &&
sudo apt-get install -y clang curl libssl-dev llvm libudev-dev protobuf-compiler

- name: Install Rust ${{ matrix.rust-branch }}
uses: actions-rs/[email protected]
with:
toolchain: ${{ matrix.rust-branch }}
components: rustfmt, clippy
profile: minimal

- name: Utilize Rust shared cached
uses: Swatinem/[email protected]
with:
Expand All @@ -322,12 +283,6 @@ jobs:
runs-on: SubtensorCI

steps:
- name: Install stable Rust
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable

- name: Install Zepter
run: cargo install --locked -q zepter && zepter --version

Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/check-testnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ jobs:
sudo apt-get install -y curl clang curl libssl-dev llvm \
libudev-dev protobuf-compiler

- name: Set up Rust Toolchain
run: curl https://sh.rustup.rs -sSf | sh -s -- -y

- name: Install substrate-spec-version
run: cargo install substrate-spec-version

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/try-runtime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: SubtensorCI
steps:
- name: Checkout sources
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Run Try Runtime Checks
uses: "paritytech/[email protected]"
Expand All @@ -29,7 +29,7 @@ jobs:
runs-on: SubtensorCI
steps:
- name: Checkout sources
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Run Try Runtime Checks
uses: "paritytech/[email protected]"
Expand Down
101 changes: 91 additions & 10 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1119,18 +1119,34 @@ parameter_types! {
const EVM_DECIMALS_FACTOR: u64 = 1_000_000_000_u64;

pub struct SubtensorEvmBalanceConverter;

impl BalanceConverter for SubtensorEvmBalanceConverter {
/// Convert from Substrate balance (u64) to EVM balance (U256)
fn into_evm_balance(value: U256) -> Option<U256> {
U256::from(UniqueSaturatedInto::<u128>::unique_saturated_into(value))
value
.checked_mul(U256::from(EVM_DECIMALS_FACTOR))
.and_then(|evm_value| {
// Ensure the result fits within the maximum U256 value
if evm_value <= U256::MAX {
Some(evm_value)
} else {
None
}
})
}

/// Convert from EVM balance (U256) to Substrate balance (u64)
fn into_substrate_balance(value: U256) -> Option<U256> {
if value <= U256::from(u64::MAX) {
value.checked_div(U256::from(EVM_DECIMALS_FACTOR))
} else {
None
}
value
.checked_div(U256::from(EVM_DECIMALS_FACTOR))
.and_then(|substrate_value| {
// Ensure the result fits within the TAO balance type (u64)
if substrate_value <= U256::from(u64::MAX) {
Some(substrate_value)
} else {
None
}
})
}
}

Expand Down Expand Up @@ -2003,9 +2019,6 @@ impl_runtime_apis! {
}
}

// #[cfg(test)]
// mod tests {

#[test]
fn check_whitelist() {
use crate::*;
Expand All @@ -2028,4 +2041,72 @@ fn check_whitelist() {
// System Events
assert!(whitelist.contains("26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7"));
}
// }

#[test]
fn test_into_substrate_balance_valid() {
// Valid conversion within u64 range
let evm_balance = U256::from(1_000_000_000_000_000_000u128); // 1 TAO in EVM
let expected_substrate_balance = U256::from(1_000_000_000u128); // 1 TAO in Substrate

let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
assert_eq!(result, Some(expected_substrate_balance));
}

#[test]
fn test_into_substrate_balance_large_value() {
// Maximum valid balance for u64
let evm_balance = U256::from(u64::MAX) * U256::from(EVM_DECIMALS_FACTOR); // Max u64 TAO in EVM
let expected_substrate_balance = U256::from(u64::MAX);

let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
assert_eq!(result, Some(expected_substrate_balance));
}

#[test]
fn test_into_substrate_balance_exceeds_u64() {
// EVM balance that exceeds u64 after conversion
let evm_balance = (U256::from(u64::MAX) + U256::from(1)) * U256::from(EVM_DECIMALS_FACTOR);

let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
assert_eq!(result, None); // Exceeds u64, should return None
}

#[test]
fn test_into_substrate_balance_precision_loss() {
// EVM balance with precision loss
let evm_balance = U256::from(1_000_000_000_123_456_789u128); // 1 TAO + extra precision in EVM
let expected_substrate_balance = U256::from(1_000_000_000u128); // Truncated to 1 TAO in Substrate

let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
assert_eq!(result, Some(expected_substrate_balance));
}

#[test]
fn test_into_substrate_balance_zero_value() {
// Zero balance should convert to zero
let evm_balance = U256::from(0);
let expected_substrate_balance = U256::from(0);

let result = SubtensorEvmBalanceConverter::into_substrate_balance(evm_balance);
assert_eq!(result, Some(expected_substrate_balance));
}

#[test]
fn test_into_evm_balance_valid() {
// Valid conversion from Substrate to EVM
let substrate_balance = U256::from(1_000_000_000u128); // 1 TAO in Substrate
let expected_evm_balance = U256::from(1_000_000_000_000_000_000u128); // 1 TAO in EVM

let result = SubtensorEvmBalanceConverter::into_evm_balance(substrate_balance);
assert_eq!(result, Some(expected_evm_balance));
}

#[test]
fn test_into_evm_balance_overflow() {
// Substrate balance larger than u64::MAX but valid within U256
let substrate_balance = U256::from(u64::MAX) + U256::from(1); // Large balance
let expected_evm_balance = substrate_balance * U256::from(EVM_DECIMALS_FACTOR);

let result = SubtensorEvmBalanceConverter::into_evm_balance(substrate_balance);
assert_eq!(result, Some(expected_evm_balance)); // Should return the scaled value
}
Loading
Loading