Example #1
0
def test_if_correct_hash_is_found_then_block_found_function_should_be_called_once(
):
    from dokuztas.blockchain import Blockchain, PendingBlock
    patcher = patch('dokuztas.node.NodeComponent.block_found')
    mock = patcher.start()
    node = NodeComponent(miner=True)
    node.chain = Blockchain(difficulty=4)
    node.chain._generate_genesis()
    pending_block = PendingBlock()
    pending_block.add_txs(['a', 'b', 'c'])
    node.pending_blocks.append(pending_block)

    def sync_runner():
        def always_mine():
            return False

        node.chain.mine(node.pending_blocks[0], always_mine, mock)

    mine_patcher = patch('dokuztas.node.NodeComponent.mine',
                         side_effect=sync_runner)
    mine_patcher.start()
    node.mine()
    patcher.stop()
    mine_patcher.stop()
    assert mock.called
Example #2
0
def test_any_other_node_found_correct_hash_then_mining_should_be_stopped():
    from dokuztas.blockchain import Blockchain, PendingBlock, Block
    node = NodeComponent(miner=True)
    node.chain = Blockchain(difficulty=10)

    def adds_genesis():
        genesis = Block(id=0,
                        blockhash=0,
                        previous_hash=0,
                        nonce=0,
                        merkleroot=0,
                        data=['genesis'])
        node.chain.blocks.append(genesis)

    genesis_patcher = patch('dokuztas.node.NodeComponent.create_genesis_chain',
                            side_effect=adds_genesis)
    genesis_patcher.start()
    node.create_genesis_chain()
    genesis_patcher.stop()

    pending_block = PendingBlock()
    pending_block.add_txs(['a', 'b', 'c'])
    node.pending_blocks.append(pending_block)
    node.mine()
    block_to_add = Block(id=123,
                         previous_hash=0,
                         nonce=123,
                         merkleroot=123,
                         blockhash=111,
                         data=['a', 'b', 'c'])
    node.block_added(block_to_add)
    assert node.chain.blocks[1].blockhash == 111
    assert len(node.chain.blocks) == 2
def test_mine_tester():
    def always_mine():
        return False

    chain = Blockchain()
    chain._generate_genesis()
    p = PendingBlock()
    p.add_txs(['a', 'b', 'c', 'd', 'f'])
    chain.mine(p, always_mine)
    assert chain.blocks[1].nonce == 13106
    assert chain.blocks[
        1].blockhash == '00005df8b04cb42e62c5be0766af2caa884dd52b51b7ff1549aab3c77e88b84d'
Example #4
0
def test_mine_tester():
    def always_mine():
        return False

    chain = Blockchain()
    chain._generate_genesis()
    p = PendingBlock()
    p.add_txs(['a', 'b', 'c', 'd', 'f'])
    chain.mine(p, always_mine)
    assert chain.blocks[1].nonce == 98346
    assert chain.blocks[
        1].blockhash == '0000ce2e9b894dcce5c482649d6f06bdb8c84215574b235cd92e616c29a02f26'
Example #5
0
def test_for_mining_it_should_be_start_new_thread():
    from dokuztas.blockchain import Blockchain, PendingBlock
    patcher = patch('dokuztas._internals.MiningThread.start')
    mock = patcher.start()
    node = NodeComponent(miner=True)
    node.chain = Blockchain(difficulty=1)
    node.chain._generate_genesis()
    pending_block = PendingBlock()
    pending_block.add_txs(['a', 'b', 'c'])
    node.pending_blocks.append(pending_block)
    node.mine()
    patcher.stop()
    assert mock.called
Example #6
0
def test_any_other_node_found_correct_hash_then_mining_should_be_stopped():
    from dokuztas.blockchain import Blockchain, PendingBlock, Block
    node = NodeComponent(miner=True)
    node.chain = Blockchain(difficulty=10)
    node.chain._generate_genesis()
    pending_block = PendingBlock()
    pending_block.add_txs(['a', 'b', 'c'])
    node.pending_blocks.append(pending_block)
    node.mine()
    block_to_add = Block(id=123,
                         previous_hash=0,
                         nonce=123,
                         merkleroot=123,
                         blockhash=111,
                         data=['a', 'b', 'c'])
    node.block_added(block_to_add)
    assert node.chain.blocks[1].blockhash == 111
    assert len(node.chain.blocks) == 2
Example #7
0
    def mine(self):
        """
        Mine işleminin başlatıldığı yerdir. Bu işlemin blockchain objesi tarafından yönetilmemesinin sebebi,
        ilerde node'ların, transaction fee'ye göre mine etme veya mine etmek istedikleri block'ları kendilerinin
        seçebilmesi gibi özellikleri olabilmesi ihtimalidir. Şu an için roadmap'te böyle bir özellik bulunmamaktadır.
        """
        self.miner_check()

        if len(self.pending_blocks) > 0:
            self.stop_mining = False
            self._internal_mine(args=(self.pending_blocks[0],
                                      self.terminate_mining, self.block_found))
        elif len(self.pending_txs) > 0:
            self.stop_mining = False
            temp_block = PendingBlock()
            temp_block.add_txs(self.pending_txs)
            self.pending_txs = []
            self._internal_mine(args=(temp_block, self.terminate_mining,
                                      self.block_found))
Example #8
0
    def add_transaction(self, tx):
        """
        Mine edilmesi için yeni bir transaction ekemek içindir.
        Her bekleyen transaction'ı, bir (1) block'a çevirir ve bu şekilde bekletir.

        Mine işlemini, tx sayısı 10'a ulaştığında bir kez tetikler. Sonrasında mine bir döngü şeklinde çalışmaya devam eder.

        :param tx: Mine edilesi için eklenen transaction.
        """
        self.miner_check()

        self.pending_txs.append(tx)

        if len(self.pending_txs) > 10:
            p_block = PendingBlock()
            p_block.add_txs(self.pending_txs)
            self.pending_blocks.append(p_block)
            self.pending_txs = []

            if len(self.pending_blocks) == 1:
                self.mine()
        def func_wrapper():
            def always_mine():
                return False

            chain = Blockchain()
            chain._generate_genesis()
            p = PendingBlock()
            p.add_txs(['a', 'b', 'c', 'd', 'f'])
            chain.mine(p, always_mine)

            p2 = PendingBlock()
            p2.add_txs(['y', 'z'])
            chain.mine(p2, always_mine)

            func(chain)