Bitcoin full node and query server

May 12, 2021

View in context in the libbitcoin Slack #general mirror.

Revive16:42
hey guys, looking at using cxx https://cxx.rs an rust tool to generate bindings for libbitcoin
(edited)
CXX
CXX — safe interop between Rust and C++
18 replies · evoskuil, Revive · last Jun 21, 2021
evoskuil18:28
Hopefully it’s more capable than swig.
Revive18:40
I'm working through how to use it properly. it has excellent feedback. can do c++ -> rust and rust -> c++. Declared structs, fn types etc are kept on the rust-side of things, so just by including the libbitcoin headers. I am still sorting out the best way to do things however
evoskuil18:58
Is it fully generated, and can it handle things that swig cannot, like templates.
Revive15:54
Sorry for the late reply. Just got something working yesterday lol
Revive15:55
I'm not sure what you mean by fully generated, but you have to declare (signatures they call them in rust) with matching functions, structs, etc. IE it doesnt run though the entirety of your library.
Revive15:57
They can be Opaque (pointers only), shared structs both C++ and rust have access to, and functions.
Revive15:58
so far I know that it has problems reading aliases from C++. I would like to find out if it translates templates well now. can you give me an example simple template fn?
Revive16:01
right now i'm building using cargo, and only including libbitcoin headers, and the dynamically linked libraries. but that was enough to get pseudo_random used within rust
👍 1
evoskuil10:32
How is it "generated" bindings if you have to declare them manually?
evoskuil10:33
Manual binding generation has been attempted several times. It's extremely hard to keep them in sync, and they become unreliable and unused as a result.
Revive17:38
You declare a high-level description of the language boundaries eg in "extern c++" or "extern rust" within the rust, then CXX uses static analysis of the types and function signatures to protect both Rust's and C++'s invariants. Then it "uses a pair of code generators to implement the boundary efficiently on both sides together with any necessary static assertions for later in the build process to verify correctness." As far as I have seen. python-libbitcoin and libbitcoin-c had to both be maintained as you pointed out. At least this way, the low-level FFI in C can be automated
Revive17:40
but this is for my current use case. I want to use libbitcoin within rust. I defer to you for exactly these opinions is something long-term is viable in libbitcoin itself
evoskuil04:18
Actually sounds pretty good, can you point me to your implementation?
Revive15:02
@evoskuil an update to this. been evaluating cxx and autocxx, which is a higher level generator for cxx that makes use of bindgen to generate the C++ bindings side automatically. CXX, by itself, needs definitions of boundaries and similar types in both the rust side and C++ etc as described above. For example, on the C++ side I created std::unique_ptr for libbitcoin::system::wallet::hd_private https://github.com/darkrenaissance/darkfi/blob/libbitcoin-gen/gen/libbitcoin/src/helpers.hpp#L8
Revive15:03
Here's what the implementation looks like for creating a new seed in rust, using libbitcoin + autocxx: https://github.com/darkrenaissance/darkfi/blob/libbitcoin-gen/gen/libbitcoin/src/main.rs#L43
Revive15:05
the autocxx magic is line 14-16 with the macro generate!("libbitcoin::system::pseudo_random_fill")
Revive15:26
that file generates bindings that looks like this: what autocxx generates (uses cxx library for you) - https://paste.dark.fi/~janus/c2d5a52cc52d96cd43b98d6c6b5539461dcd80bf#rust-autocxxgen-demo-L64 what cxx generates from this - https://paste.dark.fi/~janus/cda4b2ad0a99c701726bac3fdc5fb2e7ac2d936a#cxx-gen-L84 autocxx + bindgen generated c++ side https://paste.dark.fi/~janus/ad72b45c573cf9b27cd6d04fd718789cc4837dd9
(edited)