def test_blocks_ids_should_increase_one_by_one(): chain = Blockchain() block_id_one = Block() block_id_two = Block() chain.add_block(block_id_one) chain.add_block(block_id_two) assert block_id_one.id == 1 assert block_id_two.id == 2
def test_each_block_s_previous_hash_property_should_be_previous_block_s_hash(): chain = Blockchain() block_id_zero = Block() block_id_one = Block() block_id_two = Block() chain.add_block(block_id_zero) chain.add_block(block_id_one) chain.add_block(block_id_two) assert block_id_one.previous_hash == block_id_zero.hash assert block_id_two.previous_hash == block_id_one.hash
def test_block_hash_should_be_created_by_id_prvhash_and_nonce(): block = Block() block.id = 100 block.previous_hash = 100 block.data = {'name': 'Onur', 'surname': 'Aykaç'} block.nonce = 100 block.calculate_hash() assert block.hash == "1477b8d1c6b66c77015684ecaae6b23d6373bcab23d067f3926cf8cdd6761b07"
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_while_mining_new_txs_can_be_adding(node): from dokuztas.blockchain import Block assert len(node.pending_txs) == 1 # Mevcut mining'i durdurmak için. 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)
def adds_genesis(): genesis = Block(id=0, blockhash=0, previous_hash=0, nonce=0, merkleroot=0, data=['genesis']) node.chain.blocks.append(genesis)
def test_if_the_blocks_are_not_changed_then_chain_should_be_a_valid_chain(): chain = Blockchain() block_id_zero = Block() block_id_one = Block() block_id_two = Block() chain.add_block(block_id_zero) first_validation = chain.validate() assert first_validation == True chain.add_block(block_id_one) second_validation = chain.validate() assert second_validation == True chain.add_block(block_id_two) third_validation = chain.validate() assert third_validation == True
def test_if_a_block_is_changed_then_chain_should_not_be_a_valid_chain(): chain = Blockchain() block_id_zero = Block() block_id_one = Block() block_id_two = Block() chain.add_block(block_id_zero) chain.add_block(block_id_one) chain.add_block(block_id_two) block_id_one.data = {'hacked'} block_id_one.calculate_hash() validation = chain.validate() assert validation == False
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
def test_block_data_should_be_dict(): data = {'name': 'Onur', 'surname': 'Aykaç'} block = Block(data=data) assert isinstance(block.data, dict)
def test_genesis_block_s_previous_block_hash_should_be_assigned_zero(): chain = Blockchain() genesis_block = Block() chain.add_block(genesis_block) assert chain.blocks[0].previous_hash == 0
def test_when_genesis_block_is_added_then_genesis_block_id_should_be_assigned_zero( ): chain = Blockchain() genesis_block = Block() chain.add_block(genesis_block) assert chain.blocks[0].id == 0