Skip to content

Commit

Permalink
Merge branch 'feature/websocket' of https://github.com/xrdavies/xdag-1
Browse files Browse the repository at this point in the history
…into xrdavies-feature/websocket

# Conflicts:
#	mac/xdag.xcodeproj/project.pbxproj
  • Loading branch information
jonano614 committed Jan 21, 2019
1 parent db0ec33 commit b489c37
Show file tree
Hide file tree
Showing 24 changed files with 4,510 additions and 405 deletions.
26 changes: 21 additions & 5 deletions automake/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ http = $(SRCROOT)/client/http
moving_statistics = $(SRCROOT)/client/utils/moving_statistics
algorithms = $(SRCROOT)/client/algorithms
secp256k1 = $(SRCROOT)/secp256k1

websocket = $(SRCROOT)/client/websocket

sources = \
$(algorithms)/sha256-mb-x86_64$(HOST_SUFFIX).s \
Expand Down Expand Up @@ -60,8 +60,13 @@ sources = \
$(jsonrpc)/rpc_commands.c \
$(http)/url.c \
$(http)/http.c \
$(secp256k1)/src/secp256k1.c

$(secp256k1)/src/secp256k1.c \
$(websocket)/websocket.c \
$(websocket)/wslay/wslay_event.c \
$(websocket)/wslay/wslay_frame.c \
$(websocket)/wslay/wslay_net.c \
$(websocket)/wslay/wslay_queue.c \
$(websocket)/wslay/wslay_stack.c \

headers = \
$(client)/address.h \
Expand Down Expand Up @@ -110,7 +115,16 @@ headers = \
$(jsonrpc)/rpc_wrapper.h \
$(jsonrpc)/rpc_commands.h \
$(http)/url.h \
$(http)/http.h
$(http)/http.h \
$(websocket)/websocket.h \
$(websocket)/wslay/config.h \
$(websocket)/wslay/wslay.h \
$(websocket)/wslay/wslayver.h \
$(websocket)/wslay/wslay_event.h \
$(websocket)/wslay/wslay_frame.h \
$(websocket)/wslay/wslay_net.h \
$(websocket)/wslay/wslay_queue.h \
$(websocket)/wslay/wslay_stack.h

#program
bin_PROGRAMS = xdag
Expand All @@ -133,7 +147,9 @@ _INCLUDES = \
-I$(jsonrpc) \
-I$(http) \
-I$(secp256k1)/src \
-I$(secp256k1)/
-I$(secp256k1)/ \
-I$(websocket)/wslay \
-I$(websocket)/ \


xdag_SOURCES = $(sources)
Expand Down
25 changes: 21 additions & 4 deletions client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ moving_statistics = ./utils/moving_statistics
algorithms = ./algorithms
json-rpc = ./json-rpc
secp256k1 = ../secp256k1
http = ./http
websocket = ./websocket
OS := $(shell uname)
use_openssl_ec := $(shell if grep --quiet -e 'define USE_OPTIMIZED_EC 1' -e 'define USE_OPTIMIZED_EC 2' crypt.h; then echo false; else echo true; fi)
lgmp_installed := $(shell if cc -lgmp 2>&1 | grep --quiet main; then echo true; else echo false; fi)
Expand Down Expand Up @@ -68,8 +70,14 @@ sources = \
$(json-rpc)/rpc_service.c \
$(json-rpc)/rpc_wrapper.c \
$(json-rpc)/rpc_commands.c \
./http/url.c \
./http/http.c \
$(websocket)/websocket.c \
$(websocket)/wslay/wslay_event.c \
$(websocket)/wslay/wslay_frame.c \
$(websocket)/wslay/wslay_net.c \
$(websocket)/wslay/wslay_queue.c \
$(websocket)/wslay/wslay_stack.c \
$(http)/url.c \
$(http)/http.c


headers = \
Expand Down Expand Up @@ -125,8 +133,17 @@ headers = \
$(json-rpc)/rpc_service.h \
$(json-rpc)/rpc_wrapper.h \
$(json-rpc)/rpc_commands.h \
./http/url.h \
./http/http.h \
$(websocket)/websocket.h \
$(websocket)/wslay/config.h \
$(websocket)/wslay/wslay.h \
$(websocket)/wslay/wslayver.h \
$(websocket)/wslay/wslay_event.h \
$(websocket)/wslay/wslay_frame.h \
$(websocket)/wslay/wslay_net.h \
$(websocket)/wslay/wslay_queue.h \
$(websocket)/wslay/wslay_stack.h \
$(http)/url.h \
$(http)/http.h \



