2017-08-31 04:08:12 +02:00
|
|
|
#include "../routing.c"
|
2018-02-05 05:09:27 +01:00
|
|
|
#include <stdio.h>
|
2017-08-31 04:08:12 +02:00
|
|
|
|
|
|
|
struct broadcast_state *new_broadcast_state(tal_t *ctx UNNEEDED)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-21 16:06:07 +01:00
|
|
|
void status_fmt(enum log_level level UNUSED, const char *fmt, ...)
|
2018-02-05 05:09:27 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vprintf(fmt, ap);
|
|
|
|
printf("\n");
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2017-08-31 04:08:12 +02:00
|
|
|
/* AUTOGENERATED MOCKS START */
|
|
|
|
/* Generated stub for fromwire_channel_announcement */
|
2018-02-20 21:59:09 +01:00
|
|
|
bool fromwire_channel_announcement(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, secp256k1_ecdsa_signature *node_signature_1 UNNEEDED, secp256k1_ecdsa_signature *node_signature_2 UNNEEDED, secp256k1_ecdsa_signature *bitcoin_signature_1 UNNEEDED, secp256k1_ecdsa_signature *bitcoin_signature_2 UNNEEDED, u8 **features UNNEEDED, struct bitcoin_blkid *chain_hash UNNEEDED, struct short_channel_id *short_channel_id UNNEEDED, struct pubkey *node_id_1 UNNEEDED, struct pubkey *node_id_2 UNNEEDED, struct pubkey *bitcoin_key_1 UNNEEDED, struct pubkey *bitcoin_key_2 UNNEEDED)
|
2017-08-31 04:08:12 +02:00
|
|
|
{ fprintf(stderr, "fromwire_channel_announcement called!\n"); abort(); }
|
|
|
|
/* Generated stub for fromwire_channel_update */
|
2018-02-20 21:59:09 +01:00
|
|
|
bool fromwire_channel_update(const void *p UNNEEDED, secp256k1_ecdsa_signature *signature UNNEEDED, struct bitcoin_blkid *chain_hash UNNEEDED, struct short_channel_id *short_channel_id UNNEEDED, u32 *timestamp UNNEEDED, u16 *flags UNNEEDED, u16 *cltv_expiry_delta UNNEEDED, u64 *htlc_minimum_msat UNNEEDED, u32 *fee_base_msat UNNEEDED, u32 *fee_proportional_millionths UNNEEDED)
|
2017-08-31 04:08:12 +02:00
|
|
|
{ fprintf(stderr, "fromwire_channel_update called!\n"); abort(); }
|
|
|
|
/* Generated stub for fromwire_node_announcement */
|
2018-02-20 21:59:09 +01:00
|
|
|
bool fromwire_node_announcement(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, secp256k1_ecdsa_signature *signature UNNEEDED, u8 **features UNNEEDED, u32 *timestamp UNNEEDED, struct pubkey *node_id UNNEEDED, u8 rgb_color[3] UNNEEDED, u8 alias[32] UNNEEDED, u8 **addresses UNNEEDED)
|
2017-08-31 04:08:12 +02:00
|
|
|
{ fprintf(stderr, "fromwire_node_announcement called!\n"); abort(); }
|
2018-01-21 01:36:41 +01:00
|
|
|
/* Generated stub for fromwire_peektype */
|
|
|
|
int fromwire_peektype(const u8 *cursor UNNEEDED)
|
|
|
|
{ fprintf(stderr, "fromwire_peektype called!\n"); abort(); }
|
2017-10-22 15:33:58 +02:00
|
|
|
/* Generated stub for fromwire_u8 */
|
|
|
|
u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
|
|
|
{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); }
|
2017-10-23 06:17:38 +02:00
|
|
|
/* Generated stub for fromwire_wireaddr */
|
|
|
|
bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED)
|
|
|
|
{ fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); }
|
2018-01-18 00:32:36 +01:00
|
|
|
/* Generated stub for onion_type_name */
|
|
|
|
const char *onion_type_name(int e UNNEEDED)
|
|
|
|
{ fprintf(stderr, "onion_type_name called!\n"); abort(); }
|
2018-01-31 17:53:50 +01:00
|
|
|
/* Generated stub for replace_broadcast */
|
|
|
|
bool replace_broadcast(struct broadcast_state *bstate UNNEEDED,
|
|
|
|
u64 *index UNNEEDED,
|
|
|
|
const int type UNNEEDED,
|
|
|
|
const u8 *tag UNNEEDED,
|
|
|
|
const u8 *payload UNNEEDED)
|
|
|
|
{ fprintf(stderr, "replace_broadcast called!\n"); abort(); }
|
2017-12-22 02:10:00 +01:00
|
|
|
/* Generated stub for status_failed */
|
2018-02-08 02:25:12 +01:00
|
|
|
void status_failed(enum status_failreason code UNNEEDED,
|
|
|
|
const char *fmt UNNEEDED, ...)
|
2017-12-22 02:10:00 +01:00
|
|
|
{ fprintf(stderr, "status_failed called!\n"); abort(); }
|
2018-03-08 05:10:26 +01:00
|
|
|
/* Generated stub for towire_errorfmt */
|
|
|
|
u8 *towire_errorfmt(const tal_t *ctx UNNEEDED,
|
|
|
|
const struct channel_id *channel UNNEEDED,
|
|
|
|
const char *fmt UNNEEDED, ...)
|
|
|
|
{ fprintf(stderr, "towire_errorfmt called!\n"); abort(); }
|
2017-08-31 04:08:12 +02:00
|
|
|
/* Generated stub for towire_pubkey */
|
|
|
|
void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED)
|
|
|
|
{ fprintf(stderr, "towire_pubkey called!\n"); abort(); }
|
|
|
|
/* Generated stub for towire_short_channel_id */
|
|
|
|
void towire_short_channel_id(u8 **pptr UNNEEDED,
|
|
|
|
const struct short_channel_id *short_channel_id UNNEEDED)
|
|
|
|
{ fprintf(stderr, "towire_short_channel_id called!\n"); abort(); }
|
|
|
|
/* Generated stub for towire_u16 */
|
|
|
|
void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED)
|
|
|
|
{ fprintf(stderr, "towire_u16 called!\n"); abort(); }
|
|
|
|
/* AUTOGENERATED MOCKS END */
|
|
|
|
|
|
|
|
const void *trc;
|
|
|
|
|
2017-09-01 06:18:55 +02:00
|
|
|
/* Updates existing route if required. */
|
2018-03-04 03:26:59 +01:00
|
|
|
static struct half_chan *add_connection(struct routing_state *rstate,
|
2017-09-01 06:18:55 +02:00
|
|
|
const struct pubkey *from,
|
|
|
|
const struct pubkey *to,
|
|
|
|
u32 base_fee, s32 proportional_fee,
|
|
|
|
u32 delay)
|
|
|
|
{
|
2018-03-02 09:59:16 +01:00
|
|
|
struct short_channel_id scid;
|
2018-03-04 03:26:59 +01:00
|
|
|
struct half_chan *c;
|
|
|
|
struct chan *chan;
|
2018-03-02 09:59:16 +01:00
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
/* Make a unique scid. */
|
|
|
|
memcpy(&scid, from, sizeof(scid) / 2);
|
|
|
|
memcpy((char *)&scid + sizeof(scid) / 2, to, sizeof(scid) / 2);
|
|
|
|
|
|
|
|
chan = get_channel(rstate, &scid);
|
|
|
|
if (!chan)
|
2018-03-04 03:26:59 +01:00
|
|
|
chan = new_chan(rstate, &scid, from, to);
|
2018-03-02 09:59:17 +01:00
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
c = &chan->half[pubkey_idx(from, to)];
|
2017-09-01 06:18:55 +02:00
|
|
|
c->base_fee = base_fee;
|
|
|
|
c->proportional_fee = proportional_fee;
|
|
|
|
c->delay = delay;
|
|
|
|
c->active = true;
|
|
|
|
c->flags = get_channel_direction(from, to);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
/* Returns chan connecting from and to: *idx set to refer
|
2018-03-02 09:59:17 +01:00
|
|
|
* to connection with src=from, dst=to */
|
2018-03-04 03:26:59 +01:00
|
|
|
static struct chan *find_channel(struct routing_state *rstate,
|
2018-03-02 09:59:17 +01:00
|
|
|
const struct node *from,
|
|
|
|
const struct node *to,
|
|
|
|
int *idx)
|
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
*idx = pubkey_idx(&from->id, &to->id);
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
n = tal_count(to->chans);
|
2018-03-02 09:59:17 +01:00
|
|
|
for (i = 0; i < n; i++) {
|
2018-03-04 03:26:59 +01:00
|
|
|
if (to->chans[i]->nodes[*idx] == from)
|
|
|
|
return to->chans[i];
|
2018-03-02 09:59:17 +01:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
static struct half_chan *get_connection(struct routing_state *rstate,
|
2018-03-02 09:59:17 +01:00
|
|
|
const struct pubkey *from_id,
|
|
|
|
const struct pubkey *to_id)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
struct node *from, *to;
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *c;
|
2018-03-02 09:59:17 +01:00
|
|
|
|
|
|
|
from = get_node(rstate, from_id);
|
|
|
|
to = get_node(rstate, to_id);
|
|
|
|
if (!from || ! to)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
c = find_channel(rstate, from, to, &idx);
|
|
|
|
if (!c)
|
|
|
|
return NULL;
|
2018-03-04 03:26:59 +01:00
|
|
|
return &c->half[idx];
|
2018-03-02 09:59:17 +01:00
|
|
|
}
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
static bool channel_is_between(const struct chan *chan,
|
2018-03-04 03:23:32 +01:00
|
|
|
const struct pubkey *a, const struct pubkey *b)
|
|
|
|
{
|
|
|
|
if (pubkey_eq(&chan->nodes[0]->id, a)
|
|
|
|
&& pubkey_eq(&chan->nodes[1]->id, b))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (pubkey_eq(&chan->nodes[0]->id, b)
|
|
|
|
&& pubkey_eq(&chan->nodes[1]->id, a))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-31 04:08:12 +02:00
|
|
|
int main(void)
|
|
|
|
{
|
2017-12-18 07:44:10 +01:00
|
|
|
static const struct bitcoin_blkid zerohash;
|
2017-08-31 04:08:12 +02:00
|
|
|
const tal_t *ctx = trc = tal_tmpctx(NULL);
|
|
|
|
struct routing_state *rstate;
|
|
|
|
struct pubkey a, b, c, d;
|
|
|
|
struct privkey tmp;
|
2017-12-18 05:15:38 +01:00
|
|
|
u64 fee;
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan **route;
|
2017-12-18 05:15:41 +01:00
|
|
|
const double riskfactor = 1.0 / BLOCKS_PER_YEAR / 10000;
|
2017-08-31 04:08:12 +02:00
|
|
|
|
|
|
|
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
|
|
|
|
| SECP256K1_CONTEXT_SIGN);
|
|
|
|
|
|
|
|
memset(&tmp, 'a', sizeof(tmp));
|
2018-03-02 09:59:17 +01:00
|
|
|
rstate = new_routing_state(ctx, &zerohash, &a, 0);
|
2017-12-02 23:28:19 +01:00
|
|
|
|
2017-08-31 04:08:12 +02:00
|
|
|
pubkey_from_privkey(&tmp, &a);
|
2017-09-01 06:18:55 +02:00
|
|
|
new_node(rstate, &a);
|
2017-08-31 04:08:12 +02:00
|
|
|
|
|
|
|
memset(&tmp, 'b', sizeof(tmp));
|
|
|
|
pubkey_from_privkey(&tmp, &b);
|
2017-09-01 06:18:55 +02:00
|
|
|
new_node(rstate, &b);
|
2017-08-31 04:08:12 +02:00
|
|
|
|
|
|
|
/* A<->B */
|
2017-09-01 06:18:55 +02:00
|
|
|
add_connection(rstate, &a, &b, 1, 1, 1);
|
2017-08-31 04:08:12 +02:00
|
|
|
|
2018-03-04 03:26:54 +01:00
|
|
|
route = find_route(ctx, rstate, &a, &b, 1000, riskfactor, 0.0, NULL, &fee);
|
|
|
|
assert(tal_count(route) == 1);
|
2017-08-31 04:08:12 +02:00
|
|
|
assert(fee == 0);
|
|
|
|
|
|
|
|
/* A<->B<->C */
|
|
|
|
memset(&tmp, 'c', sizeof(tmp));
|
|
|
|
pubkey_from_privkey(&tmp, &c);
|
2017-09-01 06:18:55 +02:00
|
|
|
new_node(rstate, &c);
|
2017-08-31 04:08:12 +02:00
|
|
|
|
|
|
|
status_trace("A = %s", type_to_string(trc, struct pubkey, &a));
|
|
|
|
status_trace("B = %s", type_to_string(trc, struct pubkey, &b));
|
|
|
|
status_trace("C = %s", type_to_string(trc, struct pubkey, &c));
|
2017-09-01 06:18:55 +02:00
|
|
|
add_connection(rstate, &b, &c, 1, 1, 1);
|
2017-08-31 04:08:12 +02:00
|
|
|
|
2018-03-04 03:26:54 +01:00
|
|
|
route = find_route(ctx, rstate, &a, &c, 1000, riskfactor, 0.0, NULL, &fee);
|
|
|
|
assert(tal_count(route) == 2);
|
2017-08-31 04:08:12 +02:00
|
|
|
assert(fee == 1);
|
|
|
|
|
|
|
|
/* A<->D<->C: Lower base, higher percentage. */
|
|
|
|
memset(&tmp, 'd', sizeof(tmp));
|
|
|
|
pubkey_from_privkey(&tmp, &d);
|
2017-09-01 06:18:55 +02:00
|
|
|
new_node(rstate, &d);
|
2017-08-31 04:08:12 +02:00
|
|
|
status_trace("D = %s", type_to_string(trc, struct pubkey, &d));
|
|
|
|
|
2017-09-01 06:18:55 +02:00
|
|
|
add_connection(rstate, &a, &d, 0, 2, 1);
|
|
|
|
add_connection(rstate, &d, &c, 0, 2, 1);
|
2017-08-31 04:08:12 +02:00
|
|
|
|
|
|
|
/* Will go via D for small amounts. */
|
2018-03-04 03:26:54 +01:00
|
|
|
route = find_route(ctx, rstate, &a, &c, 1000, riskfactor, 0.0, NULL, &fee);
|
|
|
|
assert(tal_count(route) == 2);
|
|
|
|
assert(channel_is_between(route[0], &a, &d));
|
|
|
|
assert(channel_is_between(route[1], &d, &c));
|
2017-08-31 04:08:12 +02:00
|
|
|
assert(fee == 0);
|
|
|
|
|
|
|
|
/* Will go via B for large amounts. */
|
2018-03-04 03:26:54 +01:00
|
|
|
route = find_route(ctx, rstate, &a, &c, 3000000, riskfactor, 0.0, NULL, &fee);
|
|
|
|
assert(tal_count(route) == 2);
|
|
|
|
assert(channel_is_between(route[0], &a, &b));
|
|
|
|
assert(channel_is_between(route[1], &b, &c));
|
2017-09-01 06:18:38 +02:00
|
|
|
assert(fee == 1 + 3);
|
|
|
|
|
|
|
|
/* Make B->C inactive, force it back via D */
|
|
|
|
get_connection(rstate, &b, &c)->active = false;
|
2018-03-04 03:26:54 +01:00
|
|
|
route = find_route(ctx, rstate, &a, &c, 3000000, riskfactor, 0.0, NULL, &fee);
|
|
|
|
assert(tal_count(route) == 2);
|
|
|
|
assert(channel_is_between(route[0], &a, &d));
|
|
|
|
assert(channel_is_between(route[1], &d, &c));
|
2017-09-01 06:18:38 +02:00
|
|
|
assert(fee == 0 + 6);
|
2017-08-31 04:08:12 +02:00
|
|
|
|
|
|
|
tal_free(ctx);
|
2017-09-01 06:18:54 +02:00
|
|
|
secp256k1_context_destroy(secp256k1_ctx);
|
2017-08-31 04:08:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|