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

Remove invalid bundles in the process of compiling bundles to extrinsics #1700

Merged
merged 8 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/sp-domains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ subspace-core-primitives = { version = "0.1.0", default-features = false, path =
subspace-runtime-primitives = { version = "0.1.0", default-features = false, path = "../subspace-runtime-primitives" }
thiserror = { version = "1.0.38", optional = true }

[dev-dependencies]
num-traits = "0.2.15"

[features]
default = ["std"]
std = [
Expand Down
10 changes: 9 additions & 1 deletion crates/sp-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
pub mod bundle_producer_election;
pub mod fraud_proof;
pub mod merkle_tree;
#[cfg(test)]
mod tests;
pub mod transaction;

use bundle_producer_election::{BundleProducerElectionParams, VrfProofError};
Expand All @@ -40,7 +42,7 @@ use sp_runtime_interface::{pass_by, runtime_interface};
use sp_std::vec::Vec;
use sp_weights::Weight;
use subspace_core_primitives::crypto::blake2b_256_hash;
use subspace_core_primitives::{Blake2b256Hash, Randomness, U256};
use subspace_core_primitives::{bidirectional_distance, Blake2b256Hash, Randomness, U256};
use subspace_runtime_primitives::{Balance, Moment};

/// Key type for Operator.
Expand Down Expand Up @@ -569,6 +571,12 @@ pub struct DomainBlockLimit {
pub max_block_weight: Weight,
}

/// Checks if the signer Id hash is within the tx range
pub fn signer_in_tx_range(bundle_vrf_hash: &U256, signer_id_hash: &U256, tx_range: &U256) -> bool {
let distance_from_vrf_hash = bidirectional_distance(bundle_vrf_hash, signer_id_hash);
distance_from_vrf_hash <= (*tx_range / 2)
}

sp_api::decl_runtime_apis! {
/// API necessary for domains pallet.
pub trait DomainsApi<DomainNumber: Encode + Decode, DomainHash: Encode + Decode> {
Expand Down
209 changes: 209 additions & 0 deletions crates/sp-domains/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
use crate::signer_in_tx_range;
use num_traits::ops::wrapping::{WrappingAdd, WrappingSub};
use subspace_core_primitives::U256;

#[test]
fn test_tx_range() {
let tx_range = U256::MAX / 4;
let bundle_vrf_hash = U256::MAX / 2;

let signer_id_hash = bundle_vrf_hash + U256::from(10_u64);
assert!(signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash - U256::from(10_u64);
assert!(signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash + U256::MAX / 8;
assert!(signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash - U256::MAX / 8;
assert!(signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash + U256::MAX / 8 + U256::from(1_u64);
assert!(!signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash - U256::MAX / 8 - U256::from(1_u64);
assert!(!signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash + U256::MAX / 4;
assert!(!signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash - U256::MAX / 4;
assert!(!signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));
}

#[test]
fn test_tx_range_wrap_under_flow() {
let tx_range = U256::MAX / 4;
let bundle_vrf_hash = U256::from(100_u64);

let signer_id_hash = bundle_vrf_hash + U256::from(1000_u64);
assert!(signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash.wrapping_sub(&U256::from(1000_u64));
assert!(signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash + U256::MAX / 8;
assert!(signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let v = U256::MAX / 8;
let signer_id_hash = bundle_vrf_hash.wrapping_sub(&v);
assert!(signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash + U256::MAX / 8 + U256::from(1_u64);
assert!(!signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let v = U256::MAX / 8 + U256::from(1_u64);
let signer_id_hash = bundle_vrf_hash.wrapping_sub(&v);
assert!(!signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash + U256::MAX / 4;
assert!(!signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let v = U256::MAX / 4;
let signer_id_hash = bundle_vrf_hash.wrapping_sub(&v);
assert!(!signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));
}

#[test]
fn test_tx_range_wrap_over_flow() {
let tx_range = U256::MAX / 4;
let v = U256::MAX;
let bundle_vrf_hash = v.wrapping_sub(&U256::from(100_u64));

let signer_id_hash = bundle_vrf_hash.wrapping_add(&U256::from(1000_u64));
assert!(signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash - U256::from(1000_u64);
assert!(signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let v = U256::MAX / 8;
let signer_id_hash = bundle_vrf_hash.wrapping_add(&v);
assert!(signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash - U256::MAX / 8;
assert!(signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let v = U256::MAX / 8 + U256::from(1_u64);
let signer_id_hash = bundle_vrf_hash.wrapping_add(&v);
assert!(!signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash - U256::MAX / 8 - U256::from(1_u64);
assert!(!signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let v = U256::MAX / 4;
let signer_id_hash = bundle_vrf_hash.wrapping_add(&v);
assert!(!signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));

let signer_id_hash = bundle_vrf_hash - U256::MAX / 4;
assert!(!signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));
}

#[test]
fn test_tx_range_max() {
let tx_range = U256::MAX;
let bundle_vrf_hash = U256::MAX / 2;

let signer_id_hash = bundle_vrf_hash + U256::from(10_u64);
assert!(signer_in_tx_range(
&bundle_vrf_hash,
&signer_id_hash,
&tx_range
));
}
99 changes: 0 additions & 99 deletions crates/subspace-fraud-proof/src/domain_extrinsics_builder.rs

This file was deleted.

Loading