Skip to content

Commit 2ee4140

Browse files
authored
Merge pull request #791 from h4sh3d/feat/grpc-config-options
Feat: new grpc config options
2 parents b0845d4 + dccf73a commit 2ee4140

13 files changed

Lines changed: 119 additions & 55 deletions

File tree

farcasterd.toml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# Set this to true if you want to enable auto-funding, default to false
77
# if set to true you need to register the parameter for the networks you
88
# want to support: mainnet, testnet, or local
9-
auto_fund = false
9+
enable = false
1010

1111
# Auto-funding testnet parameters
1212
[farcasterd.auto_funding.testnet]
@@ -26,12 +26,16 @@ bitcoin_cookie_path = "~/.bitcoin/testnet3/.cookie"
2626
# the wallet should have spendable funds
2727
monero_rpc_wallet = "http://localhost:38084"
2828

29-
# Define grpc
30-
[farcasterd.grpc]
29+
# Defines grpc options
30+
[grpc]
3131
# Set this to true to enable the grpc daemon
32-
use_grpc = true
33-
# If use_grpc=true, also requires a port for grpc clients to connect to
34-
port = 50051
32+
enable = true
33+
# If enabled, also requires a port for grpc clients to connect to
34+
bind_port = 50051
35+
# If enabled, where to bind the grpc service. Defaults to 127.0.0.1
36+
# The grpc interface allow full management of the node, you probably want to
37+
# keep it only accessible on your local network
38+
bind_ip = "127.0.0.1"
3539

3640
# Syncers configuration
3741
# configures the Bitcoin and Monero syncers for the three

src/bin/grpcd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn main() {
4646
debug!("CTL RPC socket {}", &service_config.ctl_endpoint);
4747

4848
debug!("Starting runtime ...");
49-
grpcd::run(service_config, opts.grpc_port).expect("Error running grpcd runtime");
49+
grpcd::run(service_config, opts.grpc_port, opts.grpc_ip).expect("Error running grpcd runtime");
5050

5151
unreachable!()
5252
}

src/config.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,52 +28,64 @@ pub const FARCASTER_TESTNET_ELECTRUM_SERVER: &str = "ssl://blockstream.info:993"
2828
pub const FARCASTER_TESTNET_MONERO_DAEMON: &str = "http://stagenet.community.rino.io:38081";
2929
pub const FARCASTER_TESTNET_MONERO_RPC_WALLET: &str = "http://localhost:38083";
3030

31+
pub const GRPC_BIND_IP_ADDRESS: &str = "127.0.0.1";
32+
3133
#[derive(Deserialize, Serialize, Debug, Clone)]
3234
#[serde(crate = "serde_crate")]
3335
pub struct Config {
3436
/// Farcasterd configuration
3537
pub farcasterd: Option<FarcasterdConfig>,
38+
/// Sets the grpc server port, if none is given, no grpc server is run
39+
pub grpc: Option<GrpcConfig>,
3640
/// Syncer configuration
3741
pub syncers: Option<SyncersConfig>,
3842
}
3943

4044
impl Config {
41-
/// Returns if auto-funding functionality is enable
45+
/// Returns if auto-funding functionality is enabled
4246
pub fn is_auto_funding_enable(&self) -> bool {
4347
match &self.farcasterd {
4448
Some(FarcasterdConfig {
45-
auto_funding: Some(AutoFundingConfig { auto_fund, .. }),
46-
grpc: _,
47-
}) => *auto_fund,
49+
auto_funding: Some(AutoFundingConfig { enable, .. }),
50+
..
51+
}) => *enable,
4852
_ => false,
4953
}
5054
}
5155

