Subject: Re: [Bitcoin-development] More precise type information in API reference

From: Dario Teixeira <[email-masked]>
Date: Tue, 17 Feb 2015 13:33:28 +0000

Hi again,

My excuses: the formatting of the message seems to have been eaten
by a Gremlin.  Here's a second attempt:

=================================================================

Dear Bitcoin devs,

I am the author of OCaml-bitcoin [1], a library offering an OCaml
interface to the official Bitcoin client API.  For those who may
be unfamiliar with it, OCaml is one of those functional programming
languages with a very rich and expressive type system [2].  Given its
emphasis on safety, its industrial users are disproportionally
found in the aerospace and financial sectors.

Now, OCaml programmers care a lot about types, because experience
has taught them that deep down most programming errors are just
type errors.  From this stems my request: please consider defining
more precisely the type information associated with each API call
in the JSON-RPC reference [3].

To give you a better idea of what I'm talking about, please take a
look at the API offered by OCaml-bitcoin [4], and the associated type
definitions [5] (note that these have not been updated for Bitcoin
Core 0.10 yet).  I've created the type definitions from information
gathered from the Bitcoin wiki and from looking at the Bitcoin
Core source-code.  I wouldn't be surprised if it contains errors,
because neither the source-code nor the wiki is very precise about
the actual types being used.  As an example, consider type hexspk_t
("hex representation of script public key").  Is this really the
same type used in both signrawtransaction and createmultisig?

Improving this situation would pose a minimal burden on bitcoin
devs: all that would be required is defining the precise set of
types used in the RPC API, and annotating the RPC calls either
in the source-code itself or in the API reference documentation.
It would make writing bindings such as mine far easier and less error
prone, and it would have the added advantage of better documenting
the Bitcoin Core source-code itself.

Also, note that it is not necessary to extend this request to the
deep data structures returned by some API calls.  Consider for
instance the gettransaction function of the OCaml-bitcoin API:
it returns the raw JSON object without any attempt to process it.
This is because that's a fairly niche facility, and the bindings
would balloon in size if I were to process every single large return
object.  Instead, the bindings take the more pragmatic stance of
only processing the parameters and return results where a strong
type discipline is imperative.

When I raised this issue on IRC a number of questions were posed.
What follows is my attempt to answer them:

   Q: What does it matter, if JSON only has a tiny set of types?

   A: JSON being the serialisation format is irrelevant.  The client
      bindings know that even if a public ECDSA key is serialised as
      a string, it does not stop being a public ECDSA key, and should
      only be used where a public ECDSA key is expected.

   Q: What does it matter if the types are not even distinguished in
      the C++ source of Bitcoin Core?

   A: That is unfortunate, because it opens the door to bugs caused by
      type errors.  Moreover, even if the C++ source is "stringly-typed"
      and does not enforce a strong type discipline, that does not mean
      that the types are not there.  Even if a public and private key
      are both represented as strings, can you use one where the other
      is expected?  If not, then they actually have different types!

   Q: Isn't this a maintenance nightmare, given the changes to Bitcoin
      core?

   A: Actually, the most burdensome part is what motivated this message:
      keeping track of the types used.  If the Bitcoin API reference were
      more precise, keeping the bindings up-to-date would be trivial and
      even mechanical, because the API is now fairly stable.

Thank you very much for your attention, and for all the work you
guys put into Bitcoin development.  It is much appreciated and not
acknowledged often enough!

Best regards,
Dario Teixeira

[1] https://github.com/darioteixeira/ocaml-bitcoin
[2] http://ocaml.org/learn/description.html
[3] https://bitcoin.org/en/developer-reference#bitcoin-core-apis
[4] http://ocaml-bitcoin.forge.ocamlcore.org/apidoc/Bitcoin.ENGINE.html
[5] http://ocaml-bitcoin.forge.ocamlcore.org/apidoc/Bitcoin.html