Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Jan 24, 2025
1 parent 7a9075c commit 308a6dd
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 36 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.

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.

This file was deleted.

2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions crates/sage-database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ chia = { workspace = true }
chia-wallet-sdk = { workspace = true }
sqlx = { workspace = true, features = ["sqlite"] }
thiserror = { workspace = true }
hex = { workspace = true }
tracing = { workspace = true }
56 changes: 56 additions & 0 deletions crates/sage-database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod utils;

pub use primitives::*;
pub use rows::*;
use tracing::info;
pub use transactions::*;

pub(crate) use utils::*;
Expand All @@ -32,6 +33,45 @@ impl Database {
let tx = self.pool.begin().await?;
Ok(DatabaseTx::new(tx))
}

pub async fn run_rust_migrations(&self) -> Result<()> {
let mut tx = self.tx().await?;

let version = tx.rust_migration_version().await?;

info!("The current Sage migration version is {version}");

if version == 0 {
info!("Migrating to version 1 (fixed collection id calculation)");

for collection_id in tx.collection_ids().await? {
let collection = tx
.collection(collection_id)
.await?
.expect("collection not found");

tx.update_collection(
collection_id,
CollectionRow {
collection_id: calculate_collection_id(
collection.did_id,
&collection.metadata_collection_id,
),
did_id: collection.did_id,
metadata_collection_id: collection.metadata_collection_id,
visible: collection.visible,
name: collection.name,
icon: collection.icon,
},
)
.await?;
}

tx.set_rust_migration_version(1).await?;
}

Ok(())
}
}

#[derive(Debug)]
Expand All @@ -51,6 +91,22 @@ impl<'a> DatabaseTx<'a> {
pub async fn rollback(self) -> Result<()> {
Ok(self.tx.rollback().await?)
}

pub async fn rust_migration_version(&mut self) -> Result<i64> {
let row = sqlx::query_scalar!("SELECT `version` FROM `rust_migrations` LIMIT 1")
.fetch_one(&mut *self.tx)
.await?;

Ok(row)
}

pub async fn set_rust_migration_version(&mut self, version: i64) -> Result<()> {
sqlx::query!("UPDATE `rust_migrations` SET `version` = ?", version)
.execute(&mut *self.tx)
.await?;

Ok(())
}
}

#[derive(Debug, Error)]
Expand Down
60 changes: 45 additions & 15 deletions crates/sage-database/src/primitives/nfts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use chia::{
protocol::{Bytes32, Program},
puzzles::LineageProof,
sha2::Sha256,
};
use chia_wallet_sdk::{Nft, NftInfo};
use sqlx::SqliteExecutor;
Expand Down Expand Up @@ -47,6 +48,13 @@ pub struct NftSearchParams {
pub name: Option<String>,
}

pub fn calculate_collection_id(did_id: Bytes32, json_collection_id: &str) -> Bytes32 {
let mut hasher = Sha256::new();
hasher.update(hex::encode(did_id));
hasher.update(json_collection_id);
hasher.finalize().into()
}

impl Database {
pub async fn unchecked_nft_uris(&self, limit: u32) -> Result<Vec<NftUri>> {
unchecked_nft_uris(&self.pool, limit).await
Expand Down Expand Up @@ -232,8 +240,12 @@ impl<'a> DatabaseTx<'a> {
insert_collection(&mut *self.tx, row).await
}

pub async fn update_collection(&mut self, row: CollectionRow) -> Result<()> {
update_collection(&mut *self.tx, row).await
pub async fn update_collection(
&mut self,
collection_id: Bytes32,
row: CollectionRow,
) -> Result<()> {
update_collection(&mut *self.tx, collection_id, row).await
}

pub async fn set_nft_not_owned(&mut self, coin_id: Bytes32) -> Result<()> {
Expand All @@ -247,6 +259,10 @@ impl<'a> DatabaseTx<'a> {
) -> Result<()> {
set_nft_created_height(&mut *self.tx, coin_id, height).await
}

pub async fn collection_ids(&mut self) -> Result<Vec<Bytes32>> {
collection_ids(&mut *self.tx).await
}
}

async fn insert_collection(conn: impl SqliteExecutor<'_>, row: CollectionRow) -> Result<()> {
Expand Down Expand Up @@ -280,30 +296,35 @@ async fn insert_collection(conn: impl SqliteExecutor<'_>, row: CollectionRow) ->
Ok(())
}

async fn update_collection(conn: impl SqliteExecutor<'_>, row: CollectionRow) -> Result<()> {
let collection_id = row.collection_id.as_ref();
async fn update_collection(
conn: impl SqliteExecutor<'_>,
collection_id: Bytes32,
row: CollectionRow,
) -> Result<()> {
let collection_id = collection_id.as_ref();
let new_collection_id = row.collection_id.as_ref();
let did_id = row.did_id.as_ref();
let name = row.name.as_deref();
let icon = row.icon.as_deref();

sqlx::query!(
"
REPLACE INTO `collections` (
`collection_id`,
`did_id`,
`metadata_collection_id`,
`visible`,
`name`,
`icon`
)
VALUES (?, ?, ?, ?, ?, ?)
UPDATE `collections` SET
`collection_id` = ?,
`did_id` = ?,
`metadata_collection_id` = ?,
`visible` = ?,
`name` = ?,
`icon` = ?
WHERE `collection_id` = ?
",
collection_id,
new_collection_id,
did_id,
row.metadata_collection_id,
row.visible,
name,
icon
icon,
collection_id
)
.execute(conn)
.await?;
Expand Down Expand Up @@ -1114,3 +1135,12 @@ async fn set_nft_created_height(

Ok(())
}

async fn collection_ids(conn: impl SqliteExecutor<'_>) -> Result<Vec<Bytes32>> {
sqlx::query_scalar!("SELECT `collection_id` FROM `collections`")
.fetch_all(conn)
.await?
.into_iter()
.map(|row| to_bytes32(&row))
.collect()
}
11 changes: 2 additions & 9 deletions crates/sage-wallet/src/utils/offchain_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chia::{protocol::Bytes32, sha2::Sha256};
use chia::protocol::Bytes32;
use sage_assets::{Chip0007Metadata, Collection};
use sage_database::CollectionRow;
use sage_database::{calculate_collection_id, CollectionRow};

#[derive(Debug, Default, Clone)]
pub struct ComputedNftInfo {
Expand Down Expand Up @@ -49,13 +49,6 @@ pub fn compute_nft_info(did_id: Option<Bytes32>, blob: Option<&[u8]>) -> Compute
}
}

fn calculate_collection_id(did_id: Bytes32, json_collection_id: &str) -> Bytes32 {
let mut hasher = Sha256::new();
hasher.update(hex::encode(did_id));
hasher.update(json_collection_id);
hasher.finalize().into()
}

#[cfg(test)]
mod tests {
use hex_literal::hex;
Expand Down
5 changes: 5 additions & 0 deletions migrations/0009_rust_migrations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE `rust_migrations` (
`version` INTEGER PRIMARY KEY
);

INSERT INTO `rust_migrations` (`version`) VALUES (0);

0 comments on commit 308a6dd

Please sign in to comment.