Browse Source

contract/money/client/pow_reward: minor cleanup

skoupidi 8 months ago
parent
commit
07987674b1

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

@@ -34,7 +34,7 @@ use darkfi::{
 };
 use darkfi_money_contract::{client::pow_reward_v1::PoWRewardCallBuilder, MoneyFunction};
 use darkfi_sdk::{
-    crypto::{FuncId, MerkleTree, PublicKey, SecretKey, MONEY_CONTRACT_ID},
+    crypto::{FuncId, Keypair, MerkleTree, PublicKey, SecretKey, MONEY_CONTRACT_ID},
     pasta::pallas,
     ContractCall,
 };
@@ -335,16 +335,17 @@ pub async fn generate_next_block(
         .unproposed_txs(&extended_fork.blockchain, next_block_height, block_target, verify_fees)
         .await?;
 
-    // Create an ephemeral block signing key. It will be stored in the PowReward
-    // transaction's encrypted note for later retrieval. It is encrypted towards
-    // the recipient's public key.
-    let block_signing_secret = SecretKey::random(&mut OsRng);
+    // Create an ephemeral block signing keypair. Its secret key will
+    // be stored in the PowReward transaction's encrypted note for
+    // later retrieval. It is encrypted towards the recipient's public
+    // key.
+    let block_signing_keypair = Keypair::random(&mut OsRng);
 
     // Generate reward transaction
     let tx = generate_transaction(
         next_block_height,
         fees,
-        &block_signing_secret,
+        &block_signing_keypair,
         recipient_config,
         zkbin,
         pk,
@@ -384,21 +385,21 @@ pub async fn generate_next_block(
     // Grab the next mine target
     let target = extended_fork.module.next_mine_target()?;
 
-    Ok((target, next_block, block_signing_secret))
+    Ok((target, next_block, block_signing_keypair.secret))
 }
 
 /// Auxiliary function to generate a Money::PoWReward transaction.
 fn generate_transaction(
     block_height: u32,
     fees: u64,
-    block_signing_secret: &SecretKey,
+    block_signing_keypair: &Keypair,
     recipient_config: &MinerRewardsRecipientConfig,
     zkbin: &ZkBinary,
     pk: &ProvingKey,
 ) -> Result<Transaction> {
     // Build the transaction debris
     let debris = PoWRewardCallBuilder {
-        signature_public: PublicKey::from_secret(*block_signing_secret),
+        signature_keypair: *block_signing_keypair,
         block_height,
         fees,
         recipient: Some(recipient_config.recipient),
@@ -407,7 +408,7 @@ fn generate_transaction(
         mint_zkbin: zkbin.clone(),
         mint_pk: pk.clone(),
     }
-    .build(block_signing_secret)?;
+    .build()?;
 
     // Generate and sign the actual transaction
     let mut data = vec![MoneyFunction::PoWRewardV1 as u8];
@@ -416,7 +417,7 @@ fn generate_transaction(
     let mut tx_builder =
         TransactionBuilder::new(ContractCallLeaf { call, proofs: debris.proofs }, vec![])?;
     let mut tx = tx_builder.build()?;
-    let sigs = tx.create_sigs(&[*block_signing_secret])?;
+    let sigs = tx.create_sigs(&[block_signing_keypair.secret])?;
     tx.signatures = vec![sigs];
 
     Ok(tx)

+ 2 - 2
bin/darkfid/src/tests/harness.rs

@@ -202,7 +202,7 @@ impl Harness {
 
         // Build the transaction debris
         let debris = PoWRewardCallBuilder {
-            signature_public: keypair.public,
+            signature_keypair: keypair,
             block_height,
             fees: 0,
             recipient: None,
@@ -211,7 +211,7 @@ impl Harness {
             mint_zkbin: zkbin.clone(),
             mint_pk: pk.clone(),
         }
-        .build(&keypair.secret)?;
+        .build()?;
 
         // Generate and sign the actual transaction
         let mut data = vec![MoneyFunction::PoWRewardV1 as u8];

+ 11 - 15
src/contract/money/src/client/pow_reward_v1.rs

@@ -23,7 +23,7 @@ use darkfi::{
 };
 use darkfi_sdk::{
     blockchain::expected_reward,
-    crypto::{note::AeadEncryptedNote, pasta_prelude::*, Blind, FuncId, PublicKey, SecretKey},
+    crypto::{note::AeadEncryptedNote, pasta_prelude::*, Blind, FuncId, Keypair, PublicKey},
     pasta::pallas,
 };
 use darkfi_serial::serialize;
@@ -61,8 +61,8 @@ impl PoWRewardRevealed {
 
 /// Struct holding necessary information to build a `Money::PoWRewardV1` contract call.
 pub struct PoWRewardCallBuilder {
-    /// Caller's public key, corresponding to the one used in the signature
-    pub signature_public: PublicKey,
+    /// Caller's keypair, corresponding to the one used in the signature
+    pub signature_keypair: Keypair,
     /// Rewarded block height
     pub block_height: u32,
     /// Rewarded block transactions paid fees
@@ -80,7 +80,7 @@ pub struct PoWRewardCallBuilder {
 }
 
 impl PoWRewardCallBuilder {
-    fn _build(&self, value: u64, block_signing_secret: &SecretKey) -> Result<PoWRewardCallDebris> {
+    fn _build(&self, value: u64) -> Result<PoWRewardCallDebris> {
         debug!(target: "contract::money::client::pow_reward", "Building Money::PoWRewardV1 contract call");
 
         // In this call, we will build one clear input and one anonymous output.
@@ -96,7 +96,7 @@ impl PoWRewardCallBuilder {
             token_id,
             value_blind,
             token_blind,
-            signature_public: self.signature_public,
+            signature_public: self.signature_keypair.public,
         };
 
         // Grab the spend hook and user data to use in the output
@@ -105,7 +105,7 @@ impl PoWRewardCallBuilder {
 
         // Building the anonymous output
         let output = TransferCallOutput {
-            public_key: self.recipient.unwrap_or(self.signature_public),
+            public_key: self.recipient.unwrap_or(self.signature_keypair.public),
             value,
             token_id,
             spend_hook,
@@ -133,7 +133,7 @@ impl PoWRewardCallBuilder {
             coin_blind,
             value_blind,
             token_blind,
-            memo: serialize(block_signing_secret),
+            memo: serialize(&self.signature_keypair.secret),
         };
 
         let encrypted_note = AeadEncryptedNote::encrypt(&note, &output.public_key, &mut OsRng)?;
@@ -150,17 +150,13 @@ impl PoWRewardCallBuilder {
         Ok(debris)
     }
 
-    pub fn build(&self, block_signing_key: &SecretKey) -> Result<PoWRewardCallDebris> {
+    pub fn build(&self) -> Result<PoWRewardCallDebris> {
         let reward = expected_reward(self.block_height) + self.fees;
-        self._build(reward, block_signing_key)
+        self._build(reward)
     }
 
     /// This function should only be used for testing, as PoW reward values are predefined
-    pub fn build_with_custom_reward(
-        &self,
-        reward: u64,
-        block_signing_key: &SecretKey,
-    ) -> Result<PoWRewardCallDebris> {
-        self._build(reward + self.fees, block_signing_key)
+    pub fn build_with_custom_reward(&self, reward: u64) -> Result<PoWRewardCallDebris> {
+        self._build(reward + self.fees)
     }
 }

+ 4 - 9
src/contract/test-harness/src/money_pow_reward.rs

@@ -16,8 +16,6 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use rand::rngs::OsRng;
-
 use darkfi::{
     blockchain::{BlockInfo, BlockchainOverlay, Header},
     tx::{ContractCallLeaf, Transaction, TransactionBuilder},
@@ -30,7 +28,7 @@ use darkfi_money_contract::{
     MoneyFunction, MONEY_CONTRACT_ZKAS_MINT_NS_V1,
 };
 use darkfi_sdk::{
-    crypto::{contract_id::MONEY_CONTRACT_ID, MerkleNode, MerkleTree, SecretKey},
+    crypto::{contract_id::MONEY_CONTRACT_ID, MerkleNode, MerkleTree},
     ContractCall,
 };
 use darkfi_serial::AsyncEncodable;
@@ -67,12 +65,9 @@ impl TestHarness {
         // If there's fees paid, use them, otherwise set to zero
         let fees = fees.unwrap_or_default();
 
-        // Generate a random block signing key
-        let block_signing_key = SecretKey::random(&mut OsRng);
-
         // Build the transaction
         let builder = PoWRewardCallBuilder {
-            signature_public: wallet.keypair.public,
+            signature_keypair: wallet.keypair,
             block_height: last_block.header.height + 1,
             fees,
             recipient,
@@ -83,8 +78,8 @@ impl TestHarness {
         };
 
         let debris = match reward {
-            Some(value) => builder.build_with_custom_reward(value, &block_signing_key)?,
-            None => builder.build(&block_signing_key)?,
+            Some(value) => builder.build_with_custom_reward(value)?,
+            None => builder.build()?,
         };
 
         // Encode the transaction