| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- /* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2020-2024 Dyne.org foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- use std::{collections::HashMap, sync::Arc};
- use darkfi::{
- blockchain::{BlockInfo, Header, HeaderHash},
- net::Settings,
- rpc::jsonrpc::JsonSubscriber,
- system::sleep,
- tx::{ContractCallLeaf, TransactionBuilder},
- validator::{consensus::Proposal, Validator, ValidatorConfig},
- zk::{empty_witnesses, ProvingKey, ZkCircuit},
- Result,
- };
- use darkfi_contract_test_harness::vks;
- use darkfi_money_contract::{
- client::pow_reward_v1::PoWRewardCallBuilder, MoneyFunction, MONEY_CONTRACT_ZKAS_MINT_NS_V1,
- };
- use darkfi_sdk::{
- crypto::{Keypair, MONEY_CONTRACT_ID},
- pasta::pallas,
- ContractCall,
- };
- use darkfi_serial::Encodable;
- use num_bigint::BigUint;
- use url::Url;
- use crate::{proto::ProposalMessage, task::sync::sync_task, utils::spawn_p2p, Darkfid};
- pub struct HarnessConfig {
- pub pow_target: u32,
- pub pow_fixed_difficulty: Option<BigUint>,
- pub finalization_threshold: usize,
- pub alice_url: String,
- pub bob_url: String,
- }
- pub struct Harness {
- pub config: HarnessConfig,
- pub vks: Vec<(Vec<u8>, String, Vec<u8>)>,
- pub validator_config: ValidatorConfig,
- pub alice: Darkfid,
- pub bob: Darkfid,
- }
- impl Harness {
- pub async fn new(
- config: HarnessConfig,
- verify_fees: bool,
- ex: &Arc<smol::Executor<'static>>,
- ) -> Result<Self> {
- // Generate default genesis block
- let mut genesis_block = BlockInfo::default();
- // Retrieve genesis producer transaction
- let producer_tx = genesis_block.txs.pop().unwrap();
- // 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
- // don't get circular dependencies.
- let validator_config = ValidatorConfig {
- finalization_threshold: config.finalization_threshold,
- pow_target: config.pow_target,
- pow_fixed_difficulty: config.pow_fixed_difficulty.clone(),
- genesis_block,
- verify_fees,
- };
- // Generate validators using pregenerated vks
- let (_, vks) = vks::get_cached_pks_and_vks()?;
- let mut settings =
- Settings { localnet: true, inbound_connections: 3, ..Default::default() };
- // Alice
- let alice_url = Url::parse(&config.alice_url)?;
- settings.inbound_addrs = vec![alice_url.clone()];
- let alice = generate_node(&vks, &validator_config, &settings, ex, true, true, None).await?;
- // Bob
- let bob_url = Url::parse(&config.bob_url)?;
- settings.inbound_addrs = vec![bob_url];
- settings.peers = vec![alice_url];
- let bob = generate_node(&vks, &validator_config, &settings, ex, true, false, None).await?;
- Ok(Self { config, vks, validator_config, alice, bob })
- }
- pub async fn validate_chains(&self, total_blocks: usize) -> Result<()> {
- let alice = &self.alice.validator;
- let bob = &self.bob.validator;
- alice
- .validate_blockchain(self.config.pow_target, self.config.pow_fixed_difficulty.clone())
- .await?;
- bob.validate_blockchain(self.config.pow_target, self.config.pow_fixed_difficulty.clone())
- .await?;
- let alice_blockchain_len = alice.blockchain.len();
- assert_eq!(alice_blockchain_len, bob.blockchain.len());
- assert_eq!(alice_blockchain_len, total_blocks);
- assert!(alice.blockchain.headers.is_empty_sync());
- assert!(bob.blockchain.headers.is_empty_sync());
- Ok(())
- }
- pub async fn validate_fork_chains(&self, total_forks: usize, fork_sizes: Vec<usize>) {
- let alice = &self.alice.validator.consensus.forks.read().await;
- let bob = &self.bob.validator.consensus.forks.read().await;
- let alice_forks_len = alice.len();
- assert_eq!(alice_forks_len, bob.len());
- assert_eq!(alice_forks_len, total_forks);
- for (index, fork) in alice.iter().enumerate() {
- assert_eq!(fork.proposals.len(), fork_sizes[index]);
- assert_eq!(fork.diffs.len(), fork_sizes[index]);
- }
- for (index, fork) in bob.iter().enumerate() {
- assert_eq!(fork.proposals.len(), fork_sizes[index]);
- assert_eq!(fork.diffs.len(), fork_sizes[index]);
- }
- }
- pub async fn add_blocks(&self, blocks: &[BlockInfo]) -> Result<()> {
- // We append the block as a proposal to Alice,
- // and then we broadcast it to rest nodes
- for block in blocks {
- let proposal = Proposal::new(block.clone());
- self.alice.validator.append_proposal(&proposal).await?;
- let message = ProposalMessage(proposal);
- self.alice.p2p.broadcast(&message).await;
- }
- // Sleep a bit so blocks can be propagated and then
- // trigger finalization check to Alice and Bob
- sleep(10).await;
- self.alice.validator.finalization().await?;
- self.bob.validator.finalization().await?;
- Ok(())
- }
- pub async fn generate_next_block(&self, previous: &BlockInfo) -> Result<BlockInfo> {
- // Next block info
- let block_height = previous.header.height + 1;
- let last_nonce = previous.header.nonce;
- // Generate a producer transaction
- let keypair = Keypair::default();
- let (zkbin, _) = self.alice.validator.blockchain.contracts.get_zkas(
- &self.alice.validator.blockchain.sled_db,
- &MONEY_CONTRACT_ID,
- MONEY_CONTRACT_ZKAS_MINT_NS_V1,
- )?;
- let circuit = ZkCircuit::new(empty_witnesses(&zkbin)?, &zkbin);
- let pk = ProvingKey::build(zkbin.k, &circuit);
- // 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();
- // Build the transaction debris
- let debris = PoWRewardCallBuilder {
- secret: keypair.secret,
- recipient: keypair.public,
- block_height,
- fees: 0,
- spend_hook,
- user_data,
- mint_zkbin: zkbin.clone(),
- mint_pk: pk.clone(),
- }
- .build()?;
- // Generate and sign the actual transaction
- let mut data = vec![MoneyFunction::PoWRewardV1 as u8];
- debris.params.encode(&mut data)?;
- let call = ContractCall { contract_id: *MONEY_CONTRACT_ID, data };
- let mut tx_builder =
- TransactionBuilder::new(ContractCallLeaf { call, proofs: debris.proofs }, vec![])?;
- let mut tx = tx_builder.build()?;
- let sigs = tx.create_sigs(&[keypair.secret])?;
- tx.signatures = vec![sigs];
- // We increment timestamp so we don't have to use sleep
- let timestamp = previous.header.timestamp.checked_add(1.into())?;
- // Generate header
- let header = Header::new(previous.hash(), block_height, timestamp, last_nonce);
- // Generate the block
- let mut block = BlockInfo::new_empty(header);
- // Add producer transaction to the block
- block.append_txs(vec![tx]);
- // Attach signature
- block.sign(&keypair.secret);
- Ok(block)
- }
- }
- // Note: This function should mirror darkfid::main
- pub async fn generate_node(
- vks: &Vec<(Vec<u8>, String, Vec<u8>)>,
- config: &ValidatorConfig,
- settings: &Settings,
- ex: &Arc<smol::Executor<'static>>,
- miner: bool,
- skip_sync: bool,
- checkpoint: Option<(u32, HeaderHash)>,
- ) -> Result<Darkfid> {
- let sled_db = sled::Config::new().temporary(true).open()?;
- vks::inject(&sled_db, vks)?;
- let validator = Validator::new(&sled_db, config.clone()).await?;
- let mut subscribers = HashMap::new();
- subscribers.insert("blocks", JsonSubscriber::new("blockchain.subscribe_blocks"));
- subscribers.insert("txs", JsonSubscriber::new("blockchain.subscribe_txs"));
- subscribers.insert("proposals", JsonSubscriber::new("blockchain.subscribe_proposals"));
- let p2p = spawn_p2p(settings, &validator, &subscribers, ex.clone()).await;
- let node = Darkfid::new(p2p.clone(), validator, miner, 50, subscribers, None).await;
- p2p.start().await?;
- node.validator.consensus.generate_empty_fork().await?;
- if !skip_sync {
- sync_task(&node, checkpoint).await?;
- } else {
- *node.validator.synced.write().await = true;
- }
- node.validator.purge_pending_txs().await?;
- Ok(node)
- }
|