算法艺术创作
algorithmic-art
通过代码和可调参数创作生成艺术与交互式视觉效果。
blockchain-core【设计与创意】Use this skill when asked about blockchain fundamentals, consensus mechanisms, PoW, PoS, gas, staking, blockchain data structures, DAG consensus, Avalanche, MEV, PBS, economic security, blockchain node implementation. Languages: C++, Go. Covers core protocol engineering including consensus algorithms (Nakamoto, PBFT, HotStuff, Snowman, DAG-BFT), MEV …
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Guide core blockchain protocol engineering covering consensus mechanisms, state machine design, P2P networking, mempool architecture, MEV economics, and node implementation. This skill enables building and analyzing blockchain protocols from first principles.
"blockchain core", "consensus mechanism", "proof of work", "proof of stake", "PoW", "PoS", "DAG consensus", "Avalanche consensus", "Snowman", "Hashgraph", "gas", "staking", "MEV", "miner extractable value", "PBS", "proposer builder separation", "MEV-Boost", "ePBS", "blockchain node", "C++ blockchain", "Go blockchain", "blockchain protocol", "mempool", "transaction pool", "block finality", "blockchain data structure", "merkle tree", "cryptography blockchain", "p2p blockchain", "blockchain state machine", "economic security", "fork choice rule", "finality gadget", "light client"
Protocol architecture specification: consensus mechanism with fork choice rule, state machine design, network layer architecture, mempool policy, security analysis, implementation plan.
5000 tokens
Blockchain type:
├── Permissionless (anyone can validate)
│ ├── Need high throughput (>1000 TPS)?
│ │ ├── YES → DAG-based (Avalanche, Hashgraph, Narwhal)
│ │ └── NO → Nakamoto consensus
│ │ ├── PoW → Bitcoin, Litecoin (energy-intensive, simple)
│ │ └── PoS → Ethereum, Cosmos (energy-efficient, complex)
│ ├── Need fast finality (<1 sec)?
│ │ ├── YES → BFT-based (Tendermint, HotStuff, Jolteon)
│ │ └── NO → Nakamoto probabilistic finality
│ └── Need low node requirements?
│ ├── YES → PoS with light client support
│ └── NO → Full node with archival storage
├── Permissioned (known validators)
│ ├── Crash fault tolerance → Raft, Paxos
│ └── Byzantine fault tolerance → PBFT, IBFT, HotStuff
└── Hybrid → Nominated PoS (Polkadot), Leased PoS (Waves)
├── UTXO model (Bitcoin)
│ ├── Pros: Parallelizable, private, simple validation
│ ├── Cons: State bloat, complex smart contracts
│ └── Use: Value transfer, UTXO-based smart contracts (Cardano)
├── Account model (Ethereum)
│ ├── Pros: Intuitive, composable, efficient smart contracts
│ ├── Cons: Replay protection needed, sequential nonce
│ └── Use: General purpose smart contracts
└── Hybrid (Solana, Aptos)
├── Account-based with parallel execution
└── Requires static read/write set declaration
Finality guarantee needed:
├── Probabilistic (Nakamoto)
│ ├── Finality probability increases with confirmations
│ ├── Bitcoin: 6 confirmations = 99.9% confidence
│ ├── Pros: Simple, partition-resistant
│ └── Cons: No deterministic finality
├── Deterministic instant (BFT)
│ ├── Finality after 2/3+ supermajority
│ ├── Tendermint: 1-3 second finality
│ ├── Pros: Instant finality, predictable
│ └── Cons: Requires validator set, liveness-break if <2/3 online
├── Economic (PoS checkpoints)
│ ├── Finality after economic commitment
│ ├── Ethereum Casper FFG: ~13 min finality
│ └── Pros: Finality without BFT overhead
└── Probabilistic DAG (Avalanche)
├── Very high confidence after 3 rounds
└── ~1 second to confidence >99.99%
Tx ordering strategy:
├── Public mempool (Ethereum, Bitcoin)
│ ├── All pending txs visible to all → MEV extraction
│ ├── Fee-based ordering (gas price auction)
│ └── Policy: RBF, CPFP, eviction
├── Private mempool (Flashbots, Dark Forest)
│ ├── Txs visible only to select validators/builders
│ ├── MEV-protected, privacy of strategy
│ └── Cost: fees to private relay
├── No mempool (Solana, Aurora)
│ ├── Txs forwarded directly to current leader
│ ├── Leader orders txs in block
│ └── Pros: No MEV from frontrunning in mempool
└── Encrypted mempool (threshold encrypted)
├── Txs encrypted, decrypted only at block production
└── Shutter Network, FCFS linear ordering
// Simplified Bitcoin fork choice: most accumulated proof of work
func ForkChoice(blocks []Block) Block {
best := blocks[0]
for _, b := range blocks[1:] {
if b.TotalWork() > best.TotalWork() {
best = b
}
}
return best
}
// Tendermint consensus states: Propose → Pre-vote → Pre-commit → Commit
// 2f+1 validators needed (f = max faulty)
func ConsensusRound(height int64, round int32, state State) Decision {
proposer := SelectProposer(height, round, state.Validators)
block := proposer.Propose(height, round)
prevotes := Broadcast(PrevoteMessage{BlockID: block.ID})
if len(prevotes) > 2*len(state.Validators)/3 {
precommits := Broadcast(PrecommitMessage{BlockID: block.ID})
if len(precommits) > 2*len(state.Validators)/3 {
return Commit{Block: block}
}
}
return Nil // Timeout, next round
}
// HotStuff: 3-chain protocol (prepare → pre-commit → commit)
// Leader-based BFT with linear message complexity O(n)
// Each round has a leader; pipelined for efficiency
// Used in: Diem, Sui (Narwhal/Tusk), Flow
type HotStuffState struct {
View uint64 // Current view number
HighQC QuorumCert // Highest known quorum certificate
PreparedBlock *Block
LockedBlock *Block
}
Trie: Radix tree with 16-ary branching
├── Extension node: common prefix path (hex encoded)
├── Branch node: 16 children + value (17-element node)
├── Leaf node: key suffix + value
└── Root: Merkle hash of entire state tree
Lookup: O(n) where n = key length (not O(log n))
keccak256(account) → traverse trie path
Each node lookup: hash → leveldb read
Proof: O(log n) branch nodes for inclusion proof
Merkle proof size: ~1KB for Ethereum state
Verify: compute hashes up stack, compare to state root
UTXO = map[OutPoint]UTXOEntry
OutPoint: txid (32B) + vout (4B)
UTXOEntry: scriptPubKey + amount + height + coinbase flag
Efficient storage: LevelDB with O(1) lookups
~80M UTXOs on Bitcoin mainnet (~8GB)
UTXO cache: 500MB-2GB RAM for hot UTXOs
Operations:
- Spend: delete UTXO from set (SSTORE in account models ≠ delete)
- Create: add new UTXO entries
- Coinbase maturity: 100 blocks before coinbase UTXO can be spent
type Mempool struct {
byFee *heap.MinHeap // Sorted by fee rate (sat/vB or gwei/gas)
byTime *list.List // Sorted by entry time
byTxID map[Hash]*Tx // O(1) lookup by transaction hash
ancestors map[Hash][]Hash // CPFP ancestor sets
descendants map[Hash][]Hash // CPFP descendant sets
}
// Bitcoin total mempool size: ~300MB (default maxmempool=300MB)
// Ethereum pending tx count: ~10,000-50,000 typical
// Eviction policy:
// Bitcoin: by descendant fee rate (lowest first)
// Ethereum: by gas price (lowest first, price bump for replacement)
PoW: Cost to attack = hashrate * electricity + hardware cost
Bitcoin: ~$300K/hour to sustain 51% attack (rented hashrate)
Defense: economic disincentive (attack destroys coin value)
PoS: Cost to attack = 1/3 of total staked value (slashed)
Ethereum: ~$30B staked → attack cost ~$10B (slashed)
Defense: inactivity leak, social consensus for recovery
Economic finality:
Finality when reorganization cost exceeds attack budget
For PoS: after finality gadget, reorganization forks the entire chain
For PoW: after N confirmations, reorg cost = N * block_reward * hashrate_share
| MEV Type | Description | Example |
|---|---|---|
| DEX arbitrage | Price differences across AMMs | Buy low on Uniswap, sell high on Sushiswap |
| Sandwich | Frontrun + backrun trade | Buy before user, sell after user's trade |
| Liquidation | Liquidate undercollateralized positions | Aave/Compound health factor triggers |
| JIT liquidity | Insert liquidity before swap, remove after | Uniswap v3 concentrated liquidity |
| Time-bandit | Reorganize chain to extract MEV | Reorg last N blocks for profitable txns |
| Multi-block MEV | Control consecutive blocks | Validator with multiple consecutive proposals |
Protocol level:
├── Threshold encryption (Shutter): Txs encrypted, decrypted at block production
├── FOCIL (Forced Inclusion Lists): Validators force tx inclusion
├── ePBS (In-protocol PBS): Consensus-level proposer-builder separation
├── Single-slot finality: No time for reorg-based MEV
└── Uniform random ordering: Tx order determined by randomness, not fee
Application level:
├── Commit-reveal: Submit commitment, reveal later
├── Batch auctions (CowSwap): CoW mechanism for order matching
├── Slippage protection: minOutputAmount in all swaps
└── Private mempool (Flashbots): Bypass public mempool
// Gossip protocol options:
// 1. Full mesh (Ethereum devp2p): Every node connects to ~50 peers
// - Tx gossip: hash → request → receive (not full tx propagation)
// - Block propagation: header + body (compact blocks)
// 2. Kademlia DHT (libp2p): Structured overlay network
// Used by: IPFS, Polkadot, Filecoin
// - O(log n) lookup for peer discovery
// - Efficient routing to specific content
// 3. Tree propagation (Solana Turbine):
// - Data split into fragments, propagated via tree
// - Each node receives from parent, forwards to children
// - O(log n) depth for full network propagation
// 4. GossipSub (libp2p): Mesh + gossip
// - FloSin: Heartbeat-based mesh maintenance
// - Used by: Ethereum CL, Filecoin
// - Configurable: D (degree), D_lazy, D_high, D_low
| Rule | Mechanism | Pros | Cons |
|---|---|---|---|
| Nakamoto | Most accumulated work | Simple, proven | Vulnerable to selfish mining |
| GHOST | Greedy Heaviest Observed Subtree | Handles uncle blocks | Less studied |
| LMD-GHOST | Latest Message Driven GHOST | Byzantine agreement friendly | Complex implementation |
| Tendermint | 2/3+ precommits | Instant finality | Liveness requires 2/3+ |
| Snowman (Avalanche) | Repeated subsampling | Fast finality, energy efficient | Novel, less tested |
| Chain | Storage (Full) | Storage (Archive) | RAM | CPU | Sync Time |
|---|---|---|---|---|---|
| Bitcoin | 550GB | 5.5TB | 8GB | 8 cores | 2-3 days |
| Ethereum | 1TB | 12TB+ | 32GB | 16 cores | 1-3 days |
| Solana | 500GB | N/A | 128GB+ | 16 cores+GPU | 1-2 days |
| Cosmos | 100GB | 500GB | 8GB | 4 cores | 1 day |
interface Block { hash: string; parentHash: string; height: number; weight: number; }
interface Attestation { validatorIndex: number; blockHash: string; weight: number; }
function lmdGhost(blocks: Map<string, Block>, attestations: Attestation[], genesis: string): string {
const latest = new Map<number, string>();
for (const a of attestations) latest.set(a.validatorIndex, a.blockHash);
let current = genesis;
while (true) {
const children = [...blocks.values()].filter(b => b.parentHash === current);
if (!children.length) break;
let best = children[0], bestVotes = 0;
for (const c of children) {
let votes = 0;
for (const [, vh] of latest) if (isAncestor(blocks, c.hash, vh)) votes++;
if (votes > bestVotes) { bestVotes = votes; best = c; }
}
current = best.hash;
}
return current;
}
function isAncestor(blocks: Map<string, Block>, anc: string, hash: string): boolean {
let cur = hash;
while (cur) { if (cur === anc) return true; cur = blocks.get(cur)?.parentHash ?? ''; }
return false;
}
type Mempool struct {
txs map[string]*Transaction
byFee *txHeap // min-heap of effective tip
accountNonces map[string]uint64
maxSize int // 50,000 txs
minGasPrice *big.Int // anti-spam floor
replacementPct float64 // 10% bump for replace-by-fee
}
func (mp *Mempool) Add(tx *Transaction) bool {
if len(mp.txs) >= mp.maxSize {
// Evict cheapest tx from overpriced account
cheapest := mp.byFee.Pop()
delete(mp.txs, cheapest.Hash)
}
if tx.EffectiveTip.Cmp(mp.minGasPrice) < 0 { return false }
mp.txs[tx.Hash] = tx
mp.byFee.Push(tx)
return true
}
Consensus Mechanism Selection
├── Permissioned or permissionless?
│ ├── Permissionless → PoS (Ethereum, Cosmos) / PoW (Bitcoin)
│ ├── Permissioned (enterprise) → IBFT / Raft / Clique (Quorum)
│ └── Consortium → PoA / DPoS (multiple validators)
├── Finality requirement?
│ ├── Instant → IBFT / HotStuff / Tendermint (PBFT-based)
│ ├── Probabilistic → PoW / PoS (Ethereum, Bitcoin)
│ └── Economic → PoS with slashing (Casper, Tendermint)
├── Scalability priority?
│ ├── High tps → DPoS / HotStuff (1000+ tps)
│ ├── Moderate → PoS (100 tps)
│ └── Low → PoW (7-15 tps)
└── Energy efficiency?
├── Critical → PoS / DPoS / PoA
└── Not critical → PoW (Bitcoin)
Decision criteria: Evaluate decentralization requirements, finality speed, validator set size, and energy constraints.
// blockchain-core/contracts/MerkleVerifier.sol
pragma solidity ^0.8.20;
contract MerkleVerifier {
function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
computedHash = computedHash < proof[i]
? keccak256(abi.encodePacked(computedHash, proof[i]))
: keccak256(abi.encodePacked(proof[i], computedHash));
}
return computedHash == root;
}
}
// blockchain-core/src/discovery.rs
use libp2p::{identity, Multiaddr, PeerId};
use std::collections::HashSet;
pub struct DiscoveryService {
peers: HashSet<PeerId>,
bootnodes: Vec<Multiaddr>,
}
impl DiscoveryService {
pub fn new(bootnodes: Vec<Multiaddr>) -> Self {
Self { peers: HashSet::new(), bootnodes }
}
pub fn discover_peers(&mut self) -> Vec<PeerId> {
self.bootnodes.iter().filter_map(|addr| {
addr.iter().last().and_then(|_| Some(PeerId::random()))
}).collect()
}
}
| Anti-Pattern | Consequence | Solution |
|---|---|---|
| Single validator for testnet | No realistic failure testing | Run minimum 4 validators per network |
| No slashing protection | Accidental double-sign | Implement slashing protection per validator |
| Unbounded mempool | Memory exhaustion | Set max mempool size with fee-based eviction |
| No peer scoring | Eclipse attack vulnerability | Implement reputation-based peer scoring |
| Ignoring clock skew | Block timestamps invalid | Use NTP; tolerate 2s clock drift |
blockchain → blockchain-core