X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Forg%2Fldk%2FHumanObjectPeerTest.java;h=2df142e67cb5ecd92db1c8c3632c29665544dfa9;hb=a9dce3ef02da97ea3a8ed91f9704a96ebf446086;hp=acdf4576bca439dd787638addc5f1ceffd57103c;hpb=f3b48938648f15ef5b85b589d983e13932ff7c60;p=ldk-java diff --git a/src/test/java/org/ldk/HumanObjectPeerTest.java b/src/test/java/org/ldk/HumanObjectPeerTest.java index acdf4576..2df142e6 100644 --- a/src/test/java/org/ldk/HumanObjectPeerTest.java +++ b/src/test/java/org/ldk/HumanObjectPeerTest.java @@ -1,11 +1,10 @@ package org.ldk; import org.bitcoinj.core.*; -import org.bitcoinj.core.Transaction; import org.bitcoinj.script.Script; import org.junit.jupiter.api.Test; -import org.ldk.batteries.ChannelManagerConstructor; import org.ldk.batteries.NioPeerHandler; +import org.ldk.enums.LDKCurrency; import org.ldk.enums.LDKNetwork; import org.ldk.impl.bindings; import org.ldk.structs.*; @@ -45,7 +44,8 @@ class HumanObjectPeerTestInstance { @Override public Sign get_channel_signer(boolean inbound, long channel_value_satoshis) { Sign underlying_ck = underlying_if.get_channel_signer(inbound, channel_value_satoshis); - Sign.SignInterface si = new Sign.SignInterface() { + // TODO: Expose the underlying signer from a Sign + /*BaseSign.BaseSignInterface si = new BaseSign.BaseSignInterface() { @Override public byte[] get_per_commitment_point(long idx) { return underlying_ck.get_per_commitment_point(idx); @@ -100,12 +100,13 @@ class HumanObjectPeerTestInstance { public byte[] write() { return underlying_ck.write(); } - }; - Sign resp = Sign.new_impl(si, underlying_ck.get_pubkeys()); - must_free_objs.add(new WeakReference<>(si)); - must_free_objs.add(new WeakReference<>(resp)); + };*/ + //Sign resp = Sign.new_impl(si, underlying_ck.get_pubkeys()); + //must_free_objs.add(new WeakReference<>(si)); + //must_free_objs.add(new WeakReference<>(resp)); must_free_objs.add(new WeakReference<>(underlying_ck)); - return resp; + //return resp; + return underlying_ck; } @Override @@ -117,6 +118,11 @@ class HumanObjectPeerTestInstance { public Result_SignDecodeErrorZ read_chan_signer(byte[] reader) { return underlying_if.read_chan_signer(reader); } + + @Override + public Result_RecoverableSignatureNoneZ sign_invoice(byte[] invoice_preimage) { + return underlying_if.sign_invoice(invoice_preimage); + } }); } @@ -272,7 +278,7 @@ class HumanObjectPeerTestInstance { } Peer(byte seed) { this(null, seed); - this.chan_manager = ChannelManager.constructor_new(FeeEstimator.new_impl(confirmation_target -> 0), chain_watch, tx_broadcaster, logger, this.keys_interface, UserConfig.constructor_default(), LDKNetwork.LDKNetwork_Bitcoin, new byte[32], 0); + this.chan_manager = ChannelManager.constructor_new(FeeEstimator.new_impl(confirmation_target -> 0), chain_watch, tx_broadcaster, logger, this.keys_interface, UserConfig.constructor_default(), LDKNetwork.LDKNetwork_Bitcoin, BestBlock.constructor_new(new byte[32], 0)); this.node_id = chan_manager.get_our_node_id(); this.chan_manager_events = chan_manager.as_EventsProvider(); @@ -287,38 +293,26 @@ class HumanObjectPeerTestInstance { Object ptr_to; Peer(Peer orig) { this(null, orig.seed); - if (!break_cross_peer_refs) { - ChannelMonitor[] monitors = new ChannelMonitor[1]; - synchronized (monitors) { - assert orig.monitors.size() == 1; + // TODO: Optionally test ChannelManagerConstructor + ChannelMonitor[] monitors = new ChannelMonitor[1]; + synchronized (monitors) { + assert orig.monitors.size() == 1; + if (!break_cross_peer_refs) { monitors[0] = orig.monitors.values().stream().iterator().next(); - } - byte[] serialized = orig.chan_manager.write(); - Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ read_res = - UtilMethods.constructor_BlockHashChannelManagerZ_read(serialized, this.keys_interface, this.fee_estimator, this.chain_watch, this.tx_broadcaster, this.logger, UserConfig.constructor_default(), monitors); - assert read_res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK; - this.chan_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK) read_res).res.b; - this.chain_watch.watch_channel(monitors[0].get_funding_txo().a, monitors[0]); - } else { - final ArrayList channel_monitors = new ArrayList(); - synchronized (monitors) { - assert orig.monitors.size() == 1; - channel_monitors.add(orig.monitors.values().stream().iterator().next().write()); - } - byte[] serialized = orig.chan_manager.write(); - try { - ChannelManagerConstructor constructed = new ChannelManagerConstructor(serialized, channel_monitors.toArray(new byte[1][]), this.keys_interface, this.fee_estimator, this.chain_watch, this.filter, this.tx_broadcaster, this.logger); - this.chan_manager = constructed.channel_manager; - constructed.chain_sync_completed(); - if (use_filter && !use_manual_watch) { - // With a manual watch we don't actually use the filter object at all. - assert this.filter_additions.containsAll(orig.filter_additions) && - orig.filter_additions.containsAll(this.filter_additions); - } - } catch (ChannelManagerConstructor.InvalidSerializedDataException e) { - assert false; + } else { + byte[] serialized = orig.monitors.values().stream().iterator().next().write(); + Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ res = + UtilMethods.constructor_BlockHashChannelMonitorZ_read(serialized, this.keys_interface); + assert res instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK; + monitors[0] = ((Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_OK) res).res.b; } } + byte[] serialized = orig.chan_manager.write(); + Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ read_res = + UtilMethods.constructor_BlockHashChannelManagerZ_read(serialized, this.keys_interface, this.fee_estimator, this.chain_watch, this.tx_broadcaster, this.logger, UserConfig.constructor_default(), monitors); + assert read_res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK; + this.chan_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK) read_res).res.b; + this.chain_watch.watch_channel(monitors[0].get_funding_txo().a, monitors[0]); if (!break_cross_peer_refs && (use_manual_watch || use_km_wrapper)) { // When we pass monitors[0] into chain_watch.watch_channel we create a reference from the new Peer to a // field in the old peer, preventing freeing of the original Peer until the new Peer is freed. Thus, we @@ -356,10 +350,13 @@ class HumanObjectPeerTestInstance { txn = new TwoTuple[]{txp}; } else txn = new TwoTuple[0]; - chan_manager.as_Listen().block_connected(b.bitcoinSerialize(), height); if (chain_monitor != null) { - chain_monitor.block_connected(header, txn, height); + chan_manager.as_Listen().block_connected(b.bitcoinSerialize(), height); + chain_monitor.as_Listen().block_connected(b.bitcoinSerialize(), height); } else { + chan_manager.as_Confirm().transactions_confirmed(header, txn, height); + chan_manager.as_Confirm().best_block_updated(header, height); + // Connect manually if we aren't using a ChainMonitor and are implementing Watch ourselves synchronized (monitors) { assert monitors.size() == 1; for (ChannelMonitor mon : monitors.values()) { @@ -533,13 +530,13 @@ class HumanObjectPeerTestInstance { funding.getInputs().get(0).setWitness(new TransactionWitness(2)); // Make sure we don't complain about lack of witness funding.getInput(0).getWitness().setPush(0, new byte[]{0x1}); funding.addOutput(Coin.SATOSHI.multiply(10000), new Script(funding_spk)); - peer1.chan_manager.funding_transaction_generated(chan_id, OutPoint.constructor_new(funding.getTxId().getReversedBytes(), (short) 0)); + Result_NoneAPIErrorZ funding_res = peer1.chan_manager.funding_transaction_generated(chan_id, funding.bitcoinSerialize()); + assert funding_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK; wait_events_processed(peer1, peer2); - events = peer1.chan_manager_events.get_and_clear_pending_events(); - assert events.length == 1; - assert events[0] instanceof Event.FundingBroadcastSafe; - assert ((Event.FundingBroadcastSafe) events[0]).user_channel_id == 42; + assert peer1.broadcast_set.size() == 1; + assert Arrays.equals(peer1.broadcast_set.get(0), funding.bitcoinSerialize()); + peer1.broadcast_set.clear(); Block b = new Block(bitcoinj_net, 2, Sha256Hash.ZERO_HASH, Sha256Hash.ZERO_HASH, 42, 0, 0, Arrays.asList(new Transaction[]{funding})); peer1.connect_block(b, 1, 0); @@ -565,11 +562,17 @@ class HumanObjectPeerTestInstance { assert Arrays.equals(peer1_chans[0].get_channel_id(), funding.getTxId().getReversedBytes()); assert Arrays.equals(peer2_chans[0].get_channel_id(), funding.getTxId().getReversedBytes()); - byte[] payment_preimage = new byte[32]; - for (int i = 0; i < 32; i++) payment_preimage[i] = (byte) (i ^ 0x0f); - byte[] payment_hash = Sha256Hash.hash(payment_preimage); + Result_InvoiceSignOrCreationErrorZ invoice = UtilMethods.constructor_invoice_from_channelmanager(peer2.chan_manager, peer2.keys_interface, LDKCurrency.LDKCurrency_Bitcoin, Option_u64Z.constructor_none(), ""); + assert invoice instanceof Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK; + System.out.println("Got invoice: " + ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str()); + Result_InvoiceNoneZ parsed_invoice = Invoice.constructor_from_str(((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str()); + assert parsed_invoice instanceof Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK; + assert Arrays.equals(((Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) parsed_invoice).res.payment_hash(), ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash()); + byte[] payment_hash = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash(); + byte[] payment_secret = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_secret(); + Route route = peer1.get_route(peer2.node_id, peer1_chans); - Result_NonePaymentSendFailureZ payment_res = peer1.chan_manager.send_payment(route, payment_hash, new byte[32]); + Result_NonePaymentSendFailureZ payment_res = peer1.chan_manager.send_payment(route, payment_hash, payment_secret); assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_OK; wait_events_processed(peer1, peer2); @@ -579,7 +582,7 @@ class HumanObjectPeerTestInstance { hop_pubkey[1] = 42; hops[0][0] = RouteHop.constructor_new(hop_pubkey, NodeFeatures.constructor_known(), 42, ChannelFeatures.constructor_known(), 100, 0); Route r2 = Route.constructor_new(hops); - payment_res = peer1.chan_manager.send_payment(r2, payment_hash, new byte[32]); + payment_res = peer1.chan_manager.send_payment(r2, payment_hash, payment_secret); assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_Err; assert peer1.get_monitor_events().length == 0; @@ -593,9 +596,9 @@ class HumanObjectPeerTestInstance { WeakReference op1 = new WeakReference(peer1); peer1 = new Peer(peer1); peer2 = new Peer(peer2); - return new TestState(op1, peer1, peer2, payment_preimage, b.getHash()); + return new TestState(op1, peer1, peer2, b.getHash()); } - return new TestState(null, peer1, peer2, payment_preimage, b.getHash()); + return new TestState(null, peer1, peer2, b.getHash()); } boolean cross_reload_ref_pollution = false; @@ -603,14 +606,12 @@ class HumanObjectPeerTestInstance { private final WeakReference ref_block; private final Peer peer1; private final Peer peer2; - private final byte[] payment_preimage; public Sha256Hash best_blockhash; - public TestState(WeakReference ref_block, Peer peer1, Peer peer2, byte[] payment_preimage, Sha256Hash best_blockhash) { + public TestState(WeakReference ref_block, Peer peer1, Peer peer2, Sha256Hash best_blockhash) { this.ref_block = ref_block; this.peer1 = peer1; this.peer2 = peer2; - this.payment_preimage = payment_preimage; this.best_blockhash = best_blockhash; } } @@ -635,13 +636,15 @@ class HumanObjectPeerTestInstance { events = state.peer2.chan_manager_events.get_and_clear_pending_events(); assert events.length == 1; assert events[0] instanceof Event.PaymentReceived; - state.peer2.chan_manager.claim_funds(state.payment_preimage, new byte[32], ((Event.PaymentReceived) events[0]).amt); + byte[] payment_preimage = ((Event.PaymentReceived)events[0]).payment_preimage; + assert !Arrays.equals(payment_preimage, new byte[32]); + state.peer2.chan_manager.claim_funds(payment_preimage); wait_events_processed(state.peer1, state.peer2); events = state.peer1.chan_manager_events.get_and_clear_pending_events(); assert events.length == 1; assert events[0] instanceof Event.PaymentSent; - assert Arrays.equals(((Event.PaymentSent) events[0]).payment_preimage, state.payment_preimage); + assert Arrays.equals(((Event.PaymentSent) events[0]).payment_preimage, payment_preimage); wait_events_processed(state.peer1, state.peer2); ChannelDetails[] peer1_chans = state.peer1.chan_manager.list_channels(); @@ -658,7 +661,7 @@ class HumanObjectPeerTestInstance { wait_events_processed(state.peer1, state.peer2); assert state.peer1.broadcast_set.size() == 1; - assert state.peer2.broadcast_set.size() == 0; + assert state.peer2.broadcast_set.size() == 1; NetworkParameters bitcoinj_net = NetworkParameters.fromID(NetworkParameters.ID_MAINNET); Transaction tx = new Transaction(bitcoinj_net, state.peer1.broadcast_set.getFirst());