mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-01 03:24:41 +01:00
7401b26824
Before: Ten builds, laptop -j5, no ccache: ``` real 0m36.686000-38.956000(38.608+/-0.65)s user 2m32.864000-42.253000(40.7545+/-2.7)s sys 0m16.618000-18.316000(17.8531+/-0.48)s ``` Ten builds, laptop -j5, ccache (warm): ``` real 0m8.212000-8.577000(8.39989+/-0.13)s user 0m12.731000-13.212000(12.9751+/-0.17)s sys 0m3.697000-3.902000(3.83722+/-0.064)s ``` After: Ten builds, laptop -j5, no ccache: 8% faster ``` real 0m33.802000-35.773000(35.468+/-0.54)s user 2m19.073000-27.754000(26.2542+/-2.3)s sys 0m15.784000-17.173000(16.7165+/-0.37)s ``` Ten builds, laptop -j5, ccache (warm): 1% faster ``` real 0m8.200000-8.485000(8.30138+/-0.097)s user 0m12.485000-13.100000(12.7344+/-0.19)s sys 0m3.702000-3.889000(3.78787+/-0.056)s ``` Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
117 lines
3.4 KiB
C
117 lines
3.4 KiB
C
#include <bitcoin/short_channel_id.h>
|
|
#include <ccan/tal/str/str.h>
|
|
#include <common/type_to_string.h>
|
|
#include <stdio.h>
|
|
#include <wire/wire.h>
|
|
|
|
/* BOLT#07:
|
|
*
|
|
* The `short_channel_id` is the unique description of the funding
|
|
* transaction. It is constructed as follows:
|
|
|
|
* 1. the most significant 3 bytes: indicating the block height
|
|
* 2. the next 3 bytes: indicating the transaction index within the block
|
|
* 3. the least significant 2 bytes: indicating the output index that pays to the channel.
|
|
*
|
|
* The standard human readable format for `short_channel_id` is created
|
|
* by printing the above components, in the order: block height,
|
|
* transaction index, and output index. Each component is printed as a
|
|
* decimal number, and separated from each other by the small letter
|
|
* `x`. For example, a `short_channel_id` might be written as
|
|
* `539268x845x1`, indicating a channel on the output 1 of the
|
|
* transaction at index 845 of the block at height 539268.
|
|
*/
|
|
|
|
|
|
bool mk_short_channel_id(struct short_channel_id *scid,
|
|
u64 blocknum, u64 txnum, u64 outnum)
|
|
{
|
|
if ((blocknum & 0xFFFFFF) != blocknum
|
|
|| (txnum & 0xFFFFFF) != txnum
|
|
|| (outnum & 0xFFFF) != outnum)
|
|
return false;
|
|
scid->u64 = (((u64)blocknum & 0xFFFFFF) << 40 |
|
|
((u64)txnum & 0xFFFFFF) << 16 |
|
|
(outnum & 0xFFFF));
|
|
return true;
|
|
}
|
|
|
|
bool short_channel_id_from_str(const char *str, size_t strlen,
|
|
struct short_channel_id *dst)
|
|
{
|
|
u32 blocknum, txnum;
|
|
u16 outnum;
|
|
int matches;
|
|
|
|
char buf[strlen + 1];
|
|
memcpy(buf, str, strlen);
|
|
buf[strlen] = 0;
|
|
|
|
matches = sscanf(buf, "%ux%ux%hu", &blocknum, &txnum, &outnum);
|
|
return matches == 3
|
|
&& mk_short_channel_id(dst, blocknum, txnum, outnum);
|
|
}
|
|
|
|
char *short_channel_id_to_str(const tal_t *ctx, const struct short_channel_id *scid)
|
|
{
|
|
return tal_fmt(ctx, "%dx%dx%d",
|
|
short_channel_id_blocknum(scid),
|
|
short_channel_id_txnum(scid),
|
|
short_channel_id_outnum(scid));
|
|
}
|
|
|
|
bool short_channel_id_dir_from_str(const char *str, size_t strlen,
|
|
struct short_channel_id_dir *scidd)
|
|
{
|
|
const char *slash = memchr(str, '/', strlen);
|
|
if (!slash || slash + 2 != str + strlen)
|
|
return false;
|
|
if (!short_channel_id_from_str(str, slash - str, &scidd->scid))
|
|
return false;
|
|
if (slash[1] == '0')
|
|
scidd->dir = 0;
|
|
else if (slash[1] == '1')
|
|
scidd->dir = 1;
|
|
else
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
char *short_channel_id_dir_to_str(const tal_t *ctx,
|
|
const struct short_channel_id_dir *scidd)
|
|
{
|
|
char *str, *scidstr = short_channel_id_to_str(NULL, &scidd->scid);
|
|
str = tal_fmt(ctx, "%s/%u", scidstr, scidd->dir);
|
|
tal_free(scidstr);
|
|
return str;
|
|
}
|
|
|
|
REGISTER_TYPE_TO_STRING(short_channel_id, short_channel_id_to_str);
|
|
REGISTER_TYPE_TO_STRING(short_channel_id_dir, short_channel_id_dir_to_str);
|
|
|
|
void towire_short_channel_id(u8 **pptr,
|
|
const struct short_channel_id *short_channel_id)
|
|
{
|
|
towire_u64(pptr, short_channel_id->u64);
|
|
}
|
|
|
|
void towire_short_channel_id_dir(u8 **pptr,
|
|
const struct short_channel_id_dir *scidd)
|
|
{
|
|
towire_short_channel_id(pptr, &scidd->scid);
|
|
towire_bool(pptr, scidd->dir);
|
|
}
|
|
|
|
void fromwire_short_channel_id(const u8 **cursor, size_t *max,
|
|
struct short_channel_id *short_channel_id)
|
|
{
|
|
short_channel_id->u64 = fromwire_u64(cursor, max);
|
|
}
|
|
|
|
void fromwire_short_channel_id_dir(const u8 **cursor, size_t *max,
|
|
struct short_channel_id_dir *scidd)
|
|
{
|
|
fromwire_short_channel_id(cursor, max, &scidd->scid);
|
|
scidd->dir = fromwire_bool(cursor, max);
|
|
}
|