Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
share
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
黄刚
share
Commits
3048bb00
Commit
3048bb00
authored
Nov 06, 2018
by
黄刚
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
b6040ef6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
198 additions
and
23 deletions
+198
-23
Consensus_logic.png
resources/Consensus_logic.png
+0
-0
consensus.EAP
resources/consensus.EAP
+0
-0
共识模块.md
共识模块.md
+198
-23
No files found.
resources/Consensus_logic.png
0 → 100644
View file @
3048bb00
28.4 KB
resources/consensus.EAP
0 → 100644
View file @
3048bb00
File added
共识模块.md
View file @
3048bb00
# 共识模
块
# 共识模
块
# 共识模块
# 共识模块
## 1.模块介绍
<!-- TOC -->
-
[
共识模块
](
#共识模块
)
-
[
1.模块介绍
](
#1-模块介绍
)
-
[
2.逻辑架构及上下文
](
#2-逻辑架构及上下文
)
-
[
3.处理逻辑
](
#3-处理逻辑
)
-
[
3.1加载共识模块
](
#3.1-加载共识模块
)
-
[
接口定义
](
#3.1.1-接口定义
)
-
[
通用处理逻辑
](
#3.1.2-通用处理逻辑
)
-
[
3.2关闭共识模块
](
#3.2-关闭共识模块
)
-
[
接口定义
](
#3.2.1-接口定义
)
-
[
通用处理逻辑
](
#3.2.2-通用处理逻辑
)
-
[
3.3创建创世区块交易
](
#3.3-创建创世区块交易
)
-
[
接口定义
](
#3.3.1-接口定义
)
-
[
通用处理逻辑
](
#3.3.2-通用处理逻辑
)
-
[
3.4获取创世区块时间
](
#3.4-获取创世区块时间
)
-
[
接口定义
](
#3.4.1-接口定义
)
-
[
通用处理逻辑
](
#3.4.2-通用处理逻辑
)
-
[
3.5创建区块
](
#3.5-创建区块
)
-
[
接口定义
](
#3.5.1-接口定义
)
-
[
通用处理逻辑
](
#3.5.2-通用处理逻辑
)
-
[
3.6检查区块
](
#3.6-检查区块
)
-
[
接口定义
](
#3.6.1-接口定义
)
-
[
通用处理逻辑
](
#3.6.2-通用处理逻辑
)
-
[
3.7处理自定义事件
](
#3.7-处理自定义事件
)
-
[
接口定义
](
#3.7.1-接口定义
)
-
[
通用处理逻辑
](
#3.7.2-通用处理逻辑
)
<!-- /TOC -->
<h2
id=
"1-模块介绍"
>
1.模块介绍
</h2>
共识模块是实现区块链共识机制的模块,共识机制是区块链技术的重要组件。区块链共识机制的目标是使所有的诚实节点保存一致的区块链视图,同时满足两个性质:
共识模块是实现区块链共识机制的模块,共识机制是区块链技术的重要组件。区块链共识机制的目标是使所有的诚实节点保存一致的区块链视图,同时满足两个性质:
-
**一致性**
:所有诚实节点保存的区块链的前缀部分完全相同。
-
**一致性**
:所有诚实节点保存的区块链的前缀部分完全相同。
-
**有效性**
:由某诚实节点发布的信息终将被其他所有诚实节点记录在自己的区块链中。
-
**有效性**
:由某诚实节点发布的信息终将被其他所有诚实节点记录在自己的区块链中。
...
@@ -7,10 +34,10 @@
...
@@ -7,10 +34,10 @@
通俗来说,共识机制在区块链网络内起到决定谁负责生成新区块以及维护区块链统一的作用。
通俗来说,共识机制在区块链网络内起到决定谁负责生成新区块以及维护区块链统一的作用。
目前区块链的共识机制大致可以分为PoW(Proof of Work 工作量证明)、PoS(Proof of Stack 权益证明)、DPoS(Delegated Proof of Stake 委托权益证明)以及分布式一致性算法几类。
目前区块链的共识机制大致可以分为PoW(Proof of Work 工作量证明)、PoS(Proof of Stack 权益证明)、DPoS(Delegated Proof of Stake 委托权益证明)以及分布式一致性算法几类。
## 2.逻辑架构及上下文
<h2
id=
"2-逻辑架构及上下文"
>
2.逻辑架构及上下文
</h2>
*共识模块上下文*
*共识模块上下文*
目前chain33系统中的共识模块是可插拔的,支持平行链共识,PBFT
,
Raft,Tendermint,Ticket几种共识算法。共识模块在整个chain33系统中所处的地位如下图:
目前chain33系统中的共识模块是可插拔的,支持平行链共识,PBFT
,
Raft,Tendermint,Ticket几种共识算法。共识模块在整个chain33系统中所处的地位如下图:


