def test_create(self, time_mock):
        b = BlockHeader.create(blocknumber=1, prev_headerhash=b'headerhash', prev_timestamp=10,
                               hashedtransactions=b'some_data', fee_reward=1)
        self.assertIsNotNone(b)

        b = BlockHeader.create(blocknumber=1, prev_headerhash=b'headerhash', prev_timestamp=10,
                               hashedtransactions=b'some_data', fee_reward=1)
        self.assertEqual(b.epoch, 0)
Beispiel #2
0
 def test_init2(self):
     with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
         time_mock.return_value = 1615270948
         block_header = BlockHeader.create(1, sha256(b'prev'),
                                           time_mock.return_value,
                                           sha256(b'txs'), 10)
         self.assertIsNotNone(block_header)  # just to avoid warnings
    def test_GetBlockMiningCompatible(self):
        p2p_factory = Mock(spec=P2PFactory)
        p2p_factory.sync_state = SyncState()
        p2p_factory.num_connections = 23
        p2p_factory.pow = Mock()

        chain_manager = Mock(spec=ChainManager)
        chain_manager.height = 0
        chain_manager.get_last_block = MagicMock(return_value=Block())

        qrlnode = QRLNode(mining_address=b'')
        qrlnode.set_chain_manager(chain_manager)
        qrlnode._p2pfactory = p2p_factory
        qrlnode._pow = p2p_factory.pow

        block_header = BlockHeader.create(blocknumber=10,
                                          prev_headerhash=sha256(b'prevblock'),
                                          prev_timestamp=1234567890,
                                          hashedtransactions=sha256(b'tx1'),
                                          fee_reward=1)

        qrlnode.get_blockheader_and_metadata = MagicMock(
            return_value=[block_header, BlockMetadata()])

        service = MiningAPIService(qrlnode)
        req = qrlmining_pb2.GetBlockMiningCompatibleReq(height=10)

        answer = service.GetBlockMiningCompatible(request=req, context=None)

        self.assertEqual(10, answer.blockheader.block_number)
        self.assertEqual(1, answer.blockheader.reward_fee)
 def test_blob(self, time_mock):
     block_header = BlockHeader.create(1, sha256(b'prev'),
                                       time_mock.return_value,
                                       sha256(b'txs'), 10)
     self.assertEquals(
         '00501846b24200c31fca7172a7f701ae50322579cfdf1d7777daab4ce6ead70b76debb2c51a1'
         'c700000000000000000000000000000000002b80aecec05ad5c7c4f2259c8f69e2966a6ce102',
         bin2hstr(block_header.mining_blob))
     self.assertEquals(config.dev.mining_blob_size,
                       len(block_header.mining_blob))
Beispiel #5
0
    def test_create(self, time_mock):
        b = BlockHeader.create(dev_config=config.dev,
                               blocknumber=1,
                               prev_headerhash=b'headerhash',
                               prev_timestamp=10,
                               hashedtransactions=b'some_data',
                               fee_reward=1,
                               seed_height=0,
                               seed_hash=None)
        self.assertIsNotNone(b)

        b = BlockHeader.create(dev_config=config.dev,
                               blocknumber=1,
                               prev_headerhash=b'headerhash',
                               prev_timestamp=10,
                               hashedtransactions=b'some_data',
                               fee_reward=1,
                               seed_height=0,
                               seed_hash=None)
Beispiel #6
0
 def test_create_fails_when_prev_timestamp_is_negative(self, time_mock):
     # The only way to get it to fail in this mode is to pass a negative timestamp. Which should never happen IRL.
     time_mock.return_value = 0
     b = BlockHeader.create(config.dev,
                            1,
                            sha256(b'prev'),
                            -10,
                            sha256(b'txs'),
                            10,
                            seed_height=0,
                            seed_hash=None)
     self.assertIsNone(b)
Beispiel #7
0
    def test_GetLastBlockHeader(self):
        self.block_header_params["blocknumber"] = 20
        block_header = BlockHeader.create(**self.block_header_params)
        self.chain_manager.height = 200
        self.qrlnode.get_blockheader_and_metadata = MagicMock(
            return_value=[block_header, BlockMetadata()])

        req = qrlmining_pb2.GetLastBlockHeaderReq(height=20)
        answer = self.service.GetLastBlockHeader(request=req, context=None)

        self.assertEqual(180, answer.depth)
        self.assertEqual(200, answer.height)
