Skip to content

Commit

Permalink
fix golint problem in tendermint and 33cn#67
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo-Huang committed Nov 23, 2018
1 parent f495782 commit 77b9e50
Show file tree
Hide file tree
Showing 22 changed files with 569 additions and 358 deletions.
60 changes: 32 additions & 28 deletions plugin/consensus/tendermint/consensus_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ const (
proposalHeartbeatIntervalSeconds = 1
)

//-----------------------------------------------------------------------------
// Errors

// Errors define
var (
ErrInvalidProposalSignature = errors.New("Error invalid proposal signature")
ErrInvalidProposalPOLRound = errors.New("Error invalid proposal POL round")
Expand Down Expand Up @@ -61,7 +59,7 @@ func (ti *timeoutInfo) String() string {
// The internal state machine receives input from peers, the internal validator, and from a timer.
type ConsensusState struct {
// config details
client *TendermintClient
client *Client
privValidator ttypes.PrivValidator // for signing votes

// services for creating and executing blocks
Expand Down Expand Up @@ -90,7 +88,7 @@ type ConsensusState struct {
setProposal func(proposal *tmtypes.Proposal) error

broadcastChannel chan<- MsgInfo
ourId ID
ourID ID
started uint32 // atomic
stopped uint32 // atomic
Quit chan struct{}
Expand All @@ -101,7 +99,7 @@ type ConsensusState struct {
}

// NewConsensusState returns a new ConsensusState.
func NewConsensusState(client *TendermintClient, state State, blockExec *BlockExecutor, evpool ttypes.EvidencePool) *ConsensusState {
func NewConsensusState(client *Client, state State, blockExec *BlockExecutor, evpool ttypes.EvidencePool) *ConsensusState {
cs := &ConsensusState{
client: client,
blockExec: blockExec,
Expand All @@ -127,14 +125,17 @@ func NewConsensusState(client *TendermintClient, state State, blockExec *BlockEx
return cs
}

// SetOurID method
func (cs *ConsensusState) SetOurID(id ID) {
cs.ourId = id
cs.ourID = id
}

// SetBroadcastChannel method
func (cs *ConsensusState) SetBroadcastChannel(broadcastChannel chan<- MsgInfo) {
cs.broadcastChannel = broadcastChannel
}

// IsRunning method
func (cs *ConsensusState) IsRunning() bool {
return atomic.LoadUint32(&cs.started) == 1 && atomic.LoadUint32(&cs.stopped) == 0
}
Expand Down Expand Up @@ -194,8 +195,7 @@ func (cs *ConsensusState) LoadCommit(height int64) *tmtypes.TendermintCommit {
return cs.client.LoadBlockCommit(height + 1)
}

// OnStart implements cmn.Service.
// It loads the latest state via the WAL, and starts the timeout and receive routines.
// Start It start first time starts the timeout checkTxsAvailable routine and receive routines.
func (cs *ConsensusState) Start() {
if atomic.CompareAndSwapUint32(&cs.started, 0, 1) {
if atomic.LoadUint32(&cs.stopped) == 1 {
Expand All @@ -213,9 +213,10 @@ func (cs *ConsensusState) Start() {
}
}

// OnStop implements cmn.Service. It stops all routines and waits for the WAL to finish.
// Stop timer and receive routine
func (cs *ConsensusState) Stop() {
cs.timeoutTicker.Stop()
cs.Quit <-struct{}{}
}

//------------------------------------------------------------
Expand Down Expand Up @@ -348,7 +349,7 @@ func (cs *ConsensusState) updateToState(state State) {

func (cs *ConsensusState) newStep() {
if cs.broadcastChannel != nil {
cs.broadcastChannel <- MsgInfo{TypeID: ttypes.NewRoundStepID, Msg: cs.RoundStateMessage(), PeerID: cs.ourId, PeerIP: ""}
cs.broadcastChannel <- MsgInfo{TypeID: ttypes.NewRoundStepID, Msg: cs.RoundStateMessage(), PeerID: cs.ourID, PeerIP: ""}
}
cs.nSteps++
}
Expand Down Expand Up @@ -563,7 +564,7 @@ func (cs *ConsensusState) enterNewRound(height int64, round int) {
cs.Votes.SetRound(round + 1) // also track next round (round+1) to allow round-skipping

//cs.eventBus.PublishEventNewRound(cs.RoundStateEvent())
cs.broadcastChannel <- MsgInfo{TypeID: ttypes.NewRoundStepID, Msg: cs.RoundStateMessage(), PeerID: cs.ourId, PeerIP: ""}
cs.broadcastChannel <- MsgInfo{TypeID: ttypes.NewRoundStepID, Msg: cs.RoundStateMessage(), PeerID: cs.ourID, PeerIP: ""}

// Wait for txs to be available in the mempool
// before we enterPropose in round 0.
Expand Down Expand Up @@ -598,8 +599,8 @@ func (cs *ConsensusState) proposalHeartbeat(height int64, round int) {
}
heartbeatMsg := &ttypes.Heartbeat{Heartbeat: heartbeat}
cs.privValidator.SignHeartbeat(chainID, heartbeatMsg)
cs.broadcastChannel <- MsgInfo{TypeID: ttypes.ProposalHeartbeatID, Msg: heartbeat, PeerID: cs.ourId, PeerIP: ""}
cs.broadcastChannel <- MsgInfo{TypeID: ttypes.NewRoundStepID, Msg: rs.RoundStateMessage(), PeerID: cs.ourId, PeerIP: ""}
cs.broadcastChannel <- MsgInfo{TypeID: ttypes.ProposalHeartbeatID, Msg: heartbeat, PeerID: cs.ourID, PeerIP: ""}
cs.broadcastChannel <- MsgInfo{TypeID: ttypes.NewRoundStepID, Msg: rs.RoundStateMessage(), PeerID: cs.ourID, PeerIP: ""}
counter++
time.Sleep(proposalHeartbeatIntervalSeconds * time.Second)
}
Expand Down Expand Up @@ -680,8 +681,8 @@ func (cs *ConsensusState) defaultDecideProposal(height int64, round int) {
proposal := ttypes.NewProposal(height, round, block.Hash(), polRound, polBlockID.BlockID)
if err := cs.privValidator.SignProposal(cs.state.ChainID, proposal); err == nil {
// send proposal and block on internal msg queue
cs.sendInternalMessage(MsgInfo{ttypes.ProposalID, &proposal.Proposal, cs.ourId, ""})
cs.sendInternalMessage(MsgInfo{ttypes.ProposalBlockID, block.TendermintBlock, cs.ourId, ""})
cs.sendInternalMessage(MsgInfo{ttypes.ProposalID, &proposal.Proposal, cs.ourID, ""})
cs.sendInternalMessage(MsgInfo{ttypes.ProposalBlockID, block.TendermintBlock, cs.ourID, ""})
tendermintlog.Info("Signed proposal", "height", height, "round", round, "proposal", proposal)
} else {
tendermintlog.Error("enterPropose: Error signing proposal", "height", height, "round", round, "err", err)
Expand Down Expand Up @@ -1267,14 +1268,14 @@ func (cs *ConsensusState) addVote(vote *ttypes.Vote, peerID string, peerIP strin
height := cs.Height
added, err = cs.Votes.AddVote(vote, peerID)
if added {
//cs.broadcastChannel <- MsgInfo{TypeID: ttypes.VoteID, Msg: vote.Vote, PeerID: cs.ourId, PeerIP: ""}
//cs.broadcastChannel <- MsgInfo{TypeID: ttypes.VoteID, Msg: vote.Vote, PeerID: cs.ourID, PeerIP: ""}
hasVoteMsg := &tmtypes.HasVoteMsg{
Height: vote.Height,
Round: vote.Round,
Type: int32(vote.Type),
Index: vote.ValidatorIndex,
}
cs.broadcastChannel <- MsgInfo{TypeID: ttypes.HasVoteID, Msg: hasVoteMsg, PeerID: cs.ourId, PeerIP: ""}
cs.broadcastChannel <- MsgInfo{TypeID: ttypes.HasVoteID, Msg: hasVoteMsg, PeerID: cs.ourID, PeerIP: ""}

switch vote.Type {
case uint32(ttypes.VoteTypePrevote):
Expand Down Expand Up @@ -1358,7 +1359,7 @@ func (cs *ConsensusState) addVote(vote *ttypes.Vote, peerID string, peerIP strin
return
}

func (cs *ConsensusState) signVote(type_ byte, hash []byte) (*ttypes.Vote, error) {
func (cs *ConsensusState) signVote(voteType byte, hash []byte) (*ttypes.Vote, error) {

addr := cs.privValidator.GetAddress()
valIndex, _ := cs.Validators.GetByAddress(addr)
Expand All @@ -1371,7 +1372,7 @@ func (cs *ConsensusState) signVote(type_ byte, hash []byte) (*ttypes.Vote, error
Height: cs.Height,
Round: int32(cs.Round),
Timestamp: time.Now().UnixNano(),
Type: uint32(type_),
Type: uint32(voteType),
BlockID: &tmtypes.BlockID{Hash: hash},
Signature: nil,
},
Expand All @@ -1383,14 +1384,14 @@ func (cs *ConsensusState) signVote(type_ byte, hash []byte) (*ttypes.Vote, error
}

// sign the vote and publish on internalMsgQueue
func (cs *ConsensusState) signAddVote(type_ byte, hash []byte) *ttypes.Vote {
func (cs *ConsensusState) signAddVote(voteType byte, hash []byte) *ttypes.Vote {
// if we don't have a key or we're not in the validator set, do nothing
if cs.privValidator == nil || !cs.Validators.HasAddress(cs.privValidator.GetAddress()) {
return nil
}
vote, err := cs.signVote(type_, hash)
vote, err := cs.signVote(voteType, hash)
if err == nil {
cs.sendInternalMessage(MsgInfo{TypeID: ttypes.VoteID, Msg: vote.Vote, PeerID: cs.ourId, PeerIP: ""})
cs.sendInternalMessage(MsgInfo{TypeID: ttypes.VoteID, Msg: vote.Vote, PeerID: cs.ourID, PeerIP: ""})
tendermintlog.Info("Signed and pushed vote", "height", cs.Height, "round", cs.Round, "vote", vote, "err", err)
return vote
}
Expand All @@ -1400,8 +1401,7 @@ func (cs *ConsensusState) signAddVote(type_ byte, hash []byte) *ttypes.Vote {

}

//---------------------------------------------------------

// CompareHRS method
func CompareHRS(h1 int64, r1 int, s1 ttypes.RoundStepType, h2 int64, r2 int, s2 ttypes.RoundStepType) int {
if h1 < h2 {
return -1
Expand Down Expand Up @@ -1446,7 +1446,7 @@ func (cs *ConsensusState) WaitForTxs() bool {
return !createEmptyBlocks || createEmptyBlocksInterval > 0
}

// EmptyBlocks returns the amount of time to wait before proposing an empty block or starting the propose timer if there are no txs available
// EmptyBlocksInterval returns the amount of time to wait before proposing an empty block or starting the propose timer if there are no txs available
func (cs *ConsensusState) EmptyBlocksInterval() time.Duration {
return time.Duration(createEmptyBlocksInterval) * time.Second
}
Expand All @@ -1461,26 +1461,30 @@ func (cs *ConsensusState) PeerQueryMaj23Sleep() time.Duration {
return time.Duration(peerQueryMaj23SleepDuration) * time.Millisecond
}

// IsProposer method
func (cs *ConsensusState) IsProposer() bool {
return cs.isProposer()
}

// GetPrevotesState method
func (cs *ConsensusState) GetPrevotesState(height int64, round int, blockID *tmtypes.BlockID) *ttypes.BitArray {
if height != cs.Height {
return nil
}
return cs.Votes.Prevotes(round).BitArrayByBlockID(blockID)
}

// GetPrecommitsState method
func (cs *ConsensusState) GetPrecommitsState(height int64, round int, blockID *tmtypes.BlockID) *ttypes.BitArray {
if height != cs.Height {
return nil
}
return cs.Votes.Precommits(round).BitArrayByBlockID(blockID)
}

func (cs *ConsensusState) SetPeerMaj23(height int64, round int, type_ byte, peerID ID, blockID *tmtypes.BlockID) {
// SetPeerMaj23 when reach maj 2/3
func (cs *ConsensusState) SetPeerMaj23(height int64, round int, voteType byte, peerID ID, blockID *tmtypes.BlockID) {
if height == cs.Height {
cs.Votes.SetPeerMaj23(round, type_, string(peerID), blockID)
cs.Votes.SetPeerMaj23(round, voteType, string(peerID), blockID)
}
}
16 changes: 10 additions & 6 deletions plugin/consensus/tendermint/evidence.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type envelope struct {
Data *json.RawMessage `json:"data"`
}

// EvidenceInfo struct
type EvidenceInfo struct {
Committed bool `json:"committed"`
Priority int64 `json:"priority"`
Expand Down Expand Up @@ -77,8 +78,8 @@ func keyPending(evidence ttypes.Evidence) []byte {
return _key("%s/%s/%X", baseKeyPending, bE(evidence.Height()), evidence.Hash())
}

func _key(fmt_ string, o ...interface{}) []byte {
return []byte(fmt.Sprintf(fmt_, o...))
func _key(s string, o ...interface{}) []byte {
return []byte(fmt.Sprintf(s, o...))
}

// EvidenceStore is a store of all the evidence we've seen, including
Expand All @@ -88,6 +89,7 @@ type EvidenceStore struct {
db dbm.DB
}

// NewEvidenceStore method
func NewEvidenceStore(db dbm.DB) *EvidenceStore {
if len(ttypes.EvidenceType2Type) == 0 {
ttypes.EvidenceType2Type = map[string]reflect.Type{
Expand Down Expand Up @@ -160,8 +162,8 @@ func (store *EvidenceStore) GetEvidence(height int64, hash []byte) *EvidenceInfo
// It returns false if the evidence is already stored.
func (store *EvidenceStore) AddNewEvidence(evidence ttypes.Evidence, priority int64) bool {
// check if we already have seen it
ei_ := store.GetEvidence(evidence.Height(), evidence.Hash())
if ei_ != nil && len(ei_.Evidence.Kind) == 0 {
ei := store.GetEvidence(evidence.Height(), evidence.Hash())
if ei != nil && len(ei.Evidence.Kind) == 0 {
return false
}

Expand Down Expand Up @@ -191,7 +193,7 @@ func (store *EvidenceStore) MarkEvidenceAsBroadcasted(evidence ttypes.Evidence)
store.db.Delete(key)
}

// MarkEvidenceAsPending removes evidence from pending and outqueue and sets the state to committed.
// MarkEvidenceAsCommitted removes evidence from pending and outqueue and sets the state to committed.
func (store *EvidenceStore) MarkEvidenceAsCommitted(evidence ttypes.Evidence) {
// if its committed, its been broadcast
store.MarkEvidenceAsBroadcasted(evidence)
Expand Down Expand Up @@ -227,6 +229,7 @@ func (store *EvidenceStore) getEvidenceInfo(evidence ttypes.Evidence) EvidenceIn
return ei
}

// EvidenceToInfoBytes method
func EvidenceToInfoBytes(evidence ttypes.Evidence, priority int64) ([]byte, error) {
evi, err := json.Marshal(evidence)
if err != nil {
Expand All @@ -250,6 +253,7 @@ func EvidenceToInfoBytes(evidence ttypes.Evidence, priority int64) ([]byte, erro
return eiBytes, nil
}

// EvidenceFromInfoBytes method
func (store *EvidenceStore) EvidenceFromInfoBytes(data []byte) (ttypes.Evidence, error) {
vote2 := EvidenceInfo{}
err := json.Unmarshal(data, &vote2)
Expand All @@ -268,7 +272,6 @@ func (store *EvidenceStore) EvidenceFromInfoBytes(data []byte) (ttypes.Evidence,

}

//-------------------------evidence pool----------------------------
// EvidencePool maintains a pool of valid evidence
// in an EvidenceStore.
type EvidencePool struct {
Expand All @@ -285,6 +288,7 @@ type EvidencePool struct {
evidenceChan chan ttypes.Evidence
}

// NewEvidencePool method
func NewEvidencePool(stateDB *CSStateDB, state State, evidenceStore *EvidenceStore) *EvidencePool {
evpool := &EvidencePool{
stateDB: stateDB,
Expand Down
Loading

0 comments on commit 77b9e50

Please sign in to comment.