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

Refactor consensus #6483

Merged
merged 30 commits into from
Oct 3, 2024
Merged

Refactor consensus #6483

merged 30 commits into from
Oct 3, 2024

Conversation

AdoAdoAdo
Copy link
Contributor

@AdoAdoAdo AdoAdoAdo commented Sep 20, 2024

Reasoning behind the pull request

  • In order to create a smooth transition from consensus v1 to consensus v2, and to simplify the v2 logic, the new implementation should be clearer and better isolated

Proposed changes

  • Add a separate package for v1 and v2 consensus model
  • The v2 implementation should not handle also v1 consensus until the transition
  • Create a proxy component to handle the transition between the two versions, at the right time (activation epoch).

Testing procedure

  • regular system test - monitor especially the transition between the two consensus versions
  • adversarial - check with invalid transition blocks (part of the network not following the v2 protocol for a few blocks)

Pre-requisites

Based on the Contributing Guidelines the PR author and the reviewers must check the following requirements are met:

  • was the PR targeted to the correct branch?
  • if this is a larger feature that probably needs more than one PR, is there a feat branch created?
  • if this is a feat branch merging, do all satellite projects have a proper tag inside go.mod?

@AdoAdoAdo AdoAdoAdo marked this pull request as ready for review September 30, 2024 15:23
@@ -89,7 +56,7 @@ const (
BlockDefaultStringValue = "Undefined message type"
)

func getStringValue(msgType consensus.MessageType) string {
func GetStringValue(msgType consensus.MessageType) string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 36 to 40
type SubroundsFactory interface {
GenerateSubrounds() error
SetOutportHandler(driver outport.OutportHandler)
IsInterfaceNil() bool
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be moved to interface.go.. also, it is only used as part of this package, so perhaps unexport it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unexported it

Comment on lines 60 to 64
const (
ConsensusNone ConsensusStateMachineType = iota
ConsensusV1
ConsensusV2
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing comments.. also, do we need them exported? perhaps move them to a constants.go file, with L42

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unexported the contants

IsInterfaceNil() bool
}

type ConsensusStateMachineType int
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing comment, not sure if it is needed as exported

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unexported it

ConsensusV2
)

func NewSubroundsHandler(args *SubroundsHandlerArgs) (*SubroundsHandler, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compared with bls/subroundBlock.go from latest rc, no difference ✅

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compared with bls/subroundEndRound.go from latest rc, no difference ✅

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compared with bls/subroundSignature.go from latest rc, no difference ✅

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compared with bls/subroundStartRound.go from latest rc, similar ✅

return eligibleList
}

func CreateEligibleListFromMap(mapKeys map[string]crypto.PrivateKey) []string {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing comments on exported methods

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines +44 to +45
// SubroundsHandler struct contains the needed data for the SubroundsHandler
type SubroundsHandler struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this needs to be exported?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should start using exported types for constructors returns, even if we keep the internal fields unexported.
It would make it much simpler to work with these types

signatureThrottler core.Throttler,
) (spos.SubroundsFactory, error) {
switch consensusType {
case blsConsensusType:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we used blsConsensusType, now we have other versioning, do we need blsConsensusType const anymore? or maybe considering blsConsensusV1 and 2 for newly added versions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type here was for the type of votes used during consensus.
both versions use bls signatures as votes.
If we change to something else we could add the switch again, until then I don't think we need it

Comment on lines +433 to +438
sh.currentConsensusType = consensusNone
sh.EpochStartAction(&testscommon.HeaderHandlerStub{})
require.Nil(t, err)
require.Equal(t, consensusV1, sh.currentConsensusType)
require.Equal(t, int32(1), startCalled.Load())
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test case here for the transitions scenario from v1 to v2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added as part of the initSubroundsForEpoch tests (which will get called by epochStartAction)

logger "github.com/multiversx/mx-chain-logger-go"
)

var log = logger.GetOrCreate("consensus/spos/bls")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var log = logger.GetOrCreate("consensus/spos/bls")
var log = logger.GetOrCreate("consensus/spos/bls/v1")

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

logger "github.com/multiversx/mx-chain-logger-go"
)

var log = logger.GetOrCreate("consensus/spos/bls")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var log = logger.GetOrCreate("consensus/spos/bls")
var log = logger.GetOrCreate("consensus/spos/bls/v2")

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines +399 to +400
func (cns *ConsensusState) SetRoundCanceled(roundCanceled bool) {
cns.RoundCanceled = roundCanceled
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can now unexport the internal fields, todo for the next PRs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can also unexport ConsensusState

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would unexport maybe just the fields.

"github.com/stretchr/testify/require"

chainCommon "github.com/multiversx/mx-chain-go/common"
mock2 "github.com/multiversx/mx-chain-go/consensus/mock"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mock2 -> consensusMock

@AdoAdoAdo AdoAdoAdo merged commit 5624d0c into feat/equivalent-messages Oct 3, 2024
5 checks passed
@AdoAdoAdo AdoAdoAdo deleted the refactor-consensus branch October 3, 2024 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants