From 2e873af86f00cec539fcf61f74c259869846ad5e Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 22 Jan 2016 06:38:08 +1030 Subject: [PATCH] locktime: complete the set of conversion functions. And add protobuf_convert helpers, too. Signed-off-by: Rusty Russell --- bitcoin/locktime.c | 21 +++++++++++++++++++++ bitcoin/locktime.h | 3 +++ protobuf_convert.c | 32 ++++++++++++++++++++++++++++++++ protobuf_convert.h | 4 ++++ 4 files changed, 60 insertions(+) diff --git a/bitcoin/locktime.c b/bitcoin/locktime.c index 05b7ad2b1..0331ce20b 100644 --- a/bitcoin/locktime.c +++ b/bitcoin/locktime.c @@ -72,6 +72,16 @@ u32 rel_locktime_to_seconds(const struct rel_locktime *rel) #endif } +u32 rel_locktime_to_blocks(const struct rel_locktime *rel) +{ + assert(!rel_locktime_is_seconds(rel)); +#ifdef HAS_BIP68 + return rel->locktime & BIP68_LOCKTIME_MASK; +#else + return rel->locktime; +#endif +} + u32 bitcoin_nsequence(const struct rel_locktime *rel) { #ifdef HAS_BIP68 @@ -99,3 +109,14 @@ bool abs_locktime_is_seconds(const struct abs_locktime *abs) return abs_is_seconds(abs->locktime); } +u32 abs_locktime_to_seconds(const struct abs_locktime *abs) +{ + assert(abs_locktime_is_seconds(abs)); + return abs->locktime; +} + +u32 abs_locktime_to_blocks(const struct abs_locktime *abs) +{ + assert(!abs_locktime_is_seconds(abs)); + return abs->locktime; +} diff --git a/bitcoin/locktime.h b/bitcoin/locktime.h index 9ebbc65cf..5c56ec554 100644 --- a/bitcoin/locktime.h +++ b/bitcoin/locktime.h @@ -12,6 +12,7 @@ bool seconds_to_rel_locktime(u32 seconds, struct rel_locktime *rel); bool blocks_to_rel_locktime(u32 blocks, struct rel_locktime *rel); bool rel_locktime_is_seconds(const struct rel_locktime *rel); u32 rel_locktime_to_seconds(const struct rel_locktime *rel); +u32 rel_locktime_to_blocks(const struct rel_locktime *rel); u32 bitcoin_nsequence(const struct rel_locktime *rel); @@ -23,5 +24,7 @@ struct abs_locktime { bool seconds_to_abs_locktime(u32 seconds, struct abs_locktime *abs); bool blocks_to_abs_locktime(u32 blocks, struct abs_locktime *abs); bool abs_locktime_is_seconds(const struct abs_locktime *abs); +u32 abs_locktime_to_seconds(const struct abs_locktime *abs); +u32 abs_locktime_to_blocks(const struct abs_locktime *abs); #endif /* LIGHTNING_BITCOIN_LOCKTIME_H */ diff --git a/protobuf_convert.c b/protobuf_convert.c index d98777a87..55850b974 100644 --- a/protobuf_convert.c +++ b/protobuf_convert.c @@ -129,3 +129,35 @@ bool proto_to_abs_locktime(const Locktime *l, struct abs_locktime *locktime) return false; } } + +Locktime *rel_locktime_to_proto(const tal_t *ctx, + const struct rel_locktime *locktime) +{ + Locktime *l = tal(ctx, Locktime); + locktime__init(l); + + if (rel_locktime_is_seconds(locktime)) { + l->locktime_case = LOCKTIME__LOCKTIME_SECONDS; + l->seconds = rel_locktime_to_seconds(locktime); + } else { + l->locktime_case = LOCKTIME__LOCKTIME_BLOCKS; + l->blocks = rel_locktime_to_blocks(locktime); + } + return l; +} + +Locktime *abs_locktime_to_proto(const tal_t *ctx, + const struct abs_locktime *locktime) +{ + Locktime *l = tal(ctx, Locktime); + locktime__init(l); + + if (abs_locktime_is_seconds(locktime)) { + l->locktime_case = LOCKTIME__LOCKTIME_SECONDS; + l->seconds = abs_locktime_to_seconds(locktime); + } else { + l->locktime_case = LOCKTIME__LOCKTIME_BLOCKS; + l->blocks = abs_locktime_to_blocks(locktime); + } + return l; +} diff --git a/protobuf_convert.h b/protobuf_convert.h index ee0d95c36..18a773284 100644 --- a/protobuf_convert.h +++ b/protobuf_convert.h @@ -23,4 +23,8 @@ struct rel_locktime; struct abs_locktime; bool proto_to_rel_locktime(const Locktime *l, struct rel_locktime *locktime); bool proto_to_abs_locktime(const Locktime *l, struct abs_locktime *locktime); +Locktime *rel_locktime_to_proto(const tal_t *ctx, + const struct rel_locktime *locktime); +Locktime *abs_locktime_to_proto(const tal_t *ctx, + const struct abs_locktime *locktime); #endif /* LIGHTNING_PROTOBUF_CONVERT_H */