```
chore: refactor logging module, update genesis timestamp, remove model relationships, and reorganize routers - Rename logging.py to logger.py and update import paths in poa.py and main.py - Update devnet genesis timestamp to 1766828620 - Remove SQLModel Relationship declarations from Block, Transaction, and Receipt models - Add SessionDep type alias and get_session dependency in coordinator-api deps - Reorganize coordinator-api routers: replace explorer/registry with exchange, users, marketplace
This commit is contained in:
54
apps/blockchain-node/create_genesis.py
Normal file
54
apps/blockchain-node/create_genesis.py
Normal file
@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Simple script to create genesis block
|
||||
"""
|
||||
|
||||
import sys
|
||||
sys.path.insert(0, 'src')
|
||||
|
||||
from aitbc_chain.database import session_scope, init_db
|
||||
from aitbc_chain.models import Block
|
||||
from datetime import datetime
|
||||
import hashlib
|
||||
|
||||
def compute_block_hash(height: int, parent_hash: str, timestamp: datetime) -> str:
|
||||
"""Compute block hash"""
|
||||
data = f"{height}{parent_hash}{timestamp}".encode()
|
||||
return hashlib.sha256(data).hexdigest()
|
||||
|
||||
def create_genesis():
|
||||
"""Create the genesis block"""
|
||||
print("Creating genesis block...")
|
||||
|
||||
# Initialize database
|
||||
init_db()
|
||||
|
||||
# Check if genesis already exists
|
||||
with session_scope() as session:
|
||||
existing = session.exec(select(Block).order_by(Block.height.desc()).limit(1)).first()
|
||||
if existing:
|
||||
print(f"Genesis block already exists: #{existing.height}")
|
||||
return
|
||||
|
||||
# Create genesis block
|
||||
timestamp = datetime.utcnow()
|
||||
genesis_hash = compute_block_hash(0, "0x00", timestamp)
|
||||
genesis = Block(
|
||||
height=0,
|
||||
hash=genesis_hash,
|
||||
parent_hash="0x00",
|
||||
proposer="ait-devnet-proposer",
|
||||
timestamp=timestamp,
|
||||
tx_count=0,
|
||||
state_root=None,
|
||||
)
|
||||
session.add(genesis)
|
||||
session.commit()
|
||||
print(f"Genesis block created: #{genesis.height}")
|
||||
print(f"Hash: {genesis.hash}")
|
||||
print(f"Proposer: {genesis.proposer}")
|
||||
print(f"Timestamp: {genesis.timestamp}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
from sqlmodel import select
|
||||
create_genesis()
|
||||
Binary file not shown.
@ -19,5 +19,5 @@
|
||||
"fee_per_byte": 1,
|
||||
"mint_per_unit": 1000
|
||||
},
|
||||
"timestamp": 1766400877
|
||||
"timestamp": 1766828620
|
||||
}
|
||||
|
||||
53
apps/blockchain-node/init_genesis.py
Normal file
53
apps/blockchain-node/init_genesis.py
Normal file
@ -0,0 +1,53 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Initialize genesis block for AITBC blockchain
|
||||
"""
|
||||
|
||||
import sys
|
||||
sys.path.insert(0, 'src')
|
||||
|
||||
from dataclasses import dataclass
|
||||
from aitbc_chain.database import session_scope
|
||||
from aitbc_chain.models import Block
|
||||
from aitbc_chain.consensus.poa import PoAProposer, ProposerConfig
|
||||
from datetime import datetime
|
||||
|
||||
def init_genesis():
|
||||
"""Initialize the genesis block"""
|
||||
print("Initializing genesis block...")
|
||||
|
||||
# Check if genesis already exists
|
||||
with session_scope() as session:
|
||||
existing = session.exec(select(Block).order_by(Block.height.desc()).limit(1)).first()
|
||||
if existing:
|
||||
print(f"Genesis block already exists: #{existing.height}")
|
||||
return
|
||||
|
||||
# Create proposer config
|
||||
config = ProposerConfig(
|
||||
chain_id="ait-devnet",
|
||||
proposer_id="ait-devnet-proposer",
|
||||
interval_seconds=2,
|
||||
)
|
||||
|
||||
# Create proposer and initialize genesis
|
||||
proposer = PoAProposer(config=config, session_factory=session_scope)
|
||||
|
||||
# The _ensure_genesis_block method is called during proposer initialization
|
||||
# but we need to trigger it manually
|
||||
proposer._ensure_genesis_block()
|
||||
|
||||
print("Genesis block created successfully!")
|
||||
|
||||
# Verify
|
||||
with session_scope() as session:
|
||||
genesis = session.exec(select(Block).where(Block.height == 0)).first()
|
||||
if genesis:
|
||||
print(f"Genesis block: #{genesis.height}")
|
||||
print(f"Hash: {genesis.hash}")
|
||||
print(f"Proposer: {genesis.proposer}")
|
||||
print(f"Timestamp: {genesis.timestamp}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
from sqlmodel import select
|
||||
init_genesis()
|
||||
@ -9,7 +9,7 @@ from typing import Callable, ContextManager, Optional
|
||||
|
||||
from sqlmodel import Session, select
|
||||
|
||||
from ..logging import get_logger
|
||||
from ..logger import get_logger
|
||||
from ..metrics import metrics_registry
|
||||
|
||||
|
||||
|
||||
@ -7,7 +7,7 @@ from typing import Optional
|
||||
from .config import settings
|
||||
from .consensus import PoAProposer, ProposerConfig
|
||||
from .database import init_db, session_scope
|
||||
from .logging import get_logger
|
||||
from .logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@ from pydantic import field_validator
|
||||
from sqlalchemy import Column
|
||||
from sqlalchemy.types import JSON
|
||||
from sqlmodel import Field, Relationship, SQLModel
|
||||
from sqlalchemy.orm import Mapped
|
||||
|
||||
_HEX_PATTERN = re.compile(r"^(0x)?[0-9a-fA-F]+$")
|
||||
|
||||
@ -34,9 +35,6 @@ class Block(SQLModel, table=True):
|
||||
tx_count: int = 0
|
||||
state_root: Optional[str] = None
|
||||
|
||||
transactions: list["Transaction"] = Relationship(back_populates="block")
|
||||
receipts: list["Receipt"] = Relationship(back_populates="block")
|
||||
|
||||
@field_validator("hash", mode="before")
|
||||
@classmethod
|
||||
def _hash_is_hex(cls, value: str) -> str:
|
||||
@ -69,8 +67,6 @@ class Transaction(SQLModel, table=True):
|
||||
)
|
||||
created_at: datetime = Field(default_factory=datetime.utcnow, index=True)
|
||||
|
||||
block: Optional["Block"] = Relationship(back_populates="transactions")
|
||||
|
||||
@field_validator("tx_hash", mode="before")
|
||||
@classmethod
|
||||
def _tx_hash_is_hex(cls, value: str) -> str:
|
||||
@ -101,8 +97,6 @@ class Receipt(SQLModel, table=True):
|
||||
minted_amount: Optional[int] = None
|
||||
recorded_at: datetime = Field(default_factory=datetime.utcnow, index=True)
|
||||
|
||||
block: Optional["Block"] = Relationship(back_populates="receipts")
|
||||
|
||||
@field_validator("receipt_id", mode="before")
|
||||
@classmethod
|
||||
def _receipt_id_is_hex(cls, value: str) -> str:
|
||||
|
||||
Reference in New Issue
Block a user