Skip to content

Commit ac3afca

Browse files
committed
Fixed excess propose on epoch change.
1 parent 6ff118c commit ac3afca

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

Diff for: consensus/consensus.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,17 @@ type Proposal struct {
3838
}
3939

4040
// NewProposal creates a new proposal
41-
func NewProposal(t ProposalType, blockNum uint64) Proposal {
41+
func NewProposal(t ProposalType, blockNum uint64, stackTrace ...string) Proposal {
42+
if len(stackTrace) > 0 {
43+
return Proposal{
44+
Type: t,
45+
Caller: stackTrace[0],
46+
blockNum: blockNum,
47+
}
48+
}
4249
return Proposal{
4350
Type: t,
44-
Caller: utils.GetCallStackInfo(2),
51+
Caller: utils.GetStackTrace(2),
4552
blockNum: blockNum,
4653
}
4754
}

Diff for: consensus/consensus_service.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,12 @@ func (consensus *Consensus) updateConsensusInformation(reason string) Mode {
496496
// If the leader changed and I myself become the leader
497497
if (oldLeader != nil && consensus.getLeaderPubKey() != nil &&
498498
!consensus.getLeaderPubKey().Object.IsEqual(oldLeader.Object)) && consensus.isLeader() {
499+
trace := utils.GetStackTrace(1)
499500
go func() {
500501
consensus.GetLogger().Info().
501502
Str("myKey", myPubKeys.SerializeToHexStr()).
502503
Msg("[UpdateConsensusInformation] I am the New Leader")
503-
consensus.ReadySignal(NewProposal(SyncProposal, curHeader.NumberU64()+1), "updateConsensusInformation", "leader changed and I am the new leader")
504+
consensus.ReadySignal(NewProposal(SyncProposal, curHeader.NumberU64()+1, trace), "updateConsensusInformation", "leader changed and I am the new leader")
504505
}()
505506
}
506507
return Normal

Diff for: consensus/consensus_v2.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ func (consensus *Consensus) _finalCommit(isLeader bool) {
233233
}
234234

235235
block.SetCurrentCommitSig(commitSigAndBitmap)
236+
// commitBlock internally calls setupForNewConsensus, which updates leader key
236237
err = consensus.commitBlock(block, FBFTMsg)
237238

238239
if err != nil || consensus.BlockNum()-beforeCatchupNum != 1 {
@@ -289,10 +290,11 @@ func (consensus *Consensus) _finalCommit(isLeader bool) {
289290
if isLeader {
290291
if block.IsLastBlockInEpoch() {
291292
// No pipelining
292-
if !consensus.isRotation(block.Epoch()) {
293+
trace := utils.GetStackTrace(1)
294+
if consensus.isMyKey(consensus.getLeaderPubKey()) && !consensus.isRotation(block.Epoch()) {
293295
go func() {
294296
consensus.getLogger().Info().Msg("[finalCommit] sending block proposal signal")
295-
consensus.ReadySignal(NewProposal(SyncProposal, block.NumberU64()+1), "finalCommit", "I am leader and it's the last block in epoch")
297+
consensus.ReadySignal(NewProposal(SyncProposal, block.NumberU64()+1, trace), "finalCommit", "I am leader and it's the last block in epoch")
296298
}()
297299
}
298300
} else {
@@ -727,6 +729,9 @@ func (consensus *Consensus) commitBlock(blk *types.Block, committedMsg *FBFTMess
727729
// rotateLeader rotates the leader to the next leader in the committee.
728730
// This function must be called with enabled leader rotation.
729731
func (consensus *Consensus) rotateLeader(epoch *big.Int, defaultKey *bls.PublicKeyWrapper) *bls.PublicKeyWrapper {
732+
if !consensus.isRotation(epoch) {
733+
return defaultKey
734+
}
730735
var (
731736
bc = consensus.Blockchain()
732737
leader = consensus.getLeaderPubKey()
@@ -883,9 +888,10 @@ func (consensus *Consensus) setupForNewConsensus(blk *types.Block, committedMsg
883888
if consensus.isLeader() && newLeader && !wasLeader {
884889
// leader changed
885890
blockPeriod := consensus.BlockPeriod
891+
trace := utils.GetStackTrace(1)
886892
go func() {
887893
<-time.After(blockPeriod)
888-
consensus.ReadySignal(NewProposal(SyncProposal, blk.NumberU64()+1), "setupForNewConsensus", "I am the new leader")
894+
consensus.ReadySignal(NewProposal(SyncProposal, blk.NumberU64()+1, trace), "setupForNewConsensus", "I am the new leader")
889895
}()
890896
}
891897
}

Diff for: internal/utils/utils.go

+16
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,19 @@ func FatalError(err error) {
277277
func FatalErrMsg(err error, format string, args ...interface{}) {
278278
FatalError(errors.WithMessagef(err, format, args...))
279279
}
280+
281+
func GetStackTrace(i int) string {
282+
stackBuf := strings.Builder{}
283+
284+
for ; ; i++ {
285+
_, file, line, ok := runtime.Caller(i)
286+
if !ok {
287+
break
288+
}
289+
stackBuf.WriteString(file)
290+
stackBuf.WriteString(":")
291+
stackBuf.WriteString(fmt.Sprint(line))
292+
stackBuf.WriteString(" ")
293+
}
294+
return stackBuf.String()
295+
}

Diff for: internal/utils/utils_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,10 @@ func TestIsPrivateIP(t *testing.T) {
130130
}
131131
}
132132
}
133+
134+
func TestGetStackTrace(t *testing.T) {
135+
stack := GetStackTrace()
136+
if len(stack) == 0 {
137+
t.Errorf("GetStackTrace failed")
138+
}
139+
}

0 commit comments

Comments
 (0)