Proper fix for #1525.
Using python's GC module, I've verified that the daemon, when running,
now releases all verifiers, synchronizers and wallets - all the resources
we care about releasing.
Previously network.py had its own idea of request IDs,
and each interface had its own which was sent on the wire.
The interface would jump through hoops to translate one
to the other.
This unifies them so that a message ID is passed when
queueing a request, in addition to the method and params.
network.py is now solely responsible for message ID management.
Apart from being simpler and clearer, this also should be faster
as there is much less data structure manipulation and rebuilding
happening.
On startup we make several connections simultaneously. Socket
maintenance code checks if we're not connected, and if not
switches to a connected interface if auto_connect.
Unfortunately this meant that we frequently didn't reconnect to
the prior good server on startup, because some other connection
would happen first and this code would decide to switch to it.
Instead, only switch if a connection attempt is not in progress.
If that times out at the O/S level the switch will happen.
Occurred when switching interfaces and there were unanswered
requests that need resending. This bug isn't new; it's been
there since at least 3rd June.
Remove interface communication out of blockchain.py
into network.py. network.py handles protocol requests
for headers and chunks. blockchain.py continues to
handle their analysis and verification.
If an interface provides a header chain that doesn't
connect, it is dismissed, as per a previous TODO comment.
This removes a thread and another source of timeouts.
I see no performance issues with this when truncating the
blockchain.
Rename 'result' to 'header' for clarity.
This allows us to distinguish between connecting and connected
state in interface.py (used to be done in network.py but that
had other issues).
This means we don't switch to a connecting server, and get_interfaces()
does not report connecting ones.
interfaces we have created.
Existing code has the concept of pending servers, where a connection
thread is started but has not sent a connection notification, and
and interfaces which have received the notification.
This separation caused a couple of minor bugs, and given the cleaner
semantics of unifying the two I don't think the separation is beneficial.
The bugs:
1) When stopping the network, we only stopped the connected interface
threads, not the pending ones. This would leave Python hanging
on exit if we don't make them daemon threads.
2) start_interface() did not check pending servers before starting
a new thread. Some of its callers did, but not all, so it was
possible to initiate two threads to one server and "lose" one thread.
Apart form fixing the above two issues, unification causes one more
change in semantics: we are now willing to switch to a connection
that is pending (we don't switch to failed interfaces). I don't
think that is a problem: if it times out we'll just switch
again when we receive the disconnect notification, and previously the
fact that an interface was in the interaces dictionary wasn't a
guarantee the connection was good anyway: we might not have processed
a pending disconnection notification.
Three places used to test for lagging and switch. Commonize the
code. We test for lagging before autoconnect so lagging
diagnostics are output if not auto-connect.
Lagging diagnotics moved to server_is_lagging().