Просмотр исходного кода

darkfid: cleaned up unused PoWReward fields, use (de)serialize_async

skoupidi 2 лет назад
Родитель
Сommit
fbeb88ad45

+ 3 - 2
bin/darkfid/src/task/consensus.rs

@@ -17,7 +17,7 @@
  */
 
 use darkfi::{rpc::util::JsonValue, util::encoding::base64, Result};
-use darkfi_serial::serialize;
+use darkfi_serial::serialize_async;
 use log::info;
 
 use crate::Darkfid;
@@ -43,7 +43,8 @@ pub async fn consensus_task(node: &Darkfid) -> Result<()> {
         if !finalized.is_empty() {
             let mut notif_blocks = Vec::with_capacity(finalized.len());
             for block in finalized {
-                notif_blocks.push(JsonValue::String(base64::encode(&serialize(&block))));
+                notif_blocks
+                    .push(JsonValue::String(base64::encode(&serialize_async(&block).await)));
             }
             block_sub.notify(JsonValue::Array(notif_blocks)).await;
         }

+ 12 - 16
bin/darkfid/src/task/miner.rs

@@ -38,7 +38,7 @@ use darkfi_sdk::{
     pasta::pallas,
     ContractCall,
 };
-use darkfi_serial::{serialize, Encodable};
+use darkfi_serial::{serialize_async, Encodable};
 use log::info;
 use num_bigint::BigUint;
 use rand::rngs::OsRng;
@@ -101,7 +101,8 @@ pub async fn miner_task(node: &Darkfid, recipient: &PublicKey, skip_sync: bool)
             if !finalized.is_empty() {
                 let mut notif_blocks = Vec::with_capacity(finalized.len());
                 for block in finalized {
-                    notif_blocks.push(JsonValue::String(base64::encode(&serialize(&block))));
+                    notif_blocks
+                        .push(JsonValue::String(base64::encode(&serialize_async(&block).await)));
                 }
                 block_sub.notify(JsonValue::Array(notif_blocks)).await;
                 break;
@@ -133,7 +134,8 @@ pub async fn miner_task(node: &Darkfid, recipient: &PublicKey, skip_sync: bool)
         if !finalized.is_empty() {
             let mut notif_blocks = Vec::with_capacity(finalized.len());
             for block in finalized {
-                notif_blocks.push(JsonValue::String(base64::encode(&serialize(&block))));
+                notif_blocks
+                    .push(JsonValue::String(base64::encode(&serialize_async(&block).await)));
             }
             block_sub.notify(JsonValue::Array(notif_blocks)).await;
         }
@@ -221,7 +223,7 @@ async fn mine_next_block(
 
     // Execute request to minerd and parse response
     let target = JsonValue::String(next_target.to_string());
-    let block = JsonValue::String(base64::encode(&serialize(&next_block)));
+    let block = JsonValue::String(base64::encode(&serialize_async(&next_block).await));
     let response = node.miner_daemon_request("mine", JsonValue::Array(vec![target, block])).await?;
     next_block.header.nonce = *response.get::<f64>().unwrap() as u64;
 
@@ -250,19 +252,19 @@ async fn generate_next_block(
     zkbin: &ZkBinary,
     pk: &ProvingKey,
 ) -> Result<(BigUint, BlockInfo)> {
-    // Grab extended fork last proposal hash
+    // Grab extended fork next block height
     let last_proposal = extended_fork.last_proposal()?;
+    let next_block_height = last_proposal.block.header.height + 1;
 
     // We are deriving the next secret key for optimization.
     // Next secret is the poseidon hash of:
     //  [prefix, current(previous) secret, signing(block) height].
     let prefix = pallas::Base::from_raw([4, 0, 0, 0]);
-    let next_secret =
-        poseidon_hash([prefix, secret.inner(), (last_proposal.block.header.height + 1).into()]);
+    let next_secret = poseidon_hash([prefix, secret.inner(), next_block_height.into()]);
     *secret = SecretKey::from(next_secret);
 
     // Generate reward transaction
-    let tx = generate_transaction(&extended_fork.last_proposal()?, secret, recipient, zkbin, pk)?;
+    let tx = generate_transaction(next_block_height, secret, recipient, zkbin, pk)?;
 
     // Generate next block proposal
     let target = extended_fork.module.next_mine_target()?;
@@ -273,16 +275,12 @@ async fn generate_next_block(
 
 /// Auxiliary function to generate a Money::PoWReward transaction
 fn generate_transaction(
-    last_proposal: &Proposal,
+    block_height: u64,
     secret: &SecretKey,
     recipient: &PublicKey,
     zkbin: &ZkBinary,
     pk: &ProvingKey,
 ) -> Result<Transaction> {
-    // Grab extended proposal info
-    let last_nonce = last_proposal.block.header.nonce;
-    let fork_previous_hash = last_proposal.block.header.previous;
-
     // We're just going to be using a zero spend-hook and user-data
     let spend_hook = pallas::Base::zero().into();
     let user_data = pallas::Base::zero();
@@ -291,9 +289,7 @@ fn generate_transaction(
     let debris = PoWRewardCallBuilder {
         secret: *secret,
         recipient: *recipient,
-        block_height: last_proposal.block.header.height + 1,
-        last_nonce,
-        fork_previous_hash,
+        block_height,
         spend_hook,
         user_data,
         mint_zkbin: zkbin.clone(),

+ 3 - 13
bin/darkfid/src/tests/harness.rs

@@ -28,7 +28,7 @@ use darkfi::{
     zk::{empty_witnesses, ProvingKey, ZkCircuit},
     Result,
 };
-use darkfi_contract_test_harness::{vks, Holder, TestHarness};
+use darkfi_contract_test_harness::vks;
 use darkfi_money_contract::{
     client::pow_reward_v1::PoWRewardCallBuilder, MoneyFunction, MONEY_CONTRACT_ZKAS_MINT_NS_V1,
 };
@@ -47,8 +47,6 @@ pub struct HarnessConfig {
     pub pow_target: usize,
     pub pow_fixed_difficulty: Option<BigUint>,
     pub finalization_threshold: usize,
-    pub alice_initial: u64,
-    pub bob_initial: u64,
 }
 
 pub struct Harness {
@@ -65,19 +63,14 @@ impl Harness {
         verify_fees: bool,
         ex: &Arc<smol::Executor<'static>>,
     ) -> Result<Self> {
-        // Use test harness to generate genesis transactions
-        let mut th = TestHarness::new(&[Holder::Bob], verify_fees).await?;
-        let (genesis_mint_tx, _) =
-            th.genesis_mint(&Holder::Bob, config.bob_initial, None, None).await?;
-
         // Generate default genesis block
         let mut genesis_block = BlockInfo::default();
 
         // Retrieve genesis producer transaction
         let producer_tx = genesis_block.txs.pop().unwrap();
 
-        // Append genesis transactions
-        genesis_block.append_txs(vec![genesis_mint_tx, producer_tx])?;
+        // Append it again so its added to the merkle tree
+        genesis_block.append_txs(vec![producer_tx])?;
 
         // Generate validators configuration
         // NOTE: we are not using consensus constants here so we
@@ -169,7 +162,6 @@ impl Harness {
         // Next block info
         let block_height = previous.header.height + 1;
         let last_nonce = previous.header.nonce;
-        let fork_previous_hash = previous.header.previous;
 
         // Generate a producer transaction
         let keypair = Keypair::default();
@@ -190,8 +182,6 @@ impl Harness {
             secret: keypair.secret,
             recipient: keypair.public,
             block_height,
-            last_nonce,
-            fork_previous_hash,
             spend_hook,
             user_data,
             mint_zkbin: zkbin.clone(),

+ 0 - 2
bin/darkfid/src/tests/mod.rs

@@ -42,8 +42,6 @@ async fn sync_blocks_real(ex: Arc<Executor<'static>>) -> Result<()> {
         pow_target,
         pow_fixed_difficulty: pow_fixed_difficulty.clone(),
         finalization_threshold: 3,
-        alice_initial: 1000,
-        bob_initial: 500,
     };
     let th = Harness::new(config, true, &ex).await?;
 

+ 0 - 2
bin/darkfid/src/tests/sync_forks.rs

@@ -37,8 +37,6 @@ async fn sync_forks_real(ex: Arc<Executor<'static>>) -> Result<()> {
         pow_target,
         pow_fixed_difficulty: pow_fixed_difficulty.clone(),
         finalization_threshold: 6,
-        alice_initial: 1000,
-        bob_initial: 500,
     };
     let th = Harness::new(config, true, &ex).await?;
 

+ 0 - 4
src/contract/money/src/client/pow_reward_v1.rs

@@ -68,10 +68,6 @@ pub struct PoWRewardCallBuilder {
     pub recipient: PublicKey,
     /// Rewarded block height
     pub block_height: u64,
-    /// Extending fork last proposal/block nonce
-    pub last_nonce: u64,
-    /// Extending fork second to last proposal/block hash
-    pub fork_previous_hash: blake3::Hash,
     /// Merkle tree of coins used to create inclusion proofs
     /// Spend hook for the output
     pub spend_hook: FuncId,

+ 0 - 2
src/contract/test-harness/src/money_pow_reward.rs

@@ -67,8 +67,6 @@ impl TestHarness {
             secret: wallet.keypair.secret,
             recipient,
             block_height: last_block.header.height + 1,
-            last_nonce: last_block.header.nonce,
-            fork_previous_hash: last_block.header.previous,
             spend_hook: FuncId::none(),
             user_data: pallas::Base::ZERO,
             mint_zkbin: mint_zkbin.clone(),