github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/p2p/config/config.go (about) 1 package config 2 3 import ( 4 "time" 5 6 "github.com/gnolang/gno/tm2/pkg/errors" 7 ) 8 9 // ----------------------------------------------------------------------------- 10 // P2PConfig 11 12 const ( 13 // FuzzModeDrop is a mode in which we randomly drop reads/writes, connections or sleep 14 FuzzModeDrop = iota 15 // FuzzModeDelay is a mode in which we randomly sleep 16 FuzzModeDelay 17 ) 18 19 // P2PConfig defines the configuration options for the Tendermint peer-to-peer networking layer 20 type P2PConfig struct { 21 RootDir string `toml:"home"` 22 23 // Address to listen for incoming connections 24 ListenAddress string `toml:"laddr" comment:"Address to listen for incoming connections"` 25 26 // Address to advertise to peers for them to dial 27 ExternalAddress string `toml:"external_address" comment:"Address to advertise to peers for them to dial\n If empty, will use the same port as the laddr,\n and will introspect on the listener or use UPnP\n to figure out the address."` 28 29 // Comma separated list of seed nodes to connect to 30 Seeds string `toml:"seeds" comment:"Comma separated list of seed nodes to connect to"` 31 32 // Comma separated list of nodes to keep persistent connections to 33 PersistentPeers string `toml:"persistent_peers" comment:"Comma separated list of nodes to keep persistent connections to"` 34 35 // UPNP port forwarding 36 UPNP bool `toml:"upnp" comment:"UPNP port forwarding"` 37 38 // Maximum number of inbound peers 39 MaxNumInboundPeers int `toml:"max_num_inbound_peers" comment:"Maximum number of inbound peers"` 40 41 // Maximum number of outbound peers to connect to, excluding persistent peers 42 MaxNumOutboundPeers int `toml:"max_num_outbound_peers" comment:"Maximum number of outbound peers to connect to, excluding persistent peers"` 43 44 // Time to wait before flushing messages out on the connection 45 FlushThrottleTimeout time.Duration `toml:"flush_throttle_timeout" comment:"Time to wait before flushing messages out on the connection"` 46 47 // Maximum size of a message packet payload, in bytes 48 MaxPacketMsgPayloadSize int `toml:"max_packet_msg_payload_size" comment:"Maximum size of a message packet payload, in bytes"` 49 50 // Rate at which packets can be sent, in bytes/second 51 SendRate int64 `toml:"send_rate" comment:"Rate at which packets can be sent, in bytes/second"` 52 53 // Rate at which packets can be received, in bytes/second 54 RecvRate int64 `toml:"recv_rate" comment:"Rate at which packets can be received, in bytes/second"` 55 56 // Set true to enable the peer-exchange reactor 57 PexReactor bool `toml:"pex" comment:"Set true to enable the peer-exchange reactor"` 58 59 // Seed mode, in which node constantly crawls the network and looks for 60 // peers. If another node asks it for addresses, it responds and disconnects. 61 // 62 // Does not work if the peer-exchange reactor is disabled. 63 SeedMode bool `toml:"seed_mode" comment:"Seed mode, in which node constantly crawls the network and looks for\n peers. If another node asks it for addresses, it responds and disconnects.\n\n Does not work if the peer-exchange reactor is disabled."` 64 65 // Comma separated list of peer IDs to keep private (will not be gossiped to 66 // other peers) 67 PrivatePeerIDs string `toml:"private_peer_ids" comment:"Comma separated list of peer IDs to keep private (will not be gossiped to other peers)"` 68 69 // Toggle to disable guard against peers connecting from the same ip. 70 AllowDuplicateIP bool `toml:"allow_duplicate_ip" comment:"Toggle to disable guard against peers connecting from the same ip."` 71 72 // Peer connection configuration. 73 HandshakeTimeout time.Duration `toml:"handshake_timeout" comment:"Peer connection configuration."` 74 DialTimeout time.Duration `toml:"dial_timeout"` 75 76 // Testing params. 77 // Force dial to fail 78 TestDialFail bool `toml:"test_dial_fail"` 79 // FUzz connection 80 TestFuzz bool `toml:"test_fuzz"` 81 TestFuzzConfig *FuzzConnConfig `toml:"test_fuzz_config"` 82 } 83 84 // DefaultP2PConfig returns a default configuration for the peer-to-peer layer 85 func DefaultP2PConfig() *P2PConfig { 86 return &P2PConfig{ 87 ListenAddress: "tcp://0.0.0.0:26656", 88 ExternalAddress: "", 89 UPNP: false, 90 MaxNumInboundPeers: 40, 91 MaxNumOutboundPeers: 10, 92 FlushThrottleTimeout: 100 * time.Millisecond, 93 MaxPacketMsgPayloadSize: 1024, // 1 kB 94 SendRate: 5120000, // 5 mB/s 95 RecvRate: 5120000, // 5 mB/s 96 PexReactor: true, 97 SeedMode: false, 98 AllowDuplicateIP: false, 99 HandshakeTimeout: 20 * time.Second, 100 DialTimeout: 3 * time.Second, 101 TestDialFail: false, 102 TestFuzz: false, 103 TestFuzzConfig: DefaultFuzzConnConfig(), 104 } 105 } 106 107 // TestP2PConfig returns a configuration for testing the peer-to-peer layer 108 func TestP2PConfig() *P2PConfig { 109 cfg := DefaultP2PConfig() 110 cfg.ListenAddress = "tcp://0.0.0.0:36656" 111 cfg.FlushThrottleTimeout = 10 * time.Millisecond 112 cfg.AllowDuplicateIP = true 113 return cfg 114 } 115 116 // ValidateBasic performs basic validation (checking param bounds, etc.) and 117 // returns an error if any check fails. 118 func (cfg *P2PConfig) ValidateBasic() error { 119 if cfg.MaxNumInboundPeers < 0 { 120 return errors.New("max_num_inbound_peers can't be negative") 121 } 122 if cfg.MaxNumOutboundPeers < 0 { 123 return errors.New("max_num_outbound_peers can't be negative") 124 } 125 if cfg.FlushThrottleTimeout < 0 { 126 return errors.New("flush_throttle_timeout can't be negative") 127 } 128 if cfg.MaxPacketMsgPayloadSize < 0 { 129 return errors.New("max_packet_msg_payload_size can't be negative") 130 } 131 if cfg.SendRate < 0 { 132 return errors.New("send_rate can't be negative") 133 } 134 if cfg.RecvRate < 0 { 135 return errors.New("recv_rate can't be negative") 136 } 137 return nil 138 } 139 140 // FuzzConnConfig is a FuzzedConnection configuration. 141 type FuzzConnConfig struct { 142 Mode int 143 MaxDelay time.Duration 144 ProbDropRW float64 145 ProbDropConn float64 146 ProbSleep float64 147 } 148 149 // DefaultFuzzConnConfig returns the default config. 150 func DefaultFuzzConnConfig() *FuzzConnConfig { 151 return &FuzzConnConfig{ 152 Mode: FuzzModeDrop, 153 MaxDelay: 3 * time.Second, 154 ProbDropRW: 0.2, 155 ProbDropConn: 0.00, 156 ProbSleep: 0.00, 157 } 158 }