Skip to content

Commit

Permalink
feat: populate metadata with build information (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
itegulov authored Oct 20, 2022
1 parent eea2140 commit 2cfcf31
Show file tree
Hide file tree
Showing 22 changed files with 228 additions and 173 deletions.
7 changes: 5 additions & 2 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion cargo-near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ categories = ["development-tools", "development-tools::cargo-plugins", "developm

[dependencies]
anyhow = "1.0"
bs58 = "0.4"
camino = "1.1.1"
cargo_metadata = "0.14"
clap = { version = "3.2", features = ["derive", "env"] }
colored = "2.0"
env_logger = "0.9"
log = "0.4"
rustc_version = "0.4"
serde_json = "1.0"
sha2 = "0.10"
symbolic-debuginfo = "8.8"
schemars = "0.8"
near-abi = { version = "0.1.0-pre.0", features = ["__chunked-entries"] }
near-abi = { version = "0.2.0", features = ["__chunked-entries"] }
libloading = "0.7.3"
zstd = "0.11"
2 changes: 2 additions & 0 deletions cargo-near/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ fn extract_metadata(crate_metadata: &CrateMetadata) -> near_abi::AbiMetadata {
name: Some(package.name.clone()),
version: Some(package.version.to_string()),
authors: package.authors.clone(),
build: None,
wasm_hash: None,
other: HashMap::new(),
}
}
Expand Down
33 changes: 21 additions & 12 deletions cargo-near/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::abi::{AbiCompression, AbiFormat, AbiResult};
use crate::cargo::{manifest::CargoManifestPath, metadata::CrateMetadata};
use crate::{abi, util, BuildCommand};
use colored::Colorize;
use near_abi::BuildInfo;
use sha2::{Digest, Sha256};
use std::io::BufRead;

const COMPILATION_TARGET: &str = "wasm32-unknown-unknown";
Expand Down Expand Up @@ -35,18 +37,15 @@ pub(crate) fn run(args: BuildCommand) -> anyhow::Result<()> {
cargo_args.push("--release");
}

let mut pretty_abi_path = None;
let mut abi = None;
let mut min_abi_path = None;
if !args.no_abi {
let contract_abi = abi::generate_abi(&crate_metadata, args.doc, true)?;
let AbiResult { path } = abi::write_to_file(
&contract_abi,
&crate_metadata,
AbiFormat::Json,
AbiCompression::NoOp,
)?;
pretty_abi_path.replace(util::copy(&path, &out_dir)?);

let mut contract_abi = abi::generate_abi(&crate_metadata, args.doc, true)?;
contract_abi.metadata.build = Some(BuildInfo {
compiler: format!("rustc {}", rustc_version::version()?),
builder: format!("cargo-near {}", env!("CARGO_PKG_VERSION")),
image: None,
});
if args.embed_abi {
let path = util::handle_step("Compressing ABI to be embedded..", || {
let AbiResult { path } = abi::write_to_file(
Expand All @@ -59,6 +58,7 @@ pub(crate) fn run(args: BuildCommand) -> anyhow::Result<()> {
})?;
min_abi_path.replace(util::copy(&path, &out_dir)?);
}
abi = Some(contract_abi);
}

if let (true, Some(abi_path)) = (args.embed_abi, &min_abi_path) {
Expand All @@ -82,8 +82,17 @@ pub(crate) fn run(args: BuildCommand) -> anyhow::Result<()> {
"Binary",
wasm_artifact.path.to_string().bright_yellow().bold(),
)];
if let Some(abi_path) = pretty_abi_path {
messages.push(("ABI", abi_path.to_string().yellow().bold()));
if let Some(mut abi) = abi {
let mut hasher = Sha256::new();
hasher.update(std::fs::read(&wasm_artifact.path)?);
let hash = hasher.finalize();
let hash = bs58::encode(hash).into_string();
abi.metadata.wasm_hash = Some(hash);

let AbiResult { path } =
abi::write_to_file(&abi, &crate_metadata, AbiFormat::Json, AbiCompression::NoOp)?;
let pretty_abi_path = util::copy(&path, &out_dir)?;
messages.push(("ABI", pretty_abi_path.to_string().yellow().bold()));
}
if let Some(abi_path) = min_abi_path {
messages.push(("Embedded ABI", abi_path.to_string().yellow().bold()));
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
publish = false

[dependencies]
near-abi = "0.1.0-pre.0"
near-abi = "0.2.0"

[dev-dependencies]
anyhow = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/templates/_Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { version = "4.1.0-pre.3", features = ["abi"] }
near-sdk = { version = "4.1.0-pre.3", features = ["abi"], git = "https://github.com/near/near-sdk-rs.git", rev = "6d73c9ff4fd095fc23eaa000c14ab65c15c4aa6b" }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ schemars = "0.8"
[dependencies.near-sdk]
version = "4.1.0-pre.3"
features = ["abi"]
git = "https://github.com/near/near-sdk-rs.git"
rev = "6d73c9ff4fd095fc23eaa000c14ab65c15c4aa6b"

[workspace]
members = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { version = "4.1.0-pre.3", features = ["abi", "unstable"] }
near-sdk = { version = "4.1.0-pre.3", features = ["abi", "unstable"], git = "https://github.com/near/near-sdk-rs.git", rev = "6d73c9ff4fd095fc23eaa000c14ab65c15c4aa6b" }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = { version = "4.1.0-pre.3", default-features = false, features = ["abi"] }
near-sdk = { version = "4.1.0-pre.3", default-features = false, features = ["abi"], git = "https://github.com/near/near-sdk-rs.git", rev = "6d73c9ff4fd095fc23eaa000c14ab65c15c4aa6b" }
serde = { version = "1", features = ["derive"] }
schemars = "0.8"

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 = "792d5eb26d26a0878dbf59e304afa4e19540c317" }
near-sdk = { git = "https://github.com/near/near-sdk-rs.git", rev = "792d5eb26d26a0878dbf59e304afa4e19540c317", git = "https://github.com/near/near-sdk-rs.git", rev = "6d73c9ff4fd095fc23eaa000c14ab65c15c4aa6b" }

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

[target.'cfg(windows)'.dependencies]
near-sdk = { version = "4.1.0-pre.3", features = ["abi"] }
near-sdk = { version = "4.1.0-pre.3", features = ["abi"], git = "https://github.com/near/near-sdk-rs.git", rev = "6d73c9ff4fd095fc23eaa000c14ab65c15c4aa6b" }

[target.'cfg(unix)'.dependencies]
near-sdk = { version = "4.1.0-pre.3", features = ["abi"] }
near-sdk = { version = "4.1.0-pre.3", features = ["abi"], git = "https://github.com/near/near-sdk-rs.git", rev = "6d73c9ff4fd095fc23eaa000c14ab65c15c4aa6b" }

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

[dependencies.near]
near = { version = "4.1.0-pre.3", package = "near-sdk", features = ["abi"] }
near = { version = "4.1.0-pre.3", package = "near-sdk", features = ["abi"], git = "https://github.com/near/near-sdk-rs.git", rev = "6d73c9ff4fd095fc23eaa000c14ab65c15c4aa6b" }

[workspace]
members = []
Expand Down
Loading

0 comments on commit 2cfcf31

Please sign in to comment.