This issue is a proposal to add a CBOR RPC interface to bitcoin-core. The interface would be in addition to the current JSON-RPC, not a replacement.
The main goal of the proposal is to offer an RPC interface that is more efficient than the current JSON-RPC. Remote clients with constraints on data usage will see the biggest advantage.
CBOR seems like the best choice for a minimal RPC implementation, since it has wide support in industry, and is the de-facto standard for IoT communication protocols like CoAP. Meaning, there is likely to be a large community outside Bitcoin to get support/developers.
Advantages
- RFC standard specification: RFC 8949
- CBOR uses binary representation
- no need for Base64 or other encoding
- well-defined data types with minimal encoding overhead
- Small amount of types
- Several existing free open-source libraries to fork
- Many existing implementations are small (~400-750 LoC)
- Decreased data transmission for remote clients
Disadvantages
- Added code to the core implementation
- larger attack surface
- maintenance costs
- Added complexity
- Unclear data-size savings versus compressed JSON/Base64
Alternative approaches
External CBOR proxy
One possible alternative, suggested by @cfields, is to write a proxy translating CBOR to JSON.
The proxy could be stand-alone from bitcoin-core, thus removing the disadvantage of added code.
Also, the proxy could be written in a memory-safe language like Rust, decreasing the attack surface.
There would also be disadvantages to a proxy implementation:
- small overhead of translating CBOR to JSON, and passing to the original JSON-RPC
- installation of an additional piece of software
Internal CBOR proxy
Another alternative is to add CBOR support to Univalue, and implement the proxy in bitcoin-core.
This would have the advantage of direct access to RPC internals, and potentially reduce overall code size.
The proxy would listen on a separate port, translate the incoming CBOR to JSON, and pass the JSON to existing RPC interfaces.
Free open-source implementations
Here is a list of some of the better candidates for a bitcoin-core CBOR fork:
- cb0r: https://github.com/quartzjer/cb0r
- zero-allocation C implementation
- cppbor: https://github.com/rantydave/cppbor
- C++17 implementation based on
std::variant
- C++17 implementation based on
- cbor11: https://github.com/jakobvarmose/cbor11
- C++11 implementation
- tinycbor: https://github.com/intel/tinycbor
- Intel implementation, includes CBOR-JSON translation
- ironically, largest LoC count of the candidates
- cbor-lite: https://bitbucket.org/isode/cbor-lite
- C++14 implementation, header-only
- used in bc-ur
Comparing to JSON-RPC
@laanwj raised the point that compressed JSON may provide similar savings to CBOR. To test whether the savings from CBOR provides significant size reduction, I will implement a small number of RPC calls in CBOR, and compare the uncompressed and compressed sizes against the current JSON encoding.