Skip to content

Commit f29c4fa

Browse files
committed
Pass Wormhole arguments from command line or env. vars
1 parent f75a38f commit f29c4fa

File tree

4 files changed

+86
-31
lines changed

4 files changed

+86
-31
lines changed

hermes/src/config.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,21 @@ pub enum Options {
2424
#[structopt(long)]
2525
id_secp256k1: Option<PathBuf>,
2626

27-
/// Multiaddress for a Wormhole bootstrap peer.
28-
#[structopt(long)]
29-
wormhole_peer: Option<String>,
30-
31-
/// Multiaddress to bind Wormhole P2P to.
32-
#[structopt(long)]
33-
wormhole_addr: Option<Multiaddr>,
27+
/// Network ID for Wormhole
28+
#[structopt(long, env = "WORMHOLE_NETWORK_ID")]
29+
wh_network_id: String,
30+
31+
/// Multiaddresses for Wormhole bootstrap peers (separated by comma).
32+
#[structopt(long, env = "WORMHOLE_BOOTSTRAP_ADDRS")]
33+
wh_bootstrap_addrs: String,
34+
35+
/// Multiaddresses to bind Wormhole P2P to (separated by comma)
36+
#[structopt(
37+
long,
38+
default_value = "/ip4/0.0.0.0/udp/30910/quic,/ip6/::/udp/30910/quic",
39+
env = "WORMHOLE_LISTEN_ADDRS"
40+
)]
41+
wh_listen_addrs: String,
3442

3543
/// The address to bind the RPC server to.
3644
#[structopt(long, default_value = "127.0.0.1:3399")]

