This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: adding certificate precedence ordering
Signed-off-by: Simon Paitrault <[email protected]>
- Loading branch information
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
crates/topos-tce-api/tests/grpc/certificate_precedence.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use base64ct::{Base64, Encoding}; | ||
use rstest::rstest; | ||
use std::sync::Arc; | ||
use test_log::test; | ||
use tokio_stream::Stream; | ||
use topos_api::grpc::tce::v1::{GetLastPendingCertificatesRequest, LastPendingCertificate}; | ||
use topos_core::uci::Certificate; | ||
use topos_tce_api::RuntimeEvent; | ||
use topos_test_sdk::{ | ||
certificates::create_certificate_chain, | ||
constants::{SOURCE_SUBNET_ID_1, TARGET_SUBNET_ID_1}, | ||
storage::{create_fullnode_store, create_validator_store, storage_client}, | ||
tce::public_api::{broadcast_stream, create_public_api, PublicApiContext}, | ||
}; | ||
|
||
use topos_tce_storage::{types::CertificateDeliveredWithPositions, validator::ValidatorStore}; | ||
|
||
#[rstest] | ||
#[test(tokio::test)] | ||
async fn fetch_latest_pending_certificates() { | ||
let fullnode_store = create_fullnode_store(vec![]).await; | ||
let validator_store: Arc<ValidatorStore> = | ||
create_validator_store(vec![], futures::future::ready(fullnode_store.clone())).await; | ||
|
||
let (tx, rx): ( | ||
_, | ||
tokio::sync::broadcast::Receiver<CertificateDeliveredWithPositions>, | ||
) = tokio::sync::broadcast::channel(10); | ||
|
||
let (mut api_context, _) = create_public_api( | ||
storage_client(vec![]), | ||
broadcast_stream(), | ||
futures::future::ready(validator_store.clone()), | ||
) | ||
.await; | ||
let mut client = api_context.api_client; | ||
let certificates = create_certificate_chain(SOURCE_SUBNET_ID_1, &[TARGET_SUBNET_ID_1], 2); | ||
|
||
let expected = certificates[1].certificate.clone(); | ||
assert!(validator_store | ||
.insert_pending_certificate(&certificates[1].certificate) | ||
.unwrap() | ||
.is_none()); | ||
|
||
validator_store | ||
.insert_pending_certificate(&certificates[0].certificate) | ||
.unwrap() | ||
.unwrap(); | ||
|
||
let mut res = client | ||
.get_last_pending_certificates(GetLastPendingCertificatesRequest { | ||
subnet_ids: vec![SOURCE_SUBNET_ID_1.into()], | ||
}) | ||
.await | ||
.unwrap() | ||
.into_inner(); | ||
|
||
let res: LastPendingCertificate = res | ||
.last_pending_certificate | ||
.remove(&Base64::encode_string(SOURCE_SUBNET_ID_1.as_array())) | ||
.unwrap(); | ||
|
||
let res: Certificate = res.value.unwrap().try_into().unwrap(); | ||
|
||
assert_eq!(res, expected); | ||
} | ||
|
||
#[rstest] | ||
#[test(tokio::test)] | ||
async fn fetch_latest_pending_certificates_with_conflicts() { | ||
let fullnode_store = create_fullnode_store(vec![]).await; | ||
let validator_store: Arc<ValidatorStore> = | ||
create_validator_store(vec![], futures::future::ready(fullnode_store.clone())).await; | ||
|
||
let (tx, rx): ( | ||
_, | ||
tokio::sync::broadcast::Receiver<CertificateDeliveredWithPositions>, | ||
) = tokio::sync::broadcast::channel(10); | ||
|
||
let (mut api_context, _) = create_public_api( | ||
storage_client(vec![]), | ||
broadcast_stream(), | ||
futures::future::ready(validator_store.clone()), | ||
) | ||
.await; | ||
let mut client = api_context.api_client; | ||
let mut certificates = create_certificate_chain(SOURCE_SUBNET_ID_1, &[TARGET_SUBNET_ID_1], 3); | ||
|
||
certificates[2].certificate.prev_id = certificates[1].certificate.prev_id; | ||
|
||
let expected = certificates[2].certificate.clone(); | ||
|
||
assert!(validator_store | ||
.insert_pending_certificate(&certificates[1].certificate) | ||
.unwrap() | ||
.is_none()); | ||
|
||
assert!(validator_store | ||
.insert_pending_certificate(&certificates[2].certificate) | ||
.unwrap() | ||
.is_none()); | ||
|
||
validator_store | ||
.insert_pending_certificate(&certificates[0].certificate) | ||
.unwrap() | ||
.unwrap(); | ||
|
||
let res = validator_store.get_pending_certificates_for_subnets(&[SOURCE_SUBNET_ID_1]); | ||
|
||
let mut res = client | ||
.get_last_pending_certificates(GetLastPendingCertificatesRequest { | ||
subnet_ids: vec![SOURCE_SUBNET_ID_1.into()], | ||
}) | ||
.await | ||
.unwrap() | ||
.into_inner(); | ||
|
||
let res: LastPendingCertificate = res | ||
.last_pending_certificate | ||
.remove(&Base64::encode_string(SOURCE_SUBNET_ID_1.as_array())) | ||
.unwrap(); | ||
|
||
let res: Certificate = res.value.unwrap().try_into().unwrap(); | ||
|
||
assert_eq!(res, expected); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod certificate_precedence; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters