Skip to content

Commit

Permalink
feat(application): implement is_valid_node for query runner
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-wright committed Jul 12, 2023
1 parent 849430c commit 030cb67
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
9 changes: 7 additions & 2 deletions core/application/src/query_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
55 changes: 55 additions & 0 deletions core/application/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

0 comments on commit 030cb67

Please sign in to comment.