Ejemplo n.º 1
0
def main():
    ph = PoWHelper()
    qm = CustomQMiner()

    input_bytes = [0x03, 0x05, 0x07, 0x09, 0x19]
    difficulty = StringToUInt256("5000")

    for i in range(10):
        boundary = ph.getBoundary(difficulty)

        #       print("difficulty     ", difficulty)
        print("difficulty str ", UInt256ToString(difficulty))
        print("boundary       ", boundary)
        #        print("boundary str   ", UInt256ToString(boundary))

        start = time.time()

        # Set input bytes, nonce
        qm.setInput(input=input_bytes, nonceOffset=0, target=boundary)

        qm.start(thread_count=2)

        while not qm.solutionFound():
            time.sleep(1)

        print("time           ", qm.end - start)
        print("hash           ", qm.solutionHash())
        print()

        # Set a new difficulty
        difficulty = ph.getDifficulty(int(qm.end), int(start), difficulty)
Ejemplo n.º 2
0
    def test_adaptive_boundary(self):
        ph = PoWHelper()

        parent_difficulty = StringToUInt256("5000")

        current_difficulty = ph.getDifficulty(
            timestamp=105,
            parent_timestamp=100,
            parent_difficulty=parent_difficulty)

        expected_difficulty = StringToUInt256("8125")

        print(parent_difficulty)
        print(expected_difficulty)
        print(current_difficulty)

        self.assertEqual(expected_difficulty, current_difficulty)

        boundary = ph.getBoundary(current_difficulty)

        expected_boundary = StringToUInt256(
            "14251334059977377898285659693376973274248613497309607881779394954820077494"
        )

        self.assertEqual(expected_boundary, boundary)
Ejemplo n.º 3
0
    def test_miner_verify(self):
        class CustomQMiner(Qryptominer):
            def __init__(self):
                Qryptominer.__init__(self)

            def handleEvent(self, event):
                if event.type == pyqryptonight.SOLUTION:
                    print("Hey a solution has been found!", event.nonce)
                    self.python_nonce = event.nonce
                return True

        input_bytes = [
            0x03, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09,
            0x03, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09,
            0x03, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09,
            0x03, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09,
            0x03, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09,
            0x03, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09,
            0x03, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09,
            0x03, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09
        ]
        target = [
            0x1E, 0xE5, 0x3F, 0xE1, 0xAC, 0xF3, 0x55, 0x92,
            0x66, 0xD8, 0x43, 0x89, 0xCE, 0xDE, 0x99, 0x33,
            0xC6, 0x8F, 0xC5, 0x1E, 0xD0, 0xA6, 0xC7, 0x91,
            0xF8, 0xF9, 0xE8, 0x9D, 0xB6, 0x23, 0xF0, 0x0C
        ]

        # Create a customized miner
        qm = CustomQMiner()

        # Set input bytes, nonce
        qm.start(input=input_bytes,
                 nonceOffset=0,
                 target=target,
                 thread_count=2)

        # Python can sleep or do something else.. the callback will happen in the background
        time.sleep(5)

        # This property has been just created in the python custom class when the event is received
        self.assertEqual(37, qm.python_nonce)
        self.assertEqual(True, qm.solutionAvailable())
        self.assertEqual(37, qm.solutionNonce())

        solution_input = list(qm.solutionInput())

        print("input_bytes    ", input_bytes)
        print("solution_input ", solution_input)
        print("target         ", target)
        print("solutionHash   ", qm.solutionHash())

        qn = Qryptonight()
        output = qn.hash(solution_input)
        print("raw     Hash   ", output)

        ph = PoWHelper()
        self.assertTrue(ph.verifyInput(solution_input, target))
        solution_input[4] = 0x29
        self.assertFalse(ph.verifyInput(solution_input, target))
