Skip to content

Commit

Permalink
feat: rework view methods for getting deployment info (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksuss authored Dec 4, 2024
1 parent ef318e9 commit 63fb837
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 29 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,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 contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use near_sdk::{
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 @@ impl AuroraControllerFactory {
}
}

/// 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> {
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 contract/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 contract/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 contract/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

0 comments on commit 63fb837

Please sign in to comment.