52-
/// Returns if grpc port is set
56+
/// Returns if grpc is enabled
5357
pub fn is_grpc_enable(&self) -> bool {
54-
match &self.farcasterd {
55-
Some(FarcasterdConfig {
56-
auto_funding: _,
57-
grpc: Some(GrpcConfig { use_grpc, .. }),
58-
}) => *use_grpc,
58+
match &self.grpc {
59+
Some(GrpcConfig { enable, .. }) => *enable,
5960
_ => false,
6061
}
6162
}
6263

64+
/// Returns the Grcp bind ip address, if not set return the default value
65+
pub fn grpc_bind_ip(&self) -> String {
66+
match &self.grpc {
67+
Some(GrpcConfig {
68+
bind_ip: Some(bind_ip),
69+
..
70+
}) => bind_ip.clone(),
71+
_ => String::from(GRPC_BIND_IP_ADDRESS),
72+
}
73+
}
74+
6375
/// Returns the auto-funding configuration for a given network if enable, if None no
6476
/// configuration is found
6577
pub fn get_auto_funding_config(&self, network: Network) -> Option<AutoFundingServers> {
6678
match &self.farcasterd {
6779
Some(FarcasterdConfig {
6880
auto_funding:
6981
Some(AutoFundingConfig {
70-
auto_fund,
82+
enable,
7183
mainnet,
7284
testnet,
7385
local,
7486
}),
75-
grpc: _,
76-
}) if *auto_fund => match network {
87+
..
88+
}) if *enable => match network {
7789
Network::Mainnet => mainnet.clone(),
7890
Network::Testnet => testnet.clone(),
7991
Network::Local => local.clone(),
@@ -95,6 +107,7 @@ impl Default for Config {
95107
fn default() -> Self {
96108
Config {
97109
farcasterd: None,
110+
grpc: None,
98111
syncers: Some(SyncersConfig::default()),
99112
}
100113
}
@@ -105,24 +118,24 @@ impl Default for Config {
105118
pub struct FarcasterdConfig {
106119
/// Sets the auto-funding parameters, default to no auto-fund
107120
pub auto_funding: Option<AutoFundingConfig>,
108-
/// Sets the grpc server port, if none is given, no grpc server is run
109-
pub grpc: Option<GrpcConfig>,
110121
}
111122

112123
#[derive(Deserialize, Serialize, Debug, Clone)]
113124
#[serde(crate = "serde_crate")]
114125
pub struct GrpcConfig {
115126
/// Use grpc functionality
116-
pub use_grpc: bool,
127+
pub enable: bool,
117128
/// Grpc port configuration
118-
pub port: u64,
129+
pub bind_port: u16,
130+
/// Grpc listening ip address
131+
pub bind_ip: Option<String>,
119132
}
120133

121134
#[derive(Deserialize, Serialize, Debug, Clone)]
122135
#[serde(crate = "serde_crate")]
123136
pub struct AutoFundingConfig {
124137
/// Use auto-funding functionality
125-
pub auto_fund: bool,
138+
pub enable: bool,
126139
/// Mainnet auto-funding configuration
127140
pub mainnet: Option<AutoFundingServers>,
128141
/// Testnet auto-funding configuration

src/farcasterd/runtime.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,9 @@ pub fn run(
6565
"grpcd",
6666
&[
6767
"--grpc-port",
68-
&config
69-
.farcasterd
70-
.clone()
71-
.unwrap()
72-
.grpc
73-
.unwrap()
74-
.port
75-
.to_string(),
68+
&config.grpc.clone().unwrap().bind_port.to_string(),
69+
"--grpc-ip",
70+
&config.grpc_bind_ip(),
7671
],
7772
)?;
7873
}

src/grpcd/opts.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ pub struct Opts {
99

1010
/// Port number that the grpc server is accepting connections on
1111
#[clap(long)]
12-
pub grpc_port: u64,
12+
pub grpc_port: u16,
13+
14+
/// Ip that the grpc server is accepting connections on
15+
#[clap(long)]
16+
pub grpc_ip: String,
1317
}
1418

1519
impl Opts {

src/grpcd/runtime.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl IdCounter {
150150
}
151151
}
152152

153-
pub fn run(config: ServiceConfig, grpc_port: u64) -> Result<(), Error> {
153+
pub fn run(config: ServiceConfig, grpc_port: u16, grpc_ip: String) -> Result<(), Error> {
154154
let (tx_response, rx_response): (Sender<(u64, BusMsg)>, Receiver<(u64, BusMsg)>) =
155155
std::sync::mpsc::channel();
156156

@@ -159,7 +159,7 @@ pub fn run(config: ServiceConfig, grpc_port: u64) -> Result<(), Error> {
159159
tx_request.connect("inproc://grpcdbridge")?;
160160
rx_request.bind("inproc://grpcdbridge")?;
161161

162-
let mut server = GrpcServer { grpc_port };
162+
let mut server = GrpcServer { grpc_port, grpc_ip };
163163
server.run(rx_response, tx_request)?;
164164

165165
let runtime = Runtime {
@@ -867,7 +867,8 @@ impl Farcaster for FarcasterService {
867867
}
868868

869869
pub struct GrpcServer {
870-
grpc_port: u64,
870+
grpc_port: u16,
871+
grpc_ip: String,
871872
}
872873

873874
fn request_loop(
@@ -939,7 +940,7 @@ impl GrpcServer {
939940
rx_response: Receiver<(u64, BusMsg)>,
940941
tx_request: zmq::Socket,
941942
) -> Result<(), Error> {
942-
let addr = format!("0.0.0.0:{}", self.grpc_port)
943+
let addr = format!("{}:{}", self.grpc_ip, self.grpc_port)
943944
.parse()
944945
.expect("invalid grpc server bind address");
945946
info!("Binding grpc to address: {}", addr);

tests/cfg/config.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ ci:
3535
port: 38884
3636
protocol: "http"
3737

38+
grpc:
39+
fc1:
40+
host: "0.0.0.0"
41+
port: 23432
42+
fc2:
43+
host: "0.0.0.0"
44+
port: 23433
45+
3846
# Top level configuration for (docker-)compose setup, used if no `CI`
3947
# environment variable is found.
4048
compose:
@@ -71,3 +79,11 @@ compose:
7179
host: "localhost"
7280
port: 38884
7381
protocol: "http"
82+
83+
grpc:
84+
fc1:
85+
host: "0.0.0.0"
86+
port: 23432
87+
fc2:
88+
host: "0.0.0.0"
89+
port: 23433

tests/cfg/fc1.ci.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
[farcasterd.grpc]
2-
use_grpc = true
3-
port = 23432
1+
[grpc]
2+
enable = true
3+
bind_port = 23432
4+
bind_ip = "0.0.0.0"
45

56
[syncers.local]
67
electrum_server = "tcp://electrs:60401"

tests/cfg/fc1.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
[farcasterd.grpc]
2-
use_grpc = true
3-
port = 23432
1+
[grpc]
2+
enable = true
3+
bind_port = 23432
4+
bind_ip = "0.0.0.0"
45

56
[syncers.local]
67
electrum_server = "tcp://localhost:60401"

tests/cfg/fc2.ci.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
[farcasterd.grpc]
2-
use_grpc = true
3-
port = 23433
1+
[grpc]
2+
enable = true
3+
bind_port = 23433
4+
bind_ip = "0.0.0.0"
45

56
[syncers.local]
67
electrum_server = "tcp://electrs:60401"

0 commit comments

Comments
 (0)