Chore: remove bindgen and replace with hand-rolled FFI bindings#163
Chore: remove bindgen and replace with hand-rolled FFI bindings#163alexanderwiederin wants to merge 4 commits into
bindgen and replace with hand-rolled FFI bindings#163Conversation
|
|
bindgen and replace with hand-rolled FFI bindings
Yes! |
Yeah, this pr will help me a lot and ill be glad to help you guys to ship it. |
|
Good to hear! I think we are practically good to go here. One thing to note is that opaque types and grouped definitions are ordered alphabetically rather than following the C API order, as I felt it would scale easiest. The Correction: On second thought, I don't think the extern "C" declarations need to be in the order of the C API either. |
e49a09d to
0917624
Compare
| _unused: [u8; 0], | ||
| } | ||
| #[repr(C)] | ||
| pub struct btck_ChainstateManagerOptions { |
There was a problem hiding this comment.
I wonder if we should also derive Debug, Copy, and Clone for these types. Do you think there could be a downside?
There was a problem hiding this comment.
I removed them to keep the -sys crate minimal. openssl-sys takes the same approach, while secp256k1-sys does derive them - so there's no universal convention. For me it was mostly about keeping the surface area small.
Do you have a preference?
sedited
left a comment
There was a problem hiding this comment.
Looks good. The only drawback I see here is that we need to be careful with copying over the correct constant values.
5d80502 to
9975909
Compare
jaoleal
left a comment
There was a problem hiding this comment.
ACK 9975909
Only nits and docs suggestions, perhaps the second commit thats solely to move constants could be included in CHANGELOG.md ?
Also, the README.md under libbitcoinkernel-sys is pretty simple, we could include some sayings about the hand written bindings
| // Primitive type aliases - alphabetical order | ||
|
|
||
| pub type btck_BlockCheckFlags = u32; | ||
| pub type btck_BlockValidationResult = u32; |
There was a problem hiding this comment.
Reading this and seeing that auditability is one of the purposes for this, perhaps we could work out a little more docs here ? A good example that I saw was secp256k1 bindings.
For this case i suggest some simple docs such as
| pub type btck_BlockValidationResult = u32; | |
| /// Represents a granular reason for block validation. | |
| pub type btck_BlockValidationResult = u32; |
There was a problem hiding this comment.
Good suggestion, but I'd like to keep the scope focused on the bindings as much as possible. I'll add a top-level doc comment and update the README pointing to the C header for now.
| pub type btck_ValidationMode = u8; | ||
| pub type btck_Warning = u8; | ||
|
|
||
| // btck_BlockValidationResult |
There was a problem hiding this comment.
The constants looks fine, cant we maintain the constants.rs ? That would leave this lib.rs only for declarations right ?
There was a problem hiding this comment.
I would prefer to keep everything in lib.rs and mirror the structure of the C header. This makes auditing easier, especially when updating to newer versions. The file is organised with the section comments to make navigation straightforward. Having said that, if the API grows significantly we can revisit splitting it up.
|
Question: will this trigger a new release ? |
|
Yes :) |
| assert!(core::mem::size_of::<btck_NotificationInterfaceCallbacks>() == 72); | ||
| assert!(core::mem::align_of::<btck_NotificationInterfaceCallbacks>() == 8); | ||
| assert!(core::mem::size_of::<btck_ValidationInterfaceCallbacks>() == 48); | ||
| assert!(core::mem::align_of::<btck_ValidationInterfaceCallbacks>() == 8); |
There was a problem hiding this comment.
| assert!(core::mem::size_of::<btck_NotificationInterfaceCallbacks>() == 72); | |
| assert!(core::mem::align_of::<btck_NotificationInterfaceCallbacks>() == 8); | |
| assert!(core::mem::size_of::<btck_ValidationInterfaceCallbacks>() == 48); | |
| assert!(core::mem::align_of::<btck_ValidationInterfaceCallbacks>() == 8); | |
| assert!( | |
| core::mem::size_of::<btck_NotificationInterfaceCallbacks>() | |
| == 9 * core::mem::size_of::<*const ()>() | |
| ); | |
| assert!( | |
| core::mem::align_of::<btck_NotificationInterfaceCallbacks>() | |
| == core::mem::align_of::<*const ()>() | |
| ); | |
| assert!( | |
| core::mem::size_of::<btck_ValidationInterfaceCallbacks>() | |
| == 6 * core::mem::size_of::<*const ()>() | |
| ); | |
| assert!( | |
| core::mem::align_of::<btck_ValidationInterfaceCallbacks>() | |
| == core::mem::align_of::<*const ()>() | |
| ); |
After rebasing #158 on top of this to see if anything breaks, i found a problem regarding pointers size on armv7-linux-androideabi since pointers are 4 bytes on 32-bit targets.
the above suggestion fixed the problem and i was able to successfully build this lib to armv7-linux-androideabi. It multiplies the size of pointers at compile time but im not totally sure if its the best fix for this case.
There was a problem hiding this comment.
Good catch! The hardcoded values were assuming 64-but pointer sizes. Your suggestion make the assertions valid on both 32-bit and 64-bit targets.
Applied - thanks!
There was a problem hiding this comment.
Added #175 to formally support 32-bit targets.
ff4de75 to
81bd88d
Compare
Done.
Done. Thanks for the feedback! |
… FFI bindings Replace the bindgen-generated bindings.rs with a hand-written lib.rs. The bindgen build dependency is removed. The hand-rolled bindings are verified against bindgen output and match the types. Layout assertions enforce ABI correctness for three structs passed by value across the FFI boundary at compile time. Unnecessary derives (Debug, Copy, Clone) are removed from all types. Cargo-minimal.lock and Cargo-recent.lock have been updated to remove bindgen dependency. libbitcoinkernel-sys/CHANGELOG.md updated to reflect bindgen removal.
…om sys crate Delete the constants.rs module and update all call sites to import btck_* constants directly from libbitcoinkernel-sys. The BTCK_* aliases are no longer needed.
Add #[allow(non_upper_case_globals)] to the From impl blocks that match on btck_* constants. These names intentionally mirror the C header naming convention.
|
ACK ccb3542 |
Closes: #157
Summary
Replaces the bindgen-generated FFI bindings with a hand-written
lib.rsinlibbitcoinkernel-sys. As a follow-up theconstants.rsin the wrapper crate is removed and call sites are updated to use thebtck_*names from the sys crate.Motivation
Changes
libbitcoinkernel-sys/src/lib.rs: Hand-written FFI bindings replacing the bindgen-generatedbindings.rs.std::os::rawtype aliases replaced withcore::ffiequivalents.libbitcoinkernel-sys/build.rs: Bindgen block removed.libbitcoinkernel-sys/Cargo.toml:bindgenremoved from build-dependencies.src/ffi/constants.rs: Deleted. Constants now imported fromlibbitcoinkernel-sysusingbtck_*names.Cargo-minimal.lock,Cargo-recent.lock: Updated to reflect removed bindgen transitive dependencies.Question to Reviewers: Is it the right moment to do this?