Skip to content

Commit 6ee5357

Browse files
committed
ditch hardcoded pairs
1 parent 9dc655d commit 6ee5357

File tree

7 files changed

+316
-70
lines changed

7 files changed

+316
-70
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ target/
44
*.key
55
*.crt
66
*.log
7+
8+
.codegpt

build.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
fn main() -> Result<(), Box<dyn std::error::Error>> {
2-
tonic_build::configure()
3-
.type_attribute(".", "#[derive(Hash, Eq, PartialOrd, Ord)]")
4-
.compile(&["proto/rumi.proto"], &["proto"])?;
2+
tonic_build::compile_protos("proto/rumi.proto")?;
53
Ok(())
64
}

proto/rumi.proto

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
11
syntax = "proto3";
22
package rumi;
33

4+
// The discovery service definition
45
service Discovery {
6+
// Get the set of registered identifiers
57
rpc GetPublicSet (GetPublicSetRequest) returns (GetPublicSetResponse);
8+
9+
// Find a user by their identifier
610
rpc Find (FindRequest) returns (FindResponse);
11+
12+
// Register a new identifier-UUID pair
13+
rpc Register(RegisterRequest) returns (RegisterResponse);
714
}
815

16+
// Request for getting the public set of identifiers
917
message GetPublicSetRequest {}
1018

19+
// Response containing the public set of identifiers
1120
message GetPublicSetResponse {
1221
repeated uint64 identifiers = 1;
1322
}
1423

24+
// Request for finding a user
1525
message FindRequest {
1626
bytes hash_prefix = 1;
1727
bytes blinded_identifier = 2;
1828
string zksm_proof = 3;
1929
}
2030

31+
// A bucket entry in the response
32+
message BucketEntry {
33+
bytes blinded_identifier = 1;
34+
bytes blinded_user_id = 2;
35+
}
36+
37+
// Response for finding a user
2138
message FindResponse {
2239
bytes double_blinded_identifier = 1;
2340
repeated BucketEntry entries = 2;
2441
}
2542

