Skip to content

Commit

Permalink
Merge pull request #91 from golemfactory/kek/refactor-verify
Browse files Browse the repository at this point in the history
Make VerifyTransactionResult carry total tx amount
  • Loading branch information
kamirr authored Oct 31, 2023
2 parents d563a99 + 101e6bf commit 799dac7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
46 changes: 25 additions & 21 deletions crates/erc20_payment_lib/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,9 +651,19 @@ impl PaymentRuntime {
}
}

pub struct VerifyTransactionResult {
pub verified: bool,
pub reason: Option<String>,
pub enum VerifyTransactionResult {
Verified { amount: U256 },
Rejected(String),
}

impl VerifyTransactionResult {
pub fn verified(&self) -> bool {
matches!(self, Self::Verified { .. })
}

pub fn rejected(&self) -> bool {
matches!(self, Self::Rejected { .. })
}
}

// This is for now very limited check. It needs lot more work to be complete
Expand All @@ -678,37 +688,31 @@ pub async fn verify_transaction(
if Address::from_str(&transfer.receiver_addr).map_err(err_from!())? == receiver
&& Address::from_str(&transfer.from_addr).map_err(err_from!())? == sender
{
return if U256::from_dec_str(&transfer.token_amount).map_err(err_from!())? >= amount
{
let tx_amount = U256::from_dec_str(&transfer.token_amount).map_err(err_from!())?;
return if tx_amount >= amount {
log::info!("Transaction found and verified: {}", tx_hash);
Ok(VerifyTransactionResult {
verified: true,
reason: None,
})
Ok(VerifyTransactionResult::Verified { amount: tx_amount })
} else {
log::warn!(
"Transaction found but amount insufficient: {}: {}/{}",
tx_hash,
transfer.token_amount,
amount
);
Ok(VerifyTransactionResult {
verified: false,
reason: Some("Transaction found but amount insufficient".to_string()),
})
Ok(VerifyTransactionResult::Rejected(
"Transaction found but amount insufficient".to_string(),
))
};
}
}
log::warn!("Transaction found but not matching: {}", tx_hash);
Ok(VerifyTransactionResult {
verified: false,
reason: Some("Transaction found but not matching".to_string()),
})
Ok(VerifyTransactionResult::Rejected(
"Transaction found but not matching".to_string(),
))
} else {
Ok(VerifyTransactionResult {
verified: false,
reason: Some("Transaction not found".to_string()),
})
Ok(VerifyTransactionResult::Rejected(
"Transaction not found".to_string(),
))
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/docker_01_basic/single_erc20_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ async fn test_erc20_transfer() -> Result<(), anyhow::Error> {
let fr_str_wrong = Address::from_str("0xcfb29b133aa51c4b45b49468f9a22958eafea6fa").unwrap();
let to_str = Address::from_str("0xf2f86a61b769c91fc78f15059a5bd2c189b84be2").unwrap();
let to_str_wrong = Address::from_str("0x02f86a61b769c91fc78f15059a5bd2c189b84be2").unwrap();
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str,to_str,U256::from(2222000000000000222_u128), token_address).await.unwrap().verified, true);
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str,to_str_wrong,U256::from(2222000000000000222_u128), token_address).await.unwrap().verified, false);
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str_wrong,to_str,U256::from(2222000000000000222_u128), token_address).await.unwrap().verified, false);
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str,to_str,U256::from(2222000000000000223_u128), token_address).await.unwrap().verified, false);
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str,to_str,U256::from(2222000000000000222_u128), token_address).await.unwrap().verified(), true);
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str,to_str_wrong,U256::from(2222000000000000222_u128), token_address).await.unwrap().verified(), false);
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str_wrong,to_str,U256::from(2222000000000000222_u128), token_address).await.unwrap().verified(), false);
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str,to_str,U256::from(2222000000000000223_u128), token_address).await.unwrap().verified(), false);


}
Expand Down
8 changes: 4 additions & 4 deletions tests/docker_04_multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ async fn test_multi_erc20_transfer(payment_count: usize, use_direct_method: bool
let to_str = Address::from_str(addr).unwrap();
let fr_str_wrong = Address::from_str("0xcfb29b133aa51c4b45b49468f9a22958eafea6fa").unwrap();
let to_str_wrong = Address::from_str("0x02f86a61b769c91fc78f15059a5bd2c189b84be2").unwrap();
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str,to_str,U256::from(*val), token_address).await.unwrap().verified, true);
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str,to_str_wrong,U256::from(*val), token_address).await.unwrap().verified, false);
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str_wrong,to_str,U256::from(*val), token_address).await.unwrap().verified, false);
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str,to_str,U256::from(*val + 1), token_address).await.unwrap().verified, false);
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str,to_str,U256::from(*val), token_address).await.unwrap().verified(), true);
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str,to_str_wrong,U256::from(*val), token_address).await.unwrap().verified(), false);
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str_wrong,to_str,U256::from(*val), token_address).await.unwrap().verified(), false);
assert_eq!(verify_transaction(&web3, 987789, tx_hash,fr_str,to_str,U256::from(*val + 1), token_address).await.unwrap().verified(), false);
}
}

Expand Down

0 comments on commit 799dac7

Please sign in to comment.