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

Trace proposal's code_id #70

Merged
merged 4 commits into from
Jul 8, 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
5 changes: 4 additions & 1 deletion packages/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@ base64 = "0.13.0"
cargo-generate = {version = "0.15.2", features = ["vendored-openssl"]}
clap = {version = "3.2.5", features = ["derive"]}
config = {version = "0.13.1", features = ["preserve_order"]}
console = "0.15.0"
cosmos-sdk-proto = {version = "0.12.3", features = ["cosmwasm"]}
cosmrs = {version = "0.7.1", features = ["dev", "cosmwasm"]}
data_doc = {path = "../data_doc"}
data_doc_derive = {path = "../data_doc_derive"}
derive-new = "0.5.9"
dialoguer = "0.10.1"
getset = "0.1.2"
prost = "0.10.3"
regex = "1.5.6"
serde = "1.0.137"
serde_json = "1.0.81"
serde_yaml = "0.8"
tendermint-rpc = "0.23.7"
tokio = {version = "1.18.2", features = ["full"]}
toml = "0.5.9"

[dev_dependencies]
[dev-dependencies]
assert_fs = "1.0.7"
cargo_toml = "0.11.5"
predicates = "2.1.1"
Expand Down
24 changes: 24 additions & 0 deletions packages/cli/src/modules/wasm/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ pub enum WasmCmd {
#[clap(short, long)]
funds: Option<String>,

/// Skip the check for proposal's updated code_id
#[clap(long)]
no_proposal_sync: bool,

/// Agree to all prompts
#[clap(short, long)]
yes: bool,

#[clap(flatten)]
base_tx_args: BaseTxArgs,
},
Expand All @@ -80,6 +88,14 @@ pub enum WasmCmd {
#[clap(short, long)]
raw: Option<String>,

/// Skip the check for proposal's updated code_id
#[clap(long)]
no_proposal_sync: bool,

/// Agree to all prompts
#[clap(short, long)]
yes: bool,

#[clap(flatten)]
base_tx_args: BaseTxArgs,
},
Expand Down Expand Up @@ -198,6 +214,8 @@ impl<'a> Module<'a, WasmConfig, WasmCmd, anyhow::Error> for WasmModule {
label,
raw,
admin,
no_proposal_sync,
yes,
funds,
base_tx_args,
} => {
Expand All @@ -213,6 +231,8 @@ impl<'a> Module<'a, WasmConfig, WasmCmd, anyhow::Error> for WasmModule {
label.as_str(),
raw.as_ref(),
admin.as_ref(),
*no_proposal_sync,
*yes,
funds.as_ref().map(|s| s.as_str()).try_into()?,
network,
timeout_height,
Expand All @@ -232,6 +252,8 @@ impl<'a> Module<'a, WasmConfig, WasmCmd, anyhow::Error> for WasmModule {
contract_name,
label,
raw,
no_proposal_sync,
yes,
base_tx_args,
} => {
let BaseTxArgs {
Expand All @@ -245,6 +267,8 @@ impl<'a> Module<'a, WasmConfig, WasmCmd, anyhow::Error> for WasmModule {
contract_name,
label.as_str(),
raw.as_ref(),
*no_proposal_sync,
*yes,
network,
timeout_height,
{
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/modules/wasm/ops/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub fn deploy<'a, Ctx: Context<'a, WasmConfig>>(
label,
raw,
admin,
// deploy command is not intended to use with the gov process
true,
true,
funds,
network,
timeout_height,
Expand Down
20 changes: 15 additions & 5 deletions packages/cli/src/modules/wasm/ops/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::support::coin::Coins;
use crate::support::cosmos::ResponseValuePicker;
use crate::support::future::block;
use crate::support::gas::Gas;
use crate::support::hooks::use_code_id;
use crate::support::ops_response::OpResponseDisplay;
use crate::support::state::State;
use crate::{framework::Context, support::cosmos::Client};
Expand All @@ -13,7 +14,8 @@ use anyhow::Result;
use cosmrs::cosmwasm::MsgInstantiateContract;
use cosmrs::crypto::secp256k1::SigningKey;
use cosmrs::tx::Msg;
use std::fs;

use std::{fs, vec};

#[allow(clippy::too_many_arguments)]
pub fn instantiate<'a, Ctx: Context<'a, WasmConfig>>(
Expand All @@ -22,6 +24,8 @@ pub fn instantiate<'a, Ctx: Context<'a, WasmConfig>>(
label: &str,
raw: Option<&String>,
admin: Option<&String>,
no_proposal_sync: bool,
yes: bool,
funds: Coins,
network: &str,
timeout_height: &u32,
Expand All @@ -40,10 +44,16 @@ pub fn instantiate<'a, Ctx: Context<'a, WasmConfig>>(
let client = Client::new(network_info.clone()).to_signing_client(signing_key, account_prefix);

let state = State::load_by_network(network_info.clone(), ctx.root()?)?;
let code_id = state
.get_ref(network, contract_name)?
.code_id()
.with_context(|| format!("Unable to retrieve code_id for {contract_name}"))?;

let code_id = use_code_id(
ctx,
network,
&network_info,
state,
contract_name,
no_proposal_sync,
yes,
)?;

let msg_instantiate_contract = MsgInstantiateContract {
sender: client.signer_account_id(),
Expand Down
16 changes: 12 additions & 4 deletions packages/cli/src/modules/wasm/ops/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::modules::wasm::config::WasmConfig;
use crate::support::cosmos::ResponseValuePicker;
use crate::support::future::block;
use crate::support::gas::Gas;
use crate::support::hooks::use_code_id;
use crate::support::ops_response::OpResponseDisplay;
use crate::support::state::State;
use crate::{framework::Context, support::cosmos::Client};
Expand All @@ -21,6 +22,8 @@ pub fn migrate<'a, Ctx: Context<'a, WasmConfig>>(
contract_name: &str,
label: &str,
raw: Option<&String>,
no_proposal_sync: bool,
yes: bool,
network: &str,
timeout_height: &u32,
gas: &Gas,
Expand All @@ -38,10 +41,15 @@ pub fn migrate<'a, Ctx: Context<'a, WasmConfig>>(
let client = Client::new(network_info.clone()).to_signing_client(signing_key, account_prefix);

let state = State::load_by_network(network_info.clone(), ctx.root()?)?;
let code_id = state
.get_ref(network, contract_name)?
.code_id()
.with_context(|| format!("Unable to retrieve code_id for {contract_name}"))?;
let code_id = use_code_id(
ctx,
network,
&network_info,
state.clone(),
contract_name,
no_proposal_sync,
yes,
)?;

let contract = state
.get_ref(network, contract_name)?
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/modules/wasm/ops/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub fn upgrade<'a, Ctx: Context<'a, WasmConfig>>(
contract_name,
label,
raw,
// upgrade command is not intended to use with the gov process
true,
true,
network,
timeout_height,
gas,
Expand Down
88 changes: 88 additions & 0 deletions packages/cli/src/support/hooks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use super::{future::block, state::State, wasm::get_code_id};
use crate::{framework::config::Network, Context, WasmConfig};
use anyhow::Context as _;
use console::style;
use dialoguer::Confirm;

pub fn use_code_id<'a, Ctx: Context<'a, WasmConfig>>(
ctx: &Ctx,
network: &str,
network_info: &Network,
state: State,
contract_name: &str,
no_proposal_sync: bool,
yes: bool,
) -> Result<u64, anyhow::Error> {
let code_id = block(async {
let wasm_ref = state.get_ref(network, contract_name)?;

let current_code_id = wasm_ref
.code_id()
.with_context(|| format!("Unable to retrieve code_id for {contract_name}"))?;

if no_proposal_sync {
return anyhow::Ok(current_code_id);
}

let proposal_id = wasm_ref
.proposal()
.store_code()
.with_context(|| format!( "Proposal store code not found for contract `{contract_name}` on network `{network}`"))?;

match get_code_id(network_info.rpc_endpoint(), &proposal_id).await {
Ok(code_id_from_proposal) => {
let code_id_from_proposal = code_id_from_proposal.parse().with_context(|| {
format!(
"unable to parse code_id from proposal: {}",
code_id_from_proposal
)
})?;

// code_id from proposal found but no new update
if code_id_from_proposal == current_code_id {
anyhow::Ok(current_code_id)
} else {
println!();
println!(
" Found updated {} from proposal with {} {} :",
style("code_id").bold(),
style("proposal_id").bold(),
proposal_id
);
println!();
println!(
"{}",
style(format!(
" {}: {} → {}",
style("~ code_id").yellow(),
style(current_code_id).red(),
style(code_id_from_proposal).green()
))
.yellow()
);

println!();

if yes
|| Confirm::new()
.with_prompt("> Do you want to update `code_id`?")
.interact()?
{
State::update_state_file(
network_info.network_variant(),
ctx.root()?,
&|s: &State| -> State {
s.update_code_id(network, contract_name, &code_id_from_proposal)
},
)?;
anyhow::Ok(code_id_from_proposal)
} else {
anyhow::Ok(current_code_id)
}
}
}
Err(_) => anyhow::Ok(current_code_id),
}
})?;
Ok(code_id)
}
1 change: 1 addition & 0 deletions packages/cli/src/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod coin;
pub mod cosmos;
pub mod future;
pub mod gas;
pub mod hooks;
pub mod ops_response;
pub mod proto;
pub mod signer;
Expand Down
Loading