Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rustyrussell/pettycoin
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 17213f858254
Choose a base ref
...
head repository: rustyrussell/pettycoin
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5335d49e011f
Choose a head ref
  • 2 commits
  • 8 files changed
  • 1 contributor

Commits on Oct 20, 2014

  1. pettycoin: record input and output times, and status.

    This should help debug issue #32 (connections lost between nodes).
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Oct 20, 2014
    Copy the full SHA
    fbd25e5 View commit details
  2. getpeerinfo: new RPC command.

    Lots of information to help us debug issue #32.
    
    Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
    rustyrussell committed Oct 20, 2014
    Copy the full SHA
    5335d49 View commit details
Showing with 142 additions and 1 deletion.
  1. +1 −1 Makefile.in
  2. +102 −0 getpeerinfo.c
  3. +1 −0 jsonrpc.c
  4. +1 −0 jsonrpc.h
  5. +9 −0 packet_io.c
  6. +19 −0 peer.c
  7. +7 −0 peer.h
  8. +2 −0 test/run-01-jsonrpc.c
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
# blackbox-check: run the blackbox tests
# update-mocks: regenerate the mocks for the unit tests.

PETTYCOIN_OBJS := block.o check_block.o check_tx.o difficulty.o shadouble.o timestamp.o gateways.o hash_tx.o pettycoin.o merkle_txs.o merkle_recurse.o tx_cmp.o genesis.o marshal.o hash_block.o prev_txhashes.o state.o tal_packet.o dns.o netaddr.o peer.o peer_cache.o pseudorand.o welcome.o log.o generating.o blockfile.o pending.o log_helper.o txhash.o signature.o proof.o chain.o features.o todo.o base58.o sync.o create_refs.o shard.o packet_io.o tx.o complain.o block_shard.o recv_block.o input_refs.o peer_wants.o inputhash.o tx_in_hashes.o merkle_hashes.o recv_tx.o reward.o recv_complain.o json.o jsonrpc.o getinfo.o ecode_names.o sendrawtransaction.c pettycoin_dir.o pkt_names.o hex.o listtransactions.o json_add_tx.o gettransaction.o prev_blocks.o detached_block.o
PETTYCOIN_OBJS := block.o check_block.o check_tx.o difficulty.o shadouble.o timestamp.o gateways.o hash_tx.o pettycoin.o merkle_txs.o merkle_recurse.o tx_cmp.o genesis.o marshal.o hash_block.o prev_txhashes.o state.o tal_packet.o dns.o netaddr.o peer.o peer_cache.o pseudorand.o welcome.o log.o generating.o blockfile.o pending.o log_helper.o txhash.o signature.o proof.o chain.o features.o todo.o base58.o sync.o create_refs.o shard.o packet_io.o tx.o complain.o block_shard.o recv_block.o input_refs.o peer_wants.o inputhash.o tx_in_hashes.o merkle_hashes.o recv_tx.o reward.o recv_complain.o json.o jsonrpc.o getinfo.o ecode_names.o sendrawtransaction.c pettycoin_dir.o pkt_names.o hex.o listtransactions.o json_add_tx.o gettransaction.o prev_blocks.o detached_block.o getpeerinfo.o
PETTYCOIN_GENERATE_OBJS := pettycoin-generate.o merkle_hashes.o merkle_recurse.o hash_tx.o tx_cmp.o shadouble.o marshal.o minimal_log.o tal_packet.o hex.o
MKGENESIS_OBJS := mkgenesis.o shadouble.o hash_block.o merkle_hashes.o merkle_recurse.o minimal_log.o
SIZES_OBJS := sizes.o
102 changes: 102 additions & 0 deletions getpeerinfo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "ecode_names.h"
#include "jsonrpc.h"
#include "peer.h"
#include "pkt_names.h"
#include "state.h"
#include <ccan/io/io.h>
#include <poll.h>

/* {
"addr" : "54.69.232.231:8968",
"addrlocal" : "128.199.137.156:18333",
"services" : "00000001",
"lastsend" : 1413765750,
"lastrecv" : 1413764032,
"bytessent" : 398206,
"bytesrecv" : 485,
"conntime" : 1413763882,
"pingtime" : 0.00000000,
"version" : 70002,
"subver" : "/Satoshi:0.9.2/",
"inbound" : true,
"startingheight" : 324500,
"banscore" : 0,
"syncnode" : false
}
*/

