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
697b6b89
Commit
697b6b89
authored
Sep 03, 2019
by
张振华
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
56e73c7f
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
32 changed files
with
317 additions
and
547 deletions
+317
-547
consensus_state.go
plugin/consensus/dpos/consensus_state.go
+0
-0
dpos.go
plugin/consensus/dpos/dpos.go
+30
-29
node.go
plugin/consensus/dpos/node.go
+2
-3
dpos_msg.proto
plugin/consensus/dpos/proto/dpos_msg.proto
+6
-0
state_machine.go
plugin/consensus/dpos/state_machine.go
+0
-0
state_machine_test.go
plugin/consensus/dpos/state_machine_test.go
+2
-2
ticker_test.go
plugin/consensus/dpos/ticker_test.go
+3
-4
dpos_msg.pb.go
plugin/consensus/dpos/types/dpos_msg.pb.go
+5
-0
genesis_test.go
plugin/consensus/dpos/types/genesis_test.go
+11
-13
msg.go
plugin/consensus/dpos/types/msg.go
+0
-1
priv_validator.go
plugin/consensus/dpos/types/priv_validator.go
+6
-19
priv_validator_test.go
plugin/consensus/dpos/types/priv_validator_test.go
+24
-30
signable.go
plugin/consensus/dpos/types/signable.go
+0
-0
signable_test.go
plugin/consensus/dpos/types/signable_test.go
+3
-5
util_test.go
plugin/consensus/dpos/types/util_test.go
+19
-19
validator_set_test.go
plugin/consensus/dpos/types/validator_set_test.go
+15
-15
validator_manager.go
plugin/consensus/dpos/validator_manager.go
+14
-4
validator_manager_test.go
plugin/consensus/dpos/validator_manager_test.go
+16
-22
test-rpc.sh
plugin/dapp/dposvote/cmd/build/test-rpc.sh
+5
-261
tool.sh
plugin/dapp/dposvote/cmd/tool.sh
+42
-42
vote.go
plugin/dapp/dposvote/commands/vote.go
+10
-14
dposvote.go
plugin/dapp/dposvote/executor/dposvote.go
+2
-2
dposvotedb.go
plugin/dapp/dposvote/executor/dposvotedb.go
+0
-0
exec_del_local.go
plugin/dapp/dposvote/executor/exec_del_local.go
+2
-2
exec_local.go
plugin/dapp/dposvote/executor/exec_local.go
+3
-3
query.go
plugin/dapp/dposvote/executor/query.go
+0
-0
dposvote.proto
plugin/dapp/dposvote/proto/dposvote.proto
+36
-12
const.go
plugin/dapp/dposvote/types/const.go
+19
-4
dposvote.pb.go
plugin/dapp/dposvote/types/dposvote.pb.go
+0
-0
msg.go
plugin/dapp/dposvote/types/msg.go
+38
-33
table.go
plugin/dapp/dposvote/types/table.go
+3
-7
types.go
plugin/dapp/dposvote/types/types.go
+1
-1
No files found.
plugin/consensus/dpos/consensus_state.go
View file @
697b6b89
This diff is collapsed.
Click to expand it.
plugin/consensus/dpos/dpos.go
View file @
697b6b89
...
@@ -29,6 +29,7 @@ import (
...
@@ -29,6 +29,7 @@ import (
const
dposVersion
=
"0.1.0"
const
dposVersion
=
"0.1.0"
const
dposShuffleTypeFixOrderByAddr
=
1
const
dposShuffleTypeFixOrderByAddr
=
1
const
dposShuffleTypeOrderByVrfInfo
=
2
const
dposShuffleTypeOrderByVrfInfo
=
2
var
(
var
(
dposlog
=
log15
.
New
(
"module"
,
"dpos"
)
dposlog
=
log15
.
New
(
"module"
,
"dpos"
)
genesis
string
genesis
string
...
@@ -47,8 +48,7 @@ var (
...
@@ -47,8 +48,7 @@ var (
dposCycle
=
dposDelegateNum
*
dposBlockInterval
*
dposContinueBlockNum
dposCycle
=
dposDelegateNum
*
dposBlockInterval
*
dposContinueBlockNum
dposPeriod
=
dposBlockInterval
*
dposContinueBlockNum
dposPeriod
=
dposBlockInterval
*
dposContinueBlockNum
zeroHash
[
32
]
byte
zeroHash
[
32
]
byte
dposPort
string
=
"36656"
dposPort
=
"36656"
rpcAddr
string
=
"http://0.0.0.0:8801"
shuffleType
int32
=
dposShuffleTypeOrderByVrfInfo
//shuffleType为1表示使用固定出块顺序,为2表示使用vrf信息进行出块顺序洗牌
shuffleType
int32
=
dposShuffleTypeOrderByVrfInfo
//shuffleType为1表示使用固定出块顺序,为2表示使用vrf信息进行出块顺序洗牌
whetherUpdateTopN
=
false
//是否更新topN,如果为true,根据下面几个配置项定期更新topN节点;如果为false,则一直使用初始配置的节点,不关注投票结果
whetherUpdateTopN
=
false
//是否更新topN,如果为true,根据下面几个配置项定期更新topN节点;如果为false,则一直使用初始配置的节点,不关注投票结果
blockNumToUpdateDelegate
int64
=
20000
blockNumToUpdateDelegate
int64
=
20000
...
@@ -91,7 +91,6 @@ type subConfig struct {
...
@@ -91,7 +91,6 @@ type subConfig struct {
ContinueBlockNum
int64
`json:"continueBlockNum"`
ContinueBlockNum
int64
`json:"continueBlockNum"`
IsValidator
bool
`json:"isValidator"`
IsValidator
bool
`json:"isValidator"`
Port
string
`json:"port"`
Port
string
`json:"port"`
RpcAddr
string
`json:"rpcAddr"`
ShuffleType
int32
`json:"shuffleType"`
ShuffleType
int32
`json:"shuffleType"`
WhetherUpdateTopN
bool
`json:"whetherUpdateTopN"`
WhetherUpdateTopN
bool
`json:"whetherUpdateTopN"`
BlockNumToUpdateDelegate
int64
`json:"blockNumToUpdateDelegate"`
BlockNumToUpdateDelegate
int64
`json:"blockNumToUpdateDelegate"`
...
@@ -155,10 +154,6 @@ func (client *Client) applyConfig(sub []byte) {
...
@@ -155,10 +154,6 @@ func (client *Client) applyConfig(sub []byte) {
isValidator
=
true
isValidator
=
true
}
}
if
subcfg
.
RpcAddr
!=
""
{
rpcAddr
=
subcfg
.
RpcAddr
}
if
subcfg
.
ShuffleType
>
0
{
if
subcfg
.
ShuffleType
>
0
{
shuffleType
=
subcfg
.
ShuffleType
shuffleType
=
subcfg
.
ShuffleType
}
}
...
@@ -319,8 +314,8 @@ OuterLoop:
...
@@ -319,8 +314,8 @@ OuterLoop:
var
topN
*
dty
.
TopNCandidators
var
topN
*
dty
.
TopNCandidators
for
version
>=
0
{
for
version
>=
0
{
topN
,
err
=
client
.
QueryTopNCandidators
(
version
)
topN
,
err
=
client
.
QueryTopNCandidators
(
version
)
if
err
!=
nil
||
topN
==
nil
{
if
err
!=
nil
||
topN
==
nil
{
version
--
version
--
}
else
{
}
else
{
break
break
}
}
...
@@ -339,7 +334,7 @@ OuterLoop:
...
@@ -339,7 +334,7 @@ OuterLoop:
Address
:
address
.
PubKeyToAddress
(
val
.
Pubkey
)
.
Hash160
[
:
],
Address
:
address
.
PubKeyToAddress
(
val
.
Pubkey
)
.
Hash160
[
:
],
PubKey
:
val
.
Pubkey
,
PubKey
:
val
.
Pubkey
,
}
}
nodes
[
i
]
=
val
.
I
p
+
":"
+
dposPort
nodes
[
i
]
=
val
.
I
P
+
":"
+
dposPort
}
}
valMgr
.
Validators
=
ttypes
.
NewValidatorSet
(
validators
)
valMgr
.
Validators
=
ttypes
.
NewValidatorSet
(
validators
)
dposlog
.
Info
(
"QueryCandidators success and update validator set"
,
"old validators"
,
printValidators
(
valMgrTmp
.
Validators
),
"new validators"
,
printValidators
(
valMgr
.
Validators
))
dposlog
.
Info
(
"QueryCandidators success and update validator set"
,
"old validators"
,
printValidators
(
valMgrTmp
.
Validators
),
"new validators"
,
printValidators
(
valMgr
.
Validators
))
...
@@ -365,7 +360,7 @@ OuterLoop:
...
@@ -365,7 +360,7 @@ OuterLoop:
csState
.
SetPrivValidator
(
client
.
privValidator
,
client
.
ValidatorIndex
())
csState
.
SetPrivValidator
(
client
.
privValidator
,
client
.
ValidatorIndex
())
// Create & add listener
// Create & add listener
protocol
,
listeningAddress
:=
"tcp"
,
"0.0.0.0:"
+
dposPort
protocol
,
listeningAddress
:=
"tcp"
,
"0.0.0.0:"
+
dposPort
node
:=
NewNode
(
validatorNodes
,
protocol
,
listeningAddress
,
client
.
privKey
,
valMgr
.
ChainID
,
dposVersion
,
csState
)
node
:=
NewNode
(
validatorNodes
,
protocol
,
listeningAddress
,
client
.
privKey
,
valMgr
.
ChainID
,
dposVersion
,
csState
)
client
.
node
=
node
client
.
node
=
node
...
@@ -381,7 +376,7 @@ OuterLoop:
...
@@ -381,7 +376,7 @@ OuterLoop:
//go client.MonitorCandidators()
//go client.MonitorCandidators()
}
}
func
printValidators
(
set
*
ttypes
.
ValidatorSet
)
string
{
func
printValidators
(
set
*
ttypes
.
ValidatorSet
)
string
{
result
:=
"Validators:["
result
:=
"Validators:["
for
_
,
v
:=
range
set
.
Validators
{
for
_
,
v
:=
range
set
.
Validators
{
result
=
fmt
.
Sprintf
(
"%s%s,"
,
result
,
hex
.
EncodeToString
(
v
.
PubKey
))
result
=
fmt
.
Sprintf
(
"%s%s,"
,
result
,
hex
.
EncodeToString
(
v
.
PubKey
))
...
@@ -391,7 +386,7 @@ func printValidators(set *ttypes.ValidatorSet) string{
...
@@ -391,7 +386,7 @@ func printValidators(set *ttypes.ValidatorSet) string{
return
result
return
result
}
}
func
printNodeIPs
(
ips
[]
string
)
string
{
func
printNodeIPs
(
ips
[]
string
)
string
{
result
:=
"nodeIPs:["
result
:=
"nodeIPs:["
for
_
,
v
:=
range
ips
{
for
_
,
v
:=
range
ips
{
result
=
fmt
.
Sprintf
(
"%s%s,"
,
result
,
v
)
result
=
fmt
.
Sprintf
(
"%s%s,"
,
result
,
v
)
...
@@ -504,7 +499,8 @@ func (client *Client) ValidatorIndex() int {
...
@@ -504,7 +499,8 @@ func (client *Client) ValidatorIndex() int {
return
-
1
return
-
1
}
}
func
(
client
*
Client
)
QueryCandidators
()([]
*
dty
.
Candidator
,
error
)
{
// QueryCandidators query the topN candidators from blockchain
func
(
client
*
Client
)
QueryCandidators
()
([]
*
dty
.
Candidator
,
error
)
{
req
:=
&
dty
.
CandidatorQuery
{
req
:=
&
dty
.
CandidatorQuery
{
TopN
:
int32
(
dposDelegateNum
),
TopN
:
int32
(
dposDelegateNum
),
}
}
...
@@ -518,7 +514,7 @@ func (client *Client)QueryCandidators()([]*dty.Candidator, error) {
...
@@ -518,7 +514,7 @@ func (client *Client)QueryCandidators()([]*dty.Candidator, error) {
Driver
:
dty
.
DPosX
,
Driver
:
dty
.
DPosX
,
FuncName
:
dty
.
FuncNameQueryCandidatorByTopN
,
FuncName
:
dty
.
FuncNameQueryCandidatorByTopN
,
StateHash
:
zeroHash
[
:
],
StateHash
:
zeroHash
[
:
],
Param
:
param
,
Param
:
param
,
})
})
err
=
client
.
GetQueueClient
()
.
Send
(
msg
,
true
)
err
=
client
.
GetQueueClient
()
.
Send
(
msg
,
true
)
...
@@ -545,7 +541,7 @@ func (client *Client)QueryCandidators()([]*dty.Candidator, error) {
...
@@ -545,7 +541,7 @@ func (client *Client)QueryCandidators()([]*dty.Candidator, error) {
cand
:=
&
dty
.
Candidator
{
cand
:=
&
dty
.
Candidator
{
Pubkey
:
bPubkey
,
Pubkey
:
bPubkey
,
Address
:
val
.
Address
,
Address
:
val
.
Address
,
I
p
:
val
.
Ip
,
I
P
:
val
.
IP
,
Votes
:
val
.
Votes
,
Votes
:
val
.
Votes
,
Status
:
val
.
Status
,
Status
:
val
.
Status
,
}
}
...
@@ -555,13 +551,13 @@ func (client *Client)QueryCandidators()([]*dty.Candidator, error) {
...
@@ -555,13 +551,13 @@ func (client *Client)QueryCandidators()([]*dty.Candidator, error) {
return
cands
,
nil
return
cands
,
nil
}
}
func
(
client
*
Client
)
isValidatorSetSame
(
v1
,
v2
*
ttypes
.
ValidatorSet
)
bool
{
func
(
client
*
Client
)
isValidatorSetSame
(
v1
,
v2
*
ttypes
.
ValidatorSet
)
bool
{
if
v1
==
nil
||
v2
==
nil
||
len
(
v1
.
Validators
)
!=
len
(
v2
.
Validators
){
if
v1
==
nil
||
v2
==
nil
||
len
(
v1
.
Validators
)
!=
len
(
v2
.
Validators
)
{
return
false
return
false
}
}
for
i
:=
0
;
i
<
len
(
v1
.
Validators
);
i
++
{
for
i
:=
0
;
i
<
len
(
v1
.
Validators
);
i
++
{
if
!
bytes
.
Equal
(
v1
.
Validators
[
i
]
.
PubKey
,
v2
.
Validators
[
i
]
.
PubKey
){
if
!
bytes
.
Equal
(
v1
.
Validators
[
i
]
.
PubKey
,
v2
.
Validators
[
i
]
.
PubKey
)
{
return
false
return
false
}
}
}
}
...
@@ -569,7 +565,8 @@ func (client *Client)isValidatorSetSame(v1, v2 *ttypes.ValidatorSet) bool {
...
@@ -569,7 +565,8 @@ func (client *Client)isValidatorSetSame(v1, v2 *ttypes.ValidatorSet) bool {
return
true
return
true
}
}
func
(
client
*
Client
)
CreateRecordCBTx
(
info
*
dty
.
DposCBInfo
)(
tx
*
types
.
Transaction
,
err
error
)
{
// CreateRecordCBTx create the tx to record cb
func
(
client
*
Client
)
CreateRecordCBTx
(
info
*
dty
.
DposCBInfo
)
(
tx
*
types
.
Transaction
,
err
error
)
{
var
action
dty
.
DposVoteAction
var
action
dty
.
DposVoteAction
action
.
Value
=
&
dty
.
DposVoteAction_RecordCB
{
action
.
Value
=
&
dty
.
DposVoteAction_RecordCB
{
RecordCB
:
info
,
RecordCB
:
info
,
...
@@ -583,7 +580,8 @@ func (client *Client)CreateRecordCBTx(info *dty.DposCBInfo)(tx*types.Transaction
...
@@ -583,7 +580,8 @@ func (client *Client)CreateRecordCBTx(info *dty.DposCBInfo)(tx*types.Transaction
return
tx
,
nil
return
tx
,
nil
}
}
func
(
client
*
Client
)
CreateRegVrfMTx
(
info
*
dty
.
DposVrfMRegist
)(
tx
*
types
.
Transaction
,
err
error
)
{
// CreateRegVrfMTx create the tx to regist Vrf M
func
(
client
*
Client
)
CreateRegVrfMTx
(
info
*
dty
.
DposVrfMRegist
)
(
tx
*
types
.
Transaction
,
err
error
)
{
var
action
dty
.
DposVoteAction
var
action
dty
.
DposVoteAction
action
.
Value
=
&
dty
.
DposVoteAction_RegistVrfM
{
action
.
Value
=
&
dty
.
DposVoteAction_RegistVrfM
{
RegistVrfM
:
info
,
RegistVrfM
:
info
,
...
@@ -597,7 +595,8 @@ func (client *Client)CreateRegVrfMTx(info *dty.DposVrfMRegist)(tx*types.Transact
...
@@ -597,7 +595,8 @@ func (client *Client)CreateRegVrfMTx(info *dty.DposVrfMRegist)(tx*types.Transact
return
tx
,
nil
return
tx
,
nil
}
}
func
(
client
*
Client
)
CreateRegVrfRPTx
(
info
*
dty
.
DposVrfRPRegist
)(
tx
*
types
.
Transaction
,
err
error
)
{
// CreateRegVrfRPTx create the tx to regist Vrf RP
func
(
client
*
Client
)
CreateRegVrfRPTx
(
info
*
dty
.
DposVrfRPRegist
)
(
tx
*
types
.
Transaction
,
err
error
)
{
var
action
dty
.
DposVoteAction
var
action
dty
.
DposVoteAction
action
.
Value
=
&
dty
.
DposVoteAction_RegistVrfRP
{
action
.
Value
=
&
dty
.
DposVoteAction_RegistVrfRP
{
RegistVrfRP
:
info
,
RegistVrfRP
:
info
,
...
@@ -611,7 +610,8 @@ func (client *Client)CreateRegVrfRPTx(info *dty.DposVrfRPRegist)(tx*types.Transa
...
@@ -611,7 +610,8 @@ func (client *Client)CreateRegVrfRPTx(info *dty.DposVrfRPRegist)(tx*types.Transa
return
tx
,
nil
return
tx
,
nil
}
}
func
(
client
*
Client
)
QueryVrfInfos
(
pubkeys
[][]
byte
,
cycle
int64
)([]
*
dty
.
VrfInfo
,
error
)
{
// QueryVrfInfos query the vrf infos by pubkeys
func
(
client
*
Client
)
QueryVrfInfos
(
pubkeys
[][]
byte
,
cycle
int64
)
([]
*
dty
.
VrfInfo
,
error
)
{
req
:=
&
dty
.
DposVrfQuery
{
req
:=
&
dty
.
DposVrfQuery
{
Cycle
:
cycle
,
Cycle
:
cycle
,
Ty
:
dty
.
QueryVrfByCycleForPubkeys
,
Ty
:
dty
.
QueryVrfByCycleForPubkeys
,
...
@@ -631,7 +631,7 @@ func (client *Client)QueryVrfInfos(pubkeys [][]byte, cycle int64)([]*dty.VrfInfo
...
@@ -631,7 +631,7 @@ func (client *Client)QueryVrfInfos(pubkeys [][]byte, cycle int64)([]*dty.VrfInfo
Driver
:
dty
.
DPosX
,
Driver
:
dty
.
DPosX
,
FuncName
:
dty
.
FuncNameQueryVrfByCycleForPubkeys
,
FuncName
:
dty
.
FuncNameQueryVrfByCycleForPubkeys
,
StateHash
:
zeroHash
[
:
],
StateHash
:
zeroHash
[
:
],
Param
:
param
,
Param
:
param
,
})
})
err
=
client
.
GetQueueClient
()
.
Send
(
msg
,
true
)
err
=
client
.
GetQueueClient
()
.
Send
(
msg
,
true
)
...
@@ -693,7 +693,8 @@ func (client *Client)QueryVrfInfos(pubkeys [][]byte, cycle int64)([]*dty.VrfInfo
...
@@ -693,7 +693,8 @@ func (client *Client)QueryVrfInfos(pubkeys [][]byte, cycle int64)([]*dty.VrfInfo
return
infos
,
nil
return
infos
,
nil
}
}
func
(
client
*
Client
)
CreateTopNRegistTx
(
reg
*
dty
.
TopNCandidatorRegist
)(
tx
*
types
.
Transaction
,
err
error
)
{
// CreateTopNRegistTx create tx to regist topN
func
(
client
*
Client
)
CreateTopNRegistTx
(
reg
*
dty
.
TopNCandidatorRegist
)
(
tx
*
types
.
Transaction
,
err
error
)
{
var
action
dty
.
DposVoteAction
var
action
dty
.
DposVoteAction
action
.
Value
=
&
dty
.
DposVoteAction_RegistTopN
{
action
.
Value
=
&
dty
.
DposVoteAction_RegistTopN
{
RegistTopN
:
reg
,
RegistTopN
:
reg
,
...
@@ -707,8 +708,8 @@ func (client *Client)CreateTopNRegistTx(reg *dty.TopNCandidatorRegist)(tx*types.
...
@@ -707,8 +708,8 @@ func (client *Client)CreateTopNRegistTx(reg *dty.TopNCandidatorRegist)(tx*types.
return
tx
,
nil
return
tx
,
nil
}
}
// Query
CycleBoundaryInfo
method
// Query
TopNCandidators
method
func
(
client
*
Client
)
QueryTopNCandidators
(
version
int64
)
(
*
dty
.
TopNCandidators
,
error
)
{
func
(
client
*
Client
)
QueryTopNCandidators
(
version
int64
)
(
*
dty
.
TopNCandidators
,
error
)
{
req
:=
&
dty
.
TopNCandidatorsQuery
{
Version
:
version
}
req
:=
&
dty
.
TopNCandidatorsQuery
{
Version
:
version
}
param
,
err
:=
proto
.
Marshal
(
req
)
param
,
err
:=
proto
.
Marshal
(
req
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -720,7 +721,7 @@ func (client *Client) QueryTopNCandidators(version int64)(*dty.TopNCandidators,
...
@@ -720,7 +721,7 @@ func (client *Client) QueryTopNCandidators(version int64)(*dty.TopNCandidators,
Driver
:
dty
.
DPosX
,
Driver
:
dty
.
DPosX
,
FuncName
:
dty
.
FuncNameQueryTopNByVersion
,
FuncName
:
dty
.
FuncNameQueryTopNByVersion
,
StateHash
:
zeroHash
[
:
],
StateHash
:
zeroHash
[
:
],
Param
:
param
,
Param
:
param
,
})
})
err
=
client
.
GetQueueClient
()
.
Send
(
msg
,
true
)
err
=
client
.
GetQueueClient
()
.
Send
(
msg
,
true
)
...
@@ -745,7 +746,7 @@ func (client *Client) QueryTopNCandidators(version int64)(*dty.TopNCandidators,
...
@@ -745,7 +746,7 @@ func (client *Client) QueryTopNCandidators(version int64)(*dty.TopNCandidators,
func
printCandidators
(
cands
[]
*
dty
.
Candidator
)
string
{
func
printCandidators
(
cands
[]
*
dty
.
Candidator
)
string
{
result
:=
"["
result
:=
"["
for
i
:=
0
;
i
<
len
(
cands
);
i
++
{
for
i
:=
0
;
i
<
len
(
cands
);
i
++
{
fmt
.
Sprintf
(
"%spubkey:%s,ip:%s;"
,
result
,
hex
.
EncodeToString
(
cands
[
i
]
.
Pubkey
),
cands
[
i
]
.
Ip
)
result
=
fmt
.
Sprintf
(
"%spubkey:%s,ip:%s;"
,
result
,
hex
.
EncodeToString
(
cands
[
i
]
.
Pubkey
),
cands
[
i
]
.
IP
)
}
}
result
+=
"]"
result
+=
"]"
...
...
plugin/consensus/dpos/node.go
View file @
697b6b89
...
@@ -14,7 +14,6 @@ import (
...
@@ -14,7 +14,6 @@ import (
"sync"
"sync"
"sync/atomic"
"sync/atomic"
"time"
"time"
"errors"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/common/crypto"
ttypes
"github.com/33cn/plugin/plugin/consensus/dpos/types"
ttypes
"github.com/33cn/plugin/plugin/consensus/dpos/types"
...
@@ -294,7 +293,7 @@ func (node *Node) listenRoutine() {
...
@@ -294,7 +293,7 @@ func (node *Node) listenRoutine() {
func
(
node
*
Node
)
StartConsensusRoutine
()
{
func
(
node
*
Node
)
StartConsensusRoutine
()
{
for
{
for
{
//zzh
//zzh
if
!
node
.
IsRunning
(){
if
!
node
.
IsRunning
()
{
break
break
}
}
//TODO:the peer count need be optimized
//TODO:the peer count need be optimized
...
@@ -502,7 +501,7 @@ func (node *Node) FilterConnByAddr(addr net.Addr) error {
...
@@ -502,7 +501,7 @@ func (node *Node) FilterConnByAddr(addr net.Addr) error {
}
}
if
!
legalIP
{
if
!
legalIP
{
return
errors
.
New
(
fmt
.
Sprintf
(
"%s is not legal seeds ip"
,
ip
)
)
return
fmt
.
Errorf
(
"%s is not legal seeds ip"
,
ip
)
}
}
return
nil
return
nil
}
}
...
...
plugin/consensus/dpos/proto/dpos_msg.proto
View file @
697b6b89
...
@@ -3,16 +3,20 @@ syntax = "proto3";
...
@@ -3,16 +3,20 @@ syntax = "proto3";
package
types
;
package
types
;
//CycleBoundaryInfo cycle边界信息
message
CycleBoundaryInfo
{
message
CycleBoundaryInfo
{
int64
cycle
=
1
;
int64
cycle
=
1
;
int64
stopHeight
=
2
;
int64
stopHeight
=
2
;
string
stopHash
=
3
;
string
stopHash
=
3
;
}
}
//SuperNode 超级节点信息
message
SuperNode
{
message
SuperNode
{
bytes
address
=
1
;
bytes
address
=
1
;
bytes
pubKey
=
2
;
bytes
pubKey
=
2
;
}
}
//VoteItem 投票信息
message
VoteItem
{
message
VoteItem
{
int32
votedNodeIndex
=
1
;
//被投票的节点索引
int32
votedNodeIndex
=
1
;
//被投票的节点索引
bytes
votedNodeAddress
=
2
;
//被投票的节点地址
bytes
votedNodeAddress
=
2
;
//被投票的节点地址
...
@@ -39,6 +43,7 @@ message DPosVote {
...
@@ -39,6 +43,7 @@ message DPosVote {
bytes
signature
=
5
;
//投票者签名
bytes
signature
=
5
;
//投票者签名
}
}
//DPosVoteReply 投票响应。
message
DPosVoteReply
{
message
DPosVoteReply
{
DPosVote
vote
=
1
;
DPosVote
vote
=
1
;
}
}
...
@@ -54,6 +59,7 @@ message DPosNotify {
...
@@ -54,6 +59,7 @@ message DPosNotify {
bytes
signature
=
7
;
//通知节点的签名
bytes
signature
=
7
;
//通知节点的签名
}
}
//DPosCBInfo Cycle boundary注册信息。
message
DPosCBInfo
{
message
DPosCBInfo
{
int64
cycle
=
1
;
int64
cycle
=
1
;
int64
stopHeight
=
2
;
int64
stopHeight
=
2
;
...
...
plugin/consensus/dpos/state_machine.go
View file @
697b6b89
This diff is collapsed.
Click to expand it.
plugin/consensus/dpos/state_machine_test.go
View file @
697b6b89
...
@@ -69,7 +69,7 @@ func TestDecideTaskByTime(t *testing.T) {
...
@@ -69,7 +69,7 @@ func TestDecideTaskByTime(t *testing.T) {
setParams
(
2
,
3
,
12
)
setParams
(
2
,
3
,
12
)
/*
/*
for i := 0; i < 120; i++ {
for i := 0; i < 120; i++ {
now = time.Now().Unix()
now = time.Now().Unix()
task = DecideTaskByTime(now)
task = DecideTaskByTime(now)
...
@@ -77,5 +77,5 @@ func TestDecideTaskByTime(t *testing.T) {
...
@@ -77,5 +77,5 @@ func TestDecideTaskByTime(t *testing.T) {
assertTask(&task, t)
assertTask(&task, t)
time.Sleep(time.Second * 1)
time.Sleep(time.Second * 1)
}
}
*/
*/
}
}
plugin/consensus/dpos/ticker_test.go
View file @
697b6b89
package
dpos
package
dpos
import
(
import
(
"github.com/stretchr/testify/assert"
"testing"
"testing"
"time"
"time"
"github.com/stretchr/testify/assert"
)
)
func
TestTicker
(
t
*
testing
.
T
)
{
func
TestTicker
(
t
*
testing
.
T
)
{
...
@@ -18,10 +17,10 @@ func TestTicker(t *testing.T) {
...
@@ -18,10 +17,10 @@ func TestTicker(t *testing.T) {
now
:=
time
.
Now
()
.
Unix
()
now
:=
time
.
Now
()
.
Unix
()
ticker
.
ScheduleTimeout
(
ti
)
ticker
.
ScheduleTimeout
(
ti
)
<-
ticker
.
Chan
()
<-
ticker
.
Chan
()
end
:=
time
.
Now
()
.
Unix
()
end
:=
time
.
Now
()
.
Unix
()
ticker
.
Stop
()
ticker
.
Stop
()
assert
.
True
(
t
,
end
-
now
==
2
)
assert
.
True
(
t
,
end
-
now
==
2
)
}
}
plugin/consensus/dpos/types/dpos_msg.pb.go
View file @
697b6b89
...
@@ -33,6 +33,7 @@ var _ = math.Inf
...
@@ -33,6 +33,7 @@ var _ = math.Inf
// proto package needs to be updated.
// proto package needs to be updated.
const
_
=
proto
.
ProtoPackageIsVersion2
// please upgrade the proto package
const
_
=
proto
.
ProtoPackageIsVersion2
// please upgrade the proto package
// CycleBoundaryInfo cycle边界信息
type
CycleBoundaryInfo
struct
{
type
CycleBoundaryInfo
struct
{
Cycle
int64
`protobuf:"varint,1,opt,name=cycle" json:"cycle,omitempty"`
Cycle
int64
`protobuf:"varint,1,opt,name=cycle" json:"cycle,omitempty"`
StopHeight
int64
`protobuf:"varint,2,opt,name=stopHeight" json:"stopHeight,omitempty"`
StopHeight
int64
`protobuf:"varint,2,opt,name=stopHeight" json:"stopHeight,omitempty"`
...
@@ -65,6 +66,7 @@ func (m *CycleBoundaryInfo) GetStopHash() string {
...
@@ -65,6 +66,7 @@ func (m *CycleBoundaryInfo) GetStopHash() string {
return
""
return
""
}
}
// SuperNode 超级节点信息
type
SuperNode
struct
{
type
SuperNode
struct
{
Address
[]
byte
`protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"`
Address
[]
byte
`protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"`
PubKey
[]
byte
`protobuf:"bytes,2,opt,name=pubKey,proto3" json:"pubKey,omitempty"`
PubKey
[]
byte
`protobuf:"bytes,2,opt,name=pubKey,proto3" json:"pubKey,omitempty"`
...
@@ -89,6 +91,7 @@ func (m *SuperNode) GetPubKey() []byte {
...
@@ -89,6 +91,7 @@ func (m *SuperNode) GetPubKey() []byte {
return
nil
return
nil
}
}
// VoteItem 投票信息
type
VoteItem
struct
{
type
VoteItem
struct
{
VotedNodeIndex
int32
`protobuf:"varint,1,opt,name=votedNodeIndex" json:"votedNodeIndex,omitempty"`
VotedNodeIndex
int32
`protobuf:"varint,1,opt,name=votedNodeIndex" json:"votedNodeIndex,omitempty"`
VotedNodeAddress
[]
byte
`protobuf:"bytes,2,opt,name=votedNodeAddress,proto3" json:"votedNodeAddress,omitempty"`
VotedNodeAddress
[]
byte
`protobuf:"bytes,2,opt,name=votedNodeAddress,proto3" json:"votedNodeAddress,omitempty"`
...
@@ -258,6 +261,7 @@ func (m *DPosVote) GetSignature() []byte {
...
@@ -258,6 +261,7 @@ func (m *DPosVote) GetSignature() []byte {
return
nil
return
nil
}
}
// DPosVoteReply 投票响应。
type
DPosVoteReply
struct
{
type
DPosVoteReply
struct
{
Vote
*
DPosVote
`protobuf:"bytes,1,opt,name=vote" json:"vote,omitempty"`
Vote
*
DPosVote
`protobuf:"bytes,1,opt,name=vote" json:"vote,omitempty"`
}
}
...
@@ -339,6 +343,7 @@ func (m *DPosNotify) GetSignature() []byte {
...
@@ -339,6 +343,7 @@ func (m *DPosNotify) GetSignature() []byte {
return
nil
return
nil
}
}
// DPosCBInfo Cycle boundary注册信息。
type
DPosCBInfo
struct
{
type
DPosCBInfo
struct
{
Cycle
int64
`protobuf:"varint,1,opt,name=cycle" json:"cycle,omitempty"`
Cycle
int64
`protobuf:"varint,1,opt,name=cycle" json:"cycle,omitempty"`
StopHeight
int64
`protobuf:"varint,2,opt,name=stopHeight" json:"stopHeight,omitempty"`
StopHeight
int64
`protobuf:"varint,2,opt,name=stopHeight" json:"stopHeight,omitempty"`
...
...
plugin/consensus/dpos/types/genesis_test.go
View file @
697b6b89
...
@@ -9,18 +9,18 @@ import (
...
@@ -9,18 +9,18 @@ import (
"os"
"os"
"testing"
"testing"
)
)
const
(
err_genesis_file
=
`{"genesis_time:"2018-08-16T15:38:56.951569432+08:00","chain_id":"chain33-Z2cgFj","validators":[{"pub_key":{"type":"secp256k1","data":"03EF0E1D3112CF571743A3318125EDE2E52A4EB904BCBAA4B1F75020C2846A7EB4"},"name":""},{"pub_key":{"type":"secp256k1","data":"027848E7FA630B759DB406940B5506B666A344B1060794BBF314EB459D40881BB3"},"name":""},{"pub_key":{"type":"secp256k1","data":"03F4AB6659E61E8512C9A24AC385CC1AC4D52B87D10ADBDF060086EA82BE62CDDE"},"name":""}],"app_hash":null}`
genesis_file
=
`{"genesis_time":"2018-08-16T15:38:56.951569432+08:00","chain_id":"chain33-Z2cgFj","validators":[{"pub_key":{"type":"secp256k1","data":"03EF0E1D3112CF571743A3318125EDE2E52A4EB904BCBAA4B1F75020C2846A7EB4"},"name":""},{"pub_key":{"type":"secp256k1","data":"027848E7FA630B759DB406940B5506B666A344B1060794BBF314EB459D40881BB3"},"name":""},{"pub_key":{"type":"secp256k1","data":"03F4AB6659E61E8512C9A24AC385CC1AC4D52B87D10ADBDF060086EA82BE62CDDE"},"name":""}],"app_hash":null}`
const
(
errGenesisFile
=
`{"genesis_time:"2018-08-16T15:38:56.951569432+08:00","chain_id":"chain33-Z2cgFj","validators":[{"pub_key":{"type":"secp256k1","data":"03EF0E1D3112CF571743A3318125EDE2E52A4EB904BCBAA4B1F75020C2846A7EB4"},"name":""},{"pub_key":{"type":"secp256k1","data":"027848E7FA630B759DB406940B5506B666A344B1060794BBF314EB459D40881BB3"},"name":""},{"pub_key":{"type":"secp256k1","data":"03F4AB6659E61E8512C9A24AC385CC1AC4D52B87D10ADBDF060086EA82BE62CDDE"},"name":""}],"app_hash":null}`
genesisFile
=
`{"genesis_time":"2018-08-16T15:38:56.951569432+08:00","chain_id":"chain33-Z2cgFj","validators":[{"pub_key":{"type":"secp256k1","data":"03EF0E1D3112CF571743A3318125EDE2E52A4EB904BCBAA4B1F75020C2846A7EB4"},"name":""},{"pub_key":{"type":"secp256k1","data":"027848E7FA630B759DB406940B5506B666A344B1060794BBF314EB459D40881BB3"},"name":""},{"pub_key":{"type":"secp256k1","data":"03F4AB6659E61E8512C9A24AC385CC1AC4D52B87D10ADBDF060086EA82BE62CDDE"},"name":""}],"app_hash":null}`
)
)
func
init
(){
func
init
()
{
//为了使用VRF,需要使用SECP256K1体系的公私钥
//为了使用VRF,需要使用SECP256K1体系的公私钥
cr
,
err
:=
crypto
.
New
(
types
.
GetSignName
(
""
,
types
.
SECP256K1
))
cr
,
err
:=
crypto
.
New
(
types
.
GetSignName
(
""
,
types
.
SECP256K1
))
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
"init ConsensusCrypto failed."
)
panic
(
"init ConsensusCrypto failed."
)
return
}
}
ConsensusCrypto
=
cr
ConsensusCrypto
=
cr
...
@@ -37,21 +37,20 @@ func TestGenesisDocFromFile(t *testing.T) {
...
@@ -37,21 +37,20 @@ func TestGenesisDocFromFile(t *testing.T) {
}
}
func
TestGenesisDocFromJSON
(
t
*
testing
.
T
)
{
func
TestGenesisDocFromJSON
(
t
*
testing
.
T
)
{
genDoc
,
err
:=
GenesisDocFromJSON
([]
byte
(
genesis
_f
ile
))
genDoc
,
err
:=
GenesisDocFromJSON
([]
byte
(
genesis
F
ile
))
require
.
NotNil
(
t
,
genDoc
)
require
.
NotNil
(
t
,
genDoc
)
require
.
Nil
(
t
,
err
)
require
.
Nil
(
t
,
err
)
assert
.
True
(
t
,
genDoc
.
ChainID
==
"chain33-Z2cgFj"
)
assert
.
True
(
t
,
genDoc
.
ChainID
==
"chain33-Z2cgFj"
)
assert
.
True
(
t
,
genDoc
.
AppHash
==
nil
)
assert
.
True
(
t
,
genDoc
.
AppHash
==
nil
)
assert
.
True
(
t
,
len
(
genDoc
.
Validators
)
==
3
)
assert
.
True
(
t
,
len
(
genDoc
.
Validators
)
==
3
)
genDoc
,
err
=
GenesisDocFromJSON
([]
byte
(
errGenesisFile
))
genDoc
,
err
=
GenesisDocFromJSON
([]
byte
(
err_genesis_file
))
require
.
NotNil
(
t
,
err
)
require
.
NotNil
(
t
,
err
)
require
.
Nil
(
t
,
genDoc
)
require
.
Nil
(
t
,
genDoc
)
}
}
func
TestSaveAs
(
t
*
testing
.
T
)
{
func
TestSaveAs
(
t
*
testing
.
T
)
{
genDoc
,
err
:=
GenesisDocFromJSON
([]
byte
(
genesis
_f
ile
))
genDoc
,
err
:=
GenesisDocFromJSON
([]
byte
(
genesis
F
ile
))
require
.
NotNil
(
t
,
genDoc
)
require
.
NotNil
(
t
,
genDoc
)
require
.
Nil
(
t
,
err
)
require
.
Nil
(
t
,
err
)
assert
.
True
(
t
,
genDoc
.
ChainID
==
"chain33-Z2cgFj"
)
assert
.
True
(
t
,
genDoc
.
ChainID
==
"chain33-Z2cgFj"
)
...
@@ -61,13 +60,12 @@ func TestSaveAs(t *testing.T) {
...
@@ -61,13 +60,12 @@ func TestSaveAs(t *testing.T) {
err
=
genDoc
.
SaveAs
(
"./tmp_genesis.json"
)
err
=
genDoc
.
SaveAs
(
"./tmp_genesis.json"
)
require
.
Nil
(
t
,
err
)
require
.
Nil
(
t
,
err
)
genDoc2
,
err
:=
GenesisDocFromFile
(
"./tmp_genesis.json"
)
genDoc2
,
err
:=
GenesisDocFromFile
(
"./tmp_genesis.json"
)
require
.
NotNil
(
t
,
genDoc2
)
require
.
NotNil
(
t
,
genDoc2
)
require
.
Nil
(
t
,
err
)
require
.
Nil
(
t
,
err
)
assert
.
True
(
t
,
genDoc
.
ChainID
==
genDoc2
.
ChainID
)
assert
.
True
(
t
,
genDoc
.
ChainID
==
genDoc2
.
ChainID
)
assert
.
True
(
t
,
genDoc
.
GenesisTime
==
genDoc2
.
GenesisTime
)
assert
.
True
(
t
,
genDoc
.
GenesisTime
==
genDoc2
.
GenesisTime
)
assert
.
True
(
t
,
bytes
.
Equal
(
genDoc
.
AppHash
,
genDoc2
.
AppHash
))
assert
.
True
(
t
,
bytes
.
Equal
(
genDoc
.
AppHash
,
genDoc2
.
AppHash
))
assert
.
True
(
t
,
genDoc
.
Validators
[
0
]
.
Name
==
genDoc2
.
Validators
[
0
]
.
Name
)
assert
.
True
(
t
,
genDoc
.
Validators
[
0
]
.
Name
==
genDoc2
.
Validators
[
0
]
.
Name
)
assert
.
True
(
t
,
genDoc
.
Validators
[
0
]
.
PubKey
.
Data
==
genDoc2
.
Validators
[
0
]
.
PubKey
.
Data
)
assert
.
True
(
t
,
genDoc
.
Validators
[
0
]
.
PubKey
.
Data
==
genDoc2
.
Validators
[
0
]
.
PubKey
.
Data
)
...
@@ -87,7 +85,7 @@ func TestSaveAs(t *testing.T) {
...
@@ -87,7 +85,7 @@ func TestSaveAs(t *testing.T) {
}
}
func
TestValidateAndComplete
(
t
*
testing
.
T
)
{
func
TestValidateAndComplete
(
t
*
testing
.
T
)
{
genDoc
,
err
:=
GenesisDocFromJSON
([]
byte
(
genesis
_f
ile
))
genDoc
,
err
:=
GenesisDocFromJSON
([]
byte
(
genesis
F
ile
))
require
.
NotNil
(
t
,
genDoc
)
require
.
NotNil
(
t
,
genDoc
)
require
.
Nil
(
t
,
err
)
require
.
Nil
(
t
,
err
)
...
@@ -110,7 +108,7 @@ func TestValidateAndComplete(t *testing.T) {
...
@@ -110,7 +108,7 @@ func TestValidateAndComplete(t *testing.T) {
}
}
func
TestValidatorHash
(
t
*
testing
.
T
)
{
func
TestValidatorHash
(
t
*
testing
.
T
)
{
genDoc
,
err
:=
GenesisDocFromJSON
([]
byte
(
genesis
_f
ile
))
genDoc
,
err
:=
GenesisDocFromJSON
([]
byte
(
genesis
F
ile
))
require
.
NotNil
(
t
,
genDoc
)
require
.
NotNil
(
t
,
genDoc
)
require
.
Nil
(
t
,
err
)
require
.
Nil
(
t
,
err
)
...
...
plugin/consensus/dpos/types/msg.go
View file @
697b6b89
...
@@ -21,7 +21,6 @@ const (
...
@@ -21,7 +21,6 @@ const (
NotifyID
=
byte
(
0x08
)
NotifyID
=
byte
(
0x08
)
CBInfoID
=
byte
(
0x09
)
CBInfoID
=
byte
(
0x09
)
PacketTypePing
=
byte
(
0xff
)
PacketTypePing
=
byte
(
0xff
)
PacketTypePong
=
byte
(
0xfe
)
PacketTypePong
=
byte
(
0xfe
)
)
)
...
...
plugin/consensus/dpos/types/priv_validator.go
View file @
697b6b89
...
@@ -129,22 +129,7 @@ func PubKeyFromString(pubkeystring string) (crypto.PubKey, error) {
...
@@ -129,22 +129,7 @@ func PubKeyFromString(pubkeystring string) (crypto.PubKey, error) {
return
pubkey
,
nil
return
pubkey
,
nil
}
}
// SignatureFromString ...
// GenPrivValidatorImp generates a new validator with randomly generated private key and sets the filePath, but does not call Save().
/*
func SignatureFromString(sigString string) (crypto.Signature, error) {
sigbyte, err := hex.DecodeString(sigString)
if err != nil {
return nil, errors.New(Fmt("PubKeyFromString:DecodeString:%v failed,err:%v", sigString, err))
}
sig, err := ConsensusCrypto.SignatureFromBytes(sigbyte)
if err != nil {
return nil, errors.New(Fmt("PubKeyFromString:SignatureFromBytes:%v failed,err:%v", sigbyte, err))
}
return sig, nil
}
*/
// GenPrivValidatorImp generates a new validator with randomly generated private key
// and sets the filePath, but does not call Save().
func
GenPrivValidatorImp
(
filePath
string
)
*
PrivValidatorImp
{
func
GenPrivValidatorImp
(
filePath
string
)
*
PrivValidatorImp
{
privKey
,
err
:=
ConsensusCrypto
.
GenKey
()
privKey
,
err
:=
ConsensusCrypto
.
GenKey
()
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -326,7 +311,7 @@ func (pv *PrivValidatorImp) SignNotify(chainID string, notify *Notify) error {
...
@@ -326,7 +311,7 @@ func (pv *PrivValidatorImp) SignNotify(chainID string, notify *Notify) error {
return
nil
return
nil
}
}
// Sign
CBInfo signs a canonical representation of the DposCBInfo, Implements PrivValidator
.
// Sign
Msg signs a msg
.
func
(
pv
*
PrivValidatorImp
)
SignMsg
(
msg
[]
byte
)
(
sig
crypto
.
Signature
,
err
error
)
{
func
(
pv
*
PrivValidatorImp
)
SignMsg
(
msg
[]
byte
)
(
sig
crypto
.
Signature
,
err
error
)
{
pv
.
mtx
.
Lock
()
pv
.
mtx
.
Lock
()
defer
pv
.
mtx
.
Unlock
()
defer
pv
.
mtx
.
Unlock
()
...
@@ -335,8 +320,9 @@ func (pv *PrivValidatorImp) SignMsg(msg []byte) (sig crypto.Signature, err error
...
@@ -335,8 +320,9 @@ func (pv *PrivValidatorImp) SignMsg(msg []byte) (sig crypto.Signature, err error
//sig = hex.EncodeToString(signature.Bytes())
//sig = hex.EncodeToString(signature.Bytes())
return
signature
,
nil
return
signature
,
nil
}
}
// SignTx signs a tx, Implements PrivValidator.
// SignTx signs a tx, Implements PrivValidator.
func
(
pv
*
PrivValidatorImp
)
SignTx
(
tx
*
types
.
Transaction
)
{
func
(
pv
*
PrivValidatorImp
)
SignTx
(
tx
*
types
.
Transaction
)
{
tx
.
Sign
(
types
.
SECP256K1
,
pv
.
PrivKey
)
tx
.
Sign
(
types
.
SECP256K1
,
pv
.
PrivKey
)
}
}
...
@@ -351,6 +337,7 @@ func (pv *PrivValidatorImp) VrfEvaluate(input []byte) (hash [32]byte, proof []by
...
@@ -351,6 +337,7 @@ func (pv *PrivValidatorImp) VrfEvaluate(input []byte) (hash [32]byte, proof []by
return
hash
,
proof
return
hash
,
proof
}
}
// VrfProof check the vrf.
func
(
pv
*
PrivValidatorImp
)
VrfProof
(
pubkey
[]
byte
,
input
[]
byte
,
hash
[
32
]
byte
,
proof
[]
byte
)
bool
{
func
(
pv
*
PrivValidatorImp
)
VrfProof
(
pubkey
[]
byte
,
input
[]
byte
,
hash
[
32
]
byte
,
proof
[]
byte
)
bool
{
pv
.
mtx
.
Lock
()
pv
.
mtx
.
Lock
()
defer
pv
.
mtx
.
Unlock
()
defer
pv
.
mtx
.
Unlock
()
...
@@ -364,7 +351,7 @@ func (pv *PrivValidatorImp) VrfProof(pubkey []byte, input []byte, hash [32]byte,
...
@@ -364,7 +351,7 @@ func (pv *PrivValidatorImp) VrfProof(pubkey []byte, input []byte, hash [32]byte,
if
err
!=
nil
{
if
err
!=
nil
{
return
false
return
false
}
}
if
bytes
.
Equal
(
hash
[
:
],
vrfHash
[
:
]){
if
bytes
.
Equal
(
hash
[
:
],
vrfHash
[
:
])
{
return
true
return
true
}
}
...
...
plugin/consensus/dpos/types/priv_validator_test.go
View file @
697b6b89
...
@@ -16,8 +16,9 @@ import (
...
@@ -16,8 +16,9 @@ import (
"testing"
"testing"
"time"
"time"
)
)
const
(
priv_validator_file
=
`{"address":"2FA286246F0222C4FF93210E91AECE0C66723F15","pub_key":{"type":"secp256k1","data":"03EF0E1D3112CF571743A3318125EDE2E52A4EB904BCBAA4B1F75020C2846A7EB4"},"last_height":1679,"last_round":0,"last_step":3,"last_signature":{"type":"secp256k1","data":"37892A916D6E487ADF90F9E88FE37024597677B6C6FED47444AD582F74144B3D6E4B364EAF16AF03A4E42827B6D3C86415D734A5A6CCA92E114B23EB9265AF09"},"last_signbytes":"7B22636861696E5F6964223A22636861696E33332D5A326367466A222C22766F7465223A7B22626C6F636B5F6964223A7B2268617368223A224F6A657975396B2B4149426A6E4859456739584765356A7A462B673D222C227061727473223A7B2268617368223A6E756C6C2C22746F74616C223A307D7D2C22686569676874223A313637392C22726F756E64223A302C2274696D657374616D70223A22323031382D30382D33315430373A35313A34332E3935395A222C2274797065223A327D7D","priv_key":{"type":"secp256k1","data":"5A6A14DA6F5A42835E529D75D87CC8904544F59EEE5387A37D87EEAD194D7EB2"}}`
const
(
privValidatorFile
=
`{"address":"2FA286246F0222C4FF93210E91AECE0C66723F15","pub_key":{"type":"secp256k1","data":"03EF0E1D3112CF571743A3318125EDE2E52A4EB904BCBAA4B1F75020C2846A7EB4"},"last_height":1679,"last_round":0,"last_step":3,"last_signature":{"type":"secp256k1","data":"37892A916D6E487ADF90F9E88FE37024597677B6C6FED47444AD582F74144B3D6E4B364EAF16AF03A4E42827B6D3C86415D734A5A6CCA92E114B23EB9265AF09"},"last_signbytes":"7B22636861696E5F6964223A22636861696E33332D5A326367466A222C22766F7465223A7B22626C6F636B5F6964223A7B2268617368223A224F6A657975396B2B4149426A6E4859456739584765356A7A462B673D222C227061727473223A7B2268617368223A6E756C6C2C22746F74616C223A307D7D2C22686569676874223A313637392C22726F756E64223A302C2274696D657374616D70223A22323031382D30382D33315430373A35313A34332E3935395A222C2274797065223A327D7D","priv_key":{"type":"secp256k1","data":"5A6A14DA6F5A42835E529D75D87CC8904544F59EEE5387A37D87EEAD194D7EB2"}}`
strAddr
=
"2FA286246F0222C4FF93210E91AECE0C66723F15"
strAddr
=
"2FA286246F0222C4FF93210E91AECE0C66723F15"
strPubkey
=
"03EF0E1D3112CF571743A3318125EDE2E52A4EB904BCBAA4B1F75020C2846A7EB4"
strPubkey
=
"03EF0E1D3112CF571743A3318125EDE2E52A4EB904BCBAA4B1F75020C2846A7EB4"
...
@@ -27,18 +28,17 @@ const(
...
@@ -27,18 +28,17 @@ const(
addr4
=
"70A51AD9777EF1F97250F7E4C156D8637BC7143C"
addr4
=
"70A51AD9777EF1F97250F7E4C156D8637BC7143C"
)
)
func
init
(){
func
init
()
{
//为了使用VRF,需要使用SECP256K1体系的公私钥
//为了使用VRF,需要使用SECP256K1体系的公私钥
cr
,
err
:=
crypto
.
New
(
types
.
GetSignName
(
""
,
types
.
SECP256K1
))
cr
,
err
:=
crypto
.
New
(
types
.
GetSignName
(
""
,
types
.
SECP256K1
))
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
"init ConsensusCrypto failed."
)
panic
(
"init ConsensusCrypto failed."
)
return
}
}
ConsensusCrypto
=
cr
ConsensusCrypto
=
cr
}
}
func
save
(
filename
,
filecontent
string
)
{
func
save
(
filename
,
filecontent
string
)
{
f
,
err
:=
os
.
Create
(
filename
)
f
,
err
:=
os
.
Create
(
filename
)
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Println
(
"err = "
,
err
)
fmt
.
Println
(
"err = "
,
err
)
...
@@ -61,7 +61,7 @@ func remove(filename string) {
...
@@ -61,7 +61,7 @@ func remove(filename string) {
}
}
func
read
(
filename
string
)
bool
{
func
read
(
filename
string
)
bool
{
f
,
err
:=
os
.
Open
(
filename
)
f
,
err
:=
os
.
Open
(
filename
)
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Println
(
"err="
,
err
)
fmt
.
Println
(
"err="
,
err
)
...
@@ -69,7 +69,7 @@ func read(filename string) bool{
...
@@ -69,7 +69,7 @@ func read(filename string) bool{
}
}
defer
f
.
Close
()
defer
f
.
Close
()
buf
:=
make
([]
byte
,
1024
*
2
)
buf
:=
make
([]
byte
,
1024
*
2
)
_
,
err1
:=
f
.
Read
(
buf
)
_
,
err1
:=
f
.
Read
(
buf
)
if
err1
!=
nil
&&
err1
!=
io
.
EOF
{
if
err1
!=
nil
&&
err1
!=
io
.
EOF
{
fmt
.
Println
(
"err1="
,
err1
)
fmt
.
Println
(
"err1="
,
err1
)
...
@@ -80,15 +80,14 @@ func read(filename string) bool{
...
@@ -80,15 +80,14 @@ func read(filename string) bool{
return
true
return
true
}
}
func
TestLoadOrGenPrivValidatorFS
(
t
*
testing
.
T
)
{
func
TestLoadOrGenPrivValidatorFS
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
filename
:=
"./tmp_priv_validator.json"
save
(
filename
,
priv
_validator_f
ile
)
save
(
filename
,
priv
ValidatorF
ile
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
require
.
NotNil
(
t
,
privValidator
)
require
.
NotNil
(
t
,
privValidator
)
assert
.
True
(
t
,
strings
.
EqualFold
(
strAddr
,
hex
.
EncodeToString
(
privValidator
.
GetAddress
())))
assert
.
True
(
t
,
strings
.
EqualFold
(
strAddr
,
hex
.
EncodeToString
(
privValidator
.
GetAddress
())))
assert
.
True
(
t
,
strings
.
EqualFold
(
strPubkey
,
hex
.
EncodeToString
(
privValidator
.
GetPubKey
()
.
Bytes
())))
assert
.
True
(
t
,
strings
.
EqualFold
(
strPubkey
,
hex
.
EncodeToString
(
privValidator
.
GetPubKey
()
.
Bytes
())))
fmt
.
Println
(
privValidator
.
String
())
fmt
.
Println
(
privValidator
.
String
())
...
@@ -97,7 +96,7 @@ func TestLoadOrGenPrivValidatorFS(t *testing.T) {
...
@@ -97,7 +96,7 @@ func TestLoadOrGenPrivValidatorFS(t *testing.T) {
func
TestGenPrivValidatorImp
(
t
*
testing
.
T
)
{
func
TestGenPrivValidatorImp
(
t
*
testing
.
T
)
{
filename
:=
"tmp_priv_validator2.json"
filename
:=
"tmp_priv_validator2.json"
//save(filename, priv
_validator_f
ile)
//save(filename, priv
ValidatorF
ile)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
require
.
NotNil
(
t
,
privValidator
)
require
.
NotNil
(
t
,
privValidator
)
...
@@ -117,10 +116,10 @@ func TestGenPrivValidatorImp(t *testing.T) {
...
@@ -117,10 +116,10 @@ func TestGenPrivValidatorImp(t *testing.T) {
func
TestPrivValidatorImpSort
(
t
*
testing
.
T
)
{
func
TestPrivValidatorImpSort
(
t
*
testing
.
T
)
{
var
arr
[]
*
PrivValidatorImp
var
arr
[]
*
PrivValidatorImp
Addr1
,
_
:=
hex
.
DecodeString
(
addr1
)
Addr1
,
_
:=
hex
.
DecodeString
(
addr1
)
Addr2
,
_
:=
hex
.
DecodeString
(
addr2
)
Addr2
,
_
:=
hex
.
DecodeString
(
addr2
)
Addr3
,
_
:=
hex
.
DecodeString
(
addr3
)
Addr3
,
_
:=
hex
.
DecodeString
(
addr3
)
Addr4
,
_
:=
hex
.
DecodeString
(
addr4
)
Addr4
,
_
:=
hex
.
DecodeString
(
addr4
)
imp1
:=
&
PrivValidatorImp
{
imp1
:=
&
PrivValidatorImp
{
Address
:
Addr1
,
Address
:
Addr1
,
...
@@ -132,7 +131,6 @@ func TestPrivValidatorImpSort(t *testing.T) {
...
@@ -132,7 +131,6 @@ func TestPrivValidatorImpSort(t *testing.T) {
}
}
arr
=
append
(
arr
,
imp2
)
arr
=
append
(
arr
,
imp2
)
imp3
:=
&
PrivValidatorImp
{
imp3
:=
&
PrivValidatorImp
{
Address
:
Addr3
,
Address
:
Addr3
,
}
}
...
@@ -145,15 +143,15 @@ func TestPrivValidatorImpSort(t *testing.T) {
...
@@ -145,15 +143,15 @@ func TestPrivValidatorImpSort(t *testing.T) {
sort
.
Sort
(
PrivValidatorsByAddress
(
arr
))
sort
.
Sort
(
PrivValidatorsByAddress
(
arr
))
assert
.
True
(
t
,
strings
.
EqualFold
(
addr2
,
hex
.
EncodeToString
(
arr
[
0
]
.
Address
)))
assert
.
True
(
t
,
strings
.
EqualFold
(
addr2
,
hex
.
EncodeToString
(
arr
[
0
]
.
Address
)))
assert
.
True
(
t
,
strings
.
EqualFold
(
addr4
,
hex
.
EncodeToString
(
arr
[
1
]
.
Address
)))
assert
.
True
(
t
,
strings
.
EqualFold
(
addr4
,
hex
.
EncodeToString
(
arr
[
1
]
.
Address
)))
assert
.
True
(
t
,
strings
.
EqualFold
(
addr1
,
hex
.
EncodeToString
(
arr
[
2
]
.
Address
)))
assert
.
True
(
t
,
strings
.
EqualFold
(
addr1
,
hex
.
EncodeToString
(
arr
[
2
]
.
Address
)))
assert
.
True
(
t
,
strings
.
EqualFold
(
addr3
,
hex
.
EncodeToString
(
arr
[
3
]
.
Address
)))
assert
.
True
(
t
,
strings
.
EqualFold
(
addr3
,
hex
.
EncodeToString
(
arr
[
3
]
.
Address
)))
}
}
func
TestSignAndVerifyVote
(
t
*
testing
.
T
)
{
func
TestSignAndVerifyVote
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
filename
:=
"./tmp_priv_validator.json"
save
(
filename
,
priv
_validator_f
ile
)
save
(
filename
,
priv
ValidatorF
ile
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
now
:=
time
.
Now
()
.
Unix
()
now
:=
time
.
Now
()
.
Unix
()
...
@@ -207,7 +205,7 @@ func TestSignAndVerifyVote(t *testing.T) {
...
@@ -207,7 +205,7 @@ func TestSignAndVerifyVote(t *testing.T) {
func
TestSignAndVerifyNotify
(
t
*
testing
.
T
)
{
func
TestSignAndVerifyNotify
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
filename
:=
"./tmp_priv_validator.json"
save
(
filename
,
priv
_validator_f
ile
)
save
(
filename
,
priv
ValidatorF
ile
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
now
:=
time
.
Now
()
.
Unix
()
now
:=
time
.
Now
()
.
Unix
()
...
@@ -230,7 +228,6 @@ func TestSignAndVerifyNotify(t *testing.T) {
...
@@ -230,7 +228,6 @@ func TestSignAndVerifyNotify(t *testing.T) {
voteItem
.
VoteID
=
crypto
.
Ripemd160
(
encode
)
voteItem
.
VoteID
=
crypto
.
Ripemd160
(
encode
)
chainID
:=
"test-chain-Ep9EcD"
chainID
:=
"test-chain-Ep9EcD"
notify
:=
&
Notify
{
notify
:=
&
Notify
{
...
@@ -263,7 +260,7 @@ func TestSignAndVerifyNotify(t *testing.T) {
...
@@ -263,7 +260,7 @@ func TestSignAndVerifyNotify(t *testing.T) {
func
TestSignMsg
(
t
*
testing
.
T
)
{
func
TestSignMsg
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
filename
:=
"./tmp_priv_validator.json"
save
(
filename
,
priv
_validator_f
ile
)
save
(
filename
,
priv
ValidatorF
ile
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
byteCB
:=
[]
byte
(
"asdfadsasf"
)
byteCB
:=
[]
byte
(
"asdfadsasf"
)
...
@@ -277,16 +274,14 @@ func TestSignMsg(t *testing.T) {
...
@@ -277,16 +274,14 @@ func TestSignMsg(t *testing.T) {
func
TestVrf
(
t
*
testing
.
T
)
{
func
TestVrf
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
filename
:=
"./tmp_priv_validator.json"
save
(
filename
,
priv
_validator_f
ile
)
save
(
filename
,
priv
ValidatorF
ile
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
input
:=
[]
byte
(
"abcdefghijklmn"
)
input
:=
[]
byte
(
"abcdefghijklmn"
)
hash
,
proof
:=
privValidator
.
VrfEvaluate
(
input
)
hash
,
proof
:=
privValidator
.
VrfEvaluate
(
input
)
assert
.
True
(
t
,
32
==
len
(
hash
))
assert
.
True
(
t
,
32
==
len
(
hash
))
assert
.
True
(
t
,
0
<
len
(
proof
))
assert
.
True
(
t
,
0
<
len
(
proof
))
result
:=
privValidator
.
VrfProof
(
privValidator
.
PubKey
.
Bytes
(),
input
,
hash
,
proof
)
result
:=
privValidator
.
VrfProof
(
privValidator
.
PubKey
.
Bytes
(),
input
,
hash
,
proof
)
assert
.
True
(
t
,
result
)
assert
.
True
(
t
,
result
)
...
@@ -295,10 +290,9 @@ func TestVrf(t *testing.T) {
...
@@ -295,10 +290,9 @@ func TestVrf(t *testing.T) {
func
TestSignTx
(
t
*
testing
.
T
)
{
func
TestSignTx
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
filename
:=
"./tmp_priv_validator.json"
save
(
filename
,
priv
_validator_f
ile
)
save
(
filename
,
priv
ValidatorF
ile
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
tx
:=
&
types
.
Transaction
{}
tx
:=
&
types
.
Transaction
{}
privValidator
.
SignTx
(
tx
)
privValidator
.
SignTx
(
tx
)
assert
.
True
(
t
,
types
.
SECP256K1
==
tx
.
Signature
.
Ty
)
assert
.
True
(
t
,
types
.
SECP256K1
==
tx
.
Signature
.
Ty
)
...
...
plugin/consensus/dpos/types/signable.go
View file @
697b6b89
plugin/consensus/dpos/types/signable_test.go
View file @
697b6b89
...
@@ -11,12 +11,11 @@ import (
...
@@ -11,12 +11,11 @@ import (
"time"
"time"
)
)
func
init
(){
func
init
()
{
//为了使用VRF,需要使用SECP256K1体系的公私钥
//为了使用VRF,需要使用SECP256K1体系的公私钥
cr
,
err
:=
crypto
.
New
(
types
.
GetSignName
(
""
,
types
.
SECP256K1
))
cr
,
err
:=
crypto
.
New
(
types
.
GetSignName
(
""
,
types
.
SECP256K1
))
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
"init ConsensusCrypto failed."
)
panic
(
"init ConsensusCrypto failed."
)
return
}
}
ConsensusCrypto
=
cr
ConsensusCrypto
=
cr
...
@@ -24,7 +23,7 @@ func init(){
...
@@ -24,7 +23,7 @@ func init(){
func
TestVote
(
t
*
testing
.
T
)
{
func
TestVote
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
filename
:=
"./tmp_priv_validator.json"
save
(
filename
,
priv
_validator_f
ile
)
save
(
filename
,
priv
ValidatorF
ile
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
now
:=
time
.
Now
()
.
Unix
()
now
:=
time
.
Now
()
.
Unix
()
...
@@ -69,7 +68,7 @@ func TestVote(t *testing.T) {
...
@@ -69,7 +68,7 @@ func TestVote(t *testing.T) {
func
TestNotify
(
t
*
testing
.
T
)
{
func
TestNotify
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
filename
:=
"./tmp_priv_validator.json"
save
(
filename
,
priv
_validator_f
ile
)
save
(
filename
,
priv
ValidatorF
ile
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
now
:=
time
.
Now
()
.
Unix
()
now
:=
time
.
Now
()
.
Unix
()
...
@@ -92,7 +91,6 @@ func TestNotify(t *testing.T) {
...
@@ -92,7 +91,6 @@ func TestNotify(t *testing.T) {
voteItem
.
VoteID
=
crypto
.
Ripemd160
(
encode
)
voteItem
.
VoteID
=
crypto
.
Ripemd160
(
encode
)
chainID
:=
"test-chain-Ep9EcD"
chainID
:=
"test-chain-Ep9EcD"
notify
:=
&
Notify
{
notify
:=
&
Notify
{
...
...
plugin/consensus/dpos/types/util_test.go
View file @
697b6b89
...
@@ -12,13 +12,13 @@ import (
...
@@ -12,13 +12,13 @@ import (
"time"
"time"
)
)
func
init
(){
func
init
()
{
Init
()
Init
()
}
}
func
TestWriteFile
(
t
*
testing
.
T
)
{
func
TestWriteFile
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
filename
:=
"./tmp_priv_validator.json"
err
:=
WriteFile
(
filename
,
[]
byte
(
priv
_validator_f
ile
),
0664
)
err
:=
WriteFile
(
filename
,
[]
byte
(
priv
ValidatorF
ile
),
0664
)
require
.
Nil
(
t
,
err
)
require
.
Nil
(
t
,
err
)
file
,
err
:=
os
.
Stat
(
filename
)
file
,
err
:=
os
.
Stat
(
filename
)
...
@@ -35,7 +35,7 @@ func TestWriteFile(t *testing.T) {
...
@@ -35,7 +35,7 @@ func TestWriteFile(t *testing.T) {
func
TestWriteFileAtomic
(
t
*
testing
.
T
)
{
func
TestWriteFileAtomic
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
filename
:=
"./tmp_priv_validator.json"
err
:=
WriteFileAtomic
(
filename
,
[]
byte
(
priv
_validator_f
ile
),
0664
)
err
:=
WriteFileAtomic
(
filename
,
[]
byte
(
priv
ValidatorF
ile
),
0664
)
require
.
Nil
(
t
,
err
)
require
.
Nil
(
t
,
err
)
file
,
err
:=
os
.
Stat
(
filename
)
file
,
err
:=
os
.
Stat
(
filename
)
...
@@ -56,7 +56,7 @@ func TestTempfile(t *testing.T) {
...
@@ -56,7 +56,7 @@ func TestTempfile(t *testing.T) {
fmt
.
Println
(
name
)
fmt
.
Println
(
name
)
require
.
NotNil
(
t
,
file
)
require
.
NotNil
(
t
,
file
)
_
,
err
:=
file
.
Write
([]
byte
(
priv
_validator_f
ile
))
_
,
err
:=
file
.
Write
([]
byte
(
priv
ValidatorF
ile
))
if
err
==
nil
{
if
err
==
nil
{
err
=
file
.
Sync
()
err
=
file
.
Sync
()
}
}
...
@@ -85,19 +85,19 @@ func TestKill(t *testing.T) {
...
@@ -85,19 +85,19 @@ func TestKill(t *testing.T) {
c
:=
make
(
chan
os
.
Signal
)
c
:=
make
(
chan
os
.
Signal
)
signal
.
Notify
(
c
)
signal
.
Notify
(
c
)
go
Kill
()
go
Kill
()
s
:=
<-
c
s
:=
<-
c
assert
.
True
(
t
,
s
.
String
()
==
"terminated"
)
assert
.
True
(
t
,
s
.
String
()
==
"terminated"
)
}
}
var
(
var
(
go_index
=
0
goIndex
=
0
go_sum
=
0
goSum
=
0
)
)
func
test
()
{
func
test
()
{
go
_i
ndex
++
go
I
ndex
++
time
.
Sleep
(
time
.
Second
*
time
.
Duration
(
go
_i
ndex
))
time
.
Sleep
(
time
.
Second
*
time
.
Duration
(
go
I
ndex
))
go
_s
um
++
go
S
um
++
}
}
func
TestParallel
(
t
*
testing
.
T
)
{
func
TestParallel
(
t
*
testing
.
T
)
{
...
@@ -107,9 +107,9 @@ func TestParallel(t *testing.T) {
...
@@ -107,9 +107,9 @@ func TestParallel(t *testing.T) {
f2
:=
test
f2
:=
test
f2
()
f2
()
go
_s
um
=
0
go
S
um
=
0
Parallel
(
f1
,
f2
)
Parallel
(
f1
,
f2
)
assert
.
True
(
t
,
go
_s
um
==
2
)
assert
.
True
(
t
,
go
S
um
==
2
)
}
}
func
TestRandInt63n
(
t
*
testing
.
T
)
{
func
TestRandInt63n
(
t
*
testing
.
T
)
{
...
@@ -138,8 +138,8 @@ func TestRandUint32(t *testing.T) {
...
@@ -138,8 +138,8 @@ func TestRandUint32(t *testing.T) {
}
}
func
TestPanicSanity
(
t
*
testing
.
T
)
{
func
TestPanicSanity
(
t
*
testing
.
T
)
{
defer
func
(){
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
if
r
:=
recover
();
r
!=
nil
{
//fmt.Println(r)
//fmt.Println(r)
assert
.
True
(
t
,
strings
.
HasPrefix
(
r
.
(
string
),
"Panicked on a Sanity Check: "
))
assert
.
True
(
t
,
strings
.
HasPrefix
(
r
.
(
string
),
"Panicked on a Sanity Check: "
))
}
}
...
@@ -149,8 +149,8 @@ func TestPanicSanity(t *testing.T) {
...
@@ -149,8 +149,8 @@ func TestPanicSanity(t *testing.T) {
}
}
func
TestPanicCrisis
(
t
*
testing
.
T
)
{
func
TestPanicCrisis
(
t
*
testing
.
T
)
{
defer
func
(){
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
if
r
:=
recover
();
r
!=
nil
{
//fmt.Println(r)
//fmt.Println(r)
assert
.
True
(
t
,
strings
.
HasPrefix
(
r
.
(
string
),
"Panicked on a Crisis: "
))
assert
.
True
(
t
,
strings
.
HasPrefix
(
r
.
(
string
),
"Panicked on a Crisis: "
))
}
}
...
@@ -160,8 +160,8 @@ func TestPanicCrisis(t *testing.T) {
...
@@ -160,8 +160,8 @@ func TestPanicCrisis(t *testing.T) {
}
}
func
TestPanicQ
(
t
*
testing
.
T
)
{
func
TestPanicQ
(
t
*
testing
.
T
)
{
defer
func
(){
defer
func
()
{
if
r
:=
recover
();
r
!=
nil
{
if
r
:=
recover
();
r
!=
nil
{
//fmt.Println(r)
//fmt.Println(r)
assert
.
True
(
t
,
strings
.
HasPrefix
(
r
.
(
string
),
"Panicked questionably: "
))
assert
.
True
(
t
,
strings
.
HasPrefix
(
r
.
(
string
),
"Panicked questionably: "
))
}
}
...
...
plugin/consensus/dpos/types/validator_set_test.go
View file @
697b6b89
...
@@ -3,13 +3,13 @@ package types
...
@@ -3,13 +3,13 @@ package types
import
(
import
(
"bytes"
"bytes"
"encoding/hex"
"encoding/hex"
"fmt"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/require"
"strings"
"strings"
"testing"
"testing"
"fmt"
)
)
const
(
const
(
...
@@ -20,6 +20,7 @@ const (
...
@@ -20,6 +20,7 @@ const (
pubkey11
=
"03541AB9887951C038273648545072E5B6A46A639BFF535F3957E8150CBE2A70D7"
pubkey11
=
"03541AB9887951C038273648545072E5B6A46A639BFF535F3957E8150CBE2A70D7"
pubkey12
=
"03F2A7AFFA090763C42B370C6F33CC3E9B6140228ABAF0591240F3B88E8792F890"
pubkey12
=
"03F2A7AFFA090763C42B370C6F33CC3E9B6140228ABAF0591240F3B88E8792F890"
)
)
var
(
var
(
val1
*
Validator
val1
*
Validator
val2
*
Validator
val2
*
Validator
...
@@ -29,36 +30,35 @@ var (
...
@@ -29,36 +30,35 @@ var (
val12
*
Validator
val12
*
Validator
)
)
func
init
(){
func
init
()
{
//为了使用VRF,需要使用SECP256K1体系的公私钥
//为了使用VRF,需要使用SECP256K1体系的公私钥
cr
,
err
:=
crypto
.
New
(
types
.
GetSignName
(
""
,
types
.
SECP256K1
))
cr
,
err
:=
crypto
.
New
(
types
.
GetSignName
(
""
,
types
.
SECP256K1
))
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
"init ConsensusCrypto failed."
)
panic
(
"init ConsensusCrypto failed."
)
return
}
}
ConsensusCrypto
=
cr
ConsensusCrypto
=
cr
pkbytes
,
err
:=
hex
.
DecodeString
(
pubkey1
)
pkbytes
,
_
:=
hex
.
DecodeString
(
pubkey1
)
pk1
,
err
:=
ConsensusCrypto
.
PubKeyFromBytes
(
pkbytes
)
pk1
,
_
:=
ConsensusCrypto
.
PubKeyFromBytes
(
pkbytes
)
pkbytes
,
err
=
hex
.
DecodeString
(
pubkey2
)
pkbytes
,
_
=
hex
.
DecodeString
(
pubkey2
)
pk2
,
err
:=
ConsensusCrypto
.
PubKeyFromBytes
(
pkbytes
)
pk2
,
_
:=
ConsensusCrypto
.
PubKeyFromBytes
(
pkbytes
)
pkbytes
,
err
=
hex
.
DecodeString
(
pubkey3
)
pkbytes
,
_
=
hex
.
DecodeString
(
pubkey3
)
pk3
,
err
:=
ConsensusCrypto
.
PubKeyFromBytes
(
pkbytes
)
pk3
,
_
:=
ConsensusCrypto
.
PubKeyFromBytes
(
pkbytes
)
val1
=
NewValidator
(
pk1
)
val1
=
NewValidator
(
pk1
)
val2
=
NewValidator
(
pk2
)
val2
=
NewValidator
(
pk2
)
val3
=
NewValidator
(
pk3
)
val3
=
NewValidator
(
pk3
)
pkbytes
,
err
=
hex
.
DecodeString
(
pubkey11
)
pkbytes
,
_
=
hex
.
DecodeString
(
pubkey11
)
pk11
,
err
:=
ConsensusCrypto
.
PubKeyFromBytes
(
pkbytes
)
pk11
,
_
:=
ConsensusCrypto
.
PubKeyFromBytes
(
pkbytes
)
val11
=
NewValidator
(
pk11
)
val11
=
NewValidator
(
pk11
)
pkbytes
,
err
=
hex
.
DecodeString
(
pubkey12
)
pkbytes
,
_
=
hex
.
DecodeString
(
pubkey12
)
pk12
,
err
:=
ConsensusCrypto
.
PubKeyFromBytes
(
pkbytes
)
pk12
,
_
:=
ConsensusCrypto
.
PubKeyFromBytes
(
pkbytes
)
val12
=
NewValidator
(
pk12
)
val12
=
NewValidator
(
pk12
)
}
}
...
@@ -72,7 +72,7 @@ func TestValidator(t *testing.T) {
...
@@ -72,7 +72,7 @@ func TestValidator(t *testing.T) {
}
}
func
match
(
index
int
,
val
*
Validator
)
bool
{
func
match
(
index
int
,
val
*
Validator
)
bool
{
if
bytes
.
Equal
(
val
.
Address
,
val1
.
Address
){
if
bytes
.
Equal
(
val
.
Address
,
val1
.
Address
)
{
return
true
return
true
}
}
...
@@ -157,7 +157,7 @@ func TestValidatorsByAddress(t *testing.T) {
...
@@ -157,7 +157,7 @@ func TestValidatorsByAddress(t *testing.T) {
assert
.
True
(
t
,
arr
.
Less
(
0
,
1
)
==
false
)
assert
.
True
(
t
,
arr
.
Less
(
0
,
1
)
==
false
)
assert
.
True
(
t
,
arr
.
Less
(
0
,
2
)
==
true
)
assert
.
True
(
t
,
arr
.
Less
(
0
,
2
)
==
true
)
arr
.
Swap
(
0
,
1
)
arr
.
Swap
(
0
,
1
)
assert
.
True
(
t
,
bytes
.
Equal
(
arr
[
0
]
.
PubKey
,
val2
.
PubKey
))
assert
.
True
(
t
,
bytes
.
Equal
(
arr
[
0
]
.
PubKey
,
val2
.
PubKey
))
}
}
...
...
plugin/consensus/dpos/validator_manager.go
View file @
697b6b89
...
@@ -20,8 +20,14 @@ var (
...
@@ -20,8 +20,14 @@ var (
)
)
const
(
const
(
//ShuffleTypeNoVrf shuffle type: NoVrf, use default address order
ShuffleTypeNoVrf
=
iota
ShuffleTypeNoVrf
=
iota
//ShuffleTypeVrf shuffle type: Vrf
ShuffleTypeVrf
ShuffleTypeVrf
//ShuffleTypePartVrf shuffle type: PartVrf
ShuffleTypePartVrf
ShuffleTypePartVrf
)
)
...
@@ -138,6 +144,7 @@ func MakeGenesisValidatorMgr(genDoc *ttypes.GenesisDoc) (ValidatorMgr, error) {
...
@@ -138,6 +144,7 @@ func MakeGenesisValidatorMgr(genDoc *ttypes.GenesisDoc) (ValidatorMgr, error) {
},
nil
},
nil
}
}
// GetValidatorByIndex method
func
(
s
*
ValidatorMgr
)
GetValidatorByIndex
(
index
int
)
(
addres
[]
byte
,
val
*
ttypes
.
Validator
)
{
func
(
s
*
ValidatorMgr
)
GetValidatorByIndex
(
index
int
)
(
addres
[]
byte
,
val
*
ttypes
.
Validator
)
{
if
index
<
0
||
index
>=
len
(
s
.
Validators
.
Validators
)
{
if
index
<
0
||
index
>=
len
(
s
.
Validators
.
Validators
)
{
return
nil
,
nil
return
nil
,
nil
...
@@ -153,15 +160,16 @@ func (s *ValidatorMgr) GetValidatorByIndex(index int) (addres []byte, val *ttype
...
@@ -153,15 +160,16 @@ func (s *ValidatorMgr) GetValidatorByIndex(index int) (addres []byte, val *ttype
if
index
<
len
(
s
.
VrfValidators
.
Validators
)
{
if
index
<
len
(
s
.
VrfValidators
.
Validators
)
{
val
=
s
.
VrfValidators
.
Validators
[
index
]
val
=
s
.
VrfValidators
.
Validators
[
index
]
return
address
.
PubKeyToAddress
(
val
.
PubKey
)
.
Hash160
[
:
],
val
.
Copy
()
return
address
.
PubKeyToAddress
(
val
.
PubKey
)
.
Hash160
[
:
],
val
.
Copy
()
}
else
{
val
=
s
.
NoVrfValidators
.
Validators
[
index
-
len
(
s
.
VrfValidators
.
Validators
)]
return
address
.
PubKeyToAddress
(
val
.
PubKey
)
.
Hash160
[
:
],
val
.
Copy
()
}
}
val
=
s
.
NoVrfValidators
.
Validators
[
index
-
len
(
s
.
VrfValidators
.
Validators
)]
return
address
.
PubKeyToAddress
(
val
.
PubKey
)
.
Hash160
[
:
],
val
.
Copy
()
}
}
return
nil
,
nil
return
nil
,
nil
}
}
// GetIndexByPubKey method
func
(
s
*
ValidatorMgr
)
GetIndexByPubKey
(
pubkey
[]
byte
)
(
index
int
)
{
func
(
s
*
ValidatorMgr
)
GetIndexByPubKey
(
pubkey
[]
byte
)
(
index
int
)
{
if
nil
==
pubkey
{
if
nil
==
pubkey
{
return
-
1
return
-
1
...
@@ -202,6 +210,7 @@ func (s *ValidatorMgr) GetIndexByPubKey(pubkey []byte) (index int) {
...
@@ -202,6 +210,7 @@ func (s *ValidatorMgr) GetIndexByPubKey(pubkey []byte) (index int) {
return
index
return
index
}
}
// FillVoteItem method
func
(
s
*
ValidatorMgr
)
FillVoteItem
(
voteItem
*
ttypes
.
VoteItem
)
{
func
(
s
*
ValidatorMgr
)
FillVoteItem
(
voteItem
*
ttypes
.
VoteItem
)
{
if
s
.
LastCycleBoundaryInfo
!=
nil
{
if
s
.
LastCycleBoundaryInfo
!=
nil
{
voteItem
.
LastCBInfo
=
&
ttypes
.
CycleBoundaryInfo
{
voteItem
.
LastCBInfo
=
&
ttypes
.
CycleBoundaryInfo
{
...
@@ -237,9 +246,10 @@ func (s *ValidatorMgr) FillVoteItem(voteItem *ttypes.VoteItem) {
...
@@ -237,9 +246,10 @@ func (s *ValidatorMgr) FillVoteItem(voteItem *ttypes.VoteItem) {
}
}
}
}
// UpdateFromVoteItem method
func
(
s
*
ValidatorMgr
)
UpdateFromVoteItem
(
voteItem
*
ttypes
.
VoteItem
)
bool
{
func
(
s
*
ValidatorMgr
)
UpdateFromVoteItem
(
voteItem
*
ttypes
.
VoteItem
)
bool
{
validators
:=
voteItem
.
Validators
validators
:=
voteItem
.
Validators
if
len
(
s
.
Validators
.
Validators
)
!=
len
(
voteItem
.
Validators
){
if
len
(
s
.
Validators
.
Validators
)
!=
len
(
voteItem
.
Validators
)
{
return
false
return
false
}
}
...
...
plugin/consensus/dpos/validator_manager_test.go
View file @
697b6b89
...
@@ -6,40 +6,39 @@ import (
...
@@ -6,40 +6,39 @@ import (
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/types"
ttypes
"github.com/33cn/plugin/plugin/consensus/dpos/types"
ttypes
"github.com/33cn/plugin/plugin/consensus/dpos/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"
dty
"github.com/33cn/plugin/plugin/dapp/dposvote/types"
dty
"github.com/33cn/plugin/plugin/dapp/dposvote/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"fmt"
"os"
"os"
"testing"
"testing"
"fmt"
)
)
const
(
const
(
genesis_c
ontent
=
`{"genesis_time":"2018-08-16T15:38:56.951569432+08:00","chain_id":"chain33-Z2cgFj","validators":[{"pub_key":{"type":"secp256k1","data":"03EF0E1D3112CF571743A3318125EDE2E52A4EB904BCBAA4B1F75020C2846A7EB4"},"name":""},{"pub_key":{"type":"secp256k1","data":"027848E7FA630B759DB406940B5506B666A344B1060794BBF314EB459D40881BB3"},"name":""},{"pub_key":{"type":"secp256k1","data":"03F4AB6659E61E8512C9A24AC385CC1AC4D52B87D10ADBDF060086EA82BE62CDDE"},"name":""}],"app_hash":null}`
genesisC
ontent
=
`{"genesis_time":"2018-08-16T15:38:56.951569432+08:00","chain_id":"chain33-Z2cgFj","validators":[{"pub_key":{"type":"secp256k1","data":"03EF0E1D3112CF571743A3318125EDE2E52A4EB904BCBAA4B1F75020C2846A7EB4"},"name":""},{"pub_key":{"type":"secp256k1","data":"027848E7FA630B759DB406940B5506B666A344B1060794BBF314EB459D40881BB3"},"name":""},{"pub_key":{"type":"secp256k1","data":"03F4AB6659E61E8512C9A24AC385CC1AC4D52B87D10ADBDF060086EA82BE62CDDE"},"name":""}],"app_hash":null}`
pubkey11
=
"03541AB9887951C038273648545072E5B6A46A639BFF535F3957E8150CBE2A70D7"
pubkey11
=
"03541AB9887951C038273648545072E5B6A46A639BFF535F3957E8150CBE2A70D7"
)
)
var
(
var
(
genDoc
*
ttypes
.
GenesisDoc
genDoc
*
ttypes
.
GenesisDoc
)
)
func
init
(){
func
init
()
{
//为了使用VRF,需要使用SECP256K1体系的公私钥
//为了使用VRF,需要使用SECP256K1体系的公私钥
cr
,
err
:=
crypto
.
New
(
types
.
GetSignName
(
""
,
types
.
SECP256K1
))
cr
,
err
:=
crypto
.
New
(
types
.
GetSignName
(
""
,
types
.
SECP256K1
))
if
err
!=
nil
{
if
err
!=
nil
{
panic
(
"init ConsensusCrypto failed."
)
panic
(
"init ConsensusCrypto failed."
)
return
}
}
ttypes
.
ConsensusCrypto
=
cr
ttypes
.
ConsensusCrypto
=
cr
remove
(
"./genesis.json"
)
remove
(
"./genesis.json"
)
save
(
"./genesis.json"
,
genesis
_c
ontent
)
save
(
"./genesis.json"
,
genesis
C
ontent
)
genDoc
,
_
=
ttypes
.
GenesisDocFromFile
(
"./genesis.json"
)
genDoc
,
_
=
ttypes
.
GenesisDocFromFile
(
"./genesis.json"
)
}
}
func
save
(
filename
,
filecontent
string
)
{
func
save
(
filename
,
filecontent
string
)
{
f
,
err
:=
os
.
Create
(
filename
)
f
,
err
:=
os
.
Create
(
filename
)
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Println
(
"err = "
,
err
)
fmt
.
Println
(
"err = "
,
err
)
...
@@ -115,7 +114,6 @@ func TestGetValidatorByIndex(t *testing.T) {
...
@@ -115,7 +114,6 @@ func TestGetValidatorByIndex(t *testing.T) {
val
,
flag
:=
vMgr
.
VrfValidators
.
Remove
(
addr
)
val
,
flag
:=
vMgr
.
VrfValidators
.
Remove
(
addr
)
assert
.
True
(
t
,
flag
)
assert
.
True
(
t
,
flag
)
vMgr
.
NoVrfValidators
=
&
ttypes
.
ValidatorSet
{}
vMgr
.
NoVrfValidators
=
&
ttypes
.
ValidatorSet
{}
vMgr
.
NoVrfValidators
.
Validators
=
append
(
vMgr
.
NoVrfValidators
.
Validators
,
val
)
vMgr
.
NoVrfValidators
.
Validators
=
append
(
vMgr
.
NoVrfValidators
.
Validators
,
val
)
addr
,
val
=
vMgr
.
GetValidatorByIndex
(
2
)
addr
,
val
=
vMgr
.
GetValidatorByIndex
(
2
)
...
@@ -123,7 +121,7 @@ func TestGetValidatorByIndex(t *testing.T) {
...
@@ -123,7 +121,7 @@ func TestGetValidatorByIndex(t *testing.T) {
assert
.
True
(
t
,
bytes
.
Equal
(
val
.
PubKey
,
vMgr
.
NoVrfValidators
.
Validators
[
0
]
.
PubKey
))
assert
.
True
(
t
,
bytes
.
Equal
(
val
.
PubKey
,
vMgr
.
NoVrfValidators
.
Validators
[
0
]
.
PubKey
))
}
}
func
TestGetIndexByPubKey
(
t
*
testing
.
T
){
func
TestGetIndexByPubKey
(
t
*
testing
.
T
)
{
vMgr
,
err
:=
MakeGenesisValidatorMgr
(
genDoc
)
vMgr
,
err
:=
MakeGenesisValidatorMgr
(
genDoc
)
require
.
Nil
(
t
,
err
)
require
.
Nil
(
t
,
err
)
assert
.
True
(
t
,
vMgr
.
ChainID
==
"chain33-Z2cgFj"
)
assert
.
True
(
t
,
vMgr
.
ChainID
==
"chain33-Z2cgFj"
)
...
@@ -199,9 +197,9 @@ func TestFillVoteItem(t *testing.T) {
...
@@ -199,9 +197,9 @@ func TestFillVoteItem(t *testing.T) {
vMgr
.
ShuffleType
=
ShuffleTypeVrf
vMgr
.
ShuffleType
=
ShuffleTypeVrf
vMgr
.
LastCycleBoundaryInfo
=
&
dty
.
DposCBInfo
{
vMgr
.
LastCycleBoundaryInfo
=
&
dty
.
DposCBInfo
{
Cycle
:
110
,
Cycle
:
110
,
StopHeight
:
1111
,
StopHeight
:
1111
,
StopHash
:
"abcdefg"
,
StopHash
:
"abcdefg"
,
Pubkey
:
"xxxxxxxx"
,
Pubkey
:
"xxxxxxxx"
,
}
}
voteItem
=
&
ttypes
.
VoteItem
{}
voteItem
=
&
ttypes
.
VoteItem
{}
...
@@ -215,7 +213,6 @@ func TestFillVoteItem(t *testing.T) {
...
@@ -215,7 +213,6 @@ func TestFillVoteItem(t *testing.T) {
assert
.
True
(
t
,
len
(
voteItem
.
VrfValidators
)
==
3
)
assert
.
True
(
t
,
len
(
voteItem
.
VrfValidators
)
==
3
)
assert
.
True
(
t
,
voteItem
.
NoVrfValidators
==
nil
)
assert
.
True
(
t
,
voteItem
.
NoVrfValidators
==
nil
)
vMgr
.
ShuffleType
=
ShuffleTypePartVrf
vMgr
.
ShuffleType
=
ShuffleTypePartVrf
val
,
flag
:=
vMgr
.
VrfValidators
.
Remove
(
vMgr
.
Validators
.
Validators
[
2
]
.
Address
)
val
,
flag
:=
vMgr
.
VrfValidators
.
Remove
(
vMgr
.
Validators
.
Validators
[
2
]
.
Address
)
assert
.
True
(
t
,
flag
==
true
)
assert
.
True
(
t
,
flag
==
true
)
...
@@ -267,20 +264,19 @@ func TestUpdateFromVoteItem(t *testing.T) {
...
@@ -267,20 +264,19 @@ func TestUpdateFromVoteItem(t *testing.T) {
assert
.
True
(
t
,
flag
==
false
)
assert
.
True
(
t
,
flag
==
false
)
/////
/////
pkbytes
,
err
:=
hex
.
DecodeString
(
pubkey11
)
pkbytes
,
_
:=
hex
.
DecodeString
(
pubkey11
)
pk11
,
err
:=
ttypes
.
ConsensusCrypto
.
PubKeyFromBytes
(
pkbytes
)
pk11
,
_
:=
ttypes
.
ConsensusCrypto
.
PubKeyFromBytes
(
pkbytes
)
val
.
PubKey
=
pk11
.
Bytes
()
val
.
PubKey
=
pk11
.
Bytes
()
newMgr
.
Validators
.
Add
(
val
)
newMgr
.
Validators
.
Add
(
val
)
flag
=
newMgr
.
UpdateFromVoteItem
(
voteItem
)
flag
=
newMgr
.
UpdateFromVoteItem
(
voteItem
)
assert
.
True
(
t
,
flag
==
false
)
assert
.
True
(
t
,
flag
==
false
)
/////
/////
vMgr
.
LastCycleBoundaryInfo
=
&
dty
.
DposCBInfo
{
vMgr
.
LastCycleBoundaryInfo
=
&
dty
.
DposCBInfo
{
Cycle
:
110
,
Cycle
:
110
,
StopHeight
:
1111
,
StopHeight
:
1111
,
StopHash
:
"abcdefg"
,
StopHash
:
"abcdefg"
,
Pubkey
:
"xxxxxxxx"
,
Pubkey
:
"xxxxxxxx"
,
}
}
voteItem
=
&
ttypes
.
VoteItem
{}
voteItem
=
&
ttypes
.
VoteItem
{}
vMgr
.
FillVoteItem
(
voteItem
)
vMgr
.
FillVoteItem
(
voteItem
)
...
@@ -302,7 +298,6 @@ func TestUpdateFromVoteItem(t *testing.T) {
...
@@ -302,7 +298,6 @@ func TestUpdateFromVoteItem(t *testing.T) {
assert
.
True
(
t
,
newMgr
.
LastCycleBoundaryInfo
.
StopHeight
==
voteItem
.
LastCBInfo
.
StopHeight
)
assert
.
True
(
t
,
newMgr
.
LastCycleBoundaryInfo
.
StopHeight
==
voteItem
.
LastCBInfo
.
StopHeight
)
assert
.
True
(
t
,
newMgr
.
LastCycleBoundaryInfo
.
StopHash
==
voteItem
.
LastCBInfo
.
StopHash
)
assert
.
True
(
t
,
newMgr
.
LastCycleBoundaryInfo
.
StopHash
==
voteItem
.
LastCBInfo
.
StopHash
)
/////
/////
vMgr
.
VrfValidators
=
ttypes
.
NewValidatorSet
(
vMgr
.
Validators
.
Validators
)
vMgr
.
VrfValidators
=
ttypes
.
NewValidatorSet
(
vMgr
.
Validators
.
Validators
)
vMgr
.
ShuffleType
=
ShuffleTypeVrf
vMgr
.
ShuffleType
=
ShuffleTypeVrf
...
@@ -330,7 +325,6 @@ func TestUpdateFromVoteItem(t *testing.T) {
...
@@ -330,7 +325,6 @@ func TestUpdateFromVoteItem(t *testing.T) {
assert
.
True
(
t
,
len
(
newMgr
.
Validators
.
Validators
)
==
3
)
assert
.
True
(
t
,
len
(
newMgr
.
Validators
.
Validators
)
==
3
)
assert
.
True
(
t
,
len
(
newMgr
.
VrfValidators
.
Validators
)
==
3
)
assert
.
True
(
t
,
len
(
newMgr
.
VrfValidators
.
Validators
)
==
3
)
///
///
vMgr
.
ShuffleType
=
ShuffleTypePartVrf
vMgr
.
ShuffleType
=
ShuffleTypePartVrf
val
,
flag
=
vMgr
.
VrfValidators
.
Remove
(
vMgr
.
Validators
.
Validators
[
2
]
.
Address
)
val
,
flag
=
vMgr
.
VrfValidators
.
Remove
(
vMgr
.
Validators
.
Validators
[
2
]
.
Address
)
...
...
plugin/dapp/dposvote/cmd/build/test-rpc.sh
View file @
697b6b89
This diff is collapsed.
Click to expand it.
plugin/dapp/dposvote/cmd/tool.sh
View file @
697b6b89
...
@@ -10,9 +10,9 @@ function trans() {
...
@@ -10,9 +10,9 @@ function trans() {
dst
=
$2
dst
=
$2
coins
=
$3
coins
=
$3
tx
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
coins transfer
-a
$coins
-t
$dst
`
tx
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
coins transfer
-a
$coins
-t
$dst
)
echo
$tx
echo
$tx
sig
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet sign
-a
$src
-d
"
$tx
"
`
sig
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet sign
-a
$src
-d
"
$tx
"
)
echo
$sig
echo
$sig
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet send
-d
"
$sig
"
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet send
-d
"
$sig
"
sleep
3
sleep
3
...
@@ -20,7 +20,7 @@ function trans() {
...
@@ -20,7 +20,7 @@ function trans() {
}
}
function
init
()
{
function
init
()
{
seed
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
seed generate
-l
0
`
seed
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
seed generate
-l
0
)
echo
$seed
echo
$seed
echo
"save seed..."
echo
"save seed..."
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
seed save
-s
"
${
seed
}
"
-p
zzh123456
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
seed save
-s
"
${
seed
}
"
-p
zzh123456
...
@@ -48,116 +48,116 @@ function init() {
...
@@ -48,116 +48,116 @@ function init() {
trans
"14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
"1DQUALqaqPUhJX6FWMCqhvnjrkb6ZfrRmf"
20000
trans
"14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
"1DQUALqaqPUhJX6FWMCqhvnjrkb6ZfrRmf"
20000
}
}
function
send_exec
(){
function
send_exec
()
{
addr
=
$1
addr
=
$1
coins
=
$2
coins
=
$2
tx
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
coins send_exec
-a
$coins
-e
dpos
`
tx
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
coins send_exec
-a
$coins
-e
dpos
)
sig
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet sign
-a
${
addr
}
-d
${
tx
}
`
sig
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet sign
-a
${
addr
}
-d
${
tx
})
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet send
-d
${
sig
}
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet send
-d
${
sig
}
}
}
function
reg
(){
function
reg
()
{
addr
=
$1
addr
=
$1
ip
=
$2
ip
=
$2
key
=
$3
key
=
$3
echo
"dpos regist -a
${
addr
}
-i
${
ip
}
-k
${
key
}
"
echo
"dpos regist -a
${
addr
}
-i
${
ip
}
-k
${
key
}
"
tx
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos regist
-a
${
addr
}
-i
${
ip
}
-k
${
key
}
`
tx
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos regist
-a
${
addr
}
-i
${
ip
}
-k
${
key
})
sig
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet sign
-a
${
addr
}
-d
$tx
`
sig
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet sign
-a
${
addr
}
-d
$tx
)
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet send
-d
"
$sig
"
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet send
-d
"
$sig
"
sleep
3
sleep
3
}
}
function
reReg
(){
function
reReg
()
{
addr
=
$1
addr
=
$1
ip
=
$2
ip
=
$2
key
=
$3
key
=
$3
echo
"dpos reRegist -a
${
addr
}
-i
${
ip
}
-k
${
key
}
"
echo
"dpos reRegist -a
${
addr
}
-i
${
ip
}
-k
${
key
}
"
tx
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos reRegist
-a
${
addr
}
-i
${
ip
}
-k
${
key
}
`
tx
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos reRegist
-a
${
addr
}
-i
${
ip
}
-k
${
key
})
sig
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet sign
-a
${
addr
}
-d
$tx
`
sig
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet sign
-a
${
addr
}
-d
$tx
)
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet send
-d
"
$sig
"
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet send
-d
"
$sig
"
sleep
3
sleep
3
}
}
function
cancelReg
(){
function
cancelReg
()
{
addr
=
$1
addr
=
$1
key
=
$2
key
=
$2
echo
"dpos cancelRegist -a
${
addr
}
-k
${
key
}
"
echo
"dpos cancelRegist -a
${
addr
}
-k
${
key
}
"
tx
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos cancelRegist
-a
${
addr
}
-k
${
key
}
`
tx
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos cancelRegist
-a
${
addr
}
-k
${
key
})
sig
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet sign
-a
${
addr
}
-d
$tx
`
sig
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet sign
-a
${
addr
}
-d
$tx
)
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet send
-d
"
$sig
"
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet send
-d
"
$sig
"
sleep
3
sleep
3
}
}
function
vote
(){
function
vote
()
{
addr
=
$1
addr
=
$1
key
=
$2
key
=
$2
votes
=
$3
votes
=
$3
echo
"dpos vote from addr:
${
addr
}
to key:
${
key
}
$votes
votes"
echo
"dpos vote from addr:
${
addr
}
to key:
${
key
}
$votes
votes"
tx
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos vote
-k
${
key
}
-v
${
votes
}
-a
${
addr
}
`
tx
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos vote
-k
${
key
}
-v
${
votes
}
-a
${
addr
})
sig
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet sign
-a
${
addr
}
-d
$tx
`
sig
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet sign
-a
${
addr
}
-d
$tx
)
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet send
-d
"
$sig
"
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet send
-d
"
$sig
"
sleep
3
sleep
3
}
}
function
cancelVote
(){
function
cancelVote
()
{
addr
=
$1
addr
=
$1
key
=
$2
key
=
$2
votes
=
$3
votes
=
$3
echo
"dpos cancel vote from addr:
${
addr
}
to key:
${
key
}
$votes
votes"
echo
"dpos cancel vote from addr:
${
addr
}
to key:
${
key
}
$votes
votes"
tx
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos cancelVote
-k
${
key
}
-v
${
votes
}
`
tx
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos cancelVote
-k
${
key
}
-v
${
votes
})
sig
=
`
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet sign
-a
${
addr
}
-d
$tx
`
sig
=
$(
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet sign
-a
${
addr
}
-d
$tx
)
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet send
-d
"
$sig
"
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet send
-d
"
$sig
"
sleep
3
sleep
3
}
}
function
qtx
(){
function
qtx
()
{
tx
=
$1
tx
=
$1
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
tx query
-s
${
tx
}
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
tx query
-s
${
tx
}
}
}
function
qn
(){
function
qn
()
{
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos candidatorQuery
-t
topN
-n
$1
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos candidatorQuery
-t
topN
-n
$1
}
}
function
qk
(){
function
qk
()
{
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos candidatorQuery
-t
pubkeys
-k
$1
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos candidatorQuery
-t
pubkeys
-k
$1
}
}
function
qv
(){
function
qv
()
{
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos voteQuery
-a
$1
-k
$2
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
dpos voteQuery
-a
$1
-k
$2
}
}
function
unlock
(){
function
unlock
()
{
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet unlock
-p
zzh123456
./chain33-cli
--rpc_laddr
=
"
${
rcpAddr
}
"
wallet unlock
-p
zzh123456
}
}
#main
#main
para
=
"
$1
"
para
=
"
$1
"
if
[
"
$para
"
==
"init"
]
;
then
if
[
"
$para
"
==
"init"
]
;
then
init
init
elif
[
"
$para
"
==
"sendExec"
]
;
then
elif
[
"
$para
"
==
"sendExec"
]
;
then
send_exec 15LsTP6tkYGZcN7tc1Xo2iYifQfowxot3b 15000
send_exec 15LsTP6tkYGZcN7tc1Xo2iYifQfowxot3b 15000
send_exec 14w5JaGDrXTZwF5Wv51UAtuGgAupenLAok 15000
send_exec 14w5JaGDrXTZwF5Wv51UAtuGgAupenLAok 15000
send_exec 1DQUALqaqPUhJX6FWMCqhvnjrkb6ZfrRmf 15000
send_exec 1DQUALqaqPUhJX6FWMCqhvnjrkb6ZfrRmf 15000
elif
[
"
$para
"
==
"reg"
]
;
then
elif
[
"
$para
"
==
"reg"
]
;
then
reg
"
$2
"
"
$3
"
"
$4
"
reg
"
$2
"
"
$3
"
"
$4
"
elif
[
"
$para
"
==
"cancelReg"
]
;
then
elif
[
"
$para
"
==
"cancelReg"
]
;
then
cancelReg
"
$2
"
"
$3
"
cancelReg
"
$2
"
"
$3
"
elif
[
"
$para
"
==
"reReg"
]
;
then
elif
[
"
$para
"
==
"reReg"
]
;
then
reReg
"
$2
"
"
$3
"
"
$4
"
reReg
"
$2
"
"
$3
"
"
$4
"
elif
[
"
$para
"
==
"vote"
]
;
then
elif
[
"
$para
"
==
"vote"
]
;
then
vote
"
$2
"
"
$3
"
"
$4
"
vote
"
$2
"
"
$3
"
"
$4
"
elif
[
"
$para
"
==
"cancelVote"
]
;
then
elif
[
"
$para
"
==
"cancelVote"
]
;
then
cancelVote
"
$2
"
"
$3
"
"
$4
"
cancelVote
"
$2
"
"
$3
"
"
$4
"
elif
[
"
$para
"
==
"qtx"
]
;
then
elif
[
"
$para
"
==
"qtx"
]
;
then
qtx
"
$2
"
qtx
"
$2
"
elif
[
"
$para
"
==
"qtx"
]
;
then
elif
[
"
$para
"
==
"qtx"
]
;
then
qtx
"
$2
"
qtx
"
$2
"
elif
[
"
$para
"
==
"sendDpos"
]
;
then
elif
[
"
$para
"
==
"sendDpos"
]
;
then
send_exec
$2
$3
send_exec
$2
$3
elif
[
"
$para
"
==
"qn"
]
;
then
elif
[
"
$para
"
==
"qn"
]
;
then
qn
$2
qn
$2
elif
[
"
$para
"
==
"qv"
]
;
then
elif
[
"
$para
"
==
"qv"
]
;
then
qv
$2
$3
qv
$2
$3
elif
[
"
$para
"
==
"qk"
]
;
then
elif
[
"
$para
"
==
"qk"
]
;
then
qk
$2
qk
$2
elif
[
"
$para
"
==
"unlock"
]
;
then
elif
[
"
$para
"
==
"unlock"
]
;
then
unlock
unlock
fi
fi
plugin/dapp/dposvote/commands/vote.go
View file @
697b6b89
...
@@ -16,14 +16,14 @@ import (
...
@@ -16,14 +16,14 @@ import (
"strings"
"strings"
"time"
"time"
vrf
"github.com/33cn/chain33/common/vrf/secp256k1"
jsonrpc
"github.com/33cn/chain33/rpc/jsonclient"
jsonrpc
"github.com/33cn/chain33/rpc/jsonclient"
rpctypes
"github.com/33cn/chain33/rpc/types"
rpctypes
"github.com/33cn/chain33/rpc/types"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/types"
dty
"github.com/33cn/plugin/plugin/dapp/dposvote/types"
ttypes
"github.com/33cn/plugin/plugin/consensus/dpos/types"
ttypes
"github.com/33cn/plugin/plugin/consensus/dpos/types"
"github.com/spf13/cobra"
dty
"github.com/33cn/plugin/plugin/dapp/dposvote/types"
vrf
"github.com/33cn/chain33/common/vrf/secp256k1"
secp256k1
"github.com/btcsuite/btcd/btcec"
secp256k1
"github.com/btcsuite/btcd/btcec"
"github.com/spf13/cobra"
)
)
var
(
var
(
...
@@ -31,6 +31,7 @@ var (
...
@@ -31,6 +31,7 @@ var (
genFile
=
"genesis_file.json"
genFile
=
"genesis_file.json"
pvFile
=
"priv_validator_"
pvFile
=
"priv_validator_"
)
)
//DPosCmd DPosVote合约命令行
//DPosCmd DPosVote合约命令行
func
DPosCmd
()
*
cobra
.
Command
{
func
DPosCmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
...
@@ -89,7 +90,6 @@ func regist(cmd *cobra.Command, args []string) {
...
@@ -89,7 +90,6 @@ func regist(cmd *cobra.Command, args []string) {
address
,
_
:=
cmd
.
Flags
()
.
GetString
(
"address"
)
address
,
_
:=
cmd
.
Flags
()
.
GetString
(
"address"
)
ip
,
_
:=
cmd
.
Flags
()
.
GetString
(
"ip"
)
ip
,
_
:=
cmd
.
Flags
()
.
GetString
(
"ip"
)
payload
:=
fmt
.
Sprintf
(
"{
\"
pubkey
\"
:
\"
%s
\"
,
\"
address
\"
:
\"
%s
\"
,
\"
ip
\"
:
\"
%s
\"
}"
,
pubkey
,
address
,
ip
)
payload
:=
fmt
.
Sprintf
(
"{
\"
pubkey
\"
:
\"
%s
\"
,
\"
address
\"
:
\"
%s
\"
,
\"
ip
\"
:
\"
%s
\"
}"
,
pubkey
,
address
,
ip
)
params
:=
&
rpctypes
.
CreateTxIn
{
params
:=
&
rpctypes
.
CreateTxIn
{
Execer
:
types
.
ExecName
(
dty
.
DPosX
),
Execer
:
types
.
ExecName
(
dty
.
DPosX
),
...
@@ -296,8 +296,7 @@ func candidatorQuery(cmd *cobra.Command, args []string) {
...
@@ -296,8 +296,7 @@ func candidatorQuery(cmd *cobra.Command, args []string) {
case
"pubkeys"
:
case
"pubkeys"
:
keys
:=
strings
.
Split
(
pubkeys
,
";"
)
keys
:=
strings
.
Split
(
pubkeys
,
";"
)
req
:=
&
dty
.
CandidatorQuery
{
req
:=
&
dty
.
CandidatorQuery
{}
}
for
_
,
key
:=
range
keys
{
for
_
,
key
:=
range
keys
{
req
.
Pubkeys
=
append
(
req
.
Pubkeys
,
key
)
req
.
Pubkeys
=
append
(
req
.
Pubkeys
,
key
)
}
}
...
@@ -309,7 +308,6 @@ func candidatorQuery(cmd *cobra.Command, args []string) {
...
@@ -309,7 +308,6 @@ func candidatorQuery(cmd *cobra.Command, args []string) {
}
}
}
}
//DPosVoteQueryCmd 构造投票信息查询的命令行
//DPosVoteQueryCmd 构造投票信息查询的命令行
func
DPosVoteQueryCmd
()
*
cobra
.
Command
{
func
DPosVoteQueryCmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
...
@@ -563,7 +561,7 @@ func vrfQuery(cmd *cobra.Command, args []string) {
...
@@ -563,7 +561,7 @@ func vrfQuery(cmd *cobra.Command, args []string) {
}
}
//CreateCmd to create keyfiles
//
DPos
CreateCmd to create keyfiles
func
DPosCreateCmd
()
*
cobra
.
Command
{
func
DPosCreateCmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"init_keyfile"
,
Use
:
"init_keyfile"
,
...
@@ -656,7 +654,7 @@ func createFiles(cmd *cobra.Command, args []string) {
...
@@ -656,7 +654,7 @@ func createFiles(cmd *cobra.Command, args []string) {
fmt
.
Printf
(
"Generated genesis file path %v
\n
"
,
genFile
)
fmt
.
Printf
(
"Generated genesis file path %v
\n
"
,
genFile
)
}
}
//
Create
Cmd to create keyfiles
//
DPosVrfVerify
Cmd to create keyfiles
func
DPosVrfVerifyCmd
()
*
cobra
.
Command
{
func
DPosVrfVerifyCmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"vrfVerify"
,
Use
:
"vrfVerify"
,
...
@@ -693,7 +691,6 @@ func verify(cmd *cobra.Command, args []string) {
...
@@ -693,7 +691,6 @@ func verify(cmd *cobra.Command, args []string) {
hash
,
_
:=
cmd
.
Flags
()
.
GetString
(
"hash"
)
hash
,
_
:=
cmd
.
Flags
()
.
GetString
(
"hash"
)
proof
,
_
:=
cmd
.
Flags
()
.
GetString
(
"proof"
)
proof
,
_
:=
cmd
.
Flags
()
.
GetString
(
"proof"
)
m
:=
[]
byte
(
data
)
m
:=
[]
byte
(
data
)
r
,
err
:=
hex
.
DecodeString
(
hash
)
r
,
err
:=
hex
.
DecodeString
(
hash
)
...
@@ -739,7 +736,7 @@ func verify(cmd *cobra.Command, args []string) {
...
@@ -739,7 +736,7 @@ func verify(cmd *cobra.Command, args []string) {
fmt
.
Println
(
"vrf hash is same with input hash, vrf Verify succeed"
)
fmt
.
Println
(
"vrf hash is same with input hash, vrf Verify succeed"
)
}
}
//
Cre
ateCmd to create keyfiles
//
DPosVrfEvalu
ateCmd to create keyfiles
func
DPosVrfEvaluateCmd
()
*
cobra
.
Command
{
func
DPosVrfEvaluateCmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"vrfEvaluate"
,
Use
:
"vrfEvaluate"
,
...
@@ -785,7 +782,7 @@ func evaluate(cmd *cobra.Command, args []string) {
...
@@ -785,7 +782,7 @@ func evaluate(cmd *cobra.Command, args []string) {
fmt
.
Println
(
fmt
.
Sprintf
(
"proof:%x"
,
vrfProof
))
fmt
.
Println
(
fmt
.
Sprintf
(
"proof:%x"
,
vrfProof
))
}
}
//
Create
Cmd to create keyfiles
//
DPosCBRecord
Cmd to create keyfiles
func
DPosCBRecordCmd
()
*
cobra
.
Command
{
func
DPosCBRecordCmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"cbRecord"
,
Use
:
"cbRecord"
,
...
@@ -938,7 +935,7 @@ func cbQuery(cmd *cobra.Command, args []string) {
...
@@ -938,7 +935,7 @@ func cbQuery(cmd *cobra.Command, args []string) {
}
}
}
}
//DPos
VrfQueryCmd 构造VRF
相关信息查询的命令行
//DPos
TopNQueryCmd 构造TopN
相关信息查询的命令行
func
DPosTopNQueryCmd
()
*
cobra
.
Command
{
func
DPosTopNQueryCmd
()
*
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"topNQuery"
,
Use
:
"topNQuery"
,
...
@@ -958,7 +955,6 @@ func topNQuery(cmd *cobra.Command, args []string) {
...
@@ -958,7 +955,6 @@ func topNQuery(cmd *cobra.Command, args []string) {
rpcLaddr
,
_
:=
cmd
.
Flags
()
.
GetString
(
"rpc_laddr"
)
rpcLaddr
,
_
:=
cmd
.
Flags
()
.
GetString
(
"rpc_laddr"
)
version
,
_
:=
cmd
.
Flags
()
.
GetInt64
(
"version"
)
version
,
_
:=
cmd
.
Flags
()
.
GetInt64
(
"version"
)
var
params
rpctypes
.
Query4Jrpc
var
params
rpctypes
.
Query4Jrpc
params
.
Execer
=
dty
.
DPosX
params
.
Execer
=
dty
.
DPosX
...
...
plugin/dapp/dposvote/executor/dposvote.go
View file @
697b6b89
...
@@ -26,16 +26,16 @@ var (
...
@@ -26,16 +26,16 @@ var (
updateTopNHeightLimit
int64
=
200
updateTopNHeightLimit
int64
=
200
)
)
// CycleInfo indicates the start and stop of a cycle
type
CycleInfo
struct
{
type
CycleInfo
struct
{
cycle
int64
cycle
int64
cycleStart
int64
cycleStart
int64
cycleStop
int64
cycleStop
int64
}
}
func
calcCycleByTime
(
now
int64
)
*
CycleInfo
{
func
calcCycleByTime
(
now
int64
)
*
CycleInfo
{
cycle
:=
now
/
dposCycle
cycle
:=
now
/
dposCycle
cycleStart
:=
now
-
now
%
dposCycle
cycleStart
:=
now
-
now
%
dposCycle
cycleStop
:=
cycleStart
+
dposCycle
-
1
cycleStop
:=
cycleStart
+
dposCycle
-
1
return
&
CycleInfo
{
return
&
CycleInfo
{
...
...
plugin/dapp/dposvote/executor/dposvotedb.go
View file @
697b6b89
This diff is collapsed.
Click to expand it.
plugin/dapp/dposvote/executor/exec_del_local.go
View file @
697b6b89
...
@@ -25,8 +25,8 @@ func (d *DPos) rollbackCand(cand *dty.CandidatorInfo, log *dty.ReceiptCandicator
...
@@ -25,8 +25,8 @@ func (d *DPos) rollbackCand(cand *dty.CandidatorInfo, log *dty.ReceiptCandicator
//如果投票了,则需要把投票回滚
//如果投票了,则需要把投票回滚
if
log
.
VoteType
==
dty
.
VoteTypeVote
{
if
log
.
VoteType
==
dty
.
VoteTypeVote
{
for
i
:=
0
;
i
<
len
(
cand
.
Voters
);
i
++
{
for
i
:=
0
;
i
<
len
(
cand
.
Voters
);
i
++
{
if
cand
.
Voters
[
i
]
.
Index
==
log
.
Vote
.
Index
&&
cand
.
Voters
[
i
]
.
FromAddr
==
log
.
Vote
.
FromAddr
&&
bytes
.
Equal
(
cand
.
Voters
[
i
]
.
Pubkey
,
log
.
Vote
.
Pubkey
){
if
cand
.
Voters
[
i
]
.
Index
==
log
.
Vote
.
Index
&&
cand
.
Voters
[
i
]
.
FromAddr
==
log
.
Vote
.
FromAddr
&&
bytes
.
Equal
(
cand
.
Voters
[
i
]
.
Pubkey
,
log
.
Vote
.
Pubkey
)
{
cand
.
Voters
=
append
(
cand
.
Voters
[
0
:
i
],
cand
.
Voters
[
i
+
1
:
]
...
)
cand
.
Voters
=
append
(
cand
.
Voters
[
0
:
i
],
cand
.
Voters
[
i
+
1
:
]
...
)
break
break
}
}
...
...
plugin/dapp/dposvote/executor/exec_local.go
View file @
697b6b89
...
@@ -5,9 +5,9 @@
...
@@ -5,9 +5,9 @@
package
executor
package
executor
import
(
import
(
"fmt"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/types"
dty
"github.com/33cn/plugin/plugin/dapp/dposvote/types"
dty
"github.com/33cn/plugin/plugin/dapp/dposvote/types"
"fmt"
)
)
func
(
d
*
DPos
)
updateCandVote
(
log
*
dty
.
ReceiptCandicator
)
(
kvs
[]
*
types
.
KeyValue
,
err
error
)
{
func
(
d
*
DPos
)
updateCandVote
(
log
*
dty
.
ReceiptCandicator
)
(
kvs
[]
*
types
.
KeyValue
,
err
error
)
{
...
@@ -242,12 +242,12 @@ func (d *DPos) ExecLocal_CancelVote(payload *dty.DposCancelVote, tx *types.Trans
...
@@ -242,12 +242,12 @@ func (d *DPos) ExecLocal_CancelVote(payload *dty.DposCancelVote, tx *types.Trans
return
d
.
execLocal
(
receiptData
)
return
d
.
execLocal
(
receiptData
)
}
}
//ExecLocal_
VrfMRegist
method
//ExecLocal_
RegistVrfM
method
func
(
d
*
DPos
)
ExecLocal_RegistVrfM
(
payload
*
dty
.
DposVrfMRegist
,
tx
*
types
.
Transaction
,
receiptData
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
func
(
d
*
DPos
)
ExecLocal_RegistVrfM
(
payload
*
dty
.
DposVrfMRegist
,
tx
*
types
.
Transaction
,
receiptData
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
return
d
.
execLocal
(
receiptData
)
return
d
.
execLocal
(
receiptData
)
}
}
//ExecLocal_
VrfRPRegist
method
//ExecLocal_
RegistVrfRP
method
func
(
d
*
DPos
)
ExecLocal_RegistVrfRP
(
payload
*
dty
.
DposVrfRPRegist
,
tx
*
types
.
Transaction
,
receiptData
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
func
(
d
*
DPos
)
ExecLocal_RegistVrfRP
(
payload
*
dty
.
DposVrfRPRegist
,
tx
*
types
.
Transaction
,
receiptData
*
types
.
ReceiptData
,
index
int
)
(
*
types
.
LocalDBSet
,
error
)
{
return
d
.
execLocal
(
receiptData
)
return
d
.
execLocal
(
receiptData
)
}
}
...
...
plugin/dapp/dposvote/executor/query.go
View file @
697b6b89
plugin/dapp/dposvote/proto/dposvote.proto
View file @
697b6b89
...
@@ -6,7 +6,7 @@ package types;
...
@@ -6,7 +6,7 @@ package types;
message
CandidatorInfo
{
message
CandidatorInfo
{
bytes
pubkey
=
1
;
//候选节点的公钥
bytes
pubkey
=
1
;
//候选节点的公钥
string
address
=
2
;
//后续节点的地址
string
address
=
2
;
//后续节点的地址
string
ip
=
3
;
//候选节点的运行IP
string
IP
=
3
;
//候选节点的运行IP
int64
votes
=
4
;
//候选节点的投票数
int64
votes
=
4
;
//候选节点的投票数
int64
status
=
5
;
//候选节点的状态,0:注册,1:当选,2:取消注册
int64
status
=
5
;
//候选节点的状态,0:注册,1:当选,2:取消注册
int64
preStatus
=
6
;
int64
preStatus
=
6
;
...
@@ -19,6 +19,7 @@ message CandidatorInfo {
...
@@ -19,6 +19,7 @@ message CandidatorInfo {
repeated
DposVoter
voters
=
13
;
repeated
DposVoter
voters
=
13
;
}
}
//DposVoter 投票者信息
message
DposVoter
{
message
DposVoter
{
string
fromAddr
=
1
;
string
fromAddr
=
1
;
bytes
pubkey
=
2
;
//候选节点的公钥
bytes
pubkey
=
2
;
//候选节点的公钥
...
@@ -31,7 +32,7 @@ message DposVoter{
...
@@ -31,7 +32,7 @@ message DposVoter{
message
Candidator
{
message
Candidator
{
bytes
pubkey
=
1
;
//候选节点的公钥
bytes
pubkey
=
1
;
//候选节点的公钥
string
address
=
2
;
//后续节点的地址
string
address
=
2
;
//后续节点的地址
string
ip
=
3
;
//候选节点的运行IP
string
IP
=
3
;
//候选节点的运行IP
int64
votes
=
4
;
//候选节点的投票数
int64
votes
=
4
;
//候选节点的投票数
int64
status
=
5
;
//候选节点的状态,0:注册,1:当选,2:取消注册
int64
status
=
5
;
//候选节点的状态,0:注册,1:当选,2:取消注册
}
}
...
@@ -40,7 +41,7 @@ message Candidator {
...
@@ -40,7 +41,7 @@ message Candidator {
message
DposCandidatorRegist
{
message
DposCandidatorRegist
{
string
pubkey
=
1
;
//候选节点的公钥
string
pubkey
=
1
;
//候选节点的公钥
string
address
=
2
;
//候选节点的地址
string
address
=
2
;
//候选节点的地址
string
ip
=
3
;
//候选节点的共识IP地址
string
IP
=
3
;
//候选节点的共识IP地址
}
}
//DposCandidatorCancelRegist 注销Dpos候选节点,解冻抵押的币
//DposCandidatorCancelRegist 注销Dpos候选节点,解冻抵押的币
...
@@ -83,6 +84,7 @@ message DposVoteAction {
...
@@ -83,6 +84,7 @@ message DposVoteAction {
int32
ty
=
15
;
int32
ty
=
15
;
}
}
//CandidatorQuery 候选节点查询
message
CandidatorQuery
{
message
CandidatorQuery
{
repeated
string
pubkeys
=
1
;
//候选节点公钥集合
repeated
string
pubkeys
=
1
;
//候选节点公钥集合
int32
topN
=
2
;
//topN
int32
topN
=
2
;
//topN
...
@@ -91,24 +93,28 @@ message CandidatorQuery{
...
@@ -91,24 +93,28 @@ message CandidatorQuery{
//
Candidator 候选节点
信息
//
JSONCandidator 候选节点Json格式
信息
message
J
son
Candidator
{
message
J
SON
Candidator
{
string
pubkey
=
1
;
//候选节点的公钥
string
pubkey
=
1
;
//候选节点的公钥
string
address
=
2
;
//后续节点的地址
string
address
=
2
;
//后续节点的地址
string
ip
=
3
;
//候选节点的运行IP
string
IP
=
3
;
//候选节点的运行IP
int64
votes
=
4
;
//候选节点的投票数
int64
votes
=
4
;
//候选节点的投票数
int64
status
=
5
;
//候选节点的状态,0:注册,1:当选,2:取消注册
int64
status
=
5
;
//候选节点的状态,0:注册,1:当选,2:取消注册
}
}
//CandidatorReply 候选节点查询响应
message
CandidatorReply
{
message
CandidatorReply
{
repeated
J
son
Candidator
candidators
=
1
;
//候选节点
repeated
J
SON
Candidator
candidators
=
1
;
//候选节点
}
}
//DposVoteQuery 投票查询
message
DposVoteQuery
{
message
DposVoteQuery
{
repeated
string
pubkeys
=
1
;
//候选节点的公钥,如果为空,则查询所有,否则,查询该地址给具体候选节点的投票
repeated
string
pubkeys
=
1
;
//候选节点的公钥,如果为空,则查询所有,否则,查询该地址给具体候选节点的投票
string
addr
=
2
;
//要查询的地址
string
addr
=
2
;
//要查询的地址
}
}
message
JsonDposVoter
{
//JSONDposVoter Json格式的投票者信息
message
JSONDposVoter
{
string
fromAddr
=
1
;
string
fromAddr
=
1
;
string
pubkey
=
2
;
//候选节点的公钥
string
pubkey
=
2
;
//候选节点的公钥
int64
votes
=
3
;
//投给候选节点的票数,不能超过锁在合约中的未使用票数
int64
votes
=
3
;
//投给候选节点的票数,不能超过锁在合约中的未使用票数
...
@@ -116,8 +122,9 @@ message JsonDposVoter{
...
@@ -116,8 +122,9 @@ message JsonDposVoter{
int64
time
=
5
;
int64
time
=
5
;
}
}
//DposVoteReply 投票查询响应
message
DposVoteReply
{
message
DposVoteReply
{
repeated
J
son
DposVoter
votes
=
1
;
//某地址对具体候选节点的投票情况
repeated
J
SON
DposVoter
votes
=
1
;
//某地址对具体候选节点的投票情况
}
}
//ReceiptCandicator 候选者收据信息
//ReceiptCandicator 候选者收据信息
...
@@ -135,6 +142,7 @@ message ReceiptCandicator {
...
@@ -135,6 +142,7 @@ message ReceiptCandicator {
int64
time
=
11
;
int64
time
=
11
;
}
}
//DposVrfM VrfM信息
message
DposVrfM
{
message
DposVrfM
{
int64
Index
=
1
;
int64
Index
=
1
;
bytes
pubkey
=
2
;
bytes
pubkey
=
2
;
...
@@ -147,6 +155,7 @@ message DposVrfM{
...
@@ -147,6 +155,7 @@ message DposVrfM{
int64
cycleStop
=
9
;
int64
cycleStop
=
9
;
}
}
//DposVrfRP VrfRP信息
message
DposVrfRP
{
message
DposVrfRP
{
int64
Index
=
1
;
int64
Index
=
1
;
bytes
pubkey
=
2
;
bytes
pubkey
=
2
;
...
@@ -161,12 +170,14 @@ message DposVrfRP{
...
@@ -161,12 +170,14 @@ message DposVrfRP{
int64
cycleStop
=
11
;
int64
cycleStop
=
11
;
}
}
//DposVrfMRegist VrfM注册请求
message
DposVrfMRegist
{
message
DposVrfMRegist
{
string
pubkey
=
1
;
string
pubkey
=
1
;
int64
cycle
=
2
;
int64
cycle
=
2
;
string
m
=
3
;
//vrf的输入
string
m
=
3
;
//vrf的输入
}
}
//DposVrfRPRegist VrfRP注册请求
message
DposVrfRPRegist
{
message
DposVrfRPRegist
{
string
pubkey
=
1
;
string
pubkey
=
1
;
int64
cycle
=
2
;
int64
cycle
=
2
;
...
@@ -191,7 +202,7 @@ message ReceiptVrf {
...
@@ -191,7 +202,7 @@ message ReceiptVrf {
int64
cycleStop
=
12
;
int64
cycleStop
=
12
;
}
}
//VrfInfo vrf信息
message
VrfInfo
{
message
VrfInfo
{
int64
Index
=
1
;
int64
Index
=
1
;
bytes
pubkey
=
2
;
bytes
pubkey
=
2
;
...
@@ -203,6 +214,7 @@ message VrfInfo {
...
@@ -203,6 +214,7 @@ message VrfInfo {
int64
time
=
9
;
int64
time
=
9
;
}
}
//DposVrfQuery vrf查询请求
message
DposVrfQuery
{
message
DposVrfQuery
{
repeated
string
pubkeys
=
1
;
repeated
string
pubkeys
=
1
;
int64
ty
=
2
;
int64
ty
=
2
;
...
@@ -210,7 +222,8 @@ message DposVrfQuery{
...
@@ -210,7 +222,8 @@ message DposVrfQuery{
int64
cycle
=
4
;
int64
cycle
=
4
;
}
}
message
JsonVrfInfo
{
//JSONVrfInfo json格式的vrf信息
message
JSONVrfInfo
{
int64
Index
=
1
;
int64
Index
=
1
;
string
pubkey
=
2
;
string
pubkey
=
2
;
int64
cycle
=
4
;
int64
cycle
=
4
;
...
@@ -221,10 +234,12 @@ message JsonVrfInfo {
...
@@ -221,10 +234,12 @@ message JsonVrfInfo {
int64
time
=
9
;
int64
time
=
9
;
}
}
//DposVrfReply vrf查询响应
message
DposVrfReply
{
message
DposVrfReply
{
repeated
J
son
VrfInfo
vrf
=
1
;
repeated
J
SON
VrfInfo
vrf
=
1
;
}
}
//DposCycleBoundaryInfo cycle边界信息
message
DposCycleBoundaryInfo
{
message
DposCycleBoundaryInfo
{
int64
cycle
=
1
;
int64
cycle
=
1
;
int64
stopHeight
=
2
;
int64
stopHeight
=
2
;
...
@@ -233,6 +248,7 @@ message DposCycleBoundaryInfo {
...
@@ -233,6 +248,7 @@ message DposCycleBoundaryInfo {
bytes
signature
=
5
;
bytes
signature
=
5
;
}
}
//DposCBInfo cycle边界记录请求消息
message
DposCBInfo
{
message
DposCBInfo
{
int64
cycle
=
1
;
int64
cycle
=
1
;
int64
stopHeight
=
2
;
int64
stopHeight
=
2
;
...
@@ -241,6 +257,7 @@ message DposCBInfo {
...
@@ -241,6 +257,7 @@ message DposCBInfo {
string
signature
=
5
;
string
signature
=
5
;
}
}
//DposCBQuery cycle边界记录查询请求
message
DposCBQuery
{
message
DposCBQuery
{
int64
cycle
=
1
;
int64
cycle
=
1
;
int64
stopHeight
=
2
;
int64
stopHeight
=
2
;
...
@@ -248,6 +265,7 @@ message DposCBQuery {
...
@@ -248,6 +265,7 @@ message DposCBQuery {
int32
ty
=
4
;
int32
ty
=
4
;
}
}
//DposCBReply cycle边界记录查询响应
message
DposCBReply
{
message
DposCBReply
{
DposCBInfo
cbInfo
=
1
;
DposCBInfo
cbInfo
=
1
;
}
}
...
@@ -266,6 +284,7 @@ message ReceiptCB {
...
@@ -266,6 +284,7 @@ message ReceiptCB {
DposCycleBoundaryInfo
cbInfo
=
10
;
DposCycleBoundaryInfo
cbInfo
=
10
;
}
}
//TopNCandidator topN候选者
message
TopNCandidator
{
message
TopNCandidator
{
repeated
Candidator
cands
=
1
;
repeated
Candidator
cands
=
1
;
bytes
hash
=
2
;
bytes
hash
=
2
;
...
@@ -274,6 +293,7 @@ message TopNCandidator{
...
@@ -274,6 +293,7 @@ message TopNCandidator{
bytes
signature
=
5
;
bytes
signature
=
5
;
}
}
//TopNCandidators topN候选者(复数)
message
TopNCandidators
{
message
TopNCandidators
{
repeated
TopNCandidator
candsVotes
=
1
;
repeated
TopNCandidator
candsVotes
=
1
;
int64
version
=
2
;
int64
version
=
2
;
...
@@ -281,18 +301,22 @@ message TopNCandidators{
...
@@ -281,18 +301,22 @@ message TopNCandidators{
repeated
Candidator
finalCands
=
4
;
repeated
Candidator
finalCands
=
4
;
}
}
//TopNCandidatorRegist topN候选者注册请求
message
TopNCandidatorRegist
{
message
TopNCandidatorRegist
{
TopNCandidator
cand
=
1
;
TopNCandidator
cand
=
1
;
}
}
//TopNCandidatorsQuery topN候选者查询请求
message
TopNCandidatorsQuery
{
message
TopNCandidatorsQuery
{
int64
version
=
1
;
int64
version
=
1
;
}
}
//TopNCandidatorsReply topN候选者查询响应
message
TopNCandidatorsReply
{
message
TopNCandidatorsReply
{
TopNCandidators
topN
=
1
;
TopNCandidators
topN
=
1
;
}
}
//ReceiptTopN topN注册的收据信息
message
ReceiptTopN
{
message
ReceiptTopN
{
int64
Index
=
1
;
int64
Index
=
1
;
bytes
pubkey
=
2
;
bytes
pubkey
=
2
;
...
...
plugin/dapp/dposvote/types/const.go
View file @
697b6b89
...
@@ -44,18 +44,33 @@ const (
...
@@ -44,18 +44,33 @@ const (
)
)
const
(
const
(
VoteFrozenTime
=
3
*
24
*
3600
//VoteFrozenTime = 3 * 24 * 3600
//RegistFrozenCoins 注册为候选节点需要抵押冻结的代币数量
RegistFrozenCoins
=
1000000000000
RegistFrozenCoins
=
1000000000000
//VoteTypeNone 非投票类型
VoteTypeNone
int32
=
1
VoteTypeNone
int32
=
1
//VoteTypeVote 投票类型
VoteTypeVote
int32
=
2
VoteTypeVote
int32
=
2
//VoteTypeCancelVote 撤销投票类型
VoteTypeCancelVote
int32
=
3
VoteTypeCancelVote
int32
=
3
//VoteTypeCancelAllVote 撤销所有投票类型
VoteTypeCancelAllVote
int32
=
4
VoteTypeCancelAllVote
int32
=
4
//TopNCandidatorsVoteInit topN投票状态:初始状态
TopNCandidatorsVoteInit
int64
=
0
TopNCandidatorsVoteInit
int64
=
0
//TopNCandidatorsVoteMajorOK topN投票状态:2/3多数达成一致
TopNCandidatorsVoteMajorOK
int64
=
1
TopNCandidatorsVoteMajorOK
int64
=
1
//TopNCandidatorsVoteMajorFail topN投票状态:2/3多数达成一致失败
TopNCandidatorsVoteMajorFail
int64
=
2
TopNCandidatorsVoteMajorFail
int64
=
2
)
)
//包的名字可以通过配置文件来配置
//包的名字可以通过配置文件来配置
//建议用github的组织名称,或者用户名字开头, 再加上自己的插件的名字
//建议用github的组织名称,或者用户名字开头, 再加上自己的插件的名字
//如果发生重名,可以通过配置文件修改这些名字
//如果发生重名,可以通过配置文件修改这些名字
...
@@ -132,7 +147,7 @@ const (
...
@@ -132,7 +147,7 @@ const (
FuncNameQueryCBInfoByHash
=
"QueryCBInfoByHash"
FuncNameQueryCBInfoByHash
=
"QueryCBInfoByHash"
//FuncNameQueryLatestCBInfoByHeight func name
//FuncNameQueryLatestCBInfoByHeight func name
FuncNameQueryLatestCBInfoByHeight
=
"QueryLatestCBInfoByHeight"
//
FuncNameQueryLatestCBInfoByHeight = "QueryLatestCBInfoByHeight"
//QueryCBInfoByCycle 根据cycle查询cycle boundary信息
//QueryCBInfoByCycle 根据cycle查询cycle boundary信息
QueryCBInfoByCycle
=
1
QueryCBInfoByCycle
=
1
...
@@ -143,8 +158,8 @@ const (
...
@@ -143,8 +158,8 @@ const (
//QueryCBInfoByHash 根据stopHash查询cycle boundary信息
//QueryCBInfoByHash 根据stopHash查询cycle boundary信息
QueryCBInfoByHash
=
3
QueryCBInfoByHash
=
3
//QueryCBInfoByHeight 根据stopHeight查询cycle boundary信息
//Query
Latest
CBInfoByHeight 根据stopHeight查询cycle boundary信息
QueryLatestCBInfoByHeight
=
4
//
QueryLatestCBInfoByHeight = 4
//FuncNameQueryTopNByVersion func name
//FuncNameQueryTopNByVersion func name
FuncNameQueryTopNByVersion
=
"QueryTopNByVersion"
FuncNameQueryTopNByVersion
=
"QueryTopNByVersion"
...
...
plugin/dapp/dposvote/types/dposvote.pb.go
View file @
697b6b89
This diff is collapsed.
Click to expand it.
plugin/dapp/dposvote/types/msg.go
View file @
697b6b89
...
@@ -4,10 +4,9 @@ import (
...
@@ -4,10 +4,9 @@ import (
"bytes"
"bytes"
"encoding/hex"
"encoding/hex"
"encoding/json"
"encoding/json"
"errors"
"fmt"
"fmt"
ttypes
"github.com/33cn/plugin/plugin/consensus/dpos/types"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/common/crypto"
ttypes
"github.com/33cn/plugin/plugin/consensus/dpos/types"
)
)
// CanonicalOnceCBInfo ...
// CanonicalOnceCBInfo ...
...
@@ -28,54 +27,53 @@ func CanonicalCBInfo(cb *DposCBInfo) CanonicalOnceCBInfo {
...
@@ -28,54 +27,53 @@ func CanonicalCBInfo(cb *DposCBInfo) CanonicalOnceCBInfo {
}
}
}
}
// Verify ...
// CanonicalCBInfo ...
func
(
cb
*
DposCBInfo
)
Verify
()
error
{
func
(
cb
*
DposCBInfo
)
Verify
()
error
{
buf
:=
new
(
bytes
.
Buffer
)
buf
:=
new
(
bytes
.
Buffer
)
canonical
:=
CanonicalCBInfo
(
cb
)
canonical
:=
CanonicalCBInfo
(
cb
)
byteCB
,
err
:=
json
.
Marshal
(
&
canonical
)
byteCB
,
err
:=
json
.
Marshal
(
&
canonical
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
New
(
fmt
.
Sprintf
(
"Error marshal CanonicalOnceCBInfo: %v"
,
err
)
)
return
fmt
.
Errorf
(
"Error marshal CanonicalOnceCBInfo: %v"
,
err
)
}
}
_
,
err
=
buf
.
Write
(
byteCB
)
_
,
err
=
buf
.
Write
(
byteCB
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
New
(
fmt
.
Sprintf
(
"Error write buffer: %v"
,
err
)
)
return
fmt
.
Errorf
(
"Error write buffer: %v"
,
err
)
}
}
bPubkey
,
err
:=
hex
.
DecodeString
(
cb
.
Pubkey
)
bPubkey
,
err
:=
hex
.
DecodeString
(
cb
.
Pubkey
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
New
(
fmt
.
Sprintf
(
"Error Decode pubkey: %v"
,
err
)
)
return
fmt
.
Errorf
(
"Error Decode pubkey: %v"
,
err
)
}
}
pubkey
,
err
:=
ttypes
.
ConsensusCrypto
.
PubKeyFromBytes
(
bPubkey
)
pubkey
,
err
:=
ttypes
.
ConsensusCrypto
.
PubKeyFromBytes
(
bPubkey
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
New
(
fmt
.
Sprintf
(
"Error PubKeyFromBytes: %v"
,
err
)
)
return
fmt
.
Errorf
(
"Error PubKeyFromBytes: %v"
,
err
)
}
}
signature
,
err
:=
hex
.
DecodeString
(
cb
.
Signature
)
signature
,
err
:=
hex
.
DecodeString
(
cb
.
Signature
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
New
(
fmt
.
Sprintf
(
"Error Decode Signature: %v"
,
err
)
)
return
fmt
.
Errorf
(
"Error Decode Signature: %v"
,
err
)
}
}
sig
,
err
:=
ttypes
.
ConsensusCrypto
.
SignatureFromBytes
(
signature
)
sig
,
err
:=
ttypes
.
ConsensusCrypto
.
SignatureFromBytes
(
signature
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
New
(
fmt
.
Sprintf
(
"Error SignatureFromBytes: %v"
,
err
)
)
return
fmt
.
Errorf
(
"Error SignatureFromBytes: %v"
,
err
)
}
}
if
!
pubkey
.
VerifyBytes
(
buf
.
Bytes
(),
sig
)
{
if
!
pubkey
.
VerifyBytes
(
buf
.
Bytes
(),
sig
)
{
return
errors
.
New
(
fmt
.
Sprintf
(
"Error VerifyBytes: %v"
,
err
)
)
return
fmt
.
Errorf
(
"Error VerifyBytes: %v"
,
err
)
}
}
return
nil
return
nil
}
}
// OnceCandidator ...
type
OnceCandidator
struct
{
type
OnceCandidator
struct
{
Pubkey
[]
byte
`json:"pubkey,omitempty"`
Pubkey
[]
byte
`json:"pubkey,omitempty"`
Address
string
`json:"address,omitempty"`
Address
string
`json:"address,omitempty"`
I
p
string
`json:"ip,omitempty"`
I
P
string
`json:"ip,omitempty"`
}
}
// CanonicalOnceTopNCandidator ...
// CanonicalOnceTopNCandidator ...
...
@@ -87,13 +85,13 @@ type CanonicalOnceTopNCandidator struct {
...
@@ -87,13 +85,13 @@ type CanonicalOnceTopNCandidator struct {
Signature
[]
byte
`json:"signature,omitempty"`
Signature
[]
byte
`json:"signature,omitempty"`
}
}
func
(
topN
*
CanonicalOnceTopNCandidator
)
onlyCopyCands
()
CanonicalOnceTopNCandidator
{
func
(
topN
*
CanonicalOnceTopNCandidator
)
onlyCopyCands
()
CanonicalOnceTopNCandidator
{
obj
:=
CanonicalOnceTopNCandidator
{}
obj
:=
CanonicalOnceTopNCandidator
{}
for
i
:=
0
;
i
<
len
(
topN
.
Cands
);
i
++
{
for
i
:=
0
;
i
<
len
(
topN
.
Cands
);
i
++
{
cand
:=
&
OnceCandidator
{
cand
:=
&
OnceCandidator
{
Pubkey
:
topN
.
Cands
[
i
]
.
Pubkey
,
Pubkey
:
topN
.
Cands
[
i
]
.
Pubkey
,
Address
:
topN
.
Cands
[
i
]
.
Address
,
Address
:
topN
.
Cands
[
i
]
.
Address
,
I
p
:
topN
.
Cands
[
i
]
.
Ip
,
I
P
:
topN
.
Cands
[
i
]
.
IP
,
}
}
obj
.
Cands
=
append
(
obj
.
Cands
,
cand
)
obj
.
Cands
=
append
(
obj
.
Cands
,
cand
)
}
}
...
@@ -101,7 +99,8 @@ func (topN *CanonicalOnceTopNCandidator) onlyCopyCands() CanonicalOnceTopNCandid
...
@@ -101,7 +99,8 @@ func (topN *CanonicalOnceTopNCandidator) onlyCopyCands() CanonicalOnceTopNCandid
return
obj
return
obj
}
}
func
(
topN
*
CanonicalOnceTopNCandidator
)
ID
()
[]
byte
{
// ID ...
func
(
topN
*
CanonicalOnceTopNCandidator
)
ID
()
[]
byte
{
obj
:=
topN
.
onlyCopyCands
()
obj
:=
topN
.
onlyCopyCands
()
encode
,
err
:=
json
.
Marshal
(
&
obj
)
encode
,
err
:=
json
.
Marshal
(
&
obj
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -109,7 +108,8 @@ func (topN *CanonicalOnceTopNCandidator) ID() []byte{
...
@@ -109,7 +108,8 @@ func (topN *CanonicalOnceTopNCandidator) ID() []byte{
}
}
return
crypto
.
Ripemd160
(
encode
)
return
crypto
.
Ripemd160
(
encode
)
}
}
// CanonicalCBInfo ...
// CanonicalTopNCandidator ...
func
CanonicalTopNCandidator
(
topN
*
TopNCandidator
)
CanonicalOnceTopNCandidator
{
func
CanonicalTopNCandidator
(
topN
*
TopNCandidator
)
CanonicalOnceTopNCandidator
{
onceTopNCandidator
:=
CanonicalOnceTopNCandidator
{
onceTopNCandidator
:=
CanonicalOnceTopNCandidator
{
Height
:
topN
.
Height
,
Height
:
topN
.
Height
,
...
@@ -120,14 +120,14 @@ func CanonicalTopNCandidator(topN *TopNCandidator) CanonicalOnceTopNCandidator {
...
@@ -120,14 +120,14 @@ func CanonicalTopNCandidator(topN *TopNCandidator) CanonicalOnceTopNCandidator {
cand
:=
&
OnceCandidator
{
cand
:=
&
OnceCandidator
{
Pubkey
:
topN
.
Cands
[
i
]
.
Pubkey
,
Pubkey
:
topN
.
Cands
[
i
]
.
Pubkey
,
Address
:
topN
.
Cands
[
i
]
.
Address
,
Address
:
topN
.
Cands
[
i
]
.
Address
,
I
p
:
topN
.
Cands
[
i
]
.
Ip
,
I
P
:
topN
.
Cands
[
i
]
.
IP
,
}
}
onceTopNCandidator
.
Cands
=
append
(
onceTopNCandidator
.
Cands
,
cand
)
onceTopNCandidator
.
Cands
=
append
(
onceTopNCandidator
.
Cands
,
cand
)
}
}
return
onceTopNCandidator
return
onceTopNCandidator
}
}
func
(
topN
*
TopNCandidator
)
copyWithoutSig
()
*
TopNCandidator
{
func
(
topN
*
TopNCandidator
)
copyWithoutSig
()
*
TopNCandidator
{
cpy
:=
&
TopNCandidator
{
cpy
:=
&
TopNCandidator
{
Hash
:
topN
.
Hash
,
Hash
:
topN
.
Hash
,
Height
:
topN
.
Height
,
Height
:
topN
.
Height
,
...
@@ -140,41 +140,44 @@ func (topN *TopNCandidator)copyWithoutSig() *TopNCandidator {
...
@@ -140,41 +140,44 @@ func (topN *TopNCandidator)copyWithoutSig() *TopNCandidator {
}
}
return
cpy
return
cpy
}
}
// Verify ...
// Verify ...
func
(
topN
*
TopNCandidator
)
Verify
()
error
{
func
(
topN
*
TopNCandidator
)
Verify
()
error
{
buf
:=
new
(
bytes
.
Buffer
)
buf
:=
new
(
bytes
.
Buffer
)
cpy
:=
topN
.
copyWithoutSig
()
cpy
:=
topN
.
copyWithoutSig
()
byteCB
,
err
:=
json
.
Marshal
(
cpy
)
byteCB
,
err
:=
json
.
Marshal
(
cpy
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
New
(
fmt
.
Sprintf
(
"Error marshal TopNCandidator: %v"
,
err
)
)
return
fmt
.
Errorf
(
"Error marshal TopNCandidator: %v"
,
err
)
}
}
_
,
err
=
buf
.
Write
(
byteCB
)
_
,
err
=
buf
.
Write
(
byteCB
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
New
(
fmt
.
Sprintf
(
"Error write buffer: %v"
,
err
)
)
return
fmt
.
Errorf
(
"Error write buffer: %v"
,
err
)
}
}
pubkey
,
err
:=
ttypes
.
ConsensusCrypto
.
PubKeyFromBytes
(
topN
.
SignerPubkey
)
pubkey
,
err
:=
ttypes
.
ConsensusCrypto
.
PubKeyFromBytes
(
topN
.
SignerPubkey
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
New
(
fmt
.
Sprintf
(
"Error PubKeyFromBytes: %v"
,
err
)
)
return
fmt
.
Errorf
(
"Error PubKeyFromBytes: %v"
,
err
)
}
}
sig
,
err
:=
ttypes
.
ConsensusCrypto
.
SignatureFromBytes
(
topN
.
Signature
)
sig
,
err
:=
ttypes
.
ConsensusCrypto
.
SignatureFromBytes
(
topN
.
Signature
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
New
(
fmt
.
Sprintf
(
"Error SignatureFromBytes: %v"
,
err
)
)
return
fmt
.
Errorf
(
"Error SignatureFromBytes: %v"
,
err
)
}
}
if
!
pubkey
.
VerifyBytes
(
buf
.
Bytes
(),
sig
)
{
if
!
pubkey
.
VerifyBytes
(
buf
.
Bytes
(),
sig
)
{
return
errors
.
New
(
fmt
.
Sprintf
(
"Error VerifyBytes: %v"
,
err
)
)
return
fmt
.
Errorf
(
"Error VerifyBytes: %v"
,
err
)
}
}
return
nil
return
nil
}
}
func
(
cand
*
Candidator
)
Copy
()
*
Candidator
{
// Copy ...
func
(
cand
*
Candidator
)
Copy
()
*
Candidator
{
cpy
:=
&
Candidator
{
cpy
:=
&
Candidator
{
Address
:
cand
.
Address
,
Address
:
cand
.
Address
,
I
p
:
cand
.
Ip
,
I
P
:
cand
.
IP
,
Votes
:
cand
.
Votes
,
Votes
:
cand
.
Votes
,
Status
:
cand
.
Status
,
Status
:
cand
.
Status
,
}
}
...
@@ -183,12 +186,14 @@ func (cand *Candidator)Copy() *Candidator {
...
@@ -183,12 +186,14 @@ func (cand *Candidator)Copy() *Candidator {
copy
(
cpy
.
Pubkey
,
cand
.
Pubkey
)
copy
(
cpy
.
Pubkey
,
cand
.
Pubkey
)
return
cpy
return
cpy
}
}
func
(
topNs
*
TopNCandidators
)
CheckVoteStauts
(
delegateNum
int64
)
{
// CheckVoteStauts ...
func
(
topNs
*
TopNCandidators
)
CheckVoteStauts
(
delegateNum
int64
)
{
if
topNs
.
Status
==
TopNCandidatorsVoteMajorOK
||
topNs
.
Status
==
TopNCandidatorsVoteMajorFail
{
if
topNs
.
Status
==
TopNCandidatorsVoteMajorOK
||
topNs
.
Status
==
TopNCandidatorsVoteMajorFail
{
return
return
}
}
voteMap
:=
make
(
map
[
string
]
int64
)
voteMap
:=
make
(
map
[
string
]
int64
)
for
i
:=
0
;
i
<
len
(
topNs
.
CandsVotes
);
i
++
{
for
i
:=
0
;
i
<
len
(
topNs
.
CandsVotes
);
i
++
{
key
:=
hex
.
EncodeToString
(
topNs
.
CandsVotes
[
i
]
.
Hash
)
key
:=
hex
.
EncodeToString
(
topNs
.
CandsVotes
[
i
]
.
Hash
)
...
@@ -206,8 +211,8 @@ func (topNs *TopNCandidators)CheckVoteStauts(delegateNum int64) {
...
@@ -206,8 +211,8 @@ func (topNs *TopNCandidators)CheckVoteStauts(delegateNum int64) {
}
}
}
}
var
maxVotes
int64
=
0
var
maxVotes
int64
var
sumVotes
int64
=
0
var
sumVotes
int64
for
_
,
v
:=
range
voteMap
{
for
_
,
v
:=
range
voteMap
{
if
v
>
maxVotes
{
if
v
>
maxVotes
{
maxVotes
=
v
maxVotes
=
v
...
@@ -215,7 +220,7 @@ func (topNs *TopNCandidators)CheckVoteStauts(delegateNum int64) {
...
@@ -215,7 +220,7 @@ func (topNs *TopNCandidators)CheckVoteStauts(delegateNum int64) {
sumVotes
+=
v
sumVotes
+=
v
}
}
if
maxVotes
+
(
delegateNum
-
sumVotes
)
<
(
delegateNum
*
2
/
3
)
{
if
maxVotes
+
(
delegateNum
-
sumVotes
)
<
(
delegateNum
*
2
/
3
)
{
topNs
.
Status
=
TopNCandidatorsVoteMajorFail
topNs
.
Status
=
TopNCandidatorsVoteMajorFail
}
}
}
}
plugin/dapp/dposvote/types/table.go
View file @
697b6b89
...
@@ -121,12 +121,11 @@ func (tx *DposCandidatorRow) Get(key string) ([]byte, error) {
...
@@ -121,12 +121,11 @@ func (tx *DposCandidatorRow) Get(key string) ([]byte, error) {
return
nil
,
types
.
ErrNotFound
return
nil
,
types
.
ErrNotFound
}
}
var
opt_dpos_vrfm
=
&
table
.
Option
{
var
opt_dpos_vrfm
=
&
table
.
Option
{
Prefix
:
"LODB-dpos"
,
Prefix
:
"LODB-dpos"
,
Name
:
"vrfm"
,
Name
:
"vrfm"
,
Primary
:
"index"
,
Primary
:
"index"
,
Index
:
[]
string
{
"pubkey_cycle"
,
"cycle"
},
Index
:
[]
string
{
"pubkey_cycle"
,
"cycle"
},
}
}
//NewDposVrfMTable 新建表
//NewDposVrfMTable 新建表
...
@@ -176,7 +175,6 @@ func (tx *DposVrfMRow) Get(key string) ([]byte, error) {
...
@@ -176,7 +175,6 @@ func (tx *DposVrfMRow) Get(key string) ([]byte, error) {
return
nil
,
types
.
ErrNotFound
return
nil
,
types
.
ErrNotFound
}
}
var
opt_dpos_vrfrp
=
&
table
.
Option
{
var
opt_dpos_vrfrp
=
&
table
.
Option
{
Prefix
:
"LODB-dpos"
,
Prefix
:
"LODB-dpos"
,
Name
:
"vrfrp"
,
Name
:
"vrfrp"
,
...
@@ -231,8 +229,6 @@ func (tx *DposVrfRPRow) Get(key string) ([]byte, error) {
...
@@ -231,8 +229,6 @@ func (tx *DposVrfRPRow) Get(key string) ([]byte, error) {
return
nil
,
types
.
ErrNotFound
return
nil
,
types
.
ErrNotFound
}
}
var
opt_dpos_cb
=
&
table
.
Option
{
var
opt_dpos_cb
=
&
table
.
Option
{
Prefix
:
"LODB-dpos"
,
Prefix
:
"LODB-dpos"
,
Name
:
"cb"
,
Name
:
"cb"
,
...
@@ -240,7 +236,7 @@ var opt_dpos_cb = &table.Option{
...
@@ -240,7 +236,7 @@ var opt_dpos_cb = &table.Option{
Index
:
[]
string
{
"height"
,
"hash"
},
Index
:
[]
string
{
"height"
,
"hash"
},
}
}
//NewDpos
VrfRP
Table 新建表
//NewDpos
CB
Table 新建表
func
NewDposCBTable
(
kvdb
db
.
KV
)
*
table
.
Table
{
func
NewDposCBTable
(
kvdb
db
.
KV
)
*
table
.
Table
{
rowmeta
:=
NewDposCBRow
()
rowmeta
:=
NewDposCBRow
()
table
,
err
:=
table
.
NewTable
(
rowmeta
,
kvdb
,
opt_dpos_cb
)
table
,
err
:=
table
.
NewTable
(
rowmeta
,
kvdb
,
opt_dpos_cb
)
...
@@ -255,7 +251,7 @@ type DposCBRow struct {
...
@@ -255,7 +251,7 @@ type DposCBRow struct {
*
DposCycleBoundaryInfo
*
DposCycleBoundaryInfo
}
}
//NewDpos
VrfRP
Row 新建一个meta 结构
//NewDpos
CB
Row 新建一个meta 结构
func
NewDposCBRow
()
*
DposCBRow
{
func
NewDposCBRow
()
*
DposCBRow
{
return
&
DposCBRow
{
DposCycleBoundaryInfo
:
&
DposCycleBoundaryInfo
{}}
return
&
DposCBRow
{
DposCycleBoundaryInfo
:
&
DposCycleBoundaryInfo
{}}
}
}
...
...
plugin/dapp/dposvote/types/types.go
View file @
697b6b89
...
@@ -17,7 +17,7 @@ func init() {
...
@@ -17,7 +17,7 @@ func init() {
types
.
RegisterDappFork
(
DPosX
,
"Enable"
,
0
)
types
.
RegisterDappFork
(
DPosX
,
"Enable"
,
0
)
}
}
//
Gues
sType struct
//
DPo
sType struct
type
DPosType
struct
{
type
DPosType
struct
{
types
.
ExecTypeBase
types
.
ExecTypeBase
}
}
...
...
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