-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: build and run F3 sidecar in Forest process via FFI
- Loading branch information
1 parent
9c1a366
commit 9f41432
Showing
12 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2019-2024 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
fn main() { | ||
// whitelist the cfg for cargo clippy | ||
println!("cargo::rustc-check-cfg=cfg(f3sidecar)"); | ||
|
||
// Do not build f3-sidecar on docs.rs publishing | ||
// No proper version of Go compiler is available. | ||
if !is_docs_rs() && is_sidecar_ffi_enabled() { | ||
println!("cargo:rustc-cfg=f3sidecar"); | ||
rust2go::Builder::default() | ||
.with_go_src("./f3-sidecar") | ||
.with_regen_arg(rust2go::RegenArgs { | ||
src: "./src/f3/go_ffi.rs".into(), | ||
dst: "./f3-sidecar/ffi_gen.go".into(), | ||
without_main: true, | ||
..Default::default() | ||
}) | ||
.build(); | ||
} | ||
} | ||
|
||
// See <https://docs.rs/about/builds#detecting-docsrs> | ||
fn is_docs_rs() -> bool { | ||
std::env::var("DOCS_RS").is_ok() | ||
} | ||
|
||
fn is_sidecar_ffi_enabled() -> bool { | ||
// Opt-out building the F3 sidecar staticlib | ||
match std::env::var("FOREST_F3_SIDECAR_FFI_BUILD_OPT_OUT") { | ||
Ok(value) => !matches!(value.to_lowercase().as_str(), "1" | "true"), | ||
_ => true, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,6 @@ Cargo.lock | |
|
||
# The default f3 database folder | ||
/f3-db | ||
|
||
# generated by rust2go | ||
ffi_gen.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
logging "github.com/ipfs/go-log/v2" | ||
) | ||
|
||
func init() { | ||
setGoDebugEnv() | ||
logging.SetAllLoggers(logging.LevelWarn) | ||
err := logging.SetLogLevel("f3/sidecar", "info") | ||
checkError(err) | ||
err = logging.SetLogLevel("f3", "info") | ||
checkError(err) | ||
GoF3NodeImpl = &f3Impl{ctx: context.Background()} | ||
} | ||
|
||
type f3Impl struct { | ||
ctx context.Context | ||
} | ||
|
||
func (f3 *f3Impl) run(rpc_endpoint string, f3_rpc_endpoint string, finality int64, db string, manifest_server string) bool { | ||
err := run(f3.ctx, rpc_endpoint, f3_rpc_endpoint, finality, db, manifest_server) | ||
return err == nil | ||
} | ||
|
||
func checkError(err error) { | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// To avoid potential panics | ||
// See <https://github.com/ChainSafe/forest/pull/4636#issuecomment-2306500753> | ||
func setGoDebugEnv() { | ||
os.Setenv("GODEBUG", "invalidptr=0,cgocheck=0") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2019-2024 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
pub mod binding { | ||
#![allow(warnings)] | ||
rust2go::r2g_include_binding!(); | ||
} | ||
|
||
#[derive(rust2go::R2G, Clone, Default)] | ||
pub struct EmptyReq {} | ||
|
||
#[rust2go::r2g] | ||
pub trait GoF3Node { | ||
fn run( | ||
rpc_endpoint: String, | ||
f3_rpc_endpoint: String, | ||
finality: i64, | ||
db: String, | ||
manifest_server: String, | ||
) -> bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2019-2024 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
#[cfg(f3sidecar)] | ||
mod go_ffi; | ||
#[cfg(f3sidecar)] | ||
use go_ffi::*; | ||
|
||
pub fn run_f3_sidecar_if_enabled( | ||
_rpc_endpoint: String, | ||
_f3_rpc_endpoint: String, | ||
_finality: i64, | ||
_db: String, | ||
_manifest_server: String, | ||
) { | ||
if is_sidecar_ffi_enabled() { | ||
#[cfg(f3sidecar)] | ||
{ | ||
GoF3NodeImpl::run( | ||
_rpc_endpoint, | ||
_f3_rpc_endpoint, | ||
_finality, | ||
_db, | ||
_manifest_server, | ||
); | ||
} | ||
} | ||
} | ||
|
||
// Use opt-in mode for now. Consider switching to opt-out mode once F3 is shipped. | ||
fn is_sidecar_ffi_enabled() -> bool { | ||
// Opt-out building the F3 sidecar staticlib | ||
match std::env::var("FOREST_F3_SIDECAR_FFI_ENABLED") { | ||
Ok(value) => { | ||
let enabled = matches!(value.to_lowercase().as_str(), "1" | "true"); | ||
cfg_if::cfg_if! { | ||
if #[cfg(f3sidecar)] { | ||
enabled | ||
} | ||
else { | ||
if enabled { | ||
tracing::error!("Failed to enable F3 sidecar, the forerst binary is not compiled with f3-sidecar Go lib"); | ||
} | ||
false | ||
} | ||
} | ||
} | ||
_ => false, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ mod daemon; | |
mod db; | ||
mod documentation; | ||
mod eth; | ||
mod f3; | ||
mod fil_cns; | ||
mod genesis; | ||
mod health; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters