Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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

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

This file was deleted.

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

13 changes: 9 additions & 4 deletions crates/sage-database/src/tables/assets/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ impl Database {
}

impl DatabaseTx<'_> {
pub async fn vault_launcher_ids(&mut self) -> Result<Vec<Bytes32>> {
vault_launcher_ids(&mut *self.tx).await
}

pub async fn is_vault(&mut self, launcher_id: Bytes32) -> Result<bool> {
is_vault(&mut *self.tx, launcher_id).await
}
Expand All @@ -100,7 +104,8 @@ impl DatabaseTx<'_> {
vault_launcher_id(&mut *self.tx, coin_id).await
}

/// Adds a coin to a vault's lineage. A known custody hash is never replaced by an unknown one.
/// Adds a coin to a vault's lineage, if the coin is known. A known custody hash is never
/// replaced by an unknown one.
pub async fn insert_vault_coin(
&mut self,
coin_id: Bytes32,
Expand Down Expand Up @@ -184,12 +189,12 @@ async fn insert_vault_coin(
query!(
"
INSERT INTO vault_coins (coin_id, custody_hash)
VALUES ((SELECT id FROM coins WHERE hash = ?), ?)
SELECT id, ? FROM coins WHERE hash = ?
ON CONFLICT(coin_id) DO UPDATE SET
custody_hash = COALESCE(excluded.custody_hash, custody_hash)
",
coin_id,
custody_hash
custody_hash,
coin_id
)
.execute(conn)
.await?;
Expand Down
64 changes: 64 additions & 0 deletions crates/sage-database/src/tables/p2_puzzles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,20 @@ impl DatabaseTx<'_> {
delete_external_p2_puzzle(&mut *self.tx, p2_puzzle_hash).await
}

/// The vault address with the puzzle hash, if it's one of ours.
pub async fn p2_singleton(&mut self, p2_puzzle_hash: Bytes32) -> Result<Option<P2Singleton>> {
optional_p2_singleton(&mut *self.tx, p2_puzzle_hash).await
}

/// The highest nonce of a vault's addresses, and the highest nonce of an address that has
/// received a coin, if any.
pub async fn p2_singleton_nonces(
&mut self,
launcher_id: Bytes32,
) -> Result<(Option<u32>, Option<u32>)> {
p2_singleton_nonces(&mut *self.tx, launcher_id).await
}

/// Inserts a vault address. It isn't marked as synced, so it's synced from the start of the
/// chain.
pub async fn insert_p2_singleton_p2_puzzle(&mut self, p2_singleton: P2Singleton) -> Result<()> {
Expand Down Expand Up @@ -741,6 +755,56 @@ async fn p2_singleton(
))
}

async fn optional_p2_singleton(
conn: impl SqliteExecutor<'_>,
p2_puzzle_hash: Bytes32,
) -> Result<Option<P2Singleton>> {
let p2_puzzle_hash = p2_puzzle_hash.as_ref();

query!(
"
SELECT launcher_id, nonce
FROM p2_puzzles
INNER JOIN p2_singletons ON p2_singletons.p2_puzzle_id = p2_puzzles.id
WHERE p2_puzzles.hash = ?
",
p2_puzzle_hash
)
.fetch_optional(conn)
.await?
.map(|row| {
Ok(P2Singleton::new(
row.launcher_id.convert()?,
row.nonce.try_into()?,
))
})
.transpose()
}

async fn p2_singleton_nonces(
conn: impl SqliteExecutor<'_>,
launcher_id: Bytes32,
) -> Result<(Option<u32>, Option<u32>)> {
let launcher_id = launcher_id.as_ref();

let row = query!(
"
SELECT
MAX(p2_singletons.nonce) AS \"max_nonce?: i64\",
MAX(CASE WHEN EXISTS (
SELECT 1 FROM coins WHERE coins.p2_puzzle_id = p2_singletons.p2_puzzle_id
) THEN p2_singletons.nonce END) AS \"max_used_nonce?: i64\"
FROM p2_singletons
WHERE p2_singletons.launcher_id = ?
",
launcher_id
)
.fetch_one(conn)
.await?;

Ok((row.max_nonce.convert()?, row.max_used_nonce.convert()?))
}

async fn p2_singleton_puzzle_hashes(
conn: impl SqliteExecutor<'_>,
launcher_id: Bytes32,
Expand Down
Loading
Loading