Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialization logic is rewritten #476

Merged
merged 8 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ sources = \
time.c \
math.c \
xdag_config.c \
global.c \
$(dnet)/dnet_crypt.c \
$(dnet)/dnet_xdag.c \
$(dfslib)/dfslib_crypt.c \
Expand Down Expand Up @@ -93,6 +94,7 @@ headers = \
time.h \
math.h \
xdag_config.h \
global.h \
$(dnet)/dnet_crypt.h \
$(dnet)/dnet_history.h \
$(dnet)/dnet_main.h \
Expand Down
43 changes: 15 additions & 28 deletions client/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "../ldus/source/include/ldus/rbtree.h"
#include "block.h"
#include "crypt.h"
#include "global.h"
#include "wallet.h"
#include "storage.h"
#include "transport.h"
Expand Down Expand Up @@ -114,7 +115,6 @@ static struct cache_block *cache_first = NULL, *cache_last = NULL;
static pthread_mutex_t block_mutex;
static pthread_mutex_t rbtree_mutex;
//TODO: this variable duplicates existing global variable g_is_pool. Probably should be removed
static int g_light_mode = 0;
static uint32_t cache_bounded_counter = 0;
static struct orphan_block *g_orphan_first[ORPHAN_HASH_SIZE], *g_orphan_last[ORPHAN_HASH_SIZE];

Expand Down Expand Up @@ -542,7 +542,7 @@ static int add_block_nolock(struct xdag_block *newBlock, xtime_t limit)
}
}

if(g_light_mode) {
if(g_xdag_type == XDAG_WALLET) {
outmask = 0;
}

Expand All @@ -561,7 +561,7 @@ static int add_block_nolock(struct xdag_block *newBlock, xtime_t limit)
}

/* if not read from storage and timestamp is ...ffff and last field is nonce then the block is extra */
if (!g_light_mode && (transportHeader & (sizeof(struct xdag_block) - 1))
if (g_xdag_type == XDAG_POOL && (transportHeader & (sizeof(struct xdag_block) - 1))
&& (tmpNodeBlock.time & (MAIN_CHAIN_PERIOD - 1)) == (MAIN_CHAIN_PERIOD - 1)
&& (signinmask & 1 << (XDAG_BLOCK_FIELDS - 1))) {
tmpNodeBlock.flags |= BI_EXTRA;
Expand All @@ -585,7 +585,7 @@ static int add_block_nolock(struct xdag_block *newBlock, xtime_t limit)
}
}

