lib: Add necessary traits around for async serial.

This commit is contained in:
parazyd
2023-08-24 14:26:04 +02:00
parent 75cb654068
commit 98703d361a
30 changed files with 108 additions and 13 deletions

2
Cargo.lock generated
View File

@@ -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",

View File

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

View File

@@ -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};

View File

@@ -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};

View File

@@ -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},

View File

@@ -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::{

View File

@@ -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},

View File

@@ -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;

View File

@@ -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,

View File

@@ -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},

View File

@@ -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};

View File

@@ -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::{

View File

@@ -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::{

View File

@@ -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,
};

View File

@@ -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> {

View File

@@ -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

View File

@@ -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};

View File

@@ -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};

View File

@@ -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::{

View File

@@ -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::{

View File

@@ -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;

View File

@@ -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};

View File

@@ -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};

View File

@@ -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::{

View File

@@ -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};

View File

@@ -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;

View File

@@ -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,

View File

@@ -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;

View File

@@ -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::{

View File

@@ -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 {