Refactor process_validation
This commit is contained in:
parent
5d18fd7688
commit
dbca46a739
@ -229,37 +229,28 @@ fn register_new_state(commitment: &mut Process, commit_msg: CommitMessage) -> Re
|
||||
}
|
||||
|
||||
// Process validation for a state with validation tokens
|
||||
fn process_validation(commitment: &mut Process, commit_msg: CommitMessage) -> Result<OutPoint> {
|
||||
let mut state_to_validate = commitment
|
||||
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 mut state_to_validate = updated_process
|
||||
.get_latest_concurrent_states()?
|
||||
.into_iter()
|
||||
.find(|state| state.pcd_commitment == commit_msg.pcd_commitment)
|
||||
.find(|state| state.state_id == new_state_id_hex)
|
||||
.ok_or(Error::msg("Unknown state"))?
|
||||
.clone();
|
||||
|
||||
state_to_validate.validation_tokens = commit_msg.validation_tokens;
|
||||
state_to_validate.is_valid(commitment.get_latest_commited_state())?;
|
||||
state_to_validate.is_valid(updated_process.get_latest_commited_state())?;
|
||||
|
||||
let mandatory_input = prepare_next_transaction(&state_to_validate)?;
|
||||
let commited_in = commit_new_transaction(commitment, mandatory_input)?;
|
||||
let commited_in = commit_new_transaction(updated_process, state_to_validate)?;
|
||||
|
||||
Ok(commited_in)
|
||||
}
|
||||
|
||||
// Prepare the next transaction based on the validated state
|
||||
fn prepare_next_transaction(state_to_validate: &ProcessState) -> Result<OutPoint> {
|
||||
let mut freezed_utxos = lock_freezed_utxos()?;
|
||||
if freezed_utxos.remove(&state_to_validate.commited_in) {
|
||||
Ok(state_to_validate.commited_in)
|
||||
} else {
|
||||
Err(Error::msg("Commitment UTXO doesn't exist"))
|
||||
}
|
||||
}
|
||||
|
||||
// Commit the new transaction and update the process state
|
||||
fn commit_new_transaction(
|
||||
commitment: &mut Process,
|
||||
mandatory_input: OutPoint,
|
||||
updated_process: &mut Process,
|
||||
state_to_commit: ProcessState,
|
||||
) -> Result<OutPoint> {
|
||||
let sp_wallet = WALLET
|
||||
.get()
|
||||
@ -280,24 +271,32 @@ fn commit_new_transaction(
|
||||
|
||||
let mut freezed_utxos = lock_freezed_utxos()?;
|
||||
|
||||
let next_commited_in = updated_process.get_process_tip()?;
|
||||
let mandatory_input = if let Some(next_outpoint) = freezed_utxos.take(&next_commited_in) {
|
||||
next_outpoint
|
||||
} else {
|
||||
return Err(Error::msg(format!("Missing next commitment outpoint for process {}", updated_process.get_process_id()?)));
|
||||
};
|
||||
|
||||
let commitment_payload = Vec::from_hex(state_to_commit.state_id)?;
|
||||
|
||||
let psbt = create_transaction(
|
||||
vec![mandatory_input],
|
||||
&freezed_utxos,
|
||||
&sp_wallet,
|
||||
vec![recipient],
|
||||
None,
|
||||
Some(commitment_payload),
|
||||
fee_rate,
|
||||
None,
|
||||
)?;
|
||||
|
||||
let new_tx = psbt.extract_tx()?;
|
||||
daemon.test_mempool_accept(&new_tx)?;
|
||||
|
||||
let txid = daemon.broadcast(&new_tx)?;
|
||||
let commited_in = OutPoint::new(txid, 0);
|
||||
|
||||
freezed_utxos.insert(commited_in);
|
||||
commitment.update_states_tip(commited_in)?;
|
||||
updated_process.update_states_tip(commited_in)?;
|
||||
|
||||
Ok(commited_in)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user