Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
plugin
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
plugin
Commits
b08c65a3
Commit
b08c65a3
authored
Apr 30, 2019
by
张振华
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lint
parent
07cde0ca
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
386 additions
and
382 deletions
+386
-382
consensus_state.go
plugin/consensus/dpos/consensus_state.go
+65
-49
dpos.go
plugin/consensus/dpos/dpos.go
+24
-25
node.go
plugin/consensus/dpos/node.go
+1
-2
peer_set.go
plugin/consensus/dpos/peer_set.go
+2
-1
dpos_msg.proto
plugin/consensus/dpos/proto/dpos_msg.proto
+1
-1
secret_connection.go
plugin/consensus/dpos/secret_connection.go
+1
-1
state_machine.go
plugin/consensus/dpos/state_machine.go
+183
-181
state_machine_test.go
plugin/consensus/dpos/state_machine_test.go
+7
-10
dpos_msg.pb.go
plugin/consensus/dpos/types/dpos_msg.pb.go
+29
-29
genesis.go
plugin/consensus/dpos/types/genesis.go
+5
-5
msg.go
plugin/consensus/dpos/types/msg.go
+43
-43
priv_validator.go
plugin/consensus/dpos/types/priv_validator.go
+2
-4
signable.go
plugin/consensus/dpos/types/signable.go
+9
-10
validator_set.go
plugin/consensus/dpos/types/validator_set.go
+5
-5
validator_manager.go
plugin/consensus/dpos/validator_manager.go
+9
-16
No files found.
plugin/consensus/dpos/consensus_state.go
View file @
b08c65a3
...
@@ -25,16 +25,15 @@ const (
...
@@ -25,16 +25,15 @@ const (
proposalHeartbeatIntervalSeconds
=
1
proposalHeartbeatIntervalSeconds
=
1
continueToVote
=
0
continueToVote
=
0
voteSuccess
=
1
voteSuccess
=
1
voteFail
=
2
voteFail
=
2
)
)
// Errors define
// Errors define
var
(
var
(
ErrInvalidVoteSignature
=
errors
.
New
(
"Error invalid vote signature"
)
ErrInvalidVoteSignature
=
errors
.
New
(
"Error invalid vote signature"
)
ErrInvalidVoteReplySignature
=
errors
.
New
(
"Error invalid vote reply signature"
)
ErrInvalidVoteReplySignature
=
errors
.
New
(
"Error invalid vote reply signature"
)
ErrInvalidNotifySignature
=
errors
.
New
(
"Error invalid notify signature"
)
ErrInvalidNotifySignature
=
errors
.
New
(
"Error invalid notify signature"
)
)
)
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
...
@@ -45,8 +44,8 @@ var (
...
@@ -45,8 +44,8 @@ var (
// internally generated messages which may update the state
// internally generated messages which may update the state
type
timeoutInfo
struct
{
type
timeoutInfo
struct
{
Duration
time
.
Duration
`json:"duration"`
Duration
time
.
Duration
`json:"duration"`
State
int
`json:"state"`
State
int
`json:"state"`
}
}
func
(
ti
*
timeoutInfo
)
String
()
string
{
func
(
ti
*
timeoutInfo
)
String
()
string
{
...
@@ -59,12 +58,12 @@ func (ti *timeoutInfo) String() string {
...
@@ -59,12 +58,12 @@ func (ti *timeoutInfo) String() string {
// The internal state machine receives input from peers, the internal validator, and from a timer.
// The internal state machine receives input from peers, the internal validator, and from a timer.
type
ConsensusState
struct
{
type
ConsensusState
struct
{
// config details
// config details
client
*
Client
client
*
Client
privValidator
ttypes
.
PrivValidator
// for signing votes
privValidator
ttypes
.
PrivValidator
// for signing votes
privValidatorIndex
int
privValidatorIndex
int
// internal state
// internal state
mtx
sync
.
Mutex
mtx
sync
.
Mutex
validatorMgr
ValidatorMgr
// State until height-1.
validatorMgr
ValidatorMgr
// State until height-1.
// state changes may be triggered by msgs from peers,
// state changes may be triggered by msgs from peers,
...
@@ -87,12 +86,12 @@ type ConsensusState struct {
...
@@ -87,12 +86,12 @@ type ConsensusState struct {
//当前达成共识的选票
//当前达成共识的选票
currentVote
*
dpostype
.
VoteItem
currentVote
*
dpostype
.
VoteItem
lastVote
*
dpostype
.
VoteItem
lastVote
*
dpostype
.
VoteItem
myVote
*
dpostype
.
DPosVote
myVote
*
dpostype
.
DPosVote
lastMyVote
*
dpostype
.
DPosVote
lastMyVote
*
dpostype
.
DPosVote
notify
*
dpostype
.
DPosNotify
notify
*
dpostype
.
DPosNotify
lastNotify
*
dpostype
.
DPosNotify
lastNotify
*
dpostype
.
DPosNotify
//所有选票,包括自己的和从网络中接收到的
//所有选票,包括自己的和从网络中接收到的
...
@@ -109,7 +108,7 @@ func NewConsensusState(client *Client, valMgr ValidatorMgr) *ConsensusState {
...
@@ -109,7 +108,7 @@ func NewConsensusState(client *Client, valMgr ValidatorMgr) *ConsensusState {
internalMsgQueue
:
make
(
chan
MsgInfo
,
msgQueueSize
),
internalMsgQueue
:
make
(
chan
MsgInfo
,
msgQueueSize
),
timeoutTicker
:
NewTimeoutTicker
(),
timeoutTicker
:
NewTimeoutTicker
(),
Quit
:
make
(
chan
struct
{}),
Quit
:
make
(
chan
struct
{}),
dposState
:
InitStateObj
,
dposState
:
InitStateObj
,
dposVotes
:
nil
,
dposVotes
:
nil
,
}
}
...
@@ -141,7 +140,7 @@ func (cs *ConsensusState) String() string {
...
@@ -141,7 +140,7 @@ func (cs *ConsensusState) String() string {
return
fmt
.
Sprintf
(
"ConsensusState"
)
//(H:%v R:%v S:%v", cs.Height, cs.Round, cs.Step)
return
fmt
.
Sprintf
(
"ConsensusState"
)
//(H:%v R:%v S:%v", cs.Height, cs.Round, cs.Step)
}
}
// Get
State returns a copy of the chain state
.
// Get
ValidatorMgr returns a copy of the ValidatorMgr
.
func
(
cs
*
ConsensusState
)
GetValidatorMgr
()
ValidatorMgr
{
func
(
cs
*
ConsensusState
)
GetValidatorMgr
()
ValidatorMgr
{
cs
.
mtx
.
Lock
()
cs
.
mtx
.
Lock
()
defer
cs
.
mtx
.
Unlock
()
defer
cs
.
mtx
.
Unlock
()
...
@@ -149,7 +148,7 @@ func (cs *ConsensusState) GetValidatorMgr() ValidatorMgr {
...
@@ -149,7 +148,7 @@ func (cs *ConsensusState) GetValidatorMgr() ValidatorMgr {
}
}
// GetValidators returns a copy of the current validators.
// GetValidators returns a copy of the current validators.
func
(
cs
*
ConsensusState
)
GetValidators
()
([]
*
ttypes
.
Validator
)
{
func
(
cs
*
ConsensusState
)
GetValidators
()
[]
*
ttypes
.
Validator
{
cs
.
mtx
.
Lock
()
cs
.
mtx
.
Lock
()
defer
cs
.
mtx
.
Unlock
()
defer
cs
.
mtx
.
Unlock
()
return
cs
.
validatorMgr
.
Validators
.
Copy
()
.
Validators
return
cs
.
validatorMgr
.
Validators
.
Copy
()
.
Validators
...
@@ -182,7 +181,7 @@ func (cs *ConsensusState) Start() {
...
@@ -182,7 +181,7 @@ func (cs *ConsensusState) Start() {
go
cs
.
receiveRoutine
()
go
cs
.
receiveRoutine
()
// schedule the first round!
// schedule the first round!
cs
.
scheduleDPosTimeout
(
time
.
Second
*
3
,
InitStateType
)
cs
.
scheduleDPosTimeout
(
time
.
Second
*
3
,
InitStateType
)
}
}
}
}
...
@@ -194,7 +193,7 @@ func (cs *ConsensusState) Stop() {
...
@@ -194,7 +193,7 @@ func (cs *ConsensusState) Stop() {
// Attempt to schedule a timeout (by sending timeoutInfo on the tickChan)
// Attempt to schedule a timeout (by sending timeoutInfo on the tickChan)
func
(
cs
*
ConsensusState
)
scheduleDPosTimeout
(
duration
time
.
Duration
,
stateType
int
)
{
func
(
cs
*
ConsensusState
)
scheduleDPosTimeout
(
duration
time
.
Duration
,
stateType
int
)
{
cs
.
timeoutTicker
.
ScheduleTimeout
(
timeoutInfo
{
Duration
:
duration
,
State
:
stateType
})
cs
.
timeoutTicker
.
ScheduleTimeout
(
timeoutInfo
{
Duration
:
duration
,
State
:
stateType
})
}
}
// send a msg into the receiveRoutine regarding our own proposal, block part, or vote
// send a msg into the receiveRoutine regarding our own proposal, block part, or vote
...
@@ -216,6 +215,7 @@ func (cs *ConsensusState) sendInternalMessage(mi MsgInfo) {
...
@@ -216,6 +215,7 @@ func (cs *ConsensusState) sendInternalMessage(mi MsgInfo) {
func
(
cs
*
ConsensusState
)
updateToValMgr
(
valMgr
ValidatorMgr
)
{
func
(
cs
*
ConsensusState
)
updateToValMgr
(
valMgr
ValidatorMgr
)
{
cs
.
validatorMgr
=
valMgr
cs
.
validatorMgr
=
valMgr
}
}
//-----------------------------------------
//-----------------------------------------
// the main go routines
// the main go routines
...
@@ -263,14 +263,14 @@ func (cs *ConsensusState) handleMsg(mi MsgInfo) {
...
@@ -263,14 +263,14 @@ func (cs *ConsensusState) handleMsg(mi MsgInfo) {
var
err
error
var
err
error
msg
,
peerID
,
peerIP
:=
mi
.
Msg
,
string
(
mi
.
PeerID
),
mi
.
PeerIP
msg
,
peerID
,
peerIP
:=
mi
.
Msg
,
string
(
mi
.
PeerID
),
mi
.
PeerIP
switch
msg
:=
msg
.
(
type
)
{
switch
msg
:=
msg
.
(
type
)
{
case
*
dpostype
.
DPosVote
:
case
*
dpostype
.
DPosVote
:
cs
.
dposState
.
recvVote
(
cs
,
msg
)
cs
.
dposState
.
recvVote
(
cs
,
msg
)
case
*
dpostype
.
DPosNotify
:
case
*
dpostype
.
DPosNotify
:
cs
.
dposState
.
recvNotify
(
cs
,
msg
)
cs
.
dposState
.
recvNotify
(
cs
,
msg
)
case
*
dpostype
.
DPosVoteReply
:
case
*
dpostype
.
DPosVoteReply
:
cs
.
dposState
.
recvVoteReply
(
cs
,
msg
)
cs
.
dposState
.
recvVoteReply
(
cs
,
msg
)
default
:
default
:
dposlog
.
Error
(
"Unknown msg type"
,
msg
.
String
(),
"peerid"
,
peerID
,
"peerip"
,
peerIP
)
dposlog
.
Error
(
"Unknown msg type"
,
msg
.
String
(),
"peerid"
,
peerID
,
"peerip"
,
peerIP
)
}
}
if
err
!=
nil
{
if
err
!=
nil
{
dposlog
.
Error
(
"Error with msg"
,
"type"
,
reflect
.
TypeOf
(
msg
),
"peerid"
,
peerID
,
"peerip"
,
peerIP
,
"err"
,
err
,
"msg"
,
msg
)
dposlog
.
Error
(
"Error with msg"
,
"type"
,
reflect
.
TypeOf
(
msg
),
"peerid"
,
peerID
,
"peerip"
,
peerIP
,
"err"
,
err
,
"msg"
,
msg
)
...
@@ -297,22 +297,26 @@ func (cs *ConsensusState) IsProposer() bool {
...
@@ -297,22 +297,26 @@ func (cs *ConsensusState) IsProposer() bool {
return
false
return
false
}
}
func
(
cs
*
ConsensusState
)
SetState
(
state
DposState
){
// SetState method
func
(
cs
*
ConsensusState
)
SetState
(
state
DposState
)
{
cs
.
dposState
=
state
cs
.
dposState
=
state
}
}
// SaveVote method
func
(
cs
*
ConsensusState
)
SaveVote
()
{
func
(
cs
*
ConsensusState
)
SaveVote
()
{
if
cs
.
lastVote
==
nil
{
if
cs
.
lastVote
==
nil
{
cs
.
lastVote
=
cs
.
currentVote
cs
.
lastVote
=
cs
.
currentVote
}
else
if
cs
.
currentVote
!=
nil
&&
!
bytes
.
Equal
(
cs
.
currentVote
.
VoteI
d
,
cs
.
lastVote
.
VoteId
)
{
}
else
if
cs
.
currentVote
!=
nil
&&
!
bytes
.
Equal
(
cs
.
currentVote
.
VoteI
D
,
cs
.
lastVote
.
VoteID
)
{
cs
.
lastVote
=
cs
.
currentVote
cs
.
lastVote
=
cs
.
currentVote
}
}
}
}
func
(
cs
*
ConsensusState
)
SetCurrentVote
(
vote
*
dpostype
.
VoteItem
)
{
// SetCurrentVote method
func
(
cs
*
ConsensusState
)
SetCurrentVote
(
vote
*
dpostype
.
VoteItem
)
{
cs
.
currentVote
=
vote
cs
.
currentVote
=
vote
}
}
// SaveMyVote method
func
(
cs
*
ConsensusState
)
SaveMyVote
()
{
func
(
cs
*
ConsensusState
)
SaveMyVote
()
{
if
cs
.
lastMyVote
==
nil
{
if
cs
.
lastMyVote
==
nil
{
cs
.
lastMyVote
=
cs
.
myVote
cs
.
lastMyVote
=
cs
.
myVote
...
@@ -321,10 +325,12 @@ func (cs *ConsensusState) SaveMyVote() {
...
@@ -321,10 +325,12 @@ func (cs *ConsensusState) SaveMyVote() {
}
}
}
}
func
(
cs
*
ConsensusState
)
SetMyVote
(
vote
*
dpostype
.
DPosVote
){
// SetMyVote method
func
(
cs
*
ConsensusState
)
SetMyVote
(
vote
*
dpostype
.
DPosVote
)
{
cs
.
myVote
=
vote
cs
.
myVote
=
vote
}
}
// SaveNotify method
func
(
cs
*
ConsensusState
)
SaveNotify
()
{
func
(
cs
*
ConsensusState
)
SaveNotify
()
{
if
cs
.
lastNotify
==
nil
{
if
cs
.
lastNotify
==
nil
{
cs
.
lastNotify
=
cs
.
notify
cs
.
lastNotify
=
cs
.
notify
...
@@ -333,7 +339,8 @@ func (cs *ConsensusState) SaveNotify() {
...
@@ -333,7 +339,8 @@ func (cs *ConsensusState) SaveNotify() {
}
}
}
}
func
(
cs
*
ConsensusState
)
SetNotify
(
notify
*
dpostype
.
DPosNotify
){
// SetNotify method
func
(
cs
*
ConsensusState
)
SetNotify
(
notify
*
dpostype
.
DPosNotify
)
{
if
cs
.
notify
!=
nil
&&
!
bytes
.
Equal
(
cs
.
lastNotify
.
Signature
,
notify
.
Signature
)
{
if
cs
.
notify
!=
nil
&&
!
bytes
.
Equal
(
cs
.
lastNotify
.
Signature
,
notify
.
Signature
)
{
cs
.
lastNotify
=
cs
.
notify
cs
.
lastNotify
=
cs
.
notify
}
}
...
@@ -341,15 +348,18 @@ func (cs *ConsensusState) SetNotify(notify * dpostype.DPosNotify){
...
@@ -341,15 +348,18 @@ func (cs *ConsensusState) SetNotify(notify * dpostype.DPosNotify){
cs
.
notify
=
notify
cs
.
notify
=
notify
}
}
func
(
cs
*
ConsensusState
)
CacheNotify
(
notify
*
dpostype
.
DPosNotify
){
// CacheNotify method
func
(
cs
*
ConsensusState
)
CacheNotify
(
notify
*
dpostype
.
DPosNotify
)
{
cs
.
cachedNotify
=
notify
cs
.
cachedNotify
=
notify
}
}
func
(
cs
*
ConsensusState
)
ClearCachedNotify
(){
// ClearCachedNotify method
func
(
cs
*
ConsensusState
)
ClearCachedNotify
()
{
cs
.
cachedNotify
=
nil
cs
.
cachedNotify
=
nil
}
}
func
(
cs
*
ConsensusState
)
AddVotes
(
vote
*
dpostype
.
DPosVote
){
// AddVotes method
func
(
cs
*
ConsensusState
)
AddVotes
(
vote
*
dpostype
.
DPosVote
)
{
repeatFlag
:=
false
repeatFlag
:=
false
addrExistFlag
:=
false
addrExistFlag
:=
false
index
:=
-
1
index
:=
-
1
...
@@ -384,7 +394,8 @@ func (cs *ConsensusState) AddVotes(vote * dpostype.DPosVote){
...
@@ -384,7 +394,8 @@ func (cs *ConsensusState) AddVotes(vote * dpostype.DPosVote){
}
}
}
}
func
(
cs
*
ConsensusState
)
CacheVotes
(
vote
*
dpostype
.
DPosVote
){
// CacheVotes method
func
(
cs
*
ConsensusState
)
CacheVotes
(
vote
*
dpostype
.
DPosVote
)
{
repeatFlag
:=
false
repeatFlag
:=
false
addrExistFlag
:=
false
addrExistFlag
:=
false
index
:=
-
1
index
:=
-
1
...
@@ -410,18 +421,19 @@ func (cs *ConsensusState) CacheVotes(vote * dpostype.DPosVote){
...
@@ -410,18 +421,19 @@ func (cs *ConsensusState) CacheVotes(vote * dpostype.DPosVote){
cs
.
cachedVotes
=
append
(
cs
.
cachedVotes
,
vote
)
cs
.
cachedVotes
=
append
(
cs
.
cachedVotes
,
vote
)
}
else
if
vote
.
VoteTimestamp
>
cs
.
cachedVotes
[
index
]
.
VoteTimestamp
{
}
else
if
vote
.
VoteTimestamp
>
cs
.
cachedVotes
[
index
]
.
VoteTimestamp
{
/*
/*
if index == len(cs.cachedVotes) - 1 {
if index == len(cs.cachedVotes) - 1 {
cs.cachedVotes = append(cs.cachedVotes, vote)
cs.cachedVotes = append(cs.cachedVotes, vote)
}else {
}else {
cs.cachedVotes = append(cs.cachedVotes[:index], cs.dposVotes[(index + 1):]...)
cs.cachedVotes = append(cs.cachedVotes[:index], cs.dposVotes[(index + 1):]...)
cs.cachedVotes = append(cs.cachedVotes, vote)
cs.cachedVotes = append(cs.cachedVotes, vote)
}
}
*/
*/
cs
.
cachedVotes
[
index
]
=
vote
cs
.
cachedVotes
[
index
]
=
vote
}
}
}
}
func
(
cs
*
ConsensusState
)
CheckVotes
()(
ty
int
,
vote
*
dpostype
.
VoteItem
){
// CheckVotes method
func
(
cs
*
ConsensusState
)
CheckVotes
()
(
ty
int
,
vote
*
dpostype
.
VoteItem
)
{
major32
:=
int
(
dposDelegateNum
*
2
/
3
)
major32
:=
int
(
dposDelegateNum
*
2
/
3
)
//总的票数还不够2/3,先不做决定
//总的票数还不够2/3,先不做决定
...
@@ -429,9 +441,9 @@ func (cs *ConsensusState) CheckVotes()(ty int, vote * dpostype.VoteItem){
...
@@ -429,9 +441,9 @@ func (cs *ConsensusState) CheckVotes()(ty int, vote * dpostype.VoteItem){
return
continueToVote
,
nil
return
continueToVote
,
nil
}
}
voteStat
:=
map
[
string
]
int
{}
voteStat
:=
map
[
string
]
int
{}
for
i
:=
0
;
i
<
len
(
cs
.
dposVotes
);
i
++
{
for
i
:=
0
;
i
<
len
(
cs
.
dposVotes
);
i
++
{
key
:=
string
(
cs
.
dposVotes
[
i
]
.
VoteItem
.
VoteI
d
)
key
:=
string
(
cs
.
dposVotes
[
i
]
.
VoteItem
.
VoteI
D
)
if
_
,
ok
:=
voteStat
[
key
];
ok
{
if
_
,
ok
:=
voteStat
[
key
];
ok
{
voteStat
[
key
]
++
voteStat
[
key
]
++
}
else
{
}
else
{
...
@@ -452,11 +464,11 @@ func (cs *ConsensusState) CheckVotes()(ty int, vote * dpostype.VoteItem){
...
@@ -452,11 +464,11 @@ func (cs *ConsensusState) CheckVotes()(ty int, vote * dpostype.VoteItem){
//如果一个节点的投票数已经过2/3,则返回最终票数超过2/3的选票
//如果一个节点的投票数已经过2/3,则返回最终票数超过2/3的选票
if
value
>=
major32
{
if
value
>=
major32
{
for
i
:=
0
;
i
<
len
(
cs
.
dposVotes
);
i
++
{
for
i
:=
0
;
i
<
len
(
cs
.
dposVotes
);
i
++
{
if
key
==
string
(
cs
.
dposVotes
[
i
]
.
VoteItem
.
VoteI
d
)
{
if
key
==
string
(
cs
.
dposVotes
[
i
]
.
VoteItem
.
VoteI
D
)
{
return
voteSuccess
,
cs
.
dposVotes
[
i
]
.
VoteItem
return
voteSuccess
,
cs
.
dposVotes
[
i
]
.
VoteItem
}
}
}
}
}
else
if
(
value
+
(
int
(
dposDelegateNum
)
-
len
(
cs
.
dposVotes
)))
<
major32
{
}
else
if
(
value
+
(
int
(
dposDelegateNum
)
-
len
(
cs
.
dposVotes
)))
<
major32
{
//得票最多的节点,即使后续所有票都选它,也不满足2/3多数,不能达成共识。
//得票最多的节点,即使后续所有票都选它,也不满足2/3多数,不能达成共识。
return
voteFail
,
nil
return
voteFail
,
nil
}
}
...
@@ -464,17 +476,20 @@ func (cs *ConsensusState) CheckVotes()(ty int, vote * dpostype.VoteItem){
...
@@ -464,17 +476,20 @@ func (cs *ConsensusState) CheckVotes()(ty int, vote * dpostype.VoteItem){
return
continueToVote
,
nil
return
continueToVote
,
nil
}
}
func
(
cs
*
ConsensusState
)
ClearVotes
(){
// ClearVotes method
func
(
cs
*
ConsensusState
)
ClearVotes
()
{
cs
.
dposVotes
=
nil
cs
.
dposVotes
=
nil
cs
.
currentVote
=
nil
cs
.
currentVote
=
nil
cs
.
myVote
=
nil
cs
.
myVote
=
nil
}
}
// ClearCachedVotes method
func
(
cs
*
ConsensusState
)
ClearCachedVotes
()
{
func
(
cs
*
ConsensusState
)
ClearCachedVotes
()
{
cs
.
cachedVotes
=
nil
cs
.
cachedVotes
=
nil
}
}
func
(
cs
*
ConsensusState
)
VerifyVote
(
vote
*
dpostype
.
DPosVote
)
bool
{
// VerifyVote method
func
(
cs
*
ConsensusState
)
VerifyVote
(
vote
*
dpostype
.
DPosVote
)
bool
{
// Check validator
// Check validator
index
,
val
:=
cs
.
validatorMgr
.
Validators
.
GetByAddress
(
vote
.
VoterNodeAddress
)
index
,
val
:=
cs
.
validatorMgr
.
Validators
.
GetByAddress
(
vote
.
VoterNodeAddress
)
if
index
==
-
1
&&
val
==
nil
{
if
index
==
-
1
&&
val
==
nil
{
...
@@ -497,7 +512,8 @@ func (cs *ConsensusState)VerifyVote(vote * dpostype.DPosVote) bool{
...
@@ -497,7 +512,8 @@ func (cs *ConsensusState)VerifyVote(vote * dpostype.DPosVote) bool{
return
true
return
true
}
}
func
(
cs
*
ConsensusState
)
VerifyNotify
(
notify
*
dpostype
.
DPosNotify
)
bool
{
// VerifyNotify method
func
(
cs
*
ConsensusState
)
VerifyNotify
(
notify
*
dpostype
.
DPosNotify
)
bool
{
// Check validator
// Check validator
index
,
val
:=
cs
.
validatorMgr
.
Validators
.
GetByAddress
(
notify
.
NotifyNodeAddress
)
index
,
val
:=
cs
.
validatorMgr
.
Validators
.
GetByAddress
(
notify
.
NotifyNodeAddress
)
if
index
==
-
1
&&
val
==
nil
{
if
index
==
-
1
&&
val
==
nil
{
...
...
plugin/consensus/dpos/dpos.go
View file @
b08c65a3
...
@@ -22,24 +22,23 @@ import (
...
@@ -22,24 +22,23 @@ import (
const
tendermintVersion
=
"0.1.0"
const
tendermintVersion
=
"0.1.0"
var
(
var
(
dposlog
=
log15
.
New
(
"module"
,
"dpos"
)
dposlog
=
log15
.
New
(
"module"
,
"dpos"
)
genesis
string
genesis
string
genesisBlockTime
int64
genesisBlockTime
int64
timeoutCheckConnections
int32
=
1000
timeoutCheckConnections
int32
=
1000
timeoutVoting
int32
=
3000
timeoutVoting
int32
=
3000
timeoutWaitNotify
int32
=
2000
timeoutWaitNotify
int32
=
2000
createEmptyBlocks
=
false
createEmptyBlocks
=
false
createEmptyBlocksInterval
int32
// second
createEmptyBlocksInterval
int32
// second
validatorNodes
=
[]
string
{
"127.0.0.1:46656"
}
validatorNodes
=
[]
string
{
"127.0.0.1:46656"
}
isValidator
=
false
isValidator
=
false
dposDelegateNum
int64
=
3
//委托节点个数,从配置读取,以后可以根据投票结果来定
dposDelegateNum
int64
=
3
//委托节点个数,从配置读取,以后可以根据投票结果来定
dposBlockInterval
int64
=
3
//出块间隔,当前按3s
dposBlockInterval
int64
=
3
//出块间隔,当前按3s
dposContinueBlockNum
int64
=
6
//一个委托节点当选后,一次性持续出块数量
dposContinueBlockNum
int64
=
6
//一个委托节点当选后,一次性持续出块数量
dposCycle
=
int64
(
dposDelegateNum
*
dposBlockInterval
*
dposContinueBlockNum
)
dposCycle
=
int64
(
dposDelegateNum
*
dposBlockInterval
*
dposContinueBlockNum
)
dposPeriod
=
int64
(
dposBlockInterval
*
dposContinueBlockNum
)
dposPeriod
=
int64
(
dposBlockInterval
*
dposContinueBlockNum
)
zeroHash
[
32
]
byte
zeroHash
[
32
]
byte
)
)
func
init
()
{
func
init
()
{
...
@@ -221,7 +220,7 @@ OuterLoop:
...
@@ -221,7 +220,7 @@ OuterLoop:
for
!
DebugCatchup
{
for
!
DebugCatchup
{
select
{
select
{
case
<-
hint
.
C
:
case
<-
hint
.
C
:
dposlog
.
Info
(
"Still catching up max height......"
,
"cost"
,
time
.
Since
(
beg
))
dposlog
.
Info
(
"Still catching up max height......"
,
"cost"
,
time
.
Since
(
beg
))
default
:
default
:
if
client
.
IsCaughtUp
()
{
if
client
.
IsCaughtUp
()
{
dposlog
.
Info
(
"This node has caught up max height"
)
dposlog
.
Info
(
"This node has caught up max height"
)
...
@@ -328,10 +327,9 @@ func (client *Client) CreateBlock() {
...
@@ -328,10 +327,9 @@ func (client *Client) CreateBlock() {
return
return
}
}
}
else
{
}
else
{
dposlog
.
Info
(
"Ignore to create new Block for no tx in mempool"
,
"Height"
,
block
.
Height
+
1
)
dposlog
.
Info
(
"Ignore to create new Block for no tx in mempool"
,
"Height"
,
block
.
Height
+
1
)
}
}
return
return
}
}
//check dup
//check dup
...
@@ -368,15 +366,17 @@ func (client *Client) CheckTxDup(txs []*types.Transaction, height int64) (transa
...
@@ -368,15 +366,17 @@ func (client *Client) CheckTxDup(txs []*types.Transaction, height int64) (transa
return
types
.
CacheToTxs
(
cacheTxs
)
return
types
.
CacheToTxs
(
cacheTxs
)
}
}
func
(
client
*
Client
)
SetBlockTime
(
blockTime
int64
)
{
// SetBlockTime set current block time to generate new block
func
(
client
*
Client
)
SetBlockTime
(
blockTime
int64
)
{
client
.
blockTime
=
blockTime
client
.
blockTime
=
blockTime
}
}
func
(
client
*
Client
)
ValidatorIndex
()
int
{
// ValidatorIndex get the index of local this validator if it's
func
(
client
*
Client
)
ValidatorIndex
()
int
{
if
client
.
isDelegator
{
if
client
.
isDelegator
{
index
,
_
:=
client
.
csState
.
validatorMgr
.
Validators
.
GetByAddress
(
client
.
privValidator
.
GetAddress
())
index
,
_
:=
client
.
csState
.
validatorMgr
.
Validators
.
GetByAddress
(
client
.
privValidator
.
GetAddress
())
return
index
return
index
}
}
return
-
1
return
-
1
}
}
\ No newline at end of file
plugin/consensus/dpos/node.go
View file @
b08c65a3
...
@@ -42,7 +42,7 @@ const (
...
@@ -42,7 +42,7 @@ const (
minReadBufferSize
=
1024
minReadBufferSize
=
1024
minWriteBufferSize
=
65536
minWriteBufferSize
=
65536
)
)
// Parallel method
// Parallel method
func
Parallel
(
tasks
...
func
())
{
func
Parallel
(
tasks
...
func
())
{
...
@@ -301,7 +301,6 @@ func (node *Node) StartConsensusRoutine() {
...
@@ -301,7 +301,6 @@ func (node *Node) StartConsensusRoutine() {
}
}
}
}
// BroadcastRoutine receive to broadcast
// BroadcastRoutine receive to broadcast
func
(
node
*
Node
)
BroadcastRoutine
()
{
func
(
node
*
Node
)
BroadcastRoutine
()
{
for
{
for
{
...
...
plugin/consensus/dpos/peer_set.go
View file @
b08c65a3
...
@@ -85,7 +85,7 @@ type peerConn struct {
...
@@ -85,7 +85,7 @@ type peerConn struct {
onPeerError
func
(
Peer
,
interface
{})
onPeerError
func
(
Peer
,
interface
{})
myState
*
ConsensusState
myState
*
ConsensusState
}
}
// PeerSet struct
// PeerSet struct
...
@@ -530,6 +530,7 @@ FOR_LOOP:
...
@@ -530,6 +530,7 @@ FOR_LOOP:
// Drain
// Drain
}
}
}
}
// StackError struct
// StackError struct
type
StackError
struct
{
type
StackError
struct
{
Err
interface
{}
Err
interface
{}
...
...
plugin/consensus/dpos/proto/dpos_msg.proto
View file @
b08c65a3
...
@@ -11,7 +11,7 @@ message VoteItem {
...
@@ -11,7 +11,7 @@ message VoteItem {
int64
periodStart
=
5
;
//新节点负责出块的起始时间
int64
periodStart
=
5
;
//新节点负责出块的起始时间
int64
periodStop
=
6
;
//新节点负责出块的终止时间
int64
periodStop
=
6
;
//新节点负责出块的终止时间
int64
height
=
7
;
//新节点负责出块的起始高度
int64
height
=
7
;
//新节点负责出块的起始高度
bytes
voteI
d
=
8
;
//选票ID
bytes
voteI
D
=
8
;
//选票ID
}
}
//DPosVote Dpos共识的节点投票,为达成共识用。
//DPosVote Dpos共识的节点投票,为达成共识用。
...
...
plugin/consensus/dpos/secret_connection.go
View file @
b08c65a3
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// license that can be found in the LICENSE file.
// Package
tendermint
Uses nacl's secret_box to encrypt a net.Conn.
// Package
dpos
Uses nacl's secret_box to encrypt a net.Conn.
// It is (meant to be) an implementation of the STS protocol.
// It is (meant to be) an implementation of the STS protocol.
// Note we do not (yet) assume that a remote peer's pubkey
// Note we do not (yet) assume that a remote peer's pubkey
// is known ahead of time, and thus we are technically
// is known ahead of time, and thus we are technically
...
...
plugin/consensus/dpos/state_machine.go
View file @
b08c65a3
...
@@ -10,113 +10,126 @@ import (
...
@@ -10,113 +10,126 @@ import (
"github.com/outbrain/golib/math"
"github.com/outbrain/golib/math"
"time"
"time"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/common/crypto"
dpostype
"github.com/33cn/plugin/plugin/consensus/dpos/types"
dpostype
"github.com/33cn/plugin/plugin/consensus/dpos/types"
"github.com/33cn/chain33/common"
)
)
var
(
var
(
InitStateType
=
1
// InitStateType 为状态机的初始状态
VotingStateType
=
2
InitStateType
=
1
VotedStateType
=
3
// VotingStateType 为状态机的投票状态
VotingStateType
=
2
// VotedStateType 为状态机的已投票状态
VotedStateType
=
3
// WaitNotifyStateType 为状态机的等待通知状态
WaitNotifyStateType
=
4
WaitNotifyStateType
=
4
StateTypeMapping
=
map
[
int
]
string
{
InitStateType
:
"InitState"
,
VotingStateType
:
"VotingState"
,
VotedStateType
:
"VotedState"
,
WaitNotifyStateType
:
"WaitNotifyState"
}
// StateTypeMapping 为状态的整型值和字符串值的对应关系
StateTypeMapping
=
map
[
int
]
string
{
InitStateType
:
"InitState"
,
VotingStateType
:
"VotingState"
,
VotedStateType
:
"VotedState"
,
WaitNotifyStateType
:
"WaitNotifyState"
}
)
)
// InitStateObj is the InitState obj
var
InitStateObj
=
&
InitState
{}
// VotingStateObj is the VotingState obj
var
VotingStateObj
=
&
VotingState
{}
// VotedStateObj is the VotedState obj
var
VotedStateObj
=
&
VotedState
{}
// WaitNotifyStateObj is the WaitNotifyState obj
var
WaitNotifyStateObj
=
&
WaitNofifyState
{}
// DPosTask 为计算当前时间所属周期的数据结构
type
DPosTask
struct
{
type
DPosTask
struct
{
nodeI
d
int64
nodeI
D
int64
cycleStart
int64
cycleStart
int64
cycleStop
int64
cycleStop
int64
periodStart
int64
periodStart
int64
periodStop
int64
periodStop
int64
blockStart
int64
blockStart
int64
blockStop
int64
blockStop
int64
}
}
func
DecideTaskByTime
(
now
int64
)
(
task
DPosTask
){
// DecideTaskByTime 根据时间戳计算所属的周期,包括cycle周期,负责出块周期,当前出块周期
task
.
nodeId
=
now
%
dposCycle
/
dposPeriod
func
DecideTaskByTime
(
now
int64
)
(
task
DPosTask
)
{
task
.
nodeID
=
now
%
dposCycle
/
dposPeriod
task
.
cycleStart
=
now
-
now
%
dposCycle
task
.
cycleStart
=
now
-
now
%
dposCycle
task
.
cycleStop
=
task
.
cycleStart
+
dposCycle
-
1
task
.
cycleStop
=
task
.
cycleStart
+
dposCycle
-
1
task
.
periodStart
=
task
.
cycleStart
+
task
.
nodeI
d
*
dposBlockInterval
*
dposContinueBlockNum
task
.
periodStart
=
task
.
cycleStart
+
task
.
nodeI
D
*
dposBlockInterval
*
dposContinueBlockNum
task
.
periodStop
=
task
.
periodStart
+
dposPeriod
-
1
task
.
periodStop
=
task
.
periodStart
+
dposPeriod
-
1
task
.
blockStart
=
task
.
periodStart
+
now
%
dposCycle
%
dposPeriod
/
dposBlockInterval
*
dposBlockInterval
task
.
blockStart
=
task
.
periodStart
+
now
%
dposCycle
%
dposPeriod
/
dposBlockInterval
*
dposBlockInterval
task
.
blockStop
=
task
.
blockStart
+
dposBlockInterval
-
1
task
.
blockStop
=
task
.
blockStart
+
dposBlockInterval
-
1
return
task
return
task
}
}
// DposState is the base class of dpos state machine, it defines some interfaces.
// DposState is the base class of dpos state machine, it defines some interfaces.
type
DposState
interface
{
type
DposState
interface
{
timeOut
(
cs
*
ConsensusState
)
timeOut
(
cs
*
ConsensusState
)
sendVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
sendVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
recvVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
recvVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
sendVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
sendVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
recvVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
recvVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
sendNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
sendNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
recvNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
recvNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
}
}
// InitState is the initial state of dpos state machine
// InitState is the initial state of dpos state machine
type
InitState
struct
{
type
InitState
struct
{
}
}
func
(
init
*
InitState
)
timeOut
(
cs
*
ConsensusState
)
{
func
(
init
*
InitState
)
timeOut
(
cs
*
ConsensusState
)
{
//if available noes < 2/3, don't change the state to voting.
//if available noes < 2/3, don't change the state to voting.
connections
:=
cs
.
client
.
node
.
peerSet
.
Size
()
connections
:=
cs
.
client
.
node
.
peerSet
.
Size
()
validators
:=
cs
.
validatorMgr
.
Validators
.
Size
()
validators
:=
cs
.
validatorMgr
.
Validators
.
Size
()
if
connections
==
0
||
connections
<
(
validators
*
2
/
3
-
1
)
{
if
connections
==
0
||
connections
<
(
validators
*
2
/
3
-
1
)
{
dposlog
.
Error
(
"InitState timeout but available nodes less than 2/3,waiting for more connections"
,
"connections"
,
connections
,
"validators"
,
validators
)
dposlog
.
Error
(
"InitState timeout but available nodes less than 2/3,waiting for more connections"
,
"connections"
,
connections
,
"validators"
,
validators
)
cs
.
ClearVotes
()
cs
.
ClearVotes
()
//设定超时时间,超时后再检查链接数量
//设定超时时间,超时后再检查链接数量
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
}
else
{
}
else
{
//获得当前高度
//获得当前高度
height
:=
cs
.
client
.
GetCurrentHeight
()
height
:=
cs
.
client
.
GetCurrentHeight
()
now
:=
time
.
Now
()
.
Unix
()
now
:=
time
.
Now
()
.
Unix
()
if
cs
.
lastMyVote
!=
nil
&&
math
.
AbsInt64
(
now
-
cs
.
lastMyVote
.
VoteItem
.
PeriodStop
)
<=
1
{
if
cs
.
lastMyVote
!=
nil
&&
math
.
AbsInt64
(
now
-
cs
.
lastMyVote
.
VoteItem
.
PeriodStop
)
<=
1
{
now
+=
2
now
+=
2
}
}
//计算当前时间,属于哪一个周期,应该哪一个节点出块,应该出块的高度
//计算当前时间,属于哪一个周期,应该哪一个节点出块,应该出块的高度
task
:=
DecideTaskByTime
(
now
)
task
:=
DecideTaskByTime
(
now
)
addr
,
validator
:=
cs
.
validatorMgr
.
Validators
.
GetByIndex
(
int
(
task
.
nodeI
d
))
addr
,
validator
:=
cs
.
validatorMgr
.
Validators
.
GetByIndex
(
int
(
task
.
nodeI
D
))
if
addr
==
nil
&&
validator
==
nil
{
if
addr
==
nil
&&
validator
==
nil
{
dposlog
.
Error
(
"Address and Validator is nil"
,
"node index"
,
task
.
nodeI
d
,
"now"
,
now
,
"cycle"
,
dposCycle
,
"period"
,
dposPeriod
)
dposlog
.
Error
(
"Address and Validator is nil"
,
"node index"
,
task
.
nodeI
D
,
"now"
,
now
,
"cycle"
,
dposCycle
,
"period"
,
dposPeriod
)
//cs.SetState(InitStateObj)
//cs.SetState(InitStateObj)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
return
return
}
}
//生成vote, 对于vote进行签名
//生成vote, 对于vote进行签名
voteItem
:=
&
dpostype
.
VoteItem
{
voteItem
:=
&
dpostype
.
VoteItem
{
VotedNodeAddress
:
addr
,
VotedNodeAddress
:
addr
,
VotedNodeIndex
:
int32
(
task
.
nodeId
),
VotedNodeIndex
:
int32
(
task
.
nodeID
),
CycleStart
:
task
.
cycleStart
,
CycleStart
:
task
.
cycleStart
,
CycleStop
:
task
.
cycleStop
,
CycleStop
:
task
.
cycleStop
,
PeriodStart
:
task
.
periodStart
,
PeriodStart
:
task
.
periodStart
,
PeriodStop
:
task
.
periodStop
,
PeriodStop
:
task
.
periodStop
,
Height
:
height
+
1
,
Height
:
height
+
1
,
}
}
encode
,
err
:=
json
.
Marshal
(
voteItem
)
encode
,
err
:=
json
.
Marshal
(
voteItem
)
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
"Marshal vote failed."
)
panic
(
"Marshal vote failed."
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
//cs.scheduleDPosTimeout(time.Duration(timeoutCheckConnections)*
time.Millisecond, InitStateType)
return
//
return
}
}
voteItem
.
VoteI
d
=
crypto
.
Ripemd160
(
encode
)
voteItem
.
VoteI
D
=
crypto
.
Ripemd160
(
encode
)
index
:=
-
1
index
:=
-
1
for
i
:=
0
;
i
<
cs
.
validatorMgr
.
Validators
.
Size
();
i
++
{
for
i
:=
0
;
i
<
cs
.
validatorMgr
.
Validators
.
Size
();
i
++
{
if
bytes
.
Compare
(
cs
.
validatorMgr
.
Validators
.
Validators
[
i
]
.
Address
,
cs
.
privValidator
.
GetAddress
())
==
0
{
if
bytes
.
Compare
(
cs
.
validatorMgr
.
Validators
.
Validators
[
i
]
.
Address
,
cs
.
privValidator
.
GetAddress
())
==
0
{
index
=
i
index
=
i
break
break
...
@@ -128,17 +141,17 @@ func (init *InitState)timeOut(cs *ConsensusState) {
...
@@ -128,17 +141,17 @@ func (init *InitState)timeOut(cs *ConsensusState) {
}
}
vote
:=
&
dpostype
.
Vote
{
DPosVote
:
&
dpostype
.
DPosVote
{
vote
:=
&
dpostype
.
Vote
{
DPosVote
:
&
dpostype
.
DPosVote
{
VoteItem
:
voteItem
,
VoteItem
:
voteItem
,
VoteTimestamp
:
now
,
VoteTimestamp
:
now
,
VoterNodeAddress
:
cs
.
privValidator
.
GetAddress
(),
VoterNodeAddress
:
cs
.
privValidator
.
GetAddress
(),
VoterNodeIndex
:
int32
(
index
),
VoterNodeIndex
:
int32
(
index
),
},
},
}
}
if
err
:=
cs
.
privValidator
.
SignVote
(
cs
.
validatorMgr
.
ChainID
,
vote
);
err
!=
nil
{
if
err
:=
cs
.
privValidator
.
SignVote
(
cs
.
validatorMgr
.
ChainID
,
vote
);
err
!=
nil
{
dposlog
.
Error
(
"SignVote failed"
,
"vote"
,
vote
.
String
())
dposlog
.
Error
(
"SignVote failed"
,
"vote"
,
vote
.
String
())
//cs.SetState(InitStateObj)
//cs.SetState(InitStateObj)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
return
return
}
}
...
@@ -152,7 +165,7 @@ func (init *InitState)timeOut(cs *ConsensusState) {
...
@@ -152,7 +165,7 @@ func (init *InitState)timeOut(cs *ConsensusState) {
//通过node发送p2p消息到其他节点
//通过node发送p2p消息到其他节点
cs
.
dposState
.
sendVote
(
cs
,
vote
.
DPosVote
)
cs
.
dposState
.
sendVote
(
cs
,
vote
.
DPosVote
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutVoting
)
*
time
.
Millisecond
,
VotingStateType
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutVoting
)
*
time
.
Millisecond
,
VotingStateType
)
//处理之前缓存的投票信息
//处理之前缓存的投票信息
for
i
:=
0
;
i
<
len
(
cs
.
cachedVotes
);
i
++
{
for
i
:=
0
;
i
<
len
(
cs
.
cachedVotes
);
i
++
{
cs
.
dposState
.
recvVote
(
cs
,
cs
.
cachedVotes
[
i
])
cs
.
dposState
.
recvVote
(
cs
,
cs
.
cachedVotes
[
i
])
...
@@ -161,29 +174,29 @@ func (init *InitState)timeOut(cs *ConsensusState) {
...
@@ -161,29 +174,29 @@ func (init *InitState)timeOut(cs *ConsensusState) {
}
}
}
}
func
(
init
*
InitState
)
sendVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
func
(
init
*
InitState
)
sendVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
dposlog
.
Info
(
"InitState does not support sendVote,so do nothing"
)
dposlog
.
Info
(
"InitState does not support sendVote,so do nothing"
)
}
}
func
(
init
*
InitState
)
recvVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
func
(
init
*
InitState
)
recvVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
dposlog
.
Info
(
"InitState recvVote ,add it and will handle it later."
)
dposlog
.
Info
(
"InitState recvVote ,add it and will handle it later."
)
//cs.AddVotes(vote)
//cs.AddVotes(vote)
cs
.
CacheVotes
(
vote
)
cs
.
CacheVotes
(
vote
)
}
}
func
(
init
*
InitState
)
sendVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
func
(
init
*
InitState
)
sendVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
dposlog
.
Info
(
"InitState don't support sendVoteReply,so do nothing"
)
dposlog
.
Info
(
"InitState don't support sendVoteReply,so do nothing"
)
}
}
func
(
init
*
InitState
)
recvVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
func
(
init
*
InitState
)
recvVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
dposlog
.
Info
(
"InitState recv Vote reply,ignore it."
)
dposlog
.
Info
(
"InitState recv Vote reply,ignore it."
)
}
}
func
(
init
*
InitState
)
sendNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
func
(
init
*
InitState
)
sendNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
dposlog
.
Info
(
"InitState does not support sendNotify,so do nothing"
)
dposlog
.
Info
(
"InitState does not support sendNotify,so do nothing"
)
}
}
func
(
init
*
InitState
)
recvNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
func
(
init
*
InitState
)
recvNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
dposlog
.
Info
(
"InitState recvNotify"
)
dposlog
.
Info
(
"InitState recvNotify"
)
//zzh:需要增加对Notify的处理,可以考虑记录已经确认过的出快记录
//zzh:需要增加对Notify的处理,可以考虑记录已经确认过的出快记录
...
@@ -192,10 +205,9 @@ func (init *InitState)recvNotify(cs *ConsensusState, notify * dpostype.DPosNotif
...
@@ -192,10 +205,9 @@ func (init *InitState)recvNotify(cs *ConsensusState, notify * dpostype.DPosNotif
// VotingState is the voting state of dpos state machine until timeout or get an agreement by votes.
// VotingState is the voting state of dpos state machine until timeout or get an agreement by votes.
type
VotingState
struct
{
type
VotingState
struct
{
}
}
func
(
voting
*
VotingState
)
timeOut
(
cs
*
ConsensusState
)
{
func
(
voting
*
VotingState
)
timeOut
(
cs
*
ConsensusState
)
{
dposlog
.
Info
(
"VotingState timeout but don't get an agreement. change state to InitState"
)
dposlog
.
Info
(
"VotingState timeout but don't get an agreement. change state to InitState"
)
//清理掉之前的选票记录,从初始状态重新开始
//清理掉之前的选票记录,从初始状态重新开始
...
@@ -205,27 +217,27 @@ func (voting *VotingState)timeOut(cs *ConsensusState) {
...
@@ -205,27 +217,27 @@ func (voting *VotingState)timeOut(cs *ConsensusState) {
dposlog
.
Info
(
"Change state because of timeOut."
,
"from"
,
"VotingState"
,
"to"
,
"InitState"
)
dposlog
.
Info
(
"Change state because of timeOut."
,
"from"
,
"VotingState"
,
"to"
,
"InitState"
)
//由于连接多数情况下正常,快速触发InitState的超时处理
//由于连接多数情况下正常,快速触发InitState的超时处理
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
}
}
func
(
voting
*
VotingState
)
sendVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
func
(
voting
*
VotingState
)
sendVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
cs
.
broadcastChannel
<-
MsgInfo
{
TypeID
:
dpostype
.
VoteID
,
Msg
:
vote
,
PeerID
:
cs
.
ourID
,
PeerIP
:
""
}
cs
.
broadcastChannel
<-
MsgInfo
{
TypeID
:
dpostype
.
VoteID
,
Msg
:
vote
,
PeerID
:
cs
.
ourID
,
PeerIP
:
""
}
}
}
func
(
voting
*
VotingState
)
recvVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
func
(
voting
*
VotingState
)
recvVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
dposlog
.
Info
(
"VotingState get a vote"
,
"VotedNodeIndex"
,
vote
.
VoteItem
.
VotedNodeIndex
,
dposlog
.
Info
(
"VotingState get a vote"
,
"VotedNodeIndex"
,
vote
.
VoteItem
.
VotedNodeIndex
,
"VotedNodeAddress"
,
common
.
ToHex
(
vote
.
VoteItem
.
VotedNodeAddress
),
"VotedNodeAddress"
,
common
.
ToHex
(
vote
.
VoteItem
.
VotedNodeAddress
),
"CycleStart"
,
vote
.
VoteItem
.
CycleStart
,
"CycleStart"
,
vote
.
VoteItem
.
CycleStart
,
"CycleStop"
,
vote
.
VoteItem
.
CycleStop
,
"CycleStop"
,
vote
.
VoteItem
.
CycleStop
,
"PeriodStart"
,
vote
.
VoteItem
.
PeriodStart
,
"PeriodStart"
,
vote
.
VoteItem
.
PeriodStart
,
"periodStop"
,
vote
.
VoteItem
.
PeriodStop
,
"periodStop"
,
vote
.
VoteItem
.
PeriodStop
,
"Height"
,
vote
.
VoteItem
.
Height
,
"Height"
,
vote
.
VoteItem
.
Height
,
"VoteId"
,
common
.
ToHex
(
vote
.
VoteItem
.
VoteId
),
"VoteID"
,
common
.
ToHex
(
vote
.
VoteItem
.
VoteID
),
"VoteTimestamp"
,
vote
.
VoteTimestamp
,
"VoteTimestamp"
,
vote
.
VoteTimestamp
,
"VoterNodeIndex"
,
vote
.
VoterNodeIndex
,
"VoterNodeIndex"
,
vote
.
VoterNodeIndex
,
"VoterNodeAddress"
,
common
.
ToHex
(
vote
.
VoterNodeAddress
),
"VoterNodeAddress"
,
common
.
ToHex
(
vote
.
VoterNodeAddress
),
"Signature"
,
common
.
ToHex
(
vote
.
Signature
),
"Signature"
,
common
.
ToHex
(
vote
.
Signature
),
"localNodeIndex"
,
cs
.
client
.
ValidatorIndex
(),
"now"
,
time
.
Now
()
.
Unix
())
"localNodeIndex"
,
cs
.
client
.
ValidatorIndex
(),
"now"
,
time
.
Now
()
.
Unix
())
if
!
cs
.
VerifyVote
(
vote
)
{
if
!
cs
.
VerifyVote
(
vote
)
{
dposlog
.
Info
(
"VotingState verify vote failed"
)
dposlog
.
Info
(
"VotingState verify vote failed"
)
...
@@ -246,7 +258,7 @@ func (voting *VotingState)recvVote(cs *ConsensusState,vote * dpostype.DPosVote)
...
@@ -246,7 +258,7 @@ func (voting *VotingState)recvVote(cs *ConsensusState,vote * dpostype.DPosVote)
cs
.
SetCurrentVote
(
voteItem
)
cs
.
SetCurrentVote
(
voteItem
)
//1s后检查是否出块,是否需要重新投票
//1s后检查是否出块,是否需要重新投票
cs
.
scheduleDPosTimeout
(
time
.
Millisecond
*
500
,
VotedStateType
)
cs
.
scheduleDPosTimeout
(
time
.
Millisecond
*
500
,
VotedStateType
)
}
else
if
result
==
continueToVote
{
}
else
if
result
==
continueToVote
{
dposlog
.
Info
(
"VotingState get a vote, but don't get an agreement,waiting for new votes..."
)
dposlog
.
Info
(
"VotingState get a vote, but don't get an agreement,waiting for new votes..."
)
}
else
{
}
else
{
...
@@ -255,34 +267,33 @@ func (voting *VotingState)recvVote(cs *ConsensusState,vote * dpostype.DPosVote)
...
@@ -255,34 +267,33 @@ func (voting *VotingState)recvVote(cs *ConsensusState,vote * dpostype.DPosVote)
cs
.
ClearVotes
()
cs
.
ClearVotes
()
cs
.
SetState
(
InitStateObj
)
cs
.
SetState
(
InitStateObj
)
dposlog
.
Info
(
"Change state because of vote failed."
,
"from"
,
"VotingState"
,
"to"
,
"InitState"
)
dposlog
.
Info
(
"Change state because of vote failed."
,
"from"
,
"VotingState"
,
"to"
,
"InitState"
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
}
}
}
}
func
(
voting
*
VotingState
)
sendVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
func
(
voting
*
VotingState
)
sendVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
dposlog
.
Info
(
"VotingState don't support sendVoteReply,so do nothing"
)
dposlog
.
Info
(
"VotingState don't support sendVoteReply,so do nothing"
)
//cs.broadcastChannel <- MsgInfo{TypeID: dpostype.VoteReplyID, Msg: reply, PeerID: cs.ourID, PeerIP: ""}
//cs.broadcastChannel <- MsgInfo{TypeID: dpostype.VoteReplyID, Msg: reply, PeerID: cs.ourID, PeerIP: ""}
}
}
func
(
voting
*
VotingState
)
recvVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
func
(
voting
*
VotingState
)
recvVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
dposlog
.
Info
(
"VotingState recv Vote reply"
)
dposlog
.
Info
(
"VotingState recv Vote reply"
)
voting
.
recvVote
(
cs
,
reply
.
Vote
)
voting
.
recvVote
(
cs
,
reply
.
Vote
)
}
}
func
(
voting
*
VotingState
)
sendNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
func
(
voting
*
VotingState
)
sendNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
dposlog
.
Info
(
"VotingState does not support sendNotify,so do nothing"
)
dposlog
.
Info
(
"VotingState does not support sendNotify,so do nothing"
)
}
}
func
(
voting
*
VotingState
)
recvNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
func
(
voting
*
VotingState
)
recvNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
dposlog
.
Info
(
"VotingState does not support recvNotify,so do nothing"
)
dposlog
.
Info
(
"VotingState does not support recvNotify,so do nothing"
)
}
}
// Vot
ingState is the voting state of dpos state machine until timeout or get an agreement by votes.
// Vot
edState is the voted state of dpos state machine after getting an agreement for a period
type
VotedState
struct
{
type
VotedState
struct
{
}
}
func
(
voted
*
VotedState
)
timeOut
(
cs
*
ConsensusState
)
{
func
(
voted
*
VotedState
)
timeOut
(
cs
*
ConsensusState
)
{
now
:=
time
.
Now
()
.
Unix
()
now
:=
time
.
Now
()
.
Unix
()
block
:=
cs
.
client
.
GetCurrentBlock
()
block
:=
cs
.
client
.
GetCurrentBlock
()
task
:=
DecideTaskByTime
(
now
)
task
:=
DecideTaskByTime
(
now
)
...
@@ -294,14 +305,14 @@ func (voted *VotedState)timeOut(cs *ConsensusState) {
...
@@ -294,14 +305,14 @@ func (voted *VotedState)timeOut(cs *ConsensusState) {
dposlog
.
Info
(
"VotedState timeOut over periodStop."
,
"periodStop"
,
cs
.
currentVote
.
PeriodStop
)
dposlog
.
Info
(
"VotedState timeOut over periodStop."
,
"periodStop"
,
cs
.
currentVote
.
PeriodStop
)
//当前时间超过了节点切换时间,需要进行重新投票
//当前时间超过了节点切换时间,需要进行重新投票
notify
:=
&
dpostype
.
Notify
{
notify
:=
&
dpostype
.
Notify
{
DPosNotify
:
&
dpostype
.
DPosNotify
{
DPosNotify
:
&
dpostype
.
DPosNotify
{
Vote
:
cs
.
currentVote
,
Vote
:
cs
.
currentVote
,
HeightStop
:
block
.
Height
,
HeightStop
:
block
.
Height
,
HashStop
:
block
.
Hash
(),
HashStop
:
block
.
Hash
(),
NotifyTimestamp
:
now
,
NotifyTimestamp
:
now
,
NotifyNodeAddress
:
cs
.
privValidator
.
GetAddress
(),
NotifyNodeAddress
:
cs
.
privValidator
.
GetAddress
(),
NotifyNodeIndex
:
int32
(
cs
.
privValidatorIndex
),
NotifyNodeIndex
:
int32
(
cs
.
privValidatorIndex
),
},
},
}
}
dposlog
.
Info
(
"Send notify."
,
"HeightStop"
,
notify
.
HeightStop
,
"HashStop"
,
common
.
ToHex
(
notify
.
HashStop
))
dposlog
.
Info
(
"Send notify."
,
"HeightStop"
,
notify
.
HeightStop
,
"HashStop"
,
common
.
ToHex
(
notify
.
HashStop
))
...
@@ -330,47 +341,45 @@ func (voted *VotedState)timeOut(cs *ConsensusState) {
...
@@ -330,47 +341,45 @@ func (voted *VotedState)timeOut(cs *ConsensusState) {
dposlog
.
Info
(
"Change state because of time."
,
"from"
,
"VotedState"
,
"to"
,
"InitState"
)
dposlog
.
Info
(
"Change state because of time."
,
"from"
,
"VotedState"
,
"to"
,
"InitState"
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
return
return
}
else
{
}
//如果区块未同步,则等待;如果区块已同步,则进行后续正常出块的判断和处理。
//如果区块未同步,则等待;如果区块已同步,则进行后续正常出块的判断和处理。
if
block
.
Height
+
1
<
cs
.
currentVote
.
Height
{
if
block
.
Height
+
1
<
cs
.
currentVote
.
Height
{
dposlog
.
Info
(
"VotedState timeOut but block is not sync,wait..."
,
"localHeight"
,
block
.
Height
,
"vote height"
,
cs
.
currentVote
.
Height
)
dposlog
.
Info
(
"VotedState timeOut but block is not sync,wait..."
,
"localHeight"
,
block
.
Height
,
"vote height"
,
cs
.
currentVote
.
Height
)
cs
.
scheduleDPosTimeout
(
time
.
Second
*
1
,
VotedStateType
)
cs
.
scheduleDPosTimeout
(
time
.
Second
*
1
,
VotedStateType
)
return
return
}
}
//当前时间未到节点切换时间,则继续进行出块判断
if
block
.
BlockTime
>=
task
.
blockStop
{
//已出块,或者时间落后了。
dposlog
.
Info
(
"VotedState timeOut but block already is generated."
,
"blocktime"
,
block
.
BlockTime
,
"blockStop"
,
task
.
blockStop
,
"now"
,
now
)
cs
.
scheduleDPosTimeout
(
time
.
Second
*
1
,
VotedStateType
)
return
//当前时间未到节点切换时间,则继续进行出块判断
}
else
if
block
.
BlockTime
<
task
.
blockStart
{
if
block
.
BlockTime
>=
task
.
blockStop
{
//本出块周期尚未出块,则进行出块
//已出块,或者时间落后了。
if
task
.
blockStop
-
now
<=
1
{
dposlog
.
Info
(
"VotedState timeOut but block already is generated."
,
"blocktime"
,
block
.
BlockTime
,
"blockStop"
,
task
.
blockStop
,
"now"
,
now
)
dposlog
.
Info
(
"Create new block."
,
"height"
,
block
.
Height
+
1
)
cs
.
scheduleDPosTimeout
(
time
.
Second
*
1
,
VotedStateType
)
cs
.
client
.
SetBlockTime
(
task
.
blockStop
)
cs
.
client
.
CreateBlock
()
cs
.
scheduleDPosTimeout
(
time
.
Millisecond
*
500
,
VotedStateType
)
return
}
else
{
dposlog
.
Info
(
"Wait time to create block near blockStop."
,
"waittime"
,
task
.
blockStop
-
now
-
1
)
//cs.scheduleDPosTimeout(time.Second * time.Duration(task.blockStop - now - 1), VotedStateType)
cs
.
scheduleDPosTimeout
(
time
.
Millisecond
*
500
,
VotedStateType
)
return
}
}
else
{
return
//本周期已经出块
}
else
if
block
.
BlockTime
<
task
.
blockStart
{
dposlog
.
Info
(
"Wait to next block cycle."
,
"waittime"
,
task
.
blockStop
-
now
+
1
)
//本出块周期尚未出块,则进行出块
if
task
.
blockStop
-
now
<=
1
{
dposlog
.
Info
(
"Create new block."
,
"height"
,
block
.
Height
+
1
)
//cs.scheduleDPosTimeout(time.Second * time.Duration(task.blockStop-now+1), VotedStateType)
cs
.
client
.
SetBlockTime
(
task
.
blockStop
)
cs
.
client
.
CreateBlock
()
cs
.
scheduleDPosTimeout
(
time
.
Millisecond
*
500
,
VotedStateType
)
cs
.
scheduleDPosTimeout
(
time
.
Millisecond
*
500
,
VotedStateType
)
return
return
}
}
dposlog
.
Info
(
"Wait time to create block near blockStop."
)
cs
.
scheduleDPosTimeout
(
time
.
Millisecond
*
500
,
VotedStateType
)
return
}
else
{
//本周期已经出块
dposlog
.
Info
(
"Wait to next block cycle."
,
"waittime"
,
task
.
blockStop
-
now
+
1
)
//cs.scheduleDPosTimeout(time.Second * time.Duration(task.blockStop-now+1), VotedStateType)
cs
.
scheduleDPosTimeout
(
time
.
Millisecond
*
500
,
VotedStateType
)
return
}
}
}
else
{
}
else
{
dposlog
.
Info
(
"This node is not current owner."
,
"current owner index"
,
cs
.
currentVote
.
VotedNodeIndex
,
"this node index"
,
cs
.
client
.
ValidatorIndex
())
dposlog
.
Info
(
"This node is not current owner."
,
"current owner index"
,
cs
.
currentVote
.
VotedNodeIndex
,
"this node index"
,
cs
.
client
.
ValidatorIndex
())
//非当前出块节点,如果到了切换出块节点的时间,则进行状态切换,进行投票
//非当前出块节点,如果到了切换出块节点的时间,则进行状态切换,进行投票
...
@@ -381,36 +390,36 @@ func (voted *VotedState)timeOut(cs *ConsensusState) {
...
@@ -381,36 +390,36 @@ func (voted *VotedState)timeOut(cs *ConsensusState) {
cs
.
ClearVotes
()
cs
.
ClearVotes
()
cs
.
SetState
(
WaitNotifyStateObj
)
cs
.
SetState
(
WaitNotifyStateObj
)
dposlog
.
Info
(
"Change state because of time."
,
"from"
,
"VotedState"
,
"to"
,
"WaitNotifyState"
)
dposlog
.
Info
(
"Change state because of time."
,
"from"
,
"VotedState"
,
"to"
,
"WaitNotifyState"
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutWaitNotify
)
*
time
.
Millisecond
,
WaitNotifyStateType
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutWaitNotify
)
*
time
.
Millisecond
,
WaitNotifyStateType
)
if
cs
.
cachedNotify
!=
nil
{
if
cs
.
cachedNotify
!=
nil
{
cs
.
dposState
.
recvNotify
(
cs
,
cs
.
cachedNotify
)
cs
.
dposState
.
recvNotify
(
cs
,
cs
.
cachedNotify
)
}
}
return
return
}
else
{
//设置超时时间
dposlog
.
Info
(
"wait until change state."
,
"waittime"
,
cs
.
currentVote
.
PeriodStop
-
now
+
1
)
cs
.
scheduleDPosTimeout
(
time
.
Second
*
time
.
Duration
(
cs
.
currentVote
.
PeriodStop
-
now
+
1
),
VotedStateType
)
return
}
}
//设置超时时间
dposlog
.
Info
(
"wait until change state."
,
"waittime"
,
cs
.
currentVote
.
PeriodStop
-
now
+
1
)
cs
.
scheduleDPosTimeout
(
time
.
Second
*
time
.
Duration
(
cs
.
currentVote
.
PeriodStop
-
now
+
1
),
VotedStateType
)
return
}
}
}
}
func
(
voted
*
VotedState
)
sendVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
func
(
voted
*
VotedState
)
sendVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
dposlog
.
Info
(
"VotedState sendVoteReply"
)
dposlog
.
Info
(
"VotedState sendVoteReply"
)
cs
.
broadcastChannel
<-
MsgInfo
{
TypeID
:
dpostype
.
VoteReplyID
,
Msg
:
reply
,
PeerID
:
cs
.
ourID
,
PeerIP
:
""
}
cs
.
broadcastChannel
<-
MsgInfo
{
TypeID
:
dpostype
.
VoteReplyID
,
Msg
:
reply
,
PeerID
:
cs
.
ourID
,
PeerIP
:
""
}
}
}
func
(
voted
*
VotedState
)
recvVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
func
(
voted
*
VotedState
)
recvVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
dposlog
.
Info
(
"VotedState recv Vote reply"
,
"from index"
,
reply
.
Vote
.
VoterNodeIndex
,
"local index"
,
cs
.
privValidatorIndex
)
dposlog
.
Info
(
"VotedState recv Vote reply"
,
"from index"
,
reply
.
Vote
.
VoterNodeIndex
,
"local index"
,
cs
.
privValidatorIndex
)
cs
.
AddVotes
(
reply
.
Vote
)
cs
.
AddVotes
(
reply
.
Vote
)
}
}
func
(
voted
*
VotedState
)
sendVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
func
(
voted
*
VotedState
)
sendVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
dposlog
.
Info
(
"VotedState does not support sendVote,so do nothing"
)
dposlog
.
Info
(
"VotedState does not support sendVote,so do nothing"
)
}
}
func
(
voted
*
VotedState
)
recvVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
func
(
voted
*
VotedState
)
recvVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
dposlog
.
Info
(
"VotedState recv Vote, will reply it"
,
"from index"
,
vote
.
VoterNodeIndex
,
"local index"
,
cs
.
privValidatorIndex
)
dposlog
.
Info
(
"VotedState recv Vote, will reply it"
,
"from index"
,
vote
.
VoterNodeIndex
,
"local index"
,
cs
.
privValidatorIndex
)
if
cs
.
currentVote
.
PeriodStart
>=
vote
.
VoteItem
.
PeriodStart
{
if
cs
.
currentVote
.
PeriodStart
>=
vote
.
VoteItem
.
PeriodStart
{
vote2
:=
*
cs
.
myVote
vote2
:=
*
cs
.
myVote
...
@@ -423,49 +432,48 @@ func (voted *VotedState)recvVote(cs *ConsensusState,vote * dpostype.DPosVote) {
...
@@ -423,49 +432,48 @@ func (voted *VotedState)recvVote(cs *ConsensusState,vote * dpostype.DPosVote) {
}
}
}
}
func
(
voted
*
VotedState
)
sendNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
func
(
voted
*
VotedState
)
sendNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
cs
.
broadcastChannel
<-
MsgInfo
{
TypeID
:
dpostype
.
NotifyID
,
Msg
:
notify
,
PeerID
:
cs
.
ourID
,
PeerIP
:
""
}
cs
.
broadcastChannel
<-
MsgInfo
{
TypeID
:
dpostype
.
NotifyID
,
Msg
:
notify
,
PeerID
:
cs
.
ourID
,
PeerIP
:
""
}
}
}
func
(
voted
*
VotedState
)
recvNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
func
(
voted
*
VotedState
)
recvNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
dposlog
.
Info
(
"VotedState recvNotify"
)
dposlog
.
Info
(
"VotedState recvNotify"
)
if
bytes
.
Equal
(
cs
.
privValidator
.
GetAddress
(),
cs
.
currentVote
.
VotedNodeAddress
)
{
if
bytes
.
Equal
(
cs
.
privValidator
.
GetAddress
(),
cs
.
currentVote
.
VotedNodeAddress
)
{
dposlog
.
Info
(
"ignore recvNotify because this node is the owner now."
)
dposlog
.
Info
(
"ignore recvNotify because this node is the owner now."
)
return
return
}
else
{
}
cs
.
CacheNotify
(
notify
)
cs
.
SaveVote
()
cs
.
CacheNotify
(
notify
)
cs
.
SaveMyVote
()
cs
.
SaveVote
()
cs
.
ClearVotes
()
cs
.
SaveMyVote
()
cs
.
SetState
(
WaitNotifyStateObj
)
cs
.
ClearVotes
()
dposlog
.
Info
(
"Change state because of recv notify."
,
"from"
,
"VotedState"
,
"to"
,
"WaitNotifyState"
)
cs
.
SetState
(
WaitNotifyStateObj
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutWaitNotify
)
*
time
.
Millisecond
,
WaitNotifyStateType
)
dposlog
.
Info
(
"Change state because of recv notify."
,
"from"
,
"VotedState"
,
"to"
,
"WaitNotifyState"
)
if
cs
.
cachedNotify
!=
nil
{
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutWaitNotify
)
*
time
.
Millisecond
,
WaitNotifyStateType
)
cs
.
dposState
.
recvNotify
(
cs
,
cs
.
cachedNotify
)
if
cs
.
cachedNotify
!=
nil
{
cs
.
dposState
.
recvNotify
(
cs
,
cs
.
cachedNotify
)
}
return
}
}
return
}
}
//
VotingState is the voting state of dpos state machine until timeout or get an agreement by votes
.
//
WaitNofifyState is the state of dpos state machine to wait notify
.
type
WaitNofifyState
struct
{
type
WaitNofifyState
struct
{
}
}
func
(
wait
*
WaitNofifyState
)
timeOut
(
cs
*
ConsensusState
)
{
func
(
wait
*
WaitNofifyState
)
timeOut
(
cs
*
ConsensusState
)
{
//cs.clearVotes()
//cs.clearVotes()
cs
.
SetState
(
InitStateObj
)
cs
.
SetState
(
InitStateObj
)
dposlog
.
Info
(
"Change state because of time."
,
"from"
,
"WaitNofifyState"
,
"to"
,
"InitState"
)
dposlog
.
Info
(
"Change state because of time."
,
"from"
,
"WaitNofifyState"
,
"to"
,
"InitState"
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
cs
.
scheduleDPosTimeout
(
time
.
Duration
(
timeoutCheckConnections
)
*
time
.
Millisecond
,
InitStateType
)
}
}
func
(
wait
*
WaitNofifyState
)
sendVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
func
(
wait
*
WaitNofifyState
)
sendVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
dposlog
.
Info
(
"WaitNofifyState does not support sendVote,so do nothing"
)
dposlog
.
Info
(
"WaitNofifyState does not support sendVote,so do nothing"
)
}
}
func
(
wait
*
WaitNofifyState
)
recvVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
func
(
wait
*
WaitNofifyState
)
recvVote
(
cs
*
ConsensusState
,
vote
*
dpostype
.
DPosVote
)
{
dposlog
.
Info
(
"WaitNofifyState recvVote,store it."
)
dposlog
.
Info
(
"WaitNofifyState recvVote,store it."
)
//对于vote进行保存,在后续状态中进行处理。 保存的选票有先后,同一个节点发来的最新的选票被保存。
//对于vote进行保存,在后续状态中进行处理。 保存的选票有先后,同一个节点发来的最新的选票被保存。
//if !cs.VerifyVote(vote) {
//if !cs.VerifyVote(vote) {
...
@@ -477,19 +485,19 @@ func (wait *WaitNofifyState)recvVote(cs *ConsensusState,vote * dpostype.DPosVote
...
@@ -477,19 +485,19 @@ func (wait *WaitNofifyState)recvVote(cs *ConsensusState,vote * dpostype.DPosVote
cs
.
CacheVotes
(
vote
)
cs
.
CacheVotes
(
vote
)
}
}
func
(
wait
*
WaitNofifyState
)
sendVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
func
(
wait
*
WaitNofifyState
)
sendVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
dposlog
.
Info
(
"WaitNofifyState does not support sendVoteReply,so do nothing"
)
dposlog
.
Info
(
"WaitNofifyState does not support sendVoteReply,so do nothing"
)
}
}
func
(
wait
*
WaitNofifyState
)
recvVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
func
(
wait
*
WaitNofifyState
)
recvVoteReply
(
cs
*
ConsensusState
,
reply
*
dpostype
.
DPosVoteReply
)
{
dposlog
.
Info
(
"WaitNofifyState recv Vote reply,ignore it."
)
dposlog
.
Info
(
"WaitNofifyState recv Vote reply,ignore it."
)
}
}
func
(
wait
*
WaitNofifyState
)
sendNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
func
(
wait
*
WaitNofifyState
)
sendNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
dposlog
.
Info
(
"WaitNofifyState does not support sendNotify,so do nothing"
)
dposlog
.
Info
(
"WaitNofifyState does not support sendNotify,so do nothing"
)
}
}
func
(
wait
*
WaitNofifyState
)
recvNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
func
(
wait
*
WaitNofifyState
)
recvNotify
(
cs
*
ConsensusState
,
notify
*
dpostype
.
DPosNotify
)
{
dposlog
.
Info
(
"WaitNofifyState recvNotify"
)
dposlog
.
Info
(
"WaitNofifyState recvNotify"
)
//记录Notify,校验区块,标记不可逆区块高度
//记录Notify,校验区块,标记不可逆区块高度
if
!
cs
.
VerifyNotify
(
notify
)
{
if
!
cs
.
VerifyNotify
(
notify
)
{
...
@@ -498,34 +506,34 @@ func (wait *WaitNofifyState)recvNotify(cs *ConsensusState, notify * dpostype.DPo
...
@@ -498,34 +506,34 @@ func (wait *WaitNofifyState)recvNotify(cs *ConsensusState, notify * dpostype.DPo
}
}
block
:=
cs
.
client
.
GetCurrentBlock
()
block
:=
cs
.
client
.
GetCurrentBlock
()
if
(
block
.
Height
>
notify
.
HeightStop
)
{
if
block
.
Height
>
notify
.
HeightStop
{
dposlog
.
Info
(
"Local block height is advanced than notify, discard it."
,
"localheight"
,
block
.
Height
,
"notify"
,
notify
.
String
())
dposlog
.
Info
(
"Local block height is advanced than notify, discard it."
,
"localheight"
,
block
.
Height
,
"notify"
,
notify
.
String
())
return
return
}
else
if
block
.
Height
==
notify
.
HeightStop
&&
bytes
.
Equal
(
block
.
Hash
(),
notify
.
HashStop
){
}
else
if
block
.
Height
==
notify
.
HeightStop
&&
bytes
.
Equal
(
block
.
Hash
(),
notify
.
HashStop
)
{
dposlog
.
Info
(
"Local block height is sync with notify"
,
"notify"
,
notify
.
String
())
dposlog
.
Info
(
"Local block height is sync with notify"
,
"notify"
,
notify
.
String
())
}
else
{
}
else
{
dposlog
.
Info
(
"Local block height is not sync with notify"
,
"localheight"
,
cs
.
client
.
GetCurrentHeight
(),
"notify"
,
notify
.
String
())
dposlog
.
Info
(
"Local block height is not sync with notify"
,
"localheight"
,
cs
.
client
.
GetCurrentHeight
(),
"notify"
,
notify
.
String
())
hint
:=
time
.
NewTicker
(
3
*
time
.
Second
)
hint
:=
time
.
NewTicker
(
3
*
time
.
Second
)
beg
:=
time
.
Now
()
beg
:=
time
.
Now
()
catchupFlag
:=
false
catchupFlag
:=
false
OuterLoop
:
OuterLoop
:
for
!
catchupFlag
{
for
!
catchupFlag
{
select
{
select
{
case
<-
hint
.
C
:
case
<-
hint
.
C
:
dposlog
.
Info
(
"Still catching up max height......"
,
"Height"
,
cs
.
client
.
GetCurrentHeight
(),
"notifyHeight"
,
notify
.
HeightStop
,
"cost"
,
time
.
Since
(
beg
))
dposlog
.
Info
(
"Still catching up max height......"
,
"Height"
,
cs
.
client
.
GetCurrentHeight
(),
"notifyHeight"
,
notify
.
HeightStop
,
"cost"
,
time
.
Since
(
beg
))
if
cs
.
client
.
IsCaughtUp
()
&&
cs
.
client
.
GetCurrentHeight
()
>=
notify
.
HeightStop
{
if
cs
.
client
.
IsCaughtUp
()
&&
cs
.
client
.
GetCurrentHeight
()
>=
notify
.
HeightStop
{
dposlog
.
Info
(
"This node has caught up max height"
,
"Height"
,
cs
.
client
.
GetCurrentHeight
(),
"isHashSame"
,
bytes
.
Equal
(
block
.
Hash
(),
notify
.
HashStop
))
dposlog
.
Info
(
"This node has caught up max height"
,
"Height"
,
cs
.
client
.
GetCurrentHeight
(),
"isHashSame"
,
bytes
.
Equal
(
block
.
Hash
(),
notify
.
HashStop
))
break
OuterLoop
break
OuterLoop
}
}
default
:
default
:
if
cs
.
client
.
IsCaughtUp
()
&&
cs
.
client
.
GetCurrentHeight
()
>=
notify
.
HeightStop
{
if
cs
.
client
.
IsCaughtUp
()
&&
cs
.
client
.
GetCurrentHeight
()
>=
notify
.
HeightStop
{
dposlog
.
Info
(
"This node has caught up max height"
,
"Height"
,
cs
.
client
.
GetCurrentHeight
())
dposlog
.
Info
(
"This node has caught up max height"
,
"Height"
,
cs
.
client
.
GetCurrentHeight
())
break
OuterLoop
break
OuterLoop
}
time
.
Sleep
(
time
.
Second
)
}
}
time
.
Sleep
(
time
.
Second
)
}
}
}
hint
.
Stop
()
hint
.
Stop
()
}
}
...
@@ -539,8 +547,3 @@ func (wait *WaitNofifyState)recvNotify(cs *ConsensusState, notify * dpostype.DPo
...
@@ -539,8 +547,3 @@ func (wait *WaitNofifyState)recvNotify(cs *ConsensusState, notify * dpostype.DPo
cs
.
dposState
.
timeOut
(
cs
)
cs
.
dposState
.
timeOut
(
cs
)
//cs.scheduleDPosTimeout(time.Second * 1, InitStateType)
//cs.scheduleDPosTimeout(time.Second * 1, InitStateType)
}
}
var
InitStateObj
=
&
InitState
{}
var
VotingStateObj
=
&
VotingState
{}
var
VotedStateObj
=
&
VotedState
{}
var
WaitNotifyStateObj
=
&
WaitNofifyState
{}
\ No newline at end of file
plugin/consensus/dpos/state_machine_test.go
View file @
b08c65a3
...
@@ -29,14 +29,14 @@ func init() {
...
@@ -29,14 +29,14 @@ func init() {
setParams
(
3
,
3
,
6
)
setParams
(
3
,
3
,
6
)
}
}
func
setParams
(
delegateNum
int64
,
blockInterval
int64
,
continueBlockNum
int64
)
{
func
setParams
(
delegateNum
int64
,
blockInterval
int64
,
continueBlockNum
int64
)
{
dposDelegateNum
=
delegateNum
//委托节点个数,从配置读取,以后可以根据投票结果来定
dposDelegateNum
=
delegateNum
//委托节点个数,从配置读取,以后可以根据投票结果来定
dposBlockInterval
=
blockInterval
//出块间隔,当前按3s
dposBlockInterval
=
blockInterval
//出块间隔,当前按3s
dposContinueBlockNum
=
continueBlockNum
//一个委托节点当选后,一次性持续出块数量
dposContinueBlockNum
=
continueBlockNum
//一个委托节点当选后,一次性持续出块数量
dposCycle
=
int64
(
dposDelegateNum
*
dposBlockInterval
*
dposContinueBlockNum
)
dposCycle
=
int64
(
dposDelegateNum
*
dposBlockInterval
*
dposContinueBlockNum
)
dposPeriod
=
int64
(
dposBlockInterval
*
dposContinueBlockNum
)
dposPeriod
=
int64
(
dposBlockInterval
*
dposContinueBlockNum
)
}
}
func
printTask
(
now
int64
,
task
*
DPosTask
){
func
printTask
(
now
int64
,
task
*
DPosTask
)
{
fmt
.
Printf
(
"now:%v|cycleStart:%v|cycleStop:%v|periodStart:%v|periodStop:%v|blockStart:%v|blockStop:%v|nodeId:%v
\n
"
,
fmt
.
Printf
(
"now:%v|cycleStart:%v|cycleStop:%v|periodStart:%v|periodStop:%v|blockStart:%v|blockStop:%v|nodeId:%v
\n
"
,
now
,
now
,
task
.
cycleStart
,
task
.
cycleStart
,
...
@@ -45,10 +45,10 @@ func printTask(now int64, task *DPosTask){
...
@@ -45,10 +45,10 @@ func printTask(now int64, task *DPosTask){
task
.
periodStop
,
task
.
periodStop
,
task
.
blockStart
,
task
.
blockStart
,
task
.
blockStop
,
task
.
blockStop
,
task
.
nodeI
d
)
task
.
nodeI
D
)
}
}
func
assertTask
(
task
*
DPosTask
,
t
*
testing
.
T
)
{
func
assertTask
(
task
*
DPosTask
,
t
*
testing
.
T
)
{
assert
.
Equal
(
t
,
true
,
task
.
nodeI
d
>=
0
&&
task
.
nodeId
<
dposDelegateNum
)
assert
.
Equal
(
t
,
true
,
task
.
nodeI
D
>=
0
&&
task
.
nodeID
<
dposDelegateNum
)
assert
.
Equal
(
t
,
true
,
task
.
cycleStart
<=
task
.
periodStart
&&
task
.
periodStart
<=
task
.
blockStart
&&
task
.
blockStop
<=
task
.
periodStop
&&
task
.
periodStop
<=
task
.
cycleStop
)
assert
.
Equal
(
t
,
true
,
task
.
cycleStart
<=
task
.
periodStart
&&
task
.
periodStart
<=
task
.
blockStart
&&
task
.
blockStop
<=
task
.
periodStop
&&
task
.
periodStop
<=
task
.
cycleStop
)
}
}
...
@@ -59,7 +59,6 @@ func TestDecideTaskByTime(t *testing.T) {
...
@@ -59,7 +59,6 @@ func TestDecideTaskByTime(t *testing.T) {
printTask
(
now
,
&
task
)
printTask
(
now
,
&
task
)
assertTask
(
&
task
,
t
)
assertTask
(
&
task
,
t
)
setParams
(
2
,
1
,
6
)
setParams
(
2
,
1
,
6
)
now
=
time
.
Now
()
.
Unix
()
now
=
time
.
Now
()
.
Unix
()
task
=
DecideTaskByTime
(
now
)
task
=
DecideTaskByTime
(
now
)
...
@@ -72,7 +71,6 @@ func TestDecideTaskByTime(t *testing.T) {
...
@@ -72,7 +71,6 @@ func TestDecideTaskByTime(t *testing.T) {
printTask
(
now
,
&
task
)
printTask
(
now
,
&
task
)
assertTask
(
&
task
,
t
)
assertTask
(
&
task
,
t
)
setParams
(
21
,
2
,
12
)
setParams
(
21
,
2
,
12
)
now
=
time
.
Now
()
.
Unix
()
now
=
time
.
Now
()
.
Unix
()
task
=
DecideTaskByTime
(
now
)
task
=
DecideTaskByTime
(
now
)
...
@@ -89,4 +87,3 @@ func TestDecideTaskByTime(t *testing.T) {
...
@@ -89,4 +87,3 @@ func TestDecideTaskByTime(t *testing.T) {
time
.
Sleep
(
time
.
Second
*
1
)
time
.
Sleep
(
time
.
Second
*
1
)
}
}
}
}
plugin/consensus/dpos/types/dpos_msg.pb.go
View file @
b08c65a3
...
@@ -38,7 +38,7 @@ type VoteItem struct {
...
@@ -38,7 +38,7 @@ type VoteItem struct {
PeriodStart
int64
`protobuf:"varint,5,opt,name=periodStart" json:"periodStart,omitempty"`
PeriodStart
int64
`protobuf:"varint,5,opt,name=periodStart" json:"periodStart,omitempty"`
PeriodStop
int64
`protobuf:"varint,6,opt,name=periodStop" json:"periodStop,omitempty"`
PeriodStop
int64
`protobuf:"varint,6,opt,name=periodStop" json:"periodStop,omitempty"`
Height
int64
`protobuf:"varint,7,opt,name=height" json:"height,omitempty"`
Height
int64
`protobuf:"varint,7,opt,name=height" json:"height,omitempty"`
VoteI
d
[]
byte
`protobuf:"bytes,8,opt,name=voteId,proto3" json:"voteId
,omitempty"`
VoteI
D
[]
byte
`protobuf:"bytes,8,opt,name=voteID,proto3" json:"voteID
,omitempty"`
}
}
func
(
m
*
VoteItem
)
Reset
()
{
*
m
=
VoteItem
{}
}
func
(
m
*
VoteItem
)
Reset
()
{
*
m
=
VoteItem
{}
}
...
@@ -95,9 +95,9 @@ func (m *VoteItem) GetHeight() int64 {
...
@@ -95,9 +95,9 @@ func (m *VoteItem) GetHeight() int64 {
return
0
return
0
}
}
func
(
m
*
VoteItem
)
GetVoteI
d
()
[]
byte
{
func
(
m
*
VoteItem
)
GetVoteI
D
()
[]
byte
{
if
m
!=
nil
{
if
m
!=
nil
{
return
m
.
VoteI
d
return
m
.
VoteI
D
}
}
return
nil
return
nil
}
}
...
@@ -242,30 +242,30 @@ func init() {
...
@@ -242,30 +242,30 @@ func init() {
func
init
()
{
proto
.
RegisterFile
(
"dpos_msg.proto"
,
fileDescriptor0
)
}
func
init
()
{
proto
.
RegisterFile
(
"dpos_msg.proto"
,
fileDescriptor0
)
}
var
fileDescriptor0
=
[]
byte
{
var
fileDescriptor0
=
[]
byte
{
// 38
6
bytes of a gzipped FileDescriptorProto
// 38
5
bytes of a gzipped FileDescriptorProto
0x1f
,
0x8b
,
0x08
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x02
,
0xff
,
0x74
,
0x93
,
0x
dd
,
0x6a
,
0xdb
,
0x3
0
,
0x1f
,
0x8b
,
0x08
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x02
,
0xff
,
0x74
,
0x93
,
0x
cd
,
0x4a
,
0xeb
,
0x4
0
,
0x14
,
0xc7
,
0x
71
,
0x1c
,
0x3b
,
0xde
,
0xc9
,
0xd7
,
0xa6
,
0x8b
,
0x61
,
0xc6
,
0x18
,
0xc6
,
0x1b
,
0xc3
,
0x14
,
0xc7
,
0x
49
,
0xd3
,
0xa4
,
0xb9
,
0xa7
,
0x5f
,
0xf7
,
0xce
,
0xe2
,
0x12
,
0x2e
,
0x17
,
0x09
,
0x51
,
0x
6c
,
0x25
,
0x17
,
0x6d
,
0x5f
,
0xa0
,
0xd0
,
0x9b
,
0xdc
,
0x84
,
0xe2
,
0x96
,
0xde
,
0x16
,
0x37
,
0x52
,
0x
24
,
0xa8
,
0x74
,
0xa1
,
0xbe
,
0x80
,
0xd0
,
0x4d
,
0x37
,
0x45
,
0xa2
,
0xb8
,
0x95
,
0xd8
,
0x8c
,
0x4d
,
0x
63
,
0x43
,
0x1c
,
0x09
,
0x49
,
0x0d
,
0xcd
,
0x43
,
0xb4
,
0xaf
,
0xd6
,
0x57
,
0x2a
,
0x3a
,
0xfe
,
0x90
,
0x
a0
,
0xe9
,
0x0c
,
0x33
,
0x63
,
0xb1
,
0x0f
,
0xa1
,
0xaf
,
0xe6
,
0x2b
,
0xc9
,
0x9c
,
0x7c
,
0x4c
,
0x9a
,
0xe
3
,
0xd0
,
0xbb
,
0x9c
,
0xdf
,
0x5f
,
0x1f
,
0xe7
,
0xfc
,
0x14
,
0xc3
,
0x8c
,
0x0a
,
0xae
,
0x1e
,
0x4a
,
0xe
2
,
0xae
,
0xe7
,
0xf7
,
0x9f
,
0x8f
,
0x73
,
0x7e
,
0xd3
,
0xc0
,
0x24
,
0xe5
,
0x4c
,
0x3e
,
0x17
,
0x72
,
0x
b5
,
0x59
,
0x08
,
0xc9
,
0x35
,
0x27
,
0x9e
,
0x3e
,
0x08
,
0xa6
,
0xe2
,
0xb7
,
0x01
,
0x04
,
0xf7
,
0x5c
,
0x
3d
,
0xe3
,
0x82
,
0x29
,
0x46
,
0x1c
,
0xb5
,
0xe7
,
0x54
,
0x86
,
0x9f
,
0x3d
,
0xf0
,
0x9e
,
0x98
,
0xa2
,
0x
b3
,
0xa5
,
0x66
,
0x25
,
0xf9
,
0x0b
,
0xb3
,
0x3d
,
0xd7
,
0x8c
,
0xae
,
0x38
,
0x65
,
0xcb
,
0x1d
,
0x65
,
0x
0b
,
0x45
,
0x0b
,
0x72
,
0x0e
,
0x93
,
0x1d
,
0x53
,
0x34
,
0x5d
,
0xb2
,
0x94
,
0x2e
,
0xb6
,
0x29
,
0x7d
,
0x
2f
,
0xa1
,
0x13
,
0x39
,
0x89
,
0x97
,
0xf6
,
0x28
,
0xf9
,
0x07
,
0x5f
,
0x5b
,
0x72
,
0x45
,
0xa9
,
0x64
,
0x
f7
,
0xad
,
0xc0
,
0x8a
,
0x9c
,
0xb8
,
0x43
,
0xc9
,
0x05
,
0xfc
,
0x6e
,
0xc8
,
0x5d
,
0x9a
,
0x0a
,
0x2a
,
0x
4a
,
0x85
,
0x83
,
0xc8
,
0x49
,
0x26
,
0xe9
,
0x09
,
0x27
,
0xbf
,
0x00
,
0xd6
,
0x87
,
0xf5
,
0x96
,
0xdd
,
0x
a5
,
0xdf
,
0x0b
,
0xac
,
0x68
,
0x14
,
0x1f
,
0x71
,
0x72
,
0x02
,
0xb0
,
0xda
,
0xaf
,
0x36
,
0xf4
,
0x41
,
0x
ea
,
0x4c
,
0xea
,
0xd0
,
0x8d
,
0x9c
,
0xc4
,
0x4d
,
0x3b
,
0x84
,
0xfc
,
0x84
,
0x2f
,
0x75
,
0xc5
,
0x45
,
0x
25
,
0x42
,
0xf9
,
0x76
,
0x60
,
0x45
,
0x76
,
0xdc
,
0x22
,
0xe4
,
0x3f
,
0xfc
,
0xaa
,
0x2a
,
0xc6
,
0xfd
,
0x3
8
,
0xc4
,
0xd8
,
0x02
,
0x12
,
0xc1
,
0x58
,
0x30
,
0x59
,
0x70
,
0x5a
,
0x6d
,
0xf7
,
0x30
,
0xef
,
0x2
2
,
0x3
e
,
0xc6
,
0x06
,
0x90
,
0x00
,
0x86
,
0x9c
,
0x8a
,
0x9c
,
0xa5
,
0xe5
,
0x76
,
0x07
,
0xf3
,
0x36
,
0xd
2
,
0x
73
,
0x7e
,
0x53
,
0x72
,
0x11
,
0xfa
,
0xd5
,
0xf9
,
0x96
,
0x90
,
0xef
,
0xe0
,
0xe7
,
0xac
,
0xd8
,
0xe4
,
0x
e7
,
0xd7
,
0x25
,
0xe3
,
0xbe
,
0x5b
,
0x9e
,
0x6f
,
0x08
,
0xf9
,
0x0b
,
0x6e
,
0x46
,
0xf3
,
0x75
,
0xa6
,
0x
3a
,
0x1c
,
0x61
,
0x56
,
0x57
,
0x86
,
0x9b
,
0x5e
,
0x97
,
0x34
,
0x0c
,
0xb0
,
0xf3
,
0xba
,
0x8a
,
0xd
f
,
0x
fc
,
0x01
,
0x66
,
0x55
,
0xa5
,
0xb9
,
0xee
,
0x75
,
0x31
,
0xf7
,
0x3d
,
0xec
,
0xbc
,
0xaa
,
0xc2
,
0x2
f
,
0x
1d
,
0x08
,
0xae
,
0x6f
,
0xb8
,
0x32
,
0x52
,
0xc8
,
0x7f
,
0x08
,
0xf6
,
0xb5
,
0x1c
,
0x54
,
0x31
,
0x3e
,
0x
0b
,
0xbc
,
0xf9
,
0x3d
,
0x93
,
0x5a
,
0x0a
,
0xb9
,
0x04
,
0x6f
,
0x57
,
0xc9
,
0x41
,
0x15
,
0xc3
,
0xeb
,
0x
9f
,
0x2f
,
0xd0
,
0xdb
,
0xa2
,
0x71
,
0x96
,
0xb6
,
0x0b
,
0xc8
,
0x1f
,
0x98
,
0x9a
,
0xdf
,
0x77
,
0x45
,
0x
e9
,
0x0c
,
0xbd
,
0xcd
,
0x6a
,
0x67
,
0x71
,
0xb3
,
0x80
,
0x9c
,
0xc1
,
0x58
,
0xff
,
0x7e
,
0xcc
,
0x0b
,
0x
c9
,
0x94
,
0xce
,
0x4a
,
0x81
,
0x4a
,
0xdc
,
0xf4
,
0x18
,
0x36
,
0x8e
,
0xa5
,
0x75
,
0xec
,
0x5a
,
0xc7
,
0x
2a
,
0x55
,
0x52
,
0x70
,
0x54
,
0x62
,
0xc7
,
0x87
,
0xb0
,
0x76
,
0x2c
,
0x8c
,
0x63
,
0xdb
,
0x38
,
0x16
,
0x
f2
,
0xc4
,
0xb1
,
0xec
,
0x3a
,
0x1e
,
0x5a
,
0xc7
,
0x5d
,
0x6e
,
0x1c
,
0xaa
,
0x62
,
0xb3
,
0xcb
,
0xf4
,
0x
47
,
0x8e
,
0x45
,
0xdb
,
0x71
,
0xdf
,
0x38
,
0x6e
,
0x73
,
0xed
,
0x50
,
0xe6
,
0xeb
,
0x6d
,
0xa2
,
0xde
,
0x
b3
,
0x64
,
0xe8
,
0x68
,
0x92
,
0x5a
,
0x10
,
0x5f
,
0xc2
,
0xb4
,
0x19
,
0x28
,
0x65
,
0x62
,
0x7b
,
0x20
,
0x
04
,
0x45
,
0x47
,
0xa3
,
0xd8
,
0x80
,
0xf0
,
0x16
,
0xc6
,
0xf5
,
0x40
,
0x31
,
0xe5
,
0x9b
,
0x3d
,
0x39
,
0x
bf
,
0x61
,
0x68
,
0x8e
,
0xe8
,
0x4d
,
0xd4
,
0xae
,
0xc1
,
0x30
,
0x7e
,
0x1d
,
0x00
,
0x18
,
0xb4
,
0xe2
,
0x
85
,
0xbe
,
0x3e
,
0xa2
,
0x33
,
0x51
,
0xb3
,
0x06
,
0xc3
,
0xf0
,
0xa3
,
0x07
,
0xa0
,
0xd1
,
0x92
,
0xa9
,
0x
ba
,
0x78
,
0xfa
,
0x6c
,
0x4f
,
0x6b
,
0x01
,
0x43
,
0xf3
,
0x16
,
0x95
,
0x5d
,
0x7c
,
0x8b
,
0x6a
,
0xfc
,
0x
fc
,
0xf5
,
0xa7
,
0x3d
,
0x8d
,
0x05
,
0x0c
,
0xf5
,
0x5b
,
0x94
,
0x76
,
0xf1
,
0x2d
,
0xca
,
0xf1
,
0x5b
,
0x
0e
,
0x21
,
0x3f
,
0x20
,
0xc8
,
0x33
,
0x95
,
0x63
,
0xea
,
0x62
,
0x9b
,
0x6d
,
0x4d
,
0x12
,
0x98
,
0xef
,
0x
84
,
0xfc
,
0x03
,
0x2f
,
0x4b
,
0x64
,
0x86
,
0xa9
,
0x8d
,
0x6d
,
0x36
,
0x35
,
0x89
,
0x60
,
0xba
,
0xc5
,
0x
f0
,
0x2a
,
0xeb
,
0xaf
,
0xfa
,
0x37
,
0xf4
,
0xb1
,
0x5d
,
0x69
,
0x15
,
0x7a
,
0xa8
,
0xb0
,
0x8f
,
0xc9
,
0x
ab
,
0x8c
,
0xbf
,
0xf2
,
0xdf
,
0xd0
,
0xc5
,
0x66
,
0xa5
,
0x51
,
0xe8
,
0xa0
,
0xc2
,
0x2e
,
0x26
,
0x57
,
0x
19
,
0x7c
,
0xb3
,
0xa8
,
0x91
,
0xe8
,
0xe3
,
0xc5
,
0xa7
,
0xc1
,
0xb1
,
0xc5
,
0x51
,
0xcf
,
0xe2
,
0xa3
,
0x
f0
,
0xc7
,
0xa0
,
0x5a
,
0xa2
,
0x8b
,
0x17
,
0x1f
,
0x07
,
0x87
,
0x16
,
0x07
,
0x1d
,
0x8b
,
0x2f
,
0x2e
,
0x
8f
,
0x9f
,
0xcd
,
0xc5
,
0x47
,
0x00
,
0x00
,
0x00
,
0xff
,
0xff
,
0xc3
,
0x26
,
0x39
,
0x79
,
0x48
,
0x03
,
0x
7e
,
0x36
,
0x37
,
0xdf
,
0x01
,
0x00
,
0x00
,
0xff
,
0xff
,
0x6e
,
0xf4
,
0x71
,
0xcb
,
0x48
,
0x03
,
0x00
,
0x00
,
0x00
,
0x00
,
}
}
plugin/consensus/dpos/types/genesis.go
View file @
b08c65a3
...
@@ -23,11 +23,11 @@ type GenesisValidator struct {
...
@@ -23,11 +23,11 @@ type GenesisValidator struct {
// GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set.
// GenesisDoc defines the initial conditions for a tendermint blockchain, in particular its validator set.
type
GenesisDoc
struct
{
type
GenesisDoc
struct
{
GenesisTime
time
.
Time
`json:"genesis_time"`
GenesisTime
time
.
Time
`json:"genesis_time"`
ChainID
string
`json:"chain_id"`
ChainID
string
`json:"chain_id"`
Validators
[]
GenesisValidator
`json:"validators"`
Validators
[]
GenesisValidator
`json:"validators"`
AppHash
[]
byte
`json:"app_hash"`
AppHash
[]
byte
`json:"app_hash"`
AppOptions
interface
{}
`json:"app_options,omitempty"`
AppOptions
interface
{}
`json:"app_options,omitempty"`
}
}
// SaveAs is a utility method for saving GenensisDoc as a JSON file.
// SaveAs is a utility method for saving GenensisDoc as a JSON file.
...
...
plugin/consensus/dpos/types/msg.go
View file @
b08c65a3
...
@@ -5,9 +5,9 @@
...
@@ -5,9 +5,9 @@
package
types
package
types
import
(
import
(
"time"
"reflect"
"reflect"
)
"time"
)
var
(
var
(
// MsgMap define
// MsgMap define
...
@@ -16,9 +16,9 @@ var (
...
@@ -16,9 +16,9 @@ var (
// step and message id define
// step and message id define
const
(
const
(
VoteID
=
byte
(
0x06
)
VoteID
=
byte
(
0x06
)
VoteReplyID
=
byte
(
0x07
)
VoteReplyID
=
byte
(
0x07
)
NotifyID
=
byte
(
0x08
)
NotifyID
=
byte
(
0x08
)
PacketTypePing
=
byte
(
0xff
)
PacketTypePing
=
byte
(
0xff
)
PacketTypePong
=
byte
(
0xfe
)
PacketTypePong
=
byte
(
0xfe
)
...
@@ -27,14 +27,13 @@ const (
...
@@ -27,14 +27,13 @@ const (
// InitMessageMap ...
// InitMessageMap ...
func
InitMessageMap
()
{
func
InitMessageMap
()
{
MsgMap
=
map
[
byte
]
reflect
.
Type
{
MsgMap
=
map
[
byte
]
reflect
.
Type
{
VoteID
:
reflect
.
TypeOf
(
DPosVote
{}),
VoteID
:
reflect
.
TypeOf
(
DPosVote
{}),
VoteReplyID
:
reflect
.
TypeOf
(
DPosVoteReply
{}),
VoteReplyID
:
reflect
.
TypeOf
(
DPosVoteReply
{}),
NotifyID
:
reflect
.
TypeOf
(
DPosNotify
{}),
NotifyID
:
reflect
.
TypeOf
(
DPosNotify
{}),
}
}
}
}
//---------------------Canonical json-----------------------------------
// CanonicalJSONVoteItem ...
// CanonicalJSONVote ...
type
CanonicalJSONVoteItem
struct
{
type
CanonicalJSONVoteItem
struct
{
VotedNodeIndex
int32
`json:"votedNodeIndex,omitempty"`
VotedNodeIndex
int32
`json:"votedNodeIndex,omitempty"`
VotedNodeAddress
[]
byte
`json:"votedNodeAddress,omitempty"`
VotedNodeAddress
[]
byte
`json:"votedNodeAddress,omitempty"`
...
@@ -43,15 +42,15 @@ type CanonicalJSONVoteItem struct {
...
@@ -43,15 +42,15 @@ type CanonicalJSONVoteItem struct {
PeriodStart
int64
`json:"periodStart,omitempty"`
PeriodStart
int64
`json:"periodStart,omitempty"`
PeriodStop
int64
`json:"periodStop,omitempty"`
PeriodStop
int64
`json:"periodStop,omitempty"`
Height
int64
`json:"height,omitempty"`
Height
int64
`json:"height,omitempty"`
VoteI
d
[]
byte
`json:"voteId
,omitempty"`
VoteI
D
[]
byte
`json:"voteID
,omitempty"`
}
}
// CanonicalJSONVote ...
type
CanonicalJSONVote
struct
{
type
CanonicalJSONVote
struct
{
VoteItem
*
CanonicalJSONVoteItem
`json:"vote,omitempty"`
VoteItem
*
CanonicalJSONVoteItem
`json:"vote,omitempty"`
VoteTimestamp
int64
`json:"voteTimestamp,omitempty"`
VoteTimestamp
int64
`json:"voteTimestamp,omitempty"`
VoterNodeIndex
int32
`json:"voterNodeIndex,omitempty"`
VoterNodeIndex
int32
`json:"voterNodeIndex,omitempty"`
VoterNodeAddress
[]
byte
`json:"voterNodeAddress,omitempty"`
VoterNodeAddress
[]
byte
`json:"voterNodeAddress,omitempty"`
}
}
// CanonicalJSONOnceVote ...
// CanonicalJSONOnceVote ...
...
@@ -63,49 +62,50 @@ type CanonicalJSONOnceVote struct {
...
@@ -63,49 +62,50 @@ type CanonicalJSONOnceVote struct {
// CanonicalVote ...
// CanonicalVote ...
func
CanonicalVote
(
vote
*
Vote
)
CanonicalJSONVote
{
func
CanonicalVote
(
vote
*
Vote
)
CanonicalJSONVote
{
return
CanonicalJSONVote
{
return
CanonicalJSONVote
{
VoteItem
:
&
CanonicalJSONVoteItem
{
VoteItem
:
&
CanonicalJSONVoteItem
{
VotedNodeIndex
:
vote
.
VoteItem
.
VotedNodeIndex
,
VotedNodeIndex
:
vote
.
VoteItem
.
VotedNodeIndex
,
VotedNodeAddress
:
vote
.
VoteItem
.
VotedNodeAddress
,
VotedNodeAddress
:
vote
.
VoteItem
.
VotedNodeAddress
,
CycleStart
:
vote
.
VoteItem
.
CycleStart
,
CycleStart
:
vote
.
VoteItem
.
CycleStart
,
CycleStop
:
vote
.
VoteItem
.
CycleStop
,
CycleStop
:
vote
.
VoteItem
.
CycleStop
,
PeriodStart
:
vote
.
VoteItem
.
PeriodStart
,
PeriodStart
:
vote
.
VoteItem
.
PeriodStart
,
PeriodStop
:
vote
.
VoteItem
.
PeriodStop
,
PeriodStop
:
vote
.
VoteItem
.
PeriodStop
,
Height
:
vote
.
VoteItem
.
Height
,
Height
:
vote
.
VoteItem
.
Height
,
VoteId
:
vote
.
VoteItem
.
VoteId
,
VoteID
:
vote
.
VoteItem
.
VoteID
,
},
},
VoteTimestamp
:
vote
.
VoteTimestamp
,
VoteTimestamp
:
vote
.
VoteTimestamp
,
VoterNodeIndex
:
vote
.
VoterNodeIndex
,
VoterNodeIndex
:
vote
.
VoterNodeIndex
,
VoterNodeAddress
:
vote
.
VoterNodeAddress
,
VoterNodeAddress
:
vote
.
VoterNodeAddress
,
}
}
}
}
// CanonicalJSONNotify ...
type
CanonicalJSONNotify
struct
{
type
CanonicalJSONNotify
struct
{
VoteItem
*
CanonicalJSONVoteItem
`json:"vote,omitempty"`
VoteItem
*
CanonicalJSONVoteItem
`json:"vote,omitempty"`
HeightStop
int64
`json:"heightStop,omitempty"`
HeightStop
int64
`json:"heightStop,omitempty"`
NotifyTimestamp
int64
`json:"notifyTimestamp,omitempty"`
NotifyTimestamp
int64
`json:"notifyTimestamp,omitempty"`
}
}
// CanonicalJSONOnce
Vote
...
// CanonicalJSONOnce
Notify
...
type
CanonicalJSONOnceNotify
struct
{
type
CanonicalJSONOnceNotify
struct
{
ChainID
string
`json:"chain_id"`
ChainID
string
`json:"chain_id"`
Notify
CanonicalJSONNotify
`json:"vote"`
Notify
CanonicalJSONNotify
`json:"vote"`
}
}
// Canonical
Vote
...
// Canonical
Notify
...
func
CanonicalNotify
(
notify
*
Notify
)
CanonicalJSONNotify
{
func
CanonicalNotify
(
notify
*
Notify
)
CanonicalJSONNotify
{
return
CanonicalJSONNotify
{
return
CanonicalJSONNotify
{
VoteItem
:
&
CanonicalJSONVoteItem
{
VoteItem
:
&
CanonicalJSONVoteItem
{
VotedNodeIndex
:
notify
.
Vote
.
VotedNodeIndex
,
VotedNodeIndex
:
notify
.
Vote
.
VotedNodeIndex
,
VotedNodeAddress
:
notify
.
Vote
.
VotedNodeAddress
,
VotedNodeAddress
:
notify
.
Vote
.
VotedNodeAddress
,
CycleStart
:
notify
.
Vote
.
CycleStart
,
CycleStart
:
notify
.
Vote
.
CycleStart
,
CycleStop
:
notify
.
Vote
.
CycleStop
,
CycleStop
:
notify
.
Vote
.
CycleStop
,
PeriodStart
:
notify
.
Vote
.
PeriodStart
,
PeriodStart
:
notify
.
Vote
.
PeriodStart
,
PeriodStop
:
notify
.
Vote
.
PeriodStop
,
PeriodStop
:
notify
.
Vote
.
PeriodStop
,
Height
:
notify
.
Vote
.
Height
,
Height
:
notify
.
Vote
.
Height
,
VoteI
d
:
notify
.
Vote
.
VoteId
,
VoteI
D
:
notify
.
Vote
.
VoteID
,
},
},
HeightStop
:
notify
.
HeightStop
,
HeightStop
:
notify
.
HeightStop
,
NotifyTimestamp
:
notify
.
NotifyTimestamp
,
NotifyTimestamp
:
notify
.
NotifyTimestamp
,
}
}
}
}
...
...
plugin/consensus/dpos/types/priv_validator.go
View file @
b08c65a3
...
@@ -195,7 +195,7 @@ func LoadPrivValidatorFSWithSigner(filePath string, signerFunc func(PrivValidato
...
@@ -195,7 +195,7 @@ func LoadPrivValidatorFSWithSigner(filePath string, signerFunc func(PrivValidato
Exit
(
Fmt
(
"Error PrivValidator DecodeString failed:%v
\n
"
,
err
))
Exit
(
Fmt
(
"Error PrivValidator DecodeString failed:%v
\n
"
,
err
))
}
}
privValImp
:=
&
PrivValidatorImp
{
privValImp
:=
&
PrivValidatorImp
{
Address
:
addr
,
Address
:
addr
,
}
}
tmp
,
err
:=
hex
.
DecodeString
(
privVal
.
PrivKey
.
Data
)
tmp
,
err
:=
hex
.
DecodeString
(
privVal
.
PrivKey
.
Data
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -290,7 +290,6 @@ func (pv *PrivValidatorImp) SignVote(chainID string, vote *Vote) error {
...
@@ -290,7 +290,6 @@ func (pv *PrivValidatorImp) SignVote(chainID string, vote *Vote) error {
signBytes
:=
SignBytes
(
chainID
,
vote
)
signBytes
:=
SignBytes
(
chainID
,
vote
)
signature
,
err
:=
pv
.
Sign
(
signBytes
)
signature
,
err
:=
pv
.
Sign
(
signBytes
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
New
(
Fmt
(
"Error signing vote: %v"
,
err
))
return
errors
.
New
(
Fmt
(
"Error signing vote: %v"
,
err
))
...
@@ -299,7 +298,7 @@ func (pv *PrivValidatorImp) SignVote(chainID string, vote *Vote) error {
...
@@ -299,7 +298,7 @@ func (pv *PrivValidatorImp) SignVote(chainID string, vote *Vote) error {
return
nil
return
nil
}
}
// Sign
Vote signs a canonical representation of the vote
, along with the
// Sign
Notify signs a canonical representation of the notify
, along with the
// chainID. Implements PrivValidator.
// chainID. Implements PrivValidator.
func
(
pv
*
PrivValidatorImp
)
SignNotify
(
chainID
string
,
notify
*
Notify
)
error
{
func
(
pv
*
PrivValidatorImp
)
SignNotify
(
chainID
string
,
notify
*
Notify
)
error
{
pv
.
mtx
.
Lock
()
pv
.
mtx
.
Lock
()
...
@@ -307,7 +306,6 @@ func (pv *PrivValidatorImp) SignNotify(chainID string, notify *Notify) error {
...
@@ -307,7 +306,6 @@ func (pv *PrivValidatorImp) SignNotify(chainID string, notify *Notify) error {
signBytes
:=
SignBytes
(
chainID
,
notify
)
signBytes
:=
SignBytes
(
chainID
,
notify
)
signature
,
err
:=
pv
.
Sign
(
signBytes
)
signature
,
err
:=
pv
.
Sign
(
signBytes
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
New
(
Fmt
(
"Error signing vote: %v"
,
err
))
return
errors
.
New
(
Fmt
(
"Error signing vote: %v"
,
err
))
...
...
plugin/consensus/dpos/types/signable.go
View file @
b08c65a3
...
@@ -21,12 +21,12 @@ var (
...
@@ -21,12 +21,12 @@ var (
ErrNotifyInvalidValidatorIndex
=
errors
.
New
(
"Invalid validator index for notify"
)
ErrNotifyInvalidValidatorIndex
=
errors
.
New
(
"Invalid validator index for notify"
)
ErrNotifyInvalidSignature
=
errors
.
New
(
"Invalid notify signature"
)
ErrNotifyInvalidSignature
=
errors
.
New
(
"Invalid notify signature"
)
ErrVoteInvalidValidatorIndex
=
errors
.
New
(
"Invalid validator index for vote"
)
ErrVoteInvalidValidatorIndex
=
errors
.
New
(
"Invalid validator index for vote"
)
ErrVoteInvalidValidatorAddress
=
errors
.
New
(
"Invalid validator address for vote"
)
ErrVoteInvalidValidatorAddress
=
errors
.
New
(
"Invalid validator address for vote"
)
ErrVoteInvalidSignature
=
errors
.
New
(
"Invalid vote signature"
)
ErrVoteInvalidSignature
=
errors
.
New
(
"Invalid vote signature"
)
ErrVoteNil
=
errors
.
New
(
"Nil vote"
)
ErrVoteNil
=
errors
.
New
(
"Nil vote"
)
votelog
=
log15
.
New
(
"module"
,
"tendermint-vote"
)
votelog
=
log15
.
New
(
"module"
,
"tendermint-vote"
)
ConsensusCrypto
crypto
.
Crypto
ConsensusCrypto
crypto
.
Crypto
)
)
...
@@ -91,12 +91,12 @@ func (vote *Vote) String() string {
...
@@ -91,12 +91,12 @@ func (vote *Vote) String() string {
vote
.
VoteItem
.
PeriodStart
,
vote
.
VoteItem
.
PeriodStart
,
vote
.
VoteItem
.
PeriodStop
,
vote
.
VoteItem
.
PeriodStop
,
vote
.
VoteItem
.
Height
,
vote
.
VoteItem
.
Height
,
Fingerprint
(
vote
.
VoteItem
.
VoteI
d
),
Fingerprint
(
vote
.
VoteItem
.
VoteI
D
),
CanonicalTime
(
time
.
Unix
(
0
,
vote
.
VoteTimestamp
)),
CanonicalTime
(
time
.
Unix
(
0
,
vote
.
VoteTimestamp
)),
vote
.
VoterNodeIndex
,
vote
.
VoterNodeIndex
,
Fingerprint
(
vote
.
VoterNodeAddress
),
Fingerprint
(
vote
.
VoterNodeAddress
),
Fingerprint
(
vote
.
Signature
),
Fingerprint
(
vote
.
Signature
),
)
)
}
}
// Verify ...
// Verify ...
...
@@ -133,8 +133,7 @@ func (vote *Vote) Hash() []byte {
...
@@ -133,8 +133,7 @@ func (vote *Vote) Hash() []byte {
return
crypto
.
Ripemd160
(
bytes
)
return
crypto
.
Ripemd160
(
bytes
)
}
}
// Notify Represents a notify from validators for consensus.
// Vote Represents a vote from validators for consensus.
type
Notify
struct
{
type
Notify
struct
{
*
DPosNotify
*
DPosNotify
}
}
...
@@ -178,7 +177,7 @@ func (notify *Notify) String() string {
...
@@ -178,7 +177,7 @@ func (notify *Notify) String() string {
notify
.
Vote
.
PeriodStart
,
notify
.
Vote
.
PeriodStart
,
notify
.
Vote
.
PeriodStop
,
notify
.
Vote
.
PeriodStop
,
notify
.
Vote
.
Height
,
notify
.
Vote
.
Height
,
Fingerprint
(
notify
.
Vote
.
VoteI
d
),
Fingerprint
(
notify
.
Vote
.
VoteI
D
),
CanonicalTime
(
time
.
Unix
(
0
,
notify
.
NotifyTimestamp
)),
CanonicalTime
(
time
.
Unix
(
0
,
notify
.
NotifyTimestamp
)),
notify
.
HeightStop
,
notify
.
HeightStop
,
notify
.
NotifyNodeIndex
,
notify
.
NotifyNodeIndex
,
...
...
plugin/consensus/dpos/types/validator_set.go
View file @
b08c65a3
...
@@ -18,15 +18,15 @@ var validatorsetlog = log15.New("module", "dpos-val")
...
@@ -18,15 +18,15 @@ var validatorsetlog = log15.New("module", "dpos-val")
// Validator ...
// Validator ...
type
Validator
struct
{
type
Validator
struct
{
Address
[]
byte
`json:"address"`
Address
[]
byte
`json:"address"`
PubKey
[]
byte
`json:"pub_key"`
PubKey
[]
byte
`json:"pub_key"`
}
}
// NewValidator ...
// NewValidator ...
func
NewValidator
(
pubKey
crypto
.
PubKey
)
*
Validator
{
func
NewValidator
(
pubKey
crypto
.
PubKey
)
*
Validator
{
return
&
Validator
{
return
&
Validator
{
Address
:
GenAddressByPubKey
(
pubKey
),
Address
:
GenAddressByPubKey
(
pubKey
),
PubKey
:
pubKey
.
Bytes
(),
PubKey
:
pubKey
.
Bytes
(),
}
}
}
}
...
@@ -91,7 +91,7 @@ func (valSet *ValidatorSet) Copy() *ValidatorSet {
...
@@ -91,7 +91,7 @@ func (valSet *ValidatorSet) Copy() *ValidatorSet {
validators
[
i
]
=
val
.
Copy
()
validators
[
i
]
=
val
.
Copy
()
}
}
return
&
ValidatorSet
{
return
&
ValidatorSet
{
Validators
:
validators
,
Validators
:
validators
,
}
}
}
}
...
...
plugin/consensus/dpos/validator_manager.go
View file @
b08c65a3
...
@@ -18,13 +18,7 @@ var (
...
@@ -18,13 +18,7 @@ var (
r
*
rand
.
Rand
r
*
rand
.
Rand
)
)
// State is a short description of the latest committed block of the Tendermint consensus.
// ValidatorMgr ...
// It keeps all information necessary to validate new blocks,
// including the last validator set and the consensus params.
// All fields are exposed so the struct can be easily serialized,
// but none of them should be mutated directly.
// Instead, use state.Copy() or state.NextState(...).
// NOTE: not goroutine-safe.
type
ValidatorMgr
struct
{
type
ValidatorMgr
struct
{
// Immutable
// Immutable
ChainID
string
ChainID
string
...
@@ -33,7 +27,7 @@ type ValidatorMgr struct {
...
@@ -33,7 +27,7 @@ type ValidatorMgr struct {
// so we can query for historical validator sets.
// so we can query for historical validator sets.
// Note that if s.LastBlockHeight causes a valset change,
// Note that if s.LastBlockHeight causes a valset change,
// we set s.LastHeightValidatorsChanged = s.LastBlockHeight + 1
// we set s.LastHeightValidatorsChanged = s.LastBlockHeight + 1
Validators
*
ttypes
.
ValidatorSet
Validators
*
ttypes
.
ValidatorSet
// The latest AppHash we've received from calling abci.Commit()
// The latest AppHash we've received from calling abci.Commit()
AppHash
[]
byte
AppHash
[]
byte
...
@@ -44,7 +38,7 @@ func (s ValidatorMgr) Copy() ValidatorMgr {
...
@@ -44,7 +38,7 @@ func (s ValidatorMgr) Copy() ValidatorMgr {
return
ValidatorMgr
{
return
ValidatorMgr
{
ChainID
:
s
.
ChainID
,
ChainID
:
s
.
ChainID
,
Validators
:
s
.
Validators
.
Copy
(),
Validators
:
s
.
Validators
.
Copy
(),
AppHash
:
s
.
AppHash
,
AppHash
:
s
.
AppHash
,
}
}
...
@@ -75,8 +69,7 @@ func (s ValidatorMgr) GetValidators() (current *ttypes.ValidatorSet) {
...
@@ -75,8 +69,7 @@ func (s ValidatorMgr) GetValidators() (current *ttypes.ValidatorSet) {
return
s
.
Validators
return
s
.
Validators
}
}
// MakeGenesisValidatorMgr creates validators from ttypes.GenesisDoc.
// MakeGenesisState creates state from ttypes.GenesisDoc.
func
MakeGenesisValidatorMgr
(
genDoc
*
ttypes
.
GenesisDoc
)
(
ValidatorMgr
,
error
)
{
func
MakeGenesisValidatorMgr
(
genDoc
*
ttypes
.
GenesisDoc
)
(
ValidatorMgr
,
error
)
{
err
:=
genDoc
.
ValidateAndComplete
()
err
:=
genDoc
.
ValidateAndComplete
()
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -93,14 +86,14 @@ func MakeGenesisValidatorMgr(genDoc *ttypes.GenesisDoc) (ValidatorMgr, error) {
...
@@ -93,14 +86,14 @@ func MakeGenesisValidatorMgr(genDoc *ttypes.GenesisDoc) (ValidatorMgr, error) {
// Make validator
// Make validator
validators
[
i
]
=
&
ttypes
.
Validator
{
validators
[
i
]
=
&
ttypes
.
Validator
{
Address
:
ttypes
.
GenAddressByPubKey
(
pubKey
),
Address
:
ttypes
.
GenAddressByPubKey
(
pubKey
),
PubKey
:
pubKey
.
Bytes
(),
PubKey
:
pubKey
.
Bytes
(),
}
}
}
}
return
ValidatorMgr
{
return
ValidatorMgr
{
ChainID
:
genDoc
.
ChainID
,
ChainID
:
genDoc
.
ChainID
,
Validators
:
ttypes
.
NewValidatorSet
(
validators
),
Validators
:
ttypes
.
NewValidatorSet
(
validators
),
AppHash
:
genDoc
.
AppHash
,
AppHash
:
genDoc
.
AppHash
,
},
nil
},
nil
}
}
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