Skip to content

Commit

Permalink
Enter your commit message:
Browse files Browse the repository at this point in the history
almost...
  • Loading branch information
fahertym committed Jul 21, 2024
1 parent 8c6cb34 commit b220678
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 56 deletions.
58 changes: 30 additions & 28 deletions all_code_files_content.txt
Original file line number Diff line number Diff line change
Expand Up @@ -917,58 +917,60 @@ use icn_utils::error::{Error, Result};
pub struct TransactionValidator;

impl TransactionValidator {
pub fn validate_transaction(transaction: &Transaction, blockchain: &Blockchain) -> bool {
if !Self::is_double_spend(transaction, blockchain) &&
Self::validate_currency_and_amount(transaction) &&
Self::check_sufficient_balance(transaction, blockchain) &&
Self::validate_signature(transaction)
{
true
} else {
false
pub fn validate_transaction(transaction: &Transaction, blockchain: &Blockchain) -> Result<()> {
if Self::is_double_spend(transaction, blockchain)? {
return Err(Error::BlockchainError("Double spend detected".to_string()));
}

if !Self::validate_currency_and_amount(transaction) {
return Err(Error::BlockchainError("Invalid currency or amount".to_string()));
}

if !Self::check_sufficient_balance(transaction, blockchain)? {
return Err(Error::BlockchainError("Insufficient balance".to_string()));
}

if !Self::validate_signature(transaction)? {
return Err(Error::BlockchainError("Invalid signature".to_string()));
}

Ok(())
}

fn is_double_spend(transaction: &Transaction, blockchain: &Blockchain) -> bool {
fn is_double_spend(transaction: &Transaction, blockchain: &Blockchain) -> Result<bool> {
for block in &blockchain.chain {
for tx in &block.transactions {
if tx == transaction {
return true;
return Ok(true);
}
}
}
false
Ok(false)
}

fn validate_currency_and_amount(transaction: &Transaction) -> bool {
transaction.amount > 0.0
}

fn check_sufficient_balance(transaction: &Transaction, blockchain: &Blockchain) -> bool {
let balance = blockchain.get_balance(&transaction.from, &transaction.currency_type);
balance >= transaction.amount + transaction.get_fee()
fn check_sufficient_balance(transaction: &Transaction, blockchain: &Blockchain) -> Result<bool> {
let balance = blockchain.get_balance(&transaction.from, &transaction.currency_type)?;
Ok(balance >= transaction.amount + transaction.get_fee())
}

fn validate_signature(transaction: &Transaction) -> bool {
if let Ok(is_valid) = transaction.verify() {
is_valid
} else {
false
}
fn validate_signature(transaction: &Transaction) -> Result<bool> {
transaction.verify().map_err(|e| Error::BlockchainError(format!("Signature verification failed: {}", e)))
}

pub fn can_process_transaction(transaction: &Transaction, blockchain: &Blockchain) -> Result<()> {
if !Self::validate_transaction(transaction, blockchain) {
return Err(Error::BlockchainError("Invalid transaction".to_string()));
}
Ok(())
Self::validate_transaction(transaction, blockchain)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::currency::CurrencyType;
use icn_common::{Transaction, Blockchain};
use ed25519_dalek::Keypair;
use rand::rngs::OsRng;

Expand All @@ -995,7 +997,7 @@ mod tests {
blockchain.add_transaction(create_signed_transaction("Genesis", "Alice", 100.0)).unwrap();
blockchain.create_block().unwrap();

assert!(TransactionValidator::validate_transaction(&tx, &blockchain));
assert!(TransactionValidator::validate_transaction(&tx, &blockchain).is_ok());
}

#[test]
Expand All @@ -1007,7 +1009,7 @@ mod tests {
blockchain.add_transaction(create_signed_transaction("Genesis", "Alice", 100.0)).unwrap();
blockchain.create_block().unwrap();

assert!(!TransactionValidator::validate_transaction(&tx, &blockchain));
assert!(TransactionValidator::validate_transaction(&tx, &blockchain).is_err());
}

#[test]
Expand All @@ -1024,7 +1026,7 @@ mod tests {
blockchain.create_block().unwrap();

// Try to validate the same transaction again
assert!(!TransactionValidator::validate_transaction(&tx, &blockchain));
assert!(TransactionValidator::validate_transaction(&tx, &blockchain).is_err());
}
}
===== END OF /home/matt/InterCooperative-Network/crates/icn_blockchain/src/transaction_validator.rs =====
Expand Down
58 changes: 30 additions & 28 deletions crates/icn_blockchain/src/transaction_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,60 @@ use icn_utils::error::{Error, Result};
pub struct TransactionValidator;

impl TransactionValidator {
pub fn validate_transaction(transaction: &Transaction, blockchain: &Blockchain) -> bool {
if !Self::is_double_spend(transaction, blockchain) &&
Self::validate_currency_and_amount(transaction) &&
Self::check_sufficient_balance(transaction, blockchain) &&
Self::validate_signature(transaction)
{
true
} else {
false
pub fn validate_transaction(transaction: &Transaction, blockchain: &Blockchain) -> Result<()> {
if Self::is_double_spend(transaction, blockchain)? {
return Err(Error::BlockchainError("Double spend detected".to_string()));
}

if !Self::validate_currency_and_amount(transaction) {
return Err(Error::BlockchainError("Invalid currency or amount".to_string()));
}

if !Self::check_sufficient_balance(transaction, blockchain)? {
return Err(Error::BlockchainError("Insufficient balance".to_string()));
}

if !Self::validate_signature(transaction)? {
return Err(Error::BlockchainError("Invalid signature".to_string()));
}

Ok(())
}

fn is_double_spend(transaction: &Transaction, blockchain: &Blockchain) -> bool {
fn is_double_spend(transaction: &Transaction, blockchain: &Blockchain) -> Result<bool> {
for block in &blockchain.chain {
for tx in &block.transactions {
if tx == transaction {
return true;
return Ok(true);
}
}
}
false
Ok(false)
}

fn validate_currency_and_amount(transaction: &Transaction) -> bool {
transaction.amount > 0.0
}

fn check_sufficient_balance(transaction: &Transaction, blockchain: &Blockchain) -> bool {
let balance = blockchain.get_balance(&transaction.from, &transaction.currency_type);
balance >= transaction.amount + transaction.get_fee()
fn check_sufficient_balance(transaction: &Transaction, blockchain: &Blockchain) -> Result<bool> {
let balance = blockchain.get_balance(&transaction.from, &transaction.currency_type)?;
Ok(balance >= transaction.amount + transaction.get_fee())
}

fn validate_signature(transaction: &Transaction) -> bool {
if let Ok(is_valid) = transaction.verify() {
is_valid
} else {
false
}
fn validate_signature(transaction: &Transaction) -> Result<bool> {
transaction.verify().map_err(|e| Error::BlockchainError(format!("Signature verification failed: {}", e)))
}

pub fn can_process_transaction(transaction: &Transaction, blockchain: &Blockchain) -> Result<()> {
if !Self::validate_transaction(transaction, blockchain) {
return Err(Error::BlockchainError("Invalid transaction".to_string()));
}
Ok(())
Self::validate_transaction(transaction, blockchain)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::currency::CurrencyType;
use icn_common::{Transaction, Blockchain};
use ed25519_dalek::Keypair;
use rand::rngs::OsRng;

Expand All @@ -82,7 +84,7 @@ mod tests {
blockchain.add_transaction(create_signed_transaction("Genesis", "Alice", 100.0)).unwrap();
blockchain.create_block().unwrap();

assert!(TransactionValidator::validate_transaction(&tx, &blockchain));
assert!(TransactionValidator::validate_transaction(&tx, &blockchain).is_ok());
}

#[test]
Expand All @@ -94,7 +96,7 @@ mod tests {
blockchain.add_transaction(create_signed_transaction("Genesis", "Alice", 100.0)).unwrap();
blockchain.create_block().unwrap();

assert!(!TransactionValidator::validate_transaction(&tx, &blockchain));
assert!(TransactionValidator::validate_transaction(&tx, &blockchain).is_err());
}

#[test]
Expand All @@ -111,6 +113,6 @@ mod tests {
blockchain.create_block().unwrap();

// Try to validate the same transaction again
assert!(!TransactionValidator::validate_transaction(&tx, &blockchain));
assert!(TransactionValidator::validate_transaction(&tx, &blockchain).is_err());
}
}

0 comments on commit b220678

Please sign in to comment.