def start(): with open(DATA_FILE) as fin: blocks = fin.read().strip().split('\n') for block in blocks[:1]: block = json.loads(block) txs = block['txs'] n_txs = len(txs) bytes_pushed['n_txs'] = n_txs logging.info("Read %d transactions" % n_txs) logging.info( "Selecting %d transactions at random for inclusion in mempool" % (MEMPOOL_PCT * n_txs)) # random.shuffle(txs) n_selected = 0 for i, tx in enumerate(txs): if n_selected < MEMPOOL_PCT * n_txs: selected_txs.append(tx) n_selected += 1 else: # print("Excluding ", tx['hash']) skipped_txs_ids.add(tx['hash'].encode('utf-8')) bytes_pushed['txs'].append(len(tx_to_bytes(tx))) skipped_txs.append(tx) bytes_pushed['cmpct'] = n_txs * 6 + 10 + 80 logging.info("Skipped %d transactions" % len(skipped_txs)) client = network.GeventNetworkClient() client.register_handler('inv', inv_handler) client.register_handler('graphene', graphene_handler) network.ClientBehavior(client) client.listen(address, port) client.run_forever()
def start(): client = network.GeventNetworkClient() client.register_handler('inv', on_inv_received) client.register_handler('tx', on_tx_received) client.register_handler('block', on_block_received) network.ClientBehavior(client) client.connect(('seed.bitcoinstats.com', 8333)) client.run_forever()
def test_listen(self, msocket): nc = network.GeventNetworkClient() group_size = len(nc.connection_group) nc.listen() self.assertTrue(nc.socket.bind.called) self.assertTrue(nc.socket.listen.called) self.assertEquals(len(nc.connection_group), group_size + 1)
def test_accept(self, mspawn, mspawn_later): nc = network.GeventNetworkClient() nc.socket = mock.Mock() connection_handler = mock.Mock() nc.register_handler(network.ConnectionEstablishedEvent.type, connection_handler) nc.socket.accept = mock.Mock(side_effect=[(mock.Mock(), ( '10.0.0.1', 8333)), StopIteration()]) self.assertRaises(StopIteration, nc.accept) self.assertTrue(connection_handler.called)
def start(): with open(DATA_FILE) as fin: blocks = fin.read().strip().split('\n') for block in blocks[:1]: block = json.loads(block) txs = block['txs'] n_txs = len(txs) logging.info("Read %d transactions" % n_txs) # random.shuffle(txs) n_selected = 0 selected_txs.extend(txs) client = network.GeventNetworkClient() client.register_handler('getdata', getdata_handler) client.register_handler('verack', verack_handler) network.ClientBehavior(client) client.connect((peer_address, peer_port)) client.run_forever()
def test_accept_idle_timeout(self, mspawn, mspawn_later): nc = network.GeventNetworkClient() nc.socket = mock.Mock() connection_handler = mock.Mock() nc.register_handler(network.ConnectionLostEvent.type, connection_handler) nc.socket.accept = mock.Mock(side_effect=[(mock.Mock(), ( '10.0.0.1', 8333)), StopIteration()]) def spawn_later(t, callable, *args, **kwargs): callable(*args, **kwargs) # Wire the idle timeout handler to be called immediately mspawn_later.side_effect = spawn_later self.assertRaises(StopIteration, nc.accept) self.assertEquals(len(nc.connections), 0) self.assertTrue(connection_handler.called)
from bitcoin import network from socket import gethostbyname import logging __author__ = 'cdecker' network_client = network.GeventNetworkClient() ip = gethostbyname('seed.bitcoinstats.com') network_client.connect((ip, 8333)) def send_version(connection, message): v = network.messages.VersionPacket() v.addr_from = network.messages.Address('127.0.0.1', True, 8333, 1) v.addr_recv = network.messages.Address( connection.host[0], True, connection.host[1], 1 ) v.best_height = 0 connection.send('version', v) def send_verack(connection, message): connection.send('verack', '') def handle_message(connection, message): logging.info('%s message from %s:%d', message.type, *connection.host) print '%s message from %s:%d' % ( message.type, connection.host[0], connection.host[1]
def test_connect(self, mgevent): nc = network.GeventNetworkClient() conn = nc.connect(('10.0.0.1', 8333)) self.assertTrue(conn) self.assertTrue(mgevent.spawn.called)
def test_init(self): network.GeventNetworkClient()
def test_run_forever(self): nc = network.GeventNetworkClient() nc.connection_group = mock.Mock() nc.run_forever() self.assertTrue(nc.connection_group.join.called)