darkfi_contract_test_harness/
money_pow_reward.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use darkfi::{
20    blockchain::{BlockInfo, BlockchainOverlay, Header},
21    tx::{ContractCallLeaf, Transaction, TransactionBuilder},
22    validator::verification::apply_producer_transaction,
23    Result,
24};
25use darkfi_money_contract::{
26    client::{pow_reward_v1::PoWRewardCallBuilder, MoneyNote, OwnCoin},
27    model::MoneyPoWRewardParamsV1,
28    MoneyFunction, MONEY_CONTRACT_ZKAS_MINT_NS_V1,
29};
30use darkfi_sdk::{
31    crypto::{contract_id::MONEY_CONTRACT_ID, MerkleNode, MerkleTree},
32    ContractCall,
33};
34use darkfi_serial::AsyncEncodable;
35use log::info;
36
37use super::{Holder, TestHarness};
38
39impl TestHarness {
40    /// Create a `Money::PoWReward` transaction for a given [`Holder`].
41    ///
42    /// Optionally takes a specific reward recipient and a nonstandard reward value.
43    /// Returns the created [`Transaction`] and [`MoneyPoWRewardParamsV1`].
44    async fn pow_reward(
45        &mut self,
46        holder: &Holder,
47        recipient: Option<&Holder>,
48        reward: Option<u64>,
49        fees: Option<u64>,
50    ) -> Result<(Transaction, MoneyPoWRewardParamsV1)> {
51        let wallet = self.holders.get(holder).unwrap();
52
53        let (mint_pk, mint_zkbin) = self.proving_keys.get(MONEY_CONTRACT_ZKAS_MINT_NS_V1).unwrap();
54
55        // Reference the last block in the holder's blockchain
56        let last_block = wallet.validator.blockchain.last_block()?;
57
58        // If there's a set reward recipient, use it, otherwise reward the holder
59        let recipient = if let Some(holder) = recipient {
60            Some(self.holders.get(holder).unwrap().keypair.public)
61        } else {
62            None
63        };
64
65        // If there's fees paid, use them, otherwise set to zero
66        let fees = fees.unwrap_or_default();
67
68        // Build the transaction
69        let builder = PoWRewardCallBuilder {
70            signature_public: wallet.keypair.public,
71            block_height: last_block.header.height + 1,
72            fees,
73            recipient,
74            spend_hook: None,
75            user_data: None,
76            mint_zkbin: mint_zkbin.clone(),
77            mint_pk: mint_pk.clone(),
78        };
79
80        let debris = match reward {
81            Some(value) => builder.build_with_custom_reward(value)?,
82            None => builder.build()?,
83        };
84
85        // Encode the transaction
86        let mut data = vec![MoneyFunction::PoWRewardV1 as u8];
87        debris.params.encode_async(&mut data).await?;
88        let call = ContractCall { contract_id: *MONEY_CONTRACT_ID, data };
89        let mut tx_builder =
90            TransactionBuilder::new(ContractCallLeaf { call, proofs: debris.proofs }, vec![])?;
91        let mut tx = tx_builder.build()?;
92        let sigs = tx.create_sigs(&[wallet.keypair.secret])?;
93        tx.signatures = vec![sigs];
94
95        Ok((tx, debris.params))
96    }
97
98    /// Generate and add an empty block to the given [`Holder`]s blockchains.
99    /// The `miner` holder will produce the block and receive the reward.
100    ///
101    /// Returns any found [`OwnCoin`]s.
102    pub async fn generate_block(
103        &mut self,
104        miner: &Holder,
105        holders: &[Holder],
106    ) -> Result<Vec<OwnCoin>> {
107        // Build the POW reward transaction
108        info!("Building PoWReward transaction for {miner:?}");
109        let (tx, params) = self.pow_reward(miner, None, None, None).await?;
110
111        // Fetch the last block in the blockchain
112        let wallet = self.holders.get(miner).unwrap();
113        let previous = wallet.validator.blockchain.last_block()?;
114
115        // We increment timestamp so we don't have to use sleep
116        let timestamp = previous.header.timestamp.checked_add(1.into())?;
117
118        // Generate block header
119        let header = Header::new(
120            previous.hash(),
121            previous.header.height + 1,
122            timestamp,
123            previous.header.nonce,
124        );
125
126        // Generate the block
127        let mut block = BlockInfo::new_empty(header);
128
129        // Add producer transaction to the block
130        block.append_txs(vec![tx]);
131
132        // Compute block contracts states monotree root
133        let overlay = BlockchainOverlay::new(&wallet.validator.blockchain)?;
134        let _ = apply_producer_transaction(
135            &overlay,
136            block.header.height,
137            wallet.validator.consensus.module.read().await.target,
138            block.txs.last().unwrap(),
139            &mut MerkleTree::new(1),
140        )
141        .await?;
142        block.header.state_root =
143            overlay.lock().unwrap().contracts.get_state_monotree()?.get_headroot()?.unwrap();
144
145        // Attach signature
146        block.sign(&wallet.keypair.secret);
147
148        // For all holders, append the block
149        let mut found_owncoins = vec![];
150        for holder in holders {
151            let wallet = self.holders.get_mut(holder).unwrap();
152            wallet.validator.add_test_blocks(&[block.clone()]).await?;
153            wallet.money_merkle_tree.append(MerkleNode::from(params.output.coin.inner()));
154
155            // Attempt to decrypt the note to see if this is a coin for the holder
156            let Ok(note) = params.output.note.decrypt::<MoneyNote>(&wallet.keypair.secret) else {
157                continue
158            };
159
160            let owncoin = OwnCoin {
161                coin: params.output.coin,
162                note: note.clone(),
163                secret: wallet.keypair.secret,
164                leaf_position: wallet.money_merkle_tree.mark().unwrap(),
165            };
166
167            wallet.unspent_money_coins.push(owncoin.clone());
168            found_owncoins.push(owncoin);
169        }
170
171        Ok(found_owncoins)
172    }
173}