How 0xpolygon/polygon-edge works
Project Type and Purpose
• This is a full-stack, modular Ethereum‐compatible blockchain client (“Polygon Edge”) implemented in Go.
• It provides:
• A CLI (“command” package) for chain setup, genesis, bridge operations, secrets management, etc.
• A P2P network layer for block and transaction gossip (“network” package).
• A consensus engine supporting IBFT and PolyBFT (“consensus” package).
• A JSON-RPC and gRPC API server (“jsonrpc” and “server” packages).
• State execution and storage (EVM, trie, snapshot) (“state,” “blockchain,” “txpool,” “syncer”).
• Secret management and key handling (“secrets,” “bls,” “crypto”).
Key Architectural Components
A. CLI Layer (“command”)
• Entry point (main.go) parses user commands → calls specific sub‐commands (genesis, bridge, server, secrets, etc.).
B. API Server Layer
• JSON-RPC endpoints (jsonrpc/*) expose Eth, Net, Web3, Txpool, Bridge RPCs over HTTP.
• gRPC “system” and consensus operator services (server/proto, consensus/ibft/proto, consensus/polybft/proto).
C. P2P Network Layer (“network”)
• Discovery, Dialer, Gossip modules over libp2p.
• Bootnodes, peer management, message/event handling.
D. Consensus Engines (“consensus”)
• Pluggable backends: IBFT, PolyBFT, dummy/dev.
• Block builder, proposer calculator, state sync relayer.
E. Sync & Mempool
• Syncer fetches new blocks from peers via gRPC (syncer/proto) or P2P.
• TxPool manages pending transactions, event subscriptions, and reorg handling.
F. State & Execution (“state”)
• Immutable Merkle‐Patricia trie for state storage.
• EVM interpreter, precompiles, tracer.
• Transaction application and fork handling.
G. Blockchain Storage (“blockchain”)
• LevelDB/memory storage for blocks, receipts, validators.
• Batch writer abstraction.
H. Crypto & Validators
• BLS, ECDSA key management and signatures.
• Validator set, snapshots, contract fetchers.
I. Secrets Management (“secrets”)
• AWS SSM, GCP SSM, Vault, local keystore backends.
Relationships and Data Flows
• CLI invokes → API Server or direct backends (e.g., genesis builder).
• API Server ←→ Consensus Engine: to propose and seal blocks, manage state sync.
• Consensus Engine ↔ P2P Network: gossip proposals, votes, state sync commitments.
• Syncer ↔ P2P Network: request and receive block data.
• Consensus Engine & Syncer → Blockchain Storage: commit blocks.
• State Engine reads/writes State Trie → stored by storage backends.
• TxPool receives new transactions from JSON-RPC and network gossip → passes them to state/execution for validation, forwards via network.
• Secrets Manager supplies keys for signing at consensus and transaction signing.
Architectural Patterns & Principles
• Modular, plug-in engines (consensus, storage).
• Clear separation of concerns: CLI vs. API vs. engine vs. storage vs. network.
• Interface‐driven Go packages for easy swapping (e.g., StorageBackend interface, ConsensusBackend).
• Asynchronous event handling (network events, txpool subscriptions).
• gRPC and JSON-RPC coexist for operator and user APIs.