From ce15620aa043ef0a7656aa55155f2d059db17fa1 Mon Sep 17 00:00:00 2001 From: Neotamandua <107320179+Neotamandua@users.noreply.github.com> Date: Wed, 4 Dec 2024 16:05:20 +0200 Subject: [PATCH] node: add phoenix info to finalized blocks - Change SQLite Schema - Add SQLite function to retrieve next block that includes phoenix events - Update queries --- ...0561d9b67031fb25026a53da9c2d097c9e1ae.json | 20 +++++++++++ ...a557d6c04b2d7f24b55f0cea3f5e8438f7c8.json} | 6 ++-- node/migrations/20241108164533_archive.sql | 2 ++ node/src/archive/sqlite.rs | 35 ++++++++++++++++--- 4 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 node/.sqlx/query-31801aeca4aa835ca1ec5a27cfc0561d9b67031fb25026a53da9c2d097c9e1ae.json rename node/.sqlx/{query-6c94f167efd5efed0ad4467a8e70242c67cce76460cee23eb2a86c62954bad34.json => query-cf4cdc732b5ba571226bc23da16ba557d6c04b2d7f24b55f0cea3f5e8438f7c8.json} (56%) diff --git a/node/.sqlx/query-31801aeca4aa835ca1ec5a27cfc0561d9b67031fb25026a53da9c2d097c9e1ae.json b/node/.sqlx/query-31801aeca4aa835ca1ec5a27cfc0561d9b67031fb25026a53da9c2d097c9e1ae.json new file mode 100644 index 000000000..c2189817a --- /dev/null +++ b/node/.sqlx/query-31801aeca4aa835ca1ec5a27cfc0561d9b67031fb25026a53da9c2d097c9e1ae.json @@ -0,0 +1,20 @@ +{ + "db_name": "SQLite", + "query": "SELECT block_height FROM finalized_blocks WHERE block_height > ? AND phoenix_present = 1", + "describe": { + "columns": [ + { + "name": "block_height", + "ordinal": 0, + "type_info": "Integer" + } + ], + "parameters": { + "Right": 1 + }, + "nullable": [ + false + ] + }, + "hash": "31801aeca4aa835ca1ec5a27cfc0561d9b67031fb25026a53da9c2d097c9e1ae" +} diff --git a/node/.sqlx/query-6c94f167efd5efed0ad4467a8e70242c67cce76460cee23eb2a86c62954bad34.json b/node/.sqlx/query-cf4cdc732b5ba571226bc23da16ba557d6c04b2d7f24b55f0cea3f5e8438f7c8.json similarity index 56% rename from node/.sqlx/query-6c94f167efd5efed0ad4467a8e70242c67cce76460cee23eb2a86c62954bad34.json rename to node/.sqlx/query-cf4cdc732b5ba571226bc23da16ba557d6c04b2d7f24b55f0cea3f5e8438f7c8.json index dd1365241..ba85c524f 100644 --- a/node/.sqlx/query-6c94f167efd5efed0ad4467a8e70242c67cce76460cee23eb2a86c62954bad34.json +++ b/node/.sqlx/query-cf4cdc732b5ba571226bc23da16ba557d6c04b2d7f24b55f0cea3f5e8438f7c8.json @@ -1,12 +1,12 @@ { "db_name": "SQLite", - "query": "INSERT INTO finalized_blocks (block_height, block_hash) VALUES (?, ?)", + "query": "INSERT INTO finalized_blocks (block_height, block_hash, phoenix_present) VALUES (?, ?, ?)", "describe": { "columns": [], "parameters": { - "Right": 2 + "Right": 3 }, "nullable": [] }, - "hash": "6c94f167efd5efed0ad4467a8e70242c67cce76460cee23eb2a86c62954bad34" + "hash": "cf4cdc732b5ba571226bc23da16ba557d6c04b2d7f24b55f0cea3f5e8438f7c8" } diff --git a/node/migrations/20241108164533_archive.sql b/node/migrations/20241108164533_archive.sql index e88ea9769..1badd26fb 100644 --- a/node/migrations/20241108164533_archive.sql +++ b/node/migrations/20241108164533_archive.sql @@ -28,9 +28,11 @@ CREATE TABLE finalized_blocks ( id INTEGER PRIMARY KEY NOT NULL, block_height INTEGER NOT NULL, block_hash TEXT NOT NULL, + phoenix_present INTEGER NOT NULL, UNIQUE (block_height, block_hash) check(id = block_height) + check(phoenix_present IN (0, 1)) ) STRICT; CREATE UNIQUE INDEX block_height_idx ON finalized_blocks (block_height); diff --git a/node/src/archive/sqlite.rs b/node/src/archive/sqlite.rs index 3ec570e08..1d5717ed7 100644 --- a/node/src/archive/sqlite.rs +++ b/node/src/archive/sqlite.rs @@ -203,6 +203,25 @@ impl Archive { Ok(r.is_some()) } + + /// Gives you the next block height that contains a phoenix event from a + /// given starting block height + pub async fn next_phoenix(&self, block_height: i64) -> Result> { + let mut conn = self.sqlite_archive.acquire().await?; + + let r = sqlx::query!( + r#"SELECT block_height FROM finalized_blocks WHERE block_height > ? AND phoenix_present = 1"#, + block_height + ) + .fetch_optional(&mut *conn) + .await?; + + if let Some(record) = r { + Ok(Some(record.block_height as u64)) + } else { + Ok(None) + } + } } /// Mutating methods for the SQLite Archive @@ -283,8 +302,16 @@ impl Archive { .fetch_unfinalized_events_by_hash(hex_block_hash) .await?; - // Group events by origin (block height, TxHash) & throw away the ones - // that don't have an origin + /* + Cases of phoenix transfers that produced `Notes` as output: + 1. Any PhoenixTransactionEvent (through notes & refund_note) + */ + let phoenix_event_present = events.iter().any(|event| { + event.event.target.0 == execution_core::transfer::TRANSFER_CONTRACT + && event.event.topic == execution_core::transfer::PHOENIX_TOPIC + }); + + // Group events by origin (block height, OriginHash) let grouped_events = transformer::group_by_origins( events, finalized_block_height as u64, @@ -294,8 +321,8 @@ impl Archive { // this data to another table sqlx::query!( - r#"INSERT INTO finalized_blocks (block_height, block_hash) VALUES (?, ?)"#, - finalized_block_height, hex_block_hash + r#"INSERT INTO finalized_blocks (block_height, block_hash, phoenix_present) VALUES (?, ?, ?)"#, + finalized_block_height, hex_block_hash, phoenix_event_present ).execute(&mut *tx).await?; sqlx::query!(