From d28e93e6ada9c3b8228ea86b55a22dae28acf65c Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Fri, 30 Jun 2023 09:27:12 +0800 Subject: [PATCH] Release `v0.1.5` (#20) --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/hunter.rs | 21 ++++++------------ src/hunter/util.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++ src/primitive.rs | 5 ++--- 5 files changed, 64 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8437fa9..8d3e928 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2868,7 +2868,7 @@ dependencies = [ [[package]] name = "slothunter" -version = "0.1.4" +version = "0.1.5" dependencies = [ "anyhow", "app_dirs2", diff --git a/Cargo.toml b/Cargo.toml index b6a18a2..9282496 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ license = "GPL-3.0" name = "slothunter" readme = "README.md" repository = "https://github.com/hack-ink/slothunter" -version = "0.1.4" +version = "0.1.5" [features] node-test = [] diff --git a/src/hunter.rs b/src/hunter.rs index 3135f7f..04d0319 100644 --- a/src/hunter.rs +++ b/src/hunter.rs @@ -355,21 +355,12 @@ impl Hunter { } fn check_leases(&self, first_lease_period: u32) { - const E_INVALID_LEASES: &str = "invalid leases configuration"; - - let c_first = self.configuration.bid.leases.0; - let c_last = self.configuration.bid.leases.1; - let first = first_lease_period; - let last = first_lease_period + C_RANGE_COUNT - 1; - - assert!( - c_first >= first, - "{E_INVALID_LEASES}, available range(#{first}, #{last}) but found range(#{c_first}, #{c_last})" - ); - assert!( - c_last <= last, - "{E_INVALID_LEASES}, available range(#{first}, #{last}) but found range(#{c_first}, #{c_last})" - ); + let a @ (first, last) = util::range_of(first_lease_period); + let b @ (c_first, c_last) = self.configuration.bid.leases; + + if !util::check_leases(&a, &b) { + panic!("invalid leases configuration, available range(#{first}, #{last}) but found range(#{c_first}, #{c_last})") + } } async fn analyze_bidders( diff --git a/src/hunter/util.rs b/src/hunter/util.rs index b91eb88..a7cb843 100644 --- a/src/hunter/util.rs +++ b/src/hunter/util.rs @@ -70,6 +70,60 @@ fn crowdloan_id_of_should_work() { ); } +pub fn range_of(first_lease_period: u32) -> SlotRange { + (first_lease_period, first_lease_period + C_RANGE_COUNT - 1) +} +#[test] +fn range_of_should_work() { + assert_eq!(range_of(10), (10, 17)); +} + +pub fn check_leases(a: &SlotRange, b: &SlotRange) -> bool { + (a.0 <= b.0) && (a.1 >= b.1) +} +#[test] +fn check_leases_should_work() { + // Test when a completely overlaps b. + let a = (5, 10); + let b = (7, 8); + assert!(check_leases(&a, &b)); + + // Test when b completely overlaps a. + let a = (5, 10); + let b = (3, 12); + assert!(!check_leases(&a, &b)); + + // Test when a starts before b and ends before b. + let a = (5, 10); + let b = (7, 12); + assert!(!check_leases(&a, &b)); + + // Test when a starts before b and ends after b. + let a = (5, 15); + let b = (7, 12); + assert!(check_leases(&a, &b)); + + // Test when a starts after b and ends after b. + let a = (7, 12); + let b = (5, 10); + assert!(!check_leases(&a, &b)); + + // Test when a starts after b and ends before b. + let a = (7, 8); + let b = (5, 10); + assert!(!check_leases(&a, &b)); + + // Test when a and b are the same. + let a = (5, 10); + let b = (5, 10); + assert!(check_leases(&a, &b)); + + // Test when a and b do not overlap. + let a = (5, 10); + let b = (12, 15); + assert!(!check_leases(&a, &b)); +} + pub fn winning_offset_of( block_number: BlockNumber, ending_period_start_at: BlockNumber, diff --git a/src/primitive.rs b/src/primitive.rs index 92c37a1..d2dfc50 100644 --- a/src/primitive.rs +++ b/src/primitive.rs @@ -23,12 +23,11 @@ pub struct AuctionDetail { impl AuctionDetail { pub fn fmt(&self, now: BlockNumber, end_at: BlockNumber) -> String { let remain_blocks = end_at.saturating_sub(now); + let (first, last) = util::range_of(self.first_lease_period); format!( - "auction(#{}) for leases[#{}, #{}) has been activated for block range[#{}, #{end_at}], remain {remain_blocks} block(s) approximately {}", + "auction(#{}) for leases(#{first}, #{last}) has been activated for block range(#{}, #{end_at}), remain {remain_blocks} block(s) approximately {}", self.index, - self.first_lease_period, - self.first_lease_period + C_RANGE_COUNT, self.ending_period_start_at, util::blocks2time(remain_blocks) )