-
Notifications
You must be signed in to change notification settings - Fork 41
feat(node): metrics for attestation freshness #4236
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| //! Attestation-freshness gauges. | ||
| //! | ||
| //! Both are absolute timestamps rather than remaining durations: a node that stops re-attesting | ||
| //! also stops updating them, and only the absolute form keeps decaying towards now, so a staleness | ||
| //! alert still fires on a frozen gauge. At rest both advance hourly — the expiry is re-read on | ||
| //! every submission observation, the landing timestamp only when one is confirmed. | ||
|
|
||
| use near_mpc_contract_interface::types::VerifiedAttestation; | ||
| use near_time::Clock; | ||
|
|
||
| use crate::metrics::{ | ||
| MPC_ATTESTATION_EXPIRY_TIMESTAMP_SECONDS, MPC_ATTESTATION_LAST_LANDED_TIMESTAMP_SECONDS, | ||
| }; | ||
|
|
||
| const NO_ATTESTATION_STORED: i64 = 0; | ||
| const NO_EXPIRY: i64 = -1; | ||
|
Comment on lines
+15
to
+16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could create an enum with values NO_ATTESTATION_STORED, NO_EXPIRY, EXPIRES_AT(i64).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd lean towards keeping the consts. The gauge takes an i64, so an enum needs a conversion on top and the 6-line match becomes ~15 for the same mapping. also I follow the pattern from image_expiry_metrics.rs And |
||
|
|
||
| pub(crate) fn record_stored_attestation_expiry(stored: Option<&VerifiedAttestation>) { | ||
| MPC_ATTESTATION_EXPIRY_TIMESTAMP_SECONDS.set(expiry_gauge_value(stored)); | ||
| } | ||
|
|
||
| pub(crate) fn record_attestation_landed(clock: &Clock) { | ||
| MPC_ATTESTATION_LAST_LANDED_TIMESTAMP_SECONDS.set(clock.now_utc().unix_timestamp()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you not have Clock::real() inside this? Then the function would take no inputs
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see it's for unit testing... still would be interested in your opinion
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only for unit test, originally I didn't have this parameter, then the Claude bot flagged that we don't have a unit test for this. so I added it. Also think it's the right call regardless: engineering standards ask for time to be injected, and the test guards a seconds-vs-milliseconds mix-up. |
||
| } | ||
|
|
||
| fn expiry_gauge_value(stored: Option<&VerifiedAttestation>) -> i64 { | ||
| match stored.map(VerifiedAttestation::expiry_timestamp_seconds) { | ||
| None => NO_ATTESTATION_STORED, | ||
| Some(None) => NO_EXPIRY, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That does this mean in real world, which attestation has no expiry?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I don't think we need this, but we need to clean up the interface to get rid of this (c.f. #4236 (comment))
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see repose in here #4236 (comment) |
||
| Some(Some(expiry)) => i64::try_from(expiry).unwrap_or(i64::MAX), | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| #[expect(non_snake_case)] | ||
| mod tests { | ||
| use super::*; | ||
| use near_mpc_contract_interface::types::MockAttestation; | ||
| use near_time::{FakeClock, Utc}; | ||
| use rstest::rstest; | ||
|
|
||
| const EXPIRES_AT: u64 = 1_754_000_000; | ||
|
|
||
| fn mock_with_expiry(expiry_timestamp_seconds: Option<u64>) -> VerifiedAttestation { | ||
| VerifiedAttestation::Mock(MockAttestation::WithConstraints { | ||
| mpc_docker_image_hash: None, | ||
| launcher_docker_compose_hash: None, | ||
| expiry_timestamp_seconds, | ||
| expected_measurements: None, | ||
| }) | ||
| } | ||
|
|
||
| #[test] | ||
| fn record_attestation_landed__should_set_the_gauge_to_the_landing_time() { | ||
| // Given | ||
| let landed_at = Utc::from_unix_timestamp(EXPIRES_AT as i64).unwrap(); | ||
|
|
||
| // When | ||
| record_attestation_landed(&FakeClock::new(landed_at).clock()); | ||
|
|
||
| // Then | ||
| assert_eq!( | ||
| MPC_ATTESTATION_LAST_LANDED_TIMESTAMP_SECONDS.get(), | ||
| EXPIRES_AT as i64 | ||
| ); | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[case::nothing_stored(None, NO_ATTESTATION_STORED)] | ||
| #[case::stored_with_expiry(Some(mock_with_expiry(Some(EXPIRES_AT))), EXPIRES_AT as i64)] | ||
| #[case::stored_without_expiry(Some(mock_with_expiry(None)), NO_EXPIRY)] | ||
| #[case::unstamped_mock(Some(VerifiedAttestation::Mock(MockAttestation::Valid)), NO_EXPIRY)] | ||
| #[case::expiry_beyond_i64(Some(mock_with_expiry(Some(u64::MAX))), i64::MAX)] | ||
| fn expiry_gauge_value__should_report_stored_expiry_or_a_sentinel( | ||
| #[case] stored: Option<VerifiedAttestation>, | ||
| #[case] expected: i64, | ||
| ) { | ||
| // When | ||
| let value = expiry_gauge_value(stored.as_ref()); | ||
|
|
||
| // Then | ||
| assert_eq!(value, expected); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please refine this comment to be in line with our engineering standards. Every sentence is violating one of our rules.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update in commit 6490442