Fix network race conditions
Map accesses were causing some race conditions. For example, Broadcast
was looping through the addressbook and calling an RPC on each address, but the RPC could have the side effect of modifying the addressbook. Mutexes and copying have been introduced to prevent this.
An unrelated race condition was occurring in the network test due to the use of global variables. These have been replaced with channels. It isn't the cleanest solution, but it does the job.
Also, RandomPeer()
now explicitly uses math/rand
instead of relying on randomized map iteration. As a side effect, sharePeers
may now return duplicate peers, but this would be fairly trivial to correct if necessary.