链
共识协议,核心、软件服务端
- 处理外部(客户端、其它组件)请求
- 软件管理
- 实现核心功能(主要是对区块的处理,以及对交易、密钥镜像等的处理)
- 封装其它模块(存储、网络、交易池)
- 交易进交易池
- 将区块上链#
- 主链的选择确认,Ghost 协议*#
- 区块的全排序,Epoch 概念*#
- 非法区块、交易的判定规则(区块数据异常、打包非法交易)#
- 对“重复交易”的惩罚*
- 对“冲突交易”的处理*
- 严格的对交易和区块的各种校验、验证#
- 矿工收入的奖惩机制(含矿工收入算法)#
- 交易随机选择策略*#
- 交易费用激励机制*#
- 主块、副块奖励差异*
- 奖励挖矿打包时不能确定,上链时才计算,可称为“秋后算账”
- 特有解决方案(部分会涉及到算法)
- 严格的防范措施
- 客户端实现对 51% 攻击的拒绝*
- 客户端标记特殊交易*
- 智能合约的执行*
- 去中心化,保证正确性和可篡改性
- 其它
数据结构:
// all components requiring access to blockchain must use , this struct to communicate
// this structure must be update while mutex
type Blockchain struct {
store storage.Store // interface to storage layer
Height int64 // chain height is always 1 more than block
height_seen int64 // height seen on peers
Top_ID crypto.Hash // id of the top block
//Tips map[crypto.Hash]crypto.Hash // current tips
dag_unsettled map[crypto.Hash]bool // current unsettled dag
dag_past_unsettled_cache *lru.Cache
dag_future_unsettled_cache *lru.Cache
lrucache_workscore *lru.Cache
lrucache_fullorder *lru.Cache // keeps full order for tips upto a certain height
MINING_BLOCK bool // used to pause mining
Difficulty uint64 // current cumulative difficulty
Median_Block_Size uint64 // current median block size
Mempool *mempool.Mempool
Exit_Event chan bool // blockchain is shutting down and we must quit ASAP
Top_Block_Median_Size uint64 // median block size of current top block
Top_Block_Base_Reward uint64 // top block base reward
checkpints_disabled bool // are checkpoints disabled
simulator bool // is simulator mode
P2P_Block_Relayer func(*block.Complete_Block, uint64) // tell p2p to broadcast any block this daemon hash found
sync.RWMutex
}
内容:
元数据 + 关联对象
元数据,不同的区块链会有所不同,Dero 具体见上表;
关联对象,网络、存储、交易池。
type cachekey struct {
blid crypto.Hash
base crypto.Hash
base_height int64
}
将区块上链
重要方法之一,由挖矿程序执行。需要经过三四十道程序层层校验、处理,最终完成,确保数据有效、有序、完全正确。
架构设计:
它是整个系统的顶端。
区别于区块和交易,它对“数据结构”的依赖较弱,它更偏向于算法、功能,即所谓的“共识”。
当前可大致分为三部分:
- 管理自身(它有自己的数据结构,有数据、有状态,需要管理)
- 对外服务(通常我们需要对外提供一个服务,外部通过接口访问链上的数据,或提交数据给链)
- 耦合各个组件(除了管理自身及对外服务外,它还耦合各个组件,使之成为一个整体)
它是很复杂的,体现是数据多、功能多,不同的区块链系统有不同的数据和功能,不是固定的。
比较好的做法是,从下往上,根据需求进行补充。