Beispiel #8
0
    def test_blob(self):
        with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
            time_mock.return_value = 1615270948

            block_header = BlockHeader.create(1, sha256(b'prev'),
                                              sha256(b'txs'), 10)
            self.assertEquals(
                '74aa496ffe19107faaf418b720fb5b8446ba4b595c178fcf099c99b3dee99860d788c77910a9ed000000'
                '000000000000000000e0d022b37421b81b7bbcf5b497fb89408c05c7d713c5e1e5187b02aa9344cf83bb',
                bin2hstr(block_header.mining_blob))
            self.assertEquals(config.dev.mining_blob_size,
                              len(block_header.mining_blob))
Beispiel #9
0
    def create(staking_address: bytes, block_number: int, reveal_hash: bytes,
               prevblock_headerhash: bytes, transactions: list,
               duplicate_transactions: OrderedDict, vote: VoteMetadata,
               signing_xmss: XMSS, nonce: int):

        block = Block()
        block._data.transactions.extend([qrl_pb2.Transaction()
                                         ])  # FIXME: Empty for coinbase?

        # Process transactions
        hashedtransactions = []
        fee_reward = 0

        for tx in transactions:
            if tx.subtype == qrl_pb2.Transaction.TRANSFER:
                fee_reward += tx.fee
            hashedtransactions.append(tx.txhash)
            block._data.transactions.extend(
                [tx.pbdata])  # copy memory rather than sym link

        if not hashedtransactions:
            hashedtransactions = [sha256(b'')]

        txs_hash = merkle_tx_hash(
            hashedtransactions)  # FIXME: Find a better name, type changes

        for tx in duplicate_transactions.values(
        ):  # TODO: Add merkle hash for dup txn
            block._data.duplicate_transactions.extend([tx.pbdata])

        for staker in vote.stake_validator_vote:  # TODO: Add merkle hash for vote
            block._data.vote.extend([vote.stake_validator_vote[staker].pbdata])

        tmp_blockheader = BlockHeader.create(
            staking_address=staking_address,
            blocknumber=block_number,
            reveal_hash=reveal_hash,
            prev_blockheaderhash=prevblock_headerhash,
            hashedtransactions=txs_hash,
            fee_reward=fee_reward)

        block._data.header.MergeFrom(tmp_blockheader.pbdata)

        # Prepare coinbase tx
        coinbase_tx = CoinBase.create(tmp_blockheader, signing_xmss)
        coinbase_tx.pbdata.nonce = nonce
        coinbase_tx.sign(signing_xmss)  # Sign after nonce has been set

        # Replace first tx
        block._data.transactions[0].CopyFrom(coinbase_tx.pbdata)

        return block
Beispiel #10
0
    def test_blob(self):
        with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
            time_mock.return_value = 1615270948

            block_header = BlockHeader.create(1, sha256(b'prev'),
                                              time_mock.return_value,
                                              sha256(b'txs'), 10)
            self.assertEquals(
                '00501846b24200c31fca7172a7f701ae50322579cfdf1d7777daab4ce6ead70b76debb2c51a1'
                'c70000000000000000000000002b80aecec05ad5c7c4f2259c8f69e2966a6ce102d4609af2cd',
                bin2hstr(block_header.mining_blob))
            self.assertEquals(config.dev.mining_blob_size,
                              len(block_header.mining_blob))
Beispiel #11
0
    def test_hash(self, time_mock):
        block_header = BlockHeader.create(1, sha256(b'prev'),
                                          time_mock.return_value,
                                          sha256(b'txs'), 10)
        header_hash = block_header.generate_headerhash()

        self.assertEquals(
            'ac021e63df860ea930ea9de05e350d3f74af35341688134f92957f1dac3a62fb',
            bin2hstr(header_hash))

        self.assertEquals(bin2hstr(header_hash),
                          bin2hstr(block_header.headerhash))

        self.assertEquals(32, len(block_header.headerhash))
Beispiel #12
0
    def setUp(self):
        with mock.patch('qrl.core.misc.ntp.getTime', return_value=1615270948) as time_mock:
            self.block_header = BlockHeader.create(1, sha256(b'prev'), time_mock.return_value, sha256(b'txs'), 10)

        self.fee_reward = 10
        self.coinbase_amount = self.block_header.block_reward + self.fee_reward

        # this variable is for validate_parent_child_relation()
        self.m_parent_block = Mock(
            name='mock Parent Block',
            block_number=self.block_header.block_number - 1,
            headerhash=self.block_header.prev_headerhash,
            timestamp=self.block_header.timestamp - 1
        )
