def repair_block(self, block): previous = self[block.identifier]['previous'] if previous and self.block_is_broken(previous): print('When trying to repair block {}, found that previous block {} also needed repair'.format( block.identifier, previous.identifier, )) self.repair_block(previous) self[block.identifier]['previous_hash'] = previous.hash() if self.block_is_broken(block): print('Repairing block {}'.format(block.identifier)) block.mine()
def test_add_block2(self): target = 10**72 # Ensures that block_count is being initializes correctly self.assertEqual(0, self.bc.block_count) # Ensures that last_block member of blockchain is empty when initialized self.assertEqual(b'', self.bc.last_block) # Creates 4 blocks and add to file b1 = mine(0, hash_SHA("Root".encode()), "Block1".encode(), target) self.bc.add_block(b1) # Ensures that block count is being updated correctly self.assertEqual(1, self.bc.block_count) # Ensures that last_block member of blockchain properly updated self.assertEqual(b1, self.bc.last_block) b2 = mine(0, hash_SHA("Block1".encode()), "Block2".encode(), target) self.bc.add_block(b2) # Ensures that block count is being updated correctly self.assertEqual(2, self.bc.block_count) # Ensures that last_block member of blockchain properly updated self.assertEqual(b2, self.bc.last_block) b3 = mine(0, hash_SHA("Block2".encode()), "Block3".encode(), target) self.bc.add_block(b3) # Ensures that block count is being updated correctly self.assertEqual(3, self.bc.block_count) # Ensures that last_block member of blockchain properly updated self.assertEqual(b3, self.bc.last_block) b4 = mine(0, hash_SHA("Block3".encode()), "Block4".encode(), target) self.bc.add_block(b4) # Ensures that block count is being updated correctly self.assertEqual(4, self.bc.block_count) # Ensures that last_block member of blockchain properly updated self.assertEqual(b4, self.bc.last_block) # Creates expected with preceding magic_bytes and size values # Split into multiple lines for readability expected = magic_bytes + get_size_bytes(b1) + b1 expected += magic_bytes + get_size_bytes(b2) + b2 expected += magic_bytes + get_size_bytes(b3) + b3 expected += magic_bytes + get_size_bytes(b4) + b4 #Get size of block size = bytes_to_int(get_size_bytes(expected)) # Extracts blocks from blockchain actual = extract(self.bc.blockfile, 0, size) # Confirms that extracted blocks is identical to actual blocks self.assertEqual(expected, actual)
def repair_block(self, block): previous = self[block.identifier]['previous'] if previous and self.block_is_broken(previous): print( 'When trying to update block {}, found that previous block {} also needed updation' .format( block.identifier, previous.identifier, )) self.repair_block(previous) self[block.identifier]['previous_hash'] = previous.hash() if self.block_is_broken(block): time.sleep(0.8) print('Updating block {}'.format(block.identifier)) block.mine()
def test_add_block1(self): target = 10**72 data = hash_SHA("Testing block".encode()) prev_hash = hash_SHA("0123456789ABCDEF".encode()) #Create a block using .mine() block = mine(0, prev_hash, data, target) # Creates expected with preceding magic_bytes and size values and block expected = magic_bytes + get_size_bytes(block) + block #Get size of block size = bytes_to_int(get_size_bytes(expected)) # Ensures that block_count is being initializes correctly self.assertEqual(0, self.bc.block_count) # Ensures that last_block member of blockchain is empty when initialized self.assertEqual(b'', self.bc.last_block) # Add block to blockchain self.bc.add_block(block) # Ensures that block count is being updated correctly self.assertEqual(1, self.bc.block_count) # Ensures that last_block member of blockchain properly updated self.assertEqual(block, self.bc.last_block) # Extracts block from blockchain actual = extract(self.bc.blockfile, 0, size) # Confirms that extracted block is identical to actual block self.assertEqual(expected, actual)
def main(): transactions = [{'de': 'ninguem', 'para': 'minerador', 'valor': '100.00'}] # block genesis block0 = Block( blocknumber=0, version='1.0', difficulty=2, timestamp=get_timestamp(), transaction=transactions, hashpreviousblock= '0000000000000000000000000000000000000000000000000000000000000000') mine(block0) print('Bloco 0 minerado - nonce: ' + str(block0.nonce)) # block 1 transactions = [{ 'de': 'minerador', 'para': 'joao', 'valor': '1.00' }, { 'de': 'minerador', 'para': 'maria', 'valor': '5.00' }, { 'de': 'minerador', 'para': 'helena', 'valor': '1.00' }] block1 = Block(blocknumber=0, version='1.0', difficulty=2, timestamp=get_timestamp(), transaction=transactions, hashpreviousblock=block0.hashHeaderBlock) mine(block1) print('Bloco 1 minerado - nonce: ' + str(block1.nonce))
def mine(self, block): if block.mine(self.complexity): if self.resolve_conflicts() == False: """нужно вернуть транзакции в пул""" return self.BLOCKCHAIN.append(block) requests.post(self.URL + self.NODE_PORT + '/newblock', json=system.obj_to_dictionary(block)) print(block.hash) return True else: self.resolve_conflicts() return False
def testMine(self): self.assertTrue(mine(self.block))