From d22227f748ead07a075022d835e957618f11141b Mon Sep 17 00:00:00 2001 From: Nicolas Pennie Date: Wed, 23 Aug 2023 15:56:16 -0700 Subject: [PATCH] fix: backwards compatibility for old data post out-of-order fix (#116) --- das_api/src/api/mod.rs | 1 + .../src/dao/generated/asset_grouping.rs | 2 +- digital_asset_types/src/dao/scopes/asset.rs | 32 +++++++++++++++---- digital_asset_types/src/dapi/common/asset.rs | 14 +++----- .../src/program_transformers/bubblegum/db.rs | 2 +- .../token_metadata/v1_asset.rs | 6 ++-- 6 files changed, 37 insertions(+), 20 deletions(-) diff --git a/das_api/src/api/mod.rs b/das_api/src/api/mod.rs index 57c479ef2..122306c31 100644 --- a/das_api/src/api/mod.rs +++ b/das_api/src/api/mod.rs @@ -95,6 +95,7 @@ pub struct SearchAssets { #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[serde(deny_unknown_fields, rename_all = "camelCase")] + pub struct GetAssetsByAuthority { pub authority_address: String, pub sort_by: Option, diff --git a/digital_asset_types/src/dao/generated/asset_grouping.rs b/digital_asset_types/src/dao/generated/asset_grouping.rs index d362c4db2..aae51d6d8 100644 --- a/digital_asset_types/src/dao/generated/asset_grouping.rs +++ b/digital_asset_types/src/dao/generated/asset_grouping.rs @@ -20,7 +20,7 @@ pub struct Model { pub group_value: Option, pub seq: Option, pub slot_updated: Option, - pub verified: bool, + pub verified: Option, pub group_info_seq: Option, } diff --git a/digital_asset_types/src/dao/scopes/asset.rs b/digital_asset_types/src/dao/scopes/asset.rs index acebb8580..3d2c50feb 100644 --- a/digital_asset_types/src/dao/scopes/asset.rs +++ b/digital_asset_types/src/dao/scopes/asset.rs @@ -75,7 +75,11 @@ pub async fn get_grouping( Condition::all() .add(asset_grouping::Column::GroupKey.eq(group_key)) .add(asset_grouping::Column::GroupValue.eq(group_value)) - .add(asset_grouping::Column::Verified.eq(true)), + .add( + Condition::any() + .add(asset_grouping::Column::Verified.eq(true)) + .add(asset_grouping::Column::Verified.is_null()), + ), ) .count(conn) .await?; @@ -95,7 +99,11 @@ pub async fn get_by_grouping( let condition = asset_grouping::Column::GroupKey .eq(group_key) .and(asset_grouping::Column::GroupValue.eq(group_value)) - .and(asset_grouping::Column::Verified.eq(true)); + .and( + asset_grouping::Column::Verified + .eq(true) + .or(asset_grouping::Column::Verified.is_null()), + ); get_by_related_condition( conn, Condition::all() @@ -270,7 +278,13 @@ pub async fn get_related_for_assets( let grouping = asset_grouping::Entity::find() .filter(asset_grouping::Column::AssetId.is_in(ids.clone())) .filter(asset_grouping::Column::GroupValue.is_not_null()) - .filter(asset_grouping::Column::Verified.eq(true)) + .filter( + Condition::any() + .add(asset_grouping::Column::Verified.eq(true)) + // Older versions of the indexer did not have the verified flag. A group would be present if and only if it was verified. + // Therefore if verified is null, we can assume that the group is verified. + .add(asset_grouping::Column::Verified.is_null()), + ) .order_by_asc(asset_grouping::Column::AssetId) .all(conn) .await?; @@ -322,7 +336,7 @@ pub async fn get_by_id( let asset_data: (asset::Model, asset_data::Model) = asset_data.one(conn).await.and_then(|o| match o { Some((a, Some(d))) => Ok((a, d)), - _ => Err(DbErr::RecordNotFound("Asset Not Found".to_string())) + _ => Err(DbErr::RecordNotFound("Asset Not Found".to_string())), })?; let (asset, data) = asset_data; @@ -333,13 +347,19 @@ pub async fn get_by_id( .await?; let creators: Vec = asset_creators::Entity::find() .filter(asset_creators::Column::AssetId.eq(asset.id.clone())) - .order_by_asc(asset_creators::Column::AssetId) + .order_by_asc(asset_creators::Column::Position) .all(conn) .await?; let grouping: Vec = asset_grouping::Entity::find() .filter(asset_grouping::Column::AssetId.eq(asset.id.clone())) .filter(asset_grouping::Column::GroupValue.is_not_null()) - .filter(asset_grouping::Column::Verified.eq(true)) + .filter( + Condition::any() + .add(asset_grouping::Column::Verified.eq(true)) + // Older versions of the indexer did not have the verified flag. A group would be present if and only if it was verified. + // Therefore if verified is null, we can assume that the group is verified. + .add(asset_grouping::Column::Verified.is_null()), + ) .order_by_asc(asset_grouping::Column::AssetId) .all(conn) .await?; diff --git a/digital_asset_types/src/dapi/common/asset.rs b/digital_asset_types/src/dapi/common/asset.rs index f37af45a7..61882581e 100644 --- a/digital_asset_types/src/dapi/common/asset.rs +++ b/digital_asset_types/src/dapi/common/asset.rs @@ -348,9 +348,9 @@ pub fn to_grouping(groups: Vec) -> Result, DbE group_key: model.group_key.clone(), group_value: Some( model - .group_value - .clone() - .ok_or(DbErr::Custom("Group value not found".to_string()))?, + .group_value + .clone() + .ok_or(DbErr::Custom("Group value not found".to_string()))?, ), collection_metadata: None, }) @@ -408,12 +408,8 @@ pub fn asset_to_rpc( compression: Some(Compression { eligible: asset.compressible, compressed: asset.compressed, - leaf_id: asset - .nonce - .ok_or(DbErr::Custom("Nonce not found".to_string()))?, - seq: asset - .seq - .ok_or(DbErr::Custom("Seq not found".to_string()))?, + leaf_id: asset.nonce.unwrap_or(0 as i64), + seq: asset.seq.unwrap_or(0 as i64), tree: asset .tree_id .map(|s| bs58::encode(s).into_string()) diff --git a/nft_ingester/src/program_transformers/bubblegum/db.rs b/nft_ingester/src/program_transformers/bubblegum/db.rs index 2828469c5..d12411e67 100644 --- a/nft_ingester/src/program_transformers/bubblegum/db.rs +++ b/nft_ingester/src/program_transformers/bubblegum/db.rs @@ -426,7 +426,7 @@ where asset_id: Set(asset_id), group_key: Set("collection".to_string()), group_value: Set(group_value), - verified: Set(verified), + verified: Set(Some(verified)), slot_updated: Set(Some(slot_updated)), group_info_seq: Set(Some(seq)), ..Default::default() diff --git a/nft_ingester/src/program_transformers/token_metadata/v1_asset.rs b/nft_ingester/src/program_transformers/token_metadata/v1_asset.rs index 112b4bc8d..adacd5736 100644 --- a/nft_ingester/src/program_transformers/token_metadata/v1_asset.rs +++ b/nft_ingester/src/program_transformers/token_metadata/v1_asset.rs @@ -178,8 +178,8 @@ pub async fn save_v1_asset( specification_version: Set(Some(SpecificationVersions::V1)), specification_asset_class: Set(Some(class)), tree_id: Set(None), - nonce: Set(None), - seq: Set(None), + nonce: Set(Some(0)), + seq: Set(Some(0)), leaf: Set(None), compressed: Set(false), compressible: Set(false), @@ -272,7 +272,7 @@ pub async fn save_v1_asset( asset_id: Set(id.to_vec()), group_key: Set("collection".to_string()), group_value: Set(Some(c.key.to_string())), - verified: Set(c.verified), + verified: Set(Some(c.verified)), seq: Set(None), slot_updated: Set(Some(slot_i)), ..Default::default()