mirror of
				https://git.wownero.com/wownero/wownero.git
				synced 2024-08-15 01:03:23 +00:00 
			
		
		
		
	Make difficulty 128 bit instead of 64 bit
Based on Boolberry work by: jahrsg <jahr@jahr.me> cr.zoidberg <crypto.zoidberg@gmail.com>
This commit is contained in:
		
							parent
							
								
									e4b049da05
								
							
						
					
					
						commit
						91f4c7f45f
					
				
					 30 changed files with 787 additions and 62 deletions
				
			
		| 
						 | 
				
			
			@ -45,3 +45,6 @@ set_property(TARGET difficulty-tests
 | 
			
		|||
add_test(
 | 
			
		||||
  NAME    difficulty
 | 
			
		||||
  COMMAND difficulty-tests "${CMAKE_CURRENT_SOURCE_DIR}/data.txt")
 | 
			
		||||
add_test(
 | 
			
		||||
  NAME    wide_difficulty
 | 
			
		||||
  COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/wide_difficulty.py" "${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/gen_wide_data.py" "${CMAKE_CURRENT_BINARY_DIR}/difficulty-tests" "${CMAKE_CURRENT_BINARY_DIR}/wide_data.txt")
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -43,16 +43,15 @@ using namespace std;
 | 
			
		|||
 | 
			
		||||
#define DEFAULT_TEST_DIFFICULTY_TARGET        120
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[]) {
 | 
			
		||||
    if (argc != 2) {
 | 
			
		||||
        cerr << "Wrong arguments" << endl;
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
    vector<uint64_t> timestamps, cumulative_difficulties;
 | 
			
		||||
    fstream data(argv[1], fstream::in);
 | 
			
		||||
static int test_wide_difficulty(const char *filename)
 | 
			
		||||
{
 | 
			
		||||
    std::vector<uint64_t> timestamps;
 | 
			
		||||
    std::vector<cryptonote::difficulty_type> cumulative_difficulties;
 | 
			
		||||
    fstream data(filename, fstream::in);
 | 
			
		||||
    data.exceptions(fstream::badbit);
 | 
			
		||||
    data.clear(data.rdstate());
 | 
			
		||||
    uint64_t timestamp, difficulty, cumulative_difficulty = 0;
 | 
			
		||||
    uint64_t timestamp;
 | 
			
		||||
    cryptonote::difficulty_type difficulty, cumulative_difficulty = 0;
 | 
			
		||||
    size_t n = 0;
 | 
			
		||||
    while (data >> timestamp >> difficulty) {
 | 
			
		||||
        size_t begin, end;
 | 
			
		||||
| 
						 | 
				
			
			@ -63,11 +62,11 @@ int main(int argc, char *argv[]) {
 | 
			
		|||
            end = n - DIFFICULTY_LAG;
 | 
			
		||||
            begin = end - DIFFICULTY_WINDOW;
 | 
			
		||||
        }
 | 
			
		||||
        uint64_t res = cryptonote::next_difficulty(
 | 
			
		||||
            vector<uint64_t>(timestamps.begin() + begin, timestamps.begin() + end),
 | 
			
		||||
            vector<uint64_t>(cumulative_difficulties.begin() + begin, cumulative_difficulties.begin() + end), DEFAULT_TEST_DIFFICULTY_TARGET);
 | 
			
		||||
        cryptonote::difficulty_type res = cryptonote::next_difficulty(
 | 
			
		||||
            std::vector<uint64_t>(timestamps.begin() + begin, timestamps.begin() + end),
 | 
			
		||||
            std::vector<cryptonote::difficulty_type>(cumulative_difficulties.begin() + begin, cumulative_difficulties.begin() + end), DEFAULT_TEST_DIFFICULTY_TARGET);
 | 
			
		||||
        if (res != difficulty) {
 | 
			
		||||
            cerr << "Wrong difficulty for block " << n << endl
 | 
			
		||||
            cerr << "Wrong wide difficulty for block " << n << endl
 | 
			
		||||
                << "Expected: " << difficulty << endl
 | 
			
		||||
                << "Found: " << res << endl;
 | 
			
		||||
            return 1;
 | 
			
		||||
| 
						 | 
				
			
			@ -81,3 +80,60 @@ int main(int argc, char *argv[]) {
 | 
			
		|||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[]) {
 | 
			
		||||
    if (argc < 2) {
 | 
			
		||||
        cerr << "Wrong arguments" << endl;
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
    if (argc == 3 && strcmp(argv[1], "--wide") == 0)
 | 
			
		||||
    {
 | 
			
		||||
        return test_wide_difficulty(argv[2]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    vector<uint64_t> timestamps, cumulative_difficulties;
 | 
			
		||||
    std::vector<cryptonote::difficulty_type> wide_cumulative_difficulties;
 | 
			
		||||
    fstream data(argv[1], fstream::in);
 | 
			
		||||
    data.exceptions(fstream::badbit);
 | 
			
		||||
    data.clear(data.rdstate());
 | 
			
		||||
    uint64_t timestamp;
 | 
			
		||||
    uint64_t difficulty, cumulative_difficulty = 0;
 | 
			
		||||
    cryptonote::difficulty_type wide_cumulative_difficulty = 0;
 | 
			
		||||
    size_t n = 0;
 | 
			
		||||
    while (data >> timestamp >> difficulty) {
 | 
			
		||||
        size_t begin, end;
 | 
			
		||||
        if (n < DIFFICULTY_WINDOW + DIFFICULTY_LAG) {
 | 
			
		||||
            begin = 0;
 | 
			
		||||
            end = min(n, (size_t) DIFFICULTY_WINDOW);
 | 
			
		||||
        } else {
 | 
			
		||||
            end = n - DIFFICULTY_LAG;
 | 
			
		||||
            begin = end - DIFFICULTY_WINDOW;
 | 
			
		||||
        }
 | 
			
		||||
        uint64_t res = cryptonote::next_difficulty_64(
 | 
			
		||||
            vector<uint64_t>(timestamps.begin() + begin, timestamps.begin() + end),
 | 
			
		||||
            std::vector<uint64_t>(cumulative_difficulties.begin() + begin, cumulative_difficulties.begin() + end), DEFAULT_TEST_DIFFICULTY_TARGET);
 | 
			
		||||
        if (res != difficulty) {
 | 
			
		||||
            cerr << "Wrong difficulty for block " << n << endl
 | 
			
		||||
                << "Expected: " << difficulty << endl
 | 
			
		||||
                << "Found: " << res << endl;
 | 
			
		||||
            return 1;
 | 
			
		||||
        }
 | 
			
		||||
        cryptonote::difficulty_type wide_res = cryptonote::next_difficulty(
 | 
			
		||||
            std::vector<uint64_t>(timestamps.begin() + begin, timestamps.begin() + end),
 | 
			
		||||
            std::vector<cryptonote::difficulty_type>(wide_cumulative_difficulties.begin() + begin, wide_cumulative_difficulties.begin() + end), DEFAULT_TEST_DIFFICULTY_TARGET);
 | 
			
		||||
        if (wide_res.convert_to<uint64_t>() != res) {
 | 
			
		||||
            cerr << "Wrong wide difficulty for block " << n << endl
 | 
			
		||||
                << "Expected: " << res << endl
 | 
			
		||||
                << "Found: " << wide_res << endl;
 | 
			
		||||
            return 1;
 | 
			
		||||
        }
 | 
			
		||||
        timestamps.push_back(timestamp);
 | 
			
		||||
        cumulative_difficulties.push_back(cumulative_difficulty += difficulty);
 | 
			
		||||
        wide_cumulative_difficulties.push_back(wide_cumulative_difficulty += difficulty);
 | 
			
		||||
        ++n;
 | 
			
		||||
    }
 | 
			
		||||
    if (!data.eof()) {
 | 
			
		||||
        data.clear(fstream::badbit);
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										47
									
								
								tests/difficulty/gen_wide_data.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								tests/difficulty/gen_wide_data.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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)
 | 
			
		||||
							
								
								
									
										22
									
								
								tests/difficulty/wide_difficulty.py
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										22
									
								
								tests/difficulty/wide_difficulty.py
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
				
			
			@ -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)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue