Skip to content

Commit 618cc63

Browse files
refactor: replace tuple with AttestedContract struct
Replace the tuple (ContractInstanceId, ClientId, Instant) with a properly named struct to improve code readability and maintainability. The AttestedContract struct provides: - Named fields instead of positional tuple access - Self-documenting code - Easier future extensibility Co-authored-by: nacho.d.g <iduartgomez@users.noreply.github.com>
1 parent 62d3324 commit 618cc63

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

crates/core/src/client_events/websocket.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ async fn websocket_commands(
298298
}
299299

300300
if let Some(entry) = attested_contracts.get(token) {
301-
let (cid, _, _) = entry.value();
302-
tracing::trace!(?token, ?cid, "Found token in attested_contracts map");
303-
Some((token.clone(), *cid))
301+
let attested = entry.value();
302+
tracing::trace!(?token, contract_id = ?attested.contract_id, "Found token in attested_contracts map");
303+
Some((token.clone(), attested.contract_id))
304304
} else {
305305
tracing::warn!(?token, "Auth token not found in attested_contracts map");
306306
None

crates/core/src/node/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ pub async fn run_local_node(
13351335
let attested_contract = token.and_then(|token| {
13361336
gw.attested_contracts
13371337
.get(&token)
1338-
.map(|entry| entry.value().0)
1338+
.map(|entry| entry.value().contract_id)
13391339
});
13401340
let op_name = match op {
13411341
DelegateRequest::RegisterDelegate { .. } => "RegisterDelegate",

crates/core/src/server/http_gateway.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,30 @@ impl std::ops::Deref for HttpGatewayRequest {
3434
}
3535
}
3636

37-
/// Maps authentication tokens to contract instances, client IDs, and last access time.
38-
/// The Instant tracks when the token was last used to enable time-based expiration.
39-
pub type AttestedContractMap = Arc<DashMap<AuthToken, (ContractInstanceId, ClientId, Instant)>>;
37+
/// Represents an attested contract entry with metadata for token expiration.
38+
#[derive(Clone, Debug)]
39+
pub struct AttestedContract {
40+
/// The contract instance ID
41+
pub contract_id: ContractInstanceId,
42+
/// The client ID associated with this token
43+
pub client_id: ClientId,
44+
/// Timestamp of when the token was last accessed (for expiration tracking)
45+
pub last_accessed: Instant,
46+
}
47+
48+
impl AttestedContract {
49+
/// Create a new attested contract entry
50+
pub fn new(contract_id: ContractInstanceId, client_id: ClientId) -> Self {
51+
Self {
52+
contract_id,
53+
client_id,
54+
last_accessed: Instant::now(),
55+
}
56+
}
57+
}
58+
59+
/// Maps authentication tokens to attested contract metadata.
60+
pub type AttestedContractMap = Arc<DashMap<AuthToken, AttestedContract>>;
4061

4162
/// A gateway to access and interact with contracts through an HTTP interface.
4263
pub(crate) struct HttpGateway {
@@ -86,9 +107,9 @@ impl ClientEventsProxy for HttpGateway {
86107
.send(HostCallbackResult::NewId { id: cli_id })
87108
.map_err(|_e| ErrorKind::NodeUnavailable)?;
88109
if let Some((assigned_token, contract)) = assigned_token {
89-
let now = Instant::now();
110+
let attested = AttestedContract::new(contract, cli_id);
90111
self.attested_contracts
91-
.insert(assigned_token.clone(), (contract, cli_id, now));
112+
.insert(assigned_token.clone(), attested);
92113
tracing::debug!(
93114
?assigned_token,
94115
?contract,

crates/core/src/server/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ pub mod local_node {
155155
.attested_contracts
156156
.iter()
157157
.find_map(|entry| {
158-
let (k, (_, eid, _)) = entry.pair();
159-
(eid == &id).then(|| k.clone())
158+
let (k, attested) = entry.pair();
159+
(attested.client_id == id).then(|| k.clone())
160160
})
161161
{
162162
gw.attested_contracts.remove(&rm_token);
@@ -250,15 +250,15 @@ fn spawn_token_cleanup_task(attested_contracts: AttestedContractMap) {
250250
let initial_count = attested_contracts.len();
251251

252252
// Remove tokens that haven't been accessed in TOKEN_TTL
253-
attested_contracts.retain(|token, (contract_id, client_id, last_used)| {
254-
let elapsed = now.duration_since(*last_used);
253+
attested_contracts.retain(|token, attested| {
254+
let elapsed = now.duration_since(attested.last_accessed);
255255
let should_keep = elapsed < TOKEN_TTL;
256256

257257
if !should_keep {
258258
tracing::info!(
259259
?token,
260-
?contract_id,
261-
?client_id,
260+
contract_id = ?attested.contract_id,
261+
client_id = ?attested.client_id,
262262
elapsed_hours = elapsed.as_secs() / 3600,
263263
"Removing expired authentication token"
264264
);

0 commit comments

Comments
 (0)