From 7d03d04c8e420bbdac250332246936b8d405232f Mon Sep 17 00:00:00 2001 From: Andrii Rakhimov Date: Thu, 11 Apr 2024 11:50:50 +0100 Subject: [PATCH] [ICP]: Update ingress processing timeout (#3782) * [ICP]: Update ingress processing timeout * [ICP]: Update ingress processing timeout * Format * Format --------- Co-authored-by: satoshiotomakan <127754187+satoshiotomakan@users.noreply.github.com> --- .../fuzz_targets/tw_internet_computer_transfer.rs | 1 + rust/tw_internet_computer/src/protocol/mod.rs | 15 +++++++++------ rust/tw_internet_computer/src/transactions/mod.rs | 5 +++++ .../src/transactions/transfer.rs | 5 ++++- src/proto/InternetComputer.proto | 1 + 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/rust/tw_internet_computer/fuzz/fuzz_targets/tw_internet_computer_transfer.rs b/rust/tw_internet_computer/fuzz/fuzz_targets/tw_internet_computer_transfer.rs index cfd4b494762..568b15610b2 100644 --- a/rust/tw_internet_computer/fuzz/fuzz_targets/tw_internet_computer_transfer.rs +++ b/rust/tw_internet_computer/fuzz/fuzz_targets/tw_internet_computer_transfer.rs @@ -16,6 +16,7 @@ struct ArbitraryTransferArgs { #[arbitrary(with = arbitrary_to_field)] to: String, current_timestamp_nanos: u64, + permitted_drift: Option, } fn arbitrary_to_field(u: &mut arbitrary::Unstructured) -> arbitrary::Result { diff --git a/rust/tw_internet_computer/src/protocol/mod.rs b/rust/tw_internet_computer/src/protocol/mod.rs index 19b13630cc2..5d1f0380c38 100644 --- a/rust/tw_internet_computer/src/protocol/mod.rs +++ b/rust/tw_internet_computer/src/protocol/mod.rs @@ -20,14 +20,17 @@ use std::time::Duration; /// is maintained by the IC before it is deleted from the ingress history. const MAX_INGRESS_TTL: Duration = Duration::from_secs(5 * 60); -/// Duration subtracted from `MAX_INGRESS_TTL` by -/// `expiry_time_from_now()` when creating an ingress message. -const PERMITTED_DRIFT: Duration = Duration::from_secs(60); - /// An upper limit on the validity of the request, expressed in nanoseconds since 1970-01-01. -pub fn get_ingress_expiry(current_timestamp_duration: Duration) -> u64 { +pub fn get_ingress_expiry( + current_timestamp_duration: Duration, + permitted_drift_in_seconds: Option, +) -> u64 { + let permitted_drift = permitted_drift_in_seconds + .map(Duration::from_secs) + .unwrap_or(Duration::from_secs(60)); + current_timestamp_duration .saturating_add(MAX_INGRESS_TTL) - .saturating_sub(PERMITTED_DRIFT) + .saturating_sub(permitted_drift) .as_nanos() as u64 } diff --git a/rust/tw_internet_computer/src/transactions/mod.rs b/rust/tw_internet_computer/src/transactions/mod.rs index d15fa9be777..e0c21697c1d 100644 --- a/rust/tw_internet_computer/src/transactions/mod.rs +++ b/rust/tw_internet_computer/src/transactions/mod.rs @@ -38,6 +38,11 @@ pub fn sign_transaction( max_fee: None, to: transfer_args.to_account_identifier.to_string(), current_timestamp_nanos: transfer_args.current_timestamp_nanos, + permitted_drift: if transfer_args.permitted_drift > 0 { + Some(transfer_args.permitted_drift) + } else { + None + }, }, ), Tx::None => Err(SignTransactionError::InvalidArguments), diff --git a/rust/tw_internet_computer/src/transactions/transfer.rs b/rust/tw_internet_computer/src/transactions/transfer.rs index 78708226468..7cc846f3b53 100644 --- a/rust/tw_internet_computer/src/transactions/transfer.rs +++ b/rust/tw_internet_computer/src/transactions/transfer.rs @@ -39,6 +39,8 @@ pub struct TransferArgs { pub to: String, /// The current timestamp in nanoseconds. pub current_timestamp_nanos: u64, + /// The duration to tune up ingress expiry in seconds. + pub permitted_drift: Option, } impl TryFrom for SendRequest { @@ -82,7 +84,7 @@ pub fn transfer( } let current_timestamp_duration = Duration::from_nanos(args.current_timestamp_nanos); - let ingress_expiry = get_ingress_expiry(current_timestamp_duration); + let ingress_expiry = get_ingress_expiry(current_timestamp_duration, args.permitted_drift); let identity = Identity::new(private_key); // Encode the arguments for the ledger `send_pb` endpoint. @@ -189,6 +191,7 @@ mod test { max_fee: None, to: to_account_identifier.to_hex(), current_timestamp_nanos, + permitted_drift: None, } } diff --git a/src/proto/InternetComputer.proto b/src/proto/InternetComputer.proto index f76380f21da..a74c86107d3 100644 --- a/src/proto/InternetComputer.proto +++ b/src/proto/InternetComputer.proto @@ -18,6 +18,7 @@ message Transaction { uint64 amount = 2; uint64 memo = 3; uint64 current_timestamp_nanos = 4; + uint64 permitted_drift = 5; } // Payload transfer