Browse Source

lib: Add necessary traits around for async serial.

parazyd 3 years ago
parent
commit
98703d361a

+ 2 - 0
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",

+ 7 - 7
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",
 ]
 

+ 2 - 0
src/blockchain/block_store.rs

@@ -16,12 +16,14 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+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};
 

+ 2 - 0
src/blockchain/header_store.rs

@@ -16,8 +16,10 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+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};
 

+ 2 - 0
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},

+ 2 - 0
src/consensus/lead_coin.rs

@@ -16,6 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+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::{

+ 2 - 0
src/consensus/lead_info.rs

@@ -16,12 +16,14 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+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},

+ 2 - 0
src/consensus/rcpt.rs

@@ -16,6 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+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;
 

+ 2 - 0
src/consensus/state.rs

@@ -16,6 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+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,

+ 2 - 0
src/consensus/stx.rs

@@ -16,10 +16,12 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+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},

+ 2 - 0
src/consensus/tx.rs

@@ -16,7 +16,9 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+use async_trait::async_trait;
 use darkfi_serial::{SerialDecodable, SerialEncodable};
+use smol::io::{AsyncRead, AsyncWrite};
 
 use crate::consensus::{EncryptedTxRcpt, TransferStx};
 

+ 5 - 1
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::{

+ 4 - 1
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::{

+ 2 - 1
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,
 };

+ 6 - 3
src/net/message.rs

@@ -16,7 +16,10 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-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<R: AsyncRead + Unpin + Sized>(stream: &mut R) -> Result<Packet> {
+pub async fn read_packet<R: AsyncRead + Unpin + Send + Sized>(stream: &mut R) -> Result<Packet> {
     // 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<R: AsyncRead + Unpin + Sized>(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<W: AsyncWrite + Unpin + Sized>(
+pub async fn send_packet<W: AsyncWrite + Unpin + Send + Sized>(
     stream: &mut W,
     packet: Packet,
 ) -> Result<usize> {

+ 6 - 0
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

+ 5 - 0
src/sdk/src/blockchain.rs

@@ -16,6 +16,11 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+#[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};
 

+ 5 - 0
src/sdk/src/crypto/contract_id.rs

@@ -16,6 +16,11 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+#[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};

+ 5 - 0
src/sdk/src/crypto/ecvrf.rs

@@ -19,6 +19,11 @@
 //! <https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-vrf-04#section-5>
 #![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::{

+ 5 - 0
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::{

+ 5 - 0
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;

+ 5 - 0
src/sdk/src/crypto/note.rs

@@ -16,6 +16,11 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+#[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};

+ 5 - 0
src/sdk/src/crypto/nullifier.rs

@@ -16,6 +16,11 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+#[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};
 

+ 5 - 0
src/sdk/src/crypto/schnorr.rs

@@ -16,6 +16,11 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+#[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::{

+ 5 - 0
src/sdk/src/crypto/token_id.rs

@@ -16,6 +16,11 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+#[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};

+ 5 - 0
src/sdk/src/tx.rs

@@ -16,6 +16,11 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+#[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;

+ 2 - 0
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,

+ 2 - 0
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;
 

+ 2 - 0
src/validator/consensus/mod.rs

@@ -16,6 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+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::{

+ 2 - 0
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 {