1
1
package org .ldk ;
2
2
3
- import org .bitcoinj .base .BitcoinNetwork ;
4
3
import org .bitcoinj .base .Coin ;
5
4
import org .bitcoinj .base .Sha256Hash ;
6
5
import org .bitcoinj .core .*;
7
- import org .bitcoinj .params .BitcoinNetworkParams ;
8
6
import org .bitcoinj .script .Script ;
9
7
import org .junit .jupiter .api .Test ;
10
8
import org .ldk .batteries .ChannelManagerConstructor ;
19
17
import java .io .IOException ;
20
18
import java .lang .ref .WeakReference ;
21
19
import java .net .InetSocketAddress ;
20
+ import java .nio .ByteBuffer ;
21
+ import java .time .Instant ;
22
22
import java .util .*;
23
23
import java .util .function .IntConsumer ;
24
24
@@ -35,6 +35,7 @@ class HumanObjectPeerTestInstance {
35
35
private final boolean use_chan_manager_constructor ;
36
36
private final boolean use_invoice_payer ;
37
37
TxOut gossip_txout = null ;
38
+ final Instant TIME_42 = Instant .ofEpochSecond (42 );
38
39
39
40
HumanObjectPeerTestInstance (boolean nice_close , boolean use_km_wrapper , boolean use_full_km_wrapper , boolean use_manual_watch , boolean reload_peers , boolean break_cross_peer_refs , boolean use_nio_peer_handler , boolean use_filter , boolean use_ignore_handler , boolean use_chan_manager_constructor , boolean use_invoice_payer ) {
40
41
this .nice_close = nice_close ;
@@ -653,17 +654,17 @@ public Result_CVec_BlindedMessagePathZNoneZ create_compact_blinded_paths(byte[]
653
654
}
654
655
655
656
TwoTuple_ThirtyTwoBytesCVec_C2Tuple_u32TxOutZZZ [] connect_block (Block b , int height , long expected_monitor_update_len ) {
656
- byte [] header = Arrays .copyOfRange (b .bitcoinSerialize (), 0 , 80 );
657
+ byte [] header = Arrays .copyOfRange (b .serialize (), 0 , 80 );
657
658
TwoTuple_usizeTransactionZ [] txn ;
658
659
if (b .hasTransactions ()) {
659
660
assert b .getTransactions ().size () == 1 ;
660
- TwoTuple_usizeTransactionZ txp = TwoTuple_usizeTransactionZ .of ((long ) 0 , b .getTransactions ().get (0 ).bitcoinSerialize ());
661
+ TwoTuple_usizeTransactionZ txp = TwoTuple_usizeTransactionZ .of ((long ) 0 , b .getTransactions ().get (0 ).serialize ());
661
662
txn = new TwoTuple_usizeTransactionZ []{txp };
662
663
} else
663
664
txn = new TwoTuple_usizeTransactionZ [0 ];
664
665
if (chain_monitor != null ) {
665
- chan_manager .as_Listen ().block_connected (b .bitcoinSerialize (), height );
666
- chain_monitor .as_Listen ().block_connected (b .bitcoinSerialize (), height );
666
+ chan_manager .as_Listen ().block_connected (b .serialize (), height );
667
+ chain_monitor .as_Listen ().block_connected (b .serialize (), height );
667
668
} else {
668
669
chan_manager .as_Confirm ().transactions_confirmed (header , txn , height );
669
670
chan_manager .as_Confirm ().best_block_updated (header , height );
@@ -769,7 +770,7 @@ Event[] get_manager_events(int expected_len, Peer peer1, Peer peer2) {
769
770
static class DescriptorHolder { SocketDescriptor val ; }
770
771
771
772
boolean running = false ;
772
- final LinkedList <Runnable > runqueue = new LinkedList ();
773
+ final LinkedList <Runnable > runqueue = new LinkedList <> ();
773
774
boolean ran = false ;
774
775
Thread t = new Thread (() -> {
775
776
while (true ) {
@@ -913,16 +914,13 @@ TestState do_test_message_handler() throws InterruptedException {
913
914
ChannelId chan_id = ((Event .FundingGenerationReady ) events [0 ]).temporary_channel_id ;
914
915
mid_test_must_free_objs .add (new WeakReference <>(chan_id ));
915
916
916
- BitcoinNetworkParams bitcoinj_net = BitcoinNetworkParams .of (BitcoinNetwork .MAINNET );
917
-
918
- Transaction funding_input = new Transaction (bitcoinj_net );
919
- funding_input .addOutput (Coin .SATOSHI .multiply (100000 ), new Script (funding_spk ));
920
- Transaction funding = new Transaction (bitcoinj_net );
917
+ Transaction funding_input = new Transaction ();
918
+ funding_input .addOutput (Coin .ofSat (100000 ), Script .parse (funding_spk ));
919
+ Transaction funding = new Transaction ();
921
920
funding .addInput (funding_input .getOutput (0 ));
922
- funding .getInputs ().get (0 ).setWitness (new TransactionWitness (2 )); // Make sure we don't complain about lack of witness
923
- funding .getInput (0 ).getWitness ().setPush (0 , new byte []{0x1 });
924
- funding .addOutput (Coin .SATOSHI .multiply (100000 ), new Script (funding_spk ));
925
- Result_NoneAPIErrorZ funding_res = peer1 .chan_manager .funding_transaction_generated (chan_id , peer2 .node_id , funding .bitcoinSerialize ());
921
+ funding .getInput (0 ).setWitness (TransactionWitness .of (new byte []{0x1 })); // Make sure we don't complain about lack of witness
922
+ funding .addOutput (Coin .ofSat (100000 ), Script .parse (funding_spk ));
923
+ Result_NoneAPIErrorZ funding_res = peer1 .chan_manager .funding_transaction_generated (chan_id , peer2 .node_id , funding .serialize ());
926
924
assert funding_res instanceof Result_NoneAPIErrorZ .Result_NoneAPIErrorZ_OK ;
927
925
928
926
gossip_txout = new TxOut (100000 , funding_spk );
@@ -943,16 +941,16 @@ TestState do_test_message_handler() throws InterruptedException {
943
941
mid_test_must_free_objs .add (new WeakReference <>(events [0 ]));
944
942
945
943
assert peer1 .broadcast_set .size () == 1 ;
946
- assert Arrays .equals (peer1 .broadcast_set .get (0 ), funding .bitcoinSerialize ());
944
+ assert Arrays .equals (peer1 .broadcast_set .get (0 ), funding .serialize ());
947
945
mid_test_must_free_objs .add (new WeakReference <>(peer1 .broadcast_set .get (0 )));
948
946
peer1 .broadcast_set .clear ();
949
947
950
- Block b = new Block (bitcoinj_net , 2 , Sha256Hash .ZERO_HASH , Sha256Hash .ZERO_HASH , 42 , 0 , 0 , Arrays . asList ( new Transaction []{ funding } ));
948
+ Block b = new Block (2 , Sha256Hash .ZERO_HASH , Sha256Hash .ZERO_HASH , TIME_42 , 0 , 0 , List . of ( funding ));
951
949
peer1 .connect_block (b , 1 , 0 );
952
950
peer2 .connect_block (b , 1 , 0 );
953
951
954
952
for (int height = 2 ; height < 10 ; height ++) {
955
- b = new Block (bitcoinj_net , 2 , b .getHash (), Sha256Hash .ZERO_HASH , 42 , 0 , 0 , Arrays . asList ( new Transaction [ 0 ] ));
953
+ b = new Block (2 , b .getHash (), Sha256Hash .ZERO_HASH , TIME_42 , 0 , 0 , List . of ( ));
956
954
peer1 .connect_block (b , height , 0 );
957
955
peer2 .connect_block (b , height , 0 );
958
956
}
@@ -977,8 +975,8 @@ TestState do_test_message_handler() throws InterruptedException {
977
975
Option_u64Z short_chan_id = peer1_chans [0 ].get_short_channel_id ();
978
976
assert short_chan_id instanceof Option_u64Z .Some ;
979
977
assert ((Option_u64Z .Some )short_chan_id ).some == (1L << 40 ); // 0th output in the 0th transaction in the 1st block
980
- assert Arrays .equals (peer1_chans [0 ].get_channel_id ().get_a (), funding .getTxId ().getReversedBytes ());
981
- assert Arrays .equals (peer2_chans [0 ].get_channel_id ().get_a (), funding .getTxId ().getReversedBytes ());
978
+ assert Arrays .equals (peer1_chans [0 ].get_channel_id ().get_a (), funding .getTxId ().serialize ());
979
+ assert Arrays .equals (peer2_chans [0 ].get_channel_id ().get_a (), funding .getTxId ().serialize ());
982
980
mid_test_must_free_objs .add (new WeakReference <>(peer1_chans [0 ]));
983
981
mid_test_must_free_objs .add (new WeakReference <>(peer2_chans [0 ]));
984
982
@@ -1222,21 +1220,19 @@ void do_test_message_handler_b(TestState state) throws Exception {
1222
1220
}
1223
1221
1224
1222
if (!nice_close ) {
1225
- BitcoinNetworkParams bitcoinj_net = BitcoinNetworkParams .of (BitcoinNetwork .MAINNET );
1226
- Transaction tx = new Transaction (bitcoinj_net , state .peer1 .broadcast_set .getFirst ());
1227
- Block b = new Block (bitcoinj_net , 2 , state .best_blockhash , Sha256Hash .ZERO_HASH , 42 , 0 , 0 ,
1228
- Arrays .asList (new Transaction []{tx }));
1223
+ Transaction tx = Transaction .read (ByteBuffer .wrap (state .peer1 .broadcast_set .getFirst ()));
1224
+ Block b = new Block (2 , state .best_blockhash , Sha256Hash .ZERO_HASH , TIME_42 , 0 , 0 , List .of (tx ));
1229
1225
TwoTuple_ThirtyTwoBytesCVec_C2Tuple_u32TxOutZZZ [] watch_outputs = state .peer2 .connect_block (b , 10 , 1 );
1230
1226
if (watch_outputs != null ) { // We only process watch_outputs manually when we use a manually-build Watch impl
1231
1227
assert watch_outputs .length == 1 ;
1232
- assert Arrays .equals (watch_outputs [0 ].get_a (), tx .getTxId ().getReversedBytes ());
1228
+ assert Arrays .equals (watch_outputs [0 ].get_a (), tx .getTxId ().serialize ());
1233
1229
assert watch_outputs [0 ].get_b ().length == 2 ;
1234
1230
assert watch_outputs [0 ].get_b ()[0 ].get_a () == 0 ;
1235
1231
assert watch_outputs [0 ].get_b ()[1 ].get_a () == 1 ;
1236
1232
}
1237
1233
1238
1234
for (int i = 11 ; i < 21 ; i ++) {
1239
- b = new Block (bitcoinj_net , 2 , b .getHash (), Sha256Hash .ZERO_HASH , 42 , 0 , 0 , new ArrayList <> ());
1235
+ b = new Block (2 , b .getHash (), Sha256Hash .ZERO_HASH , TIME_42 , 0 , 0 , List . of ());
1240
1236
state .peer2 .connect_block (b , i , 0 );
1241
1237
}
1242
1238
@@ -1247,7 +1243,7 @@ void do_test_message_handler_b(TestState state) throws Exception {
1247
1243
TxOut [] additional_outputs = new TxOut []{new TxOut (420 , new byte []{0x42 })};
1248
1244
Result_TransactionNoneZ tx_res = state .peer2 .explicit_keys_manager .as_OutputSpender ().spend_spendable_outputs (((Event .SpendableOutputs ) broadcastable_event [0 ]).outputs , additional_outputs , new byte []{0x00 }, 253 , Option_u32Z .none ());
1249
1245
assert tx_res instanceof Result_TransactionNoneZ .Result_TransactionNoneZ_OK ;
1250
- Transaction built_tx = new Transaction ( bitcoinj_net , (( Result_TransactionNoneZ .Result_TransactionNoneZ_OK ) tx_res ).res );
1246
+ Transaction built_tx = Transaction . read ( ByteBuffer . wrap ((( Result_TransactionNoneZ .Result_TransactionNoneZ_OK ) tx_res ).res ) );
1251
1247
assert built_tx .getOutputs ().size () == 2 ;
1252
1248
assert Arrays .equals (built_tx .getOutput (1 ).getScriptBytes (), new byte []{0x00 });
1253
1249
assert Arrays .equals (built_tx .getOutput (0 ).getScriptBytes (), new byte []{0x42 });
@@ -1323,8 +1319,8 @@ void do_test_message_handler_b(TestState state) throws Exception {
1323
1319
}
1324
1320
}
1325
1321
1326
- LinkedList <WeakReference <Object >> must_free_objs = new LinkedList ();
1327
- LinkedList <WeakReference <Object >> mid_test_must_free_objs = new LinkedList ();
1322
+ LinkedList <WeakReference <Object >> must_free_objs = new LinkedList <> ();
1323
+ LinkedList <WeakReference <Object >> mid_test_must_free_objs = new LinkedList <> ();
1328
1324
int gc_count = 0 ;
1329
1325
int gc_exp_count = 0 ;
1330
1326
class GcCheck {
0 commit comments