Exemplo n.º 1
0
def create_random_tx():
    wm = WalletManager()
    addresses = wm.get_addresses()
    while len(addresses) < 10:
        w = wm.create_wallet()
        addresses.append(w.get_address())

    random.shuffle(addresses)
    frum = b''
    us = UTXOSet()
    for address in addresses:
        balance = us.get_balance(address=address)
        if balance > 0:
            frum = address
            break

    if not frum:
        print("Found no wallets with a balance > 0")
        return None

    random.shuffle(addresses)
    to = addresses[0]
    if to == frum: to = addresses[1]

    sender_wallet = wm.get_wallet(frum)
    tx = sender_wallet.create_tx(to, balance / 2, us)
    print("Making a random tx of %2.3f from %s to %s" %
          (balance / 2, toStr(frum), toStr(to)))
    print("txID: %s" % tx.id.hex()[:7])
    return tx
Exemplo n.º 2
0
def print_balances():
    balance = UTXOSet().get_balance(address=miner.address)
    print("My balance: %3.3f" % balance)
    addresses = WalletManager().get_addresses()
    for address in addresses:
        if address == miner.address: continue
        print("Balance of %s: %3.3f" %
              (toStr(address), UTXOSet().get_balance(address=address)))
Exemplo n.º 3
0
def listAddresses():
    wm = WalletManager()
    addresses = wm.get_addresses()
    if not addresses:
        print("Error: Could not get wallets")
        return

    for address in addresses:
        print(util.toStr(address))
Exemplo n.º 4
0
def createWallet():
    wm = WalletManager()
    w = wm.create_wallet()
    newAddress = util.toStr(w.get_address())
    print("New address: %s" % newAddress)
    return newAddress
Exemplo n.º 5
0
def startServer(mineAddr):
    global knownNodes, miner
    print("My host: %s" % nodeAddress)
    miner = Miner(mineAddr)

    print("My mining address %s" % toStr(miner.address))

    nodes = discoverNodes()
    for name in nodes:
        address = gethostbyname(name)
        if address != nodeAddress:
            knownNodes.append(address)

    print("Known nodes: %s" % str(knownNodes))

    sr = p2p.SocketReader(nodeAddress)
    sr.setMsgHandlers(msgHandlers)
    sr.start()
    bc = Blockchain()

    #done = False
    #while not done:
    while 1:
        start = time.time()
        bestHeight = bc.getBestHeight()
        # generate empty blocks to seed the miners' wallets
        if bestHeight < 5:
            new_block = miner.mine_block()
            broadcast_block(new_block)
        # for blocks 5-10, generate random txs. These will get broadcast
        # across the network (randomly) and eventually fill the mempool.
        # A full mempool will trigger the mining of a new block.
        elif bestHeight < 10:
            if bestHeight == 5:
                print("My balance after 5 blocks: %3.3f" %
                      UTXOSet().get_balance(address=miner.address))
            # Produce a tx every ~2-3 seconds
            time.sleep(random.random() + 2)
            rand_tx = create_random_tx()
            if rand_tx:
                miner.add_txs([rand_tx])
                broadcast_tx(rand_tx)

            # NOTE: We DO want to mine blocks in the miner's thread
            if len(miner.mempool) >= 2:
                # HACK: For testing, gives the other nodes a chance to
                # win the next block
                time.sleep(1.5)
                new_block = miner.mine_transactions()
                broadcast_block(new_block)
                print_balances()
        else:
            pubKeyHash = base58.decode(miner.address)[1:-4]
            for i, block in enumerate(BlockExplorer().iter_blocks()):
                proof = pow.ProofOfWork(block)
                if not proof.validate():
                    print("Error! This block could not be validated")
                #print(json.dumps(block.toDict(), indent=2)
                mined_block = ' no'
                for tx in block.transactions:
                    if tx.isCoinbase() and tx.outDict[0].isLockedWithKey(
                            pubKeyHash):
                        mined_block = ' yes'
                print(block.hash.hex()[:14] + mined_block)
            # pubKeyHash = base58.decode(miner.address)[1:-4]
            # print("My unspent outputs from UTXOSet:")
            # for txOutput in UTXOSet().findUTXO(pubKeyHash):
            #     print(txOutput.toDict())
            #
            # print("My unspent outputs from BlockExplorer:")
            # for txId, txOutputs in BlockExplorer().findUTXO().items():
            #     for txOutput in txOutputs:
            #         if txOutput.isLockedWithKey(pubKeyHash):
            #             print("TXID: %s" % txId.hex())
            #             print(txOutput.toDict())
            sr.stop()
            break