From 9a0163ec85bedb16ea3781bf84af8c5be9d3e22f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 7 Aug 2015 12:45:30 +0930 Subject: [PATCH] proto_to_locktime: abs and relative locktime handlers. Our current proto_to_locktime actually handles relative locktimes, and HTLCs use absolute. Fix that. Signed-off-by: Rusty Russell --- commit_tx.c | 2 +- protobuf_convert.c | 20 ++++++++++++++++++-- protobuf_convert.h | 3 ++- test-cli/create-commit-spend-tx.c | 2 +- test-cli/create-steal-tx.c | 2 +- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/commit_tx.c b/commit_tx.c index 46444d4e6..078d8ae51 100644 --- a/commit_tx.c +++ b/commit_tx.c @@ -35,7 +35,7 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx, if (!proto_to_pubkey(theirs->final_key, &theirkey)) return tal_free(tx); - if (!proto_to_locktime(theirs->delay, &locktime)) + if (!proto_to_rel_locktime(theirs->delay, &locktime)) return tal_free(tx); /* First output is a P2SH to a complex redeem script (usu. for me) */ diff --git a/protobuf_convert.c b/protobuf_convert.c index 248c07b19..288508199 100644 --- a/protobuf_convert.c +++ b/protobuf_convert.c @@ -81,13 +81,19 @@ void proto_to_sha256(const Sha256Hash *pb, struct sha256 *hash) memcpy(hash->u.u8 + 24, &pb->d, 8); } -bool proto_to_locktime(const Locktime *l, uint32_t *locktime) +static bool proto_to_locktime(const Locktime *l, uint32_t off, + uint32_t *locktime) { switch (l->locktime_case) { case LOCKTIME__LOCKTIME_SECONDS: - *locktime = 500000000 + l->seconds; + *locktime = off + l->seconds; + /* Check for wrap, or too low value */ + if (*locktime < 500000000) + return false; break; case LOCKTIME__LOCKTIME_BLOCKS: + if (l->blocks >= 500000000) + return false; *locktime = l->blocks; break; default: @@ -95,3 +101,13 @@ bool proto_to_locktime(const Locktime *l, uint32_t *locktime) } return true; } + +bool proto_to_rel_locktime(const Locktime *l, uint32_t *locktime) +{ + return proto_to_locktime(l, 500000000, locktime); +} + +bool proto_to_abs_locktime(const Locktime *l, uint32_t *locktime) +{ + return proto_to_locktime(l, 0, locktime); +} diff --git a/protobuf_convert.h b/protobuf_convert.h index f75939680..98713e5f7 100644 --- a/protobuf_convert.h +++ b/protobuf_convert.h @@ -19,5 +19,6 @@ struct sha256; Sha256Hash *sha256_to_proto(const tal_t *ctx, const struct sha256 *hash); void proto_to_sha256(const Sha256Hash *pb, struct sha256 *hash); -bool proto_to_locktime(const Locktime *l, uint32_t *locktime); +bool proto_to_rel_locktime(const Locktime *l, uint32_t *locktime); +bool proto_to_abs_locktime(const Locktime *l, uint32_t *locktime); #endif /* LIGHTNING_PROTOBUF_CONVERT_H */ diff --git a/test-cli/create-commit-spend-tx.c b/test-cli/create-commit-spend-tx.c index a6f20ed8a..e6ae5784d 100644 --- a/test-cli/create-commit-spend-tx.c +++ b/test-cli/create-commit-spend-tx.c @@ -60,7 +60,7 @@ int main(int argc, char *argv[]) o1 = pkt_from_file(argv[2], PKT__PKT_OPEN)->open; o2 = pkt_from_file(argv[3], PKT__PKT_OPEN)->open; a = pkt_from_file(argv[4], PKT__PKT_OPEN_ANCHOR)->open_anchor; - if (!proto_to_locktime(o2->delay, &locktime)) + if (!proto_to_rel_locktime(o2->delay, &locktime)) errx(1, "Invalid locktime in o2"); /* We need our private key to spend commit output. */ diff --git a/test-cli/create-steal-tx.c b/test-cli/create-steal-tx.c index 8fdfd4867..feb50d26e 100644 --- a/test-cli/create-steal-tx.c +++ b/test-cli/create-steal-tx.c @@ -66,7 +66,7 @@ int main(int argc, char *argv[]) o1 = pkt_from_file(argv[4], PKT__PKT_OPEN)->open; o2 = pkt_from_file(argv[5], PKT__PKT_OPEN)->open; - if (!proto_to_locktime(o1->delay, &locktime_seconds)) + if (!proto_to_rel_locktime(o1->delay, &locktime_seconds)) errx(1, "Invalid locktime in o2"); if (!pubkey_from_hexstr(argv[6], &outpubkey))