Rusty Russell
8 years ago
committed by
Christian Decker
7 changed files with 224 additions and 3 deletions
Can't render this file because it has a wrong number of fields in line 2.
|
@ -0,0 +1,91 @@ |
|||
#include <daemon/jsonrpc.h> |
|||
#include <daemon/log.h> |
|||
#include <daemon/sphinx.h> |
|||
#include <lightningd/channel/gen_channel_wire.h> |
|||
#include <lightningd/htlc_end.h> |
|||
#include <lightningd/lightningd.h> |
|||
#include <lightningd/peer_control.h> |
|||
#include <lightningd/subd.h> |
|||
#include <utils.h> |
|||
|
|||
static bool ping_reply(struct subd *subd, const u8 *msg, const int *fds, |
|||
struct command *cmd) |
|||
{ |
|||
u16 totlen; |
|||
bool ok; |
|||
|
|||
log_debug(subd->ld->log, "Got ping reply!"); |
|||
ok = fromwire_channel_ping_reply(msg, NULL, &totlen); |
|||
|
|||
if (!ok) |
|||
command_fail(cmd, "Bad reply message"); |
|||
else { |
|||
struct json_result *response = new_json_result(cmd); |
|||
|
|||
json_object_start(response, NULL); |
|||
json_add_num(response, "totlen", totlen); |
|||
json_object_end(response); |
|||
command_success(cmd, response); |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
static void json_dev_ping(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, *lentok, *pongbytestok; |
|||
unsigned int len, pongbytes; |
|||
|
|||
if (!json_get_params(buffer, params, |
|||
"peerid", &peeridtok, |
|||
"len", &lentok, |
|||
"pongbytes", &pongbytestok, |
|||
NULL)) { |
|||
command_fail(cmd, "Need peerid, len and pongbytes"); |
|||
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 in %s", |
|||
peer->owner ? peer->owner->name : "unattached"); |
|||
return; |
|||
} |
|||
|
|||
if (!json_tok_number(buffer, lentok, &len)) { |
|||
command_fail(cmd, "'%.*s' is not a valid number", |
|||
(int)(lentok->end - lentok->start), |
|||
buffer + lentok->start); |
|||
return; |
|||
} |
|||
|
|||
if (!json_tok_number(buffer, pongbytestok, &pongbytes)) { |
|||
command_fail(cmd, "'%.*s' is not a valid number", |
|||
(int)(pongbytestok->end - pongbytestok->start), |
|||
buffer + pongbytestok->start); |
|||
return; |
|||
} |
|||
|
|||
msg = towire_channel_ping(cmd, pongbytes, len); |
|||
|
|||
/* FIXME: If subdaemon dies? */ |
|||
subd_req(peer->owner, peer->owner, take(msg), -1, 0, ping_reply, cmd); |
|||
} |
|||
|
|||
static const struct json_command dev_ping_command = { |
|||
"dev-ping", |
|||
json_dev_ping, |
|||
"Offer {peerid} a ping of length {len} asking for {pongbytes}", |
|||
"Returns { totlen: u32 } on success" |
|||
}; |
|||
AUTODATA(json_command, &dev_ping_command); |
@ -0,0 +1,56 @@ |
|||
#! /bin/sh -e |
|||
|
|||
# Wherever we are, we want to be in daemon/test dir. |
|||
cd `git rev-parse --show-toplevel`/daemon/test |
|||
|
|||
add_funds() |
|||
{ |
|||
local NEWADDR=`$1 newaddr | get_field address` |
|||
local FUND_INPUT_TXID=`$CLI sendtoaddress $NEWADDR $2` |
|||
local FUND_INPUT_TX=`$CLI getrawtransaction $FUND_INPUT_TXID` |
|||
$1 addfunds $FUND_INPUT_TX |
|||
} |
|||
|
|||
. scripts/vars.sh |
|||
. scripts/helpers.sh |
|||
|
|||
parse_cmdline 2 "$@" |
|||
setup_lightning 2 |
|||
start_lightningd 2 lightningd/lightningd |
|||
|
|||
lcli1 connect localhost $PORT2 $ID2 |
|||
|
|||
add_funds lcli1 0.2 |
|||
|
|||
# Now fund the channels |
|||
CHANNEL_SAT=10000000 |
|||
CHANNEL_MSAT=$(($CHANNEL_SAT * 1000)) |
|||
lcli1 fundchannel $ID2 $CHANNEL_SAT |
|||
|
|||
# Lock them in. |
|||
$CLI generate 10 |
|||
|
|||
check "lcli1 getpeers info | $FGREP 'Funding tx reached depth'" |
|||
|
|||
# 0-byte pong gives just type + length field. |
|||
[ `lcli1 dev-ping $ID2 0 0 | get_field totlen` = 4 ] |
|||
|
|||
# 1000-byte ping, 0-byte pong. |
|||
[ `lcli1 dev-ping $ID2 1000 0 | get_field totlen` = 4 ] |
|||
|
|||
# 1000 byte pong. |
|||
[ `lcli1 dev-ping $ID2 1000 1000 | get_field totlen` = 1004 ] |
|||
|
|||
# Maximum length pong. |
|||
[ `lcli1 dev-ping $ID2 1000 65531 | get_field totlen` = 65535 ] |
|||
|
|||
# Overlength -> no reply. |
|||
[ `lcli1 dev-ping $ID2 1000 65532 | get_field totlen` = 0 ] |
|||
[ `lcli1 dev-ping $ID2 1000 65533 | get_field totlen` = 0 ] |
|||
[ `lcli1 dev-ping $ID2 1000 65534 | get_field totlen` = 0 ] |
|||
[ `lcli1 dev-ping $ID2 1000 65535 | get_field totlen` = 0 ] |
|||
|
|||
lcli1 stop |
|||
lcli2 stop |
|||
|
|||
all_ok |
Loading…
Reference in new issue