Skip to content

Commit

Permalink
pytest: Add a test for the sendonion method
Browse files Browse the repository at this point in the history
  • Loading branch information
cdecker committed Nov 14, 2019
1 parent 8cb8861 commit 05c5146
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions tests/test_pay.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from binascii import hexlify
from fixtures import * # noqa: F401,F403
from fixtures import TEST_NETWORK
from flaky import flaky # noqa: F401
from lightning import RpcError, Millisatoshi
from utils import DEVELOPER, wait_for, only_one, sync_blockheight, SLOW_MACHINE, TIMEOUT, VALGRIND, EXPERIMENTAL_FEATURES


import copy
import concurrent.futures
import copy
import os
import pytest
import random
import re
import string
import struct
import time
import unittest

Expand Down Expand Up @@ -2467,3 +2468,47 @@ def test_createonion_rpc(node_factory):
# The trailer is generated using the filler and can be ued as a
# checksum. This trailer is from the test-vector in the specs.
assert(res['onion'].endswith('be89e4701eb870f8ed64fafa446c78df3ea'))


def test_sendonion_rpc(node_factory):
l1, l2, l3, l4 = node_factory.line_graph(4, wait_for_announce=True)
amt = 10**3
route = l1.rpc.getroute(l4.info['id'], 10**3, 10)['route']
inv = l4.rpc.invoice(amt, "lbl", "desc")

first_hop = route[0]
blockheight = l1.rpc.getinfo()['blockheight']

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

# Need to shift the parameters by one hop
hops = []
for h, n in zip(route[:-1], route[1:]):
# We tell the node h about the parameters to use for n (a.k.a. h + 1)
hops.append({
"type": "legacy",
"pubkey": h['id'],
"payload": serialize_payload(n)
})

# The last hop has a special payload:
hops.append({
"type": "legacy",
"pubkey": route[-1]['id'],
"payload": serialize_payload(route[-1])
})

onion = l1.rpc.createonion(hops=hops, assocdata=inv['payment_hash'])

l1.rpc.sendonion(onion=onion['onion'], first_hop=first_hop,
payment_hash=inv['payment_hash'])

l1.rpc.waitsendpay(payment_hash=inv['payment_hash'])

0 comments on commit 05c5146

Please sign in to comment.