Skip to content

Commit 5cede6d

Browse files
committed
global: use clock_time in place of time_now().
Except for tracing, that sticks with time_now(). Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 4a9f6eb commit 5cede6d

51 files changed

Lines changed: 142 additions & 82 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ check-tmpctx:
600600
@if git grep -n 'tal_free[(]tmpctx)' | grep -Ev '^ccan/|/test/|^common/setup.c:|^common/utils.c:'; then echo "Don't free tmpctx!">&2; exit 1; fi
601601

602602
check-discouraged-functions:
603-
@if git grep -nE "[^a-z_/](fgets|fputs|gets|scanf|sprintf|randombytes_buf)\(" -- "*.c" "*.h" ":(exclude)ccan/" ":(exclude)contrib/" | grep -Fv '/* discouraged:'; then exit 1; fi
603+
@if git grep -nE "[^a-z_/](fgets|fputs|gets|scanf|sprintf|randombytes_buf|time_now)\(" -- "*.c" "*.h" ":(exclude)ccan/" ":(exclude)contrib/" | grep -Fv '/* discouraged:'; then exit 1; fi
604604

605605
check-bad-sprintf:
606606
@if git grep -n "%[*]\.s"; then exit 1; fi

common/coin_mvt.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <ccan/bitops/bitops.h>
44
#include <ccan/ccan/cast/cast.h>
55
#include <ccan/tal/str/str.h>
6-
#include <ccan/time/time.h>
6+
#include <common/clock_time.h>
77
#include <common/coin_mvt.h>
88
#include <common/node_id.h>
99
#include <common/utils.h>
@@ -155,6 +155,12 @@ static enum mvt_tag mvt_tag_in_db(enum mvt_tag mvt_tag)
155155
abort();
156156
}
157157