Expand Down
67 changes: 67 additions & 0 deletions client/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "math.h"
#include "utils/atomic.h"
#include "utils/random.h"
#include "websocket/websocket.h"

#define MAX_WAITING_MAIN 1
#define MAIN_START_AMOUNT (1ll << 42)
Expand Down Expand Up @@ -120,6 +121,7 @@ static uint32_t cache_bounded_counter = 0;
static struct orphan_block *g_orphan_first[ORPHAN_HASH_SIZE], *g_orphan_last[ORPHAN_HASH_SIZE];

//functions
void append_block_info(struct block_internal *bi);
void cache_retarget(int32_t, int32_t);
void cache_add(struct xdag_block*, xdag_hash_t);
int32_t check_signature_out_cached(struct block_internal*, struct xdag_public_key*, const int, int32_t*, int32_t*);
Expand Down Expand Up @@ -247,6 +249,7 @@ static uint64_t apply_block(struct block_internal *bi)
accept_amount(bi, sum_in - sum_out);
bi->flags |= BI_APPLIED;

append_block_info(bi); //TODO: figure out how to detect when the block is rejected.
return bi->fee;
}

Expand Down Expand Up @@ -1540,6 +1543,70 @@ const char* xdag_get_block_state_info(uint8_t flags)
return "Pending";
}

