def create_genesis_block(self): genesis_block = Block(index=0, transactions=[], timestamp=time.time(), previous_hash='0') genesis_block.hash = genesis_block.compute_hash() self.chain.append(genesis_block)
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)