github.com/koko1123/flow-go-1@v0.29.6/consensus/hotstuff/notifications/log_consumer.go (about)

     1  package notifications
     2  
     3  import (
     4  	"github.com/rs/zerolog"
     5  
     6  	"github.com/koko1123/flow-go-1/consensus/hotstuff"
     7  	"github.com/koko1123/flow-go-1/consensus/hotstuff/model"
     8  	"github.com/koko1123/flow-go-1/model/flow"
     9  	"github.com/koko1123/flow-go-1/utils/logging"
    10  )
    11  
    12  // LogConsumer is an implementation of the notifications consumer that logs a
    13  // message for each event.
    14  type LogConsumer struct {
    15  	log zerolog.Logger
    16  }
    17  
    18  var _ hotstuff.Consumer = (*LogConsumer)(nil)
    19  
    20  func NewLogConsumer(log zerolog.Logger) *LogConsumer {
    21  	lc := &LogConsumer{
    22  		log: log,
    23  	}
    24  	return lc
    25  }
    26  
    27  func (lc *LogConsumer) OnEventProcessed() {
    28  	lc.log.Debug().Msg("event processed")
    29  }
    30  
    31  func (lc *LogConsumer) OnBlockIncorporated(block *model.Block) {
    32  	lc.logBasicBlockData(lc.log.Debug(), block).
    33  		Msg("block incorporated")
    34  }
    35  
    36  func (lc *LogConsumer) OnFinalizedBlock(block *model.Block) {
    37  	lc.logBasicBlockData(lc.log.Debug(), block).
    38  		Msg("block finalized")
    39  }
    40  
    41  func (lc *LogConsumer) OnDoubleProposeDetected(block *model.Block, alt *model.Block) {
    42  	lc.log.Warn().
    43  		Uint64("block_view", block.View).
    44  		Hex("block_id", block.BlockID[:]).
    45  		Hex("alt_id", alt.BlockID[:]).
    46  		Hex("proposer_id", block.ProposerID[:]).
    47  		Msg("double proposal detected")
    48  }
    49  
    50  func (lc *LogConsumer) OnReceiveVote(currentView uint64, vote *model.Vote) {
    51  	lc.log.Debug().
    52  		Uint64("cur_view", currentView).
    53  		Uint64("vote_view", vote.View).
    54  		Hex("voted_block_id", vote.BlockID[:]).
    55  		Hex("voter_id", vote.SignerID[:]).
    56  		Msg("processing vote")
    57  }
    58  
    59  func (lc *LogConsumer) OnReceiveProposal(currentView uint64, proposal *model.Proposal) {
    60  	lc.logBasicBlockData(lc.log.Debug(), proposal.Block).
    61  		Uint64("cur_view", currentView).
    62  		Msg("processing proposal")
    63  }
    64  
    65  func (lc *LogConsumer) OnEnteringView(view uint64, leader flow.Identifier) {
    66  	lc.log.Debug().
    67  		Uint64("view", view).
    68  		Hex("leader", leader[:]).
    69  		Msg("view entered")
    70  }
    71  
    72  func (lc *LogConsumer) OnQcTriggeredViewChange(qc *flow.QuorumCertificate, newView uint64) {
    73  	lc.log.Debug().
    74  		Uint64("qc_view", qc.View).
    75  		Hex("qc_id", qc.BlockID[:]).
    76  		Uint64("new_view", newView).
    77  		Msg("QC triggered view change")
    78  }
    79  
    80  func (lc *LogConsumer) OnProposingBlock(block *model.Proposal) {
    81  	lc.logBasicBlockData(lc.log.Debug(), block.Block).
    82  		Msg("proposing block")
    83  }
    84  
    85  func (lc *LogConsumer) OnVoting(vote *model.Vote) {
    86  	lc.log.Debug().
    87  		Uint64("block_view", vote.View).
    88  		Hex("block_id", vote.BlockID[:]).
    89  		Msg("voting for block")
    90  }
    91  
    92  func (lc *LogConsumer) OnQcConstructedFromVotes(curView uint64, qc *flow.QuorumCertificate) {
    93  	lc.log.Debug().
    94  		Uint64("cur_view", curView).
    95  		Uint64("qc_view", qc.View).
    96  		Hex("qc_id", qc.BlockID[:]).
    97  		Msg("QC constructed from votes")
    98  }
    99  
   100  func (lc *LogConsumer) OnStartingTimeout(info *model.TimerInfo) {
   101  	lc.log.Debug().
   102  		Uint64("timeout_view", info.View).
   103  		Time("timeout_cutoff", info.StartTime.Add(info.Duration)).
   104  		Str("timeout_mode", info.Mode.String()).
   105  		Msg("timeout started")
   106  }
   107  
   108  func (lc *LogConsumer) OnReachedTimeout(info *model.TimerInfo) {
   109  	lc.log.Debug().
   110  		Uint64("timeout_view", info.View).
   111  		Time("timeout_cutoff", info.StartTime.Add(info.Duration)).
   112  		Str("timeout_mode", info.Mode.String()).
   113  		Msg("timeout reached")
   114  }
   115  
   116  func (lc *LogConsumer) OnQcIncorporated(qc *flow.QuorumCertificate) {
   117  	lc.log.Debug().
   118  		Uint64("qc_view", qc.View).
   119  		Hex("qc_id", qc.BlockID[:]).
   120  		Msg("QC incorporated")
   121  }
   122  
   123  func (lc *LogConsumer) OnForkChoiceGenerated(view uint64, qc *flow.QuorumCertificate) {
   124  	lc.log.Debug().
   125  		Uint64("proposal_view", view).
   126  		Uint64("qc_view", qc.View).
   127  		Hex("qc_id", qc.BlockID[:]).
   128  		Msg("fork choice generated")
   129  }
   130  
   131  func (lc *LogConsumer) OnDoubleVotingDetected(vote *model.Vote, alt *model.Vote) {
   132  	lc.log.Warn().
   133  		Uint64("vote_view", vote.View).
   134  		Hex("voted_block_id", vote.BlockID[:]).
   135  		Hex("alt_id", alt.BlockID[:]).
   136  		Hex("voter_id", vote.SignerID[:]).
   137  		Msg("double vote detected")
   138  }
   139  
   140  func (lc *LogConsumer) OnInvalidVoteDetected(vote *model.Vote) {
   141  	lc.log.Warn().
   142  		Uint64("vote_view", vote.View).
   143  		Hex("voted_block_id", vote.BlockID[:]).
   144  		Hex("voter_id", vote.SignerID[:]).
   145  		Msg("invalid vote detected")
   146  }
   147  
   148  func (lc *LogConsumer) OnVoteForInvalidBlockDetected(vote *model.Vote, proposal *model.Proposal) {
   149  	lc.log.Warn().
   150  		Uint64("vote_view", vote.View).
   151  		Hex("voted_block_id", vote.BlockID[:]).
   152  		Hex("voter_id", vote.SignerID[:]).
   153  		Hex("proposer_id", proposal.Block.ProposerID[:]).
   154  		Msg("vote for invalid proposal detected")
   155  }
   156  
   157  func (lc *LogConsumer) logBasicBlockData(loggerEvent *zerolog.Event, block *model.Block) *zerolog.Event {
   158  	loggerEvent.
   159  		Uint64("block_view", block.View).
   160  		Hex("block_id", logging.ID(block.BlockID)).
   161  		Hex("proposer_id", logging.ID(block.ProposerID)).
   162  		Hex("payload_hash", logging.ID(block.PayloadHash))
   163  	if block.QC != nil {
   164  		loggerEvent.
   165  			Uint64("qc_view", block.QC.View).
   166  			Hex("qc_id", logging.ID(block.QC.BlockID))
   167  	}
   168  	return loggerEvent
   169  }