1 Commits
Author SHA1 Message Date
Tanguy Pruvot a6822e6d0d ethash test to finish...
remains the mixHash thing to compute, and to check the submitted nonce64 endian/value

Signed-off-by: Tanguy Pruvot <tanguy.pruvot@gmail.com>
2015-08-16 13:35:01 +02:00
9 changed files with 1275 additions and 3 deletions

No files matched your search

+1
View File
@@ -21,6 +21,7 @@ ccminer_SOURCES = elist.h miner.h compat.h \
crc32.c hefty1.c \
ccminer.cpp pools.cpp util.cpp bignum.cpp \
api.cpp hashlog.cpp nvml.cpp stats.cpp sysinfos.cpp cuda.cpp \
ethash/etherpc.cpp ethash/ethash.cu \
heavy/heavy.cu \
heavy/cuda_blake512.cu heavy/cuda_blake512.h \
heavy/cuda_combine.cu heavy/cuda_combine.h \
+17
View File
@@ -90,6 +90,7 @@ enum sha_algos {
ALGO_C11,
ALGO_DEEP,
ALGO_DMD_GR,
ALGO_ETHER,
ALGO_FRESH,
ALGO_FUGUE256, /* Fugue256 */
ALGO_GROESTL,
@@ -127,6 +128,7 @@ static const char *algo_names[] = {
"c11",
"deep",
"dmd-gr",
"ethash",
"fresh",
"fugue256",
"groestl",
@@ -807,6 +809,9 @@ static bool submit_upstream_work(CURL *curl, struct work *work)
bool stale_work = false;
char s[384];
if (pool->type & POOL_ETHER)
return ether_submitwork(curl, pool, work);
/* discard if a newer bloc was received */
stale_work = work->height && work->height < g_work.height;
if (have_stratum && !stale_work && opt_algo != ALGO_ZR5 && opt_algo != ALGO_SCRYPT_JANE) {
@@ -1083,6 +1088,10 @@ static bool get_upstream_work(CURL *curl, struct work *work)
struct pool_infos *pool = &pools[work->pooln];
json_t *val;
if (pool->type & POOL_ETHER) {
return ether_getwork(curl, pool, work);
}
if (opt_debug_threads)
applog(LOG_DEBUG, "%s: want_longpoll=%d have_longpoll=%d",
__func__, want_longpoll, have_longpoll);
@@ -1976,6 +1985,10 @@ static void *miner_thread(void *userdata)
rc = scanhash_zr5(thr_id, &work, max_nonce, &hashes_done);
break;
case ALGO_ETHER:
rc = scanhash_ether(thr_id, &work, max_nonce, &hashes_done);
break;
default:
/* should never happen */
goto out;
@@ -3161,6 +3174,10 @@ int main(int argc, char *argv[])
cur_pooln = pool_get_first_valid(0);
pool_switch(cur_pooln);
if (opt_algo == ALGO_ETHER) {
pools[cur_pooln].type |= POOL_ETHER;
}
flags = !opt_benchmark && strncmp(rpc_url, "https:", 6)
? (CURL_GLOBAL_ALL & ~CURL_GLOBAL_SSL)
: CURL_GLOBAL_ALL;
+3 -3
View File
@@ -162,7 +162,7 @@
#define PACKAGE_NAME "ccminer"
/* Define to the full name and version of this package. */
#define PACKAGE_STRING "ccminer 1.6.5-C11"
#define PACKAGE_STRING "ccminer 1.6.6-dev"
/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "ccminer"
@@ -171,7 +171,7 @@
#define PACKAGE_URL "http://github.com/tpruvot/ccminer"
/* Define to the version of this package. */
#define PACKAGE_VERSION "1.6.5-C11"
#define PACKAGE_VERSION "1.6.6-dev"
/* If using the C implementation of alloca, define if you know the
direction of stack growth for your system; otherwise it will be
@@ -185,7 +185,7 @@
#define STDC_HEADERS 1
/* Version number of package */
#define VERSION "1.6.5-C11"
#define VERSION "1.6.6-dev"
/* Define curl_free() as free() if our version of curl lacks curl_free. */
/* #undef curl_free */
+531
View File
@@ -0,0 +1,531 @@
/*
* Genoil's CUDA mining kernel for Ethereum
* based on Tim Hughes' opencl kernel.
* thanks to tpruvot,djm34,sp,cbuchner for things i took from ccminer.
*/
#define SHUFFLE_MIN_VER 9350
#include "miner.h"
#include "cuda_helper.h"
#include "ethash_cu_miner_kernel.h"
#include <stdio.h>
#include <unistd.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "vector_types.h"
#include "rotl64.cuh"
#include "keccak.cuh"
//#include "ethash_cu_miner_kernel_globals.h"
__constant__ uint32_t d_dag_size;
__constant__ uint32_t d_max_outputs;
#define ACCESSES 64
#define THREADS_PER_HASH (128 / 16) /* 8 */
#define FNV_PRIME 0x01000193
#define SWAP64(v) \
((ROTL64L(v, 8) & 0x000000FF000000FF) | \
(ROTL64L(v, 24) & 0x0000FF000000FF00) | \
(ROTL64H(v, 40) & 0x00FF000000FF0000) | \
(ROTL64H(v, 56) & 0xFF000000FF000000))
#undef SWAP64
#define SWAP64(v) cuda_swab64(v)
#define PACK64(result, lo, hi) asm("mov.b64 %0, {%1,%2};//pack64" : "=l"(result) : "r"(lo), "r"(hi));
#define UNPACK64(lo, hi, input) asm("mov.b64 {%0, %1}, %2;//unpack64" : "=r"(lo),"=r"(hi) : "l"(input));
#define copy(dst, src, count) for (uint32_t i = 0; i < count; i++) { (dst)[i] = (src)[i]; }
#define countof(x) (sizeof(x) / sizeof(x[0]))
#define fnv(x,y) ((x) * FNV_PRIME ^(y))
__device__ uint4 fnv4(uint4 a, uint4 b)
{
uint4 c;
c.x = a.x * FNV_PRIME ^ b.x;
c.y = a.y * FNV_PRIME ^ b.y;
c.z = a.z * FNV_PRIME ^ b.z;
c.w = a.w * FNV_PRIME ^ b.w;
return c;
}
__device__ uint32_t fnv_reduce(uint4 v)
{
return fnv(fnv(fnv(v.x, v.y), v.z), v.w);
}
__device__ void init_hash(hash32_t const* header, uint64_t nonce, hash64_t &init)
{
// sha3_512(header .. nonce)
uint64_t state[25] = { 0 };
//copy(state, header->uint64s, 4);
#pragma unroll
for (int i = 0; i < 4; i++) state[i] = header->uint64s[i];
state[4] = nonce;
state[5] = 0x0000000000000001;
//state[6] = 0;
//state[7] = 0;
state[8] = 0x8000000000000000;
//for (uint32_t i = 9; i < 25; i++) {
// state[i] = 0;
//}
keccak_f1600_block(state, 8);
//copy(init.uint64s, state, 8);
#pragma unroll
for (int i = 0; i < 8; i++) init.uint64s[i] = state[i];
}
__device__ uint32_t inner_loop(uint4 mix, uint32_t thread_id, uint32_t* share, hash128_t const* g_dag)
{
// share init0
if (thread_id == 0)
*share = mix.x;
uint32_t init0 = *share;
uint32_t a = 0;
do
{
bool update_share = thread_id == ((a >> 2) & (THREADS_PER_HASH-1));
//#pragma unroll 4
for (uint32_t i = 0; i < 4; i++)
{
if (update_share)
{
uint32_t m[4] = { mix.x, mix.y, mix.z, mix.w };
*share = fnv(init0 ^ (a + i), m[i]) % d_dag_size;
}
__threadfence_block();
#if __CUDA_ARCH__ >= 350
mix = fnv4(mix, __ldg(&g_dag[*share].uint4s[thread_id]));
#else
mix = fnv4(mix, g_dag[*share].uint4s[thread_id]);
#endif
}
} while ((a += 4) != ACCESSES);
return fnv_reduce(mix);
}
__device__ void final_hash(hash64_t const* init, hash32_t const* mix, hash32_t &hash)
{
uint64_t state[25] = { 0 };
// keccak_256(keccak_512(header..nonce) .. mix);
copy(state, init->uint64s, 8);
copy(state + 8, mix->uint64s, 4);
state[12] = 0x0000000000000001;
//#pragma unroll
//for (uint32_t i = 13; i < 16; i++)
//{
//state[i] = 0;
//}
state[16] = 0x8000000000000000;
//#pragma unroll
//for (uint32_t i = 17; i < 25; i++)
//{
//state[i] = 0;
//}
keccak_f1600_block(state,1);
// copy out
copy(hash.uint64s, state, 4);
}
typedef union
{
hash64_t init;
hash32_t mix;
} compute_hash_share;
#if __CUDA_ARCH__ >= SHUFFLE_MIN_VER
__device__ uint64_t compute_hash_shuffle(
hash32_t const* g_header,
hash128_t const* g_dag,
uint64_t nonce
)
{
// sha3_512(header .. nonce)
uint64_t state[25];
copy(state, g_header->uint64s, 4);
state[4] = nonce;
state[5] = 0x0000000000000001ULL;
for (uint32_t i = 6; i < 25; i++)
{
state[i] = 0;
}
state[8] = 0x8000000000000000ULL;
keccak_f1600_block(state, 8);
// Threads work together in this phase in groups of 8.
const int thread_id = threadIdx.x & (THREADS_PER_HASH - 1);
const int start_lane = threadIdx.x & ~(THREADS_PER_HASH - 1);
const int mix_idx = (thread_id & 3);
uint4 mix;
uint32_t shuffle[16];
//uint32_t * init = (uint32_t *)state;
uint32_t init[16];
UNPACK64(init[0], init[1], state[0]);
UNPACK64(init[2], init[3], state[1]);
UNPACK64(init[4], init[5], state[2]);
UNPACK64(init[6], init[7], state[3]);
UNPACK64(init[8], init[9], state[4]);
UNPACK64(init[10], init[11], state[5]);
UNPACK64(init[12], init[13], state[6]);
UNPACK64(init[14], init[15], state[7]);
for (int i = 0; i < THREADS_PER_HASH; i++)
{
// share init among threads
for (int j = 0; j < 16; j++)
shuffle[j] = __shfl(init[j], start_lane + i);
// ugly but avoids local reads/writes
if (mix_idx == 0) {
mix = make_uint4(shuffle[0], shuffle[1], shuffle[2], shuffle[3]);
}
else if (mix_idx == 1) {
mix = make_uint4(shuffle[4], shuffle[5], shuffle[6], shuffle[7]);
}
else if (mix_idx == 2) {
mix = make_uint4(shuffle[8], shuffle[9], shuffle[10], shuffle[11]);
}
else {
mix = make_uint4(shuffle[12], shuffle[13], shuffle[14], shuffle[15]);
}
uint32_t init0 = __shfl(shuffle[0], start_lane);
for (uint32_t a = 0; a < ACCESSES; a+=4)
{
int t = ((a >> 2) & (THREADS_PER_HASH - 1));
for (uint32_t b = 0; b < 4; b++)
{
if (thread_id == t)
{
shuffle[0] = fnv(init0 ^ (a + b), ((uint32_t *)&mix)[b]) % d_dag_size;;
}
shuffle[0] = __shfl(shuffle[0], start_lane + t);
mix = fnv4(mix, g_dag[shuffle[0]].uint4s[thread_id]);
}
}
uint32_t thread_mix = fnv_reduce(mix);
// update mix accross threads
for (int j = 0; j < 8; j++)
shuffle[j] = __shfl(thread_mix, start_lane + j);
if (i == thread_id) {
//move mix into state:
PACK64(state[8], shuffle[0], shuffle[1]);
PACK64(state[9], shuffle[2], shuffle[3]);
PACK64(state[10], shuffle[4], shuffle[5]);
PACK64(state[11], shuffle[6], shuffle[7]);
}
}
// keccak_256(keccak_512(header..nonce) .. mix);
state[12] = 0x0000000000000001ULL;
for (uint32_t i = 13; i < 25; i++)
{
state[i] = 0ULL;
}
state[16] = 0x8000000000000000;
keccak_f1600_block(state, 1);
return state[0];
}
#endif
__device__ void compute_hash(
hash32_t const* g_header,
hash128_t const* g_dag,
uint64_t nonce,
hash32_t &out
)
{
extern __shared__ compute_hash_share share[];
// Compute one init hash per work item.
hash64_t init;
init_hash(g_header, nonce, init);
// Threads work together in this phase in groups of 8.
uint32_t const thread_id = threadIdx.x & (THREADS_PER_HASH-1);
uint32_t const hash_id = threadIdx.x >> 3;
hash32_t mix;
for (int i = 0; i < THREADS_PER_HASH; i++)
{
// share init with other threads
if (i == thread_id)
share[hash_id].init = init;
uint4 thread_init = share[hash_id].init.uint4s[thread_id & 3];
uint32_t thread_mix = inner_loop(thread_init, thread_id, share[hash_id].mix.uint32s, g_dag);
share[hash_id].mix.uint32s[thread_id] = thread_mix;
if (i == thread_id)
mix = share[hash_id].mix;
}
final_hash(&init, &mix, out);
}
__global__ void
__launch_bounds__(128, 7)
ethash_search(
uint32_t* g_output,
hash32_t const* g_header,
hash128_t const* g_dag,
uint64_t start_nonce,
uint64_t target
)
{
uint32_t const gid = blockIdx.x * blockDim.x + threadIdx.x;
#if 0 // _CUDA_ARCH__ >= SHUFFLE_MIN_VER
uint64_t hash = compute_hash_shuffle(g_header, g_dag, start_nonce + gid);
if (SWAP64(hash) < target)
{
atomicInc(g_output, d_max_outputs);
g_output[g_output[0]] = gid;
}
#else
hash32_t hash;
compute_hash(g_header, g_dag, start_nonce + gid, hash);
if (SWAP64(hash.uint64s[0]) <= target)
{
atomicInc(g_output,d_max_outputs);
g_output[g_output[0]] = gid;
}
#endif
}
void run_ethash_hash(
hash32_t* g_hashes,
hash32_t const* g_header,
hash128_t const* g_dag,
uint64_t start_nonce
)
{
}
void run_ethash_search(
uint32_t blocks,
uint32_t threads,
cudaStream_t stream,
uint32_t* g_output,
hash32_t const* g_header,
hash128_t const* g_dag,
uint64_t start_nonce,
uint64_t target
)
{
#if __CUDA_ARCH__ >= SHUFFLE_MIN_VER
ethash_search <<<blocks, threads, 0, stream>>>(g_output, g_header, g_dag, start_nonce, target);
#else
ethash_search <<<blocks, threads, (sizeof(compute_hash_share) * threads) / THREADS_PER_HASH, stream>>>(g_output, g_header, g_dag, start_nonce, target);
#endif
}
cudaError set_constants(
uint32_t * dag_size,
uint32_t * max_outputs
)
{
cudaError result;
result = cudaMemcpyToSymbol(d_dag_size, dag_size, sizeof(uint32_t));
result = cudaMemcpyToSymbol(d_max_outputs, max_outputs, sizeof(uint32_t));
return result;
}
static unsigned m_search_batch_size = 262144;
static unsigned m_workgroup_size = 64;
static __thread hash128_t * m_dag_ptr;
static __thread hash32_t * m_header;
//static void* m_hash_buf[MAX_GPUS] = { 0 };
static uint32_t* m_search_buf[MAX_GPUS] = { 0 };
static cudaStream_t m_streams[MAX_GPUS] = { 0 };
static uint64_t dagSize = 0;
static int fdDAG = -1;
static uint8_t const *dag = NULL;
uint32_t const c_zero = 0;
#define ETHASH_MIX_BYTES 128
#define MIX_WORDS (ETHASH_MIX_BYTES/4)
uint8_t* alloc_dag_file(uint32_t rev, uint32_t* seedhash)
{
uint8_t* buffer = NULL;
char fn[256];
struct stat stat_buf;
int rc;
sprintf(fn, "%s/.ethash/full-R%u-%08x%08x", getenv("HOME"), rev, seedhash[7], seedhash[6]);
rc = stat(fn, &stat_buf);
fdDAG = open(fn, O_RDONLY);
if (rc != 0) {
applog(LOG_ERR, "Unable to open DAG file %s (%d)!", fn, rc);
return NULL;
}
dagSize = stat_buf.st_size - 8;
#ifdef __linux__
buffer = (uint8_t*) (mmap(NULL, dagSize, PROT_READ, MAP_FILE | MAP_SHARED | MAP_POPULATE , fdDAG, 0));
#else
buffer = (uint8_t*) (mmap(NULL, dagSize, PROT_READ, MAP_FILE | MAP_SHARED), fdDAG, 0));
#endif
if (buffer == MAP_FAILED) {
applog(LOG_ERR, "Unable to map DAG file %s!", fn);
return NULL;
}
if (buffer[0] != 0xfe) {
applog(LOG_ERR, "DAG file header is bad %s!", fn);
close(fdDAG);
return NULL;
}
return (buffer+8);
}
void close_dag_file(uint8_t const *dag)
{
uint8_t* buffer = dag ? (uint8_t*) dag - 8 : NULL;
munmap(buffer, dagSize);
dag = NULL;
close(fdDAG);
fdDAG = -1;
}
static bool init[MAX_GPUS] = { 0 };
int scanhash_ether(int thr_id, struct work *work, uint64_t max_nonce, unsigned long *hashes_done)
{
uint32_t *pdata = work->data;
uint32_t *ptarget = work->target;
uint32_t *pseed = &work->data[8];
const uint64_t first_nonce = (((uint64_t)pdata[20]) << 32) + pdata[19];
uint64_t nonce = first_nonce;
uint32_t throughput = device_intensity(thr_id, __func__, 1U << 18);
throughput = min(throughput, max_nonce - first_nonce);
if (!dagSize) {
dag = alloc_dag_file(23, pseed);
applog(LOG_NOTICE, "DAG size=%ld", (long int) dagSize);
}
//0000.0000 0050.4c39c 047378ad262a995be73cc504643443728e1ca415bae9208
uint64_t target64 = ptarget[7];
target64 = (target64 << 32) + ptarget[6];
if (!init[thr_id])
{
uint32_t dag_size = (unsigned)(dagSize / ETHASH_MIX_BYTES);
uint32_t max_outputs=63;
cudaSetDevice(device_map[thr_id]);
// create buffer for header256
cudaMalloc(&m_header, 32);
// create mining buffer
cudaMalloc(&m_search_buf[thr_id], 8 * sizeof(uint32_t));
//cudaMalloc(&m_hash_buf[thr_id], c_hash_batch_size * 32);
//cudaStreamCreate(&m_streams[thr_id]);
m_streams[thr_id] = 0;
// create buffer for dag
CUDA_SAFE_CALL(cudaMalloc(&m_dag_ptr, dagSize));
CUDA_SAFE_CALL(cudaMemcpy(m_dag_ptr, dag, dagSize, cudaMemcpyHostToDevice));
CUDA_SAFE_CALL(set_constants(&dag_size, &max_outputs));
init[thr_id] = true;
}
else if (dag) {
close_dag_file(dag);
}
CUDA_SAFE_CALL(cudaMemcpy(m_header, work->data, 32, cudaMemcpyHostToDevice));
CUDA_SAFE_CALL(cudaMemcpy(m_search_buf[thr_id], &c_zero, 4, cudaMemcpyHostToDevice));
uint32_t results[8] = { 0 };
do {
results[0] = 0;
CUDA_SAFE_CALL(cudaMemset(m_search_buf[thr_id], 0, sizeof(uint32_t)));
run_ethash_search(m_search_batch_size / m_workgroup_size, m_workgroup_size, m_streams[thr_id], m_search_buf[thr_id], m_header, m_dag_ptr, nonce, target64);
CUDA_SAFE_CALL(cudaThreadSynchronize());
CUDA_SAFE_CALL(cudaMemcpyAsync(results, m_search_buf[thr_id], 2*sizeof(uint32_t), cudaMemcpyDeviceToHost, m_streams[thr_id]));
unsigned num_found = results[0];
if (num_found) {
nonce = nonce + results[1];
pdata[19] = (uint32_t) nonce;
pdata[20] = (uint32_t) (nonce >> 32);
return 1;
}
nonce += throughput;
} while (nonce < max_nonce && !work_restart[thr_id].restart);
*hashes_done = nonce - first_nonce + 1;
return 0;
}
+59
View File
@@ -0,0 +1,59 @@
#ifndef _ETHASH_CU_MINER_KERNEL_H_
#define _ETHASH_CU_MINER_KERNEL_H_
#include <stdint.h>
typedef union
{
uint64_t uint64s[16 / sizeof(uint64_t)];
uint32_t uint32s[16 / sizeof(uint32_t)];
} hash16_t;
typedef union
{
uint32_t uint32s[32 / sizeof(uint32_t)];
uint64_t uint64s[32 / sizeof(uint64_t)];
} hash32_t;
typedef union
{
uint32_t uint32s[64 / sizeof(uint32_t)];
uint64_t uint64s[64 / sizeof(uint64_t)];
uint4 uint4s[64 / sizeof(uint4)];
} hash64_t;
typedef union
{
uint32_t uint32s[128 / sizeof(uint32_t)];
uint4 uint4s[128 / sizeof(uint4)];
} hash128_t;
//typedef uint32_t hash128_t;
cudaError set_constants(
uint32_t * dag_size,
uint32_t * max_outputs
);
void run_ethash_hash(
hash32_t* g_hashes,
hash32_t const* g_header,
hash128_t const* g_dag,
uint64_t start_nonce
);
void run_ethash_search(
uint32_t search_batch_size,
uint32_t workgroup_size,
cudaStream_t stream,
uint32_t* g_output,
hash32_t const* g_header,
hash128_t const* g_dag,
uint64_t start_nonce,
uint64_t target
);
#endif
+510
View File
@@ -0,0 +1,510 @@
#include "cpuminer-config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <unistd.h>
//#include <jansson.h>
#include <curl/curl.h>
#ifdef WIN32
#include <winsock2.h>
#include <mstcpip.h>
#else
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#endif
#include "miner.h"
struct data_buffer {
void *buf;
size_t len;
};
struct upload_buffer {
const void *buf;
size_t len;
size_t pos;
};
struct header_info {
char *lp_path;
char *reason;
char *stratum_url;
};
static void databuf_free(struct data_buffer *db)
{
if (!db)
return;
free(db->buf);
memset(db, 0, sizeof(*db));
}
static size_t all_data_cb(const void *ptr, size_t size, size_t nmemb,
void *user_data)
{
struct data_buffer *db = (struct data_buffer *)user_data;
size_t len = size * nmemb;
size_t oldlen, newlen;
void *newmem;
static const uchar zero = 0;
oldlen = db->len;
newlen = oldlen + len;
newmem = realloc(db->buf, newlen + 1);
if (!newmem)
return 0;
db->buf = newmem;
db->len = newlen;
memcpy((char*)db->buf + oldlen, ptr, len);
memcpy((char*)db->buf + newlen, &zero, 1); /* null terminate */
return len;
}
static size_t upload_data_cb(void *ptr, size_t size, size_t nmemb,
void *user_data)
{
struct upload_buffer *ub = (struct upload_buffer *)user_data;
unsigned int len = (unsigned int)(size * nmemb);
if (len > ub->len - ub->pos)
len = (unsigned int)(ub->len - ub->pos);
if (len) {
memcpy(ptr, (char*)ub->buf + ub->pos, len);
ub->pos += len;
}
return len;
}
#if LIBCURL_VERSION_NUM >= 0x071200
static int seek_data_cb(void *user_data, curl_off_t offset, int origin)
{
struct upload_buffer *ub = (struct upload_buffer *)user_data;
switch (origin) {
case SEEK_SET:
ub->pos = (size_t)offset;
break;
case SEEK_CUR:
ub->pos += (size_t)offset;
break;
case SEEK_END:
ub->pos = ub->len + (size_t)offset;
break;
default:
return 1; /* CURL_SEEKFUNC_FAIL */
}
return 0; /* CURL_SEEKFUNC_OK */
}
#endif
static size_t resp_hdr_cb(void *ptr, size_t size, size_t nmemb, void *user_data)
{
struct header_info *hi = (struct header_info *)user_data;
size_t remlen, slen, ptrlen = size * nmemb;
char *rem, *val = NULL, *key = NULL;
void *tmp;
val = (char*)calloc(1, ptrlen);
key = (char*)calloc(1, ptrlen);
if (!key || !val)
goto out;
tmp = memchr(ptr, ':', ptrlen);
if (!tmp || (tmp == ptr)) /* skip empty keys / blanks */
goto out;
slen = (size_t)((char*)tmp - (char*)ptr);
if ((slen + 1) == ptrlen) /* skip key w/ no value */
goto out;
memcpy(key, ptr, slen); /* store & nul term key */
key[slen] = 0;
rem = (char*)ptr + slen + 1; /* trim value's leading whitespace */
remlen = ptrlen - slen - 1;
while ((remlen > 0) && (isspace(*rem))) {
remlen--;
rem++;
}
memcpy(val, rem, remlen); /* store value, trim trailing ws */
val[remlen] = 0;
while ((*val) && (isspace(val[strlen(val) - 1]))) {
val[strlen(val) - 1] = 0;
}
if (!*val) /* skip blank value */
goto out;
if (!strcasecmp("X-Long-Polling", key)) {
hi->lp_path = val; /* X-Mining-Extensions: longpoll */
val = NULL;
}
if (!strcasecmp("X-Reject-Reason", key)) {
hi->reason = val; /* X-Mining-Extensions: reject-reason */
//applog(LOG_WARNING, "%s:%s", key, val);
val = NULL;
}
if (!strcasecmp("X-Stratum", key)) {
hi->stratum_url = val; /* steal memory reference */
val = NULL;
}
if (!strcasecmp("X-Nonce-Range", key)) {
/* todo when available: X-Mining-Extensions: noncerange */
}
out:
free(key);
free(val);
return ptrlen;
}
#if LIBCURL_VERSION_NUM >= 0x070f06
static int sockopt_keepalive_cb(void *userdata, curl_socket_t fd,
curlsocktype purpose)
{
int keepalive = 1;
int tcp_keepcnt = 3;
int tcp_keepidle = 50;
int tcp_keepintvl = 50;
#ifdef WIN32
DWORD outputBytes;
#endif
#ifndef WIN32
if (unlikely(setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keepalive,
sizeof(keepalive))))
return 1;
#ifdef __linux
if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPCNT,
&tcp_keepcnt, sizeof(tcp_keepcnt))))
return 1;
if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPIDLE,
&tcp_keepidle, sizeof(tcp_keepidle))))
return 1;
if (unlikely(setsockopt(fd, SOL_TCP, TCP_KEEPINTVL,
&tcp_keepintvl, sizeof(tcp_keepintvl))))
return 1;
#endif /* __linux */
#ifdef __APPLE_CC__
if (unlikely(setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE,
&tcp_keepintvl, sizeof(tcp_keepintvl))))
return 1;
#endif /* __APPLE_CC__ */
#else /* WIN32 */
struct tcp_keepalive vals;
vals.onoff = 1;
vals.keepalivetime = tcp_keepidle * 1000;
vals.keepaliveinterval = tcp_keepintvl * 1000;
if (unlikely(WSAIoctl(fd, SIO_KEEPALIVE_VALS, &vals, sizeof(vals),
NULL, 0, &outputBytes, NULL, NULL)))
return 1;
#endif /* WIN32 */
return 0;
}
#endif
static json_t *ether_json_rpc(CURL *curl, const char *url, const char *rpc_req, int *curl_err)
{
char len_hdr[64], hashrate_hdr[64];
char curl_err_str[CURL_ERROR_SIZE] = { 0 };
struct data_buffer all_data = { 0 };
struct upload_buffer upload_data;
struct curl_slist *headers = NULL;
struct header_info hi = { 0 };
char *httpdata;
json_t *val, *err_val, *res_val;
json_error_t err;
int rc;
long timeout = opt_timeout;
bool keepalive = false;
/* it is assumed that 'curl' is freshly [re]initialized at this pt */
if (opt_protocol)
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_URL, url);
if (opt_cert)
curl_easy_setopt(curl, CURLOPT_CAINFO, opt_cert);
curl_easy_setopt(curl, CURLOPT_ENCODING, "");
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 0);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_TCP_NODELAY, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, all_data_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_data);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, upload_data_cb);
curl_easy_setopt(curl, CURLOPT_READDATA, &upload_data);
#if LIBCURL_VERSION_NUM >= 0x071200
curl_easy_setopt(curl, CURLOPT_SEEKFUNCTION, &seek_data_cb);
curl_easy_setopt(curl, CURLOPT_SEEKDATA, &upload_data);
#endif
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_err_str);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, resp_hdr_cb);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &hi);
if (opt_proxy) {
curl_easy_setopt(curl, CURLOPT_PROXY, opt_proxy);
curl_easy_setopt(curl, CURLOPT_PROXYTYPE, opt_proxy_type);
}
#if 0
if (userpass) {
curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
#endif
#if LIBCURL_VERSION_NUM >= 0x070f06
if (keepalive)
curl_easy_setopt(curl, CURLOPT_SOCKOPTFUNCTION, sockopt_keepalive_cb);
#endif
curl_easy_setopt(curl, CURLOPT_POST, 1);
if (opt_protocol)
applog(LOG_DEBUG, "JSON protocol request:\n%s", rpc_req);
upload_data.buf = rpc_req;
upload_data.len = strlen(rpc_req);
upload_data.pos = 0;
sprintf(len_hdr, "Content-Length: %lu", (unsigned long) upload_data.len);
sprintf(hashrate_hdr, "X-Mining-Hashrate: %llu", (unsigned long long) global_hashrate);
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, len_hdr);
headers = curl_slist_append(headers, "User-Agent: " USER_AGENT);
headers = curl_slist_append(headers, "X-Mining-Extensions: longpoll noncerange reject-reason");
headers = curl_slist_append(headers, hashrate_hdr);
headers = curl_slist_append(headers, "Accept:"); /* disable Accept hdr*/
headers = curl_slist_append(headers, "Expect:"); /* disable Expect hdr*/
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
rc = curl_easy_perform(curl);
if (curl_err != NULL)
*curl_err = rc;
if (rc) {
if (rc != CURLE_OPERATION_TIMEDOUT) {
applog(LOG_ERR, "HTTP request failed: %s", curl_err_str);
goto err_out;
}
}
if (!all_data.buf || !all_data.len) {
applog(LOG_ERR, "Empty data received in json_rpc_call.");
goto err_out;
}
httpdata = (char*) all_data.buf;
if (*httpdata != '{' && *httpdata != '[') {
long errcode = 0;
CURLcode c = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &errcode);
if (c == CURLE_OK && errcode == 401) {
applog(LOG_ERR, "You are not authorized, check your login and password.");
goto err_out;
}
}
val = JSON_LOADS(httpdata, &err);
if (!val) {
applog(LOG_ERR, "JSON decode failed(%d): %s", err.line, err.text);
if (opt_protocol)
applog(LOG_DEBUG, "%s", httpdata);
goto err_out;
}
if (opt_protocol) {
char *s = json_dumps(val, JSON_INDENT(3));
applog(LOG_DEBUG, "JSON protocol response:\n%s\n", s);
free(s);
}
/* JSON-RPC valid response returns a non-null 'result',
* and a null 'error'. */
res_val = json_object_get(val, "result");
err_val = json_object_get(val, "error");
if (!res_val || json_is_null(res_val) ||
(err_val && !json_is_null(err_val))) {
char *s = NULL;
if (err_val) {
s = json_dumps(err_val, 0);
json_t *msg = json_object_get(err_val, "message");
json_t *err_code = json_object_get(err_val, "code");
if (curl_err && json_integer_value(err_code))
*curl_err = (int) json_integer_value(err_code);
if (json_is_string(msg)) {
free(s);
s = strdup(json_string_value(msg));
if (have_longpoll && s && !strcmp(s, "method not getwork")) {
json_decref(err_val);
free(s);
goto err_out;
}
}
json_decref(err_val);
}
else
s = strdup("(unknown reason)");
if (!curl_err || opt_debug)
applog(LOG_ERR, "JSON-RPC call failed: %s", s);
free(s);
goto err_out;
}
if (hi.reason)
json_object_set_new(val, "reject-reason", json_string(hi.reason));
databuf_free(&all_data);
curl_slist_free_all(headers);
curl_easy_reset(curl);
return val;
err_out:
free(hi.lp_path);
free(hi.reason);
free(hi.stratum_url);
databuf_free(&all_data);
curl_slist_free_all(headers);
curl_easy_reset(curl);
return NULL;
}
// Made for len 32
bool hex2uint(uint32_t *target, const char *hexstr, int len)
{
char buf[32];
char pad[80];
char hex_byte[3];
char *ep;
char *p = buf;
char *hex = pad;
hex_byte[2] = '\0';
// pad, we can receive non padded numbers (0x44 => 0x0044)
size_t hexlen = strlen(hexstr);
if (hexlen < len*2)
memset(hex, '0', len*2 - hexlen);
sprintf(&hex[len*2 - hexlen],"%s", hexstr);
while (*hex && len) {
if (!hex[1]) {
applog(LOG_ERR, "hex2uint str truncated");
return false;
}
hex_byte[0] = hex[0];
hex_byte[1] = hex[1];
*p = (uchar) strtol(hex_byte, &ep, 16);
if (*ep) {
applog(LOG_ERR, "hex2uint failed on '%s'", hex_byte);
return false;
}
p++;
hex += 2;
len--;
if (len % 8 == 0) {
//..
}
}
for (int i = 0; i < 8; i++) {
be32enc(&target[7 - i], ((uint32_t*)buf)[i]);
}
return (len == 0 && *hexstr == 0) ? true : false;
}
void uint2hex(char *out, uint32_t *in, int len)
{
if (out) {
for (int i = 0; i < len/sizeof(uint32_t); i++)
sprintf(out + (i * 8), "%08x", in[7-i]);
}
}
bool ether_getwork(CURL *curl, struct pool_infos *pool, struct work *work)
{
char req[512];
bool ret = false;
sprintf(req, "{ \"jsonrpc\": \"2.0\", \"method\": \"eth_getWork\", \"params\": [], \"id\": 4 }\r\n");
json_t *result = ether_json_rpc(curl, pool->url, req, NULL);
if (result)
ret = true;
json_t *arr = json_object_get(result, "result");
if (!json_is_array(arr)) {
return false;
}
const char *header = json_string_value(json_array_get(arr, 0));
const char *seed = json_string_value(json_array_get(arr, 1));
const char *target = json_string_value(json_array_get(arr, 2));
//0x2b800e3ffcafdf7ffdd5b2ea1c0ed79c534e92b3459331eae61597943d0743bc
//0x356e5a2cc1eba076e650ac7473fccc37952b46bc2e419a200cec0c451dce2336
//0x000000000050ec7dea051867e987e3eed0066a128f161b5776a8f5da326b06ec
applog(LOG_NOTICE, "%s %s %s", header, seed, target);
hex2uint(work->data, &header[2], 32);
hex2uint(&work->data[8], &seed[2], 32);
hex2uint(work->target, &target[2], 32);
//applog(LOG_BLUE, "%08x %08x", work->target[7], work->target[6]);
return ret;
}
bool ether_submitwork(CURL *curl, struct pool_infos *pool, struct work *work)
{
char req[512];
bool ret = false;
char noncehex[32]={0}, header[80]={0}, mixhash[80]={0};
sprintf(noncehex, "0x%08x%08x", work->data[20], work->data[19]);
uint2hex(header, work->data, 32);
uint2hex(mixhash, &work->data[24], 32);
applog(LOG_NOTICE, "%s mixhash:", __func__);
applog_hash((uchar*)&work->data[24]);
sprintf(req, "{ \"jsonrpc\": \"2.0\", \"method\": \"eth_submitWork\","
"\"params\": [\"%s\", \"0x%s\", \"0x%s\"], \"id\": 4 }\r\n",
noncehex, header, mixhash
);
json_t *result = ether_json_rpc(curl, pool->url, req, NULL);
if (result)
ret = true;
json_t *res = json_object_get(result, "result");
if (!json_is_true(res)) {
applog(LOG_ERR, "not true! %s", json_dumps(result, JSON_INDENT(3)));
exit(0);
return false;
}
return ret;
}
+83
View File
@@ -0,0 +1,83 @@
__device__ __constant__ uint64_t const keccak_round_constants[24] = {
0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808AULL,
0x8000000080008000ULL, 0x000000000000808BULL, 0x0000000080000001ULL,
0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008AULL,
0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000AULL,
0x000000008000808BULL, 0x800000000000008BULL, 0x8000000000008089ULL,
0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL,
0x000000000000800AULL, 0x800000008000000AULL, 0x8000000080008081ULL,
0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL
};
__device__ static void keccak_f1600_block(uint64_t* s, uint32_t out_size)//, uint32_t in_size, uint32_t out_size)
{
uint64_t t[5], u, v;
for (size_t i = 0; i < 24; i++) {
/* theta: c = a[0,i] ^ a[1,i] ^ .. a[4,i] */
t[0] = s[0] ^ s[5] ^ s[10] ^ s[15] ^ s[20];
t[1] = s[1] ^ s[6] ^ s[11] ^ s[16] ^ s[21];
t[2] = s[2] ^ s[7] ^ s[12] ^ s[17] ^ s[22];
t[3] = s[3] ^ s[8] ^ s[13] ^ s[18] ^ s[23];
t[4] = s[4] ^ s[9] ^ s[14] ^ s[19] ^ s[24];
/* theta: d[i] = c[i+4] ^ rotl(c[i+1],1) */
/* theta: a[0,i], a[1,i], .. a[4,i] ^= d[i] */
u = t[4] ^ ROTL64L(t[1], 1);
s[0] ^= u; s[5] ^= u; s[10] ^= u; s[15] ^= u; s[20] ^= u;
u = t[0] ^ ROTL64L(t[2], 1);
s[1] ^= u; s[6] ^= u; s[11] ^= u; s[16] ^= u; s[21] ^= u;
u = t[1] ^ ROTL64L(t[3], 1);
s[2] ^= u; s[7] ^= u; s[12] ^= u; s[17] ^= u; s[22] ^= u;
u = t[2] ^ ROTL64L(t[4], 1);
s[3] ^= u; s[8] ^= u; s[13] ^= u; s[18] ^= u; s[23] ^= u;
u = t[3] ^ ROTL64L(t[0], 1);
s[4] ^= u; s[9] ^= u; s[14] ^= u; s[19] ^= u; s[24] ^= u;
/* rho pi: b[..] = rotl(a[..], ..) */
u = s[1];
s[1] = ROTL64H(s[6], 44);
s[6] = ROTL64L(s[9], 20);
s[9] = ROTL64H(s[22], 61);
s[22] = ROTL64H(s[14], 39);
s[14] = ROTL64L(s[20], 18);
s[20] = ROTL64H(s[2], 62);
s[2] = ROTL64H(s[12], 43);
s[12] = ROTL64L(s[13], 25);
s[13] = ROTL64L(s[19], 8);
s[19] = ROTL64H(s[23], 56);
s[23] = ROTL64H(s[15], 41);
s[15] = ROTL64L(s[4], 27);
s[4] = ROTL64L(s[24], 14);
s[24] = ROTL64L(s[21], 2);
s[21] = ROTL64H(s[8], 55);
s[8] = ROTL64H(s[16], 45);
s[16] = ROTL64H(s[5], 36);
s[5] = ROTL64L(s[3], 28);
s[3] = ROTL64L(s[18], 21);
s[18] = ROTL64L(s[17], 15);
s[17] = ROTL64L(s[11], 10);
s[11] = ROTL64L(s[7], 6);
s[7] = ROTL64L(s[10], 3);
s[10] = ROTL64L(u, 1);
// squeeze this in here
/* chi: a[i,j] ^= ~b[i,j+1] & b[i,j+2] */
u = s[0]; v = s[1]; s[0] ^= (~v) & s[2];
/* iota: a[0,0] ^= round constant */
s[0] ^= keccak_round_constants[i];
if (i == 23 && out_size == 1) return;
// continue chi
s[1] ^= (~s[2]) & s[3]; s[2] ^= (~s[3]) & s[4]; s[3] ^= (~s[4]) & u; s[4] ^= (~u) & v;
u = s[5]; v = s[6]; s[5] ^= (~v) & s[7]; s[6] ^= (~s[7]) & s[8]; s[7] ^= (~s[8]) & s[9];
if (i == 23) return;
s[8] ^= (~s[9]) & u; s[9] ^= (~u) & v;
u = s[10]; v = s[11]; s[10] ^= (~v) & s[12]; s[11] ^= (~s[12]) & s[13]; s[12] ^= (~s[13]) & s[14]; s[13] ^= (~s[14]) & u; s[14] ^= (~u) & v;
u = s[15]; v = s[16]; s[15] ^= (~v) & s[17]; s[16] ^= (~s[17]) & s[18]; s[17] ^= (~s[18]) & s[19]; s[18] ^= (~s[19]) & u; s[19] ^= (~u) & v;
u = s[20]; v = s[21]; s[20] ^= (~v) & s[22]; s[21] ^= (~s[22]) & s[23]; s[22] ^= (~s[23]) & s[24]; s[23] ^= (~s[24]) & u; s[24] ^= (~u) & v;
}
}
+64
View File
@@ -0,0 +1,64 @@
#include <cuda.h>
#include <cuda_runtime.h>
#include <stdint.h>
// 64-bit ROTATE LEFT
#if __CUDA_ARCH__ >= 320
__device__ uint64_t ROTL64H(const uint64_t x, const int offset)
{
uint64_t res;
asm("{\n\t"
".reg .u32 tl,th,vl,vh;\n\t"
"mov.b64 {tl,th}, %1;\n\t"
"shf.l.wrap.b32 vl, tl, th, %2;\n\t"
"shf.l.wrap.b32 vh, th, tl, %2;\n\t"
"mov.b64 %0, {vl,vh};\n\t"
"}"
: "=l"(res) : "l"(x), "r"(offset)
);
return res;
}
__device__ uint64_t ROTL64L(const uint64_t x, const int offset)
{
uint64_t res;
asm("{\n\t"
".reg .u32 tl,th,vl,vh;\n\t"
"mov.b64 {tl,th}, %1;\n\t"
"shf.l.wrap.b32 vl, tl, th, %2;\n\t"
"shf.l.wrap.b32 vh, th, tl, %2;\n\t"
"mov.b64 %0, {vh,vl};\n\t"
"}"
: "=l"(res) : "l"(x), "r"(offset)
);
return res;
}
#elif __CUDA_ARCH__ >= 120
#define ROTL64H(x, n) ROTL64(x,n)
#define ROTL64L(x, n) ROTL64(x,n)
__device__ __forceinline__
uint64_t ROTL64(const uint64_t x, const int offset)
{
uint64_t result;
asm("{\n\t"
".reg .b64 lhs;\n\t"
".reg .u32 roff;\n\t"
"shl.b64 lhs, %1, %2;\n\t"
"sub.u32 roff, 64, %2;\n\t"
"shr.b64 %0, %1, roff;\n\t"
"add.u64 %0, lhs, %0;\n\t"
"}\n"
: "=l"(result) : "l"(x), "r"(offset));
return result;
}
#else
#define ROTL64H(x, n) ROTL64(x,n)
#define ROTL64L(x, n) ROTL64(x,n)
/* host */
#define ROTL64(x, n) (((x) << (n)) | ((x) >> (64 - (n))))
#endif
+7
View File
@@ -381,6 +381,9 @@ extern int scanhash_whirlpoolx(int thr_id, uint32_t *pdata,
extern int scanhash_zr5(int thr_id, struct work *work, uint32_t max_nonce,
unsigned long *hashes_done);
extern int scanhash_ether(int thr_id, struct work *work, uint64_t max_nonce,
unsigned long *hashes_done);
/* api related */
void *api_thread(void *userdata);
void api_set_throughput(int thr_id, uint32_t throughput);
@@ -659,6 +662,7 @@ struct pool_infos {
#define POOL_GETWORK 1
#define POOL_STRATUM 2
#define POOL_LONGPOLL 4
#define POOL_ETHER 128
uint8_t type;
#define POOL_ST_DEFINED 1
#define POOL_ST_VALID 2
@@ -711,6 +715,9 @@ json_t * json_rpc_call_pool(CURL *curl, struct pool_infos*,
json_t * json_rpc_longpoll(CURL *curl, char *lp_url, struct pool_infos*,
const char *req, int *err);
bool ether_getwork(CURL *curl, struct pool_infos *pool, struct work *work);
bool ether_submitwork(CURL *curl, struct pool_infos *pool, struct work *work);
bool stratum_socket_full(struct stratum_ctx *sctx, int timeout);
bool stratum_send_line(struct stratum_ctx *sctx, char *s);
char *stratum_recv_line(struct stratum_ctx *sctx);