forked from ProcyonCoin/ProcyonCoonCoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkawpow_tests.cpp
142 lines (112 loc) · 5.15 KB
/
kawpow_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2019 Veil developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/test_procyon.h>
#include <boost/test/unit_test.hpp>
#include <crypto/ethash/lib/ethash/endianness.hpp>
#include <crypto/ethash/include/ethash/progpow.hpp>
#include "crypto/ethash/helpers.hpp"
#include "crypto/ethash/progpow_test_vectors.hpp"
#include <array>
BOOST_FIXTURE_TEST_SUITE(kawpow_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(kawpow_l1_cache)
{
auto& context = get_ethash_epoch_context_0();
constexpr auto test_size = 20;
std::array<uint32_t, test_size> cache_slice;
for (size_t i = 0; i < cache_slice.size(); ++i)
cache_slice[i] = ethash::le::uint32(context.l1_cache[i]);
const std::array<uint32_t, test_size> expected{
{2492749011, 430724829, 2029256771, 3095580433, 3583790154, 3025086503,
805985885, 4121693337, 2320382801, 3763444918, 1006127899, 1480743010,
2592936015, 2598973744, 3038068233, 2754267228, 2867798800, 2342573634,
467767296, 246004123}};
int i = 0;
for (auto item : cache_slice) {
BOOST_CHECK(item == expected[i]);
i++;
}
}
BOOST_AUTO_TEST_CASE(kawpow_hash_empty)
{
auto& context = get_ethash_epoch_context_0();
int count = 1000;
ethash_result result;
while (count > 0) {
result = progpow::hash(context, count, {}, 0);
--count;
}
const auto mix_hex = "6e97b47b134fda0c7888802988e1a373affeb28bcd813b6e9a0fc669c935d03a";
const auto final_hex = "e601a7257a70dc48fccc97a7330d704d776047623b92883d77111fb36870f3d1";
BOOST_CHECK_EQUAL(to_hex(result.mix_hash), mix_hex);
BOOST_CHECK_EQUAL(to_hex(result.final_hash), final_hex);
}
BOOST_AUTO_TEST_CASE(kawpow_hash_30000)
{
const int block_number = 30000;
const auto header =
to_hash256("ffeeddccbbaa9988776655443322110000112233445566778899aabbccddeeff");
const uint64_t nonce = 0x123456789abcdef0;
auto context = ethash::create_epoch_context(ethash::get_epoch_number(block_number));
const auto result = progpow::hash(*context, block_number, header, nonce);
const auto mix_hex = "177b565752a375501e11b6d9d3679c2df6197b2cab3a1ba2d6b10b8c71a3d459";
const auto final_hex = "c824bee0418e3cfb7fae56e0d5b3b8b14ba895777feea81c70c0ba947146da69";
BOOST_CHECK_EQUAL(to_hex(result.mix_hash), mix_hex);
BOOST_CHECK_EQUAL(to_hex(result.final_hash), final_hex);
}
BOOST_AUTO_TEST_CASE(kawpow_hash_and_verify)
{
ethash::epoch_context_ptr context{nullptr, nullptr};
for (auto& t : progpow_hash_test_cases)
{
const auto epoch_number = ethash::get_epoch_number(t.block_number);
if (!context || context->epoch_number != epoch_number)
context = ethash::create_epoch_context(epoch_number);
const auto header_hash = to_hash256(t.header_hash_hex);
const auto nonce = std::stoull(t.nonce_hex, nullptr, 16);
const auto result = progpow::hash(*context, t.block_number, header_hash, nonce);
BOOST_CHECK_EQUAL(to_hex(result.mix_hash), t.mix_hash_hex);
BOOST_CHECK_EQUAL(to_hex(result.final_hash), t.final_hash_hex);
auto success = progpow::verify(
*context, t.block_number, header_hash, result.mix_hash, nonce, result.final_hash);
BOOST_CHECK(success);
auto lower_boundary = result.final_hash;
--lower_boundary.bytes[31];
auto final_failure = progpow::verify(
*context, t.block_number, header_hash, result.mix_hash, nonce, lower_boundary);
BOOST_CHECK(!final_failure);
auto different_mix = result.mix_hash;
++different_mix.bytes[7];
auto mix_failure = progpow::verify(
*context, t.block_number, header_hash, different_mix, nonce, result.final_hash);
BOOST_CHECK(!mix_failure);
}
}
BOOST_AUTO_TEST_CASE(kawpow_search)
{
auto ctxp = ethash::create_epoch_context_full(0);
auto& ctx = *ctxp;
auto& ctxl = reinterpret_cast<const ethash::epoch_context&>(ctx);
auto boundary = to_hash256("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
auto sr = progpow::search(ctx, 0, {}, boundary, 700, 100);
auto srl = progpow::search_light(ctxl, 0, {}, boundary, 700, 100);
BOOST_CHECK(sr.mix_hash == ethash::hash256{});
BOOST_CHECK(sr.final_hash == ethash::hash256{});
BOOST_CHECK(sr.nonce == 0x0);
BOOST_CHECK(sr.mix_hash == srl.mix_hash);
BOOST_CHECK(sr.final_hash == srl.final_hash);
BOOST_CHECK(sr.nonce == srl.nonce);
// Switch it to a different starting nonce and find another solution
sr = progpow::search(ctx, 0, {}, boundary, 300, 100);
srl = progpow::search_light(ctxl, 0, {}, boundary, 300, 100);
BOOST_CHECK(sr.mix_hash != ethash::hash256{});
BOOST_CHECK(sr.final_hash != ethash::hash256{});
BOOST_CHECK(sr.nonce == 395);
BOOST_CHECK(sr.mix_hash == srl.mix_hash);
BOOST_CHECK(sr.final_hash == srl.final_hash);
BOOST_CHECK(sr.nonce == srl.nonce);
auto r = progpow::hash(ctx, 0, {}, 395);
BOOST_CHECK(sr.final_hash == r.final_hash);
BOOST_CHECK(sr.mix_hash == r.mix_hash);
}
BOOST_AUTO_TEST_SUITE_END()