Browse Source

newhtlc command: return the HTLC id.

This is in preparation for using the HTLC id in other low-level JSON commands.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
dd895e3c07
  1. 14
      daemon/peer.c
  2. 35
      daemon/test/test.sh

14
daemon/peer.c

@ -3339,6 +3339,8 @@ static void json_newhtlc(struct command *cmd,
unsigned int expiry; unsigned int expiry;
u64 msatoshis; u64 msatoshis;
struct sha256 rhash; struct sha256 rhash;
struct json_result *response = new_json_result(cmd);
struct htlc *htlc;
if (!json_get_params(buffer, params, if (!json_get_params(buffer, params,
"peerid", &peeridtok, "peerid", &peeridtok,
@ -3383,13 +3385,17 @@ static void json_newhtlc(struct command *cmd,
return; return;
} }
if (!command_htlc_add(peer, msatoshis, expiry, &rhash, NULL, htlc = command_htlc_add(peer, msatoshis, expiry, &rhash, NULL,
dummy_single_route(cmd, peer, msatoshis))) { dummy_single_route(cmd, peer, msatoshis));
if (!htlc) {
command_fail(cmd, "could not add htlc"); command_fail(cmd, "could not add htlc");
return; return;
} }
command_success(cmd, null_response(cmd)); json_object_start(response, NULL);
json_add_u64(response, "id", htlc->id);
json_object_end(response);
command_success(cmd, response);
} }
/* FIXME: Use HTLC ids, not r values! */ /* FIXME: Use HTLC ids, not r values! */
@ -3397,7 +3403,7 @@ const struct json_command newhtlc_command = {
"newhtlc", "newhtlc",
json_newhtlc, json_newhtlc,
"Offer {peerid} an HTLC worth {msatoshis} in {expiry} (block number) with {rhash}", "Offer {peerid} an HTLC worth {msatoshis} in {expiry} (block number) with {rhash}",
"Returns an empty result on success" "Returns { id: u64 } result on success"
}; };
/* Looks for their HTLC, but must be committed. */ /* Looks for their HTLC, but must be committed. */

35
daemon/test/test.sh

@ -231,7 +231,18 @@ check_no_peers()
exit 1 exit 1
fi fi
} }
extract_id()
{
XID=`tr -s '\012\011\" ' ' ' | sed -n 's/{ id : \([0-9]*\) }/\1/p'`
case "$XID" in
[0-9]*)
echo $XID;;
*)
return 1;;
esac
}
all_ok() all_ok()
{ {
# Look for valgrind errors. # Look for valgrind errors.
@ -416,7 +427,7 @@ if [ -n "$DIFFERENT_FEES" ]; then
EXPIRY=$(( $(blockheight) + 10)) EXPIRY=$(( $(blockheight) + 10))
SECRET=1de08917a61cb2b62ed5937d38577f6a7bfe59c176781c6d8128018e8b5ccdfd SECRET=1de08917a61cb2b62ed5937d38577f6a7bfe59c176781c6d8128018e8b5ccdfd
RHASH=`lcli1 dev-rhash $SECRET | sed 's/.*"\([0-9a-f]*\)".*/\1/'` RHASH=`lcli1 dev-rhash $SECRET | sed 's/.*"\([0-9a-f]*\)".*/\1/'`
lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH HTLCID=`lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH | extract_id`
[ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2 [ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2
[ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1 [ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1
check_status_single lcli2 0 0 "" $(($AMOUNT - $HTLC_AMOUNT - $ONE_HTLCS_FEE2)) $(($ONE_HTLCS_FEE2)) "{ msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH , state : RCVD_ADD_ACK_REVOCATION } " check_status_single lcli2 0 0 "" $(($AMOUNT - $HTLC_AMOUNT - $ONE_HTLCS_FEE2)) $(($ONE_HTLCS_FEE2)) "{ msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH , state : RCVD_ADD_ACK_REVOCATION } "
@ -458,7 +469,7 @@ EXPIRY=$(( $(blockheight) + 10))
SECRET=1de08917a61cb2b62ed5937d38577f6a7bfe59c176781c6d8128018e8b5ccdfd SECRET=1de08917a61cb2b62ed5937d38577f6a7bfe59c176781c6d8128018e8b5ccdfd
RHASH=`lcli1 dev-rhash $SECRET | sed 's/.*"\([0-9a-f]*\)".*/\1/'` RHASH=`lcli1 dev-rhash $SECRET | sed 's/.*"\([0-9a-f]*\)".*/\1/'`
lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH HTLCID=`lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH | extract_id`
if [ -n "$MANUALCOMMIT" ]; then if [ -n "$MANUALCOMMIT" ]; then
# They should register a staged htlc. # They should register a staged htlc.
@ -577,7 +588,7 @@ check_status $A_AMOUNT $A_FEE "" $B_AMOUNT $B_FEE ""
# A new one, at 10x the amount. # A new one, at 10x the amount.
HTLC_AMOUNT=100000000 HTLC_AMOUNT=100000000
lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH HTLCID=`lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH | extract_id`
[ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2 [ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2
[ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1 [ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1
@ -597,7 +608,7 @@ check_status $A_AMOUNT $A_FEE "" $B_AMOUNT $B_FEE ""
# Same again, but this time it expires. # Same again, but this time it expires.
HTLC_AMOUNT=10000001 HTLC_AMOUNT=10000001
lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH HTLCID=`lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH | extract_id`
[ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2 [ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2
[ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1 [ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1
@ -650,7 +661,7 @@ fi
# First, give more money to node2, so it can offer HTLCs. # First, give more money to node2, so it can offer HTLCs.
EXPIRY=$(( $(blockheight) + 10)) EXPIRY=$(( $(blockheight) + 10))
HTLC_AMOUNT=100000000 HTLC_AMOUNT=100000000
lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH HTLCID=`lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH | extract_id`
[ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2 [ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2
[ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1 [ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1
@ -672,10 +683,10 @@ check_status $A_AMOUNT $A_FEE "" $B_AMOUNT $B_FEE ""
# Now, two HTLCs at once, one from each direction. # Now, two HTLCs at once, one from each direction.
# Both sides can afford this. # Both sides can afford this.
HTLC_AMOUNT=1000000 HTLC_AMOUNT=1000000
lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH HTLCID=`lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH | extract_id`
SECRET2=1de08917a61cb2b62ed5937d38577f6a7bfe59c176781c6d8128018e8b5ccdfe SECRET2=1de08917a61cb2b62ed5937d38577f6a7bfe59c176781c6d8128018e8b5ccdfe
RHASH2=`lcli1 dev-rhash $SECRET2 | sed 's/.*"\([0-9a-f]*\)".*/\1/'` RHASH2=`lcli1 dev-rhash $SECRET2 | sed 's/.*"\([0-9a-f]*\)".*/\1/'`
lcli2 newhtlc $ID1 $HTLC_AMOUNT $EXPIRY $RHASH2 HTLCID2=`lcli2 newhtlc $ID1 $HTLC_AMOUNT $EXPIRY $RHASH2 | extract_id`
[ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2 [ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2
[ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1 [ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1
[ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2 [ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2
@ -730,7 +741,7 @@ check_status $A_AMOUNT $A_FEE "" $B_AMOUNT $B_FEE ""
# Now, test making more changes before receiving commit reply. # Now, test making more changes before receiving commit reply.
lcli2 dev-output $ID1 false lcli2 dev-output $ID1 false
lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH HTLCID=`lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH | extract_id`
# Make sure node1 sends commit (in the background, since it will block!) # Make sure node1 sends commit (in the background, since it will block!)
[ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2 & [ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2 &
@ -747,7 +758,7 @@ fi
check_status_single lcli1 $(($A_AMOUNT)) $(($A_FEE)) "{ msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH , state : SENT_ADD_COMMIT } " $B_AMOUNT $B_FEE "" check_status_single lcli1 $(($A_AMOUNT)) $(($A_FEE)) "{ msatoshis : $HTLC_AMOUNT, expiry : { block : $EXPIRY }, rhash : $RHASH , state : SENT_ADD_COMMIT } " $B_AMOUNT $B_FEE ""
# Now send another offer, and enable node2 output. # Now send another offer, and enable node2 output.
lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH2 HTLCID2=`lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH2 | extract_id`
lcli2 dev-output $ID1 true lcli2 dev-output $ID1 true
[ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1 [ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1
@ -781,7 +792,7 @@ lcli1 dev-routefail true
lcli2 dev-routefail true lcli2 dev-routefail true
RHASH3=`lcli2 accept-payment $HTLC_AMOUNT | sed 's/.*"\([0-9a-f]*\)".*/\1/'` RHASH3=`lcli2 accept-payment $HTLC_AMOUNT | sed 's/.*"\([0-9a-f]*\)".*/\1/'`
lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH3 HTLCID3=`lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH3 | extract_id`
[ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2 [ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2
[ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1 [ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1
@ -799,7 +810,7 @@ RHASH4=`lcli2 accept-payment $HTLC_AMOUNT | sed 's/.*"\([0-9a-f]*\)".*/\1/'`
# Shouldn't have this already. # Shouldn't have this already.
if lcli2 getlog | $FGREP 'Short payment for HTLC'; then exit 1; fi if lcli2 getlog | $FGREP 'Short payment for HTLC'; then exit 1; fi
lcli1 newhtlc $ID2 $(($HTLC_AMOUNT - 1)) $EXPIRY $RHASH4 HTLCID4=`lcli1 newhtlc $ID2 $(($HTLC_AMOUNT - 1)) $EXPIRY $RHASH4 | extract_id`
[ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2 [ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2
[ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1 [ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1

Loading…
Cancel
Save