Ejemplo n.º 1
0
def generateNextBlock(blockData, pubKey, previousBlock, gwPvtKey, consensus):
    """ Receive the information of a new block and create it\n
    @param blockData - information of the new block\n
    @param pubKey - public key of the device how wants to generate the new block\n
    @param previouBlock - BlockHeader object with the last block on the chain\n
    @param gwPvtKey - private key of the gateway\n
    @param consensus - it is specified current consensus adopted
    @return BlockHeader - the new block
    """
    nextIndex = previousBlock.index + 1
    nextTimestamp = time.time()
    previousBlockHash = criptoFunctions.calculateHashForBlock(previousBlock)
    nonce = 0
    nextHash = criptoFunctions.calculateHash(nextIndex, previousBlockHash,
                                             nextTimestamp, pubKey, nonce)
    if (consensus == 'PoW'):
        difficulty_bits = 12  #2 bytes or 4 hex or 16 bits of zeros in the left of hash
        target = 2**(
            256 - difficulty_bits
        )  #resulting value is lower when it has more 0 in the left of hash
        while ((long(nextHash, 16) > target) and (nonce < (2**32))
               ):  #convert hash to long to verify when it achieve difficulty
            nonce = nonce + 1
            nextHash = criptoFunctions.calculateHash(nextIndex,
                                                     previousBlockHash,
                                                     nextTimestamp, pubKey,
                                                     nonce)
    print("####nonce = " + str(nonce))
    sign = criptoFunctions.signInfo(gwPvtKey, nextHash)
    inf = Transaction.Transaction(0, nextHash, nextTimestamp, blockData, sign)

    return BlockHeader.BlockHeader(nextIndex, previousBlockHash, nextTimestamp,
                                   inf, nextHash, nonce, pubKey)
Ejemplo n.º 2
0
def verifyBlockCandidate(newBlock, generatorGwPub, generatorDevicePub,
                         alivePeers):
    blockValidation = True
    lastBlk = chainFunctions.getLatestBlock()
    # print("Index:"+str(lastBlk.index)+" prevHash:"+str(lastBlk.previousHash)+ " time:"+str(lastBlk.timestamp)+ " pubKey:")
    lastBlkHash = criptoFunctions.calculateHash(lastBlk.index,
                                                lastBlk.previousHash,
                                                lastBlk.timestamp,
                                                lastBlk.publicKey)
    # print ("This Hash:"+str(lastBlkHash))
    # print ("Last Hash:"+str(block.previousHash))
    if (lastBlkHash != newBlock.previousHash):
        blockValidation = False
        return blockValidation
    if (lastBlk.index != (newBlock.index + 1)):
        blockValidation = False
        return blockValidation
    if (lastBlk.timestamp >= newBlock.timestamp):
        blockValidation = False
        return blockValidation
#TODO -> verifySIGNATURE!!!!!
    if blockValidation:
        voterSign = criptoFunctions.signInfo(gwPvt, newBlock)
        addVoteBlockPBFT(newBlock, gwPub, voterSign)  #adiciona o seu p
        for p in alivePeers:
            p.object.addVoteBlockPBFT(
                newBlock, gwPub,
                voterSign)  #altera a lista de confirmacao de todos os peers
        return True
    else:
        return False
Ejemplo n.º 3
0
def generateNextBlock(blockData, pubKey, previousBlock, gwPvtKey):
    nextIndex = previousBlock.index + 1
    nextTimestamp = time.time()
    nextHash = criptoFunctions.calculateHash(nextIndex, previousBlock.hash,
                                             nextTimestamp, pubKey)
    sign = criptoFunctions.signInfo(gwPvtKey, nextHash)
    inf = Transaction.Transaction(0, nextHash, nextTimestamp, blockData, sign)
    return BlockHeader.BlockHeader(nextIndex, previousBlock.hash,
                                   nextTimestamp, inf, nextHash, pubKey)
Ejemplo n.º 4
0
def isBlockValid(block):
    #Todo Fix the comparison between the hashes... for now is just a mater to simulate the time spend calculating the hashes...
    #global BlockHeaderChain
    #print(str(len(BlockHeaderChain)))
    lastBlk = chainFunctions.getLatestBlock()
    #print("Index:"+str(lastBlk.index)+" prevHash:"+str(lastBlk.previousHash)+ " time:"+str(lastBlk.timestamp)+ " pubKey:")
    lastBlkHash = criptoFunctions.calculateHash(lastBlk.index,
                                                lastBlk.previousHash,
                                                lastBlk.timestamp,
                                                lastBlk.publicKey)
    #print ("This Hash:"+str(lastBlkHash))
    #print ("Last Hash:"+str(block.previousHash))
    if (lastBlkHash == block.previousHash):
        return True
    else:
        return True
Ejemplo n.º 5
0
def generateNextBlock(blockData, pubKey, previousBlock, gwPvtKey, **kwargs):
    """ Receive the information of a new block and create it\n
    @param blockData - information of the new block\n
    @param pubKey - public key of the device how wants to generate the new block\n
    @param previouBlock - BlockHeader object with the last block on the chain\n
    @param gwPvtKey - private key of the gateway\n
    @return BlockHeader - the new block
    """
    nextIndex = previousBlock.index + 1    
    nextTimestamp = time.time()
    #nextHash = criptoFunctions.calculateHash(nextIndex, previousBlock.hash, nextTimestamp, pubKey);
    previousBlockHash = criptoFunctions.calculateHashForBlock(previousBlock)
    nextHash = criptoFunctions.calculateHash(nextIndex, previousBlockHash, nextTimestamp, pubKey)
    sign = criptoFunctions.signInfo(gwPvtKey, nextHash)
    inf = Transaction.Transaction(0, nextHash, nextTimestamp, blockData, sign)

    blockType = kwargs.get('type', None)
    if (blockType):
        return BlockHeader.BlockHeader(nextIndex, previousBlockHash, nextTimestamp, inf, nextHash, pubKey, type=blockType)
    else:
        return BlockHeader.BlockHeader(nextIndex, previousBlockHash, nextTimestamp, inf, nextHash, pubKey)