37
37
#define ETHASH_DATASET_PARENTS 256
38
38
#define ETHASH_CACHE_ROUNDS 3
39
39
#define ETHASH_ACCESSES 64
40
+ #define ETHASH_DAG_MAGIC_NUM_SIZE 8
41
+ #define ETHASH_DAG_MAGIC_NUM 0xFEE1DEADBADDCAFE
40
42
41
43
#ifdef __cplusplus
42
44
extern "C" {
43
45
#endif
44
46
45
- // LTODO: for consistency's sake maybe use ethash_params_t?
46
- typedef struct ethash_params {
47
- /// Size of full data set (in bytes, multiple of mix size (128)).
48
- uint64_t full_size ;
49
- /// Size of compute cache (in bytes, multiple of node size (64)).
50
- uint64_t cache_size ;
51
- } ethash_params ;
52
-
53
47
/// Type of a seedhash/blockhash e.t.c.
54
48
typedef struct ethash_h256 { uint8_t b [32 ]; } ethash_h256_t ;
55
- static inline uint8_t ethash_h256_get (ethash_h256_t const * hash , unsigned int i )
56
- {
57
- return hash -> b [i ];
58
- }
59
-
60
- static inline void ethash_h256_set (ethash_h256_t * hash , unsigned int i , uint8_t v )
61
- {
62
- hash -> b [i ] = v ;
63
- }
64
-
65
- static inline void ethash_h256_reset (ethash_h256_t * hash )
66
- {
67
- memset (hash , 0 , 32 );
68
- }
69
49
70
50
// convenience macro to statically initialize an h256_t
71
51
// usage:
@@ -81,55 +61,20 @@ struct ethash_full;
81
61
typedef struct ethash_full * ethash_full_t ;
82
62
typedef int (* ethash_callback_t )(unsigned );
83
63
84
- // LTODO: for consistency's sake maybe use ethash_return_value_t?
85
64
typedef struct ethash_return_value {
86
65
ethash_h256_t result ;
87
66
ethash_h256_t mix_hash ;
88
- } ethash_return_value ;
89
-
90
- uint64_t ethash_get_datasize (uint32_t const block_number );
91
- uint64_t ethash_get_cachesize (uint32_t const block_number );
92
-
93
- // initialize the parameters
94
- static inline void ethash_params_init (ethash_params * params , uint32_t const block_number )
95
- {
96
- params -> full_size = ethash_get_datasize (block_number );
97
- params -> cache_size = ethash_get_cachesize (block_number );
98
- }
99
-
100
- // LTODO: for consistency's sake maybe use ethash_cache_t?
101
- typedef struct ethash_cache {
102
- void * mem ;
103
- } ethash_cache ;
104
-
105
- /**
106
- * Allocate and initialize a new ethash_cache object
107
- *
108
- * @param params The parameters to initialize it with. We are interested in
109
- * the cache_size from here
110
- * @param seed Block seedhash to be used during the computation of the
111
- * cache nodes
112
- * @return Newly allocated ethash_cache on success or NULL in case of
113
- * ERRNOMEM or invalid parameters used for @ref ethash_compute_cache_nodes()
114
- */
115
- ethash_cache * ethash_cache_new (ethash_params const * params , ethash_h256_t const * seed );
116
- /**
117
- * Frees a previously allocated ethash_cache
118
- * @param c The object to free
119
- */
120
- void ethash_cache_delete (ethash_cache * c );
67
+ bool success ;
68
+ } ethash_return_value_t ;
121
69
122
70
/**
123
71
* Allocate and initialize a new ethash_light handler
124
72
*
125
- * @param params The parameters to initialize it with. We are interested in
126
- * the cache_size from here
127
- * @param seed Block seedhash to be used during the computation of the
128
- * cache nodes
129
- * @return Newly allocated ethash_light handler or NULL in case of
130
- * ERRNOMEM or invalid parameters used for @ref ethash_compute_cache_nodes()
73
+ * @param block_number The block number for which to create the handler
74
+ * @return Newly allocated ethash_light handler or NULL in case of
75
+ * ERRNOMEM or invalid parameters used for @ref ethash_compute_cache_nodes()
131
76
*/
132
- ethash_light_t ethash_light_new (ethash_params const * params , ethash_h256_t const * seed );
77
+ ethash_light_t ethash_light_new (uint64_t block_number );
133
78
/**
134
79
* Frees a previously allocated ethash_light handler
135
80
* @param light The light handler to free
@@ -138,63 +83,33 @@ void ethash_light_delete(ethash_light_t light);
138
83
/**
139
84
* Calculate the light client data
140
85
*
141
- * @param ret An object of ethash_return_value to hold the return value
142
86
* @param light The light client handler
143
- * @param params The parameters to use
144
87
* @param header_hash The header hash to pack into the mix
145
88
* @param nonce The nonce to pack into the mix
146
- * @return true if all went well and false if there were invalid
147
- * parameters given.
89
+ * @return an object of ethash_return_value_t holding the return values
148
90
*/
149
- bool ethash_light_compute (
150
- ethash_return_value * ret ,
91
+ ethash_return_value_t ethash_light_compute (
151
92
ethash_light_t light ,
152
- ethash_params const * params ,
153
- const ethash_h256_t * header_hash ,
154
- uint64_t const nonce
93
+ ethash_h256_t const header_hash ,
94
+ uint64_t nonce
155
95
);
156
- /**
157
- * Get a pointer to the cache object held by the light client
158
- *
159
- * @param light The light client whose cache to request
160
- * @return A pointer to the cache held by the light client or NULL if
161
- * there was no cache in the first place
162
- */
163
- ethash_cache * ethash_light_get_cache (ethash_light_t light );
164
- /**
165
- * Move the memory ownership of the cache somewhere else
166
- *
167
- * @param light The light client whose cache's memory ownership to acquire.
168
- * After this function concludes it will no longer have a cache.
169
- * @return A pointer to the moved cache or NULL if there was no cache in the first place
170
- */
171
- ethash_cache * ethash_light_acquire_cache (ethash_light_t light );
172
96
173
97
/**
174
98
* Allocate and initialize a new ethash_full handler
175
99
*
176
- * @param dirname The directory in which to put the DAG file.
177
- * @param seedhash The seed hash of the block. Used in the DAG file naming.
178
- * @param params The parameters to initialize it with. We are interested in
179
- * the full_size from here
180
- * @param cache A cache object to use that was allocated with @ref ethash_cache_new().
181
- * Iff this function succeeds the ethash_full_t will take memory
182
- * ownership of the cache and free it at deletion. If not then the user
183
- * still has to handle freeing of the cache himself.
184
- * @param callback A callback function with signature of @ref ethash_callback_t
185
- * It accepts an unsigned with which a progress of DAG calculation
186
- * can be displayed. If all goes well the callback should return 0.
187
- * If a non-zero value is returned then DAG generation will stop.
188
- * @return Newly allocated ethash_full handler or NULL in case of
189
- * ERRNOMEM or invalid parameters used for @ref ethash_compute_full_data()
100
+ * @param light The light handler containing the cache.
101
+ * @param callback A callback function with signature of @ref ethash_callback_t
102
+ * It accepts an unsigned with which a progress of DAG calculation
103
+ * can be displayed. If all goes well the callback should return 0.
104
+ * If a non-zero value is returned then DAG generation will stop.
105
+ * Be advised. A progress value of 100 means that DAG creation is
106
+ * almost complete and that this function will soon return succesfully.
107
+ * It does not mean that the function has already had a succesfull return.
108
+ * @return Newly allocated ethash_full handler or NULL in case of
109
+ * ERRNOMEM or invalid parameters used for @ref ethash_compute_full_data()
190
110
*/
191
- ethash_full_t ethash_full_new (
192
- char const * dirname ,
193
- ethash_h256_t const * seed_hash ,
194
- ethash_params const * params ,
195
- ethash_cache const * cache ,
196
- ethash_callback_t callback
197
- );
111
+ ethash_full_t ethash_full_new (ethash_light_t light , ethash_callback_t callback );
112
+
198
113
/**
199
114
* Frees a previously allocated ethash_full handler
200
115
* @param full The light handler to free
@@ -203,97 +118,29 @@ void ethash_full_delete(ethash_full_t full);
203
118
/**
204
119
* Calculate the full client data
205
120
*
206
- * @param ret An object of ethash_return_value to hold the return value
207
121
* @param full The full client handler
208
- * @param params The parameters to use
209
122
* @param header_hash The header hash to pack into the mix
210
123
* @param nonce The nonce to pack into the mix
211
- * @return true if all went well and false if there were invalid
212
- * parameters given or if there was a callback given and
213
- * at some point return a non-zero value
124
+ * @return An object of ethash_return_value to hold the return value
214
125
*/
215
- bool ethash_full_compute (
216
- ethash_return_value * ret ,
126
+ ethash_return_value_t ethash_full_compute (
217
127
ethash_full_t full ,
218
- ethash_params const * params ,
219
- ethash_h256_t const * header_hash ,
220
- uint64_t const nonce
128
+ ethash_h256_t const header_hash ,
129
+ uint64_t nonce
221
130
);
222
131
/**
223
- * Get a pointer to the cache object held by the full client
224
- *
225
- * @param full The full client whose cache to request
226
- * @return A pointer to the cache held by the full client or NULL
227
- * if there was no cache in the first place
132
+ * Get a pointer to the full DAG data
228
133
*/
229
- ethash_cache * ethash_full_get_cache (ethash_full_t full );
134
+ void const * ethash_full_dag (ethash_full_t full );
230
135
/**
231
- * Move the memory ownership of the cache somewhere else
232
- *
233
- * @param full The full client whose cache's memory ownership to acquire.
234
- * After this function concludes it will no longer have a cache.
235
- * @return A pointer to the moved cache or NULL if there was no cache in the first place
136
+ * Get the size of the DAG data
236
137
*/
237
- ethash_cache * ethash_full_acquire_cache (ethash_full_t full );
238
-
239
- void ethash_get_seedhash (ethash_h256_t * seedhash , const uint32_t block_number );
240
-
241
- // Returns if hash is less than or equal to difficulty
242
- static inline int ethash_check_difficulty (
243
- ethash_h256_t const * hash ,
244
- ethash_h256_t const * difficulty
245
- )
246
- {
247
- // Difficulty is big endian
248
- for (int i = 0 ; i < 32 ; i ++ ) {
249
- if (ethash_h256_get (hash , i ) == ethash_h256_get (difficulty , i )) {
250
- continue ;
251
- }
252
- return ethash_h256_get (hash , i ) < ethash_h256_get (difficulty , i );
253
- }
254
- return 1 ;
255
- }
256
-
257
- int ethash_quick_check_difficulty (
258
- ethash_h256_t const * header_hash ,
259
- uint64_t const nonce ,
260
- ethash_h256_t const * mix_hash ,
261
- ethash_h256_t const * difficulty
262
- );
263
-
138
+ uint64_t ethash_full_dag_size (ethash_full_t full );
264
139
265
140
/**
266
- * =========================
267
- * = DEPRECATED API =
268
- * =========================
269
- *
270
- * Kept for backwards compatibility with whoever still uses it. Please consider
271
- * switching to the new API (look above)
272
- */
273
- void ethash_mkcache (ethash_cache * cache , ethash_params const * params , ethash_h256_t const * seed );
274
- void ethash_full (
275
- ethash_return_value * ret ,
276
- void const * full_mem ,
277
- ethash_params const * params ,
278
- ethash_h256_t const * header_hash ,
279
- uint64_t const nonce
280
- );
281
- void ethash_light (
282
- ethash_return_value * ret ,
283
- ethash_cache const * cache ,
284
- ethash_params const * params ,
285
- ethash_h256_t const * header_hash ,
286
- uint64_t const nonce
287
- );
288
- /**
289
- * Compute the memory data for a full node's memory
290
- *
291
- * @param mem A pointer to an ethash full's memory
292
- * @param params The parameters to compute the data with
293
- * @param cache A cache object to use in the calculation
294
- * @return true if all went fine and false for invalid parameters
141
+ * Calculate the seedhash for a given block number
295
142
*/
296
- bool ethash_compute_full_data ( void * mem , ethash_params const * params , ethash_cache const * cache );
143
+ ethash_h256_t ethash_get_seedhash ( uint64_t block_number );
297
144
298
145
#ifdef __cplusplus
299
146
}
0 commit comments