def refresh(self, transaction_data, last_update_timestamp): updated, my_ledger_path = blockchain.ledger_updated( self.blockchain.cnx, self.blockchain.client, self.blockchain.gpg) if not updated: self.blockchain = blockchain.download_ledger( self.blockchain.cnx, self.blockchain.client, self.blockchain.gpg) ipfs_hash = blockchain.insert_transaction( self.blockchain.cnx, self.blockchain.client, self.blockchain.gpg, self.contract_id, transaction_data, recipients=[ self.contract_header['engineer_email'], self.contract_header['technician_email'] ]) blockdata = self.contract_header blockdata['ipfs_hash'] = ipfs_hash block = Blockchain.hash(blockdata) blockchain.update_world_state(self.blockchain.cnx, self.contract_id, last_update_timestamp, self.contract_header['current_state']) block_added, self.blockchain.ledger_path = new_block( self.blockchain, block, ipfs_hash) self.blockchain.chain = self.blockchain.load_blockchain( self.blockchain.ledger_path) print('Transaction block added to blockchain at ', self.blockchain.ledger_path, '\n', block_added) return block_added
def mine(): # We run the proof of work algorithm to get the next proof... last_block = blockchain.last_block last_proof = last_block['proof'] proof = blockchain.proof_of_work(last_proof) # We must receive a reward for finding the proof. # The sender is "0" to signify that this node has mined a new coin. blockchain.new_transaction( sender="0", recipient=node_identifier, amount=1, ) # Forge the new Block by adding it to the chain previous_hash = blockchain.hash(last_block) block = blockchain.new_block(proof, previous_hash) response = { 'message': "New Block Forged", 'index': block['index'], 'transactions': block['transactions'], 'proof': block['proof'], 'previous_hash': block['previous_hash'], } return jsonify(response), 200
def thread_mine(current_index): # sleep for a random number of seconds from 10 to 60 seconds to simulate mining time.sleep(random.randint(1, 6)) last_block = blockchain.last_block last_proof = last_block['proof'] proof = blockchain.proof_of_work(last_proof) # if faulty, edit the hash to be incorrect if blockchain.faulty: proof = 0 previous_hash = blockchain.hash(last_block) # if the blockchain is still the same length as when we started, add the new block if len(blockchain.chain) == current_index: # reward miner blockchain.new_transaction( 1, "0", node_identifier, 1, False, ) blockchain.new_block(proof, previous_hash) # print the new block mined b_index = blockchain.last_block["index"] b_hash = blockchain.last_block["previous_hash"] b_proof = blockchain.last_block["proof"] b_timestamp = blockchain.last_block["timestamp"] mine_log.write(f"{b_index}, {b_hash}, {b_proof}, {b_timestamp}\n") for transaction in blockchain.last_block["transactions"]: mine_log.write(f"\t{transaction['trans_id']}\n") # if we are proactively telling other nodes of the new chain if blockchain.proactive: proactive_consensus() blockchain.mining = False
def create_blockchain(): # Our block chain is a list blockchain = [create_genesis_block()] previous_block = blockchain[0] # Total blocks in chain number_of_blocks = 20 for each_block in range(0, number_of_blocks): next_block = new_block(previous_block) blockchain.append(next_block) previous_block = next_block # Print details print("New block: Block {} added!! ".format(previous_block.index)) print("Block data: {}".format(previous_block.data)) print("Block hash: {}".format(previous_block.hash))
def mine(): req = request.form.get('mine') lt_block = blockchain.last_block lt_proof = lt_block['proof'] proof = blockchain.proof_of_work(lt_proof, lt_block) blockchain.new_transaction(sender='0', receiver=node_id, amount=1) previous_hash = blockchain.hash(lt_block) block = blockchain.new_block(proof, previous_hash) response = { 'message': "Block created", 'index': block['index'], 'proof': block['proof'], 'previous_hash': block['previous_hash'] } return render_template('/mine.html', response=response)
def mine(): last_block = blockchain.last_block last_proof = last_block['proof'] proof = blockchain.proof_of_work(last_proof) blockchain.new_transaction(sender="0", recipient=node_identifier, amount=1) previous_hash = blockchain.hash(last_block) block = blockchain.new_block(proof, previous_hash) response = { 'message': "New block Forged", 'index': block['index'], 'transactions': block['transactions'], 'proof': block['proof'], 'previous_hash': block['previous_hash'], } return jsonify(response), 200
def mine(): # We run the proof of work algorithm to get the next proof... last_block = blockchain.last_block last_proof = last_block['proof'] proof = blockchain.proof_of_work(last_proof) # 给工作量证明的节点提供奖励. # 发送者为 "0" 表明是新挖出的币 blockchain.new_transaction( sender="0", recipient=node_identifier, amount=1, ) # Forge the new Block by adding it to the chain block = blockchain.new_block(proof) response = { 'message': "New Block Forged", 'index': block['index'], 'transactions': block['transactions'], 'proof': block['proof'], 'previous_hash': block['previous_hash'], } return jsonify(response), 200
import blockchain as bc blockchain = [bc.create_genesis_block()] previous_block = blockchain[0] num_of_blocks = 30 for i in range(0, num_of_blocks): current_block = bc.new_block(previous_block) blockchain.append(current_block) previous_block = current_block print("Block " + str(current_block.counter) + " has been added\n") print("Transactoin info:") print("Sender - " + current_block.data.sender) print("Recipient - " + current_block.data.recipient) print("Amount of transaction - " + str(current_block.data.amount)) print("\nHash - " + current_block.hash + "\n\n\n")
blockchain = blockchain.Blockchain() # the blockchain used in this test # create a test record for a patient patient_test_vc = [ "Administration Date: MAY-01-2021 10:00 AM", "Patient ID: 10132", "Patient Name: John Doe", "Patient Address: 10 Example St. NE", "Administered by: Dr. Jill Fakeington" ] # additional info is non-personally identifying info stored with transaction additional_data = ["Vaccine Type: Pfizer", "Vaccine ID: 1234"] # create the provider provider = Provider.Provider(provider_key) # create the patient patient = Patient.Patient(patient_key) # create the third party verifier verifier = Verifier.Verifier(verifier_key, blockchain) # generate patient vaccine card provider.generate_card(patient_test_vc) # provider posts the transaction to the blockchain provider.post_transaction(blockchain, patient_key.public_key(), additional_data) # a new block is created blockchain.new_block() # provider sends encrypted vaccine care to the patient provider.send_patient_info(patient) # Patient sends encrypted record to the verifier to prove his vaccination patient.send_records(verifier, verifier.get_pub_key()) # verifier verifies the record is in the blockchain verifier.verify_record(blockchain)