taler-rust

GNU Taler code in Rust. Largely core banking integrations.
Log | Files | Refs | Submodules | README | LICENSE

commit 3adb0e5d138b6646320fa61c76789a17294a7dd3
parent 347379a20e932c6c52eee5d6f48c5d98c8f7a8cb
Author: Antoine A <>
Date:   Wed,  5 Feb 2025 17:29:56 +0100

magnet-bank: clean code

Diffstat:
MCargo.lock | 18+++++++++---------
Mtaler-magnet-bank/src/adapter.rs | 16++++++++++------
Mtaler-magnet-bank/src/db.rs | 57+++++++++++++++++++++++++++++++--------------------------
Mtaler-magnet-bank/src/magnet.rs | 10+++-------
Mtaler-magnet-bank/src/worker.rs | 8++++----
5 files changed, 57 insertions(+), 52 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -1861,7 +1861,7 @@ checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" dependencies = [ "rand_chacha 0.9.0", "rand_core 0.9.0", - "zerocopy 0.8.15", + "zerocopy 0.8.16", ] [[package]] @@ -1900,7 +1900,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b08f3c9802962f7e1b25113931d94f43ed9725bebc59db9d0c3e9a23b67e15ff" dependencies = [ "getrandom 0.3.1", - "zerocopy 0.8.15", + "zerocopy 0.8.16", ] [[package]] @@ -2933,9 +2933,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "uuid" -version = "1.12.1" +version = "1.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3758f5e68192bb96cc8f9b7e2c2cfdabb435499a28499a42f8f984092adad4b" +checksum = "ced87ca4be083373936a67f8de945faa23b6b42384bd5b64434850802c6dccd0" [[package]] name = "valuable" @@ -3367,11 +3367,11 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.8.15" +version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1e101d4bc320b6f9abb68846837b70e25e380ca2f467ab494bf29fcc435fcc3" +checksum = "7b8c07a70861ce02bad1607b5753ecb2501f67847b9f9ada7c160fff0ec6300c" dependencies = [ - "zerocopy-derive 0.8.15", + "zerocopy-derive 0.8.16", ] [[package]] @@ -3387,9 +3387,9 @@ dependencies = [ [[package]] name = "zerocopy-derive" -version = "0.8.15" +version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03a73df1008145cd135b3c780d275c57c3e6ba8324a41bd5e0008fe167c3bc7c" +checksum = "5226bc9a9a9836e7428936cde76bb6b22feea1a8bfdbc0d241136e4d13417e25" dependencies = [ "proc-macro2", "quote", diff --git a/taler-magnet-bank/src/adapter.rs b/taler-magnet-bank/src/adapter.rs @@ -152,9 +152,11 @@ impl WireGateway for MagnetApi { ) .await?; match res { - AddIncomingResult::Success(res) => Ok(AddIncomingResponse { - row_id: safe_u64(res.row_id), - timestamp: res.timestamp, + AddIncomingResult::Success { + row_id, timestamp, .. + } => Ok(AddIncomingResponse { + row_id: safe_u64(row_id), + timestamp, }), AddIncomingResult::ReservePubReuse => Err(failure( ErrorCode::BANK_DUPLICATE_RESERVE_PUB_SUBJECT, @@ -177,9 +179,11 @@ impl WireGateway for MagnetApi { ) .await?; match res { - AddIncomingResult::Success(res) => Ok(AddKycauthResponse { - row_id: safe_u64(res.row_id), - timestamp: res.timestamp, + AddIncomingResult::Success { + row_id, timestamp, .. + } => Ok(AddKycauthResponse { + row_id: safe_u64(row_id), + timestamp, }), AddIncomingResult::ReservePubReuse => Err(failure( ErrorCode::BANK_DUPLICATE_RESERVE_PUB_SUBJECT, diff --git a/taler-magnet-bank/src/db.rs b/taler-magnet-bank/src/db.rs @@ -111,7 +111,7 @@ pub struct TxInAdmin { } #[derive(Debug, PartialEq, Eq)] -pub struct RegisteredTx { +pub struct AddOutgoingResult { pub new: bool, pub row_id: u64, pub timestamp: Timestamp, @@ -119,7 +119,11 @@ pub struct RegisteredTx { #[derive(Debug, PartialEq, Eq)] pub enum AddIncomingResult { - Success(RegisteredTx), + Success { + new: bool, + row_id: u64, + timestamp: Timestamp, + }, ReservePubReuse, } @@ -174,11 +178,11 @@ pub async fn register_tx_in_admin(db: &PgPool, tx: &TxInAdmin) -> sqlx::Result<A Ok(if r.try_get(0)? { AddIncomingResult::ReservePubReuse } else { - AddIncomingResult::Success(RegisteredTx { + AddIncomingResult::Success { new: r.try_get(1)?, row_id: r.try_get_u64(2)?, timestamp: r.try_get_timestamp(3)?, - }) + } }) }) .fetch_one(db) @@ -208,11 +212,11 @@ pub async fn register_tx_in( Ok(if r.try_get(0)? { AddIncomingResult::ReservePubReuse } else { - AddIncomingResult::Success(RegisteredTx { + AddIncomingResult::Success { new: r.try_get(1)?, row_id: r.try_get_u64(2)?, timestamp: r.try_get_timestamp(3)?, - }) + } }) }) .fetch_one(db) @@ -223,7 +227,7 @@ pub async fn register_tx_out( db: &mut PgConnection, tx: &TxOut, subject: &Option<OutgoingSubject>, -) -> sqlx::Result<RegisteredTx> { +) -> sqlx::Result<AddOutgoingResult> { sqlx::query( " SELECT out_new, out_tx_row_id, out_timestamp @@ -239,7 +243,7 @@ pub async fn register_tx_out( .bind(subject.as_ref().map(|it| it.0.as_ref())) .bind(subject.as_ref().map(|it| it.1.as_str())) .try_map(|r: PgRow| { - Ok(RegisteredTx { + Ok(AddOutgoingResult { new: r.try_get(0)?, row_id: r.try_get_u64(1)?, timestamp: r.try_get_timestamp(2)?, @@ -628,7 +632,8 @@ mod test { constant::CURRENCY, db::{ self, make_transfer, register_tx_in, register_tx_in_admin, register_tx_out, - AddIncomingResult, BounceResult, Initiated, RegisteredTx, TransferResult, TxIn, TxOut, + AddIncomingResult, AddOutgoingResult, BounceResult, Initiated, TransferResult, TxIn, + TxOut, }, magnet_payto, }; @@ -675,11 +680,11 @@ mod test { register_tx_in(db, &tx, &first) .await .expect("register tx in"), - AddIncomingResult::Success(RegisteredTx { + AddIncomingResult::Success { new: true, row_id: id, timestamp: tx.timestamp - }) + } ); // Idempotent assert_eq!( @@ -693,11 +698,11 @@ mod test { ) .await .expect("register tx in"), - AddIncomingResult::Success(RegisteredTx { + AddIncomingResult::Success { new: false, row_id: id, timestamp: tx.timestamp - }) + } ); // Many assert_eq!( @@ -711,11 +716,11 @@ mod test { ) .await .expect("register tx in"), - AddIncomingResult::Success(RegisteredTx { + AddIncomingResult::Success { new: true, row_id: id + 1, timestamp: tx.timestamp - }) + } ); } @@ -793,11 +798,11 @@ mod test { register_tx_in_admin(&pool, &tx) .await .expect("register tx in"), - AddIncomingResult::Success(RegisteredTx { + AddIncomingResult::Success { new: true, row_id: 1, timestamp: tx.timestamp - }) + } ); // Idempotent assert_eq!( @@ -810,11 +815,11 @@ mod test { ) .await .expect("register tx in"), - AddIncomingResult::Success(RegisteredTx { + AddIncomingResult::Success { new: false, row_id: 1, timestamp: tx.timestamp - }) + } ); // Many assert_eq!( @@ -828,11 +833,11 @@ mod test { ) .await .expect("register tx in"), - AddIncomingResult::Success(RegisteredTx { + AddIncomingResult::Success { new: true, row_id: 2, timestamp: tx.timestamp - }) + } ); // History @@ -874,7 +879,7 @@ mod test { register_tx_out(db, &tx, &first) .await .expect("register tx out"), - RegisteredTx { + AddOutgoingResult { new: true, row_id: id, timestamp: tx.timestamp @@ -892,7 +897,7 @@ mod test { ) .await .expect("register tx out"), - RegisteredTx { + AddOutgoingResult { new: false, row_id: id, timestamp: tx.timestamp @@ -910,7 +915,7 @@ mod test { ) .await .expect("register tx out"), - RegisteredTx { + AddOutgoingResult { new: true, row_id: id + 1, timestamp: tx.timestamp @@ -1087,11 +1092,11 @@ mod test { ) .await .expect("register tx in"), - AddIncomingResult::Success(RegisteredTx { + AddIncomingResult::Success { new: true, row_id: 1, timestamp - }) + } ); // Bounce assert_eq!( diff --git a/taler-magnet-bank/src/magnet.rs b/taler-magnet-bank/src/magnet.rs @@ -442,17 +442,13 @@ impl ApiClient<'_> { if let Some(next) = next { req = req .query(&[("nextId", next.next_id)]) - .query(&[("nextTipus", &next.next_type)]) - .query(&[("tranzakciofrissite", true)]); + .query(&[("nextTipus", &next.next_type)]); } if let Some(status) = status { req = req.query(&[("statusz", status)]); } - if direction != Direction::Both { - req = req.query(&[("terheles", direction)]) - } - - req.query(&[("tranzakciofrissites", "true")]) + req.query(&[("terheles", direction)]) + .query(&[("tranzakciofrissites", true)]) .oauth(self.consumer, Some(self.access), None) .await .magnet_call() diff --git a/taler-magnet-bank/src/worker.rs b/taler-magnet-bank/src/worker.rs @@ -73,8 +73,8 @@ impl Worker<'_> { // TODO bounce in db let res = db::register_tx_in(db, tx_in, &None).await?; match res { - AddIncomingResult::Success(registered_tx) => { - if registered_tx.new { + AddIncomingResult::Success { new, .. } => { + if new { info!("incoming {tx_in} bounced in TODO"); } else { debug!("incoming {tx_in} already seen and bounced in TODO"); @@ -93,8 +93,8 @@ impl Worker<'_> { let res = db::register_tx_in(self.db, &tx_in, &Some(subject)).await?; match res { - AddIncomingResult::Success(registered_tx) => { - if registered_tx.new { + AddIncomingResult::Success { new, .. } => { + if new { info!("incoming {tx_in}"); } else { debug!("incoming {tx_in}");