diff --git a/errors/errors.go b/errors/errors.go index f44a324bfd9..3b1cad8e408 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -514,3 +514,6 @@ var ErrInvalidPID = errors.New("invalid PID") // ErrInvalidSignature signals that the given signature is invalid var ErrInvalidSignature = errors.New("invalid signature") + +// ErrInvalidHeartbeatV2Config signals that an invalid heartbeat v2 configuration has been provided +var ErrInvalidHeartbeatV2Config = errors.New("invalid heartbeat v2 configuration") diff --git a/factory/heartbeat/heartbeatV2Components.go b/factory/heartbeat/heartbeatV2Components.go index 599d89c48dd..402fd72f9dd 100644 --- a/factory/heartbeat/heartbeatV2Components.go +++ b/factory/heartbeat/heartbeatV2Components.go @@ -1,6 +1,7 @@ package heartbeat import ( + "fmt" "time" "github.com/ElrondNetwork/elrond-go-core/core" @@ -113,6 +114,11 @@ func (hcf *heartbeatV2ComponentsFactory) Create() (*heartbeatV2Components, error } } + cfg := hcf.config.HeartbeatV2 + if cfg.HeartbeatExpiryTimespanInSec <= cfg.PeerAuthenticationTimeBetweenSendsInSec { + return nil, fmt.Errorf("%w, HeartbeatExpiryTimespanInSec must be greater than PeerAuthenticationTimeBetweenSendsInSec", errors.ErrInvalidHeartbeatV2Config) + } + peerSubType := core.RegularPeer if hcf.prefs.Preferences.FullArchive { peerSubType = core.FullHistoryObserver @@ -121,8 +127,6 @@ func (hcf *heartbeatV2ComponentsFactory) Create() (*heartbeatV2Components, error shardC := hcf.boostrapComponents.ShardCoordinator() heartbeatTopic := common.HeartbeatV2Topic + shardC.CommunicationIdentifier(shardC.SelfId()) - cfg := hcf.config.HeartbeatV2 - argPeerTypeProvider := peer.ArgPeerTypeProvider{ NodesCoordinator: hcf.processComponents.NodesCoordinator(), StartEpoch: hcf.processComponents.EpochStartTrigger().MetaEpoch(), diff --git a/factory/heartbeat/heartbeatV2Components_test.go b/factory/heartbeat/heartbeatV2Components_test.go index 8c923520902..8d2829c1f3c 100644 --- a/factory/heartbeat/heartbeatV2Components_test.go +++ b/factory/heartbeat/heartbeatV2Components_test.go @@ -1,10 +1,12 @@ package heartbeat_test import ( + "errors" "testing" "github.com/ElrondNetwork/elrond-go-core/core/check" "github.com/ElrondNetwork/elrond-go/config" + errErd "github.com/ElrondNetwork/elrond-go/errors" bootstrapComp "github.com/ElrondNetwork/elrond-go/factory/bootstrap" heartbeatComp "github.com/ElrondNetwork/elrond-go/factory/heartbeat" "github.com/ElrondNetwork/elrond-go/factory/mock" @@ -72,25 +74,42 @@ func createMockHeartbeatV2ComponentsFactoryArgs() heartbeatComp.ArgHeartbeatV2Co } } -func Test_heartbeatV2Components_Create_ShouldWork(t *testing.T) { +func Test_heartbeatV2Components_Create(t *testing.T) { t.Parallel() - defer func() { - r := recover() - if r != nil { - assert.Fail(t, "should not panic") - } - }() + t.Run("invalid config should error", func(t *testing.T) { + t.Parallel() - args := createMockHeartbeatV2ComponentsFactoryArgs() - hcf, err := heartbeatComp.NewHeartbeatV2ComponentsFactory(args) - assert.False(t, check.IfNil(hcf)) - assert.Nil(t, err) + args := createMockHeartbeatV2ComponentsFactoryArgs() + args.Config.HeartbeatV2.HeartbeatExpiryTimespanInSec = args.Config.HeartbeatV2.PeerAuthenticationTimeBetweenSendsInSec + hcf, err := heartbeatComp.NewHeartbeatV2ComponentsFactory(args) + assert.False(t, check.IfNil(hcf)) + assert.Nil(t, err) - hc, err := hcf.Create() - assert.NotNil(t, hc) - assert.Nil(t, err) + hc, err := hcf.Create() + assert.Nil(t, hc) + assert.True(t, errors.Is(err, errErd.ErrInvalidHeartbeatV2Config)) + }) + t.Run("should work", func(t *testing.T) { + t.Parallel() - err = hc.Close() - assert.Nil(t, err) + defer func() { + r := recover() + if r != nil { + assert.Fail(t, "should not panic") + } + }() + + args := createMockHeartbeatV2ComponentsFactoryArgs() + hcf, err := heartbeatComp.NewHeartbeatV2ComponentsFactory(args) + assert.False(t, check.IfNil(hcf)) + assert.Nil(t, err) + + hc, err := hcf.Create() + assert.NotNil(t, hc) + assert.Nil(t, err) + + err = hc.Close() + assert.Nil(t, err) + }) } diff --git a/heartbeat/processor/peerAuthenticationRequestsProcessor.go b/heartbeat/processor/peerAuthenticationRequestsProcessor.go index d27769e4348..f8119b9c536 100644 --- a/heartbeat/processor/peerAuthenticationRequestsProcessor.go +++ b/heartbeat/processor/peerAuthenticationRequestsProcessor.go @@ -48,7 +48,6 @@ type peerAuthenticationRequestsProcessor struct { epoch uint32 minPeersThreshold float32 delayBetweenRequests time.Duration - maxTimeout time.Duration maxMissingKeysInRequest uint32 randomizer dataRetriever.IntRandomizer cancel func() @@ -69,7 +68,6 @@ func NewPeerAuthenticationRequestsProcessor(args ArgPeerAuthenticationRequestsPr epoch: args.Epoch, minPeersThreshold: args.MinPeersThreshold, delayBetweenRequests: args.DelayBetweenRequests, - maxTimeout: args.MaxTimeout, maxMissingKeysInRequest: args.MaxMissingKeysInRequest, randomizer: args.Randomizer, }