mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-19 18:11:28 +01:00
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:
parent
85554761c8
commit
8bd334380e
@ -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);
|
||||
|
@ -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 */
|
||||
|
@ -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. */
|
||||
|
Loading…
Reference in New Issue
Block a user