Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cat-gateway): Fix native asset indexing to be more flexible #1150

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ INSERT INTO txo_assets_by_stake (
txn,
txo,
policy_id,
policy_name,
asset_name,
value
) VALUES (
:stake_address,
:slot_no,
:txn,
:txo,
:policy_id,
:policy_name,
:asset_name,
:value
);
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ INSERT INTO unstaked_txo_assets_by_txn_hash (
txn_hash,
txo,
policy_id,
policy_name,
asset_name,
slot_no,
txn,
value
) VALUES (
:txn_hash,
:txo,
:policy_id,
:policy_name,
:asset_name,
:slot_no,
:txn,
:value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(super) struct Params {
/// Policy hash of the asset
policy_id: Vec<u8>,
/// Policy name of the asset
policy_name: String,
asset_name: Vec<u8>,
/// Value of the asset
value: num_bigint::BigInt,
}
Expand All @@ -40,16 +40,16 @@ impl Params {
/// values.
#[allow(clippy::too_many_arguments)]
pub(super) fn new(
stake_address: &[u8], slot_no: u64, txn: i16, txo: i16, policy_id: &[u8],
policy_name: &str, value: i128,
stake_address: &[u8], slot_no: u64, txn: i16, txo: i16, policy_id: &[u8], asset_name: &str,
value: i128,
) -> Self {
Self {
stake_address: stake_address.to_vec(),
slot_no: slot_no.into(),
txn,
txo,
policy_id: policy_id.to_vec(),
policy_name: policy_name.to_owned(),
asset_name: asset_name.as_bytes().to_vec(),
value: value.into(),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(super) struct Params {
/// Policy hash of the asset
policy_id: Vec<u8>,
/// Policy name of the asset
policy_name: String,
asset_name: Vec<u8>,
/// Block Slot Number
slot_no: num_bigint::BigInt,
/// Transaction Offset inside the block.
Expand All @@ -40,14 +40,14 @@ impl Params {
/// values.
#[allow(clippy::too_many_arguments)]
pub(super) fn new(
txn_hash: &[u8], txo: i16, policy_id: &[u8], policy_name: &str, slot_no: u64, txn: i16,
txn_hash: &[u8], txo: i16, policy_id: &[u8], asset_name: &str, slot_no: u64, txn: i16,
value: i128,
) -> Self {
Self {
txn_hash: txn_hash.to_vec(),
txo,
policy_id: policy_id.to_vec(),
policy_name: policy_name.to_owned(),
asset_name: asset_name.as_bytes().to_vec(),
slot_no: slot_no.into(),
txn,
value: value.into(),
Expand Down
6 changes: 3 additions & 3 deletions catalyst-gateway/bin/src/db/index/block/txo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl TxoInsertQuery {
let policy_id = asset.policy().to_vec();
for policy_asset in asset.assets() {
if policy_asset.is_output() {
let policy_name = policy_asset.to_ascii_name().unwrap_or_default();
let asset_name = policy_asset.to_ascii_name().unwrap_or_default();
let value = policy_asset.any_coin();

if staked {
Expand All @@ -191,7 +191,7 @@ impl TxoInsertQuery {
txn,
txo_index,
&policy_id,
&policy_name,
&asset_name,
value,
);
self.staked_txo_asset.push(params);
Expand All @@ -200,7 +200,7 @@ impl TxoInsertQuery {
txn_hash,
txo_index,
&policy_id,
&policy_name,
&asset_name,
slot_no,
txn,
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SELECT
txo,
slot_no,
policy_id,
policy_name,
asset_name,
value
FROM txo_assets_by_stake
WHERE stake_address = :stake_address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ mod result {
/// Asset hash.
pub policy_id: Vec<u8>,
/// Asset name.
pub policy_name: String,
pub asset_name: Vec<u8>,
/// Asset value.
pub value: num_bigint::BigInt,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ CREATE TABLE IF NOT EXISTS txo_assets_by_stake (
txn smallint, -- Which Transaction in the Slot is the TXO.
txo smallint, -- offset in the txo list of the transaction the txo is in.
policy_id blob, -- asset policy hash (id) (28 byte binary hash)
policy_name text, -- name of the policy (UTF8) TODO: https://github.com/input-output-hk/catalyst-voices/issues/1121
asset_name blob, -- name of the asset policy (UTF8) (32 bytes)


-- None Key Data of the asset.
value varint, -- Value of the asset (i128)

PRIMARY KEY (stake_address, slot_no, txn, txo, policy_id, policy_name)
PRIMARY KEY (stake_address, slot_no, txn, txo, policy_id, asset_name)
);
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CREATE TABLE IF NOT EXISTS unstaked_txo_assets_by_txn_hash (
txn_hash blob, -- 32 byte hash of this transaction.
txo smallint, -- offset in the txo list of the transaction the txo is in.
policy_id blob, -- asset policy hash (id) (28 byte binary hash)
policy_name text, -- name of the policy (UTF8)
asset_name blob, -- name of the policy (UTF8) (32 bytes)

-- Secondary Location information for the transaction.
slot_no varint, -- slot number the txo was created in.
Expand All @@ -13,5 +13,5 @@ CREATE TABLE IF NOT EXISTS unstaked_txo_assets_by_txn_hash (
-- Value of the asset.
value varint, -- Value of the asset (u64)

PRIMARY KEY (txn_hash, txo, policy_id, policy_name)
PRIMARY KEY (txn_hash, txo, policy_id, asset_name)
);
2 changes: 1 addition & 1 deletion catalyst-gateway/bin/src/db/index/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{settings::cassandra_db, utils::blake2b_hash::generate_uuid_string_fr
/// change accidentally, and is NOT to be used directly to set the schema version of the
/// table namespaces.
#[allow(dead_code)]
const SCHEMA_VERSION: &str = "08193dfe-698a-8177-bdf8-20c5691a06e7";
const SCHEMA_VERSION: &str = "75ae6ac9-ddd8-8472-8a7a-8676d04f8679";

/// Keyspace Create (Templated)
const CREATE_NAMESPACE_CQL: &str = include_str!("./cql/namespace.cql");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ struct TxoAssetInfo {
/// Asset hash.
id: Vec<u8>,
/// Asset name.
// TODO: https://github.com/input-output-hk/catalyst-voices/issues/1121
name: String,
name: Vec<u8>,
/// Asset amount.
amount: num_bigint::BigInt,
}
Expand All @@ -99,8 +98,7 @@ struct TxoInfo {
/// Whether the TXO was spent.
spent_slot_no: Option<num_bigint::BigInt>,
/// TXO assets.
// TODO: https://github.com/input-output-hk/catalyst-voices/issues/1121
assets: HashMap<Vec<u8>, TxoAssetInfo>,
assets: HashMap<Vec<u8>, Vec<TxoAssetInfo>>,
}

/// Calculate the stake info for a given stake address.
Expand Down Expand Up @@ -185,12 +183,18 @@ async fn get_txo_by_txn(
let entry = txo_info
.assets
.entry(row.policy_id.clone())
.or_insert(TxoAssetInfo {
id: row.policy_id,
name: row.policy_name,
amount: num_bigint::BigInt::ZERO,
});
entry.amount += row.value;
.or_insert_with(Vec::new);

match entry.iter_mut().find(|item| item.id == row.policy_id) {
Some(item) => item.amount += row.value,
None => {
entry.push(TxoAssetInfo {
id: row.policy_id,
name: row.asset_name,
amount: row.value,
});
},
}
}

let mut txos_by_txn = HashMap::new();
Expand Down Expand Up @@ -273,7 +277,7 @@ fn build_stake_info(
stake_info.ada_amount +=
i64::try_from(txo_info.value).map_err(|err| anyhow!(err))?;

for asset in txo_info.assets.into_values() {
for asset in txo_info.assets.into_values().flatten() {
stake_info.native_tokens.push(StakedNativeTokenInfo {
policy_hash: asset.id.try_into()?,
asset_name: asset.name.into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ impl Example for AssetName {
}
}

// TODO: https://github.com/input-output-hk/catalyst-voices/issues/1121
impl From<Vec<u8>> for AssetName {
fn from(value: Vec<u8>) -> Self {
match String::from_utf8(value.clone()) {
Expand Down
Loading