| 1 | #!/usr/bin/python3 |
| 2 | |
| 3 | # Copyright (c) 2014-2020, The Monero Project |
| 4 | # |
| 5 | # All rights reserved. |
| 6 | # |
| 7 | # Redistribution and use in source and binary forms, with or without modification, are |
| 8 | # permitted provided that the following conditions are met: |
| 9 | # |
| 10 | # 1. Redistributions of source code must retain the above copyright notice, this list of |
| 11 | # conditions and the following disclaimer. |
| 12 | # |
| 13 | # 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 14 | # of conditions and the following disclaimer in the documentation and/or other |
| 15 | # materials provided with the distribution. |
| 16 | # |
| 17 | # 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 18 | # used to endorse or promote products derived from this software without specific |
| 19 | # prior written permission. |
| 20 | # |
| 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 22 | # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 24 | # THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 28 | # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 29 | # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | # |
| 31 | # Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 32 | |
| 33 | from random import randint |
| 34 | |
| 35 | DIFFICULTY_TARGET = 120 |
| 36 | DIFFICULTY_WINDOW = 720 |
| 37 | DIFFICULTY_LAG = 15 |
| 38 | DIFFICULTY_CUT = 60 |
| 39 | |
| 40 | UINT_MAX = (1 << 64) - 1 |
| 41 | |
| 42 | def difficulty(): |
| 43 | times = [] |
| 44 | diffs = [] |
| 45 | while True: |
| 46 | if len(times) <= 1: |
| 47 | diff = 1 |
| 48 | else: |
| 49 | begin = max(len(times) - DIFFICULTY_WINDOW - DIFFICULTY_LAG, 0) |
| 50 | end = min(begin + DIFFICULTY_WINDOW, len(times)) |
| 51 | length = end - begin |
| 52 | assert length >= 2 |
| 53 | if length <= DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT: |
| 54 | cut_begin = 0 |
| 55 | cut_end = length |
| 56 | else: |
| 57 | excess = length - (DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT) |
| 58 | cut_begin = (excess + 1) // 2 |
| 59 | cut_end = length - excess // 2 |
| 60 | assert cut_begin + 2 <= cut_end |
| 61 | wnd = times[begin:end] |
| 62 | wnd.sort() |
| 63 | dtime = wnd[cut_end - 1] - wnd[cut_begin] |
| 64 | assert 0 <= dtime <= UINT_MAX |
| 65 | dtime = max(dtime, 1) |
| 66 | ddiff = sum(diffs[begin + cut_begin + 1:begin + cut_end]) |
| 67 | assert 0 < ddiff <= UINT_MAX |
| 68 | assert dtime <= ddiff * DIFFICULTY_TARGET + dtime - 1 <= UINT_MAX |
| 69 | diff = (ddiff * DIFFICULTY_TARGET + dtime - 1) // dtime |
| 70 | assert 0 < diff <= UINT_MAX |
| 71 | times.append((yield diff)) |
| 72 | diffs.append(diff) |
| 73 | |
| 74 | time = 1000 |
| 75 | gen = difficulty() |
| 76 | diff = next(gen) |
| 77 | for i in range(1000): |
| 78 | power = 10 if i < 100 else 10000 if i < 500 else 1000000 |
| 79 | time += randint(-diff // power - 10, 3 * diff // power + 10) |
| 80 | assert 0 <= time <= UINT_MAX |
| 81 | print(time, diff) |
| 82 | diff = gen.send(time) |