Skip to content

Commit 87a0ef5

Browse files
authored
Merge branch 'master' into shutdown-fixes
2 parents 3288334 + b7856e3 commit 87a0ef5

15 files changed

+332
-76
lines changed

bitcoin/tx.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,24 @@ static void push_witnesses(const struct bitcoin_tx *tx,
8080

8181
static void push_tx(const struct bitcoin_tx *tx,
8282
void (*push)(const void *, size_t, void *), void *pushp,
83-
bool extended)
83+
bool bip144)
8484
{
8585
varint_t i;
8686
u8 flag = 0;
8787

8888
push_le32(tx->version, push, pushp);
8989

90-
if (extended) {
91-
u8 marker;
90+
if (bip144 && uses_witness(tx))
91+
flag |= SEGREGATED_WITNESS_FLAG;
92+
93+
/* BIP 141: The flag MUST be a 1-byte non-zero value. */
94+
/* ie. if no flags set, we fallback to pre-BIP144-style */
95+
if (flag) {
96+
u8 marker = 0;
9297
/* BIP 144 */
9398
/* marker char Must be zero */
9499
/* flag char Must be nonzero */
95-
marker = 0;
96100
push(&marker, 1, pushp);
97-
/* BIP 141: The flag MUST be a 1-byte non-zero
98-
* value. Currently, 0x01 MUST be used.
99-
*
100-
* BUT: Current segwit4 branch breaks fundrawtransaction;
101-
* it sees 0 inputs and thinks it's extended format.
102-
* Make it really an extended format, but without
103-
* witness. */
104-
if (uses_witness(tx))
105-
flag = SEGREGATED_WITNESS_FLAG;
106101
push(&flag, 1, pushp);
107102
}
108103

@@ -256,7 +251,7 @@ static void push_linearize(const void *data, size_t len, void *pptr_)
256251
u8 *linearize_tx(const tal_t *ctx, const struct bitcoin_tx *tx)
257252
{
258253
u8 *arr = tal_arr(ctx, u8, 0);
259-
push_tx(tx, push_linearize, &arr, uses_witness(tx));
254+
push_tx(tx, push_linearize, &arr, true);
260255
return arr;
261256
}
262257

devtools/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
bolt11-cli
2+
decodemsg

devtools/Makefile

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
1-
DEVTOOLS_CLI_SRC := devtools/bolt11-cli.c
2-
DEVTOOLS_CLI_OBJS := $(DEVTOOLS_CLI_SRC:.c=.o)
1+
DEVTOOLS_SRC := devtools/gen_print_wire.c devtools/print_wire.c
2+
DEVTOOLS_OBJS := $(DEVTOOLS_SRC:.c=.o)
3+
DEVTOOLS_TOOL_SRC := devtools/bolt11-cli.c devtools/decodemsg.c
4+
DEVTOOLS_TOOL_OBJS := $(DEVTOOLS_TOOL_SRC:.c=.o)
35

4-
DEVTOOLS_CLI_COMMON_OBJS := \
6+
DEVTOOLS_COMMON_OBJS := \
57
common/bech32.o \
68
common/bolt11.o \
79
common/hash_u5.o \
810
common/type_to_string.o \
911
common/utils.o \
1012
common/version.o
1113

12-
devtools-all: devtools/bolt11-cli
14+
devtools-all: devtools/bolt11-cli devtools/decodemsg
1315

14-
devtools/bolt11-cli: $(DEVTOOLS_CLI_OBJS) $(DEVTOOLS_CLI_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) $(BITCOIN_OBJS) wire/fromwire.o wire/towire.o
16+
devtools/gen_print_wire.h: $(WIRE_GEN) wire/gen_peer_wire_csv
17+
$(WIRE_GEN) --bolt --printwire --header $@ wire_type < wire/gen_peer_wire_csv > $@
1518

16-
$(DEVTOOLS_CLI_OBJS): wire/wire.h
19+
devtools/gen_print_wire.c: $(WIRE_GEN) wire/gen_peer_wire_csv
20+
$(WIRE_GEN) --bolt --printwire ${@:.c=.h} wire_type < wire/gen_peer_wire_csv > $@
21+
22+
devtools/bolt11-cli: $(DEVTOOLS_OBJS) $(DEVTOOLS_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) $(BITCOIN_OBJS) wire/fromwire.o wire/towire.o devtools/bolt11-cli.o
23+
24+
devtools/decodemsg: $(DEVTOOLS_OBJS) $(DEVTOOLS_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) $(BITCOIN_OBJS) wire/fromwire.o wire/towire.o devtools/decodemsg.o
25+
26+
$(DEVTOOLS_OBJS) $(DEVTOOLS_TOOL_OBJS): wire/wire.h devtools/gen_print_wire.h
27+
devtools/gen_print_wire.o: devtools/gen_print_wire.h wire/gen_peer_wire.h
1728

1829
# Make sure these depend on everything.
19-
ALL_PROGRAMS += devtools/bolt11-cli
20-
ALL_OBJS += $(DEVTOOLS_CLI_OBJS)
30+
ALL_PROGRAMS += devtools/bolt11-cli devtools/decodemsg
31+
ALL_OBJS += $(DEVTOOLS_OBJS) $(DEVTOOLS_TOOL_OBJS)
2132

22-
check-source: $(DEVTOOLS_CLI_SRC:%=check-src-include-order/%)
33+
check-source: $(DEVTOOLS_SRC:%=check-src-include-order/%) $(DEVTOOLS_TOOLS_SRC:%=check-src-include-order/%)
2334

2435
clean: devtools-clean
2536

devtools/decodemsg.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <common/utils.h>
2+
#include <devtools/gen_print_wire.h>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
u8 *m = tal_hexdata(NULL, argv[1], strlen(argv[1]));
7+
print_message(m);
8+
return 0;
9+
}

devtools/print_wire.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <common/type_to_string.h>
2+
#include <devtools/print_wire.h>
3+
#include <inttypes.h>
4+
#include <stdio.h>
5+
6+
void printwire_u8(const u8 *v)
7+
{
8+
printf("%u", *v);
9+
}
10+
11+
void printwire_u16(const u16 *v)
12+
{
13+
printf("%u", *v);
14+
}
15+
16+
void printwire_u32(const u32 *v)
17+
{
18+
printf("%u", *v);
19+
}
20+
21+
void printwire_u64(const u64 *v)
22+
{
23+
printf("%"PRIu64, *v);
24+
}
25+
26+
void printwire_u8_array(const u8 **cursor, size_t *plen, size_t len)
27+
{
28+
printf("[");
29+
while (len) {
30+
u8 v = fromwire_u8(cursor, plen);
31+
if (!*cursor)
32+
return;
33+
if (isprint(v))
34+
printf("%c", v);
35+
else
36+
printf("\\x%02x", v);
37+
len--;
38+
}
39+
printf("]\n");
40+
}
41+
42+
#define PRINTWIRE_TYPE_TO_STRING(T, N) \
43+
void printwire_##N(const T *v) \
44+
{ \
45+
const char *s = type_to_string(NULL, T, v); \
46+
printf("%s\n", s); \
47+
tal_free(s); \
48+
}
49+
50+
#define PRINTWIRE_STRUCT_TYPE_TO_STRING(T) \
51+
PRINTWIRE_TYPE_TO_STRING(struct T, T)
52+
53+
PRINTWIRE_STRUCT_TYPE_TO_STRING(bitcoin_blkid);
54+
PRINTWIRE_STRUCT_TYPE_TO_STRING(bitcoin_txid);
55+
PRINTWIRE_STRUCT_TYPE_TO_STRING(channel_id);
56+
PRINTWIRE_STRUCT_TYPE_TO_STRING(preimage);
57+
PRINTWIRE_STRUCT_TYPE_TO_STRING(pubkey);
58+
PRINTWIRE_STRUCT_TYPE_TO_STRING(sha256);
59+
PRINTWIRE_STRUCT_TYPE_TO_STRING(short_channel_id);
60+
PRINTWIRE_TYPE_TO_STRING(secp256k1_ecdsa_signature, secp256k1_ecdsa_signature);

devtools/print_wire.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef LIGHTNING_DEVTOOLS_PRINT_WIRE_H
2+
#define LIGHTNING_DEVTOOLS_PRINT_WIRE_H
3+
#include <bitcoin/preimage.h>
4+
#include <bitcoin/tx.h>
5+
#include <wire/gen_peer_wire.h>
6+
7+
void printwire_u8(const u8 *v);
8+
void printwire_u16(const u16 *v);
9+
void printwire_u32(const u32 *v);
10+
void printwire_u64(const u64 *v);
11+
void printwire_u8_array(const u8 **cursor, size_t *plen, size_t len);
12+
13+
void printwire_bitcoin_blkid(const struct bitcoin_blkid *bitcoin_blkid);
14+
void printwire_bitcoin_txid(const struct bitcoin_txid *bitcoin_txid);
15+
void printwire_channel_id(const struct channel_id *channel_id);
16+
void printwire_preimage(const struct preimage *preimage);
17+
void printwire_pubkey(const struct pubkey *pubkey);
18+
void printwire_secp256k1_ecdsa_signature(const secp256k1_ecdsa_signature *);
19+
void printwire_sha256(const struct sha256 *sha256);
20+
void printwire_short_channel_id(const struct short_channel_id *short_channel_id);
21+
22+
#endif /* LIGHTNING_DEVTOOLS_PRINT_WIRE_H */

lightningd/chaintopology.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ void json_dev_broadcast(struct command *cmd,
563563
}
564564

565565
if (!json_tok_bool(buffer, enabletok, &enable)) {
566-
command_fail(cmd, "enable must be true or false");
566+
command_fail(cmd, "Enable must be true or false");
567567
return;
568568
}
569569

@@ -625,7 +625,7 @@ static void json_dev_setfees(struct command *cmd,
625625
continue;
626626
if (!json_tok_number(buffer, ratetok[i],
627627
&topo->override_fee_rate[i])) {
628-
command_fail(cmd, "invalid feerate %.*s",
628+
command_fail(cmd, "Invalid feerate %.*s",
629629
(int)(ratetok[i]->end - ratetok[i]->start),
630630
buffer + ratetok[i]->start);
631631
return;

lightningd/invoice.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static void tell_waiter(struct command *cmd, const struct invoice *paid)
6666
}
6767
static void tell_waiter_deleted(struct command *cmd)
6868
{
69-
command_fail(cmd, "invoice deleted during wait");
69+
command_fail(cmd, "Invoice deleted during wait");
7070
}
7171
static void wait_on_invoice(const struct invoice *invoice, void *cmd)
7272
{
@@ -140,12 +140,12 @@ static void json_invoice(struct command *cmd,
140140
return;
141141
}
142142
if (strlen(label_val) > INVOICE_MAX_LABEL_LEN) {
143-
command_fail(cmd, "label '%s' over %u bytes", label_val,
143+
command_fail(cmd, "Label '%s' over %u bytes", label_val,
144144
INVOICE_MAX_LABEL_LEN);
145145
return;
146146
}
147147
if (exp && !json_tok_u64(buffer, exp, &expiry)) {
148-
command_fail(cmd, "expiry '%.*s' invalid seconds",
148+
command_fail(cmd, "Expiry '%.*s' invalid seconds",
149149
exp->end - exp->start,
150150
buffer + exp->start);
151151
return;

lightningd/jsonrpc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ static void parse_request(struct json_connection *jcon, const jsmntok_t tok[])
617617
if (cmd->deprecated && !deprecated_apis) {
618618
command_fail_detailed(jcon->current,
619619
JSONRPC2_METHOD_NOT_FOUND, NULL,
620-
"command '%.*s' is deprecated",
620+
"Command '%.*s' is deprecated",
621621
(int)(method->end - method->start),
622622
jcon->buffer + method->start);
623623
return;

lightningd/log.c

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct log_book {
3535
void (*print)(const char *prefix,
3636
enum log_level level,
3737
bool continued,
38+
const struct timeabs *time,
3839
const char *str, void *arg);
3940
void *print_arg;
4041
enum log_level print_level;
@@ -48,17 +49,32 @@ struct log {
4849
const char *prefix;
4950
};
5051

51-
static void log_default_print(const char *prefix,
52-
enum log_level level,
53-
bool continued,
54-
const char *str, void *arg)
52+
static void log_to_file(const char *prefix,
53+
enum log_level level,
54+
bool continued,
55+
const struct timeabs *time,
56+
const char *str,
57+
FILE *logf)
5558
{
59+
char iso8601_msec_fmt[sizeof("YYYY-mm-ddTHH:MM:SS.%03dZ")];
60+
strftime(iso8601_msec_fmt, sizeof(iso8601_msec_fmt), "%FT%T.%%03dZ", gmtime(&time->ts.tv_sec));
61+
char iso8601_s[sizeof("YYYY-mm-ddTHH:MM:SS.nnnZ")];
62+
snprintf(iso8601_s, sizeof(iso8601_s), iso8601_msec_fmt, (int) time->ts.tv_nsec / 1000000);
5663
if (!continued) {
57-
printf("%s %s\n", prefix, str);
64+
fprintf(logf, "%s %s %s\n", iso8601_s, prefix, str);
5865
} else {
59-
printf("%s \t%s\n", prefix, str);
66+
fprintf(logf, "%s %s \t%s\n", iso8601_s, prefix, str);
6067
}
61-
fflush(stdout);
68+
fflush(logf);
69+
}
70+
71+
static void log_to_stdout(const char *prefix,
72+
enum log_level level,
73+
bool continued,
74+
const struct timeabs *time,
75+
const char *str, void *arg)
76+
{
77+
log_to_file(prefix, level, continued, time, str, stdout);
6278
}
6379

6480
static size_t log_bufsize(const struct log_entry *e)
@@ -106,7 +122,7 @@ struct log_book *new_log_book(const tal_t *ctx,
106122
assert(max_mem > sizeof(struct log) * 2);
107123
lr->mem_used = 0;
108124
lr->max_mem = max_mem;
109-
lr->print = log_default_print;
125+
lr->print = log_to_stdout;
110126
lr->print_level = printlevel;
111127
lr->init_time = time_now();
112128
list_head_init(&lr->log);
@@ -155,6 +171,7 @@ void set_log_outfn_(struct log_book *lr,
155171
void (*print)(const char *prefix,
156172
enum log_level level,
157173
bool continued,
174+
const struct timeabs *time,
158175
const char *str, void *arg),
159176
void *arg)
160177
{
@@ -220,7 +237,7 @@ void logv(struct log *log, enum log_level level, const char *fmt, va_list ap)
220237
l->log = tal_vfmt(l, fmt, ap);
221238

222239
if (level >= log->lr->print_level)
223-
log->lr->print(log->prefix, level, false, l->log,
240+
log->lr->print(log->prefix, level, false, &l->time, l->log,
224241
log->lr->print_arg);
225242

226243
add_entry(log, l);
@@ -240,7 +257,7 @@ void log_io(struct log *log, bool in, const void *data, size_t len)
240257
char *hex = tal_arr(l, char, strlen(dir) + hex_str_size(len));
241258
strcpy(hex, dir);
242259
hex_encode(data, len, hex + strlen(dir), hex_str_size(len));
243-
log->lr->print(log->prefix, LOG_IO, false, l->log,
260+
log->lr->print(log->prefix, LOG_IO, false, &l->time, l->log,
244261
log->lr->print_arg);
245262
tal_free(hex);
246263
}
@@ -262,7 +279,7 @@ void logv_add(struct log *log, const char *fmt, va_list ap)
262279
add_entry(log, l);
263280

264281
if (l->level >= log->lr->print_level)
265-
log->lr->print(log->prefix, l->level, true, l->log + oldlen,
282+
log->lr->print(log->prefix, l->level, true, &l->time, l->log + oldlen,
266283
log->lr->print_arg);
267284
}
268285

@@ -400,20 +417,6 @@ static void show_log_prefix(char buf[OPT_SHOW_LEN], const struct log *log)
400417
strncpy(buf, log->prefix, OPT_SHOW_LEN);
401418
}
402419

403-
static void log_to_file(const char *prefix,
404-
enum log_level level,
405-
bool continued,
406-
const char *str,
407-
FILE *logf)
408-
{
409-
if (!continued) {
410-
fprintf(logf, "%s %s\n", prefix, str);
411-
} else {
412-
fprintf(logf, "%s \t%s\n", prefix, str);
413-
}
414-
fflush(logf);
415-
}
416-
417420
char *arg_log_to_file(const char *arg, struct lightningd *ld)
418421
{
419422
FILE *logf;
@@ -464,7 +467,7 @@ static void log_crash(int sig)
464467
crashlog);
465468
}
466469

467-
if (crashlog->lr->print == log_default_print) {
470+
if (crashlog->lr->print == log_to_stdout) {
468471
int fd;
469472

470473
/* We expect to be in config dir. */

lightningd/log.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define LIGHTNING_LIGHTNINGD_LOG_H
33
#include "config.h"
44
#include <ccan/tal/tal.h>
5+
#include <ccan/time/time.h>
56
#include <ccan/typesafe_cb/typesafe_cb.h>
67
#include <common/type_to_string.h>
78
#include <stdarg.h>
@@ -54,12 +55,14 @@ const char *log_prefix(const struct log *log);
5455
const char *, \
5556
enum log_level, \
5657
bool, \
58+
const struct timeabs *, \
5759
const char *), (arg))
5860

5961
void set_log_outfn_(struct log_book *lr,
6062
void (*print)(const char *prefix,
6163
enum log_level level,
6264
bool continued,
65+
const struct timeabs *time,
6366
const char *str, void *arg),
6467
void *arg);
6568

0 commit comments

Comments
 (0)