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
7915e316
Commit
7915e316
authored
Oct 15, 2019
by
caopingcp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tendermint unit test
parent
6904435a
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
206 additions
and
32 deletions
+206
-32
chain33.test.toml
plugin/consensus/tendermint/chain33.test.toml
+2
-2
consensus_state.go
plugin/consensus/tendermint/consensus_state.go
+28
-13
execution.go
plugin/consensus/tendermint/execution.go
+5
-1
node_test.go
plugin/consensus/tendermint/node_test.go
+105
-0
tendermint.go
plugin/consensus/tendermint/tendermint.go
+1
-2
tendermint_test.go
plugin/consensus/tendermint/tendermint_test.go
+33
-1
block.go
plugin/consensus/tendermint/types/block.go
+32
-13
No files found.
plugin/consensus/tendermint/chain33.test.toml
View file @
7915e316
...
@@ -83,13 +83,13 @@ powLimitBits = "0x1f2fffff"
...
@@ -83,13 +83,13 @@ powLimitBits = "0x1f2fffff"
genesis
=
"14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
genesis
=
"14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
genesisBlockTime
=
1514533394
genesisBlockTime
=
1514533394
timeoutTxAvail
=
1000
timeoutTxAvail
=
1000
timeoutPropose
=
3
000
timeoutPropose
=
1
000
timeoutProposeDelta
=
500
timeoutProposeDelta
=
500
timeoutPrevote
=
1000
timeoutPrevote
=
1000
timeoutPrevoteDelta
=
500
timeoutPrevoteDelta
=
500
timeoutPrecommit
=
1000
timeoutPrecommit
=
1000
timeoutPrecommitDelta
=
500
timeoutPrecommitDelta
=
500
timeoutCommit
=
10
00
timeoutCommit
=
5
00
skipTimeoutCommit
=
false
skipTimeoutCommit
=
false
createEmptyBlocks
=
false
createEmptyBlocks
=
false
createEmptyBlocksInterval
=
0
createEmptyBlocksInterval
=
0
...
...
plugin/consensus/tendermint/consensus_state.go
View file @
7915e316
...
@@ -269,11 +269,11 @@ func (cs *ConsensusState) reconstructLastCommit(state State) {
...
@@ -269,11 +269,11 @@ func (cs *ConsensusState) reconstructLastCommit(state State) {
precommit
:=
&
ttypes
.
Vote
{
Vote
:
item
}
precommit
:=
&
ttypes
.
Vote
{
Vote
:
item
}
added
,
err
:=
lastPrecommits
.
AddVote
(
precommit
)
added
,
err
:=
lastPrecommits
.
AddVote
(
precommit
)
if
!
added
||
err
!=
nil
{
if
!
added
||
err
!=
nil
{
panic
(
fmt
.
Sprintf
(
"
Panicked on a Crisis: %v"
,
fmt
.
Sprintf
(
"Failed to reconstruct LastCommit: %v"
,
err
)
))
panic
(
fmt
.
Sprintf
(
"
Failed to reconstruct LastCommit: %v"
,
err
))
}
}
}
}
if
!
lastPrecommits
.
HasTwoThirdsMajority
()
{
if
!
lastPrecommits
.
HasTwoThirdsMajority
()
{
panic
(
fmt
.
Sprintf
(
"Panicked on a Sanity Check: %v"
,
"Failed to reconstruct LastCommit: Does not have +2/3 maj"
)
)
panic
(
"Failed to reconstruct LastCommit: Does not have +2/3 maj"
)
}
}
cs
.
LastCommit
=
lastPrecommits
cs
.
LastCommit
=
lastPrecommits
}
}
...
@@ -282,14 +282,12 @@ func (cs *ConsensusState) reconstructLastCommit(state State) {
...
@@ -282,14 +282,12 @@ func (cs *ConsensusState) reconstructLastCommit(state State) {
// The round becomes 0 and cs.Step becomes ttypes.RoundStepNewHeight.
// The round becomes 0 and cs.Step becomes ttypes.RoundStepNewHeight.
func
(
cs
*
ConsensusState
)
updateToState
(
state
State
)
{
func
(
cs
*
ConsensusState
)
updateToState
(
state
State
)
{
if
cs
.
CommitRound
>
-
1
&&
0
<
cs
.
Height
&&
cs
.
Height
!=
state
.
LastBlockHeight
{
if
cs
.
CommitRound
>
-
1
&&
0
<
cs
.
Height
&&
cs
.
Height
!=
state
.
LastBlockHeight
{
panic
(
fmt
.
Sprintf
(
"Panicked on a Sanity Check: %v"
,
fmt
.
Sprintf
(
"updateToState() expected state height of %v but found %v"
,
panic
(
fmt
.
Sprintf
(
"updateToState expected state height of %v but found %v"
,
cs
.
Height
,
state
.
LastBlockHeight
))
cs
.
Height
,
state
.
LastBlockHeight
)))
}
}
if
!
cs
.
state
.
IsEmpty
()
&&
cs
.
state
.
LastBlockHeight
+
1
!=
cs
.
Height
{
if
!
cs
.
state
.
IsEmpty
()
&&
cs
.
state
.
LastBlockHeight
+
1
!=
cs
.
Height
{
// This might happen when someone else is mutating cs.state.
// This might happen when someone else is mutating cs.state.
// Someone forgot to pass in state.Copy() somewhere?!
// Someone forgot to pass in state.Copy() somewhere?!
panic
(
fmt
.
Sprintf
(
"Panicked on a Sanity Check: %v"
,
fmt
.
Sprintf
(
"Inconsistent cs.state.LastBlockHeight+1 %v vs cs.Height %v"
,
panic
(
fmt
.
Sprintf
(
"Inconsistent cs.state.LastBlockHeight+1 %v vs cs.Height %v"
,
cs
.
state
.
LastBlockHeight
+
1
,
cs
.
Height
))
cs
.
state
.
LastBlockHeight
+
1
,
cs
.
Height
)))
}
}
// If state isn't further out than cs.state, just ignore.
// If state isn't further out than cs.state, just ignore.
...
@@ -305,7 +303,7 @@ func (cs *ConsensusState) updateToState(state State) {
...
@@ -305,7 +303,7 @@ func (cs *ConsensusState) updateToState(state State) {
lastPrecommits
:=
(
*
ttypes
.
VoteSet
)(
nil
)
lastPrecommits
:=
(
*
ttypes
.
VoteSet
)(
nil
)
if
cs
.
CommitRound
>
-
1
&&
cs
.
Votes
!=
nil
{
if
cs
.
CommitRound
>
-
1
&&
cs
.
Votes
!=
nil
{
if
!
cs
.
Votes
.
Precommits
(
cs
.
CommitRound
)
.
HasTwoThirdsMajority
()
{
if
!
cs
.
Votes
.
Precommits
(
cs
.
CommitRound
)
.
HasTwoThirdsMajority
()
{
panic
(
fmt
.
Sprintf
(
"Panicked on a Sanity Check: %v"
,
"updateToState(state) called but last Precommit round didn't have +2/3"
)
)
panic
(
"updateToState called but last Precommit round didn't have +2/3"
)
}
}
lastPrecommits
=
cs
.
Votes
.
Precommits
(
cs
.
CommitRound
)
lastPrecommits
=
cs
.
Votes
.
Precommits
(
cs
.
CommitRound
)
}
}
...
@@ -556,6 +554,9 @@ func (cs *ConsensusState) enterNewRound(height int64, round int) {
...
@@ -556,6 +554,9 @@ func (cs *ConsensusState) enterNewRound(height int64, round int) {
// We've already reset these upon new height,
// We've already reset these upon new height,
// and meanwhile we might have received a proposal
// and meanwhile we might have received a proposal
// for round 0.
// for round 0.
if
cs
.
begCons
.
IsZero
()
{
cs
.
begCons
=
time
.
Now
()
}
}
else
{
}
else
{
tendermintlog
.
Info
(
"Resetting Proposal info"
)
tendermintlog
.
Info
(
"Resetting Proposal info"
)
cs
.
Proposal
=
nil
cs
.
Proposal
=
nil
...
@@ -616,7 +617,7 @@ func (cs *ConsensusState) proposalHeartbeat(height int64, round int) {
...
@@ -616,7 +617,7 @@ func (cs *ConsensusState) proposalHeartbeat(height int64, round int) {
// Enter (!CreateEmptyBlocks) : after enterNewRound(height,round), once txs are in the mempool
// Enter (!CreateEmptyBlocks) : after enterNewRound(height,round), once txs are in the mempool
func
(
cs
*
ConsensusState
)
enterPropose
(
height
int64
,
round
int
)
{
func
(
cs
*
ConsensusState
)
enterPropose
(
height
int64
,
round
int
)
{
if
cs
.
Height
!=
height
||
round
<
cs
.
Round
||
(
cs
.
Round
==
round
&&
ttypes
.
RoundStepPropose
<=
cs
.
Step
)
{
if
cs
.
Height
!=
height
||
round
<
cs
.
Round
||
(
cs
.
Round
==
round
&&
ttypes
.
RoundStepPropose
<=
cs
.
Step
)
{
tendermintlog
.
Info
(
fmt
.
Sprintf
(
"enterPropose(%v/%v): Invalid args. Current step: %v/%v/%v"
,
height
,
round
,
cs
.
Height
,
cs
.
Round
,
cs
.
Step
))
tendermintlog
.
Debug
(
fmt
.
Sprintf
(
"enterPropose(%v/%v): Invalid args. Current step: %v/%v/%v"
,
height
,
round
,
cs
.
Height
,
cs
.
Round
,
cs
.
Step
))
return
return
}
}
tendermintlog
.
Info
(
fmt
.
Sprintf
(
"enterPropose(%v/%v). Current: %v/%v/%v"
,
height
,
round
,
cs
.
Height
,
cs
.
Round
,
cs
.
Step
))
tendermintlog
.
Info
(
fmt
.
Sprintf
(
"enterPropose(%v/%v). Current: %v/%v/%v"
,
height
,
round
,
cs
.
Height
,
cs
.
Round
,
cs
.
Step
))
...
@@ -730,7 +731,7 @@ func (cs *ConsensusState) createProposalBlock() (block *ttypes.TendermintBlock)
...
@@ -730,7 +731,7 @@ func (cs *ConsensusState) createProposalBlock() (block *ttypes.TendermintBlock)
}
else
{
}
else
{
// This shouldn't happen.
// This shouldn't happen.
tendermintlog
.
Error
(
"enterPropose: Cannot propose anything: No commit for the previous block."
)
tendermintlog
.
Error
(
"enterPropose: Cannot propose anything: No commit for the previous block."
)
return
return
nil
}
}
// Mempool validated transactions
// Mempool validated transactions
...
@@ -741,7 +742,7 @@ func (cs *ConsensusState) createProposalBlock() (block *ttypes.TendermintBlock)
...
@@ -741,7 +742,7 @@ func (cs *ConsensusState) createProposalBlock() (block *ttypes.TendermintBlock)
if
pblock
.
Height
!=
cs
.
Height
{
if
pblock
.
Height
!=
cs
.
Height
{
tendermintlog
.
Error
(
"pblock.Height is not equal to cs.Height"
)
tendermintlog
.
Error
(
"pblock.Height is not equal to cs.Height"
)
return
return
nil
}
}
proposerAddr
:=
cs
.
privValidator
.
GetAddress
()
proposerAddr
:=
cs
.
privValidator
.
GetAddress
()
...
@@ -752,7 +753,7 @@ func (cs *ConsensusState) createProposalBlock() (block *ttypes.TendermintBlock)
...
@@ -752,7 +753,7 @@ func (cs *ConsensusState) createProposalBlock() (block *ttypes.TendermintBlock)
pblockNew
:=
cs
.
client
.
ExecBlock
(
block
.
Data
)
pblockNew
:=
cs
.
client
.
ExecBlock
(
block
.
Data
)
if
pblockNew
==
nil
{
if
pblockNew
==
nil
{
tendermintlog
.
Error
(
"createProposalBlock ExecBlock fail"
)
tendermintlog
.
Error
(
"createProposalBlock ExecBlock fail"
)
return
return
nil
}
}
block
.
Data
=
pblockNew
block
.
Data
=
pblockNew
evidence
:=
cs
.
evpool
.
PendingEvidence
()
evidence
:=
cs
.
evpool
.
PendingEvidence
()
...
@@ -825,6 +826,20 @@ func (cs *ConsensusState) defaultDoPrevote(height int64, round int) {
...
@@ -825,6 +826,20 @@ func (cs *ConsensusState) defaultDoPrevote(height int64, round int) {
return
return
}
}
// Exec proposal block
blockCopy
:=
*
cs
.
ProposalBlock
.
Data
blockNew
:=
cs
.
client
.
ExecBlock
(
&
blockCopy
)
if
blockNew
==
nil
{
tendermintlog
.
Error
(
"enterPrevote: Exec ProposalBlock fail"
)
cs
.
signAddVote
(
ttypes
.
VoteTypePrevote
,
nil
)
return
}
if
!
bytes
.
Equal
(
blockNew
.
Hash
(),
cs
.
ProposalBlock
.
Data
.
Hash
())
{
tendermintlog
.
Error
(
"enterPrevote: Exec ProposalBlock has change"
)
cs
.
signAddVote
(
ttypes
.
VoteTypePrevote
,
nil
)
return
}
// Prevote cs.ProposalBlock
// Prevote cs.ProposalBlock
// NOTE: the proposal signature is validated when it is received,
// NOTE: the proposal signature is validated when it is received,
// and the proposal block parts are validated as they are received (against the merkle hash in the proposal)
// and the proposal block parts are validated as they are received (against the merkle hash in the proposal)
...
@@ -839,7 +854,7 @@ func (cs *ConsensusState) enterPrevoteWait(height int64, round int) {
...
@@ -839,7 +854,7 @@ func (cs *ConsensusState) enterPrevoteWait(height int64, round int) {
return
return
}
}
if
!
cs
.
Votes
.
Prevotes
(
round
)
.
HasTwoThirdsAny
()
{
if
!
cs
.
Votes
.
Prevotes
(
round
)
.
HasTwoThirdsAny
()
{
panic
(
fmt
.
Sprintf
(
"
Panicked on a Sanity Check: %v"
,
fmt
.
Sprintf
(
"enterPrevoteWait(%v/%v), but Prevotes does not have any +2/3 votes"
,
height
,
round
)
))
panic
(
fmt
.
Sprintf
(
"
enterPrevoteWait(%v/%v), but Prevotes does not have any +2/3 votes"
,
height
,
round
))
}
}
tendermintlog
.
Info
(
fmt
.
Sprintf
(
"enterPrevoteWait(%v/%v). Current: %v/%v/%v"
,
height
,
round
,
cs
.
Height
,
cs
.
Round
,
cs
.
Step
))
tendermintlog
.
Info
(
fmt
.
Sprintf
(
"enterPrevoteWait(%v/%v). Current: %v/%v/%v"
,
height
,
round
,
cs
.
Height
,
cs
.
Round
,
cs
.
Step
))
...
@@ -920,7 +935,7 @@ func (cs *ConsensusState) enterPrecommit(height int64, round int) {
...
@@ -920,7 +935,7 @@ func (cs *ConsensusState) enterPrecommit(height int64, round int) {
tendermintlog
.
Info
(
"enterPrecommit: +2/3 prevoted proposal block. Locking"
,
"hash"
,
fmt
.
Sprintf
(
"%X"
,
blockID
.
Hash
))
tendermintlog
.
Info
(
"enterPrecommit: +2/3 prevoted proposal block. Locking"
,
"hash"
,
fmt
.
Sprintf
(
"%X"
,
blockID
.
Hash
))
// Validate the block.
// Validate the block.
if
err
:=
cs
.
blockExec
.
ValidateBlock
(
cs
.
state
,
cs
.
ProposalBlock
);
err
!=
nil
{
if
err
:=
cs
.
blockExec
.
ValidateBlock
(
cs
.
state
,
cs
.
ProposalBlock
);
err
!=
nil
{
panic
(
fmt
.
Sprintf
(
"
Panicked on a Consensus Failure: %v"
,
fmt
.
Sprintf
(
"enterPrecommit: +2/3 prevoted for an invalid block: %v"
,
err
)
))
panic
(
fmt
.
Sprintf
(
"
enterPrecommit: +2/3 prevoted for an invalid block: %v"
,
err
))
}
}
cs
.
LockedRound
=
round
cs
.
LockedRound
=
round
cs
.
LockedBlock
=
cs
.
ProposalBlock
cs
.
LockedBlock
=
cs
.
ProposalBlock
...
...
plugin/consensus/tendermint/execution.go
View file @
7915e316
...
@@ -191,7 +191,10 @@ func changeInVotingPowerMoreOrEqualToOneThird(currentSet *ttypes.ValidatorSet, u
...
@@ -191,7 +191,10 @@ func changeInVotingPowerMoreOrEqualToOneThird(currentSet *ttypes.ValidatorSet, u
}
}
func
validateBlock
(
stateDB
*
CSStateDB
,
s
State
,
b
*
ttypes
.
TendermintBlock
)
error
{
func
validateBlock
(
stateDB
*
CSStateDB
,
s
State
,
b
*
ttypes
.
TendermintBlock
)
error
{
newTxs
:=
b
.
Header
.
NumTxs
// Validate internal consistency.
if
err
:=
b
.
ValidateBasic
();
err
!=
nil
{
return
err
}
// validate basic info
// validate basic info
if
b
.
Header
.
ChainID
!=
s
.
ChainID
{
if
b
.
Header
.
ChainID
!=
s
.
ChainID
{
...
@@ -206,6 +209,7 @@ func validateBlock(stateDB *CSStateDB, s State, b *ttypes.TendermintBlock) error
...
@@ -206,6 +209,7 @@ func validateBlock(stateDB *CSStateDB, s State, b *ttypes.TendermintBlock) error
return
fmt
.
Errorf
(
"Wrong Block.Header.LastBlockID. Expected %v, got %v"
,
s
.
LastBlockID
,
b
.
Header
.
LastBlockID
)
return
fmt
.
Errorf
(
"Wrong Block.Header.LastBlockID. Expected %v, got %v"
,
s
.
LastBlockID
,
b
.
Header
.
LastBlockID
)
}
}
newTxs
:=
b
.
Header
.
NumTxs
if
b
.
Header
.
TotalTxs
!=
s
.
LastBlockTotalTx
+
newTxs
{
if
b
.
Header
.
TotalTxs
!=
s
.
LastBlockTotalTx
+
newTxs
{
return
fmt
.
Errorf
(
"Wrong Block.Header.TotalTxs. Expected %v, got %v"
,
s
.
LastBlockTotalTx
+
newTxs
,
b
.
Header
.
TotalTxs
)
return
fmt
.
Errorf
(
"Wrong Block.Header.TotalTxs. Expected %v, got %v"
,
s
.
LastBlockTotalTx
+
newTxs
,
b
.
Header
.
TotalTxs
)
}
}
...
...
plugin/consensus/tendermint/node_test.go
0 → 100644
View file @
7915e316
package
tendermint
import
(
"encoding/hex"
"fmt"
"github.com/33cn/chain33/types"
"sync"
"testing"
"github.com/33cn/chain33/common/crypto"
"github.com/stretchr/testify/assert"
)
var
(
secureConnCrypto
crypto
.
Crypto
sum
=
0
mutx
sync
.
Mutex
privKey
=
"B3DC4C0725884EBB7264B92F1D8D37584A64ADE1799D997EC64B4FE3973E08DE220ACBE680DF2473A0CB48987A00FCC1812F106A7390BE6B8E2D31122C992A19"
expectAddress
=
"02A13174B92727C4902DB099E51A3339F48BD45E"
)
func
init
()
{
cr2
,
err
:=
crypto
.
New
(
types
.
GetSignName
(
""
,
types
.
ED25519
))
if
err
!=
nil
{
fmt
.
Println
(
"crypto.New failed for types.ED25519"
)
return
}
secureConnCrypto
=
cr2
}
func
TestParallel
(
t
*
testing
.
T
)
{
Parallel
(
func
()
{
mutx
.
Lock
()
sum
++
mutx
.
Unlock
()
},
func
()
{
mutx
.
Lock
()
sum
+=
2
mutx
.
Unlock
()
},
func
()
{
mutx
.
Lock
()
sum
+=
3
mutx
.
Unlock
()
},
)
fmt
.
Println
(
"TestParallel ok"
)
assert
.
Equal
(
t
,
6
,
sum
)
}
func
TestGenAddressByPubKey
(
t
*
testing
.
T
)
{
tmp
,
err
:=
hex
.
DecodeString
(
privKey
)
assert
.
Nil
(
t
,
err
)
priv
,
err
:=
secureConnCrypto
.
PrivKeyFromBytes
(
tmp
)
assert
.
Nil
(
t
,
err
)
addr
:=
GenAddressByPubKey
(
priv
.
PubKey
())
strAddr
:=
fmt
.
Sprintf
(
"%X"
,
addr
)
assert
.
Equal
(
t
,
expectAddress
,
strAddr
)
fmt
.
Println
(
"TestGenAddressByPubKey ok"
)
}
func
TestIP2IPPort
(
t
*
testing
.
T
)
{
testMap
:=
NewMutexMap
()
assert
.
Equal
(
t
,
false
,
testMap
.
Has
(
"1.1.1.1"
))
testMap
.
Set
(
"1.1.1.1"
,
"1.1.1.1:80"
)
assert
.
Equal
(
t
,
true
,
testMap
.
Has
(
"1.1.1.1"
))
testMap
.
Set
(
"1.1.1.2"
,
"1.1.1.2:80"
)
assert
.
Equal
(
t
,
true
,
testMap
.
Has
(
"1.1.1.2"
))
testMap
.
Delete
(
"1.1.1.1"
)
assert
.
Equal
(
t
,
false
,
testMap
.
Has
(
"1.1.1.1"
))
fmt
.
Println
(
"TestIP2IPPort ok"
)
}
func
TestPeerSet
(
t
*
testing
.
T
)
{
testSet
:=
NewPeerSet
()
assert
.
Equal
(
t
,
false
,
testSet
.
Has
(
"1"
))
peer1
:=
&
peerConn
{
id
:
"1"
,
ip
:
[]
byte
(
"1.1.1.1"
)}
testSet
.
Add
(
peer1
)
assert
.
Equal
(
t
,
true
,
testSet
.
Has
(
"1"
))
assert
.
Equal
(
t
,
true
,
testSet
.
HasIP
([]
byte
(
"1.1.1.1"
)))
err
:=
testSet
.
Add
(
peer1
)
assert
.
NotNil
(
t
,
err
)
peer2
:=
&
peerConn
{
id
:
"2"
,
ip
:
[]
byte
(
"1.1.1.2"
)}
testSet
.
Add
(
peer2
)
assert
.
Equal
(
t
,
true
,
testSet
.
Has
(
"2"
))
assert
.
Equal
(
t
,
2
,
testSet
.
Size
())
testSet
.
Remove
(
peer1
)
assert
.
Equal
(
t
,
1
,
testSet
.
Size
())
assert
.
Equal
(
t
,
false
,
testSet
.
Has
(
"1"
))
assert
.
Equal
(
t
,
false
,
testSet
.
HasIP
([]
byte
(
"1.1.1.1"
)))
fmt
.
Println
(
"TestPeerSet ok"
)
}
plugin/consensus/tendermint/tendermint.go
View file @
7915e316
...
@@ -361,7 +361,7 @@ func (client *Client) getBlockInfoTx(current *types.Block) (*tmtypes.ValNodeActi
...
@@ -361,7 +361,7 @@ func (client *Client) getBlockInfoTx(current *types.Block) (*tmtypes.ValNodeActi
return
&
valAction
,
nil
return
&
valAction
,
nil
}
}
// CheckBlock
暂不检查任何的交易
// CheckBlock
检查区块
func
(
client
*
Client
)
CheckBlock
(
parent
*
types
.
Block
,
current
*
types
.
BlockDetail
)
error
{
func
(
client
*
Client
)
CheckBlock
(
parent
*
types
.
Block
,
current
*
types
.
BlockDetail
)
error
{
if
current
.
Block
.
Difficulty
!=
types
.
GetP
(
0
)
.
PowLimitBits
{
if
current
.
Block
.
Difficulty
!=
types
.
GetP
(
0
)
.
PowLimitBits
{
return
types
.
ErrBlockHeaderDifficulty
return
types
.
ErrBlockHeaderDifficulty
...
@@ -385,7 +385,6 @@ func (client *Client) CheckBlock(parent *types.Block, current *types.BlockDetail
...
@@ -385,7 +385,6 @@ func (client *Client) CheckBlock(parent *types.Block, current *types.BlockDetail
}
}
lastInfo
:=
lastValAction
.
GetBlockInfo
()
lastInfo
:=
lastValAction
.
GetBlockInfo
()
lastProposalBlock
:=
&
ttypes
.
TendermintBlock
{
TendermintBlock
:
lastInfo
.
GetBlock
()}
lastProposalBlock
:=
&
ttypes
.
TendermintBlock
{
TendermintBlock
:
lastInfo
.
GetBlock
()}
lastProposalBlock
.
Data
=
parent
if
!
lastProposalBlock
.
HashesTo
(
info
.
Block
.
Header
.
LastBlockID
.
Hash
)
{
if
!
lastProposalBlock
.
HashesTo
(
info
.
Block
.
Header
.
LastBlockID
.
Hash
)
{
return
ttypes
.
ErrLastBlockID
return
ttypes
.
ErrLastBlockID
}
}
...
...
plugin/consensus/tendermint/tendermint_test.go
View file @
7915e316
...
@@ -7,6 +7,7 @@ package tendermint
...
@@ -7,6 +7,7 @@ package tendermint
import
(
import
(
"context"
"context"
"encoding/binary"
"encoding/binary"
"encoding/hex"
"errors"
"errors"
"flag"
"flag"
"fmt"
"fmt"
...
@@ -27,6 +28,7 @@ import (
...
@@ -27,6 +28,7 @@ import (
"github.com/33cn/chain33/store"
"github.com/33cn/chain33/store"
"github.com/33cn/chain33/types"
"github.com/33cn/chain33/types"
pty
"github.com/33cn/plugin/plugin/dapp/norm/types"
pty
"github.com/33cn/plugin/plugin/dapp/norm/types"
ty
"github.com/33cn/plugin/plugin/dapp/valnode/types"
"google.golang.org/grpc"
"google.golang.org/grpc"
_
"github.com/33cn/chain33/system"
_
"github.com/33cn/chain33/system"
...
@@ -36,7 +38,7 @@ import (
...
@@ -36,7 +38,7 @@ import (
var
(
var
(
random
*
rand
.
Rand
random
*
rand
.
Rand
loopCount
=
10
loopCount
=
3
conn
*
grpc
.
ClientConn
conn
*
grpc
.
ClientConn
c
types
.
Chain33Client
c
types
.
Chain33Client
)
)
...
@@ -73,6 +75,11 @@ func TendermintPerf() {
...
@@ -73,6 +75,11 @@ func TendermintPerf() {
NormPut
()
NormPut
()
time
.
Sleep
(
time
.
Second
)
time
.
Sleep
(
time
.
Second
)
}
}
AddNode
()
for
i
:=
0
;
i
<
loopCount
*
3
;
i
++
{
NormPut
()
time
.
Sleep
(
time
.
Second
)
}
time
.
Sleep
(
2
*
time
.
Second
)
time
.
Sleep
(
2
*
time
.
Second
)
}
}
...
@@ -177,3 +184,28 @@ func NormPut() {
...
@@ -177,3 +184,28 @@ func NormPut() {
return
return
}
}
}
}
func
AddNode
()
{
pubkey
:=
"788657125A5A547B499F8B74239092EBB6466E8A205348D9EA645D510235A671"
pubkeybyte
,
err
:=
hex
.
DecodeString
(
pubkey
)
if
err
!=
nil
{
fmt
.
Fprintln
(
os
.
Stderr
,
err
)
return
}
nput
:=
&
ty
.
ValNodeAction_Node
{
Node
:
&
ty
.
ValNode
{
PubKey
:
pubkeybyte
,
Power
:
int64
(
2
)}}
action
:=
&
ty
.
ValNodeAction
{
Value
:
nput
,
Ty
:
ty
.
ValNodeActionUpdate
}
tx
:=
&
types
.
Transaction
{
Execer
:
[]
byte
(
"valnode"
),
Payload
:
types
.
Encode
(
action
),
Fee
:
fee
}
tx
.
To
=
address
.
ExecAddress
(
"valnode"
)
tx
.
Nonce
=
r
.
Int63
()
tx
.
Sign
(
types
.
SECP256K1
,
getprivkey
(
"CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944"
))
reply
,
err
:=
c
.
SendTransaction
(
context
.
Background
(),
tx
)
if
err
!=
nil
{
fmt
.
Fprintln
(
os
.
Stderr
,
err
)
return
}
if
!
reply
.
IsOk
{
fmt
.
Fprintln
(
os
.
Stderr
,
errors
.
New
(
string
(
reply
.
GetMsg
())))
return
}
}
plugin/consensus/tendermint/types/block.go
View file @
7915e316
...
@@ -97,29 +97,47 @@ func (b *TendermintBlock) AddEvidence(evidence []Evidence) {
...
@@ -97,29 +97,47 @@ func (b *TendermintBlock) AddEvidence(evidence []Evidence) {
// ValidateBasic performs basic validation that doesn't involve state data.
// ValidateBasic performs basic validation that doesn't involve state data.
// It checks the internal consistency of the block.
// It checks the internal consistency of the block.
func
(
b
*
TendermintBlock
)
ValidateBasic
()
(
int64
,
error
)
{
// Further validation is done using state#ValidateBlock.
newTxs
:=
int64
(
len
(
b
.
Data
.
Txs
))
func
(
b
*
TendermintBlock
)
ValidateBasic
()
error
{
if
b
==
nil
{
return
errors
.
New
(
"nil block"
)
}
if
b
.
Header
.
Height
<
0
{
return
errors
.
New
(
"Negative Header.Height"
)
}
else
if
b
.
Header
.
Height
==
0
{
return
errors
.
New
(
"Zero Header.Height"
)
}
newTxs
:=
int64
(
len
(
b
.
Data
.
Txs
))
if
b
.
Header
.
NumTxs
!=
newTxs
{
if
b
.
Header
.
NumTxs
!=
newTxs
{
return
0
,
fmt
.
Errorf
(
"Wrong Block.Header.NumTxs. Expected %v, got %v"
,
newTxs
,
b
.
Header
.
NumTxs
)
return
fmt
.
Errorf
(
"Wrong Header.NumTxs. Expected %v, got %v"
,
newTxs
,
b
.
Header
.
NumTxs
)
}
if
b
.
Header
.
TotalTxs
<
0
{
return
errors
.
New
(
"Negative Header.TotalTxs"
)
}
}
lastCommit
:=
Commit
{
lastCommit
:=
Commit
{
TendermintCommit
:
b
.
LastCommit
,
TendermintCommit
:
b
.
LastCommit
,
}
}
if
!
bytes
.
Equal
(
b
.
Header
.
LastCommitHash
,
lastCommit
.
Hash
())
{
if
b
.
Header
.
Height
>
1
{
return
0
,
fmt
.
Errorf
(
"Wrong Block.Header.LastCommitHash. Expected %v, got %v"
,
b
.
Header
.
LastCommitHash
,
lastCommit
.
Hash
())
if
b
.
LastCommit
==
nil
{
return
errors
.
New
(
"nil LastCommit"
)
}
}
if
b
.
Header
.
Height
!=
1
{
if
err
:=
lastCommit
.
ValidateBasic
();
err
!=
nil
{
if
err
:=
lastCommit
.
ValidateBasic
();
err
!=
nil
{
return
0
,
err
return
err
}
}
}
}
if
!
bytes
.
Equal
(
b
.
Header
.
LastCommitHash
,
lastCommit
.
Hash
())
{
return
fmt
.
Errorf
(
"Wrong Header.LastCommitHash. Expected %v, got %v"
,
b
.
Header
.
LastCommitHash
,
lastCommit
.
Hash
())
}
evidence
:=
&
EvidenceData
{
EvidenceData
:
b
.
Evidence
}
evidence
:=
&
EvidenceData
{
EvidenceData
:
b
.
Evidence
}
if
!
bytes
.
Equal
(
b
.
Header
.
EvidenceHash
,
evidence
.
Hash
())
{
if
!
bytes
.
Equal
(
b
.
Header
.
EvidenceHash
,
evidence
.
Hash
())
{
return
0
,
errors
.
New
(
Fmt
(
"Wrong Block.
Header.EvidenceHash. Expected %v, got %v"
,
b
.
Header
.
EvidenceHash
,
evidence
.
Hash
()))
return
errors
.
New
(
Fmt
(
"Wrong
Header.EvidenceHash. Expected %v, got %v"
,
b
.
Header
.
EvidenceHash
,
evidence
.
Hash
()))
}
}
return
n
ewTxs
,
n
il
return
nil
}
}
// FillHeader fills in any remaining header fields that are a function of the block data
// FillHeader fills in any remaining header fields that are a function of the block data
...
@@ -331,13 +349,14 @@ func (commit *Commit) ValidateBasic() error {
...
@@ -331,13 +349,14 @@ func (commit *Commit) ValidateBasic() error {
height
,
round
:=
commit
.
Height
(),
commit
.
Round
()
height
,
round
:=
commit
.
Height
(),
commit
.
Round
()
// validate the precommits
// validate the precommits
for
_
,
precommit
:=
range
commit
.
Precommits
{
for
_
,
item
:=
range
commit
.
Precommits
{
//
It's OK for precommits to be missing
.
//
may be nil if validator skipped
.
if
precommit
==
nil
{
if
item
==
nil
||
len
(
item
.
Signature
)
==
0
{
continue
continue
}
}
precommit
:=
&
Vote
{
Vote
:
item
}
// Ensure that all votes are precommits
// Ensure that all votes are precommits
if
byte
(
precommit
.
Type
)
!=
VoteTypePrecommit
{
if
precommit
.
Type
!=
uint32
(
VoteTypePrecommit
)
{
return
fmt
.
Errorf
(
"Invalid commit vote. Expected precommit, got %v"
,
return
fmt
.
Errorf
(
"Invalid commit vote. Expected precommit, got %v"
,
precommit
.
Type
)
precommit
.
Type
)
}
}
...
...
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