Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor unwraps #3171

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
18ce0db
fix merge
scx1332 Apr 3, 2024
0b20dd8
Merge branch 'master' into kek/deposit-allocations
scx1332 Apr 3, 2024
ed436e7
new goth test :)
scx1332 Apr 11, 2024
325681e
Test GOTH scripts
scx1332 Apr 11, 2024
1ca194a
Test GOTH scripts
scx1332 Apr 11, 2024
0745945
Test GOTH scripts
scx1332 Apr 11, 2024
f1a93d2
payment: finish deposit release
kamirr Apr 12, 2024
c75535a
erc20: add lock-contract to payment-config.toml
kamirr Apr 12, 2024
f8e06f4
ref
scx1332 Apr 12, 2024
1c97987
driver: fix schedule payment
kamirr Apr 12, 2024
1452e65
driver: fix schedule payment 2
kamirr Apr 12, 2024
dbcf407
payment: fix deposit allocation validation
kamirr Apr 15, 2024
3fc7186
payment: fix allocation create/amend REST endpoints
kamirr Apr 15, 2024
5d3048c
payment: refactor validate allocation to return an enum instead of bool
kamirr Apr 15, 2024
aa888a5
chore: clippy
kamirr Apr 15, 2024
9a35190
payment: fix panics in deposit validation
kamirr Apr 15, 2024
d96bac0
payment: fix allocation overcommit
kamirr Apr 15, 2024
52a61bd
erc20: prevent deposit reuse
kamirr Apr 15, 2024
2a7e9a7
typo
kamirr Apr 15, 2024
55d2a17
chore: clippy
kamirr Apr 15, 2024
5fa71d1
Merge branch 'kek/deposit-allocations' into scx1332/refactorcode
scx1332 Apr 16, 2024
1d0181f
Merge branch 'deposits' into scx1332/refactorcode
scx1332 Apr 16, 2024
ec0a039
Merge branch 'deposits' into scx1332/refactorcode
scx1332 Apr 18, 2024
4c15d55
Merge branch 'deposits' into scx1332/refactorcode
scx1332 Apr 26, 2024
68734ba
Merge branch 'deposits' into scx1332/refactorcode
scx1332 May 6, 2024
705911f
Merge branch 'deposits' into scx1332/refactorcode
scx1332 May 23, 2024
341d6c9
Merge branch 'deposits' into scx1332/refactorcode
scx1332 May 25, 2024
86f22d7
Update payment.py
scx1332 May 25, 2024
ceebaba
Merge branch 'deposits' into scx1332/refactorcode
scx1332 Jun 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions core/payment/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub mod processor {
use bigdecimal::BigDecimal;
use std::fmt::Display;
use tokio::time::error::Elapsed;
use ya_client_model::NodeId;
use ya_core_model::driver::AccountMode;
use ya_core_model::payment::local::{
GenericError, ValidateAllocationError as GsbValidateAllocationError,
Expand Down Expand Up @@ -172,35 +173,38 @@ pub mod processor {
Self(e.to_string())
}

pub fn platform(order: &Order, platform: &str) -> Result<(), Self> {
pub fn platform(order: &Order, platform: &str) -> Result<(NodeId, NodeId), Self> {
Err(Self(format!(
"Invalid platform for payment order {}: {} != {}",
order.id, order.payment_platform, platform
)))
}

pub fn payer_addr(order: &Order, payer_addr: &str) -> Result<(), Self> {
pub fn payer_addr(order: &Order, payer_addr: &str) -> Result<(NodeId, NodeId), Self> {
Err(Self(format!(
"Invalid payer address for payment order {}: {} != {}",
order.id, order.payer_addr, payer_addr
)))
}

pub fn payee_addr(order: &Order, payee_addr: &str) -> Result<(), Self> {
pub fn payee_addr(order: &Order, payee_addr: &str) -> Result<(NodeId, NodeId), Self> {
Err(Self(format!(
"Invalid payee address for payment order {}: {} != {}",
order.id, order.payee_addr, payee_addr
)))
}

pub fn amount(expected: &BigDecimal, actual: &BigDecimal) -> Result<(), Self> {
pub fn amount(
expected: &BigDecimal,
actual: &BigDecimal,
) -> Result<(NodeId, NodeId), Self> {
Err(Self(format!(
"Invalid payment amount: {} != {}",
expected, actual
)))
}

pub fn zero_amount(order: &Order) -> Result<(), Self> {
pub fn zero_amount(order: &Order) -> Result<(NodeId, NodeId), Self> {
Err(Self(format!(
"Payment order can not have 0 amount. order_id={}",
order.id
Expand All @@ -222,6 +226,8 @@ pub mod processor {
Sign(#[from] ya_core_model::driver::GenericError),
#[error("Internal timeout")]
InternalTimeout(#[from] Elapsed),
#[error("Critical error when notifying: {0}")]
Critical(String),
}

impl NotifyPaymentError {
Expand Down
48 changes: 36 additions & 12 deletions core/payment/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,39 @@
payer_addr: &str,
payee_addr: &str,
amount: &BigDecimal,
) -> Result<(), OrderValidationError> {
) -> Result<(NodeId, NodeId), OrderValidationError> {
if orders.is_empty() {
return Err(OrderValidationError::new(
"orders not found in the database",
));
}

let mut payer_id: Option<NodeId> = None;
let mut payee_id: Option<NodeId> = None;

let mut total_amount = BigDecimal::zero();
for order in orders.iter() {
if let Some(payer_id) = payer_id {
if order.payer_id != payer_id {
return Err(OrderValidationError::new(format!(
"payer_id mismatch {} vs {}",
order.payer_id, payer_id
)));
}
} else {
payer_id = Some(order.payer_id);
}
if let Some(payee_id) = payee_id {
if order.payee_id != payee_id {
return Err(OrderValidationError::new(format!(
"payee_id mismatch {} vs {}",
order.payee_id, payee_id
)));
}
} else {
payee_id = Some(order.payee_id);
}

if order.amount.0 == BigDecimal::zero() {
return OrderValidationError::zero_amount(order);
}
Expand All @@ -82,7 +106,10 @@
return OrderValidationError::amount(&total_amount, amount);
}

Ok(())
Ok((
payer_id.ok_or(OrderValidationError::new("payer_id is null"))?,
payee_id.ok_or(OrderValidationError::new("payee_id is null"))?,
))
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -425,16 +452,18 @@
let payer_id: NodeId;
let payee_id: NodeId;
let payment_id: String;
let mut payment: Payment;

{
let mut payment = {
let db_executor = self.db_executor.timeout_lock(DB_LOCK_TIMEOUT).await?;

let orders = db_executor
.as_dao::<OrderDao>()
.get_many(msg.order_ids, driver.clone())
.await?;
validate_orders(

// FIXME: This is a hack. Payment orders realized by a single transaction are not guaranteed
// to have the same payer and payee IDs. Fixing this requires a major redesign of the
// data model. Payments can no longer by assigned to a single payer and payee.
(payer_id, payee_id) = validate_orders(
&orders,
&payment_platform,
&payer_addr,
Expand All @@ -461,12 +490,6 @@
}
}

// FIXME: This is a hack. Payment orders realized by a single transaction are not guaranteed
// to have the same payer and payee IDs. Fixing this requires a major redesign of the
// data model. Payments can no longer by assigned to a single payer and payee.
payer_id = orders.get(0).unwrap().payer_id;
payee_id = orders.get(0).unwrap().payee_id;

let payment_dao: PaymentDao = db_executor.as_dao();

payment_id = payment_dao
Expand All @@ -483,34 +506,35 @@
)
.await?;


let signed_payment = payment_dao
.get(payment_id.clone(), payer_id)
.await?
.unwrap();
payment = signed_payment.payload;

Check failure on line 514 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / System Tests (ubuntu-latest)

cannot find value `payment` in this scope

Check failure on line 514 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

cannot find value `payment` in this scope

Check failure on line 514 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Build binaries (aarch64)

cannot find value `payment` in this scope

Check failure on line 514 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

cannot find value `payment` in this scope

Check failure on line 514 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

cannot find value `payment` in this scope
}

Check failure on line 515 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / System Tests (ubuntu-latest)

expected `;`, found keyword `for`

Check failure on line 515 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

expected `;`, found keyword `for`

Check failure on line 515 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Build binaries (aarch64)

expected `;`, found keyword `for`

Check failure on line 515 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

expected `;`, found keyword `for`

Check failure on line 515 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

expected `;`, found keyword `for`

// Allocation IDs are requestor's private matter and should not be sent to provider
for agreement_payment in payment.agreement_payments.iter_mut() {

Check failure on line 518 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / System Tests (ubuntu-latest)

no field `agreement_payments` on type `()`

Check failure on line 518 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

no field `agreement_payments` on type `()`

Check failure on line 518 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Build binaries (aarch64)

no field `agreement_payments` on type `()`

Check failure on line 518 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

no field `agreement_payments` on type `()`

Check failure on line 518 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

no field `agreement_payments` on type `()`
agreement_payment.allocation_id = None;
}
for activity_payment in payment.activity_payments.iter_mut() {

Check failure on line 521 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / System Tests (ubuntu-latest)

no field `activity_payments` on type `()`

Check failure on line 521 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

no field `activity_payments` on type `()`

Check failure on line 521 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Build binaries (aarch64)

no field `activity_payments` on type `()`

Check failure on line 521 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

no field `activity_payments` on type `()`

Check failure on line 521 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

no field `activity_payments` on type `()`
activity_payment.allocation_id = None;
}

let signature_canonicalized = driver_endpoint(&driver)
.send(driver::SignPaymentCanonicalized(payment.clone()))

Check failure on line 526 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / System Tests (ubuntu-latest)

mismatched types

Check failure on line 526 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

mismatched types

Check failure on line 526 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Build binaries (aarch64)

mismatched types

Check failure on line 526 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

mismatched types

Check failure on line 526 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

mismatched types
.await??;
let signature = driver_endpoint(&driver)
.send(driver::SignPayment(payment.clone()))

Check failure on line 529 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / System Tests (ubuntu-latest)

mismatched types

Check failure on line 529 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

mismatched types

Check failure on line 529 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Build binaries (aarch64)

mismatched types

Check failure on line 529 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

mismatched types

Check failure on line 529 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

mismatched types
.await??;

counter!("payment.amount.sent", ya_metrics::utils::cryptocurrency_to_u64(&msg.amount), "platform" => payment_platform);
// This is unconditional because at this point the invoice *has been paid*.
// Whether the provider was correctly notified of this fact is another matter.
counter!("payment.invoices.requestor.paid", 1);
let msg = SendPayment::new(payment.clone(), signature);

Check failure on line 536 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / System Tests (ubuntu-latest)

mismatched types

Check failure on line 536 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

mismatched types

Check failure on line 536 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Build binaries (aarch64)

mismatched types

Check failure on line 536 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

mismatched types

Check failure on line 536 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

mismatched types
let msg_with_bytes = SendSignedPayment::new(payment, signature_canonicalized);

Check failure on line 537 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / System Tests (ubuntu-latest)

mismatched types

Check failure on line 537 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (ubuntu-latest)

mismatched types

Check failure on line 537 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Build binaries (aarch64)

mismatched types

Check failure on line 537 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (macos-latest)

mismatched types

Check failure on line 537 in core/payment/src/processor.rs

View workflow job for this annotation

GitHub Actions / Unit Tests (windows-latest)

mismatched types

let db_executor = Arc::clone(&self.db_executor);

Expand Down
Loading