Skip to content

Commit

Permalink
update cucumber mining code
Browse files Browse the repository at this point in the history
  • Loading branch information
sdbondi committed Jan 26, 2024
1 parent 548258a commit 5dbbdce
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 70 deletions.
82 changes: 21 additions & 61 deletions integration_tests/src/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@

use minotari_app_grpc::{
tari_rpc,
tari_rpc::{pow_algo::PowAlgos, NewBlockTemplate, NewBlockTemplateRequest, PowAlgo},
tari_rpc::{pow_algo::PowAlgos, GetIdentityRequest, NewBlockTemplate, NewBlockTemplateRequest, PowAlgo},
};
use minotari_node_grpc_client::BaseNodeGrpcClient;
use tari_common_types::tari_address::TariAddress;
use tari_common::configuration::Network;
use tari_common_types::{tari_address::TariAddress, types::PublicKey};
use tari_core::{
consensus::ConsensusManager,
transactions::{
Expand All @@ -35,6 +36,7 @@ use tari_core::{
transaction_components::{RangeProofType, WalletOutput},
},
};
use tari_crypto::tari_utilities::ByteArray;

use crate::TariWorld;

Expand All @@ -57,14 +59,23 @@ pub fn register_miner_process(world: &mut TariWorld, miner_name: String, base_no
}

pub async fn mine_blocks(world: &mut TariWorld, miner_name: String, num_blocks: u64) {
let miner = world.get_miner(&miner_name);
let mut base_client = create_base_node_client(world, &miner_name).await;
let mut wallet_client = world.get_wallet(&miner.wallet_name).create_client().await;

let wallet_pk = PublicKey::from_canonical_bytes(
&wallet_client
.identify(GetIdentityRequest {})
.await
.unwrap()
.into_inner()
.public_key,
)
.unwrap();
let payment_address = TariAddress::new(wallet_pk, Network::LocalNet);

for _ in 0..num_blocks {
mine_block(world, &mut base_client).await;
// Makes less likely that base layer will fail with
// "Chain storage error: You tried to execute an invalid Database operation: UTXO 248a... was already marked as
// deleted."
// tokio::time::sleep(Duration::from_millis(100)).await;
mine_block(world, &payment_address, &mut base_client).await;
}
}

Expand All @@ -76,13 +87,13 @@ async fn create_base_node_client(world: &TariWorld, miner_name: &String) -> Base
BaseNodeClient::connect(base_node_grpc_url).await.unwrap()
}

