diff --git a/daemon/peer.c b/daemon/peer.c index 87aa160cb..2adb04ca8 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -3339,6 +3339,8 @@ static void json_newhtlc(struct command *cmd, unsigned int expiry; u64 msatoshis; struct sha256 rhash; + struct json_result *response = new_json_result(cmd); + struct htlc *htlc; if (!json_get_params(buffer, params, "peerid", &peeridtok, @@ -3383,13 +3385,17 @@ static void json_newhtlc(struct command *cmd, return; } - if (!command_htlc_add(peer, msatoshis, expiry, &rhash, NULL, - dummy_single_route(cmd, peer, msatoshis))) { + htlc = command_htlc_add(peer, msatoshis, expiry, &rhash, NULL, + dummy_single_route(cmd, peer, msatoshis)); + if (!htlc) { command_fail(cmd, "could not add htlc"); 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! */ @@ -3397,7 +3403,7 @@ const struct json_command newhtlc_command = { "newhtlc", json_newhtlc, "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. */ diff --git a/daemon/test/test.sh b/daemon/test/test.sh index 972132d77..59d5656da 100755 --- a/daemon/test/test.sh +++ b/daemon/test/test.sh @@ -231,7 +231,18 @@ check_no_peers() exit 1 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() { # Look for valgrind errors. @@ -416,7 +427,7 @@ if [ -n "$DIFFERENT_FEES" ]; then EXPIRY=$(( $(blockheight) + 10)) SECRET=1de08917a61cb2b62ed5937d38577f6a7bfe59c176781c6d8128018e8b5ccdfd 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" ] || 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 } " @@ -458,7 +469,7 @@ EXPIRY=$(( $(blockheight) + 10)) SECRET=1de08917a61cb2b62ed5937d38577f6a7bfe59c176781c6d8128018e8b5ccdfd 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 # 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. 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" ] || lcli2 commit $ID1 @@ -597,7 +608,7 @@ check_status $A_AMOUNT $A_FEE "" $B_AMOUNT $B_FEE "" # Same again, but this time it expires. 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" ] || lcli2 commit $ID1 @@ -650,7 +661,7 @@ fi # First, give more money to node2, so it can offer HTLCs. EXPIRY=$(( $(blockheight) + 10)) 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" ] || 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. # Both sides can afford this. HTLC_AMOUNT=1000000 -lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH +HTLCID=`lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH | extract_id` SECRET2=1de08917a61cb2b62ed5937d38577f6a7bfe59c176781c6d8128018e8b5ccdfe 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" ] || lcli2 commit $ID1 [ ! -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. 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!) [ ! -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 "" # 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 [ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1 @@ -781,7 +792,7 @@ lcli1 dev-routefail true lcli2 dev-routefail true 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" ] || lcli2 commit $ID1 @@ -799,7 +810,7 @@ RHASH4=`lcli2 accept-payment $HTLC_AMOUNT | sed 's/.*"\([0-9a-f]*\)".*/\1/'` # Shouldn't have this already. 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" ] || lcli2 commit $ID1