wownero-puddle/src/pool.c

3041 lines
89 KiB
C
Raw Normal View History

2018-08-12 13:46:08 +00:00
/*
2019-05-11 03:44:28 +00:00
Copyright (c) 2014-2019, The Monero Project
2018-08-12 13:46:08 +00:00
2019-05-11 03:44:28 +00:00
All rights reserved.
2018-08-12 13:46:08 +00:00
2019-05-11 03:44:28 +00:00
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
2018-08-12 13:46:08 +00:00
2019-05-11 03:44:28 +00:00
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2018-08-12 13:46:08 +00:00
2019-05-11 03:44:28 +00:00
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
2018-08-12 13:46:08 +00:00
2019-05-11 03:44:28 +00:00
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
2018-08-12 13:46:08 +00:00
2019-05-11 03:44:28 +00:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2018-08-12 13:46:08 +00:00
2019-05-11 03:44:28 +00:00
Parts of the project are originally copyright (c) 2012-2013 The Cryptonote
developers.
2018-08-12 13:46:08 +00:00
*/
#include <netinet/in.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/http.h>
#include <lmdb.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <math.h>
#include <uuid/uuid.h>
#include <getopt.h>
2019-02-13 01:08:30 +00:00
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
2018-08-12 13:46:08 +00:00
#include <json-c/json.h>
#include <openssl/bn.h>
#include <pthread.h>
2019-05-11 05:33:26 +00:00
#include "bstack.h"
2018-08-12 13:46:08 +00:00
#include "util.h"
#include "xmr.h"
#include "log.h"
#include "webui.h"
2019-11-17 20:10:56 +00:00
#include "forkoff.h"
2018-08-12 13:46:08 +00:00
2019-07-07 19:52:59 +00:00
#define MAX_LINE 8192
2018-08-12 13:46:08 +00:00
#define POOL_CLIENTS_GROW 1024
2019-07-07 19:52:59 +00:00
#define RPC_BODY_MAX 8192
#define JOB_BODY_MAX 8192
2019-05-04 17:04:39 +00:00
#define ERROR_BODY_MAX 512
#define STATUS_BODY_MAX 256
2018-08-12 13:46:08 +00:00
#define CLIENT_JOBS_MAX 4
#define BLOCK_HEADERS_MAX 4
#define BLOCK_TEMPLATES_MAX 4
#define MAINNET_ADDRESS_PREFIX 18
#define TESTNET_ADDRESS_PREFIX 53
#define BLOCK_HEADERS_RANGE 10
2019-02-13 01:08:30 +00:00
#define DB_SIZE 0x140000000 /* 5G */
2018-08-12 13:46:08 +00:00
#define DB_COUNT_MAX 10
#define MAX_PATH 1024
#define RPC_PATH "/json_rpc"
#define ADDRESS_MAX 128
#define BLOCK_TIME 120
#define HR_BLOCK_COUNT 5
#define TEMLATE_HEIGHT_VARIANCE 5
2019-07-14 16:33:02 +00:00
#define MAX_BAD_SHARES 5
2018-08-12 13:46:08 +00:00
#define uint128_t unsigned __int128
/*
A block is initially locked.
After height + 60 blocks, we check to see if its on the chain.
If not, it becomes orphaned, otherwise we unlock it for payouts.
*/
/*
Tables:
Shares
------
height <-> share_t
Blocks
------
height <-> block_t
Balance
-------
wallet addr <-> balance
Payments
--------
wallet addr <-> payment_t
*/
enum block_status { BLOCK_LOCKED=0, BLOCK_UNLOCKED=1, BLOCK_ORPHANED=2 };
enum stratum_mode { MODE_NORMAL=0, MODE_SELF_SELECT=1 };
2018-08-12 13:46:08 +00:00
typedef struct config_t
{
char rpc_host[256];
uint32_t rpc_port;
uint32_t rpc_timeout;
char wallet_rpc_host[256];
uint32_t wallet_rpc_port;
char pool_wallet[ADDRESS_MAX];
2019-04-05 21:47:59 +00:00
uint64_t pool_start_diff;
2019-05-12 21:31:54 +00:00
double share_mul;
double pool_fee;
double payment_threshold;
2018-08-12 13:46:08 +00:00
uint32_t pool_port;
uint32_t log_level;
uint32_t webui_port;
2018-08-17 13:37:04 +00:00
char log_file[MAX_PATH];
bool block_notified;
bool disable_self_select;
2019-07-14 17:47:08 +00:00
char data_dir[MAX_PATH];
2019-11-17 20:10:56 +00:00
char pid_file[MAX_PATH];
bool forked;
2018-08-12 13:46:08 +00:00
} config_t;
typedef struct block_template_t
{
char *blockhashing_blob;
char *blocktemplate_blob;
uint64_t difficulty;
uint64_t height;
char prev_hash[64];
uint32_t reserved_offset;
2019-06-03 01:14:44 +00:00
char seed_hash[64];
char next_seed_hash[64];
2018-08-12 13:46:08 +00:00
} block_template_t;
typedef struct job_t
{
uuid_t id;
char *blob;
block_template_t *block_template;
uint32_t extra_nonce;
uint64_t target;
uint128_t *submissions;
size_t submissions_count;
block_template_t *miner_template;
2018-08-12 13:46:08 +00:00
} job_t;
typedef struct client_t
{
int fd;
int json_id;
struct bufferevent *bev;
char address[ADDRESS_MAX];
char worker_id[64];
char client_id[32];
char agent[256];
job_t active_jobs[CLIENT_JOBS_MAX];
uint64_t hashes;
time_t connected_since;
bool is_xnp;
uint32_t mode;
2019-07-14 16:33:02 +00:00
uint8_t bad_shares;
2018-08-12 13:46:08 +00:00
} client_t;
typedef struct pool_clients_t
{
client_t *clients;
size_t count;
} pool_clients_t;
2019-05-10 05:59:47 +00:00
typedef struct share_t
2018-08-12 13:46:08 +00:00
{
uint64_t height;
uint64_t difficulty;
char address[ADDRESS_MAX];
time_t timestamp;
} share_t;
2019-05-10 05:59:47 +00:00
typedef struct block_t
2018-08-12 13:46:08 +00:00
{
uint64_t height;
char hash[64];
char prev_hash[64];
uint64_t difficulty;
uint32_t status;
uint64_t reward;
time_t timestamp;
} block_t;
typedef struct payment_t
{
uint64_t amount;
time_t timestamp;
char address[ADDRESS_MAX];
2018-08-12 13:46:08 +00:00
} payment_t;
2019-05-11 05:46:36 +00:00
typedef struct rpc_callback_t rpc_callback_t;
typedef void (*rpc_callback_fun)(const char*, rpc_callback_t*);
struct rpc_callback_t
2018-08-12 13:46:08 +00:00
{
2019-05-11 05:46:36 +00:00
rpc_callback_fun f;
2018-08-12 13:46:08 +00:00
void *data;
2019-05-11 05:46:36 +00:00
};
2018-08-12 13:46:08 +00:00
static config_t config;
static pool_clients_t pool_clients;
2019-05-11 05:33:26 +00:00
static bstack_t *bst;
static bstack_t *bsh;
2018-08-12 13:46:08 +00:00
static struct event_base *base;
static struct event *listener_event;
2018-08-12 13:46:08 +00:00
static struct event *timer_120s;
static struct event *timer_10m;
static struct event *signal_usr1;
2018-08-12 13:46:08 +00:00
static uint32_t extra_nonce;
2019-06-07 20:52:15 +00:00
static uint32_t instance_id;
2018-08-12 13:46:08 +00:00
static block_t block_headers_range[BLOCK_HEADERS_RANGE];
static MDB_env *env;
static MDB_dbi db_shares;
static MDB_dbi db_blocks;
static MDB_dbi db_balance;
static MDB_dbi db_payments;
static BN_CTX *bn_ctx;
static BIGNUM *base_diff;
static pool_stats_t pool_stats;
2019-03-08 01:47:16 +00:00
static pthread_mutex_t mutex_clients = PTHREAD_MUTEX_INITIALIZER;
2018-08-17 13:37:04 +00:00
static FILE *fd_log;
static unsigned char sec_view[32];
static unsigned char pub_spend[32];
2018-08-12 13:46:08 +00:00
2019-08-21 23:35:30 +00:00
#ifdef HAVE_RX
2019-06-03 01:14:44 +00:00
extern void rx_stop_mining();
extern void rx_slow_hash_free_state();
2019-08-21 23:35:30 +00:00
#else
void rx_stop_mining(){}
void rx_slow_hash_free_state(){}
#endif
2019-06-03 01:14:44 +00:00
2019-02-11 03:56:32 +00:00
#define JSON_GET_OR_ERROR(name, parent, type, client) \
json_object *name = NULL; \
if (!json_object_object_get_ex(parent, #name, &name)) { \
send_validation_error(client, #name " not found"); \
return; \
} \
if (!json_object_is_type(name, type)) { \
send_validation_error(client, #name " not a " #type); \
return; \
}
2019-02-11 03:56:32 +00:00
2019-04-07 17:48:59 +00:00
#define JSON_GET_OR_WARN(name, parent, type) \
json_object *name = NULL; \
if (!json_object_object_get_ex(parent, #name, &name)) { \
log_warn(#name " not found"); \
} else { \
if (!json_object_is_type(name, type)) { \
log_warn(#name " not a " #type); \
} \
}
2019-05-11 05:46:36 +00:00
static inline rpc_callback_t *
rpc_callback_new(rpc_callback_fun f, void *data)
{
rpc_callback_t *c = calloc(1, sizeof(rpc_callback_t));
c->f = f;
c->data = data;
return c;
}
static inline void
rpc_callback_free(rpc_callback_t *callback)
{
if (!callback)
return;
if (callback->data)
free(callback->data);
free(callback);
}
2019-03-08 01:47:16 +00:00
static int
2018-08-12 13:46:08 +00:00
compare_uint64(const MDB_val *a, const MDB_val *b)
{
const uint64_t va = *(const uint64_t *)a->mv_data;
const uint64_t vb = *(const uint64_t *)b->mv_data;
return (va < vb) ? -1 : va > vb;
}
2019-03-08 01:47:16 +00:00
static int
2018-08-12 13:46:08 +00:00
compare_string(const MDB_val *a, const MDB_val *b)
{
const char *va = (const char*) a->mv_data;
const char *vb = (const char*) b->mv_data;
return strcmp(va, vb);
}
2019-03-08 01:47:16 +00:00
static int
2018-08-12 13:46:08 +00:00
compare_block(const MDB_val *a, const MDB_val *b)
{
const block_t *va = (const block_t*) a->mv_data;
const block_t *vb = (const block_t*) b->mv_data;
2018-08-13 03:33:34 +00:00
int sc = memcmp(va->hash, vb->hash, 64);
2018-08-12 13:46:08 +00:00
if (sc == 0)
2019-05-10 05:59:47 +00:00
return (va->timestamp < vb->timestamp) ? -1 :
va->timestamp > vb->timestamp;
2018-08-12 13:46:08 +00:00
else
return sc;
}
2019-03-08 01:47:16 +00:00
static int
2018-08-12 13:46:08 +00:00
compare_share(const MDB_val *a, const MDB_val *b)
{
const share_t *va = (const share_t*) a->mv_data;
const share_t *vb = (const share_t*) b->mv_data;
int sc = strcmp(va->address, vb->address);
if (sc == 0)
2019-05-10 05:59:47 +00:00
return (va->timestamp < vb->timestamp) ? -1 :
va->timestamp > vb->timestamp;
2018-08-12 13:46:08 +00:00
else
return sc;
}
2019-03-08 01:47:16 +00:00
static int
2018-08-12 13:46:08 +00:00
compare_payment(const MDB_val *a, const MDB_val *b)
{
const payment_t *va = (const payment_t*) a->mv_data;
const payment_t *vb = (const payment_t*) b->mv_data;
2019-05-10 05:59:47 +00:00
return (va->timestamp < vb->timestamp) ? -1 :
va->timestamp > vb->timestamp;
2018-08-12 13:46:08 +00:00
}
static int
2019-07-14 17:47:08 +00:00
database_init(const char* data_dir)
2018-08-12 13:46:08 +00:00
{
int rc;
char *err;
MDB_txn *txn;
rc = mdb_env_create(&env);
mdb_env_set_maxdbs(env, (MDB_dbi) DB_COUNT_MAX);
mdb_env_set_mapsize(env, DB_SIZE);
2019-07-14 17:47:08 +00:00
if ((rc = mdb_env_open(env, data_dir, 0, 0664)) != 0)
2018-08-12 13:46:08 +00:00
{
err = mdb_strerror(rc);
log_fatal("%s\n", err);
exit(rc);
}
if ((rc = mdb_txn_begin(env, NULL, 0, &txn)) != 0)
{
err = mdb_strerror(rc);
log_fatal("%s\n", err);
exit(rc);
}
2019-05-10 05:59:47 +00:00
uint32_t flags = MDB_INTEGERKEY | MDB_CREATE | MDB_DUPSORT | MDB_DUPFIXED;
if ((rc = mdb_dbi_open(txn, "shares", flags, &db_shares)) != 0)
2018-08-12 13:46:08 +00:00
{
err = mdb_strerror(rc);
log_fatal("%s\n", err);
exit(rc);
}
2019-05-10 05:59:47 +00:00
if ((rc = mdb_dbi_open(txn, "blocks", flags, &db_blocks)) != 0)
2018-08-12 13:46:08 +00:00
{
err = mdb_strerror(rc);
log_fatal("%s\n", err);
exit(rc);
}
2019-05-10 05:59:47 +00:00
flags = MDB_CREATE | MDB_DUPSORT | MDB_DUPFIXED;
if ((rc = mdb_dbi_open(txn, "payments", flags, &db_payments)) != 0)
2018-08-12 13:46:08 +00:00
{
err = mdb_strerror(rc);
log_fatal("%s\n", err);
exit(rc);
}
2019-05-10 05:59:47 +00:00
flags = MDB_CREATE;
if ((rc = mdb_dbi_open(txn, "balance", flags, &db_balance)) != 0)
2018-08-12 13:46:08 +00:00
{
err = mdb_strerror(rc);
log_fatal("%s\n", err);
exit(rc);
}
mdb_set_compare(txn, db_shares, compare_uint64);
mdb_set_dupsort(txn, db_shares, compare_share);
mdb_set_compare(txn, db_blocks, compare_uint64);
mdb_set_dupsort(txn, db_blocks, compare_block);
2019-05-10 05:59:47 +00:00
2018-08-12 13:46:08 +00:00
mdb_set_compare(txn, db_payments, compare_string);
mdb_set_dupsort(txn, db_payments, compare_payment);
mdb_set_compare(txn, db_balance, compare_string);
rc = mdb_txn_commit(txn);
return rc;
}
static void
2019-05-11 22:00:50 +00:00
database_close(void)
2018-08-12 13:46:08 +00:00
{
log_info("Closing database");
mdb_dbi_close(env, db_shares);
mdb_dbi_close(env, db_blocks);
mdb_dbi_close(env, db_balance);
mdb_dbi_close(env, db_payments);
mdb_env_close(env);
2018-08-12 13:46:08 +00:00
}
static int
store_share(uint64_t height, share_t *share)
{
int rc;
char *err;
MDB_txn *txn;
MDB_cursor *cursor;
if ((rc = mdb_txn_begin(env, NULL, 0, &txn)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
return rc;
}
if ((rc = mdb_cursor_open(txn, db_shares, &cursor)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
mdb_txn_abort(txn);
return rc;
}
MDB_val key = { sizeof(height), (void*)&height };
MDB_val val = { sizeof(share_t), (void*)share };
mdb_cursor_put(cursor, &key, &val, MDB_APPENDDUP);
rc = mdb_txn_commit(txn);
return rc;
}
static int
store_block(uint64_t height, block_t *block)
{
int rc;
char *err;
MDB_txn *txn;
MDB_cursor *cursor;
if ((rc = mdb_txn_begin(env, NULL, 0, &txn)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
return rc;
}
if ((rc = mdb_cursor_open(txn, db_blocks, &cursor)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
mdb_txn_abort(txn);
return rc;
}
MDB_val key = { sizeof(height), (void*)&height };
MDB_val val = { sizeof(block_t), (void*)block };
mdb_cursor_put(cursor, &key, &val, MDB_APPENDDUP);
rc = mdb_txn_commit(txn);
return rc;
}
uint64_t
miner_hr(const char *address)
{
2019-03-08 01:47:16 +00:00
pthread_mutex_lock(&mutex_clients);
2018-08-12 13:46:08 +00:00
client_t *c = pool_clients.clients;
uint64_t hr = 0;
for (size_t i = 0; i < pool_clients.count; i++, c++)
{
2019-05-10 05:59:47 +00:00
if (c->connected_since != 0
&& strncmp(c->address, address, ADDRESS_MAX) == 0)
2018-08-12 13:46:08 +00:00
{
double d = difftime(time(NULL), c->connected_since);
2018-08-13 03:33:34 +00:00
if (d == 0.0)
2019-04-07 04:20:31 +00:00
continue;
hr += c->hashes / d;
continue;
2018-08-12 13:46:08 +00:00
}
}
2019-03-08 01:47:16 +00:00
pthread_mutex_unlock(&mutex_clients);
2018-08-12 13:46:08 +00:00
return hr;
}
uint64_t
miner_balance(const char *address)
{
2018-08-13 03:33:34 +00:00
if (strlen(address) > ADDRESS_MAX)
return 0;
2018-08-12 13:46:08 +00:00
int rc;
char *err;
MDB_txn *txn;
MDB_cursor *cursor;
if ((rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
return 0;
}
if ((rc = mdb_cursor_open(txn, db_balance, &cursor)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
mdb_txn_abort(txn);
return 0;
}
MDB_val key = {ADDRESS_MAX, (void*)address};
MDB_val val;
uint64_t balance = 0;
rc = mdb_cursor_get(cursor, &key, &val, MDB_SET);
if (rc != 0 && rc != MDB_NOTFOUND)
{
err = mdb_strerror(rc);
log_error("%s", err);
goto cleanup;
}
if (rc != 0)
goto cleanup;
balance = *(uint64_t*)val.mv_data;
cleanup:
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
return balance;
}
static int
2019-05-10 05:59:47 +00:00
balance_add(const char *address, uint64_t amount, MDB_txn *parent)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
log_trace("Adding %"PRIu64" to %s's balance", amount, address);
2018-08-12 13:46:08 +00:00
int rc;
char *err;
MDB_txn *txn;
MDB_cursor *cursor;
2019-05-10 05:59:47 +00:00
if ((rc = mdb_txn_begin(env, parent, 0, &txn)) != 0)
2018-08-12 13:46:08 +00:00
{
err = mdb_strerror(rc);
log_error("%s", err);
return rc;
}
2019-05-10 05:59:47 +00:00
if ((rc = mdb_cursor_open(txn, db_balance, &cursor)) != 0)
2018-08-12 13:46:08 +00:00
{
err = mdb_strerror(rc);
log_error("%s", err);
mdb_txn_abort(txn);
return rc;
}
2019-05-10 05:59:47 +00:00
MDB_val key = {ADDRESS_MAX, (void*)address};
MDB_val val;
rc = mdb_cursor_get(cursor, &key, &val, MDB_SET);
if (rc == MDB_NOTFOUND)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
log_trace("Adding new balance entry");
MDB_val new_val = { sizeof(amount), (void*)&amount };
2019-08-21 22:32:57 +00:00
rc = mdb_cursor_put(cursor, &key, &new_val, 0);
if (rc != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
}
2019-05-10 05:59:47 +00:00
}
else if (rc == 0)
{
log_trace("Updating existing balance entry");
uint64_t current_amount = *(uint64_t*)val.mv_data;
current_amount += amount;
MDB_val new_val = {sizeof(current_amount), (void*)&current_amount};
rc = mdb_cursor_put(cursor, &key, &new_val, MDB_CURRENT);
if (rc != 0)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
err = mdb_strerror(rc);
log_error("%s", err);
2018-08-12 13:46:08 +00:00
}
}
2019-05-10 05:59:47 +00:00
else
{
err = mdb_strerror(rc);
log_error("%s", err);
mdb_txn_abort(txn);
return rc;
}
2018-08-12 13:46:08 +00:00
rc = mdb_txn_commit(txn);
return rc;
}
static int
payout_block(block_t *block, MDB_txn *parent)
{
/*
PPLNS
*/
2019-02-13 01:08:30 +00:00
log_info("Payout on block at height %"PRIu64, block->height);
2018-08-12 13:46:08 +00:00
int rc;
char *err;
MDB_txn *txn;
MDB_cursor *cursor;
uint64_t height = block->height;
uint64_t total_paid = 0;
if ((rc = mdb_txn_begin(env, parent, 0, &txn)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
return rc;
}
if ((rc = mdb_cursor_open(txn, db_shares, &cursor)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
mdb_txn_abort(txn);
return rc;
}
MDB_cursor_op op = MDB_SET;
while (1)
{
uint64_t current_height = height;
MDB_val key = { sizeof(current_height), (void*)&current_height };
MDB_val val;
rc = mdb_cursor_get(cursor, &key, &val, op);
op = MDB_NEXT_DUP;
if (rc == MDB_NOTFOUND && total_paid < block->reward)
{
if (height == 0)
break;
height--;
op = MDB_SET;
continue;
}
if (rc != 0 && rc != MDB_NOTFOUND)
{
log_error("Error getting balance: %s", mdb_strerror(rc));
break;
}
if (total_paid == block->reward)
break;
share_t *share = (share_t*)val.mv_data;
2019-05-10 05:59:47 +00:00
uint64_t amount = floor((double)share->difficulty /
((double)block->difficulty * config.share_mul) * block->reward);
2018-08-12 13:46:08 +00:00
if (total_paid + amount > block->reward)
amount = block->reward - total_paid;
total_paid += amount;
uint64_t fee = amount * config.pool_fee;
amount -= fee;
if (amount == 0)
continue;
rc = balance_add(share->address, amount, txn);
if (rc != 0)
{
2019-08-21 22:32:57 +00:00
err = mdb_strerror(rc);
log_error("%s", err);
2018-08-12 13:46:08 +00:00
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
return rc;
}
}
rc = mdb_txn_commit(txn);
return rc;
}
static int
2019-05-10 05:59:47 +00:00
process_blocks(block_t *blocks, size_t count)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
log_debug("Processing blocks");
/*
For each block, lookup block in db.
If found, make sure found is locked and not orphaned.
If both not orphaned and unlocked, payout, set unlocked.
If block heights differ / orphaned, set orphaned.
*/
2018-08-12 13:46:08 +00:00
int rc;
char *err;
MDB_txn *txn;
MDB_cursor *cursor;
2019-05-10 05:59:47 +00:00
if ((rc = mdb_txn_begin(env, NULL, 0, &txn)) != 0)
2018-08-12 13:46:08 +00:00
{
err = mdb_strerror(rc);
log_error("%s", err);
return rc;
}
2019-05-10 05:59:47 +00:00
if ((rc = mdb_cursor_open(txn, db_blocks, &cursor)) != 0)
2018-08-12 13:46:08 +00:00
{
err = mdb_strerror(rc);
log_error("%s", err);
mdb_txn_abort(txn);
return rc;
}
for (size_t i=0; i<count; i++)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
block_t *ib = &blocks[i];
log_trace("Processing block at height %"PRIu64, ib->height);
MDB_val key = { sizeof(ib->height), (void*)&ib->height };
MDB_val val;
MDB_cursor_op op = MDB_SET;
while (1)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
rc = mdb_cursor_get(cursor, &key, &val, op);
op = MDB_NEXT_DUP;
if (rc == MDB_NOTFOUND || rc != 0)
{
log_trace("No stored block at height %"PRIu64, ib->height);
if (rc != MDB_NOTFOUND)
{
err = mdb_strerror(rc);
log_debug("No stored block at height %"PRIu64
" with error: %d",
ib->height, err);
}
break;
}
block_t *sb = (block_t*)val.mv_data;
if (sb->status != BLOCK_LOCKED)
{
continue;
}
block_t nb;
memcpy(&nb, sb, sizeof(block_t));
2019-08-23 20:24:03 +00:00
if (memcmp(ib->hash, sb->hash, 64) != 0)
2019-05-10 05:59:47 +00:00
{
2019-08-24 00:33:23 +00:00
log_trace("Orphaning because hashes differ: %.64s, %.64s",
ib->hash, sb->hash);
2019-05-10 05:59:47 +00:00
log_debug("Orphaned block at height %"PRIu64, ib->height);
nb.status |= BLOCK_ORPHANED;
MDB_val new_val = {sizeof(block_t), (void*)&nb};
2019-05-10 05:59:47 +00:00
mdb_cursor_put(cursor, &key, &new_val, MDB_CURRENT);
continue;
}
2019-08-23 20:24:03 +00:00
if (memcmp(ib->prev_hash, sb->prev_hash, 64) != 0)
2019-05-10 05:59:47 +00:00
{
2019-08-23 20:24:03 +00:00
log_warn("Block with matching height and hash "
"but differing parent! "
2019-05-10 05:59:47 +00:00
"Setting orphaned.\n");
nb.status |= BLOCK_ORPHANED;
MDB_val new_val = {sizeof(block_t), (void*)&nb};
2019-05-10 05:59:47 +00:00
mdb_cursor_put(cursor, &key, &new_val, MDB_CURRENT);
continue;
}
2019-08-23 20:24:03 +00:00
if (ib->status & BLOCK_ORPHANED)
{
log_debug("Orphaned block at height %"PRIu64, ib->height);
nb.status |= BLOCK_ORPHANED;
MDB_val new_val = {sizeof(block_t), (void*)&nb};
mdb_cursor_put(cursor, &key, &new_val, MDB_CURRENT);
continue;
}
nb.status |= BLOCK_UNLOCKED;
nb.reward = ib->reward;
rc = payout_block(&nb, txn);
2019-05-10 05:59:47 +00:00
if (rc == 0)
{
log_debug("Paided out block %"PRIu64, nb.height);
MDB_val new_val = {sizeof(block_t), (void*)&nb};
2019-05-10 05:59:47 +00:00
mdb_cursor_put(cursor, &key, &new_val, MDB_CURRENT);
}
else
log_trace("%s", mdb_strerror(rc));
2018-08-12 13:46:08 +00:00
}
}
rc = mdb_txn_commit(txn);
return rc;
}
2019-05-10 05:59:47 +00:00
static void
2019-05-11 22:00:50 +00:00
update_pool_hr(void)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
uint64_t hr = 0;
client_t *c = pool_clients.clients;
for (size_t i = 0; i < pool_clients.count; i++, c++)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
if (c->connected_since != 0)
{
double d = difftime(time(NULL), c->connected_since);
if (d == 0.0)
continue;
hr += c->hashes / d;
}
2018-08-12 13:46:08 +00:00
}
2019-05-10 05:59:47 +00:00
pool_stats.pool_hashrate = hr;
}
static void
template_recycle(void *item)
{
block_template_t *bt = (block_template_t*) item;
log_trace("Recycle block template at height: %"PRIu64, bt->height);
if (bt->blockhashing_blob)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
free(bt->blockhashing_blob);
bt->blockhashing_blob = NULL;
2018-08-12 13:46:08 +00:00
}
2019-05-10 05:59:47 +00:00
if (bt->blocktemplate_blob)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
free(bt->blocktemplate_blob);
bt->blocktemplate_blob = NULL;
}
}
2018-08-12 13:46:08 +00:00
static void
retarget(client_t *client, job_t *job)
{
double duration = difftime(time(NULL), client->connected_since);
uint8_t retarget_time = client->is_xnp ? 5 : 120;
uint64_t target = fmax((double)client->hashes /
duration * retarget_time, config.pool_start_diff);
job->target = target;
log_debug("Client %.32s target now %"PRIu64, client->client_id, target);
}
2019-05-10 05:59:47 +00:00
static void
target_to_hex(uint64_t target, char *target_hex)
{
if (target & 0xFFFFFFFF00000000)
{
2019-05-10 05:59:47 +00:00
log_debug("High target requested: %"PRIu64, target);
bin_to_hex((const unsigned char*)&target, 8, &target_hex[0], 16);
2019-05-10 05:59:47 +00:00
return;
2018-08-12 13:46:08 +00:00
}
2019-05-10 05:59:47 +00:00
BIGNUM *diff = BN_new();
BIGNUM *bnt = BN_new();
#ifdef SIXTY_FOUR_BIT_LONG
BN_set_word(bnt, target);
#else
char tmp[24];
snprintf(tmp, 24, "%"PRIu64, target);
BN_dec2bn(&bnt, tmp);
2019-05-10 05:59:47 +00:00
#endif
BN_div(diff, NULL, base_diff, bnt, bn_ctx);
BN_rshift(diff, diff, 224);
uint32_t w = BN_get_word(diff);
bin_to_hex((const unsigned char*)&w, 4, &target_hex[0], 8);
2019-05-10 05:59:47 +00:00
BN_free(bnt);
BN_free(diff);
2018-08-12 13:46:08 +00:00
}
2019-05-10 05:59:47 +00:00
static void
stratum_get_proxy_job_body(char *body, const client_t *client,
const char *block_hex, bool response)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
int json_id = client->json_id;
const char *client_id = client->client_id;
const job_t *job = &client->active_jobs[0];
char job_id[33] = {0};
bin_to_hex((const unsigned char*)job->id, sizeof(uuid_t), job_id, 32);
2019-05-10 05:59:47 +00:00
uint64_t target = job->target;
char target_hex[17] = {0};
2019-05-10 05:59:47 +00:00
target_to_hex(target, &target_hex[0]);
const block_template_t *bt = job->block_template;
2018-08-12 13:46:08 +00:00
2019-05-10 05:59:47 +00:00
if (response)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
snprintf(body, JOB_BODY_MAX, "{\"id\":%d,\"jsonrpc\":\"2.0\","
"\"error\":null,\"result\""
":{\"id\":\"%.32s\",\"job\":{\"blocktemplate_blob\":\"%s\","
"\"job_id\":\"%.32s\","
"\"difficulty\":%"PRIu64",\"height\":%"PRIu64","
"\"reserved_offset\":%u,"
"\"client_nonce_offset\":%u,\"client_pool_offset\":%u,"
2019-06-03 01:14:44 +00:00
"\"target_diff\":%"PRIu64",\"target_diff_hex\":\"%s\","
"\"seed_hash\":\"%.64s\",\"next_seed_hash\":\"%.64s\"},"
2019-05-10 05:59:47 +00:00
"\"status\":\"OK\"}}\n",
json_id, client_id, block_hex, job_id,
bt->difficulty, bt->height, bt->reserved_offset,
bt->reserved_offset + 12,
2019-06-03 01:14:44 +00:00
bt->reserved_offset + 8, target, target_hex,
bt->seed_hash, bt->next_seed_hash);
2018-08-12 13:46:08 +00:00
}
2019-05-10 05:59:47 +00:00
else
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
snprintf(body, JOB_BODY_MAX, "{\"jsonrpc\":\"2.0\",\"method\":"
"\"job\",\"params\""
":{\"id\":\"%.32s\",\"job\":{\"blocktemplate_blob\":\"%s\","
"\"job_id\":\"%.32s\","
"\"difficulty\":%"PRIu64",\"height\":%"PRIu64","
"\"reserved_offset\":%u,"
"\"client_nonce_offset\":%u,\"client_pool_offset\":%u,"
2019-06-03 01:14:44 +00:00
"\"target_diff\":%"PRIu64",\"target_diff_hex\":\"%s\","
"\"seed_hash\":\"%.64s\",\"next_seed_hash\":\"%.64s\"},"
2019-05-10 05:59:47 +00:00
"\"status\":\"OK\"}}\n", client_id, block_hex, job_id,
bt->difficulty, bt->height,
bt->reserved_offset, bt->reserved_offset + 12,
2019-06-03 01:14:44 +00:00
bt->reserved_offset + 8, target, target_hex,
bt->seed_hash, bt->next_seed_hash);
2018-08-12 13:46:08 +00:00
}
}
static void
stratum_get_job_body_ss(char *body, const client_t *client, bool response)
{
/* job_id, target, pool_wallet, extra_nonce */
int json_id = client->json_id;
const char *client_id = client->client_id;
const job_t *job = &client->active_jobs[0];
char job_id[33] = {0};
bin_to_hex((const unsigned char*)job->id, sizeof(uuid_t), job_id, 32);
uint64_t target = job->target;
char target_hex[17] = {0};
target_to_hex(target, &target_hex[0]);
2019-06-03 01:14:44 +00:00
char empty[] = "";
char *seed_hash = empty;
char *next_seed_hash = empty;
if (job->miner_template)
{
seed_hash = job->miner_template->seed_hash;
next_seed_hash = job->miner_template->next_seed_hash;
}
unsigned char extra_bin[8];
memcpy(extra_bin, &job->extra_nonce, 4);
memcpy(extra_bin+4, &instance_id, 4);
char extra_hex[17] = {0};
bin_to_hex(extra_bin, 8, extra_hex, 16);
if (response)
{
snprintf(body, JOB_BODY_MAX, "{\"id\":%d,\"jsonrpc\":\"2.0\","
"\"error\":null,\"result\""
":{\"id\":\"%.32s\",\"job\":{"
"\"job_id\":\"%.32s\",\"target\":\"%s\","
2019-06-03 01:14:44 +00:00
"\"extra_nonce\":\"%s\", \"pool_wallet\":\"%s\","
"\"seed_hash\":\"%.64s\",\"next_seed_hash\":\"%.64s\"},"
"\"status\":\"OK\"}}\n",
json_id, client_id, job_id, target_hex, extra_hex,
2019-06-03 01:14:44 +00:00
config.pool_wallet, seed_hash, next_seed_hash);
}
else
{
snprintf(body, JOB_BODY_MAX, "{\"jsonrpc\":\"2.0\",\"method\":"
"\"job\",\"params\""
":{\"id\":\"%.32s\",\"job_id\":\"%.32s\","
"\"target\":\"%s\","
2019-06-03 01:14:44 +00:00
"\"extra_nonce\":\"%s\", \"pool_wallet\":\"%s\","
"\"seed_hash\":\"%.64s\",\"next_seed_hash\":\"%.64s\"}}\n",
client_id, job_id, target_hex, extra_hex, config.pool_wallet,
seed_hash, next_seed_hash);
}
}
2018-08-12 13:46:08 +00:00
static void
2019-05-10 05:59:47 +00:00
stratum_get_job_body(char *body, const client_t *client, bool response)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
int json_id = client->json_id;
const char *client_id = client->client_id;
const job_t *job = &client->active_jobs[0];
char job_id[33] = {0};
bin_to_hex((const unsigned char*)job->id, sizeof(uuid_t), job_id, 32);
2019-05-10 05:59:47 +00:00
const char *blob = job->blob;
uint64_t target = job->target;
uint64_t height = job->block_template->height;
char target_hex[17] = {0};
2019-05-10 05:59:47 +00:00
target_to_hex(target, &target_hex[0]);
2019-06-03 01:14:44 +00:00
char *seed_hash = job->block_template->seed_hash;
char *next_seed_hash = job->block_template->next_seed_hash;
2019-05-10 05:59:47 +00:00
if (response)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
snprintf(body, JOB_BODY_MAX, "{\"id\":%d,\"jsonrpc\":\"2.0\","
"\"error\":null,\"result\""
":{\"id\":\"%.32s\",\"job\":{"
"\"blob\":\"%s\",\"job_id\":\"%.32s\",\"target\":\"%s\","
2019-06-03 01:14:44 +00:00
"\"height\":%"PRIu64",\"seed_hash\":\"%.64s\","
"\"next_seed_hash\":\"%.64s\"},"
2019-05-10 05:59:47 +00:00
"\"status\":\"OK\"}}\n",
2019-06-03 01:14:44 +00:00
json_id, client_id, blob, job_id, target_hex, height,
seed_hash, next_seed_hash);
2018-08-12 13:46:08 +00:00
}
2019-05-10 05:59:47 +00:00
else
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
snprintf(body, JOB_BODY_MAX, "{\"jsonrpc\":\"2.0\",\"method\":"
"\"job\",\"params\""
":{\"id\":\"%.32s\",\"blob\":\"%s\",\"job_id\":\"%.32s\","
"\"target\":\"%s\","
2019-06-03 01:14:44 +00:00
"\"height\":%"PRIu64",\"seed_hash\":\"%.64s\","
"\"next_seed_hash\":\"%.64s\"}}\n",
client_id, blob, job_id, target_hex, height,
seed_hash, next_seed_hash);
2018-08-12 13:46:08 +00:00
}
}
2019-05-10 05:59:47 +00:00
static inline void
stratum_get_error_body(char *body, int json_id, const char *error)
{
snprintf(body, ERROR_BODY_MAX, "{\"id\":%d,\"jsonrpc\":\"2.0\","
"\"error\":"
"{\"code\":-1, \"message\":\"%s\"}}\n", json_id, error);
}
static inline void
stratum_get_status_body(char *body, int json_id, const char *status)
{
snprintf(body, STATUS_BODY_MAX, "{\"id\":%d,\"jsonrpc\":\"2.0\","
"\"error\":null,\"result\":{\"status\":\"%s\"}}\n",
json_id, status);
}
2018-08-12 13:46:08 +00:00
static void
2019-05-10 05:59:47 +00:00
send_validation_error(const client_t *client, const char *message)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
struct evbuffer *output = bufferevent_get_output(client->bev);
char body[ERROR_BODY_MAX];
stratum_get_error_body(body, client->json_id, message);
evbuffer_add(output, body, strlen(body));
log_debug("Validation error: %s", message);
2018-08-12 13:46:08 +00:00
}
static void
2019-05-10 05:59:47 +00:00
client_clear_jobs(client_t *client)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
for (size_t i=0; i<CLIENT_JOBS_MAX; i++)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
job_t *job = &client->active_jobs[i];
if (job->blob)
2019-05-10 05:59:47 +00:00
{
free(job->blob);
job->blob = NULL;
}
if (job->submissions)
2019-05-10 05:59:47 +00:00
{
free(job->submissions);
job->submissions = NULL;
job->submissions_count = 0;
}
if (job->miner_template)
{
block_template_t *bt = job->miner_template;
if (bt->blocktemplate_blob)
{
free(bt->blocktemplate_blob);
bt->blocktemplate_blob = NULL;
}
free(job->miner_template);
job->miner_template = NULL;
}
2018-08-12 13:46:08 +00:00
}
}
static job_t *
client_find_job(client_t *client, const char *job_id)
{
2019-05-06 03:13:17 +00:00
uuid_t jid;
hex_to_bin(job_id, strlen(job_id), (unsigned char*)&jid, sizeof(uuid_t));
2018-08-12 13:46:08 +00:00
for (size_t i=0; i<CLIENT_JOBS_MAX; i++)
{
job_t *job = &client->active_jobs[i];
2019-05-06 03:13:17 +00:00
if (memcmp(job->id, jid, sizeof(uuid_t)) == 0)
2018-08-12 13:46:08 +00:00
return job;
}
return NULL;
}
static void
2019-05-10 05:59:47 +00:00
client_send_job(client_t *client, bool response)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
/* First cycle jobs */
job_t *last = &client->active_jobs[CLIENT_JOBS_MAX-1];
if (last->blob)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
free(last->blob);
last->blob = NULL;
2018-08-12 13:46:08 +00:00
}
if (last->submissions)
2019-05-10 05:59:47 +00:00
{
free(last->submissions);
last->submissions = NULL;
last->submissions_count = 0;
}
if (last->miner_template)
{
block_template_t *bt = last->miner_template;
if (bt->blocktemplate_blob)
{
free(bt->blocktemplate_blob);
bt->blocktemplate_blob = NULL;
}
free(last->miner_template);
last->miner_template = NULL;
}
2019-05-10 05:59:47 +00:00
for (size_t i=CLIENT_JOBS_MAX-1; i>0; i--)
{
job_t *current = &client->active_jobs[i];
job_t *prev = &client->active_jobs[i-1];
memcpy(current, prev, sizeof(job_t));
}
job_t *job = &client->active_jobs[0];
memset(job, 0, sizeof(job_t));
2018-08-12 13:46:08 +00:00
if (client->mode == MODE_SELF_SELECT)
{
uuid_generate(job->id);
retarget(client, job);
++extra_nonce;
job->extra_nonce = extra_nonce;
char body[JOB_BODY_MAX];
stratum_get_job_body_ss(body, client, response);
log_trace("Client job: %s", body);
struct evbuffer *output = bufferevent_get_output(client->bev);
evbuffer_add(output, body, strlen(body));
return;
}
2019-05-10 05:59:47 +00:00
/* Quick check we actually have a block template */
block_template_t *bt = bstack_peek(bst);
if (!bt)
{
2019-05-10 05:59:47 +00:00
log_warn("Cannot send client a job: No block template");
2019-04-05 21:47:59 +00:00
return;
}
2018-08-12 13:46:08 +00:00
2019-05-10 05:59:47 +00:00
/*
1. Convert blocktemplate_blob to binary
2. Update bytes in reserved space at reserved_offset
3. Get block hashing blob for job
4. Send
*/
/* Convert template to blob */
2019-10-30 06:41:15 +00:00
size_t hex_size = strlen(bt->blocktemplate_blob);
size_t bin_size = hex_size >> 1;
unsigned char *block = calloc(bin_size, sizeof(char));
2019-10-30 06:41:15 +00:00
hex_to_bin(bt->blocktemplate_blob, hex_size, block, bin_size);
2019-05-10 05:59:47 +00:00
/* Set the extra nonce in our reserved space */
unsigned char *p = block;
2019-05-10 05:59:47 +00:00
p += bt->reserved_offset;
++extra_nonce;
memcpy(p, &extra_nonce, sizeof(extra_nonce));
job->extra_nonce = extra_nonce;
2019-06-07 20:52:15 +00:00
/* Add our instance ID */
p += 4;
memcpy(p, &instance_id, sizeof(instance_id));
2019-05-10 05:59:47 +00:00
/* Get hashong blob */
size_t hashing_blob_size;
unsigned char *hashing_blob = NULL;
2019-05-10 05:59:47 +00:00
get_hashing_blob(block, bin_size, &hashing_blob, &hashing_blob_size);
/* Make hex */
job->blob = calloc((hashing_blob_size << 1) +1, sizeof(char));
bin_to_hex(hashing_blob, hashing_blob_size, job->blob,
hashing_blob_size << 1);
log_trace("Miner hashing blob: %s", job->blob);
/* Save a job id */
uuid_generate(job->id);
/* Hold reference to block template */
job->block_template = bt;
/* Send */
char job_id[33] = {0};
bin_to_hex((const unsigned char*)job->id, sizeof(uuid_t), &job_id[0], 32);
2018-08-12 13:46:08 +00:00
2019-05-10 05:59:47 +00:00
/* Retarget */
retarget(client, job);
2019-05-10 05:59:47 +00:00
char body[JOB_BODY_MAX];
if (!client->is_xnp)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
stratum_get_job_body(body, client, response);
2018-08-12 13:46:08 +00:00
}
else
{
2019-10-30 06:41:15 +00:00
char *block_hex = calloc(hex_size+1, sizeof(char));
bin_to_hex(block, bin_size, block_hex, hex_size);
2019-05-10 05:59:47 +00:00
stratum_get_proxy_job_body(body, client, block_hex, response);
free(block_hex);
2018-08-12 13:46:08 +00:00
}
2019-05-10 05:59:47 +00:00
log_trace("Client job: %s", body);
struct evbuffer *output = bufferevent_get_output(client->bev);
evbuffer_add(output, body, strlen(body));
free(block);
free(hashing_blob);
2018-08-12 13:46:08 +00:00
}
2019-05-04 17:04:39 +00:00
static void
2019-05-11 22:00:50 +00:00
pool_clients_send_job(void)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
client_t *c = pool_clients.clients;
for (size_t i = 0; i < pool_clients.count; i++, c++)
2018-08-12 13:46:08 +00:00
{
2019-12-09 21:41:50 +00:00
if (c->fd == 0 || c->address[0] == 0)
2019-05-10 05:59:47 +00:00
continue;
client_send_job(c, false);
2018-08-12 13:46:08 +00:00
}
}
2019-05-10 05:59:47 +00:00
static void
2019-05-11 22:00:50 +00:00
pool_clients_init(void)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
assert(pool_clients.count == 0);
pool_clients.count = POOL_CLIENTS_GROW;
pool_clients.clients = (client_t*) calloc(pool_clients.count,
sizeof(client_t));
2018-08-12 13:46:08 +00:00
}
2019-05-10 05:59:47 +00:00
static void
2019-05-11 22:00:50 +00:00
pool_clients_free(void)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
assert(pool_clients.count != 0);
client_t *c = pool_clients.clients;
for (size_t i = 0; i < pool_clients.count; i++, c++)
{
client_clear_jobs(c);
}
free(pool_clients.clients);
2018-08-12 13:46:08 +00:00
}
static void
2019-05-10 05:59:47 +00:00
response_to_block_template(json_object *result,
block_template_t *block_template)
2018-08-12 13:46:08 +00:00
{
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(blockhashing_blob, result, json_type_string);
JSON_GET_OR_WARN(blocktemplate_blob, result, json_type_string);
JSON_GET_OR_WARN(difficulty, result, json_type_int);
JSON_GET_OR_WARN(height, result, json_type_int);
JSON_GET_OR_WARN(prev_hash, result, json_type_string);
JSON_GET_OR_WARN(reserved_offset, result, json_type_int);
2019-05-10 05:59:47 +00:00
block_template->blockhashing_blob = strdup(
json_object_get_string(blockhashing_blob));
block_template->blocktemplate_blob = strdup(
json_object_get_string(blocktemplate_blob));
2019-04-07 17:48:59 +00:00
block_template->difficulty = json_object_get_int64(difficulty);
block_template->height = json_object_get_int64(height);
2019-11-28 00:46:23 +00:00
strncpy(block_template->prev_hash, json_object_get_string(prev_hash), 64);
2019-04-07 17:48:59 +00:00
block_template->reserved_offset = json_object_get_int(reserved_offset);
2019-06-03 01:14:44 +00:00
unsigned int major_version = 0;
sscanf(block_template->blocktemplate_blob, "%2x", &major_version);
uint8_t pow_variant = major_version >= 7 ? major_version - 6 : 0;
log_trace("Variant: %u", pow_variant);
if (pow_variant >= 6)
{
JSON_GET_OR_WARN(seed_hash, result, json_type_string);
JSON_GET_OR_WARN(next_seed_hash, result, json_type_string);
assert(seed_hash != NULL);
assert(next_seed_hash != NULL);
2019-11-28 00:46:23 +00:00
strncpy(block_template->seed_hash,
2019-06-03 01:14:44 +00:00
json_object_get_string(seed_hash), 64);
2019-11-28 00:46:23 +00:00
strncpy(block_template->next_seed_hash,
2019-06-03 01:14:44 +00:00
json_object_get_string(next_seed_hash), 64);
}
2018-08-12 13:46:08 +00:00
}
static void
2019-04-07 17:48:59 +00:00
response_to_block(json_object *block_header, block_t *block)
2018-08-12 13:46:08 +00:00
{
memset(block, 0, sizeof(block_t));
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(height, block_header, json_type_int);
JSON_GET_OR_WARN(difficulty, block_header, json_type_int);
JSON_GET_OR_WARN(hash, block_header, json_type_string);
JSON_GET_OR_WARN(prev_hash, block_header, json_type_string);
JSON_GET_OR_WARN(timestamp, block_header, json_type_int);
JSON_GET_OR_WARN(reward, block_header, json_type_int);
JSON_GET_OR_WARN(orphan_status, block_header, json_type_boolean);
block->height = json_object_get_int64(height);
block->difficulty = json_object_get_int64(difficulty);
2019-11-28 00:46:23 +00:00
strncpy(block->hash, json_object_get_string(hash), 64);
strncpy(block->prev_hash, json_object_get_string(prev_hash), 64);
2019-04-07 17:48:59 +00:00
block->timestamp = json_object_get_int64(timestamp);
block->reward = json_object_get_int64(reward);
if (json_object_get_int(orphan_status))
2018-08-12 13:46:08 +00:00
block->status |= BLOCK_ORPHANED;
}
static void
rpc_on_response(struct evhttp_request *req, void *arg)
{
struct evbuffer *input;
rpc_callback_t *callback = (rpc_callback_t*) arg;
if (!req)
{
log_error("Request failure. Aborting.");
2019-05-11 05:46:36 +00:00
rpc_callback_free(callback);
return;
2018-08-12 13:46:08 +00:00
}
int rc = evhttp_request_get_response_code(req);
if (rc < 200 || rc >= 300)
{
2019-05-11 05:46:36 +00:00
log_error("HTTP status code %d for %s. Aborting.",
rc, evhttp_request_get_uri(req));
rpc_callback_free(callback);
return;
2018-08-12 13:46:08 +00:00
}
input = evhttp_request_get_input_buffer(req);
size_t len = evbuffer_get_length(input);
2019-05-11 05:46:36 +00:00
char body[len+1];
evbuffer_remove(input, body, len);
body[len] = '\0';
callback->f(body, callback);
rpc_callback_free(callback);
2018-08-12 13:46:08 +00:00
}
static void
rpc_request(struct event_base *base, const char *body, rpc_callback_t *callback)
{
struct evhttp_connection *con;
struct evhttp_request *req;
struct evkeyvalq *headers;
struct evbuffer *output;
2019-05-10 05:59:47 +00:00
con = evhttp_connection_base_new(base, NULL,
config.rpc_host, config.rpc_port);
2019-05-04 17:04:39 +00:00
evhttp_connection_free_on_completion(con);
2018-08-12 13:46:08 +00:00
evhttp_connection_set_timeout(con, config.rpc_timeout);
req = evhttp_request_new(rpc_on_response, callback);
output = evhttp_request_get_output_buffer(req);
evbuffer_add(output, body, strlen(body));
headers = evhttp_request_get_output_headers(req);
evhttp_add_header(headers, "Content-Type", "application/json");
evhttp_add_header(headers, "Connection", "close");
evhttp_make_request(con, req, EVHTTP_REQ_POST, RPC_PATH);
}
static void
2019-05-10 05:59:47 +00:00
rpc_wallet_request(struct event_base *base, const char *body,
rpc_callback_t *callback)
2018-08-12 13:46:08 +00:00
{
struct evhttp_connection *con;
struct evhttp_request *req;
struct evkeyvalq *headers;
struct evbuffer *output;
2019-05-10 05:59:47 +00:00
con = evhttp_connection_base_new(base, NULL,
config.wallet_rpc_host, config.wallet_rpc_port);
2019-05-04 17:04:39 +00:00
evhttp_connection_free_on_completion(con);
2018-08-12 13:46:08 +00:00
evhttp_connection_set_timeout(con, config.rpc_timeout);
req = evhttp_request_new(rpc_on_response, callback);
output = evhttp_request_get_output_buffer(req);
evbuffer_add(output, body, strlen(body));
headers = evhttp_request_get_output_headers(req);
evhttp_add_header(headers, "Content-Type", "application/json");
evhttp_add_header(headers, "Connection", "close");
evhttp_make_request(con, req, EVHTTP_REQ_POST, RPC_PATH);
}
static void
rpc_get_request_body(char *body, const char *method, char *fmt, ...)
2019-05-10 05:59:47 +00:00
{
char *pb = body;
char *end = body + RPC_BODY_MAX;
2019-05-10 05:59:47 +00:00
pb = stecpy(pb, "{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"", end);
pb = stecpy(pb, method, end);
pb = stecpy(pb, "\"", end);
2019-05-10 05:59:47 +00:00
if (fmt && *fmt)
{
char *s;
uint64_t d;
pb = stecpy(pb, ",\"params\":{", end);
2019-05-10 05:59:47 +00:00
va_list args;
va_start(args, fmt);
uint8_t count = 0;
while (*fmt)
{
switch (*fmt++)
{
case 's':
s = va_arg(args, char *);
pb = stecpy(pb, "\"", end);
pb = stecpy(pb, s, end);
pb = stecpy(pb, "\"", end);
2019-05-10 05:59:47 +00:00
break;
case 'd':
d = va_arg(args, uint64_t);
char tmp[24];
snprintf(tmp, 24, "%"PRIu64, d);
pb = stecpy(pb, tmp, end);
2019-05-10 05:59:47 +00:00
break;
}
*pb++ = count++ % 2 ? ',' : ':';
}
va_end(args);
*--pb = '}';
pb++;
}
*pb++ = '}';
*pb = '\0';
log_trace("Payload: %s", body);
}
static void
rpc_on_block_header_by_height(const char* data, rpc_callback_t *callback)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
log_trace("Got block header by height: \n%s", data);
2018-08-12 13:46:08 +00:00
json_object *root = json_tokener_parse(data);
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(result, root, json_type_object);
JSON_GET_OR_WARN(status, result, json_type_string);
const char *ss = json_object_get_string(status);
json_object *error = NULL;
json_object_object_get_ex(root, "error", &error);
if (error)
2018-08-12 13:46:08 +00:00
{
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(code, error, json_type_object);
JSON_GET_OR_WARN(message, error, json_type_string);
int ec = json_object_get_int(code);
const char *em = json_object_get_string(message);
2019-05-10 05:59:47 +00:00
log_error("Error (%d) getting block header by height: %s", ec, em);
2018-08-12 13:46:08 +00:00
json_object_put(root);
return;
}
if (!status || strcmp(ss, "OK") != 0)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
log_error("Error getting block header by height: %s", ss);
2018-08-12 13:46:08 +00:00
json_object_put(root);
return;
}
2019-05-10 05:59:47 +00:00
block_t rb;
JSON_GET_OR_WARN(block_header, result, json_type_object);
response_to_block(block_header, &rb);
process_blocks(&rb, 1);
2018-08-12 13:46:08 +00:00
json_object_put(root);
}
static void
2019-05-10 05:59:47 +00:00
rpc_on_block_headers_range(const char* data, rpc_callback_t *callback)
2018-08-12 13:46:08 +00:00
{
json_object *root = json_tokener_parse(data);
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(result, root, json_type_object);
JSON_GET_OR_WARN(status, result, json_type_string);
const char *ss = json_object_get_string(status);
json_object *error = NULL;
json_object_object_get_ex(root, "error", &error);
if (error)
2018-08-12 13:46:08 +00:00
{
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(code, error, json_type_object);
JSON_GET_OR_WARN(message, error, json_type_string);
int ec = json_object_get_int(code);
const char *em = json_object_get_string(message);
2019-05-10 05:59:47 +00:00
log_warn("Error (%d) getting block headers by range: %s", ec, em);
2018-08-12 13:46:08 +00:00
json_object_put(root);
return;
}
if (!status || strcmp(ss, "OK") != 0)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
log_warn("Error getting block headers by range: %s", ss);
2018-08-12 13:46:08 +00:00
json_object_put(root);
return;
}
2019-05-10 05:59:47 +00:00
JSON_GET_OR_WARN(headers, result, json_type_array);
size_t headers_len = json_object_array_length(headers);
assert(headers_len == BLOCK_HEADERS_RANGE);
for (size_t i=0; i<headers_len; i++)
2019-05-10 05:59:47 +00:00
{
json_object *header = json_object_array_get_idx(headers, i);
block_t *bh = &block_headers_range[i];
response_to_block(header, bh);
}
process_blocks(block_headers_range, BLOCK_HEADERS_RANGE);
2018-08-12 13:46:08 +00:00
json_object_put(root);
}
static void
rpc_on_block_template(const char* data, rpc_callback_t *callback)
{
log_trace("Got block template: \n%s", data);
json_object *root = json_tokener_parse(data);
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(result, root, json_type_object);
JSON_GET_OR_WARN(status, result, json_type_string);
const char *ss = json_object_get_string(status);
json_object *error = NULL;
json_object_object_get_ex(root, "error", &error);
if (error)
2018-08-12 13:46:08 +00:00
{
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(code, error, json_type_object);
JSON_GET_OR_WARN(message, error, json_type_string);
int ec = json_object_get_int(code);
const char *em = json_object_get_string(message);
2018-08-12 13:46:08 +00:00
log_error("Error (%d) getting block template: %s", ec, em);
json_object_put(root);
return;
}
if (!status || strcmp(ss, "OK") != 0)
2018-08-12 13:46:08 +00:00
{
2019-04-07 17:48:59 +00:00
log_error("Error getting block template: %s", ss);
2018-08-12 13:46:08 +00:00
json_object_put(root);
return;
}
pool_stats.last_template_fetched = time(NULL);
2019-05-10 05:59:47 +00:00
block_template_t *front = (block_template_t*) bstack_push(bst, NULL);
response_to_block_template(result, front);
pool_clients_send_job();
json_object_put(root);
}
static int
startup_pauout(uint64_t height)
{
/*
Loop stored blocks < height - 60
If block locked & not orphaned, payout
*/
int rc;
char *err;
MDB_txn *txn;
MDB_cursor *cursor;
if ((rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
return rc;
}
if ((rc = mdb_cursor_open(txn, db_blocks, &cursor)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
mdb_txn_abort(txn);
return rc;
}
pool_stats.pool_blocks_found = 0;
MDB_cursor_op op = MDB_FIRST;
while (1)
{
MDB_val key;
MDB_val val;
rc = mdb_cursor_get(cursor, &key, &val, op);
op = MDB_NEXT;
if (rc != 0 && rc != MDB_NOTFOUND)
{
err = mdb_strerror(rc);
log_error("%s", err);
break;
}
if (rc == MDB_NOTFOUND)
break;
pool_stats.pool_blocks_found++;
block_t *block = (block_t*)val.mv_data;
pool_stats.last_block_found = block->timestamp;
if (block->height > height - 60)
continue;
if (block->status != BLOCK_LOCKED)
2019-05-10 05:59:47 +00:00
continue;
char body[RPC_BODY_MAX];
rpc_get_request_body(body, "get_block_header_by_height", "sd",
"height", block->height);
rpc_callback_t *cb = rpc_callback_new(
rpc_on_block_header_by_height, NULL);
rpc_request(base, body, cb);
}
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
return 0;
2018-08-12 13:46:08 +00:00
}
static void
rpc_on_view_key(const char* data, rpc_callback_t *callback)
{
json_object *root = json_tokener_parse(data);
JSON_GET_OR_WARN(result, root, json_type_object);
JSON_GET_OR_WARN(key, result, json_type_string);
json_object *error = NULL;
json_object_object_get_ex(root, "error", &error);
if (error)
{
JSON_GET_OR_WARN(code, error, json_type_object);
JSON_GET_OR_WARN(message, error, json_type_string);
int ec = json_object_get_int(code);
const char *em = json_object_get_string(message);
log_error("Error (%d) getting key: %s", ec, em);
json_object_put(root);
return;
}
const char *vk = json_object_get_string(key);
hex_to_bin(vk, strlen(vk), &sec_view[0], 32);
json_object_put(root);
uint64_t prefix;
parse_address(config.pool_wallet, &prefix, &pub_spend[0]);
}
2018-08-12 13:46:08 +00:00
static void
rpc_on_last_block_header(const char* data, rpc_callback_t *callback)
{
log_trace("Got last block header: \n%s", data);
json_object *root = json_tokener_parse(data);
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(result, root, json_type_object);
JSON_GET_OR_WARN(status, result, json_type_string);
const char *ss = json_object_get_string(status);
json_object *error = NULL;
json_object_object_get_ex(root, "error", &error);
if (error)
2018-08-12 13:46:08 +00:00
{
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(code, error, json_type_object);
JSON_GET_OR_WARN(message, error, json_type_string);
int ec = json_object_get_int(code);
const char *em = json_object_get_string(message);
2018-08-12 13:46:08 +00:00
log_error("Error (%d) getting last block header: %s", ec, em);
json_object_put(root);
return;
}
if (!status || strcmp(ss, "OK") != 0)
2018-08-12 13:46:08 +00:00
{
2019-04-07 17:48:59 +00:00
log_error("Error getting last block header: %s", ss);
2018-08-12 13:46:08 +00:00
json_object_put(root);
return;
}
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(block_header, result, json_type_object);
JSON_GET_OR_WARN(height, block_header, json_type_int);
uint64_t bh = json_object_get_int64(height);
2018-08-12 13:46:08 +00:00
bool need_new_template = false;
block_t *front = bstack_peek(bsh);
if (front && bh > front->height)
2018-08-12 13:46:08 +00:00
{
need_new_template = true;
block_t *block = bstack_push(bsh, NULL);
response_to_block(block_header, block);
2018-08-12 13:46:08 +00:00
}
else if (!front)
2018-08-12 13:46:08 +00:00
{
block_t *block = bstack_push(bsh, NULL);
response_to_block(block_header, block);
2019-05-11 05:33:26 +00:00
startup_pauout(block->height);
2018-08-12 13:46:08 +00:00
need_new_template = true;
}
2019-05-11 05:33:26 +00:00
front = bstack_peek(bsh);
pool_stats.network_difficulty = front->difficulty;
pool_stats.network_hashrate = front->difficulty / BLOCK_TIME;
pool_stats.network_height = front->height;
2019-03-08 01:47:16 +00:00
update_pool_hr();
2018-08-12 13:46:08 +00:00
if (need_new_template)
{
log_info("Fetching new block template");
2019-05-04 17:04:39 +00:00
char body[RPC_BODY_MAX];
uint64_t reserve = 17;
2019-05-10 05:59:47 +00:00
rpc_get_request_body(body, "get_block_template", "sssd",
"wallet_address", config.pool_wallet, "reserve_size", reserve);
2019-05-11 05:46:36 +00:00
rpc_callback_t *cb1 = rpc_callback_new(rpc_on_block_template, NULL);
rpc_request(base, body, cb1);
2018-08-12 13:46:08 +00:00
uint64_t end = front->height - 60;
2019-05-04 17:04:39 +00:00
uint64_t start = end - BLOCK_HEADERS_RANGE + 1;
2019-05-10 05:59:47 +00:00
rpc_get_request_body(body, "get_block_headers_range", "sdsd",
"start_height", start, "end_height", end);
2019-05-11 05:46:36 +00:00
rpc_callback_t *cb2 = rpc_callback_new(
rpc_on_block_headers_range, NULL);
rpc_request(base, body, cb2);
2018-08-12 13:46:08 +00:00
}
json_object_put(root);
}
static void
rpc_on_block_submitted(const char* data, rpc_callback_t *callback)
{
json_object *root = json_tokener_parse(data);
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(result, root, json_type_object);
JSON_GET_OR_WARN(status, result, json_type_string);
const char *ss = json_object_get_string(status);
json_object *error = NULL;
json_object_object_get_ex(root, "error", &error);
2018-08-12 13:46:08 +00:00
/*
The RPC reports submission as an error even when it's added as
an alternative block. Thus, still store it. This doesn't matter
as upon payout, blocks are checked whether they are orphaned or not.
*/
if (error)
2018-08-12 13:46:08 +00:00
{
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(code, error, json_type_object);
JSON_GET_OR_WARN(message, error, json_type_string);
int ec = json_object_get_int(code);
const char *em = json_object_get_string(message);
log_warn("Error (%d) with block submission: %s", ec, em);
2018-08-12 13:46:08 +00:00
}
if (!status || strcmp(ss, "OK") != 0)
2018-08-12 13:46:08 +00:00
{
log_warn("Error submitting block: %s", ss);
2018-08-12 13:46:08 +00:00
}
pool_stats.pool_blocks_found++;
block_t *b = (block_t*)callback->data;
pool_stats.last_block_found = b->timestamp;
2019-02-13 01:08:30 +00:00
log_info("Block submitted at height: %"PRIu64, b->height);
2018-08-12 13:46:08 +00:00
int rc = store_block(b->height, b);
if (rc != 0)
log_warn("Failed to store block: %s", mdb_strerror(rc));
json_object_put(root);
}
static void
rpc_on_wallet_transferred(const char* data, rpc_callback_t *callback)
{
log_trace("Transfer response: \n%s", data);
json_object *root = json_tokener_parse(data);
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(result, root, json_type_object);
json_object *error = NULL;
json_object_object_get_ex(root, "error", &error);
if (error)
2018-08-12 13:46:08 +00:00
{
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(code, error, json_type_object);
JSON_GET_OR_WARN(message, error, json_type_string);
int ec = json_object_get_int(code);
const char *em = json_object_get_string(message);
2018-08-12 13:46:08 +00:00
log_error("Error (%d) with wallet transfer: %s", ec, em);
}
2019-08-23 20:53:29 +00:00
else
log_info("Payout transfer successfull");
2018-08-12 13:46:08 +00:00
int rc;
char *err;
MDB_txn *txn;
MDB_cursor *cursor;
/* First, updated balance(s) */
2018-08-12 13:46:08 +00:00
if ((rc = mdb_txn_begin(env, NULL, 0, &txn)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
goto cleanup;
}
if ((rc = mdb_cursor_open(txn, db_balance, &cursor)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
mdb_txn_abort(txn);
goto cleanup;
}
payment_t *payment = (payment_t*) callback->data;
for (; payment->amount; payment++)
2018-08-12 13:46:08 +00:00
{
MDB_cursor_op op = MDB_SET;
MDB_val key = {ADDRESS_MAX, (void*)payment->address};
MDB_val val;
rc = mdb_cursor_get(cursor, &key, &val, op);
if (rc == MDB_NOTFOUND)
{
log_error("Payment made to non-existent address");
continue;
}
else if (rc != 0 && rc != MDB_NOTFOUND)
{
err = mdb_strerror(rc);
log_error("%s", err);
continue;
}
uint64_t current_amount = *(uint64_t*)val.mv_data;
current_amount -= payment->amount;
2019-08-23 20:53:29 +00:00
if (error)
{
log_warn("Error seen on transfer for %s with amount %"PRIu64,
payment->address, payment->amount);
}
MDB_val new_val = {sizeof(current_amount), (void*)&current_amount};
rc = mdb_cursor_put(cursor, &key, &new_val, MDB_CURRENT);
if (rc != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
}
2018-08-12 13:46:08 +00:00
}
if ((rc = mdb_txn_commit(txn)) != 0)
2018-08-12 13:46:08 +00:00
{
err = mdb_strerror(rc);
log_error("Error committing updated balance(s): %s", err);
2018-08-12 13:46:08 +00:00
mdb_txn_abort(txn);
goto cleanup;
}
/* Now store payment info */
if ((rc = mdb_txn_begin(env, NULL, 0, &txn)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
goto cleanup;
}
if ((rc = mdb_cursor_open(txn, db_payments, &cursor)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
mdb_txn_abort(txn);
goto cleanup;
}
time_t now = time(NULL);
payment = (payment_t*) callback->data;
for (; payment->amount; payment++)
2018-08-12 13:46:08 +00:00
{
payment->timestamp = now;
MDB_val key = {ADDRESS_MAX, (void*)payment->address};
MDB_val val = {sizeof(payment_t), payment};
if ((rc = mdb_cursor_put(cursor, &key, &val, MDB_APPENDDUP)) != 0)
{
err = mdb_strerror(rc);
log_error("Error putting payment: %s", err);
continue;
}
2018-08-12 13:46:08 +00:00
}
if ((rc = mdb_txn_commit(txn)) != 0)
{
err = mdb_strerror(rc);
log_error("Error committing payment: %s", err);
mdb_txn_abort(txn);
goto cleanup;
}
2018-08-12 13:46:08 +00:00
cleanup:
json_object_put(root);
}
2019-05-10 05:59:47 +00:00
static int
2019-05-11 22:00:50 +00:00
send_payments(void)
2019-05-10 05:59:47 +00:00
{
uint64_t threshold = 1000000000000 * config.payment_threshold;
int rc;
char *err;
MDB_txn *txn;
MDB_cursor *cursor;
if ((rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
return rc;
}
if ((rc = mdb_cursor_open(txn, db_balance, &cursor)) != 0)
{
err = mdb_strerror(rc);
log_error("%s", err);
mdb_txn_abort(txn);
return rc;
}
size_t payments_count = 0;
size_t payments_max_count = 25;
size_t payments_size = payments_max_count * sizeof(payment_t);
payment_t *payments = (payment_t*) calloc(1, payments_size);
payment_t *payment = payments;
payment_t *end_payment = payment + payments_max_count;
MDB_cursor_op op = MDB_FIRST;
while (1)
{
MDB_val key;
MDB_val val;
rc = mdb_cursor_get(cursor, &key, &val, op);
op = MDB_NEXT;
if (rc != 0)
break;
const char *address = (const char*)key.mv_data;
uint64_t amount = *(uint64_t*)val.mv_data;
if (amount < threshold)
continue;
log_info("Sending payment of %"PRIu64" to %s\n", amount, address);
2019-11-28 00:46:23 +00:00
strncpy(payment->address, address, ADDRESS_MAX);
2019-05-10 05:59:47 +00:00
payment->amount = amount;
payments_count++;
if (++payment == end_payment)
{
payments_size <<= 1;
payments = (payment_t*) realloc(payments, payments_size);
payment = payments + payments_max_count;
memset(payment, 0, sizeof(payment_t) * payments_max_count);
payments_max_count <<= 1;
end_payment = payments + payments_max_count;
}
}
mdb_cursor_close(cursor);
mdb_txn_abort(txn);
if (payments_count)
{
size_t body_size = 160 * payments_count + 128;
char body[body_size];
char *start = body;
char *end = body + body_size;
start = stecpy(start, "{\"id\":\"0\",\"jsonrpc\":\"2.0\",\"method\":"
"\"transfer_split\",\"params\":{"
"\"ring_size\":11,\"destinations\":[", end);
for (size_t i=0; i<payments_count; i++)
{
payment_t *p = &payments[i];
start = stecpy(start, "{\"address\":\"", end);
start = stecpy(start, p->address, end);
start = stecpy(start, "\",\"amount\":", end);
sprintf(start, "%"PRIu64"}", p->amount);
start = body + strlen(body);
if (i != payments_count -1)
start = stecpy(start, ",", end);
else
start = stecpy(start, "]}}", end);
}
log_trace(body);
rpc_callback_t *cb = rpc_callback_new(
rpc_on_wallet_transferred, payments);
rpc_wallet_request(base, body, cb);
}
else
free(payments);
return 0;
}
static void
fetch_view_key(void)
{
char body[RPC_BODY_MAX];
rpc_get_request_body(body, "query_key", "ss", "key_type", "view_key");
rpc_callback_t *cb = rpc_callback_new(rpc_on_view_key, NULL);
rpc_wallet_request(base, body, cb);
}
2018-08-12 13:46:08 +00:00
static void
2019-05-11 22:00:50 +00:00
fetch_last_block_header(void)
2018-08-12 13:46:08 +00:00
{
log_info("Fetching last block header");
2019-05-04 17:04:39 +00:00
char body[RPC_BODY_MAX];
rpc_get_request_body(body, "get_last_block_header", NULL);
2019-05-11 05:46:36 +00:00
rpc_callback_t *cb = rpc_callback_new(rpc_on_last_block_header, NULL);
rpc_request(base, body, cb);
}
static void
timer_on_120s(int fd, short kind, void *ctx)
{
log_trace("Fetching last block header from timer");
fetch_last_block_header();
2018-08-12 13:46:08 +00:00
struct timeval timeout = { .tv_sec = 120, .tv_usec = 0 };
evtimer_add(timer_120s, &timeout);
}
static void
timer_on_10m(int fd, short kind, void *ctx)
{
send_payments();
struct timeval timeout = { .tv_sec = 600, .tv_usec = 0 };
evtimer_add(timer_10m, &timeout);
}
static void
client_add(int fd, struct bufferevent *bev)
{
log_info("New client connected");
client_t *c = pool_clients.clients;
bool resize = true;
for (size_t i = 0; i < pool_clients.count; i++, c++)
{
2019-03-08 01:47:16 +00:00
if (c->connected_since == 0)
2018-08-12 13:46:08 +00:00
{
resize = false;
break;
}
}
if (resize)
{
2019-03-08 01:47:16 +00:00
pthread_mutex_lock(&mutex_clients);
2018-08-12 13:46:08 +00:00
pool_clients.count += POOL_CLIENTS_GROW;
2019-05-10 05:59:47 +00:00
c = realloc(pool_clients.clients, sizeof(client_t) *
pool_clients.count);
2018-08-12 13:46:08 +00:00
pool_clients.clients = c;
c += pool_clients.count - POOL_CLIENTS_GROW;
2019-12-22 20:27:33 +00:00
memset(c, 0, sizeof(client_t) * POOL_CLIENTS_GROW);
2019-03-08 01:47:16 +00:00
pthread_mutex_unlock(&mutex_clients);
2018-08-12 13:46:08 +00:00
log_debug("Client pool can now hold %zu clients", pool_clients.count);
}
2019-12-22 20:27:33 +00:00
else
memset(c, 0, sizeof(client_t));
2018-08-12 13:46:08 +00:00
c->fd = fd;
c->bev = bev;
c->connected_since = time(NULL);
pool_stats.connected_miners++;
}
static void
client_find(struct bufferevent *bev, client_t **client)
{
int fd = bufferevent_getfd(bev);
2019-12-09 21:41:50 +00:00
if (fd < 0)
{
*client = NULL;
return;
}
2018-08-12 13:46:08 +00:00
client_t *c = pool_clients.clients;
for (size_t i = 0; i < pool_clients.count; i++, c++)
{
if (c->fd == fd)
{
*client = c;
return;
}
}
*client = NULL;
}
static void
client_clear(struct bufferevent *bev)
{
client_t *client = NULL;
2018-08-12 13:46:08 +00:00
client_find(bev, &client);
if (!client)
2018-08-12 13:46:08 +00:00
return;
client_clear_jobs(client);
memset(client, 0, sizeof(client_t));
bufferevent_free(bev);
pool_stats.connected_miners--;
}
static void
client_on_login(json_object *message, client_t *client)
{
2019-02-11 03:56:32 +00:00
JSON_GET_OR_ERROR(params, message, json_type_object, client);
JSON_GET_OR_ERROR(login, params, json_type_string, client);
JSON_GET_OR_ERROR(pass, params, json_type_string, client);
client->mode = MODE_NORMAL;
json_object *mode = NULL;
if (json_object_object_get_ex(params, "mode", &mode))
{
if (!json_object_is_type(mode, json_type_string))
log_warn("mode not a json_type_string");
else
{
const char *modestr = json_object_get_string(mode);
if (strcmp(modestr, "self-select") == 0)
{
if (config.disable_self_select)
{
send_validation_error(client,
"pool disabled self-select");
return;
}
client->mode = MODE_SELF_SELECT;
log_trace("Client login for mode: self-select");
}
}
}
2019-02-11 03:56:32 +00:00
2019-02-09 19:23:51 +00:00
const char *address = json_object_get_string(login);
2018-08-12 13:46:08 +00:00
uint64_t prefix;
parse_address(address, &prefix, NULL);
2018-08-12 13:46:08 +00:00
if (prefix != MAINNET_ADDRESS_PREFIX && prefix != TESTNET_ADDRESS_PREFIX)
{
send_validation_error(client,
2019-05-10 05:59:47 +00:00
"login only main wallet addresses are supported");
return;
}
2019-02-09 19:23:51 +00:00
const char *worker_id = json_object_get_string(pass);
2019-02-11 03:56:32 +00:00
json_object *agent = NULL;
if (json_object_object_get_ex(params, "agent", &agent))
2018-08-12 13:46:08 +00:00
{
2019-02-11 03:56:32 +00:00
const char *user_agent = json_object_get_string(agent);
if (user_agent)
2019-02-09 19:23:51 +00:00
{
2019-02-11 03:56:32 +00:00
strncpy(client->agent, user_agent, 255);
client->is_xnp = strstr(user_agent, "xmr-node-proxy") != NULL
2019-05-10 05:59:47 +00:00
? true : false;
2019-02-09 19:23:51 +00:00
}
2018-08-12 13:46:08 +00:00
}
2019-02-09 19:23:51 +00:00
if (client->is_xnp && client->mode == MODE_SELF_SELECT)
{
send_validation_error(client,
"mode self-select not supported by xmr-node-proxy");
return;
}
2018-08-12 13:46:08 +00:00
strncpy(client->address, address, sizeof(client->address));
strncpy(client->worker_id, worker_id, sizeof(client->worker_id));
uuid_t cid;
uuid_generate(cid);
bin_to_hex((const unsigned char*)cid, sizeof(uuid_t),
client->client_id, 32);
2018-08-12 13:46:08 +00:00
client_send_job(client, true);
}
static void
client_on_block_template(json_object *message, client_t *client)
{
struct evbuffer *output = bufferevent_get_output(client->bev);
JSON_GET_OR_ERROR(params, message, json_type_object, client);
JSON_GET_OR_ERROR(id, params, json_type_string, client);
JSON_GET_OR_ERROR(job_id, params, json_type_string, client);
JSON_GET_OR_ERROR(blob, params, json_type_string, client);
JSON_GET_OR_ERROR(difficulty, params, json_type_int, client);
JSON_GET_OR_ERROR(height, params, json_type_int, client);
JSON_GET_OR_ERROR(prev_hash, params, json_type_string, client);
const char *jid = json_object_get_string(job_id);
if (strlen(jid) != 32)
{
send_validation_error(client, "job_id invalid length");
return;
}
int64_t h = json_object_get_int64(height);
int64_t dh = llabs(h - (int64_t)pool_stats.network_height);
if (dh > TEMLATE_HEIGHT_VARIANCE)
{
char m[64];
snprintf(m, 64, "Bad height. Differs to pool by %"PRIu64" blocks.", dh);
send_validation_error(client, m);
return;
}
const char *btb = json_object_get_string(blob);
int rc = validate_block_from_blob(btb, &sec_view[0], &pub_spend[0]);
if (rc != 0)
{
log_warn("Bad template submitted: %d", rc);
send_validation_error(client, "block template blob invalid");
return;
}
job_t *job = client_find_job(client, jid);
if (!job)
{
send_validation_error(client, "cannot find job with job_id");
return;
}
if (job->miner_template)
{
send_validation_error(client, "job already has block template");
return;
}
job->miner_template = calloc(1, sizeof(block_template_t));
job->miner_template->blocktemplate_blob = strdup(btb);
job->miner_template->difficulty = json_object_get_int64(difficulty);
job->miner_template->height = json_object_get_int64(height);
2019-11-28 00:46:23 +00:00
strncpy(job->miner_template->prev_hash,
json_object_get_string(prev_hash), 64);
2019-06-03 01:14:44 +00:00
unsigned int major_version = 0;
sscanf(btb, "%2x", &major_version);
uint8_t pow_variant = major_version >= 7 ? major_version - 6 : 0;
log_trace("Variant: %u", pow_variant);
if (pow_variant >= 6)
{
JSON_GET_OR_WARN(seed_hash, params, json_type_string);
JSON_GET_OR_WARN(next_seed_hash, params, json_type_string);
assert(seed_hash != NULL);
assert(next_seed_hash != NULL);
2019-11-28 00:46:23 +00:00
strncpy(job->miner_template->seed_hash,
2019-06-03 01:14:44 +00:00
json_object_get_string(seed_hash), 64);
2019-11-28 00:46:23 +00:00
strncpy(job->miner_template->next_seed_hash,
2019-06-03 01:14:44 +00:00
json_object_get_string(next_seed_hash), 64);
}
log_trace("Client set template: %s", btb);
char body[STATUS_BODY_MAX];
stratum_get_status_body(body, client->json_id, "OK");
evbuffer_add(output, body, strlen(body));
}
2018-08-12 13:46:08 +00:00
static void
client_on_submit(json_object *message, client_t *client)
{
struct evbuffer *output = bufferevent_get_output(client->bev);
2019-02-09 19:23:51 +00:00
2019-02-11 03:56:32 +00:00
JSON_GET_OR_ERROR(params, message, json_type_object, client);
JSON_GET_OR_ERROR(nonce, params, json_type_string, client);
JSON_GET_OR_ERROR(result, params, json_type_string, client);
JSON_GET_OR_ERROR(job_id, params, json_type_string, client);
2019-02-09 19:23:51 +00:00
char *endptr = NULL;
2019-02-11 03:56:32 +00:00
const char *nptr = json_object_get_string(nonce);
2019-02-09 19:23:51 +00:00
errno = 0;
2019-03-16 02:13:19 +00:00
unsigned long int uli = strtoul(nptr, &endptr, 16);
2019-02-09 19:23:51 +00:00
if (errno != 0 || nptr == endptr)
{
send_validation_error(client, "nonce not an unsigned long int");
return;
}
2019-03-16 02:13:19 +00:00
const uint32_t result_nonce = ntohl(uli);
2019-02-11 03:56:32 +00:00
const char *result_hex = json_object_get_string(result);
2019-02-09 19:23:51 +00:00
if (strlen(result_hex) != 64)
{
send_validation_error(client, "result invalid length");
return;
}
2019-02-09 19:23:51 +00:00
if (is_hex_string(result_hex) != 0)
{
send_validation_error(client, "result not hex string");
return;
}
2019-02-09 19:23:51 +00:00
2019-02-11 03:56:32 +00:00
const char *jid = json_object_get_string(job_id);
if (strlen(jid) != 32)
{
send_validation_error(client, "job_id invalid length");
return;
}
2018-08-12 13:46:08 +00:00
2019-02-11 03:56:32 +00:00
job_t *job = client_find_job(client, jid);
if (!job)
{
send_validation_error(client, "cannot find job with job_id");
return;
}
2018-08-12 13:46:08 +00:00
2019-05-10 05:59:47 +00:00
log_trace("Client submitted nonce=%u, result=%s",
result_nonce, result_hex);
2018-08-12 13:46:08 +00:00
/*
1. Validate submission
active_job->blocktemplate_blob to bin
add extra_nonce at reserved offset
add nonce
get hashing blob
hash
compare result
check result hash against block difficulty (if ge then mined block)
check result hash against target difficulty (if not ge, invalid share)
2. Process share
2019-05-10 05:59:47 +00:00
check result hash against template difficulty
(submit to network if good) add share to db
2018-08-12 13:46:08 +00:00
Note reserved space is: extra_nonce, instance_id, pool_nonce, worker_nonce
2019-06-07 20:52:15 +00:00
4 bytes each.
2018-08-12 13:46:08 +00:00
*/
/* Convert template to blob */
if (client->mode == MODE_SELF_SELECT && !job->miner_template)
{
send_validation_error(client, "mode self-selct and no template");
return;
}
block_template_t *bt;
if (job->miner_template)
bt = job->miner_template;
else
bt = job->block_template;
2018-08-12 13:46:08 +00:00
char *btb = bt->blocktemplate_blob;
size_t bin_size = strlen(btb) >> 1;
unsigned char *block = calloc(bin_size, sizeof(char));
hex_to_bin(bt->blocktemplate_blob, bin_size << 1, block, bin_size);
2018-08-12 13:46:08 +00:00
unsigned char *p = block;
2018-08-12 13:46:08 +00:00
uint32_t pool_nonce = 0;
uint32_t worker_nonce = 0;
if (client->mode != MODE_SELF_SELECT)
{
/* Set the extra nonce and instance_id in our reserved space */
p += bt->reserved_offset;
memcpy(p, &job->extra_nonce, sizeof(extra_nonce));
2018-08-12 13:46:08 +00:00
p += 4;
memcpy(p, &instance_id, sizeof(instance_id));
if (client->is_xnp)
{
/*
A proxy supplies pool_nonce and worker_nonce
so add them in the resrved space too.
*/
JSON_GET_OR_WARN(poolNonce, params, json_type_int);
JSON_GET_OR_WARN(workerNonce, params, json_type_int);
pool_nonce = json_object_get_int(poolNonce);
worker_nonce = json_object_get_int(workerNonce);
p += 4;
memcpy(p, &pool_nonce, sizeof(pool_nonce));
p += 4;
memcpy(p, &worker_nonce, sizeof(worker_nonce));
}
2018-08-12 13:46:08 +00:00
}
2018-08-12 13:46:08 +00:00
uint128_t sub = 0;
uint32_t *psub = (uint32_t*) &sub;
2019-02-11 03:56:32 +00:00
*psub++ = result_nonce;
2018-08-12 13:46:08 +00:00
*psub++ = job->extra_nonce;
*psub++ = pool_nonce;
*psub++ = worker_nonce;
psub -= 4;
2019-05-10 05:59:47 +00:00
log_trace("Submission reserved values: %u %u %u %u",
*psub, *(psub+1), *(psub+2), *(psub+3));
2018-08-12 13:46:08 +00:00
/* Check not already submitted */
uint128_t *submissions = job->submissions;
for (size_t i=0; i<job->submissions_count; i++)
{
if (submissions[i] == sub)
{
2019-05-04 17:04:39 +00:00
char body[ERROR_BODY_MAX];
stratum_get_error_body(body, client->json_id, "Duplicate share");
2018-08-12 13:46:08 +00:00
evbuffer_add(output, body, strlen(body));
log_debug("Duplicate share");
free(block);
return;
}
}
job->submissions = realloc((void*)submissions,
sizeof(uint128_t) * ++job->submissions_count);
job->submissions[job->submissions_count-1] = sub;
/* And the supplied nonce */
p = block;
p += 39;
2019-02-11 03:56:32 +00:00
memcpy(p, &result_nonce, sizeof(result_nonce));
2018-08-12 13:46:08 +00:00
/* Get hashong blob */
size_t hashing_blob_size;
unsigned char *hashing_blob = NULL;
2019-05-10 05:59:47 +00:00
if (get_hashing_blob(block, bin_size,
&hashing_blob, &hashing_blob_size) != 0)
2018-08-12 13:46:08 +00:00
{
2019-05-04 17:04:39 +00:00
char body[ERROR_BODY_MAX];
stratum_get_error_body(body, client->json_id, "Invalid block");
2018-08-12 13:46:08 +00:00
evbuffer_add(output, body, strlen(body));
log_debug("Invalid block");
free(block);
return;
}
/* Hash and compare */
unsigned char result_hash[32] = {0};
unsigned char submitted_hash[32] = {0};
2019-02-24 16:30:51 +00:00
uint8_t major_version = (uint8_t)block[0];
2019-06-03 01:14:44 +00:00
uint8_t pow_variant = major_version >= 7 ? major_version - 6 : 0;
if (pow_variant >= 6)
{
unsigned char seed_hash[32];
hex_to_bin(bt->seed_hash, 64, seed_hash, 32);
get_rx_hash(hashing_blob, hashing_blob_size,
(unsigned char*)result_hash, seed_hash, bt->height);
}
else
{
get_hash(hashing_blob, hashing_blob_size,
(unsigned char*)result_hash, pow_variant, bt->height);
}
hex_to_bin(result_hex, 64, submitted_hash, 32);
2018-08-12 13:46:08 +00:00
if (memcmp(submitted_hash, result_hash, 32) != 0)
{
2019-05-04 17:04:39 +00:00
char body[ERROR_BODY_MAX];
stratum_get_error_body(body, client->json_id, "Invalid share");
2018-08-12 13:46:08 +00:00
evbuffer_add(output, body, strlen(body));
log_debug("Invalid share");
2019-07-14 16:33:02 +00:00
client->bad_shares++;
2018-08-12 13:46:08 +00:00
free(block);
free(hashing_blob);
return;
}
BIGNUM *hd = BN_new();
BIGNUM *jd = BN_new();
BIGNUM *bd = BN_new();
BIGNUM *rh = NULL;
BN_set_word(jd, job->target);
BN_set_word(bd, bt->difficulty);
reverse_bin(result_hash, 32);
rh = BN_bin2bn((const unsigned char*)result_hash, 32, NULL);
BN_div(hd, NULL, base_diff, rh, bn_ctx);
BN_free(rh);
/* Process share */
client->hashes += job->target;
time_t now = time(NULL);
bool can_store = true;
2019-12-19 03:38:12 +00:00
log_trace("Checking hash against block difficulty: "
2019-05-10 05:59:47 +00:00
"%lu, job difficulty: %lu",
2018-08-12 13:46:08 +00:00
BN_get_word(bd), BN_get_word(jd));
if (BN_cmp(hd, bd) >= 0)
{
/* Yay! Mined a block so submit to network */
log_info("+++ MINED A BLOCK +++");
char *block_hex = calloc((bin_size << 1)+1, sizeof(char));
2019-05-06 03:53:36 +00:00
bin_to_hex(block, bin_size, block_hex, bin_size << 1);
2019-05-04 17:04:39 +00:00
char body[RPC_BODY_MAX];
2018-08-12 13:46:08 +00:00
snprintf(body, RPC_BODY_MAX,
2019-05-10 05:59:47 +00:00
"{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":"
"\"submit_block\", \"params\":[\"%s\"]}",
2019-05-04 17:04:39 +00:00
block_hex);
2018-08-12 13:46:08 +00:00
2019-05-11 05:46:36 +00:00
rpc_callback_t *cb = rpc_callback_new(rpc_on_block_submitted, NULL);
cb->data = calloc(1, sizeof(block_t));
2019-05-10 05:59:47 +00:00
block_t* b = (block_t*) cb->data;
2018-08-12 13:46:08 +00:00
b->height = bt->height;
2019-08-24 00:33:23 +00:00
unsigned char block_hash[32] = {0};
if (get_block_hash(block, bin_size, block_hash) != 0)
log_error("Error getting block hash!");
bin_to_hex(block_hash, 32, b->hash, 64);
2019-11-28 00:46:23 +00:00
strncpy(b->prev_hash, bt->prev_hash, 64);
2018-08-12 13:46:08 +00:00
b->difficulty = bt->difficulty;
b->status = BLOCK_LOCKED;
b->timestamp = now;
2019-05-11 05:46:36 +00:00
rpc_request(base, body, cb);
2018-08-12 13:46:08 +00:00
free(block_hex);
}
else if (BN_cmp(hd, jd) < 0)
{
can_store = false;
2019-05-04 17:04:39 +00:00
char body[ERROR_BODY_MAX];
stratum_get_error_body(body, client->json_id, "Low difficulty share");
2018-08-12 13:46:08 +00:00
evbuffer_add(output, body, strlen(body));
2019-07-14 16:33:02 +00:00
log_debug("Low difficulty (%lu) share", BN_get_word(jd));
client->bad_shares++;
2018-08-12 13:46:08 +00:00
}
BN_free(hd);
BN_free(jd);
BN_free(bd);
free(block);
free(hashing_blob);
if (can_store)
{
2019-07-14 16:33:02 +00:00
if (client->bad_shares)
client->bad_shares--;
2018-08-12 13:46:08 +00:00
share_t share;
share.height = bt->height;
share.difficulty = job->target;
strncpy(share.address, client->address, sizeof(share.address));
share.timestamp = now;
2019-02-13 01:08:30 +00:00
log_debug("Storing share with difficulty: %"PRIu64, share.difficulty);
2018-08-12 13:46:08 +00:00
int rc = store_share(share.height, &share);
if (rc != 0)
log_warn("Failed to store share: %s", mdb_strerror(rc));
2019-05-04 17:04:39 +00:00
char body[STATUS_BODY_MAX];
stratum_get_status_body(body, client->json_id, "OK");
2018-08-12 13:46:08 +00:00
evbuffer_add(output, body, strlen(body));
}
}
static void
client_on_read(struct bufferevent *bev, void *ctx)
{
2019-07-14 16:33:02 +00:00
const char *unknown_method = "Removing client. Unknown method called.";
const char *too_bad = "Removing client. Too many bad shares.";
const char *too_long = "Removing client. Message too long.";
2019-12-09 21:41:50 +00:00
const char *invalid_json = "Removing client. Invalid JSON.";
2018-08-12 13:46:08 +00:00
struct evbuffer *input, *output;
char *line;
size_t n;
client_t *client = NULL;
2018-08-12 13:46:08 +00:00
client_find(bev, &client);
if (!client)
2018-08-12 13:46:08 +00:00
return;
input = bufferevent_get_input(bev);
output = bufferevent_get_output(bev);
2019-04-05 04:27:37 +00:00
size_t len = evbuffer_get_length(input);
2019-07-07 19:52:59 +00:00
if (len > MAX_LINE)
2019-02-10 00:14:11 +00:00
{
2019-07-14 16:33:02 +00:00
char body[ERROR_BODY_MAX];
stratum_get_error_body(body, client->json_id, too_long);
evbuffer_add(output, body, strlen(body));
log_info(too_long);
evbuffer_drain(input, len);
client_clear(bev);
return;
}
if (client->bad_shares > MAX_BAD_SHARES)
{
char body[ERROR_BODY_MAX];
stratum_get_error_body(body, client->json_id, too_bad);
evbuffer_add(output, body, strlen(body));
log_info(too_bad);
2019-04-05 04:27:37 +00:00
evbuffer_drain(input, len);
client_clear(bev);
2019-02-10 00:14:11 +00:00
return;
}
2018-08-12 13:46:08 +00:00
while ((line = evbuffer_readln(input, &n, EVBUFFER_EOL_LF)))
{
json_object *message = json_tokener_parse(line);
2019-12-09 21:41:50 +00:00
if (!message)
{
free(line);
char body[ERROR_BODY_MAX];
stratum_get_error_body(body, client->json_id, invalid_json);
evbuffer_add(output, body, strlen(body));
log_info(invalid_json);
evbuffer_drain(input, len);
client_clear(bev);
return;
}
2019-04-07 17:48:59 +00:00
JSON_GET_OR_WARN(method, message, json_type_string);
JSON_GET_OR_WARN(id, message, json_type_int);
const char *method_name = json_object_get_string(method);
client->json_id = json_object_get_int(id);
2018-08-12 13:46:08 +00:00
2019-05-04 18:02:27 +00:00
bool unknown = false;
2019-04-05 04:27:37 +00:00
if (!method || !method_name)
2018-08-12 13:46:08 +00:00
{
2019-05-04 18:02:27 +00:00
unknown = true;
2018-08-12 13:46:08 +00:00
}
2019-04-07 17:48:59 +00:00
else if (strcmp(method_name, "login") == 0)
2018-08-12 13:46:08 +00:00
{
client_on_login(message, client);
}
else if (strcmp(method_name, "block_template") == 0)
{
client_on_block_template(message, client);
}
2019-04-07 17:48:59 +00:00
else if (strcmp(method_name, "submit") == 0)
2018-08-12 13:46:08 +00:00
{
client_on_submit(message, client);
}
2019-04-07 17:48:59 +00:00
else if (strcmp(method_name, "getjob") == 0)
2018-08-12 13:46:08 +00:00
{
client_send_job(client, false);
}
2019-04-07 17:48:59 +00:00
else if (strcmp(method_name, "keepalived") == 0)
2019-03-16 00:19:04 +00:00
{
2019-05-04 17:04:39 +00:00
char body[STATUS_BODY_MAX];
stratum_get_status_body(body, client->json_id, "KEEPALIVED");
2019-03-16 00:19:04 +00:00
evbuffer_add(output, body, strlen(body));
}
2018-08-12 13:46:08 +00:00
else
{
2019-05-04 18:02:27 +00:00
unknown = true;
2018-08-12 13:46:08 +00:00
}
json_object_put(message);
free(line);
2019-04-05 04:27:37 +00:00
2019-05-04 18:02:27 +00:00
if (unknown)
2019-04-05 04:27:37 +00:00
{
2019-05-04 18:02:27 +00:00
char body[ERROR_BODY_MAX];
stratum_get_error_body(body, client->json_id, unknown_method);
2019-04-05 04:27:37 +00:00
evbuffer_add(output, body, strlen(body));
2019-07-14 16:33:02 +00:00
log_info(unknown_method);
2019-04-05 04:27:37 +00:00
evbuffer_drain(input, len);
client_clear(bev);
return;
}
2018-08-12 13:46:08 +00:00
}
}
static void
client_on_error(struct bufferevent *bev, short error, void *ctx)
{
if (error & BEV_EVENT_EOF)
{
/* connection has been closed */
log_debug("Client disconnected. Removing.");
}
else if (error & BEV_EVENT_ERROR)
{
/* check errno to see what error occurred */
2018-10-12 00:13:19 +00:00
log_debug("Client error: %d. Removing.", errno);
2018-08-12 13:46:08 +00:00
}
else if (error & BEV_EVENT_TIMEOUT)
{
/* must be a timeout event handle, handle it */
log_debug("Client timeout. Removing.");
}
client_clear(bev);
}
static void
client_on_accept(evutil_socket_t listener, short event, void *arg)
{
struct event_base *base = (struct event_base*)arg;
struct sockaddr_storage ss;
socklen_t slen = sizeof(ss);
int fd = accept(listener, (struct sockaddr*)&ss, &slen);
if (fd < 0)
{
perror("accept");
2019-11-30 01:03:11 +00:00
return;
2018-08-12 13:46:08 +00:00
}
2019-11-30 01:03:11 +00:00
struct bufferevent *bev;
evutil_make_socket_nonblocking(fd);
bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(bev, client_on_read, NULL, client_on_error, NULL);
bufferevent_setwatermark(bev, EV_READ, 0, MAX_LINE);
client_add(fd, bev);
2019-12-09 21:41:50 +00:00
bufferevent_enable(bev, EV_READ|EV_WRITE);
2018-08-12 13:46:08 +00:00
}
static void
2019-12-23 07:57:40 +00:00
read_config(const char *config_file)
2018-08-12 13:46:08 +00:00
{
/* Start with some defaults for any missing... */
strncpy(config.rpc_host, "127.0.0.1", 10);
config.rpc_port = 18081;
config.rpc_timeout = 15;
config.pool_start_diff = 100;
config.share_mul = 2.0;
config.pool_fee = 0.01;
config.payment_threshold = 0.33;
config.pool_port = 4242;
config.log_level = 5;
config.webui_port = 4243;
config.disable_self_select = false;
2019-07-14 17:47:08 +00:00
strncpy(config.data_dir, "./data", 7);
2018-08-12 13:46:08 +00:00
char path[MAX_PATH] = {0};
2018-08-17 13:37:04 +00:00
if (config_file)
2018-08-12 13:46:08 +00:00
{
2018-08-17 13:37:04 +00:00
strncpy(path, config_file, MAX_PATH);
2018-08-12 13:46:08 +00:00
}
else
{
2019-04-07 18:01:16 +00:00
if (!getcwd(path, MAX_PATH))
{
log_fatal("Cannot getcwd (%s). Aborting.", errno);
2019-12-23 07:57:40 +00:00
exit(-1);
2019-04-07 18:01:16 +00:00
}
2018-08-17 13:37:04 +00:00
strcat(path, "/pool.conf");
if (access(path, R_OK) != 0)
2018-08-12 13:46:08 +00:00
{
2018-08-17 13:37:04 +00:00
strncpy(path, getenv("HOME"), MAX_PATH);
strcat(path, "/pool.conf");
if (access(path, R_OK) != 0)
2018-08-12 13:46:08 +00:00
{
2019-05-10 05:59:47 +00:00
log_fatal("Cannot find a config file in ./ or ~/ "
"and no option supplied. Aborting.");
2019-12-23 07:57:40 +00:00
exit(-1);
2018-08-12 13:46:08 +00:00
}
}
}
2018-08-17 13:37:04 +00:00
log_info("Reading config at: %s", path);
2018-08-12 13:46:08 +00:00
2018-08-17 13:37:04 +00:00
FILE *fp = fopen(path, "r");
if (!fp)
2018-08-12 13:46:08 +00:00
{
log_fatal("Cannot open config file. Aborting.");
2019-12-23 07:57:40 +00:00
exit(-1);
2018-08-12 13:46:08 +00:00
}
char line[256];
char *key;
char *val;
const char *tok = " =";
while (fgets(line, sizeof(line), fp))
2018-08-12 13:46:08 +00:00
{
key = strtok(line, tok);
if (!key)
2018-08-12 13:46:08 +00:00
continue;
val = strtok(NULL, tok);
if (!val)
2018-08-12 13:46:08 +00:00
continue;
val[strcspn(val, "\r\n")] = 0;
if (strcmp(key, "rpc-host") == 0)
{
strncpy(config.rpc_host, val, sizeof(config.rpc_host));
}
else if (strcmp(key, "rpc-port") == 0)
{
config.rpc_port = atoi(val);
}
else if (strcmp(key, "rpc-timeout") == 0)
{
config.rpc_timeout = atoi(val);
}
else if (strcmp(key, "wallet-rpc-host") == 0)
{
strncpy(config.wallet_rpc_host, val, sizeof(config.rpc_host));
}
else if (strcmp(key, "wallet-rpc-port") == 0)
{
config.wallet_rpc_port = atoi(val);
}
else if (strcmp(key, "pool-wallet") == 0)
{
strncpy(config.pool_wallet, val, sizeof(config.pool_wallet));
}
else if (strcmp(key, "pool-start-diff") == 0)
{
2019-04-05 21:47:59 +00:00
config.pool_start_diff = strtoumax(val, NULL, 10);
2018-08-12 13:46:08 +00:00
}
else if (strcmp(key, "share-mul") == 0)
{
config.share_mul = atof(val);
}
else if (strcmp(key, "pool-fee") == 0)
{
config.pool_fee = atof(val);
}
else if (strcmp(key, "payment-threshold") == 0)
{
config.payment_threshold = atof(val);
}
else if (strcmp(key, "pool-port") == 0)
{
config.pool_port = atoi(val);
}
else if (strcmp(key, "log-level") == 0)
{
config.log_level = atoi(val);
}
else if (strcmp(key, "webui-port") == 0)
{
config.webui_port = atoi(val);
}
2018-08-17 13:37:04 +00:00
else if (strcmp(key, "log-file") == 0)
{
strncpy(config.log_file, val, sizeof(config.log_file));
}
else if (strcmp(key, "block-notified") == 0)
{
config.block_notified = atoi(val);
}
else if (strcmp(key, "disable-self-select") == 0)
{
config.disable_self_select = atoi(val);
}
2019-07-14 17:47:08 +00:00
else if (strcmp(key, "data-dir") == 0)
{
2019-11-17 20:10:56 +00:00
strncpy(config.data_dir, val, sizeof(config.data_dir));
}
else if (strcmp(key, "pid-file") == 0)
{
strncpy(config.pid_file, val, sizeof(config.pid_file));
}
else if (strcmp(key, "forked") == 0)
{
config.forked = atoi(val);
2019-07-14 17:47:08 +00:00
}
2018-08-12 13:46:08 +00:00
}
fclose(fp);
if (!config.pool_wallet[0])
{
log_fatal("No pool wallet supplied. Aborting.");
2019-12-23 07:57:40 +00:00
exit(-1);
2018-08-12 13:46:08 +00:00
}
if (!config.wallet_rpc_host[0] || config.wallet_rpc_port == 0)
{
2019-05-10 05:59:47 +00:00
log_fatal("Both wallet-rpc-host and wallet-rpc-port need setting. "
"Aborting.");
2019-12-23 07:57:40 +00:00
exit(-1);
2018-08-12 13:46:08 +00:00
}
2019-07-14 17:47:08 +00:00
2019-12-23 07:57:40 +00:00
}
static void print_config()
{
2019-05-10 05:59:47 +00:00
log_info("\nCONFIG:\n rpc_host = %s\n rpc_port = %u\n "
"rpc_timeout = %u\n pool_wallet = %s\n "
"pool_start_diff = %"PRIu64"\n share_mul = %.2f\n "
2019-05-12 21:31:54 +00:00
"pool_fee = %.3f\n payment_threshold = %.2f\n "
2019-03-15 23:55:35 +00:00
"wallet_rpc_host = %s\n wallet_rpc_port = %u\n pool_port = %u\n "
2019-05-10 05:59:47 +00:00
"log_level = %u\n webui_port=%u\n "
"log-file = %s\n block-notified = %u\n "
2019-07-14 17:47:08 +00:00
"disable-self-select = %u\n "
2019-11-17 20:10:56 +00:00
"data-dir = %s\n pid-file = %s\n forked = %u\n",
2018-08-12 13:46:08 +00:00
config.rpc_host, config.rpc_port, config.rpc_timeout,
config.pool_wallet, config.pool_start_diff, config.share_mul,
config.pool_fee, config.payment_threshold,
config.wallet_rpc_host, config.wallet_rpc_port, config.pool_port,
2019-05-10 05:59:47 +00:00
config.log_level, config.webui_port,
2019-07-14 17:47:08 +00:00
config.log_file, config.block_notified, config.disable_self_select,
2019-11-17 20:10:56 +00:00
config.data_dir, config.pid_file, config.forked);
2019-05-10 05:59:47 +00:00
}
static void
sigusr1_handler(evutil_socket_t fd, short event, void *arg)
{
log_trace("Fetching last block header from signal");
fetch_last_block_header();
}
static void
sigint_handler(int sig)
{
signal(SIGINT, SIG_DFL);
exit(0);
2018-08-12 13:46:08 +00:00
}
static void
run(void)
{
evutil_socket_t listener;
struct sockaddr_in sin;
base = event_base_new();
if (!base)
{
log_fatal("Failed to create event base");
return;
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = 0;
sin.sin_port = htons(config.pool_port);
listener = socket(AF_INET, SOCK_STREAM, 0);
evutil_make_socket_nonblocking(listener);
#ifndef WIN32
{
int one = 1;
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
}
#endif
if (bind(listener, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
perror("bind");
return;
}
if (listen(listener, 16)<0)
{
perror("listen");
return;
}
2019-05-10 05:59:47 +00:00
listener_event = event_new(base, listener, EV_READ|EV_PERSIST,
client_on_accept, (void*)base);
2018-08-12 13:46:08 +00:00
if (event_add(listener_event, NULL) != 0)
{
log_fatal("Failed to add socket listener event");
return;
}
signal_usr1 = evsignal_new(base, SIGUSR1, sigusr1_handler, NULL);
event_add(signal_usr1, NULL);
if (!config.block_notified)
{
timer_120s = evtimer_new(base, timer_on_120s, NULL);
timer_on_120s(-1, EV_TIMEOUT, NULL);
}
else
fetch_last_block_header();
2018-08-12 13:46:08 +00:00
fetch_view_key();
2018-08-12 13:46:08 +00:00
timer_10m = evtimer_new(base, timer_on_10m, NULL);
timer_on_10m(-1, EV_TIMEOUT, NULL);
event_base_dispatch(base);
}
static void
2019-05-11 22:00:50 +00:00
cleanup(void)
2018-08-12 13:46:08 +00:00
{
log_info("\nPerforming cleanup");
if (listener_event)
event_free(listener_event);
2018-08-12 13:46:08 +00:00
stop_web_ui();
if (signal_usr1)
event_free(signal_usr1);
if (timer_120s)
event_free(timer_120s);
if (timer_10m)
event_free(timer_10m);
2018-08-12 13:46:08 +00:00
event_base_free(base);
pool_clients_free();
2019-05-11 05:33:26 +00:00
bstack_free(bsh);
bstack_free(bst);
2018-08-12 13:46:08 +00:00
database_close();
BN_free(base_diff);
BN_CTX_free(bn_ctx);
2019-06-03 01:14:44 +00:00
rx_stop_mining();
rx_slow_hash_free_state();
2019-03-08 01:47:16 +00:00
pthread_mutex_destroy(&mutex_clients);
2018-08-12 13:46:08 +00:00
log_info("Pool shutdown successfully");
if (fd_log)
2018-08-17 13:37:04 +00:00
fclose(fd_log);
2018-08-12 13:46:08 +00:00
}
int main(int argc, char **argv)
{
static struct option options[] =
{
{"config-file", required_argument, 0, 'c'},
2018-08-17 13:37:04 +00:00
{"log-file", required_argument, 0, 'l'},
{"block-notified", optional_argument, 0, 'b'},
2019-07-14 17:47:08 +00:00
{"data-dir", required_argument, 0, 'd'},
2019-11-17 20:10:56 +00:00
{"pid-file", required_argument, 0, 'p'},
{"forked", optional_argument, 0, 'f'},
2018-08-12 13:46:08 +00:00
{0, 0, 0, 0}
};
2018-08-17 13:37:04 +00:00
char *config_file = NULL;
char *log_file = NULL;
bool block_notified = false;
2019-07-14 17:47:08 +00:00
char *data_dir = NULL;
2019-11-17 20:10:56 +00:00
char *pid_file = NULL;
bool forked = false;
2018-08-12 13:46:08 +00:00
int c;
while (1)
{
int option_index = 0;
2019-11-17 20:10:56 +00:00
c = getopt_long (argc, argv, "c:l:b::d:p:f::",
2018-08-12 13:46:08 +00:00
options, &option_index);
if (c == -1)
break;
switch (c)
{
case 'c':
2018-08-17 13:37:04 +00:00
config_file = strdup(optarg);
break;
case 'l':
log_file = strdup(optarg);
2018-08-12 13:46:08 +00:00
break;
case 'b':
block_notified = true;
if (optarg)
block_notified = atoi(optarg);
break;
2019-07-14 17:47:08 +00:00
case 'd':
data_dir = strdup(optarg);
break;
2019-11-17 20:10:56 +00:00
case 'p':
pid_file = strdup(optarg);
break;
case 'f':
forked = true;
if (optarg)
forked = atoi(optarg);
break;
2018-08-12 13:46:08 +00:00
}
}
2019-11-17 20:10:56 +00:00
setvbuf(stdout, NULL, _IONBF, 0);
log_set_level(LOG_INFO);
2019-07-14 17:47:08 +00:00
2019-12-23 07:57:40 +00:00
read_config(config_file);
2019-07-14 17:47:08 +00:00
if (config_file)
free(config_file);
2019-12-23 07:57:40 +00:00
/* Any supplied command line options take precedent... */
2019-07-14 17:47:08 +00:00
if (log_file)
2019-12-23 07:57:40 +00:00
{
strncpy(config.log_file, log_file, sizeof(config.log_file));
2019-07-14 17:47:08 +00:00
free(log_file);
2019-12-23 07:57:40 +00:00
}
2019-07-14 17:47:08 +00:00
if (data_dir)
2019-12-23 07:57:40 +00:00
{
strncpy(config.data_dir, data_dir, sizeof(config.data_dir));
2019-07-14 17:47:08 +00:00
free(data_dir);
2019-12-23 07:57:40 +00:00
}
2019-11-17 20:10:56 +00:00
if (pid_file)
2019-12-23 07:57:40 +00:00
{
strncpy(config.pid_file, pid_file, sizeof(config.pid_file));
2019-11-17 20:10:56 +00:00
free(pid_file);
2019-12-23 07:57:40 +00:00
}
if (forked)
config.forked = forked;
if (block_notified)
config.block_notified = block_notified;
2018-08-12 13:46:08 +00:00
log_set_level(LOG_FATAL - config.log_level);
if (config.log_file[0])
2018-08-17 13:37:04 +00:00
{
fd_log = fopen(config.log_file, "a");
if (!fd_log)
2018-08-17 13:37:04 +00:00
log_info("Failed to open log file: %s", config.log_file);
else
log_set_fp(fd_log);
}
2019-12-23 07:57:40 +00:00
print_config();
log_info("Starting pool");
2019-11-17 20:10:56 +00:00
if (config.forked)
{
log_info("Daemonizing");
char *pf = NULL;
if (config.pid_file[0])
pf = config.pid_file;
forkoff(pf);
}
signal(SIGINT, sigint_handler);
atexit(cleanup);
2018-08-12 13:46:08 +00:00
int err = 0;
2019-07-14 17:47:08 +00:00
if ((err = database_init(config.data_dir)) != 0)
2018-08-12 13:46:08 +00:00
{
log_fatal("Failed to initialize database. Return code: %d", err);
goto cleanup;
}
2019-05-11 05:33:26 +00:00
bstack_new(&bst, BLOCK_TEMPLATES_MAX, sizeof(block_template_t),
template_recycle);
bstack_new(&bsh, BLOCK_HEADERS_MAX, sizeof(block_t), NULL);
2018-08-12 13:46:08 +00:00
bn_ctx = BN_CTX_new();
base_diff = NULL;
2019-05-10 05:59:47 +00:00
BN_hex2bn(&base_diff,
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
2018-08-12 13:46:08 +00:00
2019-06-07 20:52:15 +00:00
uuid_t iid;
uuid_generate(iid);
memcpy(&instance_id, iid, 4);
2018-08-12 13:46:08 +00:00
pool_clients_init();
wui_context_t uic;
uic.port = config.webui_port;
uic.pool_stats = &pool_stats;
uic.pool_fee = config.pool_fee;
2018-08-18 13:32:29 +00:00
uic.pool_port = config.pool_port;
uic.allow_self_select = !config.disable_self_select;
2018-08-12 13:46:08 +00:00
uic.payment_threshold = config.payment_threshold;
start_web_ui(&uic);
run();
cleanup:
return 0;
}