Skip to content

Commit

Permalink
Add test_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind committed Oct 19, 2024
1 parent 754373f commit bfb69c8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 57 deletions.
2 changes: 1 addition & 1 deletion crates/sui-indexer-alt/src/ingestion/local_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl IngestionClientTrait for LocalIngestionClient {
#[cfg(test)]
pub(crate) mod tests {
use crate::ingestion::client::IngestionClient;
use crate::ingestion::remote_client::tests::test_checkpoint_data;
use crate::ingestion::test_utils::test_checkpoint_data;
use crate::metrics::tests::test_metrics;
use std::sync::Arc;
use sui_storage::blob::{Blob, BlobEncoding};
Expand Down
5 changes: 4 additions & 1 deletion crates/sui-indexer-alt/src/ingestion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ mod client;
pub mod error;
mod local_client;
mod remote_client;
#[cfg(test)]
mod test_utils;

pub struct IngestionService {
config: IngestionConfig,
Expand Down Expand Up @@ -202,7 +204,8 @@ mod tests {
use reqwest::StatusCode;
use wiremock::{MockServer, Request};

use crate::ingestion::remote_client::tests::{respond_with, status, test_checkpoint_data};
use crate::ingestion::remote_client::tests::{respond_with, status};
use crate::ingestion::test_utils::test_checkpoint_data;
use crate::metrics::tests::test_metrics;

use super::*;
Expand Down
56 changes: 1 addition & 55 deletions crates/sui-indexer-alt/src/ingestion/remote_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,16 @@ impl IngestionClientTrait for RemoteIngestionClient {
pub(crate) mod tests {
use super::*;
use crate::ingestion::client::IngestionClient;
use crate::ingestion::test_utils::test_checkpoint_data;
use crate::metrics::tests::test_metrics;
use axum::http::StatusCode;
use rand::{rngs::StdRng, SeedableRng};
use std::sync::Mutex;
use sui_storage::blob::{Blob, BlobEncoding};
use sui_types::full_checkpoint_content::CheckpointData;
use sui_types::{
crypto::KeypairTraits,
gas::GasCostSummary,
messages_checkpoint::{
CertifiedCheckpointSummary, CheckpointContents, CheckpointSummary,
SignedCheckpointSummary,
},
supported_protocol_versions::ProtocolConfig,
utils::make_committee_key,
};
use tokio_util::sync::CancellationToken;
use wiremock::{
matchers::{method, path_regex},
Mock, MockServer, Request, Respond, ResponseTemplate,
};

const RNG_SEED: [u8; 32] = [
21, 23, 199, 200, 234, 250, 252, 178, 94, 15, 202, 178, 62, 186, 88, 137, 233, 192, 130,
157, 179, 179, 65, 9, 31, 249, 221, 123, 225, 112, 199, 247,
];

pub(crate) async fn respond_with(server: &MockServer, response: impl Respond + 'static) {
Mock::given(method("GET"))
.and(path_regex(r"/\d+.chk"))
Expand All @@ -144,43 +127,6 @@ pub(crate) mod tests {
ResponseTemplate::new(code.as_u16())
}

pub(crate) fn test_checkpoint_data(cp: u64) -> Vec<u8> {
let mut rng = StdRng::from_seed(RNG_SEED);
let (keys, committee) = make_committee_key(&mut rng);
let contents = CheckpointContents::new_with_digests_only_for_tests(vec![]);
let summary = CheckpointSummary::new(
&ProtocolConfig::get_for_max_version_UNSAFE(),
0,
cp,
0,
&contents,
None,
GasCostSummary::default(),
None,
0,
Vec::new(),
);

let sign_infos: Vec<_> = keys
.iter()
.map(|k| {
let name = k.public().into();
SignedCheckpointSummary::sign(committee.epoch, &summary, k, name)
})
.collect();

let checkpoint_data = CheckpointData {
checkpoint_summary: CertifiedCheckpointSummary::new(summary, sign_infos, &committee)
.unwrap(),
checkpoint_contents: contents,
transactions: vec![],
};

Blob::encode(&checkpoint_data, BlobEncoding::Bcs)
.unwrap()
.to_bytes()
}

fn remote_test_client(uri: String) -> IngestionClient {
IngestionClient::new_remote(Url::parse(&uri).unwrap(), Arc::new(test_metrics())).unwrap()
}
Expand Down
56 changes: 56 additions & 0 deletions crates/sui-indexer-alt/src/ingestion/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use rand::prelude::StdRng;
use rand::SeedableRng;
use sui_storage::blob::{Blob, BlobEncoding};
use sui_types::crypto::KeypairTraits;
use sui_types::full_checkpoint_content::CheckpointData;
use sui_types::gas::GasCostSummary;
use sui_types::messages_checkpoint::{
CertifiedCheckpointSummary, CheckpointContents, CheckpointSummary, SignedCheckpointSummary,
};
use sui_types::supported_protocol_versions::ProtocolConfig;
use sui_types::utils::make_committee_key;

const RNG_SEED: [u8; 32] = [
21, 23, 199, 200, 234, 250, 252, 178, 94, 15, 202, 178, 62, 186, 88, 137, 233, 192, 130, 157,
179, 179, 65, 9, 31, 249, 221, 123, 225, 112, 199, 247,
];

pub(crate) fn test_checkpoint_data(cp: u64) -> Vec<u8> {
let mut rng = StdRng::from_seed(RNG_SEED);
let (keys, committee) = make_committee_key(&mut rng);
let contents = CheckpointContents::new_with_digests_only_for_tests(vec![]);
let summary = CheckpointSummary::new(
&ProtocolConfig::get_for_max_version_UNSAFE(),
0,
cp,
0,
&contents,
None,
GasCostSummary::default(),
None,
0,
Vec::new(),
);

let sign_infos: Vec<_> = keys
.iter()
.map(|k| {
let name = k.public().into();
SignedCheckpointSummary::sign(committee.epoch, &summary, k, name)
})
.collect();

let checkpoint_data = CheckpointData {
checkpoint_summary: CertifiedCheckpointSummary::new(summary, sign_infos, &committee)
.unwrap(),
checkpoint_contents: contents,
transactions: vec![],
};

Blob::encode(&checkpoint_data, BlobEncoding::Bcs)
.unwrap()
.to_bytes()
}

0 comments on commit bfb69c8

Please sign in to comment.