diff --git a/chain-indexer/src/domain/dust.rs b/chain-indexer/src/domain/dust.rs index e924a87c..000d0a45 100644 --- a/chain-indexer/src/domain/dust.rs +++ b/chain-indexer/src/domain/dust.rs @@ -16,21 +16,21 @@ use indexer_common::domain::{CardanoRewardAddress, DustPublicKey, DustUtxoId}; /// Domain representation of DUST registration events from the NativeTokenObservation pallet. #[derive(Debug, Clone, PartialEq)] pub enum DustRegistrationEvent { - /// Cardano address registered with DUST address. + /// Cardano stake key registered with DUST address. Registration { - cardano_address: CardanoRewardAddress, + cardano_stake_key: CardanoRewardAddress, dust_address: DustPublicKey, }, - /// Cardano address deregistered from DUST address. + /// Cardano stake key deregistered from DUST address. Deregistration { - cardano_address: CardanoRewardAddress, + cardano_stake_key: CardanoRewardAddress, dust_address: DustPublicKey, }, /// UTXO mapping added for registration. MappingAdded { - cardano_address: CardanoRewardAddress, + cardano_stake_key: CardanoRewardAddress, dust_address: DustPublicKey, utxo_id: DustUtxoId, utxo_index: u32, @@ -38,7 +38,7 @@ pub enum DustRegistrationEvent { /// UTXO mapping removed from registration. MappingRemoved { - cardano_address: CardanoRewardAddress, + cardano_stake_key: CardanoRewardAddress, dust_address: DustPublicKey, utxo_id: DustUtxoId, utxo_index: u32, diff --git a/chain-indexer/src/infra/storage.rs b/chain-indexer/src/infra/storage.rs index c763dc70..c2b6fd74 100644 --- a/chain-indexer/src/infra/storage.rs +++ b/chain-indexer/src/infra/storage.rs @@ -916,19 +916,19 @@ async fn save_dust_registration_events( for event in events { match event { DustRegistrationEvent::Registration { - cardano_address, + cardano_stake_key, dust_address, } => { let query = indoc! {" INSERT INTO cnight_registrations ( - cardano_address, + cardano_stake_key, dust_address, valid, registered_at, block_id ) VALUES ($1, $2, $3, $4, $5) - ON CONFLICT (cardano_address, dust_address) + ON CONFLICT (cardano_stake_key, dust_address) DO UPDATE SET valid = EXCLUDED.valid, registered_at = EXCLUDED.registered_at, @@ -937,7 +937,7 @@ async fn save_dust_registration_events( "}; sqlx::query(query) - .bind(cardano_address.as_ref()) + .bind(cardano_stake_key.as_ref()) .bind(dust_address.as_ref()) .bind(true) .bind(block_timestamp as i64) @@ -947,7 +947,7 @@ async fn save_dust_registration_events( } DustRegistrationEvent::Deregistration { - cardano_address, + cardano_stake_key, dust_address, } => { let query = indoc! {" @@ -955,7 +955,7 @@ async fn save_dust_registration_events( SET valid = $1, removed_at = $2, block_id = $3 - WHERE cardano_address = $4 + WHERE cardano_stake_key = $4 AND dust_address = $5 "}; @@ -963,14 +963,14 @@ async fn save_dust_registration_events( .bind(false) .bind(block_timestamp as i64) .bind(block_id) - .bind(cardano_address.as_ref()) + .bind(cardano_stake_key.as_ref()) .bind(dust_address.as_ref()) .execute(&mut **tx) .await?; } DustRegistrationEvent::MappingAdded { - cardano_address, + cardano_stake_key, dust_address, utxo_id, utxo_index, @@ -979,21 +979,21 @@ async fn save_dust_registration_events( UPDATE cnight_registrations SET utxo_tx_hash = $1, utxo_output_index = $2 - WHERE cardano_address = $3 + WHERE cardano_stake_key = $3 AND dust_address = $4 "}; sqlx::query(query) .bind(utxo_id.as_ref()) .bind(*utxo_index as i64) - .bind(cardano_address.as_ref()) + .bind(cardano_stake_key.as_ref()) .bind(dust_address.as_ref()) .execute(&mut **tx) .await?; } DustRegistrationEvent::MappingRemoved { - cardano_address, + cardano_stake_key, dust_address, .. } => { @@ -1001,12 +1001,12 @@ async fn save_dust_registration_events( UPDATE cnight_registrations SET utxo_tx_hash = NULL, utxo_output_index = NULL - WHERE cardano_address = $1 + WHERE cardano_stake_key = $1 AND dust_address = $2 "}; sqlx::query(query) - .bind(cardano_address.as_ref()) + .bind(cardano_stake_key.as_ref()) .bind(dust_address.as_ref()) .execute(&mut **tx) .await?; diff --git a/chain-indexer/src/infra/subxt_node/runtimes.rs b/chain-indexer/src/infra/subxt_node/runtimes.rs index 37e4ceb2..5f118042 100644 --- a/chain-indexer/src/infra/subxt_node/runtimes.rs +++ b/chain-indexer/src/infra/subxt_node/runtimes.rs @@ -196,21 +196,21 @@ async fn make_block_details_runtime_0_18( Event::CNightObservation(native_token_event) => match native_token_event { CnightObservationEvent::Registration(event) => { dust_registration_events.push(DustRegistrationEvent::Registration { - cardano_address: event.cardano_reward_address.0.into(), + cardano_stake_key: event.cardano_reward_address.0.into(), dust_address: event.dust_public_key.0.0.into(), }); } CnightObservationEvent::Deregistration(event) => { dust_registration_events.push(DustRegistrationEvent::Deregistration { - cardano_address: event.cardano_reward_address.0.into(), + cardano_stake_key: event.cardano_reward_address.0.into(), dust_address: event.dust_public_key.0.0.into(), }); } CnightObservationEvent::MappingAdded(event) => { dust_registration_events.push(DustRegistrationEvent::MappingAdded { - cardano_address: event.cardano_reward_address.0.into(), + cardano_stake_key: event.cardano_reward_address.0.into(), dust_address: event.dust_public_key.0.0.into(), utxo_id: event.utxo_tx_hash.0.as_ref().into(), utxo_index: event.utxo_index.into(), @@ -219,7 +219,7 @@ async fn make_block_details_runtime_0_18( CnightObservationEvent::MappingRemoved(event) => { dust_registration_events.push(DustRegistrationEvent::MappingRemoved { - cardano_address: event.cardano_reward_address.0.into(), + cardano_stake_key: event.cardano_reward_address.0.into(), dust_address: event.dust_public_key.0.0.into(), utxo_id: event.utxo_tx_hash.0.as_ref().into(), utxo_index: event.utxo_index.into(), diff --git a/indexer-api/src/infra/storage/dust.rs b/indexer-api/src/infra/storage/dust.rs index dc5029e5..9383dcb6 100644 --- a/indexer-api/src/infra/storage/dust.rs +++ b/indexer-api/src/infra/storage/dust.rs @@ -42,7 +42,7 @@ impl DustStorage for Storage { let registration_query = indoc! {" SELECT dust_address, valid, utxo_tx_hash, utxo_output_index FROM cnight_registrations - WHERE cardano_address = $1 + WHERE cardano_stake_key = $1 AND removed_at IS NULL ORDER BY registered_at DESC LIMIT 1 diff --git a/indexer-common/migrations/postgres/001_initial.sql b/indexer-common/migrations/postgres/001_initial.sql index 37ef5283..ed9bd7f0 100644 --- a/indexer-common/migrations/postgres/001_initial.sql +++ b/indexer-common/migrations/postgres/001_initial.sql @@ -164,7 +164,7 @@ CREATE INDEX ON dust_generation_info (night_utxo_hash); -- cNIGHT registration tracking CREATE TABLE cnight_registrations ( id BIGSERIAL PRIMARY KEY, - cardano_address BYTEA NOT NULL, + cardano_stake_key BYTEA NOT NULL, dust_address BYTEA NOT NULL, valid BOOLEAN NOT NULL, registered_at BIGINT NOT NULL, @@ -172,8 +172,8 @@ CREATE TABLE cnight_registrations ( block_id BIGINT REFERENCES blocks (id), utxo_tx_hash BYTEA, utxo_output_index BIGINT, - UNIQUE (cardano_address, dust_address) + UNIQUE (cardano_stake_key, dust_address) ); -CREATE INDEX ON cnight_registrations (cardano_address); +CREATE INDEX ON cnight_registrations (cardano_stake_key); CREATE INDEX ON cnight_registrations (dust_address); CREATE INDEX ON cnight_registrations (block_id); diff --git a/indexer-common/migrations/postgres/003_rename_cardano_address.sql b/indexer-common/migrations/postgres/003_rename_cardano_address.sql new file mode 100644 index 00000000..73a0e4b5 --- /dev/null +++ b/indexer-common/migrations/postgres/003_rename_cardano_address.sql @@ -0,0 +1,22 @@ +-------------------------------------------------------------------------------- +-- Migration: Rename cardano_address to cardano_stake_key for clarity +-- Issue: #440 - The column name was confusing as it actually stores a Cardano +-- stake key (reward address), not a regular Cardano address. +-------------------------------------------------------------------------------- + +-- Rename the column in cnight_registrations table +ALTER TABLE cnight_registrations + RENAME COLUMN cardano_address TO cardano_stake_key; + +-- Drop and recreate the index with the new column name +DROP INDEX IF EXISTS cnight_registrations_cardano_address_idx; +CREATE INDEX ON cnight_registrations (cardano_stake_key); + +-- Update the unique constraint +-- Note: PostgreSQL automatically updates the constraint when the column is renamed, +-- but we recreate it for clarity and to ensure the constraint name reflects the change. +ALTER TABLE cnight_registrations + DROP CONSTRAINT IF EXISTS cnight_registrations_cardano_address_dust_address_key; +ALTER TABLE cnight_registrations + ADD CONSTRAINT cnight_registrations_cardano_stake_key_dust_address_key + UNIQUE (cardano_stake_key, dust_address); diff --git a/indexer-common/migrations/sqlite/001_initial.sql b/indexer-common/migrations/sqlite/001_initial.sql index a56811b2..e1c888f1 100644 --- a/indexer-common/migrations/sqlite/001_initial.sql +++ b/indexer-common/migrations/sqlite/001_initial.sql @@ -165,7 +165,7 @@ CREATE INDEX dust_generation_info_night_utxo_hash_idx ON dust_generation_info (n -- cNIGHT registration tracking CREATE TABLE cnight_registrations ( id INTEGER PRIMARY KEY, - cardano_address BLOB NOT NULL, + cardano_stake_key BLOB NOT NULL, dust_address BLOB NOT NULL, valid BOOLEAN NOT NULL, registered_at INTEGER NOT NULL, @@ -173,8 +173,8 @@ CREATE TABLE cnight_registrations ( block_id INTEGER REFERENCES blocks (id), utxo_tx_hash BLOB, utxo_output_index INTEGER, - UNIQUE (cardano_address, dust_address) + UNIQUE (cardano_stake_key, dust_address) ); -CREATE INDEX cnight_registrations_cardano_address_idx ON cnight_registrations (cardano_address); +CREATE INDEX cnight_registrations_cardano_stake_key_idx ON cnight_registrations (cardano_stake_key); CREATE INDEX cnight_registrations_dust_address_idx ON cnight_registrations (dust_address); CREATE INDEX cnight_registrations_block_id_idx ON cnight_registrations (block_id); diff --git a/indexer-common/migrations/sqlite/003_rename_cardano_address.sql b/indexer-common/migrations/sqlite/003_rename_cardano_address.sql new file mode 100644 index 00000000..b9589ffc --- /dev/null +++ b/indexer-common/migrations/sqlite/003_rename_cardano_address.sql @@ -0,0 +1,43 @@ +-------------------------------------------------------------------------------- +-- Migration: Rename cardano_address to cardano_stake_key for clarity +-- Issue: #440 - The column name was confusing as it actually stores a Cardano +-- stake key (reward address), not a regular Cardano address. +-------------------------------------------------------------------------------- + +-- SQLite does not support RENAME COLUMN directly in older versions, +-- so we need to recreate the table with the new column name. + +-- Step 1: Create a new table with the correct column name +CREATE TABLE cnight_registrations_new ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + cardano_stake_key BLOB NOT NULL, + dust_address BLOB NOT NULL, + valid INTEGER NOT NULL, + registered_at INTEGER NOT NULL, + removed_at INTEGER, + block_id INTEGER REFERENCES blocks (id), + utxo_tx_hash BLOB, + utxo_output_index INTEGER, + UNIQUE (cardano_stake_key, dust_address) +); + +-- Step 2: Copy data from the old table to the new table +INSERT INTO cnight_registrations_new ( + id, cardano_stake_key, dust_address, valid, registered_at, removed_at, + block_id, utxo_tx_hash, utxo_output_index +) +SELECT + id, cardano_address, dust_address, valid, registered_at, removed_at, + block_id, utxo_tx_hash, utxo_output_index +FROM cnight_registrations; + +-- Step 3: Drop the old table +DROP TABLE cnight_registrations; + +-- Step 4: Rename the new table to the original name +ALTER TABLE cnight_registrations_new RENAME TO cnight_registrations; + +-- Step 5: Recreate indexes with the new column name +CREATE INDEX cnight_registrations_cardano_stake_key_idx ON cnight_registrations (cardano_stake_key); +CREATE INDEX cnight_registrations_dust_address_idx ON cnight_registrations (dust_address); +CREATE INDEX cnight_registrations_block_id_idx ON cnight_registrations (block_id);