diff --git a/Cargo.lock b/Cargo.lock index 1447598bd..3c9cebae2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1563,12 +1563,14 @@ dependencies = [ name = "darkfi-sdk" version = "0.4.1" dependencies = [ + "async-trait", "blake2b_simd", "blake3", "bridgetree", "bs58", "chacha20poly1305", "darkfi-serial", + "futures-lite", "halo2_gadgets", "halo2_proofs", "lazy_static", diff --git a/Cargo.toml b/Cargo.toml index 2979edd1e..4813d2a56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -155,7 +155,7 @@ blockchain = [ #"url", #"async-runtime", - "darkfi-sdk", + "darkfi-sdk/async", "darkfi-serial/crypto", "net", "rpc", @@ -177,7 +177,7 @@ event-graph = [ "tinyjson", "async-runtime", - "darkfi-serial", + "darkfi-serial/async", "darkfi-serial/collections", "darkfi-serial/hash", "net", @@ -229,7 +229,7 @@ tx = [ "blake3", "rand", - "darkfi-sdk", + "darkfi-sdk/async", "darkfi-serial", "zk", ] @@ -241,7 +241,7 @@ util = [ "tinyjson", "toml", - "darkfi-serial", + "darkfi-serial/async", ] wallet = [ @@ -258,7 +258,7 @@ wasm-runtime = [ "wasmer-middlewares", "blockchain", - "darkfi-sdk", + "darkfi-sdk/async", "darkfi-serial", "util", "zk", @@ -270,8 +270,8 @@ zk = [ "halo2_gadgets", "rand", - "darkfi-sdk", - "darkfi-serial", + "darkfi-sdk/async", + "darkfi-serial/async", "zkas", ] diff --git a/src/blockchain/block_store.rs b/src/blockchain/block_store.rs index 028ec9e64..5b90955bf 100644 --- a/src/blockchain/block_store.rs +++ b/src/blockchain/block_store.rs @@ -16,12 +16,14 @@ * along with this program. If not, see . */ +use async_trait::async_trait; use darkfi_sdk::{ blockchain::Slot, crypto::schnorr::Signature, pasta::{group::ff::Field, pallas}, }; use darkfi_serial::{deserialize, serialize, SerialDecodable, SerialEncodable}; +use smol::io::{AsyncRead, AsyncWrite}; use crate::{tx::Transaction, Error, Result}; diff --git a/src/blockchain/header_store.rs b/src/blockchain/header_store.rs index 9b019342e..5d7ca7822 100644 --- a/src/blockchain/header_store.rs +++ b/src/blockchain/header_store.rs @@ -16,8 +16,10 @@ * along with this program. If not, see . */ +use async_trait::async_trait; use darkfi_sdk::crypto::{MerkleNode, MerkleTree}; use darkfi_serial::{deserialize, serialize, SerialDecodable, SerialEncodable}; +use smol::io::{AsyncRead, AsyncWrite}; use crate::{util::time::Timestamp, Error, Result}; diff --git a/src/consensus/block.rs b/src/consensus/block.rs index 2d0561acf..dc9355175 100644 --- a/src/consensus/block.rs +++ b/src/consensus/block.rs @@ -18,11 +18,13 @@ use std::fmt; +use async_trait::async_trait; use darkfi_sdk::{ crypto::{MerkleNode, MerkleTree}, pasta::pallas, }; use darkfi_serial::{serialize, SerialDecodable, SerialEncodable}; +use smol::io::{AsyncRead, AsyncWrite}; use super::{ constants::{BLOCK_MAGIC_BYTES, BLOCK_VERSION}, diff --git a/src/consensus/lead_coin.rs b/src/consensus/lead_coin.rs index 4a035562a..56ad816b6 100644 --- a/src/consensus/lead_coin.rs +++ b/src/consensus/lead_coin.rs @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +use async_trait::async_trait; use darkfi_sdk::{ crypto::{ pedersen::{pedersen_commitment_base, pedersen_commitment_u64}, @@ -29,6 +30,7 @@ use darkfi_serial::{SerialDecodable, SerialEncodable}; use halo2_proofs::{arithmetic::Field, circuit::Value}; use log::info; use rand::rngs::OsRng; +use smol::io::{AsyncRead, AsyncWrite}; use super::constants::EPOCH_LENGTH; use crate::{ diff --git a/src/consensus/lead_info.rs b/src/consensus/lead_info.rs index 286d75e41..eb6443396 100644 --- a/src/consensus/lead_info.rs +++ b/src/consensus/lead_info.rs @@ -16,12 +16,14 @@ * along with this program. If not, see . */ +use async_trait::async_trait; use darkfi_sdk::{ crypto::{schnorr::Signature, Keypair, PublicKey}, pasta::pallas, }; use darkfi_serial::{SerialDecodable, SerialEncodable}; use log::error; +use smol::io::{AsyncRead, AsyncWrite}; use crate::{ zk::proof::{Proof, VerifyingKey}, diff --git a/src/consensus/rcpt.rs b/src/consensus/rcpt.rs index 33cd1ebc4..37d002123 100644 --- a/src/consensus/rcpt.rs +++ b/src/consensus/rcpt.rs @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +use async_trait::async_trait; use crypto_api_chachapoly::ChachaPolyIetf; use darkfi_sdk::{ crypto::{ @@ -27,6 +28,7 @@ use darkfi_sdk::{ }; use darkfi_serial::{Decodable, Encodable, SerialDecodable, SerialEncodable}; use rand::rngs::OsRng; +use smol::io::{AsyncRead, AsyncWrite}; use crate::Error; diff --git a/src/consensus/state.rs b/src/consensus/state.rs index 34f6b593b..f186b3365 100644 --- a/src/consensus/state.rs +++ b/src/consensus/state.rs @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +use async_trait::async_trait; use darkfi_sdk::{ blockchain::{PidOutput, PreviousSlot, Slot}, crypto::MerkleTree, @@ -24,6 +25,7 @@ use darkfi_sdk::{ use darkfi_serial::{deserialize, serialize, SerialDecodable, SerialEncodable}; use log::info; use rand::{thread_rng, Rng}; +use smol::io::{AsyncRead, AsyncWrite}; use super::{ constants, diff --git a/src/consensus/stx.rs b/src/consensus/stx.rs index 64b88a4ab..cc4c550df 100644 --- a/src/consensus/stx.rs +++ b/src/consensus/stx.rs @@ -16,10 +16,12 @@ * along with this program. If not, see . */ +use async_trait::async_trait; use darkfi_sdk::{ crypto::MerkleNode, pasta::{arithmetic::CurveAffine, group::Curve, pallas}, }; +use smol::io::{AsyncRead, AsyncWrite}; use crate::{ zk::{proof::VerifyingKey, Proof}, diff --git a/src/consensus/tx.rs b/src/consensus/tx.rs index 743b7d40b..5fe4d25d2 100644 --- a/src/consensus/tx.rs +++ b/src/consensus/tx.rs @@ -16,7 +16,9 @@ * along with this program. If not, see . */ +use async_trait::async_trait; use darkfi_serial::{SerialDecodable, SerialEncodable}; +use smol::io::{AsyncRead, AsyncWrite}; use crate::consensus::{EncryptedTxRcpt, TransferStx}; diff --git a/src/event_graph/model.rs b/src/event_graph/model.rs index 63a089d96..75eef8dfc 100644 --- a/src/event_graph/model.rs +++ b/src/event_graph/model.rs @@ -18,11 +18,15 @@ use std::{cmp::Ordering, collections::HashMap, fmt::Debug, path::Path, sync::Arc}; +use async_trait::async_trait; use darkfi_serial::{ deserialize, serialize, Decodable, Encodable, SerialDecodable, SerialEncodable, }; use log::{error, info}; -use smol::lock::Mutex; +use smol::{ + io::{AsyncRead, AsyncWrite}, + lock::Mutex, +}; use tinyjson::JsonValue; use crate::{ diff --git a/src/event_graph/protocol_event.rs b/src/event_graph/protocol_event.rs index 6e78aabd3..1a796b1a5 100644 --- a/src/event_graph/protocol_event.rs +++ b/src/event_graph/protocol_event.rs @@ -21,7 +21,10 @@ use std::{fmt::Debug, sync::Arc}; use async_trait::async_trait; use darkfi_serial::{Decodable, Encodable, SerialDecodable, SerialEncodable}; use log::debug; -use smol::lock::Mutex; +use smol::{ + io::{AsyncRead, AsyncWrite}, + lock::Mutex, +}; use super::EventMsg; use crate::{ diff --git a/src/net/channel.rs b/src/net/channel.rs index b3047d2a3..6ed3b42a4 100644 --- a/src/net/channel.rs +++ b/src/net/channel.rs @@ -18,11 +18,12 @@ use std::sync::Arc; +use async_trait::async_trait; use darkfi_serial::{serialize, SerialDecodable, SerialEncodable}; use log::{debug, error, info}; use rand::{rngs::OsRng, Rng}; use smol::{ - io::{self, ReadHalf, WriteHalf}, + io::{self, AsyncRead, AsyncWrite, ReadHalf, WriteHalf}, lock::Mutex, Executor, }; diff --git a/src/net/message.rs b/src/net/message.rs index 884d10285..c3087f8fa 100644 --- a/src/net/message.rs +++ b/src/net/message.rs @@ -16,7 +16,10 @@ * along with this program. If not, see . */ -use darkfi_serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt}; +use async_trait::async_trait; +use darkfi_serial::{ + AsyncDecodable, AsyncEncodable, Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt, +}; use log::trace; use smol::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}; use url::Url; @@ -96,7 +99,7 @@ pub struct Packet { /// Reads and decodes an inbound payload from the given async stream. /// Returns decoded [`Packet`]. -pub async fn read_packet(stream: &mut R) -> Result { +pub async fn read_packet(stream: &mut R) -> Result { // Packets should have a 4 byte header of magic digits. // This is used for network debugging. let mut magic = [0u8; 4]; @@ -127,7 +130,7 @@ pub async fn read_packet(stream: &mut R) -> Result /// Sends an outbound packet by writing data to the given async stream. /// Returns the total written bytes. -pub async fn send_packet( +pub async fn send_packet( stream: &mut W, packet: Packet, ) -> Result { diff --git a/src/sdk/Cargo.toml b/src/sdk/Cargo.toml index 2220183ea..5db7986fc 100644 --- a/src/sdk/Cargo.toml +++ b/src/sdk/Cargo.toml @@ -11,11 +11,17 @@ edition = "2021" [lib] doctest = false +[features] +default = [] +async = ["async-trait", "futures-lite"] + [dependencies] # Error handling thiserror = "1.0.47" # Serialization +async-trait = {version = "0.1.73", optional = true} +futures-lite = {version = "1.13.0", optional = true} darkfi-serial = {version = "0.4.1", path = "../serial", features = ["derive", "crypto"]} # Encoding diff --git a/src/sdk/src/blockchain.rs b/src/sdk/src/blockchain.rs index cf02a9d65..632d1a4d3 100644 --- a/src/sdk/src/blockchain.rs +++ b/src/sdk/src/blockchain.rs @@ -16,6 +16,11 @@ * along with this program. If not, see . */ +#[cfg(feature = "async-trait")] +use async_trait::async_trait; +#[cfg(feature = "async")] +use futures_lite::{AsyncRead, AsyncWrite}; + use darkfi_serial::{SerialDecodable, SerialEncodable}; use pasta_curves::{group::ff::Field, pallas}; diff --git a/src/sdk/src/crypto/contract_id.rs b/src/sdk/src/crypto/contract_id.rs index 321c450a5..adb254c5c 100644 --- a/src/sdk/src/crypto/contract_id.rs +++ b/src/sdk/src/crypto/contract_id.rs @@ -16,6 +16,11 @@ * along with this program. If not, see . */ +#[cfg(feature = "async")] +use async_trait::async_trait; +#[cfg(feature = "async")] +use futures_lite::{AsyncRead, AsyncWrite}; + use darkfi_serial::{serialize, SerialDecodable, SerialEncodable}; use lazy_static::lazy_static; use pasta_curves::{group::ff::PrimeField, pallas}; diff --git a/src/sdk/src/crypto/ecvrf.rs b/src/sdk/src/crypto/ecvrf.rs index 8ca1bfa92..fd64d584e 100644 --- a/src/sdk/src/crypto/ecvrf.rs +++ b/src/sdk/src/crypto/ecvrf.rs @@ -19,6 +19,11 @@ //! #![allow(non_snake_case)] +#[cfg(feature = "async")] +use async_trait::async_trait; +#[cfg(feature = "async")] +use futures_lite::{AsyncRead, AsyncWrite}; + use darkfi_serial::{SerialDecodable, SerialEncodable}; use halo2_gadgets::ecc::chip::FixedPoint; use pasta_curves::{ diff --git a/src/sdk/src/crypto/keypair.rs b/src/sdk/src/crypto/keypair.rs index 2de6f3973..f66df7c7c 100644 --- a/src/sdk/src/crypto/keypair.rs +++ b/src/sdk/src/crypto/keypair.rs @@ -18,6 +18,11 @@ use core::str::FromStr; +#[cfg(feature = "async")] +use async_trait::async_trait; +#[cfg(feature = "async")] +use futures_lite::{AsyncRead, AsyncWrite}; + use darkfi_serial::{SerialDecodable, SerialEncodable}; use halo2_gadgets::ecc::chip::FixedPoint; use pasta_curves::{ diff --git a/src/sdk/src/crypto/merkle_node.rs b/src/sdk/src/crypto/merkle_node.rs index b544a2f6a..a4377edca 100644 --- a/src/sdk/src/crypto/merkle_node.rs +++ b/src/sdk/src/crypto/merkle_node.rs @@ -19,6 +19,11 @@ use core::{fmt, str::FromStr}; use std::{io, iter}; +#[cfg(feature = "async")] +use async_trait::async_trait; +#[cfg(feature = "async")] +use futures_lite::{AsyncRead, AsyncWrite}; + use bridgetree::{BridgeTree, Hashable, Level}; use darkfi_serial::{SerialDecodable, SerialEncodable}; use halo2_gadgets::sinsemilla::primitives::HashDomain; diff --git a/src/sdk/src/crypto/note.rs b/src/sdk/src/crypto/note.rs index 1497140fd..5a3548d2a 100644 --- a/src/sdk/src/crypto/note.rs +++ b/src/sdk/src/crypto/note.rs @@ -16,6 +16,11 @@ * along with this program. If not, see . */ +#[cfg(feature = "async")] +use async_trait::async_trait; +#[cfg(feature = "async")] +use futures_lite::{AsyncRead, AsyncWrite}; + use chacha20poly1305::{AeadInPlace, ChaCha20Poly1305, KeyInit}; use darkfi_serial::{Decodable, Encodable, SerialDecodable, SerialEncodable}; use pasta_curves::{group::ff::Field, pallas}; diff --git a/src/sdk/src/crypto/nullifier.rs b/src/sdk/src/crypto/nullifier.rs index bb4102271..d5b365785 100644 --- a/src/sdk/src/crypto/nullifier.rs +++ b/src/sdk/src/crypto/nullifier.rs @@ -16,6 +16,11 @@ * along with this program. If not, see . */ +#[cfg(feature = "async")] +use async_trait::async_trait; +#[cfg(feature = "async")] +use futures_lite::{AsyncRead, AsyncWrite}; + use darkfi_serial::{SerialDecodable, SerialEncodable}; use pasta_curves::{group::ff::PrimeField, pallas}; diff --git a/src/sdk/src/crypto/schnorr.rs b/src/sdk/src/crypto/schnorr.rs index f84cd8d0d..243e5d227 100644 --- a/src/sdk/src/crypto/schnorr.rs +++ b/src/sdk/src/crypto/schnorr.rs @@ -16,6 +16,11 @@ * along with this program. If not, see . */ +#[cfg(feature = "async")] +use async_trait::async_trait; +#[cfg(feature = "async")] +use futures_lite::{AsyncRead, AsyncWrite}; + use darkfi_serial::{SerialDecodable, SerialEncodable}; use halo2_gadgets::ecc::chip::FixedPoint; use pasta_curves::{ diff --git a/src/sdk/src/crypto/token_id.rs b/src/sdk/src/crypto/token_id.rs index 0bcc6e15d..33c0db14a 100644 --- a/src/sdk/src/crypto/token_id.rs +++ b/src/sdk/src/crypto/token_id.rs @@ -16,6 +16,11 @@ * along with this program. If not, see . */ +#[cfg(feature = "async")] +use async_trait::async_trait; +#[cfg(feature = "async")] +use futures_lite::{AsyncRead, AsyncWrite}; + use darkfi_serial::{SerialDecodable, SerialEncodable}; use lazy_static::lazy_static; use pasta_curves::{group::ff::PrimeField, pallas}; diff --git a/src/sdk/src/tx.rs b/src/sdk/src/tx.rs index 986e33b3b..65436ea07 100644 --- a/src/sdk/src/tx.rs +++ b/src/sdk/src/tx.rs @@ -16,6 +16,11 @@ * along with this program. If not, see . */ +#[cfg(feature = "async")] +use async_trait::async_trait; +#[cfg(feature = "async")] +use futures_lite::{AsyncRead, AsyncWrite}; + use darkfi_serial::{SerialDecodable, SerialEncodable}; use super::crypto::ContractId; diff --git a/src/tx/mod.rs b/src/tx/mod.rs index f9254ebe0..1dcc997dd 100644 --- a/src/tx/mod.rs +++ b/src/tx/mod.rs @@ -18,6 +18,7 @@ use std::collections::HashMap; +use async_trait::async_trait; use darkfi_sdk::{ crypto::{ schnorr::{SchnorrPublic, SchnorrSecret, Signature}, @@ -29,6 +30,7 @@ use darkfi_sdk::{ use darkfi_serial::{serialize, Encodable, SerialDecodable, SerialEncodable}; use log::{debug, error}; use rand::{CryptoRng, RngCore}; +use smol::io::{AsyncRead, AsyncWrite}; use crate::{ error::TxVerifyFailed, diff --git a/src/util/time.rs b/src/util/time.rs index 47c2231e7..b68b0a697 100644 --- a/src/util/time.rs +++ b/src/util/time.rs @@ -18,7 +18,9 @@ use std::{fmt, time::UNIX_EPOCH}; +use async_trait::async_trait; use darkfi_serial::{SerialDecodable, SerialEncodable}; +use smol::io::{AsyncRead, AsyncWrite}; use crate::Result; diff --git a/src/validator/consensus/mod.rs b/src/validator/consensus/mod.rs index 4a139867b..45efb1f1f 100644 --- a/src/validator/consensus/mod.rs +++ b/src/validator/consensus/mod.rs @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +use async_trait::async_trait; use darkfi_sdk::{ blockchain::{PidOutput, PreviousSlot, Slot}, crypto::{schnorr::SchnorrSecret, MerkleNode, MerkleTree, SecretKey}, @@ -24,6 +25,7 @@ use darkfi_sdk::{ use darkfi_serial::{serialize, SerialDecodable, SerialEncodable}; use log::{error, info, warn}; use rand::rngs::OsRng; +use smol::io::{AsyncRead, AsyncWrite}; use crate::{ blockchain::{ diff --git a/src/zk/proof.rs b/src/zk/proof.rs index 9539cbd90..91aab2366 100644 --- a/src/zk/proof.rs +++ b/src/zk/proof.rs @@ -17,6 +17,7 @@ */ use std::{io, io::Cursor}; +use async_trait::async_trait; use darkfi_sdk::pasta::{pallas, vesta}; use darkfi_serial::{SerialDecodable, SerialEncodable}; use halo2_proofs::{ @@ -27,6 +28,7 @@ use halo2_proofs::{ transcript::{Blake2bRead, Blake2bWrite}, }; use rand::RngCore; +use smol::io::{AsyncRead, AsyncWrite}; #[derive(Clone, Debug)] pub struct VerifyingKey {