From cca6e933934d9330a5f30e4607e2fdcd598079ad Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Tue, 14 May 2024 13:25:43 +0200 Subject: [PATCH 01/10] refactor p2p options --- block/manager_test.go | 6 +- block/submit_test.go | 11 ++-- config/config.go | 16 +++--- config/config_test.go | 27 +++++---- config/defaults.go | 28 +++++----- config/flags.go | 1 - config/p2p.go | 32 +++++++++-- conv/addr.go | 67 ---------------------- conv/addr_test.go | 115 -------------------------------------- conv/config.go | 10 +--- conv/config_test.go | 8 --- node/node.go | 2 - node/node_test.go | 9 ++- p2p/client.go | 10 ++-- p2p/client_test.go | 11 ++-- rpc/client/client_test.go | 94 ++++++++++++++++++------------- rpc/json/service_test.go | 13 ++--- testutil/block.go | 16 +++--- 18 files changed, 156 insertions(+), 320 deletions(-) delete mode 100644 conv/addr.go delete mode 100644 conv/addr_test.go diff --git a/block/manager_test.go b/block/manager_test.go index ca23601b8..ab333b650 100644 --- a/block/manager_test.go +++ b/block/manager_test.go @@ -53,8 +53,10 @@ func TestInitialState(t *testing.T) { // Init p2p client privKey, _, _ := crypto.GenerateEd25519Key(rand.Reader) p2pClient, err := p2p.NewClient(config.P2PConfig{ - GossipCacheSize: 50, - BoostrapTime: 30 * time.Second, + ListenAddress: config.DefaultListenAddress, + GossipedBlocksCacheSize: 50, + BootstrapTime: 30 * time.Second, + AdvertisingEnabled: true, }, privKey, "TestChain", pubsubServer, logger) assert.NoError(err) assert.NotNil(p2pClient) diff --git a/block/submit_test.go b/block/submit_test.go index b067d49e7..c6b00998f 100644 --- a/block/submit_test.go +++ b/block/submit_test.go @@ -122,12 +122,11 @@ func TestSubmissionByTime(t *testing.T) { // Init manager with empty blocks feature enabled managerConfig := config.BlockManagerConfig{ - BlockTime: blockTime, - MaxIdleTime: 0, - MaxSupportedBatchSkew: 10, - BatchSubmitMaxTime: submitTimeout, - BlockBatchMaxSizeBytes: 1000, - GossipedBlocksCacheSize: 50, + BlockTime: blockTime, + MaxIdleTime: 0, + MaxSupportedBatchSkew: 10, + BatchSubmitMaxTime: submitTimeout, + BlockBatchMaxSizeBytes: 1000, } manager, err := testutil.GetManager(managerConfig, nil, nil, 1, 1, 0, proxyApp, nil) diff --git a/config/config.go b/config/config.go index d2418e29e..bb9781bb3 100644 --- a/config/config.go +++ b/config/config.go @@ -37,8 +37,7 @@ type NodeConfig struct { SettlementConfig settlement.Config `mapstructure:",squash"` Instrumentation *InstrumentationConfig `mapstructure:"instrumentation"` // Config params for mock grpc da - DAGrpc grpc.Config `mapstructure:",squash"` - BootstrapTime time.Duration `mapstructure:"bootstrap_time"` + DAGrpc grpc.Config `mapstructure:",squash"` } // BlockManagerConfig consists of all parameters required by BlockManagerConfig @@ -55,9 +54,8 @@ type BlockManagerConfig struct { MaxSupportedBatchSkew uint64 `mapstructure:"max_supported_batch_skew"` // The size of the batch in Bytes. Every batch we'll write to the DA and the settlement layer. BlockBatchMaxSizeBytes uint64 `mapstructure:"block_batch_max_size_bytes"` - // The number of messages cached by gossipsub protocol - GossipedBlocksCacheSize int `mapstructure:"gossiped_blocks_cache_size"` - NamespaceID string `mapstructure:"namespace_id"` + // Namespaceid included in the header (not used) + NamespaceID string `mapstructure:"namespace_id"` } // GetViperConfig reads configuration parameters from Viper instance. @@ -100,6 +98,10 @@ func (nc NodeConfig) Validate() error { return fmt.Errorf("BlockManagerConfig: %w", err) } + if err := nc.P2P.Validate(); err != nil { + return fmt.Errorf("BlockManagerConfig: %w", err) + } + if err := nc.validateSettlementLayer(); err != nil { return fmt.Errorf("SettlementLayer: %w", err) } @@ -150,10 +152,6 @@ func (c BlockManagerConfig) Validate() error { return fmt.Errorf("block_batch_size_bytes must be positive") } - if c.GossipedBlocksCacheSize <= 0 { - return fmt.Errorf("gossiped_blocks_cache_size must be positive") - } - if c.MaxSupportedBatchSkew <= 0 { return fmt.Errorf("max_supported_batch_skew must be positive") } diff --git a/config/config_test.go b/config/config_test.go index ab2e088bc..8e4436362 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -89,12 +89,6 @@ func TestNodeConfig_Validate(t *testing.T) { nc.BlockManagerConfig.BlockBatchMaxSizeBytes = 0 }, wantErr: assert.Error, - }, { - name: "missing gossiped blocks cache size", - malleate: func(nc *config.NodeConfig) { - nc.BlockManagerConfig.GossipedBlocksCacheSize = 0 - }, - wantErr: assert.Error, }, { name: "empty settlement layer", malleate: func(nc *config.NodeConfig) { @@ -193,14 +187,13 @@ func TestNodeConfig_Validate(t *testing.T) { func fullNodeConfig() config.NodeConfig { return config.NodeConfig{ BlockManagerConfig: config.BlockManagerConfig{ - BlockTime: 1 * time.Second, - MaxIdleTime: 20 * time.Second, - MaxProofTime: 20 * time.Second, - BatchSubmitMaxTime: 20 * time.Second, - MaxSupportedBatchSkew: 10, - NamespaceID: "test", - BlockBatchMaxSizeBytes: 1, - GossipedBlocksCacheSize: 1, + BlockTime: 1 * time.Second, + MaxIdleTime: 20 * time.Second, + MaxProofTime: 20 * time.Second, + BatchSubmitMaxTime: 20 * time.Second, + MaxSupportedBatchSkew: 10, + NamespaceID: "test", + BlockBatchMaxSizeBytes: 1, }, DALayer: "celestia", DAConfig: "da-config", @@ -229,5 +222,11 @@ func fullNodeConfig() config.NodeConfig { Host: "localhost", Port: 9090, }, + P2P: config.P2PConfig{ + GossipedBlocksCacheSize: 50, + BootstrapTime: 30 * time.Second, + ListenAddress: config.DefaultListenAddress, + BootstrapNodes: "", + }, } } diff --git a/config/defaults.go b/config/defaults.go index ec3349579..f1a6d0136 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -10,7 +10,7 @@ import ( const ( // DefaultListenAddress is a default listen address for P2P client. - DefaultListenAddress = "/ip4/0.0.0.0/tcp/7676" + DefaultListenAddress = "/ip4/0.0.0.0/tcp/26656" DefaultHomeDir = "sequencer_keys" DefaultChainID = "dymint-testnet" @@ -22,19 +22,14 @@ var DefaultNodeConfig = *DefaultConfig("", "") // DefaultConfig returns a default configuration for dymint node. func DefaultConfig(home, chainId string) *NodeConfig { cfg := &NodeConfig{ - P2P: P2PConfig{ - ListenAddress: DefaultListenAddress, - Seeds: "", - }, BlockManagerConfig: BlockManagerConfig{ - BlockTime: 200 * time.Millisecond, - MaxIdleTime: 3600 * time.Second, - MaxProofTime: 100 * time.Second, - BatchSubmitMaxTime: 3600 * time.Second, - MaxSupportedBatchSkew: 20, - NamespaceID: "0000000000000000ffff", - BlockBatchMaxSizeBytes: 500000, - GossipedBlocksCacheSize: 50, + BlockTime: 200 * time.Millisecond, + MaxIdleTime: 3600 * time.Second, + MaxProofTime: 100 * time.Second, + BatchSubmitMaxTime: 3600 * time.Second, + MaxSupportedBatchSkew: 20, + NamespaceID: "0000000000000000ffff", + BlockBatchMaxSizeBytes: 500000, }, DALayer: "mock", SettlementLayer: "mock", @@ -42,7 +37,12 @@ func DefaultConfig(home, chainId string) *NodeConfig { Prometheus: false, PrometheusListenAddr: ":2112", }, - BootstrapTime: 30 * time.Second, + P2P: P2PConfig{ + GossipedBlocksCacheSize: 50, + BootstrapTime: 30 * time.Second, + ListenAddress: DefaultListenAddress, + BootstrapNodes: "", + }, } if home == "" { diff --git a/config/flags.go b/config/flags.go index 5dbeb57e7..d467be2ab 100644 --- a/config/flags.go +++ b/config/flags.go @@ -42,7 +42,6 @@ func AddNodeFlags(cmd *cobra.Command) { cmd.Flags().Duration(FlagBlockTime, def.BlockTime, "block time (for aggregator mode)") cmd.Flags().Duration(FlagMaxIdleTime, def.MaxIdleTime, "max time for empty blocks (for aggregator mode)") cmd.Flags().Duration(FlagBatchSubmitMaxTime, def.BatchSubmitMaxTime, "max time for batch submit (for aggregator mode)") - cmd.Flags().String(FlagNamespaceID, def.NamespaceID, "namespace identifies (8 bytes in hex)") cmd.Flags().Uint64(FlagBlockBatchMaxSizeBytes, def.BlockBatchMaxSizeBytes, "block batch size in bytes") cmd.Flags().String(FlagSettlementLayer, def.SettlementLayer, "Settlement Layer Client name") diff --git a/config/p2p.go b/config/p2p.go index c6c6e57d9..539461d4a 100644 --- a/config/p2p.go +++ b/config/p2p.go @@ -1,11 +1,33 @@ package config -import "time" +import ( + "fmt" + "time" +) // P2PConfig stores configuration related to peer-to-peer networking. type P2PConfig struct { - ListenAddress string // Address to listen for incoming connections - Seeds string // Comma separated list of seed nodes to connect to - GossipCacheSize int - BoostrapTime time.Duration + //Listening address for P2P connections + ListenAddress string `mapstructure:"p2p_listen_address"` + //List of nodes used for P2P bootstrapping + BootstrapNodes string `mapstructure:"p2p_bootstrap_nodes"` + //Size of the Gossipsub router cache + GossipedBlocksCacheSize int `mapstructure:"gossiped_blocks_cache_size"` + //If true, the node is advertised in the DHT + AdvertisingEnabled bool `mapstructure:"advertising"` + //Time interval a node tries to bootstrap again, in case no nodes connected + BootstrapTime time.Duration `mapstructure:"bootstrap_time"` +} + +// Validate BlockManagerConfig +func (c P2PConfig) Validate() error { + + if c.GossipedBlocksCacheSize < 0 { + return fmt.Errorf("gossipsub cache size cannot be negative.") + } + + if c.BootstrapTime <= 0 { + return fmt.Errorf("bootstrap time must be positive.") + } + return nil } diff --git a/conv/addr.go b/conv/addr.go deleted file mode 100644 index e015e8c74..000000000 --- a/conv/addr.go +++ /dev/null @@ -1,67 +0,0 @@ -package conv - -import ( - "strings" - - "github.com/multiformats/go-multiaddr" - - "github.com/dymensionxyz/dymint/config" -) - -// TranslateAddresses updates conf by changing Cosmos-style addresses to Multiaddr format. -func TranslateAddresses(conf *config.NodeConfig) error { - if conf.P2P.ListenAddress != "" { - addr, err := GetMultiAddr(conf.P2P.ListenAddress) - if err != nil { - return err - } - conf.P2P.ListenAddress = addr.String() - } - - seeds := strings.Split(conf.P2P.Seeds, ",") - for i, seed := range seeds { - if seed != "" { - addr, err := GetMultiAddr(seed) - if err != nil { - return err - } - seeds[i] = addr.String() - } - } - conf.P2P.Seeds = strings.Join(seeds, ",") - - return nil -} - -// GetMultiAddr converts single Cosmos-style network address into Multiaddr. -// Input format: [protocol://][@]: -func GetMultiAddr(addr string) (multiaddr.Multiaddr, error) { - var err error - var p2pID multiaddr.Multiaddr - parts := strings.Split(addr, "://") - proto := "tcp" - if len(parts) == 2 { - proto = parts[0] - addr = parts[1] - } - - if at := strings.IndexRune(addr, '@'); at != -1 { - p2pID, err = multiaddr.NewMultiaddr("/p2p/" + addr[:at]) - if err != nil { - return nil, err - } - addr = addr[at+1:] - } - parts = strings.Split(addr, ":") - if len(parts) != 2 { - return nil, ErrInvalidAddress - } - maddr, err := multiaddr.NewMultiaddr("/ip4/" + parts[0] + "/" + proto + "/" + parts[1]) - if err != nil { - return nil, err - } - if p2pID != nil { - maddr = maddr.Encapsulate(p2pID) - } - return maddr, nil -} diff --git a/conv/addr_test.go b/conv/addr_test.go deleted file mode 100644 index 887836070..000000000 --- a/conv/addr_test.go +++ /dev/null @@ -1,115 +0,0 @@ -package conv_test - -import ( - "strings" - "testing" - - "github.com/multiformats/go-multiaddr" - "github.com/stretchr/testify/assert" - - "github.com/dymensionxyz/dymint/config" - "github.com/dymensionxyz/dymint/conv" -) - -func TestTranslateAddresses(t *testing.T) { - t.Parallel() - - invalidCosmos := "foobar" - validCosmos := "127.0.0.1:1234" - validDymint := "/ip4/127.0.0.1/tcp/1234" - - cases := []struct { - name string - input config.NodeConfig - expected config.NodeConfig - expectedErr string - }{ - {"empty", config.NodeConfig{}, config.NodeConfig{}, ""}, - { - "valid listen address", - config.NodeConfig{P2P: config.P2PConfig{ListenAddress: validCosmos}}, - config.NodeConfig{P2P: config.P2PConfig{ListenAddress: validDymint}}, - "", - }, - { - "valid seed address", - config.NodeConfig{P2P: config.P2PConfig{Seeds: validCosmos + "," + validCosmos}}, - config.NodeConfig{P2P: config.P2PConfig{Seeds: validDymint + "," + validDymint}}, - "", - }, - { - "invalid listen address", - config.NodeConfig{P2P: config.P2PConfig{ListenAddress: invalidCosmos}}, - config.NodeConfig{}, - conv.ErrInvalidAddress.Error(), - }, - { - "invalid seed address", - config.NodeConfig{P2P: config.P2PConfig{Seeds: validCosmos + "," + invalidCosmos}}, - config.NodeConfig{}, - conv.ErrInvalidAddress.Error(), - }, - } - - for _, c := range cases { - t.Run(c.name, func(t *testing.T) { - assert := assert.New(t) - // c.input is changed in place - err := conv.TranslateAddresses(&c.input) - if c.expectedErr != "" { - assert.Error(err) - assert.True(strings.HasPrefix(err.Error(), c.expectedErr), "invalid error message") - } else { - assert.NoError(err) - assert.Equal(c.expected, c.input) - } - }) - } -} - -func TestGetMultiaddr(t *testing.T) { - t.Parallel() - - valid := mustGetMultiaddr(t, "/ip4/127.0.0.1/tcp/1234") - withID := mustGetMultiaddr(t, "/ip4/127.0.0.1/tcp/1234/p2p/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7") - udpWithID := mustGetMultiaddr(t, "/ip4/127.0.0.1/udp/1234/p2p/k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7") - - cases := []struct { - name string - input string - expected multiaddr.Multiaddr - expectedErr string - }{ - {"empty", "", nil, conv.ErrInvalidAddress.Error()}, - {"no port", "127.0.0.1:", nil, "failed to parse multiaddr"}, - {"ip only", "127.0.0.1", nil, conv.ErrInvalidAddress.Error()}, - {"with invalid id", "deadbeef@127.0.0.1:1234", nil, "failed to parse multiaddr"}, - {"valid", "127.0.0.1:1234", valid, ""}, - {"valid with id", "k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7@127.0.0.1:1234", withID, ""}, - {"valid with id and proto", "udp://k2k4r8oqamigqdo6o7hsbfwd45y70oyynp98usk7zmyfrzpqxh1pohl7@127.0.0.1:1234", udpWithID, ""}, - } - - for _, c := range cases { - t.Run(c.name, func(t *testing.T) { - assert := assert.New(t) - actual, err := conv.GetMultiAddr(c.input) - if c.expectedErr != "" { - assert.Error(err) - assert.Nil(actual) - assert.True(strings.HasPrefix(err.Error(), c.expectedErr), "invalid error message") - } else { - assert.NoError(err) - assert.Equal(c.expected, actual) - } - }) - } -} - -func mustGetMultiaddr(t *testing.T, addr string) multiaddr.Multiaddr { - t.Helper() - maddr, err := multiaddr.NewMultiaddr(addr) - if err != nil { - t.Fatal(err) - } - return maddr -} diff --git a/conv/config.go b/conv/config.go index 6e714fc78..65498dc12 100644 --- a/conv/config.go +++ b/conv/config.go @@ -18,10 +18,7 @@ func GetNodeConfig(nodeConf *config.NodeConfig, tmConf *tmcfg.Config) error { } nodeConf.RootDir = tmConf.RootDir nodeConf.DBPath = tmConf.DBPath - if tmConf.P2P != nil { - nodeConf.P2P.ListenAddress = tmConf.P2P.ListenAddress - nodeConf.P2P.Seeds = tmConf.P2P.Seeds - } + if tmConf.RPC != nil { nodeConf.RPC.ListenAddress = tmConf.RPC.ListenAddress nodeConf.RPC.CORSAllowedOrigins = tmConf.RPC.CORSAllowedOrigins @@ -42,10 +39,5 @@ func GetNodeConfig(nodeConf *config.NodeConfig, tmConf *tmcfg.Config) error { */ nodeConf.MempoolConfig = *tmConf.Mempool - err := TranslateAddresses(nodeConf) - if err != nil { - return err - } - return nil } diff --git a/conv/config_test.go b/conv/config_test.go index 0f488d194..a9f1d9412 100644 --- a/conv/config_test.go +++ b/conv/config_test.go @@ -30,14 +30,6 @@ func TestGetNodeConfig(t *testing.T) { nil, true, }, - { - "Seeds", - func(c *tmcfg.Config) { - c.P2P.Seeds = validCosmos + "," + validCosmos - }, - func(nc *config.NodeConfig) bool { return nc.P2P.Seeds == validDymint+","+validDymint }, - false, - }, // GetNodeConfig translates the listen address, so we expect the translated address { "ListenAddress", diff --git a/node/node.go b/node/node.go index a60ccbe4f..a92b3c9ff 100644 --- a/node/node.go +++ b/node/node.go @@ -164,8 +164,6 @@ func NewNode( // Set p2p client and it's validators p2pValidator := p2p.NewValidator(logger.With("module", "p2p_validator"), settlementlc) - conf.P2P.GossipCacheSize = conf.BlockManagerConfig.GossipedBlocksCacheSize - conf.P2P.BoostrapTime = conf.BootstrapTime p2pClient, err := p2p.NewClient(conf.P2P, p2pKey, genesis.ChainID, pubsubServer, logger.With("module", "p2p")) if err != nil { return nil, err diff --git a/node/node_test.go b/node/node_test.go index ae061c244..2d6f92337 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -68,11 +68,10 @@ func TestMempoolDirectly(t *testing.T) { RPC: config.RPCConfig{}, MempoolConfig: *tmcfg.DefaultMempoolConfig(), BlockManagerConfig: config.BlockManagerConfig{ - BlockTime: 100 * time.Millisecond, - BatchSubmitMaxTime: 60 * time.Second, - BlockBatchMaxSizeBytes: 1000, - MaxSupportedBatchSkew: 10, - GossipedBlocksCacheSize: 50, + BlockTime: 100 * time.Millisecond, + BatchSubmitMaxTime: 60 * time.Second, + BlockBatchMaxSizeBytes: 1000, + MaxSupportedBatchSkew: 10, }, DALayer: "mock", DAConfig: "", diff --git a/p2p/client.go b/p2p/client.go index 07a617594..9d3de0b59 100644 --- a/p2p/client.go +++ b/p2p/client.go @@ -82,6 +82,7 @@ func NewClient(conf config.P2PConfig, privKey crypto.PrivKey, chainID string, lo if conf.ListenAddress == "" { conf.ListenAddress = config.DefaultListenAddress } + return &Client{ conf: conf, privKey: privKey, @@ -229,7 +230,7 @@ func (c *Client) listen(ctx context.Context) (host.Host, error) { } func (c *Client) setupDHT(ctx context.Context) error { - seedNodes := c.GetSeedAddrInfo(c.conf.Seeds) + seedNodes := c.GetSeedAddrInfo(c.conf.BootstrapNodes) if len(seedNodes) == 0 { c.logger.Info("no seed nodes - only listening for connections") } @@ -318,9 +319,8 @@ func (c *Client) tryConnect(ctx context.Context, peer peer.AddrInfo) { } func (c *Client) setupGossiping(ctx context.Context) error { - pubsub.GossipSubHistoryGossip = c.conf.GossipCacheSize - pubsub.GossipSubHistoryLength = c.conf.GossipCacheSize - pubsub.GossipSubMaxIHaveMessages = c.conf.GossipCacheSize + pubsub.GossipSubHistoryGossip = c.conf.GossipedBlocksCacheSize + pubsub.GossipSubHistoryLength = c.conf.GossipedBlocksCacheSize ps, err := pubsub.NewGossipSub(ctx, c.Host) if err != nil { @@ -402,7 +402,7 @@ func (c *Client) gossipedBlockReceived(msg *GossipMessage) { } func (c *Client) bootstrapLoop(ctx context.Context) { - ticker := time.NewTicker(c.conf.BoostrapTime) + ticker := time.NewTicker(c.conf.BootstrapTime) defer ticker.Stop() for { select { diff --git a/p2p/client_test.go b/p2p/client_test.go index f9e467bae..5fcbb5f26 100644 --- a/p2p/client_test.go +++ b/p2p/client_test.go @@ -27,8 +27,10 @@ func TestClientStartup(t *testing.T) { err := pubsubServer.Start() require.NoError(t, err) client, err := p2p.NewClient(config.P2PConfig{ - GossipCacheSize: 50, - BoostrapTime: 30 * time.Second, + ListenAddress: config.DefaultListenAddress, + GossipedBlocksCacheSize: 50, + AdvertisingEnabled: true, + BootstrapTime: 30 * time.Second, }, privKey, "TestChain", pubsubServer, log.TestingLogger()) assert := assert.New(t) assert.NoError(err) @@ -178,8 +180,9 @@ func TestSeedStringParsing(t *testing.T) { require := require.New(t) logger := &testutil.MockLogger{} client, err := p2p.NewClient(config.P2PConfig{ - GossipCacheSize: 50, - BoostrapTime: 30 * time.Second, + GossipedBlocksCacheSize: 50, + AdvertisingEnabled: true, + BootstrapTime: 30 * time.Second, }, privKey, "TestNetwork", pubsubServer, logger) require.NoError(err) require.NotNil(client) diff --git a/rpc/client/client_test.go b/rpc/client/client_test.go index 2e42e7a42..168bda2f7 100644 --- a/rpc/client/client_test.go +++ b/rpc/client/client_test.go @@ -100,16 +100,20 @@ func TestGenesisChunked(t *testing.T) { config := config.NodeConfig{ RootDir: "", DBPath: "", - P2P: config.P2PConfig{}, - RPC: config.RPCConfig{}, - BlockManagerConfig: config.BlockManagerConfig{ - BlockTime: 100 * time.Millisecond, - BatchSubmitMaxTime: 60 * time.Second, - BlockBatchMaxSizeBytes: 1000, + P2P: config.P2PConfig{ + ListenAddress: config.DefaultListenAddress, + AdvertisingEnabled: true, + BootstrapNodes: "", GossipedBlocksCacheSize: 50, - MaxSupportedBatchSkew: 10, + BootstrapTime: 30 * time.Second, + }, + RPC: config.RPCConfig{}, + BlockManagerConfig: config.BlockManagerConfig{ + BlockTime: 100 * time.Millisecond, + BatchSubmitMaxTime: 60 * time.Second, + BlockBatchMaxSizeBytes: 1000, + MaxSupportedBatchSkew: 10, }, - BootstrapTime: 30 * time.Second, DALayer: "mock", DAConfig: "", SettlementLayer: "mock", @@ -697,14 +701,19 @@ func TestValidatorSetHandling(t *testing.T) { nodeConfig := config.NodeConfig{ DALayer: "mock", SettlementLayer: "mock", - BlockManagerConfig: config.BlockManagerConfig{ - BlockTime: 10 * time.Millisecond, - BatchSubmitMaxTime: 60 * time.Second, - BlockBatchMaxSizeBytes: 1000, + P2P: config.P2PConfig{ + ListenAddress: config.DefaultListenAddress, + AdvertisingEnabled: true, + BootstrapNodes: "", GossipedBlocksCacheSize: 50, - MaxSupportedBatchSkew: 10, + BootstrapTime: 30 * time.Second, + }, + BlockManagerConfig: config.BlockManagerConfig{ + BlockTime: 10 * time.Millisecond, + BatchSubmitMaxTime: 60 * time.Second, + BlockBatchMaxSizeBytes: 1000, + MaxSupportedBatchSkew: 10, }, - BootstrapTime: 30 * time.Second, SettlementConfig: settlement.Config{ ProposerPubKey: hex.EncodeToString(proposerPubKeyBytes), RollappID: rollappID, @@ -843,19 +852,23 @@ func getRPCInternal(t *testing.T, aggregator bool) (*tmmocks.MockApplication, *C rollappID := "rollapp_1234-1" config := config.NodeConfig{ - RootDir: "", - DBPath: "", - P2P: config.P2PConfig{}, + RootDir: "", + DBPath: "", + P2P: config.P2PConfig{ + ListenAddress: config.DefaultListenAddress, + AdvertisingEnabled: true, + BootstrapNodes: "", + GossipedBlocksCacheSize: 50, + BootstrapTime: 30 * time.Second, + }, RPC: config.RPCConfig{}, MempoolConfig: *tmcfg.DefaultMempoolConfig(), BlockManagerConfig: config.BlockManagerConfig{ - BlockTime: 100 * time.Millisecond, - BatchSubmitMaxTime: 60 * time.Second, - BlockBatchMaxSizeBytes: 1000, - GossipedBlocksCacheSize: 50, - MaxSupportedBatchSkew: 10, + BlockTime: 100 * time.Millisecond, + BatchSubmitMaxTime: 60 * time.Second, + BlockBatchMaxSizeBytes: 1000, + MaxSupportedBatchSkew: 10, }, - BootstrapTime: 30 * time.Second, DALayer: "mock", DAConfig: "", SettlementLayer: "mock", @@ -953,16 +966,18 @@ func TestMempool2Nodes(t *testing.T) { ProposerPubKey: hex.EncodeToString(proposerPK), RollappID: rollappID, }, - BlockManagerConfig: config.BlockManagerConfig{ - BlockTime: 100 * time.Millisecond, - BatchSubmitMaxTime: 60 * time.Second, - BlockBatchMaxSizeBytes: 1000, + P2P: config.P2PConfig{ + ListenAddress: "/ip4/127.0.0.1/tcp/9001", + AdvertisingEnabled: true, + BootstrapNodes: "", GossipedBlocksCacheSize: 50, - MaxSupportedBatchSkew: 10, + BootstrapTime: 30 * time.Second, }, - BootstrapTime: 30 * time.Second, - P2P: config.P2PConfig{ - ListenAddress: "/ip4/127.0.0.1/tcp/9001", + BlockManagerConfig: config.BlockManagerConfig{ + BlockTime: 100 * time.Millisecond, + BatchSubmitMaxTime: 60 * time.Second, + BlockBatchMaxSizeBytes: 1000, + MaxSupportedBatchSkew: 10, }, MempoolConfig: *tmcfg.DefaultMempoolConfig(), }, key1, signingKey1, proxy.NewLocalClientCreator(app), &tmtypes.GenesisDoc{ChainID: rollappID}, log.TestingLogger(), mempool.NopMetrics()) @@ -977,16 +992,17 @@ func TestMempool2Nodes(t *testing.T) { RollappID: rollappID, }, BlockManagerConfig: config.BlockManagerConfig{ - BlockTime: 100 * time.Millisecond, - BatchSubmitMaxTime: 60 * time.Second, - BlockBatchMaxSizeBytes: 1000, - GossipedBlocksCacheSize: 50, - MaxSupportedBatchSkew: 10, + BlockTime: 100 * time.Millisecond, + BatchSubmitMaxTime: 60 * time.Second, + BlockBatchMaxSizeBytes: 1000, + MaxSupportedBatchSkew: 10, }, - BootstrapTime: 30 * time.Second, P2P: config.P2PConfig{ - ListenAddress: "/ip4/127.0.0.1/tcp/9002", - Seeds: "/ip4/127.0.0.1/tcp/9001/p2p/" + id1.String(), + ListenAddress: "/ip4/127.0.0.1/tcp/9002", + BootstrapNodes: "/ip4/127.0.0.1/tcp/9001/p2p/" + id1.String(), + AdvertisingEnabled: true, + BootstrapTime: 30 * time.Second, + GossipedBlocksCacheSize: 50, }, MempoolConfig: *tmcfg.DefaultMempoolConfig(), }, key2, signingKey2, proxy.NewLocalClientCreator(app), &tmtypes.GenesisDoc{ChainID: rollappID}, log.TestingLogger(), mempool.NopMetrics()) diff --git a/rpc/json/service_test.go b/rpc/json/service_test.go index 1a9215454..dbdae0a23 100644 --- a/rpc/json/service_test.go +++ b/rpc/json/service_test.go @@ -302,13 +302,12 @@ func getRPC(t *testing.T) (*tmmocks.MockApplication, *client.Client) { config := config.NodeConfig{ DALayer: "mock", SettlementLayer: "mock", BlockManagerConfig: config.BlockManagerConfig{ - BlockTime: 1 * time.Second, - MaxIdleTime: 0, - MaxSupportedBatchSkew: 10, - BatchSubmitMaxTime: 30 * time.Minute, - NamespaceID: "0102030405060708", - BlockBatchMaxSizeBytes: 1000, - GossipedBlocksCacheSize: 50, + BlockTime: 1 * time.Second, + MaxIdleTime: 0, + MaxSupportedBatchSkew: 10, + BatchSubmitMaxTime: 30 * time.Minute, + NamespaceID: "0102030405060708", + BlockBatchMaxSizeBytes: 1000, }, SettlementConfig: settlement.Config{ ProposerPubKey: hex.EncodeToString(proposerPubKeyBytes), diff --git a/testutil/block.go b/testutil/block.go index b809b0181..8a6e8e6b7 100644 --- a/testutil/block.go +++ b/testutil/block.go @@ -91,8 +91,9 @@ func GetManagerWithProposerKey(conf config.BlockManagerConfig, proposerKey crypt // Init p2p client and validator p2pKey, _, _ := crypto.GenerateEd25519Key(rand.Reader) p2pClient, err := p2p.NewClient(config.P2PConfig{ - GossipCacheSize: 50, - BoostrapTime: 30 * time.Second, + AdvertisingEnabled: true, + GossipedBlocksCacheSize: 50, + BootstrapTime: 30 * time.Second, }, p2pKey, "TestChain", pubsubServer, logger) if err != nil { return nil, err @@ -146,11 +147,10 @@ func initSettlementLayerMock(settlementlc settlement.LayerI, proposer string, pu func GetManagerConfig() config.BlockManagerConfig { return config.BlockManagerConfig{ - BlockTime: 100 * time.Millisecond, - BlockBatchMaxSizeBytes: 1000000, - BatchSubmitMaxTime: 30 * time.Minute, - MaxSupportedBatchSkew: 10, - NamespaceID: "0102030405060708", - GossipedBlocksCacheSize: 50, + BlockTime: 100 * time.Millisecond, + BlockBatchMaxSizeBytes: 1000000, + BatchSubmitMaxTime: 30 * time.Minute, + MaxSupportedBatchSkew: 10, + NamespaceID: "0102030405060708", } } From d53728cf3adf4ff0c12f6ec39510edacb1bb67bf Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Tue, 14 May 2024 13:52:07 +0200 Subject: [PATCH 02/10] fixing tests --- config/flags.go | 1 + config/toml.go | 15 +++++++++++++-- conv/config_test.go | 12 ------------ node/node_test.go | 12 +++++++++--- rpc/json/service_test.go | 6 ++++++ testutil/node.go | 9 ++++----- testutil/p2p.go | 8 +++++--- 7 files changed, 38 insertions(+), 25 deletions(-) diff --git a/config/flags.go b/config/flags.go index d467be2ab..5dbeb57e7 100644 --- a/config/flags.go +++ b/config/flags.go @@ -42,6 +42,7 @@ func AddNodeFlags(cmd *cobra.Command) { cmd.Flags().Duration(FlagBlockTime, def.BlockTime, "block time (for aggregator mode)") cmd.Flags().Duration(FlagMaxIdleTime, def.MaxIdleTime, "max time for empty blocks (for aggregator mode)") cmd.Flags().Duration(FlagBatchSubmitMaxTime, def.BatchSubmitMaxTime, "max time for batch submit (for aggregator mode)") + cmd.Flags().String(FlagNamespaceID, def.NamespaceID, "namespace identifies (8 bytes in hex)") cmd.Flags().Uint64(FlagBlockBatchMaxSizeBytes, def.BlockBatchMaxSizeBytes, "block batch size in bytes") cmd.Flags().String(FlagSettlementLayer, def.SettlementLayer, "Settlement Layer Client name") diff --git a/config/toml.go b/config/toml.go index 28dd23acb..02ec4e86c 100644 --- a/config/toml.go +++ b/config/toml.go @@ -86,11 +86,22 @@ namespace_id = "{{ .BlockManagerConfig.NamespaceID }}" da_config = "{{ .DAConfig }}" +### p2p config ### + +# p2p listen address in the format of /ip4/ip_address/tcp/tcp_port +p2p_listen_address = "{{ .P2P.ListenAddress }}" + +# list of nodes used for P2P bootstrapping +p2p_bootstrap_nodes = "{{ .P2P.BootstrapNodes }}" + +# if not enabled, nodes will not advertised to other nodes to accept connections +advertising = "{{ .P2P.AdvertisingEnabled }}" + # max number of cached messages by gossipsub protocol -gossiped_blocks_cache_size = {{ .BlockManagerConfig.GossipedBlocksCacheSize }} +gossiped_blocks_cache_size = {{ .P2P.GossipedBlocksCacheSize }} # time interval to check if no p2p nodes are connected to bootstrap again -bootstrap_time = "{{ .BootstrapTime }}" +bootstrap_time = "{{ .P2P.BootstrapTime }}" #celestia config example: # da_config = "{\"base_url\":\"http:\/\/127.0.0.1:26658\",\"timeout\":5000000000,\"gas_prices\":0.1,\"auth_token\":\"TOKEN\",\"backoff\":{\"initial_delay\":6000000000,\"max_delay\":6000000000,\"growth_factor\":2},\"retry_attempts\":4,\"retry_delay\":3000000000}" diff --git a/conv/config_test.go b/conv/config_test.go index a9f1d9412..b8707096d 100644 --- a/conv/config_test.go +++ b/conv/config_test.go @@ -13,9 +13,6 @@ import ( func TestGetNodeConfig(t *testing.T) { t.Parallel() - validCosmos := "127.0.0.1:1234" - validDymint := "/ip4/127.0.0.1/tcp/1234" - cases := []struct { name string input func(*tmcfg.Config) @@ -30,15 +27,6 @@ func TestGetNodeConfig(t *testing.T) { nil, true, }, - // GetNodeConfig translates the listen address, so we expect the translated address - { - "ListenAddress", - func(c *tmcfg.Config) { - c.P2P.ListenAddress = validCosmos - }, - func(nc *config.NodeConfig) bool { return nc.P2P.ListenAddress == validDymint }, - false, - }, { "RootDir", func(c *tmcfg.Config) { diff --git a/node/node_test.go b/node/node_test.go index 2d6f92337..60d8391a4 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -62,9 +62,15 @@ func TestMempoolDirectly(t *testing.T) { rollappID := "rollapp_1234-1" nodeConfig := config.NodeConfig{ - RootDir: "", - DBPath: "", - P2P: config.P2PConfig{}, + RootDir: "", + DBPath: "", + P2P: config.P2PConfig{ + ListenAddress: config.DefaultListenAddress, + GossipedBlocksCacheSize: 50, + AdvertisingEnabled: true, + BootstrapTime: 30 * time.Second, + BootstrapNodes: "", + }, RPC: config.RPCConfig{}, MempoolConfig: *tmcfg.DefaultMempoolConfig(), BlockManagerConfig: config.BlockManagerConfig{ diff --git a/rpc/json/service_test.go b/rpc/json/service_test.go index dbdae0a23..0ae256d0d 100644 --- a/rpc/json/service_test.go +++ b/rpc/json/service_test.go @@ -313,6 +313,12 @@ func getRPC(t *testing.T) (*tmmocks.MockApplication, *client.Client) { ProposerPubKey: hex.EncodeToString(proposerPubKeyBytes), RollappID: rollappID, }, + P2P: config.P2PConfig{ + ListenAddress: config.DefaultListenAddress, + GossipedBlocksCacheSize: 50, + AdvertisingEnabled: true, + BootstrapTime: 30 * time.Second, + }, } node, err := node.NewNode( context.Background(), diff --git a/testutil/node.go b/testutil/node.go index faf51a444..d9d1f9a00 100644 --- a/testutil/node.go +++ b/testutil/node.go @@ -28,11 +28,10 @@ func CreateNode(isAggregator bool, blockManagerConfig *config.BlockManagerConfig if blockManagerConfig == nil { blockManagerConfig = &config.BlockManagerConfig{ - BlockTime: 100 * time.Millisecond, - BatchSubmitMaxTime: 60 * time.Second, - BlockBatchMaxSizeBytes: 1000, - GossipedBlocksCacheSize: 50, - MaxSupportedBatchSkew: 10, + BlockTime: 100 * time.Millisecond, + BatchSubmitMaxTime: 60 * time.Second, + BlockBatchMaxSizeBytes: 1000, + MaxSupportedBatchSkew: 10, } } nodeConfig.BlockManagerConfig = *blockManagerConfig diff --git a/testutil/p2p.go b/testutil/p2p.go index 29df50f7d..fb4ec6898 100644 --- a/testutil/p2p.go +++ b/testutil/p2p.go @@ -108,9 +108,11 @@ func StartTestNetwork(ctx context.Context, t *testing.T, n int, conf map[int]Hos clients := make([]*p2p.Client, n) for i := 0; i < n; i++ { client, err := p2p.NewClient(config.P2PConfig{ - Seeds: seeds[i], - GossipCacheSize: 50, - BoostrapTime: 30 * time.Second, + BootstrapNodes: seeds[i], + GossipedBlocksCacheSize: 50, + BootstrapTime: 30 * time.Second, + AdvertisingEnabled: true, + ListenAddress: config.DefaultListenAddress, }, mnet.Hosts()[i].Peerstore().PrivKey(mnet.Hosts()[i].ID()), conf[i].ChainID, From abf5bc287d2b5e0445df5e77eb737cfee6d4d250 Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Tue, 14 May 2024 16:09:25 +0200 Subject: [PATCH 03/10] config fix --- block/manager_test.go | 1 - config/config.go | 7 ++++--- config/config_test.go | 2 +- config/defaults.go | 2 +- config/flags.go | 23 +++++++++++++++++++++++ config/p2p.go | 10 +++------- config/toml.go | 5 +---- node/node.go | 2 +- node/node_test.go | 3 +-- p2p/client.go | 1 - p2p/client_test.go | 2 -- rpc/client/client_test.go | 15 +++++---------- rpc/json/service_test.go | 3 +-- testutil/block.go | 1 - testutil/p2p.go | 1 - 15 files changed, 41 insertions(+), 37 deletions(-) diff --git a/block/manager_test.go b/block/manager_test.go index ab333b650..b70a7a3b5 100644 --- a/block/manager_test.go +++ b/block/manager_test.go @@ -56,7 +56,6 @@ func TestInitialState(t *testing.T) { ListenAddress: config.DefaultListenAddress, GossipedBlocksCacheSize: 50, BootstrapTime: 30 * time.Second, - AdvertisingEnabled: true, }, privKey, "TestChain", pubsubServer, logger) assert.NoError(err) assert.NotNil(p2pClient) diff --git a/config/config.go b/config/config.go index bb9781bb3..59dbd6835 100644 --- a/config/config.go +++ b/config/config.go @@ -25,7 +25,6 @@ type NodeConfig struct { // parameters below are translated from existing config RootDir string DBPath string - P2P P2PConfig RPC RPCConfig MempoolConfig tmcfg.MempoolConfig @@ -38,6 +37,8 @@ type NodeConfig struct { Instrumentation *InstrumentationConfig `mapstructure:"instrumentation"` // Config params for mock grpc da DAGrpc grpc.Config `mapstructure:",squash"` + //P2P Options + P2PConfig `mapstructure:",squash"` } // BlockManagerConfig consists of all parameters required by BlockManagerConfig @@ -98,8 +99,8 @@ func (nc NodeConfig) Validate() error { return fmt.Errorf("BlockManagerConfig: %w", err) } - if err := nc.P2P.Validate(); err != nil { - return fmt.Errorf("BlockManagerConfig: %w", err) + if err := nc.P2PConfig.Validate(); err != nil { + return fmt.Errorf("P2PConfig: %w", err) } if err := nc.validateSettlementLayer(); err != nil { diff --git a/config/config_test.go b/config/config_test.go index 8e4436362..45ca6d96b 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -222,7 +222,7 @@ func fullNodeConfig() config.NodeConfig { Host: "localhost", Port: 9090, }, - P2P: config.P2PConfig{ + P2PConfig: config.P2PConfig{ GossipedBlocksCacheSize: 50, BootstrapTime: 30 * time.Second, ListenAddress: config.DefaultListenAddress, diff --git a/config/defaults.go b/config/defaults.go index f1a6d0136..14a63552d 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -37,7 +37,7 @@ func DefaultConfig(home, chainId string) *NodeConfig { Prometheus: false, PrometheusListenAddr: ":2112", }, - P2P: P2PConfig{ + P2PConfig: P2PConfig{ GossipedBlocksCacheSize: 50, BootstrapTime: 30 * time.Second, ListenAddress: DefaultListenAddress, diff --git a/config/flags.go b/config/flags.go index 5dbeb57e7..d840b2c3a 100644 --- a/config/flags.go +++ b/config/flags.go @@ -28,6 +28,13 @@ const ( FlagRollappID = "dymint.settlement_config.rollapp_id" ) +const ( + FlagP2PListenAddress = "dymint.p2p_config.listen_address" + FlagP2PBootstrapNodes = "dymint.p2p_config.bootstrap_nodes" + FlagP2PGossipCacheSize = "dymint.p2p_config.gossip_cache_size" + FlagP2PBootstrapTime = "dymint.p2p_config.bootstrap_time" +) + // AddNodeFlags adds Dymint specific configuration options to cobra Command. // // This function is called in cosmos-sdk. @@ -54,6 +61,12 @@ func AddNodeFlags(cmd *cobra.Command) { cmd.Flags().String(FlagSLGasPrices, def.SettlementConfig.GasPrices, "Settlement Layer gas prices") cmd.Flags().Uint64(FlagSLGasLimit, def.SettlementConfig.GasLimit, "Settlement Layer batch submit gas limit") cmd.Flags().String(FlagRollappID, def.SettlementConfig.RollappID, "The chainID of the rollapp") + + cmd.Flags().String(FlagP2PListenAddress, def.P2PConfig.ListenAddress, "P2P listen address") + cmd.Flags().String(FlagP2PBootstrapNodes, def.P2PConfig.BootstrapNodes, "P2P bootstrap nodes") + cmd.Flags().Duration(FlagP2PBootstrapTime, def.P2PConfig.BootstrapTime, "P2P bootstrap time") + cmd.Flags().Uint64(FlagP2PGossipCacheSize, uint64(def.P2PConfig.GossipedBlocksCacheSize), "P2P Gossiped blocks cache size") + } func BindDymintFlags(cmd *cobra.Command, v *viper.Viper) error { @@ -105,5 +118,15 @@ func BindDymintFlags(cmd *cobra.Command, v *viper.Viper) error { if err := v.BindPFlag("rollapp_id", cmd.Flags().Lookup(FlagRollappID)); err != nil { return err } + if err := v.BindPFlag("p2p_listen_address", cmd.Flags().Lookup(FlagP2PListenAddress)); err != nil { + return err + } + if err := v.BindPFlag("gossiped_blocks_cache_size", cmd.Flags().Lookup(FlagP2PGossipCacheSize)); err != nil { + return err + } + if err := v.BindPFlag("bootstrap_time", cmd.Flags().Lookup(FlagP2PBootstrapTime)); err != nil { + return err + } + return nil } diff --git a/config/p2p.go b/config/p2p.go index 539461d4a..bc5597e9d 100644 --- a/config/p2p.go +++ b/config/p2p.go @@ -13,21 +13,17 @@ type P2PConfig struct { BootstrapNodes string `mapstructure:"p2p_bootstrap_nodes"` //Size of the Gossipsub router cache GossipedBlocksCacheSize int `mapstructure:"gossiped_blocks_cache_size"` - //If true, the node is advertised in the DHT - AdvertisingEnabled bool `mapstructure:"advertising"` //Time interval a node tries to bootstrap again, in case no nodes connected BootstrapTime time.Duration `mapstructure:"bootstrap_time"` } -// Validate BlockManagerConfig +// Validate P2PConfig func (c P2PConfig) Validate() error { - if c.GossipedBlocksCacheSize < 0 { - return fmt.Errorf("gossipsub cache size cannot be negative.") + return fmt.Errorf("gossipsub cache size cannot be negative") } - if c.BootstrapTime <= 0 { - return fmt.Errorf("bootstrap time must be positive.") + return fmt.Errorf("bootstrap time must be positive") } return nil } diff --git a/config/toml.go b/config/toml.go index 02ec4e86c..93b19108e 100644 --- a/config/toml.go +++ b/config/toml.go @@ -91,12 +91,9 @@ da_config = "{{ .DAConfig }}" # p2p listen address in the format of /ip4/ip_address/tcp/tcp_port p2p_listen_address = "{{ .P2P.ListenAddress }}" -# list of nodes used for P2P bootstrapping +# list of nodes used for P2P bootstrapping in the format of /ip4/ip_address/tcp/port/p2p/ID p2p_bootstrap_nodes = "{{ .P2P.BootstrapNodes }}" -# if not enabled, nodes will not advertised to other nodes to accept connections -advertising = "{{ .P2P.AdvertisingEnabled }}" - # max number of cached messages by gossipsub protocol gossiped_blocks_cache_size = {{ .P2P.GossipedBlocksCacheSize }} diff --git a/node/node.go b/node/node.go index a92b3c9ff..2499ab60a 100644 --- a/node/node.go +++ b/node/node.go @@ -164,7 +164,7 @@ func NewNode( // Set p2p client and it's validators p2pValidator := p2p.NewValidator(logger.With("module", "p2p_validator"), settlementlc) - p2pClient, err := p2p.NewClient(conf.P2P, p2pKey, genesis.ChainID, pubsubServer, logger.With("module", "p2p")) + p2pClient, err := p2p.NewClient(conf.P2PConfig, p2pKey, genesis.ChainID, pubsubServer, logger.With("module", "p2p")) if err != nil { return nil, err } diff --git a/node/node_test.go b/node/node_test.go index 60d8391a4..af181d0f0 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -64,10 +64,9 @@ func TestMempoolDirectly(t *testing.T) { nodeConfig := config.NodeConfig{ RootDir: "", DBPath: "", - P2P: config.P2PConfig{ + P2PConfig: config.P2PConfig{ ListenAddress: config.DefaultListenAddress, GossipedBlocksCacheSize: 50, - AdvertisingEnabled: true, BootstrapTime: 30 * time.Second, BootstrapNodes: "", }, diff --git a/p2p/client.go b/p2p/client.go index 9d3de0b59..8b11f233b 100644 --- a/p2p/client.go +++ b/p2p/client.go @@ -102,7 +102,6 @@ func NewClient(conf config.P2PConfig, privKey crypto.PrivKey, chainID string, lo func (c *Client) Start(ctx context.Context) error { // create new, cancelable context ctx, c.cancel = context.WithCancel(ctx) - c.logger.Debug("starting P2P client") host, err := c.listen(ctx) if err != nil { return err diff --git a/p2p/client_test.go b/p2p/client_test.go index 5fcbb5f26..5b6c847d7 100644 --- a/p2p/client_test.go +++ b/p2p/client_test.go @@ -29,7 +29,6 @@ func TestClientStartup(t *testing.T) { client, err := p2p.NewClient(config.P2PConfig{ ListenAddress: config.DefaultListenAddress, GossipedBlocksCacheSize: 50, - AdvertisingEnabled: true, BootstrapTime: 30 * time.Second, }, privKey, "TestChain", pubsubServer, log.TestingLogger()) assert := assert.New(t) @@ -181,7 +180,6 @@ func TestSeedStringParsing(t *testing.T) { logger := &testutil.MockLogger{} client, err := p2p.NewClient(config.P2PConfig{ GossipedBlocksCacheSize: 50, - AdvertisingEnabled: true, BootstrapTime: 30 * time.Second, }, privKey, "TestNetwork", pubsubServer, logger) require.NoError(err) diff --git a/rpc/client/client_test.go b/rpc/client/client_test.go index 168bda2f7..d0139746e 100644 --- a/rpc/client/client_test.go +++ b/rpc/client/client_test.go @@ -100,9 +100,8 @@ func TestGenesisChunked(t *testing.T) { config := config.NodeConfig{ RootDir: "", DBPath: "", - P2P: config.P2PConfig{ + P2PConfig: config.P2PConfig{ ListenAddress: config.DefaultListenAddress, - AdvertisingEnabled: true, BootstrapNodes: "", GossipedBlocksCacheSize: 50, BootstrapTime: 30 * time.Second, @@ -701,9 +700,8 @@ func TestValidatorSetHandling(t *testing.T) { nodeConfig := config.NodeConfig{ DALayer: "mock", SettlementLayer: "mock", - P2P: config.P2PConfig{ + P2PConfig: config.P2PConfig{ ListenAddress: config.DefaultListenAddress, - AdvertisingEnabled: true, BootstrapNodes: "", GossipedBlocksCacheSize: 50, BootstrapTime: 30 * time.Second, @@ -854,9 +852,8 @@ func getRPCInternal(t *testing.T, aggregator bool) (*tmmocks.MockApplication, *C config := config.NodeConfig{ RootDir: "", DBPath: "", - P2P: config.P2PConfig{ + P2PConfig: config.P2PConfig{ ListenAddress: config.DefaultListenAddress, - AdvertisingEnabled: true, BootstrapNodes: "", GossipedBlocksCacheSize: 50, BootstrapTime: 30 * time.Second, @@ -966,9 +963,8 @@ func TestMempool2Nodes(t *testing.T) { ProposerPubKey: hex.EncodeToString(proposerPK), RollappID: rollappID, }, - P2P: config.P2PConfig{ + P2PConfig: config.P2PConfig{ ListenAddress: "/ip4/127.0.0.1/tcp/9001", - AdvertisingEnabled: true, BootstrapNodes: "", GossipedBlocksCacheSize: 50, BootstrapTime: 30 * time.Second, @@ -997,10 +993,9 @@ func TestMempool2Nodes(t *testing.T) { BlockBatchMaxSizeBytes: 1000, MaxSupportedBatchSkew: 10, }, - P2P: config.P2PConfig{ + P2PConfig: config.P2PConfig{ ListenAddress: "/ip4/127.0.0.1/tcp/9002", BootstrapNodes: "/ip4/127.0.0.1/tcp/9001/p2p/" + id1.String(), - AdvertisingEnabled: true, BootstrapTime: 30 * time.Second, GossipedBlocksCacheSize: 50, }, diff --git a/rpc/json/service_test.go b/rpc/json/service_test.go index 0ae256d0d..9cf26cecc 100644 --- a/rpc/json/service_test.go +++ b/rpc/json/service_test.go @@ -313,10 +313,9 @@ func getRPC(t *testing.T) (*tmmocks.MockApplication, *client.Client) { ProposerPubKey: hex.EncodeToString(proposerPubKeyBytes), RollappID: rollappID, }, - P2P: config.P2PConfig{ + P2PConfig: config.P2PConfig{ ListenAddress: config.DefaultListenAddress, GossipedBlocksCacheSize: 50, - AdvertisingEnabled: true, BootstrapTime: 30 * time.Second, }, } diff --git a/testutil/block.go b/testutil/block.go index 8a6e8e6b7..26ef95c4e 100644 --- a/testutil/block.go +++ b/testutil/block.go @@ -91,7 +91,6 @@ func GetManagerWithProposerKey(conf config.BlockManagerConfig, proposerKey crypt // Init p2p client and validator p2pKey, _, _ := crypto.GenerateEd25519Key(rand.Reader) p2pClient, err := p2p.NewClient(config.P2PConfig{ - AdvertisingEnabled: true, GossipedBlocksCacheSize: 50, BootstrapTime: 30 * time.Second, }, p2pKey, "TestChain", pubsubServer, logger) diff --git a/testutil/p2p.go b/testutil/p2p.go index fb4ec6898..752019b33 100644 --- a/testutil/p2p.go +++ b/testutil/p2p.go @@ -111,7 +111,6 @@ func StartTestNetwork(ctx context.Context, t *testing.T, n int, conf map[int]Hos BootstrapNodes: seeds[i], GossipedBlocksCacheSize: 50, BootstrapTime: 30 * time.Second, - AdvertisingEnabled: true, ListenAddress: config.DefaultListenAddress, }, mnet.Hosts()[i].Peerstore().PrivKey(mnet.Hosts()[i].ID()), From 923ccbf33768bc5afaf8edd5bf70bd43e54c5445 Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Tue, 14 May 2024 16:19:49 +0200 Subject: [PATCH 04/10] params rename --- config/flags.go | 7 +++++-- config/p2p.go | 4 ++-- config/toml.go | 6 ++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/config/flags.go b/config/flags.go index d840b2c3a..a63bd3995 100644 --- a/config/flags.go +++ b/config/flags.go @@ -121,10 +121,13 @@ func BindDymintFlags(cmd *cobra.Command, v *viper.Viper) error { if err := v.BindPFlag("p2p_listen_address", cmd.Flags().Lookup(FlagP2PListenAddress)); err != nil { return err } - if err := v.BindPFlag("gossiped_blocks_cache_size", cmd.Flags().Lookup(FlagP2PGossipCacheSize)); err != nil { + if err := v.BindPFlag("p2p_gossiped_blocks_cache_size", cmd.Flags().Lookup(FlagP2PGossipCacheSize)); err != nil { return err } - if err := v.BindPFlag("bootstrap_time", cmd.Flags().Lookup(FlagP2PBootstrapTime)); err != nil { + if err := v.BindPFlag("p2p_bootstrap_time", cmd.Flags().Lookup(FlagP2PBootstrapTime)); err != nil { + return err + } + if err := v.BindPFlag("p2p_bootstrap_nodes", cmd.Flags().Lookup(FlagP2PBootstrapNodes)); err != nil { return err } diff --git a/config/p2p.go b/config/p2p.go index bc5597e9d..5f79c6091 100644 --- a/config/p2p.go +++ b/config/p2p.go @@ -12,9 +12,9 @@ type P2PConfig struct { //List of nodes used for P2P bootstrapping BootstrapNodes string `mapstructure:"p2p_bootstrap_nodes"` //Size of the Gossipsub router cache - GossipedBlocksCacheSize int `mapstructure:"gossiped_blocks_cache_size"` + GossipedBlocksCacheSize int `mapstructure:"p2p_gossiped_blocks_cache_size"` //Time interval a node tries to bootstrap again, in case no nodes connected - BootstrapTime time.Duration `mapstructure:"bootstrap_time"` + BootstrapTime time.Duration `mapstructure:"p2p_bootstrap_time"` } // Validate P2PConfig diff --git a/config/toml.go b/config/toml.go index 93b19108e..273daacc4 100644 --- a/config/toml.go +++ b/config/toml.go @@ -95,10 +95,12 @@ p2p_listen_address = "{{ .P2P.ListenAddress }}" p2p_bootstrap_nodes = "{{ .P2P.BootstrapNodes }}" # max number of cached messages by gossipsub protocol -gossiped_blocks_cache_size = {{ .P2P.GossipedBlocksCacheSize }} +p2p_gossiped_blocks_cache_size = {{ .P2P.GossipedBlocksCacheSize }} # time interval to check if no p2p nodes are connected to bootstrap again -bootstrap_time = "{{ .P2P.BootstrapTime }}" +p2p_bootstrap_time = "{{ .P2P.BootstrapTime }}" + + #celestia config example: # da_config = "{\"base_url\":\"http:\/\/127.0.0.1:26658\",\"timeout\":5000000000,\"gas_prices\":0.1,\"auth_token\":\"TOKEN\",\"backoff\":{\"initial_delay\":6000000000,\"max_delay\":6000000000,\"growth_factor\":2},\"retry_attempts\":4,\"retry_delay\":3000000000}" From e76dacda9d85df928f8ec65391fa2ee2f4e14596 Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Tue, 14 May 2024 16:24:41 +0200 Subject: [PATCH 05/10] remove extra param config --- config/toml.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/config/toml.go b/config/toml.go index 273daacc4..123c79af1 100644 --- a/config/toml.go +++ b/config/toml.go @@ -100,8 +100,6 @@ p2p_gossiped_blocks_cache_size = {{ .P2P.GossipedBlocksCacheSize }} # time interval to check if no p2p nodes are connected to bootstrap again p2p_bootstrap_time = "{{ .P2P.BootstrapTime }}" - - #celestia config example: # da_config = "{\"base_url\":\"http:\/\/127.0.0.1:26658\",\"timeout\":5000000000,\"gas_prices\":0.1,\"auth_token\":\"TOKEN\",\"backoff\":{\"initial_delay\":6000000000,\"max_delay\":6000000000,\"growth_factor\":2},\"retry_attempts\":4,\"retry_delay\":3000000000}" # Avail config example: From 1913a9ee3ebdc4ff764bdfdaf14d97af35637d98 Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Tue, 14 May 2024 16:28:29 +0200 Subject: [PATCH 06/10] toml fix --- config/toml.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/toml.go b/config/toml.go index 123c79af1..842fd9597 100644 --- a/config/toml.go +++ b/config/toml.go @@ -89,16 +89,16 @@ da_config = "{{ .DAConfig }}" ### p2p config ### # p2p listen address in the format of /ip4/ip_address/tcp/tcp_port -p2p_listen_address = "{{ .P2P.ListenAddress }}" +p2p_listen_address = "{{ .P2PConfig.ListenAddress }}" # list of nodes used for P2P bootstrapping in the format of /ip4/ip_address/tcp/port/p2p/ID -p2p_bootstrap_nodes = "{{ .P2P.BootstrapNodes }}" +p2p_bootstrap_nodes = "{{ .P2PConfig.BootstrapNodes }}" # max number of cached messages by gossipsub protocol -p2p_gossiped_blocks_cache_size = {{ .P2P.GossipedBlocksCacheSize }} +p2p_gossiped_blocks_cache_size = {{ .P2PConfig.GossipedBlocksCacheSize }} # time interval to check if no p2p nodes are connected to bootstrap again -p2p_bootstrap_time = "{{ .P2P.BootstrapTime }}" +p2p_bootstrap_time = "{{ .P2PConfig.BootstrapTime }}" #celestia config example: # da_config = "{\"base_url\":\"http:\/\/127.0.0.1:26658\",\"timeout\":5000000000,\"gas_prices\":0.1,\"auth_token\":\"TOKEN\",\"backoff\":{\"initial_delay\":6000000000,\"max_delay\":6000000000,\"growth_factor\":2},\"retry_attempts\":4,\"retry_delay\":3000000000}" From 5e5091c8f6ce99bec9ef03473383024bc560f6ca Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Tue, 14 May 2024 16:39:17 +0200 Subject: [PATCH 07/10] set advertising option --- config/defaults.go | 1 + config/p2p.go | 2 ++ config/toml.go | 4 ++++ p2p/client.go | 8 +++++--- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/config/defaults.go b/config/defaults.go index 14a63552d..64f2b6fc0 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -42,6 +42,7 @@ func DefaultConfig(home, chainId string) *NodeConfig { BootstrapTime: 30 * time.Second, ListenAddress: DefaultListenAddress, BootstrapNodes: "", + Advertising: true, }, } diff --git a/config/p2p.go b/config/p2p.go index 5f79c6091..b4eb45f46 100644 --- a/config/p2p.go +++ b/config/p2p.go @@ -15,6 +15,8 @@ type P2PConfig struct { GossipedBlocksCacheSize int `mapstructure:"p2p_gossiped_blocks_cache_size"` //Time interval a node tries to bootstrap again, in case no nodes connected BootstrapTime time.Duration `mapstructure:"p2p_bootstrap_time"` + //Time interval a node tries to bootstrap again, in case no nodes connected + Advertising bool `mapstructure:"p2p_advertising"` } // Validate P2PConfig diff --git a/config/toml.go b/config/toml.go index 842fd9597..e180ef2a2 100644 --- a/config/toml.go +++ b/config/toml.go @@ -100,6 +100,10 @@ p2p_gossiped_blocks_cache_size = {{ .P2PConfig.GossipedBlocksCacheSize }} # time interval to check if no p2p nodes are connected to bootstrap again p2p_bootstrap_time = "{{ .P2PConfig.BootstrapTime }}" +# set to false to disable advertising the node to the P2P network +p2p_advertising= "{{ .P2PConfig.Advertising }}" + + #celestia config example: # da_config = "{\"base_url\":\"http:\/\/127.0.0.1:26658\",\"timeout\":5000000000,\"gas_prices\":0.1,\"auth_token\":\"TOKEN\",\"backoff\":{\"initial_delay\":6000000000,\"max_delay\":6000000000,\"growth_factor\":2},\"retry_attempts\":4,\"retry_delay\":3000000000}" # Avail config example: diff --git a/p2p/client.go b/p2p/client.go index 8b11f233b..8d8a65c29 100644 --- a/p2p/client.go +++ b/p2p/client.go @@ -264,9 +264,11 @@ func (c *Client) peerDiscovery(ctx context.Context) error { return err } - err = c.advertise(ctx) - if err != nil { - return err + if c.conf.Advertising { + err = c.advertise(ctx) + if err != nil { + return err + } } err = c.findPeers(ctx) From 68091c2a8877afd3fcf546596c10d341cb2932be Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Wed, 15 May 2024 09:23:01 +0200 Subject: [PATCH 08/10] godoc fix --- config/p2p.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/p2p.go b/config/p2p.go index b4eb45f46..4f0f9f748 100644 --- a/config/p2p.go +++ b/config/p2p.go @@ -15,7 +15,7 @@ type P2PConfig struct { GossipedBlocksCacheSize int `mapstructure:"p2p_gossiped_blocks_cache_size"` //Time interval a node tries to bootstrap again, in case no nodes connected BootstrapTime time.Duration `mapstructure:"p2p_bootstrap_time"` - //Time interval a node tries to bootstrap again, in case no nodes connected + //Param used to enable the advertisement of the node to be part of the P2P network in the DHT Advertising bool `mapstructure:"p2p_advertising"` } From 49d0c2d15e9ceef1e44b7de54ad504c0529c55d8 Mon Sep 17 00:00:00 2001 From: Sergi Rene Date: Wed, 15 May 2024 21:25:18 +0200 Subject: [PATCH 09/10] param renamed --- config/defaults.go | 2 +- config/p2p.go | 2 +- config/toml.go | 2 +- p2p/client.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/config/defaults.go b/config/defaults.go index 64f2b6fc0..8d4be6473 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -42,7 +42,7 @@ func DefaultConfig(home, chainId string) *NodeConfig { BootstrapTime: 30 * time.Second, ListenAddress: DefaultListenAddress, BootstrapNodes: "", - Advertising: true, + AdvertisingEnabled: true, }, } diff --git a/config/p2p.go b/config/p2p.go index 4f0f9f748..6ea8f3c2f 100644 --- a/config/p2p.go +++ b/config/p2p.go @@ -16,7 +16,7 @@ type P2PConfig struct { //Time interval a node tries to bootstrap again, in case no nodes connected BootstrapTime time.Duration `mapstructure:"p2p_bootstrap_time"` //Param used to enable the advertisement of the node to be part of the P2P network in the DHT - Advertising bool `mapstructure:"p2p_advertising"` + AdvertisingEnabled bool `mapstructure:"p2p_advertising_enabled"` } // Validate P2PConfig diff --git a/config/toml.go b/config/toml.go index e180ef2a2..fb99ef967 100644 --- a/config/toml.go +++ b/config/toml.go @@ -101,7 +101,7 @@ p2p_gossiped_blocks_cache_size = {{ .P2PConfig.GossipedBlocksCacheSize }} p2p_bootstrap_time = "{{ .P2PConfig.BootstrapTime }}" # set to false to disable advertising the node to the P2P network -p2p_advertising= "{{ .P2PConfig.Advertising }}" +p2p_advertising_enabled= "{{ .P2PConfig.AdvertisingEnabled }}" #celestia config example: diff --git a/p2p/client.go b/p2p/client.go index 8d8a65c29..15da8bb33 100644 --- a/p2p/client.go +++ b/p2p/client.go @@ -264,7 +264,7 @@ func (c *Client) peerDiscovery(ctx context.Context) error { return err } - if c.conf.Advertising { + if c.conf.AdvertisingEnabled { err = c.advertise(ctx) if err != nil { return err From 0454f42101ddc673ded0f0044b4f7541ccded021 Mon Sep 17 00:00:00 2001 From: Michael Tsitrin Date: Thu, 16 May 2024 10:11:47 +0300 Subject: [PATCH 10/10] linter --- config/p2p.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/p2p.go b/config/p2p.go index 544ae768b..e3d3bd6a7 100644 --- a/config/p2p.go +++ b/config/p2p.go @@ -15,7 +15,7 @@ type P2PConfig struct { GossipedBlocksCacheSize int `mapstructure:"p2p_gossiped_blocks_cache_size"` // Time interval a node tries to bootstrap again, in case no nodes connected BootstrapRetryTime time.Duration `mapstructure:"p2p_bootstrap_retry_time"` - //Param used to enable the advertisement of the node to be part of the P2P network in the DHT + // Param used to enable the advertisement of the node to be part of the P2P network in the DHT AdvertisingEnabled bool `mapstructure:"p2p_advertising_enabled"` }