bitcoind – How does bitcoin core API work locally (network) – bitcoinlib in python using too many web sockets

[ad_1]

I’m trying to understand at a high level, the local network aspects of calling the bitcoin core api via bitcoinlib in python

So the context around this question is that I wanted to work on some python (for which I have very minimal experience or understanding)
so I sat down to attempt to write a blockchain indexer. I run a full node and figured I would see what I could do with the api.

Bitcoinlib offers a class called AuthServiceProxy. You pass the local api url & port to this, it looks like this:
rpc_connection = AuthServiceProxy(“http://user:[emailย protected]:8332″)

I dug a little bit into this class to find that it’s using httlib.HTTPConnection to open the connection itself.

        if connection:
            # Callables re-use the connection of the original proxy
            self.__conn = connection
        elif self.__url.scheme == 'https':
            self.__conn = httplib.HTTPSConnection(self.__url.hostname, port,
                                                  timeout=timeout)
        else:
            self.__conn = httplib.HTTPConnection(self.__url.hostname, port,
                                                 timeout=timeout)

The first thing I attempted to do was to write a loop that would iterate over all the blocks stored locally to create an array out of the hashes of each block.

        while block_counter < total_blocks:
            rpc_connection = rpcConnector.rpcConnect()
            block_hash = rpc_connection.getblockhash(total_blocks)

this is essentially how it is getting called. The rpcConnector is just that AuthServiceProxy call essentially.

So I ran this and printed the output of each block hash just to see how it was working. It worked up until about the 16200th iteration. Then it fails with the error message
“[WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted”. It fails at this mark each time I run it.

I thought that maybe the connections weren’t being closed out correctly so I tried a bunch of different things to try and close the connection within each loop iteration
Nothing worked.

The last thing I tried was this: rpc_connection._AuthServiceProxy__conn.close(). I figured since the AuthServiceProxy was just using HTTPConnection I could just call the .close()
I stepped through the code to see that it was in fact getting into the .close() method but it wasn’t ever hitting close.

    def close(self):
        """Close the connection to the HTTP server."""
        self.__state = _CS_IDLE
        try:
            sock = self.sock
            if sock:
                self.sock = None
                sock.close()   # close it manually... there may be other refs
        finally:
            response = self.__response
            if response:
                self.__response = None
                response.close()

both the Sock and Response were None.

So maybe this is a networking question but it seems like when I call the API it’s not keeping a connection open, so if that is the case then my question is:
how am I using up the addresses, and why is it always stopping around 16200?

and I guess to close it out, how can I resolve this issue?

[ad_2]

Source link


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *