miner.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. use darkfi::{
  19. blockchain::{BlockInfo, Header},
  20. rpc::{jsonrpc::JsonNotification, util::JsonValue},
  21. system::{ExecutorPtr, StoppableTask, Subscription},
  22. tx::{ContractCallLeaf, Transaction, TransactionBuilder},
  23. util::{encoding::base64, time::Timestamp},
  24. validator::{
  25. consensus::{Fork, Proposal},
  26. utils::best_fork_index,
  27. },
  28. zk::{empty_witnesses, ProvingKey, ZkCircuit},
  29. zkas::ZkBinary,
  30. Error, Result,
  31. };
  32. use darkfi_money_contract::{
  33. client::pow_reward_v1::PoWRewardCallBuilder, MoneyFunction, MONEY_CONTRACT_ZKAS_MINT_NS_V1,
  34. };
  35. use darkfi_sdk::{
  36. crypto::{poseidon_hash, FuncId, PublicKey, SecretKey, MONEY_CONTRACT_ID},
  37. pasta::pallas,
  38. ContractCall,
  39. };
  40. use darkfi_serial::{serialize_async, Encodable};
  41. use log::{error, info};
  42. use num_bigint::BigUint;
  43. use rand::rngs::OsRng;
  44. use smol::channel::{Receiver, Sender};
  45. use crate::{proto::ProposalMessage, task::garbage_collect_task, DarkfiNodePtr};
  46. /// Auxiliary structure representing node miner rewards recipient configuration
  47. pub struct MinerRewardsRecipientConfig {
  48. pub recipient: PublicKey,
  49. pub spend_hook: Option<FuncId>,
  50. pub user_data: Option<pallas::Base>,
  51. }
  52. /// Async task used for participating in the PoW block production.
  53. ///
  54. /// Miner initializes their setup and waits for next confirmation,
  55. /// by listenning for new proposals from the network, for optimal
  56. /// conditions. After confirmation occurs, they start the actual
  57. /// miner loop, where they first grab the best ranking fork to extend,
  58. /// and start mining procedure for its next block. Additionally, they
  59. /// listen to the network for new proposals, and check if these
  60. /// proposals produce a new best ranking fork. If they do, the stop
  61. /// mining. These two tasks run in parallel, and after one of them
  62. /// finishes, node triggers confirmation check.
  63. pub async fn miner_task(
  64. node: &DarkfiNodePtr,
  65. recipient_config: &MinerRewardsRecipientConfig,
  66. skip_sync: bool,
  67. ex: &ExecutorPtr,
  68. ) -> Result<()> {
  69. // Initialize miner configuration
  70. info!(target: "darkfid::task::miner_task", "Starting miner task...");
  71. // Grab zkas proving keys and bin for PoWReward transaction
  72. info!(target: "darkfid::task::miner_task", "Generating zkas bin and proving keys...");
  73. let (zkbin, _) = node.validator.blockchain.contracts.get_zkas(
  74. &node.validator.blockchain.sled_db,
  75. &MONEY_CONTRACT_ID,
  76. MONEY_CONTRACT_ZKAS_MINT_NS_V1,
  77. )?;
  78. let circuit = ZkCircuit::new(empty_witnesses(&zkbin)?, &zkbin);
  79. let pk = ProvingKey::build(zkbin.k, &circuit);
  80. // Generate a random master secret key, to derive all signing keys from.
  81. // This enables us to deanonimize proposals from reward recipient(miner).
  82. // TODO: maybe miner wants to keep this master secret so they can
  83. // verify their signature in the future?
  84. info!(target: "darkfid::task::miner_task", "Generating signing key...");
  85. let mut secret = SecretKey::random(&mut OsRng);
  86. // Grab blocks subscriber
  87. let block_sub = node.subscribers.get("blocks").unwrap();
  88. // Grab proposals subscriber and subscribe to it
  89. let proposals_sub = node.subscribers.get("proposals").unwrap();
  90. let subscription = proposals_sub.publisher.clone().subscribe().await;
  91. // Listen for blocks until next confirmation, for optimal conditions
  92. if !skip_sync {
  93. info!(target: "darkfid::task::miner_task", "Waiting for next confirmation...");
  94. loop {
  95. subscription.receive().await;
  96. // Check if we can confirmation anything and broadcast them
  97. let confirmed = node.validator.confirmation().await?;
  98. if confirmed.is_empty() {
  99. continue
  100. }
  101. let mut notif_blocks = Vec::with_capacity(confirmed.len());
  102. for block in confirmed {
  103. notif_blocks
  104. .push(JsonValue::String(base64::encode(&serialize_async(&block).await)));
  105. }
  106. block_sub.notify(JsonValue::Array(notif_blocks)).await;
  107. break;
  108. }
  109. }
  110. // Create channels so threads can signal each other
  111. let (sender, stop_signal) = smol::channel::bounded(1);
  112. // Create the garbage collection task using a dummy task
  113. let gc_task = StoppableTask::new();
  114. gc_task.clone().start(
  115. async { Ok(()) },
  116. |_| async { /* Do nothing */ },
  117. Error::GarbageCollectionTaskStopped,
  118. ex.clone(),
  119. );
  120. info!(target: "darkfid::task::miner_task", "Miner initialized successfully!");
  121. // Start miner loop
  122. loop {
  123. // Grab best current fork
  124. let forks = node.validator.consensus.forks.read().await;
  125. let index = match best_fork_index(&forks) {
  126. Ok(i) => i,
  127. Err(e) => {
  128. error!(
  129. target: "darkfid::task::miner_task",
  130. "Finding best fork index failed: {e}"
  131. );
  132. continue
  133. }
  134. };
  135. let extended_fork = match forks[index].full_clone() {
  136. Ok(f) => f,
  137. Err(e) => {
  138. error!(
  139. target: "darkfid::task::miner_task",
  140. "Fork full clone creation failed: {e}"
  141. );
  142. continue
  143. }
  144. };
  145. drop(forks);
  146. // Start listenning for network proposals and mining next block for best fork.
  147. match smol::future::or(
  148. listen_to_network(node, &extended_fork, &subscription, &sender),
  149. mine(
  150. node,
  151. &extended_fork,
  152. &mut secret,
  153. recipient_config,
  154. &zkbin,
  155. &pk,
  156. &stop_signal,
  157. skip_sync,
  158. ),
  159. )
  160. .await
  161. {
  162. Ok(_) => { /* Do nothing */ }
  163. Err(Error::NetworkNotConnected) => {
  164. error!(target: "darkfid::task::miner_task", "Node disconnected from the network");
  165. subscription.unsubscribe().await;
  166. return Err(Error::NetworkNotConnected)
  167. }
  168. Err(e) => {
  169. error!(
  170. target: "darkfid::task::miner_task",
  171. "Error during listen_to_network() or mine(): {e}"
  172. );
  173. continue
  174. }
  175. }
  176. // Check if we can confirm anything and broadcast them
  177. let confirmed = match node.validator.confirmation().await {
  178. Ok(f) => f,
  179. Err(e) => {
  180. error!(
  181. target: "darkfid::task::miner_task",
  182. "Confirmation failed: {e}"
  183. );
  184. continue
  185. }
  186. };
  187. if confirmed.is_empty() {
  188. continue
  189. }
  190. let mut notif_blocks = Vec::with_capacity(confirmed.len());
  191. for block in confirmed {
  192. notif_blocks.push(JsonValue::String(base64::encode(&serialize_async(&block).await)));
  193. }
  194. block_sub.notify(JsonValue::Array(notif_blocks)).await;
  195. // Invoke the detached garbage collection task
  196. gc_task.clone().stop().await;
  197. gc_task.clone().start(
  198. garbage_collect_task(node.clone()),
  199. |res| async {
  200. match res {
  201. Ok(()) | Err(Error::GarbageCollectionTaskStopped) => { /* Do nothing */ }
  202. Err(e) => {
  203. error!(target: "darkfid", "Failed starting garbage collection task: {}", e)
  204. }
  205. }
  206. },
  207. Error::GarbageCollectionTaskStopped,
  208. ex.clone(),
  209. );
  210. }
  211. }
  212. /// Async task to listen for incoming proposals and check if the best fork has changed.
  213. async fn listen_to_network(
  214. node: &DarkfiNodePtr,
  215. extended_fork: &Fork,
  216. subscription: &Subscription<JsonNotification>,
  217. sender: &Sender<()>,
  218. ) -> Result<()> {
  219. // Grab extended fork last proposal hash
  220. let last_proposal_hash = extended_fork.last_proposal()?.hash;
  221. loop {
  222. // Wait until a new proposal has been received
  223. subscription.receive().await;
  224. // Grab a lock over node forks
  225. let forks = node.validator.consensus.forks.read().await;
  226. // Grab best current fork index
  227. let index = best_fork_index(&forks)?;
  228. // Verify if proposals sequence has changed
  229. if forks[index].last_proposal()?.hash != last_proposal_hash {
  230. drop(forks);
  231. break
  232. }
  233. drop(forks);
  234. }
  235. // Signal miner to abort mining
  236. sender.send(()).await?;
  237. if let Err(e) = node.miner_daemon_request("abort", &JsonValue::Array(vec![])).await {
  238. error!(target: "darkfid::task::miner::listen_to_network", "Failed to execute miner daemon abort request: {}", e);
  239. }
  240. Ok(())
  241. }
  242. /// Async task to generate and mine provided fork index next block,
  243. /// while listening for a stop signal.
  244. #[allow(clippy::too_many_arguments)]
  245. async fn mine(
  246. node: &DarkfiNodePtr,
  247. extended_fork: &Fork,
  248. secret: &mut SecretKey,
  249. recipient_config: &MinerRewardsRecipientConfig,
  250. zkbin: &ZkBinary,
  251. pk: &ProvingKey,
  252. stop_signal: &Receiver<()>,
  253. skip_sync: bool,
  254. ) -> Result<()> {
  255. smol::future::or(
  256. wait_stop_signal(stop_signal),
  257. mine_next_block(node, extended_fork, secret, recipient_config, zkbin, pk, skip_sync),
  258. )
  259. .await
  260. }
  261. /// Async task to wait for listener's stop signal.
  262. pub async fn wait_stop_signal(stop_signal: &Receiver<()>) -> Result<()> {
  263. // Clean stop signal channel
  264. if stop_signal.is_full() {
  265. stop_signal.recv().await?;
  266. }
  267. // Wait for listener signal
  268. stop_signal.recv().await?;
  269. Ok(())
  270. }
  271. /// Async task to generate and mine provided fork index next block.
  272. async fn mine_next_block(
  273. node: &DarkfiNodePtr,
  274. extended_fork: &Fork,
  275. secret: &mut SecretKey,
  276. recipient_config: &MinerRewardsRecipientConfig,
  277. zkbin: &ZkBinary,
  278. pk: &ProvingKey,
  279. skip_sync: bool,
  280. ) -> Result<()> {
  281. // Grab next target and block
  282. let (next_target, mut next_block) = generate_next_block(
  283. extended_fork,
  284. secret,
  285. recipient_config,
  286. zkbin,
  287. pk,
  288. node.validator.consensus.module.read().await.target,
  289. node.validator.verify_fees,
  290. )
  291. .await?;
  292. // Execute request to minerd and parse response
  293. let target = JsonValue::String(next_target.to_string());
  294. let block = JsonValue::String(base64::encode(&serialize_async(&next_block).await));
  295. let response =
  296. node.miner_daemon_request_with_retry("mine", &JsonValue::Array(vec![target, block])).await;
  297. next_block.header.nonce = *response.get::<f64>().unwrap() as u64;
  298. // Sign the mined block
  299. next_block.sign(secret);
  300. // Verify it
  301. extended_fork.module.verify_current_block(&next_block)?;
  302. // Check if we are connected to the network
  303. if !skip_sync && !node.p2p_handler.p2p.is_connected() {
  304. return Err(Error::NetworkNotConnected)
  305. }
  306. // Append the mined block as a proposal
  307. let proposal = Proposal::new(next_block);
  308. node.validator.append_proposal(&proposal).await?;
  309. // Broadcast proposal to the network
  310. let message = ProposalMessage(proposal);
  311. node.p2p_handler.p2p.broadcast(&message).await;
  312. Ok(())
  313. }
  314. /// Auxiliary function to generate next block in an atomic manner.
  315. async fn generate_next_block(
  316. extended_fork: &Fork,
  317. secret: &mut SecretKey,
  318. recipient_config: &MinerRewardsRecipientConfig,
  319. zkbin: &ZkBinary,
  320. pk: &ProvingKey,
  321. block_target: u32,
  322. verify_fees: bool,
  323. ) -> Result<(BigUint, BlockInfo)> {
  324. // Grab forks' last block proposal(previous)
  325. let last_proposal = extended_fork.last_proposal()?;
  326. // Grab forks' next block height
  327. let next_block_height = last_proposal.block.header.height + 1;
  328. // Grab forks' unproposed transactions
  329. let (mut txs, _, fees) = extended_fork
  330. .unproposed_txs(&extended_fork.blockchain, next_block_height, block_target, verify_fees)
  331. .await?;
  332. // We are deriving the next secret key for optimization.
  333. // Next secret is the poseidon hash of:
  334. // [prefix, current(previous) secret, signing(block) height].
  335. let prefix = pallas::Base::from_raw([4, 0, 0, 0]);
  336. let next_secret = poseidon_hash([prefix, secret.inner(), (next_block_height as u64).into()]);
  337. *secret = SecretKey::from(next_secret);
  338. // Generate reward transaction
  339. let tx = generate_transaction(next_block_height, fees, secret, recipient_config, zkbin, pk)?;
  340. txs.push(tx);
  341. // Generate the new header
  342. let header = Header::new(last_proposal.hash, next_block_height, Timestamp::current_time(), 0);
  343. // Generate the block
  344. let mut next_block = BlockInfo::new_empty(header);
  345. // Add transactions to the block
  346. next_block.append_txs(txs);
  347. // Grab the next mine target
  348. let target = extended_fork.module.next_mine_target()?;
  349. Ok((target, next_block))
  350. }
  351. /// Auxiliary function to generate a Money::PoWReward transaction.
  352. fn generate_transaction(
  353. block_height: u32,
  354. fees: u64,
  355. secret: &SecretKey,
  356. recipient_config: &MinerRewardsRecipientConfig,
  357. zkbin: &ZkBinary,
  358. pk: &ProvingKey,
  359. ) -> Result<Transaction> {
  360. // Build the transaction debris
  361. let debris = PoWRewardCallBuilder {
  362. signature_public: PublicKey::from_secret(*secret),
  363. block_height,
  364. fees,
  365. recipient: Some(recipient_config.recipient),
  366. spend_hook: recipient_config.spend_hook,
  367. user_data: recipient_config.user_data,
  368. mint_zkbin: zkbin.clone(),
  369. mint_pk: pk.clone(),
  370. }
  371. .build()?;
  372. // Generate and sign the actual transaction
  373. let mut data = vec![MoneyFunction::PoWRewardV1 as u8];
  374. debris.params.encode(&mut data)?;
  375. let call = ContractCall { contract_id: *MONEY_CONTRACT_ID, data };
  376. let mut tx_builder =
  377. TransactionBuilder::new(ContractCallLeaf { call, proofs: debris.proofs }, vec![])?;
  378. let mut tx = tx_builder.build()?;
  379. let sigs = tx.create_sigs(&[*secret])?;
  380. tx.signatures = vec![sigs];
  381. Ok(tx)
  382. }