Skip to content

Commit dbc497c

Browse files
committed
Add query to obtain the chain ID of a user (#4817)
Making debugging easier add query to obtain the chain ID of a user CI
1 parent 73c3833 commit dbc497c

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
@@ -398,6 +398,38 @@ async fn test_faucet_persistence() {
398398
// Create a new faucet instance with the same database path (simulating restart)
399399
let faucet_storage_2 = Arc::new(FaucetDatabase::new(&storage_path).await.unwrap());
400400

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

0 commit comments

Comments
 (0)