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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9f2f0cf
versioning for consensus with and without equivalent messages
AdoAdoAdo Sep 19, 2024
d20c0e2
fixes for consensus v1 - after initial split
AdoAdoAdo Sep 20, 2024
c815841
extract exported constants
AdoAdoAdo Sep 20, 2024
5f82c80
adapt v2
AdoAdoAdo Sep 20, 2024
9ec2d11
fix start round reset
AdoAdoAdo Sep 20, 2024
ba61a2d
tests fixes
AdoAdoAdo Sep 20, 2024
f474f14
cleanup and v1 unit tests fixes
AdoAdoAdo Sep 23, 2024
4246b29
fix v1 consensus package unit tests
AdoAdoAdo Sep 23, 2024
b8ca228
extract common worker for v1 and v2
AdoAdoAdo Sep 23, 2024
1c6f59a
add handler for consensus subrounds stm
AdoAdoAdo Sep 25, 2024
29b083d
fix cyclic import
AdoAdoAdo Sep 25, 2024
299bc50
fix unit tests
AdoAdoAdo Sep 25, 2024
357d2c2
fix missing proofs pool in unit tests
AdoAdoAdo Sep 26, 2024
19fcca2
fix linter
AdoAdoAdo Sep 26, 2024
381e376
register subrounds handler on epoch trigger
AdoAdoAdo Sep 27, 2024
308a952
remove unused method
AdoAdoAdo Sep 27, 2024
27dbc82
renaming import aliases
AdoAdoAdo Sep 27, 2024
9f14328
extract interfaces for consensus state
AdoAdoAdo Sep 27, 2024
488dd06
adapt to use interface instead of struct pointer
AdoAdoAdo Sep 27, 2024
ae7f18a
fixes
AdoAdoAdo Sep 27, 2024
1b85621
fixes unit tests
AdoAdoAdo Sep 27, 2024
30df93e
fixes unit tests - part 2
AdoAdoAdo Sep 27, 2024
07c6afe
fixes unit tests - part 3
AdoAdoAdo Sep 27, 2024
c7b4ef7
add constructor tests
AdoAdoAdo Sep 27, 2024
351c118
fix cyclic import
AdoAdoAdo Sep 27, 2024
7052ecb
update consensusStateMock
AdoAdoAdo Sep 30, 2024
9edc7c7
add initSubroundsForEpoch tests
AdoAdoAdo Sep 30, 2024
c8648e7
add more unit tests for the consensus proxy
AdoAdoAdo Sep 30, 2024
492041c
fixes after review
AdoAdoAdo Oct 1, 2024
3163799
add outport handler on factory constructor
AdoAdoAdo Oct 2, 2024
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
6 changes: 4 additions & 2 deletions common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,10 @@ const (
ChainParametersOrder
// NodesCoordinatorOrder defines the order in which NodesCoordinator is notified of a start of epoch event
NodesCoordinatorOrder
// ConsensusOrder defines the order in which Consensus is notified of a start of epoch event
ConsensusOrder
// ConsensusHandlerOrder defines the order in which ConsensusHandler is notified of a start of epoch event
ConsensusHandlerOrder
// ConsensusStartRoundOrder defines the order in which Consensus StartRound subround is notified of a start of epoch event
ConsensusStartRoundOrder
// NetworkShardingOrder defines the order in which the network sharding subsystem is notified of a start of epoch event
NetworkShardingOrder
// IndexerOrder defines the order in which indexer is notified of a start of epoch event
Expand Down
19 changes: 10 additions & 9 deletions consensus/chronology/chronology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/multiversx/mx-chain-go/consensus"
"github.com/multiversx/mx-chain-go/consensus/chronology"
"github.com/multiversx/mx-chain-go/consensus/mock"
consensusMocks "github.com/multiversx/mx-chain-go/testscommon/consensus"
statusHandlerMock "github.com/multiversx/mx-chain-go/testscommon/statusHandler"
)

