def channel_monitor_data(in_mem_chan_keys, holder_commitment_tx): shutdown_pk = PublicKey(get_random_pk_bytes()) on_counterparty_tx_csv = 20 destination_script = Script(get_random_bytes((40))) funding_info = (OutPoint.from_bytes(get_random_bytes(34)), Script(get_random_bytes(50))) counterparty_htlc_base_key = PublicKey(get_random_pk_bytes()) counterparty_delayed_payment_base_key = PublicKey(get_random_pk_bytes()) on_holder_tx_csv = 30 funding_redeemscript = Script(get_random_bytes(40)) channel_value_satoshis = 42 commitment_transaction_number_obscure_factor = 10 return { "in_mem_chan_keys": in_mem_chan_keys, "shutdown_pk": shutdown_pk, "on_counterparty_tx_csv": on_counterparty_tx_csv, "destination_script": destination_script, "funding_info": funding_info, "counterparty_htlc_base_key": counterparty_htlc_base_key, "counterparty_delayed_payment_base_key": counterparty_delayed_payment_base_key, "on_holder_tx_csv": on_holder_tx_csv, "funding_redeemscript": funding_redeemscript, "channel_value_satoshis": channel_value_satoshis, "commitment_transaction_number_obscure_factor": commitment_transaction_number_obscure_factor, "holder_commitment_tx": holder_commitment_tx, }
def test_txin_init(): prev_out = OutPoint(TxId(get_random_bytes(32)), 42) script_sig = Script(get_random_bytes(120)) sequence = 0 witness = [get_random_bytes(72), get_random_bytes(73)] assert isinstance(TxIn(prev_out, script_sig, sequence, witness), TxIn)
def test_static_output(): outpoint = OutPoint.from_bytes(get_random_bytes(34)) txout = TxOut(get_random_int(8), Script(get_random_bytes(30))) descriptor = SpendableOutputDescriptor.static_output(outpoint, txout) assert isinstance( descriptor, SpendableOutputDescriptor) and descriptor.type == "StaticOutput"
def test_funding_generation_ready(): temporary_channel_id = get_random_bytes(32) channel_value_satoshis = 42 output_script = Script(get_random_bytes(50)) user_channel_id = get_random_int(8) event = Event.funding_generation_ready(temporary_channel_id, channel_value_satoshis, output_script, user_channel_id) assert isinstance(event, Event) and event.type == "FundingGenerationReady"
def test_static_output_counterparty_payment(): outpoint = OutPoint.from_bytes(get_random_bytes(34)) txout = TxOut(get_random_int(8), Script(get_random_bytes(30))) key_derivation_params = (get_random_int(8), get_random_int(8)) descriptor = SpendableOutputDescriptor.static_output_counterparty_payment( outpoint, txout, key_derivation_params) assert isinstance( descriptor, SpendableOutputDescriptor ) and descriptor.type == "StaticOutputCounterpartyPayment"
def test_spendable_outputs_getters(): outpoint = OutPoint.from_bytes(get_random_bytes(34)) output = TxOut(get_random_int(8), Script(get_random_bytes(50))) descriptor = SpendableOutputDescriptor.static_output(outpoint, output) event = Event.spendable_outputs([descriptor]) for local_output, binded_output in zip(event.outputs, [descriptor]): assert local_output.type == binded_output.type assert local_output.outpoint == binded_output.outpoint assert local_output.output == binded_output.output
def test_txin_getters(): prev_out = OutPoint(TxId(get_random_bytes(32)), 42) script_sig = Script(get_random_bytes(120)) sequence = pow(2, 16) - 1 witness = [get_random_bytes(72), get_random_bytes(73)] txin = TxIn(prev_out, script_sig, sequence, witness) assert txin.previous_output == prev_out assert txin.script_sig == script_sig assert txin.sequence == sequence assert txin.witness == witness
def test_static_output_getters(): outpoint = OutPoint.from_bytes(get_random_bytes(34)) txout = TxOut(get_random_int(8), Script(get_random_bytes(30))) descriptor = SpendableOutputDescriptor.static_output(outpoint, txout) assert descriptor.outpoint == outpoint assert descriptor.output == txout # Check no other getters are available local_attributes = ["outpoint", "output"] check_not_available_getters(descriptor, local_attributes, all_attributes)
def test_dynamic_output_pwsh(): outpoint = OutPoint.from_bytes(get_random_bytes(34)) per_commitment_point = PublicKey(get_random_pk_bytes()) to_self_delay = 20 txout = TxOut(get_random_int(8), Script(get_random_bytes(30))) key_derivation_params = (get_random_int(8), get_random_int(8)) revocation_pubkey = PublicKey(get_random_pk_bytes()) descriptor = SpendableOutputDescriptor.dynamic_output_pwsh( outpoint, per_commitment_point, to_self_delay, txout, key_derivation_params, revocation_pubkey) assert isinstance( descriptor, SpendableOutputDescriptor) and descriptor.type == "DynamicOutputP2WSH"
def test_static_output_counterparty_payment_getters(): outpoint = OutPoint.from_bytes(get_random_bytes(34)) txout = TxOut(get_random_int(8), Script(get_random_bytes(30))) key_derivation_params = (get_random_int(8), get_random_int(8)) descriptor = SpendableOutputDescriptor.static_output_counterparty_payment( outpoint, txout, key_derivation_params) assert descriptor.outpoint == outpoint assert descriptor.output == txout assert descriptor.key_derivation_params == key_derivation_params # Check no other getters are available local_attributes = ["outpoint", "output", "key_derivation_params"] check_not_available_getters(descriptor, local_attributes, all_attributes)
def holder_commitment_tx_data(tx): counterparty_sk = SecretKey(get_random_sk_bytes()) counterparty_pk = PublicKey.from_secret_key(counterparty_sk) counterparty_sig = counterparty_sk.sign(tx.hex()) holder_pk = PublicKey(get_random_pk_bytes()) keys = TxCreationKeys( PublicKey(get_random_pk_bytes()), PublicKey(get_random_pk_bytes()), PublicKey(get_random_pk_bytes()), PublicKey(get_random_pk_bytes()), PublicKey(get_random_pk_bytes()), ) feerate_kw = 1000 # HTLC DATA offered = True amount_msat = 500000 cltv_expiry = 30 payment_hash = PaymentHash(get_random_bytes(32)) tx_out_index = None htlc_out = HTLCOutputInCommitment(offered, amount_msat, cltv_expiry, payment_hash, tx_out_index) htlc_data = [(htlc_out, None)] return Transaction.from_bytes(tx), counterparty_sig, holder_pk, counterparty_pk, keys, feerate_kw, htlc_data
def node_announcement_info_data(node_announcement_bytes): features = NodeFeatures.known() last_update = get_random_int(4) rgb = get_random_bytes(3) alias = get_random_bytes(32) addresses = [NetAddress.ipv4([127, 0, 0, 1], 1234)] announcement_message = NodeAnnouncement.from_bytes(node_announcement_bytes) return { "features": features, "last_update": last_update, "rgb": rgb, "alias": alias, "addresses": addresses, "announcement_message": announcement_message, }
def test_outpoint_getters(): txid = get_random_bytes(32) index = 42 outpoint = OutPoint(TxId(txid), index) assert outpoint.txid == txid assert outpoint.index == index
def test_txout_getters(): value = pow(2, 64) - 15 script_pubkey = Script(get_random_bytes(80)) txout = TxOut(value, script_pubkey) assert txout.value == value assert txout.script_pubkey == script_pubkey
def peer_manager(channel_message_handler, routing_message_handler): our_node_secret = SecretKey(get_random_sk_bytes()) ephemeral_random_data = get_random_bytes(32) logger = LDKLogger(Logger()) return PeerManager(channel_message_handler, routing_message_handler, our_node_secret, ephemeral_random_data, logger)
def test_dynamic_output_pwsh_getters(): outpoint = OutPoint.from_bytes(get_random_bytes(34)) per_commitment_point = PublicKey(get_random_pk_bytes()) to_self_delay = 20 txout = TxOut(get_random_int(8), Script(get_random_bytes(30))) key_derivation_params = (get_random_int(8), get_random_int(8)) revocation_pubkey = PublicKey(get_random_pk_bytes()) descriptor = SpendableOutputDescriptor.dynamic_output_pwsh( outpoint, per_commitment_point, to_self_delay, txout, key_derivation_params, revocation_pubkey) assert descriptor.outpoint == outpoint assert descriptor.per_commitment_point == per_commitment_point assert descriptor.to_self_delay == to_self_delay assert descriptor.output == txout assert descriptor.key_derivation_params == key_derivation_params assert descriptor.revocation_pubkey == revocation_pubkey
def test_block_disconnected(channel_monitor, tx): block_header = BlockHeader(get_random_bytes(80)) height = 1 broadcaster = BroadcasterInterface(Broadcaster()) fee_estimator = FeeEstimator(FeeEst()) logger = LDKLogger(Logger()) channel_monitor.block_disconnected(block_header, height, broadcaster, fee_estimator, logger)
def test_update_channel_unknown_channel(chain_monitor, channel_monitor_update_data): outpoint = OutPoint.from_bytes(get_random_bytes(34)) update = ChannelMonitorUpdate.from_bytes(channel_monitor_update_data) # Update without having watched the channel with pytest.raises(PermanentChannelMonitorUpdateErr): chain_monitor.update_channel(outpoint, update)
def test_commitment_tx_broadcasted_getters(): outpoint = OutPoint.from_bytes(get_random_bytes(34)) event = MonitorEvent.commitment_tx_broadcasted(outpoint) assert event.type == "CommitmentTxBroadcasted" assert event.outpoint == outpoint # Check no other getters are available check_not_available_getters(event, ["outpoint"], all_attributes)
def get_utxo(self, genesis_hash, short_channel_id): if genesis_hash == BlockHash(bytes(32)): raise UnknownChain() if short_channel_id % 2: raise UnknownTx() value = pow(2, 64) - 15 script_pubkey = Script(get_random_bytes(80)) return TxOut(value, script_pubkey)
def test_payment_sent_getters(): payment_preimage = PaymentPreimage(get_random_bytes(32)) event = Event.payment_sent(payment_preimage) assert event.payment_preimage == payment_preimage # Check no other getters are available local_attributes = ["payment_preimage"] check_not_available_getters(event, local_attributes, all_attributes)
def test_update_channel(chain_monitor, channel_monitor, channel_monitor_update_data): outpoint = OutPoint.from_bytes(get_random_bytes(34)) update = ChannelMonitorUpdate.from_bytes(channel_monitor_update_data) # First watch the channel chain_monitor.watch_channel(outpoint, channel_monitor) # Then update chain_monitor.update_channel(outpoint, update)
def test_htlc_output_in_commitment(): amount_msat = 500000 cltv_expiry = 30 payment_hash = PaymentHash(get_random_bytes(32)) htlc_out_1 = HTLCOutputInCommitment(True, amount_msat, cltv_expiry, payment_hash, 0) htlc_out_2 = HTLCOutputInCommitment(False, amount_msat, cltv_expiry, payment_hash, None) assert isinstance(htlc_out_1, HTLCOutputInCommitment) assert isinstance(htlc_out_2, HTLCOutputInCommitment)
def test_payment_failed_getters(): payment_hash = PaymentHash(get_random_bytes(32)) rejected_by_dest = True event = Event.payment_failed(payment_hash, rejected_by_dest) assert event.payment_hash == payment_hash assert event.rejected_by_dest == rejected_by_dest # Check no other getters are available local_attributes = ["payment_hash", "rejected_by_dest"] check_not_available_getters(event, local_attributes, all_attributes)
def test_funding_broadcasting_safe_getters(): outpoint = OutPoint.from_bytes(get_random_bytes(34)) user_channel_id = get_random_int(8) event = Event.funding_broadcasting_safe(outpoint, user_channel_id) assert event.funding_txo == outpoint assert event.user_channel_id == user_channel_id # Check no other getters are available local_attributes = ["funding_txo", "user_channel_id"] check_not_available_getters(event, local_attributes, all_attributes)
def test_funding_generation_ready_getters(): temporary_channel_id = get_random_bytes(32) channel_value_satoshis = 42 output_script = Script(get_random_bytes(50)) user_channel_id = get_random_int(8) event = Event.funding_generation_ready(temporary_channel_id, channel_value_satoshis, output_script, user_channel_id) assert event.temporary_channel_id == temporary_channel_id assert event.channel_value_satoshis == channel_value_satoshis assert event.output_script == output_script assert event.user_channel_id == user_channel_id # Check no other getters are available local_attributes = [ "temporary_channel_id", "channel_value_satoshis", "output_script", "user_channel_id" ] check_not_available_getters(event, local_attributes, all_attributes)
def test_block_connected(channel_monitor, tx): block_header = BlockHeader(get_random_bytes(80)) txdata = [(len(tx), Transaction.from_bytes(tx))] height = 1 broadcaster = BroadcasterInterface(Broadcaster()) fee_estimator = FeeEstimator(FeeEst()) logger = LDKLogger(Logger()) outs_to_watch = channel_monitor.block_connected(block_header, txdata, height, broadcaster, fee_estimator, logger) # The data is completely made up, so there should be no outputs to watch assert outs_to_watch == []
def test_access_get_utxo(): access = Access(A()) genesis_block = BlockHash(get_random_bytes(32)) short_channel_id = 42 access.get_utxo(genesis_block, short_channel_id) # Test exceptions with pytest.raises(UnknownChain): access.get_utxo(BlockHash(bytes(32)), short_channel_id) with pytest.raises(UnknownTx): access.get_utxo(genesis_block, short_channel_id + 1)
def test_payment_received_getters(): payment_hash = PaymentHash(get_random_bytes(32)) payment_secret = None amt = get_random_int(8) event = Event.payment_received(payment_hash, payment_secret, amt) assert event.payment_hash == payment_hash assert event.payment_secret == payment_secret assert event.amt == amt # Check no other getters are available local_attributes = ["payment_hash", "payment_secret", "amt"] check_not_available_getters(event, local_attributes, all_attributes)
def test_htlc_output_in_commitment_getters(): offered = True amount_msat = 500000 cltv_expiry = 30 payment_hash = PaymentHash(get_random_bytes(32)) tx_out_index = None htlc_out = HTLCOutputInCommitment(offered, amount_msat, cltv_expiry, payment_hash, tx_out_index) assert htlc_out.offered == offered assert htlc_out.amount_msat == amount_msat assert htlc_out.cltv_expiry == cltv_expiry assert htlc_out.payment_hash == payment_hash assert htlc_out.transaction_output_index == tx_out_index