static char *json_getpeerinfo(struct json_connection *jcon,
const jsmntok_t *params,
char **response)
{
struct peer *peer;
struct state *state = jcon->state;

json_array_start(response, NULL);
list_for_each(&state->peers, peer, list) {
const struct protocol_net_hdr *pkt;
struct pollfd fds;

assert(peer->state == state);
json_object_start(response, NULL);
json_add_num(response, "peer_num", peer->peer_num);
assert(bitmap_test_bit(state->peer_map, peer->peer_num));
json_add_bool(response, "we_are_syncing", peer->we_are_syncing);
json_add_bool(response, "they_are_syncing", peer->they_are_syncing);

/* These should be the same. */
json_add_num(response, "fd", peer->fd);
json_add_num(response, "conn_fd", io_conn_fd(peer->conn));

json_add_string(response, "error",
peer->error_pkt ?
ecode_name(le32_to_cpu(peer->error_pkt->error)) :
ecode_name(PROTOCOL_ECODE_NONE));

json_add_num(response, "last-input-time",
peer->last_time_in.ts.tv_sec);
json_add_string(response, "last-input-type",
pkt_name(peer->last_type_in));
json_add_num(response, "last-input-length", peer->last_len_in);

/* Examining incoming is unreliable! */
pkt = peer->incoming;
json_add_bool(response, "input-pending", peer->in_pending);
json_add_num(response, "incoming-len",
pkt ? le32_to_cpu(pkt->len) : -1);
json_add_string(response, "incoming-type",
pkt ? pkt_name(le32_to_cpu(pkt->type)) : "NONE");

json_add_num(response, "last-output-time",
peer->last_time_out.ts.tv_sec);
json_add_string(response, "last-output-type",
pkt_name(peer->last_type_out));
json_add_num(response, "last-output-length", peer->last_len_out);
json_add_bool(response, "output-pending", peer->out_pending);

pkt = peer->outgoing;
json_add_num(response, "outgoing-len",
pkt ? le32_to_cpu(pkt->len) : -1);
json_add_string(response, "outgoing-type",
pkt ? pkt_name(le32_to_cpu(pkt->type)) : "NONE");

fds.fd = peer->fd;
fds.events = POLLIN|POLLOUT;

poll(&fds, 1, 0);
json_add_bool(response, "fd-ok", !(fds.revents & POLLNVAL));
json_add_bool(response, "fd-readable", fds.revents & POLLIN);
json_add_bool(response, "fd-writable", fds.revents & POLLOUT);

json_object_end(response);
}
json_array_end(response);

return NULL;
}

const struct json_command getpeerinfo_command = {
"getpeerinfo", json_getpeerinfo,
"Get information about our peers",
""
};
1 change: 1 addition & 0 deletions jsonrpc.c
Original file line number Diff line number Diff line change
@@ -78,6 +78,7 @@ static const struct json_command *cmdlist[] = {
&help_command, &getinfo_command, &sendrawtransaction_command,
&stop_command, &listtransactions_command, &getblock_command,
&getblockhash_command, &submitblock_command, &gettransaction_command,
&getpeerinfo_command,
/* Developer/debugging options. */
&echo_command, &listtodo_command
};
1 change: 1 addition & 0 deletions jsonrpc.h
Original file line number Diff line number Diff line change
@@ -51,5 +51,6 @@ extern const struct json_command getblock_command;
extern const struct json_command getblockhash_command;
extern const struct json_command submitblock_command;
extern const struct json_command gettransaction_command;
extern const struct json_command getpeerinfo_command;

#endif /* PETTYCOIN_JSONRPC_H */
9 changes: 9 additions & 0 deletions packet_io.c
Original file line number Diff line number Diff line change
@@ -120,7 +120,9 @@ struct io_plan *peer_read_packet(struct peer *peer,
struct peer *))
{
assert(get_log_for_fd(io_conn_fd(peer->conn)));
assert(!peer->in_pending);

peer->in_pending = true;
return io_read_packet(peer->conn, &peer->incoming, cb, peer);
}

