|
| 1 | +package p2p |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/libp2p/go-libp2p/core" |
| 9 | + "github.com/libp2p/go-libp2p/core/peer" |
| 10 | + "github.com/multiformats/go-multiaddr" |
| 11 | + manet "github.com/multiformats/go-multiaddr/net" |
| 12 | + |
| 13 | + "github.com/oasisprotocol/oasis-core/go/common/crypto/signature" |
| 14 | + "github.com/oasisprotocol/oasis-core/go/common/node" |
| 15 | + "github.com/oasisprotocol/oasis-core/go/common/persistent" |
| 16 | + "github.com/oasisprotocol/oasis-core/go/common/version" |
| 17 | + "github.com/oasisprotocol/oasis-core/go/p2p/api" |
| 18 | + "github.com/oasisprotocol/oasis-core/go/p2p/config" |
| 19 | + "github.com/oasisprotocol/oasis-core/go/worker/common/configparser" |
| 20 | +) |
| 21 | + |
| 22 | +// Config describes a set of P2P settings for a peer. |
| 23 | +type Config struct { |
| 24 | + Addresses []multiaddr.Multiaddr |
| 25 | + |
| 26 | + HostConfig |
| 27 | + GossipSubConfig |
| 28 | + BootstrapDiscoveryConfig |
| 29 | +} |
| 30 | + |
| 31 | +// HostConfig describes a set of settings for a host. |
| 32 | +type HostConfig struct { |
| 33 | + Signer signature.Signer |
| 34 | + |
| 35 | + UserAgent string |
| 36 | + ListenAddr multiaddr.Multiaddr |
| 37 | + Port uint16 |
| 38 | + |
| 39 | + ConnManagerConfig |
| 40 | + ConnGaterConfig |
| 41 | +} |
| 42 | + |
| 43 | +// ConnManagerConfig describes a set of settings for a connection manager. |
| 44 | +type ConnManagerConfig struct { |
| 45 | + MinPeers int |
| 46 | + MaxPeers int |
| 47 | + GracePeriod time.Duration |
| 48 | + PersistentPeers []peer.ID |
| 49 | +} |
| 50 | + |
| 51 | +// ConnGaterConfig describes a set of settings for a connection gater. |
| 52 | +type ConnGaterConfig struct { |
| 53 | + BlockedPeers []net.IP |
| 54 | +} |
| 55 | + |
| 56 | +// GossipSubConfig describes a set of settings for a gossip pubsub. |
| 57 | +type GossipSubConfig struct { |
| 58 | + // XXX: Main config has int64, but here just int -- investigate. |
| 59 | + PeerOutboundQueueSize int |
| 60 | + ValidateQueueSize int |
| 61 | + ValidateThrottle int |
| 62 | + |
| 63 | + PersistentPeers []peer.AddrInfo |
| 64 | +} |
| 65 | + |
| 66 | +// Load loads a default P2P configuration. |
| 67 | +func (cfg *Config) Load(yamlCfg *config.Config) error { |
| 68 | + rawAddresses, err := configparser.ParseAddressList(yamlCfg.Registration.Addresses) |
| 69 | + if err != nil { |
| 70 | + return fmt.Errorf("failed to parse address list: %w", err) |
| 71 | + } |
| 72 | + var addresses []multiaddr.Multiaddr |
| 73 | + for _, addr := range rawAddresses { |
| 74 | + var mAddr multiaddr.Multiaddr |
| 75 | + mAddr, err = manet.FromNetAddr(addr.ToTCPAddr()) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("failed to convert address to multiaddress: %w", err) |
| 78 | + } |
| 79 | + addresses = append(addresses, mAddr) |
| 80 | + } |
| 81 | + |
| 82 | + var hostCfg HostConfig |
| 83 | + if err := hostCfg.Load(yamlCfg); err != nil { |
| 84 | + return fmt.Errorf("failed to load host config: %w", err) |
| 85 | + } |
| 86 | + |
| 87 | + var gossipSubCfg GossipSubConfig |
| 88 | + if err := gossipSubCfg.Load(yamlCfg); err != nil { |
| 89 | + return fmt.Errorf("failed to load gossipsub config: %w", err) |
| 90 | + } |
| 91 | + |
| 92 | + var bootstrapCfg BootstrapDiscoveryConfig |
| 93 | + if err := bootstrapCfg.Load(yamlCfg); err != nil { |
| 94 | + return fmt.Errorf("failed to load bootstrap config: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + cfg.Addresses = addresses |
| 98 | + cfg.HostConfig = hostCfg |
| 99 | + cfg.GossipSubConfig = gossipSubCfg |
| 100 | + cfg.BootstrapDiscoveryConfig = bootstrapCfg |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +// Load loads host configuration. |
| 106 | +func (cfg *HostConfig) Load(yamlCfg *config.Config) error { |
| 107 | + userAgent := fmt.Sprintf("oasis-core/%s", version.SoftwareVersion) |
| 108 | + |
| 109 | + port := yamlCfg.Port |
| 110 | + |
| 111 | + // Listen for connections on all interfaces. |
| 112 | + listenAddr, err := multiaddr.NewMultiaddr( |
| 113 | + fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port), |
| 114 | + ) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("failed to create multiaddress: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + var cmCfg ConnManagerConfig |
| 120 | + if err = cmCfg.Load(&yamlCfg.ConnectionManager); err != nil { |
| 121 | + return fmt.Errorf("failed to load connection manager config: %w", err) |
| 122 | + } |
| 123 | + |
| 124 | + var cgCfg ConnGaterConfig |
| 125 | + if err = cgCfg.Load(yamlCfg.ConnectionGater.BlockedPeerIPs); err != nil { |
| 126 | + return fmt.Errorf("failed to load connection gater config: %w", err) |
| 127 | + } |
| 128 | + |
| 129 | + cfg.UserAgent = userAgent |
| 130 | + cfg.Port = port |
| 131 | + cfg.ListenAddr = listenAddr |
| 132 | + cfg.ConnManagerConfig = cmCfg |
| 133 | + cfg.ConnGaterConfig = cgCfg |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +// Load loads connection manager configuration. |
| 139 | +func (cfg *ConnManagerConfig) Load(yamlCfg *config.ConnectionManagerConfig) error { |
| 140 | + persistentPeersMap := make(map[core.PeerID]struct{}) |
| 141 | + for _, pp := range yamlCfg.PersistentPeers { |
| 142 | + var addr node.ConsensusAddress |
| 143 | + if err := addr.UnmarshalText([]byte(pp)); err != nil { |
| 144 | + return fmt.Errorf("malformed address (expected pubkey@IP:port): %w", err) |
| 145 | + } |
| 146 | + |
| 147 | + pid, err := api.PublicKeyToPeerID(addr.ID) |
| 148 | + if err != nil { |
| 149 | + return fmt.Errorf("invalid public key (%s): %w", addr.ID, err) |
| 150 | + } |
| 151 | + |
| 152 | + persistentPeersMap[pid] = struct{}{} |
| 153 | + } |
| 154 | + |
| 155 | + persistentPeers := make([]peer.ID, 0) |
| 156 | + for pid := range persistentPeersMap { |
| 157 | + persistentPeers = append(persistentPeers, pid) |
| 158 | + } |
| 159 | + |
| 160 | + cfg.MinPeers = yamlCfg.MaxNumPeers |
| 161 | + cfg.MaxPeers = cfg.MinPeers + peersHighWatermarkDelta |
| 162 | + cfg.GracePeriod = yamlCfg.PeerGracePeriod |
| 163 | + cfg.PersistentPeers = persistentPeers |
| 164 | + |
| 165 | + return nil |
| 166 | +} |
| 167 | + |
| 168 | +// Load loads connection gater configuration. |
| 169 | +func (cfg *ConnGaterConfig) Load(blocked []string) error { |
| 170 | + blockedPeers := make([]net.IP, 0) |
| 171 | + for _, blockedIP := range blocked { |
| 172 | + parsedIP := net.ParseIP(blockedIP) |
| 173 | + if parsedIP == nil { |
| 174 | + return fmt.Errorf("malformed blocked IP: %s", blockedIP) |
| 175 | + } |
| 176 | + blockedPeers = append(blockedPeers, parsedIP) |
| 177 | + } |
| 178 | + |
| 179 | + cfg.BlockedPeers = blockedPeers |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
| 183 | + |
| 184 | +// Load loads gossipsub configuration. |
| 185 | +func (cfg *GossipSubConfig) Load(yamlCfg *config.Config) error { |
| 186 | + persistentPeers, err := api.AddrInfosFromConsensusAddrs(yamlCfg.ConnectionManager.PersistentPeers) |
| 187 | + if err != nil { |
| 188 | + return fmt.Errorf("failed to convert persistent peers' addresses: %w", err) |
| 189 | + } |
| 190 | + |
| 191 | + cfg.PeerOutboundQueueSize = yamlCfg.Gossipsub.PeerOutboundQueueSize |
| 192 | + cfg.ValidateQueueSize = yamlCfg.Gossipsub.ValidateQueueSize |
| 193 | + cfg.ValidateThrottle = yamlCfg.Gossipsub.ValidateThrottle |
| 194 | + cfg.PersistentPeers = persistentPeers |
| 195 | + |
| 196 | + return nil |
| 197 | +} |
| 198 | + |
| 199 | +// BootstrapDiscoveryConfig describes a set of settings for a discovery. |
| 200 | +type BootstrapDiscoveryConfig struct { |
| 201 | + Enable bool |
| 202 | + Seeds []peer.AddrInfo |
| 203 | + RetentionPeriod time.Duration |
| 204 | +} |
| 205 | + |
| 206 | +// Load loads bootstrap discovery configuration. |
| 207 | +func (cfg *BootstrapDiscoveryConfig) Load(yamlCfg *config.Config) error { |
| 208 | + seeds, err := api.AddrInfosFromConsensusAddrs(yamlCfg.Seeds) |
| 209 | + if err != nil { |
| 210 | + return fmt.Errorf("failed to convert seeds' addresses: %w", err) |
| 211 | + } |
| 212 | + |
| 213 | + cfg.Seeds = seeds |
| 214 | + cfg.Enable = yamlCfg.Discovery.Bootstrap.Enable |
| 215 | + cfg.RetentionPeriod = yamlCfg.Discovery.Bootstrap.RetentionPeriod |
| 216 | + |
| 217 | + return nil |
| 218 | +} |
| 219 | + |
| 220 | +// SeedConfig describes a set of settings for a seed. |
| 221 | +type SeedConfig struct { |
| 222 | + CommonStore *persistent.CommonStore |
| 223 | + |
| 224 | + HostConfig |
| 225 | + BootstrapDiscoveryConfig |
| 226 | +} |
| 227 | + |
| 228 | +// Load loads seed configuration. |
| 229 | +func (cfg *SeedConfig) Load(yamlCfg *config.Config) error { |
| 230 | + var hostCfg HostConfig |
| 231 | + if err := hostCfg.Load(yamlCfg); err != nil { |
| 232 | + return fmt.Errorf("failed to load host config: %w", err) |
| 233 | + } |
| 234 | + |
| 235 | + var bootstrapCfg BootstrapDiscoveryConfig |
| 236 | + if err := bootstrapCfg.Load(yamlCfg); err != nil { |
| 237 | + return fmt.Errorf("failed to load bootstrap config: %w", err) |
| 238 | + } |
| 239 | + |
| 240 | + cfg.HostConfig = hostCfg |
| 241 | + cfg.BootstrapDiscoveryConfig = bootstrapCfg |
| 242 | + |
| 243 | + return nil |
| 244 | +} |
0 commit comments