Ejemplo n.º 4
0
    def get(measurement, parent_difficulty, dev_config):
        ph = PoWHelper(kp=dev_config.kp,
                       set_point=dev_config.block_timing_in_seconds)

        current_difficulty = ph.getDifficulty(measurement=measurement,
                                              parent_difficulty=parent_difficulty)

        current_target = ph.getTarget(current_difficulty)
        return current_difficulty, current_target
Ejemplo n.º 5
0
    def get(measurement,
            parent_difficulty):

        ph = PoWHelper(kp=config.dev.kp,
                       set_point=config.dev.mining_setpoint_blocktime)

        current_difficulty = ph.getDifficulty(measurement=measurement,
                                              parent_difficulty=parent_difficulty)

        current_target = ph.getTarget(current_difficulty)
        return current_difficulty, current_target
Ejemplo n.º 6
0
    def get(self,
            measurement,
            parent_difficulty):

        ph = PoWHelper(kp=config.dev.kp,
                       set_point=config.dev.mining_setpoint_blocktime)

        current_difficulty = ph.getDifficulty(measurement=measurement,
                                              parent_difficulty=parent_difficulty)

        current_target = ph.getBoundary(current_difficulty)
        return current_difficulty, current_target
Ejemplo n.º 7
0
    def test_target_1(self):
        ph = PoWHelper()

        difficulty = (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                      0x00, 0x00, 0x0F, 0x42, 0x40)

        target = ph.getTarget(difficulty)

        expected_target = (169, 158, 204, 63, 250, 38, 77, 131, 162, 121, 0,
                           139, 252, 250, 33, 54, 88, 56, 73, 243, 199, 180,
                           54, 141, 237, 181, 160, 247, 198, 16, 0, 0)

        self.assertEqual(expected_target, target)
Ejemplo n.º 8
0
    def test_boundary(self):
        ph = PoWHelper()

        difficulty = (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                      0x00, 0x00, 0x0F, 0x42, 0x40)

        boundary = ph.getBoundary(difficulty)

        expected_boundary = (0, 0, 16, 198, 247, 160, 181, 237, 141, 54, 180,
                             199, 243, 73, 56, 88, 54, 33, 250, 252, 139, 0,
                             121, 162, 131, 77, 38, 250, 63, 204, 158, 169)

        self.assertEqual(expected_boundary, boundary)
Ejemplo n.º 9
0
    def validate_mining_nonce(self, block, enable_logging=False):
        parent_metadata = self.state.get_block_metadata(block.prev_headerhash)
        parent_block = self.state.get_block(block.prev_headerhash)
        input_bytes = StringToUInt256(str(block.mining_nonce))[-4:] + tuple(block.mining_hash)

        measurement = self.state.get_measurement(block.timestamp, block.prev_headerhash)
        diff, target = self._difficulty_tracker.get(
            measurement=measurement,
            parent_difficulty=parent_metadata.block_difficulty)

        if enable_logging:
            logger.debug('-----------------START--------------------')
            logger.debug('Validate #%s', block.block_number)
            logger.debug('block.timestamp %s', block.timestamp)
            logger.debug('parent_block.timestamp %s', parent_block.timestamp)
            logger.debug('parent_block.difficulty %s', UInt256ToString(parent_metadata.block_difficulty))
            logger.debug('input_bytes %s', UInt256ToString(input_bytes))
            logger.debug('diff : %s | target : %s', UInt256ToString(diff), target)
            logger.debug('-------------------END--------------------')

        if not PoWHelper.verifyInput(input_bytes, target):
            if enable_logging:
                logger.warning("PoW verification failed")
                qn = Qryptonight()
                tmp_hash = qn.hash(input_bytes)
                logger.warning("{}".format(tmp_hash))
                logger.debug('%s', block.to_json())
            return False

        return True
Ejemplo n.º 10
0
class PoWValidator(object, metaclass=Singleton):
    def __init__(self):
        self.lock = threading.Lock()
        self._powv = PoWHelper()

    def verify_input(self, mining_blob, target):
        return self._verify_input_cached(mining_blob, target)

    @functools.lru_cache(maxsize=5)
    def _verify_input_cached(self, mining_blob, target):
        return self._powv.verifyInput(mining_blob, target)
