Skip to content

Commit

Permalink
Add support for public broadcast IP (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
galt-tr committed Feb 15, 2024
1 parent ac1e8fa commit 16daf6e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type (
P2PConfig struct {
AlertSystemProtocolID string `json:"alert_system_protocol_id" mapstructure:"alert_system_protocol_id"` // AlertSystemProtocolID is the protocol ID to use on the libp2p network for alert system communication
BootstrapPeer string `json:"bootstrap_peer" mapstructure:"bootstrap_peer"` // BootstrapPeer is the bootstrap peer for the libp2p network
BroadcastIP string `json:"broadcast_ip" mapstructure:"broadcast_ip"` // BroadcastIP is the public facing IP address to broadcast to other peers
IP string `json:"ip" mapstructure:"ip"` // IP is the IP address for the P2P server
Port string `json:"port" mapstructure:"port"` // Port is the port for the P2P server
PrivateKeyPath string `json:"private_key_path" mapstructure:"private_key_path"` // PrivateKeyPath is the path to the private key
Expand Down
1 change: 1 addition & 0 deletions app/config/envs/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"p2p": {
"ip": "0.0.0.0",
"port": "9906",
"broadcast_ip": "",
"alert_system_protocol_id": "/bitcoin/alert-system/1.0.0",
"bootstrap_peer": "",
"private_key_path": "",
Expand Down
1 change: 1 addition & 0 deletions app/config/envs/stn.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"port": "9906",
"alert_system_protocol_id": "/bitcoin-stn/alert-system/0.0.1",
"bootstrap_peer": "",
"broadcast_ip": "",
"private_key_path": "",
"topic_name": "bitcoin_alert_system_stn"
},
Expand Down
1 change: 1 addition & 0 deletions app/config/envs/testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"port": "9906",
"alert_system_protocol_id": "/bitcoin-testnet/alert-system/0.0.1",
"bootstrap_peer": "",
"broadcast_ip": "",
"private_key_path": "",
"topic_name": "bitcoin_alert_system_testnet"
},
Expand Down
19 changes: 19 additions & 0 deletions app/p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"time"

maddr "github.com/multiformats/go-multiaddr"

"github.com/libp2p/go-libp2p/core/discovery"

dht "github.com/libp2p/go-libp2p-kad-dht"
Expand Down Expand Up @@ -73,12 +75,29 @@ func NewServer(o ServerOptions) (*Server, error) {
}
}

var extMultiAddr maddr.Multiaddr
if o.Config.P2P.BroadcastIP != "" {
extMultiAddr, err = maddr.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%s", o.Config.P2P.BroadcastIP, o.Config.P2P.Port))
if err != nil {
return nil, err
}
}

addressFactory := func(addrs []maddr.Multiaddr) []maddr.Multiaddr {
if extMultiAddr != nil {
// here we're appending the external facing multiaddr we created above to the addressFactory so it will be broadcast out when I connect to a bootstrap node.
addrs = append(addrs, extMultiAddr)
}
return addrs
}

// Create a new host
var h host.Host
if h, err = libp2p.New(
libp2p.ListenAddrStrings(fmt.Sprintf("/ip4/%s/tcp/%s", o.Config.P2P.IP, o.Config.P2P.Port)),
libp2p.Identity(*pk),
libp2p.EnableHolePunching(),
libp2p.AddrsFactory(addressFactory),
); err != nil {
return nil, err
}
Expand Down

0 comments on commit 16daf6e

Please sign in to comment.