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
4f86f81b
Commit
4f86f81b
authored
Aug 09, 2019
by
张振华
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update
parent
2cb03695
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
625 additions
and
159 deletions
+625
-159
consensus_state.go
plugin/consensus/dpos/consensus_state.go
+8
-8
dpos_msg.proto
plugin/consensus/dpos/proto/dpos_msg.proto
+8
-0
state_machine.go
plugin/consensus/dpos/state_machine.go
+59
-82
state_machine_test.go
plugin/consensus/dpos/state_machine_test.go
+9
-9
dpos_msg.pb.go
plugin/consensus/dpos/types/dpos_msg.pb.go
+88
-36
genesis_test.go
plugin/consensus/dpos/types/genesis_test.go
+120
-0
msg.go
plugin/consensus/dpos/types/msg.go
+1
-0
priv_validator.go
plugin/consensus/dpos/types/priv_validator.go
+22
-24
priv_validator_test.go
plugin/consensus/dpos/types/priv_validator_test.go
+310
-0
No files found.
plugin/consensus/dpos/consensus_state.go
View file @
4f86f81b
...
@@ -291,7 +291,7 @@ func (cs *ConsensusState) handleMsg(mi MsgInfo) {
...
@@ -291,7 +291,7 @@ func (cs *ConsensusState) handleMsg(mi MsgInfo) {
cs
.
dposState
.
recvNotify
(
cs
,
msg
)
cs
.
dposState
.
recvNotify
(
cs
,
msg
)
case
*
dpostype
.
DPosVoteReply
:
case
*
dpostype
.
DPosVoteReply
:
cs
.
dposState
.
recvVoteReply
(
cs
,
msg
)
cs
.
dposState
.
recvVoteReply
(
cs
,
msg
)
case
*
d
ty
.
Dp
osCBInfo
:
case
*
d
postype
.
DP
osCBInfo
:
cs
.
dposState
.
recvCBInfo
(
cs
,
msg
)
cs
.
dposState
.
recvCBInfo
(
cs
,
msg
)
default
:
default
:
dposlog
.
Error
(
"Unknown msg type"
,
"msg"
,
msg
.
String
(),
"peerid"
,
peerID
,
"peerip"
,
peerIP
)
dposlog
.
Error
(
"Unknown msg type"
,
"msg"
,
msg
.
String
(),
"peerid"
,
peerID
,
"peerip"
,
peerIP
)
...
@@ -602,14 +602,14 @@ func (cs *ConsensusState) Init() {
...
@@ -602,14 +602,14 @@ func (cs *ConsensusState) Init() {
// InitCycleBoundaryInfo method
// InitCycleBoundaryInfo method
func
(
cs
*
ConsensusState
)
InitCycleBoundaryInfo
(
task
Task
){
func
(
cs
*
ConsensusState
)
InitCycleBoundaryInfo
(
task
Task
){
info
,
err
:=
cs
.
QueryCycleBoundaryInfo
(
task
.
c
ycle
)
info
,
err
:=
cs
.
QueryCycleBoundaryInfo
(
task
.
C
ycle
)
if
err
==
nil
&&
info
!=
nil
{
if
err
==
nil
&&
info
!=
nil
{
//cs.cycleBoundaryMap[task.cycle] = info
//cs.cycleBoundaryMap[task.cycle] = info
cs
.
UpdateCBInfo
(
info
)
cs
.
UpdateCBInfo
(
info
)
return
return
}
}
info
,
err
=
cs
.
QueryCycleBoundaryInfo
(
task
.
c
ycle
-
1
)
info
,
err
=
cs
.
QueryCycleBoundaryInfo
(
task
.
C
ycle
-
1
)
if
err
==
nil
&&
info
!=
nil
{
if
err
==
nil
&&
info
!=
nil
{
//cs.cycleBoundaryMap[task.cycle] = info
//cs.cycleBoundaryMap[task.cycle] = info
cs
.
UpdateCBInfo
(
info
)
cs
.
UpdateCBInfo
(
info
)
...
@@ -737,7 +737,7 @@ func (cs *ConsensusState) SendCBTx(info *dty.DposCBInfo) bool {
...
@@ -737,7 +737,7 @@ func (cs *ConsensusState) SendCBTx(info *dty.DposCBInfo) bool {
dposlog
.
Error
(
"SignCBInfo failed."
,
"err"
,
err
)
dposlog
.
Error
(
"SignCBInfo failed."
,
"err"
,
err
)
return
false
return
false
}
else
{
}
else
{
info
.
Signature
=
sig
info
.
Signature
=
hex
.
EncodeToString
(
sig
.
Bytes
())
tx
,
err
:=
cs
.
client
.
CreateRecordCBTx
(
info
)
tx
,
err
:=
cs
.
client
.
CreateRecordCBTx
(
info
)
if
err
!=
nil
{
if
err
!=
nil
{
dposlog
.
Error
(
"CreateRecordCBTx failed."
,
"err"
,
err
)
dposlog
.
Error
(
"CreateRecordCBTx failed."
,
"err"
,
err
)
...
@@ -824,14 +824,14 @@ func (cs *ConsensusState) QueryVrf(pubkey []byte, cycle int64) (info *dty.VrfInf
...
@@ -824,14 +824,14 @@ func (cs *ConsensusState) QueryVrf(pubkey []byte, cycle int64) (info *dty.VrfInf
// InitCycleVrfInfo method
// InitCycleVrfInfo method
func
(
cs
*
ConsensusState
)
InitCycleVrfInfo
(
task
Task
){
func
(
cs
*
ConsensusState
)
InitCycleVrfInfo
(
task
Task
){
info
,
err
:=
cs
.
QueryVrf
(
cs
.
privValidator
.
GetPubKey
()
.
Bytes
(),
task
.
c
ycle
)
info
,
err
:=
cs
.
QueryVrf
(
cs
.
privValidator
.
GetPubKey
()
.
Bytes
(),
task
.
C
ycle
)
if
err
==
nil
&&
info
!=
nil
{
if
err
==
nil
&&
info
!=
nil
{
//cs.cycleBoundaryMap[task.cycle] = info
//cs.cycleBoundaryMap[task.cycle] = info
cs
.
UpdateVrfInfo
(
info
)
cs
.
UpdateVrfInfo
(
info
)
return
return
}
}
info
,
err
=
cs
.
QueryVrf
(
cs
.
privValidator
.
GetPubKey
()
.
Bytes
(),
task
.
c
ycle
-
1
)
info
,
err
=
cs
.
QueryVrf
(
cs
.
privValidator
.
GetPubKey
()
.
Bytes
(),
task
.
C
ycle
-
1
)
if
err
==
nil
&&
info
!=
nil
{
if
err
==
nil
&&
info
!=
nil
{
//cs.cycleBoundaryMap[task.cycle] = info
//cs.cycleBoundaryMap[task.cycle] = info
cs
.
UpdateVrfInfo
(
info
)
cs
.
UpdateVrfInfo
(
info
)
...
@@ -909,10 +909,10 @@ func (cs *ConsensusState) QueryVrfs(set *ttypes.ValidatorSet, cycle int64) (info
...
@@ -909,10 +909,10 @@ func (cs *ConsensusState) QueryVrfs(set *ttypes.ValidatorSet, cycle int64) (info
// InitCycleVrfInfo method
// InitCycleVrfInfo method
func
(
cs
*
ConsensusState
)
InitCycleVrfInfos
(
task
Task
){
func
(
cs
*
ConsensusState
)
InitCycleVrfInfos
(
task
Task
){
infos
,
err
:=
cs
.
QueryVrfs
(
cs
.
validatorMgr
.
Validators
,
task
.
c
ycle
-
1
)
infos
,
err
:=
cs
.
QueryVrfs
(
cs
.
validatorMgr
.
Validators
,
task
.
C
ycle
-
1
)
if
err
==
nil
&&
infos
!=
nil
{
if
err
==
nil
&&
infos
!=
nil
{
//cs.cycleBoundaryMap[task.cycle] = info
//cs.cycleBoundaryMap[task.cycle] = info
cs
.
UpdateVrfInfos
(
task
.
c
ycle
,
infos
)
cs
.
UpdateVrfInfos
(
task
.
C
ycle
,
infos
)
}
}
return
return
...
...
plugin/consensus/dpos/proto/dpos_msg.proto
View file @
4f86f81b
...
@@ -54,3 +54,10 @@ message DPosNotify {
...
@@ -54,3 +54,10 @@ message DPosNotify {
bytes
signature
=
7
;
//通知节点的签名
bytes
signature
=
7
;
//通知节点的签名
}
}
message
DPosCBInfo
{
int64
cycle
=
1
;
int64
stopHeight
=
2
;
string
stopHash
=
3
;
string
pubkey
=
4
;
string
signature
=
5
;
}
\ No newline at end of file
plugin/consensus/dpos/state_machine.go
View file @
4f86f81b
This diff is collapsed.
Click to expand it.
plugin/consensus/dpos/state_machine_test.go
View file @
4f86f81b
...
@@ -29,17 +29,17 @@ func setParams(delegateNum int64, blockInterval int64, continueBlockNum int64) {
...
@@ -29,17 +29,17 @@ func setParams(delegateNum int64, blockInterval int64, continueBlockNum int64) {
func
printTask
(
now
int64
,
task
*
Task
)
{
func
printTask
(
now
int64
,
task
*
Task
)
{
fmt
.
Printf
(
"now:%v|cycleStart:%v|cycleStop:%v|periodStart:%v|periodStop:%v|blockStart:%v|blockStop:%v|nodeId:%v
\n
"
,
fmt
.
Printf
(
"now:%v|cycleStart:%v|cycleStop:%v|periodStart:%v|periodStop:%v|blockStart:%v|blockStop:%v|nodeId:%v
\n
"
,
now
,
now
,
task
.
c
ycleStart
,
task
.
C
ycleStart
,
task
.
c
ycleStop
,
task
.
C
ycleStop
,
task
.
p
eriodStart
,
task
.
P
eriodStart
,
task
.
p
eriodStop
,
task
.
P
eriodStop
,
task
.
b
lockStart
,
task
.
B
lockStart
,
task
.
b
lockStop
,
task
.
B
lockStop
,
task
.
n
odeID
)
task
.
N
odeID
)
}
}
func
assertTask
(
task
*
Task
,
t
*
testing
.
T
)
{
func
assertTask
(
task
*
Task
,
t
*
testing
.
T
)
{
assert
.
Equal
(
t
,
true
,
task
.
nodeID
>=
0
&&
task
.
n
odeID
<
dposDelegateNum
)
assert
.
Equal
(
t
,
true
,
task
.
NodeID
>=
0
&&
task
.
N
odeID
<
dposDelegateNum
)
assert
.
Equal
(
t
,
true
,
task
.
cycleStart
<=
task
.
periodStart
&&
task
.
periodStart
<=
task
.
blockStart
&&
task
.
blockStop
<=
task
.
periodStop
&&
task
.
periodStop
<=
task
.
c
ycleStop
)
assert
.
Equal
(
t
,
true
,
task
.
CycleStart
<=
task
.
PeriodStart
&&
task
.
PeriodStart
<=
task
.
BlockStart
&&
task
.
BlockStop
<=
task
.
PeriodStop
&&
task
.
PeriodStop
<=
task
.
C
ycleStop
)
}
}
func
TestDecideTaskByTime
(
t
*
testing
.
T
)
{
func
TestDecideTaskByTime
(
t
*
testing
.
T
)
{
...
...
plugin/consensus/dpos/types/dpos_msg.pb.go
View file @
4f86f81b
...
@@ -14,6 +14,7 @@ It has these top-level messages:
...
@@ -14,6 +14,7 @@ It has these top-level messages:
DPosVote
DPosVote
DPosVoteReply
DPosVoteReply
DPosNotify
DPosNotify
DPosCBInfo
*/
*/
package
types
package
types
...
@@ -338,6 +339,54 @@ func (m *DPosNotify) GetSignature() []byte {
...
@@ -338,6 +339,54 @@ func (m *DPosNotify) GetSignature() []byte {
return
nil
return
nil
}
}
type
DPosCBInfo
struct
{
Cycle
int64
`protobuf:"varint,1,opt,name=cycle" json:"cycle,omitempty"`
StopHeight
int64
`protobuf:"varint,2,opt,name=stopHeight" json:"stopHeight,omitempty"`
StopHash
string
`protobuf:"bytes,3,opt,name=stopHash" json:"stopHash,omitempty"`
Pubkey
string
`protobuf:"bytes,4,opt,name=pubkey" json:"pubkey,omitempty"`
Signature
string
`protobuf:"bytes,5,opt,name=signature" json:"signature,omitempty"`
}
func
(
m
*
DPosCBInfo
)
Reset
()
{
*
m
=
DPosCBInfo
{}
}
func
(
m
*
DPosCBInfo
)
String
()
string
{
return
proto
.
CompactTextString
(
m
)
}
func
(
*
DPosCBInfo
)
ProtoMessage
()
{}
func
(
*
DPosCBInfo
)
Descriptor
()
([]
byte
,
[]
int
)
{
return
fileDescriptor0
,
[]
int
{
6
}
}
func
(
m
*
DPosCBInfo
)
GetCycle
()
int64
{
if
m
!=
nil
{
return
m
.
Cycle
}
return
0
}
func
(
m
*
DPosCBInfo
)
GetStopHeight
()
int64
{
if
m
!=
nil
{
return
m
.
StopHeight
}
return
0
}
func
(
m
*
DPosCBInfo
)
GetStopHash
()
string
{
if
m
!=
nil
{
return
m
.
StopHash
}
return
""
}
func
(
m
*
DPosCBInfo
)
GetPubkey
()
string
{
if
m
!=
nil
{
return
m
.
Pubkey
}
return
""
}
func
(
m
*
DPosCBInfo
)
GetSignature
()
string
{
if
m
!=
nil
{
return
m
.
Signature
}
return
""
}
func
init
()
{
func
init
()
{
proto
.
RegisterType
((
*
CycleBoundaryInfo
)(
nil
),
"types.CycleBoundaryInfo"
)
proto
.
RegisterType
((
*
CycleBoundaryInfo
)(
nil
),
"types.CycleBoundaryInfo"
)
proto
.
RegisterType
((
*
SuperNode
)(
nil
),
"types.SuperNode"
)
proto
.
RegisterType
((
*
SuperNode
)(
nil
),
"types.SuperNode"
)
...
@@ -345,45 +394,48 @@ func init() {
...
@@ -345,45 +394,48 @@ func init() {
proto
.
RegisterType
((
*
DPosVote
)(
nil
),
"types.DPosVote"
)
proto
.
RegisterType
((
*
DPosVote
)(
nil
),
"types.DPosVote"
)
proto
.
RegisterType
((
*
DPosVoteReply
)(
nil
),
"types.DPosVoteReply"
)
proto
.
RegisterType
((
*
DPosVoteReply
)(
nil
),
"types.DPosVoteReply"
)
proto
.
RegisterType
((
*
DPosNotify
)(
nil
),
"types.DPosNotify"
)
proto
.
RegisterType
((
*
DPosNotify
)(
nil
),
"types.DPosNotify"
)
proto
.
RegisterType
((
*
DPosCBInfo
)(
nil
),
"types.DPosCBInfo"
)
}
}
func
init
()
{
proto
.
RegisterFile
(
"dpos_msg.proto"
,
fileDescriptor0
)
}
func
init
()
{
proto
.
RegisterFile
(
"dpos_msg.proto"
,
fileDescriptor0
)
}
var
fileDescriptor0
=
[]
byte
{
var
fileDescriptor0
=
[]
byte
{
// 558 bytes of a gzipped FileDescriptorProto
// 587 bytes of a gzipped FileDescriptorProto
0x1f
,
0x8b
,
0x08
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x02
,
0xff
,
0x74
,
0x54
,
0xdd
,
0x8a
,
0x13
,
0x31
,
0x1f
,
0x8b
,
0x08
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x02
,
0xff
,
0xac
,
0x54
,
0xdd
,
0x8a
,
0xd3
,
0x4c
,
0x14
,
0xa6
,
0x3b
,
0xfd
,
0x3d
,
0xfd
,
0xd9
,
0xdd
,
0x20
,
0x12
,
0x44
,
0xa4
,
0x8c
,
0x22
,
0x45
,
0xa5
,
0x18
,
0x26
,
0x9b
,
0xb6
,
0xdb
,
0xbc
,
0xfd
,
0xd9
,
0xdd
,
0xe1
,
0xe3
,
0x63
,
0x10
,
0x91
,
0x12
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0xc9
,
0xc4
,
0xee
,
0xcc
,
0x1e
,
0xe5
,
0xf5
,
0x73
,
0xcf
,
0x3d
,
0xa3
,
0xff
,
0xef
,
0x2c
,
0x91
,
0x99
,
0x89
,
0x86
,
0xe6
,
0x9a
,
0x15
,
0x46
,
0x63
,
0xbd
,
0x33
,
0xa5
,
0x4e
,
0xfb
,
0x4c
,
0x2f
,
0x61
,
0x0b
,
0x25
,
0xac
,
0xd3
,
0xe4
,
0x1d
,
0x5c
,
0x7a
,
0xaa
,
0x10
,
0x7d
,
0x94
,
0xd7
,
0xcf
,
0xfe
,
0x2e
,
0x4d
,
0xda
,
0x67
,
0x7a
,
0x09
,
0xdb
,
0x46
,
0xc2
,
0x26
,
0x8d
,
0xb1
,
0x8d
,
0x07
,
0x1f
,
0x07
,
0x0e
,
0x55
,
0xec
,
0xd4
,
0x54
,
0x7c
,
0x68
,
0xe3
,
0x4f
,
0xef
,
0xc3
,
0x5e
,
0xc1
,
0x85
,
0xa7
,
0x4a
,
0x11
,
0x3b
,
0xe6
,
0xe0
,
0xc3
,
0xc0
,
0xbe
,
0x8a
,
0xa7
,
0x4d
,
0x15
,
0xff
,
0x00
,
0x00
,
0x00
,
0xff
,
0xff
,
0x1d
,
0x49
,
0x1a
,
0xb6
,
0x06
,
0x05
,
0x00
,
0x00
,
0x7f
,
0x05
,
0x56
,
0x0f
,
0x67
,
0xf7
,
0x7f
,
0xfe
,
0xc0
,
0xb9
,
0x97
,
0xeb
,
0x3b
,
0x2d
,
0xcc
,
0xdc
,
0x11
,
0x71
,
0xe8
,
0x70
,
0xb9
,
0x51
,
0xed
,
0x5a
,
0xf7
,
0x1d
,
0xf3
,
0x16
,
0xbf
,
0xf9
,
0x1b
,
0x00
,
0x00
,
0xff
,
0xff
,
0xea
,
0x85
,
0x23
,
0x04
,
0x9d
,
0x05
,
0x00
,
0x00
,
}
}
plugin/consensus/dpos/types/genesis_test.go
0 → 100644
View file @
4f86f81b
package
types
import
(
"bytes"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"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}`
)
func
init
(){
//为了使用VRF,需要使用SECP256K1体系的公私钥
cr
,
err
:=
crypto
.
New
(
types
.
GetSignName
(
""
,
types
.
SECP256K1
))
if
err
!=
nil
{
panic
(
"init ConsensusCrypto failed."
)
return
}
ConsensusCrypto
=
cr
}
func
TestGenesisDocFromFile
(
t
*
testing
.
T
)
{
genDoc
,
err
:=
GenesisDocFromFile
(
"./genesis.json"
)
require
.
NotNil
(
t
,
err
)
require
.
Nil
(
t
,
genDoc
)
genDoc
,
err
=
GenesisDocFromFile
(
"../genesis.json"
)
require
.
NotNil
(
t
,
genDoc
)
require
.
Nil
(
t
,
err
)
}
func
TestGenesisDocFromJSON
(
t
*
testing
.
T
)
{
genDoc
,
err
:=
GenesisDocFromJSON
([]
byte
(
genesis_file
))
require
.
NotNil
(
t
,
genDoc
)
require
.
Nil
(
t
,
err
)
assert
.
True
(
t
,
genDoc
.
ChainID
==
"chain33-Z2cgFj"
)
assert
.
True
(
t
,
genDoc
.
AppHash
==
nil
)
assert
.
True
(
t
,
len
(
genDoc
.
Validators
)
==
3
)
genDoc
,
err
=
GenesisDocFromJSON
([]
byte
(
err_genesis_file
))
require
.
NotNil
(
t
,
err
)
require
.
Nil
(
t
,
genDoc
)
}
func
TestSaveAs
(
t
*
testing
.
T
)
{
genDoc
,
err
:=
GenesisDocFromJSON
([]
byte
(
genesis_file
))
require
.
NotNil
(
t
,
genDoc
)
require
.
Nil
(
t
,
err
)
assert
.
True
(
t
,
genDoc
.
ChainID
==
"chain33-Z2cgFj"
)
assert
.
True
(
t
,
genDoc
.
AppHash
==
nil
)
assert
.
True
(
t
,
len
(
genDoc
.
Validators
)
==
3
)
err
=
genDoc
.
SaveAs
(
"./tmp_genesis.json"
)
require
.
Nil
(
t
,
err
)
genDoc2
,
err
:=
GenesisDocFromFile
(
"./tmp_genesis.json"
)
require
.
NotNil
(
t
,
genDoc2
)
require
.
Nil
(
t
,
err
)
assert
.
True
(
t
,
genDoc
.
ChainID
==
genDoc2
.
ChainID
)
assert
.
True
(
t
,
genDoc
.
GenesisTime
==
genDoc2
.
GenesisTime
)
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
]
.
PubKey
.
Data
==
genDoc2
.
Validators
[
0
]
.
PubKey
.
Data
)
assert
.
True
(
t
,
genDoc
.
Validators
[
0
]
.
PubKey
.
Kind
==
genDoc2
.
Validators
[
0
]
.
PubKey
.
Kind
)
assert
.
True
(
t
,
genDoc
.
Validators
[
1
]
.
Name
==
genDoc2
.
Validators
[
1
]
.
Name
)
assert
.
True
(
t
,
genDoc
.
Validators
[
1
]
.
PubKey
.
Data
==
genDoc2
.
Validators
[
1
]
.
PubKey
.
Data
)
assert
.
True
(
t
,
genDoc
.
Validators
[
1
]
.
PubKey
.
Kind
==
genDoc2
.
Validators
[
1
]
.
PubKey
.
Kind
)
assert
.
True
(
t
,
genDoc
.
Validators
[
2
]
.
Name
==
genDoc2
.
Validators
[
2
]
.
Name
)
assert
.
True
(
t
,
genDoc
.
Validators
[
2
]
.
PubKey
.
Data
==
genDoc2
.
Validators
[
2
]
.
PubKey
.
Data
)
assert
.
True
(
t
,
genDoc
.
Validators
[
2
]
.
PubKey
.
Kind
==
genDoc2
.
Validators
[
2
]
.
PubKey
.
Kind
)
err
=
os
.
Remove
(
"./tmp_genesis.json"
)
require
.
Nil
(
t
,
err
)
}
func
TestValidateAndComplete
(
t
*
testing
.
T
)
{
genDoc
,
err
:=
GenesisDocFromJSON
([]
byte
(
genesis_file
))
require
.
NotNil
(
t
,
genDoc
)
require
.
Nil
(
t
,
err
)
tt
:=
genDoc
.
GenesisTime
setSize
:=
len
(
genDoc
.
Validators
)
err
=
genDoc
.
ValidateAndComplete
()
assert
.
True
(
t
,
tt
==
genDoc
.
GenesisTime
)
require
.
Nil
(
t
,
err
)
assert
.
True
(
t
,
setSize
==
len
(
genDoc
.
Validators
))
vals
:=
genDoc
.
Validators
genDoc
.
Validators
=
nil
err
=
genDoc
.
ValidateAndComplete
()
require
.
NotNil
(
t
,
err
)
genDoc
.
Validators
=
vals
genDoc
.
ChainID
=
""
err
=
genDoc
.
ValidateAndComplete
()
require
.
NotNil
(
t
,
err
)
}
func
TestValidatorHash
(
t
*
testing
.
T
)
{
genDoc
,
err
:=
GenesisDocFromJSON
([]
byte
(
genesis_file
))
require
.
NotNil
(
t
,
genDoc
)
require
.
Nil
(
t
,
err
)
hash
:=
genDoc
.
ValidatorHash
()
assert
.
True
(
t
,
len
(
hash
)
>
0
)
}
\ No newline at end of file
plugin/consensus/dpos/types/msg.go
View file @
4f86f81b
...
@@ -32,6 +32,7 @@ func InitMessageMap() {
...
@@ -32,6 +32,7 @@ func InitMessageMap() {
VoteID
:
reflect
.
TypeOf
(
DPosVote
{}),
VoteID
:
reflect
.
TypeOf
(
DPosVote
{}),
VoteReplyID
:
reflect
.
TypeOf
(
DPosVoteReply
{}),
VoteReplyID
:
reflect
.
TypeOf
(
DPosVoteReply
{}),
NotifyID
:
reflect
.
TypeOf
(
DPosNotify
{}),
NotifyID
:
reflect
.
TypeOf
(
DPosNotify
{}),
CBInfoID
:
reflect
.
TypeOf
(
DPosCBInfo
{}),
}
}
}
}
...
...
plugin/consensus/dpos/types/priv_validator.go
View file @
4f86f81b
...
@@ -34,7 +34,7 @@ type PrivValidator interface {
...
@@ -34,7 +34,7 @@ type PrivValidator interface {
SignVote
(
chainID
string
,
vote
*
Vote
)
error
SignVote
(
chainID
string
,
vote
*
Vote
)
error
SignNotify
(
chainID
string
,
notify
*
Notify
)
error
SignNotify
(
chainID
string
,
notify
*
Notify
)
error
SignMsg
(
msg
[]
byte
)
(
sig
string
,
err
error
)
SignMsg
(
msg
[]
byte
)
(
sig
crypto
.
Signature
,
err
error
)
SignTx
(
tx
*
types
.
Transaction
)
SignTx
(
tx
*
types
.
Transaction
)
VrfEvaluate
(
input
[]
byte
)
(
hash
[
32
]
byte
,
proof
[]
byte
)
VrfEvaluate
(
input
[]
byte
)
(
hash
[
32
]
byte
,
proof
[]
byte
)
VrfProof
(
pubkey
[]
byte
,
input
[]
byte
,
hash
[
32
]
byte
,
proof
[]
byte
)
bool
VrfProof
(
pubkey
[]
byte
,
input
[]
byte
,
hash
[
32
]
byte
,
proof
[]
byte
)
bool
...
@@ -46,8 +46,8 @@ type PrivValidator interface {
...
@@ -46,8 +46,8 @@ type PrivValidator interface {
type
PrivValidatorFS
struct
{
type
PrivValidatorFS
struct
{
Address
string
`json:"address"`
Address
string
`json:"address"`
PubKey
KeyText
`json:"pub_key"`
PubKey
KeyText
`json:"pub_key"`
LastSignature
*
KeyText
`json:"last_signature,omitempty"`
// so we dont lose signatures
//
LastSignature *KeyText `json:"last_signature,omitempty"` // so we dont lose signatures
LastSignBytes
string
`json:"last_signbytes,omitempty"`
// so we dont lose signatures
//
LastSignBytes string `json:"last_signbytes,omitempty"` // so we dont lose signatures
// PrivKey should be empty if a Signer other than the default is being used.
// PrivKey should be empty if a Signer other than the default is being used.
PrivKey
KeyText
`json:"priv_key"`
PrivKey
KeyText
`json:"priv_key"`
...
@@ -57,8 +57,8 @@ type PrivValidatorFS struct {
...
@@ -57,8 +57,8 @@ type PrivValidatorFS struct {
type
PrivValidatorImp
struct
{
type
PrivValidatorImp
struct
{
Address
[]
byte
Address
[]
byte
PubKey
crypto
.
PubKey
PubKey
crypto
.
PubKey
LastSignature
crypto
.
Signature
//
LastSignature crypto.Signature
LastSignBytes
[]
byte
//
LastSignBytes []byte
// PrivKey should be empty if a Signer other than the default is being used.
// PrivKey should be empty if a Signer other than the default is being used.
PrivKey
crypto
.
PrivKey
PrivKey
crypto
.
PrivKey
...
@@ -130,6 +130,7 @@ func PubKeyFromString(pubkeystring string) (crypto.PubKey, error) {
...
@@ -130,6 +130,7 @@ func PubKeyFromString(pubkeystring string) (crypto.PubKey, error) {
}
}
// SignatureFromString ...
// SignatureFromString ...
/*
func SignatureFromString(sigString string) (crypto.Signature, error) {
func SignatureFromString(sigString string) (crypto.Signature, error) {
sigbyte, err := hex.DecodeString(sigString)
sigbyte, err := hex.DecodeString(sigString)
if err != nil {
if err != nil {
...
@@ -141,7 +142,7 @@ func SignatureFromString(sigString string) (crypto.Signature, error) {
...
@@ -141,7 +142,7 @@ func SignatureFromString(sigString string) (crypto.Signature, error) {
}
}
return sig, nil
return sig, nil
}
}
*/
// GenPrivValidatorImp generates a new validator with randomly generated private key
// GenPrivValidatorImp generates a new validator with randomly generated private key
// and sets the filePath, but does not call Save().
// and sets the filePath, but does not call Save().
func
GenPrivValidatorImp
(
filePath
string
)
*
PrivValidatorImp
{
func
GenPrivValidatorImp
(
filePath
string
)
*
PrivValidatorImp
{
...
@@ -221,6 +222,7 @@ func LoadPrivValidatorFSWithSigner(filePath string, signerFunc func(PrivValidato
...
@@ -221,6 +222,7 @@ func LoadPrivValidatorFSWithSigner(filePath string, signerFunc func(PrivValidato
}
}
privValImp
.
PubKey
=
pubKey
privValImp
.
PubKey
=
pubKey
/*
if len(privVal.LastSignBytes) != 0 {
if len(privVal.LastSignBytes) != 0 {
tmp, err = hex.DecodeString(privVal.LastSignBytes)
tmp, err = hex.DecodeString(privVal.LastSignBytes)
if err != nil {
if err != nil {
...
@@ -237,7 +239,7 @@ func LoadPrivValidatorFSWithSigner(filePath string, signerFunc func(PrivValidato
...
@@ -237,7 +239,7 @@ func LoadPrivValidatorFSWithSigner(filePath string, signerFunc func(PrivValidato
} else {
} else {
privValImp.LastSignature = nil
privValImp.LastSignature = nil
}
}
*/
privValImp
.
filePath
=
filePath
privValImp
.
filePath
=
filePath
privValImp
.
Signer
=
signerFunc
(
privValImp
)
privValImp
.
Signer
=
signerFunc
(
privValImp
)
return
privValImp
return
privValImp
...
@@ -258,10 +260,11 @@ func (pv *PrivValidatorImp) save() {
...
@@ -258,10 +260,11 @@ func (pv *PrivValidatorImp) save() {
privValFS
:=
&
PrivValidatorFS
{
privValFS
:=
&
PrivValidatorFS
{
Address
:
addr
,
Address
:
addr
,
LastSignature
:
nil
,
//
LastSignature: nil,
}
}
privValFS
.
PrivKey
=
KeyText
{
Kind
:
"secp256k1"
,
Data
:
Fmt
(
"%X"
,
pv
.
PrivKey
.
Bytes
()[
:
])}
privValFS
.
PrivKey
=
KeyText
{
Kind
:
"secp256k1"
,
Data
:
Fmt
(
"%X"
,
pv
.
PrivKey
.
Bytes
()[
:
])}
privValFS
.
PubKey
=
KeyText
{
Kind
:
"secp256k1"
,
Data
:
pv
.
PubKey
.
KeyString
()}
privValFS
.
PubKey
=
KeyText
{
Kind
:
"secp256k1"
,
Data
:
pv
.
PubKey
.
KeyString
()}
/*
if len(pv.LastSignBytes) != 0 {
if len(pv.LastSignBytes) != 0 {
tmp := Fmt("%X", pv.LastSignBytes[:])
tmp := Fmt("%X", pv.LastSignBytes[:])
privValFS.LastSignBytes = tmp
privValFS.LastSignBytes = tmp
...
@@ -270,6 +273,7 @@ func (pv *PrivValidatorImp) save() {
...
@@ -270,6 +273,7 @@ func (pv *PrivValidatorImp) save() {
sig := Fmt("%X", pv.LastSignature.Bytes()[:])
sig := Fmt("%X", pv.LastSignature.Bytes()[:])
privValFS.LastSignature = &KeyText{Kind: "ed25519", Data: sig}
privValFS.LastSignature = &KeyText{Kind: "ed25519", Data: sig}
}
}
*/
jsonBytes
,
err
:=
json
.
Marshal
(
privValFS
)
jsonBytes
,
err
:=
json
.
Marshal
(
privValFS
)
if
err
!=
nil
{
if
err
!=
nil
{
// `@; BOOM!!!
// `@; BOOM!!!
...
@@ -285,8 +289,8 @@ func (pv *PrivValidatorImp) save() {
...
@@ -285,8 +289,8 @@ func (pv *PrivValidatorImp) save() {
// Reset resets all fields in the PrivValidatorFS.
// Reset resets all fields in the PrivValidatorFS.
// NOTE: Unsafe!
// NOTE: Unsafe!
func
(
pv
*
PrivValidatorImp
)
Reset
()
{
func
(
pv
*
PrivValidatorImp
)
Reset
()
{
pv
.
LastSignature
=
nil
//
pv.LastSignature = nil
pv
.
LastSignBytes
=
nil
//
pv.LastSignBytes = nil
pv
.
Save
()
pv
.
Save
()
}
}
...
@@ -323,21 +327,13 @@ func (pv *PrivValidatorImp) SignNotify(chainID string, notify *Notify) error {
...
@@ -323,21 +327,13 @@ func (pv *PrivValidatorImp) SignNotify(chainID string, notify *Notify) error {
}
}
// SignCBInfo signs a canonical representation of the DposCBInfo, Implements PrivValidator.
// SignCBInfo signs a canonical representation of the DposCBInfo, Implements PrivValidator.
func
(
pv
*
PrivValidatorImp
)
SignMsg
(
msg
[]
byte
)
(
sig
string
,
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
()
buf
:=
new
(
bytes
.
Buffer
)
signature
:=
pv
.
PrivKey
.
Sign
(
msg
)
//sig = hex.EncodeToString(signature.Bytes())
_
,
err
=
buf
.
Write
(
msg
)
return
signature
,
nil
if
err
!=
nil
{
return
""
,
errors
.
New
(
Fmt
(
"Error write buffer: %v"
,
err
))
}
signature
:=
pv
.
PrivKey
.
Sign
(
buf
.
Bytes
())
sig
=
hex
.
EncodeToString
(
signature
.
Bytes
())
return
sig
,
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
){
...
@@ -376,12 +372,14 @@ func (pv *PrivValidatorImp) VrfProof(pubkey []byte, input []byte, hash [32]byte,
...
@@ -376,12 +372,14 @@ func (pv *PrivValidatorImp) VrfProof(pubkey []byte, input []byte, hash [32]byte,
}
}
// Persist height/round/step and signature
// Persist height/round/step and signature
/*
func (pv *PrivValidatorImp) saveSigned(signBytes []byte, sig crypto.Signature) {
func (pv *PrivValidatorImp) saveSigned(signBytes []byte, sig crypto.Signature) {
pv
.
LastSignature
=
sig
//
pv.LastSignature = sig
pv
.
LastSignBytes
=
signBytes
//
pv.LastSignBytes = signBytes
pv.save()
pv.save()
}
}
*/
// String returns a string representation of the PrivValidatorImp.
// String returns a string representation of the PrivValidatorImp.
func
(
pv
*
PrivValidatorImp
)
String
()
string
{
func
(
pv
*
PrivValidatorImp
)
String
()
string
{
...
...
plugin/consensus/dpos/types/priv_validator_test.go
0 → 100644
View file @
4f86f81b
package
types
import
(
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/33cn/chain33/common/crypto"
"github.com/33cn/chain33/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"os"
"sort"
"strings"
"testing"
"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"}}`
strAddr
=
"2FA286246F0222C4FF93210E91AECE0C66723F15"
strPubkey
=
"03EF0E1D3112CF571743A3318125EDE2E52A4EB904BCBAA4B1F75020C2846A7EB4"
addr1
=
"79F9608B6826762CACCA843E81AE86837ABFFB21"
addr2
=
"3480088E35099CBA75958DAE7A364A8AAD2C1BD0"
addr3
=
"9FF8678DBDA4EAE2F999CBFBCBD8F5F3FC47FBAE"
addr4
=
"70A51AD9777EF1F97250F7E4C156D8637BC7143C"
)
func
init
(){
//为了使用VRF,需要使用SECP256K1体系的公私钥
cr
,
err
:=
crypto
.
New
(
types
.
GetSignName
(
""
,
types
.
SECP256K1
))
if
err
!=
nil
{
panic
(
"init ConsensusCrypto failed."
)
return
}
ConsensusCrypto
=
cr
}
func
save
(
filename
,
filecontent
string
)
{
f
,
err
:=
os
.
Create
(
filename
)
if
err
!=
nil
{
fmt
.
Println
(
"err = "
,
err
)
return
}
defer
f
.
Close
()
n
,
err
:=
f
.
WriteString
(
filecontent
)
if
err
!=
nil
{
fmt
.
Println
(
"err = "
,
err
)
return
}
fmt
.
Println
(
"n="
,
n
,
" contentlen="
,
len
(
filecontent
))
}
func
remove
(
filename
string
)
{
os
.
Remove
(
filename
)
}
func
read
(
filename
string
)
bool
{
f
,
err
:=
os
.
Open
(
filename
)
if
err
!=
nil
{
fmt
.
Println
(
"err="
,
err
)
return
false
}
defer
f
.
Close
()
buf
:=
make
([]
byte
,
1024
*
2
)
_
,
err1
:=
f
.
Read
(
buf
)
if
err1
!=
nil
&&
err1
!=
io
.
EOF
{
fmt
.
Println
(
"err1="
,
err1
)
return
false
}
//fmt.Println("buf=",string(buf[:n]))
return
true
}
func
TestLoadOrGenPrivValidatorFS
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
save
(
filename
,
priv_validator_file
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
require
.
NotNil
(
t
,
privValidator
)
assert
.
True
(
t
,
strings
.
EqualFold
(
strAddr
,
hex
.
EncodeToString
(
privValidator
.
GetAddress
())))
assert
.
True
(
t
,
strings
.
EqualFold
(
strPubkey
,
hex
.
EncodeToString
(
privValidator
.
GetPubKey
()
.
Bytes
())))
fmt
.
Println
(
privValidator
.
String
())
remove
(
filename
)
}
func
TestGenPrivValidatorImp
(
t
*
testing
.
T
)
{
filename
:=
"tmp_priv_validator2.json"
//save(filename, priv_validator_file)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
require
.
NotNil
(
t
,
privValidator
)
assert
.
True
(
t
,
true
==
read
(
filename
))
remove
(
filename
)
assert
.
True
(
t
,
false
==
read
(
filename
))
privValidator
.
Reset
()
assert
.
True
(
t
,
true
==
read
(
filename
))
assert
.
True
(
t
,
len
(
privValidator
.
GetPubKey
()
.
Bytes
())
>
0
)
assert
.
True
(
t
,
len
(
privValidator
.
GetAddress
())
>
0
)
remove
(
filename
)
}
func
TestPrivValidatorImpSort
(
t
*
testing
.
T
)
{
var
arr
[]
*
PrivValidatorImp
Addr1
,
_
:=
hex
.
DecodeString
(
addr1
)
Addr2
,
_
:=
hex
.
DecodeString
(
addr2
)
Addr3
,
_
:=
hex
.
DecodeString
(
addr3
)
Addr4
,
_
:=
hex
.
DecodeString
(
addr4
)
imp1
:=
&
PrivValidatorImp
{
Address
:
Addr1
,
}
arr
=
append
(
arr
,
imp1
)
imp2
:=
&
PrivValidatorImp
{
Address
:
Addr2
,
}
arr
=
append
(
arr
,
imp2
)
imp3
:=
&
PrivValidatorImp
{
Address
:
Addr3
,
}
arr
=
append
(
arr
,
imp3
)
imp4
:=
&
PrivValidatorImp
{
Address
:
Addr4
,
}
arr
=
append
(
arr
,
imp4
)
sort
.
Sort
(
PrivValidatorsByAddress
(
arr
))
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
(
addr1
,
hex
.
EncodeToString
(
arr
[
2
]
.
Address
)))
assert
.
True
(
t
,
strings
.
EqualFold
(
addr3
,
hex
.
EncodeToString
(
arr
[
3
]
.
Address
)))
}
func
TestSignAndVerifyVote
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
save
(
filename
,
priv_validator_file
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
now
:=
time
.
Now
()
.
Unix
()
//task := dpos.DecideTaskByTime(now)
//生成vote, 对于vote进行签名
voteItem
:=
&
VoteItem
{
VotedNodeAddress
:
privValidator
.
Address
,
VotedNodeIndex
:
int32
(
0
),
Cycle
:
100
,
CycleStart
:
18888
,
CycleStop
:
28888
,
PeriodStart
:
20000
,
PeriodStop
:
21000
,
Height
:
100
,
}
encode
,
err
:=
json
.
Marshal
(
voteItem
)
if
err
!=
nil
{
panic
(
"Marshal vote failed."
)
}
voteItem
.
VoteID
=
crypto
.
Ripemd160
(
encode
)
vote
:=
&
Vote
{
DPosVote
:
&
DPosVote
{
VoteItem
:
voteItem
,
VoteTimestamp
:
now
,
VoterNodeAddress
:
privValidator
.
GetAddress
(),
VoterNodeIndex
:
int32
(
0
),
},
}
assert
.
True
(
t
,
0
==
len
(
vote
.
Signature
))
chainID
:=
"test-chain-Ep9EcD"
privValidator
.
SignVote
(
chainID
,
vote
)
assert
.
True
(
t
,
0
<
len
(
vote
.
Signature
))
vote2
:=
vote
.
Copy
()
err
=
vote2
.
Verify
(
chainID
,
privValidator
.
PubKey
)
require
.
Nil
(
t
,
err
)
remove
(
filename
)
privValidator2
:=
LoadOrGenPrivValidatorFS
(
filename
)
require
.
NotNil
(
t
,
privValidator2
)
err
=
vote2
.
Verify
(
chainID
,
privValidator2
.
PubKey
)
require
.
NotNil
(
t
,
err
)
remove
(
filename
)
}
func
TestSignAndVerifyNotify
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
save
(
filename
,
priv_validator_file
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
now
:=
time
.
Now
()
.
Unix
()
//task := dpos.DecideTaskByTime(now)
//生成vote, 对于vote进行签名
voteItem
:=
&
VoteItem
{
VotedNodeAddress
:
privValidator
.
Address
,
VotedNodeIndex
:
int32
(
0
),
Cycle
:
100
,
CycleStart
:
18888
,
CycleStop
:
28888
,
PeriodStart
:
20000
,
PeriodStop
:
21000
,
Height
:
100
,
}
encode
,
err
:=
json
.
Marshal
(
voteItem
)
if
err
!=
nil
{
panic
(
"Marshal vote failed."
)
}
voteItem
.
VoteID
=
crypto
.
Ripemd160
(
encode
)
chainID
:=
"test-chain-Ep9EcD"
notify
:=
&
Notify
{
DPosNotify
:
&
DPosNotify
{
Vote
:
voteItem
,
HeightStop
:
200
,
HashStop
:
[]
byte
(
"abcdef121212"
),
NotifyTimestamp
:
now
,
NotifyNodeAddress
:
privValidator
.
GetAddress
(),
NotifyNodeIndex
:
int32
(
0
),
},
}
err
=
privValidator
.
SignNotify
(
chainID
,
notify
)
require
.
Nil
(
t
,
err
)
notify2
:=
notify
.
Copy
()
err
=
notify2
.
Verify
(
chainID
,
privValidator
.
PubKey
)
require
.
Nil
(
t
,
err
)
remove
(
filename
)
privValidator2
:=
LoadOrGenPrivValidatorFS
(
filename
)
require
.
NotNil
(
t
,
privValidator2
)
err
=
notify2
.
Verify
(
chainID
,
privValidator2
.
PubKey
)
require
.
NotNil
(
t
,
err
)
remove
(
filename
)
}
func
TestSignMsg
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
save
(
filename
,
priv_validator_file
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
byteCB
:=
[]
byte
(
"asdfadsasf"
)
sig
,
err
:=
privValidator
.
SignMsg
(
byteCB
)
require
.
Nil
(
t
,
err
)
assert
.
True
(
t
,
0
<
len
(
sig
.
Bytes
()))
remove
(
filename
)
}
func
TestVrf
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
save
(
filename
,
priv_validator_file
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
input
:=
[]
byte
(
"abcdefghijklmn"
)
hash
,
proof
:=
privValidator
.
VrfEvaluate
(
input
)
assert
.
True
(
t
,
32
==
len
(
hash
))
assert
.
True
(
t
,
0
<
len
(
proof
))
result
:=
privValidator
.
VrfProof
(
privValidator
.
PubKey
.
Bytes
(),
input
,
hash
,
proof
)
assert
.
True
(
t
,
result
)
remove
(
filename
)
}
func
TestSignTx
(
t
*
testing
.
T
)
{
filename
:=
"./tmp_priv_validator.json"
save
(
filename
,
priv_validator_file
)
privValidator
:=
LoadOrGenPrivValidatorFS
(
filename
)
tx
:=
&
types
.
Transaction
{}
privValidator
.
SignTx
(
tx
)
assert
.
True
(
t
,
types
.
SECP256K1
==
tx
.
Signature
.
Ty
)
assert
.
True
(
t
,
bytes
.
Equal
(
privValidator
.
PubKey
.
Bytes
(),
tx
.
Signature
.
Pubkey
))
assert
.
True
(
t
,
0
<
len
(
tx
.
Signature
.
Signature
))
remove
(
filename
)
}
\ No newline at end of file
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