-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: populate metadata with build information #55
Changes from 5 commits
9d4672b
2dc8d9f
9b454d5
258e7fe
758c7a4
0c2bb6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious how we're going to inject this bit of information. Env var only set when built from contract-builder? Because either way, that information needs to be known at this stage to be embedded in the wasm file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Most likely. There no built-in way to get the docker image name from inside of the container from what I have researched. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was actually wondering if it makes sense to "demote" Also, presumably sometime in the future rustc/cargo will reach the point where wasm builds can be bit-by-bit reproducible and hedonistic and we will not need an image anymore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good point, not sure what the size overhead would be from including the JS SDK in the same docker image, but it would certainly help unify things. Also, imho, the current docker image might be a little too big, I'll try to see if we can cut things down a bit, drop unnecessary binaries, strip some maybe etc. Doesn't seem like the official rust image makes much of an effort to reduce size anyway. |
||
}); | ||
if args.embed_abi { | ||
let path = util::handle_step("Compressing ABI to be embedded..", || { | ||
let AbiResult { path } = abi::write_to_file( | ||
|
@@ -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) { | ||
|
@@ -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())); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hahaha, interesting that this is still a valid toml syntax There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am working on a PR right now that will allow us not to update all the toml files every time we upgrade the SDK version, so will fix there There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Can't wait to see it. |
||
|
||
[workspace] | ||
members = [] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we do this before writing out the embedded version of the ABI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is opinionated I guess. Do you think this information should be embedded inside of the wasm file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unsure, but this, in a sense, is part of the contract's signature.
We could make it opt-in, though, so another flag for including build information in the embedded ABI?
near/near-abi-rs#8 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, decided to include the build information in the end. It is only ~30 extra bytes anyway and we can always come up with a flag that disables it if there is a demand.
Also, probably wise to try and keep embedded and non-embedded ABI to look as close as possible.