|
@ -1,8 +1,6 @@ |
|
|
#include "bitcoin/locktime.h" |
|
|
#include "bitcoin/locktime.h" |
|
|
#include <assert.h> |
|
|
#include <assert.h> |
|
|
|
|
|
|
|
|
/* Alpha uses simple locktimes a-la the tx locktime field; BIP68 uses
|
|
|
|
|
|
* a bitmask */ |
|
|
|
|
|
#define SECONDS_POINT 500000000 |
|
|
#define SECONDS_POINT 500000000 |
|
|
|
|
|
|
|
|
#define BIP68_SECONDS_FLAG (1<<22) |
|
|
#define BIP68_SECONDS_FLAG (1<<22) |
|
@ -32,66 +30,42 @@ static bool abs_is_seconds(u32 locktime) |
|
|
|
|
|
|
|
|
bool seconds_to_rel_locktime(u32 seconds, struct rel_locktime *rel) |
|
|
bool seconds_to_rel_locktime(u32 seconds, struct rel_locktime *rel) |
|
|
{ |
|
|
{ |
|
|
#if HAS_BIP68 |
|
|
|
|
|
if ((seconds >> BIP68_SECONDS_SHIFT) > BIP68_LOCKTIME_MASK) |
|
|
if ((seconds >> BIP68_SECONDS_SHIFT) > BIP68_LOCKTIME_MASK) |
|
|
return false; |
|
|
return false; |
|
|
rel->locktime = BIP68_SECONDS_FLAG | (seconds >> BIP68_SECONDS_SHIFT); |
|
|
rel->locktime = BIP68_SECONDS_FLAG | (seconds >> BIP68_SECONDS_SHIFT); |
|
|
return true; |
|
|
return true; |
|
|
#else |
|
|
|
|
|
/* Make abs-style time by adding SECONDS_POINT. */ |
|
|
|
|
|
return abs_seconds_to_locktime(seconds + SECONDS_POINT, &rel->locktime); |
|
|
|
|
|
#endif |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
bool blocks_to_rel_locktime(u32 blocks, struct rel_locktime *rel) |
|
|
bool blocks_to_rel_locktime(u32 blocks, struct rel_locktime *rel) |
|
|
{ |
|
|
{ |
|
|
#if HAS_BIP68 |
|
|
|
|
|
if (blocks > BIP68_LOCKTIME_MASK) |
|
|
if (blocks > BIP68_LOCKTIME_MASK) |
|
|
return false; |
|
|
return false; |
|
|
#endif |
|
|
|
|
|
rel->locktime = blocks; |
|
|
rel->locktime = blocks; |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
bool rel_locktime_is_seconds(const struct rel_locktime *rel) |
|
|
bool rel_locktime_is_seconds(const struct rel_locktime *rel) |
|
|
{ |
|
|
{ |
|
|
#if HAS_BIP68 |
|
|
|
|
|
return rel->locktime & BIP68_SECONDS_FLAG; |
|
|
return rel->locktime & BIP68_SECONDS_FLAG; |
|
|
#else |
|
|
|
|
|
return abs_is_seconds(rel->locktime); |
|
|
|
|
|
#endif |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
u32 rel_locktime_to_seconds(const struct rel_locktime *rel) |
|
|
u32 rel_locktime_to_seconds(const struct rel_locktime *rel) |
|
|
{ |
|
|
{ |
|
|
assert(rel_locktime_is_seconds(rel)); |
|
|
assert(rel_locktime_is_seconds(rel)); |
|
|
#if HAS_BIP68 |
|
|
|
|
|
return (rel->locktime & BIP68_LOCKTIME_MASK) << BIP68_SECONDS_SHIFT; |
|
|
return (rel->locktime & BIP68_LOCKTIME_MASK) << BIP68_SECONDS_SHIFT; |
|
|
#else |
|
|
|
|
|
return rel->locktime - SECONDS_POINT; |
|
|
|
|
|
#endif |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
u32 rel_locktime_to_blocks(const struct rel_locktime *rel) |
|
|
u32 rel_locktime_to_blocks(const struct rel_locktime *rel) |
|
|
{ |
|
|
{ |
|
|
assert(!rel_locktime_is_seconds(rel)); |
|
|
assert(!rel_locktime_is_seconds(rel)); |
|
|
#if HAS_BIP68 |
|
|
|
|
|
return rel->locktime & BIP68_LOCKTIME_MASK; |
|
|
return rel->locktime & BIP68_LOCKTIME_MASK; |
|
|
#else |
|
|
|
|
|
return rel->locktime; |
|
|
|
|
|
#endif |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
u32 bitcoin_nsequence(const struct rel_locktime *rel) |
|
|
u32 bitcoin_nsequence(const struct rel_locktime *rel) |
|
|
{ |
|
|
{ |
|
|
#if HAS_BIP68 |
|
|
|
|
|
/* Can't set disable bit, or other bits except low 16 and bit 22 */ |
|
|
/* Can't set disable bit, or other bits except low 16 and bit 22 */ |
|
|
assert(!(rel->locktime & ~(BIP68_SECONDS_FLAG|BIP68_LOCKTIME_MASK))); |
|
|
assert(!(rel->locktime & ~(BIP68_SECONDS_FLAG|BIP68_LOCKTIME_MASK))); |
|
|
return rel->locktime; |
|
|
return rel->locktime; |
|
|
#else |
|
|
|
|
|
/* Alpha uses the original proposal: simply invert the bits. */ |
|
|
|
|
|
return ~rel->locktime; |
|
|
|
|
|
#endif |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
bool seconds_to_abs_locktime(u32 seconds, struct abs_locktime *abs) |
|
|
bool seconds_to_abs_locktime(u32 seconds, struct abs_locktime *abs) |
|
|