Skip to content

Commit

Permalink
timestamp support in htlc
Browse files Browse the repository at this point in the history
  • Loading branch information
backpacker69 committed Oct 15, 2019
1 parent 7ba484e commit 85a9889
Show file tree
Hide file tree
Showing 21 changed files with 95 additions and 26 deletions.
1 change: 1 addition & 0 deletions channeld/channel_wire.csv
Expand Up @@ -80,6 +80,7 @@ msgdata,channel_funding_depth,depth,u32,
msgtype,channel_offer_htlc,1004
msgdata,channel_offer_htlc,amount_msat,amount_msat,
msgdata,channel_offer_htlc,cltv_expiry,u32,
msgdata,channel_offer_htlc,timestamp,u32,
msgdata,channel_offer_htlc,payment_hash,sha256,
msgdata,channel_offer_htlc,onion_routing_packet,u8,1366

Expand Down
25 changes: 19 additions & 6 deletions channeld/channeld.c
Expand Up @@ -625,21 +625,30 @@ static void handle_peer_add_htlc(struct peer *peer, const u8 *msg)
u64 id;
struct amount_msat amount;
u32 cltv_expiry;
u32 timestamp;
struct sha256 payment_hash;
u8 onion_routing_packet[TOTAL_PACKET_SIZE];
enum channel_add_err add_err;
struct htlc *htlc;

if (!fromwire_update_add_htlc(msg, &channel_id, &id, &amount,
&payment_hash, &cltv_expiry,
&payment_hash, &cltv_expiry, &timestamp,
onion_routing_packet))
peer_failed(peer->pps,
&peer->channel_id,
"Bad peer_add_htlc %s", tal_hex(msg, msg));

add_err = channel_add_htlc(peer->channel, REMOTE, id, amount,
cltv_expiry, &payment_hash,
cltv_expiry, timestamp, &payment_hash,
onion_routing_packet, &htlc, NULL);

status_trace("Peer adding HTLC %"PRIu64" amount=%s cltv=%u timestamp=%u gave %s",
id,
type_to_string(tmpctx, struct amount_msat, &amount),
cltv_expiry,
timestamp,
channel_add_err_name(add_err));

