diff --git a/core/application/src/query_runner.rs b/core/application/src/query_runner.rs index 0c809a9d6..72aee97d0 100644 --- a/core/application/src/query_runner.rs +++ b/core/application/src/query_runner.rs @@ -162,8 +162,13 @@ impl SyncQueryRunnerInterface for QueryRunner { todo!() } - fn is_valid_node(&self, _id: &NodePublicKey) -> bool { - todo!() + fn is_valid_node(&self, id: &NodePublicKey) -> bool { + // TODO(matthias): we can use `is_some_and` once we update the rust version to 1.70 + if let Some(node_info) = self.get_node_info(id) { + node_info.stake.staked >= self.get_staking_amount().into() + } else { + false + } } fn get_staking_amount(&self) -> u128 { diff --git a/core/application/src/tests.rs b/core/application/src/tests.rs index 0ebd1f112..2289ad43c 100644 --- a/core/application/src/tests.rs +++ b/core/application/src/tests.rs @@ -1023,3 +1023,58 @@ async fn test_validate_txn() { ); assert_eq!(res.txn_receipts[0], query_runner.validate_txn(req)); } + +#[test] +async fn test_is_valid_node() { + let (update_socket, query_runner) = init_app(None).await; + + let owner_secret_key = AccountOwnerSecretKey::generate(); + let node_secret_key = NodeSecretKey::generate(); + + // Stake minimum required amount. + let minimum_stake_amount = query_runner.get_staking_amount(); + deposit( + minimum_stake_amount.into(), + Tokens::FLK, + owner_secret_key, + &update_socket, + 1, + ) + .await; + stake( + minimum_stake_amount.into(), + node_secret_key.to_pk(), + owner_secret_key, + &update_socket, + 2, + ) + .await; + // Make sure that this node is a valid node. + assert!(query_runner.is_valid_node(&node_secret_key.to_pk())); + + // Generate new keys for a different node. + let owner_secret_key = AccountOwnerSecretKey::generate(); + let node_secret_key = NodeSecretKey::generate(); + + // Stake less than the minimum required amount. + let minimum_stake_amount = query_runner.get_staking_amount(); + let less_than_minimum_skate_amount = minimum_stake_amount / 2; + deposit( + less_than_minimum_skate_amount.into(), + Tokens::FLK, + owner_secret_key, + &update_socket, + 1, + ) + .await; + stake( + less_than_minimum_skate_amount.into(), + node_secret_key.to_pk(), + owner_secret_key, + &update_socket, + 2, + ) + .await; + // Make sure that this node is not a valid node. + assert!(!query_runner.is_valid_node(&node_secret_key.to_pk())); +}