void append_block_info(struct block_internal *bi)
{
// if websocket service is not running return directly
if(!g_websocket_running) {
return;
}

int flags, nlinks;
struct block_internal *ref, *link[MAX_LINKS];
pthread_mutex_lock(&block_mutex);
ref = bi->ref;
flags = bi->flags;
nlinks = bi->nlinks;
memcpy(link, bi->link, nlinks * sizeof(struct block_internal*));
pthread_mutex_unlock(&block_mutex);

char time_buf[64] = {0};
char address[33] = {0};
uint64_t *h = bi->hash;
xdag_hash2address(h, address);
xdag_xtime_to_string(bi->time, time_buf);

char message[4096] = {0};
char buf[128] = {0};

sprintf(message,
"{\"time\":\"%s\""
",\"flags\":\"%x\""
",\"state\":\"%s\""
",\"hash\":\"%016llx%016llx%016llx%016llx\""
",\"difficulty\":\"%llx%016llx\""
",\"remark\":\"%s\""
",\"address\":\"%s\""
",\"balance\":\"%u.%09u\""
",\"fields\":["
, time_buf
, flags & ~BI_OURS
, xdag_get_block_state_info(flags)
, (unsigned long long)h[3], (unsigned long long)h[2], (unsigned long long)h[1], (unsigned long long)h[0]
, xdag_diff_args(bi->difficulty)
, get_remark(bi)
, address
, pramount(bi->amount)
);

if((flags & BI_REF) && ref != NULL) {
xdag_hash2address(ref->hash, address);
} else {
strcpy(address, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
}
sprintf(buf, "{\"direction\":\"fee\",\"address\":\"%s\",\"amount\":\"%u.%09u\"}", address, pramount(bi->fee));
strcat(message, buf);

for (int i = 0; i < nlinks; ++i) {
xdag_hash2address(link[i]->hash, address);
sprintf(buf, ",{\"direction\":\"%s\",\"address\":\"%s\",\"amount\":\"%u.%09u\"}", (1 << i & bi->in_mask ? " input" : "output"),address, pramount(bi->linkamount[i]));
strcat(message, buf);
}

strcat(message, "]}");

xdag_ws_message_append(message);
}

/* prints detailed information about block */
int xdag_print_block_info(xdag_hash_t hash, FILE *out)
{
Expand Down
4 changes: 4 additions & 0 deletions client/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "json-rpc/rpc_service.h"
#include "../dnet/dnet_crypt.h"
#include "utils/random.h"
#include "websocket/websocket.h"

#define ARG_EQUAL(a,b,c) strcmp(c, "") == 0 ? strcmp(a, b) == 0 : (strcmp(a, b) == 0 || strcmp(a, c) == 0)

Expand Down Expand Up @@ -338,6 +339,9 @@ int setup_pool(struct startup_parameters *parameters)
if(parameters->is_rpc) {
xdag_mess("Initializing RPC service...");
if(!!xdag_rpc_service_start(parameters->rpc_port)) return -1;

xdag_mess("Initializing WebSocket service...");
if(!!xdag_ws_server_start(-1, -1)) return -1;
}
xdag_mess("Starting blocks engine...");
if(xdag_blocks_start(parameters->mining_threads_count, !!parameters->miner_address)) return -1;
Expand Down
122 changes: 122 additions & 0 deletions client/utils/base64.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//
// base64.c
// xdag
//
// Created by Rui Xie on 11/16/18.
// Copyright © 2018 xrdavies. All rights reserved.
//

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

static const uint8_t bits2mime[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const uint8_t mime2bits[256] = {
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x3e,0xff,0xff,0xff,0x3f,
0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,
0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0xff,0xff,0xff,0xff,0xff,
0xff,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
};

int encode(const uint8_t *in, size_t inlen, char *out);
int decode(const char *in, size_t inlen, uint8_t *out, size_t *outlen);

int encode(const uint8_t *in, size_t inlen, char *out)
{
while(inlen) {
*out++ = bits2mime[in[0] >> 2];
*out++ = bits2mime[(in[0] << 4 & 0x3f) | (--inlen ? in[1] >> 4 : 0)];
if (!inlen) {
*out++ = '=';
*out++ = '=';
break;
}
*out++ = bits2mime[(in[1] << 2 & 0x3f) | (--inlen ? in[2] >> 6 : 0)];
if(!inlen) {
*out++ = '=';
break;
}
*out++ = bits2mime[in[2] & 0x3f];
--inlen;

in += 3;
}
*out = '\0';

return 0;
}

int decode(const char *in, size_t inlen, uint8_t *out, size_t *outlen)
{
while(inlen) {
if(mime2bits[in[0]] == 0xFF || mime2bits[in[1]] == 0xFF) {
return -1;
}
*out++ = mime2bits[in[0]] << 2 | mime2bits[in[1]] >> 4;
(*outlen)++;
if(in[2] == '=') {
break;
}
*out++ = mime2bits[in[1]] << 4 | mime2bits[in[2]] >> 2;
(*outlen)++;

if(in[3] == '=') {
break;
}
*out++ = mime2bits[in[2]] << 6 | mime2bits[in[3]];
(*outlen)++;
in += 4;
inlen -= 4;
}

return 0;
}

int base64_encode(const uint8_t *in, size_t inlen, char **out, size_t *outlen)
{
*outlen = BASE64_LENGTH(inlen) + 1; /* extra byte for '\0' */
*out = (char *)malloc(*outlen);
memset((void*)(*out), 0, *outlen);
int ret = encode(in, inlen, *out);
if(ret != 0) {
free(*out);
*out = NULL;
return ret;
}
return ret;
}

int base64_decode(const char *in, size_t inlen, uint8_t **out, size_t *outlen)
{
if(inlen % 4) { // wrong inlen
*outlen = 0;
return -1;
}

/* This may allocate a few bytes too much, depending on input,
but it's not worth the extra CPU time to compute the exact amount.
The exact amount is 3 * inlen / 4, minus 1 if the input ends
with "=" and minus another 1 if the input ends with "==".
Dividing before multiplying avoids the possibility of overflow. */

*out = malloc(3 * (inlen / 4) + 2);
memset(*out, 0, 3 * (inlen / 4) + 2);
int ret = decode(in, inlen, *out, outlen);
if(ret != 0) {
free(*out);
*out = NULL;
}
return ret;
}
31 changes: 31 additions & 0 deletions client/utils/base64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// base64.h
// xdag
//
// Created by Rui Xie on 11/16/18.
// Copyright © 2018 xrdavies. All rights reserved.
//

#ifndef base64_h
#define base64_h

#include <stdio.h>
#include <stddef.h>

/* This uses that the expression (n+(k-1))/k means the smallest
integer >= n/k, i.e., the ceiling of n/k. */
#define BASE64_LENGTH(inlen) ((((inlen) + 2) / 3) * 4)

#ifdef __cplusplus
extern "C" {
#endif
/* encode data to base64 string, out should be freed manually */
extern int base64_encode(const uint8_t *in, size_t inlen, char **out, size_t *outlen);

/* decode base64 string to uint8_t array, out should be freed manually */
extern int base64_decode(const char *in, size_t inlen, uint8_t **out, size_t *outlen);
#ifdef __cplusplus
};
#endif

#endif /* base64_h */
Loading

0 comments on commit b489c37

Please sign in to comment.