Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration tests for exchange server/client #93

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ wasm-bindgen-futures = "0.4.37"
bytes = "1.4.0"
celestia-rpc = { workspace = true }
celestia-types = { workspace = true, features = ["test-utils"] }
celestia-node = { workspace = true, features = ["test-utils"] }
dotenvy = "0.15.7"
libp2p = { version = "0.52.3", features = [
"ed25519",
Expand All @@ -48,3 +49,6 @@ libp2p = { version = "0.52.3", features = [
] }
rand = "0.8.5"
serde_json = "1.0.107"

[features]
test-utils = ["celestia-types/test-utils"]
2 changes: 1 addition & 1 deletion node/src/exchange/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ fn parse_request(request: HeaderRequest) -> Option<(u64, header_request::Data)>
mod tests {
use super::*;
use crate::exchange::utils::HeaderRequestExt;
use crate::store::tests::gen_filled_store;
use crate::store::InMemoryStore;
use crate::test_utils::gen_filled_store;
use celestia_proto::p2p::pb::header_request::Data;
use celestia_proto::p2p::pb::{HeaderRequest, StatusCode};
use celestia_types::ExtendedHeader;
Expand Down
2 changes: 2 additions & 0 deletions node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub mod p2p;
pub mod peer_tracker;
pub mod store;
pub mod syncer;
#[cfg(any(test, feature = "test-utils"))]
pub mod test_utils;
mod utils;

#[async_trait]
Expand Down
25 changes: 11 additions & 14 deletions node/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,20 @@ impl Default for InMemoryStore {
}
}

impl Clone for InMemoryStore {
fn clone(&self) -> Self {
InMemoryStore {
headers: self.headers.clone(),
height_to_hash: self.height_to_hash.clone(),
head_height: AtomicU64::new(self.head_height.load(Ordering::Acquire)),
}
}
}

#[cfg(test)]
pub mod tests {
use super::*;
use crate::test_utils::gen_filled_store;
use celestia_types::test_utils::ExtendedHeaderGenerator;
use celestia_types::Height;

Expand Down Expand Up @@ -390,18 +401,4 @@ pub mod tests {
Err(StoreError::NonContinuousAppend(0, 5))
));
}

pub fn gen_filled_store(amount: u64) -> (InMemoryStore, ExtendedHeaderGenerator) {
let s = InMemoryStore::new();
let mut gen = ExtendedHeaderGenerator::new();

let headers = gen.next_many(amount);

for header in headers {
s.append_single_unchecked(header)
.expect("inserting test data failed");
}

(s, gen)
}
}
59 changes: 59 additions & 0 deletions node/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use celestia_types::test_utils::ExtendedHeaderGenerator;

use libp2p::{
core::{muxing::StreamMuxerBox, transport::Boxed, upgrade::Version},
identity::{self, Keypair},
noise, tcp, yamux, PeerId, Transport,
};

use crate::{node::NodeConfig, store::InMemoryStore};

pub fn gen_filled_store(amount: u64) -> (InMemoryStore, ExtendedHeaderGenerator) {
let s = InMemoryStore::new();
let mut gen = ExtendedHeaderGenerator::new();

let headers = gen.next_many(amount);

for header in headers {
s.append_single_unchecked(header)
.expect("inserting test data failed");
}

(s, gen)
}

fn tcp_transport(local_keypair: &Keypair) -> Boxed<(PeerId, StreamMuxerBox)> {
tcp::tokio::Transport::default()
.upgrade(Version::V1Lazy)
.authenticate(noise::Config::new(local_keypair).unwrap())
.multiplex(yamux::Config::default())
.boxed()
}

// helpers to use with struct update syntax to avoid spelling out all the details
pub fn test_node_config() -> NodeConfig<InMemoryStore> {
let node_keypair = identity::Keypair::generate_ed25519();
NodeConfig {
network_id: "private".to_string(),
p2p_transport: tcp_transport(&node_keypair),
p2p_local_keypair: node_keypair,
p2p_bootstrap_peers: vec![],
p2p_listen_on: vec![],
store: InMemoryStore::new(),
}
}

pub fn listening_test_node_config() -> NodeConfig<InMemoryStore> {
NodeConfig {
p2p_listen_on: vec!["/ip4/0.0.0.0/tcp/0".parse().unwrap()],
..test_node_config()
}
}

pub fn test_node_config_with_keypair(keypair: Keypair) -> NodeConfig<InMemoryStore> {
NodeConfig {
p2p_transport: tcp_transport(&keypair),
p2p_local_keypair: keypair,
..test_node_config()
}
}
Loading