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

feat: rework view methods for getting deployment info #13

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ fn get_latest_release_hash(&self) -> String;
fn get_latest_release_blob(&self) -> Vec<u8>;

/// Returns a list of existing contract deployments.
fn get_deployments(&self) -> Vec<DeploymentInfo>;
fn get_deployments(&self) -> BTreeMap<AccountId, DeploymentInfo>;

/// Returns a contract deployment info for corresponding account id.
fn get_deployment(&self, account_id: AccountId) -> Option<DeploymentInfo>;
```

#### Callback
Expand Down
16 changes: 13 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
assert_one_yocto, env, ext_contract, near, require, AccountId, Gas, NearToken, PanicOnDefault,
Promise, PromiseResult, PublicKey,
};
use std::collections::BTreeMap;

use crate::event::Event;
use crate::types::{DeploymentInfo, FunctionCallArgs, ReleaseInfo, UpgradeArgs, Version};
Expand Down Expand Up @@ -396,10 +397,19 @@
}
}

/// Returns a list of existing contract deployments.
/// Returns a list of existing contract deployment infos.
#[must_use]
pub fn get_deployments(&self) -> Vec<DeploymentInfo> {
self.deployments.values().cloned().collect()
pub fn get_deployments(&self) -> BTreeMap<AccountId, DeploymentInfo> {
self.deployments
.iter()
.map(|(acc, info)| (acc.clone(), info.clone()))
.collect()
}

/// Returns a contract deployment info for corresponding account id.
#[must_use]
pub fn get_deployment(&self, account_id: AccountId) -> Option<DeploymentInfo> {

Check failure on line 411 in src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this argument is passed by value, but not consumed in the function body

error: this argument is passed by value, but not consumed in the function body --> src/lib.rs:411:46 | 411 | pub fn get_deployment(&self, account_id: AccountId) -> Option<DeploymentInfo> { | ^^^^^^^^^ help: consider taking a reference instead: `&AccountId` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value = note: `-D clippy::needless-pass-by-value` implied by `-D clippy::pedantic` = help: to override `-D clippy::pedantic` add `#[allow(clippy::needless_pass_by_value)]`
self.deployments.get(&account_id).cloned()
}

