Exemplo n.º 1
0
    def __init__(self, argv):
        self.config = yaml.load(open(".peergovrc", "r").read())
        self.basedir = self.getConfig('basedir')
        self.datadir = self.getConfig('datadir')
        self.authdir = self.getConfig('authdir')
        self.user = self.getConfig(
            'userfpr'
        )  #TODO: confirm that fingerprint actually exists in key ring
        self.port = self.getConfig('port')
        self.authorizations = {}
        self.currentAuthorization = None  #authorization to vote on currentTopic

        self.voting = SchulzeVoting.SchulzeVoting()
        self.manager = DataManager()
        self.manager.datadir = self.datadir
        self.cctx = pyme.core.Context()  #crypto context

        self.ensureDirExists(self.basedir)
        self.ensureDirExists(self.datadir)
        self.ensureDirExists(self.authdir)

        for root, dirs, files in os.walk(self.datadir):
            for dir in dirs:
                self.loadTopic(root + "/" + dir)
            for file in files:
                self.loadData(root, file)

        for root, dirs, files in os.walk(self.authdir):
            for file in files:
                self.loadAuth(root, file)

        if hasGui:
            self.initGui()
        self.peermanager = PeerManager(argv, peergov=self)
Exemplo n.º 2
0
def main():
    config = create_config()

    # chain manager
    chain_manager = ChainManager(config=config)

    # peer manager
    peer_manager = PeerManager(config=config)

    # start tcp server
    try:
        tcp_server = TcpServer(peer_manager,
                               config.get('network', 'listen_host'),
                               config.getint('network', 'listen_port'))
    except IOError as e:
        logger.error("Could not start TCP server: \"{0}\"".format(str(e)))
        sys.exit(1)

    peer_manager.local_address = (tcp_server.ip, tcp_server.port)
    tcp_server.start()
    peer_manager.start()
    chain_manager.start()

    # handle termination signals
    def signal_handler(signum=None, frame=None):
        logger.info('Signal handler called with signal {0}'.format(signum))
        peer_manager.stop()
        chain_manager.stop()
        # tcp_server checks for peer_manager.stopped()
    for sig in [signal.SIGTERM, signal.SIGHUP, signal.SIGQUIT, signal.SIGINT]:
        signal.signal(sig, signal_handler)

    # connect peer
    if config.get('network', 'remote_host'):
        peer_manager.connect_peer(
            config.get('network', 'remote_host'),
            config.getint('network', 'remote_port'))

    # loop
    while not peer_manager.stopped():
        time.sleep(0.1)
        if len(peer_manager.get_connected_peer_addresses()) > 2:
            chain_manager.bootstrap_blockchain()

    logger.info('exiting')
    # tcp_server.join() # does not work!
    peer_manager.join()
Exemplo n.º 3
0
    def __init__(self, parent=None):
        super(Client, self).__init__(parent)
        self.server = Server()#client var
        #self.peers = {}#client var
        self.peers = defaultdict(list)
        from peermanager import PeerManager
        self.peerManager = PeerManager(self)#client var
        self.peerManager.setServerPort(self.server.serverPort())
        self.peerManager.startBroadcasting()


        self.peerManager.newconnection_signal.connect(self.newConnection)
        self.server.socketnewconnection_signal.connect(self.newConnection)
Exemplo n.º 4
0
        # create blocks.dat file
        shutil.copy('genesis.dat', os.path.join(datadir + '/blocks.dat'))
        print os.path.join(datadir + '/blocks.dat')
        # create lock file for db
        with open(datadir + '/__db.001', 'a'):
            pass

    # create wallet
    wallet = Wallet()
    if new_install:
        # initialize wallet
        wallet.initialize()
    mempool = MemPool(log)
    chaindb = ChainDb(settings, settings['db'], log, mempool, wallet, netmagic, False, False)
    node = Node(None, log, mempool, chaindb, netmagic)
    peermgr = PeerManager(node, log, mempool, chaindb, netmagic)
    node.peermgr = peermgr
    wallet.chaindb = chaindb

    # load blocks.dat into db, if db is newly created
    if new_install:
        chaindb.loadfile(datadir + '/blocks.dat')
    
    if 'loadblock' in settings:
        chaindb.loadfile(settings['loadblock'])

    threads = []

    def new_connection_handler(socket, address):
        print "New incoming connection >>>>>>>>>>>"
        connection = Connection(node, socket, address)
