Skip to content

maikershq/IQ_database_contract

 
 

Repository files navigation

🧠 IQ Database System (On-Chain Structured DB)

A lightweight, fully on-chain database system built with Anchor on Solana.
This program allows you to create table structures, store data as transactions, and record immutable edit history through a rolling hash mechanism — enabling verifiable state tracking even hundreds of years later.


✨ Core Concepts

1. Root PDA (initialize_root)

  • Initializes the root database object.
  • Stores table_names: Vec<Vec<u8>>, which acts as the catalog of all tables created by a user.
  • Designed for dynamic updates — adding or editing tables will update this root structure.
  • With table name length ≤ 15, 100 tables can be stored for ~0.012 SOL rent.

2. Table Creation (create_table)

  • Creates a new table PDA with the following seeds:
b"iqdb-table",
crate::ID.as_ref(),
root.key().as_ref(),
table_name.as_ref(),
  • Adds the table name to root.table_names.
  • Stores column definitions as Vec<Vec<u8>> inside the table PDA.
  • Also initializes an instruction table PDA (table_name + b"instruction") for tracking edit operations.

3. Writing Data (write_data)

  • Writes data into the table PDA.
  • Uses the rolling hash system to keep a verifiable chain of updates in tx_ref.

4. Database Instructions (database_instruction)

  • Handles Update and Delete operations on table data.
  • Records changes in both:
    • Table PDA (actual data change)
    • Instruction PDA (operation log)

This ensures the data change and its context are both on-chain.


🌀 Rolling Hash System

Every change updates a lightweight rolling hash stored in a TxRef PDA:

keccak(domain || prev_hash || new_tx)
  • seq: Sequential counter for each update
  • last_tx: The most recent transaction ID
  • rolling: 32-byte keccak hash chaining all changes together

This creates a permanent, tamper-evident on-chain record of all updates.
Even 300 years later, the entire DB history can be verified by re-hashing transactions in order.


🧱 PDA Layout

PDA Type Seeds
Root ["iqdb-root", program_id, user_pubkey]
Table ["iqdb-table", program_id, root_pubkey, table_name]
Instruction Table ["iqdb-table", program_id, root_pubkey, table_name, "instruction"]
TxRef (content) ["iqdb-txref", program_id, user_pubkey]
TxRef (target) ["iqdb-txref", program_id, user_pubkey, "target"]

📌 Program Entry Points

Function Purpose
initialize_root Initialize the root PDA for a user
create_table Create a table PDA and register it in the root
write_data Insert new row data into a table
database_instruction Perform Update/Delete and log in instruction PDA

🧰 Data Structures

Root

pub struct Root {
    pub creator: Pubkey,
    pub table_names: Vec<Vec<u8>>,
}

Table

pub struct Table {
    pub column_names: Vec<Vec<u8>>,
}

TxRef

pub struct TxRef {
    pub seq: u64,
    pub last_tx: Vec<u8>,
    pub rolling: [u8; 32],
}

🚀 Deployment

Make sure Solana test validator is running:

solana-test-validator

Then deploy with Anchor:

anchor deploy

🧪 Example Flow

# 1. Initialize root
anchor test -- --nocapture

# 2. Create a table with columns
# (Use your TS or Rust client to call create_table)

# 3. Write data (insert rows)
# 4. Use database_instruction to update/delete rows
# 5. Verify history by reading TxRef rolling hash chain

🌐 Why This Matters

This system is designed for long-term, trustless data preservation.
Unlike typical off-chain DBs, here every change is cryptographically chained and stored on Solana, making your application’s history verifiable even in centuries to come.


🧠 Author: Zo & SpaceBun
⚡ Solana + Anchor + Rolling Hash DB

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 93.2%
  • TypeScript 6.8%