Add MEMBERLIST and update it with pairing processes
This commit is contained in:
parent
7c634ae0ed
commit
5391dca929
@ -1,19 +1,24 @@
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
|
sync::{Mutex, MutexGuard, OnceLock},
|
||||||
|
};
|
||||||
|
|
||||||
use anyhow::{Error, Result};
|
use anyhow::{Error, Result};
|
||||||
|
|
||||||
use bitcoincore_rpc::bitcoin::hex::DisplayHex;
|
use bitcoincore_rpc::bitcoin::hex::DisplayHex;
|
||||||
use hex::FromHex;
|
use hex::FromHex;
|
||||||
use sdk_common::pcd::Pcd;
|
use sdk_common::pcd::{Member, Pcd, RoleDefinition};
|
||||||
use sdk_common::silentpayments::create_transaction;
|
use sdk_common::silentpayments::create_transaction;
|
||||||
use sdk_common::sp_client::spclient::Recipient;
|
use sdk_common::sp_client::spclient::Recipient;
|
||||||
use sdk_common::network::CommitMessage;
|
use sdk_common::network::CommitMessage;
|
||||||
use sdk_common::sp_client::bitcoin::consensus::deserialize;
|
use sdk_common::sp_client::bitcoin::consensus::deserialize;
|
||||||
use sdk_common::sp_client::bitcoin::{Amount, Transaction, OutPoint};
|
use sdk_common::sp_client::bitcoin::{Amount, Transaction, OutPoint};
|
||||||
use sdk_common::process::{Process, ProcessState, CACHEDPROCESSES};
|
use sdk_common::process::{lock_processes, Process, ProcessState, CACHEDPROCESSES};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
use crate::{lock_freezed_utxos, MutexExt, DAEMON, LISTS, WALLET};
|
use crate::{lock_freezed_utxos, MutexExt, DAEMON, WALLET};
|
||||||
|
|
||||||
pub(crate) fn handle_commit_request(commit_msg: CommitMessage) -> Result<OutPoint> {
|
pub(crate) fn handle_commit_request(commit_msg: CommitMessage) -> Result<OutPoint> {
|
||||||
// Attempt to process the initial transaction or reference
|
// Attempt to process the initial transaction or reference
|
||||||
@ -58,6 +63,8 @@ fn handle_initial_transaction(tx: Transaction, commit_msg: &CommitMessage) -> Re
|
|||||||
|
|
||||||
// Process roles and commitments
|
// Process roles and commitments
|
||||||
let roles_only_map = json!({ "roles": serde_json::to_value(&commit_msg.roles)? });
|
let roles_only_map = json!({ "roles": serde_json::to_value(&commit_msg.roles)? });
|
||||||
|
|
||||||
|
let parsed_roles = roles_only_map.extract_roles()?;
|
||||||
// let roles_commitment = roles_only_map.hash_fields(root_commitment)?;
|
// let roles_commitment = roles_only_map.hash_fields(root_commitment)?;
|
||||||
|
|
||||||
// TODO make that kind of check reliable, needs more work on json serialization
|
// TODO make that kind of check reliable, needs more work on json serialization
|
||||||
@ -69,6 +76,8 @@ fn handle_initial_transaction(tx: Transaction, commit_msg: &CommitMessage) -> Re
|
|||||||
|
|
||||||
let merkle_root_bin = pcd_commitment.create_merkle_tree()?.root().unwrap();
|
let merkle_root_bin = pcd_commitment.create_merkle_tree()?.root().unwrap();
|
||||||
|
|
||||||
|
handle_member_list(pcd_commitment, &parsed_roles, root_commitment)?;
|
||||||
|
|
||||||
let mut new_process = Process::new(root_commitment);
|
let mut new_process = Process::new(root_commitment);
|
||||||
let init_state = ProcessState {
|
let init_state = ProcessState {
|
||||||
commited_in: root_commitment,
|
commited_in: root_commitment,
|
||||||
@ -81,10 +90,7 @@ fn handle_initial_transaction(tx: Transaction, commit_msg: &CommitMessage) -> Re
|
|||||||
new_process.insert_concurrent_state(init_state)?;
|
new_process.insert_concurrent_state(init_state)?;
|
||||||
|
|
||||||
// Cache the process
|
// Cache the process
|
||||||
let mut commitments = CACHEDPROCESSES
|
let mut commitments = lock_processes()?;
|
||||||
.get()
|
|
||||||
.ok_or(Error::msg("CACHEDPROCESSES not initialized"))?
|
|
||||||
.lock_anyhow()?;
|
|
||||||
commitments.insert(
|
commitments.insert(
|
||||||
root_commitment,
|
root_commitment,
|
||||||
new_process,
|
new_process,
|
||||||
@ -96,6 +102,48 @@ fn handle_initial_transaction(tx: Transaction, commit_msg: &CommitMessage) -> Re
|
|||||||
Ok(root_commitment)
|
Ok(root_commitment)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub static MEMBERLIST: OnceLock<Mutex<HashMap<Member, OutPoint>>> = OnceLock::new();
|
||||||
|
|
||||||
|
pub fn lock_members() -> Result<MutexGuard<'static, HashMap<Member, OutPoint>>, anyhow::Error> {
|
||||||
|
MEMBERLIST
|
||||||
|
.get_or_init(|| Mutex::new(HashMap::new()))
|
||||||
|
.lock_anyhow()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_member_list(pcd_commitment: &Value, roles: &HashMap<String, RoleDefinition>, root_commitment: OutPoint) -> Result<()> {
|
||||||
|
//Check if the keys exists in the pcd
|
||||||
|
if let Value::Object(ref pcd_map) = pcd_commitment {
|
||||||
|
if !pcd_map.contains_key("key_parity")
|
||||||
|
|| !pcd_map.contains_key("session_privkey")
|
||||||
|
|| !pcd_map.contains_key("session_pubkey")
|
||||||
|
{
|
||||||
|
return Err(Error::msg("Pcd must contain all keys for a pairing process"));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(Error::msg("Pcd is missing"));
|
||||||
|
}
|
||||||
|
|
||||||
|
//Check if there is one role with one member
|
||||||
|
if roles.len() != 1 {
|
||||||
|
return Err(Error::msg("Process is not a pairing process"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(owner_role) = roles.get("owner") {
|
||||||
|
if owner_role.members.len() == 1 {
|
||||||
|
let member = owner_role.members.get(0).unwrap();
|
||||||
|
let mut memberlist = lock_members()?;
|
||||||
|
memberlist.insert(
|
||||||
|
member.clone(),
|
||||||
|
root_commitment,
|
||||||
|
);
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(Error::msg("Process is not a pairing process"))
|
||||||
|
}
|
||||||
|
|
||||||
// Handle the case where `init_tx` is a reference to an existing outpoint
|
// Handle the case where `init_tx` is a reference to an existing outpoint
|
||||||
fn handle_existing_commitment(outpoint: OutPoint, commit_msg: CommitMessage) -> Result<OutPoint> {
|
fn handle_existing_commitment(outpoint: OutPoint, commit_msg: CommitMessage) -> Result<OutPoint> {
|
||||||
let mut commitments = CACHEDPROCESSES
|
let mut commitments = CACHEDPROCESSES
|
||||||
|
Loading…
x
Reference in New Issue
Block a user