From d29a6446f4f78f37650fd65a09d66366f2d77eeb Mon Sep 17 00:00:00 2001 From: MicaiahReid Date: Mon, 2 Oct 2023 20:28:08 -0400 Subject: [PATCH 1/4] chore: make status timestamps Optional --- components/chainhook-cli/src/service/mod.rs | 26 ++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/components/chainhook-cli/src/service/mod.rs b/components/chainhook-cli/src/service/mod.rs index c9c656a0a..2bdf4c541 100644 --- a/components/chainhook-cli/src/service/mod.rs +++ b/components/chainhook-cli/src/service/mod.rs @@ -554,13 +554,13 @@ pub struct ScanningData { pub number_of_blocks_to_scan: u64, pub number_of_blocks_evaluated: u64, pub number_of_times_triggered: u64, - pub last_occurrence: String, + pub last_occurrence: Option, pub last_evaluated_block_height: u64, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct StreamingData { - pub last_occurrence: String, + pub last_occurrence: Option, pub last_evaluation: String, pub number_of_times_triggered: u64, pub number_of_blocks_evaluated: u64, @@ -571,7 +571,7 @@ pub struct StreamingData { pub struct ExpiredData { pub number_of_blocks_evaluated: u64, pub number_of_times_triggered: u64, - pub last_occurrence: String, + pub last_occurrence: Option, pub last_evaluated_block_height: u64, pub expired_at_block_height: u64, } @@ -720,7 +720,7 @@ fn set_predicate_streaming_status( unreachable!("unreachable predicate status: {:?}", status) } }, - None => (format!("0"), 0, 0, 0), + None => (None, 0, 0, 0), } }; let ( @@ -733,7 +733,7 @@ fn set_predicate_streaming_status( last_triggered_height, triggered_count, } => ( - now_ms.clone(), + Some(now_ms.clone()), number_of_times_triggered + triggered_count, number_of_blocks_evaluated + triggered_count, last_triggered_height, @@ -791,37 +791,37 @@ pub fn set_predicate_scanning_status( Some(status) => match status { PredicateStatus::Scanning(scanning_data) => { if number_of_times_triggered > scanning_data.number_of_times_triggered { - now_ms + Some(now_ms) } else { scanning_data.last_occurrence } } PredicateStatus::Streaming(streaming_data) => { if number_of_times_triggered > streaming_data.number_of_times_triggered { - now_ms + Some(now_ms) } else { streaming_data.last_occurrence } } PredicateStatus::UnconfirmedExpiration(expired_data) => { if number_of_times_triggered > expired_data.number_of_times_triggered { - now_ms + Some(now_ms) } else { expired_data.last_occurrence } } PredicateStatus::New => { if number_of_times_triggered > 0 { - now_ms + Some(now_ms) } else { - format!("0") + None } } PredicateStatus::Interrupted(_) | PredicateStatus::ConfirmedExpiration(_) => { unreachable!("unreachable predicate status: {:?}", status) } }, - None => format!("0"), + None => None, }; update_predicate_status( @@ -868,7 +868,7 @@ pub fn set_unconfirmed_expiration_status( last_occurrence, last_evaluated_block_height, ), - PredicateStatus::New => (0, 0, format!("0"), 0), + PredicateStatus::New => (0, 0, None, 0), PredicateStatus::Streaming(StreamingData { last_occurrence, last_evaluation: _, @@ -904,7 +904,7 @@ pub fn set_unconfirmed_expiration_status( return; } }, - None => (0, 0, format!("0"), 0), + None => (0, 0, None, 0), }; update_predicate_status( predicate_key, From acb97ba6344ac06438b08eac84703956e5a081af Mon Sep 17 00:00:00 2001 From: MicaiahReid Date: Mon, 2 Oct 2023 20:32:32 -0400 Subject: [PATCH 2/4] use seconds for status timestamps --- components/chainhook-cli/src/service/mod.rs | 30 ++++++++++----------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/components/chainhook-cli/src/service/mod.rs b/components/chainhook-cli/src/service/mod.rs index 2bdf4c541..13c203a00 100644 --- a/components/chainhook-cli/src/service/mod.rs +++ b/components/chainhook-cli/src/service/mod.rs @@ -554,14 +554,14 @@ pub struct ScanningData { pub number_of_blocks_to_scan: u64, pub number_of_blocks_evaluated: u64, pub number_of_times_triggered: u64, - pub last_occurrence: Option, + pub last_occurrence: Option, pub last_evaluated_block_height: u64, } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct StreamingData { - pub last_occurrence: Option, - pub last_evaluation: String, + pub last_occurrence: Option, + pub last_evaluation: u64, pub number_of_times_triggered: u64, pub number_of_blocks_evaluated: u64, pub last_evaluated_block_height: u64, @@ -571,7 +571,7 @@ pub struct StreamingData { pub struct ExpiredData { pub number_of_blocks_evaluated: u64, pub number_of_times_triggered: u64, - pub last_occurrence: Option, + pub last_occurrence: Option, pub last_evaluated_block_height: u64, pub expired_at_block_height: u64, } @@ -664,11 +664,10 @@ fn set_predicate_streaming_status( predicates_db_conn: &mut Connection, ctx: &Context, ) { - let now_ms = SystemTime::now() + let now_secs = SystemTime::now() .duration_since(UNIX_EPOCH) .expect("Could not get current time in ms") - .as_millis() - .to_string(); + .as_secs(); let ( last_occurrence, number_of_blocks_evaluated, @@ -733,7 +732,7 @@ fn set_predicate_streaming_status( last_triggered_height, triggered_count, } => ( - Some(now_ms.clone()), + Some(now_secs.clone()), number_of_times_triggered + triggered_count, number_of_blocks_evaluated + triggered_count, last_triggered_height, @@ -759,7 +758,7 @@ fn set_predicate_streaming_status( predicate_key, PredicateStatus::Streaming(StreamingData { last_occurrence, - last_evaluation: now_ms, + last_evaluation: now_secs, number_of_times_triggered, last_evaluated_block_height, number_of_blocks_evaluated, @@ -781,38 +780,37 @@ pub fn set_predicate_scanning_status( predicates_db_conn: &mut Connection, ctx: &Context, ) { - let now_ms = SystemTime::now() + let now_secs = SystemTime::now() .duration_since(UNIX_EPOCH) .expect("Could not get current time in ms") - .as_millis() - .to_string(); + .as_secs(); let current_status = retrieve_predicate_status(&predicate_key, predicates_db_conn); let last_occurrence = match current_status { Some(status) => match status { PredicateStatus::Scanning(scanning_data) => { if number_of_times_triggered > scanning_data.number_of_times_triggered { - Some(now_ms) + Some(now_secs) } else { scanning_data.last_occurrence } } PredicateStatus::Streaming(streaming_data) => { if number_of_times_triggered > streaming_data.number_of_times_triggered { - Some(now_ms) + Some(now_secs) } else { streaming_data.last_occurrence } } PredicateStatus::UnconfirmedExpiration(expired_data) => { if number_of_times_triggered > expired_data.number_of_times_triggered { - Some(now_ms) + Some(now_secs) } else { expired_data.last_occurrence } } PredicateStatus::New => { if number_of_times_triggered > 0 { - Some(now_ms) + Some(now_secs) } else { None } From 6b13541126f64d4a57aac26a3344b6b2092d2ba7 Mon Sep 17 00:00:00 2001 From: MicaiahReid Date: Tue, 3 Oct 2023 14:54:37 -0400 Subject: [PATCH 3/4] remove comment --- components/chainhook-cli/src/service/mod.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/components/chainhook-cli/src/service/mod.rs b/components/chainhook-cli/src/service/mod.rs index 13c203a00..8752e51af 100644 --- a/components/chainhook-cli/src/service/mod.rs +++ b/components/chainhook-cli/src/service/mod.rs @@ -547,9 +547,6 @@ pub enum PredicateStatus { } #[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)] -// note: last_occurrence (and other time-based fields on the status structs) originally were -// of type u128. serde can't handle deserializing u128s when using an adjacently tagged enum, -// so we're having to convert to a string. serde issue: https://github.com/serde-rs/json/issues/740 pub struct ScanningData { pub number_of_blocks_to_scan: u64, pub number_of_blocks_evaluated: u64, From 80a82b5312897fd40117f3be7dff203a9b18e554 Mon Sep 17 00:00:00 2001 From: MicaiahReid Date: Wed, 4 Oct 2023 10:45:05 -0400 Subject: [PATCH 4/4] fix tests --- components/chainhook-cli/src/service/tests/mod.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/chainhook-cli/src/service/tests/mod.rs b/components/chainhook-cli/src/service/tests/mod.rs index 09f12e212..be18e9520 100644 --- a/components/chainhook-cli/src/service/tests/mod.rs +++ b/components/chainhook-cli/src/service/tests/mod.rs @@ -966,21 +966,21 @@ async fn test_deregister_predicate(chain: Chain) { number_of_blocks_evaluated: 4, number_of_blocks_to_scan: 1, number_of_times_triggered: 0, - last_occurrence: format!("0"), + last_occurrence: None, last_evaluated_block_height: 4 }), 6 => using assert_confirmed_expiration_status; "preloaded predicate with scanning status should get scanned until completion")] #[test_case(Streaming(StreamingData { number_of_blocks_evaluated: 4, number_of_times_triggered: 0, - last_occurrence: format!("0"), - last_evaluation: format!("0"), + last_occurrence: None, + last_evaluation: 0, last_evaluated_block_height: 4 }), 6 => using assert_confirmed_expiration_status; "preloaded predicate with streaming status and last evaluated height below tip should get scanned until completion")] #[test_case(Streaming(StreamingData { number_of_blocks_evaluated: 5, number_of_times_triggered: 0, - last_occurrence: format!("0"), - last_evaluation: format!("0"), + last_occurrence: None, + last_evaluation: 0, last_evaluated_block_height: 5 }), 5 => using assert_streaming_status; "preloaded predicate with streaming status and last evaluated height at tip should be streamed")] #[tokio::test]