Skip to content

Commit

Permalink
Cleaned up VDR data types
Browse files Browse the repository at this point in the history
Signed-off-by: artem.ivanov <[email protected]>
  • Loading branch information
Artemkaaas committed Jan 12, 2024
1 parent 5e10d14 commit 6de2534
Show file tree
Hide file tree
Showing 24 changed files with 271 additions and 388 deletions.
30 changes: 14 additions & 16 deletions indy-besu/vdr/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl LedgerClient {
let result = match transaction.type_ {
TransactionType::Read => {
self.client
.call_transaction(&transaction.to, &transaction.data)
.call_transaction(transaction.to.as_ref(), &transaction.data)
.await
}
TransactionType::Write => self.client.submit_transaction(&transaction.encode()?).await,
Expand Down Expand Up @@ -121,9 +121,7 @@ impl LedgerClient {
.get(name)
.map(|contract| contract.as_ref())
.ok_or_else(|| {
let vdr_error = VdrError::ContractInvalidName {
msg: name.to_string(),
};
let vdr_error = VdrError::ContractInvalidName(name.to_string());

warn!("Error during getting contract: {:?}", vdr_error);

Expand All @@ -148,14 +146,14 @@ impl LedgerClient {
(Some(spec_path), None) => ContractSpec::from_file(spec_path)?,
(None, Some(spec)) => spec.clone(),
(Some(_), Some(_)) => {
return Err(VdrError::ContractInvalidSpec {
msg: "Either `spec_path` or `spec` must be provided".to_string(),
});
return Err(VdrError::ContractInvalidSpec(
"Either `spec_path` or `spec` must be provided".to_string(),
));
}
(None, None) => {
return Err(VdrError::ContractInvalidSpec {
msg: "Either `spec_path` or `spec` must be provided".to_string(),
});
return Err(VdrError::ContractInvalidSpec(
"Either `spec_path` or `spec` must be provided".to_string(),
));
}
};

Expand All @@ -172,7 +170,7 @@ pub mod test {
use super::*;
use async_trait::async_trait;
use once_cell::sync::Lazy;
use std::{env, fs, ops::Deref};
use std::{env, fs};

pub const CHAIN_ID: u64 = 1337;
pub const CONTRACTS_SPEC_BASE_PATH: &str = "../smart_contracts/artifacts/contracts/";
Expand Down Expand Up @@ -223,27 +221,27 @@ pub mod test {
fn contracts() -> Vec<ContractConfig> {
vec![
ContractConfig {
address: DID_REGISTRY_ADDRESS.deref().to_string(),
address: DID_REGISTRY_ADDRESS.to_string(),
spec_path: Some(build_contract_path(DID_REGISTRY_SPEC_PATH)),
spec: None,
},
ContractConfig {
address: SCHEMA_REGISTRY_ADDRESS.deref().to_string(),
address: SCHEMA_REGISTRY_ADDRESS.to_string(),
spec_path: Some(build_contract_path(SCHEMA_REGISTRY_SPEC_PATH)),
spec: None,
},
ContractConfig {
address: CRED_DEF_REGISTRY_ADDRESS.deref().to_string(),
address: CRED_DEF_REGISTRY_ADDRESS.to_string(),
spec_path: Some(build_contract_path(CRED_DEF_REGISTRY_SPEC_PATH)),
spec: None,
},
ContractConfig {
address: VALIDATOR_CONTROL_ADDRESS.deref().to_string(),
address: VALIDATOR_CONTROL_ADDRESS.to_string(),
spec_path: Some(build_contract_path(VALIDATOR_CONTROL_PATH)),
spec: None,
},
ContractConfig {
address: ROLE_CONTROL_ADDRESS.deref().to_string(),
address: ROLE_CONTROL_ADDRESS.to_string(),
spec_path: Some(build_contract_path(ROLE_CONTROL_PATH)),
spec: None,
},
Expand Down
26 changes: 14 additions & 12 deletions indy-besu/vdr/src/client/implementation/web3/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ impl Web3Client {
#[cfg_attr(feature = "wasm", async_trait(? Send))]
impl Client for Web3Client {
async fn get_transaction_count(&self, address: &crate::Address) -> VdrResult<[u64; 4]> {
let account_address =
EthAddress::from_str(address).map_err(|_| VdrError::ClientInvalidTransaction {
msg: format!("Invalid transaction sender address {:?}", address),
})?;
let account_address = EthAddress::from_str(address.as_ref()).map_err(|_| {
VdrError::ClientInvalidTransaction(format!(
"Invalid transaction sender address {:?}",
address
))
})?;

let nonce = self
.client
Expand Down Expand Up @@ -100,9 +102,10 @@ impl Client for Web3Client {
);

let address = EthAddress::from_str(to).map_err(|_| {
let vdr_error = VdrError::ClientInvalidTransaction {
msg: format!("Invalid transaction target address {:?}", to),
};
let vdr_error = VdrError::ClientInvalidTransaction(format!(
"Invalid transaction target address {:?}",
to
));

warn!(
"Error: {} during calling transaction: {:?}",
Expand All @@ -129,9 +132,8 @@ impl Client for Web3Client {
.transaction_receipt(H256::from_slice(hash))
.await?
.ok_or_else(|| {
let vdr_error = VdrError::ClientInvalidResponse {
msg: "Missing transaction receipt".to_string(),
};
let vdr_error =
VdrError::ClientInvalidResponse("Missing transaction receipt".to_string());

warn!("Error: {} getting receipt", vdr_error,);

Expand Down Expand Up @@ -162,8 +164,8 @@ impl Client for Web3Client {
.eth()
.transaction(transaction_id)
.await
.map_err(|_| VdrError::GetTransactionError {
msg: "Could not get transaction by hash".to_string(),
.map_err(|_| {
VdrError::GetTransactionError("Could not get transaction by hash".to_string())
})?;

let transaction = transaction.map(|transaction| Transaction {
Expand Down
20 changes: 8 additions & 12 deletions indy-besu/vdr/src/client/implementation/web3/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,20 @@ impl Web3Contract {
trace!("Started creating new Web3Contract. Address: {:?}", address);

let abi = serde_json::to_vec(&contract_spec.abi).map_err(|err| {
let vdr_error = VdrError::CommonInvalidData {
msg: format!(
"Unable to parse contract ABI from specification. Err: {:?}",
err.to_string()
),
};
let vdr_error = VdrError::CommonInvalidData(format!(
"Unable to parse contract ABI from specification. Err: {:?}",
err.to_string()
));

warn!("Error: {:?} during creating new Web3Contract", vdr_error);

vdr_error
})?;
let parsed_address = EthAddress::from_str(address).map_err(|err| {
let vdr_error = VdrError::CommonInvalidData {
msg: format!(
"Unable to parse contract address. Err: {:?}",
err.to_string()
),
};
let vdr_error = VdrError::CommonInvalidData(format!(
"Unable to parse contract address. Err: {:?}",
err.to_string()
));

warn!("Error: {:?} during creating new Web3Contract", vdr_error);

Expand Down
33 changes: 15 additions & 18 deletions indy-besu/vdr/src/client/quorum.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ops::Deref, sync::Arc, time::Duration};
use std::{sync::Arc, time::Duration};

use futures::{
channel::{
Expand Down Expand Up @@ -169,7 +169,7 @@ impl QuorumHandler {
sender,
client,
type_,
to.deref().to_string(),
to.to_string(),
transaction_data,
request_retries,
request_timeout,
Expand All @@ -183,7 +183,7 @@ impl QuorumHandler {
sender.clone(),
client.clone(),
type_,
to.deref().to_string(),
to.to_string(),
transaction_data,
self.request_retries,
self.request_timeout,
Expand All @@ -200,9 +200,10 @@ impl QuorumHandler {
Ok(quorum_reached)
} else {
trace!("Quorum failed for transaction: {:?}", transaction);
Err(VdrError::QuorumNotReached {
msg: format!("Quorum not reached for transaction: {:?}", transaction),
})
Err(VdrError::QuorumNotReached(format!(
"Quorum not reached for transaction: {:?}",
transaction
)))
}
}
}
Expand Down Expand Up @@ -392,7 +393,7 @@ pub mod test {
mock_client
.expect_call_transaction()
.with(
eq(transaction.to.deref().to_string()),
eq(transaction.to.to_string()),
eq(transaction.data.to_vec()),
)
.returning(move |_, _| expected_output.clone());
Expand All @@ -409,7 +410,7 @@ pub mod test {
mock_client
.expect_call_transaction()
.with(
eq(transaction.to.deref().to_string()),
eq(transaction.to.to_string()),
eq(transaction.data.to_vec()),
)
.returning(move |_, _| {
Expand All @@ -430,20 +431,16 @@ pub mod test {
mock_client
.expect_call_transaction()
.with(
eq(transaction.to.deref().to_string()),
eq(transaction.to.to_string()),
eq(transaction.data.to_vec()),
)
.times(retries_num as usize - 1)
.returning(move |_, _| {
Err(VdrError::ContractInvalidResponseData {
msg: "".to_string(),
})
});
.returning(move |_, _| Err(VdrError::ContractInvalidResponseData("".to_string())));

mock_client
.expect_call_transaction()
.with(
eq(transaction.to.deref().to_string()),
eq(transaction.to.to_string()),
eq(transaction.data.to_vec()),
)
.returning(move |_, _| expected_output.clone());
Expand All @@ -464,9 +461,9 @@ pub mod test {

#[async_std::test]
async fn test_quorum_check_failed_with_timeout() {
let err = Err(VdrError::ClientTransactionReverted {
msg: "Transaction reverted".to_string(),
});
let err = Err(VdrError::ClientTransactionReverted(
"Transaction reverted".to_string(),
));
let client1 = mock_client(READ_TRANSACTION.clone(), err.clone());
let client2 = mock_client_sleep_before_return(
READ_TRANSACTION.clone(),
Expand Down
6 changes: 3 additions & 3 deletions indy-besu/vdr/src/contracts/auth/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ impl TryFrom<RoleIndex> for Role {
1 => Ok(Role::Trustee),
2 => Ok(Role::Endorser),
3 => Ok(Role::Steward),
_ => Err(VdrError::ContractInvalidResponseData {
msg: "Invalid role provided".to_string(),
}),
_ => Err(VdrError::ContractInvalidResponseData(
"Invalid role provided".to_string(),
)),
};

trace!(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use log::{debug, info};
use std::ops::Deref;

use crate::{
client::LedgerClient,
Expand Down Expand Up @@ -75,7 +74,7 @@ pub async fn build_resolve_credential_definition_transaction(
let transaction = TransactionBuilder::new()
.set_contract(CONTRACT_NAME)
.set_method(METHOD_RESOLVE_CREDENTIAL_DEFINITION)
.add_param(ContractParam::String(String::from(id.deref())))
.add_param(ContractParam::String(id.to_string()))
.set_type(TransactionType::Read)
.build(client)
.await;
Expand Down
5 changes: 2 additions & 3 deletions indy-besu/vdr/src/contracts/cl/schema_registry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use log::{debug, info};
use std::ops::Deref;

use crate::{
client::LedgerClient,
Expand Down Expand Up @@ -73,7 +72,7 @@ pub async fn build_resolve_schema_transaction(
let transaction = TransactionBuilder::new()
.set_contract(CONTRACT_NAME)
.set_method(METHOD_RESOLVE_SCHEMA)
.add_param(ContractParam::String(String::from(id.deref())))
.add_param(ContractParam::String(id.to_string()))
.set_type(TransactionType::Read)
.build(client)
.await;
Expand Down Expand Up @@ -142,7 +141,7 @@ pub mod test {
.unwrap();

let sign_bytes = transaction.get_signing_bytes().unwrap();
let signature = signer.sign(&sign_bytes, TRUSTEE_ACC.deref()).unwrap();
let signature = signer.sign(&sign_bytes, TRUSTEE_ACC.as_ref()).unwrap();
transaction.set_signature(signature);

client.submit_transaction(&transaction).await.unwrap();
Expand Down
18 changes: 8 additions & 10 deletions indy-besu/vdr/src/contracts/cl/types/credential_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{
types::{ContractOutput, ContractParam},
DID,
};
use std::ops::Deref;

use crate::contracts::cl::types::{
credential_definition_id::CredentialDefinitionId, schema_id::SchemaId,
Expand Down Expand Up @@ -44,9 +43,9 @@ impl From<CredentialDefinition> for ContractParam {
);

let cred_def_contract_param = ContractParam::Tuple(vec![
ContractParam::String(value.id.deref().to_string()),
ContractParam::String(value.issuer_id.deref().to_string()),
ContractParam::String(value.schema_id.deref().to_string()),
ContractParam::String(value.id.to_string()),
ContractParam::String(value.issuer_id.to_string()),
ContractParam::String(value.schema_id.to_string()),
ContractParam::String(value.cred_def_type.to_string()),
ContractParam::String(value.tag.to_string()),
ContractParam::String(value.value.to_string()),
Expand All @@ -73,9 +72,9 @@ impl TryFrom<ContractOutput> for CredentialDefinition {

let cred_def_value = serde_json::from_str::<serde_json::Value>(&value.get_string(5)?)
.map_err(|_err| {
let vdr_error = VdrError::ContractInvalidResponseData {
msg: "Unable get to credential definition value".to_string(),
};
let vdr_error = VdrError::ContractInvalidResponseData(
"Unable get to credential definition value".to_string(),
);

warn!(
"Error: {} during CredentialDefinition convert from ContractOutput: {:?}",
Expand Down Expand Up @@ -172,7 +171,7 @@ pub mod test {
schema_id: &SchemaId,
tag: &str,
) -> CredentialDefinitionId {
CredentialDefinitionId::build(issuer_id, schema_id.deref(), tag)
CredentialDefinitionId::build(issuer_id, schema_id.as_ref(), tag)
}

fn credential_definition_value() -> serde_json::Value {
Expand All @@ -194,7 +193,7 @@ pub mod test {
CredentialDefinition {
id: credential_definition_id(issuer_id, schema_id, tag.as_str()),
issuer_id: issuer_id.clone(),
schema_id: SchemaId::from(schema_id.deref()),
schema_id: SchemaId::from(schema_id.as_ref()),
cred_def_type: CREDENTIAL_DEFINITION_TYPE.to_string(),
tag: tag.to_string(),
value: credential_definition_value(),
Expand All @@ -209,7 +208,6 @@ pub mod test {
&SchemaId::from(SCHEMA_ID),
CREDENTIAL_DEFINITION_TAG,
)
.deref()
.to_string(),
),
ContractParam::String(ISSUER_ID.to_string()),
Expand Down
Loading

0 comments on commit 6de2534

Please sign in to comment.