diff --git a/Cargo.lock b/Cargo.lock index 6e1c845508..d905111ac3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3364,6 +3364,16 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "rustlib" +version = "0.1.0" +dependencies = [ + "env_logger", + "glob", + "gobuild", + "log", +] + [[package]] name = "rustls" version = "0.21.2" diff --git a/Cargo.toml b/Cargo.toml index c8155a1bfd..df85fda786 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,8 @@ members = [ "eth-types", "external-tracer", "mock", - "testool" + "testool", + "mpt-witness-generator/rustlib" ] [patch.crates-io] diff --git a/mpt-witness-generator/rust_call/.cargo/config b/mpt-witness-generator/rust_call/.cargo/config deleted file mode 100644 index e90e761d30..0000000000 --- a/mpt-witness-generator/rust_call/.cargo/config +++ /dev/null @@ -1,2 +0,0 @@ -[build] -rustflags = ["-C", "link-args=-framework CoreFoundation -framework Security"] diff --git a/mpt-witness-generator/rust_call/Cargo.lock b/mpt-witness-generator/rust_call/Cargo.lock deleted file mode 100644 index 2f4d2570f8..0000000000 --- a/mpt-witness-generator/rust_call/Cargo.lock +++ /dev/null @@ -1,89 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "itoa" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" - -[[package]] -name = "proc-macro2" -version = "1.0.36" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" -dependencies = [ - "unicode-xid", -] - -[[package]] -name = "quote" -version = "1.0.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "rust_call" -version = "0.1.0" -dependencies = [ - "serde", - "serde_json", -] - -[[package]] -name = "ryu" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" - -[[package]] -name = "serde" -version = "1.0.136" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.136" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.79" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95" -dependencies = [ - "itoa", - "ryu", - "serde", -] - -[[package]] -name = "syn" -version = "1.0.86" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" -dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", -] - -[[package]] -name = "unicode-xid" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" diff --git a/mpt-witness-generator/rust_call/build.rs b/mpt-witness-generator/rust_call/build.rs deleted file mode 100644 index 978a982142..0000000000 --- a/mpt-witness-generator/rust_call/build.rs +++ /dev/null @@ -1,8 +0,0 @@ -fn main() { - let path = "./build"; - let lib = "mpt"; - - println!("cargo:rustc-link-search=native={}", path); - println!("cargo:rustc-link-lib=static={}", lib); -} - diff --git a/mpt-witness-generator/rust_call/build/libmpt.a b/mpt-witness-generator/rust_call/build/libmpt.a deleted file mode 100644 index db38863154..0000000000 Binary files a/mpt-witness-generator/rust_call/build/libmpt.a and /dev/null differ diff --git a/mpt-witness-generator/rust_call/build/libmpt.h b/mpt-witness-generator/rust_call/build/libmpt.h deleted file mode 100644 index 5978e80bbb..0000000000 --- a/mpt-witness-generator/rust_call/build/libmpt.h +++ /dev/null @@ -1,81 +0,0 @@ -/* Code generated by cmd/cgo; DO NOT EDIT. */ - -/* package command-line-arguments */ - - -#line 1 "cgo-builtin-export-prolog" - -#include - -#ifndef GO_CGO_EXPORT_PROLOGUE_H -#define GO_CGO_EXPORT_PROLOGUE_H - -#ifndef GO_CGO_GOSTRING_TYPEDEF -typedef struct { const char *p; ptrdiff_t n; } _GoString_; -#endif - -#endif - -/* Start of preamble from import "C" comments. */ - - - - -/* End of preamble from import "C" comments. */ - - -/* Start of boilerplate cgo prologue. */ -#line 1 "cgo-gcc-export-header-prolog" - -#ifndef GO_CGO_PROLOGUE_H -#define GO_CGO_PROLOGUE_H - -typedef signed char GoInt8; -typedef unsigned char GoUint8; -typedef short GoInt16; -typedef unsigned short GoUint16; -typedef int GoInt32; -typedef unsigned int GoUint32; -typedef long long GoInt64; -typedef unsigned long long GoUint64; -typedef GoInt64 GoInt; -typedef GoUint64 GoUint; -typedef size_t GoUintptr; -typedef float GoFloat32; -typedef double GoFloat64; -#ifdef _MSC_VER -#include -typedef _Fcomplex GoComplex64; -typedef _Dcomplex GoComplex128; -#else -typedef float _Complex GoComplex64; -typedef double _Complex GoComplex128; -#endif - -/* - static assertion to make sure the file is being used on architecture - at least with matching size of GoInt. -*/ -typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1]; - -#ifndef GO_CGO_GOSTRING_TYPEDEF -typedef _GoString_ GoString; -#endif -typedef void *GoMap; -typedef void *GoChan; -typedef struct { void *t; void *v; } GoInterface; -typedef struct { void *data; GoInt len; GoInt cap; } GoSlice; - -#endif - -/* End of boilerplate cgo prologue. */ - -#ifdef __cplusplus -extern "C" { -#endif - -extern char* GetWitness(char* proofConf); - -#ifdef __cplusplus -} -#endif diff --git a/mpt-witness-generator/rust_call/src/main.rs b/mpt-witness-generator/rust_call/src/main.rs deleted file mode 100644 index 7144e39003..0000000000 --- a/mpt-witness-generator/rust_call/src/main.rs +++ /dev/null @@ -1,30 +0,0 @@ -// use serde_json::{json, Value}; -use std::ffi::{CStr, CString}; -use std::os::raw::c_char; -use std::fs::File; -use std::io::Write; - -extern "C" { - fn GetWitness(str: *const c_char) -> *const c_char; -} - -fn main() { - let data = r#" - { - "NodeUrl": "https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161", - "BlockNum": 14359865, - "Addr": "0x4E5B2e1dc63F6b91cb6Cd759936495434C7e972F", - "Keys": ["0x12", "0x21"], - "Values": ["0x1123e2", "0xa21"] - }"#; - - let c_config = CString::new(data).expect("invalid config"); - - let result = unsafe { GetWitness(c_config.as_ptr()) }; - let c_str = unsafe { CStr::from_ptr(result) }; - let string = c_str.to_str().expect("Error translating from library"); - println!("{:?}", string); - - let mut f = File::create("proof.json").expect("Unable to create file"); - f.write_all(string.as_bytes()).expect("Unable to write data"); -} diff --git a/mpt-witness-generator/rustlib/Cargo.lock b/mpt-witness-generator/rustlib/Cargo.lock new file mode 100644 index 0000000000..4d73989235 --- /dev/null +++ b/mpt-witness-generator/rustlib/Cargo.lock @@ -0,0 +1,165 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c378d78423fdad8089616f827526ee33c19f2fddbd5de1629152c9593ba4783" +dependencies = [ + "memchr", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "cc" +version = "1.0.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +dependencies = [ + "libc", +] + +[[package]] +name = "env_logger" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a12e6657c4c97ebab115a42dcee77225f7f482cdd841cf7088c657a42e9e00e7" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + +[[package]] +name = "gobuild" +version = "0.1.0-alpha.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e156a4ddbf3deb5e8116946c111413bd9a5679bdc1536c78a60618a7a9ac9e" +dependencies = [ + "cc", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "libc" +version = "0.2.147" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" + +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + +[[package]] +name = "memchr" +version = "2.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" + +[[package]] +name = "regex" +version = "1.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" + +[[package]] +name = "rustlib" +version = "0.1.0" +dependencies = [ + "env_logger", + "gobuild", + "log", +] + +[[package]] +name = "termcolor" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/mpt-witness-generator/rust_call/Cargo.toml b/mpt-witness-generator/rustlib/Cargo.toml similarity index 56% rename from mpt-witness-generator/rust_call/Cargo.toml rename to mpt-witness-generator/rustlib/Cargo.toml index 5197fe66aa..460c767558 100644 --- a/mpt-witness-generator/rust_call/Cargo.toml +++ b/mpt-witness-generator/rustlib/Cargo.toml @@ -1,10 +1,11 @@ [package] -name = "rust_call" +name = "rustlib" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" \ No newline at end of file +[build-dependencies] +gobuild = "0.1.0-alpha.1" +log = "0.4.14" +env_logger = "0.9" +glob = "0.3.1" \ No newline at end of file diff --git a/mpt-witness-generator/rustlib/build.rs b/mpt-witness-generator/rustlib/build.rs new file mode 100644 index 0000000000..73d6f57feb --- /dev/null +++ b/mpt-witness-generator/rustlib/build.rs @@ -0,0 +1,47 @@ +use std::{ + env, + io::{self, Write}, +}; + +fn main() { + let lib_name = "mpt-witness-generator"; + let out_dir = env::var("OUT_DIR").unwrap(); + + // Build + if let Err(e) = gobuild::Build::new() + .file("../witness_gen_wrapper.go") + .try_compile(lib_name) + { + // The error type is private so have to check the error string + if format!("{}", e).starts_with("Failed to find tool.") { + fail( + " Failed to find Go. Please install Go 1.16 or later \ + following the instructions at https://golang.org/doc/install. + On linux it is also likely available as a package." + .to_string(), + ); + } else { + fail(format!("{}", e)); + } + } + + // Files the lib depends on that should recompile the lib + let dep_files = glob::glob("../**/*.go").unwrap().filter_map(|v| v.ok()); + + for file in dep_files { + println!("cargo:rerun-if-changed={}", file.to_str().unwrap()); + } + + // Link + println!("cargo:rustc-link-search=native={}", out_dir); + println!("cargo:rustc-link-lib=static={}", lib_name); +} + +fn fail(message: String) { + let _ = writeln!( + io::stderr(), + "\n\nError while building geth-utils: {}\n\n", + message + ); + std::process::exit(1); +} diff --git a/mpt-witness-generator/rustlib/src/lib.rs b/mpt-witness-generator/rustlib/src/lib.rs new file mode 100644 index 0000000000..c79ce20150 --- /dev/null +++ b/mpt-witness-generator/rustlib/src/lib.rs @@ -0,0 +1,39 @@ +use std::{ + ffi::{CStr, CString}, + os::raw::c_char, +}; + +mod witness { + use super::*; + extern "C" { + pub fn GetWitness(str: *const c_char) -> *const c_char; + } +} + +pub fn get_witness(data: &str) -> String { + let c_config = CString::new(data).expect("invalid config"); + let result = unsafe { witness::GetWitness(c_config.as_ptr()) }; + let c_str = unsafe { CStr::from_ptr(result) }; + let c_str_string = c_str.to_str().expect("Error translating from library"); + c_str_string.to_string() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let data = r#" + { + "NodeUrl": "https://mainnet.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161", + "BlockNum": 14359865, + "Addr": "0x4E5B2e1dc63F6b91cb6Cd759936495434C7e972F", + "Keys": ["0x12", "0x21"], + "Values": ["0x1123e2", "0xa21"] + }"#; + + let result = get_witness(data); + assert!(result.contains("disable_preimage_check")); + } +}