Skip to content

Commit 055086a

Browse files
authored
add query to obtain the chain ID of a user (#4817)
## Motivation Making debugging easier ## Proposal add query to obtain the chain ID of a user ## Test Plan CI ## Release Plan - These changes should be backported to the latest `devnet` branch, then - be released in a FAUCET hotfix.
1 parent 2970dd3 commit 055086a

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

linera-faucet/server/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ mod tests;
180180
pub struct QueryRoot<C: ClientContext> {
181181
client: ChainClient<C::Environment>,
182182
genesis_config: Arc<GenesisConfig>,
183+
faucet_storage: Arc<FaucetDatabase>,
183184
}
184185

185186
/// The root GraphQL mutation type.
@@ -265,6 +266,26 @@ where
265266
async fn current_committee(&self) -> Result<Committee, Error> {
266267
Ok(self.client.local_committee().await?)
267268
}
269+
270+
/// Find the existing a chain with the given authentication key, if any.
271+
async fn chain_id(&self, owner: AccountOwner) -> Result<ChainId, Error> {
272+
// Check if this owner already has a chain.
273+
#[cfg(with_metrics)]
274+
let db_start_time = std::time::Instant::now();
275+
276+
let chain_id = self
277+
.faucet_storage
278+
.get_chain_id(&owner)
279+
.await
280+
.map_err(|e| Error::new(e.to_string()))?;
281+
282+
#[cfg(with_metrics)]
283+
metrics::DATABASE_OPERATION_LATENCY
284+
.with_label_values(&["get_chain_id"])
285+
.observe(db_start_time.elapsed().as_secs_f64() * 1000.0);
286+
287+
chain_id.ok_or(Error::new("This user has no chain yet"))
288+
}
268289
}
269290

270291
#[async_graphql::Object(cache_control(no_cache))]
@@ -896,6 +917,7 @@ where
896917
let query_root = QueryRoot {
897918
genesis_config: Arc::clone(&self.genesis_config),
898919
client: self.client.clone(),
920+
faucet_storage: Arc::clone(&self.faucet_storage),
899921
};
900922
Schema::build(query_root, mutation_root, EmptySubscription).finish()
901923
}

linera-faucet/server/src/tests.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,38 @@ async fn test_faucet_persistence() {
399399
// Create a new faucet instance with the same database path (simulating restart)
400400
let faucet_storage_2 = Arc::new(FaucetDatabase::new(&storage_path).await.unwrap());
401401

402+
// Test the chain_id query API through the database for owners that have claimed chains
403+
let queried_chain_1 = faucet_storage_2
404+
.get_chain_id(&test_owner_1)
405+
.await
406+
.expect("Query should succeed for owner 1")
407+
.expect("Owner 1 should have a chain ID");
408+
assert_eq!(
409+
chain_1_id, queried_chain_1,
410+
"Query should return correct chain ID for owner 1"
411+
);
412+
413+
let queried_chain_2 = faucet_storage_2
414+
.get_chain_id(&test_owner_2)
415+
.await
416+
.expect("Query should succeed for owner 2")
417+
.expect("Owner 2 should have a chain ID");
418+
assert_eq!(
419+
chain_2_id, queried_chain_2,
420+
"Query should return correct chain ID for owner 2"
421+
);
422+
423+
// Test the chain_id query for an owner that hasn't claimed a chain yet
424+
let test_owner_new = AccountPublicKey::test_key(99).into();
425+
let result_new = faucet_storage_2
426+
.get_chain_id(&test_owner_new)
427+
.await
428+
.expect("Query should succeed even for non-existent owner");
429+
assert!(
430+
result_new.is_none(),
431+
"Query should return None for owner that hasn't claimed a chain"
432+
);
433+
402434
// Set up the new MutationRoot instance
403435
let pending_requests_2 = Arc::new(Mutex::new(VecDeque::new()));
404436
let request_notifier_2 = Arc::new(Notify::new());

0 commit comments

Comments
 (0)