Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions chain-indexer/src/domain/dust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ 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,
},

/// UTXO mapping removed from registration.
MappingRemoved {
cardano_address: CardanoRewardAddress,
cardano_stake_key: CardanoRewardAddress,
dust_address: DustPublicKey,
utxo_id: DustUtxoId,
utxo_index: u32,
Expand Down
26 changes: 13 additions & 13 deletions chain-indexer/src/infra/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -947,30 +947,30 @@ async fn save_dust_registration_events(
}

DustRegistrationEvent::Deregistration {
cardano_address,
cardano_stake_key,
dust_address,
} => {
let query = indoc! {"
UPDATE cnight_registrations
SET valid = $1,
removed_at = $2,
block_id = $3
WHERE cardano_address = $4
WHERE cardano_stake_key = $4
AND dust_address = $5
"};

sqlx::query(query)
.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,
Expand All @@ -979,34 +979,34 @@ 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,
..
} => {
let query = indoc! {"
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?;
Expand Down
8 changes: 4 additions & 4 deletions chain-indexer/src/infra/subxt_node/runtimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion indexer-api/src/infra/storage/dust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions indexer-common/migrations/postgres/001_initial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,16 @@ 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,
removed_at BIGINT,
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);
Original file line number Diff line number Diff line change
@@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for migration, hence, we don't need operations and codes in this file.

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);
6 changes: 3 additions & 3 deletions indexer-common/migrations/sqlite/001_initial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,16 @@ 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,
removed_at INTEGER,
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);
43 changes: 43 additions & 0 deletions indexer-common/migrations/sqlite/003_rename_cardano_address.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--------------------------------------------------------------------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not create new migration files before mainnet launch (that's my understanding).
Instead, update the existing 001_initial.sql files only.

Please remove:

  • indexer-common/migrations/postgres/003_rename_cardano_address.sql
  • indexer-common/migrations/sqlite/003_rename_cardano_address.sql

Keep: Your changes to 001_initial.sql files are correct.

-- 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 (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every code in this file is not needed. they are redundant.

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);
Loading