def _connect_to_peers(self): """ Will attempt to connect to enough peers to get us up to `max_connections`. This should be called again after we disconnect from a peer to maintain a stable number of peers. """ if len(self.peers) < self.max_connections: shuffle(self.addrs) for i in range(self.max_connections - len(self.peers)): if len(self.addrs) > 0: addr = self.addrs.pop(0) peer = PeerFactory(self.params, self.user_agent, self.inventory, self.subscriptions, self.bloom_filter, self._on_peer_disconnected, self.blockchain, self.download_listener) reactor.connectTCP(addr[0], addr[1], peer) self.peers.append(peer) if self.peer_event_listener is not None: self.peer_event_listener.on_peer_connected(addr, len(self.peers)) else: # We ran out of addresses and need to hit up the seeds again. self.addrs = dns_discovery(self.testnet) self._connect_to_peers()
def _connect_to_peers(self): """ Will attempt to connect to enough peers to get us up to `max_connections`. This should be called again after we disconnect from a peer to maintain a stable number of peers. """ if len(self.peers) < self.max_connections: shuffle(self.addrs) for i in range(self.max_connections - len(self.peers)): if len(self.addrs) > 0: addr = self.addrs.pop(0) peer = PeerFactory(self.params, self.user_agent, self.inventory, self.subscriptions, self.bloom_filter, self._on_peer_disconnected, self.blockchain, self.download_listener) reactor.connectTCP(addr[0], addr[1], peer) self.peers.append(peer) if self.peer_event_listener is not None: self.peer_event_listener.on_peer_connected( addr, len(self.peers)) else: # We ran out of addresses and need to hit up the seeds again. self.addrs = dns_discovery(self.testnet) self._connect_to_peers()
self.subscriptions[address] = (len(self.peers) / 2, on_peer_announce) self.bloom_filter.insert(base58.decode(address)[1:21]) for peer in self.peers: if peer.protocol is not None: peer.protocol.load_filter() def unsubscribe_address(self, address): """ Unsubscribe to an address. Will update the bloom filter to reflect its state before the address was inserted. """ if address in self.subscriptions: self.bloom_filter.remove(base58.decode(address)[1:21]) for peer in self.peers: if peer.protocol is not None: peer.protocol.load_filter() del self.subscriptions[address] if __name__ == "__main__": # Connect to testnet logFile = logfile.LogFile.fromFullPath("bitcoin.log", rotateLength=15000000, maxRotatedFiles=1) log.addObserver(FileLogObserver(logFile).emit) log.addObserver(FileLogObserver().emit) bd = BlockDatabase("blocks.db", testnet=True) BitcoinClient(dns_discovery(True), params="testnet", blockchain=bd) reactor.run()
Listen for transactions on an address. Since we can't validate the transaction, we will only callback if a majority of our peers relay it. If less than a majority relay it, we will have to wait for block inclusion to callback. """ def on_peer_announce(tx): txhash = bitcoin.txhash(bitcoin.serialize(tx["tx"])) if txhash in self.subscriptions[address][0] and self.subscriptions[ address][0][txhash][0] != "complete": self.subscriptions[address][0][txhash][0] += 1 if self.subscriptions[address][0][txhash][ 0] >= self.subscriptions[address][0][txhash][1]: self.subscriptions[address][0][txhash][0] = "complete" self.subscriptions[address][1](tx["tx"]) elif txhash not in self.subscriptions[address][0]: self.subscriptions[address][0][txhash] = [ 1, len(self.peers) / 2 ] self.subscriptions[address] = [{}, callback] self.bloom_filter.insert(unhexlify(bitcoin.b58check_to_hex(address))) for peer in self.peers: peer.protocol.add_inv_callback(bitcoin.b58check_to_hex(address), on_peer_announce) peer.protocol.update_filter() if __name__ == "__main__": # Connect to testnet BitcoinClient(dns_discovery(True), params=TESTNET3) reactor.run()
self.subscriptions[txhash]["last_confirmation"] = self.subscriptions[txhash]["confirmations"] callback(self.subscriptions[txhash]["tx"], self.subscriptions[txhash]["in_blocks"], self.subscriptions[txhash]["confirmations"]) self.subscriptions[address] = (len(self.peers)/2, on_peer_announce) self.bloom_filter.insert(base58.decode(address)[1:21]) for peer in self.peers: if peer.protocol is not None: peer.protocol.load_filter() def unsubscribe_address(self, address): """ Unsubscribe to an address. Will update the bloom filter to reflect its state before the address was inserted. """ if address in self.subscriptions: self.bloom_filter.remove(base58.decode(address)[1:21]) for peer in self.peers: if peer.protocol is not None: peer.protocol.load_filter() del self.subscriptions[address] if __name__ == "__main__": # Connect to testnet logFile = logfile.LogFile.fromFullPath("bitcoin.log", rotateLength=15000000, maxRotatedFiles=1) log.addObserver(FileLogObserver(logFile).emit) log.addObserver(FileLogObserver().emit) bd = BlockDatabase("blocks.db", testnet=True) BitcoinClient(dns_discovery(True), params="testnet", blockchain=bd) reactor.run()
for peer in self.peers[:len(self.peers)/2]: peer.protocol.send_message(message(inv_packet, self.params)) return d def subscribe_address(self, address, callback): """ Listen for transactions on an address. Since we can't validate the transaction, we will only callback if a majority of our peers relay it. If less than a majority relay it, we will have to wait for block inclusion to callback. """ def on_peer_announce(tx): txhash = bitcoin.txhash(bitcoin.serialize(tx["tx"])) if txhash in self.subscriptions[address][0] and self.subscriptions[address][0][txhash][0] != "complete": self.subscriptions[address][0][txhash][0] += 1 if self.subscriptions[address][0][txhash][0] >= self.subscriptions[address][0][txhash][1]: self.subscriptions[address][0][txhash][0] = "complete" self.subscriptions[address][1](tx["tx"]) elif txhash not in self.subscriptions[address][0]: self.subscriptions[address][0][txhash] = [1, len(self.peers)/2] self.subscriptions[address] = [{}, callback] self.bloom_filter.insert(unhexlify(bitcoin.b58check_to_hex(address))) for peer in self.peers: peer.protocol.add_inv_callback(bitcoin.b58check_to_hex(address), on_peer_announce) peer.protocol.update_filter() if __name__ == "__main__": # Connect to testnet BitcoinClient(dns_discovery(True), params=TESTNET3) reactor.run()