Skip to content

Commit

Permalink
feat(p2p): refactor p2p config options (#861)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel T <[email protected]>
  • Loading branch information
srene and danwt authored May 16, 2024
1 parent 73aae62 commit 70d9460
Show file tree
Hide file tree
Showing 21 changed files with 203 additions and 347 deletions.
5 changes: 3 additions & 2 deletions block/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ 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,
BootstrapRetryTime: 30 * time.Second,
}, privKey, "TestChain", pubsubServer, logger)
assert.NoError(err)
assert.NotNil(p2pClient)
Expand Down
11 changes: 5 additions & 6 deletions block/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 9 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -37,8 +36,9 @@ 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"`
// P2P Options
P2PConfig `mapstructure:",squash"`
}

// BlockManagerConfig consists of all parameters required by BlockManagerConfig
Expand All @@ -55,9 +55,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.
Expand Down Expand Up @@ -100,6 +99,10 @@ func (nc NodeConfig) Validate() error {
return fmt.Errorf("BlockManagerConfig: %w", err)
}

if err := nc.P2PConfig.Validate(); err != nil {
return fmt.Errorf("p2p config: %w", err)
}

if err := nc.validateSettlementLayer(); err != nil {
return fmt.Errorf("SettlementLayer: %w", err)
}
Expand Down Expand Up @@ -150,10 +153,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")
}
Expand Down
27 changes: 13 additions & 14 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -229,5 +222,11 @@ func fullNodeConfig() config.NodeConfig {
Host: "localhost",
Port: 9090,
},
P2PConfig: config.P2PConfig{
GossipedBlocksCacheSize: 50,
BootstrapRetryTime: 30 * time.Second,
ListenAddress: config.DefaultListenAddress,
BootstrapNodes: "",
},
}
}
28 changes: 14 additions & 14 deletions config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,27 +22,27 @@ 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",
Instrumentation: &InstrumentationConfig{
Prometheus: false,
PrometheusListenAddr: ":2112",
},
BootstrapTime: 30 * time.Second,
P2PConfig: P2PConfig{
GossipedBlocksCacheSize: 50,
BootstrapRetryTime: 30 * time.Second,
ListenAddress: DefaultListenAddress,
BootstrapNodes: "",
},
}

if home == "" {
Expand Down
25 changes: 25 additions & 0 deletions config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
FlagP2PBootstrapRetryTime = "dymint.p2p_config.bootstrap_retry_time"
)

// AddNodeFlags adds Dymint specific configuration options to cobra Command.
//
// This function is called in cosmos-sdk.
Expand All @@ -54,6 +61,11 @@ 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(FlagP2PBootstrapRetryTime, def.P2PConfig.BootstrapRetryTime, "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 {
Expand Down Expand Up @@ -105,5 +117,18 @@ 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("p2p_gossiped_blocks_cache_size", cmd.Flags().Lookup(FlagP2PGossipCacheSize)); err != nil {
return err
}
if err := v.BindPFlag("p2p_bootstrap_retry_time", cmd.Flags().Lookup(FlagP2PBootstrapRetryTime)); err != nil {
return err
}
if err := v.BindPFlag("p2p_bootstrap_nodes", cmd.Flags().Lookup(FlagP2PBootstrapNodes)); err != nil {
return err
}

return nil
}
28 changes: 23 additions & 5 deletions config/p2p.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
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:"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"`
}

// Validate P2PConfig
func (c P2PConfig) Validate() error {
if c.GossipedBlocksCacheSize < 0 {
return fmt.Errorf("gossipsub cache size cannot be negative")
}
if c.BootstrapRetryTime <= 0 {
return fmt.Errorf("bootstrap time must be positive")
}
return nil
}
12 changes: 10 additions & 2 deletions config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,19 @@ 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 = "{{ .P2PConfig.ListenAddress }}"
# list of nodes used for P2P bootstrapping in the format of /ip4/ip_address/tcp/port/p2p/ID
p2p_bootstrap_nodes = "{{ .P2PConfig.BootstrapNodes }}"
# max number of cached messages by gossipsub protocol
gossiped_blocks_cache_size = {{ .BlockManagerConfig.GossipedBlocksCacheSize }}
p2p_gossiped_blocks_cache_size = {{ .P2PConfig.GossipedBlocksCacheSize }}
# time interval to check if no p2p nodes are connected to bootstrap again
bootstrap_time = "{{ .BootstrapTime }}"
p2p_bootstrap_retry_time = "{{ .P2PConfig.BootstrapRetryTime }}"
#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}"
Expand Down
67 changes: 0 additions & 67 deletions conv/addr.go

This file was deleted.

Loading

0 comments on commit 70d9460

Please sign in to comment.