Exemplo n.º 5
0
def run(config_file = '~/.bitcoinpy.cfg'):
    # check if configuration file exists
    if not os.path.isfile(os.path.expanduser(config_file)):
        print('No configuration file: {0}'.format(config_file))
        sys.exit(1)

    settings = {}
    f = open(os.path.expanduser(config_file))
    for line in f:
        m = re.search('^(\w+)\s*=\s*(\S.*)$', line)
        if m is None:
            continue
        settings[m.group(1)] = m.group(2)
    f.close()

    if 'host' not in settings:
        settings['host'] = '127.0.0.1'
    if 'port' not in settings:
        settings['port'] = 8333
    if 'rpcport' not in settings:
        settings['rpcport'] = 9332
    if 'db' not in settings:
        settings['db'] = '/tmp/chaindb'
    if 'chain' not in settings:
        settings['chain'] = 'mainnet'
    chain = settings['chain']
    if 'log' not in settings or (settings['log'] == '-'):
        settings['log'] = None

    # setup logging
    if settings['log']:
        logging.basicConfig(filename=settings['log'], level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger(__name__)

    if ('rpcuser' not in settings or
        'rpcpass' not in settings):
        logger.error("You must set the following in config: rpcuser, rpcpass")
        sys.exit(1)

    settings['port'] = int(settings['port'])
    settings['rpcport'] = int(settings['rpcport'])
    settings['db'] = os.path.expanduser(settings['db'])

    if chain not in NETWORKS:
        logger.error("invalid network, exiting")
        sys.exit(2)

    netmagic = NETWORKS[chain]
    datadir = settings['db']

    # initialize
    new_install = False
    if not os.path.isdir(os.path.expanduser(datadir)):
        new_install = True
        initialize(datadir)

    # create wallet
    walletdb = Wallet()
    if new_install:
        walletdb.initialize()
    mempool = MemPool()
    chaindb = ChainDb(settings, settings['db'], mempool, walletdb,  netmagic, False, False)
    node = Node(None, mempool, chaindb, netmagic)
    peermgr = PeerManager(node, mempool, chaindb, netmagic)
    node.peermgr = peermgr
    walletdb.chaindb = chaindb

    # load blocks.dat into db, if db is newly created
    if new_install:
        chaindb.loadfile(datadir + '/blocks.dat')

    if 'loadblock' in settings:
        chaindb.loadfile(os.path.expanduser(settings['loadblock']))

    threads = []

    def new_connection_handler(socket, address):
        logger.info("New incoming connection")
        connection = Connection(node, socket, address)
        connection.start()

    # start HTTP server for JSON-RPC
    rpcexec = rpc.RPCExec(peermgr, mempool, chaindb, walletdb, settings['rpcuser'], settings['rpcpass'])
    rpcserver = gevent.pywsgi.WSGIServer(('', settings['rpcport']),  rpcexec.handle_request, log = None)
    rpc_server_thread = gevent.Greenlet(rpcserver.serve_forever)
    threads.append(rpc_server_thread)

    # start server
    server = StreamServer((settings['host'], settings['port']), new_connection_handler)
    server_thread = gevent.Greenlet(server.serve_forever)
    threads.append(server_thread)

    # connect to specified peers
    if 'peers' in settings and settings['peers']:
        for ipport in settings['peers'].split():
            peerip, peerport = ipport.split(":")
            peer = peermgr.add(peerip, int(peerport))
            threads.append(peer)
            time.sleep(2)

    # program main loop
    def start(timeout=None):
        for thread in threads:
            thread.start()
        try:
            gevent.joinall(threads, timeout=timeout, raise_error=True)
        finally:
            for t in threads: t.kill()
            gevent.joinall(threads)
            logger.info('Flushing database')
            del chaindb.db
            chaindb.blk_write.close()
            logger.info('Finished flushing database')

    start()
Exemplo n.º 6
0
def run(config_file = '~/.bitcoinpy.cfg'):
    # check if configuration file exists
    if not os.path.isfile(os.path.expanduser(config_file)):
        print('No configuration file: {0}'.format(config_file))
        sys.exit(1)

    settings = {}
    f = open(os.path.expanduser(config_file))
    for line in f:
        m = re.search('^(\w+)\s*=\s*(\S.*)$', line)
        if m is None:
            continue
        settings[m.group(1)] = m.group(2)
    f.close()

    if 'host' not in settings:
        settings['host'] = '127.0.0.1'
    if 'port' not in settings:
        settings['port'] = 8333
    if 'rpcport' not in settings:
        settings['rpcport'] = 9332
    if 'db' not in settings:
        settings['db'] = '/tmp/chaindb'
    if 'chain' not in settings:
        settings['chain'] = 'mainnet'
    chain = settings['chain']
    if 'log' not in settings or (settings['log'] == '-'):
        settings['log'] = None

    # setup logging
    if settings['log']:
        logging.basicConfig(filename=settings['log'], level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.DEBUG)
    logger = logging.getLogger(__name__)

    if ('rpcuser' not in settings or
        'rpcpass' not in settings):
        logger.error("You must set the following in config: rpcuser, rpcpass")
        sys.exit(1)

    settings['port'] = int(settings['port'])
    settings['rpcport'] = int(settings['rpcport'])
    settings['db'] = os.path.expanduser(settings['db'])

    if chain not in NETWORKS:
        logger.error("invalid network, exiting")
        sys.exit(2)

    netmagic = NETWORKS[chain]
    datadir = settings['db']

    # initialize
    new_install = False
    if not os.path.isdir(os.path.expanduser(datadir)):
        new_install = True
        initialize(datadir)

    # create wallet
    walletdb = Wallet()
    if new_install:
        walletdb.initialize()
    mempool = MemPool()
    chaindb = ChainDb(settings, settings['db'], mempool, walletdb,  netmagic, False, False)
    node = Node(None, mempool, chaindb, netmagic)
    peermgr = PeerManager(node, mempool, chaindb, netmagic)
    node.peermgr = peermgr
    walletdb.chaindb = chaindb

    # load blocks.dat into db, if db is newly created
    if new_install:
        chaindb.loadfile(datadir + '/blocks.dat')

    if 'loadblock' in settings:
        chaindb.loadfile(os.path.expanduser(settings['loadblock']))

    threads = []

    def new_connection_handler(socket, address):
        logger.info("New incoming connection")
        connection = Connection(node, socket, address)
        connection.start()

    # start HTTP server for JSON-RPC
    rpcexec = rpc.RPCExec(peermgr, mempool, chaindb, walletdb, settings['rpcuser'], settings['rpcpass'])
    rpcserver = gevent.pywsgi.WSGIServer(('', settings['rpcport']),  rpcexec.handle_request, log = None)
    rpc_server_thread = gevent.Greenlet(rpcserver.serve_forever)
    threads.append(rpc_server_thread)

    # start server
    server = StreamServer((settings['host'], settings['port']), new_connection_handler)
    server_thread = gevent.Greenlet(server.serve_forever)
    threads.append(server_thread)

    # connect to specified peers
    if 'peers' in settings and settings['peers']:
        for ipport in settings['peers'].split():
            peerip, peerport = ipport.split(":")
            peer = peermgr.add(peerip, int(peerport))
            threads.append(peer)
            time.sleep(2)

    # program main loop
    def start(timeout=None):
        for thread in threads:
            thread.start()
        try:
            gevent.joinall(threads, timeout=timeout, raise_error=True)
        finally:
            for t in threads: t.kill()
            gevent.joinall(threads)
            logger.info('Flushing database')
            del chaindb.db
            chaindb.blk_write.close()
            logger.info('Finished flushing database')

    start()
Exemplo n.º 7
0
def main():
    config = create_config()

    # chain manager
    chain_manager = ChainManager(config=config)

    # peer manager
    peer_manager = PeerManager(config=config)

    # start tcp server
    try:
        tcp_server = TcpServer(peer_manager,
                               config.get('network', 'listen_host'),
                               config.getint('network', 'listen_port'))
    except IOError as e:
        logger.error("Could not start TCP server: \"{0}\"".format(str(e)))
        sys.exit(1)

    peer_manager.local_address = (tcp_server.ip, tcp_server.port)
    tcp_server.start()
    peer_manager.start()
    chain_manager.start()

    # handle termination signals
    def signal_handler(signum=None, frame=None):
        logger.info('Signal handler called with signal {0}'.format(signum))
        peer_manager.stop()
        chain_manager.stop()
        # tcp_server checks for peer_manager.stopped()

    for sig in [signal.SIGTERM, signal.SIGHUP, signal.SIGQUIT, signal.SIGINT]:
        signal.signal(sig, signal_handler)

    # connect peer
    if config.get('network', 'remote_host'):
        peer_manager.connect_peer(config.get('network', 'remote_host'),
                                  config.getint('network', 'remote_port'))

    # loop
    while not peer_manager.stopped():
        time.sleep(0.1)
        if len(peer_manager.get_connected_peer_addresses()) > 2:
            chain_manager.bootstrap_blockchain()

    logger.info('exiting')
    # tcp_server.join() # does not work!
    peer_manager.join()
Exemplo n.º 8
0
Arquivo: app.py Projeto: jnnk/pydevp2p
    if len(sys.argv) == 1:
        config = yaml.load(io.BytesIO(sample_config))
        pubkey = crypto.privtopub(config['p2p']['privkey_hex'].decode('hex'))
        config['p2p']['node_id'] = crypto.sha3(pubkey)
    else:
        fn = sys.argv[1]
        log.info('loading config from', fn=fn)
        config = yaml.load(open(fn))

    print config
    # create app
    app = BaseApp(config)

    # register services
    NodeDiscovery.register_with_app(app)
    PeerManager.register_with_app(app)
    #  JSONRPCServer.register_with_app(app)

    # start app
    app.start()

    # wait for interupt
    evt = gevent.event.Event()
    # gevent.signal(signal.SIGQUIT, gevent.kill) ## killall pattern
    gevent.signal(signal.SIGQUIT, evt.set)
    gevent.signal(signal.SIGTERM, evt.set)
    gevent.signal(signal.SIGINT, evt.set)
    evt.wait()

    # finally stop
    app.stop()
Exemplo n.º 9
0
def main():
    # config
    import yaml
    import io
    import sys
    import signal
    import gevent
    from peermanager import PeerManager
    from jsonrpc import JSONRPCServer
    from discovery import NodeDiscovery
    import slogging
    log = slogging.get_logger('app')
    slogging.configure(config_string=':debug')

    # read config
    sample_config = """
p2p:
    num_peers: 10
    bootstrap_nodes:
        # local bootstrap
        # - enode://6ed2fecb28ff17dec8647f08aa4368b57790000e0e9b33a7b91f32c41b6ca9ba21600e9a8c44248ce63a71544388c6745fa291f88f8b81e109ba3da11f7b41b9@127.0.0.1:30303
        # go_bootstrap
        #- enode://6cdd090303f394a1cac34ecc9f7cda18127eafa2a3a06de39f6d920b0e583e062a7362097c7c65ee490a758b442acd5c80c6fce4b148c6a391e946b45131365b@54.169.166.226:30303
        # cpp_bootstrap
        - enode://4a44599974518ea5b0f14c31c4463692ac0329cb84851f3435e6d1b18ee4eae4aa495f846a0fa1219bd58035671881d44423876e57db2abd57254d0197da0ebe@5.1.83.226:30303

    listen_host: 0.0.0.0
    listen_port: 30303
node:
    privkey_hex: 65462b0520ef7d3df61b9992ed3bea0c56ead753be7c8b3614e0ce01e4cac41b
    """
    if len(sys.argv) == 1:
        config = yaml.load(io.BytesIO(sample_config))
        pubkey = crypto.privtopub(config['node']['privkey_hex'].decode('hex'))
        config['node']['id'] = crypto.sha3(pubkey)
    else:
        fn = sys.argv[1]
        log.info('loading config from', fn=fn)
        config = yaml.load(open(fn))

    # stop on every unhandled exception!
    gevent.get_hub().SYSTEM_ERROR = BaseException  # (KeyboardInterrupt, SystemExit, SystemError)

    print config
    # create app
    app = BaseApp(config)

    # register services
    NodeDiscovery.register_with_app(app)
    PeerManager.register_with_app(app)
    #  JSONRPCServer.register_with_app(app)

    # start app
    app.start()

    # wait for interupt
    evt = gevent.event.Event()
    # gevent.signal(signal.SIGQUIT, gevent.kill) ## killall pattern
    gevent.signal(signal.SIGQUIT, evt.set)
    gevent.signal(signal.SIGTERM, evt.set)
    gevent.signal(signal.SIGINT, evt.set)
    evt.wait()

    # finally stop
    app.stop()
Exemplo n.º 10
0
class Client(QObject):
    newmessage_signal = QtCore.pyqtSignal(str, str)
    newparticipant_signal = QtCore.pyqtSignal(str)
    participant_signal = QtCore.pyqtSignal(str)
    participantleft_signal = QtCore.pyqtSignal(str)
    def __init__(self, parent=None):
        super(Client, self).__init__(parent)
        self.server = Server()#client var
        #self.peers = {}#client var
        self.peers = defaultdict(list)
        from peermanager import PeerManager
        self.peerManager = PeerManager(self)#client var
        self.peerManager.setServerPort(self.server.serverPort())
        self.peerManager.startBroadcasting()


        self.peerManager.newconnection_signal.connect(self.newConnection)
        self.server.socketnewconnection_signal.connect(self.newConnection)

    def sendMessage(self, message):
        if not message:
            return;

        #connections = self.peers.values();
        connections = self.peers.values();
        for connection in connections:
            for oneconnection in connection:
                oneconnection.sendMessage(message)

    def nickName(self):
        return str(self.peerManager.userName()) + '@' + QHostInfo.localHostName() + ':' + str(self.server.serverPort())


    def hasConnection(self, senderIp, senderPort):
        if senderPort == -1:
            #return self.peers.__contains__(senderIp)
            #return (senderIp in self.peers)
            '''
            if senderIp in self.peers:
                return True
            else:
                return False
            '''
            if self.peers[senderIp]:
                return True
            else:
                return False

        #if not self.peers.__contains__(senderIp):
        '''
        if senderIp in self.peers:
            return True
        else:
            return False
        '''
        if not self.peers[senderIp]:
            return False

        #connections = self.peers.values(senderIp)
        connections = self.peers[senderIp]
        if connections:
            for connection in connections:
                if connection.peerPort() == senderPort:
                    return True

        return False

    @pyqtSlot(Connection)
    def newConnection(self, argconnection):
        connection = argconnection
        connection.setGreetingMessage(self.peerManager.userName())

        connection.error.connect(self.connectionError)
        connection.disconnected.connect(self.disconnected)
        connection.readyForUse_signal.connect(self.readyForUse)

    @pyqtSlot()
    def readyForUse(self):
        #connection = self.sender()
        connection = QtCore.QObject.sender(self)
        if (not connection or self.hasConnection(connection.peerAddress().toString(), connection.peerPort())):
            return

        connection.newmessage_signal.connect(self.newmessage_signal)
        #self.peers[connection.peerAddress()] = connection
        self.peers[connection.peerAddress().toString()].append(connection)
        nick = str(connection.name())
        if nick:
            self.newparticipant_signal.emit(nick)

    @pyqtSlot()
    def disconnected(self):
        #connection = self.sender()
        connection = QtCore.QObject.sender(self)
        if connection:
            self.removeConnection(connection)

    @pyqtSlot()
    def connectionError(self):
        #connection = self.sender()
        connection = QtCore.QObject.sender(self)
        if connection:
            self.removeConnection(connection)

    def removeConnection(self, connection):
        #if connection.peerAddress() in self.peers:
        #if self.peers.__contains__(connection.peerAddress()):
        #if connection.peerAddress() in self.peers:
        if self.peers[connection.peerAddress().toString()]:
            del self.peers[connection.peerAddress().toString()]
            nick = connection.name()
            if not nick.isEmpty():
                self.participantleft_signal.emit(str(nick))
        
        connection.deleteLater()
Exemplo n.º 11
0
def main():

    # use this to enable/disable all the other printing

    print 'Initializing Torrent Manager...',

    data = {}

    # The first thing we want to do is have them input a file
    # For now this is just going to be hard-coded in
    #   because I don't want to have to input everytime
    # file1 is a single-file torrent
    # file2 is a multi-file torrent
    #file1 = '../referenceFiles/TestTorrent.torrent'
    #file1 = '../referenceFiles/WhySoccerMatters-Original.torrent'
    #file1 = 'ThisIsNotARealFile.torrent'
    file1 = '/Users/brent/Downloads/ubuntu-13.10-desktop-amd64.iso.torrent'
    #file1 = '/Users/brent/Downloads/t-rice.jpg.torrent'
    #file1 = '../referenceFiles/ProGit.pdf.torrent'
    #file1 = '/Users/brent/Downloads/test.torrent'

    data['torrent_file'] = file1
    data['output_path'] = '/Users/brent/Documents'
    data['peer_id'] = 'ThisIsATestOfGTENS00'
    #data['key'] = '50EDCACE'
    data['key'] = 'A89BA1C1'
    data['port'] = 61130
    data['compact'] = 1
    data['no_peer_id'] = 1
    data['max_connections'] = 10
    data['state'] = 'started'
    data['numwant'] = 20
    data['file_name'] = []
    data['length'] = []
    data['file_length'] = 0

    # Next we decode it
    # decoded_data is the data from the torrent file.
    # we will separate everything and put it into a data
    # structure so that we don't have to do things like
    # decoded_data['info']['piece length'] and we can
    # do things like data['piece_length']
    decoded_data = bdecode_torrent(data['torrent_file'])
    # print decoded_data
    # print decoded_data

    # Get the SHA1 Hash for the info dictionary
    # It has to be bencoded first
    info = decoded_data['info']
    info = bencode_data(info)
    info_hash = create_hash(info)

    # Add our stuff to the data structure
    data['info_hash'] = info_hash
    data['piece_length'] = decoded_data['info']['piece length']
    data['announce'] = decoded_data['announce']
    data['pieces_hash'] = decoded_data['info']['pieces']
    data['name'] = decoded_data['info']['name']

    # Check to see if it is a multi-file torrent or a single-file torrent
    # Multi File
    if 'files' in decoded_data['info']:
        data['multi_file'] = True
        data['path'] = data['output_path'] + os.sep + \
            decoded_data['info']['name'] + os.sep

        # Each file name is actually a path
        for one_file in decoded_data['info']['files']:
            file_name = one_file['path'][0]
            data['file_name'].append(file_name)
            data['length'].append(one_file['length'])
            data['file_length'] = data['file_length'] + one_file['length']

    # Single File
    else:
        data['multi_file'] = False
        data['file_name'] = decoded_data['info']['name']
        data['path'] = data['output_path'] + os.sep
        data['length'] = decoded_data['info']['length']
        data['file_length'] = data['length']

    print 'Initialized'

    print 'Initializing Tracker Manager...',
    TrackM = TrackerManager(data)
    print 'Initialized'
    print 'Initializing Peer Manager...',
    PeerM = PeerManager(data)
    print 'Initialized'
    print 'Initializing Piece Manager...',
    PieceM = PieceManager(data)
    print 'Initialized'
    print ''

    TrackM.peer_mgr = PeerM
    TrackM.piece_mgr = PieceM
    PeerM.track_mgr = TrackM
    PeerM.piece_mgr = PieceM
    PieceM.track_mgr = TrackM
    PieceM.peer_mgr = PeerM

    # Once this Peer Manager is spawned, this MainThread will stop
    # Calling 'print threading.enumerate() will output this:'
    # [<_MainThread(MainThread, stopped 140735224099600)>
    # , <PeerManager(Thread-1, started 4568743936)>]
    PeerM.start()