1 #ifndef __MINER_H__
2 #define __MINER_H__
3
4 #include "cpuminer-config.h"
5
6 #include <stdbool.h>
7 #include <inttypes.h>
8 #include <sys/time.h>
9 #include <pthread.h>
10 #include <jansson.h>
11 #include <curl/curl.h>
12
13 #ifdef STDC_HEADERS
14 # include <stdlib.h>
15 # include <stddef.h>
16 #else
17 # ifdef HAVE_STDLIB_H
18 # include <stdlib.h>
19 # endif
20 #endif
21 #ifdef HAVE_ALLOCA_H
22 # include <alloca.h>
23 #elif !defined alloca
24 # ifdef __GNUC__
25 # define alloca __builtin_alloca
26 # elif defined _AIX
27 # define alloca __alloca
28 # elif defined _MSC_VER
29 # include <malloc.h>
30 # define alloca _alloca
31 # elif !defined HAVE_ALLOCA
32 # ifdef __cplusplus
33 extern "C"
34 # endif
35 void *alloca (size_t);
36 # endif
37 #endif
38
39 #ifdef HAVE_SYSLOG_H
40 #include <syslog.h>
41 #else
42 enum {
43 LOG_ERR,
44 LOG_WARNING,
45 LOG_NOTICE,
46 LOG_INFO,
47 LOG_DEBUG,
48 };
49 #endif
50
51 #undef unlikely
52 #undef likely
53 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
54 #define unlikely(expr) (__builtin_expect(!!(expr), 0))
55 #define likely(expr) (__builtin_expect(!!(expr), 1))
56 #else
57 #define unlikely(expr) (expr)
58 #define likely(expr) (expr)
59 #endif
60
61 #ifndef ARRAY_SIZE
62 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
63 #endif
64
65 #if ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
66 #define WANT_BUILTIN_BSWAP
67 #else
68 #define bswap_32(x) ((((x) << 24) & 0xff000000u) | (((x) << 8) & 0x00ff0000u) \
69 | (((x) >> 8) & 0x0000ff00u) | (((x) >> 24) & 0x000000ffu))
70 #endif
71
swab32(uint32_t v)72 static inline uint32_t swab32(uint32_t v)
73 {
74 #ifdef WANT_BUILTIN_BSWAP
75 return __builtin_bswap32(v);
76 #else
77 return bswap_32(v);
78 #endif
79 }
80
81 #ifdef HAVE_SYS_ENDIAN_H
82 #include <sys/endian.h>
83 #endif
84
85 #if !HAVE_DECL_BE32DEC
be32dec(const void * pp)86 static inline uint32_t be32dec(const void *pp)
87 {
88 const uint8_t *p = (uint8_t const *)pp;
89 return ((uint32_t)(p[3]) + ((uint32_t)(p[2]) << 8) +
90 ((uint32_t)(p[1]) << 16) + ((uint32_t)(p[0]) << 24));
91 }
92 #endif
93
94 #if !HAVE_DECL_LE32DEC
le32dec(const void * pp)95 static inline uint32_t le32dec(const void *pp)
96 {
97 const uint8_t *p = (uint8_t const *)pp;
98 return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) +
99 ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24));
100 }
101 #endif
102
103 #if !HAVE_DECL_BE32ENC
be32enc(void * pp,uint32_t x)104 static inline void be32enc(void *pp, uint32_t x)
105 {
106 uint8_t *p = (uint8_t *)pp;
107 p[3] = x & 0xff;
108 p[2] = (x >> 8) & 0xff;
109 p[1] = (x >> 16) & 0xff;
110 p[0] = (x >> 24) & 0xff;
111 }
112 #endif
113
114 #if !HAVE_DECL_LE32ENC
le32enc(void * pp,uint32_t x)115 static inline void le32enc(void *pp, uint32_t x)
116 {
117 uint8_t *p = (uint8_t *)pp;
118 p[0] = x & 0xff;
119 p[1] = (x >> 8) & 0xff;
120 p[2] = (x >> 16) & 0xff;
121 p[3] = (x >> 24) & 0xff;
122 }
123 #endif
124
125 #if JANSSON_MAJOR_VERSION >= 2
126 #define JSON_LOADS(str, err_ptr) json_loads(str, 0, err_ptr)
127 #define JSON_LOAD_FILE(path, err_ptr) json_load_file(path, 0, err_ptr)
128 #else
129 #define JSON_LOADS(str, err_ptr) json_loads(str, err_ptr)
130 #define JSON_LOAD_FILE(path, err_ptr) json_load_file(path, err_ptr)
131 #endif
132
133 #define USER_AGENT PACKAGE_NAME "/" PACKAGE_VERSION
134
135 void sha256_init(uint32_t *state);
136 void sha256_transform(uint32_t *state, const uint32_t *block, int swap);
137 void sha256d(unsigned char *hash, const unsigned char *data, int len);
138
139 #ifdef USE_ASM
140 #if defined(__ARM_NEON__) || defined(__ALTIVEC__) || defined(__i386__) || defined(__x86_64__)
141 #define HAVE_SHA256_4WAY 1
142 int sha256_use_4way();
143 void sha256_init_4way(uint32_t *state);
144 void sha256_transform_4way(uint32_t *state, const uint32_t *block, int swap);
145 #endif
146 #if defined(__x86_64__) && defined(USE_AVX2)
147 #define HAVE_SHA256_8WAY 1
148 int sha256_use_8way();
149 void sha256_init_8way(uint32_t *state);
150 void sha256_transform_8way(uint32_t *state, const uint32_t *block, int swap);
151 #endif
152 #endif
153
154 extern int scanhash_sha256d(int thr_id, uint32_t *pdata,
155 const uint32_t *ptarget, uint32_t max_nonce, unsigned long *hashes_done);
156
157 extern unsigned char *scrypt_buffer_alloc(int N);
158 extern int scanhash_scrypt(int thr_id, uint32_t *pdata,
159 unsigned char *scratchbuf, const uint32_t *ptarget,
160 uint32_t max_nonce, unsigned long *hashes_done, int N);
161
162 struct thr_info {
163 int id;
164 pthread_t pth;
165 struct thread_q *q;
166 };
167
168 struct work_restart {
169 volatile unsigned long restart;
170 char padding[128 - sizeof(unsigned long)];
171 };
172
173 extern bool opt_debug;
174 extern bool opt_protocol;
175 extern bool opt_redirect;
176 extern int opt_timeout;
177 extern bool want_longpoll;
178 extern bool have_longpoll;
179 extern bool have_gbt;
180 extern bool allow_getwork;
181 extern bool want_stratum;
182 extern bool have_stratum;
183 extern char *opt_cert;
184 extern char *opt_proxy;
185 extern long opt_proxy_type;
186 extern bool use_syslog;
187 extern pthread_mutex_t applog_lock;
188 extern struct thr_info *thr_info;
189 extern int longpoll_thr_id;
190 extern int stratum_thr_id;
191 extern struct work_restart *work_restart;
192
193 #define JSON_RPC_LONGPOLL (1 << 0)
194 #define JSON_RPC_QUIET_404 (1 << 1)
195
196 extern void applog(int prio, const char *fmt, ...);
197 extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
198 const char *rpc_req, int *curl_err, int flags);
199 void memrev(unsigned char *p, size_t len);
200 extern void bin2hex(char *s, const unsigned char *p, size_t len);
201 extern char *abin2hex(const unsigned char *p, size_t len);
202 extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
203 extern int varint_encode(unsigned char *p, uint64_t n);
204 extern size_t address_to_script(unsigned char *out, size_t outsz, const char *addr);
205 extern int timeval_subtract(struct timeval *result, struct timeval *x,
206 struct timeval *y);
207 extern bool fulltest(const uint32_t *hash, const uint32_t *target);
208 extern void diff_to_target(uint32_t *target, double diff);
209
210 struct stratum_job {
211 char *job_id;
212 unsigned char prevhash[32];
213 size_t coinbase_size;
214 unsigned char *coinbase;
215 unsigned char *xnonce2;
216 int merkle_count;
217 unsigned char **merkle;
218 unsigned char version[4];
219 unsigned char nbits[4];
220 unsigned char ntime[4];
221 bool clean;
222 double diff;
223 };
224
225 struct stratum_ctx {
226 char *url;
227
228 CURL *curl;
229 char *curl_url;
230 char curl_err_str[CURL_ERROR_SIZE];
231 curl_socket_t sock;
232 size_t sockbuf_size;
233 char *sockbuf;
234 pthread_mutex_t sock_lock;
235
236 double next_diff;
237
238 char *session_id;
239 size_t xnonce1_size;
240 unsigned char *xnonce1;
241 size_t xnonce2_size;
242 struct stratum_job job;
243 pthread_mutex_t work_lock;
244 };
245
246 bool stratum_socket_full(struct stratum_ctx *sctx, int timeout);
247 bool stratum_send_line(struct stratum_ctx *sctx, char *s);
248 char *stratum_recv_line(struct stratum_ctx *sctx);
249 bool stratum_connect(struct stratum_ctx *sctx, const char *url);
250 void stratum_disconnect(struct stratum_ctx *sctx);
251 bool stratum_subscribe(struct stratum_ctx *sctx);
252 bool stratum_authorize(struct stratum_ctx *sctx, const char *user, const char *pass);
253 bool stratum_handle_method(struct stratum_ctx *sctx, const char *s);
254
255 struct thread_q;
256
257 extern struct thread_q *tq_new(void);
258 extern void tq_free(struct thread_q *tq);
259 extern bool tq_push(struct thread_q *tq, void *data);
260 extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
261 extern void tq_freeze(struct thread_q *tq);
262 extern void tq_thaw(struct thread_q *tq);
263
264 #endif /* __MINER_H__ */
265