14
14
import org .bitcoinj .core .Address ;
15
15
import org .bitcoinj .core .Coin ;
16
16
import org .bitcoinj .core .InsufficientMoneyException ;
17
+ import org .bitcoinj .core .PeerGroup ;
17
18
import org .bitcoinj .core .TransactionOutput ;
18
19
import org .bitcoinj .kits .WalletAppKit ;
19
20
import org .bitcoinj .wallet .Wallet ;
26
27
27
28
import lombok .extern .slf4j .Slf4j ;
28
29
30
+ import org .junit .jupiter .api .BeforeAll ;
29
31
import org .junit .jupiter .api .Test ;
32
+ import org .junit .jupiter .api .TestInstance ;
30
33
import org .junit .jupiter .api .extension .ExtendWith ;
31
34
import org .junit .jupiter .api .io .TempDir ;
32
35
33
36
import static org .hamcrest .MatcherAssert .assertThat ;
34
37
import static org .junit .jupiter .api .Assertions .assertEquals ;
38
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
35
39
import static org .mockito .ArgumentMatchers .any ;
36
40
import static org .mockito .Mockito .doReturn ;
37
41
import static org .mockito .Mockito .mock ;
42
46
import bisq .wallets .regtest .bitcoind .BitcoindRegtestSetup ;
43
47
44
48
@ ExtendWith (BitcoindExtension .class )
49
+ @ TestInstance (TestInstance .Lifecycle .PER_CLASS )
45
50
@ Slf4j
46
51
public class BitcoinjBsqTests {
47
52
48
- private final BitcoindRegtestSetup regtestSetup ;
53
+ private final BitcoindRegtestSetup bitcoindRegtestSetup ;
54
+ private final BitcoinjRegtestSetup bitcoinjRegtestSetup ;
49
55
private final BisqRegtestNetworkParams networkParams ;
56
+ private PeerGroup peerGroup ;
50
57
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 );
53
82
networkParams = new BisqRegtestNetworkParams ();
54
- networkParams .setPort (regtestSetup .getP2pPort ());
55
- }
83
+ networkParams .setPort (bitcoindRegtestSetup .getP2pPort ());
56
84
57
- @ Test
58
- void sendBsqTest (@ TempDir Path tempDir ) throws InterruptedException , InsufficientMoneyException , BsqChangeBelowDustException {
59
85
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
+ }
62
90
91
+ @ BeforeAll
92
+ void setup (@ TempDir Path tempDir ) throws InterruptedException {
63
93
var wallets = List .of (btcWallet , secondBsqWallet );
64
94
var regtestWalletAppKit = new RegtestWalletAppKit (networkParams , tempDir , wallets );
65
95
regtestWalletAppKit .initialize ();
66
96
67
97
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 ();
96
100
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 );
99
103
100
104
DaoStateService daoStateService = mock (DaoStateService .class );
101
105
doReturn (true ).when (daoStateService )
102
106
.isTxOutputSpendable (any (TxOutputKey .class ));
103
107
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
+ }
116
111
117
- var btcWalletV2 = new BtcWalletV2 (btcCoinSelector , btcWallet );
112
+ @ Test
113
+ void sendBsqTest () throws InterruptedException , InsufficientMoneyException , BsqChangeBelowDustException {
118
114
var bsqWalletV2 = new BsqWalletV2 (networkParams ,
119
- walletAppKit . peerGroup () ,
115
+ peerGroup ,
120
116
btcWalletV2 ,
121
117
bsqWallet ,
122
118
bsqCoinSelector );
@@ -130,13 +126,33 @@ protected boolean isTxOutputSpendable(TransactionOutput output) {
130
126
Coin receiverAmount = Coin .ofSat (100 * 100 );
131
127
bsqWalletV2 .sendBsq (receiverAddress , receiverAmount , Coin .ofSat (10 ));
132
128
133
- regtestSetup .mineOneBlock ();
129
+ bitcoindRegtestSetup .mineOneBlock ();
134
130
135
- isSuccess = secondBsqWalletReceivedLatch .await (30 , TimeUnit .SECONDS );
131
+ boolean isSuccess = secondBsqWalletReceivedLatch .await (30 , TimeUnit .SECONDS );
136
132
assertThat ("Didn't receive BSQ after 30 seconds." , isSuccess );
137
133
138
134
assertEquals (bsqWallet .getBalance (), Coin .ofSat (99990000 ));
139
135
assertEquals (btcWallet .getBalance (), Coin .ofSat (99999747 ));
140
136
assertEquals (secondBsqWallet .getBalance (), Coin .ofSat (10000 ));
141
137
}
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
+ }
142
158
}
0 commit comments