Rusty Russell
8 years ago
6 changed files with 335 additions and 1 deletions
@ -0,0 +1,66 @@ |
|||
#! /usr/bin/make
|
|||
|
|||
# Designed to be run one level up
|
|||
lightningd/channel-wrongdir: |
|||
$(MAKE) -C ../.. lightningd/channel-all |
|||
|
|||
default: lightningd/channel-all |
|||
|
|||
lightningd/channel-all: lightningd/lightningd_channel |
|||
|
|||
# lightningd/channel needs these:
|
|||
LIGHTNINGD_CHANNEL_HEADERS_GEN := \
|
|||
lightningd/channel/gen_channel_control_wire.h \
|
|||
lightningd/channel/gen_channel_status_wire.h |
|||
|
|||
LIGHTNINGD_CHANNEL_HEADERS_NOGEN := |
|||
|
|||
LIGHTNINGD_CHANNEL_HEADERS := $(LIGHTNINGD_CHANNEL_HEADERS_GEN) $(LIGHTNINGD_CHANNEL_HEADERS_NOGEN) |
|||
|
|||
LIGHTNINGD_CHANNEL_SRC := lightningd/channel/channel.c \
|
|||
$(LIGHTNINGD_CHANNEL_HEADERS:.h=.c) |
|||
LIGHTNINGD_CHANNEL_OBJS := $(LIGHTNINGD_CHANNEL_SRC:.c=.o) |
|||
|
|||
# Control daemon uses this:
|
|||
LIGHTNINGD_CHANNEL_CONTROL_HEADERS := $(LIGHTNINGD_CHANNEL_HEADERS) |
|||
LIGHTNINGD_CHANNEL_CONTROL_SRC := $(LIGHTNINGD_CHANNEL_HEADERS:.h=.c) |
|||
LIGHTNINGD_CHANNEL_CONTROL_OBJS := $(LIGHTNINGD_CHANNEL_CONTROL_SRC:.c=.o) |
|||
|
|||
LIGHTNINGD_CHANNEL_GEN_SRC := $(filter lightningd/channel/gen_%, $(LIGHTNINGD_CHANNEL_SRC) $(LIGHTNINGD_CHANNEL_CONTROL_SRC)) |
|||
|
|||
LIGHTNINGD_CHANNEL_SRC_NOGEN := $(filter-out lightningd/channel/gen_%, $(LIGHTNINGD_CHANNEL_SRC)) |
|||
|
|||
# Add to headers which any object might need.
|
|||
LIGHTNINGD_HEADERS_GEN += $(LIGHTNINGD_CHANNEL_HEADERS_GEN) |
|||
LIGHTNINGD_HEADERS_NOGEN += $(LIGHTNINGD_CHANNEL_HEADERS_NOGEN) |
|||
|
|||
$(LIGHTNINGD_CHANNEL_OBJS): $(LIGHTNINGD_HEADERS) |
|||
|
|||
lightningd/channel/gen_channel_control_wire.h: $(WIRE_GEN) lightningd/channel/channel_control_wire.csv |
|||
$(WIRE_GEN) --header $@ channel_control_wire_type < lightningd/channel/channel_control_wire.csv > $@ |
|||
|
|||
lightningd/channel/gen_channel_control_wire.c: $(WIRE_GEN) lightningd/channel/channel_control_wire.csv |
|||
$(WIRE_GEN) ${@:.c=.h} channel_control_wire_type < lightningd/channel/channel_control_wire.csv > $@ |
|||
|
|||
lightningd/channel/gen_channel_status_wire.h: $(WIRE_GEN) lightningd/channel/channel_status_wire.csv |
|||
$(WIRE_GEN) --header $@ channel_status_wire_type < lightningd/channel/channel_status_wire.csv > $@ |
|||
|
|||
lightningd/channel/gen_channel_status_wire.c: $(WIRE_GEN) lightningd/channel/channel_status_wire.csv |
|||
$(WIRE_GEN) ${@:.c=.h} channel_status_wire_type < lightningd/channel/channel_status_wire.csv > $@ |
|||
|
|||
LIGHTNINGD_CHANNEL_OBJS := $(LIGHTNINGD_CHANNEL_SRC:.c=.o) $(LIGHTNINGD_CHANNEL_GEN_SRC:.c=.o) |
|||
|
|||
lightningd/lightningd_channel: $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIGHTNINGD_CHANNEL_OBJS) $(CORE_OBJS) $(CORE_TX_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(CCAN_OBJS) $(LIGHTNINGD_HSM_CLIENT_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a libsodium.a libwallycore.a |
|||
$(CC) $(CFLAGS) -o $@ $^ $(LDLIBS) |
|||
|
|||
check-source: $(LIGHTNINGD_CHANNEL_SRC_NOGEN:%=check-src-include-order/%) |
|||
check-source-bolt: $(LIGHTNINGD_CHANNEL_SRC:%=bolt-check/%) $(LIGHTNINGD_CHANNEL_HEADERS:%=bolt-check/%) |
|||
|
|||
check-whitespace: $(LIGHTNINGD_CHANNEL_SRC_NOGEN:%=check-whitespace/%) $(LIGHTNINGD_CHANNEL_HEADERS_NOGEN:%=check-whitespace/%) |
|||
|
|||
clean: lightningd/channel-clean |
|||
|
|||
lightningd/channel-clean: |
|||
$(RM) $(LIGHTNINGD_CHANNEL_OBJS) gen_* |
|||
|
|||
-include lightningd/channel/test/Makefile |
@ -0,0 +1,226 @@ |
|||
#include <bitcoin/privkey.h> |
|||
#include <bitcoin/script.h> |
|||
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h> |
|||
#include <ccan/crypto/shachain/shachain.h> |
|||
#include <ccan/fdpass/fdpass.h> |
|||
#include <ccan/io/io.h> |
|||
#include <ccan/structeq/structeq.h> |
|||
#include <errno.h> |
|||
#include <inttypes.h> |
|||
#include <lightningd/channel.h> |
|||
#include <lightningd/channel/gen_channel_control_wire.h> |
|||
#include <lightningd/channel/gen_channel_status_wire.h> |
|||
#include <lightningd/commit_tx.h> |
|||
#include <lightningd/crypto_sync.h> |
|||
#include <lightningd/cryptomsg.h> |
|||
#include <lightningd/debug.h> |
|||
#include <lightningd/derive_basepoints.h> |
|||
#include <lightningd/key_derive.h> |
|||
#include <lightningd/peer_failed.h> |
|||
#include <secp256k1.h> |
|||
#include <signal.h> |
|||
#include <status.h> |
|||
#include <stdio.h> |
|||
#include <type_to_string.h> |
|||
#include <version.h> |
|||
#include <wire/gen_peer_wire.h> |
|||
#include <wire/wire.h> |
|||
#include <wire/wire_io.h> |
|||
#include <wire/wire_sync.h> |
|||
|
|||
/* Stdout == status, stdin == requests, 3 == peer */ |
|||
#define STATUS_FD STDOUT_FILENO |
|||
#define REQ_FD STDIN_FILENO |
|||
#define PEER_FD 3 |
|||
|
|||
struct peer { |
|||
struct peer_crypto_state pcs; |
|||
struct channel_config conf[NUM_SIDES]; |
|||
struct pubkey next_per_commit[NUM_SIDES]; |
|||
bool funding_locked[NUM_SIDES]; |
|||
|
|||
/* Their sig for current commit. */ |
|||
secp256k1_ecdsa_signature their_commit_sig; |
|||
|
|||
/* Secret keys and basepoint secrets. */ |
|||
struct secrets our_secrets; |
|||
|
|||
/* Our shaseed for generating per-commitment-secrets. */ |
|||
struct sha256 shaseed; |
|||
|
|||
struct channel_id channel_id; |
|||
struct channel *channel; |
|||
|
|||
u8 *req_in; |
|||
const u8 **peer_out; |
|||
}; |
|||
|
|||
static void msg_enqueue(const u8 ***q, const u8 *add) |
|||
{ |
|||
size_t n = tal_count(*q); |
|||
tal_resize(q, n+1); |
|||
(*q)[n] = add; |
|||
} |
|||
|
|||
static const u8 *msg_dequeue(const u8 ***q) |
|||
{ |
|||
size_t n = tal_count(*q); |
|||
const u8 *msg; |
|||
|
|||
if (!n) |
|||
return NULL; |
|||
msg = (*q)[0]; |
|||
memmove(*q, *q + 1, sizeof(**q) * (n-1)); |
|||
tal_resize(q, n-1); |
|||
return msg; |
|||
} |
|||
|
|||
static void queue_pkt(struct peer *peer, const u8 *msg) |
|||
{ |
|||
msg_enqueue(&peer->peer_out, msg); |
|||
io_wake(peer); |
|||
} |
|||
|
|||
static struct io_plan *peer_out(struct io_conn *conn, struct peer *peer) |
|||
{ |
|||
const u8 *out = msg_dequeue(&peer->peer_out); |
|||
if (!out) |
|||
return io_out_wait(conn, peer, peer_out, peer); |
|||
|
|||
return peer_write_message(conn, &peer->pcs, out, peer_out); |
|||
} |
|||
|
|||
static struct io_plan *peer_in(struct io_conn *conn, struct peer *peer, u8 *msg) |
|||
{ |
|||
struct channel_id chanid; |
|||
|
|||
status_trace("Received %s from peer", |
|||
wire_type_name(fromwire_peektype(msg))); |
|||
|
|||
if (fromwire_funding_locked(msg, NULL, &chanid, |
|||
&peer->next_per_commit[REMOTE])) { |
|||
if (!structeq(&chanid, &peer->channel_id)) |
|||
status_failed(WIRE_CHANNEL_PEER_BAD_MESSAGE, |
|||
"Wrong channel id in %s", |
|||
tal_hex(trc, msg)); |
|||
if (peer->funding_locked[REMOTE]) |
|||
status_failed(WIRE_CHANNEL_PEER_BAD_MESSAGE, |
|||
"Funding locked twice"); |
|||
peer->funding_locked[REMOTE] = true; |
|||
status_send(towire_channel_received_funding_locked(peer)); |
|||
|
|||
if (peer->funding_locked[LOCAL]) |
|||
status_send(towire_channel_normal_operation(peer)); |
|||
} |
|||
/* FIXME: Process gossip. */ |
|||
|
|||
return peer_read_message(conn, &peer->pcs, peer_in); |
|||
} |
|||
|
|||
static struct io_plan *req_in(struct io_conn *conn, struct peer *peer) |
|||
{ |
|||
if (fromwire_channel_funding_locked(peer->req_in, NULL)) { |
|||
u8 *msg = towire_funding_locked(peer, |
|||
&peer->channel_id, |
|||
&peer->next_per_commit[LOCAL]); |
|||
queue_pkt(peer, msg); |
|||
peer->funding_locked[LOCAL] = true; |
|||
|
|||
if (peer->funding_locked[REMOTE]) |
|||
status_send(towire_channel_normal_operation(peer)); |
|||
} else |
|||
status_failed(WIRE_CHANNEL_BAD_COMMAND, "%s", strerror(errno)); |
|||
|
|||
return io_read_wire(conn, peer, &peer->req_in, req_in, peer); |
|||
} |
|||
|
|||
static struct io_plan *setup_req_in(struct io_conn *conn, struct peer *peer) |
|||
{ |
|||
return io_read_wire(conn, peer, &peer->req_in, req_in, peer); |
|||
} |
|||
|
|||
static struct io_plan *setup_peer_conn(struct io_conn *conn, struct peer *peer) |
|||
{ |
|||
return io_duplex(conn, peer_read_message(conn, &peer->pcs, peer_in), |
|||
peer_out(conn, peer)); |
|||
} |
|||
|
|||
static void peer_conn_broken(struct io_conn *conn, struct peer *peer) |
|||
{ |
|||
status_failed(WIRE_CHANNEL_PEER_READ_FAILED, |
|||
"peer connection broken: %s", strerror(errno)); |
|||
} |
|||
|
|||
#ifndef TESTING |
|||
int main(int argc, char *argv[]) |
|||
{ |
|||
u8 *msg; |
|||
struct peer *peer = tal(NULL, struct peer); |
|||
struct privkey seed; |
|||
struct basepoints points[NUM_SIDES]; |
|||
struct pubkey funding_pubkey[NUM_SIDES]; |
|||
u32 feerate; |
|||
u64 funding_satoshi, push_msat; |
|||
u16 funding_txout; |
|||
struct sha256_double funding_txid; |
|||
bool am_funder; |
|||
|
|||
if (argc == 2 && streq(argv[1], "--version")) { |
|||
printf("%s\n", version()); |
|||
exit(0); |
|||
} |
|||
|
|||
subdaemon_debug(argc, argv); |
|||
|
|||
/* We handle write returning errors! */ |
|||
signal(SIGCHLD, SIG_IGN); |
|||
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY |
|||
| SECP256K1_CONTEXT_SIGN); |
|||
status_setup(STATUS_FD); |
|||
peer->peer_out = tal_arr(peer, const u8 *, 0); |
|||
init_peer_crypto_state(peer, &peer->pcs); |
|||
peer->funding_locked[LOCAL] = peer->funding_locked[REMOTE] = false; |
|||
|
|||
msg = wire_sync_read(peer, REQ_FD); |
|||
if (!msg) |
|||
status_failed(WIRE_CHANNEL_BAD_COMMAND, "%s", strerror(errno)); |
|||
|
|||
if (!fromwire_channel_init(msg, NULL, |
|||
&funding_txid, &funding_txout, |
|||
&peer->conf[LOCAL], &peer->conf[REMOTE], |
|||
&peer->their_commit_sig, |
|||
&peer->pcs.cs, |
|||
&funding_pubkey[REMOTE], |
|||
&points[REMOTE].revocation, |
|||
&points[REMOTE].payment, |
|||
&points[REMOTE].delayed_payment, |
|||
&peer->next_per_commit[REMOTE], |
|||
&am_funder, |
|||
&feerate, &funding_satoshi, &push_msat, |
|||
&seed)) |
|||
status_failed(WIRE_CHANNEL_BAD_COMMAND, "%s", |
|||
tal_hex(msg, msg)); |
|||
tal_free(msg); |
|||
|
|||
/* We derive everything from the one secret seed. */ |
|||
derive_basepoints(&seed, &funding_pubkey[LOCAL], &points[LOCAL], |
|||
&peer->our_secrets, &peer->shaseed, |
|||
&peer->next_per_commit[LOCAL], 1); |
|||
|
|||
peer->channel = new_channel(peer, &funding_txid, funding_txout, |
|||
funding_satoshi, push_msat, feerate, |
|||
&peer->conf[LOCAL], &peer->conf[REMOTE], |
|||
&points[LOCAL], &points[REMOTE], |
|||
am_funder ? LOCAL : REMOTE); |
|||
|
|||
/* OK, now we can process peer messages. */ |
|||
io_set_finish(io_new_conn(peer, PEER_FD, setup_peer_conn, peer), |
|||
peer_conn_broken, peer); |
|||
io_new_conn(peer, REQ_FD, setup_req_in, peer); |
|||
|
|||
/* We don't expect to exit here. */ |
|||
io_loop(NULL, NULL); |
|||
tal_free(peer); |
|||
return 0; |
|||
} |
|||
#endif /* TESTING */ |
Can't render this file because it has a wrong number of fields in line 5.
|
Can't render this file because it has a wrong number of fields in line 2.
|
Loading…
Reference in new issue