Skip to content

Database Structure Optimization and Transaction Data Indexing #9

@will-bitlightlabs

Description

@will-bitlightlabs

Issue Overview

While reviewing the BP Node codebase, I've identified several limitations in the current database structure that need to be addressed to properly support transaction indexing and querying requirements, especially for RGB v0.12's reorg detection capabilities.

Current Implementation

The current database structure defines several tables:

pub const TABLE_MAIN: TableDefinition<&'static str, &[u8]> = TableDefinition::new("main");
pub const TABLE_BLKS: TableDefinition<[u8; 32], DbBlockHeader> = TableDefinition::new("blocks");
pub const TABLE_TXIDS: TableDefinition<[u8; 32], TxNo> = TableDefinition::new("txids");
pub const TABLE_TXES: TableDefinition<TxNo, DbTx> = TableDefinition::new("transactions");
pub const TABLE_OUTS: TableDefinition<TxNo, Vec<TxNo>> = TableDefinition::new("spends");
pub const TABLE_SPKS: TableDefinition<&[u8], TxNo> = TableDefinition::new("scripts");

Currently, the implementation in process_block() only:

  1. Stores block headers in TABLE_BLKS
  2. Maps transaction IDs to internal transaction numbers in TABLE_TXIDS
  3. Stores full transaction data in TABLE_TXES

Issues Identified

1. TABLE_SPKS Design Limitation

The current TABLE_SPKS design maps a script pubkey to only one transaction number:

pub const TABLE_SPKS: TableDefinition<&[u8], TxNo> = TableDefinition::new("scripts");

This design is insufficient because the same address (script pubkey) can receive funds in multiple transactions. The current design only allows indexing a single transaction per script, making it impossible to look up all transactions related to a particular address.

2. Missing Transaction-Block Association

There's no way to quickly determine which block a transaction belongs to, which is essential for determining confirmation status and handling reorgs.

3. Missing UTXO Tracking

The implementation lacks proper UTXO tracking, which is crucial for wallet functionality and reorg handling.

Proposed Solutions

1. Modify TABLE_SPKS Design

pub const TABLE_SPKS: TableDefinition<&[u8], Vec<TxNo>> = TableDefinition::new("scripts");

2. Add Additional Tables

// UTXO index table
pub const TABLE_UTXOS: TableDefinition<(TxNo, u32), ()> = TableDefinition::new("utxos");

// Transaction-block association table
pub const TABLE_TX_BLOCKS: TableDefinition<TxNo, [u8; 32]> = TableDefinition::new("tx_blocks");

// ...etc

I plan to implement the logic required by the adjusted table structure.

These are the points I wanted to confirm with you before development. If there are any omissions or redundancies in my approach, I will promptly correct them. I look forward to your feedback.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions