Skip to content

Commit 848694a

Browse files
committed
Add send BSQ from empty wallet test
1 parent 5ac81f9 commit 848694a

File tree

1 file changed

+71
-55
lines changed

1 file changed

+71
-55
lines changed

core/src/integrationTest/java/bisq/core/BitcoinjBsqTests.java

+71-55
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.bitcoinj.core.Address;
1515
import org.bitcoinj.core.Coin;
1616
import org.bitcoinj.core.InsufficientMoneyException;
17+
import org.bitcoinj.core.PeerGroup;
1718
import org.bitcoinj.core.TransactionOutput;
1819
import org.bitcoinj.kits.WalletAppKit;
1920
import org.bitcoinj.wallet.Wallet;
@@ -26,12 +27,15 @@
2627

2728
import lombok.extern.slf4j.Slf4j;
2829

30+
import org.junit.jupiter.api.BeforeAll;
2931
import org.junit.jupiter.api.Test;
32+
import org.junit.jupiter.api.TestInstance;
3033
import org.junit.jupiter.api.extension.ExtendWith;
3134
import org.junit.jupiter.api.io.TempDir;
3235

3336
import static org.hamcrest.MatcherAssert.assertThat;
3437
import static org.junit.jupiter.api.Assertions.assertEquals;
38+
import static org.junit.jupiter.api.Assertions.assertThrows;
3539
import static org.mockito.ArgumentMatchers.any;
3640
import static org.mockito.Mockito.doReturn;
3741
import static org.mockito.Mockito.mock;
@@ -42,81 +46,73 @@
4246
import bisq.wallets.regtest.bitcoind.BitcoindRegtestSetup;
4347

4448
@ExtendWith(BitcoindExtension.class)
49+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
4550
@Slf4j
4651
public class BitcoinjBsqTests {
4752

48-
private final BitcoindRegtestSetup regtestSetup;
53+
private final BitcoindRegtestSetup bitcoindRegtestSetup;
54+
private final BitcoinjRegtestSetup bitcoinjRegtestSetup;
4955
private final BisqRegtestNetworkParams networkParams;
56+
private PeerGroup peerGroup;
5057

51-
public BitcoinjBsqTests(BitcoindRegtestSetup regtestSetup) {
52-
this.regtestSetup = regtestSetup;
58+
private final Wallet btcWallet;
59+
private Wallet bsqWallet;
60+
private final Wallet secondBsqWallet;
61+
private final Wallet emptyBsqWallet;
62+
63+
private final BisqDefaultCoinSelector btcCoinSelector = new BisqDefaultCoinSelector(true) {
64+
@Override
65+
protected boolean isDustAttackUtxo(TransactionOutput output) {
66+
return false;
67+
}
68+
69+
@Override
70+
protected boolean isTxOutputSpendable(TransactionOutput output) {
71+
return true;
72+
}
73+
};
74+
75+
private BsqCoinSelector bsqCoinSelector;
76+
77+
private BtcWalletV2 btcWalletV2;
78+
79+
public BitcoinjBsqTests(BitcoindRegtestSetup bitcoindRegtestSetup) {
80+
this.bitcoindRegtestSetup = bitcoindRegtestSetup;
81+
bitcoinjRegtestSetup = new BitcoinjRegtestSetup(bitcoindRegtestSetup);
5382
networkParams = new BisqRegtestNetworkParams();
54-
networkParams.setPort(regtestSetup.getP2pPort());
55-
}
83+
networkParams.setPort(bitcoindRegtestSetup.getP2pPort());
5684

57-
@Test
58-
void sendBsqTest(@TempDir Path tempDir) throws InterruptedException, InsufficientMoneyException, BsqChangeBelowDustException {
5985
var walletFactory = new WalletFactory(networkParams);
60-
Wallet btcWallet = walletFactory.createBtcWallet();
61-
Wallet secondBsqWallet = walletFactory.createBsqWallet();
86+
btcWallet = walletFactory.createBtcWallet();
87+
secondBsqWallet = walletFactory.createBsqWallet();
88+
emptyBsqWallet = walletFactory.createBsqWallet();
89+
}
6290

91+
@BeforeAll
92+
void setup(@TempDir Path tempDir) throws InterruptedException {
6393
var wallets = List.of(btcWallet, secondBsqWallet);
6494
var regtestWalletAppKit = new RegtestWalletAppKit(networkParams, tempDir, wallets);
6595
regtestWalletAppKit.initialize();
6696

6797
WalletAppKit walletAppKit = regtestWalletAppKit.getWalletAppKit();
68-
Wallet bsqWallet = walletAppKit.wallet();
69-
70-
var bsqWalletReceivedLatch = new CountDownLatch(1);
71-
bsqWallet.addCoinsReceivedEventListener((wallet, tx, prevBalance, newBalance) ->
72-
bsqWalletReceivedLatch.countDown());
73-
74-
var btcWalletReceivedLatch = new CountDownLatch(1);
75-
btcWallet.addCoinsReceivedEventListener((wallet, tx, prevBalance, newBalance) ->
76-
btcWalletReceivedLatch.countDown());
77-
78-
Address currentReceiveAddress = bsqWallet.currentReceiveAddress();
79-
String address = currentReceiveAddress.toString();
80-
regtestSetup.fundAddress(address, 1.0);
81-
82-
currentReceiveAddress = btcWallet.currentReceiveAddress();
83-
address = currentReceiveAddress.toString();
84-
regtestSetup.fundAddress(address, 1.0);
85-
86-
regtestSetup.mineOneBlock();
87-
88-
boolean isSuccess = bsqWalletReceivedLatch.await(30, TimeUnit.SECONDS);
89-
assertThat("BSQ wallet not funded after 30 seconds.", isSuccess);
90-
91-
Coin balance = bsqWallet.getBalance();
92-
assertThat("BitcoinJ BSQ wallet balance should equal 1 BTC.", balance.equals(Coin.COIN));
93-
94-
isSuccess = btcWalletReceivedLatch.await(30, TimeUnit.SECONDS);
95-
assertThat("BTC wallet not funded after 30 seconds.", isSuccess);
98+
peerGroup = walletAppKit.peerGroup();
99+
bsqWallet = walletAppKit.wallet();
96100

97-
balance = btcWallet.getBalance();
98-
assertThat("BitcoinJ BTC wallet balance should equal 1 BTC.", balance.equals(Coin.COIN));
101+
bitcoinjRegtestSetup.fundWallet(bsqWallet, 1.0);
102+
bitcoinjRegtestSetup.fundWallet(btcWallet, 1.0);
99103

100104
DaoStateService daoStateService = mock(DaoStateService.class);
101105
doReturn(true).when(daoStateService)
102106
.isTxOutputSpendable(any(TxOutputKey.class));
103107

104-
var bsqCoinSelector = new BsqCoinSelector(daoStateService, mock(UnconfirmedBsqChangeOutputListService.class));
105-
var btcCoinSelector = new BisqDefaultCoinSelector(true) {
106-
@Override
107-
protected boolean isDustAttackUtxo(TransactionOutput output) {
108-
return false;
109-
}
110-
111-
@Override
112-
protected boolean isTxOutputSpendable(TransactionOutput output) {
113-
return true;
114-
}
115-
};
108+
bsqCoinSelector = new BsqCoinSelector(daoStateService, mock(UnconfirmedBsqChangeOutputListService.class));
109+
btcWalletV2 = new BtcWalletV2(btcCoinSelector, btcWallet);
110+
}
116111

117-
var btcWalletV2 = new BtcWalletV2(btcCoinSelector, btcWallet);
112+
@Test
113+
void sendBsqTest() throws InterruptedException, InsufficientMoneyException, BsqChangeBelowDustException {
118114
var bsqWalletV2 = new BsqWalletV2(networkParams,
119-
walletAppKit.peerGroup(),
115+
peerGroup,
120116
btcWalletV2,
121117
bsqWallet,
122118
bsqCoinSelector);
@@ -130,13 +126,33 @@ protected boolean isTxOutputSpendable(TransactionOutput output) {
130126
Coin receiverAmount = Coin.ofSat(100 * 100);
131127
bsqWalletV2.sendBsq(receiverAddress, receiverAmount, Coin.ofSat(10));
132128

133-
regtestSetup.mineOneBlock();
129+
bitcoindRegtestSetup.mineOneBlock();
134130

135-
isSuccess = secondBsqWalletReceivedLatch.await(30, TimeUnit.SECONDS);
131+
boolean isSuccess = secondBsqWalletReceivedLatch.await(30, TimeUnit.SECONDS);
136132
assertThat("Didn't receive BSQ after 30 seconds.", isSuccess);
137133

138134
assertEquals(bsqWallet.getBalance(), Coin.ofSat(99990000));
139135
assertEquals(btcWallet.getBalance(), Coin.ofSat(99999747));
140136
assertEquals(secondBsqWallet.getBalance(), Coin.ofSat(10000));
141137
}
138+
139+
@Test
140+
void sendBsqButNotEnoughBsqTest() {
141+
var bsqWalletV2 = new BsqWalletV2(networkParams,
142+
peerGroup,
143+
btcWalletV2,
144+
emptyBsqWallet,
145+
bsqCoinSelector);
146+
147+
var secondBsqWalletReceivedLatch = new CountDownLatch(1);
148+
bsqWallet.addCoinsReceivedEventListener((wallet, tx, prevBalance, newBalance) ->
149+
secondBsqWalletReceivedLatch.countDown());
150+
151+
// Send 100 BSQ (1 BSQ = 100 Satoshis)
152+
Address receiverAddress = bsqWallet.currentReceiveAddress();
153+
Coin receiverAmount = Coin.ofSat(100 * 100);
154+
155+
assertThrows(InsufficientMoneyException.class, () ->
156+
bsqWalletV2.sendBsq(receiverAddress, receiverAmount, Coin.ofSat(10)));
157+
}
142158
}

0 commit comments

Comments
 (0)