Beispiel #13
0
    def update_mining_address(self, mining_address: bytes):
        self.transactions[0].update_mining_address(mining_address)
        hashedtransactions = []

        for tx in self.transactions:
            hashedtransactions.append(tx.transaction_hash)

        tmp_blockheader = BlockHeader.create(
            blocknumber=self.block_number,
            prev_blockheaderhash=self.prev_headerhash,
            hashedtransactions=merkle_tx_hash(hashedtransactions),
            fee_reward=self.fee_reward)

        self._data.header.MergeFrom(tmp_blockheader.pbdata)
Beispiel #14
0
    def test_hash(self):
        with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
            time_mock.return_value = 1615270948

            block_header = BlockHeader.create(1, sha256(b'prev'),
                                              sha256(b'txs'), 10)
            header_hash = block_header.generate_headerhash()

            self.assertEquals(
                '584f898a54269d0651cca3403843d4cdb764e5a31655bf51db39bbf8c4883b01',
                bin2hstr(header_hash))

            self.assertEquals(bin2hstr(header_hash),
                              bin2hstr(block_header.headerhash))

            self.assertEquals(32, len(block_header.headerhash))
Beispiel #15
0
    def create(dev_config: DevConfig, block_number: int,
               prev_headerhash: bytes, prev_timestamp: int, transactions: list,
               miner_address: bytes, seed_height: Optional[int],
               seed_hash: Optional[bytes]):

        block = Block()

        # Process transactions
        hashedtransactions = []
        fee_reward = 0

        for tx in transactions:
            fee_reward += tx.fee

        # Prepare coinbase tx
        total_reward_amount = BlockHeader.block_reward_calc(
            block_number, dev_config) + fee_reward
        coinbase_tx = CoinBase.create(dev_config, total_reward_amount,
                                      miner_address, block_number)
        hashedtransactions.append(coinbase_tx.txhash)
        Block._copy_tx_pbdata_into_block(
            block, coinbase_tx)  # copy memory rather than sym link

        for tx in transactions:
            hashedtransactions.append(tx.txhash)
            Block._copy_tx_pbdata_into_block(
                block, tx)  # copy memory rather than sym link

        txs_hash = merkle_tx_hash(
            hashedtransactions)  # FIXME: Find a better name, type changes

        tmp_blockheader = BlockHeader.create(dev_config=dev_config,
                                             blocknumber=block_number,
                                             prev_headerhash=prev_headerhash,
                                             prev_timestamp=prev_timestamp,
                                             hashedtransactions=txs_hash,
                                             fee_reward=fee_reward,
                                             seed_height=seed_height,
                                             seed_hash=seed_hash)

        block.blockheader = tmp_blockheader

        block._data.header.MergeFrom(tmp_blockheader.pbdata)

        block.set_nonces(dev_config, 0, 0)

        return block
Beispiel #16
0
    def test_hash(self):
        with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
            time_mock.return_value = 1615270948

            block_header = BlockHeader.create(1, sha256(b'prev'),
                                              time_mock.return_value,
                                              sha256(b'txs'), 10)
            header_hash = block_header.generate_headerhash()

            self.assertEquals(
                'eb2364355673f1d4384008dbc53a19050c6d1aaea01c724943c9a8f5d01fdece',
                bin2hstr(header_hash))

            self.assertEquals(bin2hstr(header_hash),
                              bin2hstr(block_header.headerhash))

            self.assertEquals(32, len(block_header.headerhash))
Beispiel #17
0
    def test_hash_nonce(self, time_mock):
        block_header = BlockHeader.create(1, sha256(b'prev'),
                                          time_mock.return_value,
                                          sha256(b'txs'), 10)

        block_header.set_nonces(100, 0)

        header_hash = block_header.generate_headerhash()

        self.assertEquals(
            'b6f937020f9876f3c6887e7a6759201411ed8826ed9ce4283ffe48e1aa90d692',
            bin2hstr(header_hash))

        self.assertEquals(bin2hstr(header_hash),
                          bin2hstr(block_header.headerhash))

        self.assertEquals(32, len(block_header.headerhash))
Beispiel #18
0
    def test_hash(self):
        with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
            time_mock.return_value = 1615270948

            block_header = BlockHeader.create(1, sha256(b'prev'),
                                              time_mock.return_value,
                                              sha256(b'txs'), 10)
            header_hash = block_header.generate_headerhash()

            self.assertEquals(
                '81dd8032691331fb9cb6d4b8c3cf82dfec7873eb96789a10076b70da45315a38',
                bin2hstr(header_hash))

            self.assertEquals(bin2hstr(header_hash),
                              bin2hstr(block_header.headerhash))

            self.assertEquals(32, len(block_header.headerhash))
