From 1e255e81376b1f71c2f37b3fd2e38a439a3e4ed9 Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Thu, 13 Feb 2025 16:55:54 +0100 Subject: [PATCH] refactor: implement sender account for horizon (#627) Signed-off-by: Gustavo Inacio --- ...6b3503319e12ac9bdf25ef67942d86aa68508.json | 14 ++++ ...bceda54664d0f41228f60b3a0f7f601cf9923.json | 28 ++++++++ ...b4530d70a5c1a9d6ecaeed59f20a63914d723.json | 22 ++++++ ...ca058c0d7886ca33932434c72ca82a8073567.json | 14 ++++ crates/tap-agent/src/agent/sender_account.rs | 71 +++++++++++++++---- ...11337_tap_horizon_sender_denylist.down.sql | 2 + ...2211337_tap_horizon_sender_denylist.up.sql | 4 ++ 7 files changed, 143 insertions(+), 12 deletions(-) create mode 100644 .sqlx/query-1b12436e72e588d745645c4c5286b3503319e12ac9bdf25ef67942d86aa68508.json create mode 100644 .sqlx/query-27a9c5a16714c8551c8921e4012bceda54664d0f41228f60b3a0f7f601cf9923.json create mode 100644 .sqlx/query-712d6c51098b981a3a84908eda1b4530d70a5c1a9d6ecaeed59f20a63914d723.json create mode 100644 .sqlx/query-75125066518ed99d12c1cf4e738ca058c0d7886ca33932434c72ca82a8073567.json create mode 100644 migrations/20250212211337_tap_horizon_sender_denylist.down.sql create mode 100644 migrations/20250212211337_tap_horizon_sender_denylist.up.sql diff --git a/.sqlx/query-1b12436e72e588d745645c4c5286b3503319e12ac9bdf25ef67942d86aa68508.json b/.sqlx/query-1b12436e72e588d745645c4c5286b3503319e12ac9bdf25ef67942d86aa68508.json new file mode 100644 index 00000000..979adfb4 --- /dev/null +++ b/.sqlx/query-1b12436e72e588d745645c4c5286b3503319e12ac9bdf25ef67942d86aa68508.json @@ -0,0 +1,14 @@ +{ + "db_name": "PostgreSQL", + "query": "\n DELETE FROM tap_horizon_denylist\n WHERE sender_address = $1\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Bpchar" + ] + }, + "nullable": [] + }, + "hash": "1b12436e72e588d745645c4c5286b3503319e12ac9bdf25ef67942d86aa68508" +} diff --git a/.sqlx/query-27a9c5a16714c8551c8921e4012bceda54664d0f41228f60b3a0f7f601cf9923.json b/.sqlx/query-27a9c5a16714c8551c8921e4012bceda54664d0f41228f60b3a0f7f601cf9923.json new file mode 100644 index 00000000..6a5e4b13 --- /dev/null +++ b/.sqlx/query-27a9c5a16714c8551c8921e4012bceda54664d0f41228f60b3a0f7f601cf9923.json @@ -0,0 +1,28 @@ +{ + "db_name": "PostgreSQL", + "query": "\n SELECT allocation_id, value_aggregate\n FROM tap_horizon_ravs\n WHERE payer = $1 AND last AND NOT final;\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "allocation_id", + "type_info": "Bpchar" + }, + { + "ordinal": 1, + "name": "value_aggregate", + "type_info": "Numeric" + } + ], + "parameters": { + "Left": [ + "Bpchar" + ] + }, + "nullable": [ + false, + false + ] + }, + "hash": "27a9c5a16714c8551c8921e4012bceda54664d0f41228f60b3a0f7f601cf9923" +} diff --git a/.sqlx/query-712d6c51098b981a3a84908eda1b4530d70a5c1a9d6ecaeed59f20a63914d723.json b/.sqlx/query-712d6c51098b981a3a84908eda1b4530d70a5c1a9d6ecaeed59f20a63914d723.json new file mode 100644 index 00000000..b1730373 --- /dev/null +++ b/.sqlx/query-712d6c51098b981a3a84908eda1b4530d70a5c1a9d6ecaeed59f20a63914d723.json @@ -0,0 +1,22 @@ +{ + "db_name": "PostgreSQL", + "query": "\n SELECT EXISTS (\n SELECT 1\n FROM tap_horizon_denylist\n WHERE sender_address = $1\n ) as denied\n ", + "describe": { + "columns": [ + { + "ordinal": 0, + "name": "denied", + "type_info": "Bool" + } + ], + "parameters": { + "Left": [ + "Bpchar" + ] + }, + "nullable": [ + null + ] + }, + "hash": "712d6c51098b981a3a84908eda1b4530d70a5c1a9d6ecaeed59f20a63914d723" +} diff --git a/.sqlx/query-75125066518ed99d12c1cf4e738ca058c0d7886ca33932434c72ca82a8073567.json b/.sqlx/query-75125066518ed99d12c1cf4e738ca058c0d7886ca33932434c72ca82a8073567.json new file mode 100644 index 00000000..530d4097 --- /dev/null +++ b/.sqlx/query-75125066518ed99d12c1cf4e738ca058c0d7886ca33932434c72ca82a8073567.json @@ -0,0 +1,14 @@ +{ + "db_name": "PostgreSQL", + "query": "\n INSERT INTO tap_horizon_denylist (sender_address)\n VALUES ($1) ON CONFLICT DO NOTHING\n ", + "describe": { + "columns": [], + "parameters": { + "Left": [ + "Bpchar" + ] + }, + "nullable": [] + }, + "hash": "75125066518ed99d12c1cf4e738ca058c0d7886ca33932434c72ca82a8073567" +} diff --git a/crates/tap-agent/src/agent/sender_account.rs b/crates/tap-agent/src/agent/sender_account.rs index 515f5dd2..b58d676e 100644 --- a/crates/tap-agent/src/agent/sender_account.rs +++ b/crates/tap-agent/src/agent/sender_account.rs @@ -609,7 +609,18 @@ impl State { .await .expect("Should not fail to delete from denylist"); } - SenderType::Horizon => unimplemented!(), + SenderType::Horizon => { + sqlx::query!( + r#" + DELETE FROM tap_horizon_denylist + WHERE sender_address = $1 + "#, + self.sender.encode_hex(), + ) + .execute(&self.pgpool) + .await + .expect("Should not fail to delete from denylist"); + } } self.denied = false; @@ -726,7 +737,7 @@ impl Actor for SenderAccount { .get_balance_for_sender(&sender_id) .unwrap_or_default(); async move { - let last_non_final_ravs = match sender_type { + let last_non_final_ravs: Vec<_> = match sender_type { // Get all ravs from v1 table SenderType::Legacy => sqlx::query!( r#" @@ -738,11 +749,25 @@ impl Actor for SenderAccount { ) .fetch_all(&pgpool) .await - .expect("Should not fail to fetch from scalar_tap_ravs"), + .expect("Should not fail to fetch from scalar_tap_ravs") + .into_iter() + .map(|record| (record.allocation_id, record.value_aggregate)) + .collect(), // Get all ravs from v2 table - SenderType::Horizon => { - unimplemented!() - } + SenderType::Horizon => sqlx::query!( + r#" + SELECT allocation_id, value_aggregate + FROM tap_horizon_ravs + WHERE payer = $1 AND last AND NOT final; + "#, + sender_id.encode_hex(), + ) + .fetch_all(&pgpool) + .await + .expect("Should not fail to fetch from scalar_tap_ravs") + .into_iter() + .map(|record| (record.allocation_id, record.value_aggregate)) + .collect(), }; // get a list from the subgraph of which subgraphs were already redeemed and were not marked as final @@ -754,7 +779,7 @@ impl Actor for SenderAccount { unfinalized_transactions::Variables { unfinalized_ravs_allocation_ids: last_non_final_ravs .iter() - .map(|rav| rav.allocation_id.to_string()) + .map(|rav| rav.0.to_string()) .collect::>(), sender: format!("{:x?}", sender_id), }, @@ -785,8 +810,8 @@ impl Actor for SenderAccount { .into_iter() .filter_map(|rav| { Some(( - Address::from_str(&rav.allocation_id).ok()?, - rav.value_aggregate.to_bigint().and_then(|v| v.to_u128())?, + Address::from_str(&rav.0).ok()?, + rav.1.to_bigint().and_then(|v| v.to_u128())?, )) }) .filter(|(allocation, _value)| { @@ -827,7 +852,20 @@ impl Actor for SenderAccount { .denied .expect("Deny status cannot be null"), // Get deny status from the tap horizon table - SenderType::Horizon => unimplemented!(), + SenderType::Horizon => sqlx::query!( + r#" + SELECT EXISTS ( + SELECT 1 + FROM tap_horizon_denylist + WHERE sender_address = $1 + ) as denied + "#, + sender_id.encode_hex(), + ) + .fetch_one(&pgpool) + .await? + .denied + .expect("Deny status cannot be null"), }; let sender_balance = escrow_accounts @@ -1327,8 +1365,17 @@ impl SenderAccount { .expect("Should not fail to insert into denylist"); } - async fn deny_v2_sender(_pool: &PgPool, _sender: Address) { - unimplemented!() + async fn deny_v2_sender(pool: &PgPool, sender: Address) { + sqlx::query!( + r#" + INSERT INTO tap_horizon_denylist (sender_address) + VALUES ($1) ON CONFLICT DO NOTHING + "#, + sender.encode_hex(), + ) + .execute(pool) + .await + .expect("Should not fail to insert into denylist"); } } diff --git a/migrations/20250212211337_tap_horizon_sender_denylist.down.sql b/migrations/20250212211337_tap_horizon_sender_denylist.down.sql new file mode 100644 index 00000000..cf1166a3 --- /dev/null +++ b/migrations/20250212211337_tap_horizon_sender_denylist.down.sql @@ -0,0 +1,2 @@ +-- Add down migration script here +DROP TABLE IF EXISTS tap_horizon_denylist CASCADE; diff --git a/migrations/20250212211337_tap_horizon_sender_denylist.up.sql b/migrations/20250212211337_tap_horizon_sender_denylist.up.sql new file mode 100644 index 00000000..fff69af0 --- /dev/null +++ b/migrations/20250212211337_tap_horizon_sender_denylist.up.sql @@ -0,0 +1,4 @@ +-- Add up migration script here +CREATE TABLE IF NOT EXISTS tap_horizon_denylist ( + sender_address CHAR(40) PRIMARY KEY +);