Skip to content

Commit

Permalink
Round ingress_expiry to the minute
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspofford-dfinity committed Oct 10, 2023
1 parent cd24202 commit 442d7ce
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
19 changes: 13 additions & 6 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions ic-agent/src/agent/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ impl AgentBuilder {
/// Provides a _default_ ingress expiry. This is the delta that will be applied
/// at the time an update or query is made. The default expiry cannot be a
/// fixed system time.
///
/// This duration may be rounded in order to reduce cache misses. The current
/// implementation rounds to the nearest minute if the expiry is more than a minute,
/// but this is not guaranteed.
pub fn with_ingress_expiry(mut self, ingress_expiry: Option<std::time::Duration>) -> Self {
self.config.ingress_expiry = ingress_expiry;
self
Expand Down
49 changes: 43 additions & 6 deletions ic-agent/src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,21 @@ impl Agent {
}

fn get_expiry_date(&self) -> u64 {
// TODO(hansl): evaluate if we need this on the agent side (my hunch is we don't).
let permitted_drift = Duration::from_secs(60);
self.ingress_expiry
.as_nanos()
.saturating_add(OffsetDateTime::now_utc().unix_timestamp_nanos() as u128)
.saturating_sub(permitted_drift.as_nanos()) as u64
let expiry_raw = OffsetDateTime::now_utc() + self.ingress_expiry;
let rounded = if self.ingress_expiry.as_secs() > 60 {
let mut rounded_minute = expiry_raw
.replace_second(0)
.unwrap()
.replace_nanosecond(0)
.unwrap();
if expiry_raw.second() >= 30 {
rounded_minute += Duration::from_secs(60);
}
rounded_minute
} else {
expiry_raw.replace_nanosecond(0).unwrap()
};
rounded.unix_timestamp_nanos() as u64
}

/// Return the principal of the identity.
Expand Down Expand Up @@ -1275,3 +1284,31 @@ impl<'agent> UpdateBuilder<'agent> {
})
}
}

#[cfg(test)]
mod offline_tests {
use super::*;
// Any tests that involve the network should go in agent_test, not here.

#[test]
fn rounded_expiry() {
let agent = Agent::builder()
.with_url("http://not-a-real-url")
.build()
.unwrap();
let mut prev_expiry = None;
let mut num_timestamps = 0;
for _ in 0..6 {
let update = agent
.update(&Principal::management_canister(), "not_a_method")
.sign()
.unwrap();
if prev_expiry < Some(update.ingress_expiry) {
prev_expiry = Some(update.ingress_expiry);
num_timestamps += 1;
}
}
// in six requests, there should be no more than two timestamps
assert!(num_timestamps <= 2, "num_timestamps:{num_timestamps} > 2");
}
}

0 comments on commit 442d7ce

Please sign in to comment.