Beispiel #19
0
    def create(block_number: int, prev_block_headerhash: bytes,
               prev_block_timestamp: int, transactions: list,
               miner_address: bytes):

        block = Block()

        # Process transactions
        hashedtransactions = []
        fee_reward = 0

        for tx in transactions:
            fee_reward += tx.fee

        # Prepare coinbase tx
        total_reward_amount = BlockHeader.block_reward_calc(
            block_number) + fee_reward
        coinbase_tx = CoinBase.create(total_reward_amount, miner_address,
                                      block_number)
        hashedtransactions.append(coinbase_tx.txhash)
        block._data.transactions.extend([coinbase_tx.pbdata
                                         ])  # copy memory rather than sym link

        for tx in transactions:
            hashedtransactions.append(tx.txhash)
            block._data.transactions.extend(
                [tx.pbdata])  # copy memory rather than sym link

        txs_hash = merkle_tx_hash(
            hashedtransactions)  # FIXME: Find a better name, type changes

        tmp_blockheader = BlockHeader.create(
            blocknumber=block_number,
            prev_block_headerhash=prev_block_headerhash,
            prev_block_timestamp=prev_block_timestamp,
            hashedtransactions=txs_hash,
            fee_reward=fee_reward)

        block.blockheader = tmp_blockheader

        block._data.header.MergeFrom(tmp_blockheader.pbdata)

        block.set_nonces(0, 0)

        return block
    def create(block_number: int, prevblock_headerhash: bytes,
               transactions: list, signing_xmss: XMSS, master_address: bytes,
               nonce: int):

        block = Block()
        block._data.transactions.extend([qrl_pb2.Transaction()
                                         ])  # FIXME: Empty for coinbase?

        # Process transactions
        hashedtransactions = []
        fee_reward = 0

        for tx in transactions:
            fee_reward += tx.fee
            hashedtransactions.append(tx.txhash)
            block._data.transactions.extend(
                [tx.pbdata])  # copy memory rather than sym link

        if not hashedtransactions:
            hashedtransactions = [sha256(b'')]

        txs_hash = merkle_tx_hash(
            hashedtransactions)  # FIXME: Find a better name, type changes

        tmp_blockheader = BlockHeader.create(
            blocknumber=block_number,
            PK=signing_xmss.pk,
            prev_blockheaderhash=prevblock_headerhash,
            hashedtransactions=txs_hash,
            fee_reward=fee_reward)

        block._data.header.MergeFrom(tmp_blockheader.pbdata)

        # Prepare coinbase tx
        coinbase_tx = CoinBase.create(tmp_blockheader, signing_xmss,
                                      master_address)
        coinbase_tx.pbdata.nonce = nonce
        coinbase_tx.sign(signing_xmss)  # Sign after nonce has been set

        # Replace first tx
        block._data.transactions[0].CopyFrom(coinbase_tx.pbdata)

        return block
Beispiel #21
0
    def test_GetBlockMiningCompatible(self):
        block_header = BlockHeader.create(**self.block_header_params)
        self.qrlnode.get_blockheader_and_metadata = MagicMock(
            return_value=[block_header, BlockMetadata()])

        req = qrlmining_pb2.GetBlockMiningCompatibleReq(height=10)
        answer = self.service.GetBlockMiningCompatible(request=req,
                                                       context=None)

        self.assertEqual(10, answer.blockheader.block_number)
        self.assertEqual(1, answer.blockheader.reward_fee)

        # if QRLNode responds with None, None, the GRPC response should be blank too
        self.qrlnode.get_blockheader_and_metadata = MagicMock(
            return_value=[None, None])
        answer = self.service.GetBlockMiningCompatible(request=req,
                                                       context=None)
        self.assertEqual(0, answer.blockheader.block_number)
        self.assertEqual(0, answer.blockheader.reward_fee)
