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
}
2019-01-13 00:12:30 +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 ) ) ;
2019-01-13 00:12:30 +03:00
if ( r )
m_seed = scr_seed ;
2019-01-14 19:51:32 +03:00
CRITICAL_REGION_END ( ) ;
2019-01-13 00:12:30 +03:00
return r ;
2019-01-11 06:15:34 +03:00
}
2019-01-13 00:12:30 +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-23 21:13:26 +03:00
CRITICAL_REGION_BEGIN ( m_lock ) ;
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 ( ) )
2019-01-13 00:12:30 +03:00
{
bool r = generate ( scr_seed , height ) ;
CHECK_AND_ASSERT_THROW_MES ( r , " Unable to generate scratchpad " ) ;
}
2019-01-14 19:51:32 +03:00
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 ;
}
2019-01-13 00:12:30 +03:00
crypto : : hash scratchpad_keeper : : get_pow_hash ( const block & b , const crypto : : hash & scr_seed )
2019-01-11 23:46:29 +03:00
{
blobdata bl = get_block_hashing_blob ( b ) ;
2019-01-13 00:12:30 +03:00
return get_pow_hash ( bl , get_block_height ( b ) , scr_seed ) ;
2019-01-11 23:46:29 +03:00
}
2019-01-11 06:15:34 +03:00
uint64_t scratchpad_keeper : : size ( )
{
return m_scratchpad . size ( ) ;
}
}