diff --git a/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/BitcoindMineInitialRegtestBlocksIntegrationTest.java b/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/BitcoindMineInitialRegtestBlocksIntegrationTest.java deleted file mode 100644 index ef579bc75a..0000000000 --- a/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/BitcoindMineInitialRegtestBlocksIntegrationTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq 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. - * - * Bisq 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 Bisq. If not, see . - */ - -package bisq.wallets.bitcoind; - -import bisq.wallets.bitcoind.regtest.BitcoindExtension; -import bisq.wallets.bitcoind.rpc.BitcoindDaemon; -import bisq.wallets.bitcoind.rpc.BitcoindWallet; -import bisq.wallets.core.model.AddressType; -import bisq.wallets.regtest.bitcoind.BitcoindRegtestSetup; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; - -import java.util.List; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import static org.assertj.core.api.Assertions.assertThat; - -@ExtendWith(BitcoindExtension.class) -public class BitcoindMineInitialRegtestBlocksIntegrationTest { - - private final BitcoindRegtestSetup regtestSetup; - private final BitcoindDaemon daemon; - private final BitcoindWallet minerWallet; - - private double startBalance; - - public BitcoindMineInitialRegtestBlocksIntegrationTest(BitcoindRegtestSetup regtestSetup) { - this.regtestSetup = regtestSetup; - this.daemon = regtestSetup.getDaemon(); - this.minerWallet = regtestSetup.getMinerWallet(); - } - - @BeforeEach - void setUp() { - startBalance = minerWallet.getBalance(); - } - - @Test - public void mineInitialRegtestBlocks() throws InterruptedException { - String address = minerWallet.getNewAddress(AddressType.BECH32, ""); - List blockHashes = daemon.generateToAddress(101, address); - - CountDownLatch nBlocksMinedLatch = regtestSetup.waitUntilBlocksMined(blockHashes); - boolean allBlocksMined = nBlocksMinedLatch.await(1, TimeUnit.MINUTES); - assertThat(allBlocksMined).isTrue(); - - // Other tests could've mined some blocks already. - assertThat(getMinerBalance()) - .isGreaterThanOrEqualTo(50); - } - - private double getMinerBalance() { - return minerWallet.getBalance() - startBalance; - } -} diff --git a/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/BitcoindPsbtMultiSigIntegrationTests.java b/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/BitcoindPsbtMultiSigIntegrationTests.java index bda2507a42..92584887b5 100644 --- a/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/BitcoindPsbtMultiSigIntegrationTests.java +++ b/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/BitcoindPsbtMultiSigIntegrationTests.java @@ -29,7 +29,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import java.net.MalformedURLException; import java.util.Collections; import java.util.List; import java.util.Map; @@ -53,7 +52,7 @@ public BitcoindPsbtMultiSigIntegrationTests(BitcoindRegtestSetup regtestSetup) { } @Test - public void psbtMultiSigTest() throws MalformedURLException, InterruptedException { + public void psbtMultiSigTest() throws InterruptedException { var aliceWallet = regtestSetup.createAndInitializeNewWallet("alice_wallet"); var bobWallet = regtestSetup.createAndInitializeNewWallet("bob_wallet"); var charlieWallet = regtestSetup.createAndInitializeNewWallet("charlie_wallet"); diff --git a/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/BitcoindSendUnconfirmedTxIntegrationTests.java b/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/BitcoindSendUnconfirmedTxIntegrationTests.java deleted file mode 100644 index 777811bb46..0000000000 --- a/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/BitcoindSendUnconfirmedTxIntegrationTests.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq 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. - * - * Bisq 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 Bisq. If not, see . - */ - -package bisq.wallets.bitcoind; - -import bisq.wallets.bitcoind.regtest.BitcoindExtension; -import bisq.wallets.bitcoind.rpc.BitcoindWallet; -import bisq.wallets.bitcoind.zmq.ZmqListeners; -import bisq.wallets.core.model.AddressType; -import bisq.wallets.regtest.bitcoind.BitcoindRegtestSetup; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; -import org.junit.jupiter.api.extension.ExtendWith; - -import java.io.IOException; -import java.util.Optional; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -import static org.assertj.core.api.Assertions.assertThat; - -@ExtendWith(BitcoindExtension.class) -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -public class BitcoindSendUnconfirmedTxIntegrationTests { - - private final BitcoindRegtestSetup regtestSetup; - private final ZmqListeners zmqListeners; - private final BitcoindWallet minerWallet; - - private BitcoindWallet receiverWallet; - - public BitcoindSendUnconfirmedTxIntegrationTests(BitcoindRegtestSetup regtestSetup) { - this.regtestSetup = regtestSetup; - this.zmqListeners = regtestSetup.getZmqListeners(); - this.minerWallet = regtestSetup.getMinerWallet(); - } - - @BeforeAll - public void start() throws IOException { - receiverWallet = regtestSetup.createAndInitializeNewWallet("receiver_wallet_send_check_unconfirmed_balance"); - } - - @Test - public void sendBtcAndCheckIfUnconfirmedBalanceIncluded() throws InterruptedException { - CountDownLatch didReceiveNotificationLatch = new CountDownLatch(1); - - String receiverAddress = receiverWallet.getNewAddress(AddressType.BECH32, ""); - zmqListeners.registerTxOutputAddressesListener(outputAddresses -> { - if (outputAddresses.contains(receiverAddress)) { - didReceiveNotificationLatch.countDown(); - } - }); - - minerWallet.sendToAddress(Optional.of(BitcoindRegtestSetup.WALLET_PASSPHRASE), receiverAddress, 1); - - boolean await = didReceiveNotificationLatch.await(1, TimeUnit.MINUTES); - if (!await) { - throw new IllegalStateException("Didn't receive ZMQ notification after 1 minute."); - } - - // Bitcoin Core needs a while until it updates the balance - double balance; - for (int i = 0; i < 10; i++) { - balance = receiverWallet.getBalance(); - if (balance == 1) { - break; - } - - Thread.sleep(200); - } - - assertThat(receiverWallet.getBalance()) - .isEqualTo(1); - } -} diff --git a/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/zmq/BitcoindZeroMqBlockHashIntegrationIntegrationTests.java b/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/zmq/BitcoindZeroMqBlockHashIntegrationIntegrationTests.java index 4db5689f32..33a7e61c0c 100644 --- a/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/zmq/BitcoindZeroMqBlockHashIntegrationIntegrationTests.java +++ b/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/zmq/BitcoindZeroMqBlockHashIntegrationIntegrationTests.java @@ -24,8 +24,6 @@ import org.junit.jupiter.api.extension.ExtendWith; import java.util.List; -import java.util.Set; -import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -34,7 +32,6 @@ public class BitcoindZeroMqBlockHashIntegrationIntegrationTests { private final BitcoindRegtestSetup regtestSetup; - private final Set minedBlockHashes = new CopyOnWriteArraySet<>(); private final CountDownLatch listenerReceivedBlockHashLatch = new CountDownLatch(1); public BitcoindZeroMqBlockHashIntegrationIntegrationTests(BitcoindRegtestSetup regtestSetup) { @@ -46,43 +43,36 @@ void blockHashNotification() throws InterruptedException { ZmqListeners zmqListeners = regtestSetup.getZmqListeners(); zmqListeners.registerNewBlockMinedListener((blockHash) -> { log.info("Notification: New block with hash {}", blockHash); - - if (minedBlockHashes.contains(blockHash)) { - listenerReceivedBlockHashLatch.countDown(); - } else { - minedBlockHashes.add(blockHash); - } + listenerReceivedBlockHashLatch.countDown(); }); - createAndStartDaemonThread(() -> { + Thread thread = new Thread(() -> { while (true) { try { List blockHashes = regtestSetup.mineOneBlock(); log.info("Mined block: {}", blockHashes); - for (String blockHash : blockHashes) { - if (minedBlockHashes.contains(blockHash)) { - listenerReceivedBlockHashLatch.countDown(); - return; - } else { - minedBlockHashes.add(blockHash); - } + if (Thread.interrupted()) { + break; } + + log.info("Sleeping for 200ms before mining next block."); + //noinspection BusyWait + Thread.sleep(200); + } catch (InterruptedException e) { throw new RuntimeException(e); } } }); + thread.start(); + boolean await = listenerReceivedBlockHashLatch.await(1, TimeUnit.MINUTES); + thread.interrupt(); + if (!await) { throw new IllegalStateException("Didn't connect to bitcoind after 1 minute."); } } - - private void createAndStartDaemonThread(Runnable runnable) { - Thread thread = new Thread(runnable); - thread.setDaemon(true); - thread.start(); - } } diff --git a/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/zmq/BitcoindZeroMqRawTxIntegrationTests.java b/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/zmq/BitcoindZeroMqRawTxIntegrationTests.java deleted file mode 100644 index db89c58bc5..0000000000 --- a/wallets/bitcoind/src/integrationTest/java/bisq/wallets/bitcoind/zmq/BitcoindZeroMqRawTxIntegrationTests.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq 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. - * - * Bisq 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 Bisq. If not, see . - */ - -package bisq.wallets.bitcoind.zmq; - -import bisq.wallets.bitcoind.regtest.BitcoindExtension; -import bisq.wallets.bitcoind.rpc.BitcoindWallet; -import bisq.wallets.core.model.AddressType; -import bisq.wallets.regtest.bitcoind.BitcoindRegtestSetup; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; -import org.junit.jupiter.api.extension.ExtendWith; - -import java.io.IOException; -import java.util.Optional; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -@ExtendWith(BitcoindExtension.class) -@TestInstance(TestInstance.Lifecycle.PER_CLASS) -public class BitcoindZeroMqRawTxIntegrationTests { - - private final BitcoindRegtestSetup regtestSetup; - private final ZmqListeners zmqListeners; - private final BitcoindWallet minerWallet; - private BitcoindWallet receiverWallet; - - public BitcoindZeroMqRawTxIntegrationTests(BitcoindRegtestSetup regtestSetup) { - this.regtestSetup = regtestSetup; - zmqListeners = regtestSetup.getZmqListeners(); - minerWallet = regtestSetup.getMinerWallet(); - } - - @BeforeAll - public void start() throws IOException { - receiverWallet = regtestSetup.createAndInitializeNewWallet("receiver_wallet"); - } - - @Test - void detectReceiverAddress() throws InterruptedException { - CountDownLatch didReceiveNotificationLatch = new CountDownLatch(1); - - String receiverAddress = receiverWallet.getNewAddress(AddressType.BECH32, ""); - zmqListeners.registerTxOutputAddressesListener(outputAddresses -> { - if (outputAddresses.contains(receiverAddress)) { - didReceiveNotificationLatch.countDown(); - } - }); - - minerWallet.sendToAddress(Optional.of(BitcoindRegtestSetup.WALLET_PASSPHRASE), receiverAddress, 1); - - boolean await = didReceiveNotificationLatch.await(1, TimeUnit.MINUTES); - if (!await) { - throw new IllegalStateException("Didn't receive ZMQ notification after 1 minute."); - } - } - - @Test - void detectWhetherMyInputInTx() throws InterruptedException { - CountDownLatch didReceiveNotificationLatch = new CountDownLatch(1); - - String receiverAddress = receiverWallet.getNewAddress(AddressType.BECH32, ""); - String myTxId = minerWallet.sendToAddress(Optional.of(BitcoindRegtestSetup.WALLET_PASSPHRASE), receiverAddress, 2); - - zmqListeners.registerTransactionIdInInputListener(txId -> { - if (txId.equals(myTxId)) { - didReceiveNotificationLatch.countDown(); - } - }); - - regtestSetup.mineOneBlock(); - - String someAddress = minerWallet.getNewAddress(AddressType.BECH32, ""); - for (int i = 0; i < 10; i++) { - receiverWallet.sendToAddress(Optional.of(BitcoindRegtestSetup.WALLET_PASSPHRASE), someAddress, 0.001); - } - - boolean await = didReceiveNotificationLatch.await(1, TimeUnit.MINUTES); - if (!await) { - throw new IllegalStateException("Didn't receive ZMQ notification after 1 minute."); - } - } -} diff --git a/wallets/regtest/src/main/java/bisq/wallets/regtest/bitcoind/BitcoindRegtestBlockMiner.java b/wallets/regtest/src/main/java/bisq/wallets/regtest/bitcoind/BitcoindRegtestBlockMiner.java deleted file mode 100644 index cd936a2830..0000000000 --- a/wallets/regtest/src/main/java/bisq/wallets/regtest/bitcoind/BitcoindRegtestBlockMiner.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * This file is part of Bisq. - * - * Bisq 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. - * - * Bisq 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 Bisq. If not, see . - */ - -package bisq.wallets.regtest.bitcoind; - -import bisq.wallets.bitcoind.rpc.BitcoindDaemon; -import bisq.wallets.bitcoind.rpc.BitcoindWallet; -import bisq.wallets.bitcoind.zmq.ZmqListeners; -import bisq.wallets.bitcoind.zmq.listeners.NewBlockMinedListener; -import bisq.wallets.core.model.AddressType; -import bisq.wallets.process.BisqProcess; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -public class BitcoindRegtestBlockMiner implements BisqProcess { - private final BitcoindDaemon daemon; - private final BitcoindWallet minerWallet; - private final ZmqListeners zmqListeners; - - private final Set minedBlockHashes = ConcurrentHashMap.newKeySet(); - private final NewBlockMinedListener newBlockMinedListener = minedBlockHashes::add; - - public BitcoindRegtestBlockMiner(BitcoindDaemon daemon, BitcoindWallet minerWallet, ZmqListeners zmqListeners) { - this.daemon = daemon; - this.minerWallet = minerWallet; - this.zmqListeners = zmqListeners; - } - - @Override - public void start() { - zmqListeners.registerNewBlockMinedListener(newBlockMinedListener); - } - - @Override - public void shutdown() { - zmqListeners.unregisterNewBlockMinedListener(newBlockMinedListener); - } - - public List mineBlocks(int numberOfBlocks) throws InterruptedException { - String minerAddress = minerWallet.getNewAddress(AddressType.BECH32, ""); - List blockHashes = daemon.generateToAddress(numberOfBlocks, minerAddress); - - CountDownLatch blocksMinedLatch = waitUntilBlocksMined(blockHashes); - boolean allBlocksMined = blocksMinedLatch.await(15, TimeUnit.SECONDS); - if (!allBlocksMined) { - throw new IllegalStateException("Couldn't mine " + numberOfBlocks + " blocks"); - } - return blockHashes; - } - - public CountDownLatch waitUntilBlocksMined(List frozenBlockHashList) { - List blockHashes = new ArrayList<>(frozenBlockHashList); - CountDownLatch blocksMinedLatch = new CountDownLatch(1); - new Thread(() -> { - Set foundHashes = new HashSet<>(); - - while (!blockHashes.isEmpty()) { - blockHashes.stream() - .filter(minedBlockHashes::contains) - .forEach(hash -> { - foundHashes.add(hash); - minedBlockHashes.remove(hash); - }); - - foundHashes.forEach(blockHashes::remove); - foundHashes.clear(); - } - - blocksMinedLatch.countDown(); - }).start(); - return blocksMinedLatch; - } -} diff --git a/wallets/regtest/src/main/java/bisq/wallets/regtest/bitcoind/BitcoindRegtestSetup.java b/wallets/regtest/src/main/java/bisq/wallets/regtest/bitcoind/BitcoindRegtestSetup.java index 9842314709..a77221171e 100644 --- a/wallets/regtest/src/main/java/bisq/wallets/regtest/bitcoind/BitcoindRegtestSetup.java +++ b/wallets/regtest/src/main/java/bisq/wallets/regtest/bitcoind/BitcoindRegtestSetup.java @@ -32,7 +32,6 @@ import java.util.List; import java.util.Objects; import java.util.Optional; -import java.util.concurrent.CountDownLatch; public class BitcoindRegtestSetup extends AbstractRegtestSetup { @@ -88,10 +87,6 @@ public Optional filterUtxosByAddress( .findFirst(); } - public CountDownLatch waitUntilBlocksMined(List blockHashes) { - return remoteBitcoind.waitUntilBlocksMined(blockHashes); - } - public static RpcConfig createRpcConfig(String hostname, int port) { return RpcConfig.builder() .hostname(hostname) diff --git a/wallets/regtest/src/main/java/bisq/wallets/regtest/bitcoind/RemoteBitcoind.java b/wallets/regtest/src/main/java/bisq/wallets/regtest/bitcoind/RemoteBitcoind.java index b5b65d1634..d25793c6d3 100644 --- a/wallets/regtest/src/main/java/bisq/wallets/regtest/bitcoind/RemoteBitcoind.java +++ b/wallets/regtest/src/main/java/bisq/wallets/regtest/bitcoind/RemoteBitcoind.java @@ -34,7 +34,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; -import java.util.concurrent.CountDownLatch; import static bisq.wallets.regtest.AbstractRegtestSetup.WALLET_PASSPHRASE; @@ -49,7 +48,6 @@ public class RemoteBitcoind implements BisqProcess { private final ZmqListeners zmqListeners = new ZmqListeners(); @Getter private final BitcoindWallet minerWallet; - private final BitcoindRegtestBlockMiner blockMiner; private final List loadedWallets = new ArrayList<>(); private ZmqConnection bitcoindZeroMq; @@ -57,12 +55,10 @@ public RemoteBitcoind(RpcConfig rpcConfig) { this.rpcConfig = rpcConfig; this.daemon = createBitcoindDaemon(); this.minerWallet = new BitcoindWallet(daemon, rpcConfig, MINER_WALLET_NAME); - this.blockMiner = new BitcoindRegtestBlockMiner(daemon, minerWallet, zmqListeners); } @Override public void start() throws InterruptedException { - blockMiner.start(); initializeZmqListeners(); initializeWallet(minerWallet); mineInitialRegtestBlocks(); @@ -70,7 +66,6 @@ public void start() throws InterruptedException { @Override public void shutdown() { - blockMiner.shutdown(); bitcoindZeroMq.close(); loadedWallets.forEach(BitcoindWallet::shutdown); } @@ -81,8 +76,9 @@ public BitcoindWallet createAndInitializeNewWallet(String walletName) { return bitcoindWallet; } - public List mineBlocks(int numberOfBlocks) throws InterruptedException { - return blockMiner.mineBlocks(numberOfBlocks); + public List mineBlocks(int numberOfBlocks) { + String minerAddress = minerWallet.getNewAddress(AddressType.BECH32, ""); + return daemon.generateToAddress(numberOfBlocks, minerAddress); } public String fundAddress(String address, double amount) throws InterruptedException { @@ -104,10 +100,6 @@ public List mineOneBlock() throws InterruptedException { return mineBlocks(1); } - public CountDownLatch waitUntilBlocksMined(List blockHashes) { - return blockMiner.waitUntilBlocksMined(blockHashes); - } - private BitcoindDaemon createBitcoindDaemon() { JsonRpcClient rpcClient = RpcClientFactory.createDaemonRpcClient(rpcConfig); return new BitcoindDaemon(rpcClient); @@ -127,7 +119,7 @@ private void initializeWallet(BitcoindWallet wallet) { loadedWallets.add(wallet); } - private void mineInitialRegtestBlocks() throws InterruptedException { - blockMiner.mineBlocks(101); + private void mineInitialRegtestBlocks() { + mineBlocks(101); } }