Ejemplo n.º 11
0
    def test_adaptive_target(self):
        ph = PoWHelper()

        parent_difficulty = StringToUInt256("5000")

        current_difficulty = ph.getDifficulty(
            measurement=104, parent_difficulty=parent_difficulty)

        expected_difficulty = '4644'

        print(parent_difficulty)
        print(expected_difficulty)
        print(current_difficulty)

        self.assertEqual(expected_difficulty,
                         UInt256ToString(current_difficulty))

        target = ph.getTarget(current_difficulty)
        expected_target = "12766941454368345787240450318120704813017110301439674670851728194227068997120"

        self.assertEqual(expected_target, UInt256ToString(target))
Ejemplo n.º 12
0
def main():
    persistent_state = State()
    chain_manager = ChainManager(state=persistent_state)
    chain_manager.load(GenesisBlock())

    ph = PoWHelper()
    difficulty = StringToUInt256('5000')

    filename = os.path.expanduser(
        "~/crypto/qryptonight/modeling/blockdata.csv")

    with open(filename, 'w') as f:
        f.write("i,timestamp,prev_timestamp,delta,difficulty,target\n")
        prev_timestamp = None
        for i in range(chain_manager.height):
            block = chain_manager.get_block_by_number(i)

            if i == 0:
                prev_timestamp = block.blockheader.timestamp
                continue

            target = ph.getTarget(difficulty)
            delta = block.blockheader.timestamp - prev_timestamp

            outs = "{},{},{},{},{},{}\n".format(i, block.blockheader.timestamp,
                                                prev_timestamp, delta,
                                                UInt256ToString(difficulty),
                                                UInt256ToString(target))

            f.write(outs)

            difficulty = ph.getDifficulty(block.blockheader.timestamp,
                                          prev_timestamp, difficulty)
            difficulty = StringToUInt256(
                str(max(2, int(UInt256ToString(difficulty)))))
            prev_timestamp = block.blockheader.timestamp
Ejemplo n.º 13
0
 def test_target_2(self):
     ph = PoWHelper(kp=0, set_point=0)
     val = ph.getKp()
     print(val)
Ejemplo n.º 14
0
    def test_verify(self):
        class CustomQMiner(Qryptominer):
            def __init__(self):
                Qryptominer.__init__(self)
                self._solution_lock = threading.Lock()
                self.nonce = None
                self.solution_blob = None

            def start(self, input, nonceOffset, target, thread_count):
                self.cancel()
                try:
                    self._solution_lock.release()
                except RuntimeError:
                    pass
                self._solution_lock.acquire(blocking=False)
                super().start(input, nonceOffset, target, thread_count)

            def wait_for_solution(self):
                self._solution_lock.acquire(blocking=True)
                self._solution_lock.release()

            def solutionEvent(self, nonce):
                print('Solution Found %s', nonce)
                self.nonce = nonce
                self.solution_blob = self.solutionInput()
                self._solution_lock.release()

        block_timestamp = 1515443508
        parent_block_timestamp = 1515443508

        # This could be the average of last N blocks
        measurement = block_timestamp - parent_block_timestamp

        parent_difficulty = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4)

        new_diff, new_target = DifficultyTracker.get(
            measurement, parent_difficulty=parent_difficulty)

        self.assertEqual(new_diff,
                         (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5))

        self.assertEqual(
            new_target,
            (51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51,
             51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51))

        block_json = read_data_file('core/example_block_mining.json')

        block = Block.from_json(block_json)

        expected_blob = tuple([
            241, 93, 178, 239, 171, 183, 27, 87, 2, 191, 178, 157, 32, 74, 254,
            207, 242, 82, 128, 197, 58, 86, 24, 90, 106, 33, 58, 82, 160, 251,
            118, 174, 45, 182, 72, 157, 142, 141, 219, 0, 0, 0, 15, 61, 11, 20,
            166, 132, 14, 29, 248, 65, 55, 56, 226, 12, 57, 60, 37, 64, 123,
            44, 48, 172, 218, 221, 26, 8, 143, 110, 38, 215, 83, 248, 227, 87,
            148, 88, 237, 48, 203, 111, 245, 31, 125, 45, 14, 111, 109, 0, 87,
            13, 154, 252, 49, 160
        ])

        self.assertEqual(expected_blob, tuple(block.mining_blob))

        custom_qminer = CustomQMiner()
        custom_qminer.start(input=block.mining_blob,
                            nonceOffset=block.mining_nonce_offset,
                            target=new_target,
                            thread_count=2)
        custom_qminer.wait_for_solution()

        expected_mined_blob = bytearray(expected_blob)
        tmp_offset = config.dev.mining_nonce_offset
        expected_mined_blob[tmp_offset:tmp_offset +
                            4] = custom_qminer.nonce.to_bytes(4,
                                                              byteorder='big',
                                                              signed=False)

        print(custom_qminer.nonce)
        self.assertEqual(tuple(expected_mined_blob),
                         custom_qminer.solution_blob)
        self.assertTrue(PoWHelper().verifyInput(custom_qminer.solution_blob,
                                                new_target))
