Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
miraclx committed Aug 22, 2022
1 parent 15abb7f commit 845fa86
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 142 deletions.
3 changes: 1 addition & 2 deletions cargo-near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ serde_json = "1.0"
symbolic-debuginfo = "8.8"
syn = "1.0"
quote = "1.0"
getrandom = { version = "0.2.7", features = ["std"] }

[dependencies.near-abi]
git = "https://github.com/near/near-abi-rs"
branch = "miraclx/abi-embed"
branch = "miraclx/init"
features = ["chunked-entries"]

[dev-dependencies]
Expand Down
17 changes: 6 additions & 11 deletions cargo-near/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ mod generation;

const ABI_FILE: &str = "abi.json";

/// ABI generation result.
pub(crate) struct AbiResult {
pub source_hash: u64,
/// Path to the resulting ABI file.
pub path: PathBuf,
}

Expand All @@ -28,7 +29,7 @@ pub(crate) fn write_to_file(crate_metadata: &CrateMetadata) -> anyhow::Result<Ab

let dylib_artifact = util::compile_project(
&crate_metadata.manifest_path,
&[],
&["--features", "near-sdk/__abi-generate"],
vec![
("CARGO_PROFILE_DEV_OPT_LEVEL", "0"),
("CARGO_PROFILE_DEV_DEBUG", "0"),
Expand All @@ -37,8 +38,7 @@ pub(crate) fn write_to_file(crate_metadata: &CrateMetadata) -> anyhow::Result<Ab
util::dylib_extension(),
)?;

// todo! re-use cargo-near for extracting data from the dylib
// todo! instead of a temp project
// todo! experiment with reusing cargo-near for extracting data from the dylib
if dylib_artifact.fresh {
let cargo_toml = generation::generate_toml(&crate_metadata.manifest_path)?;
fs::write(near_abi_gen_dir.join("Cargo.toml"), cargo_toml)?;
Expand All @@ -63,18 +63,13 @@ pub(crate) fn write_to_file(crate_metadata: &CrateMetadata) -> anyhow::Result<Ab
let combined_entries =
near_abi::ChunkedAbiEntry::combine(serde_json::from_slice::<Vec<_>>(&stdout)?.into_iter())?;

let source_hash = combined_entries.source_hash;

let contract_abi = near_abi::AbiRoot::new(extract_metadata(crate_metadata), combined_entries);

let near_abi_json = serde_json::to_string(&contract_abi)?;
let out_path_abi = crate_metadata.target_directory.join(ABI_FILE);
fs::write(&out_path_abi, near_abi_json)?;

Ok(AbiResult {
source_hash,
path: out_path_abi,
})
Ok(AbiResult { path: out_path_abi })
}

fn extract_metadata(crate_metadata: &CrateMetadata) -> near_abi::AbiMetadata {
Expand All @@ -92,7 +87,7 @@ pub(crate) fn dump_to_file(args: AbiCommand) -> anyhow::Result<()> {
args.manifest_path.unwrap_or_else(|| "Cargo.toml".into()),
)?)?;

let AbiResult { path, .. } = write_to_file(&crate_metadata)?;
let AbiResult { path } = write_to_file(&crate_metadata)?;

println!("ABI successfully generated at {}", path.display());

Expand Down
27 changes: 3 additions & 24 deletions cargo-near/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::env;
use std::ffi::OsStr;
use std::fmt::Write;
use std::fs;
use std::io::BufRead;
use std::process::Command;

Expand Down Expand Up @@ -61,41 +59,21 @@ pub(crate) fn build(args: BuildCommand) -> anyhow::Result<()> {

let wasm_artifact = if args.embed_abi {
let abi::AbiResult {
source_hash,
path: abi_file_path,
} = abi::write_to_file(&crate_metadata)?;

// todo! add compression.
// todo! test differences between snappy and zstd

let mut ack_secret = [0; 8];
getrandom::getrandom(&mut ack_secret).context("failed to generate ack secret")?;
let mut ack_secret_hex = String::new();
for b in ack_secret.iter() {
write!(ack_secret_hex, "{:02x}", b).unwrap();
}
cargo_args.extend(&["--features", "near-sdk/__abi-embed"]);

let wasm_artifact = util::compile_project(
&crate_metadata.manifest_path,
&cargo_args,
vec![
("RUSTC_WRAPPER", &std::env::args().next().unwrap()),
("CARGO_NEAR_ABI_SOURCE_HASH", &format!("{:x}", source_hash)),
("CARGO_NEAR_ABI_PATH", abi_file_path.to_str().unwrap()),
("CARGO_NEAR_ABI_ACK", &ack_secret_hex),
],
vec![("CARGO_NEAR_ABI_PATH", abi_file_path.to_str().unwrap())],
"wasm",
)?;

if wasm_artifact.fresh {
let ack_path = abi_file_path.with_extension("ack");
let file = fs::read_to_string(&ack_path)?;
if ack_secret_hex != file {
anyhow::bail!("cargo-near was unable to embed the ABI");
}
fs::remove_file(ack_path)?;
}

wasm_artifact
} else {
util::compile_project(&crate_metadata.manifest_path, &cargo_args, vec![], "wasm")?
Expand All @@ -110,6 +88,7 @@ pub(crate) fn build(args: BuildCommand) -> anyhow::Result<()> {
}
}

// todo! if we embedded, check that the binary exports the __contract_abi symbol
println!("Contract successfully built at {}", out_path.display());

Ok(())
Expand Down
4 changes: 0 additions & 4 deletions cargo-near/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::ffi::OsString;
use std::path::PathBuf;

use clap::{AppSettings, Args, Parser, Subcommand};
Expand All @@ -14,9 +13,6 @@ pub enum Opts {
#[clap(name = "near")]
#[clap(setting = AppSettings::DeriveDisplayOrder)]
Near(NearArgs),
/// Rustc wrapper
#[clap(name = "rustc", hidden = true, external_subcommand)]
Rustc(Vec<OsString>),
}

#[derive(Debug, Args)]
Expand Down
28 changes: 12 additions & 16 deletions cargo-near/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ use colored::Colorize;

use cargo_near::Opts;

mod rustc_wrapper;

fn main() {
env_logger::init();

match Opts::parse() {
Opts::Near(args) => match cargo_near::exec(args.cmd) {
Ok(()) => {}
Err(err) => {
eprintln!(
"{} {}",
"ERROR:".bright_red().bold(),
format!("{:?}", err).bright_red()
);
std::process::exit(1);
}
},
Opts::Rustc(args) => rustc_wrapper::run(args),
};
let Opts::Near(args) = Opts::parse();
match cargo_near::exec(args.cmd) {
Ok(()) => {}
Err(err) => {
eprintln!(
"{} {}",
"ERROR:".bright_red().bold(),
format!("{:?}", err).bright_red()
);
std::process::exit(1);
}
}
}
72 changes: 0 additions & 72 deletions cargo-near/src/rustc_wrapper.rs

This file was deleted.

2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ quote = "1.0"
[dependencies.near-abi]
git = "https://github.com/near/near-abi-rs"
branch = "miraclx/init"
features = ["versioned-entries"]
features = ["chunked-entries"]
2 changes: 1 addition & 1 deletion integration-tests/templates/_Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ schemars = "0.8"

[dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
branch = "miraclx/abi-embed"
branch = "miraclx/init"
features = ["abi"]

[workspace]
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/templates/_Cargo_no_abi_feature.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ schemars = "0.8"

[dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
branch = "miraclx/abi-embed"
branch = "miraclx/init"

[workspace]
members = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ schemars = "0.8"

[dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
branch = "miraclx/abi-embed"
branch = "miraclx/init"
features = ["abi"]

[workspace]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ schemars = "0.8"

[dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
branch = "miraclx/abi-embed"
branch = "miraclx/init"
features = ["abi", "unstable"]

[workspace]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ schemars = "0.8"

[dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
branch = "miraclx/abi-embed"
branch = "miraclx/init"
default-features = false
features = ["abi"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = { version = "1", features = ["derive"] }
schemars = "0.8"

[patch.crates-io]
near-sdk = { git = "https://github.com/near/near-sdk-rs.git", rev = "f0e38f82182314f2b1ff6d47c7f25413d49224e3" }
near-sdk = { git = "https://github.com/near/near-sdk-rs.git", rev = "b83304865584ad70db74f4e37ce37fb27d6739c2" }

[workspace]
members = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ schemars = "0.8"

[target.'cfg(windows)'.dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
branch = "miraclx/abi-embed"
branch = "miraclx/init"
features = ["abi"]

[target.'cfg(unix)'.dependencies.near-sdk]
git = "https://github.com/near/near-sdk-rs.git"
branch = "miraclx/abi-embed"
branch = "miraclx/init"
features = ["abi"]

[workspace]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ schemars = "0.8"
[dependencies.near]
package = "near-sdk"
git = "https://github.com/near/near-sdk-rs.git"
branch = "miraclx/abi-embed"
branch = "miraclx/init"
features = ["abi"]

[workspace]
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/tests/cargo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn clone_git_repo(version: &str) -> anyhow::Result<TempDir> {
#[test]
#[named]
fn test_dependency_local_path() -> anyhow::Result<()> {
let near_sdk_dir = clone_git_repo("f0e38f82182314f2b1ff6d47c7f25413d49224e3")?;
let near_sdk_dir = clone_git_repo("b83304865584ad70db74f4e37ce37fb27d6739c2")?;
let near_sdk_dep_path = near_sdk_dir.path().join("near-sdk");

// near-sdk = { path = "::path::", features = ["abi"] }
Expand All @@ -40,7 +40,7 @@ fn test_dependency_local_path() -> anyhow::Result<()> {
#[test]
#[named]
fn test_dependency_local_path_with_version() -> anyhow::Result<()> {
let near_sdk_dir = clone_git_repo("f0e38f82182314f2b1ff6d47c7f25413d49224e3")?;
let near_sdk_dir = clone_git_repo("b83304865584ad70db74f4e37ce37fb27d6739c2")?;
let near_sdk_dep_path = near_sdk_dir.path().join("near-sdk");

// near-sdk = { path = "::path::", version = "4.1.0-pre.1", features = ["abi"] }
Expand Down Expand Up @@ -172,7 +172,7 @@ fn test_dependency_patch() -> anyhow::Result<()> {
// near-sdk = { version = "4.1.0-pre.1", features = ["abi"] }
//
// [patch.crates-io]
// near-sdk = { git = "https://github.com/near/near-sdk-rs.git", rev = "f0e38f82182314f2b1ff6d47c7f25413d49224e3" }
// near-sdk = { git = "https://github.com/near/near-sdk-rs.git", rev = "b83304865584ad70db74f4e37ce37fb27d6739c2" }
let abi_root = generate_abi_fn! {
with Cargo "/templates/sdk-dependency/_Cargo_patch.toml";

Expand Down

0 comments on commit 845fa86

Please sign in to comment.