Skip to content

Commit

Permalink
Add duplicate position open check for transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
cronokirby committed Feb 28, 2024
1 parent 789ff2a commit c71f2b2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions crates/core/app/src/action_handler/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ mod stateless;

use self::stateful::{claimed_anchor_is_valid, fee_greater_than_base_fee, fmd_parameters_valid};
use stateless::{
check_memo_exists_if_outputs_absent_if_not, no_duplicate_spends, no_duplicate_votes,
num_clues_equal_to_num_outputs, valid_binding_signature,
check_memo_exists_if_outputs_absent_if_not, no_duplicate_position_opens, no_duplicate_spends,
no_duplicate_votes, num_clues_equal_to_num_outputs, valid_binding_signature,
};

#[async_trait]
Expand All @@ -32,6 +32,7 @@ impl ActionHandler for Transaction {
valid_binding_signature(self)?;
no_duplicate_spends(self)?;
no_duplicate_votes(self)?;
no_duplicate_position_opens(self)?;
num_clues_equal_to_num_outputs(self)?;
check_memo_exists_if_outputs_absent_if_not(self)?;

Expand Down
13 changes: 13 additions & 0 deletions crates/core/app/src/action_handler/transaction/stateless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ pub(super) fn no_duplicate_spends(tx: &Transaction) -> Result<()> {
Ok(())
}

pub(super) fn no_duplicate_position_opens(tx: &Transaction) -> Result<()> {
// Disallow opening multiple positions with the same id.
let mut ids = BTreeSet::new();
for position_open in tx.position_openings() {
let id = position_open.position.id();
if let Some(duplicate) = ids.replace(id) {
anyhow::bail!("Duplicate position open in transaction: {}", duplicate);
}
}

Ok(())
}

pub fn num_clues_equal_to_num_outputs(tx: &Transaction) -> anyhow::Result<()> {
if tx
.transaction_body()
Expand Down

0 comments on commit c71f2b2

Please sign in to comment.