peer: use tip mediantime for CSV timeout.

Using wallclock is gauche (and I saw it fail once in tests), so fix that
FIXME now it's easy.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-04-24 19:52:35 +09:30
parent 85554761c8
commit 8bd334380e
3 changed files with 31 additions and 6 deletions

View File

@ -517,6 +517,26 @@ u32 get_last_mediantime(struct lightningd_state *dstate,
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);

View File

@ -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 */

View File

@ -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. */