2016-11-29 20:51:50 +01:00
|
|
|
#include "wire.h"
|
2018-12-03 00:15:06 +01:00
|
|
|
#include <assert.h>
|
2019-03-11 20:03:14 +01:00
|
|
|
#include <bitcoin/chainparams.h>
|
2017-03-29 12:56:15 +02:00
|
|
|
#include <bitcoin/preimage.h>
|
2017-02-21 05:45:28 +01:00
|
|
|
#include <bitcoin/shadouble.h>
|
2017-12-18 07:41:52 +01:00
|
|
|
#include <bitcoin/tx.h>
|
2017-08-18 06:43:52 +02:00
|
|
|
#include <ccan/crypto/ripemd160/ripemd160.h>
|
2018-02-26 03:32:58 +01:00
|
|
|
#include <ccan/crypto/siphash24/siphash24.h>
|
2016-11-29 20:51:50 +01:00
|
|
|
#include <ccan/endian/endian.h>
|
|
|
|
#include <ccan/mem/mem.h>
|
|
|
|
#include <ccan/tal/tal.h>
|
2019-02-20 12:31:48 +01:00
|
|
|
#include <common/amount.h>
|
common/node_id: new type.
Node ids are pubkeys, but we only use them as pubkeys for routing and checking
gossip messages. So we're packing and unpacking them constantly, and wasting
some space and time.
This introduces a new type, explicitly the SEC1 compressed encoding
(33 bytes). We ensure its validity when we load from the db, or get it
from JSON. We still use 'struct pubkey' for peer messages, which checks
validity.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
39475-39572(39518+/-36),2880732,41.150000-41.390000(41.298+/-0.085),2.260000-2.550000(2.336+/-0.11),44.390000-65.150000(58.648+/-7.5),32.740000-33.020000(32.89+/-0.093),44.130000-45.090000(44.566+/-0.32)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:06 +02:00
|
|
|
#include <common/node_id.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
2016-11-29 20:51:50 +01:00
|
|
|
|
|
|
|
void towire(u8 **pptr, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
size_t oldsize = tal_count(*pptr);
|
|
|
|
|
|
|
|
tal_resize(pptr, oldsize + len);
|
|
|
|
memcpy(*pptr + oldsize, memcheck(data, len), len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_u8(u8 **pptr, u8 v)
|
|
|
|
{
|
|
|
|
towire(pptr, &v, sizeof(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_u16(u8 **pptr, u16 v)
|
|
|
|
{
|
|
|
|
be16 l = cpu_to_be16(v);
|
|
|
|
towire(pptr, &l, sizeof(l));
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_u32(u8 **pptr, u32 v)
|
|
|
|
{
|
|
|
|
be32 l = cpu_to_be32(v);
|
|
|
|
towire(pptr, &l, sizeof(l));
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_u64(u8 **pptr, u64 v)
|
|
|
|
{
|
|
|
|
be64 l = cpu_to_be64(v);
|
|
|
|
towire(pptr, &l, sizeof(l));
|
|
|
|
}
|
|
|
|
|
2019-07-18 07:17:16 +02:00
|
|
|
static void towire_tlv_uint(u8 **pptr, u64 v)
|
|
|
|
{
|
|
|
|
u8 bytes[8];
|
|
|
|
size_t num_zeroes;
|
|
|
|
be64 val;
|
|
|
|
|
|
|
|
val = cpu_to_be64(v);
|
|
|
|
BUILD_ASSERT(sizeof(val) == sizeof(bytes));
|
|
|
|
memcpy(bytes, &val, sizeof(bytes));
|
|
|
|
|
|
|
|
for (num_zeroes = 0; num_zeroes < sizeof(bytes); num_zeroes++)
|
|
|
|
if (bytes[num_zeroes] != 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
towire(pptr, bytes + num_zeroes, sizeof(bytes) - num_zeroes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_tu16(u8 **pptr, u16 v)
|
|
|
|
{
|
|
|
|
return towire_tlv_uint(pptr, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_tu32(u8 **pptr, u32 v)
|
|
|
|
{
|
|
|
|
return towire_tlv_uint(pptr, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_tu64(u8 **pptr, u64 v)
|
|
|
|
{
|
|
|
|
return towire_tlv_uint(pptr, v);
|
|
|
|
}
|
|
|
|
|
2018-02-21 12:48:32 +01:00
|
|
|
void towire_double(u8 **pptr, const double *v)
|
|
|
|
{
|
|
|
|
towire(pptr, v, sizeof(*v));
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:39:21 +01:00
|
|
|
void towire_bool(u8 **pptr, bool v)
|
|
|
|
{
|
2017-09-26 23:02:48 +02:00
|
|
|
u8 val = v;
|
2017-01-04 04:39:21 +01:00
|
|
|
towire(pptr, &val, sizeof(val));
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:33:59 +01:00
|
|
|
void towire_int(u8 **pptr, int v)
|
|
|
|
{
|
|
|
|
towire(pptr, &v, sizeof(v));
|
|
|
|
}
|
|
|
|
|
2019-07-30 07:25:12 +02:00
|
|
|
void towire_bigsize(u8 **pptr, const bigsize_t val)
|
2019-04-03 02:18:06 +02:00
|
|
|
{
|
2019-07-30 07:25:12 +02:00
|
|
|
u8 buf[BIGSIZE_MAX_LEN];
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = bigsize_put(buf, val);
|
|
|
|
towire(pptr, buf, len);
|
2019-04-03 02:18:06 +02:00
|
|
|
}
|
|
|
|
|
2016-12-02 08:41:06 +01:00
|
|
|
void towire_pubkey(u8 **pptr, const struct pubkey *pubkey)
|
2016-11-29 20:51:50 +01:00
|
|
|
{
|
pubkey: rename PUBKEY_DER_LEN to PUBKEY_CMPR_LEN.
Pubkeys are not not actually DER encoding, but Pieter Wuille corrected
me: it's SEC 1 documented encoding.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
38922-39297(39180.6+/-1.3e+02),2880728,41.040000-41.160000(41.106+/-0.05),2.270000-2.530000(2.338+/-0.097),44.570000-53.980000(49.696+/-3),32.840000-33.080000(32.95+/-0.095),43.060000-44.950000(43.696+/-0.72)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:05 +02:00
|
|
|
u8 output[PUBKEY_CMPR_LEN];
|
2016-11-29 20:51:50 +01:00
|
|
|
size_t outputlen = sizeof(output);
|
|
|
|
|
2018-01-10 03:43:23 +01:00
|
|
|
secp256k1_ec_pubkey_serialize(secp256k1_ctx, output, &outputlen,
|
|
|
|
&pubkey->pubkey,
|
|
|
|
SECP256K1_EC_COMPRESSED);
|
2017-05-05 08:41:44 +02:00
|
|
|
|
2016-11-29 20:51:50 +01:00
|
|
|
towire(pptr, output, outputlen);
|
|
|
|
}
|
|
|
|
|
common/node_id: new type.
Node ids are pubkeys, but we only use them as pubkeys for routing and checking
gossip messages. So we're packing and unpacking them constantly, and wasting
some space and time.
This introduces a new type, explicitly the SEC1 compressed encoding
(33 bytes). We ensure its validity when we load from the db, or get it
from JSON. We still use 'struct pubkey' for peer messages, which checks
validity.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
39475-39572(39518+/-36),2880732,41.150000-41.390000(41.298+/-0.085),2.260000-2.550000(2.336+/-0.11),44.390000-65.150000(58.648+/-7.5),32.740000-33.020000(32.89+/-0.093),44.130000-45.090000(44.566+/-0.32)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:06 +02:00
|
|
|
void towire_node_id(u8 **pptr, const struct node_id *id)
|
|
|
|
{
|
2019-04-08 11:58:32 +02:00
|
|
|
/* Cheap sanity check */
|
|
|
|
assert(id->k[0] == 0x2 || id->k[0] == 0x3);
|
common/node_id: new type.
Node ids are pubkeys, but we only use them as pubkeys for routing and checking
gossip messages. So we're packing and unpacking them constantly, and wasting
some space and time.
This introduces a new type, explicitly the SEC1 compressed encoding
(33 bytes). We ensure its validity when we load from the db, or get it
from JSON. We still use 'struct pubkey' for peer messages, which checks
validity.
Results from 5 runs, min-max(mean +/- stddev):
store_load_msec,vsz_kb,store_rewrite_sec,listnodes_sec,listchannels_sec,routing_sec,peer_write_all_sec
39475-39572(39518+/-36),2880732,41.150000-41.390000(41.298+/-0.085),2.260000-2.550000(2.336+/-0.11),44.390000-65.150000(58.648+/-7.5),32.740000-33.020000(32.89+/-0.093),44.130000-45.090000(44.566+/-0.32)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-04-08 08:34:06 +02:00
|
|
|
towire(pptr, id->k, sizeof(id->k));
|
|
|
|
}
|
|
|
|
|
2017-05-06 04:19:44 +02:00
|
|
|
void towire_secret(u8 **pptr, const struct secret *secret)
|
|
|
|
{
|
|
|
|
towire(pptr, secret->data, sizeof(secret->data));
|
|
|
|
}
|
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
void towire_privkey(u8 **pptr, const struct privkey *privkey)
|
|
|
|
{
|
2017-05-06 04:19:44 +02:00
|
|
|
towire_secret(pptr, &privkey->secret);
|
2017-02-21 05:45:28 +01:00
|
|
|
}
|
|
|
|
|
2017-01-25 00:33:42 +01:00
|
|
|
void towire_secp256k1_ecdsa_signature(u8 **pptr,
|
|
|
|
const secp256k1_ecdsa_signature *sig)
|
2016-11-29 20:51:50 +01:00
|
|
|
{
|
|
|
|
u8 compact[64];
|
|
|
|
|
2016-12-02 08:41:06 +01:00
|
|
|
secp256k1_ecdsa_signature_serialize_compact(secp256k1_ctx,
|
2017-01-25 00:33:42 +01:00
|
|
|
compact, sig);
|
2016-11-29 20:51:50 +01:00
|
|
|
towire(pptr, compact, sizeof(compact));
|
|
|
|
}
|
|
|
|
|
2017-10-26 05:01:19 +02:00
|
|
|
void towire_secp256k1_ecdsa_recoverable_signature(u8 **pptr,
|
|
|
|
const secp256k1_ecdsa_recoverable_signature *rsig)
|
|
|
|
{
|
|
|
|
u8 compact[64];
|
|
|
|
int recid;
|
|
|
|
|
|
|
|
secp256k1_ecdsa_recoverable_signature_serialize_compact(secp256k1_ctx,
|
|
|
|
compact,
|
|
|
|
&recid,
|
|
|
|
rsig);
|
|
|
|
towire(pptr, compact, sizeof(compact));
|
|
|
|
towire_u8(pptr, recid);
|
|
|
|
}
|
|
|
|
|
2016-11-29 20:51:50 +01:00
|
|
|
void towire_channel_id(u8 **pptr, const struct channel_id *channel_id)
|
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
towire(pptr, channel_id, sizeof(*channel_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
void towire_short_channel_id(u8 **pptr,
|
|
|
|
const struct short_channel_id *short_channel_id)
|
|
|
|
{
|
2018-03-01 10:22:24 +01:00
|
|
|
towire_u64(pptr, short_channel_id->u64);
|
2016-11-29 20:51:50 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 05:11:27 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-11-29 20:51:50 +01:00
|
|
|
void towire_sha256(u8 **pptr, const struct sha256 *sha256)
|
|
|
|
{
|
|
|
|
towire(pptr, sha256, sizeof(*sha256));
|
|
|
|
}
|
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
void towire_sha256_double(u8 **pptr, const struct sha256_double *sha256d)
|
|
|
|
{
|
|
|
|
towire_sha256(pptr, &sha256d->sha);
|
|
|
|
}
|
|
|
|
|
2017-12-18 07:41:52 +01:00
|
|
|
void towire_bitcoin_txid(u8 **pptr, const struct bitcoin_txid *txid)
|
|
|
|
{
|
|
|
|
towire_sha256_double(pptr, &txid->shad);
|
|
|
|
}
|
|
|
|
|
2018-12-03 00:15:06 +01:00
|
|
|
void towire_bitcoin_signature(u8 **pptr, const struct bitcoin_signature *sig)
|
|
|
|
{
|
|
|
|
assert(sighash_type_valid(sig->sighash_type));
|
|
|
|
towire_secp256k1_ecdsa_signature(pptr, &sig->s);
|
|
|
|
towire_u8(pptr, sig->sighash_type);
|
|
|
|
}
|
|
|
|
|
2017-12-18 07:44:10 +01:00
|
|
|
void towire_bitcoin_blkid(u8 **pptr, const struct bitcoin_blkid *blkid)
|
|
|
|
{
|
|
|
|
towire_sha256_double(pptr, &blkid->shad);
|
|
|
|
}
|
|
|
|
|
2017-03-29 12:56:15 +02:00
|
|
|
void towire_preimage(u8 **pptr, const struct preimage *preimage)
|
|
|
|
{
|
|
|
|
towire(pptr, preimage, sizeof(*preimage));
|
|
|
|
}
|
|
|
|
|
2017-08-18 06:43:52 +02:00
|
|
|
void towire_ripemd160(u8 **pptr, const struct ripemd160 *ripemd)
|
|
|
|
{
|
|
|
|
towire(pptr, ripemd, sizeof(*ripemd));
|
|
|
|
}
|
|
|
|
|
2016-11-29 20:51:50 +01:00
|
|
|
void towire_u8_array(u8 **pptr, const u8 *arr, size_t num)
|
|
|
|
{
|
|
|
|
towire(pptr, arr, num);
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:39:20 +01:00
|
|
|
void towire_pad(u8 **pptr, size_t num)
|
2016-11-29 20:51:50 +01:00
|
|
|
{
|
|
|
|
/* Simply insert zeros. */
|
|
|
|
size_t oldsize = tal_count(*pptr);
|
|
|
|
|
|
|
|
tal_resize(pptr, oldsize + num);
|
|
|
|
memset(*pptr + oldsize, 0, num);
|
|
|
|
}
|
2018-01-04 18:19:02 +01:00
|
|
|
|
2018-02-08 02:25:12 +01:00
|
|
|
void towire_wirestring(u8 **pptr, const char *str)
|
|
|
|
{
|
|
|
|
towire(pptr, str, strlen(str) + 1);
|
|
|
|
}
|
|
|
|
|
2018-01-04 18:19:02 +01:00
|
|
|
void towire_bitcoin_tx(u8 **pptr, const struct bitcoin_tx *tx)
|
|
|
|
{
|
|
|
|
u8 *lin = linearize_tx(tmpctx, tx);
|
2018-07-28 08:00:16 +02:00
|
|
|
towire_u8_array(pptr, lin, tal_count(lin));
|
2018-01-04 18:19:02 +01:00
|
|
|
}
|
2018-02-26 03:32:58 +01:00
|
|
|
|
|
|
|
void towire_siphash_seed(u8 **pptr, const struct siphash_seed *seed)
|
|
|
|
{
|
|
|
|
towire(pptr, seed, sizeof(*seed));
|
|
|
|
}
|
2019-02-20 12:31:48 +01:00
|
|
|
|
|
|
|
void towire_amount_msat(u8 **pptr, const struct amount_msat msat)
|
|
|
|
{
|
2019-02-21 04:45:56 +01:00
|
|
|
towire_u64(pptr, msat.millisatoshis); /* Raw: primitive */
|
2019-02-20 12:31:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void towire_amount_sat(u8 **pptr, const struct amount_sat sat)
|
|
|
|
{
|
2019-02-21 04:45:56 +01:00
|
|
|
towire_u64(pptr, sat.satoshis); /* Raw: primitive */
|
2019-02-20 12:31:48 +01:00
|
|
|
}
|
2019-03-11 20:03:14 +01:00
|
|
|
|
|
|
|
void towire_bip32_key_version(u8 **pptr, const struct bip32_key_version *version)
|
|
|
|
{
|
|
|
|
towire_u32(pptr, version->bip32_pubkey_version);
|
|
|
|
towire_u32(pptr, version->bip32_privkey_version);
|
2019-07-18 07:17:16 +02:00
|
|
|
}
|
2019-08-15 19:41:23 +02:00
|
|
|
|
|
|
|
void towire_bitcoin_tx_output(u8 **pptr, const struct bitcoin_tx_output *output)
|
|
|
|
{
|
|
|
|
towire_amount_sat(pptr, output->amount);
|
|
|
|
towire_u16(pptr, tal_count(output->script));
|
|
|
|
towire_u8_array(pptr, output->script, tal_count(output->script));
|
|
|
|
}
|
2019-09-25 22:38:45 +02:00
|
|
|
|
|
|
|
void towire_chainparams(u8 **cursor, const struct chainparams *chainparams)
|
|
|
|
{
|
|
|
|
towire_bitcoin_blkid(cursor, &chainparams->genesis_blockhash);
|
|
|
|
}
|