Move BinReader/BinWriter to io and simplify things
BinReader and BinWriter don't belong to util, actually util shouldn't
exist at all. Moving them to io and using them for all encoding/decoding
purposes allows to simplify a lot of code, especially in the error
handling space. These interfaces are designed to absorb errors until
someone can do something meaningful with them (usually that's the top
caller of encode/decode functions) and our current use of them is
inconsistent.
This patchset moves BinReader/BinWriter and size calculations (that
are mostly about Serializable things) to io package and makes all the
other code use them for encoding/decoding purposes.
It's mostly used for Serializable and in other cases where one needs to
estimate binary-encoded size of the stucture. This also simplifies future
removal of the Size() from Serializable.
The logic here is that we'll have all binary encoding/decoding done via our io
package, which simplifies error handling. This functionality doesn't belong to
util, so it's moved.
This also expands BufBinWriter with Reset() method to fit the needs of core
package.
add close function to storage interface
add common defer function call which will close db connection
remove context as soon as it's not needed anymore
updated unit tests
This one fixes#390 and some connected problems. After this patchset the node reconnects to some other nodes if anything goes wrong and it better senses when something goes wrong. It also fixes some block handling problems based on the testnet connection experience.
...and don't try to connect to the nodes we're already connected to.
Before this change we had a problem of discoverer throwing away good valid
addresses just because they are already known which lead to pool draining over
time (as address reuse was basically forbidden and getaddr may not get enough
new nodes).
Queuing one message is not reliable enough, the peer that gets it can fail to
actually make a request, so make this queue a bit deeper to have a higher
chance of success.
This makes writer side handle errors properly and fixes communication between
reader and writer goroutine to always correctly unregister the peer. This is
especially important for the case where error occurs before handshake
completes as in this case we don't even have goroutine in startProtocol()
running.