Skip to content

Commit

Permalink
chore: e2e tests typed rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
nhtyy committed Jun 26, 2024
1 parent 07cd36f commit 3f46d54
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 246 deletions.
2 changes: 1 addition & 1 deletion core/cli/src/commands/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ where
// by default loads or creates if hmac_secret_path.is_none() the secret from ~/.lightning
let url = format!("http://127.0.0.1:{}/admin", port);
let secret = lightning_rpc::load_hmac_secret(hmac_secret_path)?;
let client = lightning_rpc::clients::RpcClient::new(&url, Some(&secret)).await?;
let client = lightning_rpc::RpcClient::new(&url, Some(&secret)).await?;

for path in &input {
if let Some(path) = path.to_str() {
Expand Down
8 changes: 3 additions & 5 deletions core/cli/src/commands/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ pub async fn send_txn(update_request: UpdateRequest, nodes: &[NodeInfo]) -> Resu
let client = RpcClient::new_no_auth(&rpc_address)?;

Ok(
lightning_rpc::clients::FleekRpcClient::send_txn(&client, update_request.into())
lightning_rpc::Fleek::send_txn(&client, update_request.into())
.await
.map(|_| ())?,
)
Expand All @@ -294,7 +294,7 @@ pub async fn get_node_info_from_genesis_commitee(
.ok()?;

Some((
lightning_rpc::clients::FleekRpcClient::get_node_info_epoch(&client, public_key)
lightning_rpc::Fleek::get_node_info_epoch(&client, public_key)
.await
.ok()?,
address,
Expand Down Expand Up @@ -330,9 +330,7 @@ pub async fn get_epoch_info_from_genesis_commitee(
.ok()?;

Some((
lightning_rpc::clients::FleekRpcClient::get_epoch_info(&client)
.await
.ok()?,
lightning_rpc::Fleek::get_epoch_info(&client).await.ok()?,
address,
))
};
Expand Down
1 change: 0 additions & 1 deletion core/e2e/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod networking;
pub mod rpc;
pub mod shutdown;
35 changes: 0 additions & 35 deletions core/e2e/src/utils/rpc.rs

This file was deleted.

32 changes: 5 additions & 27 deletions core/e2e/tests/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ use std::time::{Duration, SystemTime};

use anyhow::Result;
use lightning_e2e::swarm::Swarm;
use lightning_e2e::utils::rpc;
use lightning_interfaces::types::Epoch;
use lightning_rpc::{Fleek, RpcClient};
use lightning_test_utils::config::LIGHTNING_TEST_HOME_DIR;
use lightning_test_utils::logging;
use resolved_pathbuf::ResolvedPathBuf;
use serde_json::json;
use serial_test::serial;

#[tokio::test]
Expand Down Expand Up @@ -39,38 +37,18 @@ async fn e2e_checkpoint() -> Result<()> {
// Wait for the epoch to change.
tokio::time::sleep(Duration::from_secs(35)).await;

let request = json!({
"jsonrpc": "2.0",
"method":"flk_get_epoch",
"params":[],
"id":1,
});
for (_, address) in swarm.get_rpc_addresses() {
let response = rpc::rpc_request(address, request.to_string())
.await
.unwrap();
let client = RpcClient::new_no_auth(&address)?;
let epoch = client.get_epoch().await?;

let epoch = rpc::parse_response::<u64>(response)
.await
.expect("Failed to parse response.");
assert_eq!(epoch, 1);
}

let request = json!({
"jsonrpc": "2.0",
"method":"flk_get_last_epoch_hash",
"params":[],
"id":1,
});
let mut target_hash = None;
for (_, address) in swarm.get_rpc_addresses() {
let response = rpc::rpc_request(address, request.to_string())
.await
.unwrap();
let client = RpcClient::new_no_auth(&address)?;
let (epoch_hash, _) = client.get_last_epoch_hash().await?;

let (epoch_hash, _) = rpc::parse_response::<([u8; 32], Epoch)>(response)
.await
.expect("Failed to parse response.");
if target_hash.is_none() {
target_hash = Some(epoch_hash);
// Make sure that we stored an epoch hash.
Expand Down
151 changes: 32 additions & 119 deletions core/e2e/tests/epoch_change.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ use anyhow::Result;
use fleek_crypto::NodePublicKey;
use hp_fixed::unsigned::HpUfixed;
use lightning_e2e::swarm::{Swarm, SwarmNode};
use lightning_e2e::utils::rpc;
use lightning_interfaces::types::Staking;
use lightning_rpc::{Fleek, RpcClient};
use lightning_test_utils::config::LIGHTNING_TEST_HOME_DIR;
use lightning_test_utils::logging;
use resolved_pathbuf::ResolvedPathBuf;
use serde_json::json;
use serial_test::serial;

#[tokio::test]
Expand Down Expand Up @@ -43,41 +42,21 @@ async fn e2e_epoch_change_all_nodes_on_committee() -> Result<()> {
// Wait a bit for the nodes to start.
tokio::time::sleep(Duration::from_secs(5)).await;

let request = json!({
"jsonrpc": "2.0",
"method":"flk_get_epoch",
"params":[],
"id":1,
});
for (_, address) in swarm.get_rpc_addresses() {
let response = rpc::rpc_request(address, request.to_string())
.await
.unwrap();
let client = RpcClient::new_no_auth(&address)?;

let epoch = rpc::parse_response::<u64>(response)
.await
.expect("Failed to parse response.");
let epoch = client.get_epoch().await?;
assert_eq!(epoch, 0);
}

// The epoch will change after 40 seconds, and we already waited 5 seconds.
// To give some time for the epoch change, we will wait another 30 seconds here.
tokio::time::sleep(Duration::from_secs(30)).await;

let request = json!({
"jsonrpc": "2.0",
"method":"flk_get_epoch",
"params":[],
"id":1,
});
for (_, address) in swarm.get_rpc_addresses() {
let response = rpc::rpc_request(address, request.to_string())
.await
.unwrap();
let client = RpcClient::new_no_auth(&address)?;

let epoch = rpc::parse_response::<u64>(response)
.await
.expect("Failed to parse response.");
let epoch = client.get_epoch().await?;
assert_eq!(epoch, 1);
}

Expand Down Expand Up @@ -115,41 +94,21 @@ async fn e2e_epoch_change_with_edge_node() -> Result<()> {
// Wait a bit for the nodes to start.
tokio::time::sleep(Duration::from_secs(5)).await;

let request = json!({
"jsonrpc": "2.0",
"method":"flk_get_epoch",
"params":[],
"id":1,
});
for (_, address) in swarm.get_rpc_addresses() {
let response = rpc::rpc_request(address, request.to_string())
.await
.unwrap();
let client = RpcClient::new_no_auth(&address)?;
let epoch = client.get_epoch().await?;

let epoch = rpc::parse_response::<u64>(response)
.await
.expect("Failed to parse response.");
assert_eq!(epoch, 0);
}

// The epoch will change after 40 seconds, and we already waited 5 seconds.
// To give some time for the epoch change, we will wait another 30 seconds here.
tokio::time::sleep(Duration::from_secs(30)).await;

let request = json!({
"jsonrpc": "2.0",
"method":"flk_get_epoch",
"params":[],
"id":1,
});
for (_key, address) in swarm.get_rpc_addresses() {
let response = rpc::rpc_request(address, request.to_string())
.await
.unwrap();
let client = RpcClient::new_no_auth(&address)?;

let epoch = rpc::parse_response::<u64>(response)
.await
.expect("Failed to parse response.");
let epoch = client.get_epoch().await?;
assert_eq!(epoch, 1);
}

Expand Down Expand Up @@ -282,59 +241,23 @@ async fn e2e_test_staking_auction() -> Result<()> {
// Wait for epoch to change.
tokio::time::sleep(Duration::from_secs(30)).await;

// Get the new committee after the epoch change
let committee_member_request = json!({
"jsonrpc": "2.0",
"method":"flk_get_committee_members",
"params":[],
"id":1,
});

let response = rpc::rpc_request(rpc_endpoint.1.clone(), committee_member_request.to_string())
.await
.unwrap();

let current_committee: BTreeSet<NodePublicKey> =
rpc::parse_response::<Vec<NodePublicKey>>(response)
.await
.expect("Failed to parse response.")
.into_iter()
.collect();
let client = RpcClient::new_no_auth(&rpc_endpoint.1)?;
let response = client.get_committee_members(None).await?;
let current_committee: BTreeSet<NodePublicKey> = response.into_iter().collect();

current_committee
.iter()
.for_each(|node| println!("{:?}", node));

// Figure out the rep of our low staked nodes so we know which one shouldnt be on the committee
let rep_request_one = json!({
"jsonrpc": "2.0",
"method":"flk_get_reputation",
"params":[low_stake_nodes[0].clone()],
"id":1,
});
let rep_request_two = json!({
"jsonrpc": "2.0",
"method":"flk_get_reputation",
"params":[low_stake_nodes[1].clone()],
"id":1,
});

let response_one = rpc::rpc_request(rpc_endpoint.1.clone(), rep_request_one.to_string())
.await
.unwrap();
let response_two = rpc::rpc_request(rpc_endpoint.1.clone(), rep_request_two.to_string())
.await
.unwrap();

let rep_one: Option<u8> = rpc::parse_response::<Option<u8>>(response_one)
.await
.expect("Failed to parse response.");
let rep_two: Option<u8> = rpc::parse_response::<Option<u8>>(response_two)
.await
.expect("Failed to parse response.");
let rep_one = client
.get_reputation(low_stake_nodes[0].clone(), None)
.await?;
let rep_two = client
.get_reputation(low_stake_nodes[1].clone(), None)
.await?;

// Make sure the lower reputation node lost the tiebreaker and is not on the active node list
if rep_one.unwrap() <= rep_two.unwrap() {
if rep_one <= rep_two {
assert!(!current_committee.contains(low_stake_nodes[0]));
} else {
assert!(!current_committee.contains(low_stake_nodes[1]));
Expand All @@ -348,24 +271,15 @@ async fn compare_committee(
rpc_addresses: HashMap<NodePublicKey, String>,
committee_size: usize,
) -> BTreeSet<NodePublicKey> {
let request = json!({
"jsonrpc": "2.0",
"method":"flk_get_committee_members",
"params":[],
"id":1,
});

let rpc_addresses: Vec<(NodePublicKey, String)> = rpc_addresses.into_iter().collect();

let response = rpc::rpc_request(rpc_addresses[0].1.clone(), request.to_string())
let client = RpcClient::new_no_auth(&rpc_addresses[0].1).unwrap();
let target_committee: BTreeSet<_> = client
.get_committee_members(None)
.await
.unwrap();
let target_committee: BTreeSet<NodePublicKey> =
rpc::parse_response::<Vec<NodePublicKey>>(response)
.await
.expect("Failed to parse response.")
.into_iter()
.collect();
.unwrap()
.into_iter()
.collect();

// Make sure that the committee size equals the configured size.
assert_eq!(target_committee.len(), committee_size);
Expand All @@ -374,15 +288,14 @@ async fn compare_committee(
if &rpc_addresses[0].1 == address {
continue;
}
let response = rpc::rpc_request(address.clone(), request.to_string())
let client = RpcClient::new_no_auth(&address).unwrap();

let committee: BTreeSet<_> = client
.get_committee_members(None)
.await
.unwrap();
let committee: BTreeSet<NodePublicKey> =
rpc::parse_response::<Vec<NodePublicKey>>(response)
.await
.expect("Failed to parse response.")
.into_iter()
.collect();
.unwrap()
.into_iter()
.collect();

// Make sure all nodes have the same committee
assert_eq!(target_committee, committee);
Expand Down
Loading

0 comments on commit 3f46d54

Please sign in to comment.