How projectpai/paicoin works
Project Type and Purpose
• This is a full-stack, open-source blockchain node and wallet application (PAI Coin Core) written primarily in C++.
• It implements a hybrid PoW/PoS consensus engine, peer-to-peer networking, transaction and script validation, UTXO storage (via LevelDB), and both CLI/RPC and Qt GUI front-ends for end users.
Main Architectural Components
a. P2P Network Layer
• Peer discovery & connection management (src/net.cpp/net.h)
• Message parsing/serialization (protocol.h, netmessagemaker.h)
• Network processing & relaying (src/net_processing.cpp/net_processing.h)
b. Consensus & Validation Engine
• Block and transaction verification (src/consensus, src/validation.cpp)
• PoW and PoS hybrid consensus (src/pow.cpp, src/stake/)
• Mempool management (src/txmempool.cpp)
• Script interpreter (src/script/)
c. Storage Layer
• UTXO set and block index (src/coins.cpp/coins.h, src/txdb.cpp)
• Underlying database (LevelDB in src/leveldb/)
• Block/chain parameters and config (src/chainparams.cpp)
d. Wallet & Key Management
• Wallet DB and key store (src/wallet/db.cpp, src/wallet/keystore.cpp)
• RPC wallet interface (src/wallet/rpcwallet.cpp)
• Fee estimation & coin selection (src/wallet/fees.cpp, src/policy/)
• Staking and ticket management (src/wallet/auto-voter, src/wallet/ticket-buyer)
e. RPC & HTTP Server
• JSON-RPC over HTTP(S) (src/httpserver.cpp, src/httprpc.cpp)
• UniValue JSON library (src/univalue/)
• REST endpoints (src/rest.cpp)
• ZeroMQ notifications (src/zmq/)
f. Front-End Clients
• CLI tools (paicoind, paicoin-cli, paicoin-tx)
• Qt GUI wallet (src/qt/…)
g. Auxiliary Modules
• Crypto primitives (src/crypto/, secp256k1/)
• Utilities & configuration (src/util*, src/init.cpp)
• Testing harness (src/test/, test/functional/)
Relationships & Data Flow
• Incoming P2P messages → Network Layer → Net Processing → Validation Engine → Storage Layer
• Transactions submitted via CLI/RPC/Qt → RPC Server → Validation → Mempool → P2P Broadcast
• Mining module → pulls transactions from Mempool → builds block template → PoW miner
• Staking module → selects tickets from wallet → signs & submits via P2P
• Wallet GUI/CLI → RPC client → RPC Server (could be local IPC)
• ZeroMQ/REST subscribers → receive notifications on consensus events (new block, new TX)
Architectural Patterns & Principles
• Layered architecture: Network, Consensus/Validation, Storage, API, UI
• Modular design: clear separation of P2P code, consensus code, wallet code, GUI code
• Plugin-like optional components: ZeroMQ, REST, LevelDB, secp256k1 can be enabled/disabled at build
• Threaded concurrency: networking, validation, scheduler (src/scheduler.cpp), wallet IO run in separate threads