From 1d633712dfe3d04ecfd5a1710fbc44bb109748f2 Mon Sep 17 00:00:00 2001 From: frozen <355847+Frozen@users.noreply.github.com> Date: Mon, 11 Mar 2024 21:50:00 -0400 Subject: [PATCH] Added `Proposal` along with `ProposalType` --- consensus/consensus.go | 19 +++++++++++++++---- consensus/consensus_service.go | 2 +- consensus/consensus_v2.go | 8 ++++---- consensus/view_change.go | 2 +- internal/utils/singleton.go | 7 +++++++ internal/utils/singleton_test.go | 13 +++++++++++++ node/node_newblock.go | 4 ++-- 7 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 internal/utils/singleton_test.go diff --git a/consensus/consensus.go b/consensus/consensus.go index 63f18a85e..1c26d51ba 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -34,6 +34,17 @@ var errLeaderPriKeyNotFound = errors.New("leader private key not found locally") // ProposalType is to indicate the type of signal for new block proposal type ProposalType byte +// Proposal is to indicate the type of signal for new block proposal +type Proposal struct { + Type ProposalType + Message string +} + +// NewProposal creates a new proposal +func NewProposal(t ProposalType) Proposal { + return Proposal{Type: t, Message: utils.FileNo()} +} + // Constant of the type of new block proposal const ( SyncProposal ProposalType = iota @@ -90,7 +101,7 @@ type Consensus struct { // ViewChange struct vc *viewChange // Signal channel for proposing a new block and start new consensus - readySignal chan ProposalType + readySignal chan Proposal // Channel to send full commit signatures to finish new block proposal commitSigChannel chan []byte // The post-consensus job func passed from Node object @@ -151,11 +162,11 @@ func (consensus *Consensus) ChainReader() engine.ChainReader { return consensus.Blockchain() } -func (consensus *Consensus) ReadySignal(p ProposalType) { +func (consensus *Consensus) ReadySignal(p Proposal) { consensus.readySignal <- p } -func (consensus *Consensus) GetReadySignal() chan ProposalType { +func (consensus *Consensus) GetReadySignal() chan Proposal { return consensus.readySignal } @@ -304,7 +315,7 @@ func New( // displayed on explorer as Height right now consensus.setCurBlockViewID(0) consensus.SlashChan = make(chan slash.Record) - consensus.readySignal = make(chan ProposalType) + consensus.readySignal = make(chan Proposal) consensus.commitSigChannel = make(chan []byte) // channel for receiving newly generated VDF consensus.RndChannel = make(chan [vdfAndSeedSize]byte) diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index d658fe83d..873606ca6 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -459,7 +459,7 @@ func (consensus *Consensus) updateConsensusInformation() Mode { consensus.GetLogger().Info(). Str("myKey", myPubKeys.SerializeToHexStr()). Msg("[UpdateConsensusInformation] I am the New Leader") - consensus.ReadySignal(SyncProposal) + consensus.ReadySignal(NewProposal(SyncProposal)) }() } return Normal diff --git a/consensus/consensus_v2.go b/consensus/consensus_v2.go index 33ba54b1d..822d23900 100644 --- a/consensus/consensus_v2.go +++ b/consensus/consensus_v2.go @@ -258,7 +258,7 @@ func (consensus *Consensus) finalCommit() { // No pipelining go func() { consensus.getLogger().Info().Msg("[finalCommit] sending block proposal signal") - consensus.ReadySignal(SyncProposal) + consensus.ReadySignal(NewProposal(SyncProposal)) }() } else { // pipelining @@ -334,7 +334,7 @@ func (consensus *Consensus) StartChannel() { consensus.start = true consensus.getLogger().Info().Time("time", time.Now()).Msg("[ConsensusMainLoop] Send ReadySignal") consensus.mutex.Unlock() - consensus.ReadySignal(SyncProposal) + consensus.ReadySignal(NewProposal(SyncProposal)) return } consensus.mutex.Unlock() @@ -586,7 +586,7 @@ func (consensus *Consensus) preCommitAndPropose(blk *types.Block) error { // Send signal to Node to propose the new block for consensus consensus.getLogger().Info().Msg("[preCommitAndPropose] sending block proposal signal") consensus.mutex.Unlock() - consensus.ReadySignal(AsyncProposal) + consensus.ReadySignal(NewProposal(AsyncProposal)) }() return nil @@ -814,7 +814,7 @@ func (consensus *Consensus) setupForNewConsensus(blk *types.Block, committedMsg blockPeriod := consensus.BlockPeriod go func() { <-time.After(blockPeriod) - consensus.ReadySignal(SyncProposal) + consensus.ReadySignal(NewProposal(SyncProposal)) }() } } diff --git a/consensus/view_change.go b/consensus/view_change.go index f55c5400f..5525e1226 100644 --- a/consensus/view_change.go +++ b/consensus/view_change.go @@ -429,7 +429,7 @@ func (consensus *Consensus) onViewChange(recvMsg *FBFTMessage) { consensus.getLogger().Error().Err(err).Msg("[onViewChange] startNewView failed") return } - go consensus.ReadySignal(SyncProposal) + go consensus.ReadySignal(NewProposal(SyncProposal)) return } diff --git a/internal/utils/singleton.go b/internal/utils/singleton.go index 10101d767..bc3402224 100644 --- a/internal/utils/singleton.go +++ b/internal/utils/singleton.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path" + "runtime" "strconv" "sync" "time" @@ -212,3 +213,9 @@ func GetPort() int { } return 0 } + +// FileNo returns source code file and line +func FileNo() string { + _, file, line, _ := runtime.Caller(1) + return fmt.Sprintf("%s:%d", file, line) +} diff --git a/internal/utils/singleton_test.go b/internal/utils/singleton_test.go new file mode 100644 index 000000000..e93754318 --- /dev/null +++ b/internal/utils/singleton_test.go @@ -0,0 +1,13 @@ +package utils + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestFileNo(t *testing.T) { + f1 := FileNo() + f2 := FileNo() + require.NotEqual(t, f1, f2) +} diff --git a/node/node_newblock.go b/node/node_newblock.go index bafb340a8..746835d4c 100644 --- a/node/node_newblock.go +++ b/node/node_newblock.go @@ -53,7 +53,7 @@ func (node *Node) WaitForConsensusReadyV2(cs *consensus.Consensus, stopChan chan time.Sleep(SleepPeriod) utils.Logger().Info(). Uint64("blockNum", cs.Blockchain().CurrentBlock().NumberU64()+1). - Bool("asyncProposal", proposalType == consensus.AsyncProposal). + Bool("asyncProposal", proposalType.Type == consensus.AsyncProposal). Msg("PROPOSING NEW BLOCK ------------------------------------------------") // Prepare last commit signatures @@ -61,7 +61,7 @@ func (node *Node) WaitForConsensusReadyV2(cs *consensus.Consensus, stopChan chan go func() { waitTime := 0 * time.Second - if proposalType == consensus.AsyncProposal { + if proposalType.Type == consensus.AsyncProposal { waitTime = consensus.CommitSigReceiverTimeout } select {