Example #1
0
 def add_block(self, block):
     transactions = [
         Transaction(tx['sender'], tx['recipient'], tx['signature'],
                     tx['amount']) for tx in block['transactions']
     ]
     proof_is_valid = verification.valid_proof(transactions[:-1],
                                               block['previous_hash'],
                                               block['proof'])
     match_hash = hash_block(self.__chain[-1]) == block['previous_hash']
     if not proof_is_valid or not match_hash:
         return False
     converted_block = Block(block['index'], block['previous_hash'],
                             transactions, block['proof'],
                             block['timestamp'])
     self.__chain.append(converted_block)
     stored_transactions = self.__open_transactions[:]
     for tx in block['transactions']:
         for opentx in stored_transactions:
             if opentx.sender == tx['sender'] and opentx.recipient == tx[
                     'recipient'] and opentx.amount == tx[
                         'amount'] and opentx.signature in tx['signature']:
                 try:
                     self.__open_transactions.remove(opentx)
                 except ValueError:
                     print('Item was already removed')
     self.save_data()
     return True
Example #2
0
def proof_of_work():
    last_block=blockchain[-1]
    last_hash=hash_block(last_block)
    proof=0
    while not valid_proof(open_transactions, last_hash, proof):
        proof +=1
    return proof
Example #3
0
 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_transactions, last_hash,
                                        proof):
         proof += 1
     return proof
Example #4
0
def verify_chain(blockchain):
    for (index, block) in enumerate(blockchain):
        if index > 0:
            if block.previous_hash != hash_block(blockchain[index - 1]):
                return False
            if not valid_proof(block.transactions[:-1], block.previous_hash,
                               block.proof):
                print('Proof of work is invalid')
                return False
    return True
Example #5
0
def verify_chain():
    """verify the current blockchain and return true if it is valid else false"""
    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['transactions'][:-1], block['previous_hash'], block['proof']):
            print('Proof of work is invalid')
            return False
        return True
Example #6
0
def validate_block_data_for_chain(chain: Chain, previous_hash, proof,
                                  data) -> (bool, [str]):
    errors = []
    last_hash = hash_block(last_block(chain))
    if last_hash != previous_hash:
        errors.append('Previous hash does not match!')
    if type(data) is not str or len(data) == 0:
        errors.append('Data property is empty!')
    last_proof = last_block(chain).proof
    validation_success, hex_result = validate_proof(proof, last_proof)
    if not validation_success:
        errors.append('{} is not a valid proof! sha_256({} * {}) = {}'.format(
            proof, proof, last_proof, hex_result))
    return not errors, errors
Example #7
0
def mine_block():
    last_block=blockchain[-1]
    hashed_block=hash_block(last_block)
    proof=proof_of_work()
    #reward_transaction={
     #   'sender':'MINING',
      #  'recipient':owner,
       # 'amount':MINING_REWARD
    #}
    reward_transaction=OrderedDict([('sender', 'MINING'),('recipient', owner),('amount', MINING_REWARD)])
    copied_transactions=open_transactions[:] 
    copied_transactions.append(reward_transaction)
    block={
        'previous_hash':'xyz',
        'index':len(blockchain),
        'transactions':copied_transactions,
        'proof':proof
    }
    blockchain.append(block)
    save_data()
    return True
Example #8
0
    def mine_block(self):
        if self.public_key is None:
            return None

        last_block = self.__chain[-1]
        previous_hash = hash_block(last_block)
        proof = self.proof_of_work()

        reward_transaction = Transaction('MINING', self.public_key, '',
                                         MINING_REWARD)

        copied_transactions = self.__open_transactions[:]

        for tx in copied_transactions:
            if not Wallet.verify_transaction(tx):
                return None

        copied_transactions.append(reward_transaction)
        mined_block = Block(len(self.__chain), previous_hash,
                            copied_transactions, proof)

        self.__chain.append(mined_block)
        self.__open_transactions = []
        self.save_data()
        for node in self.__peer_nodes:
            url = 'http://{}/broadcast-block'.format(node)
            converted_mined_block = mined_block.__dict__.copy()
            converted_mined_block['transactions'] = [
                tx.__dict__ for tx in converted_mined_block['transactions']
            ]
            try:
                response = requests.post(url,
                                         json={'block': converted_mined_block})
                if response.status_code == 400 or response.status_code == 500:
                    print('Block declined, needs resolving.')
                if response.status_code == 409:
                    self.conflicts = True
            except requests.exceptions.ConnectionError:
                continue
        return mined_block
Example #9
0
def add_new_block_to_chain(chain: Chain, data: str, proof: int) -> Chain:
    block = _new_block(index=len(chain.blocks),
                       previous_hash=hash_block(last_block(chain)),
                       data=data,
                       proof=proof)
    return add_block_to_chain(chain, block)
Example #10
0
def next_previous_hash(chain: Chain) -> str:
    return hash_block(last_block(chain))