Browse Source

locktime: complete the set of conversion functions.

And add protobuf_convert helpers, too.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
2e873af86f
  1. 21
      bitcoin/locktime.c
  2. 3
      bitcoin/locktime.h
  3. 32
      protobuf_convert.c
  4. 4
      protobuf_convert.h

21
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;
}

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

32
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;
}

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

Loading…
Cancel
Save