Update to latest common

This commit is contained in:
NicolasCantu 2025-03-12 10:28:21 +01:00 committed by Nicolas Cantu
parent 00821af9a7
commit df9140458b

View File

@ -1,13 +1,12 @@
use std::{
collections::{BTreeMap, HashMap, HashSet},
collections::HashMap,
sync::{Mutex, MutexGuard, OnceLock},
};
use anyhow::{Error, Result};
use bitcoincore_rpc::bitcoin::hex::DisplayHex;
use hex::FromHex;
use sdk_common::pcd::{Member, Pcd, RoleDefinition};
use sdk_common::pcd::{Member, Roles};
use sdk_common::serialization::{OutPointMemberMap, OutPointProcessMap};
use sdk_common::silentpayments::create_transaction;
use sdk_common::sp_client::spclient::Recipient;
@ -105,9 +104,9 @@ fn handle_new_process(commit_msg: &CommitMessage) -> Result<Process> {
let mut new_process = Process::new(commit_msg.process_id);
let init_state = ProcessState {
commited_in: commit_msg.process_id,
roles: commit_msg.roles.clone().into_iter().collect(),
roles: commit_msg.roles.clone(),
pcd_commitment: commit_msg.pcd_commitment.clone(),
state_id: merkle_root_bin.to_lower_hex_string(),
state_id: merkle_root_bin,
public_data: commit_msg.public_data.clone(),
..Default::default()
};
@ -125,7 +124,7 @@ pub fn lock_members() -> Result<MutexGuard<'static, HashMap<OutPoint, Member>>,
.lock_anyhow()
}
fn handle_member_list(roles: &BTreeMap<String, RoleDefinition>, process_id: OutPoint) -> Result<OutPoint> {
fn handle_member_list(roles: &Roles, process_id: OutPoint) -> Result<OutPoint> {
//Check if there is one role with one member
if roles.len() != 1 {
return Err(Error::msg("Process is not a pairing process"));
@ -149,7 +148,7 @@ fn handle_member_list(roles: &BTreeMap<String, RoleDefinition>, process_id: OutP
fn handle_existing_commitment(process_to_udpate: &mut Process, commit_msg: &CommitMessage) -> Result<()> {
let process_id = process_to_udpate.get_process_id()?;
match register_new_state(process_to_udpate, &commit_msg) {
Ok(new_state_id) => log::debug!("Registering new state for process {} with state id {}", process_id, new_state_id),
Ok(new_state_id) => log::debug!("Registering new state for process {} with state id {}", process_id, new_state_id.to_lower_hex_string()),
Err(existing_state_id) => log::debug!("State {} already exists", existing_state_id)
}
@ -193,14 +192,14 @@ pub fn dump_cached_processes(processes: HashMap<OutPoint, Process>) -> Result<()
}
// Register a new state
fn register_new_state(process: &mut Process, commit_msg: &CommitMessage) -> Result<String> {
fn register_new_state(process: &mut Process, commit_msg: &CommitMessage) -> Result<[u8; 32]> {
let last_commited_state = process.get_latest_commited_state();
let new_state_id = commit_msg.pcd_commitment.create_merkle_tree()?.root().unwrap().to_lower_hex_string();
let new_state_id = commit_msg.pcd_commitment.create_merkle_tree()?.root().unwrap();
if let Some(state) = last_commited_state {
if new_state_id == state.state_id {
return Err(Error::msg(format!("{}", new_state_id)));
return Err(Error::msg(format!("{}", new_state_id.to_lower_hex_string())));
}
}
@ -214,7 +213,7 @@ fn register_new_state(process: &mut Process, commit_msg: &CommitMessage) -> Resu
.iter()
.any(|state| state.state_id == new_state_id)
{
return Err(Error::msg(format!("{}", new_state_id)));
return Err(Error::msg(format!("{}", new_state_id.to_lower_hex_string())));
}
// Add the new state
@ -222,7 +221,7 @@ fn register_new_state(process: &mut Process, commit_msg: &CommitMessage) -> Resu
commited_in: current_outpoint,
pcd_commitment: commit_msg.pcd_commitment.clone(),
state_id: new_state_id.clone(),
roles: commit_msg.roles.clone().into_iter().collect(),
roles: commit_msg.roles.clone(),
public_data: commit_msg.public_data.clone(),
..Default::default()
};
@ -234,10 +233,9 @@ fn register_new_state(process: &mut Process, commit_msg: &CommitMessage) -> Resu
// Process validation for a state with validation tokens
fn process_validation(updated_process: &mut Process, commit_msg: &CommitMessage) -> Result<OutPoint> {
let new_state_id = commit_msg.pcd_commitment.create_merkle_tree()?.root().ok_or(Error::msg("Invalid merkle tree"))?;
let new_state_id_hex = new_state_id.to_lower_hex_string();
{
let state_to_update = updated_process
.get_state_for_id_mut(&new_state_id_hex)?;
.get_state_for_id_mut(&new_state_id)?;
// Complete with the received tokens
state_to_update.validation_tokens.extend(commit_msg.validation_tokens.iter());
@ -247,7 +245,7 @@ fn process_validation(updated_process: &mut Process, commit_msg: &CommitMessage)
}
let state_to_validate = updated_process
.get_state_for_id(&new_state_id_hex)?;
.get_state_for_id(&new_state_id)?;
state_to_validate.is_valid(updated_process.get_latest_commited_state())?;
let commited_in = commit_new_transaction(updated_process, state_to_validate.clone())?;
@ -324,6 +322,7 @@ fn commit_new_transaction(
Ok(commited_in)
}
// TODO tests are broken, we need a complete overhaul to make it work again
#[cfg(test)]
mod tests {
use super::*;