@@ -131,6 +133,8 @@ struct io_plan *peer_write_packet(struct peer *peer, const void *pkt,
{
le32 len;

assert(!peer->out_pending);

tal_free(peer->outgoing);
peer->outgoing = pkt;

@@ -139,6 +143,11 @@ struct io_plan *peer_write_packet(struct peer *peer, const void *pkt,
assert(le32_to_cpu(len) >= sizeof(struct protocol_net_hdr));
assert(le32_to_cpu(len) <= PROTOCOL_MAX_PACKET_LEN);

peer->last_time_out = time_now();
peer->last_type_out = le32_to_cpu(((struct protocol_net_hdr*)pkt)->type);
peer->last_len_out = le32_to_cpu(len);
peer->out_pending = true;

log_io(peer->log, false, pkt, le32_to_cpu(len));
return io_write(peer->conn, pkt, le32_to_cpu(len), next, peer);
}
19 changes: 19 additions & 0 deletions peer.c
Original file line number Diff line number Diff line change
@@ -444,6 +444,8 @@ static struct io_plan *plan_output(struct io_conn *conn, struct peer *peer)
{
void *pkt;

peer->out_pending = false;

/* There was an error? Send that then close. */
if (peer->error_pkt) {
log_info(peer->log, "sending error packet ");
@@ -1296,6 +1298,11 @@ static struct io_plan *pkt_in(struct io_conn *conn, struct peer *peer)
enum protocol_ecode err;
void *reply = NULL;

peer->in_pending = false;
peer->last_time_in = time_now();
peer->last_type_in = le32_to_cpu(hdr->type);
peer->last_len_in = le32_to_cpu(hdr->len);

len = le32_to_cpu(hdr->len);
type = le32_to_cpu(hdr->type);

@@ -1467,6 +1474,11 @@ static struct io_plan *welcome_received(struct io_conn *conn, struct peer *peer)
struct block *prev;
const struct protocol_block_header *hdr;

peer->in_pending = false;
peer->last_time_in = time_now();
peer->last_type_in = le32_to_cpu(peer->welcome->type);
peer->last_len_in = le32_to_cpu(peer->welcome->len);

log_debug(peer->log, "Their welcome received");

tal_steal(peer, peer->welcome);
@@ -1548,7 +1560,9 @@ static struct io_plan *welcome_received(struct io_conn *conn, struct peer *peer)

static struct io_plan *welcome_sent(struct io_conn *conn, struct peer *peer)
{
peer->out_pending = false;
log_debug(peer->log, "Our welcome sent, awaiting theirs");
peer->in_pending = true;
return io_read_packet(conn, &peer->welcome, welcome_received, peer);
}

@@ -1624,6 +1638,11 @@ struct io_plan *peer_connected(struct io_conn *conn, struct state *state,
peer->you = *addr;
peer->conn = conn;
peer->fd = io_conn_fd(conn);
peer->last_time_in.ts.tv_sec = peer->last_time_out.ts.tv_sec = 0;
peer->last_time_in.ts.tv_nsec = peer->last_time_out.ts.tv_nsec = 0;
peer->last_type_in = peer->last_type_out = PROTOCOL_PKT_NONE;
peer->last_len_in = peer->last_len_out = 0;
peer->out_pending = peer->in_pending = 0;

/* Use address as log prefix. */
sprintf(prefix, "Peer %u @", peer->peer_num);
7 changes: 7 additions & 0 deletions peer.h
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
#include "block_info.h"
#include "protocol_net.h"
#include <ccan/list/list.h>
#include <ccan/time/time.h>
#include <stdbool.h>

#define MAX_PEERS 64
@@ -54,6 +55,12 @@ struct peer {
struct protocol_pkt_welcome *welcome;
struct welcome_block wblock;

/* Debugging */
struct timeabs last_time_in, last_time_out;
enum protocol_pkt_type last_type_in, last_type_out;
size_t last_len_in, last_len_out;
bool out_pending, in_pending;

/* Number of requests we have outstanding (see todo.c) */
unsigned int requests_outstanding;

2 changes: 2 additions & 0 deletions test/run-01-jsonrpc.c
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@ const struct json_command getblock_command;
const struct json_command getblockhash_command;
/* Generated stub for getinfo_command */
const struct json_command getinfo_command;
/* Generated stub for getpeerinfo_command */
const struct json_command getpeerinfo_command;
/* Generated stub for gettransaction_command */
const struct json_command gettransaction_command;
/* Generated stub for listtodo_command */