This repository has been archived by the owner on Aug 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Parse args and run in a separate thread * Native helper struct for static FFI invocation * Wait until node starts when run as daemon * Flags for success, daemon and displaying help
- Loading branch information
1 parent
b592186
commit bce3809
Showing
10 changed files
with
296 additions
and
22 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CARGO ?= cargo | ||
TARGET ?= | ||
|
||
build-native-pkg: | ||
CRATE_CC_NO_DEFAULTS=1 $(CARGO) build --package meta-node --release $(if $(TARGET),--target $(TARGET),) | ||
mkdir -p pkg/metachain/include pkg/metachain/lib | ||
cp target/$(if $(TARGET),$(TARGET)/,)release/libmeta_node.a pkg/metachain/lib/libmetachain.a | ||
cp target/libmc.hpp pkg/metachain/include/ | ||
cp target/libmc.cpp pkg/metachain/ |
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 |
---|---|---|
@@ -1,7 +1,39 @@ | ||
use proc_macro2::TokenStream; | ||
use substrate_build_script_utils::{generate_cargo_keys, rerun_if_git_head_changed}; | ||
|
||
use std::env; | ||
use std::fs::File; | ||
use std::io::{Read, Write}; | ||
use std::path::PathBuf; | ||
|
||
fn main() { | ||
generate_cargo_keys(); | ||
|
||
rerun_if_git_head_changed(); | ||
|
||
let mut root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); | ||
let parent = root.clone(); | ||
root.pop(); | ||
root.pop(); | ||
let lib_path = &parent.join("src").join("lib.rs"); | ||
println!("cargo:rerun-if-changed={}", lib_path.display()); | ||
let target_dir = &root.join("target"); | ||
|
||
let mut content = String::new(); | ||
File::open(lib_path) | ||
.unwrap() | ||
.read_to_string(&mut content) | ||
.unwrap(); | ||
let tt: TokenStream = content.parse().unwrap(); | ||
let codegen = cxx_gen::generate_header_and_cc(tt, &cxx_gen::Opt::default()).unwrap(); | ||
|
||
let cpp_stuff = String::from_utf8(codegen.implementation).unwrap(); | ||
File::create(target_dir.join("libmc.hpp")) | ||
.unwrap() | ||
.write_all(&codegen.header) | ||
.unwrap(); | ||
File::create(target_dir.join("libmc.cpp")) | ||
.unwrap() | ||
.write_all(cpp_stuff.as_bytes()) | ||
.unwrap(); | ||
} |
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,87 @@ | ||
mod chain_spec; | ||
mod native; | ||
#[macro_use] | ||
mod service; | ||
mod cli; | ||
mod command; | ||
mod rpc; | ||
|
||
use std::error::Error; | ||
|
||
use clap::{CommandFactory, ErrorKind, FromArgMatches}; | ||
use cli::Cli; | ||
use cxx::{CxxString, CxxVector}; | ||
|
||
// NOTE: Struct definitions for FFI should match those defined in libain-rs/protobuf | ||
|
||
#[cxx::bridge] | ||
mod ffi { | ||
pub struct DmcTx { | ||
pub from: String, | ||
pub to: String, | ||
pub amount: i64, | ||
} | ||
|
||
pub struct DmcBlock { | ||
pub payload: Vec<u8>, | ||
} | ||
|
||
struct ExecResult { | ||
is_help: bool, | ||
success: bool, | ||
daemon: bool, | ||
} | ||
|
||
extern "Rust" { | ||
fn mint_block(dmc_txs: &CxxVector<DmcTx>) -> Result<DmcBlock>; | ||
fn parse_args_and_run(args: &CxxVector<CxxString>) -> ExecResult; | ||
} | ||
} | ||
|
||
#[inline] | ||
fn mint_block(dmc_txs: &CxxVector<ffi::DmcTx>) -> Result<ffi::DmcBlock, Box<dyn Error>> { | ||
self::native::NATIVE_HELPER | ||
.read().unwrap().as_ref().expect("uninitialized native helper") | ||
.mint_block(dmc_txs.iter()) | ||
} | ||
|
||
fn parse_args_and_run(args: &CxxVector<CxxString>) -> ffi::ExecResult { | ||
let args: Vec<String> = args | ||
.iter() | ||
.map(|s| s.to_string_lossy().into_owned()) | ||
.collect(); | ||
let res = Cli::command().try_get_matches_from(args) | ||
.and_then(|mut m| Cli::from_arg_matches_mut(&mut m)); | ||
|
||
let cli = match res { | ||
Ok(c) => c, | ||
Err(e) => { | ||
let _ = e.print(); | ||
let is_help = match e.kind() { | ||
ErrorKind::DisplayHelp | ErrorKind::DisplayVersion => true, | ||
_ => false, | ||
}; | ||
return ffi::ExecResult { | ||
is_help, | ||
success: is_help, | ||
daemon: false, | ||
} | ||
}, | ||
}; | ||
|
||
let mut result = ffi::ExecResult { | ||
is_help: false, | ||
success: false, | ||
daemon: cli.subcommand.is_none(), | ||
}; | ||
match command::run(cli) { | ||
Ok(_) => { | ||
// Deciding to run the node, give control back to defid without joining handle | ||
// FIXME: Figure out how to send SIGINT on defid exiting for cleanly shutting down | ||
result.success = true; | ||
}, | ||
Err(e) => eprintln!("{:?}", e), | ||
} | ||
|
||
result | ||
} |
Oops, something went wrong.