GO-HPB源码解读--挖矿流程(一)

在开析流程前先看下主要的数据结构,挖矿工作主要由三个对象来完成的,miner、worker、cpuAgent,其中cpuAgent实现Producer接口类

  • miner是入口,负责与外部交互,控制挖矿流程的启停操作
  • worker管理Work任务,他代理着Producer接口类,也就是代理着具体挖矿执行着
  • cpuAgent负责具体挖矿计算工作
1
2
3
4
5
6
7
8
9
10
11
// Miner creates blocks and searches for proof-of-work values.
type Miner struct {
mux *sub.TypeMux
worker *worker
coinbase common.Address
mining int32
engine consensus.Engine

canStart int32 // can start indicates whether we can start the mining operation
shouldStart int32 // should start indicates whether we should start after sync
}
字段 类型 含义 备注
mux *sub.TypeMux 接收来自同步模块的StartEvent DoneEvent FailedEvent事件通知。在网络中,不可能只有一个矿工节点,当节点开始从其他节点同步Block时,我们就没有必要再继续挖矿了
worker *worker 具体执行挖矿的工人
coinbase common.Address 挖矿地址,挖矿所得的收入将计入该账户
mining int32 挖矿状态
engine consensus.Engine 共识引擎
canStart int32 是否可以开始挖矿
shouldStart int32 quitCurrentOp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// worker is the main object which takes care of applying messages to the new state
type worker struct {
config *config.ChainConfig
engine consensus.Engine
mu sync.Mutex
// update loop
mux *sub.TypeMux
pool *txpool.TxPool
txCh chan bc.TxPreEvent
txSub sub.Subscription
//txSub sub.Subscription
chainHeadCh chan bc.ChainHeadEvent
chainHeadSub sub.Subscription
chainSideCh chan bc.ChainSideEvent
chainSideSub sub.Subscription
wg sync.WaitGroup
producers map[Producer]struct{}
recv chan *Result
chain *bc.BlockChain
proc bc.Validator
chainDb hpbdb.Database
coinbase common.Address
extra []byte
currentMu sync.Mutex
current *Work
uncleMu sync.Mutex
possibleUncles map[common.Hash]*types.Block
unconfirmed *unconfirmedBlocks // set of locally mined blocks pending canonicalness confirmations
// atomic status counters
mining int32
atWork int32
}
字段 类型 含义 备注
config *config.ChainConfig
engine consensus.Engine
mu sync.Mutex
mux *sub.TypeMux 注意与miner.mux属性不同,这里是向外部发布已经挖到新Block
pool *txpool.TxPool
txCh chan bc.TxPreEvent 接收txPool中tx的通道
txSub sub.Subscription
chainHeadCh chan bc.ChainHeadEvent 接收区块头的通道
chainHeadSub sub.Subscription
chainSideCh chan bc.ChainSideEvent 接收主备链变更通道
chainSideSub sub.Subscription
wg sync.WaitGroup
producers map[Producer]struct{} worker拥有一个Producer的map集合
recv chan *Result Producer结果发送通道,每个管理的Producer都可能将挖出的Block发到该Channel,也就是说,这个收方向Channel是一对多的
chain *bc.BlockChain
proc bc.Validator
chainDb hpbdb.Database 存储数据库
coinbase common.Address 挖矿地址
extra []byte
currentMu sync.Mutex
current *Work
uncleMu sync.Mutex
possibleUncles map[common.Hash]*types.Block
unconfirmed *unconfirmedBlocks 本地挖出的待确认的块
mining int32
atWork int32
1
2
3
4
5
6
7
// Agent can register themself with the worker
type Producer interface {
Work() chan<- *Work
SetReturnCh(chan<- *Result)
Stop()
Start()
}

Producer是个接口类,主要定义了一些操作方法,在HPB中,该接口类只有CpuAgent这一个实现类

1
2
3
4
5
6
7
8
9
10
11
12
13
type CpuAgent struct {
mu sync.Mutex

workCh chan *Work
stop chan struct{}
quitCurrentOp chan struct{}
returnCh chan<- *Result

chain consensus.ChainReader
engine consensus.Engine

isMining int32 // 正在挖矿
}
字段 类型 含义 备注
mu sync.Mutex
workCh chan *Work 接收来自worker下发的工作任务Work
stop chan struct{} 使该CpuAgent停止工作的信号
quitCurrentOp chan struct{} 退出当前操作通道
returnCh chan<- *Result 向worker反馈工作任务的完成情况,实际上就是挖出的新Block
chain consensus.ChainReader 用于访问本地节点BlockChain数据的接口
engine consensus.Engine 计算所采用的共识引擎
isMining int32 是否正在挖矿