From b0c28a1cf03a36ca848290c1eebb0af844edfac3 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sun, 16 Jul 2023 20:32:04 -0400 Subject: [PATCH] Move message_queue over to deduplication via intents Due to each service having multiple distinct clocks, we can't expect a stable ordering except the ordering an intact message-queue provides. The messages emitted should be consistent however, solely with unknown order, which is why we can craft intents based on their contents (already implemented by processor-messages). --- Cargo.lock | 1 + message-queue/Cargo.toml | 1 + message-queue/src/main.rs | 4 ++-- message-queue/src/messages.rs | 11 ++++++++--- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fc0782b7..d3a545b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8689,6 +8689,7 @@ dependencies = [ name = "serai-message-queue" version = "0.1.0" dependencies = [ + "bincode", "ciphersuite", "flexible-transcript", "hex", diff --git a/message-queue/Cargo.toml b/message-queue/Cargo.toml index cadefb37..9f366887 100644 --- a/message-queue/Cargo.toml +++ b/message-queue/Cargo.toml @@ -20,6 +20,7 @@ serde = { version = "1", features = ["derive"] } # Encoders hex = "0.4" +bincode = "1" serde_json = "1" # Cryptography diff --git a/message-queue/src/main.rs b/message-queue/src/main.rs index a94fc5d2..aa059de5 100644 --- a/message-queue/src/main.rs +++ b/message-queue/src/main.rs @@ -27,13 +27,13 @@ lazy_static::lazy_static! { fn queue_message(meta: Metadata, msg: Vec, sig: SchnorrSignature) { { let from = (*KEYS).read().unwrap()[&meta.from]; - assert!(sig.verify(from, message_challenge(from, &msg, sig.R))); + assert!(sig.verify(from, message_challenge(from, meta.to, &meta.intent, &msg, sig.R))); } // Assert one, and only one of these, is the coordinator assert!(matches!(meta.from, Service::Coordinator) ^ matches!(meta.to, Service::Coordinator)); - // TODO: Verify the from_id hasn't been prior seen + // TODO: Verify the intent hasn't been prior seen // Queue it (*QUEUES).read().unwrap()[&meta.to].write().unwrap().queue_message(QueuedMessage { diff --git a/message-queue/src/messages.rs b/message-queue/src/messages.rs index 87bb54c1..ce26aed8 100644 --- a/message-queue/src/messages.rs +++ b/message-queue/src/messages.rs @@ -18,21 +18,26 @@ pub struct QueuedMessage { pub sig: Vec, } -#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)] +#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] pub struct Metadata { pub from: Service, pub to: Service, - pub from_id: u64, + pub intent: Vec, } pub fn message_challenge( from: ::G, + to: Service, + intent: &[u8], msg: &[u8], nonce: ::G, ) -> ::F { let mut transcript = RecommendedTranscript::new(b"Serai Message Queue v0.1"); - transcript.domain_separate(b"message"); + transcript.domain_separate(b"metadata"); transcript.append_message(b"from", from.to_bytes()); + transcript.append_message(b"to", bincode::serialize(&to).unwrap()); + transcript.append_message(b"intent", intent); + transcript.domain_separate(b"message"); transcript.append_message(b"msg", msg); transcript.domain_separate(b"signature"); transcript.append_message(b"nonce", nonce.to_bytes());