Skip to content

Commit

Permalink
Merge Windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
jb55 committed Nov 28, 2024
2 parents 7365c20 + 3d471ea commit e03588e
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 84 deletions.
53 changes: 2 additions & 51 deletions ccan/ccan/tal/str/str.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/* Licensed under BSD-MIT - see LICENSE file for details */
#include <unistd.h>
//#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include "str.h"
#include <sys/types.h>
#include <regex.h>
//#include <regex.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdio.h>
#include <ccan/str/str.h>

Expand Down Expand Up @@ -236,51 +235,3 @@ static size_t count_open_braces(const char *string)
#endif
}

bool tal_strreg_(const tal_t *ctx, const char *string, const char *label,
const char *regex, ...)
{
size_t nmatch = 1 + count_open_braces(regex);
regmatch_t matches[nmatch];
regex_t r;
bool ret = false;
unsigned int i;
va_list ap;

if (regcomp(&r, regex, REG_EXTENDED) != 0)
goto fail_no_re;

if (regexec(&r, string, nmatch, matches, 0) != 0)
goto fail;

ret = true;
va_start(ap, regex);
for (i = 1; i < nmatch; i++) {
char **arg = va_arg(ap, char **);
if (arg) {
/* eg. ([a-z])? can give "no match". */
if (matches[i].rm_so == -1)
*arg = NULL;
else {
*arg = tal_strndup_(ctx,
string + matches[i].rm_so,
matches[i].rm_eo
- matches[i].rm_so,
label);
/* FIXME: If we fail, we set some and leak! */
if (!*arg) {
ret = false;
break;
}
}
}
}
va_end(ap);
fail:
regfree(&r);
fail_no_re:
if (taken(regex))
tal_free(regex);
if (taken(string))
tal_free(string);
return ret;
}
3 changes: 2 additions & 1 deletion ccan/ccan/tal/tal.c
Original file line number Diff line number Diff line change
Expand Up @@ -952,9 +952,10 @@ static bool check_err(struct tal_hdr *t, const char *errorstr,
{
if (errorstr) {
/* Try not to malloc: it may be corrupted. */
char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
char *msg = malloc(strlen(errorstr) + 20 + strlen(errmsg) + 1);
sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
call_error(msg);
free(msg);
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion deps/secp256k1
Submodule secp256k1 updated 91 files
+9 −3 .cirrus.yml
+1 −1 .github/actions/install-homebrew-valgrind/action.yml
+5 −0 .github/actions/run-in-docker-action/action.yml
+120 −36 .github/workflows/ci.yml
+2 −0 .gitignore
+52 −0 CHANGELOG.md
+134 −70 CMakeLists.txt
+2 −1 CONTRIBUTING.md
+29 −0 Makefile.am
+6 −3 README.md
+16 −0 build-aux/m4/bitcoin_secp.m4
+7 −4 ci/ci.sh
+1 −1 ci/linux-debian.Dockerfile
+2 −2 cmake/CheckArm32Assembly.cmake
+18 −0 cmake/CheckMemorySanitizer.cmake
+82 −51 configure.ac
+2 −2 contrib/lax_der_parsing.h
+54 −0 doc/musig.md
+38 −37 doc/release-process.md
+9 −8 examples/CMakeLists.txt
+11 −13 examples/ecdh.c
+11 −13 examples/ecdsa.c
+121 −0 examples/ellswift.c
+260 −0 examples/musig.c
+14 −17 examples/schnorr.c
+64 −74 include/secp256k1.h
+1 −1 include/secp256k1_ecdh.h
+3 −3 include/secp256k1_ellswift.h
+13 −10 include/secp256k1_extrakeys.h
+588 −0 include/secp256k1_musig.h
+7 −7 include/secp256k1_preallocated.h
+12 −12 include/secp256k1_recovery.h
+3 −3 include/secp256k1_schnorrsig.h
+6 −3 src/CMakeLists.txt
+52 −48 src/assumptions.h
+1 −1 src/bench_ecmult.c
+7 −0 src/checkmem.h
+57 −0 src/ctime_tests.c
+2 −2 src/ecmult_const_impl.h
+108 −13 src/ecmult_gen.h
+2 −2 src/ecmult_gen_compute_table.h
+79 −55 src/ecmult_gen_compute_table_impl.h
+264 −53 src/ecmult_gen_impl.h
+10 −20 src/ecmult_impl.h
+3 −8 src/field.h
+0 −7 src/field_10x26_impl.h
+0 −7 src/field_5x52_impl.h
+4 −9 src/field_impl.h
+16 −0 src/group.h
+46 −16 src/group_impl.h
+3 −0 src/hash.h
+14 −5 src/hash_impl.h
+33 −0 src/hsort.h
+125 −0 src/hsort_impl.h
+4 −6 src/modinv32_impl.h
+4 −6 src/modinv64_impl.h
+5 −2 src/modules/ecdh/main_impl.h
+3 −3 src/modules/ecdh/tests_impl.h
+3 −1 src/modules/ellswift/main_impl.h
+26 −26 src/modules/ellswift/tests_impl.h
+18 −18 src/modules/extrakeys/tests_impl.h
+8 −0 src/modules/musig/Makefile.am.include
+32 −0 src/modules/musig/keyagg.h
+291 −0 src/modules/musig/keyagg_impl.h
+12 −0 src/modules/musig/main_impl.h
+24 −0 src/modules/musig/session.h
+816 −0 src/modules/musig/session_impl.h
+1,143 −0 src/modules/musig/tests_impl.h
+346 −0 src/modules/musig/vectors.h
+4 −4 src/modules/recovery/tests_impl.h
+4 −2 src/modules/schnorrsig/main_impl.h
+3 −3 src/modules/schnorrsig/tests_exhaustive_impl.h
+30 −30 src/modules/schnorrsig/tests_impl.h
+49 −28 src/precompute_ecmult_gen.c
+1,767 −9,734 src/precomputed_ecmult_gen.c
+2 −2 src/precomputed_ecmult_gen.h
+4 −4 src/scalar.h
+47 −32 src/scalar_4x64_impl.h
+7 −17 src/scalar_8x32_impl.h
+6 −2 src/scalar_impl.h
+19 −14 src/scalar_low_impl.h
+2 −0 src/scratch.h
+45 −31 src/secp256k1.c
+11 −11 src/testrand.h
+22 −22 src/testrand_impl.h
+523 −334 src/tests.c
+6 −6 src/tests_exhaustive.c
+123 −4 src/testutil.h
+81 −10 src/util.h
+23 −13 tools/check-abi.sh
+656 −0 tools/test_vectors_musig2_generate.py
17 changes: 17 additions & 0 deletions src/content_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
#include "block.h"
#include "nostrdb.h"
#include "invoice.h"

#ifndef _WIN32
#include "bolt11/bolt11.h"
#endif

#include "bolt11/bech32.h"

#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -164,6 +169,11 @@ static int push_bech32_mention(struct ndb_content_parser *p, struct ndb_str_bloc

static int push_invoice_str(struct ndb_content_parser *p, struct ndb_str_block *str)
{
#ifdef _WIN32
// we shouldn't be pushing invoices on windows until we fix
// bolt11 parser portability
return 0;
#else
unsigned char *start;
struct bolt11 *bolt11;
char *fail;
Expand All @@ -186,6 +196,7 @@ static int push_invoice_str(struct ndb_content_parser *p, struct ndb_str_block *

tal_free(bolt11);
return 1;
#endif
}

int push_block(struct ndb_content_parser *p, struct ndb_block *block);
Expand Down Expand Up @@ -455,6 +466,11 @@ static int parse_url(struct cursor *cur, struct ndb_block *block) {
static int parse_invoice(struct cursor *cur, struct ndb_block *block) {
unsigned char *start, *end;

#ifdef _WIN32
// bolt11 stuff requires non-portable cc stuff, so ignore for now
return 0;
#else

// optional
parse_str(cur, "lightning:");

Expand All @@ -478,6 +494,7 @@ static int parse_invoice(struct cursor *cur, struct ndb_block *block) {
cur->p = end;

return 1;
#endif
}


Expand Down
6 changes: 4 additions & 2 deletions src/nostr_bech32.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ int parse_nostr_bech32(unsigned char *buf, int buflen,
unsigned char *start;
size_t parsed_len, u5_out_len, u8_out_len;
enum nostr_bech32_type type;
static const int MAX_PREFIX = 8;
#define MAX_PREFIX 8
struct cursor cur, bech32, u8;

make_cursor(buf, buf + buflen, &cur);
Expand All @@ -302,7 +302,7 @@ int parse_nostr_bech32(unsigned char *buf, int buflen,
if (parsed_len < 10 || parsed_len > 10000)
return 0;

unsigned char u5[parsed_len];
unsigned char *u5 = malloc(parsed_len);
char prefix[MAX_PREFIX];

if (bech32_decode_len(prefix, u5, &u5_out_len, (const char*)start,
Expand All @@ -314,6 +314,8 @@ int parse_nostr_bech32(unsigned char *buf, int buflen,
if (!bech32_convert_bits(cur.p, &u8_out_len, 8, u5, u5_out_len, 5, 0))
return 0;

free(u5);

make_cursor(cur.p, cur.p + u8_out_len, &u8);

return parse_nostr_bech32_buffer(&u8, type, obj);
Expand Down
50 changes: 35 additions & 15 deletions src/nostrdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "cpu.h"
#include "block.h"
#include "threadpool.h"
#include "thread.h"
#include "protected_queue.h"
#include "memchr.h"
#include "print_util.h"
Expand All @@ -32,7 +33,7 @@
#define min(a,b) ((a) < (b) ? (a) : (b))

// the maximum number of things threads pop and push in bulk
static const int THREAD_QUEUE_BATCH = 4096;
#define THREAD_QUEUE_BATCH 4096

// maximum number of active subscriptions
#define MAX_SUBSCRIPTIONS 256
Expand Down Expand Up @@ -84,7 +85,6 @@ struct ndb_tag {
struct ndb_tags {
uint16_t padding;
uint16_t count;
struct ndb_tag tag[0];
};

// v1
Expand Down Expand Up @@ -364,8 +364,8 @@ static int ndb_tag_key_compare(const MDB_val *a, const MDB_val *b)
if ((cmp = mdb_cmp_memn(&va, &vb)))
return cmp;

ts_a = *(uint64_t*)(va.mv_data + va.mv_size);
ts_b = *(uint64_t*)(vb.mv_data + vb.mv_size);
ts_a = *(uint64_t*)((unsigned char *)va.mv_data + va.mv_size);
ts_b = *(uint64_t*)((unsigned char *)vb.mv_data + vb.mv_size);

if (ts_a < ts_b)
return -1;
Expand All @@ -381,8 +381,8 @@ static int ndb_text_search_key_compare(const MDB_val *a, const MDB_val *b)
uint64_t sa, sb, nid_a, nid_b;
MDB_val a2, b2;

make_cursor(a->mv_data, a->mv_data + a->mv_size, &ca);
make_cursor(b->mv_data, b->mv_data + b->mv_size, &cb);
make_cursor(a->mv_data, (unsigned char *)a->mv_data + a->mv_size, &ca);
make_cursor(b->mv_data, (unsigned char *)b->mv_data + b->mv_size, &cb);

// note_id
if (unlikely(!cursor_pull_varint(&ca, &nid_a) || !cursor_pull_varint(&cb, &nid_b)))
Expand Down Expand Up @@ -3035,7 +3035,7 @@ static int ndb_query_plan_execute_tags(struct ndb_txn *txn,
if (taglen != k.mv_size - 9)
break;

if (memcmp(k.mv_data+1, tag, k.mv_size-9))
if (memcmp((unsigned char *)k.mv_data+1, tag, k.mv_size-9))
break;

note_id = *(uint64_t*)v.mv_data;
Expand Down Expand Up @@ -3538,7 +3538,7 @@ static int ndb_text_search_next_word(MDB_cursor *cursor, MDB_cursor_op op,
int retries;
retries = -1;

make_cursor(k->mv_data, k->mv_data + k->mv_size, &key_cursor);
make_cursor(k->mv_data, (unsigned char *)k->mv_data + k->mv_size, &key_cursor);

// When op is MDB_SET_RANGE, this initializes the search. Position
// the cursor at the next key greater than or equal to the specified
Expand Down Expand Up @@ -3567,7 +3567,7 @@ static int ndb_text_search_next_word(MDB_cursor *cursor, MDB_cursor_op op,
printf("\n");
*/

make_cursor(k->mv_data, k->mv_data + k->mv_size, &key_cursor);
make_cursor(k->mv_data, (unsigned char *)k->mv_data + k->mv_size, &key_cursor);

if (unlikely(!ndb_unpack_text_search_key_noteid(&key_cursor, &result->key.note_id))) {
fprintf(stderr, "UNUSUAL: failed to unpack text search key note_id\n");
Expand Down Expand Up @@ -3980,6 +3980,7 @@ static void ndb_notify_subscriptions(struct ndb_monitor *monitor,

static void *ndb_writer_thread(void *data)
{
ndb_debug("started writer thread\n");
struct ndb_writer *writer = data;
struct ndb_writer_msg msgs[THREAD_QUEUE_BATCH], *msg;
struct written_note written_notes[THREAD_QUEUE_BATCH];
Expand All @@ -3999,6 +4000,7 @@ static void *ndb_writer_thread(void *data)
while (!done) {
txn.mdb_txn = NULL;
num_notes = 0;
ndb_debug("writer waiting for items\n");
popped = prot_queue_pop_all(&writer->inbox, msgs, THREAD_QUEUE_BATCH);
ndb_debug("writer popped %d items\n", popped);

Expand Down Expand Up @@ -4029,6 +4031,7 @@ static void *ndb_writer_thread(void *data)
switch (msg->type) {
case NDB_WRITER_QUIT:
// quits are handled before this
ndb_debug("writer thread got quit message\n");
done = 1;
continue;
case NDB_WRITER_PROFILE:
Expand Down Expand Up @@ -4098,7 +4101,7 @@ static void *ndb_writer_thread(void *data)
free(msg->note.note);
} else if (msg->type == NDB_WRITER_PROFILE) {
free(msg->profile.note.note);
ndb_profile_record_builder_free(&msg->profile.record);
//ndb_profile_record_builder_free(&msg->profile.record);
} else if (msg->type == NDB_WRITER_BLOCKS) {
ndb_blocks_free(msg->blocks.blocks);
}
Expand Down Expand Up @@ -4199,7 +4202,7 @@ static int ndb_writer_init(struct ndb_writer *writer, struct ndb_lmdb *lmdb,
writer->queue_buflen, sizeof(struct ndb_writer_msg));

// spin up the writer thread
if (pthread_create(&writer->thread_id, NULL, ndb_writer_thread, writer))
if (THREAD_CREATE(writer->thread_id, ndb_writer_thread, writer))
{
fprintf(stderr, "ndb writer thread failed to create\n");
return 0;
Expand Down Expand Up @@ -4242,14 +4245,18 @@ static int ndb_writer_destroy(struct ndb_writer *writer)

// kill thread
msg.type = NDB_WRITER_QUIT;
ndb_debug("writer: pushing quit message\n");
if (!prot_queue_push(&writer->inbox, &msg)) {
// queue is too full to push quit message. just kill it.
pthread_exit(&writer->thread_id);
ndb_debug("writer: terminating thread\n");
THREAD_TERMINATE(writer->thread_id);
} else {
pthread_join(writer->thread_id, NULL);
ndb_debug("writer: joining thread\n");
THREAD_FINISH(writer->thread_id);
}

// cleanup
ndb_debug("writer: cleaning up protected queue\n");
prot_queue_destroy(&writer->inbox);

free(writer->queue_buf);
Expand Down Expand Up @@ -4515,12 +4522,17 @@ void ndb_destroy(struct ndb *ndb)
return;

// ingester depends on writer and must be destroyed first
ndb_debug("destroying ingester\n");
ndb_ingester_destroy(&ndb->ingester);
ndb_debug("destroying writer\n");
ndb_writer_destroy(&ndb->writer);
ndb_debug("destroying monitor\n");
ndb_monitor_destroy(&ndb->monitor);

ndb_debug("closing env\n");
mdb_env_close(ndb->lmdb.env);

ndb_debug("ndb destroyed\n");
free(ndb);
}

Expand Down Expand Up @@ -4588,6 +4600,8 @@ int _ndb_process_events(struct ndb *ndb, const char *ldjson, size_t json_len, in
return 1;
}

#ifndef _WIN32
// TODO: windows
int ndb_process_events_stream(struct ndb *ndb, FILE* fp)
{
char *line = NULL;
Expand All @@ -4605,6 +4619,7 @@ int ndb_process_events_stream(struct ndb *ndb, FILE* fp)

return 1;
}
#endif

int ndb_process_client_events(struct ndb *ndb, const char *ldjson, size_t json_len)
{
Expand Down Expand Up @@ -6126,7 +6141,7 @@ int ndb_stat(struct ndb *ndb, struct ndb_stat *stat)
/// Push an element to the current tag
///
/// Basic idea is to call ndb_builder_new_tag
inline int ndb_builder_push_tag_str(struct ndb_builder *builder,
int ndb_builder_push_tag_str(struct ndb_builder *builder,
const char *str, int len)
{
union ndb_packed_str pstr;
Expand Down Expand Up @@ -6338,12 +6353,17 @@ void ndb_tags_iterate_start(struct ndb_note *note, struct ndb_iterator *iter)
iter->index = -1;
}

// Helper function to get a pointer to the nth tag
static struct ndb_tag *ndb_tags_tag(struct ndb_tags *tags, size_t index) {
return (struct ndb_tag *)((uint8_t *)tags + sizeof(struct ndb_tags) + index * sizeof(struct ndb_tag));
}

int ndb_tags_iterate_next(struct ndb_iterator *iter)
{
struct ndb_tags *tags;

if (iter->tag == NULL || iter->index == -1) {
iter->tag = iter->note->tags.tag;
iter->tag = ndb_tags_tag(&iter->note->tags, 0);
iter->index = 0;
return iter->note->tags.count != 0;
}
Expand Down
4 changes: 4 additions & 0 deletions src/nostrdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define NOSTRDB_H

#include <inttypes.h>
#include "win.h"
#include "cursor.h"

// maximum number of filters allowed in a filter group
Expand Down Expand Up @@ -455,7 +456,10 @@ int ndb_init(struct ndb **ndb, const char *dbdir, const struct ndb_config *);
int ndb_db_version(struct ndb *ndb);
int ndb_process_event(struct ndb *, const char *json, int len);
int ndb_process_events(struct ndb *, const char *ldjson, size_t len);
#ifndef _WIN32
// TODO: fix on windows
int ndb_process_events_stream(struct ndb *, FILE* fp);
#endif
int ndb_process_client_event(struct ndb *, const char *json, int len);
int ndb_process_client_events(struct ndb *, const char *json, size_t len);
int ndb_begin_query(struct ndb *, struct ndb_txn *);
Expand Down
4 changes: 2 additions & 2 deletions src/print_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ static void print_tag_kv(struct ndb_txn *txn, MDB_val *k, MDB_val *v)
struct ndb_note *note;
uint64_t ts;

ts = *(uint64_t*)(k->mv_data+(k->mv_size-8));
ts = *(uint64_t*)((uint8_t*)k->mv_data+(k->mv_size-8));

// TODO: p tags, etc
if (((const char*)k->mv_data)[0] == 'e' && k->mv_size == (1 + 32 + 8)) {
printf("note_tags 'e");
print_hex(k->mv_data+1, 32);
print_hex((uint8_t*)k->mv_data+1, 32);
printf("' %" PRIu64, ts);
} else {
printf("note_tags '%.*s' %" PRIu64, (int)k->mv_size-8,
Expand Down
Loading

0 comments on commit e03588e

Please sign in to comment.