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

Add flag --all to cargo contract info #1319

Merged
merged 11 commits into from
Sep 12, 2023
Merged

Add flag --all to cargo contract info #1319

merged 11 commits into from
Sep 12, 2023

Conversation

smiasojed
Copy link
Collaborator

@smiasojed smiasojed commented Sep 7, 2023

Closes #797

  • Output for command cargo info -all is:
5DpyotvcUDkFxTtHzkwR7Rqe37bdjc6M5Rxeiy57Gv2RQzp8
5GsoMKLTgFEpzHQTi7c4ZKCLZC6VxtHZxwMydSi79uemABez
  • Flag combination --all --output-json outputs:
{
  "contracts": [
    "5DpyotvcUDkFxTtHzkwR7Rqe37bdjc6M5Rxeiy57Gv2RQzp8",
    "5GsoMKLTgFEpzHQTi7c4ZKCLZC6VxtHZxwMydSi79uemABez"
  ]
}
  • Flags combination --binary --all is forbidden

  • Current implementation does not support response paging

// Binary flag applied
(_, true) => {
let wasm_code =
fetch_wasm_code(*info_to_json.code_hash(), &client).await?;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: using ok_or may simplify code below by removing match.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

crates/cargo-contract/src/cmd/info.rs Outdated Show resolved Hide resolved
crates/cargo-contract/src/cmd/info.rs Outdated Show resolved Hide resolved
crates/cargo-contract/src/cmd/info.rs Outdated Show resolved Hide resolved
crates/cargo-contract/src/cmd/info.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@SkymanOne SkymanOne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Just a few nitpicks and changes mainly to do with the coding style

// All flag applied
if self.all {
// 1000 is max allowed value
const COUNT: u32 = 1000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
const COUNT: u32 = 1000;
const MAX_COUNT: u32 = 1000;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

);
pub async fn run(&self) -> Result<(), ErrorVariant> {
let url = self.url.clone();
let client = OnlineClient::<DefaultConfig>::from_url(url).await?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let client = OnlineClient::<DefaultConfig>::from_url(url).await?;
let client = OnlineClient::<DefaultConfig>::from_url(&self.url).await?;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

self.contract
);
pub async fn run(&self) -> Result<(), ErrorVariant> {
let url = self.url.clone();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let url = self.url.clone();

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -378,6 +384,42 @@ pub async fn fetch_wasm_code(hash: CodeHash, client: &Client) -> Result<Option<V
Ok(pristine_bytes)
}

/// Fetch all contracts addresses from the storage using the provided client and count of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Fetch all contracts addresses from the storage using the provided client and count of
/// Fetch all contract addresses from the storage using the provided client and count of

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

pub async fn fetch_all_contracts(
client: &Client,
count: u32,
from: Option<&AccountId32>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from: Option<&AccountId32>,
count_from: Option<&AccountId32>,

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if self.all {
// 1000 is max allowed value
const COUNT: u32 = 1000;
let mut from = None;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut from = None;
let mut count_from = None;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

.fetch_keys(key.as_ref(), count, start_key.as_deref())
.await?;

// StorageKey is a concatention of contract_info_of root key and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// StorageKey is a concatention of contract_info_of root key and
// StorageKey is a concatenation of contract_info_of root key and

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 413 to 416
e.0.get(key.len() + 8..)
.ok_or(anyhow!("Unexpected storage key size"))?;
AccountId32::decode(&mut account)
.map_err(|err| anyhow!("AccountId deserialization error: {}", err))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would extract it into a separate function for better readability

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 238 to 244
pub fn display_all_contracts(contracts: &[<DefaultConfig as Config>::AccountId]) {
contracts
.iter()
.for_each(|e: &<DefaultConfig as Config>::AccountId| {
name_value_println!("Contract", format!("{}", e), MAX_KEY_COL_WIDTH);
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rewrite it to display the result something like:

Found contracts:
    * <Address>
    * <Address>
    * <Address>

Copy link
Collaborator Author

@smiasojed smiasojed Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that in case when we have more than 1000 contracts, it will be hard to see this line Found contracts at the log beginning. I would opt to keep it as is. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I would enumerate them at least. Useful when you want to pipe the output into another bash command:

Found contracts:
    1. <Address>
    2. <Address>
    ...
    1000. <Address>

It is up to you if you want to keep Found contracts: or not.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful when you want to pipe the output into another bash command

In this case it is best then to have only the list of contract addresses so instead of

        Contract 5DpyotvcUDkFxTtHzkwR7Rqe37bdjc6M5Rxeiy57Gv2RQzp8
        Contract 5GsoMKLTgFEpzHQTi7c4ZKCLZC6VxtHZxwMydSi79uemABez

have

5DpyotvcUDkFxTtHzkwR7Rqe37bdjc6M5Rxeiy57Gv2RQzp8
5GsoMKLTgFEpzHQTi7c4ZKCLZC6VxtHZxwMydSi79uemABez

I like it this way, unix command compatible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

@SkymanOne SkymanOne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks

@ascjones ascjones merged commit 5173489 into master Sep 12, 2023
10 checks passed
@ascjones ascjones deleted the sm/contract-all branch September 12, 2023 11:55
@smiasojed smiasojed mentioned this pull request Nov 30, 2023
@smiasojed smiasojed mentioned this pull request Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add flag --all to cargo contract info
4 participants