Skip to content

Commit

Permalink
payments: allocation errors according to rfc9457
Browse files Browse the repository at this point in the history
  • Loading branch information
kamirr committed Jul 9, 2024
1 parent 3a2d9ea commit 234d684
Show file tree
Hide file tree
Showing 6 changed files with 302 additions and 95 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 22 additions & 6 deletions core/model/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,30 @@ impl ValidateAllocation {

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum ValidateAllocationResult {
InsufficientAccountFunds,
InsufficientDepositFunds,
TimeoutExceedsDeposit,
TimeoutPassed,
InsufficientAccountFunds {
requested_funds: BigDecimal,
available_funds: BigDecimal,
reserved_funds: BigDecimal,
},
InsufficientDepositFunds {
requested_funds: BigDecimal,
available_funds: BigDecimal,
},
TimeoutExceedsDeposit {
requested_timeout: Option<DateTime<Utc>>,
deposit_timeout: DateTime<Utc>,
},
TimeoutPassed {
requested_timeout: DateTime<Utc>,
},
MalformedDepositContract,
MalformedDepositId,
DepositReused,
DepositSpenderMismatch,
DepositReused {
allocation_id: String,
},
DepositSpenderMismatch {
deposit_spender: String,
},
DepositValidationError(String),
Valid,
}
Expand Down
45 changes: 32 additions & 13 deletions core/payment-driver/erc20/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,12 @@ impl Erc20Driver {
);

Ok(
if msg.amount > account_balance.token_balance - total_allocated_amount {
ValidateAllocationResult::InsufficientAccountFunds
if msg.amount > account_balance.token_balance.clone() - total_allocated_amount.clone() {
ValidateAllocationResult::InsufficientAccountFunds {
requested_funds: msg.amount,
available_funds: account_balance.token_balance - total_allocated_amount.clone(),
reserved_funds: total_allocated_amount,
}
} else {
ValidateAllocationResult::Valid
},
Expand Down Expand Up @@ -444,14 +448,18 @@ impl Erc20Driver {
return Ok(ValidateAllocationResult::MalformedDepositId);
};

let deposit_reused = msg
let conflicting_allocation = msg
.active_allocations
.iter()
.chain(msg.past_allocations.iter())
.any(|allocation| allocation.deposit.as_ref() == Some(&deposit));

if deposit_reused && msg.new_allocation {
return Ok(ValidateAllocationResult::DepositReused);
.into_iter()
.chain(msg.past_allocations.into_iter())
.find(|allocation| allocation.deposit.as_ref() == Some(&deposit));

if msg.new_allocation {
if let Some(allocation) = conflicting_allocation {
return Ok(ValidateAllocationResult::DepositReused {
allocation_id: allocation.allocation_id,
});
}
}

let deposit_details = self
Expand Down Expand Up @@ -497,7 +505,9 @@ impl Erc20Driver {
deposit_spender
);

return Ok(ValidateAllocationResult::DepositSpenderMismatch);
return Ok(ValidateAllocationResult::DepositSpenderMismatch {
deposit_spender: deposit_spender.to_string(),
});
}

if msg.amount > deposit_balance {
Expand All @@ -507,7 +517,10 @@ impl Erc20Driver {
deposit_balance
);

return Ok(ValidateAllocationResult::InsufficientDepositFunds);
return Ok(ValidateAllocationResult::InsufficientDepositFunds {
requested_funds: msg.amount,
available_funds: deposit_balance,
});
}

if let Some(timeout) = msg.timeout {
Expand All @@ -518,15 +531,21 @@ impl Erc20Driver {
deposit_timeout
);

return Ok(ValidateAllocationResult::TimeoutExceedsDeposit);
return Ok(ValidateAllocationResult::TimeoutExceedsDeposit {
requested_timeout: Some(timeout),
deposit_timeout: deposit_details.valid_to,
});
}
} else {
log::debug!(
"Deposit validation failed: allocations with deposits must have a timeout. Deposit timeout: {}",
deposit_timeout
);

return Ok(ValidateAllocationResult::TimeoutExceedsDeposit);
return Ok(ValidateAllocationResult::TimeoutExceedsDeposit {
requested_timeout: None,
deposit_timeout: deposit_details.valid_to,
});
};

if let Some(extra_validation) = deposit.validate {
Expand Down
5 changes: 3 additions & 2 deletions core/payment/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ ya-persistence = "0.3"
ya-service-api = "0.1"
ya-service-api-interfaces = "0.2"
ya-service-api-web = "0.2"
ya-service-bus = { workspace = true }
ya-service-bus = { workspace = true }

actix-web = "4"
anyhow = "1.0"
Expand All @@ -43,12 +43,14 @@ erc20_payment_lib = { workspace = true }
futures = "0.3"
hex = { workspace = true }
humantime = "2.0.1"
http = "1.1.0"
lazy_static = "1.4"
libsqlite3-sys = { workspace = true }
log = "0.4"
metrics = "0.12"
num-bigint = "0.3"
open = "5.1.2"
problem_details = "0.6.0"
r2d2 = "0.8"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand All @@ -72,4 +74,3 @@ ethsign = "0.8"

[lints]
workspace = true

Loading

0 comments on commit 234d684

Please sign in to comment.