forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial settlement tx testing stub
- Loading branch information
1 parent
19439a7
commit 3f274c1
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
#include "config.h" | ||
#include <inttypes.h> | ||
#include <stdio.h> | ||
#include <common/type_to_string.h> | ||
static bool print_superverbose; | ||
#define SUPERVERBOSE(...) \ | ||
do { if (print_superverbose) printf(__VA_ARGS__); } while(0) | ||
#define PRINT_ACTUAL_FEE | ||
#include "../commit_tx.c" | ||
#include <bitcoin/preimage.h> | ||
#include <ccan/array_size/array_size.h> | ||
#include <ccan/err/err.h> | ||
#include <ccan/str/hex/hex.h> | ||
#include <common/channel_id.h> | ||
#include <common/key_derive.h> | ||
#include <common/setup.h> | ||
#include <common/status.h> | ||
|
||
/* Turn this on to brute-force fee values */ | ||
/*#define DEBUG */ | ||
|
||
/* AUTOGENERATED MOCKS START */ | ||
/* Generated stub for fromwire_bigsize */ | ||
bigsize_t fromwire_bigsize(const u8 **cursor UNNEEDED, size_t *max UNNEEDED) | ||
{ fprintf(stderr, "fromwire_bigsize called!\n"); abort(); } | ||
/* Generated stub for fromwire_channel_id */ | ||
bool fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, | ||
struct channel_id *channel_id UNNEEDED) | ||
{ fprintf(stderr, "fromwire_channel_id called!\n"); abort(); } | ||
/* Generated stub for fromwire_node_id */ | ||
void fromwire_node_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct node_id *id UNNEEDED) | ||
{ fprintf(stderr, "fromwire_node_id called!\n"); abort(); } | ||
/* 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(); } | ||
/* Generated stub for status_fmt */ | ||
void status_fmt(enum log_level level UNNEEDED, | ||
const struct node_id *peer UNNEEDED, | ||
const char *fmt UNNEEDED, ...) | ||
|
||
{ fprintf(stderr, "status_fmt called!\n"); abort(); } | ||
/* Generated stub for towire_bigsize */ | ||
void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED) | ||
{ fprintf(stderr, "towire_bigsize called!\n"); abort(); } | ||
/* Generated stub for towire_channel_id */ | ||
void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id UNNEEDED) | ||
{ fprintf(stderr, "towire_channel_id called!\n"); abort(); } | ||
/* Generated stub for towire_node_id */ | ||
void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED) | ||
{ fprintf(stderr, "towire_node_id called!\n"); abort(); } | ||
/* Generated stub for towire_wireaddr */ | ||
void towire_wireaddr(u8 **pptr UNNEEDED, const struct wireaddr *addr UNNEEDED) | ||
{ fprintf(stderr, "towire_wireaddr called!\n"); abort(); } | ||
/* AUTOGENERATED MOCKS END */ | ||
|
||
/* bitcoind loves its backwards txids! */ | ||
static struct bitcoin_txid txid_from_hex(const char *hex) | ||
{ | ||
struct bitcoin_txid txid; | ||
|
||
if (!bitcoin_txid_from_hex(hex, strlen(hex), &txid)) | ||
abort(); | ||
return txid; | ||
} | ||
|
||
static struct secret secret_from_hex(const char *hex) | ||
{ | ||
struct secret s; | ||
size_t len; | ||
if (strstarts(hex, "0x")) | ||
hex += 2; | ||
len = strlen(hex); | ||
/* BOLT #3: | ||
* | ||
* - Private keys are displayed as 32 bytes plus a trailing 1 | ||
* (Bitcoin's convention for "compressed" private keys, i.e. keys | ||
* for which the public key is compressed). | ||
*/ | ||
if (len == 66 && strends(hex, "01")) | ||
len -= 2; | ||
if (!hex_decode(hex, len, &s, sizeof(s))) | ||
abort(); | ||
return s; | ||
} | ||
|
||
static void tx_must_be_eq(const struct bitcoin_tx *a, | ||
const struct bitcoin_tx *b) | ||
{ | ||
u8 *lina, *linb; | ||
size_t i; | ||
|
||
lina = linearize_tx(tmpctx, a); | ||
linb = linearize_tx(tmpctx, b); | ||
|
||
for (i = 0; i < tal_count(lina); i++) { | ||
if (i >= tal_count(linb)) | ||
errx(1, "Second tx is truncated:\n" | ||
"%s\n" | ||
"%s", | ||
tal_hex(tmpctx, lina), | ||
tal_hex(tmpctx, linb)); | ||
if (lina[i] != linb[i]) | ||
errx(1, "tx differ at offset %zu:\n" | ||
"%s\n" | ||
"%s", | ||
i, | ||
tal_hex(tmpctx, lina), | ||
tal_hex(tmpctx, linb)); | ||
} | ||
if (i != tal_count(linb)) | ||
errx(1, "First tx is truncated:\n" | ||
"%s\n" | ||
"%s", | ||
tal_hex(tmpctx, lina), | ||
tal_hex(tmpctx, linb)); | ||
} | ||
|
||
int main(int argc, const char *argv[]) | ||
{ | ||
common_setup(argv[0]); | ||
tx_must_be_eq(NULL, NULL); | ||
secret_from_hex(NULL); | ||
txid_from_hex(NULL); | ||
common_shutdown(); | ||
|
||
/* FIXME: Do BOLT comparison! */ | ||
return 0; | ||
} |