Exemple #1
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
    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))
Exemple #3
0
    def test_hash(self):
        qn = Qryptonight()

        input = [
            0x01, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09,
            0x01, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09,
            0x01, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09,
            0x01, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09,
            0x01, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09,
            0x01, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09,
            0x01, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09,
            0x01, 0x05, 0x07, 0x09, 0x03, 0x05, 0x07, 0x09
        ]
        output = qn.hash(input)

        output_expected = (
            0xc5, 0xbe, 0xe5, 0x37, 0x1c, 0x54, 0x7f, 0x1c,
            0x01, 0x71, 0x39, 0x38, 0x54, 0x68, 0xf9, 0xa9,
            0x8d, 0x57, 0xb2, 0x3c, 0xa6, 0xac, 0xb3, 0x80,
            0x91, 0x61, 0x8e, 0xd2, 0x81, 0x5d, 0x09, 0x46,
        )

        for i in output:
            print("0x{:02x}, ".format(i), sep='', end='')

        self.assertEqual(output_expected, output)
Exemple #4
0
    def test_hash2(self):
        qn = Qryptonight()

        input = [0x03, 0x05, 0x07, 0x09] * 30
        output = qn.hash(input)

        output_expected = (255, 253, 83, 78, 29, 24, 160, 224, 30, 240, 158,
                           39, 233, 125, 90, 170, 78, 59, 157, 146, 97, 86,
                           205, 161, 160, 155, 48, 144, 51, 148, 155, 99)

        self.assertEqual(output_expected, output)
Exemple #5
0
    def test_hash3(self):
        qn = Qryptonight()

        input = [0x03, 0x05, 0x07, 0x09] * 200
        output = qn.hash(input)

        output_expected = (216, 31, 227, 138, 9, 118, 5, 200, 136, 40, 156,
                           168, 86, 35, 146, 223, 199, 76, 188, 213, 25, 117,
                           247, 195, 15, 183, 236, 219, 104, 212, 75, 211)

        self.assertEqual(output_expected, output)
    def test_hash(self):
        qn = Qryptonight()

        input = [0x03, 0x05, 0x07, 0x09]
        output = qn.hash(input)

        output_expected = (0x3E, 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, 0xF6)

        self.assertEqual(output_expected, output)
Exemple #7
0
    def test_hash(self):
        qn = Qryptonight()

        input = [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]
        output = qn.hash(input)

        output_expected = (
            0x96, 0x76, 0x53, 0x26, 0x2F, 0x9B, 0x15, 0x90,
            0xB9, 0x88, 0x0F, 0x64, 0xA3, 0x80, 0x8C, 0x4B,
            0x01, 0xEA, 0x29, 0x2C, 0x48, 0xFC, 0x7C, 0x47,
            0x0D, 0x25, 0x50, 0x00, 0x57, 0xCA, 0x07, 0x70,
        )

        self.assertEqual(output_expected, output)
    def validate_mining_nonce(self,
                              state: State,
                              blockheader: BlockHeader,
                              enable_logging=False):
        parent_metadata = state.get_block_metadata(
            blockheader.prev_blockheaderhash)
        parent_block = state.get_block(blockheader.prev_blockheaderhash)

        measurement = state.get_measurement(blockheader.timestamp,
                                            blockheader.prev_blockheaderhash,
                                            parent_metadata)
        diff, target = DifficultyTracker.get(
            measurement=measurement,
            parent_difficulty=parent_metadata.block_difficulty)

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

        if not self.verify_input_cached(blockheader.mining_blob, target):
            if enable_logging:
                logger.warning("PoW verification failed")
                qn = Qryptonight()
                tmp_hash = qn.hash(blockheader.mining_blob)
                logger.warning("{}".format(tmp_hash))
                logger.debug('%s', blockheader.to_json())
            return False

        return True
Exemple #9
0
 def test_init(self):
     qn = Qryptonight()
     self.assertIsNotNone(qn)
Exemple #10
0
 def test_init(self):
     qn = Qryptonight()
Exemple #11
0
 def calc_hash(input_bytes):
     qn = Qryptonight()
     return qn.hash(input_bytes)