Skip to content

Commit

Permalink
add test-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
fl0rek committed Sep 20, 2023
1 parent da5c6fc commit 00f886a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 68 deletions.
2 changes: 1 addition & 1 deletion node/src/exchange/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ where
mod tests {
use super::*;
use crate::exchange::utils::ExtendedHeaderExt;
use crate::store::tests::gen_extended_header;
use crate::test_utils::gen_extended_header;
use celestia_proto::p2p::pb::header_request::Data;
use celestia_proto::p2p::pb::StatusCode;
use celestia_types::consts::HASH_SIZE;
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(test)]
pub mod test_utils;
mod utils;

#[async_trait]
Expand Down
70 changes: 3 additions & 67 deletions node/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use dashmap::DashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use tendermint::Hash;
use thiserror::Error;
use tracing::{error, info, instrument};
use tracing::{info, instrument};

type Result<T, E = StoreError> = std::result::Result<T, E>;

Expand Down Expand Up @@ -134,6 +134,7 @@ impl InMemoryStore {
self.head_height.load(Ordering::Acquire)
}

#[instrument(err)]
pub fn append_continuous(&self, header: ExtendedHeader) -> Result<(), StoreError> {
let hash = header.hash();
let height = header.height();
Expand Down Expand Up @@ -227,73 +228,8 @@ impl Default for InMemoryStore {
#[cfg(test)]
pub mod tests {
use super::*;
use celestia_proto::header::pb::ExtendedHeader as RawExtendedHeader;
use celestia_types::ExtendedHeader;
use celestia_types::{DataAvailabilityHeader, ValidatorSet};
use tendermint::block::header::Header;
use tendermint::block::Commit;
use crate::test_utils::{gen_extended_header, gen_filled_store};
use tendermint::block::Height;
use tendermint::Hash;
use tendermint::Time;
use tendermint::{block::header::Version, AppHash};

pub fn gen_extended_header(height: u64) -> ExtendedHeader {
RawExtendedHeader {
header: Some(
Header {
version: Version { block: 11, app: 1 },
chain_id: "private".to_string().try_into().unwrap(),
height: height.try_into().unwrap(),
time: Time::now(),
last_block_id: None,
last_commit_hash: Hash::default(),
data_hash: Hash::default(),
validators_hash: Hash::default(),
next_validators_hash: Hash::default(),
consensus_hash: Hash::default(),
app_hash: AppHash::default(),
last_results_hash: Hash::default(),
evidence_hash: Hash::default(),
proposer_address: tendermint::account::Id::new([0; 20]),
}
.into(),
),
commit: Some(
Commit {
height: height.try_into().unwrap(),
block_id: tendermint::block::Id {
hash: Hash::Sha256(rand::random()),
..Default::default()
},
..Default::default()
}
.into(),
),
validator_set: Some(ValidatorSet::new(Vec::new(), None).into()),
dah: Some(
DataAvailabilityHeader {
row_roots: Vec::new(),
column_roots: Vec::new(),
hash: [0; 32],
}
.into(),
),
}
.try_into()
.unwrap()
}

pub fn gen_filled_store(height: u64) -> InMemoryStore {
let s = InMemoryStore::new();

// block height is 1-indexed
for height in 1..=height {
s.append_continuous(gen_extended_header(height))
.expect("inserting test data failed");
}

s
}

#[test]
fn test_empty_store() {
Expand Down
68 changes: 68 additions & 0 deletions node/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use celestia_proto::header::pb::ExtendedHeader as RawExtendedHeader;
use celestia_types::ExtendedHeader;
use celestia_types::{DataAvailabilityHeader, ValidatorSet};
use tendermint::block::header::Header;
use tendermint::block::Commit;
use tendermint::Hash;
use tendermint::Time;
use tendermint::{block::header::Version, AppHash};

use crate::store::InMemoryStore;

pub fn gen_extended_header(height: u64) -> ExtendedHeader {
RawExtendedHeader {
header: Some(
Header {
version: Version { block: 11, app: 1 },
chain_id: "private".to_string().try_into().unwrap(),
height: height.try_into().unwrap(),
time: Time::now(),
last_block_id: None,
last_commit_hash: Hash::default(),
data_hash: Hash::default(),
validators_hash: Hash::default(),
next_validators_hash: Hash::default(),
consensus_hash: Hash::default(),
app_hash: AppHash::default(),
last_results_hash: Hash::default(),
evidence_hash: Hash::default(),
proposer_address: tendermint::account::Id::new([0; 20]),
}
.into(),
),
commit: Some(
Commit {
height: height.try_into().unwrap(),
block_id: tendermint::block::Id {
hash: Hash::Sha256(rand::random()),
..Default::default()
},
..Default::default()
}
.into(),
),
validator_set: Some(ValidatorSet::new(Vec::new(), None).into()),
dah: Some(
DataAvailabilityHeader {
row_roots: Vec::new(),
column_roots: Vec::new(),
hash: [0; 32],
}
.into(),
),
}
.try_into()
.unwrap()
}

pub fn gen_filled_store(height: u64) -> InMemoryStore {
let s = InMemoryStore::new();

// block height is 1-indexed
for height in 1..=height {
s.append_continuous(gen_extended_header(height))
.expect("inserting test data failed");
}

s
}

0 comments on commit 00f886a

Please sign in to comment.