Skip to content

Commit

Permalink
Merge pull request #374 from iotaledger/feat/revive-featurenet
Browse files Browse the repository at this point in the history
Publish ExternalMultiAddresses via DHT
  • Loading branch information
karimodm authored Sep 27, 2023
2 parents 26f46d5 + e99104e commit 67756f4
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 40 deletions.
32 changes: 18 additions & 14 deletions components/p2p/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ type dependencies struct {
}

func initConfigParams(c *dig.Container) error {

type cfgResult struct {
dig.Out
P2PDatabasePath string `name:"p2pDatabasePath"`
Expand Down Expand Up @@ -151,7 +150,6 @@ func provide(c *dig.Container) error {
}

if err := c.Provide(func(deps configManagerDeps) *p2p.ConfigManager {

p2pConfigManager := p2p.NewConfigManager(func(peers []*p2p.PeerConfigItem) error {
if err := deps.PeeringConfig.Set(CfgPeers, peers); err != nil {
return err
Expand Down Expand Up @@ -230,7 +228,6 @@ func provide(c *dig.Container) error {
}

if err := c.Provide(func(deps p2pDeps) p2pResult {

res := p2pResult{}

privKeyFilePath := filepath.Join(deps.P2PDatabasePath, "identity.key")
Expand All @@ -251,15 +248,6 @@ func provide(c *dig.Container) error {
Component.LogInfof(`loaded existing private key for peer identity from "%s"`, privKeyFilePath)
}

peeringIP, err := readPeerIP()
if err != nil {
Component.LogFatalAndExit(err)
}

if !peeringIP.IsGlobalUnicast() {
Component.LogWarnf("IP is not a global unicast address: %s", peeringIP)
}

connManager, err := connmgr.NewConnManager(
ParamsP2P.ConnectionManager.LowWatermark,
ParamsP2P.ConnectionManager.HighWatermark,
Expand All @@ -269,13 +257,30 @@ func provide(c *dig.Container) error {
Component.LogPanicf("unable to initialize connection manager: %s", err)
}

// also see "defaults.go" in go-libp2p for the default values
createdHost, err := libp2p.New(
libp2p.ListenAddrStrings(ParamsP2P.BindMultiAddresses...),
libp2p.Identity(nodePrivateKey),
libp2p.Transport(tcp.NewTCPTransport),
libp2p.ConnectionManager(connManager),
libp2p.NATPortMap(),
// Define a custom address factory to inject external addresses to the DHT advertisements.
libp2p.AddrsFactory(func() func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {
var externalMultiAddrs []multiaddr.Multiaddr
if len(ParamsP2P.ExternalMultiAddresses) > 0 {
for _, externalMultiAddress := range ParamsP2P.ExternalMultiAddresses {
addr, err := multiaddr.NewMultiaddr(externalMultiAddress)
if err != nil {
Component.LogPanicf("unable to parse external multi address %s: %s", externalMultiAddress, err)
}

externalMultiAddrs = append(externalMultiAddrs, addr)
}
}

return func(addrs []multiaddr.Multiaddr) []multiaddr.Multiaddr {
return append(addrs, externalMultiAddrs...)
}
}()),
)
if err != nil {
Component.LogFatalfAndExit("unable to initialize libp2p host: %s", err)
Expand All @@ -292,7 +297,6 @@ func provide(c *dig.Container) error {
return c.Provide(func(host host.Host, peerDB *network.DB) *p2p.Manager {
return p2p.NewManager(host, peerDB, Component.Logger())
})

}

func configure() error {
Expand Down
8 changes: 2 additions & 6 deletions components/p2p/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ type ParametersP2P struct {
LowWatermark int `default:"5" usage:"the minimum connections count to hold after the high watermark was reached"`
}

// Seed defines the config flag of the autopeering private key seed.
Seed string `usage:"private key seed used to derive the node identity; optional base58 or base64 encoded 256-bit string. Prefix with 'base58:' or 'base64', respectively"`
// OverwriteStoredSeed defines whether the private key stored in an existing peerdb should be overwritten.
OverwriteStoredSeed bool `default:"false" usage:"whether to overwrite the private key if an existing peerdb exists"`
// ExternalAddress defines the config flag of the network external address.
ExternalAddress string `default:"auto" usage:"external IP address under which the node is reachable; or 'auto' to determine it automatically"`
// ExternalMultiAddress defines additional p2p multiaddresses to be advertised via DHT.
ExternalMultiAddresses []string `default:"" usage:"external reacheable multi addresses advertised to the network"`

// Defines the private key used to derive the node identity (optional).
IdentityPrivateKey string `default:"" usage:"private key used to derive the node identity (optional)"`
Expand Down
16 changes: 0 additions & 16 deletions components/p2p/utils.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package p2p

import (
"net"
"path/filepath"
"strings"

"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/kvstore"
Expand All @@ -12,20 +10,6 @@ import (
"github.com/iotaledger/iota-core/pkg/storage/database"
)

func readPeerIP() (net.IP, error) {
if strings.ToLower(ParamsP2P.ExternalAddress) == "auto" {
// let the autopeering discover the IP
return net.IPv4zero, nil
}

peeringIP := net.ParseIP(ParamsP2P.ExternalAddress)
if peeringIP == nil {
return nil, ierrors.Errorf("invalid IP address: %s", ParamsP2P.ExternalAddress)
}

return peeringIP, nil
}

// inits the peer database.
func initPeerDB() (peerDB *network.DB, peerDBKVStore kvstore.KVStore, err error) {
if err = checkValidPeerDBPath(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion deploy/ansible/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- firewall

- hosts: cores:&internal_nodes
gather_facts: no
gather_facts: yes
roles:
- wireguard
- iota-core-node
Expand Down
2 changes: 1 addition & 1 deletion deploy/ansible/deploy_cores.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- hosts: cores:&internal_nodes
gather_facts: no
gather_facts: yes
roles:
- iota-core-node
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#jinja2: trim_blocks:True, lstrip_blocks:True
{% set ips = [] -%}
{% for interface in ansible_interfaces -%}
{%- set interface_details = hostvars[inventory_hostname]['ansible_' + interface] %}
{%- if interface_details.ipv4 is defined and 'address' in interface_details.ipv4 -%}
{%- set _ = ips.append("/ip4/" + interface_details.ipv4.address + "/tcp/14666") -%}
{%- endif -%}
{% endfor -%}
version: '3.3'

services:
Expand All @@ -18,7 +25,6 @@ services:
# prometheus
- "0.0.0.0:9311:9311/tcp"
environment:
- P2P_BINDMULTIADDRESSES=/ip4/0.0.0.0/tcp/14666
- WEBAPI_BINDADDRESS=0.0.0.0:8080
- DASHBOARD_BINDADDRESS=0.0.0.0:8081
- PROFILING_BINDADDRESS=0.0.0.0:6061
Expand Down Expand Up @@ -48,7 +54,8 @@ services:
--restAPI.blockIssuerPrivateKey={{validatorPrivKey}}
--inx.blockIssuerAccount={{validatorAccount}}
--inx.blockIssuerPrivateKey={{validatorPrivKey}}
--p2p.peers=/dns/node-01.feature/tcp/14666/p2p/12D3KooWPZt81CSFZLswrV6y6GogLTqUHDAcNxWtov1sSMQiHmSn
--p2p.peers=/dns/node-01.feature/tcp/14666/p2p/12D3KooWCrjmh4dUCWfGVQT6ivzArieJB9Z3eKdy2mdEEN95NDPS
--p2p.externalMultiAddresses={{ ips | join(',') }}
--p2p.identityPrivateKey={{p2pIdentityPrivateKey}}
--inx.enabled=true
--inx.bindAddress=iota-core:9029
Expand Down

0 comments on commit 67756f4

Please sign in to comment.