I have been planning to do this for a long time and finally I decided I had to opensource my Bitcoin network protocol implementation. I was hoping to have it ready and robust by the time I'd release it into the open but my master thesis is about to start and I still have to complete some papers I've been writing for quiet some time, so I'm forced to release an unfinished implementation.
My goal was to create a stable and versatile implementation of the P2P protocol the clients use to communicate with each other in Java. This is not a simple frontend but an attempt to create a clean room implementation of the actual protocol! I know there are quiet a few people out there trying to do exactly the same, and I would love to get them on board with this project.
The need for an alternative client is there, especially if we want to get Bitcoin to go mobile, as an Android app, or simply because we don't want to put all eggs in one basket (right now, should a bug be discovered in the Mainline client the whole network can be taken down by a single attacker).
As for what is implemented:
- Client can connect to other clients
- A connection can be maintained (proper handshake, pinging, ...)
- All messages except getblocks and getdata have been implemented and work correctly
- The code can notify any number of listeners about incoming messages and messages can be sent back
- 70%+ code coverage with our unit tests!
What will soon follow:
- A simple driver to keep a pool of connections alive
- Switch to Non-blocking IO in order to avoid having threads idling waiting for incoming messages
- More unit tests
- Less ugliness, more coolness
What will not be implemented in the network part:
- Crypto stuff: I wanted to keep the network implementation as small as possible, and cool things can be done even without the crypto stuff (network crawling, relaying messages, ...) so I decided to move the crypto stuff out of this module and implement it as a listener that will plug into the network core.
- Block storage: again this is to keep things simple, and it would be cool to have multiple implementations of this (flat file/SQL storage, keep everything/keep only inputs for my transaction, ...)
Using itAs I said, it is a library and not a standalone program. In order to use it your program has to provide an implementation of the BitcoinEventListener interface which allows the library to notify your code about messages from the network, and also allow your program to send messages back. For an example about how to use it see BitcoinClientDriver which is the code that does the handshake and keeps the connection alive.
CodingAs I said, I'm hoping to get more people on board with this. The code is released under the Apache License, which should make it possible for you to reuse it. I tried to keep the number of dependencies as low as possible (come to think of it, the only dependency is SLF4J, which allows you to plug in any logging facility you want).
The code is on GitHub (
https://github.com/cdecker/BitDroid-Network), feel free to fork it, I'm only asking you to help me improve it by contributing changes back to the me (just send a pull request and I'll merge it in if it's a reasonable change).
I'm using Maven2 as a build tool, to get started check out the code and run
mvn package and it'll build.
Under src/test/resources we have 24 hours worth of network dump split into single messages, they are used in the unit tests, which I'm proud to say cover most of the code. Should you find a bug please create a unit test that illustrates the problem.
And now start flaming about the quality of the code