From 03a9c622fc221a0042c4de5363cc4c3cdcba0335 Mon Sep 17 00:00:00 2001 From: clabby Date: Sun, 19 Nov 2023 10:37:33 -0500 Subject: [PATCH] Bindgen --- Cargo.lock | 26 +++++++++++++++++++++++++- crates/db-utils/Cargo.toml | 3 +++ crates/db-utils/build.rs | 17 +++++++++++++++-- crates/db-utils/geth-bind/Makefile | 2 +- crates/db-utils/geth-bind/freezer.go | 13 ++++++++----- crates/db-utils/src/leveldb/mod.rs | 10 ++++------ crates/db-utils/src/lib.rs | 3 +++ 7 files changed, 59 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 98781ad..94307a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -350,6 +350,29 @@ dependencies = [ "which", ] +[[package]] +name = "bindgen" +version = "0.69.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ffcebc3849946a7170a05992aac39da343a90676ab392c51a4280981d6379c2" +dependencies = [ + "bitflags 2.4.1", + "cexpr", + "clang-sys", + "lazy_static", + "lazycell", + "log", + "peeking_take_while", + "prettyplease", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex", + "syn 2.0.32", + "which", +] + [[package]] name = "bit-set" version = "0.5.3" @@ -447,7 +470,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32700dc7904064bb64e857d38a1766607372928e2466ee5f02a869829b3297d7" dependencies = [ - "bindgen", + "bindgen 0.66.1", "blst", "cc", "glob", @@ -1448,6 +1471,7 @@ dependencies = [ "alloy-primitives", "alloy-rlp", "anyhow", + "bindgen 0.69.1", "leveldb", "reth-primitives", "tracing", diff --git a/crates/db-utils/Cargo.toml b/crates/db-utils/Cargo.toml index 4b31a1b..7d37de5 100644 --- a/crates/db-utils/Cargo.toml +++ b/crates/db-utils/Cargo.toml @@ -20,3 +20,6 @@ reth-primitives = { git = "https://github.com/clabby/reth", branch = "cl/op-lega # Misc anyhow = "1.0.70" tracing = "0.1.37" + +[build-dependencies] +bindgen = "0.69.1" diff --git a/crates/db-utils/build.rs b/crates/db-utils/build.rs index b83fab1..80e4d6c 100644 --- a/crates/db-utils/build.rs +++ b/crates/db-utils/build.rs @@ -1,3 +1,5 @@ +extern crate bindgen; + use std::env; use std::path::PathBuf; use std::process::Command; @@ -11,11 +13,22 @@ fn main() { .arg("-buildmode=c-archive") .arg("-o") .arg(out_path.join("libfreezer.a")) - .arg("./geth-bind/freezer.go"); + .arg("./freezer.go") + .current_dir("./geth-bind"); go_build.status().expect("Go build failed"); - println!("cargo:rerun-if-changed=./geth-bind/freezer.go"); + let bindings = bindgen::Builder::default() + .header(out_path.join("libfreezer.h").to_str().unwrap()) + .parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) + .generate() + .expect("Unable to generate bindings"); + + bindings + .write_to_file(out_path.join("freezer-bindings.rs")) + .expect("Couldn't write bindings!"); + + println!("cargo:rerun-if-changed=geth-bind/freezer.go"); println!( "cargo:rustc-link-search=native={}", out_path.to_str().unwrap() diff --git a/crates/db-utils/geth-bind/Makefile b/crates/db-utils/geth-bind/Makefile index 8717c23..d4dbfe6 100644 --- a/crates/db-utils/geth-bind/Makefile +++ b/crates/db-utils/geth-bind/Makefile @@ -1,2 +1,2 @@ all: - go build -buildmode=c-archive -o out/libfreezer.so + go build -buildmode=c-archive -o out/libfreezer.so ./freezer.go diff --git a/crates/db-utils/geth-bind/freezer.go b/crates/db-utils/geth-bind/freezer.go index 3f99fa3..be50142 100644 --- a/crates/db-utils/geth-bind/freezer.go +++ b/crates/db-utils/geth-bind/freezer.go @@ -4,15 +4,18 @@ package main #include */ import "C" -import "unsafe" +import ( + "fmt" + "unsafe" -// import ( -// "github.com/ethereum/go-ethereum/core/rawdb" -// ) + "github.com/ethereum/go-ethereum/common" +) //export FetchReceipts func FetchReceipts() *C.char { - return C.CString("hi from the freezer") + // TODO + hash := common.Hash{0xBE, 0xEF} + return C.CString(fmt.Sprintf("%s", hash.String())) } //export GoFree diff --git a/crates/db-utils/src/leveldb/mod.rs b/crates/db-utils/src/leveldb/mod.rs index ebb0813..31030ae 100644 --- a/crates/db-utils/src/leveldb/mod.rs +++ b/crates/db-utils/src/leveldb/mod.rs @@ -167,6 +167,8 @@ mod db_test { const BEDROCK_TRANSITION: u64 = 4_061_224; const FULL_PRUNE_DEPTH: u64 = 90_000; + include!(concat!(env!("OUT_DIR"), "/freezer-bindings.rs")); + fn testdata_reader() -> GethDBReader { let mut db_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); db_path.push("testdata/bedrock/geth/chaindata"); @@ -212,19 +214,15 @@ mod db_test { } } - extern "C" { - fn FetchReceipts() -> *const std::ffi::c_char; - fn GoFree(s: *const std::ffi::c_char); - } - #[test] + #[ignore] fn read_freezer() { unsafe { let cstr = std::ffi::CStr::from_ptr(FetchReceipts()); let s = String::from_utf8_lossy(cstr.to_bytes()).to_string(); dbg!(s); - GoFree(cstr.as_ptr()); + GoFree(cstr.as_ptr() as *mut std::ffi::c_char); } } } diff --git a/crates/db-utils/src/lib.rs b/crates/db-utils/src/lib.rs index 76ff140..61efd8b 100644 --- a/crates/db-utils/src/lib.rs +++ b/crates/db-utils/src/lib.rs @@ -1,3 +1,6 @@ #![doc = include_str!("../README.md")] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] pub mod leveldb;