mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-20 15:04:22 +00:00
pretty print ConsensusState
This commit is contained in:
@@ -144,6 +144,13 @@ func (ps *PartSet) RootHash() []byte {
|
||||
return ps.rootHash
|
||||
}
|
||||
|
||||
func (ps *PartSet) Count() uint16 {
|
||||
if ps == nil {
|
||||
return 0
|
||||
}
|
||||
return ps.count
|
||||
}
|
||||
|
||||
func (ps *PartSet) Total() uint16 {
|
||||
if ps == nil {
|
||||
return 0
|
||||
@@ -197,3 +204,11 @@ func (ps *PartSet) GetReader() io.Reader {
|
||||
}
|
||||
return bytes.NewReader(buf)
|
||||
}
|
||||
|
||||
func (ps *PartSet) Description() string {
|
||||
if ps == nil {
|
||||
return "nil-PartSet"
|
||||
} else {
|
||||
return fmt.Sprintf("(%v of %v)", ps.Count(), ps.Total())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package consensus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
. "github.com/tendermint/tendermint/binary"
|
||||
@@ -102,3 +103,15 @@ func (pol *POL) Verify(vset *state.ValidatorSet) error {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (pol *POL) Description() string {
|
||||
if pol == nil {
|
||||
return "nil-POL"
|
||||
} else {
|
||||
blockHash := pol.BlockHash
|
||||
if blockHash != nil {
|
||||
blockHash = blockHash[:6]
|
||||
}
|
||||
return fmt.Sprintf("POL{H:%v R:%v BH:%X}", pol.Height, pol.Round, blockHash)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,10 @@ type PrivValidator struct {
|
||||
func (pv *PrivValidator) Sign(o Signable) {
|
||||
switch o.(type) {
|
||||
case *Proposal:
|
||||
//TODO: prevent double signing.
|
||||
//TODO: prevent double signing && test.
|
||||
pv.PrivAccount.Sign(o.(*Proposal))
|
||||
case *Vote:
|
||||
//TODO: prevent double signing.
|
||||
//TODO: prevent double signing && test.
|
||||
pv.PrivAccount.Sign(o.(*Vote))
|
||||
}
|
||||
}
|
||||
|
||||
+36
-9
@@ -2,6 +2,7 @@ package consensus
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -33,7 +34,6 @@ type RoundState struct {
|
||||
Step uint8
|
||||
StartTime time.Time
|
||||
Validators *state.ValidatorSet
|
||||
Proposer *state.Validator
|
||||
Proposal *Proposal
|
||||
ProposalBlock *Block
|
||||
ProposalBlockPartSet *PartSet
|
||||
@@ -47,6 +47,38 @@ type RoundState struct {
|
||||
PrivValidator *PrivValidator
|
||||
}
|
||||
|
||||
func (rs *RoundState) String() string {
|
||||
return rs.StringWithIndent("")
|
||||
}
|
||||
|
||||
func (rs *RoundState) StringWithIndent(indent string) string {
|
||||
return fmt.Sprintf(`RoundState{
|
||||
%s H:%v R:%v S:%v
|
||||
%s StartTime: %v
|
||||
%s Validators: %v
|
||||
%s Proposal: %v
|
||||
%s ProposalBlock: %v %v
|
||||
%s ProposalPOL: %v %v
|
||||
%s LockedBlock: %v
|
||||
%s LockedPOL: %v
|
||||
%s Votes: %v
|
||||
%s Precommits: %v
|
||||
%s Commits: %v
|
||||
%s}`,
|
||||
indent, rs.Height, rs.Round, rs.Step,
|
||||
indent, rs.StartTime,
|
||||
indent, rs.Validators.StringWithIndent(indent+" "),
|
||||
indent, rs.Proposal,
|
||||
indent, rs.ProposalBlockPartSet.Description(), rs.ProposalBlock.Description(),
|
||||
indent, rs.ProposalPOLPartSet.Description(), rs.ProposalPOL.Description(),
|
||||
indent, rs.LockedBlock.Description(),
|
||||
indent, rs.LockedPOL.Description(),
|
||||
indent, rs.Votes.StringWithIndent(indent+" "),
|
||||
indent, rs.Precommits.StringWithIndent(indent+" "),
|
||||
indent, rs.Commits.StringWithIndent(indent+" "),
|
||||
indent)
|
||||
}
|
||||
|
||||
//-------------------------------------
|
||||
|
||||
// Tracks consensus state across block heights and rounds.
|
||||
@@ -92,7 +124,6 @@ func (cs *ConsensusState) updateToState(state *state.State) {
|
||||
cs.Step = RoundStepStart
|
||||
cs.StartTime = state.CommitTime.Add(newBlockWaitDuration)
|
||||
cs.Validators = validators
|
||||
cs.Proposer = validators.Proposer()
|
||||
cs.Proposal = nil
|
||||
cs.ProposalBlock = nil
|
||||
cs.ProposalBlockPartSet = nil
|
||||
@@ -135,7 +166,6 @@ func (cs *ConsensusState) setupRound(round uint16) {
|
||||
cs.Round = round
|
||||
cs.Step = RoundStepStart
|
||||
cs.Validators = validators
|
||||
cs.Proposer = validators.Proposer()
|
||||
cs.Proposal = nil
|
||||
cs.ProposalBlock = nil
|
||||
cs.ProposalBlockPartSet = nil
|
||||
@@ -178,7 +208,7 @@ func (cs *ConsensusState) SetProposal(proposal *Proposal) error {
|
||||
}
|
||||
|
||||
// Verify signature
|
||||
if !cs.Proposer.Verify(proposal) {
|
||||
if !cs.Validators.Proposer().Verify(proposal) {
|
||||
return ErrInvalidProposalSignature
|
||||
}
|
||||
|
||||
@@ -192,7 +222,7 @@ func (cs *ConsensusState) MakeProposal() {
|
||||
cs.mtx.Lock()
|
||||
defer cs.mtx.Unlock()
|
||||
|
||||
if cs.PrivValidator == nil || cs.Proposer.Id != cs.PrivValidator.Id {
|
||||
if cs.PrivValidator == nil || cs.Validators.Proposer().Id != cs.PrivValidator.Id {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -382,10 +412,7 @@ func (cs *ConsensusState) Commit(height uint32, round uint16) *Block {
|
||||
}
|
||||
|
||||
// Save to blockStore
|
||||
err := cs.blockStore.SaveBlock(block)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
cs.blockStore.SaveBlock(block)
|
||||
|
||||
// What was staged becomes committed.
|
||||
state := cs.stagedState
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package consensus
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/tendermint/tendermint/blocks"
|
||||
. "github.com/tendermint/tendermint/common"
|
||||
db_ "github.com/tendermint/tendermint/db"
|
||||
"github.com/tendermint/tendermint/mempool"
|
||||
"github.com/tendermint/tendermint/state"
|
||||
)
|
||||
|
||||
func randAccountDetail(id uint64, status byte) (*state.AccountDetail, *state.PrivAccount) {
|
||||
privAccount := state.GenPrivAccount()
|
||||
privAccount.Id = id
|
||||
account := privAccount.Account
|
||||
return &state.AccountDetail{
|
||||
Account: account,
|
||||
Sequence: RandUInt(),
|
||||
Balance: RandUInt64() + 1000, // At least 1000.
|
||||
Status: status,
|
||||
}, privAccount
|
||||
}
|
||||
|
||||
// The first numValidators accounts are validators.
|
||||
func randGenesisState(numAccounts int, numValidators int) (*state.State, []*state.PrivAccount) {
|
||||
db := db_.NewMemDB()
|
||||
accountDetails := make([]*state.AccountDetail, numAccounts)
|
||||
privAccounts := make([]*state.PrivAccount, numAccounts)
|
||||
for i := 0; i < numAccounts; i++ {
|
||||
if i < numValidators {
|
||||
accountDetails[i], privAccounts[i] =
|
||||
randAccountDetail(uint64(i), state.AccountStatusBonded)
|
||||
} else {
|
||||
accountDetails[i], privAccounts[i] =
|
||||
randAccountDetail(uint64(i), state.AccountStatusNominal)
|
||||
}
|
||||
}
|
||||
s0 := state.GenesisState(db, time.Now(), accountDetails)
|
||||
s0.Save(time.Now())
|
||||
return s0, privAccounts
|
||||
}
|
||||
|
||||
func makeConsensusState() (*ConsensusState, []*state.PrivAccount) {
|
||||
state, privAccounts := randGenesisState(20, 10)
|
||||
blockStore := NewBlockStore(db_.NewMemDB())
|
||||
mempool := mempool.NewMempool(nil, state)
|
||||
cs := NewConsensusState(state, blockStore, mempool)
|
||||
return cs, privAccounts
|
||||
}
|
||||
|
||||
func TestUnit(t *testing.T) {
|
||||
cs, privAccounts := makeConsensusState()
|
||||
rs := cs.GetRoundState()
|
||||
t.Log(rs)
|
||||
if false {
|
||||
t.Log(privAccounts)
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"github.com/tendermint/tendermint/state"
|
||||
)
|
||||
|
||||
// Common test methods
|
||||
|
||||
func makeValidator(id uint64, votingPower uint64) (*state.Validator, *state.PrivAccount) {
|
||||
privAccount := state.GenPrivAccount()
|
||||
privAccount.Id = id
|
||||
|
||||
Reference in New Issue
Block a user