示例#1
0
def AcceptBlock(block):

    bc = Blockchain()
    if bc.haveBlock(block.hash):
        utils.logg('ProccessBlock() Error - Block %s already exists' %
                   block.hash)
        return False

    # Check prev block
    if bc._tip != block.prev_block_hash:
        utils.logg("Proccess().thisBlock : prev block not found")
        return False

    # Check timestamp against prev
    if bc.getBlock(bc._tip).timestamp >= block.timestamp:
        utils.logg("Proccess().thisBlock : block's timestamp is too early")
        return False

    #  Check Proof Of Work
    if block.bits != GetNextWorkRequired(ctx.bestBlockHash):
        utils.logg("Proccess().thisBlock : incorrect proof of work")
        return False

    if not bc._block_put(block):
        utils.logg("AcceptBlock() : WriteToDisk failed")
        return False

    utxo_set = UTXOSet(bc)
    utxo_set.reindex()

    ctx.mapBlockIndex[block.hash] = block
    ctx.BestHeight = ctx.BestHeight + 1
    ctx.mapBlockHeight[block.hash] = ctx.BestHeight
    ctx.bestBlockHash = block.hash

    return True
示例#2
0
def add_block(msg):
    bc = Blockchain()
    new_block = Block([], bc._tip, bc.length, msg).pow_of_block()
    if new_block:
        bc._block_put(new_block)
示例#3
0
def loadBlockIndex():
    blockchain = Blockchain()

    utils.logg('Loading BlockIndex')

    #
    # Load block index
    #

    for block in reversed(list(blockchain.blocks)):
        # add to index
        ctx.mapBlockIndex[block.hash] = block
        ctx.BestHeight = ctx.BestHeight + 1
        ctx.bestBlockHash = block.hash
        ctx.mapBlockHeight[block.hash] = ctx.BestHeight

    #
    # Init with genesis block
    #

    if len(ctx.mapBlockIndex) == 0:
        txnew = Transaction()

        # Transaction

        # inputs
        txnew.vin[0].tx_id = ''
        txnew.vin[0].vout = -1
        txnew.vin[
            0].public_key = 'The Times 03/Jan/2009 Chancellor on brink of second bailout for banks'
        # output
        txnew.vout[0].value = 50 * COIN
        txnew.vout[0].address = '1F6kc25HfrXcn9b4brsNzCiTDWuqsDzAmx'
        txnew.set_id()

        # Transaction(id='12c86ae42baf87e9186cd2fabae575e5534ee0bb82a61a8d0d5616d3ec9bf029',
        # vin=[TXInput(tx_id=b'', vout=-1, signature=None, public_key='The Times 03/Jan/2009 Chancellor on brink of second bailout for banks')],
        # vout=[TXOutput(address='1F6kc25HfrXcn9b4brsNzCiTDWuqsDzAmx', value=5000000000, public_key_hash='9aa83d479c33c697cd727fc87d753d2d7f2daf19')])

        # Block

        block = Block([txnew])
        block.prev_block_hash = ''
        block.timestamp = 1607965519
        block.bits = 0x1e0fffff
        block.nonce = 1484712

        #Block(timestamp=b'1607965519', tx_lst=[Transaction(id='12c86ae42baf87e9186cd2fabae575e5534ee0bb82a61a8d0d5616d3ec9bf029',
        #vin=[TXInput(tx_id=b'', vout=-1, signature=None, public_key='The Times 03/Jan/2009 Chancellor on brink of second bailout for banks')],
        #vout=[TXOutput(address='1F6kc25HfrXcn9b4brsNzCiTDWuqsDzAmx', value=5000000000, public_key_hash='9aa83d479c33c697cd727fc87d753d2d7f2daf19')])],
        #prev_block_hash=b'', hash=None, nonce=1484712, bits=504365055)

        assert (
            block.getHash() ==
            '0000054bd29593ff231e77f7005a9e288e162bbda8cb8962077d57d9be8f87c0')

        blockchain._block_put(block)
        utxo_set = UTXOSet(blockchain)
        utxo_set.reindex()

        utils.logg('Genesis block added to database')

        # add to index
        ctx.mapBlockIndex[block.hash] = block
        ctx.mapBlockHeight[block.hash] = 1
        ctx.BestHeight = ctx.BestHeight + 1
        ctx.bestBlockHash = block.hash

        # Miner
        '''
        target = utils.bits2target(block.bits)

        i = 0 
        while 1:
            block.nonce = i 
            hash_hex = block.getHash()
            hash_int = int(hash_hex, 16)
            if hash_int < target:
                print (hash_hex, block.nonce, block.timestamp)

            i +=1

        '''

    return True