Browse Source
- DAG and memo file creation will now be taken care of in libethash itself. - To that end we crete a very minimal IO module for ethash. - Depending on the target system io_posix or io_win32 will be used. - Implemented ethash_io_prepare() for posixcl-refactor
3 changed files with 141 additions and 0 deletions
@ -0,0 +1,53 @@ |
|||||
|
/*
|
||||
|
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.h
|
||||
|
* @author Lefteris Karapetsas <lefteris@ethdev.com> |
||||
|
* @date 2015 |
||||
|
*/ |
||||
|
#pragma once |
||||
|
#include <stdlib.h> |
||||
|
#include <stdint.h> |
||||
|
#include <stdbool.h> |
||||
|
#include "ethash.h" |
||||
|
|
||||
|
#ifdef __cplusplus |
||||
|
extern "C" { |
||||
|
#endif |
||||
|
|
||||
|
/**
|
||||
|
* Prepares io for ethash |
||||
|
* @param dirname A null terminated c-string of the path of the ethash |
||||
|
* data directory. If it does not exist it's created. |
||||
|
* @param block_number The current block number. Used in seedhash calculation. |
||||
|
* @returns True if all went fine, and false if there was any kind |
||||
|
* of error |
||||
|
*/ |
||||
|
bool ethash_io_prepare(char const *dirname, uint32_t block_number); |
||||
|
void ethash_io_write(); |
||||
|
static inline void ethash_io_serialize_info(uint32_t revision, |
||||
|
uint32_t block_number, |
||||
|
char *output) |
||||
|
{ |
||||
|
// if .info is only consumed locally we don't really care about endianess
|
||||
|
memcpy(output, &revision, 4); |
||||
|
ethash_get_seedhash((uint8_t*)(output + 4), block_number); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
#ifdef __cplusplus |
||||
|
} |
||||
|
#endif |
@ -0,0 +1,82 @@ |
|||||
|
/*
|
||||
|
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_posix.c
|
||||
|
* @author Lefteris Karapetsas <lefteris@ethdev.com> |
||||
|
* @date 2015 |
||||
|
*/ |
||||
|
#include "io.h" |
||||
|
#include <sys/types.h> |
||||
|
#include <sys/stat.h> |
||||
|
#include <errno.h> |
||||
|
#include <libgen.h> |
||||
|
#include <string.h> |
||||
|
#include <stdio.h> |
||||
|
#include <unistd.h> |
||||
|
|
||||
|
static const char DAG_FILE_NAME[] = "full"; |
||||
|
static const char DAG_MEMO_NAME[] = "full.info"; |
||||
|
static const unsigned int DAG_MEMO_BYTESIZE = 36; |
||||
|
|
||||
|
bool ethash_io_prepare(char const *dirname, uint32_t block_number) |
||||
|
{ |
||||
|
char read_buffer[DAG_MEMO_BYTESIZE]; |
||||
|
char expect_buffer[DAG_MEMO_BYTESIZE]; |
||||
|
bool ret = false; |
||||
|
|
||||
|
// assert directory exists, full owner permissions and read/search for others
|
||||
|
int rc = mkdir(dirname, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); |
||||
|
if (rc == -1 && errno != EEXIST) { |
||||
|
goto end; |
||||
|
} |
||||
|
|
||||
|
// try to open memo file
|
||||
|
char *memofile = malloc(strlen(dirname) + sizeof(DAG_MEMO_NAME)); |
||||
|
if (!memofile) { |
||||
|
goto end; |
||||
|
} |
||||
|
|
||||
|
FILE *f = fopen(memofile, "rb"); |
||||
|
if (!f) { |
||||
|
// file does not exist, so no checking happens. All is fine.
|
||||
|
ret = true; |
||||
|
goto free_memo; |
||||
|
} |
||||
|
|
||||
|
if (fread(read_buffer, 1, DAG_MEMO_BYTESIZE, f) != DAG_MEMO_BYTESIZE) { |
||||
|
goto free_memo; |
||||
|
} |
||||
|
|
||||
|
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) { |
||||
|
goto free_memo; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
ret = true; |
||||
|
|
||||
|
free_memo: |
||||
|
free(memofile); |
||||
|
end: |
||||
|
return ret; |
||||
|
} |
||||
|
|
||||
|
void ethash_io_write() |
||||
|
{ |
||||
|
} |
||||
|
|
Loading…
Reference in new issue