Skip to content

Commit

Permalink
Merge branch 'feat/heartbeat-v2' into resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
sstanculeanu authored Feb 7, 2022
2 parents 28ca464 + 8131c1e commit 8e75c02
Show file tree
Hide file tree
Showing 6 changed files with 341 additions and 23 deletions.
29 changes: 17 additions & 12 deletions process/interceptors/factory/argInterceptedDataFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/ElrondNetwork/elrond-go-core/marshal"
"github.com/ElrondNetwork/elrond-go-crypto"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/process/heartbeat"
"github.com/ElrondNetwork/elrond-go/sharding"
)

Expand Down Expand Up @@ -39,16 +40,20 @@ type interceptedDataCryptoComponentsHolder interface {
// ArgInterceptedDataFactory holds all dependencies required by the shard and meta intercepted data factory in order to create
// new instances
type ArgInterceptedDataFactory struct {
CoreComponents interceptedDataCoreComponentsHolder
CryptoComponents interceptedDataCryptoComponentsHolder
ShardCoordinator sharding.Coordinator
NodesCoordinator sharding.NodesCoordinator
FeeHandler process.FeeHandler
WhiteListerVerifiedTxs process.WhiteListHandler
HeaderSigVerifier process.InterceptedHeaderSigVerifier
ValidityAttester process.ValidityAttester
HeaderIntegrityVerifier process.HeaderIntegrityVerifier
EpochStartTrigger process.EpochStartTriggerHandler
ArgsParser process.ArgumentsParser
EnableSignTxWithHashEpoch uint32
CoreComponents interceptedDataCoreComponentsHolder
CryptoComponents interceptedDataCryptoComponentsHolder
ShardCoordinator sharding.Coordinator
NodesCoordinator sharding.NodesCoordinator
FeeHandler process.FeeHandler
WhiteListerVerifiedTxs process.WhiteListHandler
HeaderSigVerifier process.InterceptedHeaderSigVerifier
ValidityAttester process.ValidityAttester
HeaderIntegrityVerifier process.HeaderIntegrityVerifier
EpochStartTrigger process.EpochStartTriggerHandler
ArgsParser process.ArgumentsParser
EnableSignTxWithHashEpoch uint32
PeerSignatureHandler crypto.PeerSignatureHandler
SignaturesHandler heartbeat.SignaturesHandler
HeartbeatExpiryTimespanInSec int64
PeerID core.PeerID
}
47 changes: 47 additions & 0 deletions process/interceptors/factory/interceptedHeartbeatDataFactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package factory

import (
"github.com/ElrondNetwork/elrond-go-core/core"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/marshal"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/process/heartbeat"
)

type interceptedHeartbeatDataFactory struct {
marshalizer marshal.Marshalizer
peerID core.PeerID
}

// NewInterceptedHeartbeatDataFactory creates an instance of interceptedHeartbeatDataFactory
func NewInterceptedHeartbeatDataFactory(arg ArgInterceptedDataFactory) (*interceptedHeartbeatDataFactory, error) {
if check.IfNil(arg.CoreComponents.InternalMarshalizer()) {
return nil, process.ErrNilMarshalizer
}
if len(arg.PeerID) == 0 {
return nil, process.ErrEmptyPeerID
}

return &interceptedHeartbeatDataFactory{
marshalizer: arg.CoreComponents.InternalMarshalizer(),
peerID: arg.PeerID,
}, nil
}

// Create creates instances of InterceptedData by unmarshalling provided buffer
func (ihdf *interceptedHeartbeatDataFactory) Create(buff []byte) (process.InterceptedData, error) {
arg := heartbeat.ArgInterceptedHeartbeat{
ArgBaseInterceptedHeartbeat: heartbeat.ArgBaseInterceptedHeartbeat{
DataBuff: buff,
Marshalizer: ihdf.marshalizer,
},
PeerId: ihdf.peerID,
}

return heartbeat.NewInterceptedHeartbeat(arg)
}

// IsInterfaceNil returns true if there is no value under the interface
func (ihdf *interceptedHeartbeatDataFactory) IsInterfaceNil() bool {
return ihdf == nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package factory

import (
"fmt"
"strings"
"testing"
"time"

"github.com/ElrondNetwork/elrond-go/heartbeat"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/process/mock"
"github.com/stretchr/testify/assert"
)

func TestNewInterceptedHeartbeatDataFactory(t *testing.T) {
t.Parallel()

t.Run("nil InternalMarshalizer should error", func(t *testing.T) {
t.Parallel()

coreComp, cryptoComp := createMockComponentHolders()
coreComp.IntMarsh = nil
arg := createMockArgument(coreComp, cryptoComp)

ihdf, err := NewInterceptedHeartbeatDataFactory(*arg)
assert.Nil(t, ihdf)
assert.Equal(t, process.ErrNilMarshalizer, err)
})
t.Run("empty peer id should error", func(t *testing.T) {
t.Parallel()

coreComp, cryptoComp := createMockComponentHolders()
arg := createMockArgument(coreComp, cryptoComp)
arg.PeerID = ""

ihdf, err := NewInterceptedHeartbeatDataFactory(*arg)
assert.Nil(t, ihdf)
assert.Equal(t, process.ErrEmptyPeerID, err)
})
t.Run("should work and create", func(t *testing.T) {
t.Parallel()

coreComp, cryptoComp := createMockComponentHolders()
arg := createMockArgument(coreComp, cryptoComp)

ihdf, err := NewInterceptedHeartbeatDataFactory(*arg)
assert.False(t, ihdf.IsInterfaceNil())
assert.Nil(t, err)

payload := &heartbeat.Payload{
Timestamp: time.Now().Unix(),
HardforkMessage: "hardfork message",
}
marshalizer := mock.MarshalizerMock{}
payloadBytes, err := marshalizer.Marshal(payload)
assert.Nil(t, err)

hb := &heartbeat.HeartbeatV2{
Payload: payloadBytes,
VersionNumber: "version number",
NodeDisplayName: "node display name",
Identity: "identity",
Nonce: 10,
PeerSubType: 0,
}
marshaledHeartbeat, err := marshalizer.Marshal(hb)
assert.Nil(t, err)

interceptedData, err := ihdf.Create(marshaledHeartbeat)
assert.NotNil(t, interceptedData)
assert.Nil(t, err)
assert.True(t, strings.Contains(fmt.Sprintf("%T", interceptedData), "*heartbeat.interceptedHeartbeat"))
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/process/block/interceptedBlocks"
"github.com/ElrondNetwork/elrond-go/process/mock"
processMocks "github.com/ElrondNetwork/elrond-go/process/mock"
"github.com/ElrondNetwork/elrond-go/testscommon"
"github.com/ElrondNetwork/elrond-go/testscommon/cryptoMocks"
"github.com/ElrondNetwork/elrond-go/testscommon/epochNotifier"
Expand Down Expand Up @@ -85,17 +86,21 @@ func createMockArgument(
cryptoComponents *mock.CryptoComponentsMock,
) *ArgInterceptedDataFactory {
return &ArgInterceptedDataFactory{
CoreComponents: coreComponents,
CryptoComponents: cryptoComponents,
ShardCoordinator: mock.NewOneShardCoordinatorMock(),
NodesCoordinator: mock.NewNodesCoordinatorMock(),
FeeHandler: createMockFeeHandler(),
HeaderSigVerifier: &mock.HeaderSigVerifierStub{},
HeaderIntegrityVerifier: &mock.HeaderIntegrityVerifierStub{},
ValidityAttester: &mock.ValidityAttesterStub{},
EpochStartTrigger: &mock.EpochStartTriggerStub{},
WhiteListerVerifiedTxs: &testscommon.WhiteListHandlerStub{},
ArgsParser: &mock.ArgumentParserMock{},
CoreComponents: coreComponents,
CryptoComponents: cryptoComponents,
ShardCoordinator: mock.NewOneShardCoordinatorMock(),
NodesCoordinator: mock.NewNodesCoordinatorMock(),
FeeHandler: createMockFeeHandler(),
WhiteListerVerifiedTxs: &testscommon.WhiteListHandlerStub{},
HeaderSigVerifier: &mock.HeaderSigVerifierStub{},
ValidityAttester: &mock.ValidityAttesterStub{},
HeaderIntegrityVerifier: &mock.HeaderIntegrityVerifierStub{},
EpochStartTrigger: &mock.EpochStartTriggerStub{},
ArgsParser: &mock.ArgumentParserMock{},
PeerSignatureHandler: &processMocks.PeerSignatureHandlerStub{},
SignaturesHandler: &processMocks.SignaturesHandlerStub{},
HeartbeatExpiryTimespanInSec: 30,
PeerID: "pid",
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package factory

import (
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/marshal"
crypto "github.com/ElrondNetwork/elrond-go-crypto"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/process/heartbeat"
)

const minDurationInSec = 10

type interceptedPeerAuthenticationDataFactory struct {
marshalizer marshal.Marshalizer
nodesCoordinator heartbeat.NodesCoordinator
signaturesHandler heartbeat.SignaturesHandler
peerSignatureHandler crypto.PeerSignatureHandler
ExpiryTimespanInSec int64
}

// NewInterceptedPeerAuthenticationDataFactory creates an instance of interceptedPeerAuthenticationDataFactory
func NewInterceptedPeerAuthenticationDataFactory(arg ArgInterceptedDataFactory) (*interceptedPeerAuthenticationDataFactory, error) {
if check.IfNil(arg.CoreComponents) {
return nil, process.ErrNilCoreComponentsHolder
}
if check.IfNil(arg.CoreComponents.InternalMarshalizer()) {
return nil, process.ErrNilMarshalizer
}
if check.IfNil(arg.NodesCoordinator) {
return nil, process.ErrNilNodesCoordinator
}
if check.IfNil(arg.SignaturesHandler) {
return nil, process.ErrNilSignaturesHandler
}
if check.IfNil(arg.PeerSignatureHandler) {
return nil, process.ErrNilPeerSignatureHandler
}
if arg.HeartbeatExpiryTimespanInSec < minDurationInSec {
return nil, process.ErrInvalidExpiryTimespan
}

return &interceptedPeerAuthenticationDataFactory{
marshalizer: arg.CoreComponents.InternalMarshalizer(),
nodesCoordinator: arg.NodesCoordinator,
signaturesHandler: arg.SignaturesHandler,
peerSignatureHandler: arg.PeerSignatureHandler,
ExpiryTimespanInSec: arg.HeartbeatExpiryTimespanInSec,
}, nil
}

// Create creates instances of InterceptedData by unmarshalling provided buffer
func (ipadf *interceptedPeerAuthenticationDataFactory) Create(buff []byte) (process.InterceptedData, error) {
arg := heartbeat.ArgInterceptedPeerAuthentication{
ArgBaseInterceptedHeartbeat: heartbeat.ArgBaseInterceptedHeartbeat{
DataBuff: buff,
Marshalizer: ipadf.marshalizer,
},
NodesCoordinator: ipadf.nodesCoordinator,
SignaturesHandler: ipadf.signaturesHandler,
PeerSignatureHandler: ipadf.peerSignatureHandler,
ExpiryTimespanInSec: ipadf.ExpiryTimespanInSec,
}

return heartbeat.NewInterceptedPeerAuthentication(arg)
}

// IsInterfaceNil returns true if there is no value under the interface
func (ipadf *interceptedPeerAuthenticationDataFactory) IsInterfaceNil() bool {
return ipadf == nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package factory

import (
"fmt"
"strings"
"testing"
"time"

"github.com/ElrondNetwork/elrond-go/heartbeat"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/process/mock"
"github.com/stretchr/testify/assert"
)

func TestNewInterceptedPeerAuthenticationDataFactory(t *testing.T) {
t.Parallel()

t.Run("nil CoreComponents should error", func(t *testing.T) {
t.Parallel()

coreComp, cryptoComp := createMockComponentHolders()
arg := createMockArgument(coreComp, cryptoComp)
arg.CoreComponents = nil

ipadf, err := NewInterceptedPeerAuthenticationDataFactory(*arg)
assert.Nil(t, ipadf)
assert.Equal(t, process.ErrNilCoreComponentsHolder, err)
})
t.Run("nil InternalMarshalizer should error", func(t *testing.T) {
t.Parallel()

coreComp, cryptoComp := createMockComponentHolders()
coreComp.IntMarsh = nil
arg := createMockArgument(coreComp, cryptoComp)

ipadf, err := NewInterceptedPeerAuthenticationDataFactory(*arg)
assert.Nil(t, ipadf)
assert.Equal(t, process.ErrNilMarshalizer, err)
})
t.Run("nil NodesCoordinator should error", func(t *testing.T) {
t.Parallel()

coreComp, cryptoComp := createMockComponentHolders()
arg := createMockArgument(coreComp, cryptoComp)
arg.NodesCoordinator = nil

ipadf, err := NewInterceptedPeerAuthenticationDataFactory(*arg)
assert.Nil(t, ipadf)
assert.Equal(t, process.ErrNilNodesCoordinator, err)
})
t.Run("nil SignaturesHandler should error", func(t *testing.T) {
t.Parallel()

coreComp, cryptoComp := createMockComponentHolders()
arg := createMockArgument(coreComp, cryptoComp)
arg.SignaturesHandler = nil

ipadf, err := NewInterceptedPeerAuthenticationDataFactory(*arg)
assert.Nil(t, ipadf)
assert.Equal(t, process.ErrNilSignaturesHandler, err)
})
t.Run("nil PeerSignatureHandler should error", func(t *testing.T) {
t.Parallel()

coreComp, cryptoComp := createMockComponentHolders()
arg := createMockArgument(coreComp, cryptoComp)
arg.PeerSignatureHandler = nil

ipadf, err := NewInterceptedPeerAuthenticationDataFactory(*arg)
assert.Nil(t, ipadf)
assert.Equal(t, process.ErrNilPeerSignatureHandler, err)
})
t.Run("invalid expiry timespan should error", func(t *testing.T) {
t.Parallel()

coreComp, cryptoComp := createMockComponentHolders()
arg := createMockArgument(coreComp, cryptoComp)
arg.HeartbeatExpiryTimespanInSec = 1

ipadf, err := NewInterceptedPeerAuthenticationDataFactory(*arg)
assert.Nil(t, ipadf)
assert.Equal(t, process.ErrInvalidExpiryTimespan, err)
})
t.Run("should work and create", func(t *testing.T) {
t.Parallel()

coreComp, cryptoComp := createMockComponentHolders()
arg := createMockArgument(coreComp, cryptoComp)

ipadf, err := NewInterceptedPeerAuthenticationDataFactory(*arg)
assert.False(t, ipadf.IsInterfaceNil())
assert.Nil(t, err)

payload := &heartbeat.Payload{
Timestamp: time.Now().Unix(),
HardforkMessage: "hardfork message",
}
marshalizer := mock.MarshalizerMock{}
payloadBytes, err := marshalizer.Marshal(payload)
assert.Nil(t, err)

peerAuthentication := &heartbeat.PeerAuthentication{
Pubkey: []byte("public key"),
Signature: []byte("signature"),
Pid: []byte("peer id"),
Payload: payloadBytes,
PayloadSignature: []byte("payload signature"),
}
marshaledPeerAuthentication, err := marshalizer.Marshal(peerAuthentication)
assert.Nil(t, err)

interceptedData, err := ipadf.Create(marshaledPeerAuthentication)
assert.NotNil(t, interceptedData)
assert.Nil(t, err)
assert.True(t, strings.Contains(fmt.Sprintf("%T", interceptedData), "*heartbeat.interceptedPeerAuthentication"))
})
}

0 comments on commit 8e75c02

Please sign in to comment.