async fn mine_block(world: &TariWorld, base_client: &mut BaseNodeClient) {
async fn mine_block(world: &TariWorld, payment_address: &TariAddress, base_client: &mut BaseNodeClient) {
let (block_template, _) = create_block_template_with_coinbase(
base_client,
0,
&world.key_manager,
&world.script_key_id().await,
&world.default_payment_address,
payment_address,
false,
&world.consensus_manager,
)
Expand All @@ -96,65 +107,14 @@ async fn mine_block(world: &TariWorld, base_client: &mut BaseNodeClient) {
.into_inner();
let block = block_result.block.unwrap();

// We don't need to mine, as Localnet blocks have difficulty 1s
// We don't need to mine, as Localnet blocks have difficulty target of 1s
let submit_res = base_client.submit_block(block).await.unwrap().into_inner();
log::info!(
"Block {} successfully mined at height {:?}",
tari_crypto::tari_utilities::hex::to_hex(&submit_res.block_hash),
block_template.header.unwrap().height
);
}

// async fn create_block_template_with_coinbase(
// base_client: &mut BaseNodeClient,
// wallet_client: &mut WalletGrpcClient,
// ) -> NewBlockTemplate {
// // get the block template from the base node
// let template_req = NewBlockTemplateRequest {
// algo: Some(PowAlgo {
// pow_algo: PowAlgos::Sha3x.into(),
// }),
// max_weight: 0,
// };
// let template_res = base_client
// .get_new_block_template(template_req)
// .await
// .unwrap()
// .into_inner();
//
// let NewBlockTemplateResponse {
// new_block_template: Some(mut template),
// miner_data: Some(miner_data),
// ..
// } = template_res
// else {
// panic!("Failed to get block template");
// };
//
// let height = template.header.as_ref().unwrap().height;
// let coinbase_res = wallet_client
// .get_coinbase(GetCoinbaseRequest {
// reward: miner_data.reward,
// fee: miner_data.total_fees,
// height,
// extra: vec![],
// })
// .await
// .unwrap()
// .into_inner();
//
// let mut transaction_body = coinbase_res.transaction.unwrap().body.unwrap();
// let tx_out = transaction_body.outputs.remove(0);
// let tx_kernel = transaction_body.kernels.remove(0);
//
// // add the coinbase outputs and kernels to the block template
// let body = template.body.as_mut().unwrap();
// body.outputs.push(tx_out);
// body.kernels.push(tx_kernel);
//
// template
// }

async fn create_block_template_with_coinbase(
base_client: &mut BaseNodeClient,
weight: u64,
Expand Down
7 changes: 6 additions & 1 deletion integration_tests/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,19 @@ pub async fn spawn_wallet(world: &mut TariWorld, wallet_name: String, base_node_

eprintln!("Using wallet temp_dir: {}", temp_dir.display());

wallet_config.wallet.set_base_path(temp_dir.clone());
wallet_config.wallet.network = Network::LocalNet;
wallet_config.wallet.password = Some("test".into());
wallet_config.wallet.grpc_enabled = true;
wallet_config.wallet.grpc_address =
Some(Multiaddr::from_str(&format!("/ip4/127.0.0.1/tcp/{}", grpc_port)).unwrap());
wallet_config.wallet.data_dir = temp_dir.join("data/wallet");
wallet_config.wallet.db_file = temp_dir.join("db/console_wallet.db");

wallet_config.wallet.contacts_auto_ping_interval = Duration::from_secs(2);
wallet_config
.wallet
.base_node_service_config
.base_node_monitor_max_refresh_interval = Duration::from_secs(15);
wallet_config.wallet.p2p.transport.transport_type = TransportType::Tcp;
wallet_config.wallet.p2p.transport.tcp.listener_address =
Multiaddr::from_str(&format!("/ip4/127.0.0.1/tcp/{}", port)).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions integration_tests/tests/log4rs/cucumber.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,21 @@ loggers:

# Route log events sent to the "core" logger to the "base_layer" appender
c:
level: info
level: debug
appenders:
- base_layer
# Route log events sent to the "wallet" logger to the "base_layer" appender
wallet:
level: info
level: debug
appenders:
- console_wallet
# Route log events sent to the "comms" logger to the "network" appender
comms:
level: info
level: debug
appenders:
- network
# Route log events sent to the "p2p" logger to the "network" appender
p2p:
level: info
level: debug
appenders:
- network
9 changes: 5 additions & 4 deletions integration_tests/tests/steps/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::time::Duration;

use cucumber::{given, when};
use minotari_app_grpc::tari_rpc::GetBalanceRequest;
use minotari_app_grpc::tari_rpc::{GetBalanceRequest, ValidateRequest};
use tari_common_types::types::{Commitment, PrivateKey, PublicKey};
use tari_crypto::{ristretto::RistrettoComSig, tari_utilities::ByteArray};
use tokio::time::sleep;
Expand Down Expand Up @@ -86,6 +86,7 @@ async fn check_balance(world: &mut TariWorld, wallet_name: String, balance: u64,
_ => panic!("Unknown unit {}", units),
};
loop {
let _result = client.validate_all_transactions(ValidateRequest {}).await;
let resp = client.get_balance(GetBalanceRequest {}).await.unwrap().into_inner();
if resp.available_balance >= balance {
break;
Expand All @@ -94,11 +95,11 @@ async fn check_balance(world: &mut TariWorld, wallet_name: String, balance: u64,
"Waiting for wallet {} to have at least {} uT (balance: {} uT, pending: {} uT)",
wallet_name, balance, resp.available_balance, resp.pending_incoming_balance
);
sleep(Duration::from_secs(1)).await;
sleep(Duration::from_secs(5)).await;

if iterations == 40 {
if iterations == 100 {
panic!(
"Wallet {} did not have at least {} uT after 40 seconds (balance: {} uT, pending: {} uT)",
"Wallet {} did not have at least {} uT after 500 seconds (balance: {} uT, pending: {} uT)",
wallet_name, balance, resp.available_balance, resp.pending_incoming_balance
);
}
Expand Down

0 comments on commit 5dbbdce

Please sign in to comment.