lightningd: remove dev-newhtlc command.

It added another path to the local-htlc handling, and we have full
invoice support now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-06-20 15:22:03 +09:30
parent 61906ea415
commit ae62509b58
4 changed files with 0 additions and 205 deletions

View File

@ -62,7 +62,6 @@ LIGHTNINGD_LIB_HEADERS := $(LIGHTNINGD_LIB_SRC:.c=.h)
LIGHTNINGD_SRC := \
lightningd/build_utxos.c \
lightningd/dev_newhtlc.c \
lightningd/dev_ping.c \
lightningd/gossip_control.c \
lightningd/htlc_end.c \

View File

@ -1,156 +0,0 @@
#include <ccan/str/hex/hex.h>
#include <daemon/jsonrpc.h>
#include <daemon/log.h>
#include <lightningd/channel/gen_channel_wire.h>
#include <lightningd/htlc_end.h>
#include <lightningd/lightningd.h>
#include <lightningd/peer_control.h>
#include <lightningd/sphinx.h>
#include <lightningd/subd.h>
#include <utils.h>
static bool offer_htlc_reply(struct subd *subd, const u8 *msg, const int *fds,
struct htlc_end *hend)
{
u16 failcode;
u8 *failstr;
/* We hack this in, since we don't have a real pay_command here. */
struct command *cmd = (void *)hend->pay_command;
/* This suppresses normal callback when it's actually paid! */
hend->pay_command = NULL;
if (!fromwire_channel_offer_htlc_reply(msg, msg, NULL,
&hend->htlc_id,
&failcode, &failstr)) {
command_fail(cmd, "Invalid reply from daemon: %s",
tal_hex(msg, msg));
return true;
}
if (failcode != 0) {
command_fail(cmd, "failure %u: %.*s", failcode,
(int)tal_len(failstr), (char *)failstr);
} else {
struct json_result *response = new_json_result(cmd);
/* Peer owns it now (we're about to free cmd) */
tal_steal(hend->peer, hend);
connect_htlc_end(&subd->ld->htlc_ends, hend);
json_object_start(response, NULL);
json_add_u64(response, "id", hend->htlc_id);
json_object_end(response);
command_success(cmd, response);
}
return true;
}
static void json_dev_newhtlc(struct command *cmd,
const char *buffer, const jsmntok_t *params)
{
struct lightningd *ld = ld_from_dstate(cmd->dstate);
struct peer *peer;
u8 *msg;
jsmntok_t *peeridtok, *msatoshitok, *expirytok, *rhashtok;
unsigned int expiry;
u64 msatoshi;
struct sha256 rhash;
struct hop_data *hopsdata;
u8 sessionkey[32];
struct onionpacket *packet;
u8 *onion;
struct htlc_end *hend;
struct pubkey *path = tal_arrz(cmd, struct pubkey, 1);
struct secret *shared_secrets;
if (!json_get_params(buffer, params,
"peerid", &peeridtok,
"msatoshi", &msatoshitok,
"expiry", &expirytok,
"rhash", &rhashtok,
NULL)) {
command_fail(cmd, "Need peerid, msatoshi, expiry and rhash");
return;
}
peer = peer_from_json(ld, buffer, peeridtok);
if (!peer) {
command_fail(cmd, "Could not find peer with that peerid");
return;
}
/* FIXME: These checks are horrible, use a peer flag to say it's
* ready to forward! */
if (peer->owner && !streq(peer->owner->name, "lightningd_channel")) {
command_fail(cmd, "Peer not in lightningd_channel (%s instead)",
peer->owner ? peer->owner->name : "unattached");
return;
}
if (!peer_can_add_htlc(peer)) {
command_fail(cmd, "Peer in state %s",
peer_state_name(peer->state));
return;
}
if (!json_tok_u64(buffer, msatoshitok, &msatoshi)) {
command_fail(cmd, "'%.*s' is not a valid number",
(int)(msatoshitok->end - msatoshitok->start),
buffer + msatoshitok->start);
return;
}
if (!json_tok_number(buffer, expirytok, &expiry)) {
command_fail(cmd, "'%.*s' is not a valid number",
(int)(expirytok->end - expirytok->start),
buffer + expirytok->start);
return;
}
if (!hex_decode(buffer + rhashtok->start,
rhashtok->end - rhashtok->start,
&rhash, sizeof(rhash))) {
command_fail(cmd, "'%.*s' is not a valid sha256 hash",
(int)(rhashtok->end - rhashtok->start),
buffer + rhashtok->start);
return;
}
tal_arr(cmd, struct pubkey, 1);
hopsdata = tal_arr(cmd, struct hop_data, 1);
hopsdata[0].realm = 0;
hopsdata[0].amt_forward = msatoshi;
hopsdata[0].outgoing_cltv = expiry;
/* This is the last hop so set an empty channel_id */
memset(&hopsdata[0].channel_id, 0, sizeof(hopsdata[0].channel_id));
path[0] = peer->id;
randombytes_buf(&sessionkey, sizeof(sessionkey));
packet = create_onionpacket(cmd, path, hopsdata, sessionkey, rhash.u.u8,
sizeof(rhash), &shared_secrets);
onion = serialize_onionpacket(cmd, packet);
log_debug(peer->log, "JSON command to add new HTLC");
hend = tal(cmd, struct htlc_end);
hend->which_end = HTLC_DST;
hend->hstate = SENT_ADD_HTLC;
hend->peer = peer;
hend->msatoshis = msatoshi;
hend->other_end = NULL;
hend->pay_command = (void *)cmd;
hend->path_secrets = tal_steal(hend, shared_secrets);
/* FIXME: If subdaemon dies? */
msg = towire_channel_offer_htlc(cmd, msatoshi, expiry, &rhash, onion);
subd_req(hend, peer->owner, take(msg), -1, 0, offer_htlc_reply, hend);
}
static const struct json_command dev_newhtlc_command = {
"dev-newhtlc",
json_dev_newhtlc,
"Offer {peerid} an HTLC worth {msatoshi} in {expiry} (block number) with {rhash}",
"Returns { id: u64 } result on success"
};
AUTODATA(json_command, &dev_newhtlc_command);