Beispiel #22
0
    def create(mining_nonce: int,
               block_number: int,
               prevblock_headerhash: bytes,
               transactions: list,
               signing_xmss: XMSS,
               master_address: bytes,
               nonce: int):

        block = Block()
        block._data.transactions.extend([qrl_pb2.Transaction()])  # FIXME: Empty for coinbase?

        # Process transactions
        hashedtransactions = []
        fee_reward = 0

        for tx in transactions:
            fee_reward += tx.fee
            hashedtransactions.append(tx.txhash)
            block._data.transactions.extend([tx.pbdata])  # copy memory rather than sym link

        if not hashedtransactions:
            hashedtransactions = [sha256(b'')]

        txs_hash = merkle_tx_hash(hashedtransactions)           # FIXME: Find a better name, type changes

        tmp_blockheader = BlockHeader.create(blocknumber=block_number,
                                             mining_nonce=mining_nonce,
                                             PK=signing_xmss.pk(),
                                             prev_blockheaderhash=prevblock_headerhash,
                                             hashedtransactions=txs_hash,
                                             fee_reward=fee_reward)

        block._data.header.MergeFrom(tmp_blockheader.pbdata)

        # Prepare coinbase tx
        coinbase_tx = CoinBase.create(tmp_blockheader, signing_xmss, master_address)
        coinbase_tx.pbdata.nonce = nonce
        coinbase_tx.sign(signing_xmss)  # Sign after nonce has been set

        # Replace first tx
        block._data.transactions[0].CopyFrom(coinbase_tx.pbdata)

        return block
Beispiel #23
0
    def test_hash_nonce(self):
        with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
            time_mock.return_value = 1615270948

            block_header = BlockHeader.create(1, sha256(b'prev'),
                                              sha256(b'txs'), 10)

            block_header.set_nonces(100, 0)

            header_hash = block_header.generate_headerhash()

            self.assertEquals(
                '28556460d0b3b4830ef3d74fab1cd52bca25ee00d1a6396a7f053d53549f73ba',
                bin2hstr(header_hash))

            self.assertEquals(bin2hstr(header_hash),
                              bin2hstr(block_header.headerhash))

            self.assertEquals(32, len(block_header.headerhash))
Beispiel #24
0
    def test_hash_nonce(self):
        with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
            time_mock.return_value = 1615270948

            block_header = BlockHeader.create(1, sha256(b'prev'),
                                              time_mock.return_value,
                                              sha256(b'txs'), 10)

            block_header.set_nonces(100, 0)

            header_hash = block_header.generate_headerhash()

            self.assertEquals(
                'f48ef2a482b2b85b429979ea1d7014806754b3ff37705c4f61a54f17bca4ccc4',
                bin2hstr(header_hash))

            self.assertEquals(bin2hstr(header_hash),
                              bin2hstr(block_header.headerhash))

            self.assertEquals(32, len(block_header.headerhash))
Beispiel #25
0
    def test_hash_nonce(self):
        with mock.patch('qrl.core.misc.ntp.getTime') as time_mock:
            time_mock.return_value = 1615270948

            block_header = BlockHeader.create(1, sha256(b'prev'),
                                              time_mock.return_value,
                                              sha256(b'txs'), 10)

            block_header.set_nonces(100, 0)

            header_hash = block_header.generate_headerhash()

            self.assertEquals(
                '8ab90d398ef7d219fb985a15814525b42f597a44478599a4a25b4b0a10697cf2',
                bin2hstr(header_hash))

            self.assertEquals(bin2hstr(header_hash),
                              bin2hstr(block_header.headerhash))

            self.assertEquals(32, len(block_header.headerhash))
Beispiel #26
0
 def test_create_uses_prev_timestamp_when_genesis_block(self, time_mock):
     genesis = BlockHeader.create(0, sha256(b'Random Scifi Book Title'), time_mock.return_value, sha256(b'txs'), 10)
     self.assertEqual(genesis.timestamp, time_mock.return_value)
Beispiel #27
0
 def test_init2(self, time_mock):
     block_header = BlockHeader.create(1, sha256(b'prev'),
                                       time_mock.return_value,
                                       sha256(b'txs'), 10)
     self.assertIsNotNone(block_header)  # just to avoid warnings
Beispiel #28
0
 def test_init2(self):
     block_header = BlockHeader.create(1, sha256(b'prev'), sha256(b'txs'),
                                       10)
     self.assertIsNotNone(block_header)  # just to avoid warnings
Beispiel #29
0
 def test_block_reward_calc_genesis_is_total_coin_supply(self, time_mock):
     genesis = BlockHeader.create(0, sha256(b'Random Scifi Book Title'), time_mock.return_value, sha256(b'txs'), 10)
     self.assertEqual(config.dev.supplied_coins, genesis.block_reward)