File size: 3,158 Bytes
037c74d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0610845
 
 
 
 
 
 
 
 
 
037c74d
0610845
037c74d
 
0610845
037c74d
0610845
037c74d
 
0610845
037c74d
0610845
037c74d
 
0610845
037c74d
0610845
037c74d
 
 
 
 
 
0610845
037c74d
0610845
037c74d
 
 
 
 
 
 
0610845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
037c74d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Dict, Any
from core.nuclear_intelligence import NuclearIntelligenceCore
from blockchain.virtual_ledger import VirtualLedger
from core.operation_loop import OperationLoop
import uvicorn

app = FastAPI(title="Nuclear Intelligence API", version="1.0.0")

# Shared components (initialized in main)
core = None
ledger = None
op_loop = None

def init_components(c, l, o):
    global core, ledger, op_loop
    core = c
    ledger = l
    op_loop = o

@app.get("/health")
def health():
    return {"status": "healthy"}

@app.get("/status")
def get_status():
    return {
        "status": "active",
        "nes_supply": ledger.nes_supply if ledger else 0,
        "blocks": len(ledger.chain) if ledger else 0,
        "loop_running": op_loop.is_running if op_loop else False
    }

@app.get("/api/v1/blockchain/state")
def get_blockchain():
    if not ledger: raise HTTPException(status_code=503, detail="Ledger not initialized")
    return [block.to_dict() for block in ledger.chain]

@app.get("/api/v1/blockchain/balance/{address}")
def get_balance(address: str):
    if not ledger: raise HTTPException(status_code=503, detail="Ledger not initialized")
    return {"address": address, "balance": ledger.get_balance(address)}

@app.get("/api/v1/knowledge/base")
def get_kg():
    if not core: raise HTTPException(status_code=503, detail="Core not initialized")
    return core.kg.graph

@app.post("/api/v1/operations/cycle")
def trigger_cycle():
    if not op_loop: raise HTTPException(status_code=503, detail="Operation loop not initialized")
    try:
        result = op_loop.run_cycle()
        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/v1/operations/stats")
def get_stats():
    if not (ledger and core and op_loop): raise HTTPException(status_code=503, detail="System not fully initialized")
    return {
        "nes_supply": ledger.nes_supply,
        "blocks": len(ledger.chain),
        "entities": len(core.kg.graph["entities"]),
        "history_count": len(op_loop.history)
    }

@app.get("/api/v1/blockchain/chain")
def get_full_chain():
    if not ledger: raise HTTPException(status_code=503, detail="Ledger not initialized")
    return [block.to_dict() for block in ledger.chain]

@app.post("/api/v1/blockchain/mine")
def manual_mine():
    if not ledger: raise HTTPException(status_code=503, detail="Ledger not initialized")
    block = ledger.mine_pending_transactions()
    if block:
        return block.to_dict()
    return {"message": "No pending transactions to mine"}

@app.get("/api/v1/operations/cycles")
def get_cycle_history():
    if not op_loop: raise HTTPException(status_code=503, detail="Operation loop not initialized")
    return op_loop.history

@app.post("/api/v1/system/verify-integrity")
def verify_integrity():
    if not ledger: raise HTTPException(status_code=503, detail="Ledger not initialized")
    is_valid = ledger.is_chain_valid()
    return {"is_valid": is_valid}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)