ETH
ETH 区块链2.0;白皮书,黄皮书,源代码
基础
资料总结
1. author:vitalik
- [memory hard]mining puzzle ->需要大内存:限制ASIC芯片,BTC支持ASIC
- POW -> POS(stake:权益证明) -> DPOS
- smart contract-> 去中心化的合同
2. 基于帐户的分布式账本
- UTXO:数据结构
- 基于账户模型: 不会出现dubbo spending attack(花钱的人不诚实,想重新花钱);会出现replay attack(重放攻击:收钱的人不诚实,收钱人想重新收钱)
- replay attack:通过nonce计数器防止,nonce记录当前账户转账的次数,并使用当前私钥加密写进交易
- 外部账户:(pub/pri管理)[包括:balance,nonce] -> 源码的数据结构
- 合约账户:不能主动发起交易,通过外部账户发起,[包括:code,storge] ->源码数据结构
3. 数据结构:状态树、交易树、收据树 => MPT
- 状态树
- eth地址:(160bit->20Byte->40Hex) [addr:state] -> 不能使用hash,Merkel tree(merkle指针的类型不是地址)
- Key: trie(字典数) -> patricia trie(经过路径压缩的前缀树,对trie空间优化) -> MPT -> modified MPT(修改的节点指向原节点)
- Value: 使用RLP序列化
- 交易树:当前区块的交易:key不是txid
- 收据树(查询交易结果)当前区块的交易
- Bloom Filter: 查找交易的原理 -> 很牛逼:结合bit操作
- ETH: 交易驱动的状态机 -> 有限状态机
// Header represents a block header in the Ethereum blockchain.
type Header struct {
ParentHash common.Hash `json:"parentHash" gencodec:"required"`
UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
Coinbase common.Address `json:"miner" gencodec:"required"`
// 状态树,交易树,收据树
Root common.Hash `json:"stateRoot" gencodec:"required"`
TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
// bloom filter 的并集
Bloom Bloom `json:"logsBloom" gencodec:"required"`
Difficulty *big.Int `json:"difficulty" gencodec:"required"`
Number *big.Int `json:"number" gencodec:"required"`
GasLimit uint64 `json:"gasLimit" gencodec:"required"`
GasUsed uint64 `json:"gasUsed" gencodec:"required"`
Time *big.Int `json:"timestamp" gencodec:"required"`
Extra []byte `json:"extraData" gencodec:"required"`
MixDigest common.Hash `json:"mixHash" gencodec:"required"`
Nonce BlockNonce `json:"nonce" gencodec:"required"`
}
// Block represents an entire block in the Ethereum blockchain.
type Block struct {
header *Header
uncles []*Header
transactions Transactions
// caches
hash atomic.Value
size atomic.Value
// Td is used by package core to store the total difficulty
// of the chain up to and including the block.
td *big.Int
// These fields are used by package eth to track
// inter-peer block relay.
ReceivedAt time.Time
ReceivedFrom interface{}
}
// "external" block encoding. used for eth protocol, etc.
type extblock struct {
Header *Header
Txs []*Transaction
Uncles []*Header
}
4. GHOST协议 -> 解决区块链的分叉
- 5ETH -> 3ETH: 不会自动下调
- uncle block(叔父区块,分叉的第一个区块:防止forking_attack): 7/8 block_rework,没有gas_Fee; 叔父区块上的交易不执行,只验证交易的合法性
- 7代共同祖先,2个uncle
5. 挖矿:memory-hard mining puzzle
- LiteCoin:
- 莱特币使用scrypt计算mining puzzle;对内存不友好,在head_hash计算过程中多次hash读取内存 -> 不使用ASIC,GPU; 但是硬件技术发展快
- 出块速度4倍BTC
- ETH: 16M
cache, 1Gdataset: GPU- 对内存的要求不断增加(cache,dataset)
- 挖矿算法: ethash
- 计划: POW -> POS
- pre-mining预挖矿, pre-sale:预售;
6. 挖矿难度调整
- 15秒出块时间,依赖负区块的难度,难度炸弹:最小难度131072
7. 权益证明 • proof of stake
- 权益证明算法: Casper the Friendly Finality Gadget(FFG)
- EOS: 权益证明:DPOS协议(Delegated proof of stake)
8. 智能合约
- code is low
9. DAO: 去中心化的自制组织
ETC/ETH的硬分叉