Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/libhsk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: libhsk mainnet

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Ubuntu Install Deps
run: sudo apt-get update && sudo apt-get install -y

- name: Build
run: |
./autogen.sh
./configure --without-daemon
make
sudo make install
make test_libhsk

- name: libhsk mainnet test
run: ./test_libhsk
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ m4/lt~obsolete.m4
src/stamp-h1
test_hnsd
integration/node_modules/
test_libhsk
42 changes: 32 additions & 10 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,19 @@ AM_CPPFLAGS = -I$(top_builddir) \
-I$(top_srcdir)/uv/include \
-I$(top_srcdir)/src/secp256k1

include_HEADERS =
EXTRA_DIST = README.md \
LICENSE

bin_PROGRAMS =

noinst_PROGRAMS = test_hnsd

CLEANFILES =
include_HEADERS = src/libhsk.h

CLEANFILES = test_libhsk


# Base library
lib_LTLIBRARIES = libhsk.la

libhsk_la_LIBADD = $(top_builddir)/uv/libuv.la \
Expand All @@ -41,6 +50,7 @@ libhsk_la_SOURCES = src/addr.c \
src/hash.c \
src/header.c \
src/hesiod.c \
src/libhsk.c \
src/map.c \
src/msg.c \
src/poly1305/poly1305.c \
Expand All @@ -58,10 +68,11 @@ libhsk_la_SOURCES = src/addr.c \
src/utils.c \
src/secp256k1/secp256k1.c

EXTRA_DIST = README.md \
LICENSE

bin_PROGRAMS = hnsd
# hnsd executable daemon
if BUILD_HNSD

bin_PROGRAMS += hnsd

hnsd_SOURCES = src/cache.c \
src/daemon.c \
Expand All @@ -77,8 +88,10 @@ hnsd_LDFLAGS = -static
hnsd_CFLAGS = -DHSK_BUILD $(INC_UNBOUND) $(AM_CFLAGS)
hnsd_CPPFLAGS = $(AM_CPPFLAGS)

noinst_PROGRAMS = test_hnsd
endif


# Unit tests (any network)
test_hnsd_SOURCES = test/hnsd-test.c \
test/base32-test.c \
test/dns-test.c \
Expand All @@ -87,13 +100,22 @@ test_hnsd_SOURCES = test/hnsd-test.c \
test_hnsd_LDFLAGS = -static
test_hnsd_CPPFLAGS = $(AM_CPPFLAGS)

test_hnsd_LDADD = $(LIB_UNBOUND) \
$(top_builddir)/libhsk.la
test_hnsd_LDADD = $(top_builddir)/libhsk.la


# pkgconfigdir = $(libdir)/pkgconfig
# pkgconfig_DATA = @PACKAGE_NAME@.pc

.PHONY: e2e


# Integration tests (regtest, requires daemon)
e2e:
npm --prefix ./integration/ install
npm --prefix ./integration/ run test


# libhsk test (main)
test_libhsk:
$(CC) ./test/libhsk-test.c \
-I$(includedir) -lhsk -lpthread \
-Wl,-rpath $(libdir) \
-o test_libhsk
12 changes: 12 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ AC_ARG_WITH([asm],
[req_asm=$withval],
[req_asm=auto])

AC_ARG_WITH([daemon],
[AS_HELP_STRING([--with-daemon],
[build hnsd daemon (default=yes)])],
[build_hnsd=$withval],
[build_hnsd=yes])

