fix(sync): resolve multi-chain gossip block propagation and sync across nodes
This commit is contained in:
@@ -10,7 +10,7 @@ from datetime import datetime
|
||||
import hashlib
|
||||
|
||||
def compute_block_hash(chain_id: str, height: int, parent_hash: str, timestamp: datetime) -> str:
|
||||
data = f"{chain_id}{height}{parent_hash}{timestamp}".encode()
|
||||
data = f"{chain_id}{height}{parent_hash}{timestamp.isoformat()}".encode()
|
||||
return "0x" + hashlib.sha256(data).hexdigest()
|
||||
|
||||
def create_genesis(chain_id: str):
|
||||
@@ -19,17 +19,18 @@ def create_genesis(chain_id: str):
|
||||
with session_scope() as session:
|
||||
existing = session.exec(select(Block).where(Block.chain_id == chain_id).order_by(Block.height.desc()).limit(1)).first()
|
||||
if existing:
|
||||
print(f"Genesis block already exists for {chain_id}: #{existing.height}")
|
||||
print(f"Genesis block already exists for {chain_id}: #{existing.height} (hash: {existing.hash})")
|
||||
return
|
||||
|
||||
timestamp = datetime.utcnow()
|
||||
# Use a deterministic timestamp so all nodes agree on the hash
|
||||
timestamp = datetime(2025, 1, 1, 0, 0, 0)
|
||||
genesis_hash = compute_block_hash(chain_id, 0, "0x00", timestamp)
|
||||
genesis = Block(
|
||||
chain_id=chain_id,
|
||||
height=0,
|
||||
hash=genesis_hash,
|
||||
parent_hash="0x00",
|
||||
proposer=f"{chain_id}-proposer",
|
||||
proposer="genesis",
|
||||
timestamp=timestamp,
|
||||
tx_count=0,
|
||||
state_root=None,
|
||||
@@ -39,9 +40,9 @@ def create_genesis(chain_id: str):
|
||||
print(f"Genesis block created for {chain_id}: #{genesis.height}")
|
||||
print(f"Hash: {genesis.hash}")
|
||||
print(f"Proposer: {genesis.proposer}")
|
||||
print(f"Timestamp: {genesis.timestamp}")
|
||||
print(f"Timestamp: {genesis.timestamp.isoformat()}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
init_db()
|
||||
for chain in ["ait-testnet", "ait-devnet"]:
|
||||
for chain in ["ait-testnet", "ait-devnet", "ait-healthchain"]:
|
||||
create_genesis(chain)
|
||||
|
||||
31
dev/tests/test_sync.py
Normal file
31
dev/tests/test_sync.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import asyncio
|
||||
import httpx
|
||||
import time
|
||||
|
||||
async def main():
|
||||
async with httpx.AsyncClient() as client:
|
||||
print("Submitting transaction to aitbc (testnet)...")
|
||||
tx_data = {
|
||||
"type": "transfer",
|
||||
"sender": "0xTEST_SENDER",
|
||||
"nonce": int(time.time()),
|
||||
"fee": 1,
|
||||
"payload": {"amount": 100, "recipient": "0xTEST_RECIPIENT"},
|
||||
"sig": "0xSIG"
|
||||
}
|
||||
resp = await client.post("http://10.1.223.93:8082/rpc/sendTx?chain_id=ait-testnet", json=tx_data)
|
||||
print("aitbc response:", resp.status_code, resp.text)
|
||||
|
||||
print("Waiting 5 seconds for gossip propagation and block proposing...")
|
||||
await asyncio.sleep(5)
|
||||
|
||||
print("Checking head on aitbc...")
|
||||
resp = await client.get("http://10.1.223.93:8082/rpc/head?chain_id=ait-testnet")
|
||||
print("aitbc head:", resp.status_code, resp.json())
|
||||
|
||||
print("Checking head on aitbc1...")
|
||||
resp = await client.get("http://10.1.223.40:8082/rpc/head?chain_id=ait-testnet")
|
||||
print("aitbc1 head:", resp.status_code, resp.json())
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user