Skip to content

Commit

Permalink
refactor(mint_validation): mv transaction validation to transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrusu authored and dan-da committed May 19, 2021
1 parent 1ca8f18 commit c82c885
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,17 @@ impl MintTransaction {
}
}

pub fn verify_balances(&self) -> Result<()> {
pub fn validate(&self, key_cache: &KeyCache) -> Result<()> {
self.validate_balance()?;
self.validate_input_dbcs(key_cache)?;
self.validate_outputs()?;
Ok(())
}

fn validate_balance(&self) -> Result<()> {
let input: u64 = self.inputs.iter().map(|input| input.amount()).sum();
let output: u64 = self.outputs.iter().map(|output| output.amount).sum();

if input != output {
Err(Error::DbcMintRequestDoesNotBalance { input, output })
} else {
Expand All @@ -74,6 +82,7 @@ impl MintTransaction {
}

fn validate_outputs(&self) -> Result<()> {
// Validate outputs are numbered 0..N_OUTPUTS
let number_set = self
.outputs
.iter()
Expand All @@ -90,6 +99,7 @@ impl MintTransaction {
return Err(Error::OutputsAreNotNumberedCorrectly);
}

// Validate output parents match the blinded inputs
let inputs = self.blinded().inputs;
if self.outputs.iter().any(|o| &o.parents != &inputs) {
return Err(Error::DbcContentParentsDifferentFromTransactionInputs);
Expand Down Expand Up @@ -153,16 +163,14 @@ impl Mint {
&mut self,
mint_request: MintRequest,
) -> Result<(DbcTransaction, InputSignatures)> {
mint_request.transaction.verify_balances()?;
mint_request
.transaction
.validate_input_dbcs(self.key_cache())?;
mint_request.transaction.validate_outputs()?;
mint_request.transaction.validate(self.key_cache())?;

let transaction = mint_request.transaction.blinded();

// Validate that each input has not yet been spent.
for input in transaction.inputs.iter() {
if let Some(transaction) = self.spendbook.lookup(&input).cloned() {
// This input has already been spent, return the transaction to the user
// This input has already been spent, return the spend transaction to the user
let transaction_sigs = self.sign_transaction(&transaction);
return Err(Error::DbcAlreadySpent {
transaction,
Expand Down

0 comments on commit c82c885

Please sign in to comment.