Skip to content

Commit 930a2e0

Browse files
committed
Fixed excess propose on epoch change.
1 parent 2e904ec commit 930a2e0

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

consensus/consensus.go

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

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

consensus/consensus_service.go

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

consensus/consensus_v2.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ func (consensus *Consensus) _finalCommit(isLeader bool) {
234234
}
235235

236236
block.SetCurrentCommitSig(commitSigAndBitmap)
237+
// commitBlock internally calls setupForNewConsensus, which updates leader key
237238
err = consensus.commitBlock(block, FBFTMsg)
238239

239240
if err != nil || consensus.BlockNum()-beforeCatchupNum != 1 {
@@ -290,10 +291,13 @@ func (consensus *Consensus) _finalCommit(isLeader bool) {
290291
if isLeader {
291292
if block.IsLastBlockInEpoch() {
292293
// No pipelining
293-
go func() {
294-
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")
296-
}()
294+
trace := utils.GetStackTrace(1)
295+
if consensus.isMyKey(consensus.getLeaderPubKey()) {
296+
go func() {
297+
consensus.getLogger().Info().Msg("[finalCommit] sending block proposal signal")
298+
consensus.ReadySignal(NewProposal(SyncProposal, block.NumberU64()+1, trace), "finalCommit", "I am leader and it's the last block in epoch")
299+
}()
300+
}
297301
} else {
298302
// pipelining
299303
go func() {
@@ -726,6 +730,9 @@ func (consensus *Consensus) commitBlock(blk *types.Block, committedMsg *FBFTMess
726730
// rotateLeader rotates the leader to the next leader in the committee.
727731
// This function must be called with enabled leader rotation.
728732
func (consensus *Consensus) rotateLeader(epoch *big.Int, defaultKey *bls.PublicKeyWrapper) *bls.PublicKeyWrapper {
733+
if !consensus.isRotation(epoch) {
734+
return defaultKey
735+
}
729736
var (
730737
bc = consensus.Blockchain()
731738
leader = consensus.getLeaderPubKey()
@@ -882,9 +889,10 @@ func (consensus *Consensus) setupForNewConsensus(blk *types.Block, committedMsg
882889
if consensus.isLeader() && newLeader && !wasLeader {
883890
// leader changed
884891
blockPeriod := consensus.BlockPeriod
892+
trace := utils.GetStackTrace(1)
885893
go func() {
886894
<-time.After(blockPeriod)
887-
consensus.ReadySignal(NewProposal(SyncProposal, blk.NumberU64()+1), "setupForNewConsensus", "I am the new leader")
895+
consensus.ReadySignal(NewProposal(SyncProposal, blk.NumberU64()+1, trace), "setupForNewConsensus", "I am the new leader")
888896
}()
889897
}
890898
}

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+
}

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)