Skip to content

Commit

Permalink
Fix e2e tests (#18)
Browse files Browse the repository at this point in the history
* Fix e2e tests

* Fix compiler errors

* Don't create numbers that can't be converted to i64
  • Loading branch information
ligustah authored Apr 8, 2024
1 parent 34fba54 commit d153f5c
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 16 deletions.
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ edition = "2021"
[dependencies]
const-hex = "1.11.3"
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "fmt"] }
test-log = { version = "0.2.15", features = ["trace"] }
libp2p = { version = "0.53.2", features = [
"tokio",
"tcp",
Expand Down
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ pub struct Config {
// If not provided in the environment, the default is to use the public node
#[envconfig(from = "TRUSTED_PEERS")]
pub trusted_peers: Option<String>,

// node_id will only be used for logging purposes, if set
#[envconfig(from = "NODE_ID")]
pub node_id: Option<u64>,
}

#[derive(Debug, Clone, Copy, PartialEq)]
Expand Down Expand Up @@ -144,6 +148,7 @@ mod test {
chain_inclusion_mode: ChainInclusionMode::Check,
supported_chain_ids: "7777777".to_string(),
trusted_peers: None,
node_id: None,
};

let names = config.premint_names();
Expand All @@ -164,6 +169,7 @@ mod test {
chain_inclusion_mode: ChainInclusionMode::Check,
supported_chain_ids: "7777777".to_string(),
trusted_peers: None,
node_id: None,
};

let names = config.premint_names();
Expand Down
2 changes: 1 addition & 1 deletion src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl Controller {
tracing::warn!(
"Premint failed validation: {:?}, evaluation: {:?}",
premint,
evaluation
evaluation.summary()
);
Err(eyre::eyre!(evaluation.summary()))
}
Expand Down
10 changes: 7 additions & 3 deletions src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ impl SwarmController {
}
}

pub fn node_info(&self) -> MintpoolNodeInfo {
let peer_id = *self.swarm.local_peer_id();
let addr: Vec<Multiaddr> = self.swarm.listeners().cloned().collect();
MintpoolNodeInfo { peer_id, addr }
}

fn make_swarm_controller(id_keys: Keypair) -> eyre::Result<libp2p::Swarm<MintpoolBehaviour>> {
let peer_id = id_keys.public().to_peer_id();
let swarm = libp2p::SwarmBuilder::with_existing_identity(id_keys)
Expand Down Expand Up @@ -149,9 +155,7 @@ impl SwarmController {
}
}
SwarmCommand::ReturnNodeInfo { channel } => {
let peer_id = *self.swarm.local_peer_id();
let addr: Vec<Multiaddr> = self.swarm.listeners().cloned().collect();
if channel.send(MintpoolNodeInfo { peer_id, addr }).is_err() {
if channel.send(self.node_info()).is_err() {
tracing::error!("Error sending node info from swarm",);
}
}
Expand Down
28 changes: 23 additions & 5 deletions src/run.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use libp2p::identity;
use tracing::{info_span, Instrument};

use crate::config::{ChainInclusionMode, Config};
use crate::controller::{Controller, ControllerInterface};
Expand Down Expand Up @@ -29,17 +30,34 @@ pub async fn start_services(config: &Config) -> eyre::Result<ControllerInterface
let mut controller = Controller::new(swrm_cmd_send, event_recv, ext_cmd_recv, store, rules);
let controller_interface = ControllerInterface::new(ext_cmd_send);

let node_info = swarm_controller.node_info();
tracing::info!(
"Starting mintpool node with id: {:?}",
node_info.peer_id.to_string()
);

let port = config.peer_port;
let network_ip = config.initial_network_ip();
let node_id = config.node_id.clone();

tokio::spawn(async move {
swarm_controller
.run(port, network_ip)
.await
.expect("Swarm controller failed");
let future = swarm_controller.run(port, network_ip);

match node_id {
Some(node_id) => future.instrument(info_span!("", "node_id" = node_id)).await,
None => future.await,
}
.expect("Swarm controller failed");
});

let node_id = config.node_id.clone();
tokio::spawn(async move {
controller.run_loop().await;
let future = controller.run_loop();

match node_id {
Some(node_id) => future.instrument(info_span!("", "node_id" = node_id)).await,
None => future.await,
}
});

if config.chain_inclusion_mode == ChainInclusionMode::Check {
Expand Down
2 changes: 2 additions & 0 deletions src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ mod test {
supported_chain_ids: "7777777,".to_string(),
trusted_peers: None,
api_port: 0,
node_id: None,
};

let store = PremintStorage::new(&config).await;
Expand All @@ -167,6 +168,7 @@ mod test {
chain_inclusion_mode: ChainInclusionMode::Check,
supported_chain_ids: "7777777,".to_string(),
trusted_peers: None,
node_id: None,
};

let store = PremintStorage::new(&config).await;
Expand Down
11 changes: 11 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ pub struct SimplePremint {
media: String,
}

impl SimplePremint {
pub fn new(chain_id: U256, sender: Address, token_id: u64, media: String) -> Self {
Self {
chain_id,
sender,
token_id,
media,
}
}
}

#[async_trait]
impl Premint for SimplePremint {
fn metadata(&self) -> PremintMetadata {
Expand Down
43 changes: 43 additions & 0 deletions tests/common/factories.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use alloy_primitives::{Address, U256};
use mintpool::types::SimplePremint;
use rand::{Rng, RngCore};

pub trait Factory<O>
where
Self: Sized,
O: Default,
{
fn build(options: O) -> Self;
fn build_default() -> Self {
Self::build(O::default())
}
}

#[derive(Default)]
pub struct SimplePremintOptions {
chain_id: Option<u64>,
sender: Option<Address>,
media: Option<String>,
token_id: Option<u64>,
}

impl Factory<SimplePremintOptions> for SimplePremint {
fn build(options: SimplePremintOptions) -> Self {
let mut rng = rand::thread_rng();

Self::new(
U256::from(
options
.chain_id
.unwrap_or(rng.gen_range(1..=i64::MAX as u64)),
),
options
.sender
.unwrap_or(Address::from(rng.gen::<[u8; 20]>())),
options.token_id.unwrap_or(rng.next_u64()),
options
.media
.unwrap_or("http://example.com/token".to_string()),
)
}
}
1 change: 1 addition & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod factories;
19 changes: 13 additions & 6 deletions tests/p2p_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
mod common;

use common::factories::Factory;
use mintpool::controller::ControllerCommands;
use mintpool::controller::ControllerCommands::Broadcast;
use mintpool::types::PremintTypes;
use mintpool::types::{PremintTypes, SimplePremint};
use tokio::time;
use tracing_subscriber::EnvFilter;

#[tokio::test]
#[test_log::test(tokio::test)]
// test to make sure that nodes can connect to a specified host
async fn test_connecting_to_other_nodes() {
let num_nodes = 10;
Expand All @@ -21,7 +25,7 @@ async fn test_connecting_to_other_nodes() {
}
}

#[tokio::test]
#[test_log::test(tokio::test)]
// test announcing self to the network
async fn test_announcing_to_network() {
let num_nodes = 3;
Expand All @@ -43,7 +47,7 @@ async fn test_announcing_to_network() {
}
}

#[tokio::test]
#[test_log::test(tokio::test)]
// After a premint is announced, all connected nodes should be able to list it
async fn test_list_all_premints() {
let num_nodes = 3;
Expand All @@ -60,7 +64,7 @@ async fn test_list_all_premints() {

first
.send_command(Broadcast {
message: PremintTypes::Simple(Default::default()),
message: PremintTypes::Simple(SimplePremint::build_default()),
})
.await
.unwrap();
Expand All @@ -79,7 +83,7 @@ async fn test_list_all_premints() {
}
}

#[tokio::test]
#[test_log::test(tokio::test)]
// Connections should not be able to exceed max_connections config
async fn test_max_connections() {
let num_nodes = 5;
Expand All @@ -98,6 +102,7 @@ async fn test_max_connections() {
mod build {
use mintpool::config::{ChainInclusionMode, Config};
use mintpool::controller::{ControllerCommands, ControllerInterface};
use mintpool::types::Premint;
use tokio::time;

pub async fn announce_all(nodes: Vec<ControllerInterface>) {
Expand Down Expand Up @@ -140,6 +145,7 @@ mod build {
chain_inclusion_mode: ChainInclusionMode::Check,
supported_chain_ids: "7777777".to_string(),
trusted_peers: None,
node_id: Some(i),
};

let ctl = mintpool::run::start_services(&config).await.unwrap();
Expand All @@ -152,6 +158,7 @@ mod build {
start_port: u64,
num_nodes: u64,
) -> Vec<ControllerInterface> {
tracing::info!("Generating swarm of {} nodes", num_nodes);
let nodes = make_nodes(start_port, num_nodes, 1000).await;
connect_all_to_first(nodes.clone()).await;
time::sleep(time::Duration::from_secs(1)).await;
Expand Down

0 comments on commit d153f5c

Please sign in to comment.