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).
This commit is contained in:
Luke Parker 2023-07-16 20:32:04 -04:00
parent 23b9d57305
commit b0c28a1cf0
No known key found for this signature in database
4 changed files with 12 additions and 5 deletions

1
Cargo.lock generated
View file

@ -8689,6 +8689,7 @@ dependencies = [
name = "serai-message-queue" name = "serai-message-queue"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bincode",
"ciphersuite", "ciphersuite",
"flexible-transcript", "flexible-transcript",
"hex", "hex",

View file

@ -20,6 +20,7 @@ serde = { version = "1", features = ["derive"] }
# Encoders # Encoders
hex = "0.4" hex = "0.4"
bincode = "1"
serde_json = "1" serde_json = "1"
# Cryptography # Cryptography

View file

@ -27,13 +27,13 @@ lazy_static::lazy_static! {
fn queue_message(meta: Metadata, msg: Vec<u8>, sig: SchnorrSignature<Ristretto>) { fn queue_message(meta: Metadata, msg: Vec<u8>, sig: SchnorrSignature<Ristretto>) {
{ {
let from = (*KEYS).read().unwrap()[&meta.from]; 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 one, and only one of these, is the coordinator
assert!(matches!(meta.from, Service::Coordinator) ^ matches!(meta.to, Service::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 // Queue it
(*QUEUES).read().unwrap()[&meta.to].write().unwrap().queue_message(QueuedMessage { (*QUEUES).read().unwrap()[&meta.to].write().unwrap().queue_message(QueuedMessage {

View file

@ -18,21 +18,26 @@ pub struct QueuedMessage {
pub sig: Vec<u8>, pub sig: Vec<u8>,
} }
#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)] #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
pub struct Metadata { pub struct Metadata {
pub from: Service, pub from: Service,
pub to: Service, pub to: Service,
pub from_id: u64, pub intent: Vec<u8>,
} }
pub fn message_challenge( pub fn message_challenge(
from: <Ristretto as Ciphersuite>::G, from: <Ristretto as Ciphersuite>::G,
to: Service,
intent: &[u8],
msg: &[u8], msg: &[u8],
nonce: <Ristretto as Ciphersuite>::G, nonce: <Ristretto as Ciphersuite>::G,
) -> <Ristretto as Ciphersuite>::F { ) -> <Ristretto as Ciphersuite>::F {
let mut transcript = RecommendedTranscript::new(b"Serai Message Queue v0.1"); 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"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.append_message(b"msg", msg);
transcript.domain_separate(b"signature"); transcript.domain_separate(b"signature");
transcript.append_message(b"nonce", nonce.to_bytes()); transcript.append_message(b"nonce", nonce.to_bytes());