From 230af4a96caa1f134fa6e7eb2ba042a7a8adcba6 Mon Sep 17 00:00:00 2001 From: "artem.ivanov" Date: Thu, 15 Feb 2024 10:37:37 +0400 Subject: [PATCH] Fixed merge errors Signed-off-by: artem.ivanov --- vdr/Cargo.lock | 30 ---- vdr/src/client/client.rs | 53 ++---- .../cl/credential_definition_registry.rs | 4 +- vdr/src/contracts/cl/mod.rs | 5 + vdr/src/contracts/cl/types/schema.rs | 10 +- vdr/src/contracts/network/mod.rs | 3 + vdr/src/types/contract.rs | 2 +- vdr/src/types/mod.rs | 2 +- vdr/src/types/transaction.rs | 161 +++++++++++------- 9 files changed, 135 insertions(+), 135 deletions(-) diff --git a/vdr/Cargo.lock b/vdr/Cargo.lock index 5cf13669..0a7685f5 100644 --- a/vdr/Cargo.lock +++ b/vdr/Cargo.lock @@ -1050,10 +1050,6 @@ name = "futures-timer" version = "3.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c" -dependencies = [ - "gloo-timers", - "send_wrapper", -] [[package]] name = "futures-util" @@ -1091,10 +1087,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" dependencies = [ "cfg-if", - "js-sys", "libc", "wasi", - "wasm-bindgen", ] [[package]] @@ -1441,7 +1435,6 @@ dependencies = [ "serde_json", "sha3", "thiserror", - "web-sys", "web3", ] @@ -2499,12 +2492,6 @@ version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b97ed7a9823b74f99c7742f5336af7be5ecd3eeafcb1507d1fa93347b1d589b0" -[[package]] -name = "send_wrapper" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f638d531eccd6e23b980caf34876660d38e265409d8e99b397ab71eb3612fad0" - [[package]] name = "serde" version = "1.0.196" @@ -2514,17 +2501,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde-wasm-bindgen" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3b143e2833c57ab9ad3ea280d21fd34e285a42837aeb0ee301f4f41890fa00e" -dependencies = [ - "js-sys", - "serde", - "wasm-bindgen", -] - [[package]] name = "serde_derive" version = "1.0.196" @@ -3185,22 +3161,18 @@ dependencies = [ "ethereum-types", "futures", "futures-timer", - "getrandom", "headers", "hex", "idna 0.4.0", - "js-sys", "jsonrpc-core", "log", "once_cell", "parking_lot", "pin-project", - "rand", "reqwest", "rlp", "secp256k1 0.27.0", "serde", - "serde-wasm-bindgen", "serde_json", "soketto", "tiny-keccak", @@ -3208,8 +3180,6 @@ dependencies = [ "tokio-stream", "tokio-util", "url", - "wasm-bindgen", - "wasm-bindgen-futures", "web3-async-native-tls", ] diff --git a/vdr/src/client/client.rs b/vdr/src/client/client.rs index ef45a946..f12bca8b 100644 --- a/vdr/src/client/client.rs +++ b/vdr/src/client/client.rs @@ -213,9 +213,11 @@ impl Debug for LedgerClient { #[cfg(test)] pub mod test { use super::*; - use crate::{client::MockClient, signer::basic_signer::test::basic_signer}; + use crate::{ + client::MockClient, types::transaction::test::read_transaction, utils::init_env_logger, + }; use once_cell::sync::Lazy; - use std::{env, fs, sync::RwLock}; + use std::{env, fs}; pub const CHAIN_ID: u64 = 1337; pub const CONTRACTS_SPEC_BASE_PATH: &str = "../smart_contracts/artifacts/contracts/"; @@ -313,6 +315,7 @@ pub mod test { } pub fn mock_client() -> LedgerClient { + init_env_logger(); let mut ledger_client = LedgerClient::new( CHAIN_ID, RPC_NODE_ADDRESS, @@ -328,38 +331,6 @@ pub mod test { ledger_client } - pub fn write_transaction() -> Transaction { - let transaction = Transaction { - type_: TransactionType::Write, - from: Some(TRUSTEE_ACC.clone()), - to: VALIDATOR_CONTROL_ADDRESS.clone(), - nonce: Some(DEFAULT_NONCE.clone()), - chain_id: CHAIN_ID, - data: vec![], - signature: RwLock::new(None), - hash: None, - }; - let signer = basic_signer(); - let sign_bytes = transaction.get_signing_bytes().unwrap(); - let signature = signer.sign(&sign_bytes, TRUSTEE_ACC.as_ref()).unwrap(); - transaction.set_signature(signature); - - transaction - } - - pub fn read_transaction() -> Transaction { - Transaction { - type_: TransactionType::Read, - from: None, - to: VALIDATOR_CONTROL_ADDRESS.clone(), - nonce: None, - chain_id: CHAIN_ID, - data: vec![], - signature: RwLock::new(None), - hash: None, - } - } - pub fn mock_custom_client(client: Box) -> LedgerClient { let mut ledger_client = LedgerClient::new( CHAIN_ID, @@ -413,17 +384,17 @@ pub mod test { name: VALIDATOR_CONTROL_NAME.to_string(), abi: Value::Array(vec ! []), }), - }], VdrError::ContractInvalidSpec("".to_string()))] + }], VdrError::ContractInvalidSpec("Either `spec_path` or `spec` must be provided".to_string()))] #[case::non_existent_spec_path(vec![ContractConfig { address: VALIDATOR_CONTROL_ADDRESS.to_string(), spec_path: Some(build_contract_path("")), spec: None, - }], VdrError::ContractInvalidSpec("".to_string()))] + }], VdrError::ContractInvalidSpec("Unable to read contract spec file. Err: \"Is a directory (os error 21)\"".to_string()))] #[case::empty_contract_spec(vec![ContractConfig { address: VALIDATOR_CONTROL_ADDRESS.to_string(), spec_path: None, spec: None, - }], VdrError::ContractInvalidSpec("".to_string()))] + }], VdrError::ContractInvalidSpec("Either `spec_path` or `spec` must be provided".to_string()))] fn test_create_client_errors( #[case] contract_config: Vec, #[case] expected_error: VdrError, @@ -432,12 +403,12 @@ pub mod test { .err() .unwrap(); - assert!(matches!(client_err, expected_error)); + assert_eq!(client_err, expected_error); } #[rstest] - #[case::empty_recipient_address("", VdrError::ClientInvalidTransaction("".to_string()))] - #[case::invalid_recipient_address(INVALID_ADDRESS, VdrError::ClientInvalidTransaction("".to_string()))] + #[case::empty_recipient_address("", VdrError::ClientInvalidTransaction("Invalid transaction target address \"0x\"".to_string()))] + #[case::invalid_recipient_address(INVALID_ADDRESS, VdrError::ClientInvalidTransaction("Invalid transaction target address \"0x123\"".to_string()))] async fn call_transaction_various_recipient_addresses( #[case] recipient_address: &str, #[case] expected_error: VdrError, @@ -450,7 +421,7 @@ pub mod test { let error = client.submit_transaction(&transaction).await.unwrap_err(); - assert!(matches!(error, expected_error)); + assert_eq!(error, expected_error); } #[async_std::test] diff --git a/vdr/src/contracts/cl/credential_definition_registry.rs b/vdr/src/contracts/cl/credential_definition_registry.rs index c5afd757..5773f094 100644 --- a/vdr/src/contracts/cl/credential_definition_registry.rs +++ b/vdr/src/contracts/cl/credential_definition_registry.rs @@ -400,8 +400,8 @@ pub mod test { } mod parse_resolve_credential_definition_result { - use super::*; - + // use super::*; + // // #[test] // fn parse_resolve_credential_definition_result_test() { // init_env_logger(); diff --git a/vdr/src/contracts/cl/mod.rs b/vdr/src/contracts/cl/mod.rs index 1663eb1d..391a0f31 100644 --- a/vdr/src/contracts/cl/mod.rs +++ b/vdr/src/contracts/cl/mod.rs @@ -1,3 +1,8 @@ pub mod credential_definition_registry; pub mod schema_registry; pub mod types; + +pub use types::{ + credential_definition::CredentialDefinition, credential_definition_id::CredentialDefinitionId, + schema::Schema, schema_id::SchemaId, +}; diff --git a/vdr/src/contracts/cl/types/schema.rs b/vdr/src/contracts/cl/types/schema.rs index 97716089..a557fd8e 100644 --- a/vdr/src/contracts/cl/types/schema.rs +++ b/vdr/src/contracts/cl/types/schema.rs @@ -103,12 +103,18 @@ pub mod test { contracts::{cl::types::schema_id::SchemaId, did::types::did_doc::test::ISSUER_ID}, utils::rand_string, }; + use once_cell::sync::Lazy; pub const SCHEMA_ID: &str = "did:ethr:testnet:0xf0e2db6c8dc6c681bb5d6ad121a107f300e9b2b5/anoncreds/v0/SCHEMA/F1DClaFEzi3t/1.0.0"; pub const SCHEMA_NAME: &str = "F1DClaFEzi3t"; pub const SCHEMA_VERSION: &str = "1.0.0"; pub const SCHEMA_ATTRIBUTE_FIRST_NAME: &str = "First Name"; + pub static SCHEMA_ATTRIBUTES: Lazy> = Lazy::new(|| { + let mut attr_names: HashSet = HashSet::new(); + attr_names.insert(SCHEMA_ATTRIBUTE_FIRST_NAME.to_string()); + attr_names + }); pub fn schema_id(issuer_id: &DID, name: &str) -> SchemaId { SchemaId::build(issuer_id, name, SCHEMA_VERSION) @@ -117,14 +123,12 @@ pub mod test { pub fn schema(issuer_id: &DID, name: Option<&str>) -> (SchemaId, Schema) { let name = name.map(String::from).unwrap_or_else(rand_string); let id = schema_id(issuer_id, name.as_str()); - let mut attr_names: HashSet = HashSet::new(); - attr_names.insert(SCHEMA_ATTRIBUTE_FIRST_NAME.to_string()); let schema = Schema { issuer_id: issuer_id.clone(), name, version: SCHEMA_VERSION.to_string(), - attr_names, + attr_names: SCHEMA_ATTRIBUTES.clone(), }; (id, schema) } diff --git a/vdr/src/contracts/network/mod.rs b/vdr/src/contracts/network/mod.rs index b152becc..b02c7e78 100644 --- a/vdr/src/contracts/network/mod.rs +++ b/vdr/src/contracts/network/mod.rs @@ -1,2 +1,5 @@ pub mod validator_control; pub mod validator_info; + +pub use validator_control::*; +pub use validator_info::*; diff --git a/vdr/src/types/contract.rs b/vdr/src/types/contract.rs index 385ed8cc..7367c02f 100644 --- a/vdr/src/types/contract.rs +++ b/vdr/src/types/contract.rs @@ -38,7 +38,7 @@ impl ContractSpec { let contract_spec = std::fs::read_to_string(spec_path).map_err(|err| { let vdr_error = VdrError::ContractInvalidSpec(format!( "Unable to read contract spec file. Err: {:?}", - err + err.to_string() )); warn!( diff --git a/vdr/src/types/mod.rs b/vdr/src/types/mod.rs index d27f412c..5eb54453 100644 --- a/vdr/src/types/mod.rs +++ b/vdr/src/types/mod.rs @@ -3,7 +3,7 @@ mod contract; mod event_query; mod signature; mod status; -mod transaction; +pub(crate) mod transaction; pub use address::Address; pub use contract::{ContractConfig, ContractSpec}; diff --git a/vdr/src/types/transaction.rs b/vdr/src/types/transaction.rs index 977a2c30..1a47882a 100644 --- a/vdr/src/types/transaction.rs +++ b/vdr/src/types/transaction.rs @@ -14,8 +14,7 @@ use crate::{ client::{GAS_LIMIT, GAS_PRICE}, error::{VdrError, VdrResult}, types::{ - contract::MethodUintBytesParam, signature::SignatureData, Address, ContractOutput, - ContractParam, + signature::SignatureData, Address, ContractOutput, ContractParam, MethodUintBytesParam, }, LedgerClient, }; @@ -256,6 +255,17 @@ impl TransactionBuilder { #[logfn(Trace)] #[logfn_inputs(Trace)] pub async fn build(self, client: &LedgerClient) -> VdrResult { + if self.contract.is_empty() { + return Err(VdrError::ClientInvalidState( + "Contract name is not set".to_string(), + )); + } + if self.method.is_empty() { + return Err(VdrError::ClientInvalidState( + "Contract method is not set".to_string(), + )); + } + let contract = client.contract(&self.contract)?; let data = contract @@ -323,13 +333,21 @@ impl TransactionParser { bytes: &[u8], ) -> VdrResult { if bytes.is_empty() { - let vdr_error = - VdrError::ContractInvalidResponseData("Empty response bytes".to_string()); - - warn!("Error: {:?} during transaction output parse", vdr_error); - - return Err(vdr_error); + return Err(VdrError::ContractInvalidResponseData( + "Empty response bytes".to_string(), + )); + } + if self.contract.is_empty() { + return Err(VdrError::ClientInvalidState( + "Contract name is not set".to_string(), + )); + } + if self.method.is_empty() { + return Err(VdrError::ClientInvalidState( + "Contract method is not set".to_string(), + )); } + let contract = client.contract(&self.contract)?; let output = contract .function(&self.method)? @@ -501,7 +519,10 @@ pub struct BlockDetails { pub mod test { use super::*; use crate::{ - client::client::test::{mock_client, write_transaction, INVALID_ADDRESS}, + client::client::test::{ + mock_client, CHAIN_ID, DEFAULT_NONCE, INVALID_ADDRESS, TRUSTEE_ACC, + VALIDATOR_CONTROL_ADDRESS, + }, contracts::network::test::{ ADD_VALIDATOR_METHOD, VALIDATOR_ADDRESS, VALIDATOR_CONTROL_NAME, VALIDATOR_LIST_BYTES, }, @@ -513,6 +534,32 @@ pub mod test { const CONTRACT_METHOD_EXAMPLE: Option<&str> = Some(ADD_VALIDATOR_METHOD); const CONTRACT_NAME_EXAMPLE: Option<&str> = Some(VALIDATOR_CONTROL_NAME); + pub fn write_transaction() -> Transaction { + Transaction { + type_: TransactionType::Write, + from: Some(TRUSTEE_ACC.clone()), + to: VALIDATOR_CONTROL_ADDRESS.clone(), + nonce: Some(DEFAULT_NONCE.clone()), + chain_id: CHAIN_ID, + data: vec![], + signature: RwLock::new(None), + hash: None, + } + } + + pub fn read_transaction() -> Transaction { + Transaction { + type_: TransactionType::Read, + from: None, + to: VALIDATOR_CONTROL_ADDRESS.clone(), + nonce: None, + chain_id: CHAIN_ID, + data: vec![], + signature: RwLock::new(None), + hash: None, + } + } + #[cfg(test)] pub mod txn_test { use super::*; @@ -586,39 +633,39 @@ pub mod test { #[rstest] #[case::contract_name_does_not_set( - None, - CONTRACT_METHOD_EXAMPLE, - Some(TransactionType::Read), - None, - VdrError::ContractInvalidName("".to_string()) + None, + CONTRACT_METHOD_EXAMPLE, + Some(TransactionType::Read), + None, + VdrError::ClientInvalidState("Contract name is not set".to_string()) )] #[case::contract_method_does_not_set( - CONTRACT_NAME_EXAMPLE, - None, - Some(TransactionType::Read), - None, - VdrError::ContractInvalidName("".to_string()) + CONTRACT_NAME_EXAMPLE, + None, + Some(TransactionType::Read), + None, + VdrError::ClientInvalidState("Contract method is not set".to_string()) )] #[case::contract_method_does_not_exist( - CONTRACT_NAME_EXAMPLE, - INVALID_METHOD, - Some(TransactionType::Read), - None, - VdrError::ContractInvalidName("".to_string()) + CONTRACT_NAME_EXAMPLE, + INVALID_METHOD, + Some(TransactionType::Read), + None, + VdrError::ContractInvalidName("123".to_string()) )] #[case::write_sender_does_not_set( - CONTRACT_NAME_EXAMPLE, - CONTRACT_METHOD_EXAMPLE, - Some(TransactionType::Write), - None, - VdrError::ClientInvalidTransaction("".to_string()) + CONTRACT_NAME_EXAMPLE, + CONTRACT_METHOD_EXAMPLE, + Some(TransactionType::Write), + None, + VdrError::ClientInvalidTransaction("Transaction `sender` is not set".to_string()) )] #[case::invalid_from_address( - CONTRACT_NAME_EXAMPLE, - CONTRACT_METHOD_EXAMPLE, - Some(TransactionType::Write), - INVALID_ACC_ADDRESS, - VdrError::ClientInvalidTransaction("".to_string()) + CONTRACT_NAME_EXAMPLE, + CONTRACT_METHOD_EXAMPLE, + Some(TransactionType::Write), + INVALID_ACC_ADDRESS, + VdrError::ClientInvalidTransaction("".to_string()) )] async fn transaction_builder_tests( #[case] contract: Option<&str>, @@ -652,8 +699,8 @@ pub mod test { let result = builder.build(&client).await; match result { - Ok(ref txn) => assert_eq!(txn.type_, txn_type.unwrap()), - Err(ref err) => assert!(matches!(err.clone(), expected_error)), + Ok(txn) => assert_eq!(txn.type_, txn_type.unwrap()), + Err(err) => assert_eq!(err, expected_error), } } } @@ -669,34 +716,34 @@ pub mod test { #[rstest] #[case::empty_response_bytes( - CONTRACT_NAME_EXAMPLE, - CONTRACT_METHOD_EXAMPLE, - EMPTY_RESPONSE, - VdrError::ContractInvalidResponseData("".to_string()) + CONTRACT_NAME_EXAMPLE, + CONTRACT_METHOD_EXAMPLE, + EMPTY_RESPONSE, + VdrError::ContractInvalidResponseData("Empty response bytes".to_string()) )] #[case::contract_not_set( - None, - CONTRACT_METHOD_EXAMPLE, - VALIDATOR_LIST_BYTES, - VdrError::ContractInvalidName("".to_string()) + None, + CONTRACT_METHOD_EXAMPLE, + VALIDATOR_LIST_BYTES, + VdrError::ClientInvalidState("Contract name is not set".to_string()) )] #[case::contract_does_not_exist( - INVALID_CONTRACT, - CONTRACT_METHOD_EXAMPLE, - VALIDATOR_LIST_BYTES, - VdrError::ContractInvalidName("".to_string()) + INVALID_CONTRACT, + CONTRACT_METHOD_EXAMPLE, + VALIDATOR_LIST_BYTES, + VdrError::ContractInvalidName("123".to_string()) )] #[case::contract_method_not_set( - CONTRACT_NAME_EXAMPLE, - None, - VALIDATOR_LIST_BYTES, - VdrError::ContractInvalidName("".to_string()) + CONTRACT_NAME_EXAMPLE, + None, + VALIDATOR_LIST_BYTES, + VdrError::ClientInvalidState("Contract method is not set".to_string()) )] #[case::contract_method_does_not_exist( - CONTRACT_NAME_EXAMPLE, - INVALID_METHOD, - VALIDATOR_LIST_BYTES, - VdrError::ContractInvalidName("".to_string()) + CONTRACT_NAME_EXAMPLE, + INVALID_METHOD, + VALIDATOR_LIST_BYTES, + VdrError::ContractInvalidName("123".to_string()) )] async fn transaction_parser_tests( #[case] contract: Option<&str>, @@ -717,7 +764,7 @@ pub mod test { let result = parser.parse::(&client, response.as_slice()); - assert!(matches!(result.unwrap_err(), expected_error)); + assert_eq!(result.unwrap_err(), expected_error); } } }