158+
/* This puts the coin movements in order */
159+
u64 coinmvt_current_time(void)
160+
{
161+
return clock_time_progresses().ts.tv_sec;
162+
}
163+
158164
const char *mvt_tag_str(enum mvt_tag tag)
159165
{
160166
assert((unsigned)tag < NUM_MVT_TAGS);
@@ -338,7 +344,7 @@ static struct chain_coin_mvt *new_chain_coin_mvt_sat(const tal_t *ctx,
338344
assert(ok);
339345

340346
return new_chain_coin_mvt(ctx, channel, account_name,
341-
time_now().ts.tv_sec, tx_txid,
347+
coinmvt_current_time(), tx_txid,
342348
outpoint, payment_hash,
343349
blockheight, tags, direction, amt_msat,
344350
/* All amounts that are sat are
@@ -393,7 +399,7 @@ struct chain_coin_mvt *new_coin_channel_close(const tal_t *ctx,
393399
tags = mk_mvt_tags(MVT_CHANNEL_CLOSE);
394400

395401
mvt = new_chain_coin_mvt(ctx, channel, alt_account,
396-
time_now().ts.tv_sec, txid,
402+
coinmvt_current_time(), txid,
397403
out, NULL, blockheight,
398404
tags,
399405
COIN_DEBIT, amount,
@@ -421,7 +427,7 @@ struct chain_coin_mvt *new_coin_channel_open_proposed(const tal_t *ctx,
421427
if (is_leased)
422428
mvt_tag_set(&tags, MVT_LEASED);
423429

424-
mvt = new_chain_coin_mvt(ctx, channel, NULL, time_now().ts.tv_sec,
430+
mvt = new_chain_coin_mvt(ctx, channel, NULL, coinmvt_current_time(),
425431
NULL, out, NULL, 0,
426432
tags,
427433
COIN_CREDIT, amount, output_val, 0);
@@ -474,7 +480,7 @@ struct chain_coin_mvt *new_coin_channel_open(const tal_t *ctx,
474480
bool is_leased)
475481
{
476482
return new_coin_channel_open_general(ctx, channel, NULL,
477-
time_now().ts.tv_sec,
483+
coinmvt_current_time(),
478484
out, peer_id, blockheight,
479485
amount, output_val, is_opener, is_leased);
480486
}
@@ -516,7 +522,7 @@ struct chain_coin_mvt *new_coin_external_spend(const tal_t *ctx,
516522
struct mvt_tags tags)
517523
{
518524
return new_chain_coin_mvt(ctx, NULL, ACCOUNT_NAME_EXTERNAL,
519-
time_now().ts.tv_sec, txid,
525+
coinmvt_current_time(), txid,
520526
outpoint, NULL, blockheight,
521527
tags,
522528
COIN_CREDIT, AMOUNT_MSAT(0), amount, 0);
@@ -584,7 +590,7 @@ struct channel_coin_mvt *new_coin_channel_push(const tal_t *ctx,
584590
struct mvt_tags tags)
585591
{
586592
return new_coin_channel_push_general(ctx, channel, NULL,
587-
time_now().ts.tv_sec,
593+
coinmvt_current_time(),
588594
direction, amount, tags);
589595
}
590596

@@ -731,6 +737,10 @@ void fromwire_chain_coin_mvt(const u8 **cursor, size_t *max, struct chain_coin_m
731737
} else
732738
mvt->peer_id = NULL;
733739
mvt->timestamp = fromwire_u64(cursor, max);
740+
741+
/* Align onchaind's timestamps with ours if we're deterministic */
742+
if (clock_time_overridden())
743+
mvt->timestamp = coinmvt_current_time();
734744
}
735745

736746
struct mvt_tags mk_mvt_tags_(enum mvt_tag tag, ...)

common/coin_mvt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,7 @@ bool mvt_tag_parse(const char *buf, size_t len, enum mvt_tag *tag);
346346
void towire_chain_coin_mvt(u8 **pptr, const struct chain_coin_mvt *mvt);
347347
void fromwire_chain_coin_mvt(const u8 **cursor, size_t *max, struct chain_coin_mvt *mvt);
348348

349+
/* Time helper for deterministic timestamps: always moves forwards */
350+
u64 coinmvt_current_time(void);
351+
349352
#endif /* LIGHTNING_COMMON_COIN_MVT_H */

common/test/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ COMMON_TEST_PROGRAMS := $(COMMON_TEST_OBJS:.o=)
66
COMMON_TEST_COMMON_OBJS := \
77
common/autodata.o \
88
common/randbytes.o \
9+
common/clock_time.o \
910
common/setup.o \
1011
common/utils.o
1112

common/test/run-json.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <assert.h>
55
#include <ccan/tal/str/str.h>
66
#include <common/channel_type.h>
7+
#include <common/clock_time.h>
78
#include <common/json_filter.h>
89
#include <common/randbytes.h>
910
#include <common/sciddir_or_pubkey.h>

common/test/run-param.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <assert.h>
88
#include <ccan/array_size/array_size.h>
99
#include <common/channel_type.h>
10+
#include <common/clock_time.h>
1011
#include <common/memleak.h>
1112
#include <common/randbytes.h>
1213
#include <common/setup.h>

common/test/run-route_blinding_test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <ccan/tal/grab_file/grab_file.h>
1111
#include <ccan/tal/path/path.h>
1212
#include <common/channel_id.h>
13+
#include <common/clock_time.h>
1314
#include <common/json_parse.h>
1415
#include <common/json_stream.h>
1516
#include <common/randbytes.h>

common/trace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static void init_span(struct span *s,
7575
const char *name,
7676
struct span *parent)
7777
{
78-
struct timeabs now = time_now();
78+
struct timeabs now = time_now(); /* discouraged: but tracing wants non-dev time */
7979

8080
s->key = key;
8181
s->id = pseudorand_u64();
@@ -366,7 +366,7 @@ void trace_span_end(const void *key)
366366

367367
trace_check_tree();
368368

369-
struct timeabs now = time_now();
369+
struct timeabs now = time_now(); /* discouraged: but tracing wants non-dev time */
370370
s->end_time = (now.ts.tv_sec * 1000000) + now.ts.tv_nsec / 1000;
371371
DTRACE_PROBE1(lightningd, span_end, s->id);
372372
if (trace_to_file) {

connectd/connectd.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <ccan/tal/str/str.h>
1919
#include <common/bech32.h>
2020
#include <common/bech32_util.h>
21+
#include <common/clock_time.h>
2122
#include <common/cryptomsg.h>
2223
#include <common/daemon_conn.h>
2324
#include <common/dev_disconnect.h>
@@ -2260,7 +2261,7 @@ static void dev_report_fds(struct daemon *daemon, const u8 *msg)
22602261
void update_recent_timestamp(struct daemon *daemon, struct gossmap *gossmap)
22612262
{
22622263
/* 2 hours allows for some clock drift, not too much gossip */
2263-
u32 recent = time_now().ts.tv_sec - 7200;
2264+
u32 recent = clock_time().ts.tv_sec - 7200;
22642265

22652266
/* Only update every minute */
22662267
if (daemon->gossip_recent_time + 60 > recent)

devtools/bolt11-cli.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <ccan/time/time.h>
1111
#include <common/bech32.h>
1212
#include <common/bolt11.h>
13+
#include <common/clock_time.h>
1314
#include <common/features.h>
1415
#include <common/setup.h>
1516
#include <common/utils.h>
@@ -109,7 +110,7 @@ static void encode(const tal_t *ctx,
109110
struct pubkey me;
110111
bool explicit_n = false;
111112

112-
b11->timestamp = time_now().ts.tv_sec;
113+
b11->timestamp = clock_time().ts.tv_sec;
113114
b11->chain = chainparams_for_network("regtest");
114115
b11->expiry = 3600;
115116
b11->min_final_cltv_expiry = DEFAULT_FINAL_CLTV_DELTA;

0 commit comments

Comments
 (0)