Skip to content

Commit 755e49e

Browse files
author
Nikolas Haimerl
committed
fix(ICRC-Rosetta): test for payloads
1 parent 84250b9 commit 755e49e

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

rs/rosetta-api/icrc1/rosetta/src/construction_api/endpoints.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
use axum::{extract::State, response::Result, Json};
77
use rosetta_core::{request_types::*, response_types::*};
88
use std::sync::Arc;
9+
use std::time::SystemTime;
910

1011
pub async fn construction_derive(
1112
State(state): State<Arc<AppState>>,
@@ -103,6 +104,7 @@ pub async fn construction_payloads(
103104
.transpose()?,
104105
&state.icrc1_agent.ledger_canister_id,
105106
request.public_keys.unwrap_or_else(Vec::new),
107+
SystemTime::now(),
106108
)?))
107109
}
108110

rs/rosetta-api/icrc1/rosetta/src/construction_api/services.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,14 @@ pub fn construction_payloads(
123123
metadata: Option<ConstructionPayloadsRequestMetadata>,
124124
ledger_id: &Principal,
125125
public_keys: Vec<PublicKey>,
126+
now: SystemTime,
126127
) -> Result<ConstructionPayloadsResponse, Error> {
127128
// The interval between each ingress message
128129
// The permitted drift makes sure that intervals are overlapping and there are no edge cases when trying to submit to the IC
129130
let ingress_interval: u64 =
130131
(ic_constants::MAX_INGRESS_TTL - ic_constants::PERMITTED_DRIFT).as_nanos() as u64;
131132

132-
let now = SystemTime::now()
133+
let now = now
133134
.duration_since(SystemTime::UNIX_EPOCH)
134135
.unwrap()
135136
.as_nanos() as u64;
@@ -259,7 +260,6 @@ mod tests {
259260
use proptest::test_runner::Config as TestRunnerConfig;
260261
use proptest::test_runner::TestRunner;
261262
use rosetta_core::models::RosettaSupportedKeyPair;
262-
use std::time::Duration;
263263

264264
const NUM_TEST_CASES: u32 = 100;
265265
const NUM_BLOCKS: usize = 1;
@@ -287,11 +287,10 @@ mod tests {
287287
parse_response: ConstructionParseResponse,
288288
operations: Vec<Operation>,
289289
metadata: ConstructionPayloadsRequestMetadata,
290-
now: Duration,
290+
now: u64,
291291
) {
292292
let received_metadata =
293293
ConstructionPayloadsRequestMetadata::try_from(parse_response.metadata).unwrap();
294-
let now_u64 = now.as_nanos() as u64;
295294

296295
parse_response.operations.into_iter().for_each(|operation| {
297296
assert!(
@@ -307,17 +306,17 @@ mod tests {
307306
if let Some(created_at_time) = metadata.created_at_time {
308307
assert_eq!(received_metadata.created_at_time.unwrap(), created_at_time);
309308
} else {
310-
assert!(received_metadata.created_at_time.unwrap() >= now_u64);
309+
assert!(received_metadata.created_at_time.unwrap() >= now);
311310
}
312311
assert_eq!(received_metadata.memo, metadata.memo);
313312

314313
if let Some(ingress_start) = metadata.ingress_start {
315314
assert_eq!(received_metadata.ingress_start.unwrap(), ingress_start);
316315
} else {
317-
assert!(received_metadata.ingress_start.unwrap() >= now_u64);
316+
assert!(received_metadata.ingress_start.unwrap() >= now);
318317
assert!(
319318
received_metadata.ingress_start.unwrap()
320-
<= now_u64
319+
<= now
321320
+ (ic_constants::MAX_INGRESS_TTL - ic_constants::PERMITTED_DRIFT).as_nanos()
322321
as u64
323322
);
@@ -420,17 +419,23 @@ mod tests {
420419
.try_into()
421420
.unwrap();
422421

423-
let now = SystemTime::now()
424-
.duration_since(SystemTime::UNIX_EPOCH)
425-
.unwrap();
422+
let now = SystemTime::now();
426423

427424
let construction_payloads_response = construction_payloads(
428425
rosetta_core_operations.clone(),
429426
Some(payloads_metadata.clone()),
430427
&PrincipalId::new_anonymous().0,
431428
vec![(&arg_with_caller.caller).into()],
429+
now,
432430
);
433431

432+
let now = now
433+
.duration_since(SystemTime::UNIX_EPOCH)
434+
.unwrap()
435+
.as_nanos() as u64;
436+
let ingress_interval = (ic_constants::MAX_INGRESS_TTL
437+
- ic_constants::PERMITTED_DRIFT)
438+
.as_nanos() as u64;
434439
match (
435440
payloads_metadata.ingress_end,
436441
payloads_metadata.ingress_start,
@@ -440,30 +445,27 @@ mod tests {
440445
assert!(construction_payloads_response.is_err());
441446
continue;
442447
}
443-
if ingress_end
444-
< (now + ic_constants::MAX_INGRESS_TTL).as_nanos() as u64
445-
{
448+
if ingress_end < now + ingress_interval {
446449
assert!(construction_payloads_response.is_err());
447450
continue;
448451
}
449452
}
450453
(Some(ingress_end), _) => {
451-
if ingress_end
452-
< (now + ic_constants::MAX_INGRESS_TTL).as_nanos() as u64
453-
{
454+
if ingress_end < now + ingress_interval {
454455
assert!(construction_payloads_response.is_err());
455456
continue;
456457
}
457458
}
458459
(_, Some(ingress_start)) => {
459-
if ingress_start < now.as_nanos() as u64 {
460+
let ingress_end = ingress_start + ingress_interval;
461+
if ingress_end < now + ingress_interval {
460462
assert!(construction_payloads_response.is_err());
461463
continue;
462464
}
463465
}
464466
(_, _) => {}
465467
}
466-
468+
println!("{:?}", payloads_metadata);
467469
let construction_parse_response = construction_parse(
468470
construction_payloads_response
469471
.clone()

0 commit comments

Comments
 (0)