mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-08 12:19:24 +00:00
Add a Docker-based test for the message-queue service
This commit is contained in:
@@ -57,7 +57,7 @@ impl MessageQueue {
|
||||
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,
|
||||
jsonrpc: &'static str,
|
||||
method: &'static str,
|
||||
params: serde_json::Value,
|
||||
id: u64,
|
||||
@@ -65,21 +65,29 @@ impl MessageQueue {
|
||||
|
||||
let res = loop {
|
||||
// Make the request
|
||||
if let Ok(req) = self
|
||||
match self
|
||||
.client
|
||||
.post(&self.url)
|
||||
.json(&JsonRpcRequest { version: "2.0", method, params: params.clone(), id: 0 })
|
||||
.json(&JsonRpcRequest { jsonrpc: "2.0", method, params: params.clone(), id: 0 })
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
// Get the response
|
||||
if let Ok(res) = req.text().await {
|
||||
break res;
|
||||
Ok(req) => {
|
||||
// Get the response
|
||||
match req.text().await {
|
||||
Ok(res) => break res,
|
||||
Err(e) => {
|
||||
dbg!(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
dbg!(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Sleep 5s before trying again
|
||||
tokio::time::sleep(core::time::Duration::from_secs(5)).await;
|
||||
// Sleep for a second before trying again
|
||||
tokio::time::sleep(core::time::Duration::from_secs(1)).await;
|
||||
};
|
||||
|
||||
let json =
|
||||
@@ -161,7 +169,7 @@ impl MessageQueue {
|
||||
)
|
||||
.serialize();
|
||||
|
||||
let json = self.json_call("ack", serde_json::json!([id, sig])).await;
|
||||
let json = self.json_call("ack", serde_json::json!([self.service, id, sig])).await;
|
||||
if json.get("result") != Some(&serde_json::Value::Bool(true)) {
|
||||
panic!("failed to ack message {id}: {json}");
|
||||
}
|
||||
|
||||
@@ -53,13 +53,15 @@ fn queue_message(meta: Metadata, msg: Vec<u8>, sig: SchnorrSignature<Ristretto>)
|
||||
// TODO: Verify (from, intent) hasn't been prior seen
|
||||
|
||||
// Queue it
|
||||
(*QUEUES).read().unwrap()[&meta.to].write().unwrap().queue_message(QueuedMessage {
|
||||
let id = (*QUEUES).read().unwrap()[&meta.to].write().unwrap().queue_message(QueuedMessage {
|
||||
from: meta.from,
|
||||
// Temporary value which queue_message will override
|
||||
id: u64::MAX,
|
||||
msg,
|
||||
sig: sig.serialize(),
|
||||
});
|
||||
|
||||
log::info!("Queued message from {:?}. It is {:?} {id}", meta.from, meta.to);
|
||||
}
|
||||
|
||||
// next RPC method
|
||||
@@ -100,11 +102,20 @@ fn ack_message(service: Service, id: u64, sig: SchnorrSignature<Ristretto>) {
|
||||
// It's the second if we acknowledge messages before saving them as acknowledged
|
||||
// TODO: Check only a proper message is being acked
|
||||
|
||||
log::info!("{:?} is acknowledging {}", service, id);
|
||||
|
||||
(*QUEUES).read().unwrap()[&service].write().unwrap().ack_message(id)
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
if std::env::var("RUST_LOG").is_err() {
|
||||
std::env::set_var("RUST_LOG", "info");
|
||||
}
|
||||
env_logger::init();
|
||||
|
||||
log::info!("Starting message-queue service...");
|
||||
|
||||
// Open the DB
|
||||
let db = Arc::new(
|
||||
rocksdb::TransactionDB::open_default(
|
||||
@@ -160,14 +171,13 @@ async fn main() {
|
||||
args.1,
|
||||
SchnorrSignature::<Ristretto>::read(&mut args.2.as_slice()).unwrap(),
|
||||
);
|
||||
Ok(())
|
||||
Ok(true)
|
||||
})
|
||||
.unwrap();
|
||||
module
|
||||
.register_method("next", |args, _| {
|
||||
let args = args.parse::<(Service, u64)>().unwrap();
|
||||
get_next_message(args.0, args.1);
|
||||
Ok(())
|
||||
Ok(get_next_message(args.0, args.1))
|
||||
})
|
||||
.unwrap();
|
||||
module
|
||||
@@ -178,7 +188,7 @@ async fn main() {
|
||||
args.1,
|
||||
SchnorrSignature::<Ristretto>::read(&mut args.2.as_slice()).unwrap(),
|
||||
);
|
||||
Ok(())
|
||||
Ok(true)
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ impl<D: Db> Queue<D> {
|
||||
fn message_key(&self, id: u64) -> Vec<u8> {
|
||||
Self::key(b"message", serde_json::to_vec(&(self.1, id)).unwrap())
|
||||
}
|
||||
pub(crate) fn queue_message(&mut self, mut msg: QueuedMessage) {
|
||||
pub(crate) fn queue_message(&mut self, mut msg: QueuedMessage) -> u64 {
|
||||
let id = self.message_count();
|
||||
msg.id = id;
|
||||
let msg_key = self.message_key(id);
|
||||
@@ -43,6 +43,8 @@ impl<D: Db> Queue<D> {
|
||||
txn.put(msg_key, serde_json::to_vec(&msg).unwrap());
|
||||
txn.put(msg_count_key, (id + 1).to_le_bytes());
|
||||
txn.commit();
|
||||
|
||||
id
|
||||
}
|
||||
|
||||
pub(crate) fn get_message(&self, id: u64) -> Option<QueuedMessage> {
|
||||
|
||||
Reference in New Issue
Block a user