if(!g_light_mode) {
if(g_xdag_type == XDAG_POOL) {
check_new_main();
}

Expand Down Expand Up @@ -839,7 +839,7 @@ void *add_block_callback(void *block, void *data)

pthread_mutex_unlock(&block_mutex);

if(res >= 0) {
if(res >= 0 && g_xdag_type == XDAG_POOL) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocks synchronization logic is initialized only for pools now, so xdag_sync_pop_block can be called only for pools

xdag_sync_pop_block(b);
}

Expand Down Expand Up @@ -928,7 +928,7 @@ struct xdag_block* xdag_create_block(struct xdag_field *fields, int inputsCount,
block[0].field[0].time = send_time;
block[0].field[0].amount = fee;

if (g_light_mode) {
if (g_xdag_type == XDAG_WALLET) {
pthread_mutex_lock(&g_create_block_mutex);
if (res < XDAG_BLOCK_FIELDS && ourfirst) {
setfld(XDAG_FIELD_OUT, ourfirst->hash, xdag_hashlow_t);
Expand Down Expand Up @@ -1142,7 +1142,7 @@ static void *work_thread(void *arg)

// launching of synchronization thread
g_xdag_sync_on = 1;
if (!g_light_mode && !sync_thread_running) {
if (g_xdag_type == XDAG_POOL && !sync_thread_running) {
xdag_mess("Starting sync thread...");
int err = pthread_create(&th, 0, sync_thread, 0);
if(err != 0) {
Expand All @@ -1159,7 +1159,7 @@ static void *work_thread(void *arg)
}
}

if (g_light_mode) {
if (g_xdag_type == XDAG_WALLET) {
// start mining threads
xdag_mess("Starting mining threads...");
xdag_mining_start(n_mining_threads);
Expand All @@ -1181,7 +1181,7 @@ static void *work_thread(void *arg)
g_xdag_extstats.hashrate_s = ((double)(nhashes - nhashes0) * 1024) / (t - t0);
}

if (!g_block_production_on && !g_light_mode &&
if (!g_block_production_on && g_xdag_type == XDAG_POOL &&
(g_xdag_state == XDAG_STATE_WAIT || g_xdag_state == XDAG_STATE_WTST ||
g_xdag_state == XDAG_STATE_SYNC || g_xdag_state == XDAG_STATE_STST ||
g_xdag_state == XDAG_STATE_CONN || g_xdag_state == XDAG_STATE_CTST)) {
Expand All @@ -1194,15 +1194,6 @@ static void *work_thread(void *arg)
} else if (time(NULL) - last_time_nmain_unequal > MAX_TIME_NMAIN_STALLED) {
g_block_production_on = 1;
}

if (g_block_production_on) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need remove these logs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this code block for pool at all. Maybe I just move xdag_mess("Starting refer blocks creation..."); to another place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

xdag_mess("Starting refer blocks creation...");

// start mining threads
xdag_mess("Starting mining threads...");
xdag_mining_start(n_mining_threads);
}

}

if (g_block_production_on &&
Expand Down Expand Up @@ -1249,22 +1240,22 @@ static void *work_thread(void *arg)
time_t last_received = atomic_load_explicit_uint_least64(&g_xdag_last_received, memory_order_relaxed);

if (t > (last_received << 10) && t - (last_received << 10) > 3 * MAIN_CHAIN_PERIOD) {
g_xdag_state = (g_light_mode ? (g_xdag_testnet ? XDAG_STATE_TTST : XDAG_STATE_TRYP)
g_xdag_state = (g_xdag_type == XDAG_WALLET ? (g_xdag_testnet ? XDAG_STATE_TTST : XDAG_STATE_TRYP)
: (g_xdag_testnet ? XDAG_STATE_WTST : XDAG_STATE_WAIT));
conn_time = sync_time = 0;
} else {
if (!conn_time) {
conn_time = t;
}

if (!g_light_mode && t - conn_time >= 2 * MAIN_CHAIN_PERIOD
if (g_xdag_type == XDAG_POOL && t - conn_time >= 2 * MAIN_CHAIN_PERIOD
&& !memcmp(&g_xdag_stats.difficulty, &g_xdag_stats.max_difficulty, sizeof(xdag_diff_t))) {
sync_time = t;
}

if (t - (g_xdag_xfer_last << 10) <= 2 * MAIN_CHAIN_PERIOD + 4) {
g_xdag_state = XDAG_STATE_XFER;
} else if (g_light_mode) {
} else if (g_xdag_type == XDAG_WALLET) {
g_xdag_state = (g_xdag_mining_threads > 0 ?
(g_xdag_testnet ? XDAG_STATE_MTST : XDAG_STATE_MINE)
: (g_xdag_testnet ? XDAG_STATE_PTST : XDAG_STATE_POOL));
Expand All @@ -1276,7 +1267,7 @@ static void *work_thread(void *arg)
}
}

if (!g_light_mode) {
if (g_xdag_type == XDAG_POOL) {
check_new_main();
}

Expand All @@ -1297,16 +1288,12 @@ static void *work_thread(void *arg)
* for the light node is_pool == 0;
* miner_address = 1 - the address of the miner is explicitly set
*/
int xdag_blocks_start(int is_pool, int mining_threads_count, int miner_address)
int xdag_blocks_start(int mining_threads_count, int miner_address)
{
pthread_mutexattr_t attr;
pthread_t th;

if (!is_pool) {
g_light_mode = 1;
}

if (xdag_mem_init(g_light_mode && !miner_address ? 0 : (((xdag_get_xtimestamp() - XDAG_ERA) >> 10) + (uint64_t)365 * 24 * 60 * 60) * 2 * sizeof(struct block_internal))) {
if (xdag_mem_init(g_xdag_type == XDAG_WALLET && !miner_address ? 0 : (((xdag_get_xtimestamp() - XDAG_ERA) >> 10) + (uint64_t)365 * 24 * 60 * 60) * 2 * sizeof(struct block_internal))) {
return -1;
}

Expand Down
2 changes: 1 addition & 1 deletion client/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ extern "C" {
extern int g_bi_index_enable;

// start of regular block processing
extern int xdag_blocks_start(int is_pool, int mining_threads_count, int miner_address);
extern int xdag_blocks_start(int mining_threads_count, int miner_address);

// checks and adds block to the storage. Returns non-zero value in case of error.
extern int xdag_add_block(struct xdag_block *b);
Expand Down
9 changes: 5 additions & 4 deletions client/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include "global.h"
#include "init.h"
#include "address.h"
#include "wallet.h"
Expand Down Expand Up @@ -331,7 +332,7 @@ int xdag_command(char *cmd, FILE *out)

XDAG_COMMAND *command = find_xdag_command(cmd);

if(!command || (command->avaibility == 1 && !g_is_miner) || (command->avaibility == 2 && g_is_miner)) {
if(!command || (command->avaibility == 1 && g_xdag_type == XDAG_POOL) || (command->avaibility == 2 && g_xdag_type == XDAG_WALLET)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just use two functions
isPool() {
return g_xdag_type == XDAG_POOL;
}

isWallet() {
return g_xdag_type == XDAG_WALLET;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I will add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

fprintf(out, "Illegal command.\n");
} else {
if(!strcmp(command->name, "xfer")) {
Expand All @@ -347,7 +348,7 @@ void processAccountCommand(char *nextParam, FILE *out)
{
struct account_callback_data d;
d.out = out;
d.count = (g_is_miner ? 1 : 20);
d.count = (g_xdag_type == XDAG_WALLET ? 1 : 20);
char *cmd = strtok_r(nextParam, " \t\r\n", &nextParam);
if(cmd) {
sscanf(cmd, "%d", &d.count);
Expand Down Expand Up @@ -525,7 +526,7 @@ void processPoolCommand(char *nextParam, FILE *out)

void processStatsCommand(FILE *out)
{
if(g_is_miner) {
if(g_xdag_type == XDAG_WALLET) {
fprintf(out, "your hashrate MHs: %.2lf\n", xdagGetHashRate());
} else {
fprintf(out, "Statistics for ours and maximum known parameters:\n"
Expand Down Expand Up @@ -832,7 +833,7 @@ int xfer_callback(void *data, xdag_hash_t hash, xdag_amount_t amount, xtime_t ti
if(!amount) {
return -1;
}
if(!g_is_miner && xdag_get_frame() < (time >> 16) + 2 * CONFIRMATIONS_COUNT) {
if(g_xdag_type == XDAG_POOL && xdag_get_frame() < (time >> 16) + 2 * CONFIRMATIONS_COUNT) {
return 0;
}
for(i = 0; i < xferData->keysCount; ++i) {
Expand Down
11 changes: 11 additions & 0 deletions client/global.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "global.h"

int g_xdag_state = XDAG_STATE_INIT;
int g_xdag_testnet = 0;
int g_xdag_run = 0;
enum xdag_field_type g_block_header_type = XDAG_FIELD_HEAD;
struct xdag_stats g_xdag_stats;
struct xdag_ext_stats g_xdag_extstats;
int g_disable_mining = 0;
enum xdag_type g_xdag_type = 0;
char *g_coinname, *g_progname;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emmm, there is one extra character on windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't understand what is wrong here.

69 changes: 69 additions & 0 deletions client/global.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#ifndef XDAG_GLOBAL_H
#define XDAG_GLOBAL_H

#include <stdint.h>
#include "types.h"
#include "time.h"
#include "system.h"
#include "block.h"

enum xdag_states {
#define xdag_state(n,s) XDAG_STATE_##n ,
#include "state.h"
#undef xdag_state
};

extern struct xdag_stats {
xdag_diff_t difficulty, max_difficulty;
uint64_t nblocks, total_nblocks;
uint64_t nmain, total_nmain;
uint32_t nhosts, total_nhosts;
union {
uint32_t reserved[2];
xdag_frame_t main_time;
};
} g_xdag_stats;

extern struct xdag_ext_stats {
xdag_diff_t hashrate_total[HASHRATE_LAST_MAX_TIME];
xdag_diff_t hashrate_ours[HASHRATE_LAST_MAX_TIME];
xtime_t hashrate_last_time;
uint64_t nnoref;
uint64_t nextra;
uint64_t nhashes;
double hashrate_s;
uint32_t nwaitsync;
uint32_t cache_size;
uint32_t cache_usage;
double cache_hitrate;
int use_orphan_hashtable;
} g_xdag_extstats;

enum xdag_type {
XDAG_WALLET = 1,
XDAG_POOL = 2
};

// defines if xdag started as a pool or a wallet
extern enum xdag_type g_xdag_type;

/* the program state */
extern int g_xdag_state;

/* 1 - the program works in a test network */
extern int g_xdag_testnet;

/* is there command 'run' */
extern int g_xdag_run;

/* coin token and program name */
extern char *g_coinname, *g_progname;

//defines if mining is disabled (pool)
extern int g_disable_mining;

//Default type of the block header
//Test network and main network have different types of the block headers, so blocks from different networks are incompatible
extern enum xdag_field_type g_block_header_type;

#endif
Loading