Skip to content

Commit

Permalink
node: add phoenix info to finalized blocks
Browse files Browse the repository at this point in the history
- Change SQLite Schema
- Add SQLite function to retrieve next block that includes phoenix events
- Update queries
  • Loading branch information
Neotamandua committed Dec 4, 2024
1 parent c5f6fcc commit ce15620
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.

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

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

2 changes: 2 additions & 0 deletions node/migrations/20241108164533_archive.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
35 changes: 31 additions & 4 deletions node/src/archive/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<u64>> {
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
Expand Down Expand Up @@ -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,
Expand All @@ -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!(
Expand Down

0 comments on commit ce15620

Please sign in to comment.