Skip to content

Commit

Permalink
Log code hash if contract is already uploaded. (use-ink#805)
Browse files Browse the repository at this point in the history
* Log code hash if contract is already uploaded.

* Reimplement from_metadata as TryFrom

* Copy instance of code hash rather than loading the whole metadata from file.

* Format code.

* Update crates/cargo-contract/src/cmd/extrinsics/upload.rs

Co-authored-by: Andrew Jones <ascjones@gmail.com>

* Fix compilation.

Co-authored-by: Andrew Jones <ascjones@gmail.com>
  • Loading branch information
deuszx and ascjones authored Nov 2, 2022
1 parent 1713e56 commit 70b75d5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

15 changes: 13 additions & 2 deletions crates/cargo-contract/src/cmd/extrinsics/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ impl UploadCommand {
let crate_metadata = CrateMetadata::from_manifest_path(
self.extrinsic_opts.manifest_path.as_ref(),
)?;
let transcoder = ContractMessageTranscoder::load(crate_metadata.metadata_path())?;
let contract_metadata =
contract_metadata::ContractMetadata::load(&crate_metadata.metadata_path())?;
let code_hash = contract_metadata.source.hash;
let transcoder =
ContractMessageTranscoder::try_from(contract_metadata).context(format!(
"Failed to deserialize ink project metadata from contract metadata {}",
crate_metadata.metadata_path().display()
))?;
let signer = super::pair_signer(self.extrinsic_opts.signer()?);

let wasm_path = match &self.wasm_path {
Expand Down Expand Up @@ -127,7 +134,11 @@ impl UploadCommand {
}
Ok(())
} else {
Err("This contract has already been uploaded".into())
Err(anyhow::anyhow!(
"This contract has already been uploaded with code hash: {:?}",
code_hash
)
.into())
}
})
}
Expand Down
1 change: 1 addition & 0 deletions crates/metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ semver = { version = "1.0.14", features = ["serde"] }
serde = { version = "1.0.147", default-features = false, features = ["derive"] }
serde_json = "1.0.87"
url = { version = "2.3.1", features = ["serde"] }
anyhow = "1.0.66"

[dev-dependencies]
pretty_assertions = "1.3.0"
22 changes: 21 additions & 1 deletion crates/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@

mod byte_str;

use anyhow::{
Context,
Result,
};
use semver::Version;
use serde::{
de,
Expand All @@ -75,6 +79,8 @@ use std::{
Formatter,
Result as DisplayResult,
},
fs::File,
path::Path,
str::FromStr,
};
use url::Url;
Expand Down Expand Up @@ -113,10 +119,24 @@ impl ContractMetadata {
pub fn remove_source_wasm_attribute(&mut self) {
self.source.wasm = None;
}

/// Reads the file and tries to parse it as instance of `ContractMetadata`.
pub fn load<P>(metadata_path: &P) -> Result<Self>
where
P: AsRef<Path>,
{
let path = metadata_path.as_ref();
let file = File::open(path)
.context(format!("Failed to open metadata file {}", path.display()))?;
serde_json::from_reader(file).context(format!(
"Failed to deserialize metadata file {}",
path.display()
))
}
}

/// Representation of the Wasm code hash.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct CodeHash(
#[serde(
serialize_with = "byte_str::serialize_as_byte_str",
Expand Down
22 changes: 14 additions & 8 deletions crates/transcode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ use scale_info::{
};
use std::{
fmt::Debug,
fs::File,
path::Path,
};

Expand Down Expand Up @@ -166,13 +165,8 @@ impl ContractMessageTranscoder {
P: AsRef<Path>,
{
let path = metadata_path.as_ref();
let file = File::open(path)
.context(format!("Failed to open metadata file {}", path.display()))?;
let metadata: contract_metadata::ContractMetadata = serde_json::from_reader(file)
.context(format!(
"Failed to deserialize metadata file {}",
path.display()
))?;
let metadata: contract_metadata::ContractMetadata =
contract_metadata::ContractMetadata::load(&metadata_path)?;
let ink_metadata = serde_json::from_value(serde_json::Value::Object(
metadata.abi,
))
Expand Down Expand Up @@ -344,6 +338,18 @@ impl ContractMessageTranscoder {
}
}

impl TryFrom<contract_metadata::ContractMetadata> for ContractMessageTranscoder {
type Error = anyhow::Error;

fn try_from(
metadata: contract_metadata::ContractMetadata,
) -> Result<Self, Self::Error> {
Ok(Self::new(serde_json::from_value(
serde_json::Value::Object(metadata.abi),
)?))
}
}

#[derive(Debug)]
pub enum CompositeTypeFields {
Named(Vec<CompositeTypeNamedField>),
Expand Down

0 comments on commit 70b75d5

Please sign in to comment.