mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 04:09:23 +00:00
Add a message-queue connection to processor
Still needs love, yet should get us closer to starting testing.
This commit is contained in:
@@ -32,7 +32,8 @@ bincode = "1"
|
||||
serde_json = "1"
|
||||
|
||||
# Cryptography
|
||||
group = "0.13"
|
||||
ciphersuite = { path = "../crypto/ciphersuite", features = ["ristretto"] }
|
||||
schnorr = { package = "schnorr-signatures", path = "../crypto/schnorr" }
|
||||
|
||||
transcript = { package = "flexible-transcript", path = "../crypto/transcript" }
|
||||
frost = { package = "modular-frost", path = "../crypto/frost", features = ["ristretto"] }
|
||||
@@ -60,7 +61,7 @@ serai-client = { path = "../substrate/client", default-features = false }
|
||||
|
||||
messages = { package = "serai-processor-messages", path = "./messages" }
|
||||
|
||||
jsonrpsee = { version = "0.16", features = ["client"] }
|
||||
reqwest = "0.11"
|
||||
message-queue = { package = "serai-message-queue", path = "../message-queue" }
|
||||
|
||||
[dev-dependencies]
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::{time::Duration, io, collections::HashMap};
|
||||
use async_trait::async_trait;
|
||||
|
||||
use transcript::RecommendedTranscript;
|
||||
use group::ff::PrimeField;
|
||||
use ciphersuite::group::ff::PrimeField;
|
||||
use k256::{ProjectivePoint, Scalar};
|
||||
use frost::{
|
||||
curve::{Curve, Secp256k1},
|
||||
|
||||
@@ -6,7 +6,7 @@ use zeroize::Zeroizing;
|
||||
|
||||
use transcript::RecommendedTranscript;
|
||||
|
||||
use group::{ff::Field, Group};
|
||||
use ciphersuite::group::{ff::Field, Group};
|
||||
use dalek_ff_group::{Scalar, EdwardsPoint};
|
||||
use frost::{curve::Ed25519, ThresholdKeys};
|
||||
|
||||
|
||||
@@ -1,10 +1,23 @@
|
||||
use core::ops::Deref;
|
||||
use std::{
|
||||
sync::{Arc, RwLock},
|
||||
collections::VecDeque,
|
||||
};
|
||||
|
||||
use zeroize::Zeroizing;
|
||||
use rand_core::OsRng;
|
||||
|
||||
use ciphersuite::{group::ff::Field, Ciphersuite, Ristretto};
|
||||
use schnorr::SchnorrSignature;
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
use messages::{ProcessorMessage, CoordinatorMessage};
|
||||
|
||||
use serai_client::primitives::NetworkId;
|
||||
use message_queue::{Service, Metadata, QueuedMessage, message_challenge};
|
||||
use reqwest::Client;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
pub struct Message {
|
||||
pub id: u64,
|
||||
@@ -18,6 +31,147 @@ pub trait Coordinator {
|
||||
async fn ack(&mut self, msg: Message);
|
||||
}
|
||||
|
||||
pub struct MessageQueue {
|
||||
network: NetworkId,
|
||||
priv_key: Zeroizing<<Ristretto as Ciphersuite>::F>,
|
||||
pub_key: <Ristretto as Ciphersuite>::G,
|
||||
client: Client,
|
||||
message_queue_url: String,
|
||||
}
|
||||
|
||||
impl MessageQueue {
|
||||
pub fn new(
|
||||
message_queue_url: String,
|
||||
network: NetworkId,
|
||||
priv_key: Zeroizing<<Ristretto as Ciphersuite>::F>,
|
||||
) -> MessageQueue {
|
||||
MessageQueue {
|
||||
network,
|
||||
pub_key: Ristretto::generator() * priv_key.deref(),
|
||||
priv_key,
|
||||
client: Client::new(),
|
||||
message_queue_url,
|
||||
}
|
||||
}
|
||||
|
||||
async fn json_call(&self, method: &'static str, params: serde_json::Value) -> serde_json::Value {
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
|
||||
struct JsonRpcRequest {
|
||||
version: &'static str,
|
||||
method: &'static str,
|
||||
params: serde_json::Value,
|
||||
id: u64,
|
||||
}
|
||||
|
||||
let res = loop {
|
||||
// Make the request
|
||||
if let Ok(req) = self
|
||||
.client
|
||||
.post(&self.message_queue_url)
|
||||
.json(&JsonRpcRequest { version: "2.0", method, params: params.clone(), id: 0 })
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
// Get the response
|
||||
if let Ok(res) = req.text().await {
|
||||
break res;
|
||||
}
|
||||
}
|
||||
|
||||
// Sleep 5s before trying again
|
||||
tokio::time::sleep(core::time::Duration::from_secs(5)).await;
|
||||
};
|
||||
|
||||
let json =
|
||||
serde_json::from_str::<serde_json::Value>(&res).expect("message-queue returned invalid JSON");
|
||||
if json.get("result").is_none() {
|
||||
panic!("call failed: {json}");
|
||||
}
|
||||
json
|
||||
}
|
||||
|
||||
async fn queue(&self, metadata: Metadata, msg: Vec<u8>, sig: Vec<u8>) {
|
||||
let json = self.json_call("queue", serde_json::json!([metadata, msg, sig])).await;
|
||||
if json.get("result") != Some(&serde_json::Value::Bool(true)) {
|
||||
panic!("failed to queue message: {json}");
|
||||
}
|
||||
}
|
||||
|
||||
async fn next(&self) -> Message {
|
||||
loop {
|
||||
// TODO: Use a proper expected next ID
|
||||
let json =
|
||||
self.json_call("next", serde_json::json!([Service::Processor(self.network), 0])).await;
|
||||
|
||||
// Convert from a Value to a type via reserialization
|
||||
let msg: Option<QueuedMessage> = serde_json::from_str(
|
||||
&serde_json::to_string(
|
||||
&json.get("result").expect("successful JSON RPC call didn't have result"),
|
||||
)
|
||||
.unwrap(),
|
||||
)
|
||||
.expect("next didn't return an Option<QueuedMessage>");
|
||||
|
||||
// If there wasn't a message, check again in 5s
|
||||
let Some(msg) = msg else {
|
||||
tokio::time::sleep(core::time::Duration::from_secs(5)).await;
|
||||
continue;
|
||||
};
|
||||
|
||||
// Verify the message
|
||||
assert_eq!(msg.from, Service::Coordinator, "non-coordinator sent us message");
|
||||
// TODO: Verify the coordinator's signature
|
||||
// TODO: Check the ID is sane
|
||||
let id = msg.id;
|
||||
|
||||
// Deserialize it into a CoordinatorMessage
|
||||
let msg: CoordinatorMessage = serde_json::from_str(
|
||||
&String::from_utf8(msg.msg).expect("msg wasn't valid UTF-8 (not JSON?)"),
|
||||
)
|
||||
.expect("message wasn't a JSON-encoded CoordinatorMessage");
|
||||
return Message { id, msg };
|
||||
}
|
||||
}
|
||||
|
||||
async fn ack(&self, id: u64, sig: Vec<u8>) {
|
||||
let json = self.json_call("ack", serde_json::json!([id, sig])).await;
|
||||
if json.get("result") != Some(&serde_json::Value::Bool(true)) {
|
||||
panic!("failed to ack message {id}: {json}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Coordinator for MessageQueue {
|
||||
async fn send(&mut self, msg: ProcessorMessage) {
|
||||
let metadata = Metadata {
|
||||
from: Service::Processor(self.network),
|
||||
to: Service::Coordinator,
|
||||
intent: msg.intent(),
|
||||
};
|
||||
let msg = serde_json::to_string(&msg).unwrap();
|
||||
|
||||
// TODO: Should this use OsRng? Deterministic or deterministic + random may be better.
|
||||
let nonce = Zeroizing::new(<Ristretto as Ciphersuite>::F::random(&mut OsRng));
|
||||
let nonce_pub = Ristretto::generator() * nonce.deref();
|
||||
let sig = SchnorrSignature::<Ristretto>::sign(
|
||||
&self.priv_key,
|
||||
nonce,
|
||||
message_challenge(self.pub_key, metadata.to, &metadata.intent, msg.as_bytes(), nonce_pub),
|
||||
);
|
||||
self.queue(metadata, msg.into_bytes(), sig.serialize()).await;
|
||||
}
|
||||
|
||||
async fn recv(&mut self) -> Message {
|
||||
self.next().await
|
||||
}
|
||||
|
||||
async fn ack(&mut self, msg: Message) {
|
||||
// TODO: Use a proper signature once message-queue checks ack signatures
|
||||
MessageQueue::ack(self, msg.id, vec![0; 64]).await
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Move this to tests
|
||||
pub struct MemCoordinator(Arc<RwLock<VecDeque<Message>>>);
|
||||
impl MemCoordinator {
|
||||
|
||||
@@ -7,7 +7,7 @@ use rand_core::SeedableRng;
|
||||
use rand_chacha::ChaCha20Rng;
|
||||
|
||||
use transcript::{Transcript, RecommendedTranscript};
|
||||
use group::GroupEncoding;
|
||||
use ciphersuite::group::GroupEncoding;
|
||||
use frost::{
|
||||
curve::{Ciphersuite, Ristretto},
|
||||
dkg::{Participant, ThresholdParams, ThresholdCore, ThresholdKeys, encryption::*, frost::*},
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::{
|
||||
use zeroize::{Zeroize, Zeroizing};
|
||||
|
||||
use transcript::{Transcript, RecommendedTranscript};
|
||||
use group::GroupEncoding;
|
||||
use ciphersuite::group::GroupEncoding;
|
||||
use frost::{curve::Ciphersuite, ThresholdKeys};
|
||||
|
||||
use log::{info, warn, error};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::io;
|
||||
|
||||
use transcript::{Transcript, RecommendedTranscript};
|
||||
use group::GroupEncoding;
|
||||
use ciphersuite::group::GroupEncoding;
|
||||
use frost::curve::Ciphersuite;
|
||||
|
||||
use crate::coins::{Output, Coin};
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::{
|
||||
collections::{HashSet, HashMap},
|
||||
};
|
||||
|
||||
use group::GroupEncoding;
|
||||
use ciphersuite::group::GroupEncoding;
|
||||
use frost::curve::Ciphersuite;
|
||||
|
||||
use log::{info, debug, warn};
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::collections::{VecDeque, HashMap};
|
||||
|
||||
use rand_core::OsRng;
|
||||
|
||||
use group::GroupEncoding;
|
||||
use ciphersuite::group::GroupEncoding;
|
||||
use frost::{
|
||||
ThresholdKeys,
|
||||
sign::{Writable, PreprocessMachine, SignMachine, SignatureMachine},
|
||||
|
||||
@@ -5,7 +5,7 @@ use rand_core::OsRng;
|
||||
|
||||
use scale::Encode;
|
||||
|
||||
use group::GroupEncoding;
|
||||
use ciphersuite::group::GroupEncoding;
|
||||
use frost::{
|
||||
curve::Ristretto,
|
||||
ThresholdKeys,
|
||||
|
||||
@@ -4,7 +4,7 @@ use zeroize::Zeroizing;
|
||||
|
||||
use rand_core::{RngCore, OsRng};
|
||||
|
||||
use group::GroupEncoding;
|
||||
use ciphersuite::group::GroupEncoding;
|
||||
use frost::{Participant, ThresholdParams, tests::clone_without};
|
||||
|
||||
use serai_db::{DbTxn, Db, MemDb};
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::collections::HashMap;
|
||||
|
||||
use rand_core::{RngCore, OsRng};
|
||||
|
||||
use group::GroupEncoding;
|
||||
use ciphersuite::group::GroupEncoding;
|
||||
use frost::{
|
||||
Participant, ThresholdKeys,
|
||||
dkg::tests::{key_gen, clone_without},
|
||||
|
||||
@@ -2,7 +2,7 @@ use std::collections::HashMap;
|
||||
|
||||
use rand_core::{RngCore, OsRng};
|
||||
|
||||
use group::GroupEncoding;
|
||||
use ciphersuite::group::GroupEncoding;
|
||||
use frost::{
|
||||
curve::Ristretto,
|
||||
Participant,
|
||||
|
||||
Reference in New Issue
Block a user