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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2015-08-07 12:45:30 +09:30
parent 7f21695a63
commit 9a0163ec85
5 changed files with 23 additions and 6 deletions

View File

@ -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) */

View File

@ -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);
}

View File

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

View File

@ -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. */

View File

@ -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))