Skip to content

Commit

Permalink
Merge pull request #88 from ariard/2023-fledge-protocol-more
Browse files Browse the repository at this point in the history
Call verifytxoutproof and process
  • Loading branch information
ariard authored Nov 19, 2023
2 parents 998178b + 4baf5c1 commit 31cdac0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 38 deletions.
93 changes: 64 additions & 29 deletions src/bitcoind_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ use crate::rpcclient::{Client, Auth};

use jsonrpc::Response;

use bitcoin::consensus::serialize;
use bitcoin::{MerkleBlock, Txid};
use bitcoin_hashes::hex::FromHex;
use bitcoin_hashes::hex::{ToHex, FromHex};

use staking_credentials::common::utils::Proof;

use tokio::sync::{mpsc, oneshot};
use tokio::sync::Mutex as TokioMutex;
Expand All @@ -24,12 +27,12 @@ use tokio::time::{sleep, Duration};
pub enum BitcoindRequest {
CheckRpcCall,
GenerateTxInclusionProof { txid: String, respond_to: oneshot::Sender<Option<String>> },
CheckMerkleProof { proof: String },
CheckMerkleProof { request_id: u64, proof: Proof },
}

#[derive(Debug)]
pub enum BitcoindResult {
ProofValid { hash: [u8; 32], valid: bool }
ProofValid { request_id: u64, valid: bool }
}

pub struct BitcoindClient {
Expand Down Expand Up @@ -106,34 +109,66 @@ impl BitcoindHandler {
loop {
sleep(Duration::from_millis(1000)).await;

let mut receive_bitcoind_request_lock = self.receive_bitcoind_request.lock();
if let Ok(bitcoind_request) = receive_bitcoind_request_lock.await.try_recv() {
match bitcoind_request {
BitcoindRequest::CheckRpcCall => {
println!("[CIVKITD] - BITCOIND CLIENT: Received rpc call - Test bitcoind");
let mut validation_result = Vec::new();
{
let mut receive_bitcoind_request_lock = self.receive_bitcoind_request.lock();
if let Ok(bitcoind_request) = receive_bitcoind_request_lock.await.try_recv() {
match bitcoind_request {
BitcoindRequest::CheckRpcCall => {
println!("[CIVKITD] - BITCOIND CLIENT: Received rpc call - Test bitcoind");

self.rpc_client.call("getblockchaininfo", &vec![]);
},
BitcoindRequest::GenerateTxInclusionProof { txid, respond_to } => {
println!("[CIVKITD] - BITCOIND CLIENT: Received rpc call - Generate merkle block");

let txid_json_value = serde_json::to_value(txid).unwrap();
let txid_json = serde_json::Value::Array(vec![txid_json_value]);

if let Ok(response) = self.rpc_client.call("gettxoutproof", &[txid_json]) {
if let Some(raw_value) = response.result {
let mut mb_string = raw_value.get().to_string();
let index = mb_string.find('\"').unwrap();
mb_string.remove(index);
let index = mb_string.find('\"').unwrap();
mb_string.remove(index);
//let mb_bytes = Vec::from_hex(&mb_string).unwrap();
//let mb: MerkleBlock = bitcoin::consensus::deserialize(&mb_bytes).unwrap();
respond_to.send(Some(mb_string));
self.rpc_client.call("getblockchaininfo", &vec![]);
},
BitcoindRequest::GenerateTxInclusionProof { txid, respond_to } => {
println!("[CIVKITD] - BITCOIND CLIENT: Received rpc call - Generate merkle block");

let txid_json_value = serde_json::to_value(txid).unwrap();
let txid_json = serde_json::Value::Array(vec![txid_json_value]);

if let Ok(response) = self.rpc_client.call("gettxoutproof", &[txid_json]) {
if let Some(raw_value) = response.result {
let mut mb_string = raw_value.get().to_string();
let index = mb_string.find('\"').unwrap();
mb_string.remove(index);
let index = mb_string.find('\"').unwrap();
mb_string.remove(index);
//let mb_bytes = Vec::from_hex(&mb_string).unwrap();
//let mb: MerkleBlock = bitcoin::consensus::deserialize(&mb_bytes).unwrap();
respond_to.send(Some(mb_string));
}
} else { respond_to.send(None); }
},
BitcoindRequest::CheckMerkleProof { request_id, proof } => {
println!("[CIVKITD] - BITCOIND CLIENT: Received rpc call - Check merkle proof");

match proof {
Proof::MerkleBlock(merkle_block) => {
let hex_string = serialize(&merkle_block).to_hex();
let proof_json_value = serde_json::to_value(hex_string).unwrap();
let proof_json = serde_json::Value::Array(vec![proof_json_value]);

if let Ok(response) = self.rpc_client.call("verifytxoutproof", &[proof_json]) {
if let Some(raw_value) = response.result {
let txid_array = raw_value.get();
if txid_array.len() > 0 {
validation_result.push(BitcoindResult::ProofValid { request_id, valid: true });
}
}
} else { }
},
_ => { validation_result.push(BitcoindResult::ProofValid { request_id, valid: false }); }
}
} else { respond_to.send(None); }
},
_ => {},
},
_ => {},
}
}
}


{
for result in validation_result {
let mut send_bitcoind_result_handler_lock = self.send_bitcoind_result_handler.lock();
send_bitcoind_result_handler_lock.await.send(result);
}
}
}
Expand Down
24 changes: 15 additions & 9 deletions src/credentialgateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,24 @@ impl CredentialGateway {
}
}

let mut validated_requests = Vec::new();
for (request_id, proof) in proofs_to_verify {
let mut send_bitcoind_request_lock = self.send_bitcoind_request_gateway.lock();
println!("[CIVKITD] - CREDENTIAL: credential check merkle proof");
send_bitcoind_request_lock.await.send(BitcoindRequest::CheckMerkleProof { proof: String::new() });
send_bitcoind_request_lock.await.send(BitcoindRequest::CheckMerkleProof { request_id, proof });
}


let mut validated_requests = Vec::new();
{
let mut receive_bitcoind_result_handler_lock = self.receive_bitcoind_result_handler.lock();
if let Ok(bitcoind_result) = receive_bitcoind_result_handler_lock.await.try_recv() {
match bitcoind_result {
BitcoindResult::ProofValid { request_id, valid } => {
validated_requests.push((request_id, valid));
},
_ => { println!("[CIVKITD] - CREDENTIAL: uncorrect Bitcoin backend result"); },
}
}
}

let mut authentication_result_queue = Vec::new();
Expand All @@ -271,13 +284,6 @@ impl CredentialGateway {
//TODO: send back event
}
}

{
//for result in deliverance_result_queue {
// let mut send_credential_lock = self.send_credential_events_gateway.lock();
// //TODO: send bakc event
//}
}
}
}
}

0 comments on commit 31cdac0

Please sign in to comment.