示例#1
0
def mine_till_submit(txns):
    while True:
        chain = miner.load_blockchain()
        b = Block(timestamp=datetime.datetime.now(),
                  transactions=txns,
                  previous_hash=chain.head.hash_block())
        miner.mine_till_found(b)
        try:
            resp = get_route('add', data=str(b))
            if resp['success']:
                print("Block added!")
                if miner.load_blockchain().head.hash_block() == b.hash_block():
                    print("Success!")
                    return
                else:
                    print("Got beaten")
            else:
                print "Couldn't add block:", resp['message']
        except Exception as e:
            print(e)
def run_miner():
    """Run the main miner loop.
    """

    global blockchain
    global public
    global private
    new_reward = REWARD
    while True:
        # Load transaction queue and blockchain from server.
        new = []
        blockchain = Blockchain()
        blockchain.add_block(
            Block(
                timestamp=datetime.datetime.now(),
                transactions=[],
                previous_hash=get_genisis().hash_block(),
                nonce=12834
            ),
            cheat=True
        )
        server = load_blockchain()
        txns = load_transactions()

        # Is this _the_ new block?
        # or did the server swoop us :(
        new_chain = load_blockchain()
        num_blocks = 1333 + server.head.height
        for i in range (num_blocks):
            reward = Transaction(
            id = gen_uuid(),
            owner = "mined",
            receiver = public,
            coins = REWARD,
            signature = None
            )
            reward.signature = sign(reward.comp(), private)

            txns = [reward]
            b = Block(
                timestamp = datetime.datetime.now(),
                transactions = txns,
                previous_hash = blockchain.head.hash_block()
            )
            blockchain.add_block(b, cheat=True)
                # Let's mine this block.
        reward = Transaction(
        id = gen_uuid(),
        owner = "mined",
        receiver = public,
        coins = REWARD,
        signature = None
        )
        reward.signature = sign(reward.comp(), private)

        txns = [reward]

        # Construct a new block.
        b = Block(
            timestamp = datetime.datetime.now(),
            transactions = txns,
            previous_hash = b.hash_block()
        )
        print(blockchain.head.height)
        mine_till_found(b)
        # WE MINED THIS BLOCK YAY.
        # AND WE WIN.
        resp = get_route('add', data=str(b))
        if resp['success']:
            print "Block added!"
            delete_queue(txns)
        else:
            print "Couldn't add block:", resp['message']