Skip to content

Commit

Permalink
pytest: convert test_sendonion_rpc to modern TLV onion.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Feb 28, 2022
1 parent ed03f55 commit 809a407
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions tests/test_pay.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from binascii import hexlify
from fixtures import * # noqa: F401,F403
from fixtures import TEST_NETWORK
from flaky import flaky # noqa: F401
Expand Down Expand Up @@ -2697,25 +2696,41 @@ def test_sendonion_rpc(node_factory):
first_hop = route[0]
blockheight = l1.rpc.getinfo()['blockheight']

def serialize_payload(n):
def truncate_encode(i: int):
"""Encode a tu64 (or tu32 etc) value"""
ret = struct.pack("!Q", i)
while ret.startswith(b'\0'):
ret = ret[1:]
return ret

def serialize_payload_tlv(n):
block, tx, out = n['channel'].split('x')
payload = hexlify(struct.pack(
"!BQQL",
0,
int(block) << 40 | int(tx) << 16 | int(out),
int(n['amount_msat']),
blockheight + n['delay'])).decode('ASCII')
payload += "00" * 12
return payload

def serialize_payload_final_tlv(n, payment_secret: str):
def truncate_encode(i: int):
"""Encode a tu64 (or tu32 etc) value"""
ret = struct.pack("!Q", i)
while ret.startswith(b'\0'):
ret = ret[1:]
return ret
payload = TlvPayload()
# BOLT #4:
# 1. type: 2 (`amt_to_forward`)
# 2. data:
# * [`tu64`:`amt_to_forward`]
b = BytesIO()
b.write(truncate_encode(int(n['amount_msat'])))
payload.add_field(2, b.getvalue())
# BOLT #4:
# 1. type: 4 (`outgoing_cltv_value`)
# 2. data:
# * [`tu32`:`outgoing_cltv_value`]
b = BytesIO()
b.write(truncate_encode(blockheight + n['delay']))
payload.add_field(4, b.getvalue())
# BOLT #4:
# 1. type: 6 (`short_channel_id`)
# 2. data:
# * [`short_channel_id`:`short_channel_id`]
b = BytesIO()
b.write(struct.pack("!Q", int(block) << 40 | int(tx) << 16 | int(out)))
payload.add_field(6, b.getvalue())
return payload.to_bytes().hex()

def serialize_payload_final_tlv(n, payment_secret: str):
payload = TlvPayload()
# BOLT #4:
# 1. type: 2 (`amt_to_forward`)
Expand All @@ -2729,7 +2744,7 @@ def truncate_encode(i: int):
# 2. data:
# * [`tu32`:`outgoing_cltv_value`]
b = BytesIO()
b.write(truncate_encode(n['delay']))
b.write(truncate_encode(blockheight + n['delay']))
payload.add_field(4, b.getvalue())
# BOLT #4:
# 1. type: 8 (`payment_data`)
Expand All @@ -2748,7 +2763,7 @@ def truncate_encode(i: int):
# We tell the node h about the parameters to use for n (a.k.a. h + 1)
hops.append({
"pubkey": h['id'],
"payload": serialize_payload(n)
"payload": serialize_payload_tlv(n)
})

# The last hop has a special payload:
Expand Down

0 comments on commit 809a407

Please sign in to comment.