<p>It is extremely difficult to write a specification for a complex format that is 100% watertight and has no poorly defined corner cases. Further even if the specification itself is watertight it's extremely difficult to implement it without making any mistakes. I can't find a link right now but I remember a study posted recently about differences in validation behavior between different implementations of a common signature scheme.</p>
<p>This leads to a problem in consensus-based protocols like bitcoin, if there are multiple implementations then they might disagree about whether a given block is valid, either because someone accidentally ran into a corner case or because a malicious actor is submitting blocks deliberately crafted to run into corner cases.</p>
<p>If a minority implementation accepts a block that the implementation used by the majority of miners rejects, then little harm is done, the &quot;strongest chain&quot; rules mean that everyone follows the rules set by the majority implementation.</p>
<p>But if the minority implementation rejects a block and the majority implementation accepts it then there will be a fork. Users of the minority implementation will stop seeing new blocks from the main chain and (if the minority implementation has mining support) may start to form their own chain with conflicting history.</p>
<p>So using a new implementation for &quot;full validation&quot; bitcoin chain processing is risky, even if the implementer has gone through the whole history so-far and checked it validates with their new implementation, users of that implementation could find themselves cut off from the rest of the network at any time.</p>

CC BY-SA 4.0
2020-12-07T21:52:06.753Z
2020-12-07T22:06:49.563Z
2020-12-07T22:06:49.563Z
Peter Green
<p>There are many implementations of the Bitcoin protocol written in other languages, e.g., Bcoin in JavaScript, btcd in Go, bitcoin-s in Scala, etc.</p>
<p>Bitcoin Core is widely considered as the &quot;reference implementation&quot; and it is certainly the most widely used implementation on the network. Without a written specification, the authoritative resource, if consensus diverges between implementations, would generally be considered the Bitcoin Core codebase.</p>
<p>In terms of Rust, there is a Bitcoin library called <a href="https://github.com/rust-bitcoin/rust-bitcoin" rel="nofollow noreferrer">rust-bitcoin</a>, but as it says in the rust-bitcoin <a href="https://github.com/rust-bitcoin/rust-bitcoin/blob/master/README.md" rel="nofollow noreferrer">README</a>:</p>
<blockquote>
<p>This library must not be used for consensus code (i.e. fully validating blockchain data). It technically supports doing this, but doing so is very ill-advised because there are many deviations, known and unknown, between this library and the Bitcoin Core reference implementation. In a consensus based cryptocurrency such as Bitcoin it is critical that all parties are using the same rules to validate data, and this library is simply unable to implement the same rules as Core.</p>
</blockquote>
<blockquote>
<p>Given the complexity of both C++ and Rust, it is unlikely that this will ever be fixed, and there are no plans to do so. Of course, patches to fix specific consensus incompatibilities are welcome.</p>
</blockquote>
<p>There would be two possible ways a Rust implementation could &quot;supplant&quot; the C++ Bitcoin Core implementation (assuming supplanting is a healthy goal which is disputed).</p>
<p>One would be more and more users running a full node written in Rust, perhaps based on rust-bitcoin, until it became the most widely run implementation on the network. Arguably this would be a path to supplanting Bitcoin Core as the &quot;reference implementation.&quot; (Some would argue that Bitcoin Core is the reference implementation because it is based on Satoshi's original client and a new implementation should never be considered the &quot;reference implementation.&quot;)</p>
<p>The alternative would be Rust code is introduced into Bitcoin Core, gradually replacing the C++ code in Bitcoin Core. The latter <a href="https://github.com/bitcoin/bitcoin/issues/17090" rel="nofollow noreferrer">has been discussed</a> to a greater extent than the former although if this was to ever happen it would be a long term project.</p>
<p>There are arguments against introducing Rust code to the Bitcoin Core codebase and they were discussed in this Sydney <a href="https://diyhpl.us/wiki/transcripts/sydney-bitcoin-meetup/2020-05-19-socratic-seminar/" rel="nofollow noreferrer">Socratic Seminar</a> in May 2020. With permission I include this quote from Rusty Russell:</p>
<blockquote>
<p>I really like Rust. It is going from strength to strength and it has got definite merit. But you have got an existing expertise who have tooled up in a certain language. Some of that is self selected but whether that is true or not they exist. Now you are asking people to learn two languages. If you want to broadly read the codebase you have to know two languages and that is a significant burden. Perhaps in five years time when Rust is perhaps an obvious successor to C++ then that intersection… Arguing that we will get more contributors for Rust is probably not true. You might get some different contributors but C++ is pretty well known now. There is the sexy argument. “Work on this, it is in Rust” Unfortunately that will become less compelling too over time. As Rust gets bigger and more projects are using it and there are other cool things you could be doing with Rust anyway. It would be better to do stuff in Rust as far as security. I am cautiously hopeful that the size of bitcoind is going down not up. Stuff should be farmed out to the wallet etc with better RPCs and better library calls so it is not this monolithic project anymore. The argument that we are going to be introducing all these security holes if we don’t use Rust is probably weaker. As you said the last thing that will change is the consensus code. That said I like projects that stick to two languages. It is really common to have projects where you have to know multiple languages to deal with them. Working around the edges and reworking some of the tooling in Rust, building up those skills slowly is definitely the way forward. It will take longer than you want and you hope but it is a pretty clear direction to head in. But head in in terms of a decade we will be talking about finally rewriting the core in Rust. It is not going to happen overnight.</p>
</blockquote>
<p>As Prayank says in an above comment, Bitcoin Core functional tests (not unit tests; they are in C++) are written in Python. So you already need to know C++ and Python to contribute to and review the entire Core codebase. If Rust was introduced that would be introducing a third language.</p>

CC BY-SA 4.0
2021-06-23T21:58:11.920Z
// , I always thought that a protocol could not be considered robust without supporting multiple implementations. But this does make a good case for having a go-to reference implementation.
Nathan Basanese
CC BY-SA 4.0
2020-12-07T11:13:25.227Z
2020-12-08T10:37:04.950Z
2020-12-08T10:37:04.950Z
Michael Folkson
<p>Can Bitcoin Core be rewritten in another language, let's say <a href="https://en.wikipedia.org/wiki/Rust_(programming_language)" rel="nofollow noreferrer">Rust</a>, and could such an implementation supplant the current one in <a href="https://en.wikipedia.org/wiki/C%2B%2B" rel="nofollow noreferrer">C++</a> without the users of Bitcoin experiencing some sort of reset/lost of funds?</p>
<p>Languages come and go and I was thinking that decades ahead the world may be in the similar situation with respect to C++ as it is today with respect to <a href="https://en.wikipedia.org/wiki/Fortran" rel="nofollow noreferrer">Fortran</a>.</p>

<bitcoin-core><c++><implementation>
Is it doable to supplant the current Bitcoin Core implementation with another rewritten in a different language?
John Smith
# Is it doable to supplant the current Bitcoin Core implementation with another rewritten in a different language?

(Asked by: username_0 on 2020-12-07)

Can Bitcoin Core be rewritten in another language, let's say [Rust](https://en.wikipedia.org/wiki/Rust_(programming_language)), and could such an implementation supplant the current one in [C++](https://en.wikipedia.org/wiki/C%2B%2B) without the users of Bitcoin experiencing some sort of reset/lost of funds?

Languages come and go and I was thinking that decades ahead the world may be in the similar situation with respect to C++ as it is today with respect to [Fortran](https://en.wikipedia.org/wiki/Fortran).

**Tags:** bitcoin-core, c++, implementation


---

## 2 answers

### Answer by username_2 on 2020-12-07. Score: 9 | Accepted answer

There are many implementations of the Bitcoin protocol written in other languages, e.g., Bcoin in JavaScript, btcd in Go, bitcoin-s in Scala, etc.

Bitcoin Core is widely considered as the "reference implementation" and it is certainly the most widely used implementation on the network. Without a written specification, the authoritative resource, if consensus diverges between implementations, would generally be considered the Bitcoin Core codebase.

In terms of Rust, there is a Bitcoin library called [rust-bitcoin](https://github.com/rust-bitcoin/rust-bitcoin), but as it says in the rust-bitcoin [README](https://github.com/rust-bitcoin/rust-bitcoin/blob/master/README.md):

> This library must not be used for consensus code (i.e. fully validating blockchain data). It technically supports doing this, but doing so is very ill-advised because there are many deviations, known and unknown, between this library and the Bitcoin Core reference implementation. In a consensus based cryptocurrency such as Bitcoin it is critical that all parties are using the same rules to validate data, and this library is simply unable to implement the same rules as Core.

> Given the complexity of both C++ and Rust, it is unlikely that this will ever be fixed, and there are no plans to do so. Of course, patches to fix specific consensus incompatibilities are welcome.

There would be two possible ways a Rust implementation could "supplant" the C++ Bitcoin Core implementation (assuming supplanting is a healthy goal which is disputed).

One would be more and more users running a full node written in Rust, perhaps based on rust-bitcoin, until it became the most widely run implementation on the network. Arguably this would be a path to supplanting Bitcoin Core as the "reference implementation." (Some would argue that Bitcoin Core is the reference implementation because it is based on Satoshi's original client and a new implementation should never be considered the "reference implementation.")

The alternative would be Rust code is introduced into Bitcoin Core, gradually replacing the C++ code in Bitcoin Core. The latter [has been discussed](https://github.com/bitcoin/bitcoin/issues/17090) to a greater extent than the former although if this was to ever happen it would be a long term project.

There are arguments against introducing Rust code to the Bitcoin Core codebase and they were discussed in this Sydney [Socratic Seminar](https://diyhpl.us/wiki/transcripts/sydney-bitcoin-meetup/2020-05-19-socratic-seminar/) in May 2020. With permission I include this quote from Rusty Russell:

> I really like Rust. It is going from strength to strength and it has got definite merit. But you have got an existing expertise who have tooled up in a certain language. Some of that is self selected but whether that is true or not they exist. Now you are asking people to learn two languages. If you want to broadly read the codebase you have to know two languages and that is a significant burden. Perhaps in five years time when Rust is perhaps an obvious successor to C++ then that intersection… Arguing that we will get more contributors for Rust is probably not true. You might get some different contributors but C++ is pretty well known now. There is the sexy argument. “Work on this, it is in Rust” Unfortunately that will become less compelling too over time. As Rust gets bigger and more projects are using it and there are other cool things you could be doing with Rust anyway. It would be better to do stuff in Rust as far as security. I am cautiously hopeful that the size of bitcoind is going down not up. Stuff should be farmed out to the wallet etc with better RPCs and better library calls so it is not this monolithic project anymore. The argument that we are going to be introducing all these security holes if we don’t use Rust is probably weaker. As you said the last thing that will change is the consensus code. That said I like projects that stick to two languages. It is really common to have projects where you have to know multiple languages to deal with them. Working around the edges and reworking some of the tooling in Rust, building up those skills slowly is definitely the way forward. It will take longer than you want and you hope but it is a pretty clear direction to head in. But head in in terms of a decade we will be talking about finally rewriting the core in Rust. It is not going to happen overnight.

As Prayank says in an above comment, Bitcoin Core functional tests (not unit tests; they are in C++) are written in Python. So you already need to know C++ and Python to contribute to and review the entire Core codebase. If Rust was introduced that would be introducing a third language.

> **Comments:**
> **username_3**: // , I always thought that a protocol could not be considered robust without supporting multiple implementations. But this does make a good case for having a go-to reference implementation.  

---

### Answer by username_1 on 2020-12-07. Score: 5

It is extremely difficult to write a specification for a complex format that is 100% watertight and has no poorly defined corner cases. Further even if the specification itself is watertight it's extremely difficult to implement it without making any mistakes. I can't find a link right now but I remember a study posted recently about differences in validation behavior between different implementations of a common signature scheme.

This leads to a problem in consensus-based protocols like bitcoin, if there are multiple implementations then they might disagree about whether a given block is valid, either because someone accidentally ran into a corner case or because a malicious actor is submitting blocks deliberately crafted to run into corner cases.

If a minority implementation accepts a block that the implementation used by the majority of miners rejects, then little harm is done, the "strongest chain" rules mean that everyone follows the rules set by the majority implementation.

But if the minority implementation rejects a block and the majority implementation accepts it then there will be a fork. Users of the minority implementation will stop seeing new blocks from the main chain and (if the minority implementation has mining support) may start to form their own chain with conflicting history.

So using a new implementation for "full validation" bitcoin chain processing is risky, even if the implementer has gone through the whole history so-far and checked it validates with their new implementation, users of that implementation could find themselves cut off from the rest of the network at any time.


---

