diff --git a/daemon/chaintopology.c b/daemon/chaintopology.c index b20081019..019530c1c 100644 --- a/daemon/chaintopology.c +++ b/daemon/chaintopology.c @@ -516,7 +516,27 @@ u32 get_last_mediantime(struct lightningd_state *dstate, last_mediantime(topo->root, w, &mediantime); return mediantime; } - + +u32 get_tip_mediantime(struct lightningd_state *dstate) +{ + struct topology *topo = dstate->topology; + size_t i, longest = 0; + u32 mediantime; + + mediantime = topo->tips[longest]->mediantime; + + for (i = 1; i < tal_count(topo->tips); i++) { + if (topo->tips[i]->height > topo->tips[longest]->height) { + longest = i; + mediantime = topo->tips[longest]->mediantime; + } else if (topo->tips[i]->height == topo->tips[longest]->height) { + if (topo->tips[i]->mediantime > mediantime) + mediantime = topo->tips[i]->mediantime; + } + } + return mediantime; +} + void setup_topology(struct lightningd_state *dstate) { dstate->topology = tal(dstate, struct topology); diff --git a/daemon/chaintopology.h b/daemon/chaintopology.h index baeef2dd2..16432ecc7 100644 --- a/daemon/chaintopology.h +++ b/daemon/chaintopology.h @@ -14,6 +14,10 @@ size_t get_tx_depth(struct lightningd_state *dstate, const struct txwatch *w); * Assumes the depth is > 0! */ u32 get_last_mediantime(struct lightningd_state *dstate, const struct sha256_double *txid); + +/* Get mediantime of the tip; if more than one, pick greatest time. */ +u32 get_tip_mediantime(struct lightningd_state *dstate); + void setup_topology(struct lightningd_state *dstate); #endif /* LIGHTNING_DAEMON_CRYPTOPKT_H */ diff --git a/daemon/peer.c b/daemon/peer.c index 031a8d0b9..d9af1421b 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -848,17 +848,18 @@ static void commit_tx_depth(struct peer *peer, int depth, return; mediantime = get_last_mediantime(peer->dstate, txid); - assert(mediantime); - - /* FIXME: We should really use bitcoin time here. */ - if (controlled_time().ts.tv_sec > mediantime + + if (get_tip_mediantime(peer->dstate) > mediantime + rel_locktime_to_seconds(&peer->them.locktime)) { /* Free this watch; we're done */ peer->cur_commit.watch = tal_free(peer->cur_commit.watch); state_event(peer, ptr2int(canspend), NULL); } else - log_debug(peer->log, "... still CSV locked"); + log_debug(peer->log, "... still CSV locked (mediantime %u, need %u + %u)", + get_tip_mediantime(peer->dstate), + mediantime, + rel_locktime_to_seconds(&peer->them.locktime)); } /* We should map back from commit_tx permutation to figure out what happened. */