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

Merged
merged 34 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
5856138
refactor: rename schema to asset
apskhem Nov 5, 2024
54d8e5f
refactor: vector asset
apskhem Nov 6, 2024
1d34de4
chore: rename asset fields
apskhem Nov 6, 2024
3e0beab
refactor: object mapping structs
apskhem Nov 6, 2024
353c60d
Merge branch 'main' into fix/db-native-asset
apskhem Nov 6, 2024
fa4724f
chore: minor rename
apskhem Nov 6, 2024
b164da4
fix: update operation cql
apskhem Nov 6, 2024
f69a246
fix: schema version
apskhem Nov 6, 2024
af08354
Merge branch 'main' into fix/db-native-asset
apskhem Nov 6, 2024
4b7991f
chore: change asset_id back to policy_id
apskhem Nov 7, 2024
3f69697
Merge branch 'main' into fix/db-native-asset
apskhem Nov 7, 2024
73ae09a
chore: find rename
apskhem Nov 7, 2024
e582ddd
fix: schema version
apskhem Nov 7, 2024
d7faa23
Merge branch 'main' into fix/db-native-asset
apskhem Nov 11, 2024
53585ae
Merge branch 'main' into fix/db-native-asset
minikin Nov 13, 2024
9fbd95e
Merge branch 'main' into fix/db-native-asset
apskhem Nov 17, 2024
9070e75
Merge branch 'main' into fix/db-native-asset
apskhem Nov 21, 2024
54bf98f
fix: i128
apskhem Nov 25, 2024
a6fb377
feat: asset value from i128
apskhem Nov 25, 2024
cad90d2
refactor: change &[u8] for asset name
apskhem Nov 25, 2024
2e6c4e6
refactor: try from asset value
apskhem Nov 25, 2024
6bb5193
Merge branch 'main' into fix/db-native-asset
apskhem Nov 25, 2024
85fde9e
fix: import
apskhem Nov 25, 2024
4c85eb3
chore: fmtfix
apskhem Nov 25, 2024
b96018b
Update catalyst-gateway/bin/src/db/index/block/txo/insert_txo_asset.rs
apskhem Nov 26, 2024
c3982b9
revert: i128 to bigint
apskhem Nov 26, 2024
95b0a4b
fix: unused import
apskhem Nov 26, 2024
14a1877
Merge branch 'main' into fix/db-native-asset
stevenj Nov 26, 2024
6f70a55
Merge branch 'main' into fix/db-native-asset
apskhem Nov 28, 2024
515f4ef
Merge branch 'main' into fix/db-native-asset
stevenj Nov 29, 2024
6f6e929
feat: api test
apskhem Nov 29, 2024
7371c5b
chore: cspell fix
apskhem Nov 29, 2024
45fae68
chore: cspell fix
apskhem Nov 29, 2024
1dcdd06
chore: fmtfix
apskhem Nov 29, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ INSERT INTO txo_assets_by_stake (
slot_no,
txn,
txo,
policy_id,
policy_name,
asset_id,
asset_name,
value
) VALUES (
:stake_address,
:slot_no,
:txn,
:txo,
:policy_id,
:policy_name,
:asset_id,
:asset_name,
:value
);
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
INSERT INTO unstaked_txo_assets_by_txn_hash (
txn_hash,
txo,
policy_id,
policy_name,
asset_id,
asset_name,
slot_no,
txn,
value
) VALUES (
:txn_hash,
:txo,
:policy_id,
:policy_name,
:asset_id,
:asset_name,
:slot_no,
:txn,
:value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ pub(super) struct Params {
/// Transaction Output Offset inside the transaction.
txo: i16,
/// Policy hash of the asset
policy_id: Vec<u8>,
asset_id: Vec<u8>,
/// Policy name of the asset
apskhem marked this conversation as resolved.
Show resolved Hide resolved
apskhem marked this conversation as resolved.
Show resolved Hide resolved
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, asset_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_id: asset_id.to_vec(),
asset_name: asset_name.as_bytes().to_vec(),
apskhem marked this conversation as resolved.
Show resolved Hide resolved
value: value.into(),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ pub(super) struct Params {
/// Transaction Output Offset inside the transaction.
txo: i16,
/// Policy hash of the asset
policy_id: Vec<u8>,
asset_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, asset_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_id: asset_id.to_vec(),
asset_name: asset_name.as_bytes().to_vec(),
apskhem marked this conversation as resolved.
Show resolved Hide resolved
slot_no: slot_no.into(),
txn,
value: value.into(),
Expand Down
12 changes: 6 additions & 6 deletions catalyst-gateway/bin/src/db/index/block/txo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ impl TxoInsertQuery {
}

for asset in txo.non_ada_assets() {
let policy_id = asset.policy().to_vec();
let asset_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();
apskhem marked this conversation as resolved.
Show resolved Hide resolved
let value = policy_asset.any_coin();

if staked {
Expand All @@ -190,17 +190,17 @@ impl TxoInsertQuery {
slot_no,
txn,
txo_index,
&policy_id,
&policy_name,
&asset_id,
&asset_name,
value,
);
self.staked_txo_asset.push(params);
} else {
let params = insert_unstaked_txo_asset::Params::new(
txn_hash,
txo_index,
&policy_id,
&policy_name,
&asset_id,
&asset_name,
slot_no,
txn,
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ SELECT
txn,
txo,
slot_no,
policy_id,
policy_name,
asset_id,
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 @@ -52,9 +52,9 @@ mod result {
/// TXO transaction slot number.
pub slot_no: num_bigint::BigInt,
/// Asset hash.
pub policy_id: Vec<u8>,
pub asset_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 @@ -6,12 +6,12 @@ CREATE TABLE IF NOT EXISTS txo_assets_by_stake (
slot_no varint, -- slot number the txo was created in.
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_id blob, -- asset policy hash (id) (28 byte binary hash)
apskhem marked this conversation as resolved.
Show resolved Hide resolved
asset_name text, -- name of the asset policy (UTF8) (32 bytes)
apskhem marked this conversation as resolved.
Show resolved Hide resolved


-- 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, asset_id, asset_name)
);
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ CREATE TABLE IF NOT EXISTS unstaked_txo_assets_by_txn_hash (
-- Primary Key Fields
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_id blob, -- asset policy hash (id) (28 byte binary hash)
asset_name text, -- name of the policy (UTF8) (32 bytes)
apskhem marked this conversation as resolved.
Show resolved Hide resolved

-- 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, asset_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 = "8ab4e07a-26c5-84d2-835c-46344fdc0ad5";

/// 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>,
apskhem marked this conversation as resolved.
Show resolved Hide resolved
/// 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 @@ -184,13 +182,19 @@ 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;
.entry(row.asset_id.clone())
.or_insert_with(Vec::new);
apskhem marked this conversation as resolved.
Show resolved Hide resolved

match entry.iter_mut().find(|x| x.id == row.asset_id) {
Some(item) => item.amount += row.value,
None => {
entry.push(TxoAssetInfo {
id: row.asset_id,
name: row.asset_name,
apskhem marked this conversation as resolved.
Show resolved Hide resolved
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