Skip to content

Commit

Permalink
payment: release deposits with allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
kamirr committed Jul 9, 2024
1 parent 2ce3ab3 commit 395cec9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
20 changes: 20 additions & 0 deletions core/model/src/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,26 @@ pub mod local {
type Error = GenericError;
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ReleaseDeposit {
pub signer: NodeId,
pub deposit_id: String,
}

impl RpcMessage for ReleaseDeposit {
const ID: &'static str = "ReleaseDeposit";
type Item = bool;
type Error = ReleaseDepositError;
}

#[derive(Clone, Debug, Serialize, Deserialize, thiserror::Error)]
pub enum ReleaseDepositError {
#[error("Account not registered")]
AccountNotRegistered,
#[error("Error while releasing allocation: {0}")]
Other(String),
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct GetDrivers {}

Expand Down
22 changes: 19 additions & 3 deletions core/payment/src/api/allocations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use ya_agreement_utils::{ClauseOperator, ConstraintKey, Constraints};
use ya_client_model::payment::allocation::{PaymentPlatform, PaymentPlatformEnum};
use ya_client_model::payment::*;
use ya_core_model::payment::local::{
get_token_from_network_name, DriverName, NetworkName, ValidateAllocation,
get_token_from_network_name, DriverName, NetworkName, ReleaseDeposit, ValidateAllocation,
ValidateAllocationError, BUS_ID as LOCAL_SERVICE,
};
use ya_core_model::payment::RpcMessageError;
Expand Down Expand Up @@ -523,7 +523,23 @@ async fn release_allocation(
let dao = db.as_dao::<AllocationDao>();

match dao.release(allocation_id.clone(), node_id).await {
Ok(AllocationReleaseStatus::Released) => response::ok(Null),
Ok(AllocationReleaseStatus::Released { deposit }) => {
if let Some(deposit) = deposit {
let release_result = bus::service(LOCAL_SERVICE)
.send(ReleaseDeposit {
signer: id.identity,
deposit_id: deposit.id,
})
.await;
match release_result {
Ok(Ok(_)) => response::ok(Null),
Err(e) => response::server_error(&e),
Ok(Err(e)) => response::server_error(&e),
}
} else {
response::ok(Null)
}
}
Ok(AllocationReleaseStatus::NotFound) => response::not_found(),
Ok(AllocationReleaseStatus::Gone) => response::gone(&format!(
"Allocation {} has been already released",
Expand Down Expand Up @@ -624,7 +640,7 @@ pub async fn forced_release_allocation(
.release(allocation_id.clone(), node_id)
.await
{
Ok(AllocationReleaseStatus::Released) => {
Ok(AllocationReleaseStatus::Released { deposit: _ }) => {
log::info!("Allocation {} released.", allocation_id);
}
Err(e) => {
Expand Down
11 changes: 7 additions & 4 deletions core/payment/src/dao/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::schema::pay_allocation::dsl;
use bigdecimal::BigDecimal;
use chrono::NaiveDateTime;
use diesel::{self, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl};
use ya_client_model::payment::allocation::Deposit;
use ya_client_model::payment::{Allocation, NewAllocation};
use ya_client_model::NodeId;
use ya_persistence::executor::{
Expand Down Expand Up @@ -178,7 +179,7 @@ impl<'c> AllocationDao<'c> {
.first(conn)
.optional()?;

match allocation {
let deposit = match allocation {
Some(allocation) => {
if let Some(owner_id) = owner_id {
if owner_id != allocation.owner_id {
Expand All @@ -189,9 +190,11 @@ impl<'c> AllocationDao<'c> {
if allocation.released {
return Ok(AllocationReleaseStatus::Gone);
}

Allocation::from(allocation).deposit
}
None => return Ok(AllocationReleaseStatus::NotFound),
}
};

let num_released = diesel::update(dsl::pay_allocation)
.filter(dsl::released.eq(false))
Expand All @@ -200,7 +203,7 @@ impl<'c> AllocationDao<'c> {
.execute(conn)?;

match num_released {
1 => Ok(AllocationReleaseStatus::Released),
1 => Ok(AllocationReleaseStatus::Released { deposit }),
_ => Err(DbError::Query(format!(
"Update error occurred when releasing allocation {}",
allocation_id
Expand Down Expand Up @@ -246,5 +249,5 @@ pub enum AllocationStatus {
pub enum AllocationReleaseStatus {
Gone,
NotFound,
Released,
Released { deposit: Option<Deposit> },
}

0 comments on commit 395cec9

Please sign in to comment.