Skip to content

Commit

Permalink
Merge pull request #2023 from ElrondNetwork/EN-6847-timestamp-in-p2p-…
Browse files Browse the repository at this point in the history
…message

En 6847 timestamp in p2p message
  • Loading branch information
iulianpascalau committed Jun 27, 2020
2 parents 5a869d3 + 5d3b293 commit dc0181f
Show file tree
Hide file tree
Showing 43 changed files with 988 additions and 208 deletions.
5 changes: 0 additions & 5 deletions cmd/node/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,6 @@
MessagesMarshalizer = "json"
MaxLoopTime = 1000

[P2PMessageIDAdditionalCache]
Capacity = 100000
Type = "FIFOSharded"
Shards = 100

[Hardfork]
EnableTrigger = true
EnableTriggerFromP2P = true
Expand Down
7 changes: 6 additions & 1 deletion cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,12 @@ func startNode(ctx *cli.Context, log logger.Logger, version string) error {
coreComponents.StatusHandler = statusHandlersInfo.StatusHandler

log.Trace("creating network components")
networkComponentFactory, err := mainFactory.NewNetworkComponentsFactory(*p2pConfig, *generalConfig, coreComponents.StatusHandler)
networkComponentFactory, err := mainFactory.NewNetworkComponentsFactory(
*p2pConfig,
*generalConfig,
coreComponents.StatusHandler,
coreComponents.InternalMarshalizer,
)
if err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions cmd/seednode/config/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# The main marshalizer, used in internodes communication
# Type identifies the marshalizer
# SizeCheckDelta the maximum allow drift between the input data buffer and
# the reencoded version (in percents).
# 0 disables the feature.
[Marshalizer]
Type = "gogo protobuf"
SizeCheckDelta = 10
46 changes: 42 additions & 4 deletions cmd/seednode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ import (
"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/display"
"github.com/ElrondNetwork/elrond-go/marshal"
factoryMarshalizer "github.com/ElrondNetwork/elrond-go/marshal/factory"
"github.com/ElrondNetwork/elrond-go/p2p"
"github.com/ElrondNetwork/elrond-go/p2p/libp2p"
"github.com/urfave/cli"
)

const defaultLogsPath = "logs"
const filePathPlaceholder = "[path]"

var (
seedNodeHelpTemplate = `NAME:
Expand Down Expand Up @@ -66,7 +69,13 @@ VERSION:
Name: "log-save",
Usage: "Boolean option for enabling log saving. If set, it will automatically save all the logs into a file.",
}

// configurationFile defines a flag for the path to the main toml configuration file
configurationFile = cli.StringFlag{
Name: "config",
Usage: "The `" + filePathPlaceholder + "` for the main configuration file. This TOML file contain the main " +
"configurations such as the marshalizer type",
Value: "./config/config.toml",
}
p2pConfigurationFile = "./config/p2p.toml"
)

Expand All @@ -77,7 +86,13 @@ func main() {
cli.AppHelpTemplate = seedNodeHelpTemplate
app.Name = "SeedNode CLI App"
app.Usage = "This is the entry point for starting a new seed node - the app will help bootnodes connect to the network"
app.Flags = []cli.Flag{port, p2pSeed, logLevel, logSaveFile}
app.Flags = []cli.Flag{
port,
p2pSeed,
logLevel,
logSaveFile,
configurationFile,
}
app.Version = "v0.0.1"
app.Authors = []cli.Author{
{
Expand All @@ -99,6 +114,18 @@ func main() {

func startNode(ctx *cli.Context) error {
var err error

configurationFileName := ctx.GlobalString(configurationFile.Name)
generalConfig, err := loadMainConfig(configurationFileName)
if err != nil {
return err
}

internalMarshalizer, err := factoryMarshalizer.NewMarshalizer(generalConfig.Marshalizer.Type)
if err != nil {
return fmt.Errorf("error creating marshalizer (internal): %s", err.Error())
}

withLogFile := ctx.GlobalBool(logSaveFile.Name)
if withLogFile {
var fileForLogs *os.File
Expand Down Expand Up @@ -144,7 +171,7 @@ func startNode(ctx *cli.Context) error {
return err
}

messenger, err := createNode(*p2pConfig)
messenger, err := createNode(*p2pConfig, internalMarshalizer)
if err != nil {
return err
}
Expand Down Expand Up @@ -172,8 +199,19 @@ func startNode(ctx *cli.Context) error {
}
}

func createNode(p2pConfig config.P2PConfig) (p2p.Messenger, error) {
func loadMainConfig(filepath string) (*config.Config, error) {
cfg := &config.Config{}
err := core.LoadTomlFile(cfg, filepath)
if err != nil {
return nil, err
}

return cfg, nil
}

func createNode(p2pConfig config.P2PConfig, marshalizer marshal.Marshalizer) (p2p.Messenger, error) {
arg := libp2p.ArgsNetworkMessenger{
Marshalizer: marshalizer,
ListenAddress: libp2p.ListenAddrWithIp4AndTcp,
P2pConfig: p2pConfig,
}
Expand Down
9 changes: 4 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,10 @@ type Config struct {
VmMarshalizer TypeConfig
TxSignMarshalizer TypeConfig

PublicKeyShardId CacheConfig
PublicKeyPeerId CacheConfig
PeerIdShardId CacheConfig
P2PMessageIDAdditionalCache CacheConfig
PeerHonesty CacheConfig
PublicKeyShardId CacheConfig
PublicKeyPeerId CacheConfig
PeerIdShardId CacheConfig
PeerHonesty CacheConfig

Antiflood AntifloodConfig
ResourceStats ResourceStatsConfig
Expand Down
12 changes: 12 additions & 0 deletions consensus/mock/p2pMessageMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type P2PMessageMock struct {
SignatureField []byte
KeyField []byte
PeerField core.PeerID
PayloadField []byte
TimestampField int64
}

// From -
Expand Down Expand Up @@ -50,6 +52,16 @@ func (msg *P2PMessageMock) Peer() core.PeerID {
return msg.PeerField
}

// Timestamp -
func (msg *P2PMessageMock) Timestamp() int64 {
return msg.TimestampField
}

// Payload -
func (msg *P2PMessageMock) Payload() []byte {
return msg.PayloadField
}

// IsInterfaceNil returns true if there is no value under the interface
func (msg *P2PMessageMock) IsInterfaceNil() bool {
return msg == nil
Expand Down
4 changes: 4 additions & 0 deletions core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,7 @@ const InvalidMessageBlacklistDuration = time.Second * 3600
// PublicKeyBlacklistDuration represents the time to keep a public key in the black list if it will degrade its
// rating to a minimum threshold due to improper messages
const PublicKeyBlacklistDuration = time.Second * 7200

// WrongP2PMessageBlacklistDuration represents the time to keep a peer id in the blacklist if it sends a message that
// do not follow this protocol
const WrongP2PMessageBlacklistDuration = time.Second * 7200
12 changes: 12 additions & 0 deletions dataRetriever/mock/p2pMessageMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type P2PMessageMock struct {
SignatureField []byte
KeyField []byte
PeerField core.PeerID
PayloadField []byte
TimestampField int64
}

// From -
Expand Down Expand Up @@ -50,6 +52,16 @@ func (msg *P2PMessageMock) Peer() core.PeerID {
return msg.PeerField
}

// Timestamp -
func (msg *P2PMessageMock) Timestamp() int64 {
return msg.TimestampField
}

// Payload -
func (msg *P2PMessageMock) Payload() []byte {
return msg.PayloadField
}

// IsInterfaceNil returns true if there is no value under the interface
func (msg *P2PMessageMock) IsInterfaceNil() bool {
return msg == nil
Expand Down
6 changes: 0 additions & 6 deletions factory/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import "errors"
// ErrNilEconomicsData signals that a nil economics data handler has been provided
var ErrNilEconomicsData = errors.New("nil economics data provided")

// ErrNilGenesisConfiguration signals that a nil genesis configuration has been provided
var ErrNilGenesisConfiguration = errors.New("nil genesis configuration provided")

// ErrNilCoreComponents signals that nil core components have been provided
var ErrNilCoreComponents = errors.New("nil core components provided")

Expand Down Expand Up @@ -50,9 +47,6 @@ var ErrPubKeyConverterCreation = errors.New("error creating public key converter
// ErrAccountsAdapterCreation signals that the accounts adapter cannot be created based on provided data
var ErrAccountsAdapterCreation = errors.New("error creating accounts adapter")

// ErrInitialBalancesCreation signals that the initial balances cannot be created based on provided data
var ErrInitialBalancesCreation = errors.New("error creating initial balances")

// ErrNilPubKeyConverter signals that a nil public key converter has been provided
var ErrNilPubKeyConverter = errors.New("nil public key converter provided")

Expand Down
20 changes: 8 additions & 12 deletions factory/networkComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,37 @@ import (
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/debug/antiflood"
"github.com/ElrondNetwork/elrond-go/marshal"
"github.com/ElrondNetwork/elrond-go/p2p/libp2p"
"github.com/ElrondNetwork/elrond-go/process"
antifloodFactory "github.com/ElrondNetwork/elrond-go/process/throttle/antiflood/factory"
storageFactory "github.com/ElrondNetwork/elrond-go/storage/factory"
"github.com/ElrondNetwork/elrond-go/storage/storageUnit"
)

type networkComponentsFactory struct {
p2pConfig config.P2PConfig
mainConfig config.Config
statusHandler core.AppStatusHandler
listenAddress string
marshalizer marshal.Marshalizer
}

// NewNetworkComponentsFactory returns a new instance of a network components factory
func NewNetworkComponentsFactory(
p2pConfig config.P2PConfig,
mainConfig config.Config,
statusHandler core.AppStatusHandler,
marshalizer marshal.Marshalizer,
) (*networkComponentsFactory, error) {
if check.IfNil(statusHandler) {
return nil, ErrNilStatusHandler
}
if check.IfNil(marshalizer) {
return nil, fmt.Errorf("%w in NewNetworkComponentsFactory", ErrNilMarshalizer)
}

return &networkComponentsFactory{
p2pConfig: p2pConfig,
marshalizer: marshalizer,
mainConfig: mainConfig,
statusHandler: statusHandler,
listenAddress: libp2p.ListenAddrWithIp4AndTcp,
Expand All @@ -42,6 +47,7 @@ func NewNetworkComponentsFactory(
// Create creates and returns the network components
func (ncf *networkComponentsFactory) Create() (*NetworkComponents, error) {
arg := libp2p.ArgsNetworkMessenger{
Marshalizer: ncf.marshalizer,
ListenAddress: ncf.listenAddress,
P2pConfig: ncf.p2pConfig,
}
Expand Down Expand Up @@ -88,16 +94,6 @@ func (ncf *networkComponentsFactory) Create() (*NetworkComponents, error) {
return nil, fmt.Errorf("%w when casting output antiflood handler to structs/P2PAntifloodHandler", ErrWrongTypeAssertion)
}

cache, err := storageUnit.NewCache(storageFactory.GetCacherFromConfig(ncf.mainConfig.P2PMessageIDAdditionalCache))
if err != nil {
return nil, fmt.Errorf("%w while creating p2p cacher", err)
}

err = netMessenger.SetMessageIdsCacher(cache)
if err != nil {
return nil, fmt.Errorf("%w while setting p2p cacher", err)
}

return &NetworkComponents{
NetMessenger: netMessenger,
InputAntifloodHandler: inputAntifloodHandler,
Expand Down
21 changes: 13 additions & 8 deletions factory/networkComponents_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package factory

import (
"errors"
"testing"

"github.com/ElrondNetwork/elrond-go/config"
Expand All @@ -12,15 +13,23 @@ import (
func TestNewNetworkComponentsFactory_NilStatusHandlerShouldErr(t *testing.T) {
t.Parallel()

ncf, err := NewNetworkComponentsFactory(config.P2PConfig{}, config.Config{}, nil)
ncf, err := NewNetworkComponentsFactory(config.P2PConfig{}, config.Config{}, nil, &mock.MarshalizerMock{})
require.Nil(t, ncf)
require.Equal(t, ErrNilStatusHandler, err)
}

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

ncf, err := NewNetworkComponentsFactory(config.P2PConfig{}, config.Config{}, &mock.AppStatusHandlerMock{}, nil)
require.Nil(t, ncf)
require.True(t, errors.Is(err, ErrNilMarshalizer))
}

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

ncf, err := NewNetworkComponentsFactory(config.P2PConfig{}, config.Config{}, &mock.AppStatusHandlerMock{})
ncf, err := NewNetworkComponentsFactory(config.P2PConfig{}, config.Config{}, &mock.AppStatusHandlerMock{}, &mock.MarshalizerMock{})
require.NoError(t, err)
require.NotNil(t, ncf)
}
Expand All @@ -31,7 +40,7 @@ func TestNetworkComponentsFactory_Create_ShouldErrDueToBadConfig(t *testing.T) {
t.Skip("this test fails with race detector on because of the github.com/koron/go-ssdp lib")
}

ncf, _ := NewNetworkComponentsFactory(config.P2PConfig{}, config.Config{}, &mock.AppStatusHandlerMock{})
ncf, _ := NewNetworkComponentsFactory(config.P2PConfig{}, config.Config{}, &mock.AppStatusHandlerMock{}, &mock.MarshalizerMock{})

nc, err := ncf.Create()
require.Error(t, err)
Expand Down Expand Up @@ -69,11 +78,6 @@ func TestNetworkComponentsFactory_Create_ShouldWork(t *testing.T) {
ncf, _ := NewNetworkComponentsFactory(
p2pConfig,
config.Config{
P2PMessageIDAdditionalCache: config.CacheConfig{
Type: "LRU",
Capacity: 100,
Shards: 16,
},
Debug: config.DebugConfig{
Antiflood: config.AntifloodDebugConfig{
Enabled: true,
Expand All @@ -83,6 +87,7 @@ func TestNetworkComponentsFactory_Create_ShouldWork(t *testing.T) {
},
},
&mock.AppStatusHandlerMock{},
&mock.MarshalizerMock{},
)

ncf.SetListenAddress(libp2p.ListenLocalhostAddrWithIp4AndTcp)
Expand Down
12 changes: 12 additions & 0 deletions heartbeat/mock/p2pMessageStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type P2PMessageStub struct {
SignatureField []byte
KeyField []byte
PeerField core.PeerID
PayloadField []byte
TimestampField int64
}

// From -
Expand Down Expand Up @@ -50,6 +52,16 @@ func (msg *P2PMessageStub) Peer() core.PeerID {
return msg.PeerField
}

// Timestamp -
func (msg *P2PMessageStub) Timestamp() int64 {
return msg.TimestampField
}

// Payload -
func (msg *P2PMessageStub) Payload() []byte {
return msg.PayloadField
}

// IsInterfaceNil returns true if there is no value under the interface
func (msg *P2PMessageStub) IsInterfaceNil() bool {
return msg == nil
Expand Down

0 comments on commit dc0181f

Please sign in to comment.