Skip to content

Commit

Permalink
Handle merge with creators hashing fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasPennie committed Jul 27, 2023
1 parent 3dcd948 commit 4b3ac85
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 43 deletions.
15 changes: 7 additions & 8 deletions digital_asset_types/src/dao/scopes/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,16 +307,12 @@ pub async fn get_by_id(
pub async fn fetch_transactions(
conn: &impl ConnectionTrait,
tree: Vec<u8>,
leaf_id: Option<i64>,
leaf_id: i64,
pagination: &Pagination,
limit: u64,
) -> Result<Vec<Vec<String>>, DbErr> {
let mut stmt = cl_audits::Entity::find()
.filter(cl_audits::Column::Tree.eq(tree));

if let Some(id) = leaf_id {
stmt = stmt.filter(cl_audits::Column::LeafIdx.eq(id));
}
let mut stmt = cl_audits::Entity::find().filter(cl_audits::Column::Tree.eq(tree));
stmt = stmt.filter(cl_audits::Column::LeafIdx.eq(leaf_id));
stmt = stmt.order_by(cl_audits::Column::CreatedAt, sea_orm::Order::Desc);

stmt = paginate(pagination, limit, stmt);
Expand Down Expand Up @@ -364,7 +360,10 @@ pub async fn get_signatures_for_asset(
if tree.is_empty() {
return Err(DbErr::Custom("Empty tree for asset".to_string()));
}
let transactions = fetch_transactions(conn, tree, asset.nonce, pagination, limit).await?;
let leaf_id = asset
.nonce
.ok_or(DbErr::RecordNotFound("Leaf ID does not exist".to_string()))?;
let transactions = fetch_transactions(conn, tree, leaf_id, pagination, limit).await?;
Ok(transactions)
} else {
Ok(Vec::new())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ where
upsert_asset_with_leaf_info(
txn,
id_bytes.to_vec(),
Some(le.leaf_hash.to_vec()),
Some(seq as i64),
le.leaf_hash.to_vec(),
None,
None,
seq as i64,
false,
)
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,14 @@ where
};

// Partial update of asset table with just leaf.
// TODO: Handle data/creator hash updates (in PR).
upsert_asset_with_leaf_info(
txn,
id_bytes.to_vec(),
Some(le.leaf_hash.to_vec()),
Some(seq as i64),
le.leaf_hash.to_vec(),
None,
None,
seq as i64,
false,
)
.await?;
Expand All @@ -61,6 +64,7 @@ where

upsert_asset_with_seq(txn, id_bytes.to_vec(), seq as i64).await?;

// TODO: Handle unverificaiton.
if verify {
if let Some(Payload::SetAndVerifyCollection { collection }) =
parsing_result.payload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@ use blockbuster::{
programs::bubblegum::{BubblegumInstruction, LeafSchema, Payload},
};
use digital_asset_types::dao::asset_creators;
use digital_asset_types::dao::{asset, asset_creators};
use log::{debug, info};
use mpl_bubblegum::{
hash_creators, hash_metadata,
state::metaplex_adapter::{Creator, MetadataArgs},
};
use log::debug;
use mpl_bubblegum::{hash_creators, hash_metadata, state::metaplex_adapter::Creator};
use sea_orm::{ConnectionTrait, Set, TransactionTrait};
use sea_orm::{ConnectionTrait, Set, TransactionTrait, Unchanged};

use crate::{
error::IngesterError,
Expand All @@ -37,7 +32,7 @@ where
&parsing_result.tree_update,
&parsing_result.payload,
) {
let (creator, verify, creator_hash, data_hash, metadata) = match payload {
let (creator, verify, _, _, metadata) = match payload {
Payload::CreatorVerification {
creator,
verify,
Expand Down Expand Up @@ -100,12 +95,14 @@ where
} else {
Some(delegate.to_bytes().to_vec())
};
// Partial update of asset table with just leaf.
// Partial update of asset table with just leaf info.
upsert_asset_with_leaf_info(
txn,
id_bytes.to_vec(),
Some(le.leaf_hash.to_vec()),
Some(seq as i64),
le.leaf_hash.to_vec(),
Some(updated_data_hash),
Some(updated_creator_hash),
seq as i64,
false,
)
.await?;
Expand Down
57 changes: 51 additions & 6 deletions nft_ingester/src/program_transformers/bubblegum/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,24 +146,35 @@ where
pub async fn upsert_asset_with_leaf_info<T>(
txn: &T,
id: Vec<u8>,
leaf: Option<Vec<u8>>,
seq: Option<i64>,
leaf: Vec<u8>,
data_hash: Option<String>,
creator_hash: Option<String>,
seq: i64,
was_decompressed: bool,
) -> Result<(), IngesterError>
where
T: ConnectionTrait + TransactionTrait,
{
let model = asset::ActiveModel {
let mut model = asset::ActiveModel {
id: Set(id),
leaf: Set(leaf),
leaf_seq: Set(seq),
leaf: Set(Some(leaf)),
leaf_seq: Set(Some(seq)),
..Default::default()
};
let mut columns_to_update = vec![asset::Column::Leaf, asset::Column::LeafSeq];
if data_hash.is_some() {
model.data_hash = Set(data_hash);
columns_to_update.push(asset::Column::DataHash)
}
if creator_hash.is_none() {
model.creator_hash = Set(creator_hash);
columns_to_update.push(asset::Column::CreatorHash)
}

let mut query = asset::Entity::insert(model)
.on_conflict(
OnConflict::column(asset::Column::Id)
.update_columns([asset::Column::Leaf, asset::Column::LeafSeq])
.update_columns(columns_to_update)
.to_owned(),
)
.build(DbBackend::Postgres);
Expand All @@ -184,6 +195,40 @@ where
Ok(())
}

pub async fn upsert_asset_with_leaf_info_for_decompression<T>(
txn: &T,
id: Vec<u8>,
) -> Result<(), IngesterError>
where
T: ConnectionTrait + TransactionTrait,
{
let model = asset::ActiveModel {
id: Set(id),
leaf: Set(None),
leaf_seq: Set(None),
data_hash: Set(None),
creator_hash: Set(None),
..Default::default()
};
let query = asset::Entity::insert(model)
.on_conflict(
OnConflict::column(asset::Column::Id)
.update_columns([
asset::Column::Leaf,
asset::Column::LeafSeq,
asset::Column::DataHash,
asset::Column::CreatorHash,
])
.to_owned(),
)
.build(DbBackend::Postgres);
txn.execute(query)
.await
.map_err(|db_err| IngesterError::StorageWriteError(db_err.to_string()))?;

Ok(())
}

pub async fn upsert_asset_with_owner_and_delegate_info<T>(
txn: &T,
id: Vec<u8>,
Expand Down
8 changes: 4 additions & 4 deletions nft_ingester/src/program_transformers/bubblegum/decompress.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{
error::IngesterError,
program_transformers::bubblegum::{
upsert_asset_with_compression_info, upsert_asset_with_leaf_info,
},
program_transformers::bubblegum::upsert_asset_with_leaf_info_for_decompression,
};
use blockbuster::{instruction::InstructionBundle, programs::bubblegum::BubblegumInstruction};
use sea_orm::{query::*, ConnectionTrait};

use super::upsert_asset_with_compression_info;

pub async fn decompress<'c, T>(
_parsing_result: &BubblegumInstruction,
bundle: &InstructionBundle<'c>,
Expand All @@ -18,7 +18,7 @@ where
let id_bytes = bundle.keys.get(3).unwrap().0.as_slice();

// Partial update of asset table with just leaf.
upsert_asset_with_leaf_info(txn, id_bytes.to_vec(), None, None, true).await?;
upsert_asset_with_leaf_info_for_decompression(txn, id_bytes.to_vec()).await?;
upsert_asset_with_compression_info(
txn,
id_bytes.to_vec(),
Expand Down
6 changes: 4 additions & 2 deletions nft_ingester/src/program_transformers/bubblegum/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ where
upsert_asset_with_leaf_info(
txn,
id_bytes.to_vec(),
Some(le.leaf_hash.to_vec()),
Some(seq as i64),
le.leaf_hash.to_vec(),
None,
None,
seq as i64,
false,
)
.await?;
Expand Down
10 changes: 6 additions & 4 deletions nft_ingester/src/program_transformers/bubblegum/mint_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ where
royalty_amount: Set(metadata.seller_fee_basis_points as i32), //basis points
asset_data: Set(Some(id_bytes.to_vec())),
slot_updated: Set(Some(slot_i)),
data_hash: Set(Some(data_hash)),
creator_hash: Set(Some(creator_hash)),
data_hash: Set(Some(data_hash.clone())),
creator_hash: Set(Some(creator_hash.clone())),
..Default::default()
};

Expand Down Expand Up @@ -204,8 +204,10 @@ where
upsert_asset_with_leaf_info(
txn,
id_bytes.to_vec(),
Some(le.leaf_hash.to_vec()),
Some(seq as i64),
le.leaf_hash.to_vec(),
Some(data_hash),
Some(creator_hash),
seq as i64,
false,
)
.await?;
Expand Down
6 changes: 4 additions & 2 deletions nft_ingester/src/program_transformers/bubblegum/redeem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ where
upsert_asset_with_leaf_info(
txn,
id_bytes.to_vec(),
Some(vec![0; 32]),
Some(seq as i64),
vec![0; 32],
Some(bs58::encode(vec![0; 32]).into_string()),
Some(bs58::encode(vec![0; 32]).into_string()),
seq as i64,
false,
)
.await?;
Expand Down
6 changes: 4 additions & 2 deletions nft_ingester/src/program_transformers/bubblegum/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ where
upsert_asset_with_leaf_info(
txn,
id_bytes.to_vec(),
Some(le.leaf_hash.to_vec()),
Some(seq as i64),
le.leaf_hash.to_vec(),
None,
None,
seq as i64,
false,
)
.await?;
Expand Down

0 comments on commit 4b3ac85

Please sign in to comment.