mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-22 06:41:44 +01:00
Move short_channel_id primitive into bitcoin
Not really a bitcoin primitive but the place where we keep all the small stuff currently.
This commit is contained in:
parent
5912c68185
commit
fa6e53bb08
7 changed files with 65 additions and 42 deletions
2
Makefile
2
Makefile
|
@ -39,6 +39,7 @@ BITCOIN_SRC := \
|
|||
bitcoin/pullpush.c \
|
||||
bitcoin/script.c \
|
||||
bitcoin/shadouble.c \
|
||||
bitcoin/short_channel_id.c \
|
||||
bitcoin/signature.c \
|
||||
bitcoin/tx.c \
|
||||
bitcoin/varint.c
|
||||
|
@ -177,6 +178,7 @@ BITCOIN_HEADERS := bitcoin/address.h \
|
|||
bitcoin/pullpush.h \
|
||||
bitcoin/script.h \
|
||||
bitcoin/shadouble.h \
|
||||
bitcoin/short_channel_id.h \
|
||||
bitcoin/signature.h \
|
||||
bitcoin/tx.h \
|
||||
bitcoin/varint.h
|
||||
|
|
34
bitcoin/short_channel_id.c
Normal file
34
bitcoin/short_channel_id.c
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include <bitcoin/short_channel_id.h>
|
||||
#include <ccan/tal/str/str.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
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, "%u:%u:%hu", &blocknum, &txnum, &outnum);
|
||||
dst->blocknum = blocknum;
|
||||
dst->txnum = txnum;
|
||||
dst->outnum = outnum;
|
||||
return matches == 3;
|
||||
}
|
||||
|
||||
char *short_channel_id_to_str(tal_t *ctx, const struct short_channel_id *scid)
|
||||
{
|
||||
return tal_fmt(ctx, "%d:%d:%d", scid->blocknum, scid->txnum, scid->outnum);
|
||||
}
|
||||
|
||||
bool short_channel_id_eq(const struct short_channel_id *a,
|
||||
const struct short_channel_id *b)
|
||||
{
|
||||
return a->blocknum == b->blocknum && a->txnum == b->txnum &&
|
||||
a->outnum == b->outnum;
|
||||
}
|
28
bitcoin/short_channel_id.h
Normal file
28
bitcoin/short_channel_id.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
#ifndef LIGHTNING_BITCOIN_SHORT_CHANNEL_ID_H
|
||||
#define LIGHTNING_BITCOIN_SHORT_CHANNEL_ID_H
|
||||
#include "config.h"
|
||||
#include <ccan/short_types/short_types.h>
|
||||
#include <ccan/tal/tal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/* Short Channel ID is composed of 3 bytes for the block height, 3
|
||||
* bytes of tx index in block and 2 bytes of output index. The
|
||||
* bitfield is mainly for unit tests where it is nice to be able to
|
||||
* just memset them and not have to take care about the extra byte for
|
||||
* u32 */
|
||||
struct short_channel_id {
|
||||
u32 blocknum : 24;
|
||||
u32 txnum : 24;
|
||||
u16 outnum;
|
||||
};
|
||||
|
||||
bool short_channel_id_from_str(const char *str, size_t strlen,
|
||||
struct short_channel_id *dst);
|
||||
|
||||
bool short_channel_id_eq(const struct short_channel_id *a,
|
||||
const struct short_channel_id *b);
|
||||
|
||||
char *short_channel_id_to_str(tal_t *ctx, const struct short_channel_id *scid);
|
||||
|
||||
#endif /* LIGHTNING_BITCOIN_SHORT_CHANNEL_ID_H */
|
|
@ -477,25 +477,6 @@ static bool get_slash_u32(const char **arg, u32 *v)
|
|||
return (endp == *arg);
|
||||
}
|
||||
|
||||
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, "%u:%u:%hu", &blocknum, &txnum, &outnum);
|
||||
dst->blocknum = blocknum;
|
||||
dst->txnum = txnum;
|
||||
dst->outnum = outnum;
|
||||
return matches == 3;
|
||||
}
|
||||
|
||||
|
||||
/* srcid/dstid/base/var/delay/minblocks */
|
||||
char *opt_add_route(const char *arg, struct lightningd_state *dstate)
|
||||
{
|
||||
|
|
|
@ -177,10 +177,4 @@ struct route_hop *get_route(tal_t *ctx, struct routing_state *rstate,
|
|||
* the direction bit the matching channel should get */
|
||||
#define get_channel_direction(from, to) (pubkey_cmp(from, to) > 0)
|
||||
|
||||
bool short_channel_id_from_str(const char *str, size_t strlen,
|
||||
struct short_channel_id *dst);
|
||||
|
||||
bool short_channel_id_eq(const struct short_channel_id *a,
|
||||
const struct short_channel_id *b);
|
||||
|
||||
#endif /* LIGHTNING_DAEMON_ROUTING_H */
|
||||
|
|
|
@ -73,10 +73,3 @@ bool is_unknown_msg_discardable(const u8 *cursor)
|
|||
enum wire_type t = fromwire_peektype(cursor);
|
||||
return unknown_type(t) && (t & 1);
|
||||
}
|
||||
|
||||
bool short_channel_id_eq(const struct short_channel_id *a,
|
||||
const struct short_channel_id *b)
|
||||
{
|
||||
return a->blocknum == b->blocknum && a->txnum == b->txnum &&
|
||||
a->outnum == b->outnum;
|
||||
}
|
||||
|
|
11
wire/wire.h
11
wire/wire.h
|
@ -4,21 +4,12 @@
|
|||
#include <bitcoin/privkey.h>
|
||||
#include <bitcoin/pubkey.h>
|
||||
#include <bitcoin/shadouble.h>
|
||||
#include <bitcoin/short_channel_id.h>
|
||||
#include <bitcoin/signature.h>
|
||||
#include <ccan/crypto/sha256/sha256.h>
|
||||
#include <ccan/short_types/short_types.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* Short Channel ID is composed of 3 bytes for the block height, 3
|
||||
* bytes of tx index in block and 2 bytes of output index. The
|
||||
* bitfield is mainly for unit tests where it is nice to be able to
|
||||
* just memset them and not have to take care about the extra byte for
|
||||
* u32 */
|
||||
struct short_channel_id {
|
||||
u32 blocknum : 24;
|
||||
u32 txnum : 24;
|
||||
u16 outnum;
|
||||
};
|
||||
struct channel_id {
|
||||
u8 id[32];
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue