Debian Codegraph @

tests/difficulty/difficulty.cpp

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31 #include <cstddef>
32 #include <cstdint>
33 #include <fstream>
34 #include <iostream>
35 #include <vector>
36 #include <algorithm>
37 #include <stdexcept>
38
39 #include "misc_log_ex.h"
40 #include "cryptonote_config.h"
41 #include "cryptonote_basic/difficulty.h"
42
43 using namespace std;
44
45 #define DEFAULT_TEST_DIFFICULTY_TARGET 120
46
47 static int test_wide_difficulty(const char *filename)
48 {
49 std::vector<uint64_t> timestamps;
50 std::vector<cryptonote::difficulty_type> cumulative_difficulties;
51 fstream data(filename, fstream::in);
52 data.exceptions(fstream::badbit);
53 data.clear(data.rdstate());
54 uint64_t timestamp;
55 cryptonote::difficulty_type difficulty, cumulative_difficulty = 0;
56 size_t n = 0;
57 while (data >> timestamp >> difficulty) {
58 size_t begin, end;
59 if (n < DIFFICULTY_WINDOW + DIFFICULTY_LAG) {
60 begin = 0;
61 end = min(n, (size_t) DIFFICULTY_WINDOW);
62 } else {
63 end = n - DIFFICULTY_LAG;
64 begin = end - DIFFICULTY_WINDOW;
65 }
66 cryptonote::difficulty_type res = cryptonote::next_difficulty(
67 std::vector<uint64_t>(timestamps.begin() + begin, timestamps.begin() + end),
68 std::vector<cryptonote::difficulty_type>(cumulative_difficulties.begin() + begin, cumulative_difficulties.begin() + end), DEFAULT_TEST_DIFFICULTY_TARGET);
69 if (res != difficulty) {
70 cerr << "Wrong wide difficulty for block " << n << endl
71 << "Expected: " << difficulty << endl
72 << "Found: " << res << endl;
73 return 1;
74 }
75 timestamps.push_back(timestamp);
76 cumulative_difficulties.push_back(cumulative_difficulty += difficulty);
77 ++n;
78 }
79 if (!data.eof()) {
80 data.clear(fstream::badbit);
81 }
82 return 0;
83 }
84
85 int main(int argc, char *argv[]) {
86 TRY_ENTRY();
87
88 if (argc < 2) {
89 cerr << "Wrong arguments" << endl;
90 return 1;
91 }
92 if (argc == 3 && strcmp(argv[1], "--wide") == 0)
93 {
94 return test_wide_difficulty(argv[2]);
95 }
96
97 vector<uint64_t> timestamps, cumulative_difficulties;
98 std::vector<cryptonote::difficulty_type> wide_cumulative_difficulties;
99 fstream data(argv[1], fstream::in);
100 data.exceptions(fstream::badbit);
101 data.clear(data.rdstate());
102 uint64_t timestamp;
103 uint64_t difficulty, cumulative_difficulty = 0;
104 cryptonote::difficulty_type wide_cumulative_difficulty = 0;
105 size_t n = 0;
106 while (data >> timestamp >> difficulty) {
107 size_t begin, end;
108 if (n < DIFFICULTY_WINDOW + DIFFICULTY_LAG) {
109 begin = 0;
110 end = min(n, (size_t) DIFFICULTY_WINDOW);
111 } else {
112 end = n - DIFFICULTY_LAG;
113 begin = end - DIFFICULTY_WINDOW;
114 }
115 uint64_t res = cryptonote::next_difficulty_64(
116 vector<uint64_t>(timestamps.begin() + begin, timestamps.begin() + end),
117 std::vector<uint64_t>(cumulative_difficulties.begin() + begin, cumulative_difficulties.begin() + end), DEFAULT_TEST_DIFFICULTY_TARGET);
118 if (res != difficulty) {
119 cerr << "Wrong difficulty for block " << n << endl
120 << "Expected: " << difficulty << endl
121 << "Found: " << res << endl;
122 return 1;
123 }
124 cryptonote::difficulty_type wide_res = cryptonote::next_difficulty(
125 std::vector<uint64_t>(timestamps.begin() + begin, timestamps.begin() + end),
126 std::vector<cryptonote::difficulty_type>(wide_cumulative_difficulties.begin() + begin, wide_cumulative_difficulties.begin() + end), DEFAULT_TEST_DIFFICULTY_TARGET);
127 if ((wide_res & 0xffffffffffffffff).convert_to<uint64_t>() != res) {
128 cerr << "Wrong wide difficulty for block " << n << endl
129 << "Expected: " << res << endl
130 << "Found: " << wide_res << endl;
131 return 1;
132 }
133 timestamps.push_back(timestamp);
134 cumulative_difficulties.push_back(cumulative_difficulty += difficulty);
135 wide_cumulative_difficulties.push_back(wide_cumulative_difficulty += difficulty);
136 ++n;
137 }
138 if (!data.eof()) {
139 data.clear(fstream::badbit);
140 }
141 return 0;
142
143 CATCH_ENTRY_L0("main", 1);
144 }