Ejemplo n.º 15
0
 def __init__(self):
     self.lock = threading.Lock()
     self._powv = PoWHelper()
 def __init__(self):
     self._powv = PoWHelper()
Ejemplo n.º 17
0
 def get_target(current_difficulty, dev_config):
     ph = PoWHelper(kp=dev_config.kp,
                    set_point=dev_config.block_timing_in_seconds)
     return ph.getTarget(current_difficulty)
Ejemplo n.º 18
0
 def get_target(current_difficulty):
     ph = PoWHelper(kp=config.dev.kp,
                    set_point=config.dev.mining_setpoint_blocktime)
     return ph.getTarget(current_difficulty)
Ejemplo n.º 19
0
 def get_boundary(current_difficulty):
     ph = PoWHelper(kp=config.dev.kp,
                    set_point=config.dev.mining_setpoint_blocktime)
     return ph.getBoundary(current_difficulty)
Ejemplo n.º 20
0
import os

from qrl.core.ChainManager import ChainManager
from qrl.core.GenesisBlock import GenesisBlock
from qrl.core.State import State
from pyqryptonight.pyqryptonight import PoWHelper, StringToUInt256, UInt256ToString

persistent_state = State()
chain_manager = ChainManager(state=persistent_state)
chain_manager.load(GenesisBlock())

ph = PoWHelper()
difficulty = StringToUInt256('5000')
delta = 0

filename = os.path.expanduser("~/crypto/qryptonight/modeling/blockdata.csv")

