Skip to content

Commit

Permalink
test: use OpsBuilder in prover unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jns-ps committed Oct 24, 2024
1 parent 70f8241 commit ca8af94
Showing 1 changed file with 39 additions and 82 deletions.
121 changes: 39 additions & 82 deletions crates/node_types/prover/src/prover/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use prism_common::tree::Proof;
use prism_common::{test_ops::OpsBuilder, tree::Proof};
use std::{self, sync::Arc, time::Duration};
use tokio::spawn;

Expand All @@ -17,37 +17,22 @@ async fn create_test_prover() -> Arc<Prover> {
}

fn create_mock_operations(service_id: String) -> Vec<Operation> {
let signing_key_1 = create_mock_signing_key();
let signing_key_2 = create_mock_signing_key();
let new_key = create_mock_signing_key().verifying_key();
let service_key = create_mock_signing_key();
let mut ops_builder = OpsBuilder::new();

vec![
Operation::new_register_service(service_id.clone(), service_key.clone().into()),
Operation::new_create_account(
"user1@example.com".to_string(),
&signing_key_1,
service_id.clone(),
&service_key,
)
.unwrap(),
Operation::new_create_account(
"user2@example.com".to_string(),
&signing_key_2,
service_id.clone(),
&service_key,
)
.unwrap(),
Operation::new_add_key("user1@example.com".to_string(), new_key, &signing_key_1, 0)
.unwrap(),
ops_builder.register_service_with_random_key(&service_id).ex(),
ops_builder.create_account_with_random_key("user1@example.com", &service_id).ex(),
ops_builder.create_account_with_random_key("user2@example.com", &service_id).ex(),
ops_builder.add_random_key_verified_with_root("user1@example.com").ex(),
]
}

#[tokio::test]
async fn test_validate_and_queue_update() {
let prover = create_test_prover().await;

let service_key = create_mock_signing_key();
let op = Operation::new_register_service("service_id".to_string(), service_key.clone().into());
let mut ops_builder = OpsBuilder::new();
let op = ops_builder.register_service_with_random_key("test_service").ex();

prover.clone().validate_and_queue_update(&op).await.unwrap();

Expand All @@ -61,42 +46,34 @@ async fn test_validate_and_queue_update() {
async fn test_process_operation() {
let prover = create_test_prover().await;

let signing_key = create_mock_signing_key();
let original_pubkey = signing_key.verifying_key();
let service_key = create_mock_signing_key();

let register_service_op =
Operation::new_register_service("service_id".to_string(), service_key.clone().into());
let create_account_op = Operation::new_create_account(
"test@example.com".to_string(),
&signing_key,
"service_id".to_string(),
&service_key,
)
.unwrap();

let proof = prover
.process_operation(&register_service_op)
.await
.unwrap();
let mut ops_builder = OpsBuilder::new();
let register_service_op = ops_builder.register_service_with_random_key("test_service").ex();
let create_account_op =
ops_builder.create_account_with_random_key("test_account", "test_service").ex();

let proof = prover.process_operation(&register_service_op).await.unwrap();
assert!(matches!(proof, Proof::Insert(_)));

let proof = prover.process_operation(&create_account_op).await.unwrap();
assert!(matches!(proof, Proof::Insert(_)));

let new_key = create_mock_signing_key();
let pubkey = new_key.verifying_key();
let add_key_op =
Operation::new_add_key("test@example.com".to_string(), pubkey, &signing_key, 0).unwrap();
ops_builder.add_key_verified_with_root("test_account", new_key.verifying_key()).ex();

let proof = prover.process_operation(&add_key_op).await.unwrap();

assert!(matches!(proof, Proof::Update(_)));

// Revoke original key
let revoke_op =
Operation::new_revoke_key("test@example.com".to_string(), original_pubkey, &new_key, 1)
.unwrap();
let revoke_op = ops_builder
.revoke_key(
"test_account",
create_account_op.get_public_key().cloned().unwrap(),
&new_key,
1,
)
.ex();
let proof = prover.process_operation(&revoke_op).await.unwrap();
assert!(matches!(proof, Proof::Update(_)));
}
Expand All @@ -105,45 +82,20 @@ async fn test_process_operation() {
async fn test_execute_block_with_invalid_tx() {
let prover = create_test_prover().await;

let signing_key_1 = create_mock_signing_key();
let signing_key_2 = create_mock_signing_key();
let signing_key_3 = create_mock_signing_key();
let service_key = create_mock_signing_key();
let mut ops_builder = OpsBuilder::new();

let new_key_1 = create_mock_signing_key();

let operations = vec![
Operation::new_register_service("service_id".to_string(), service_key.clone().into()),
Operation::new_create_account(
"user1@example.com".to_string(),
&signing_key_1,
"service_id".to_string(),
&service_key,
)
.unwrap(),
// add signing_key_2, so it will be index = 1
Operation::new_add_key(
"user1@example.com".to_string(),
signing_key_2.verifying_key(),
&signing_key_1,
0,
)
.unwrap(),
// try revoking signing_key_2
Operation::new_revoke_key(
"user1@example.com".to_string(),
signing_key_2.verifying_key(),
&signing_key_1,
0,
)
.unwrap(),
ops_builder.register_service_with_random_key("service_id").ex(),
ops_builder.create_account_with_random_key("account_id", "service_id").ex(),
// add new key, so it will be index = 1
ops_builder.add_key_verified_with_root("account_id", new_key_1.verifying_key()).ex(),
// revoke new key again
ops_builder.revoke_key_verified_with_root("account_id", new_key_1.verifying_key()).ex(),
// and adding in same block.
// both of these operations are valid individually, but when processed together it will fail.
Operation::new_add_key(
"user1@example.com".to_string(),
signing_key_3.verifying_key(),
&signing_key_2,
1,
)
.unwrap(),
ops_builder.add_random_key("account_id", &new_key_1, 1).op(),
];

let proofs = prover.execute_block(operations).await.unwrap();
Expand All @@ -165,11 +117,16 @@ async fn test_finalize_new_epoch() {
let prover = create_test_prover().await;
let operations = create_mock_operations("test_service".to_string());

println!("Operations");
let prev_commitment = prover.get_commitment().await.unwrap();
println!("Prev commitment: {}", prev_commitment);
prover.finalize_new_epoch(0, operations).await.unwrap();
println!("Finalized epoch");

let new_commitment = prover.get_commitment().await.unwrap();
println!("New commitment: {}", prev_commitment);
assert_ne!(prev_commitment, new_commitment);
println!("End");
}

#[tokio::test]
Expand Down

0 comments on commit ca8af94

Please sign in to comment.