From ca8af94812ab0b9b6c3c5987078e26d75c5c444a Mon Sep 17 00:00:00 2001 From: Jonas Date: Thu, 24 Oct 2024 17:20:08 +0200 Subject: [PATCH] test: use OpsBuilder in prover unit tests --- crates/node_types/prover/src/prover/tests.rs | 121 ++++++------------- 1 file changed, 39 insertions(+), 82 deletions(-) diff --git a/crates/node_types/prover/src/prover/tests.rs b/crates/node_types/prover/src/prover/tests.rs index a531a506..5dba43c2 100644 --- a/crates/node_types/prover/src/prover/tests.rs +++ b/crates/node_types/prover/src/prover/tests.rs @@ -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; @@ -17,28 +17,13 @@ async fn create_test_prover() -> Arc { } fn create_mock_operations(service_id: String) -> Vec { - 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(), ] } @@ -46,8 +31,8 @@ fn create_mock_operations(service_id: String) -> Vec { 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(); @@ -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(®ister_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(®ister_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(_))); } @@ -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(); @@ -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]