View File

@ -60,13 +60,6 @@ static void json_pay_failed(struct pay_command *pc,
void payment_succeeded(struct lightningd *ld, struct htlc_end *dst,
const struct preimage *rval)
{
/* FIXME: dev_htlc will do this! */
if (!dst->pay_command) {
log_debug(ld->log, "Payment succeeded on HTLC %"PRIu64,
dst->htlc_id);
return;
}
assert(!dst->pay_command->rval);
dst->pay_command->rval = tal_dup(dst->pay_command,
struct preimage, rval);
@ -79,10 +72,6 @@ void payment_failed(struct lightningd *ld, struct htlc_end *dst,
const struct pubkey *sender,
enum onion_type failure_code)
{
/* FIXME: dev_htlc will do this! */
if (!dst->pay_command)
return;
/* FIXME: check for routing failure / perm fail. */
/* check_for_routing_failure(i, sender, failure_code); */
json_pay_failed(dst->pay_command, sender, failure_code,

View File

@ -204,43 +204,6 @@ class LightningDTests(BaseLightningDTests):
assert p1['owner'] == 'lightningd_gossip'
assert p2['owner'] == 'lightningd_gossip'
def test_htlc(self):
l1,l2 = self.connect()
self.fund_channel(l1, l2, 10**6)
l1.daemon.wait_for_log('-> CHANNELD_NORMAL')
l2.daemon.wait_for_log('-> CHANNELD_NORMAL')
secret = '1de08917a61cb2b62ed5937d38577f6a7bfe59c176781c6d8128018e8b5ccdfd'
rhash = l1.rpc.dev_rhash(secret)['rhash']
# This is actually dust, and uncalled for.
l1.rpc.dev_newhtlc(l2.info['id'], 100000, l1.bitcoin.rpc.getblockcount() + 10, rhash)
l1.daemon.wait_for_log('Sending commit_sig with 0 htlc sigs')
l2.daemon.wait_for_log('their htlc 0 locked')
l2.daemon.wait_for_log('failed htlc 0 code 0x400f')
l1.daemon.wait_for_log('htlc 0 failed from 0th node with code 0x400f')
# Set up invoice (non-dust, just to test), and pay it.
# This one isn't dust.
rhash = l2.rpc.invoice(100000000, 'testpayment1')['rhash']
assert l2.rpc.listinvoice('testpayment1')[0]['complete'] == False
l1.rpc.dev_newhtlc(l2.info['id'], 100000000, l1.bitcoin.rpc.getblockcount() + 10, rhash)
l1.daemon.wait_for_log('Sending commit_sig with 1 htlc sigs')
l2.daemon.wait_for_log('their htlc 1 locked')
l2.daemon.wait_for_log("Resolving invoice 'testpayment1' with HTLC 1")
assert l2.rpc.listinvoice('testpayment1')[0]['complete'] == True
l1.daemon.wait_for_log('Payment succeeded on HTLC 1')
# Balances should have changed.
p1 = l1.rpc.getpeer(l2.info['id'])
p2 = l2.rpc.getpeer(l1.info['id'])
assert p1['msatoshi_to_us'] == 900000000
assert p1['msatoshi_to_them'] == 100000000
assert p2['msatoshi_to_us'] == 100000000
assert p2['msatoshi_to_them'] == 900000000
def test_sendpay(self):
l1,l2 = self.connect()