Bläddra i källkod

darkfid: Port serialization to async functions

parazyd 2 år sedan
förälder
incheckning
e11d8c2c80

+ 3 - 3
bin/darkfid2/src/main.rs

@@ -42,7 +42,7 @@ use darkfi::{
     Error, Result,
 };
 use darkfi_sdk::crypto::PublicKey;
-use darkfi_serial::deserialize;
+use darkfi_serial::deserialize_async;
 
 #[cfg(test)]
 mod tests;
@@ -226,14 +226,14 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
 
     // Parse the genesis block
     let bytes = bs58::decode(&genesis_block.trim()).into_vec()?;
-    let genesis_block: BlockInfo = deserialize(&bytes)?;
+    let genesis_block: BlockInfo = deserialize_async(&bytes).await?;
 
     // Initialize or open sled database
     let db_path = expand_path(&blockchain_config.database)?;
     let sled_db = sled::open(&db_path)?;
 
     // Initialize validator configuration
-    let genesis_txs_total = genesis_txs_total(&genesis_block.txs)?;
+    let genesis_txs_total = genesis_txs_total(&genesis_block.txs).await?;
     let time_keeper = TimeKeeper::new(
         genesis_block.header.timestamp,
         blockchain_config.epoch_length,

+ 3 - 2
bin/darkfid2/src/proto/protocol_block.rs

@@ -36,7 +36,7 @@ use darkfi::{
     validator::ValidatorPtr,
     Result,
 };
-use darkfi_serial::{serialize, SerialDecodable, SerialEncodable};
+use darkfi_serial::{serialize_async, SerialDecodable, SerialEncodable};
 
 /// Auxiliary [`BlockInfo`] wrapper structure used for messaging.
 #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
@@ -127,7 +127,8 @@ impl ProtocolBlock {
             match self.validator.append_block(&block_copy.0).await {
                 Ok(()) => {
                     self.p2p.broadcast_with_exclude(&block_copy, &exclude_list).await;
-                    let encoded_block = JsonValue::String(base64::encode(&serialize(&block_copy)));
+                    let encoded_block =
+                        JsonValue::String(base64::encode(&serialize_async(&block_copy).await));
                     self.subscriber.notify(vec![encoded_block].into()).await;
                 }
                 Err(e) => {

+ 3 - 2
bin/darkfid2/src/proto/protocol_proposal.rs

@@ -35,7 +35,7 @@ use darkfi::{
     validator::{consensus::Proposal, ValidatorPtr},
     Result,
 };
-use darkfi_serial::{serialize, SerialDecodable, SerialEncodable};
+use darkfi_serial::{serialize_async, SerialDecodable, SerialEncodable};
 
 /// Auxiliary [`Proposal`] wrapper structure used for messaging.
 #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
@@ -117,7 +117,8 @@ impl ProtocolProposal {
             match self.validator.consensus.append_proposal(&proposal_copy.0).await {
                 Ok(()) => {
                     self.p2p.broadcast_with_exclude(&proposal_copy, &exclude_list).await;
-                    let enc_prop = JsonValue::String(base64::encode(&serialize(&proposal_copy)));
+                    let enc_prop =
+                        JsonValue::String(base64::encode(&serialize_async(&proposal_copy).await));
                     self.subscriber.notify(vec![enc_prop].into()).await;
                 }
                 Err(e) => {

+ 3 - 2
bin/darkfid2/src/proto/protocol_tx.rs

@@ -35,7 +35,7 @@ use darkfi::{
     validator::ValidatorPtr,
     Result,
 };
-use darkfi_serial::serialize;
+use darkfi_serial::serialize_async;
 
 pub struct ProtocolTx {
     tx_sub: MessageSubscription<Transaction>,
@@ -106,7 +106,8 @@ impl ProtocolTx {
             match self.validator.append_tx(&tx_copy).await {
                 Ok(()) => {
                     self.p2p.broadcast_with_exclude(&tx_copy, &exclude_list).await;
-                    let encoded_tx = JsonValue::String(base64::encode(&serialize(&tx_copy)));
+                    let encoded_tx =
+                        JsonValue::String(base64::encode(&serialize_async(&tx_copy).await));
                     self.subscriber.notify(vec![encoded_tx].into()).await;
                 }
                 Err(e) => {

+ 4 - 4
bin/darkfid2/src/rpc_blockchain.rs

@@ -19,7 +19,7 @@
 use std::{collections::HashMap, str::FromStr};
 
 use darkfi_sdk::crypto::ContractId;
-use darkfi_serial::{deserialize, serialize};
+use darkfi_serial::{deserialize_async, serialize_async};
 use log::{debug, error};
 use tinyjson::JsonValue;
 
@@ -71,7 +71,7 @@ impl Darkfid {
             return server_error(RpcError::UnknownSlot, id, None)
         }
 
-        let block = base64::encode(&serialize(&blocks[0]));
+        let block = base64::encode(&serialize_async(&blocks[0]).await);
         JsonResponse::new(JsonValue::String(block), id).into()
     }
 
@@ -112,7 +112,7 @@ impl Darkfid {
         // and strict was used during .get()
         let tx = txs[0].as_ref().unwrap();
 
-        let tx_enc = base64::encode(&serialize(tx));
+        let tx_enc = base64::encode(&serialize_async(tx).await);
         JsonResponse::new(JsonValue::String(tx_enc), id).into()
     }
 
@@ -249,7 +249,7 @@ impl Darkfid {
                 return JsonError::new(InternalError, None, id).into()
             };
 
-            let Ok(zkas_ns) = deserialize(&zkas_ns) else {
+            let Ok(zkas_ns) = deserialize_async(&zkas_ns).await else {
                 return JsonError::new(InternalError, None, id).into()
             };
 

+ 3 - 3
bin/darkfid2/src/rpc_tx.rs

@@ -16,7 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use darkfi_serial::deserialize;
+use darkfi_serial::deserialize_async;
 use log::error;
 use tinyjson::JsonValue;
 
@@ -61,7 +61,7 @@ impl Darkfid {
             }
         };
 
-        let tx: Transaction = match deserialize(&tx_bytes) {
+        let tx: Transaction = match deserialize_async(&tx_bytes).await {
             Ok(v) => v,
             Err(e) => {
                 error!(target: "darkfid::rpc::tx_simulate", "Failed deserializing bytes into Transaction: {}", e);
@@ -112,7 +112,7 @@ impl Darkfid {
             }
         };
 
-        let tx: Transaction = match deserialize(&tx_bytes) {
+        let tx: Transaction = match deserialize_async(&tx_bytes).await {
             Ok(v) => v,
             Err(e) => {
                 error!(target: "darkfid::rpc::tx_broadcast", "Failed deserializing bytes into Transaction: {}", e);

+ 2 - 2
bin/darkfid2/src/task/sync.rs

@@ -17,7 +17,7 @@
  */
 
 use darkfi::{system::sleep, util::encoding::base64, Result};
-use darkfi_serial::serialize;
+use darkfi_serial::serialize_async;
 use log::{debug, info, warn};
 use tinyjson::JsonValue;
 
@@ -72,7 +72,7 @@ pub async fn sync_task(node: &Darkfid) -> Result<()> {
 
         // Notify subscriber
         for block in &response.blocks {
-            let encoded_block = JsonValue::String(base64::encode(&serialize(block)));
+            let encoded_block = JsonValue::String(base64::encode(&serialize_async(block).await));
             notif_sub.notify(vec![encoded_block].into()).await;
         }
 

+ 29 - 27
bin/darkfid2/src/tests/forks.rs

@@ -24,40 +24,42 @@ use darkfi::{
 
 #[test]
 fn forks() -> Result<()> {
-    // Dummy records we will insert
-    let record0 = blake3::hash(b"Let there be dark!");
-    let record1 = blake3::hash(b"Never skip brain day.");
+    smol::block_on(async {
+        // Dummy records we will insert
+        let record0 = blake3::hash(b"Let there be dark!");
+        let record1 = blake3::hash(b"Never skip brain day.");
 
-    // Create a temporary blockchain and a PoW module
-    let blockchain = Blockchain::new(&sled::Config::new().temporary(true).open()?)?;
-    let module = PoWModule::new(blockchain.clone(), 2, 90, None)?;
+        // Create a temporary blockchain and a PoW module
+        let blockchain = Blockchain::new(&sled::Config::new().temporary(true).open()?)?;
+        let module = PoWModule::new(blockchain.clone(), 2, 90, None)?;
 
-    // Create a fork
-    let fork = Fork::new(&blockchain, module)?;
+        // Create a fork
+        let fork = Fork::new(&blockchain, module).await?;
 
-    // Add a dummy record to fork
-    fork.overlay.lock().unwrap().order.insert(&[0], &[record0])?;
+        // Add a dummy record to fork
+        fork.overlay.lock().unwrap().order.insert(&[0], &[record0])?;
 
-    // Verify blockchain doesn't contain the record
-    assert_eq!(blockchain.order.get(&[0], false)?, [None]);
-    assert_eq!(fork.overlay.lock().unwrap().order.get(&[0], true)?, [Some(record0)]);
+        // Verify blockchain doesn't contain the record
+        assert_eq!(blockchain.order.get(&[0], false)?, [None]);
+        assert_eq!(fork.overlay.lock().unwrap().order.get(&[0], true)?, [Some(record0)]);
 
-    // Now we are going to clone the fork
-    let fork_clone = fork.full_clone()?;
+        // Now we are going to clone the fork
+        let fork_clone = fork.full_clone()?;
 
-    // Verify it cointains the original record
-    assert_eq!(fork_clone.overlay.lock().unwrap().order.get(&[0], true)?, [Some(record0)]);
+        // Verify it cointains the original record
+        assert_eq!(fork_clone.overlay.lock().unwrap().order.get(&[0], true)?, [Some(record0)]);
 
-    // Add another dummy record to cloned fork
-    fork_clone.overlay.lock().unwrap().order.insert(&[1], &[record1])?;
+        // Add another dummy record to cloned fork
+        fork_clone.overlay.lock().unwrap().order.insert(&[1], &[record1])?;
 
-    // Verify blockchain and original fork don't contain the second record
-    assert_eq!(blockchain.order.get(&[0, 1], false)?, [None, None]);
-    assert_eq!(fork.overlay.lock().unwrap().order.get(&[0, 1], false)?, [Some(record0), None]);
-    assert_eq!(
-        fork_clone.overlay.lock().unwrap().order.get(&[0, 1], true)?,
-        [Some(record0), Some(record1)]
-    );
+        // Verify blockchain and original fork don't contain the second record
+        assert_eq!(blockchain.order.get(&[0, 1], false)?, [None, None]);
+        assert_eq!(fork.overlay.lock().unwrap().order.get(&[0, 1], false)?, [Some(record0), None]);
+        assert_eq!(
+            fork_clone.overlay.lock().unwrap().order.get(&[0, 1], true)?,
+            [Some(record0), Some(record1)]
+        );
 
-    Ok(())
+        Ok(())
+    })
 }

+ 1 - 1
bin/darkfid2/src/tests/harness.rs

@@ -76,7 +76,7 @@ impl Harness {
         genesis_block.txs.push(genesis_stake_tx);
         genesis_block.txs.push(genesis_mint_tx);
         genesis_block.txs.push(producer_tx);
-        let genesis_txs_total = genesis_txs_total(&genesis_block.txs)?;
+        let genesis_txs_total = genesis_txs_total(&genesis_block.txs).await?;
         genesis_block.slots[0].total_tokens = genesis_txs_total;
 
         // Generate validators configuration