def mine_block(): previous_block = blockchain.previous_block() previous_proof = previous_block['proof'] proof = blockchain.proof_of_work(previous_proof) previous_hash = blockchain.hash(previous_block) block = blockchain.create_block(proof, previous_hash) # The sender is "0" to signify that this node has mined a new coin. blockchain.new_transaction( sender = "0", recipient = node_identifier, amount = 1 ) response = { 'message': 'Congratulations, you just mined a block!', 'index': block['index'], 'timestamp': block['timestamp'], 'proof': block['proof'], 'previous hash': block['previous_hash'], 'transaction': block['transaction'] } 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) # 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 post_transaction(self, blockchain, ppk, data): h_vc = self.hash_file() signed = self.key.sign( h_vc, padding.PSS(mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH), utils.Prehashed(hashes.SHA256())) blockchain.new_transaction(self.key.public_key(), ppk, time.time(), h_vc, data)
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 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 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 new_transaction(): values = request.get_json() # Check that the required fields are in the POST'ed data required = ['sender', 'recipient', 'amount'] if not all(k in values for k in required): return 'Missing values', 400 # Create a new Transaction index = blockchain.new_transaction(values['sender'], values['recipient'], values['amount']) response = {'message': f'Transaction will be added to Block {index}'} return jsonify(response), 201
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
def new_transaction(): values = request.get_json() # TODO : After jwt # Create a new Transaction if blockchain.new_transaction(values): response = {'message': f'transaction will be added to buffer'} return jsonify(response), 201 else: response = {'message': f'Invalid transaction'} return jsonify(response), 400 response = {'message': f'Transaction will be added to Block {index}'} return jsonify(response), 201
def new_transaction(): values = request.get_json() # check required fields are in the received data required = ['trans_id', 'sender', 'recipient', 'amount'] if not all(k in values for k in required): return 'Missing values', 400 # create the transaction index = blockchain.new_transaction(values['trans_id'], values['sender'], values['recipient'], values['amount']) # return message saying transaction added to pool response = {'message': f'Transaction will be added'} return jsonify(response), 201
def new_transaction(): val = request.form required = ['sender', 'receiver', 'amount'] if not all(k in val for k in required): return "Enter all the information required", 400 index = blockchain.new_transaction(val['sender'], val['receiver'], val['amount']) response = { 'message': 'Transaction will be added to Block {}'.format(index) } return render_template('/new-transaction.html', message=response['message'])
def index(): if request.method == 'POST': sender = request.form['sender'] recipient = request.form['recipient'] amount = request.form['amount'] # Create a new transaction new = blockchain.new_transaction(sender, recipient, amount) response = { 'message': f'The new transaction has been added to block {new}' } #return jsonify(response), 201 return redirect(url_for('index'))
def transaction(): data = request.get_json() required = [ 'sender', 'recipient', 'amount' ] # Checking to see if all the required data has been given if not all(k in data for k in required): return 'Missing data', 400 # Create a new transaction index = blockchain.new_transaction(data['sender'], data['recipient'], data['amount']) response = { 'message': f'The new transaction has been added to block {index}' } return jsonify(response), 201