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