Skip to content

Commit

Permalink
payment: fix deposit allocation validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kamirr committed Jul 9, 2024
1 parent bdd5bb2 commit 29715d6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
3 changes: 2 additions & 1 deletion core/model/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub mod local {
use strum::{EnumProperty, IntoEnumIterator, VariantNames};
use strum_macros::{Display, EnumIter, EnumString, EnumVariantNames, IntoStaticStr};

use ya_client_model::NodeId;
use ya_client_model::{payment::allocation::Deposit, NodeId};

pub const BUS_ID: &str = "/local/payment";
pub const DEFAULT_PAYMENT_DRIVER: &str = "erc20";
Expand Down Expand Up @@ -426,6 +426,7 @@ pub mod local {
pub address: String,
pub amount: BigDecimal,
pub timeout: Option<DateTime<Utc>>,
pub deposit: Option<Deposit>,
}

impl RpcMessage for ValidateAllocation {
Expand Down
35 changes: 30 additions & 5 deletions core/payment-driver/erc20/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ impl Erc20Driver {
.map_err(GenericError::new)?;
let deposit_balance =
BigDecimal::new(BigInt::from_str(&deposit_details.amount).unwrap(), 18);
let deposit_timeout = deposit_details.valid_to;

log::info!(
"Allocation validation with deposit: \
Expand All @@ -431,14 +432,38 @@ impl Erc20Driver {
msg.timeout
.map(|tm| tm.to_string())
.unwrap_or(String::from("never")),
deposit_details.valid_to,
deposit_timeout,
);

let valid_amount = msg.amount <= deposit_balance;
let valid_timeout = msg
.timeout
.map(|timeout| timeout <= deposit_details.valid_to)
.unwrap_or(false);

if !valid_amount {
log::info!(
"Deposit validation failed: requested amount [{}] > deposit balance [{}]",
msg.amount,
deposit_balance
);
}

let valid_timeout = if let Some(timeout) = msg.timeout {
let valid_timeout = timeout <= deposit_details.valid_to;

if !valid_timeout {
log::info!(
"Deposit validation failed: requested timeout [{}] > deposit timeout [{}]",
timeout,
deposit_timeout
);
}
valid_timeout
} else {
log::info!(
"Deposit validation failed: allocations with deposits must have a timeout. Deposit timeout: {}",
deposit_timeout
);

false
};

Ok(valid_amount && valid_timeout)
}
Expand Down
4 changes: 3 additions & 1 deletion core/payment/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{Mutex, RwLock};
use ya_client_model::payment::allocation::Deposit;
use ya_client_model::payment::{
Account, ActivityPayment, AgreementPayment, DriverDetails, Network, Payment,
};
Expand Down Expand Up @@ -802,6 +803,7 @@ impl PaymentProcessor {
address: String,
amount: BigDecimal,
timeout: Option<DateTime<Utc>>,
deposit: Option<Deposit>,
) -> Result<bool, ValidateAllocationError> {
if self.in_shutdown.load(Ordering::SeqCst) {
return Err(ValidateAllocationError::Shutdown);
Expand All @@ -823,7 +825,7 @@ impl PaymentProcessor {
platform,
amount,
timeout,
deposit: None,
deposit,
existing_allocations,
};
let result = driver_endpoint(&driver).send(msg).await??;
Expand Down
8 changes: 7 additions & 1 deletion core/payment/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,13 @@ mod local {
msg: ValidateAllocation,
) -> Result<bool, ValidateAllocationError> {
Ok(processor
.validate_allocation(msg.platform, msg.address, msg.amount, msg.timeout)
.validate_allocation(
msg.platform,
msg.address,
msg.amount,
msg.timeout,
msg.deposit,
)
.await?)
}

Expand Down

0 comments on commit 29715d6

Please sign in to comment.