with open(filename, 'w') as f:
    f.write("i,timestamp,prev_timestamp,delta,difficulty,boundary\n")
    prev_timestamp = None
    for i in range(chain_manager.height):
        block = chain_manager.get_block_by_number(i)

        if i == 0:
            prev_timestamp = block.blockheader.timestamp
            continue

        boundary = ph.getBoundary(difficulty)
        delta = block.blockheader.timestamp - prev_timestamp

        outs = "{},{},{},{},{},{}\n".format(i,
Ejemplo n.º 21
0
 def verify_input_cached(self, mining_blob, target):
     return PoWHelper.verifyInput(mining_blob, target)
Ejemplo n.º 22
0
    def test_verify(self):

        class CustomQMiner(Qryptominer):
            def __init__(self):
                Qryptominer.__init__(self)
                self._solution_lock = threading.Lock()
                self.nonce = None
                self.solution_blob = None

            def start(self, input, nonceOffset, target, thread_count):
                self.cancel()
                try:
                    self._solution_lock.release()
                except RuntimeError:
                    pass
                self._solution_lock.acquire(blocking=False)
                super().start(input, nonceOffset, target, thread_count)

            def wait_for_solution(self):
                self._solution_lock.acquire(blocking=True)
                self._solution_lock.release()

            def handleEvent(self, event):
                if event.type == SOLUTION:
                    self.nonce = event.nonce
                    self.solution_blob = self.solutionInput()
                    self._solution_lock.release()

        block_timestamp = 1515443508
        parent_block_timestamp = 1515443508

        # This could be the average of last N blocks
        measurement = block_timestamp - parent_block_timestamp

        parent_difficulty = (0, 0, 0, 0, 0, 0, 0, 0,
                             0, 0, 0, 0, 0, 0, 0, 0,
                             0, 0, 0, 0, 0, 0, 0, 0,
                             0, 0, 0, 0, 0, 0, 0, 4)

        new_diff, new_target = DifficultyTracker.get(
            measurement,
            parent_difficulty=parent_difficulty)

        self.assertEqual(new_diff, (0, 0, 0, 0, 0, 0, 0, 0,
                                    0, 0, 0, 0, 0, 0, 0, 0,
                                    0, 0, 0, 0, 0, 0, 0, 0,
                                    0, 0, 0, 0, 0, 0, 0, 5))

        self.assertEqual(new_target, (
            51, 51, 51, 51, 51, 51, 51, 51,
            51, 51, 51, 51, 51, 51, 51, 51,
            51, 51, 51, 51, 51, 51, 51, 51,
            51, 51, 51, 51, 51, 51, 51, 51))

        block_json = read_data_file('core/example_block_mining.json')

        block = Block.from_json(block_json)

        expected_blob = (0, 231, 90, 101, 142, 20, 245, 183, 96, 5, 216, 159, 111, 239, 93, 217, 138, 10, 227, 159, 198,
                         207, 109, 238, 83, 220, 167, 148, 247, 200, 197, 41, 37, 36, 150, 12, 116, 85, 254, 0, 0, 0, 0,
                         0, 0, 0, 0, 0, 0, 0, 0, 181, 198, 40, 62, 106, 139, 108, 83, 216, 206, 161, 148, 50, 65, 212,
                         137, 94, 102, 124, 45, 51, 57, 43, 19, 51)
        self.assertEqual(expected_blob, tuple(block.mining_blob))

        custom_qminer = CustomQMiner()
        custom_qminer.start(input=block.mining_blob,
                            nonceOffset=block.mining_nonce_offset,
                            target=new_target,
                            thread_count=2)
        custom_qminer.wait_for_solution()

        expected_mined_blob = bytearray(expected_blob)
        tmp_offset = config.dev.mining_nonce_offset
        expected_mined_blob[tmp_offset:tmp_offset + 4] = custom_qminer.nonce.to_bytes(4,
                                                                                      byteorder='big',
                                                                                      signed=False)

        print(custom_qminer.nonce)
        self.assertEqual(tuple(expected_mined_blob), custom_qminer.solution_blob)
        self.assertTrue(PoWHelper().verifyInput(custom_qminer.solution_blob, new_target))
Ejemplo n.º 23
0
import os

from qrl.core.ChainManager import ChainManager
from qrl.core.GenesisBlock import GenesisBlock
from qrl.core.State import State
from pyqryptonight.pyqryptonight import PoWHelper, StringToUInt256, UInt256ToString

persistent_state = State()
chain_manager = ChainManager(state=persistent_state)
chain_manager.load(GenesisBlock())

ph = PoWHelper()
difficulty = StringToUInt256('5000')
delta = 0

filename = os.path.expanduser("~/crypto/qryptonight/modeling/blockdata.csv")

with open(filename, 'w') as f:
    f.write("i,timestamp,prev_timestamp,delta,difficulty,target\n")
    prev_timestamp = None
    for i in range(chain_manager.height):
        block = chain_manager.get_block_by_number(i)

        if i == 0:
            prev_timestamp = block.blockheader.timestamp
            continue

        target = ph.getTarget(difficulty)
        delta = block.blockheader.timestamp - prev_timestamp

        outs = "{},{},{},{},{},{}\n".format(i, block.blockheader.timestamp,