Skip to content

Commit 12db08b

Browse files
committed
use private ips for libp2p && DigitalOcean
1 parent b7743c9 commit 12db08b

File tree

7 files changed

+57
-6
lines changed

7 files changed

+57
-6
lines changed

activities/loadtest/mocks/nodei.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ func (m MockNode) GetIP(ctx context.Context) (string, error) {
6868
return m.IP, nil
6969
}
7070

71+
func (m MockNode) GetPrivateIP(ctx context.Context) (string, error) {
72+
return m.GetIP(ctx)
73+
}
74+
7175
func (m MockNode) GetExternalAddress(ctx context.Context, s string) (string, error) {
7276
//TODO implement me
7377
panic("implement me")

petri/core/provider/digitalocean/task.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ func (t *Task) DownloadDir(ctx context.Context, s string, s2 string) error {
252252
panic("implement me")
253253
}
254254

255+
// GetIP returns *Tailscale* IP.
256+
// todo refactor to GetIP(ctx, type=[private, tailscale, public, ...])
255257
func (t *Task) GetIP(ctx context.Context) (string, error) {
256258
return t.getTailscaleIp(ctx)
257259
}
@@ -265,6 +267,16 @@ func (t *Task) GetExternalAddress(ctx context.Context, port string) (string, err
265267
return net.JoinHostPort(ip, port), nil
266268
}
267269

270+
// GetPrivateIP returns node's private IP address
271+
func (t *Task) GetPrivateIP(ctx context.Context) (string, error) {
272+
droplet, err := t.GetDroplet(ctx)
273+
if err != nil {
274+
return "", fmt.Errorf("failed to get droplet: %w", err)
275+
}
276+
277+
return droplet.PrivateIPv4()
278+
}
279+
268280
func (t *Task) RunCommand(ctx context.Context, cmd []string) (string, string, int, error) {
269281
status, err := t.GetStatus(ctx)
270282
if err != nil {

petri/core/provider/docker/task.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ func (t *Task) GetIP(ctx context.Context) (string, error) {
131131
return ip, nil
132132
}
133133

134+
// GetPrivateIP returns node's private IP address
135+
func (t *Task) GetPrivateIP(ctx context.Context) (string, error) {
136+
return t.GetIP(ctx)
137+
}
138+
134139
func (t *Task) WaitForStatus(ctx context.Context, interval time.Duration, desiredStatus provider.TaskStatus) error {
135140
for {
136141
select {

petri/core/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type TaskI interface {
3636
DownloadDir(context.Context, string, string) error
3737

3838
GetIP(context.Context) (string, error)
39+
GetPrivateIP(context.Context) (string, error)
3940
GetExternalAddress(context.Context, string) (string, error)
4041
DialContext() func(context.Context, string, string) (net.Conn, error)
4142

petri/core/types/node.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,12 @@ type NodeI interface {
103103
GetDefinition() provider.TaskDefinition
104104

105105
// GetIP returns the IP address of the node
106+
// todo refactor to GetIP(ctx, type=[private, tailscale, public, ...])
106107
GetIP(context.Context) (string, error)
107108

109+
// GetPrivateIP returns node's private IP address
110+
GetPrivateIP(context.Context) (string, error)
111+
108112
// Serialize serializes the node
109113
Serialize(context.Context, provider.ProviderI) ([]byte, error)
110114
}

petri/cosmos/chain/chain.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,6 @@ func configureNode(
10081008
return err
10091009
}
10101010

1011-
// todo if libp2p=true, then store addressbook.toml in a proper format!
10121011
persistentPeersString, err := persistentPeers.AsCometPeerString(ctx, useExternalAddress)
10131012
if err != nil {
10141013
return fmt.Errorf("failed to get comet peer string for persistent peers: %w", err)
@@ -1032,7 +1031,15 @@ func configureNode(
10321031
useLibP2P, addressBookFile := chainConfig.UseLibP2P()
10331032
if useLibP2P {
10341033
logger.Info("Using lib-p2p!", zap.String("address_book_file", addressBookFile))
1035-
err = writeLibP2PAddressBook(ctx, node, addressBookFile, useExternalAddress, seeds, persistentPeers)
1034+
err = writeLibP2PAddressBook(
1035+
ctx,
1036+
node,
1037+
addressBookFile,
1038+
useExternalAddress,
1039+
seeds,
1040+
persistentPeers,
1041+
)
1042+
10361043
if err != nil {
10371044
return fmt.Errorf("writeLibP2PAddressBook: %w", err)
10381045
}
@@ -1051,13 +1058,15 @@ func writeLibP2PAddressBook(
10511058
) error {
10521059
peers := []any{}
10531060

1061+
isDocker := !useExternalAddress
1062+
10541063
// combine all the peer sets into list of peers
10551064
for _, peerSet := range peerSets {
10561065
if peerSet.Empty() {
10571066
continue
10581067
}
10591068

1060-
elements, err := peerSet.AsLibP2PAddressBook(ctx, useExternalAddress)
1069+
elements, err := peerSet.AsLibP2PAddressBook(ctx, isDocker)
10611070
if err != nil {
10621071
return fmt.Errorf("failed to get libp2p address book for peer set: %w", err)
10631072
}

petri/cosmos/chain/peers.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,19 @@ func (ps *PeerSet) AsCometPeerString(ctx context.Context, useExternal bool) (str
6262
// AsLibP2PAddressBook returns a map of node IDs to addresses of chain nodes.
6363
// Format it [{host: "1.2.3.4:26656", id: "<lib-p2p-peer-id>"}, {...}, ...]
6464
// @see https://github.com/cometbft/cometbft/blob/608fe92cbc3774c6cdf36c59c56b6c8362489ef1/lp2p/addressbook.go#L16
65-
func (ps *PeerSet) AsLibP2PAddressBook(ctx context.Context, useExternal bool) ([]any, error) {
65+
func (ps *PeerSet) AsLibP2PAddressBook(ctx context.Context, isDocker bool) ([]any, error) {
6666
peers := make([]any, 0, len(ps.peers))
6767

6868
resolveHost := peerHostInternal
69-
if useExternal {
70-
resolveHost = peerHostExternal
69+
if !isDocker {
70+
// This breaks geo-distributed testnet for go-libp2p.
71+
//
72+
// Currently DigitalOcean Droplet are configured via Tailscale, which
73+
// causes issues go-libp2p connection (tailscale's IP are fetched via peerHostExternal)
74+
// Thus, we use VMs private IPs.
75+
//
76+
// TODO: come up with a better solution that doesn't use Tailscale, but supports multiple regions.
77+
resolveHost = peerHostPrivate
7178
}
7279

7380
for _, n := range ps.peers {
@@ -105,6 +112,15 @@ func peerHostInternal(ctx context.Context, n petri.NodeI) (string, error) {
105112
return fmt.Sprintf("%s:%s", ip, cometPort), nil
106113
}
107114

115+
func peerHostPrivate(ctx context.Context, n petri.NodeI) (string, error) {
116+
ip, err := n.GetPrivateIP(ctx)
117+
if err != nil {
118+
return "", err
119+
}
120+
121+
return fmt.Sprintf("%s:%s", ip, cometPort), nil
122+
}
123+
108124
// used for digitalocean
109125
func peerIDFromNode(ctx context.Context, n petri.NodeI) (peer.ID, error) {
110126
cometPubKey, err := n.PubKey(ctx)

0 commit comments

Comments
 (0)