-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/heartbeat-v2' into resolvers
- Loading branch information
Showing
6 changed files
with
341 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
process/interceptors/factory/interceptedHeartbeatDataFactory.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
74 changes: 74 additions & 0 deletions
74
process/interceptors/factory/interceptedHeartbeatDataFactory_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
process/interceptors/factory/interceptedPeerAuthenticationDataFactory.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
117 changes: 117 additions & 0 deletions
117
process/interceptors/factory/interceptedPeerAuthenticationDataFactory_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
} |