Ejemplo n.º 1
0
def peer_check(i, peers):
    peer = peers[i][0]
    # request their blockcount
    block_count = network_cmd(peer, {'type': 'blockCount'})
    if not isinstance(block_count, dict):
        return
    if 'error' in block_count.keys():
        return

    peers[i][2] = block_count['length']

    # tools.db_put('peers_ranked', peers)
    us_length = tools.db_get('length')
    their_length = block_count['length']

    # simple, if we are ahead, we give them our blocks
    if their_length < us_length:
        give_block(peer, custom.queues, their_length)
    elif their_length == us_length:
        # If we are at the same amount of block, we ask for new transaction they know
        try:
            ask_for_txs(peer, custom.queues)
        except Exception as exc:
            tools.log('ask for tx error')
            tools.log(exc)
    # request blocks
    # we are back
    else:
        download_blocks(peer, custom.queues, block_count, us_length)

    F = False

    # Send and receive peers
    my_peers = tools.db_get('peers_ranked')
    their_peers = network_cmd(peer, {'type': 'peers'})

    if type(their_peers) == list:
        for p in their_peers:
            if p not in my_peers:
                F = True
                my_peers.append(p)

        for p in my_peers:
            if p not in their_peers:
                network_cmd(peer, {'type': 'recieve_peer', 'peer': p})

    if F:
        my_peers = np.array(my_peers)
        tools.update_peers(my_peers[:, 0])

    return 0
Ejemplo n.º 2
0
def peer_check(i, peers):
    peer = peers[i][0]
    # request their blockcount
    block_count = network_cmd(peer, {'type': 'blockCount'})
    if not isinstance(block_count, dict):
        return
    if 'error' in block_count.keys():
        return

    peers[i][2] = block_count['length']

    # tools.db_put('peers_ranked', peers)
    us_length = tools.db_get('length')
    their_length = block_count['length']

    # simple, if we are ahead, we give them our blocks
    if their_length < us_length:
        give_block(peer, custom.queues, their_length)
    elif their_length == us_length:
        # If we are at the same amount of block, we ask for new transaction they know
        try:
            ask_for_txs(peer, custom.queues)
        except Exception as exc:
            tools.log('ask for tx error')
            tools.log(exc)
    # request blocks
    # we are back
    else:
        download_blocks(peer, custom.queues, block_count, us_length)

    F = False

    # Send and receive peers
    my_peers = tools.db_get('peers_ranked')
    their_peers = network_cmd(peer, {'type': 'peers'})

    if type(their_peers) == list:
        for p in their_peers:
            if p not in my_peers:
                F = True
                my_peers.append(p)

        for p in my_peers:
            if p not in their_peers:
                network_cmd(peer, {'type': 'recieve_peer', 'peer': p})

    if F:
        my_peers = np.array(my_peers)
        tools.update_peers(my_peers[:, 0])

    return 0
Ejemplo n.º 3
0
def main(peers):
    # Check on the peers to see if they know about more blocks than we do.

    p = tools.db_get('peers_ranked')

    if type(p) != list:
        time.sleep(3)
        return main(peers, custom.queues)

    tools.update_peers(peers)

    try:
        while True:
            if tools.db_get('stop'):
                return
            if len(peers) > 0:
                # If we find at least one peer, we call the main_once method
                main_once(custom.queues)
    except Exception as exc:
        tools.log(exc)
Ejemplo n.º 4
0
def main(peers):
    # Check on the peers to see if they know about more blocks than we do.

    p = tools.db_get('peers_ranked')

    if type(p) != list:
        time.sleep(3)
        return main(peers, custom.queues)

    tools.update_peers(peers)

    try:
        while True:
            if tools.db_get('stop'):
                return
            if len(peers) > 0:
                # If we find at least one peer, we call the main_once method
                main_once(custom.queues)
    except Exception as exc:
        tools.log(exc)