Skip to content

Commit

Permalink
moved quorum_check to quorum
Browse files Browse the repository at this point in the history
Signed-off-by: DenisRybas <[email protected]>
  • Loading branch information
DenisRybas committed Dec 5, 2023
1 parent d753ac7 commit b93fbf2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 64 deletions.
49 changes: 6 additions & 43 deletions indy-besu/vdr/src/client/implementation/web3/client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::{
client::{constants::GAS, implementation::web3::signer::Web3Signer, Client, Transaction},
client::{
constants::GAS,
implementation::web3::{quorum, signer::Web3Signer},
Client, Transaction,
},
error::{VdrError, VdrResult},
signer::Signer,
};
Expand Down Expand Up @@ -159,48 +163,7 @@ impl Client for Web3Client {
)
.await?;

let transaction_id = TransactionId::Hash(receipt.transaction_hash);

let quorum_reached = timeout(Duration::from_secs(200), async {
let quorum_reached = false;
let required_approvals: u64 = clients.len().into() * 1 / 3 + 1;
let approvals_counter = 0;

let requests = clients.clone().iter().map(|client| {
tokio::spawn(async move {
let transaction = client.eth().transaction(transaction_id).await;

if transaction.is_ok() {
approvals_counter += 1;

if approvals_counter >= required_approvals {
quorum_reached = true;
}
}
})
});

loop {
let finished_tasks = 0;

for request in requests {
if request.is_finished() {
finished_tasks += 1
}

if finished_tasks == requests.len() || quorum_reached {
break;
}
}

if finished_tasks == requests.len() || quorum_reached {
break;
}
}

Ok(quorum_reached)
})
.await?;
quorum::quorum_check(clients, receipt.transaction_hash);

trace!("Submitted transaction: {:?}", transaction);

Expand Down
47 changes: 26 additions & 21 deletions indy-besu/vdr/src/client/implementation/web3/quorum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,41 @@ use web3::{

use crate::{VdrError, VdrResult};

pub async fn quorum_check(transaction_hash: H256) -> VdrResult<bool> {
let clients = vec![Web3::new(
Http::new("123").map_err(|_| VdrError::ClientNodeUnreachable)?,
)];
pub async fn quorum_check(clients: Vec<Web3<Http>>, transaction_hash: H256) -> VdrResult<bool> {
let quorum_result = timeout(Duration::from_secs(200), async {
let quorum_reached = false;
let required_approvals: u64 = clients.len().into() * 1 / 3 + 1;
let approvals_counter = 0;
let transaction_id = TransactionId::Hash(transaction_hash);

let transaction_id = TransactionId::Hash(transaction_hash);
let requests = clients.clone().iter().map(|client| {
tokio::spawn(async move {
let transaction = client.eth().transaction(transaction_id).await;

let quorum_reached = timeout(Duration::from_secs(200), async {
let quorum_reached = false;
let required_approvals: u64 = clients.len().into() * 2 / 3 + 1;
if transaction.is_ok() {
approvals_counter += 1;

let requests = clients
.clone()
.iter()
.map(|client| client.eth().transaction(transaction_id));
if approvals_counter >= required_approvals {
quorum_reached = true;
}
}
})
});

loop {
let approvals_counter = 0;
let finished_tasks = 0;

let responses = try_join_all(requests).await?;
for request in requests {
if request.is_finished() {
finished_tasks += 1
}

for response in &responses {
match response {
Some(_) => approvals_counter += 1,
None => (),
if finished_tasks == requests.len() || quorum_reached {
break;
}
}

if approvals_counter >= required_approvals {
quorum_reached = true;
if finished_tasks == requests.len() || quorum_reached {
break;
}
}
Expand All @@ -48,5 +53,5 @@ pub async fn quorum_check(transaction_hash: H256) -> VdrResult<bool> {
})
.await?;

Ok(quorum_reached)
Ok(quorum_result)
}

0 comments on commit b93fbf2

Please sign in to comment.