if (add_err != CHANNEL_ERR_ADD_OK)
peer_failed(peer->pps,
&peer->channel_id,
Expand Down Expand Up @@ -1306,6 +1315,7 @@ static u8 *got_commitsig_msg(const tal_t *ctx,
a.amount = htlc->amount;
a.payment_hash = htlc->rhash;
a.cltv_expiry = abs_locktime_to_blocks(&htlc->expiry);
a.timestamp = htlc->timestamp;
memcpy(a.onion_routing_packet,
htlc->routing,
sizeof(a.onion_routing_packet));
Expand Down Expand Up @@ -1986,6 +1996,7 @@ static void resend_commitment(struct peer *peer, const struct changed_htlc *last
&h->rhash,
abs_locktime_to_blocks(
&h->expiry),
h->timestamp,
h->routing);
sync_crypto_write(peer->pps, take(msg));
} else if (h->state == SENT_REMOVE_COMMIT) {
Expand Down Expand Up @@ -2593,6 +2604,7 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
{
u8 *msg;
u32 cltv_expiry;
u32 timestamp;
struct amount_msat amount;
struct sha256 payment_hash;
u8 onion_routing_packet[TOTAL_PACKET_SIZE];
Expand All @@ -2607,25 +2619,26 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
"funding not locked for offer_htlc");

if (!fromwire_channel_offer_htlc(inmsg, &amount,
&cltv_expiry, &payment_hash,
&cltv_expiry, &timestamp, &payment_hash,
onion_routing_packet))
master_badmsg(WIRE_CHANNEL_OFFER_HTLC, inmsg);

e = channel_add_htlc(peer->channel, LOCAL, peer->htlc_id,
amount, cltv_expiry, &payment_hash,
amount, cltv_expiry, timestamp, &payment_hash,
onion_routing_packet, NULL, &htlc_fee);
status_debug("Adding HTLC %"PRIu64" amount=%s cltv=%u gave %s",
status_trace("Adding HTLC %"PRIu64" amount=%s cltv=%u timestamp=%u gave %s",
peer->htlc_id,
type_to_string(tmpctx, struct amount_msat, &amount),
cltv_expiry,
timestamp,
channel_add_err_name(e));

switch (e) {
case CHANNEL_ERR_ADD_OK:
/* Tell the peer. */
msg = towire_update_add_htlc(NULL, &peer->channel_id,
peer->htlc_id, amount,
&payment_hash, cltv_expiry,
&payment_hash, cltv_expiry, timestamp,
onion_routing_packet);
sync_crypto_write(peer->pps, take(msg));
start_commit_timer(peer);
Expand Down
2 changes: 2 additions & 0 deletions channeld/channeld_htlc.h
Expand Up @@ -17,6 +17,8 @@ struct htlc {
struct amount_msat amount;
/* When the HTLC can no longer be redeemed. */
struct abs_locktime expiry;
/* Timestamp when HTLC was created. */
u32 timestamp;
/* The hash of the preimage which can redeem this HTLC */
struct sha256 rhash;
/* The preimage which hashes to rhash (if known) */
Expand Down
19 changes: 18 additions & 1 deletion channeld/full_channel.c
Expand Up @@ -248,6 +248,12 @@ static void add_htlcs(const struct chainparams *chainparams,
&keyset->self_revocation_key);
}

/* set timestamp in transaction */
if (htlc->timestamp)
tx->wtx->timestamp = htlc->timestamp;
else
status_trace("missing timestamp from htlc");

/* Append to array. */
assert(tal_count(*txs) == tal_count(*wscripts));

Expand Down Expand Up @@ -290,6 +296,11 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx,
channel->view[side].owed[!side], committed, htlcmap,
commitment_number ^ channel->commitment_number_obscurer, side);

if (tal_count(*htlcmap)) {
if((*htlcmap)[0])
txs[0]->wtx->timestamp = (*htlcmap)[0]->timestamp;
}

*wscripts = tal_arr(ctx, const u8 *, 1);
(*wscripts)[0] = bitcoin_redeem_2of2(*wscripts,
&channel->funding_pubkey[side],
Expand Down Expand Up @@ -369,6 +380,7 @@ static enum channel_add_err add_htlc(struct channel *channel,
u64 id,
struct amount_msat amount,
u32 cltv_expiry,
u32 timestamp,
const struct sha256 *payment_hash,
const u8 routing[TOTAL_PACKET_SIZE],
struct htlc **htlcp,
Expand Down Expand Up @@ -403,6 +415,7 @@ static enum channel_add_err add_htlc(struct channel *channel,
return CHANNEL_ERR_INVALID_EXPIRY;
}

htlc->timestamp = timestamp;
htlc->rhash = *payment_hash;
htlc->fail = NULL;
htlc->failcode = 0;
Expand Down Expand Up @@ -617,6 +630,7 @@ enum channel_add_err channel_add_htlc(struct channel *channel,
u64 id,
struct amount_msat amount,
u32 cltv_expiry,
u32 timestamp,
const struct sha256 *payment_hash,
const u8 routing[TOTAL_PACKET_SIZE],
struct htlc **htlcp,
Expand All @@ -629,7 +643,7 @@ enum channel_add_err channel_add_htlc(struct channel *channel,
else
state = RCVD_ADD_HTLC;

return add_htlc(channel, state, id, amount, cltv_expiry,
return add_htlc(channel, state, id, amount, cltv_expiry, timestamp,
payment_hash, routing, htlcp, true, htlc_fee);
}

Expand Down Expand Up @@ -1200,18 +1214,21 @@ bool channel_force_htlcs(struct channel *channel,

status_debug("Restoring HTLC %zu/%zu:"
" id=%"PRIu64" amount=%s cltv=%u"
" timestamp=%u"
" payment_hash=%s",
i, tal_count(htlcs),
htlcs[i].id,
type_to_string(tmpctx, struct amount_msat,
&htlcs[i].amount),
htlcs[i].cltv_expiry,
htlcs[i].timestamp,
type_to_string(tmpctx, struct sha256,
&htlcs[i].payment_hash));

e = add_htlc(channel, hstates[i],
htlcs[i].id, htlcs[i].amount,
htlcs[i].cltv_expiry,
htlcs[i].timestamp,
&htlcs[i].payment_hash,
htlcs[i].onion_routing_packet, &htlc, false, NULL);
if (e != CHANNEL_ERR_ADD_OK) {
Expand Down
2 changes: 2 additions & 0 deletions channeld/full_channel.h
Expand Up @@ -93,6 +93,7 @@ u32 actual_feerate(const struct channel *channel,
* @id: unique HTLC id.
* @amount: amount in millisatoshi.
* @cltv_expiry: block number when HTLC can no longer be redeemed.
* @timestamp: timestamp of HTLC creation.
* @payment_hash: hash whose preimage can redeem HTLC.
* @routing: routing information (copied)
* @htlcp: optional pointer for resulting htlc: filled in if and only if CHANNEL_ERR_NONE.
Expand All @@ -106,6 +107,7 @@ enum channel_add_err channel_add_htlc(struct channel *channel,
u64 id,
struct amount_msat msatoshi,
u32 cltv_expiry,
u32 timestamp,
const struct sha256 *payment_hash,
const u8 routing[TOTAL_PACKET_SIZE],
struct htlc **htlcp,
Expand Down
4 changes: 2 additions & 2 deletions channeld/test/run-full_channel.c
Expand Up @@ -161,7 +161,7 @@ static const struct htlc **include_htlcs(struct channel *channel, enum side side

memset(&preimage, i, sizeof(preimage));
sha256(&hash, &preimage, sizeof(preimage));
e = channel_add_htlc(channel, sender, i, msatoshi, 500+i, &hash,
e = channel_add_htlc(channel, sender, i, msatoshi, 500+i, 0, &hash,
dummy_routing, NULL, NULL);
assert(e == CHANNEL_ERR_ADD_OK);
htlcs[i] = channel_get_htlc(channel, sender, i);
Expand Down Expand Up @@ -253,7 +253,7 @@ static void send_and_fulfill_htlc(struct channel *channel,
memset(&r, 0, sizeof(r));
sha256(&rhash, &r, sizeof(r));

assert(channel_add_htlc(channel, sender, 1337, msatoshi, 900, &rhash,
assert(channel_add_htlc(channel, sender, 1337, msatoshi, 900, 0, &rhash,
dummy_routing, NULL, NULL) == CHANNEL_ERR_ADD_OK);

changed_htlcs = tal_arr(channel, const struct htlc *, 0);
Expand Down
1 change: 1 addition & 0 deletions common/htlc_tx.c
Expand Up @@ -16,6 +16,7 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
u32 locktime)
{
struct bitcoin_tx *tx = bitcoin_tx(ctx, chainparams, 1, 1);

u8 *wscript;
struct amount_sat amount;

Expand Down
2 changes: 2 additions & 0 deletions common/htlc_wire.c
Expand Up @@ -13,6 +13,7 @@ void towire_added_htlc(u8 **pptr, const struct added_htlc *added)
towire_amount_msat(pptr, added->amount);
towire_sha256(pptr, &added->payment_hash);
towire_u32(pptr, added->cltv_expiry);
towire_u32(pptr, added->timestamp);
towire(pptr, added->onion_routing_packet,
sizeof(added->onion_routing_packet));
}
Expand Down Expand Up @@ -80,6 +81,7 @@ void fromwire_added_htlc(const u8 **cursor, size_t *max,
added->amount = fromwire_amount_msat(cursor, max);
fromwire_sha256(cursor, max, &added->payment_hash);
added->cltv_expiry = fromwire_u32(cursor, max);
added->timestamp = fromwire_u32(cursor, max);
fromwire(cursor, max, added->onion_routing_packet,
sizeof(added->onion_routing_packet));
}
Expand Down
1 change: 1 addition & 0 deletions common/htlc_wire.h
Expand Up @@ -17,6 +17,7 @@ struct added_htlc {
struct amount_msat amount;
struct sha256 payment_hash;
u32 cltv_expiry;
u32 timestamp;
u8 onion_routing_packet[TOTAL_PACKET_SIZE];
};

Expand Down
4 changes: 4 additions & 0 deletions lightningd/htlc_end.c
Expand Up @@ -110,6 +110,7 @@ struct htlc_in *htlc_in_check(const struct htlc_in *hin, const char *abortstr)
struct htlc_in *new_htlc_in(const tal_t *ctx,
struct channel *channel, u64 id,
struct amount_msat msat, u32 cltv_expiry,
u32 timestamp,
const struct sha256 *payment_hash,
const struct secret *shared_secret TAKES,
const u8 *onion_routing_packet)
Expand All @@ -121,6 +122,7 @@ struct htlc_in *new_htlc_in(const tal_t *ctx,
hin->key.id = id;
hin->msat = msat;
hin->cltv_expiry = cltv_expiry;
hin->timestamp = timestamp;
hin->payment_hash = *payment_hash;
if (shared_secret)
hin->shared_secret = tal_dup(hin, struct secret, shared_secret);
Expand Down Expand Up @@ -247,6 +249,7 @@ struct htlc_out *new_htlc_out(const tal_t *ctx,
struct channel *channel,
struct amount_msat msat,
u32 cltv_expiry,
u32 timestamp,
const struct sha256 *payment_hash,
const u8 *onion_routing_packet,
bool am_origin,
Expand All @@ -261,6 +264,7 @@ struct htlc_out *new_htlc_out(const tal_t *ctx,
hout->key.id = HTLC_INVALID_ID;
hout->msat = msat;
hout->cltv_expiry = cltv_expiry;
hout->timestamp = timestamp;
hout->payment_hash = *payment_hash;
memcpy(hout->onion_routing_packet, onion_routing_packet,
sizeof(hout->onion_routing_packet));
Expand Down
4 changes: 4 additions & 0 deletions lightningd/htlc_end.h
Expand Up @@ -26,6 +26,7 @@ struct htlc_in {
struct htlc_key key;
struct amount_msat msat;
u32 cltv_expiry;
u32 timestamp;
struct sha256 payment_hash;

enum htlc_state hstate;
Expand Down Expand Up @@ -62,6 +63,7 @@ struct htlc_out {
struct htlc_key key;
struct amount_msat msat;
u32 cltv_expiry;
u32 timestamp;
struct sha256 payment_hash;

enum htlc_state hstate;
Expand Down Expand Up @@ -128,6 +130,7 @@ struct htlc_out *find_htlc_out(const struct htlc_out_map *map,
struct htlc_in *new_htlc_in(const tal_t *ctx,
struct channel *channel, u64 id,
struct amount_msat msat, u32 cltv_expiry,
u32 timestamp,
const struct sha256 *payment_hash,
const struct secret *shared_secret TAKES,
const u8 *onion_routing_packet);
Expand All @@ -137,6 +140,7 @@ struct htlc_out *new_htlc_out(const tal_t *ctx,
struct channel *channel,
struct amount_msat msat,
u32 cltv_expiry,
u32 timestamp,
const struct sha256 *payment_hash,
const u8 *onion_routing_packet,
bool am_origin,
Expand Down
7 changes: 5 additions & 2 deletions lightningd/pay.c
Expand Up @@ -732,8 +732,11 @@ send_payment(struct lightningd *ld,
type_to_string(tmpctx, struct amount_msat, &route[0].amount),
n_hops, type_to_string(tmpctx, struct amount_msat, &msat));

/* we have to set timestamp now, otherwise htlc won't happen */
u32 timestamp = time_now().ts.tv_sec;

failcode = send_htlc_out(channel, route[0].amount,
base_expiry + route[0].delay,
base_expiry + route[0].delay, timestamp,
rhash, onion, NULL, &hout);
if (failcode) {
fail = immediate_routing_failure(cmd, ld,
Expand Down Expand Up @@ -768,7 +771,7 @@ send_payment(struct lightningd *ld,
payment->status = PAYMENT_PENDING;
payment->msatoshi = msat;
payment->msatoshi_sent = route[0].amount;
payment->timestamp = time_now().ts.tv_sec;
payment->timestamp = timestamp;
payment->payment_preimage = NULL;
payment->path_secrets = tal_steal(payment, path_secrets);
payment->route_nodes = tal_steal(payment, ids);
Expand Down
2 changes: 2 additions & 0 deletions lightningd/peer_control.c
Expand Up @@ -484,6 +484,7 @@ static void json_add_htlcs(struct lightningd *ld,
"msatoshi", "amount_msat");
json_add_u64(response, "expiry", hin->cltv_expiry);
json_add_sha256(response, "payment_hash", &hin->payment_hash);
json_add_u64(response, "timestamp", hin->timestamp);
json_add_string(response, "state",
htlc_state_name(hin->hstate));
if (htlc_is_trimmed(REMOTE, hin->msat, local_feerate,
Expand All @@ -505,6 +506,7 @@ static void json_add_htlcs(struct lightningd *ld,
"msatoshi", "amount_msat");
json_add_u64(response, "expiry", hout->cltv_expiry);
json_add_sha256(response, "payment_hash", &hout->payment_hash);
json_add_u64(response, "timestamp", hout->timestamp);
json_add_string(response, "state",
htlc_state_name(hout->hstate));
if (htlc_is_trimmed(LOCAL, hout->msat, local_feerate,
Expand Down

0 comments on commit 85a9889

Please sign in to comment.