def next_block_branch2(chain): block_information = chain.genesis_block.get_block_information() next_block_branch2 = Block(block_information, PUBLIC_KEY) next_block_branch2.timestamp = chain.genesis_block.timestamp + CONFIG.block_time + 1 next_block_branch2.sign(PRIVATE_KEY) next_block_branch2.update_hash() yield next_block_branch2
def dangling_block(chain): dangling_block = Block({"index": 4, "hash": "ab1234"}, PUBLIC_KEY) dangling_block.timestamp = chain.genesis_block.timestamp + 2 * CONFIG.block_time + 1 dangling_block.sign(PRIVATE_KEY) dangling_block.update_hash() chain.add_dangling_block(dangling_block) yield dangling_block
def add_data(self, data: str) -> bool: """ Adds a new Block to the Blockchain. Returns a bool indicating success or failure. Parameters: data -- the data (represented as a string) to be stored. """ timestamp = time.time() # The previous hash is important since this is what helps us ensure # that our Blockchain has not lost its integrity. Each block will # keep track of the block before it, using its hash, thus we # will know that no block has been tamperered with. previous_hash = self.blocks[-1].generate_hash() block = Block() block.data = data block.timestamp = timestamp block.previous_hash = previous_hash self.blocks.append(block) # We validate the entire Blockchain to ensure that # the block we just inserted did not break our # Blockchain integrity. if not self.validate(): self.blocks.remove(block) return False return True
def _read_block_from_dict(self, dict): # print(dict) previous_hash = dict['prev_block'] block = Block(previous_hash=previous_hash) block.hash = dict['hash'] block.nonce = dict['nonce'] block.timestamp = dict['time_stamp'] is_coinbase = True for transaction_dict in dict['transactions']: sender = '' if 'sender_public_key' in transaction_dict: sender = transaction_dict['sender_public_key'] signature = None if 'signature' in transaction_dict: signature = transaction_dict['signature'] reciever = transaction_dict['receiver_public_key'] value = transaction_dict['value'] transaction_inputs = [] if 'input' in transaction_dict: for transaction_input_dict in transaction_dict['input']: transaction_output_id = transaction_input_dict[ 'transactionOutputId'] transaction_input = TransactionInput( transaction_output_id=transaction_output_id) transaction_inputs.append(transaction_input) transaction = Transaction(sender, reciever, value, transaction_inputs) transaction.transaction_id = transaction_dict['id'] transaction.signature = signature block.add_transaction(transaction, all_utxos=self.blockchain.all_utxos, minimum_transaction=self.minimum_transaction, fee=self.blockchain.fee, should_check=False, is_coinbase=is_coinbase) is_coinbase = False if 'output' in transaction_dict: for transaction_output_dict in transaction_dict['output']: value = transaction_output_dict['value'] parent_transaction_id = '' if 'parent_transaction_id' in transaction_output_dict: parent_transaction_id = transaction_output_dict[ 'parent_transaction_id'] recipient_public_key = transaction_output_dict[ 'recipient_public_key'] transaction_output = TransactionOutput( recipient_public_key_str=recipient_public_key, value=value, parent_transaction_id=parent_transaction_id) transaction_output.id = transaction_output_dict['id'] transaction.outputs.append(transaction_output) self.blockchain.append_transaction(transaction) self.blockchain.append_block(block)
def add_chain_data(self, chain_data: list): """ Takes a list of blocks, usually ingested from a JSON file, and adds them to the chain. Parameters: chain_data -- a list of dicts with "data", "previous_hash", "timestamp". """ for chain_item in chain_data: block = Block() block.data = chain_item["data"] block.previous_hash = chain_item["previous_hash"] block.timestamp = chain_item["timestamp"] self.blocks.append(block)
def write_default_blockchain() -> str: """Returns the string representation of the default Blockchain data.""" genesis_block = Block() genesis_block.data = "This is the sickest Blockchain implementation ever." genesis_block.timestamp = time.time() genesis_block.previous_hash = "" # Create a temporary chain which we can convert to JSON. default_chain = Blockchain([]) default_chain.blocks.append(genesis_block) save_chain(default_chain) return default_chain.to_json()