def addBlock(self, block):
     transactions = [
         Transaction(tx['copyrightOwner'], tx['picUrl'], tx['signature'],
                     tx['picPhash']) for tx in block['transactions']
     ]
     proofIsValid = Verification.validProof(transactions,
                                            block['previousHash'],
                                            block['proof'])
     hashesMatch = hashBlock(self.__chain[-1]) == block['previousHash']
     print proofIsValid
     print hashesMatch
     if not proofIsValid or not hashesMatch:
         return False
     convertedBlock = Block(block['index'], block['previousHash'],
                            transactions, block['proof'],
                            block['timestamp'])
     self.__chain.append(convertedBlock)
     storedTransactions = self.__openTransactions[:]
     for itx in block['transactions']:
         for opentx in storedTransactions:
             if opentx.copyrightOwner == itx['copyrightOwner'] and \
                     opentx.picUrl == itx['picUrl'] and \
                     opentx.picPhash == itx['picPhash'] and opentx.signature == itx['signature']:
                 try:
                     self.__openTransactions.remove(opentx)
                 except ValueError:
                     print('事务已被移除')
     self.saveData()
     return True
예제 #2
0
class Blockchain:
    def __init__(self):
        genesis_block = Block(0, '', [], 0, 0)
        self.chain = [genesis_block]
        self.unconfirmedTransaction = []

    @property
    def chain(self):
        return self.__chain[:]

    # The setter
    @chain.setter
    def chain(self, data):
        self.__chain = data


    def proofOfWork(self):

         """proof of work:
        for adding the unconfirmed transactions,
        hashing the previous block and guessing a proof number """

        
        last_block = self.__chain[-1]
        lastHash = hashBlock(last_block)
        proof = 0
        while not Verification.validProof(self.unconfirmedTransaction, lastHash, proof):
            proof += 1
        return proof
예제 #3
0
 def proofOfWork(self):
     """proof of work:  
     the unconfirmed transactions, the hash of the previous block and a proof guessed number """
     last_block = self.__chain[-1]
     lastHash = hashBlock(last_block)
     proof = 0
     # Try different PoW numbers and return the first valid one
     while not Verification.validProof(self.unconfirmedTransaction,
                                       lastHash, proof):
         proof += 1
     return proof
 def proofOfWork(self):
     """
     工作量证明
     为返回一个Nouse使当区块hash之后前三位为0
     """
     lastBlock = self.__chain[-1]
     lastHash = hashBlock(lastBlock)
     proof = 0
     while not Verification.validProof(self.__openTransactions, lastHash,
                                       proof):
         proof += 1
     print('proof = ' + str(proof))
     return proof