AC_ARG_WITH([unbound],
[AS_HELP_STRING(
[--with-unbound=path]
Expand Down Expand Up @@ -326,6 +332,7 @@ if test x"$use_external_asm" = x"yes"; then
[Define this symbol if an external assembly implementation is used])
fi

if test x"$build_hnsd" = x"yes"; then
dnl Find libunbound, and define LIB_UNBOUND/INC_UNBOUND for hnsd.
dnl Don't add these to the default libs/includes, they're not needed by the
dnl other targets.
Expand All @@ -346,6 +353,7 @@ case $libunbound_path in
esac
AC_SUBST(LIB_UNBOUND, ${LIB_UNBOUND})
AC_SUBST(INC_UNBOUND, ${INC_UNBOUND})
fi

dnl Platform-specific libraries
case "${host_os}" in
Expand All @@ -359,15 +367,19 @@ AC_MSG_NOTICE([Using static precomputation: $use_precomp])
AC_MSG_NOTICE([Using assembly optimizations: $set_asm])
AC_MSG_NOTICE([Using field implementation: $set_field])
AC_MSG_NOTICE([Using scalar implementation: $set_scalar])

if test x"$build_hnsd" = x"yes"; then
AC_MSG_NOTICE([Linker flags for libunbound: $LIB_UNBOUND])
AC_MSG_NOTICE([Compiler flags for libunbound: $INC_UNBOUND])
fi

AM_CONDITIONAL(
[HSK_USE_ECMULT_STATIC_PRECOMPUTATION],
[test x"$use_precomp" = x"yes"])

AM_CONDITIONAL([HSK_USE_EXTERNAL_ASM], [test x"$use_external_asm" = x"yes"])
AM_CONDITIONAL([HSK_USE_ASM_ARM], [test x"$set_asm" = x"arm"])
AM_CONDITIONAL(BUILD_HNSD, [test x"$build_hnsd" = x"yes"])

dnl
dnl /Secp256k1
Expand Down
178 changes: 178 additions & 0 deletions src/libhsk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#include "config.h"

#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <getopt.h>
#include <sys/types.h>
#include <unistd.h>

#include "constants.h"
#include "hsk.h"
#include "libhsk.h"
#include "pool.h"
#include "uv.h"
#include "platform-net.h"

void
hsk_ctx_destroy(hsk_ctx_t *ctx) {
if (!ctx)
return;

if (ctx->pool) {
hsk_pool_free((hsk_pool_t *)ctx->pool);
ctx->pool = NULL;
}

if (ctx->loop) {
uv_loop_close((uv_loop_t *)ctx->loop);
ctx->loop = NULL;
}

free(ctx);
}

hsk_ctx_t *
hsk_ctx_create(int pool_size, char *user_agent, char *prefix) {
assert(pool_size && user_agent && prefix);

// Prefix must exist
if (!hsk_store_exists(prefix)) {
fprintf(stderr, "prefix path does not exist\n");
return NULL;
}

// Prefix must have enough room for filename
if (strlen(prefix) + HSK_STORE_PATH_RESERVED >= HSK_STORE_PATH_MAX) {
fprintf(stderr, "prefix path is too long\n");
return NULL;
}

// Init context
hsk_ctx_t *ctx = malloc(sizeof(hsk_ctx_t));
if (!ctx) {
fprintf(stderr, "could not initialize context\n");
return NULL;
}

// Init loop
ctx->loop = uv_default_loop();
if (!ctx->loop) {
fprintf(stderr, "could not initialize loop\n");
goto done;
}

uv_loop_t *loop = (uv_loop_t *)ctx->loop;

// Init pool
ctx->pool = hsk_pool_alloc(loop);
if(!ctx->pool) {
fprintf(stderr, "could not initialize pool\n");
goto done;
}

hsk_pool_t *pool = (hsk_pool_t *)ctx->pool;

if (!hsk_pool_set_size(pool, pool_size)){
fprintf(stderr, "could not set pool size\n");
goto done;
}

if (!hsk_pool_set_agent(pool, user_agent)){
fprintf(stderr, "could not set pool agent\n");
goto done;
}

pool->chain.prefix = prefix;

return ctx;

done:
hsk_ctx_destroy(ctx);
return NULL;
}

int
hsk_ctx_open(hsk_ctx_t *ctx) {
assert(ctx);
int rc = HSK_SUCCESS;
hsk_pool_t *pool = (hsk_pool_t *)ctx->pool;
uv_loop_t *loop = (uv_loop_t *)ctx->loop;

// Always load hard-coded checkpoint
{
uint8_t *data = (uint8_t *)HSK_CHECKPOINT;
size_t data_len = HSK_STORE_CHECKPOINT_SIZE;
if (!hsk_store_inject_checkpoint(&data, &data_len, &pool->chain)) {
fprintf(stderr, "unable to inject hard-coded checkpoint\n");
return HSK_EBADARGS;
}
}

// Maybe load the last saved runtime checkpoint from file
{
uint8_t data[HSK_STORE_CHECKPOINT_SIZE];
uint8_t *data_ptr = (uint8_t *)&data;
size_t data_len = HSK_STORE_CHECKPOINT_SIZE;
if (hsk_store_read(&data_ptr, &data_len, &pool->chain)) {
if (!hsk_store_inject_checkpoint(
&data_ptr,
&data_len,
&pool->chain
)) {
fprintf(stderr, "unable to inject checkpoint from file\n");
return HSK_EBADARGS;
} else {
// Success, checkpoint loaded
}
} else {
// Could not read file, might not exist, ignore
}
}

rc = hsk_pool_open(pool);
if (rc != HSK_SUCCESS) {
fprintf(stderr, "failed opening pool: %s\n", hsk_strerror(rc));
goto done;
}

rc = uv_run(loop, UV_RUN_DEFAULT);
if (rc != 0) {
fprintf(stderr, "failed running event loop: %s\n", uv_strerror(rc));
rc = HSK_EFAILURE;
goto done;
}

done:
hsk_ctx_destroy(ctx);
return rc;
}

void
hsk_ctx_close(hsk_ctx_t *ctx) {
hsk_pool_t *pool = (hsk_pool_t *)ctx->pool;
hsk_pool_destroy(pool);
ctx->pool = NULL;
}

float
hsk_ctx_get_sync_progress(hsk_ctx_t *ctx) {
hsk_pool_t *pool = (hsk_pool_t *)ctx->pool;
return hsk_chain_progress(&pool->chain);
}

int
hsk_ctx_resolve(
hsk_ctx_t *ctx,
const char *name,
hsk_ctx_resolve_cb callback,
const void *arg
) {
hsk_pool_t *pool = (hsk_pool_t *)ctx->pool;
return hsk_pool_resolve(pool, name, callback, arg);
}
42 changes: 42 additions & 0 deletions src/libhsk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdint.h>
#include <stdbool.h>

#include "uv.h"

typedef struct hsk_ctx_s {
void *loop; // uv_loop_t (uv.h)
void *pool; // hsk_pool_t (pool.h)
} hsk_ctx_t;

// hsk_resolve_cb (pool.h)
typedef void (*hsk_ctx_resolve_cb)(
const char *name,
int status,
bool exists,
const uint8_t *data,
size_t data_len,
const void *arg
);

void
hsk_ctx_destroy(hsk_ctx_t *ctx);

hsk_ctx_t *
hsk_ctx_create(int pool_size, char *user_agent, char *prefix);

int
hsk_ctx_open(hsk_ctx_t *ctx);

void
hsk_ctx_close(hsk_ctx_t *ctx);

float
hsk_ctx_get_sync_progress(hsk_ctx_t *ctx);

int
hsk_ctx_resolve(
hsk_ctx_t *ctx,
const char *name,
hsk_ctx_resolve_cb callback,
const void *arg
);
Loading