Browse Source
- adding ethash_io_write() function - only ethash_io_prepare() invoke system dependent functions so it's the only one going in system specific source files.cl-refactor
Lefteris Karapetsas
10 years ago
5 changed files with 238 additions and 22 deletions
@ -0,0 +1,92 @@ |
|||
/*
|
|||
This file is part of ethash. |
|||
|
|||
ethash is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethash is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with ethash. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file io.c
|
|||
* @author Lefteris Karapetsas <lefteris@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
#include "io.h" |
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
|
|||
// silly macro to save some typing
|
|||
#define PASS_ARR(c_) (c_), sizeof(c_) |
|||
|
|||
static bool ethash_io_write_file(char const *dirname, |
|||
char const* filename, |
|||
size_t filename_length, |
|||
void const* data, |
|||
size_t data_size) |
|||
{ |
|||
bool ret = false; |
|||
char *fullname = ethash_io_create_filename(dirname, filename, filename_length); |
|||
if (!fullname) { |
|||
return false; |
|||
} |
|||
FILE *f = fopen(fullname, "wb"); |
|||
if (!f) { |
|||
goto free_name; |
|||
} |
|||
if (data_size != fwrite(data, 1, data_size, f)) { |
|||
goto close; |
|||
} |
|||
|
|||
ret = true; |
|||
close: |
|||
fclose(f); |
|||
free_name: |
|||
free(fullname); |
|||
return ret; |
|||
} |
|||
|
|||
bool ethash_io_write(char const *dirname, |
|||
uint32_t block_number, |
|||
void const* cache, |
|||
uint8_t **data, |
|||
size_t *data_size) |
|||
{ |
|||
ethash_params p; |
|||
char info_buffer[DAG_MEMO_BYTESIZE]; |
|||
|
|||
p.cache_size = ethash_get_cachesize(block_number); |
|||
p.full_size = ethash_get_datasize(block_number); |
|||
// allocate the bytes
|
|||
uint8_t *temp_data_ptr = malloc(p.full_size); |
|||
if (!(*temp_data_ptr)) { |
|||
goto end; |
|||
} |
|||
ethash_prep_full(temp_data_ptr, &p, cache); |
|||
|
|||
if (!ethash_io_write_file(dirname, PASS_ARR(DAG_FILE_NAME), temp_data_ptr, p.full_size)) { |
|||
goto fail_free; |
|||
} |
|||
|
|||
ethash_io_serialize_info(REVISION, block_number, info_buffer); |
|||
if (!ethash_io_write_file(dirname, PASS_ARR(DAG_MEMO_NAME), info_buffer, DAG_MEMO_BYTESIZE)) { |
|||
goto fail_free; |
|||
} |
|||
|
|||
*data = temp_data_ptr; |
|||
*data_size = p.full_size; |
|||
return true; |
|||
|
|||
fail_free: |
|||
free(temp_data_ptr); |
|||
end: |
|||
return false; |
|||
} |
|||
|
|||
#undef PASS_ARR |
@ -0,0 +1,73 @@ |
|||
/*
|
|||
This file is part of ethash. |
|||
|
|||
ethash is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethash is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with ethash. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file io_win32.c
|
|||
* @author Lefteris Karapetsas <lefteris@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
|
|||
#include "io.h" |
|||
#include <direct.h> |
|||
#include <errno.h> |
|||
#include <stdio.h> |
|||
|
|||
enum ethash_io_rc ethash_io_prepare(char const *dirname, uint32_t block_number) |
|||
{ |
|||
char read_buffer[DAG_MEMO_BYTESIZE]; |
|||
char expect_buffer[DAG_MEMO_BYTESIZE]; |
|||
enum ethash_io_rc ret = ETHASH_IO_FAIL; |
|||
|
|||
// assert directory exists
|
|||
int rc = _mkdir(dirname); |
|||
if (rc == -1 && errno != EEXIST) { |
|||
goto end; |
|||
} |
|||
|
|||
char *memofile = ethash_io_create_filename(dirname, DAG_MEMO_NAME, sizeof(DAG_MEMO_NAME)); |
|||
if (!memofile) { |
|||
goto end; |
|||
} |
|||
|
|||
// try to open memo file
|
|||
FILE *f = fopen(memofile, "rb"); |
|||
if (!f) { |
|||
// file does not exist, so no checking happens. All is fine.
|
|||
ret = ETHASH_IO_MEMO_MISMATCH; |
|||
goto free_memo; |
|||
} |
|||
|
|||
if (fread(read_buffer, 1, DAG_MEMO_BYTESIZE, f) != DAG_MEMO_BYTESIZE) { |
|||
goto close; |
|||
} |
|||
|
|||
ethash_io_serialize_info(REVISION, block_number, expect_buffer); |
|||
if (memcmp(read_buffer, expect_buffer, DAG_MEMO_BYTESIZE) != 0) { |
|||
// we have different memo contents so delete the memo file
|
|||
if (_unlink(memofile) != 0) { |
|||
ret = ETHASH_IO_MEMO_MISMATCH; |
|||
goto close; |
|||
} |
|||
} |
|||
|
|||
ret = ETHASH_IO_MEMO_MATCH; |
|||
|
|||
close: |
|||
fclose(f); |
|||
free_memo: |
|||
free(memofile); |
|||
end: |
|||
return ret; |
|||
} |
Loading…
Reference in new issue