forked from wownero/wownero
Based on Boolberry work by: jahrsg <jahr@jahr.me> cr.zoidberg <crypto.zoidberg@gmail.com>release-v0.6.1.2
parent
e4b049da05
commit
91f4c7f45f
@ -0,0 +1,65 @@
|
||||
// Copyright (c) 2019, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cryptonote_basic/difficulty.h"
|
||||
#include "serialization.h"
|
||||
|
||||
template<> struct is_basic_type<cryptonote::difficulty_type> { typedef boost::true_type type; };
|
||||
|
||||
template <template <bool> class Archive>
|
||||
inline bool do_serialize(Archive<false>& ar, cryptonote::difficulty_type &diff)
|
||||
{
|
||||
uint64_t hi, lo;
|
||||
ar.serialize_varint(hi);
|
||||
if (!ar.stream().good())
|
||||
return false;
|
||||
ar.serialize_varint(lo);
|
||||
if (!ar.stream().good())
|
||||
return false;
|
||||
diff = hi;
|
||||
diff <<= 64;
|
||||
diff += lo;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <template <bool> class Archive>
|
||||
inline bool do_serialize(Archive<true>& ar, cryptonote::difficulty_type &diff)
|
||||
{
|
||||
if (!ar.stream().good())
|
||||
return false;
|
||||
const uint64_t hi = (diff >> 64).convert_to<uint64_t>();
|
||||
const uint64_t lo = (diff << 64 >> 64).convert_to<uint64_t>();
|
||||
ar.serialize_varint(hi);
|
||||
ar.serialize_varint(lo);
|
||||
if (!ar.stream().good())
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import print_function
|
||||
import random
|
||||
|
||||
DIFFICULTY_TARGET = 120
|
||||
DIFFICULTY_WINDOW = 720
|
||||
DIFFICULTY_LAG = 15
|
||||
DIFFICULTY_CUT = 60
|
||||
|
||||
def difficulty():
|
||||
times = []
|
||||
diffs = []
|
||||
while True:
|
||||
if len(times) <= 1:
|
||||
diff = 1
|
||||
else:
|
||||
begin = max(len(times) - DIFFICULTY_WINDOW - DIFFICULTY_LAG, 0)
|
||||
end = min(begin + DIFFICULTY_WINDOW, len(times))
|
||||
length = end - begin
|
||||
assert length >= 2
|
||||
if length <= DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT:
|
||||
cut_begin = 0
|
||||
cut_end = length
|
||||
else:
|
||||
excess = length - (DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT)
|
||||
cut_begin = (excess + 1) // 2
|
||||
cut_end = length - excess // 2
|
||||
assert cut_begin + 2 <= cut_end
|
||||
wnd = times[begin:end]
|
||||
wnd.sort()
|
||||
dtime = wnd[cut_end - 1] - wnd[cut_begin]
|
||||
dtime = max(dtime, 1)
|
||||
ddiff = sum(diffs[begin + cut_begin + 1:begin + cut_end])
|
||||
diff = (ddiff * DIFFICULTY_TARGET + dtime - 1) // dtime
|
||||
times.append((yield diff))
|
||||
diffs.append(diff)
|
||||
|
||||
random.seed(1)
|
||||
time = 1000
|
||||
gen = difficulty()
|
||||
diff = next(gen)
|
||||
for i in range(100000):
|
||||
power = 100 if i < 10000 else 100000000 if i < 500 else 1000000000000 if i < 1000 else 1000000000000000 if i < 2000 else 10000000000000000000 if i < 4000 else 1000000000000000000000000
|
||||
time += random.randint(-diff // power - 10, 3 * diff // power + 10)
|
||||
print(time, diff)
|
||||
diff = gen.send(time)
|
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import subprocess
|
||||
|
||||
python = sys.argv[1]
|
||||
py = sys.argv[2]
|
||||
c = sys.argv[3]
|
||||
data = sys.argv[4]
|
||||
|
||||
first = python + " " + py + " > " + data
|
||||
second = [c, '--wide', data]
|
||||
|
||||
try:
|
||||
print('running: ', first)
|
||||
subprocess.check_call(first, shell=True)
|
||||
print('running: ', second)
|
||||
subprocess.check_call(second)
|
||||
except:
|
||||
sys.exit(1)
|
||||
|
@ -0,0 +1,66 @@
|
||||
// Copyright (c) 2019, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "string_tools.h"
|
||||
#include "cryptonote_basic/difficulty.h"
|
||||
|
||||
template<uint64_t hash_target_high, uint64_t hash_target_low, uint64_t difficulty_high, uint64_t difficulty_low>
|
||||
class test_check_hash
|
||||
{
|
||||
public:
|
||||
static const size_t loop_count = 100000;
|
||||
|
||||
bool init()
|
||||
{
|
||||
cryptonote::difficulty_type hash_target = hash_target_high;
|
||||
hash_target = (hash_target << 64) | hash_target_low;
|
||||
difficulty = difficulty_high;
|
||||
difficulty = (difficulty << 64) | difficulty_low;
|
||||
boost::multiprecision::uint256_t hash_value = std::numeric_limits<boost::multiprecision::uint256_t>::max() / hash_target;
|
||||
((uint64_t*)&hash)[0] = (hash_value << 64 >> 64).convert_to<uint64_t>();
|
||||
hash_value >>= 64;
|
||||
((uint64_t*)&hash)[1] = (hash_value << 64 >> 64).convert_to<uint64_t>();
|
||||
hash_value >>= 64;
|
||||
((uint64_t*)&hash)[2] = (hash_value << 64 >> 64).convert_to<uint64_t>();
|
||||
hash_value >>= 64;
|
||||
((uint64_t*)&hash)[3] = (hash_value << 64 >> 64).convert_to<uint64_t>();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool test()
|
||||
{
|
||||
cryptonote::check_hash_128(hash, difficulty);
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
crypto::hash hash;
|
||||
cryptonote::difficulty_type difficulty;
|
||||
};
|
@ -0,0 +1,68 @@
|
||||
// Copyright (c) 2019, The Monero Project
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without modification, are
|
||||
// permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
// conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
// of conditions and the following disclaimer in the documentation and/or other
|
||||
// materials provided with the distribution.
|
||||
//
|
||||
// 3. Neither the name of the copyright holder nor the names of its contributors may be
|
||||
// used to endorse or promote products derived from this software without specific
|
||||
// prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||||
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
||||
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "cryptonote_basic/difficulty.h"
|
||||
|
||||
static cryptonote::difficulty_type MKDIFF(uint64_t high, uint64_t low)
|
||||
{
|
||||
cryptonote::difficulty_type d = high;
|
||||
d = (d << 64) | low;
|
||||
return d;
|
||||
}
|
||||
|
||||
static crypto::hash MKHASH(uint64_t high, uint64_t low)
|
||||
{
|
||||
cryptonote::difficulty_type hash_target = high;
|
||||
hash_target = (hash_target << 64) | low;
|
||||
boost::multiprecision::uint256_t hash_value = std::numeric_limits<boost::multiprecision::uint256_t>::max() / hash_target;
|
||||
crypto::hash h;
|
||||
((uint64_t*)&h)[0] = hash_value.convert_to<uint64_t>();
|
||||
hash_value >>= 64;
|
||||
((uint64_t*)&h)[1] = hash_value.convert_to<uint64_t>();
|
||||
hash_value >>= 64;
|
||||
((uint64_t*)&h)[2] = hash_value.convert_to<uint64_t>();
|
||||
hash_value >>= 64;
|
||||
((uint64_t*)&h)[3] = hash_value.convert_to<uint64_t>();
|
||||
return h;
|
||||
}
|
||||
|
||||
TEST(difficulty, check_hash)
|
||||
{
|
||||
ASSERT_TRUE(cryptonote::check_hash(MKHASH(0, 1), MKDIFF(0, 1)));
|
||||
ASSERT_FALSE(cryptonote::check_hash(MKHASH(0, 1), MKDIFF(0, 2)));
|
||||
|
||||
ASSERT_TRUE(cryptonote::check_hash(MKHASH(0, 0xffffffffffffffff), MKDIFF(0, 0xffffffffffffffff)));
|
||||
ASSERT_FALSE(cryptonote::check_hash(MKHASH(0, 0xffffffffffffffff), MKDIFF(1, 0)));
|
||||
|
||||
ASSERT_TRUE(cryptonote::check_hash(MKHASH(1, 1), MKDIFF(1, 1)));
|
||||
ASSERT_FALSE(cryptonote::check_hash(MKHASH(1, 1), MKDIFF(1, 2)));
|
||||
|
||||
ASSERT_TRUE(cryptonote::check_hash(MKHASH(0xffffffffffffffff, 1), MKDIFF(0xffffffffffffffff, 1)));
|
||||
ASSERT_FALSE(cryptonote::check_hash(MKHASH(0xffffffffffffffff, 1), MKDIFF(0xffffffffffffffff, 2)));
|
||||
}
|
Loading…
Reference in new issue