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 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(): 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 __getChainHash(request, f): try: A = json.loads(request) if type(A) == dict: sig = A.pop('sig') if rsa.verify(sig, json.dumps(A, sort_keys=True), A['public key']): self._chainMutex.acquire() A = blockchain.hash(self._chain.chain) node.mutexReleaser(self._chainMutex) pk, sk = self.getKeys() A = {'chain hash': A, 'public key': pk} A['sig'] = rsa.sign(json.dumps(A, sort_keys=True), sk) A = json.dumps(A) f(A) return except: pass f('NOT OK')
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 mine_block(): previous_block = blockchain.get_previous_block() previous_proof = previous_block['proof'] proof = blockchain.proof_of_work(previous_proof) previous_hash = blockchain.hash(previous_block) # Transactions from newly mined block blockchain.add_transaction(sender=blockchain.node_url, receiver='Sab', amount=1) # Create Block for work done block = blockchain.create_block(proof, previous_hash) response = { 'message': 'Block mined!', 'index': block['index'], 'timestamp': block['timestamp'], 'proof': block['proof'], 'previous_hash': block['previous_hash'], 'transactions': block['transactions'] } return jsonify(response), 200
def mine(self, ret = True): if (self._chain.isMineable() and self.isMiner()) or self._chain.gottaMine(): transactions = self._chain.mineableTransactions self._chain.mineableTransactionsMutex.acquire() self._chain.mineableTransactions = {} node.mutexReleaser(self._chain.mineableTransactionsMutex) self._chainMutex.acquire() block = { 'index': len(self._chain.chain) + 1, 'timestamp': time.time(), 'proof': None, 'transactions': transactions, 'total transactions': len(transactions), 'prev hash': blockchain.hash(self._chain.lastBlock) } node.mutexReleaser(self._chainMutex) block['proof'] = blockchain.proofOfWork(self._chain.lastBlock['proof'], block['prev hash']) self._chainMutex.acquire() self._chain.addBlock(block) node.mutexReleaser(self._chainMutex) self.__broadcastBlock(block) return ret
#Author:Jehu Xue # -*- coding:utf-8 -*- import time import blockchain # 测试上一个哈希值 blockchain = blockchain.Blockchain() last_block = {'block_number': 1, 'nonce': 0, 'previous_hash': 0, 'timestamp': 1547603975.9364002, 'transactions': []} previous_hash = blockchain.hash(last_block) print(previous_hash) # 测试monggodb数据库 # import pymongo # myclient = pymongo.MongoClient("mongodb://localhost:27017/") # mydb = myclient["BlockChain1"] # mycol = mydb["sites"] # dblist = myclient.database_names() #python2 # mydict = {"name": "RUNOOB", "alexa": "10000", "url": "https://www.runoob.com"} # x = mycol.insert_one(mydict) # dblist = myclient.list_database_names() # if "test" in dblist: # print("数据库已存在!") # print(x) # else: # print("数据库不存在!") # last = mycol.find().sort([("timestamp", -1)]).limit(1) # # last_block = last[0]