Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mem/tests/zmq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tokio_stream::StreamExt as _;

#[tokio::test]
async fn test_stream_zmq_async_receives_broadcast_tx() -> Result<()> {
let env = TestEnv::enable_zmq()?;
let mut env = TestEnv::enable_zmq()?;
// env.start_explorer_in_container()?;

let connect_string = env.zmq_pub_raw_tx_socket().expect("zmq rawtx socket");
Expand Down
32 changes: 16 additions & 16 deletions protocol/tests/protocol_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ use wallet::protocol_wallet_api::MemWallet;

#[test]
fn test_initial_tx_creation() -> anyhow::Result<()> {
let env = TestEnv::new()?;
let mut env = TestEnv::new()?;
// env.start_explorer_in_container()?;
let (_, _) = initial_tx_creation(&env)?;
let (_, _) = initial_tx_creation(&mut env)?;
Ok(())
}

pub fn funded_wallet(env: &TestEnv) -> MemWallet {
pub fn funded_wallet(env: &mut TestEnv) -> MemWallet {
// TODO move this line to TestEnv
let client =
BdkElectrumClient::new(Client::new(&env.electrum_url()).unwrap());
Expand All @@ -36,7 +36,7 @@ pub fn funded_wallet(env: &TestEnv) -> MemWallet {
wallet
}

fn initial_tx_creation(env: &TestEnv) -> anyhow::Result<(BMPProtocol, BMPProtocol)> {
fn initial_tx_creation(env: &mut TestEnv) -> anyhow::Result<(BMPProtocol, BMPProtocol)> {
tracing::debug!("running...");

let alice_funds = funded_wallet(env);
Expand Down Expand Up @@ -84,11 +84,11 @@ fn initial_tx_creation(env: &TestEnv) -> anyhow::Result<(BMPProtocol, BMPProtoco

#[test]
fn test_swap() -> anyhow::Result<()> {
let env = TestEnv::new()?;
let mut env = TestEnv::new()?;
// env.start_explorer_in_container()?;

// create all transaction and Broadcast DepositTx already
let (mut alice, mut bob) = initial_tx_creation(&env)?;
let (mut alice, mut bob) = initial_tx_creation(&mut env)?;
dbg!(alice.swap_tx.unsigned_tx()?);
dbg!(bob.swap_tx.unsigned_tx()?);

Expand All @@ -114,11 +114,11 @@ fn test_swap() -> anyhow::Result<()> {

#[test]
fn test_warning() -> anyhow::Result<()> {
let env = TestEnv::new()?;
let mut env = TestEnv::new()?;
// env.start_explorer_in_container()?;

// create all transaction and Broadcast DepositTx already
let (alice, _bob) = initial_tx_creation(&env)?;
let (alice, _bob) = initial_tx_creation(&mut env)?;
dbg!(alice.warning_tx_me.signed_tx()?);
// alice broadcasts WarningTx
dbg!(alice.warning_tx_me.broadcast(&alice.ctx)?);
Expand All @@ -128,11 +128,11 @@ fn test_warning() -> anyhow::Result<()> {

#[test]
fn test_claim() -> anyhow::Result<()> {
let env = TestEnv::new()?;
let mut env = TestEnv::new()?;
// env.start_explorer_in_container()?;

// create all transaction and Broadcast DepositTx already
let (alice, _bob) = initial_tx_creation(&env)?;
let (alice, _bob) = initial_tx_creation(&mut env)?;
// alice broadcasts WarningTx
alice.warning_tx_me.broadcast(&alice.ctx)?;
env.mine_block()?;
Expand All @@ -157,11 +157,11 @@ fn test_claim() -> anyhow::Result<()> {

#[test]
fn test_claim_too_early() -> anyhow::Result<()> {
let env = TestEnv::new()?;
let mut env = TestEnv::new()?;
// env.start_explorer_in_container()?;

// create all transaction and Broadcast DepositTx already
let (alice, _bob) = initial_tx_creation(&env)?;
let (alice, _bob) = initial_tx_creation(&mut env)?;
alice.warning_tx_me.broadcast(&alice.ctx)?;
// env.mine_block()?;
env.mine_block()?; // we have set time-delay t2 to 2 Blocks
Expand All @@ -183,11 +183,11 @@ fn test_claim_too_early() -> anyhow::Result<()> {

#[test]
fn test_redirect() -> anyhow::Result<()> {
let env = TestEnv::new()?;
let mut env = TestEnv::new()?;
// env.start_explorer_in_container()?;

// create all transaction and Broadcast DepositTx already
let (alice, bob) = initial_tx_creation(&env)?;
let (alice, bob) = initial_tx_creation(&mut env)?;
// alice broadcasts WarningTx
let bob_warn_id = bob.warning_tx_me.broadcast(&bob.ctx)?;
env.mine_block()?;
Expand All @@ -201,11 +201,11 @@ fn test_redirect() -> anyhow::Result<()> {

#[test]
fn test_q_tik() -> anyhow::Result<()> {
let env = TestEnv::new()?;
let mut env = TestEnv::new()?;
// env.start_explorer_in_container()?;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove these comments if not needed anymore?


// create all transaction and Broadcast DepositTx already
let (mut alice, bob) = initial_tx_creation(&env)?;
let (mut alice, bob) = initial_tx_creation(&mut env)?;

// message
let sighash = bob.swap_tx.builder.input_sighash()?;
Expand Down
4 changes: 2 additions & 2 deletions rpc/src/bmp_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ impl BmpProtocolService for BmpServiceImpl {
info!("Received initialize request: {req:?}");

//todo retrieve the actual wallet
let env = TestEnv::new().unwrap(); // TODO move Wallet loading
let mock_wallet = MemWallet::funded_wallet(&env);
let mut env = TestEnv::new().unwrap(); // TODO move Wallet loading
let mock_wallet = MemWallet::funded_wallet(&mut env);
let wallet_service = WalletService::new().load(mock_wallet);

let role =
Expand Down
6 changes: 4 additions & 2 deletions rpc/tests/wallet_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ use rpc::wallet::{TxConfidence, WalletService, WalletServiceImpl};
use testenv::TestEnv;
use tokio::time::{self, Duration};

// TODO fix this test, I guess we need to rewrite it, may be the whole streaming of transaction
// events.
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[ignore = "needs to be fixed"]
async fn test_wallet_service_mine_single_tx() -> Result<()> {
let testenv = TestEnv::new()?;
// testenv.start_explorer_in_container()?;
let mut testenv = TestEnv::new()?;

let rpc_client = testenv.bitcoin_core_rpc_client()?;

Expand Down
32 changes: 23 additions & 9 deletions testenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct TestEnv {
container_name: Option<String>,
explorer_port: Option<u16>,
bitcoin_rpc_pwd: String,
mempool: Vec<Txid>,
}

/// Configuration parameters.
Expand Down Expand Up @@ -259,14 +260,16 @@ impl TestEnv {
container_name: None,
explorer_port: None,
bitcoin_rpc_pwd,
mempool: Vec::new(),
};
tracing::info!("Bitcoin regtest environment ready!");
Ok(test_env)
}

pub fn broadcast(&self, tx: &Transaction) -> Result<Txid> {
pub fn broadcast(&mut self, tx: &Transaction) -> Result<Txid> {
let txid = self.bdk_electrum_client.transaction_broadcast(tx)?;
let _ = self.wait_for_tx(txid);
self.mempool.push(txid);
Ok(txid)
}

Expand Down Expand Up @@ -380,12 +383,19 @@ impl TestEnv {
}

/// Mine blocks using bitcoind RPC
pub fn mine_blocks(&self, count: usize) -> Result<Vec<BlockHash>> {
pub fn mine_blocks(&mut self, count: usize) -> Result<Vec<BlockHash>> {
let block_hashes = self
.bitcoind
.client
.generate_to_address(count, &self.new_address()?)?;

self.wait_for_block()?;

for txid in self.mempool.iter() {
let _ = self.wait_for_tx(*txid);
}
self.mempool.clear();

// Convert to BlockHash format
block_hashes
.0
Expand All @@ -395,21 +405,24 @@ impl TestEnv {
}

/// Mine a single block
pub fn mine_block(&self) -> Result<BlockHash> {
pub fn mine_block(&mut self) -> Result<BlockHash> {
let hashes = self.mine_blocks(1)?;
self.wait_for_block()?;
Ok(hashes[0])
}

pub fn fund_from_prv_key(&self, key: &Scalar, amount: Amount) -> Result<Txid> {
pub fn fund_from_prv_key(&mut self, key: &Scalar, amount: Amount) -> Result<Txid> {
let xonly_pubkey = key.base_point_mul().serialize_xonly();
let pbk = XOnlyPublicKey::from_slice(&xonly_pubkey)?;
let address = Address::p2tr(&self.ctx, pbk, None, KnownHrp::Regtest);
self.fund_address(&address, amount)
}

/// Fund an address using bitcoind RPC
pub fn fund_address(&self, address: &Address<NetworkChecked>, amount: Amount) -> Result<Txid> {
pub fn fund_address(
&mut self,
address: &Address<NetworkChecked>,
amount: Amount,
) -> Result<Txid> {
// First ensure we have some coins by mining blocks if needed
let balance = self.bitcoind.client.get_balance()?.balance()?;

Expand All @@ -429,6 +442,7 @@ impl TestEnv {
.client
.send_to_address(address, amount)?
.txid()?;
self.mempool.push(txid);
Ok(txid)
}

Expand Down Expand Up @@ -585,7 +599,7 @@ mod tests {

#[test]
fn test_mining() -> Result<()> {
let env = TestEnv::new()?;
let mut env = TestEnv::new()?;
let initial_count = env.block_count()?;

env.mine_block()?;
Expand All @@ -598,7 +612,7 @@ mod tests {

#[test]
fn test_address_operations() -> Result<()> {
let env = TestEnv::new()?;
let mut env = TestEnv::new()?;

// Create new address
let address = env.new_address()?;
Expand Down Expand Up @@ -661,7 +675,7 @@ mod tests {

#[test]
fn test_enable_zmq() -> Result<()> {
let env = TestEnv::enable_zmq()?;
let mut env = TestEnv::enable_zmq()?;

// Verify ZMQ sockets were assigned
let tx_socket = env.zmq_pub_raw_tx_socket().expect("zmq rawtx socket");
Expand Down
3 changes: 1 addition & 2 deletions wallet/src/protocol_wallet_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl MemWallet {
self.wallet.next_unused_address(KeychainKind::External)
}

pub fn funded_wallet(env: &testenv::TestEnv) -> Self {
pub fn funded_wallet(env: &mut testenv::TestEnv) -> Self {
let client = BdkElectrumClient::new(Client::new(&env.electrum_url()).unwrap());
let mut wallet = Self::new(client).unwrap();
let address = wallet.next_unused_address();
Expand All @@ -220,4 +220,3 @@ pub enum WalletErrorKind {
NotTaprootAddress,
ConversionError(#[from] ConversionError),
}

Loading
Loading