hermes/src/main.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ async fn init(_update_channel: Receiver<AccountUpdate>) -> Result<()> {
5151
config::Options::Run {
5252
id: _,
5353
id_secp256k1: _,
54-
wormhole_addr: _,
55-
wormhole_peer: _,
54+
wh_network_id,
55+
wh_bootstrap_addrs,
56+
wh_listen_addrs,
5657
rpc_addr,
5758
p2p_addr,
5859
p2p_peer: _,
@@ -61,7 +62,13 @@ async fn init(_update_channel: Receiver<AccountUpdate>) -> Result<()> {
6162

6263
// Spawn the P2P layer.
6364
log::info!("Starting P2P server on {}", p2p_addr);
64-
network::p2p::spawn(handle_message).await?;
65+
network::p2p::spawn(
66+
handle_message,
67+
wh_network_id.to_string(),
68+
wh_bootstrap_addrs.to_string(),
69+
wh_listen_addrs.to_string(),
70+
)
71+
.await?;
6572

6673
// Spawn the RPC server.
6774
log::info!("Starting RPC server on {}", rpc_addr);

hermes/src/network/p2p.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package main
99

1010
// #include <stdlib.h>
11+
// #include <string.h>
1112
//
1213
// // A structure containing Wormhole VAA observations. This must match on both
1314
// // the Go and Rust side.
@@ -27,6 +28,7 @@ import "C"
2728
import (
2829
"context"
2930
"fmt"
31+
"strings"
3032

3133
"github.com/libp2p/go-libp2p"
3234
"github.com/libp2p/go-libp2p/core/crypto"
@@ -45,17 +47,16 @@ import (
4547
)
4648

4749
//export RegisterObservationCallback
48-
func RegisterObservationCallback(f C.callback_t) {
50+
func RegisterObservationCallback(f C.callback_t, network_id, bootstrap_addrs, listen_addrs *C.char) {
4951
go func() {
5052
ctx := context.Background()
5153

54+
networkID := C.GoString(network_id)
55+
bootstrapAddrs := strings.Split(C.GoString(bootstrap_addrs), ",")
56+
listenAddrs := strings.Split(C.GoString(listen_addrs), ",")
57+
5258
// Setup base network configuration.
53-
networkID := "/wormhole/testnet/2/1"
5459
priv, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
55-
bootstrapPeers := []string{
56-
//"/dns4/wormhole-mainnet-v2-bootstrap.certus.one/udp/8999/quic/p2p/12D3KooWQp644DK27fd3d4Km3jr7gHiuJJ5ZGmy8hH4py7fP4FP7",
57-
"/dns4/wormhole-testnet-v2-bootstrap.certus.one/udp/8999/quic/p2p/12D3KooWAkB9ynDur1Jtoa97LBUp8RXdhzS5uHgAfdTquJbrbN7i",
58-
}
5960

6061
// Setup libp2p Connection Manager.
6162
mgr, err := connmgr.NewConnManager(
@@ -73,16 +74,13 @@ func RegisterObservationCallback(f C.callback_t) {
7374
// Setup libp2p Reactor.
7475
h, err := libp2p.New(
7576
libp2p.Identity(priv),
76-
libp2p.ListenAddrStrings(
77-
"/ip4/0.0.0.0/udp/30910/quic",
78-
"/ip6/::/udp/30910/quic",
79-
),
77+
libp2p.ListenAddrStrings(listenAddrs...),
8078
libp2p.Security(libp2ptls.ID, libp2ptls.New),
8179
libp2p.Transport(libp2pquic.NewTransport),
8280
libp2p.ConnectionManager(mgr),
8381
libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
8482
bootstrappers := make([]peer.AddrInfo, 0)
85-
for _, addr := range bootstrapPeers {
83+
for _, addr := range bootstrapAddrs {
8684
ma, err := multiaddr.NewMultiaddr(addr)
8785
if err != nil {
8886
continue

hermes/src/network/p2p.rs

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,28 @@
1111
1212
use {
1313
anyhow::Result,
14-
std::sync::{
15-
mpsc::{
16-
Receiver,
17-
Sender,
14+
std::{
15+
ffi::{
16+
c_char,
17+
CString,
18+
},
19+
sync::{
20+
mpsc::{
21+
Receiver,
22+
Sender,
23+
},
24+
Mutex,
1825
},
19-
Mutex,
2026
},
2127
};
2228

2329
extern "C" {
24-
fn RegisterObservationCallback(cb: extern "C" fn(o: ObservationC));
30+
fn RegisterObservationCallback(
31+
cb: extern "C" fn(o: ObservationC),
32+
network_id: *const c_char,
33+
bootstrap_addrs: *const c_char,
34+
listen_addrs: *const c_char,
35+
);
2536
}
2637

2738
// An `Observation` C type passed back to us from Go.
@@ -64,22 +75,53 @@ extern "C" fn proxy(o: ObservationC) {
6475
/// TODO: handle_message should be capable of handling more than just Observations. But we don't
6576
/// have our own P2P network, we pass it in to keep the code structure and read directly from the
6677
/// OBSERVATIONS channel in the RPC for now.
67-
pub fn bootstrap<H>(_handle_message: H) -> Result<()>
78+
pub fn bootstrap<H>(
79+
_handle_message: H,
80+
network_id: String,
81+
wh_bootstrap_addrs: String,
82+
wh_listen_addrs: String,
83+
) -> Result<()>
6884
where
6985
H: Fn(Observation) -> Result<()> + 'static,
7086
{
87+
log::warn!("Network ID: {:?}", network_id);
88+
let c_network_id = CString::new(network_id)?;
89+
let c_wh_bootstrap_addrs = CString::new(wh_bootstrap_addrs)?;
90+
let c_wh_listen_addrs = CString::new(wh_listen_addrs)?;
91+
7192
// Launch the Go LibP2P Reactor.
7293
unsafe {
73-
RegisterObservationCallback(proxy as extern "C" fn(o: ObservationC));
94+
RegisterObservationCallback(
95+
proxy as extern "C" fn(observation: ObservationC),
96+
c_network_id.as_ptr(),
97+
c_wh_bootstrap_addrs.as_ptr(),
98+
c_wh_listen_addrs.as_ptr(),
99+
);
100+
101+
// The memory will be freed when the Go function finishes using the
102+
// pointers since C.GoString creates a copy of the strings.
103+
std::mem::forget(c_network_id);
104+
std::mem::forget(c_wh_bootstrap_addrs);
105+
std::mem::forget(c_wh_listen_addrs);
74106
}
75107
Ok(())
76108
}
77109

78110
// Spawn's the P2P layer as a separate thread via Go.
79-
pub async fn spawn<H>(handle_message: H) -> Result<()>
111+
pub async fn spawn<H>(
112+
handle_message: H,
113+
network_id: String,
114+
wh_bootstrap_addrs: String,
115+
wh_listen_addrs: String,
116+
) -> Result<()>
80117
where
81118
H: Fn(Observation) -> Result<()> + Send + 'static,
82119
{
83-
bootstrap(handle_message)?;
120+
bootstrap(
121+
handle_message,
122+
network_id,
123+
wh_bootstrap_addrs,
124+
wh_listen_addrs,
125+
)?;
84126
Ok(())
85127
}

0 commit comments

Comments
 (0)