def __init__(self): self.chain = [] self.current_transactions = [] self.transaction_index = 0 # トランザクションにコインベースを追加します self.create_transaction(sender='0', recipient="my_address", amount=1) proof_of_work(self, previous_hash="00000")
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 mine(blockchain): # 直前のブロックのハッシュ値を計算.リストの[-1]は最後の要素という意味. previous_hash = calculate_hash(blockchain.chain[-1]) # proof_of_work関数を用いて、新しくブロックをマイニングしてください proof_of_work(blockchain, previous_hash) # current_transactionsとtransaction_indexを初期化してください blockchain.current_transactions = [] blockchain.transaction_index = 0 # current_transactionsにコインベースを追加しています blockchain.create_transaction(sender='0', recipient="my_address", amount=1) blockchain.create_transaction(sender='0', recipient="my_address", amount=1) block = blockchain.chain[-1] # 新しいブロックの情報をresponseに代入しています response = { 'message': '新しいブロックを採掘しました', 'index': block['index'], 'nonce': block['nonce'], 'previous_hash': block['previous_hash'], } return response
async def add_transaction(broadcast_outbox, transaction): """Adds a transaction to a block in the blockchain.""" TRANSACTIONS_QUEUE.append(transaction.__dict__) print("transactions len:", len(TRANSACTIONS_QUEUE)) if len(TRANSACTIONS_QUEUE) >= 5: previous_hash: str = None if len(blockchain.blockchain) > 0: previous_hash = blockchain.blockchain[len(blockchain.blockchain) - 1].hash block = blockchain.Block(json.dumps(TRANSACTIONS_QUEUE), previous_hash) hash_value, nonce = blockchain.proof_of_work(block, 1) print(f"proof of work completed. Hash: {hash_value} Nonce:{nonce}") block.hash_value = hash_value block.nonce = nonce block.timestamp = datetime.utcnow().strftime("%y/%m/%d %H:%M") blockchain.blockchain.append(block) await alert_all_nodes(broadcast_outbox, "new_block", block.__dict__) TRANSACTIONS_QUEUE.clear()
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 mine(): last_blk = blockchain.last_block last_proof = last_blk.proof proof = blockchain.proof_of_work(last_proof) blockchain.add_transaction(constant.MINER_KEY, node_address, constant.MINER_REWARD, "") prev_hash = last_blk.hash blk = blockchain.add_block(proof, prev_hash) response = { 'message': 'New block mined!', 'index': blk['index'], 'transactions': blk['transactions'], 'proof': blk['proof'], 'previous_hash': blk['prev_hash'] } return jsonify(response), 201
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(): # 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