Skip to content

Commit

Permalink
[ICP]: Update ingress processing timeout (#3782)
Browse files Browse the repository at this point in the history
* [ICP]: Update ingress processing timeout

* [ICP]: Update ingress processing timeout

* Format

* Format

---------

Co-authored-by: satoshiotomakan <127754187+satoshiotomakan@users.noreply.github.com>
  • Loading branch information
ar-g and satoshiotomakan committed Apr 11, 2024
1 parent 70f8636 commit 7d03d04
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
Expand Up @@ -16,6 +16,7 @@ struct ArbitraryTransferArgs {
#[arbitrary(with = arbitrary_to_field)]
to: String,
current_timestamp_nanos: u64,
permitted_drift: Option<u64>,
}

fn arbitrary_to_field(u: &mut arbitrary::Unstructured) -> arbitrary::Result<String> {
Expand Down
15 changes: 9 additions & 6 deletions rust/tw_internet_computer/src/protocol/mod.rs
Expand Up @@ -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>,
) -> 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
}
5 changes: 5 additions & 0 deletions rust/tw_internet_computer/src/transactions/mod.rs
Expand Up @@ -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),
Expand Down
5 changes: 4 additions & 1 deletion rust/tw_internet_computer/src/transactions/transfer.rs
Expand Up @@ -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<u64>,
}

impl TryFrom<TransferArgs> for SendRequest {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -189,6 +191,7 @@ mod test {
max_fee: None,
to: to_account_identifier.to_hex(),
current_timestamp_nanos,
permitted_drift: None,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/proto/InternetComputer.proto
Expand Up @@ -18,6 +18,7 @@ message Transaction {
uint64 amount = 2;
uint64 memo = 3;
uint64 current_timestamp_nanos = 4;
uint64 permitted_drift = 5;
}

// Payload transfer
Expand Down

0 comments on commit 7d03d04

Please sign in to comment.