1
0
Fork 0
forked from lthn/blockchain

ethash: implement custom logging, add log message on allocation error

This commit is contained in:
sowle 2019-08-30 03:53:20 +03:00
parent a8173575bb
commit 33598c7c19
No known key found for this signature in database
GPG key ID: C07A24B2D89D49FC
3 changed files with 90 additions and 12 deletions

View file

@ -145,7 +145,10 @@ epoch_context_full* create_epoch_context(
char* const alloc_data = static_cast<char*>(std::calloc(1, alloc_size));
if (!alloc_data)
return nullptr; // Signal out-of-memory by returning null pointer.
{
LOG_CUSTOM_WITH_CALLSTACK("CRITICAL: std::calloc(" << alloc_size << ") failed in create_epoch_context()", 0);
return nullptr; // Signal out-of-memory by returning null pointer.
}
hash512* const light_cache = reinterpret_cast<hash512*>(alloc_data + context_alloc_size);
const hash256 epoch_seed = calculate_epoch_seed(epoch_number);
@ -373,6 +376,33 @@ search_result search(const epoch_context_full& context, const hash256& header_ha
}
return {};
}
custom_log_level_function*& access_custom_log_level_function()
{
static custom_log_level_function* p_custom_log_level_function = nullptr;
return p_custom_log_level_function;
}
custom_log_function*& access_custom_log_function()
{
static custom_log_function* p_custom_log_function = nullptr;
return p_custom_log_function;
}
int get_custom_log_level()
{
if (access_custom_log_level_function() != nullptr)
return access_custom_log_level_function()();
return -1;
}
void custom_log(const std::string& m, bool add_callstack)
{
if (access_custom_log_function() != nullptr)
access_custom_log_function()(m, add_callstack);
}
} // namespace ethash
using namespace ethash;
@ -434,6 +464,8 @@ void ethash_destroy_epoch_context_full(epoch_context_full* context) noexcept
void ethash_destroy_epoch_context(epoch_context* context) noexcept
{
LOG_CUSTOM("context for epoch " << context->epoch_number << " is about to be freed", 0);
context->~epoch_context();
std::free(context);
}

View file

@ -20,6 +20,8 @@
#include <cstdint>
#include <cstring>
#include <memory>
#include <string>
#include <sstream>
namespace ethash
{
@ -157,4 +159,33 @@ const epoch_context& get_global_epoch_context(int epoch_number);
/// Get global shared epoch context with full dataset initialized.
std::shared_ptr<epoch_context_full> get_global_epoch_context_full(int epoch_number);
typedef int (custom_log_level_function)();
typedef void (custom_log_function)(const std::string& m, bool add_callstack);
custom_log_level_function*& access_custom_log_level_function();
custom_log_function*& access_custom_log_function();
int get_custom_log_level();
void custom_log(const std::string& m, bool add_callstack);
#define LOG_CUSTOM(msg, level) \
{ \
if (level <= ethash::get_custom_log_level()) \
{ \
std::stringstream ss; \
ss << msg << std::endl; \
ethash::custom_log(ss.str(), false); \
} \
}
#define LOG_CUSTOM_WITH_CALLSTACK(msg, level) \
{ \
if (level <= ethash::get_custom_log_level()) \
{ \
std::stringstream ss; \
ss << msg << std::endl; \
ethash::custom_log(ss.str(), true); \
} \
}
} // namespace ethash

View file

@ -22,17 +22,31 @@ namespace currency
{
//--------------------------------------------------------------
//global object
// crypto::ethash::cache_manager cache;
// void ethash_set_use_dag(bool use_dag)
// {
// cache.set_use_dag(use_dag);
// }
// //------------------------------------------------------------------
// const uint8_t* ethash_get_dag(uint64_t epoch, uint64_t& dag_size)
// {
// return cache.get_dag(epoch, dag_size);
// }
int ethash_custom_log_get_level()
{
return epee::log_space::get_set_log_detalisation_level();
}
//--------------------------------------------------------------
void ethash_custom_log(const std::string& m, bool add_callstack)
{
std::string msg = epee::log_space::log_singletone::get_prefix_entry() + "[ETHASH]" + m;
if (add_callstack)
msg = msg + "callstask: " + epee::misc_utils::get_callstack();
epee::log_space::log_singletone::do_log_message(msg, LOG_LEVEL_0, epee::log_space::console_color_default, true, LOG_DEFAULT_TARGET);
}
//--------------------------------------------------------------
void init_ethash_log_if_necessary()
{
static bool inited = false;
if (inited)
return;
ethash::access_custom_log_level_function() = &ethash_custom_log_get_level;
ethash::access_custom_log_function() = &ethash_custom_log;
inited = true;
}
//------------------------------------------------------------------
int ethash_height_to_epoch(uint64_t height)
{
@ -49,6 +63,7 @@ namespace currency
//--------------------------------------------------------------
crypto::hash get_block_longhash(uint64_t height, const crypto::hash& block_header_hash, uint64_t nonce)
{
init_ethash_log_if_necessary();
int epoch = ethash_height_to_epoch(height);
std::shared_ptr<ethash::epoch_context_full> p_context = progpow::get_global_epoch_context_full(static_cast<int>(epoch));
CHECK_AND_ASSERT_THROW_MES(p_context, "progpow::get_global_epoch_context_full returned null");