...
@@ -28,48 +55,196 @@
...
@@ -28,48 +55,196 @@
*注:EventGetBlocks用于向Blockchain模块获取指定高度范围的区块,EventGetLastBlock事件用于向Blockchain模块获取最新的区块。*
*注:EventGetBlocks用于向Blockchain模块获取指定高度范围的区块,EventGetLastBlock事件用于向Blockchain模块获取最新的区块。*
共识模块与chain33系统的耦合体现在两个接口:
> 共识模块内部逻辑
-
Module:

如上图所示,共识模块主要通过Miner和Module两个接口实现主要逻辑功能。
<h2
id=
"3-处理逻辑"
>
3.处理逻辑
</h2>
> 下面分别介绍每个接口的具体功能以及实现逻辑
<h3
id=
"3.1-加载共识模块"
>
3.1 加载共识模块
</h3>
<h4
id=
"3.1.1-接口定义"
>
接口定义
</h4>
```
go
```
go
/// Module be used for module interface
/// Module be used for module interface
type
Module
interface
{
type
Module
interface
{
SetQueueClient
(
client
Client
)
SetQueueClient
(
client
Client
)
Close
()
}
}
```
```
SetQueueClient主要完成如下功能:
此接口的参数Client是实现了消息队列client接口。
> **实现功能:**
> 1. 设置消息队列Client
> 2. 初始化本地区块
> 3. 订阅消息并开始消息循环
> 4. 开启共识过程
1.
设置消息队列Client
<h4
id=
"3.1.2-通用处理逻辑"
>
通用处理逻辑
</h4>
2.
初始化本地区块
```
go
3.
订阅消息并开始消息循环
func
(
bc
*
BaseClient
)
SetQueueClient
(
c
queue
.
Client
)
{
4.
开启共识过程
bc
.
InitClient
(
c
,
func
()
{
//call init block
bc
.
InitBlock
()
})
go
bc
.
EventLoop
()
go
bc
.
child
.
CreateBlock
()
}
```
在BaseClient基类中,实现了上述4点功能,其中CreateBlock()实现了最简单的共识过程,获取交易,然后打包。
Close:关闭退出共识
<h3
id=
"3.2-关闭共识模块"
>
3.2 关闭共识模块
</h3>
<h4
id=
"3.2.1-接口定义"
>
接口定义
</h4>
```
go
/// Module be used for module interface
type
Module
interface
{
Close
()
}
```
> **实现功能:** 关闭共识模块,进行必要的清理工作。
-
Miner
<h4
id=
"3.2.2-通用处理逻辑"
>
通用处理逻辑
</h4>
```
go
func
(
bc
*
BaseClient
)
Close
()
{
atomic
.
StoreInt32
(
&
bc
.
minerStart
,
0
)
bc
.
client
.
Close
()
log
.
Info
(
"consensus base closed"
)
}
```
关闭挖矿,关闭client消息接收
<h3
id=
"3.3-创建创世区块交易"
>
3.3 创建创世区块交易
</h3>
<h4
id=
"3.3.1-接口定义"
>
接口定义
</h4>
```
go
```
go
type
Miner
interface
{
type
Miner
interface
{
CreateGenesisTx
()
[]
*
types
.
Transaction
CreateGenesisTx
()
[]
*
types
.
Transaction
}
```
> **实现功能:** 创建创世区块中包含的交易。
<h4
id=
"3.3.2-通用处理逻辑"
>
通用处理逻辑
</h4>
```
go
func
(
client
*
Client
)
CreateGenesisTx
()
(
ret
[]
*
types
.
Transaction
)
{
var
tx
types
.
Transaction
//设置本次交易的执行器名
tx
.
Execer
=
[]
byte
(
"coins"
)
//设置本次交易的目的地址
tx
.
To
=
client
.
subcfg
.
Genesis
//gen payload
g
:=
&
cty
.
CoinsAction_Genesis
{}
g
.
Genesis
=
&
types
.
AssetsGenesis
{}
//转账数额
g
.
Genesis
.
Amount
=
1e8
*
types
.
Coin
tx
.
Payload
=
types
.
Encode
(
&
cty
.
CoinsAction
{
Value
:
g
,
Ty
:
cty
.
CoinsActionGenesis
})
ret
=
append
(
ret
,
&
tx
)
return
}
```
<h3
id=
"3.4-获取创世区块时间"
>
3.4 获取创世区块时间
</h3>
<h4
id=
"3.4.1-接口定义"
>
接口定义
</h4>
```
go
type
Miner
interface
{
GetGenesisBlockTime
()
int64
GetGenesisBlockTime
()
int64
CreateBlock
()
CheckBlock
(
parent
*
types
.
Block
,
current
*
types
.
BlockDetail
)
error
ProcEvent
(
msg
queue
.
Message
)
bool
}
}
```
```
> **实现功能:** 获取创世区块时间
CreateGenesisTx:创建创世区块交易
<h4
id=
"3.4.2-通用处理逻辑"
>
通用处理逻辑
</h4>
GetGenesisBlockTime:获取创世区块时间
```
go
func
(
client
*
Client
)
GetGenesisBlockTime
()
int64
{
return
client
.
subcfg
.
GenesisBlockTime
}
```
<h3
id=
"3.5-创建区块"
>
3.5 创建区块
</h3>
<h4
id=
"3.5.1-接口定义"
>
接口定义
</h4>
```
go
type
Miner
interface
{
CreateBlock
()
}
```
> **实现功能:** 获取交易,打包生成区块
<h4
id=
"3.5.2-通用处理逻辑"
>
通用处理逻辑
</h4>
```
go
func
(
client
*
Client
)
CreateBlock
()
{
issleep
:=
true
for
{
//判断是否正在挖矿,是否已完成区块同步
if
!
client
.
IsMining
()
||
!
client
.
IsCaughtUp
()
{
time
.
Sleep
(
client
.
sleepTime
)
continue
}
if
issleep
{
time
.
Sleep
(
client
.
sleepTime
)
}
//获取前一高度的区块
lastBlock
:=
client
.
GetCurrentBlock
()
//从Mempool中获取可用交易
txs
:=
client
.
RequestTx
(
int
(
types
.
GetP
(
lastBlock
.
Height
+
1
)
.
MaxTxNumber
),
nil
)
if
len
(
txs
)
==
0
{
issleep
=
true
continue
}
issleep
=
false
//检查去重
txs
=
client
.
CheckTxDup
(
txs
)
var
newblock
types
.
Block
newblock
.
ParentHash
=
lastBlock
.
Hash
()
newblock
.
Height
=
lastBlock
.
Height
+
1
//将交易加入区块
client
.
AddTxsToBlock
(
&
newblock
,
txs
)
//solo 挖矿固定难度
newblock
.
Difficulty
=
types
.
GetP
(
0
)
.
PowLimitBits
newblock
.
TxHash
=
merkle
.
CalcMerkleRoot
(
newblock
.
Txs
)
newblock
.
BlockTime
=
types
.
Now
()
.
Unix
()
if
lastBlock
.
BlockTime
>=
newblock
.
BlockTime
{
newblock
.
BlockTime
=
lastBlock
.
BlockTime
+
1
}
//写区块:写入本地并广播给其他节点
err
:=
client
.
WriteBlock
(
lastBlock
.
StateHash
,
&
newblock
)
//判断有没有交易是被删除的,这类交易要从mempool 中删除
if
err
!=
nil
{
issleep
=
true
continue
}
}
}
```
<h3
id=
"3.6-检查区块"
>
3.6 检查区块
</h3>
<h4
id=
"3.6.1-接口定义"
>
接口定义
</h4>
```
go
type
Miner
interface
{
CheckBlock
(
parent
*
types
.
Block
,
current
*
types
.
BlockDetail
)
error
}
```
> **实现功能:** 通过比较父区块和当前区块,检查当前区块的有效性
CreateBlock:获取交易,打包区块
<h4
id=
"3.6.2-通用处理逻辑"
>
通用处理逻辑
</h4>
```
go
func
(
client
*
Client
)
CheckBlock
(
parent
*
types
.
Block
,
current
*
types
.
BlockDetail
)
error
{
return
nil
}
```
此接口用于检查用户自定义的一些记录再区块内的逻辑信息,区块内交易信息校验在具体的执行器内实现。
<h3
id=
"3.7-处理自定义事件块"
>
3.7 处理自定义事件
</h3>
<h4
id=
"3.7.1-接口定义"
>
接口定义
</h4>
```
go
type
Miner
interface
{
ProcEvent
(
msg
queue
.
Message
)
bool
}
```
> **实现功能:** 处理除BaseClient处理的事件以外的自定义事件
CheckBlock:通过父区块检查当前区块是否有效
<h4
id=
"3.7.2-通用处理逻辑"
>
通用处理逻辑
</h4>
```
go
func
(
client
*
Client
)
ProcEvent
(
msg
queue
.
Message
)
bool
{
return
false
}
```
*注:如果不需要处理自定义事件直接返回false,对关心的事件处理成功后返回true。*
ProcEvent:处理除BaseClient处理的事件以外的其他事件
## 3.处理逻辑
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment