Skip to content

Commit

Permalink
Bindgen
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Nov 19, 2023
1 parent a7a707e commit 03a9c62
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 15 deletions.
26 changes: 25 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/db-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
17 changes: 15 additions & 2 deletions crates/db-utils/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate bindgen;

use std::env;
use std::path::PathBuf;
use std::process::Command;
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion crates/db-utils/geth-bind/Makefile
Original file line number Diff line number Diff line change
@@ -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
13 changes: 8 additions & 5 deletions crates/db-utils/geth-bind/freezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ package main
#include <stdlib.h>
*/
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
Expand Down
10 changes: 4 additions & 6 deletions crates/db-utils/src/leveldb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
}
}
3 changes: 3 additions & 0 deletions crates/db-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 03a9c62

Please sign in to comment.