Stub binaries' code when features binaries is not set

Allows running `cargo build` in monero-serai and message-queue without
erroring, since it'd automatically try to build the binaries which require
additional features.

While we could make those features not optional, it'd increase time to build
and disk space required, which is why the features exist for monero-serai and
message-queue in the first place (since both are frequently used as libs).
This commit is contained in:
Luke Parker 2023-08-02 14:43:49 -04:00
parent 38ad1d4bc4
commit 376b36974f
No known key found for this signature in database
3 changed files with 323 additions and 297 deletions

1
Cargo.lock generated
View file

@ -2439,6 +2439,7 @@ name = "ff-group-tests"
version = "0.13.0"
dependencies = [
"bls12_381",
"ff",
"group",
"k256",
"p256",

View file

@ -1,16 +1,18 @@
use std::sync::Arc;
#[cfg(feature = "binaries")]
mod binaries {
pub(crate) use std::sync::Arc;
use curve25519_dalek::{
pub(crate) use curve25519_dalek::{
scalar::Scalar,
edwards::{CompressedEdwardsY, EdwardsPoint},
};
use multiexp::BatchVerifier;
pub(crate) use multiexp::BatchVerifier;
use serde::Deserialize;
use serde_json::json;
pub(crate) use serde::Deserialize;
pub(crate) use serde_json::json;
use monero_serai::{
pub(crate) use monero_serai::{
Commitment,
ringct::RctPrunable,
transaction::{Input, Transaction},
@ -18,9 +20,9 @@ use monero_serai::{
rpc::{RpcError, Rpc, HttpRpc},
};
use tokio::task::JoinHandle;
pub(crate) use tokio::task::JoinHandle;
async fn check_block(rpc: Arc<Rpc<HttpRpc>>, block_i: usize) {
pub(crate) async fn check_block(rpc: Arc<Rpc<HttpRpc>>, block_i: usize) {
let hash = loop {
match rpc.get_block_hash(block_i).await {
Ok(hash) => break hash,
@ -237,9 +239,13 @@ async fn check_block(rpc: Arc<Rpc<HttpRpc>>, block_i: usize) {
println!("Deserialized, hashed, and reserialized {block_i} with {} TXs", txs_len);
}
}
#[cfg(feature = "binaries")]
#[tokio::main]
async fn main() {
use binaries::*;
let args = std::env::args().collect::<Vec<String>>();
// Read start block as the first arg
@ -307,3 +313,8 @@ async fn main() {
}
}
}
#[cfg(not(feature = "binaries"))]
fn main() {
panic!("To run binaries, please build with `--feature binaries`.");
}

View file

@ -1,27 +1,32 @@
use std::{
#[cfg(feature = "binaries")]
mod messages;
#[cfg(feature = "binaries")]
mod queue;
#[cfg(feature = "binaries")]
mod binaries {
pub(crate) use std::{
sync::{Arc, RwLock},
collections::HashMap,
};
use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto};
use schnorr_signatures::SchnorrSignature;
pub(crate) use ciphersuite::{group::GroupEncoding, Ciphersuite, Ristretto};
pub(crate) use schnorr_signatures::SchnorrSignature;
use serai_primitives::NetworkId;
pub(crate) use serai_primitives::NetworkId;
use jsonrpsee::{RpcModule, server::ServerBuilder};
pub(crate) use jsonrpsee::{RpcModule, server::ServerBuilder};
mod messages;
use messages::*;
pub(crate) use crate::messages::*;
mod queue;
use queue::Queue;
pub(crate) use crate::queue::Queue;
type Db = serai_db::RocksDB;
pub(crate) type Db = serai_db::RocksDB;
lazy_static::lazy_static! {
static ref KEYS: Arc<RwLock<HashMap<Service, <Ristretto as Ciphersuite>::G>>> =
pub(crate) static ref KEYS: Arc<RwLock<HashMap<Service, <Ristretto as Ciphersuite>::G>>> =
Arc::new(RwLock::new(HashMap::new()));
static ref QUEUES: Arc<RwLock<HashMap<Service, RwLock<Queue<Db>>>>> =
pub(crate) static ref QUEUES: Arc<RwLock<HashMap<Service, RwLock<Queue<Db>>>>> =
Arc::new(RwLock::new(HashMap::new()));
}
@ -33,13 +38,13 @@ lazy_static::lazy_static! {
independently verify signatures.
The metadata specifies an intent. Only one message, for a specified intent, will be delivered.
This allows services to safely send messages multiple times without them being delivered multiple
times.
This allows services to safely send messages multiple times without them being delivered
multiple times.
The message will be ordered by this service, with the order having no guarantees other than
successful ordering by the time this call returns.
*/
fn queue_message(meta: Metadata, msg: Vec<u8>, sig: SchnorrSignature<Ristretto>) {
pub(crate) fn queue_message(meta: Metadata, msg: Vec<u8>, sig: SchnorrSignature<Ristretto>) {
{
let from = (*KEYS).read().unwrap()[&meta.from];
assert!(
@ -75,7 +80,7 @@ fn queue_message(meta: Metadata, msg: Vec<u8>, sig: SchnorrSignature<Ristretto>)
The expected index is used to ensure a service didn't fall out of sync with this service. It
should always be either the next message's ID or *TODO*.
*/
fn get_next_message(service: Service, _expected: u64) -> Option<QueuedMessage> {
pub(crate) fn get_next_message(service: Service, _expected: u64) -> Option<QueuedMessage> {
// TODO: Verify the expected next message ID matches
let queue_outer = (*QUEUES).read().unwrap();
@ -89,7 +94,7 @@ fn get_next_message(service: Service, _expected: u64) -> Option<QueuedMessage> {
Acknowledges a message as received and handled, meaning it'll no longer be returned as the next
message.
*/
fn ack_message(service: Service, id: u64, sig: SchnorrSignature<Ristretto>) {
pub(crate) fn ack_message(service: Service, id: u64, sig: SchnorrSignature<Ristretto>) {
{
let from = (*KEYS).read().unwrap()[&service];
assert!(sig.verify(from, ack_challenge(service, from, id, sig.R)));
@ -106,9 +111,13 @@ fn ack_message(service: Service, id: u64, sig: SchnorrSignature<Ristretto>) {
(*QUEUES).read().unwrap()[&service].write().unwrap().ack_message(id)
}
}
#[cfg(feature = "binaries")]
#[tokio::main]
async fn main() {
use binaries::*;
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", serai_env::var("RUST_LOG").unwrap_or_else(|| "info".to_string()));
}
@ -192,3 +201,8 @@ async fn main() {
// Run until stopped, which it never will
server.start(module).unwrap().stopped().await;
}
#[cfg(not(feature = "binaries"))]
fn main() {
panic!("To run binaries, please build with `--feature binaries`.");
}