Skip to content

Commit 61e576e

Browse files
committed
daemon/chaintopology: use struct topology for more functions, not lightningd_state
Signed-off-by: Rusty Russell <[email protected]>
1 parent 923526b commit 61e576e

File tree

11 files changed

+166
-138
lines changed

11 files changed

+166
-138
lines changed

daemon/chaintopology.c

Lines changed: 80 additions & 97 deletions
Large diffs are not rendered by default.

daemon/chaintopology.h

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <ccan/list/list.h>
77
#include <ccan/short_types/short_types.h>
88
#include <ccan/structeq/structeq.h>
9+
#include <ccan/time/time.h>
910
#include <daemon/watch.h>
1011
#include <stddef.h>
1112

@@ -80,6 +81,18 @@ struct topology {
8081
u64 feerate;
8182
bool startup;
8283

84+
/* How far back (in blocks) to go. */
85+
unsigned int first_blocknum;
86+
87+
/* How often to poll. */
88+
struct timerel poll_time;
89+
90+
/* The bitcoind. */
91+
struct bitcoind *bitcoind;
92+
93+
/* Our timer list. */
94+
struct timers *timers;
95+
8396
/* Bitcoin transctions we're broadcasting */
8497
struct list_head outgoing_txs;
8598

@@ -100,32 +113,37 @@ struct txlocator {
100113

101114
/* This is the number of blocks which would have to be mined to invalidate
102115
* the tx. */
103-
size_t get_tx_depth(struct lightningd_state *dstate,
116+
size_t get_tx_depth(const struct topology *topo,
104117
const struct sha256_double *txid);
105118

106119
/* Get the mediantime of the block including this tx (must be one!) */
107-
u32 get_tx_mediantime(struct lightningd_state *dstate,
120+
u32 get_tx_mediantime(const struct topology *topo,
108121
const struct sha256_double *txid);
109122

110123
/* Get mediantime of the tip; if more than one, pick greatest time. */
111-
u32 get_tip_mediantime(struct lightningd_state *dstate);
124+
u32 get_tip_mediantime(const struct topology *topo);
112125

113126
/* Get highest block number. */
114-
u32 get_block_height(struct lightningd_state *dstate);
127+
u32 get_block_height(const struct topology *topo);
115128

116129
/* Get fee rate. */
117130
u64 get_feerate(struct lightningd_state *dstate);
118131

119132
/* Broadcast a single tx, and rebroadcast as reqd (copies tx).
120133
* If failed is non-NULL, call that and don't rebroadcast. */
121-
void broadcast_tx(struct peer *peer, const struct bitcoin_tx *tx,
134+
void broadcast_tx(struct topology *topo,
135+
struct peer *peer, const struct bitcoin_tx *tx,
122136
void (*failed)(struct peer *peer,
123137
int exitstatus,
124138
const char *err));
125139

126140
struct topology *new_topology(const tal_t *ctx);
127-
void setup_topology(struct topology *topology, struct bitcoind *bitcoind);
141+
void setup_topology(struct topology *topology, struct bitcoind *bitcoind,
142+
struct timers *timers,
143+
struct timerel poll_time, u32 first_peer_block);
144+
145+
struct txlocator *locate_tx(const void *ctx, const struct topology *topo, const struct sha256_double *txid);
128146

129-
struct txlocator *locate_tx(const void *ctx, struct lightningd_state *dstate, const struct sha256_double *txid);
147+
void notify_new_block(struct topology *topo, unsigned int height);
130148

131149
#endif /* LIGHTNING_DAEMON_CRYPTOPKT_H */

daemon/irc_announce.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static bool announce_channel(const tal_t *ctx, struct ircstate *state, struct pe
2828
{
2929
char txid[65];
3030
struct privmsg *msg = talz(ctx, struct privmsg);
31-
struct txlocator *loc = locate_tx(ctx, state->dstate, &p->anchor.txid);
31+
struct txlocator *loc = locate_tx(ctx, state->dstate->topology, &p->anchor.txid);
3232

3333
if (loc == NULL)
3434
return false;

daemon/jsonrpc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,8 @@ static void json_getinfo(struct command *cmd,
275275
json_add_num(response, "port", cmd->dstate->portnum);
276276
json_add_bool(response, "testnet", cmd->dstate->testnet);
277277
json_add_string(response, "version", version());
278-
json_add_num(response, "blockheight", get_block_height(cmd->dstate));
278+
json_add_num(response, "blockheight",
279+
get_block_height(cmd->dstate->topology));
279280
json_object_end(response);
280281
command_success(cmd, response);
281282
}

daemon/lightningd.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ int main(int argc, char *argv[])
6666
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
6767
| SECP256K1_CONTEXT_SIGN);
6868

69-
dstate->bitcoind = new_bitcoind(dstate, dstate->base_log);
7069
dstate->topology = new_topology(dstate);
70+
dstate->bitcoind = new_bitcoind(dstate, dstate->base_log);
7171

7272
/* Handle options and config; move to .lightningd */
7373
register_opts(dstate);
@@ -87,7 +87,9 @@ int main(int argc, char *argv[])
8787
db_init(dstate);
8888

8989
/* Initialize block topology. */
90-
setup_topology(dstate->topology, dstate->bitcoind);
90+
setup_topology(dstate->topology, dstate->bitcoind, &dstate->timers,
91+
dstate->config.poll_time,
92+
get_peer_min_block(dstate));
9193

9294
/* Create RPC socket (if any) */
9395
setup_jsonrpc(dstate, dstate->rpc_filename);

daemon/p2p_announce.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static void broadcast_channel_update(struct lightningd_state *dstate, struct pee
2222
u32 timestamp = time_now().ts.tv_sec;
2323
const tal_t *tmpctx = tal_tmpctx(dstate);
2424

25-
loc = locate_tx(tmpctx, dstate, &peer->anchor.txid);
25+
loc = locate_tx(tmpctx, dstate->topology, &peer->anchor.txid);
2626
channel_id.blocknum = loc->blkheight;
2727
channel_id.txnum = loc->index;
2828
channel_id.outnum = peer->anchor.index;
@@ -105,7 +105,7 @@ static void broadcast_channel_announcement(struct lightningd_state *dstate, stru
105105
u8 *serialized;
106106
const tal_t *tmpctx = tal_tmpctx(dstate);
107107

108-
loc = locate_tx(tmpctx, dstate, &peer->anchor.txid);
108+
loc = locate_tx(tmpctx, dstate->topology, &peer->anchor.txid);
109109

110110
channel_id.blocknum = loc->blkheight;
111111
channel_id.txnum = loc->index;

daemon/pay.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,8 @@ static void json_sendpay(struct command *cmd,
462462

463463
/* Expiry for HTLCs is absolute. And add one to give some margin. */
464464
err = command_htlc_add(peer, amount,
465-
delay + get_block_height(cmd->dstate) + 1,
465+
delay + get_block_height(cmd->dstate->topology)
466+
+ 1,
466467
&rhash, NULL,
467468
onion, &error_code, &pc->htlc);
468469
if (err) {

daemon/peer.c

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,15 @@ static void peer_breakdown(struct peer *peer)
347347
if (peer->closing.their_sig) {
348348
const struct bitcoin_tx *close = mk_bitcoin_close(peer, peer);
349349
log_unusual(peer->log, "Peer breakdown: sending close tx");
350-
broadcast_tx(peer, close, NULL);
350+
broadcast_tx(peer->dstate->topology, peer, close, NULL);
351351
tal_free(close);
352352
/* If we have a signed commit tx (maybe not if we just offered
353353
* anchor, or they supplied anchor, or no outputs to us). */
354354
} else if (peer->local.commit && peer->local.commit->sig) {
355355
log_unusual(peer->log, "Peer breakdown: sending commit tx");
356356
sign_commit_tx(peer);
357-
broadcast_tx(peer, peer->local.commit->tx, NULL);
357+
broadcast_tx(peer->dstate->topology, peer,
358+
peer->local.commit->tx, NULL);
358359
} else {
359360
log_info(peer->log, "Peer breakdown: nothing to do");
360361
/* We close immediately. */
@@ -644,7 +645,8 @@ static bool open_ouranchor_pkt_in(struct peer *peer, const Pkt *pkt)
644645
if (db_commit_transaction(peer) != NULL)
645646
return peer_database_err(peer);
646647

647-
broadcast_tx(peer, peer->anchor.tx, funding_tx_failed);
648+
broadcast_tx(peer->dstate->topology,
649+
peer, peer->anchor.tx, funding_tx_failed);
648650
peer_watch_anchor(peer, peer->local.mindepth);
649651
return true;
650652
}
@@ -878,7 +880,7 @@ static void their_htlc_added(struct peer *peer, struct htlc *htlc,
878880
}
879881

880882
if (abs_locktime_to_blocks(&htlc->expiry) <=
881-
get_block_height(peer->dstate) + peer->dstate->config.min_htlc_expiry) {
883+
get_block_height(peer->dstate->topology) + peer->dstate->config.min_htlc_expiry) {
882884
log_unusual(peer->log, "HTLC %"PRIu64" expires too soon:"
883885
" block %u",
884886
htlc->id, abs_locktime_to_blocks(&htlc->expiry));
@@ -888,7 +890,7 @@ static void their_htlc_added(struct peer *peer, struct htlc *htlc,
888890
}
889891

890892
if (abs_locktime_to_blocks(&htlc->expiry) >
891-
get_block_height(peer->dstate) + peer->dstate->config.max_htlc_expiry) {
893+
get_block_height(peer->dstate->topology) + peer->dstate->config.max_htlc_expiry) {
892894
log_unusual(peer->log, "HTLC %"PRIu64" expires too far:"
893895
" block %u",
894896
htlc->id, abs_locktime_to_blocks(&htlc->expiry));
@@ -1313,7 +1315,7 @@ static bool closing_pkt_in(struct peer *peer, const Pkt *pkt)
13131315
* SHOULD sign and broadcast the final closing transaction.
13141316
*/
13151317
close = mk_bitcoin_close(peer, peer);
1316-
broadcast_tx(peer, close, NULL);
1318+
broadcast_tx(peer->dstate->topology, peer, close, NULL);
13171319
tal_free(close);
13181320
return false;
13191321
}
@@ -2058,7 +2060,8 @@ static bool fulfill_onchain(struct peer *peer, struct htlc *htlc)
20582060
return false;
20592061
peer->onchain.resolved[i]
20602062
= htlc_fulfill_tx(peer, i);
2061-
broadcast_tx(peer, peer->onchain.resolved[i], NULL);
2063+
broadcast_tx(peer->dstate->topology,
2064+
peer, peer->onchain.resolved[i], NULL);
20622065
return true;
20632066
}
20642067
}
@@ -2106,14 +2109,14 @@ const char *command_htlc_add(struct peer *peer, u64 msatoshi,
21062109
return "bad expiry";
21072110
}
21082111

2109-
if (expiry < get_block_height(peer->dstate) + peer->dstate->config.min_htlc_expiry) {
2112+
if (expiry < get_block_height(peer->dstate->topology) + peer->dstate->config.min_htlc_expiry) {
21102113
log_unusual(peer->log, "add_htlc: fail: expiry %u is too soon",
21112114
expiry);
21122115
*error_code = BAD_REQUEST_400;
21132116
return "expiry too soon";
21142117
}
21152118

2116-
if (expiry > get_block_height(peer->dstate) + peer->dstate->config.max_htlc_expiry) {
2119+
if (expiry > get_block_height(peer->dstate->topology) + peer->dstate->config.max_htlc_expiry) {
21172120
log_unusual(peer->log, "add_htlc: fail: expiry %u is too far",
21182121
expiry);
21192122
*error_code = BAD_REQUEST_400;
@@ -2842,7 +2845,7 @@ static bool peer_first_connected(struct peer *peer,
28422845
peer->conn = conn;
28432846
io_set_finish(conn, peer_disconnect, peer);
28442847

2845-
peer->anchor.min_depth = get_block_height(peer->dstate);
2848+
peer->anchor.min_depth = get_block_height(peer->dstate->topology);
28462849

28472850
/* FIXME: Attach IO logging for this peer. */
28482851
if (!netaddr_from_fd(io_conn_fd(conn), addr_type, addr_protocol, &addr))
@@ -3249,7 +3252,7 @@ AUTODATA(json_command, &connect_command);
32493252
/* Have any of our HTLCs passed their deadline? */
32503253
static bool any_deadline_past(struct peer *peer)
32513254
{
3252-
u32 height = get_block_height(peer->dstate);
3255+
u32 height = get_block_height(peer->dstate->topology);
32533256
struct htlc_map_iter it;
32543257
struct htlc *h;
32553258

@@ -3272,7 +3275,7 @@ static bool any_deadline_past(struct peer *peer)
32723275

32733276
static void check_htlc_expiry(struct peer *peer)
32743277
{
3275-
u32 height = get_block_height(peer->dstate);
3278+
u32 height = get_block_height(peer->dstate->topology);
32763279
struct htlc_map_iter it;
32773280
struct htlc *h;
32783281

@@ -3400,8 +3403,9 @@ static enum watch_result anchor_depthchange(struct peer *peer,
34003403
return KEEP_WATCHING;
34013404
}
34023405

3403-
void peers_new_block(struct lightningd_state *dstate, unsigned int height)
3406+
void notify_new_block(struct topology *topo, unsigned int height)
34043407
{
3408+
struct lightningd_state *dstate = tal_parent(topo);
34053409
/* This is where we check for anchor timeouts. */
34063410
struct peer *peer;
34073411

@@ -3632,7 +3636,7 @@ static enum watch_result our_htlc_depth(struct peer *peer,
36323636
if (depth == 0)
36333637
return KEEP_WATCHING;
36343638

3635-
height = get_block_height(peer->dstate);
3639+
height = get_block_height(peer->dstate->topology);
36363640

36373641
/* FIXME-OLD #onchain:
36383642
*
@@ -3663,7 +3667,8 @@ static enum watch_result our_htlc_depth(struct peer *peer,
36633667
peer,
36643668
peer->onchain.resolved[out_num],
36653669
our_htlc_timeout_depth, h);
3666-
broadcast_tx(peer, peer->onchain.resolved[out_num], NULL);
3670+
broadcast_tx(peer->dstate->topology,
3671+
peer, peer->onchain.resolved[out_num], NULL);
36673672
}
36683673
return DELETE_WATCH;
36693674
}
@@ -3696,7 +3701,7 @@ static enum watch_result their_htlc_depth(struct peer *peer,
36963701
if (depth == 0)
36973702
return KEEP_WATCHING;
36983703

3699-
height = get_block_height(peer->dstate);
3704+
height = get_block_height(peer->dstate->topology);
37003705

37013706
/* FIXME-OLD #onchain:
37023707
*
@@ -3732,7 +3737,8 @@ static enum watch_result our_main_output_depth(struct peer *peer,
37323737
*/
37333738
peer->onchain.resolved[peer->onchain.to_us_idx]
37343739
= bitcoin_spend_ours(peer);
3735-
broadcast_tx(peer, peer->onchain.resolved[peer->onchain.to_us_idx],
3740+
broadcast_tx(peer->dstate->topology,
3741+
peer, peer->onchain.resolved[peer->onchain.to_us_idx],
37363742
NULL);
37373743
return DELETE_WATCH;
37383744
}
@@ -3852,7 +3858,8 @@ static void resolve_their_htlc(struct peer *peer, unsigned int out_num)
38523858
*/
38533859
if (peer->onchain.htlcs[out_num]->r) {
38543860
peer->onchain.resolved[out_num] = htlc_fulfill_tx(peer, out_num);
3855-
broadcast_tx(peer, peer->onchain.resolved[out_num], NULL);
3861+
broadcast_tx(peer->dstate->topology,
3862+
peer, peer->onchain.resolved[out_num], NULL);
38563863
} else {
38573864
/* FIXME-OLD #onchain:
38583865
*
@@ -4012,7 +4019,7 @@ static enum watch_result check_for_resolution(struct peer *peer,
40124019
struct sha256_double txid;
40134020

40144021
bitcoin_txid(peer->onchain.resolved[i], &txid);
4015-
if (get_tx_depth(peer->dstate, &txid) < forever)
4022+
if (get_tx_depth(peer->dstate->topology, &txid) < forever)
40164023
return KEEP_WATCHING;
40174024
}
40184025

@@ -4146,7 +4153,7 @@ static void resolve_their_steal(struct peer *peer,
41464153
}
41474154
assert(n == tal_count(steal_tx->input));
41484155

4149-
broadcast_tx(peer, steal_tx, NULL);
4156+
broadcast_tx(peer->dstate->topology, peer, steal_tx, NULL);
41504157
}
41514158

41524159
static struct sha256 *get_rhash(struct peer *peer, u64 commit_num,
@@ -4485,6 +4492,22 @@ void reconnect_peers(struct lightningd_state *dstate)
44854492
try_reconnect(peer);
44864493
}
44874494

4495+
/* Return earliest block we're interested in, or 0 for none. */
4496+
u32 get_peer_min_block(struct lightningd_state *dstate)
4497+
{
4498+
u32 min_block = 0;
4499+
struct peer *peer;
4500+
4501+
/* If loaded from database, go back to earliest possible peer anchor. */
4502+
list_for_each(&dstate->peers, peer, list) {
4503+
if (!peer->anchor.min_depth)
4504+
continue;
4505+
if (min_block == 0 || peer->anchor.min_depth < min_block)
4506+
min_block = peer->anchor.min_depth;
4507+
}
4508+
return min_block;
4509+
}
4510+
44884511
/* We may have gone down before broadcasting the anchor. Try again. */
44894512
void rebroadcast_anchors(struct lightningd_state *dstate)
44904513
{
@@ -4498,7 +4521,8 @@ void rebroadcast_anchors(struct lightningd_state *dstate)
44984521
if (!bitcoin_create_anchor(peer))
44994522
peer_fail(peer, __func__);
45004523
else
4501-
broadcast_tx(peer, peer->anchor.tx, NULL);
4524+
broadcast_tx(peer->dstate->topology,
4525+
peer, peer->anchor.tx, NULL);
45024526
}
45034527
}
45044528

daemon/peer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,6 @@ const char *command_htlc_add(struct peer *peer, u64 msatoshi,
270270
enum fail_error *error_code,
271271
struct htlc **htlc);
272272

273-
void peers_new_block(struct lightningd_state *dstate, unsigned int height);
274-
275273
/* Peer has an issue, breakdown and fail. */
276274
void peer_fail(struct peer *peer, const char *caller);
277275

@@ -280,6 +278,8 @@ void peer_watch_anchor(struct peer *peer, int depth);
280278
struct bitcoin_tx *peer_create_close_tx(const tal_t *ctx,
281279
struct peer *peer, u64 fee);
282280

281+
u32 get_peer_min_block(struct lightningd_state *dstate);
282+
283283
void debug_dump_peers(struct lightningd_state *dstate);
284284

285285
void reconnect_peers(struct lightningd_state *dstate);

daemon/watch.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ void watch_topology_changed(struct topology *topo)
221221
struct txwatch_hash_iter i;
222222
struct txwatch *w;
223223
bool needs_rerun;
224-
struct lightningd_state *dstate = tal_parent(topo);
225224

226225
again:
227226
/* Iterating a htable during deletes is safe, but might skip entries. */
@@ -231,7 +230,7 @@ void watch_topology_changed(struct topology *topo)
231230
w = txwatch_hash_next(&topo->txwatches, &i)) {
232231
size_t depth;
233232

234-
depth = get_tx_depth(dstate, &w->txid);
233+
depth = get_tx_depth(topo, &w->txid);
235234
if (depth != w->depth) {
236235
enum watch_result r;
237236
w->depth = depth;

0 commit comments

Comments
 (0)