Skip to content
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

Update to weights v2 #809

Merged
merged 17 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
596 changes: 302 additions & 294 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions crates/cargo-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ wasm-opt = "0.110.2"

# dependencies for extrinsics (deploying and calling a contract)
async-std = { version = "1.12.0", features = ["attributes", "tokio1"] }
sp-core = "6.0.0"
pallet-contracts-primitives = "6.0.0"
subxt = "0.24.0"
sp-core = "7.0.0"
sp-weights = "4.0.0"
pallet-contracts-primitives = "7.0.0"
subxt = "0.25.0"
hex = "0.4.3"
jsonrpsee = { version = "0.16.0", features = ["ws-client"] }

Expand Down
55 changes: 31 additions & 24 deletions crates/cargo-contract/src/cmd/extrinsics/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
use super::{
display_contract_exec_result,
prompt_confirm_tx,
runtime_api::{
api,
Weight,
},
runtime_api::api,
state_call,
submit_extrinsic,
Balance,
Expand All @@ -31,6 +28,7 @@ use super::{
DefaultConfig,
ExtrinsicOpts,
PairSigner,
StorageDeposit,
TokenMetadata,
MAX_KEY_COL_WIDTH,
};
Expand All @@ -49,11 +47,9 @@ use anyhow::{
Result,
};

use pallet_contracts_primitives::{
ContractExecResult,
StorageDeposit,
};
use pallet_contracts_primitives::ContractExecResult;
use scale::Encode;
use sp_weights::Weight;
use transcode::Value;

use std::fmt::Debug;
Expand All @@ -76,10 +72,14 @@ pub struct CallCommand {
args: Vec<String>,
#[clap(flatten)]
extrinsic_opts: ExtrinsicOpts,
/// Maximum amount of gas to be used for this command.
/// If not specified will perform a dry-run to estimate the gas consumed for the instantiation.
/// Maximum amount of gas (execution time) to be used for this command.
/// If not specified will perform a dry-run to estimate the gas consumed for the call.
#[clap(name = "gas", long)]
gas_limit: Option<u64>,
/// Maximum proof size for this call.
/// If not specified will perform a dry-run to estimate the proof size required for the call.
#[clap(long)]
proof_size: Option<u64>,
/// The value to be transferred as part of the call.
#[clap(name = "value", long, default_value = "0")]
value: BalanceVariant,
Expand Down Expand Up @@ -113,14 +113,16 @@ impl CallCommand {
match result.result {
Ok(ref ret_val) => {
let value = transcoder
.decode_return(&self.message, &mut &ret_val.data.0[..])?;
.decode_return(&self.message, &mut &ret_val.data[..])?;
let dry_run_result = CallDryRunResult {
result: String::from("Success!"),
reverted: ret_val.did_revert(),
data: value,
gas_consumed: result.gas_consumed,
gas_required: result.gas_required,
storage_deposit: result.storage_deposit.clone(),
storage_deposit: StorageDeposit::from(
&result.storage_deposit,
),
};
if self.output_json {
println!("{}", dry_run_result.to_json()?);
Expand Down Expand Up @@ -159,7 +161,6 @@ impl CallCommand {
signer: &PairSigner,
) -> Result<ContractExecResult<Balance>> {
let url = self.extrinsic_opts.url_to_string();
let gas_limit = *self.gas_limit.as_ref().unwrap_or(&5_000_000_000_000);
let token_metadata = TokenMetadata::query(client).await?;
let storage_deposit_limit = self
.extrinsic_opts
Expand All @@ -171,7 +172,7 @@ impl CallCommand {
origin: signer.account_id().clone(),
dest: self.contract.clone(),
value: self.value.denominate_balance(&token_metadata)?,
gas_limit: Weight::from_ref_time(gas_limit),
gas_limit: None,
storage_deposit_limit,
input_data,
};
Expand Down Expand Up @@ -237,11 +238,11 @@ impl CallCommand {
signer: &PairSigner,
) -> Result<Weight> {
if self.extrinsic_opts.skip_dry_run {
return match self.gas_limit {
Some(gas) => Ok(Weight::from_ref_time(gas)),
None => {
return match (self.gas_limit, self.proof_size) {
(Some(ref_time), Some(proof_size)) => Ok(Weight::from_parts(ref_time, proof_size)),
_ => {
Err(anyhow!(
"Gas limit `--gas` argument required if `--skip-dry-run` specified"
"Weight args `--gas` and `--proof-size` required if `--skip-dry-run` specified"
))
}
}
Expand All @@ -255,8 +256,14 @@ impl CallCommand {
if !self.output_json {
super::print_gas_required_success(call_result.gas_required);
}
let gas_limit = self.gas_limit.unwrap_or(call_result.gas_required);
Ok(Weight::from_ref_time(gas_limit))
// use user specified values where provided, otherwise use the estimates
let ref_time = self
.gas_limit
.unwrap_or_else(|| call_result.gas_required.ref_time());
let proof_size = self
.proof_size
.unwrap_or_else(|| call_result.gas_required.proof_size());
Ok(Weight::from_parts(ref_time, proof_size))
}
Err(ref err) => {
let object = ErrorVariant::from_dispatch_error(err, &client.metadata())?;
Expand All @@ -280,7 +287,7 @@ pub struct CallRequest {
origin: <DefaultConfig as Config>::AccountId,
dest: <DefaultConfig as Config>::AccountId,
value: Balance,
gas_limit: Weight,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<Balance>,
input_data: Vec<u8>,
}
Expand All @@ -293,10 +300,10 @@ pub struct CallDryRunResult {
/// Was the operation reverted
pub reverted: bool,
pub data: Value,
pub gas_consumed: u64,
pub gas_required: u64,
pub gas_consumed: Weight,
pub gas_required: Weight,
/// Storage deposit after the operation
pub storage_deposit: StorageDeposit<Balance>,
pub storage_deposit: StorageDeposit,
}

impl CallDryRunResult {
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-contract/src/cmd/extrinsics/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl ErrorVariant {
) -> anyhow::Result<ErrorVariant> {
match error {
DispatchError::Module(err) => {
let details = metadata.error(err.index, err.error)?;
let details = metadata.error(err.index, err.error[0])?;
Ok(ErrorVariant::Module(ModuleError {
pallet: details.pallet().to_owned(),
error: details.error().to_owned(),
Expand Down
4 changes: 2 additions & 2 deletions crates/cargo-contract/src/cmd/extrinsics/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ use anyhow::Result;
use std::fmt::Write;
use subxt::{
self,
blocks::ExtrinsicEvents,
events::StaticEvent,
tx::TxEvents,
};

/// Field that represent data of an event from invoking a contract extrinsic.
Expand Down Expand Up @@ -79,7 +79,7 @@ pub struct DisplayEvents(Vec<Event>);
impl DisplayEvents {
/// Parses events and returns an object which can be serialised
pub fn from_events(
result: &TxEvents<DefaultConfig>,
result: &ExtrinsicEvents<DefaultConfig>,
transcoder: &ContractMessageTranscoder,
subxt_metadata: &subxt::Metadata,
) -> Result<DisplayEvents> {
Expand Down
68 changes: 36 additions & 32 deletions crates/cargo-contract/src/cmd/extrinsics/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
use super::{
display_contract_exec_result,
prompt_confirm_tx,
runtime_api::{
api,
Weight,
},
runtime_api::api,
state_call,
submit_extrinsic,
Balance,
Expand All @@ -32,6 +29,7 @@ use super::{
DefaultConfig,
ExtrinsicOpts,
PairSigner,
StorageDeposit,
MAX_KEY_COL_WIDTH,
};
use crate::{
Expand All @@ -52,16 +50,14 @@ use anyhow::{
Result,
};

use pallet_contracts_primitives::{
ContractInstantiateResult,
StorageDeposit,
};
use pallet_contracts_primitives::ContractInstantiateResult;

use scale::Encode;
use sp_core::{
crypto::Ss58Codec,
Bytes,
};
use sp_weights::Weight;
use std::{
fs,
path::{
Expand All @@ -70,7 +66,7 @@ use std::{
},
};
use subxt::{
tx::TxEvents,
blocks::ExtrinsicEvents,
Config,
OnlineClient,
};
Expand Down Expand Up @@ -102,6 +98,10 @@ pub struct InstantiateCommand {
/// If not specified will perform a dry-run to estimate the gas consumed for the instantiation.
#[clap(name = "gas", long)]
gas_limit: Option<u64>,
/// Maximum proof size for this instantiation.
/// If not specified will perform a dry-run to estimate the proof size required.
#[clap(long)]
proof_size: Option<u64>,
/// A salt used in the address derivation of the new contract. Use to create multiple instances
/// of the same contract code from the same account.
#[clap(long, value_parser = parse_hex_bytes)]
Expand Down Expand Up @@ -179,7 +179,8 @@ impl InstantiateCommand {
constructor: self.constructor.clone(),
raw_args: self.args.clone(),
value: self.value.denominate_balance(&token_metadata)?,
gas_limit: self.gas_limit.map(Weight::from_ref_time),
gas_limit: self.gas_limit,
proof_size: self.proof_size,
storage_deposit_limit: self
.extrinsic_opts
.storage_deposit_limit
Expand Down Expand Up @@ -210,7 +211,8 @@ struct InstantiateArgs {
constructor: String,
raw_args: Vec<String>,
value: Balance,
gas_limit: Option<Weight>,
gas_limit: Option<u64>,
proof_size: Option<u64>,
storage_deposit_limit: Option<Balance>,
data: Vec<u8>,
salt: Vec<u8>,
Expand Down Expand Up @@ -244,10 +246,10 @@ impl Exec {
result: String::from("Success!"),
contract: ret_val.account_id.to_ss58check(),
reverted: ret_val.result.did_revert(),
data: ret_val.result.data.clone(),
data: ret_val.result.data.clone().into(),
gas_consumed: result.gas_consumed,
gas_required: result.gas_required,
storage_deposit: result.storage_deposit.clone(),
storage_deposit: StorageDeposit::from(&result.storage_deposit),
};
if self.output_json {
println!("{}", dry_run_result.to_json()?);
Expand Down Expand Up @@ -357,7 +359,7 @@ impl Exec {

async fn display_result(
&self,
result: &TxEvents<DefaultConfig>,
result: &ExtrinsicEvents<DefaultConfig>,
code_hash: Option<CodeHash>,
contract_address: sp_core::crypto::AccountId32,
token_metadata: &TokenMetadata,
Expand Down Expand Up @@ -397,15 +399,11 @@ impl Exec {
code: Code,
) -> Result<ContractInstantiateResult<<DefaultConfig as Config>::AccountId, Balance>>
{
let gas_limit = self
.args
.gas_limit
.unwrap_or_else(|| Weight::from_ref_time(5_000_000_000_000));
let storage_deposit_limit = self.args.storage_deposit_limit;
let call_request = InstantiateRequest {
origin: self.signer.account_id().clone(),
value: self.args.value,
gas_limit,
gas_limit: None,
storage_deposit_limit,
code,
data: self.args.data.clone(),
Expand All @@ -417,12 +415,12 @@ impl Exec {
/// Dry run the instantiation before tx submission. Returns the gas required estimate.
async fn pre_submit_dry_run_gas_estimate(&self, code: Code) -> Result<Weight> {
if self.opts.skip_dry_run {
return match self.args.gas_limit {
Some(gas) => Ok(gas),
None => {
return match (self.args.gas_limit, self.args.proof_size) {
(Some(ref_time), Some(proof_size)) => Ok(Weight::from_parts(ref_time, proof_size)),
_ => {
Err(anyhow!(
"Gas limit `--gas` argument required if `--skip-dry-run` specified"
))
"Weight args `--gas` and `--proof-size` required if `--skip-dry-run` specified"
))
}
}
}
Expand All @@ -433,10 +431,16 @@ impl Exec {
match instantiate_result.result {
Ok(_) => {
super::print_gas_required_success(instantiate_result.gas_required);
let gas_limit = self.args.gas_limit.unwrap_or_else(|| {
Weight::from_ref_time(instantiate_result.gas_required)
});
Ok(gas_limit)
// use user specified values where provided, otherwise use the estimates
let ref_time = self
.args
.gas_limit
.unwrap_or_else(|| instantiate_result.gas_required.ref_time());
let proof_size = self
.args
.proof_size
.unwrap_or_else(|| instantiate_result.gas_required.proof_size());
Ok(Weight::from_parts(ref_time, proof_size))
}
Err(ref err) => {
let object =
Expand Down Expand Up @@ -484,10 +488,10 @@ pub struct InstantiateDryRunResult {
/// Was the operation reverted
pub reverted: bool,
pub data: Bytes,
pub gas_consumed: u64,
pub gas_required: u64,
pub gas_consumed: Weight,
pub gas_required: Weight,
/// Storage deposit after the operation
pub storage_deposit: StorageDeposit<Balance>,
pub storage_deposit: StorageDeposit,
}

impl InstantiateDryRunResult {
Expand All @@ -513,7 +517,7 @@ impl InstantiateDryRunResult {
struct InstantiateRequest {
origin: <DefaultConfig as Config>::AccountId,
value: Balance,
gas_limit: Weight,
gas_limit: Option<Weight>,
storage_deposit_limit: Option<Balance>,
code: Code,
data: Vec<u8>,
Expand Down
Loading