Пример #1
0
def mine_block():
    last_block = blockchain[-1]
    hashed_block = hash_block(last_block)
    # print(hashed_block)
    proof = proof_of_work()
    # Miners should be rewarded for there work.
    reward_transaction = OrderedDict([('sender', "MINING"),
                                      ('recipient', owner),
                                      ('amount', MINING_REWARD)])
    # reward_transaction = {
    #    'sender': "MINING",
    #    'recipient': owner,
    #    'amount': MINING_REWARD
    # }
    # Copy transaction instead of manipulating the orignal "open_transactions".
    copied_transaction = open_transaction[:]
    copied_transaction.append(reward_transaction)
    block = {
        "previous_hash": hashed_block,
        "index": len(blockchain),
        "transaction": copied_transaction,
        "proof": proof
    }
    blockchain.append(block)
    return True
Пример #2
0
def proof_of_work():
    last_block = blockchain[-1]
    last_hash = hash_block(last_block)
    proof = 0
    while not valid_proof(open_transaction, last_hash, proof):
        proof += 1
        # Printing the number of hashes done to check the proof.
        print(proof)
    return proof
 def proof_of_work(self):
     last_block = self.__chain[-1]
     last_hash = hash_block(last_block)
     proof = 0
     while not Verification.valid_proof(self.__open_transaction, last_hash, proof):
         proof += 1
         # Printing the number of hashes done to check the proof.
         print(proof)
     return proof
Пример #4
0
def verify_chain():
    """Verifies whether the block are matching or not."""
    for index, block in enumerate(blockchain):
        if index == 0:
            continue
        if block['previous_hash'] != hash_block(blockchain[index - 1]):
            return False
        if not valid_proof(block["transaction"][:-1], block["previous_hash"],
                           block['proof']):
            print("Proof of work is Invalid!!!")
            return False
    return True
 def verify_chain(cls, blockchain):
     """Verifies whether the block are matching or not."""
     for index, block in enumerate(blockchain):
         if index == 0:
             continue
         if block.previous_hash != hash_block(blockchain[index - 1]):
             return False
         if not cls.valid_proof(block.transactions[:-1],
                                block.previous_hash, block.proof):
             print("Proof of work is Invalid!!!")
             return False
     return True
Пример #6
0
def add_block(detail):
    global main_block
    previous_block = main_block[-1]
    previous_hash, unique_num = hash_block(previous_block)

    new_block = {
        'previous_hash': previous_hash,
        'index': len(main_block),
        'details': detail,
        'id': unique_num
    }
    main_block.append(new_block)
    # print(main_block)
    return main_block
Пример #7
0
 def mine_block(self):
     last_block = self.__chain[-1]
     hashed_block = hash_block(last_block)
     # print(hashed_block)
     proof = self.proof_of_work()
     # Miners should be rewarded for there work.
     reward_transaction = Transaction("MINING", self.hosting_node,
                                      MINING_REWARD)
     # reward_transaction = {
     #    'sender': "MINING",
     #    'recipient': owner,
     #    'amount': MINING_REWARD
     # }
     # Copy transaction instead of manipulating the orignal "open_transactions".
     copied_transaction = self.__open_transaction[:]
     copied_transaction.append(reward_transaction)
     block = Block(len(self.__chain), hashed_block, copied_transaction,
                   proof)
     self.__chain.append(block)
     # Resets the open_transaction to an empty list.
     self.__open_transaction = []
     self.save_data()
     return True