/// Upgrades a contract with account id and provided or the latest hash.
Expand Down
42 changes: 24 additions & 18 deletions src/tests/workspace/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async fn test_deploy_contract() {
.unwrap();
assert!(result.is_success(), "{result:#?}");

let deployments: Vec<DeploymentInfo> = factory_owner
let deployments: BTreeMap<AccountId, DeploymentInfo> = factory_owner
.view(factory.id(), "get_deployments")
.await
.unwrap()
Expand Down Expand Up @@ -194,7 +194,7 @@ async fn test_deploy_more_than_one_contract() {
.unwrap()
.timestamp();

let deployments: Vec<DeploymentInfo> = factory_owner
let deployments: BTreeMap<AccountId, DeploymentInfo> = factory_owner
.view(factory.id(), "get_deployments")
.await
.unwrap()
Expand All @@ -203,22 +203,28 @@ async fn test_deploy_more_than_one_contract() {
assert_eq!(deployments.len(), 2);
assert_eq!(
deployments,
vec![
DeploymentInfo {
hash: HASH_3_6_4.to_string(),
version: "3.6.4".parse().unwrap(),
deployment_time: deploy_time_1,
upgrade_times: [(deploy_time_1, "3.6.4".parse().unwrap())].into(),
init_args: near_sdk::serde_json::to_string(&init_args_1).unwrap(),
},
DeploymentInfo {
hash: HASH_3_7_0.to_string(),
version: "3.7.0".parse().unwrap(),
deployment_time: deploy_time_2,
upgrade_times: [(deploy_time_2, "3.7.0".parse().unwrap())].into(),
init_args: near_sdk::serde_json::to_string(&init_args_2).unwrap(),
}
]
BTreeMap::from_iter([
(
"aurora-1.factory-owner.test.near".parse().unwrap(),
DeploymentInfo {
hash: HASH_3_6_4.to_string(),
version: "3.6.4".parse().unwrap(),
deployment_time: deploy_time_1,
upgrade_times: [(deploy_time_1, "3.6.4".parse().unwrap())].into(),
init_args: near_sdk::serde_json::to_string(&init_args_1).unwrap(),
}
),
(
"aurora-2.factory-owner.test.near".parse().unwrap(),
DeploymentInfo {
hash: HASH_3_7_0.to_string(),
version: "3.7.0".parse().unwrap(),
deployment_time: deploy_time_2,
upgrade_times: [(deploy_time_2, "3.7.0".parse().unwrap())].into(),
init_args: near_sdk::serde_json::to_string(&init_args_2).unwrap(),
}
)
])
);
}

Expand Down
15 changes: 10 additions & 5 deletions src/tests/workspace/downgrade.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use near_sdk::serde_json::json;
use near_workspaces::types::NearToken;
use near_workspaces::AccountId;
use std::collections::BTreeMap;

use super::utils;
use crate::tests::{BLOB_3_6_4, BLOB_3_7_0, HASH_3_6_4, HASH_3_7_0};
Expand Down Expand Up @@ -80,14 +81,17 @@ async fn test_downgrade_contract() {
.unwrap();
assert!(result.is_success());

let deployments: Vec<DeploymentInfo> = factory_owner
let deployments: BTreeMap<AccountId, DeploymentInfo> = factory_owner
.view(factory.id(), "get_deployments")
.await
.unwrap()
.json()
.unwrap();

assert_eq!(deployments[0].version, "3.7.0".parse().unwrap());
assert_eq!(
deployments[&new_contract_id].version,
"3.7.0".parse().unwrap()
);

let result = factory_owner
.call(factory.id(), "downgrade")
Expand All @@ -104,12 +108,13 @@ async fn test_downgrade_contract() {
let version = String::from_utf8(result.unwrap().result).unwrap();
assert_eq!(version.trim_end(), "3.6.4");

let deployments: Vec<DeploymentInfo> = factory_owner
.view(factory.id(), "get_deployments")
let deployments: DeploymentInfo = factory_owner
.view(factory.id(), "get_deployment")
.args_json(json!({ "account_id": new_contract_id }))
.await
.unwrap()
.json()
.unwrap();

assert_eq!(deployments[0].version, "3.6.4".parse().unwrap());
assert_eq!(deployments.version, "3.6.4".parse().unwrap());
}
5 changes: 3 additions & 2 deletions src/tests/workspace/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use near_sdk::serde_json::json;
use near_workspaces::types::NearToken;
use near_workspaces::AccountId;
use std::collections::BTreeMap;

use super::utils;
use crate::tests::{BLOB_3_6_4, BLOB_3_7_0, HASH_3_6_4, HASH_3_7_0, MIGRATION_GAS};
Expand Down Expand Up @@ -316,7 +317,7 @@ async fn test_upgrade_contract_with_small_gas_for_migration() {
.unwrap();
assert!(result.is_success(), "{result:#?}");

let deployments_info: Vec<DeploymentInfo> = factory_owner
let deployments_info: BTreeMap<AccountId, DeploymentInfo> = factory_owner
.view(factory.id(), "get_deployments")
.await
.unwrap()
Expand Down Expand Up @@ -363,7 +364,7 @@ async fn test_upgrade_contract_with_small_gas_for_migration() {
// So, the upgrade won't be successful.

// Check that the deployment into hasn't been changed.
let result: Vec<DeploymentInfo> = factory_owner
let result: BTreeMap<AccountId, DeploymentInfo> = factory_owner
.view(factory.id(), "get_deployments")
.await
.unwrap()
Expand Down
Loading