Expand Down Expand Up @@ -117,7 +118,7 @@ func TestChronology_StartRoundShouldReturnWhenRoundIndexIsNegative(t *testing.T)
t.Parallel()

arg := getDefaultChronologyArg()
roundHandlerMock := &mock.RoundHandlerMock{}
roundHandlerMock := &consensusMocks.RoundHandlerMock{}
roundHandlerMock.IndexCalled = func() int64 {
return -1
}
Expand Down Expand Up @@ -151,7 +152,7 @@ func TestChronology_StartRoundShouldReturnWhenDoWorkReturnsFalse(t *testing.T) {
t.Parallel()

arg := getDefaultChronologyArg()
roundHandlerMock := &mock.RoundHandlerMock{}
roundHandlerMock := &consensusMocks.RoundHandlerMock{}
roundHandlerMock.UpdateRound(roundHandlerMock.TimeStamp(), roundHandlerMock.TimeStamp().Add(roundHandlerMock.TimeDuration()))
arg.RoundHandler = roundHandlerMock
chr, _ := chronology.NewChronology(arg)
Expand All @@ -168,7 +169,7 @@ func TestChronology_StartRoundShouldWork(t *testing.T) {
t.Parallel()

arg := getDefaultChronologyArg()
roundHandlerMock := &mock.RoundHandlerMock{}
roundHandlerMock := &consensusMocks.RoundHandlerMock{}
roundHandlerMock.UpdateRound(roundHandlerMock.TimeStamp(), roundHandlerMock.TimeStamp().Add(roundHandlerMock.TimeDuration()))
arg.RoundHandler = roundHandlerMock
chr, _ := chronology.NewChronology(arg)
Expand Down Expand Up @@ -221,7 +222,7 @@ func TestChronology_InitRoundShouldNotSetSubroundWhenRoundIndexIsNegative(t *tes
t.Parallel()

arg := getDefaultChronologyArg()
roundHandlerMock := &mock.RoundHandlerMock{}
roundHandlerMock := &consensusMocks.RoundHandlerMock{}
arg.RoundHandler = roundHandlerMock
arg.GenesisTime = arg.SyncTimer.CurrentTime()
chr, _ := chronology.NewChronology(arg)
Expand All @@ -242,7 +243,7 @@ func TestChronology_InitRoundShouldSetSubroundWhenRoundIndexIsPositive(t *testin
t.Parallel()

arg := getDefaultChronologyArg()
roundHandlerMock := &mock.RoundHandlerMock{}
roundHandlerMock := &consensusMocks.RoundHandlerMock{}
roundHandlerMock.UpdateRound(roundHandlerMock.TimeStamp(), roundHandlerMock.TimeStamp().Add(roundHandlerMock.TimeDuration()))
arg.RoundHandler = roundHandlerMock
arg.GenesisTime = arg.SyncTimer.CurrentTime()
Expand All @@ -259,7 +260,7 @@ func TestChronology_StartRoundShouldNotUpdateRoundWhenCurrentRoundIsNotFinished(
t.Parallel()

arg := getDefaultChronologyArg()
roundHandlerMock := &mock.RoundHandlerMock{}
roundHandlerMock := &consensusMocks.RoundHandlerMock{}
arg.RoundHandler = roundHandlerMock
arg.GenesisTime = arg.SyncTimer.CurrentTime()
chr, _ := chronology.NewChronology(arg)
Expand All @@ -273,7 +274,7 @@ func TestChronology_StartRoundShouldNotUpdateRoundWhenCurrentRoundIsNotFinished(
func TestChronology_StartRoundShouldUpdateRoundWhenCurrentRoundIsFinished(t *testing.T) {
t.Parallel()
arg := getDefaultChronologyArg()
roundHandlerMock := &mock.RoundHandlerMock{}
roundHandlerMock := &consensusMocks.RoundHandlerMock{}
arg.RoundHandler = roundHandlerMock
arg.GenesisTime = arg.SyncTimer.CurrentTime()
chr, _ := chronology.NewChronology(arg)
Expand Down Expand Up @@ -317,8 +318,8 @@ func TestChronology_CheckIfStatusHandlerWorks(t *testing.T) {
func getDefaultChronologyArg() chronology.ArgChronology {
return chronology.ArgChronology{
GenesisTime: time.Now(),
RoundHandler: &mock.RoundHandlerMock{},
SyncTimer: &mock.SyncTimerMock{},
RoundHandler: &consensusMocks.RoundHandlerMock{},
SyncTimer: &consensusMocks.SyncTimerMock{},
AppStatusHandler: statusHandlerMock.NewAppStatusHandlerMock(),
Watchdog: &mock.WatchdogMock{},
}
Expand Down
1 change: 1 addition & 0 deletions consensus/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data"
crypto "github.com/multiversx/mx-chain-crypto-go"

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

Expand Down
20 changes: 11 additions & 9 deletions consensus/round/round_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"time"

"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-go/consensus/mock"

"github.com/multiversx/mx-chain-go/consensus/round"
consensusMocks "github.com/multiversx/mx-chain-go/testscommon/consensus"

"github.com/stretchr/testify/assert"
)

Expand All @@ -28,7 +30,7 @@ func TestRound_NewRoundShouldWork(t *testing.T) {

genesisTime := time.Now()

syncTimerMock := &mock.SyncTimerMock{}
syncTimerMock := &consensusMocks.SyncTimerMock{}

rnd, err := round.NewRound(genesisTime, genesisTime, roundTimeDuration, syncTimerMock, 0)

Expand All @@ -41,7 +43,7 @@ func TestRound_UpdateRoundShouldNotChangeAnything(t *testing.T) {

genesisTime := time.Now()

syncTimerMock := &mock.SyncTimerMock{}
syncTimerMock := &consensusMocks.SyncTimerMock{}

rnd, _ := round.NewRound(genesisTime, genesisTime, roundTimeDuration, syncTimerMock, 0)
oldIndex := rnd.Index()
Expand All @@ -61,7 +63,7 @@ func TestRound_UpdateRoundShouldAdvanceOneRound(t *testing.T) {

genesisTime := time.Now()

syncTimerMock := &mock.SyncTimerMock{}
syncTimerMock := &consensusMocks.SyncTimerMock{}

rnd, _ := round.NewRound(genesisTime, genesisTime, roundTimeDuration, syncTimerMock, 0)
oldIndex := rnd.Index()
Expand All @@ -76,7 +78,7 @@ func TestRound_IndexShouldReturnFirstIndex(t *testing.T) {

genesisTime := time.Now()

syncTimerMock := &mock.SyncTimerMock{}
syncTimerMock := &consensusMocks.SyncTimerMock{}

rnd, _ := round.NewRound(genesisTime, genesisTime, roundTimeDuration, syncTimerMock, 0)
rnd.UpdateRound(genesisTime, genesisTime.Add(roundTimeDuration/2))
Expand All @@ -90,7 +92,7 @@ func TestRound_TimeStampShouldReturnTimeStampOfTheNextRound(t *testing.T) {

genesisTime := time.Now()

syncTimerMock := &mock.SyncTimerMock{}
syncTimerMock := &consensusMocks.SyncTimerMock{}

rnd, _ := round.NewRound(genesisTime, genesisTime, roundTimeDuration, syncTimerMock, 0)
rnd.UpdateRound(genesisTime, genesisTime.Add(roundTimeDuration+roundTimeDuration/2))
Expand All @@ -104,7 +106,7 @@ func TestRound_TimeDurationShouldReturnTheDurationOfOneRound(t *testing.T) {

genesisTime := time.Now()

syncTimerMock := &mock.SyncTimerMock{}
syncTimerMock := &consensusMocks.SyncTimerMock{}

rnd, _ := round.NewRound(genesisTime, genesisTime, roundTimeDuration, syncTimerMock, 0)
timeDuration := rnd.TimeDuration()
Expand All @@ -117,7 +119,7 @@ func TestRound_RemainingTimeInCurrentRoundShouldReturnPositiveValue(t *testing.T

genesisTime := time.Unix(0, 0)

syncTimerMock := &mock.SyncTimerMock{}
syncTimerMock := &consensusMocks.SyncTimerMock{}

timeElapsed := int64(roundTimeDuration - 1)

Expand All @@ -138,7 +140,7 @@ func TestRound_RemainingTimeInCurrentRoundShouldReturnNegativeValue(t *testing.T

genesisTime := time.Unix(0, 0)

syncTimerMock := &mock.SyncTimerMock{}
syncTimerMock := &consensusMocks.SyncTimerMock{}

timeElapsed := int64(roundTimeDuration + 1)

Expand Down
22 changes: 11 additions & 11 deletions consensus/spos/bls/blsWorker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/multiversx/mx-chain-go/consensus/spos"
)

// peerMaxMessagesPerSec defines how many messages can be propagated by a pid in a round. The value was chosen by
// PeerMaxMessagesPerSec defines how many messages can be propagated by a pid in a round. The value was chosen by
// following the next premises:
// 1. a leader can propagate as maximum as 3 messages per round: proposed header block + proposed body + final info;
// 2. due to the fact that a delayed signature of the proposer (from previous round) can be received in the current round
Expand All @@ -16,15 +16,15 @@ import (
//
// Validators only send one signature message in a round, treating the edge case of a delayed message, will need at most
// 2 messages per round (which is ok as it is below the set value of 5)
const peerMaxMessagesPerSec = uint32(6)
const PeerMaxMessagesPerSec = uint32(6)

// defaultMaxNumOfMessageTypeAccepted represents the maximum number of the same message type accepted in one round to be
// DefaultMaxNumOfMessageTypeAccepted represents the maximum number of the same message type accepted in one round to be
// received from the same public key for the default message types
const defaultMaxNumOfMessageTypeAccepted = uint32(1)
const DefaultMaxNumOfMessageTypeAccepted = uint32(1)

// maxNumOfMessageTypeSignatureAccepted represents the maximum number of the signature message type accepted in one round to be
// MaxNumOfMessageTypeSignatureAccepted represents the maximum number of the signature message type accepted in one round to be
// received from the same public key
const maxNumOfMessageTypeSignatureAccepted = uint32(2)
const MaxNumOfMessageTypeSignatureAccepted = uint32(2)

// worker defines the data needed by spos to communicate between nodes which are in the validators group
type worker struct {
Expand Down Expand Up @@ -52,17 +52,17 @@ func (wrk *worker) InitReceivedMessages() map[consensus.MessageType][]*consensus

// GetMaxMessagesInARoundPerPeer returns the maximum number of messages a peer can send per round for BLS
func (wrk *worker) GetMaxMessagesInARoundPerPeer() uint32 {
return peerMaxMessagesPerSec
return PeerMaxMessagesPerSec
}

// GetStringValue gets the name of the messageType
func (wrk *worker) GetStringValue(messageType consensus.MessageType) string {
return getStringValue(messageType)
return GetStringValue(messageType)
}

// GetSubroundName gets the subround name for the subround id provided
func (wrk *worker) GetSubroundName(subroundId int) string {
return getSubroundName(subroundId)
return GetSubroundName(subroundId)
}

// IsMessageWithBlockBodyAndHeader returns if the current messageType is about block body and header
Expand Down Expand Up @@ -151,10 +151,10 @@ func (wrk *worker) CanProceed(consensusState *spos.ConsensusState, msgType conse
// GetMaxNumOfMessageTypeAccepted returns the maximum number of accepted consensus message types per round, per public key
func (wrk *worker) GetMaxNumOfMessageTypeAccepted(msgType consensus.MessageType) uint32 {
if msgType == MtSignature {
return maxNumOfMessageTypeSignatureAccepted
return MaxNumOfMessageTypeSignatureAccepted
}

return defaultMaxNumOfMessageTypeAccepted
return DefaultMaxNumOfMessageTypeAccepted
}

// IsInterfaceNil returns true if there is no value under the interface
Expand Down
Loading
Loading