1
0
Fork 0
forked from lthn/blockchain
blockchain/src/currency_core/scratchpad_helper.cpp

54 lines
1.9 KiB
C++
Raw Normal View History

2019-01-11 06:15:34 +03:00
// Copyright (c) 2018-2019 Zano Project
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "scratchpad_helper.h"
#include "currency_format_utils.h"
namespace currency
{
2019-01-14 19:51:32 +03:00
scratchpad_keeper::scratchpad_keeper():m_seed(null_hash)
{
2019-01-11 06:15:34 +03:00
2019-01-14 19:51:32 +03:00
}
bool scratchpad_keeper::generate(const crypto::hash& scr_seed, uint64_t height)
2019-01-11 06:15:34 +03:00
{
2019-01-14 19:51:32 +03:00
bool r = false;
CRITICAL_REGION_BEGIN(m_lock);
r = crypto::generate_scratchpad(scr_seed, m_scratchpad, get_scratchpad_size_for_height(height));
if (r)
m_seed = scr_seed;
2019-01-14 19:51:32 +03:00
CRITICAL_REGION_END();
return r;
2019-01-11 06:15:34 +03:00
}
crypto::hash scratchpad_keeper::get_pow_hash(const blobdata& bd, uint64_t height, const crypto::hash& scr_seed)
2019-01-11 06:15:34 +03:00
{
2019-01-14 19:51:32 +03:00
crypto::hash res_hash = null_hash;
2019-01-19 19:24:37 +03:00
if (scr_seed != m_seed || get_scratchpad_size_for_height(height) != this->size())
{
bool r = generate(scr_seed, height);
CHECK_AND_ASSERT_THROW_MES(r, "Unable to generate scratchpad");
}
2019-01-14 19:51:32 +03:00
CRITICAL_REGION_BEGIN(m_lock);
CHECK_AND_ASSERT_THROW_MES(get_scratchpad_size_for_height(height) == this->size(), "Fatal error on hash calculation: scratchpad_size=" << m_scratchpad.size() << " at height=" << height << ", scr_seed=" << scr_seed << ", m_seed=" << m_seed);
CHECK_AND_ASSERT_THROW_MES(scr_seed == m_seed, "Fatal error on hash calculation: scratchpad_seed missmatch scr_seed=" << scr_seed << ", m_seed=" << m_seed);
2019-01-11 06:15:34 +03:00
bool res = get_wild_keccak2(bd, res_hash, m_scratchpad);
CHECK_AND_ASSERT_THROW_MES(res, "Fatal error on hash calculation: scratchpad_size=" << m_scratchpad.size());
2019-01-14 19:51:32 +03:00
CRITICAL_REGION_END();
2019-01-11 06:15:34 +03:00
return res_hash;
}
crypto::hash scratchpad_keeper::get_pow_hash(const block& b, const crypto::hash& scr_seed)
{
blobdata bl = get_block_hashing_blob(b);
return get_pow_hash(bl, get_block_height(b), scr_seed);
}
2019-01-11 06:15:34 +03:00
uint64_t scratchpad_keeper::size()
{
return m_scratchpad.size();
}
}