“Satoshi left us with a monolithic blob of a codebase.”
That was bitcoin developer Cory Fields’ way of saying that, despite the innovation it’s often credited with, Bitcoin Core's code is kind of a mess behind the scenes.
It’s a problem Fields and others have been grappling with since at least 2015, when hepresented his plans to move around one piece of the code: the peer-to-peer layer.
The problem developers often encounter is that the components are not all neatly separated. So, if developers change something that’s only supposed to impact one portion of bitcoin and how it functions, Fields said, it could unintentionally impact another, say, validation, mempool, the wallet, or p2p. For one, this makes it more difficult for newer bitcoin contributors to learn the ropes.
So, in projects such as Fields' and libconsensus (an ongoing project to allow alternative implementations to remain in consensus with the bitcoin network), the goal is to separate the code into more distinct chunks in an attempt to make it easier, and less necessary, for developers to understand how the pieces fit together.
Fields, whose work is funded by MIT’s Digital Currency Initiative, has been working on breaking out the peer-to-peer codebase in particular.
Much of his work is making a first appearance in Bitcoin Core’s latest software release .
Fields told CoinDesk:
"As far as I know, this is the first large-scale overhaul of the code."
He mentioned, however, that over time many developers, such as Jonas Schnelli and Matt Corallo, have contributed to the layer, and importantly many more have had a hand in reviewing and testing the changes.
The peer-to-peer layer deals with the connections between the thousands of bitcoin nodes on the network (maybe more than 50,000 , though, exact counts are difficult to come by), which are constantly 'talking' to each other, passing information as users send bitcoins, or miners create new blocks.
One tricky part is that the bitcoin network is valued at roughly $19bn, meaning changes to the underlying code have to be undertaken extremely carefully.
For that and other reasons, refactoring hasn’t been an easy process. Not even for Fields, who is one of the more active contributors to Bitcoin Core. ("I spend nearly all of my waking hours on bitcoin," he said.)
First, he tried to a separate layer from scratch. That effort "failed miserably".
His later approach of working to improve the existing codebase arguably worked better than expected though, leading to a “Satoshi-era” bug discovery.
“As I started to break things up, I noticed something odd about Core's bandwidth usage during initial block download,” Fields said.
The problem was that, when a node would download blocks from another node, the speed at which blocks were passed along would be sometimes fast, other times slow – and at unexpected levels.
Digging further, he and other developers determined that nodes would stubbornly refuse to send data and process data at the same time (though this is an oversimplification). The node would just do one process at a time.
The fix the team finally arrived at saw its debut in the recent Bitcoin Core code release, changing how quickly nodes can pass information to one another.
“In particular, the p2p layer gets a huge speedup,” Fields said.
He ran some tests to compare the last bitcoin version (0.13.2) with the new version with the improvements (0.14.0), looking at nodes that were deployed to the cloud on Amazon EC2 instances with two CPUs and 4GB of RAM.
The tests showed a doubling in speed – at least, when comparing the performance of sending data between two v0.13.2 nodes, versus between two v0.14.0 nodes.
The caveat here is that it might result in a bit less than a two-times performance improvement on the real network, according to Fields.
Now, the peer-to-peer layer is mostly separate from the rest.
It's worth repeating that, the above performance improvements aside, most of the changes aren’t ones that users will notice, but are more of a boon for developers.
That said, the hope is that they could have longer-term impact.
"I'm hoping that allows us to improve p2p at a faster pace now," Fields said.
For example, it would have been more difficult to make the changes included in the latest version of Core if Fields had not already worked to isolate the code.
”[T]here was a relatively simple change that could be made in time for 0.14 that would've been much more complicated before.”
Speed dial image via Shutterstock. Cory Fields image via CoinDesk