26-
message BucketEntry {
27-
bytes blinded_identifier = 1;
28-
bytes blinded_user_id = 2;
43+
// Request for registering a new identifier-UUID pair
44+
message RegisterRequest {
45+
uint64 identifier = 1;
46+
bytes uuid = 2;
47+
}
48+
49+
// Response for registration
50+
message RegisterResponse {
51+
bool success = 1;
52+
string message = 2;
2953
}

src/bin/client.rs

+78-19
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
use clap::Parser;
22
use console::style;
33
use rumi::Client;
4-
use rumi_proto::discovery_client::DiscoveryClient;
5-
use rumi_proto::{FindRequest, FindResponse, GetPublicSetRequest, GetPublicSetResponse};
4+
use rumi_proto::{
5+
discovery_client::DiscoveryClient,
6+
FindRequest, FindResponse, GetPublicSetRequest, GetPublicSetResponse,
7+
RegisterRequest, RegisterResponse,
8+
};
69
use std::collections::HashMap;
710
use std::error::Error;
811
use std::vec;
912
use tonic::Response;
1013
use tracing::{debug, info, trace, warn, Level};
1114
use tracing_attributes::instrument;
1215
use tracing_subscriber::{fmt, prelude::*};
16+
use uuid::Uuid;
1317

1418
pub mod rumi_proto {
1519
tonic::include_proto!("rumi");
@@ -28,6 +32,12 @@ enum Commands {
2832
#[arg(help = "The identifier to look up")]
2933
identifier: u64,
3034
},
35+
Register {
36+
#[arg(help = "The identifier to register")]
37+
identifier: u64,
38+
#[arg(help = "The UUID to register (optional, will generate if not provided)")]
39+
uuid: Option<String>,
40+
},
3141
}
3242

3343
#[instrument(skip(client, rumi_client), fields(identifier = %identifier), ret)]
@@ -117,27 +127,72 @@ async fn lookup_identifier(
117127
Ok(())
118128
}
119129

130+
#[instrument(skip(client), fields(identifier = %identifier), ret)]
131+
async fn register_identifier(
132+
client: &mut DiscoveryClient<tonic::transport::Channel>,
133+
identifier: u64,
134+
uuid_str: Option<String>,
135+
) -> Result<(), Box<dyn Error>> {
136+
let uuid = if let Some(uuid_str) = uuid_str {
137+
Uuid::parse_str(&uuid_str)?
138+
} else {
139+
Uuid::new_v4()
140+
};
141+
142+
info!(
143+
"Registering identifier {} with UUID {}",
144+
style(identifier).cyan(),
145+
style(uuid).yellow()
146+
);
147+
148+
let request = tonic::Request::new(RegisterRequest {
149+
identifier,
150+
uuid: uuid.as_bytes().to_vec(),
151+
});
152+
153+
match client.register(request).await {
154+
Ok(response) => {
155+
let response = response.into_inner();
156+
if response.success {
157+
info!(
158+
"{} {}",
159+
style("✓").green().bold(),
160+
style(&response.message).green()
161+
);
162+
} else {
163+
info!(
164+
"{} {}",
165+
style("✗").red().bold(),
166+
style(&response.message).red()
167+
);
168+
}
169+
}
170+
Err(status) => {
171+
warn!(
172+
"{} Registration failed: {}",
173+
style("✗").red().bold(),
174+
style(status).red()
175+
);
176+
}
177+
}
178+
179+
Ok(())
180+
}
181+
120182
#[tokio::main(flavor = "multi_thread")]
121183
async fn main() -> Result<(), Box<dyn Error>> {
122-
// Set up single combined subscriber
123-
let console_layer = console_subscriber::ConsoleLayer::builder()
124-
.with_default_env()
125-
.spawn();
126-
127-
tracing_subscriber::registry()
128-
.with(console_layer)
129-
.with(
130-
fmt::layer()
131-
.with_target(false)
132-
.with_thread_ids(false)
133-
.with_line_number(false)
134-
.with_level(true),
135-
)
184+
// Set up logging based on RUST_LOG env var, defaulting to info level
185+
let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
186+
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"));
187+
188+
tracing_subscriber::fmt()
189+
.with_target(false)
190+
.with_thread_ids(false)
191+
.with_line_number(false)
192+
.with_level(true)
193+
.with_env_filter(env_filter)
136194
.init();
137195

138-
info!("RUMI Client starting up");
139-
info!("Tokio Console available on http://127.0.0.1:6669");
140-
141196
let cli = Cli::parse();
142197

143198
match cli.command {
@@ -146,6 +201,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
146201
let rumi_client = Client::new(rand::thread_rng());
147202
lookup_identifier(&mut client, &rumi_client, identifier).await?;
148203
}
204+
Commands::Register { identifier, uuid } => {
205+
let mut client = DiscoveryClient::connect("http://[::1]:50051").await?;
206+
register_identifier(&mut client, identifier, uuid).await?;
207+
}
149208
}
150209

151210
Ok(())

src/bin/server.rs

+63-45
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use crate::rumi_proto::{GetPublicSetRequest, GetPublicSetResponse};
1+
use crate::rumi_proto::{
2+
discovery_server::{Discovery, DiscoveryServer},
3+
FindRequest, FindResponse, GetPublicSetRequest, GetPublicSetResponse,
4+
RegisterRequest, RegisterResponse,
5+
};
26
use console::style;
37
use lazy_static::lazy_static;
48
use p256::EncodedPoint;
@@ -22,11 +26,6 @@ pub mod rumi_proto {
2226
tonic::include_proto!("rumi");
2327
}
2428

25-
use rumi_proto::{
26-
discovery_server::{Discovery, DiscoveryServer},
27-
FindRequest, FindResponse,
28-
};
29-
3029
// Define metrics
3130
lazy_static! {
3231
static ref REGISTRY: Registry = Registry::new();
@@ -63,16 +62,11 @@ pub struct DiscoveryService {
6362
impl DiscoveryService {
6463
pub fn new() -> Self {
6564
let mut rng = rand::thread_rng();
66-
let mut users = HashMap::new();
67-
68-
for i in 0..100 {
69-
users.insert(1_000_000_000 + i, Uuid::new_v4());
70-
}
65+
let users = HashMap::new(); // Start with empty user set
7166

7267
let server = Server::new(&mut rng, &users);
7368
debug!(
74-
"Server initialized with identifiers: {:?}",
75-
server.get_public_set()
69+
"Server initialized with empty user set",
7670
);
7771

7872
Self {
@@ -168,10 +162,62 @@ impl Discovery for DiscoveryService {
168162
timer.observe_duration();
169163
result
170164
}
165+
166+
#[instrument(skip(self, request), name = "register", ret)]
167+
async fn register(
168+
&self,
169+
request: Request<RegisterRequest>,
170+
) -> Result<Response<RegisterResponse>, Status> {
171+
let timer = REQUEST_DURATION
172+
.with_label_values(&["register"])
173+
.start_timer();
174+
REQUEST_COUNTER.with_label_values(&["register"]).inc();
175+
176+
let result = {
177+
let request_inner = request.into_inner();
178+
let identifier = request_inner.identifier;
179+
let uuid_bytes = request_inner.uuid;
180+
181+
let uuid = Uuid::from_slice(&uuid_bytes)
182+
.map_err(|_| Status::invalid_argument("Invalid UUID format"))?;
183+
184+
let mut rng = rand::thread_rng();
185+
let mut server = self
186+
.server
187+
.lock()
188+
.map_err(|_| Status::internal("Server lock poisoned"))?;
189+
190+
match server.register(identifier, &uuid, &mut rng) {
191+
Ok(()) => Ok(Response::new(RegisterResponse {
192+
success: true,
193+
message: format!("Successfully registered identifier {}", identifier),
194+
})),
195+
Err(e) => Ok(Response::new(RegisterResponse {
196+
success: false,
197+
message: e.to_string(),
198+
})),
199+
}
200+
};
201+
202+
timer.observe_duration();
203+
result
204+
}
171205
}
172206

173207
#[tokio::main(flavor = "multi_thread")]
174208
async fn main() -> Result<(), Box<dyn std::error::Error>> {
209+
// Set up logging based on RUST_LOG env var, defaulting to info level
210+
let env_filter = tracing_subscriber::EnvFilter::try_from_default_env()
211+
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info"));
212+
213+
tracing_subscriber::fmt()
214+
.with_target(false)
215+
.with_thread_ids(false)
216+
.with_line_number(false)
217+
.with_level(true)
218+
.with_env_filter(env_filter)
219+
.init();
220+
175221
let addr = "[::1]:50051".parse()?;
176222
let service = DiscoveryService::new();
177223

@@ -184,6 +230,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
184230
.unwrap();
185231
REGISTRY.register(Box::new(MEMORY_GAUGE.clone())).unwrap();
186232

233+
info!("RUMI Server starting up on {}", style(addr).cyan());
234+
187235
// Start metrics pushing in background
188236
tokio::spawn(async {
189237
let client = reqwest::Client::new();
@@ -217,41 +265,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
217265
warn!("Failed to push metrics: {}", e);
218266
}
219267
}
220-
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
268+
269+
tokio::time::sleep(tokio::time::Duration::from_secs(15)).await;
221270
}
222271
});
223272

224-
// Set up console and tracing
225-
let console_layer = console_subscriber::ConsoleLayer::builder()
226-
.with_default_env()
227-
.spawn();
228-
229-
tracing_subscriber::registry()
230-
.with(console_layer)
231-
.with(
232-
fmt::layer()
233-
.with_target(false)
234-
.with_thread_ids(true)
235-
.with_file(true)
236-
.with_line_number(true)
237-
.with_level(true),
238-
)
239-
.init();
240-
241-
info!("{}", style("RUMI Discovery Server").green().bold());
242-
info!("Listening on {}", style(addr).cyan());
243-
info!(
244-
"Initialized with {} identifiers",
245-
style(service.server.lock().unwrap().get_public_set().len()).yellow()
246-
);
247-
info!("Tokio Console available on http://127.0.0.1:6669");
248-
info!("Metrics being pushed to Prometheus");
249-
250-
debug!(
251-
"Public set: {:?}",
252-
service.server.lock().unwrap().get_public_set()
253-
);
254-
255273
TonicServer::builder()
256274
.add_service(DiscoveryServer::new(service))
257275
.serve(addr)

0 commit comments

Comments
 (0)