Skip to content

Commit

Permalink
Reworked quorum handler to compile and work for wasm/uniffi
Browse files Browse the repository at this point in the history
Signed-off-by: artem.ivanov <[email protected]>
  • Loading branch information
Artemkaaas committed Dec 27, 2023
1 parent 43cd875 commit f81a6fe
Show file tree
Hide file tree
Showing 30 changed files with 1,016 additions and 610 deletions.
102 changes: 93 additions & 9 deletions indy-besu/docs/design/vdr.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct LedgerClient {
chain_id: u64,
client: Box<dyn Client>,
contracts: HashMap<String, Box<dyn Contract>>,
quorum_handler: Option<QuorumHandler>,
}

struct ContractConfig {
Expand All @@ -42,15 +43,16 @@ enum Status {
}

impl LedgerClient {
/// Create Indy2.0 client interacting with ledger
/// Create indy2 client interacting with ledger
///
/// #Params
/// param: chain_id: u64 - chain id of network (chain ID is part of the transaction signing process to protect against transaction replay attack)
/// param: node_address: string - RPC node endpoint
/// param: contract_specs: Vec<ContractSpec> - specifications for contracts deployed on the network
/// # Params
/// - `chain_id` - chain id of network (chain ID is part of the transaction signing process to protect against transaction replay attack)
/// - `rpc_node` - string - RPC node endpoint
/// - `contract_configs` - [ContractSpec] specifications for contracts deployed on the network
/// - `quorum_config` - Option<[QuorumConfig]> quorum configuration. Can be None if quorum is not needed
///
/// #Returns
/// client - client to use for building and sending transactions
/// # Returns
/// client to use for building and sending transactions
fn new(
chain_id: u64,
node_address: String,
Expand All @@ -71,7 +73,7 @@ impl LedgerClient {
/// Depending on the transaction type Write/Read ethereum methods will be used
///
/// #Params
/// transaction - transaction to submit
/// `transaction` - transaction to submit
///
/// #Returns
/// transaction execution result:
Expand All @@ -83,7 +85,7 @@ impl LedgerClient {
/// Get receipt for the given block hash
///
/// # Params
/// transaction - transaction to submit
/// `transaction` - transaction to submit
///
/// # Returns
/// receipt for the given block
Expand All @@ -95,6 +97,88 @@ impl LedgerClient {
struct SubmitTransactionOptions {}

type Receipt = Vec<u8>;

trait Client: Sync + Send {
/// Retrieve count of transaction for the given account
///
/// # Params
/// - `address` address of an account to get number of written transactions
///
/// # Returns
/// number of transactions
async fn get_transaction_count(&self, address: &Address) -> VdrResult<[u64; 4]>;

/// Submit transaction to the ledger
///
/// # Params
/// - `transaction` transaction to submit
/// - `transaction` prepared transaction to submit
///
/// # Returns
/// hash of a block in which transaction included
async fn submit_transaction(&self, transaction: &[u8]) -> VdrResult<Vec<u8>>;

/// Submit read transaction to the ledger
///
/// # Params
/// - `transaction` prepared transaction to submit
///
/// # Returns
/// result data of transaction execution
async fn call_transaction(&self, to: &str, transaction: &[u8]) -> VdrResult<Vec<u8>>;

/// Get the receipt for the given block hash
///
/// # Params
/// - `hash` hash of a block to get the receipt
///
/// # Returns
/// receipt as JSON string for the requested block
async fn get_receipt(&self, hash: &[u8]) -> VdrResult<String>;

/// Check client connection (passed node is alive and return valid ledger data)
///
/// # Returns
/// ledger status
async fn ping(&self) -> VdrResult<PingStatus>;

/// Get the transaction for the given transaction hash
///
/// # Params
/// - `hash` hash of a transaction to get
///
/// # Returns
/// transaction for the requested hash
async fn get_transaction(&self, hash: &[u8]) -> VdrResult<Option<Transaction>>;
}

trait Contract: Sync + Send {
/// Get the address of deployed contract
///
/// # Returns
/// address of the deployed contract. Should be used to execute contract methods
fn address(&self) -> &Address;

/// Encode data required for the execution of a contract method
///
/// # Params
/// - `method` method to execute
/// - `params` data to pass/encode for contract execution
///
/// # Returns
/// encoded data to set into transaction
fn encode_input(&self, method: &str, params: &[ContractParam]) -> VdrResult<Vec<u8>>;

/// Decode the value (bytes) returned as the result of the execution of a contract method
///
/// # Params
/// - `method` method to execute
/// - `output` data to decode (returned as result of sending call transaction)
///
/// # Returns
/// contract execution result in decoded form
fn decode_output(&self, method: &str, output: &[u8]) -> VdrResult<ContractOutput>;
}
```

## Transaction methods
Expand Down
9 changes: 4 additions & 5 deletions indy-besu/vdr/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion indy-besu/vdr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ basic_signer = ["secp256k1", "rand"]
migration = []

[dependencies]
tokio = "1.34.0"
log = "0.4"
async-std = { version = "1.12.0", features = ["attributes", "tokio1"] }
async-trait = "0.1.73"
Expand Down
31 changes: 15 additions & 16 deletions indy-besu/vdr/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ impl LedgerClient {
/// Create client interacting with ledger
///
/// # Params
/// - chain_id chain id of network (chain ID is part of the transaction signing process to protect against transaction replay attack)
/// - param rpc_node: string - RPC node endpoint
/// - param contract_configs: [ContractSpec] - specifications for contracts deployed on the network
/// - param: quorum_config: Option<QuorumConfig> - quorum configuration. Can be None if quorum is not needed
/// - `chain_id` - chain id of network (chain ID is part of the transaction signing process to protect against transaction replay attack)
/// - `rpc_node` - string - RPC node endpoint
/// - `contract_configs` - [ContractSpec] specifications for contracts deployed on the network
/// - `quorum_config` - Option<[QuorumConfig]> quorum configuration. Can be None if quorum is not needed
///
/// # Returns
/// client to use for building and sending transactions
Expand Down Expand Up @@ -78,15 +78,19 @@ impl LedgerClient {
/// Depending on the transaction type Write/Read ethereum methods will be used
///
/// #Params
/// transaction - transaction to submit
/// `transaction` - transaction to submit
///
/// #Returns
/// transaction execution result:
/// depending on the type it will be either result bytes or block hash
pub async fn submit_transaction(&self, transaction: &Transaction) -> VdrResult<Vec<u8>> {
let result = match transaction.type_ {
TransactionType::Read => self.client.call_transaction(transaction).await,
TransactionType::Write => self.client.submit_transaction(transaction).await,
TransactionType::Read => {
self.client
.call_transaction(&transaction.to, &transaction.data)
.await
}
TransactionType::Write => self.client.submit_transaction(&transaction.encode()?).await,
}?;

if let Some(quorum_handler) = &self.quorum_handler {
Expand All @@ -99,7 +103,7 @@ impl LedgerClient {
/// Get receipt for the given block hash
///
/// # Params
/// transaction - transaction to submit
/// `transaction` - transaction to submit
///
/// # Returns
/// receipt for the given block
Expand Down Expand Up @@ -167,10 +171,8 @@ impl LedgerClient {
pub mod test {
use super::*;
use async_trait::async_trait;
use ethereum_types::H256;
use once_cell::sync::Lazy;
use std::{env, fs, ops::Deref};
use web3::types::Transaction as Web3Transaction;

pub const CHAIN_ID: u64 = 1337;
pub const CONTRACTS_SPEC_BASE_PATH: &str = "../smart_contracts/artifacts/contracts/";
Expand Down Expand Up @@ -266,11 +268,11 @@ pub mod test {
Ok([0, 0, 0, 0])
}

async fn submit_transaction(&self, _transaction: &Transaction) -> VdrResult<Vec<u8>> {
async fn submit_transaction(&self, _transaction: &[u8]) -> VdrResult<Vec<u8>> {
todo!()
}

async fn call_transaction(&self, _transaction: &Transaction) -> VdrResult<Vec<u8>> {
async fn call_transaction(&self, _to: &str, _transaction: &[u8]) -> VdrResult<Vec<u8>> {
todo!()
}

Expand All @@ -282,10 +284,7 @@ pub mod test {
todo!()
}

async fn get_transaction(
&self,
_transaction_hash: H256,
) -> VdrResult<Option<Web3Transaction>> {
async fn get_transaction(&self, _hash: &[u8]) -> VdrResult<Option<Transaction>> {
todo!()
}
}
Expand Down
Loading

0 comments on commit f81a6fe

Please sign in to comment.