Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/skip waiting 100 sign with lock free message validation #4740

Draft
wants to merge 9 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Pre commit 1s version.
  • Loading branch information
Frozen committed Aug 19, 2024
commit 3eca61552616f21a7fa56480aeee575f96334d27
72 changes: 72 additions & 0 deletions consensus/consensus_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,78 @@ func (consensus *Consensus) preCommitAndPropose(blk *types.Block) error {
return nil
}

// preCommitAndPropose commit the current block with 67% commit signatures and start
// proposing new block which will wait on the full commit signatures to finish
func (consensus *Consensus) preCommitAndPropose1s(blk *types.Block) error {
if blk == nil {
return errors.New("block to pre-commit is nil")
}

leaderPriKey, err := consensus.getConsensusLeaderPrivateKey()
if err != nil {
consensus.getLogger().Error().Err(err).Msg("[preCommitAndPropose] leader not found")
return err
}

// Construct committed message
network, err := consensus.construct(msg_pb.MessageType_COMMITTED, nil, []*bls.PrivateKeyWrapper{leaderPriKey})
if err != nil {
consensus.getLogger().Warn().Err(err).
Msg("[preCommitAndPropose] Unable to construct Committed message")
return err
}

msgToSend, FBFTMsg :=
network.Bytes,
network.FBFTMsg
bareMinimumCommit := FBFTMsg.Payload
consensus.fBFTLog.AddVerifiedMessage(FBFTMsg)

if err := consensus.verifyLastCommitSig(bareMinimumCommit, blk); err != nil {
return errors.Wrap(err, "[preCommitAndPropose] failed verifying last commit sig")
}

go func() {
blk.SetCurrentCommitSig(bareMinimumCommit)

// Send committed message to validators since 2/3 commit is already collected
if err := consensus.msgSender.SendWithRetry(
blk.NumberU64(),
msg_pb.MessageType_COMMITTED, []nodeconfig.GroupID{
nodeconfig.NewGroupIDByShardID(nodeconfig.ShardID(consensus.ShardID)),
},
p2p.ConstructMessage(msgToSend)); err != nil {
consensus.GetLogger().Warn().Err(err).Msg("[preCommitAndPropose] Cannot send committed message")
} else {
consensus.GetLogger().Info().
Str("blockHash", blk.Hash().Hex()).
Uint64("blockNum", consensus.BlockNum()).
Hex("lastCommitSig", bareMinimumCommit).
Msg("[preCommitAndPropose] Sent Committed Message")
}

if _, err := consensus.Blockchain().InsertChain([]*types.Block{blk}, !consensus.FBFTLog().IsBlockVerified(blk.Hash())); err != nil {
switch {
case errors.Is(err, core.ErrKnownBlock):
consensus.GetLogger().Info().Msg("[preCommitAndPropose] Block already known")
default:
consensus.GetLogger().Error().Err(err).Msg("[preCommitAndPropose] Failed to add block to chain")
return
}
}
consensus.mutex.Lock()
consensus.getLogger().Info().Msg("[preCommitAndPropose] Start consensus timer")
consensus.consensusTimeout[timeoutConsensus].Start()

// 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(NewProposal(AsyncProposal))
}()

return nil
}

func (consensus *Consensus) verifyLastCommitSig(lastCommitSig []byte, blk *types.Block) error {
if len(lastCommitSig) < bls.BLSSignatureSizeInBytes {
return errors.New("lastCommitSig not have enough length")
Expand Down
2 changes: 1 addition & 1 deletion consensus/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (consensus *Consensus) onCommit(recvMsg *FBFTMessage) {
if consensus.Blockchain().Config().IsOneSecond(currentHeader.Epoch()) {
if !blockObj.IsLastBlockInEpoch() {
// only do early commit if it's not epoch block to avoid problems
consensus.preCommitAndPropose(blockObj)
consensus.preCommitAndPropose1s(blockObj)
}
go consensus.finalCommit1s(viewID, consensus.NextBlockDue)
} else {
Expand Down