Skip to content

Commit

Permalink
ref: new version in adodb publish now should be different, not greate…
Browse files Browse the repository at this point in the history
…r (backport #752) (#766)

Co-authored-by: Joe Monem <[email protected]>
  • Loading branch information
mergify[bot] and joemonem authored Jan 15, 2025
1 parent 54ec7bd commit ebc174d
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Address Validation: Cross-chain recipients don't need to be registered in VFS [(#725)](https://github.com/andromedaprotocol/andromeda-core/pull/725)
- Weighted Splitter: Replace Recipient with AndrAddr in RemoveRecipient and GetUserWeight [(#739)](https://github.com/andromedaprotocol/andromeda-core/pull/739)
- feat: IBC packet tracking adjustments [#748](https://github.com/andromedaprotocol/andromeda-core/pull/748)
- ADODB: Version Validation during Publish ensures different, not greater, version [(#752)](https://github.com/andromedaprotocol/andromeda-core/pull/752)
- ref: Rename Set Amount Splitter to Fixed Amount Splitter [(#754)](https://github.com/andromedaprotocol/andromeda-core/pull/754)
- Point ADO: remove Rates module from the contract[(#761)](https://github.com/andromedaprotocol/andromeda-core/pull/761)

Expand Down
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion contracts/os/andromeda-adodb/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "andromeda-adodb"
version = "1.1.2"
version = "1.1.3-b.1"
authors = ["Connor Barr <[email protected]>"]
edition = "2021"
rust-version = "1.75.0"
Expand Down Expand Up @@ -32,3 +32,6 @@ cw-orch = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
cw-multi-test = { workspace = true, optional = true }

[dev-dependencies]
rstest = { workspace = true }
18 changes: 2 additions & 16 deletions contracts/os/andromeda-adodb/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::state::{
read_code_id, remove_code_id, save_action_fees, store_code_id, ACTION_FEES, ADO_TYPE,
LATEST_VERSION, PUBLISHER, UNPUBLISHED_CODE_IDS, UNPUBLISHED_VERSIONS,
PUBLISHER, UNPUBLISHED_CODE_IDS, UNPUBLISHED_VERSIONS,
};

use andromeda_std::ado_contract::ADOContract;
Expand Down Expand Up @@ -43,25 +43,13 @@ pub fn publish(
msg: Some("ado_type can't be an empty string".to_string())
}
);
let current_ado_version = LATEST_VERSION.may_load(deps.storage, &ado_type)?;

ensure!(
semver::Version::parse(&version).is_ok(),
ContractError::InvalidADOVersion {
msg: Some("Provided version is not valid semver".to_string())
}
);
let new_version = semver::Version::parse(&version).unwrap();
if let Some(ado_version) = current_ado_version {
let current_version = semver::Version::parse(&ado_version.0).unwrap();
ensure!(
new_version > current_version,
ContractError::InvalidADOVersion {
msg: Some("Version must be newer than the current version".to_string())
}
);
}

//TODO: Get Code ID info with cosmwasm 1.2

let version = ADOVersion::from_type(ado_type).with_version(version);
ensure!(
Expand Down Expand Up @@ -123,8 +111,6 @@ pub fn unpublish(
}
);

//TODO: Get Code ID info with cosmwasm 1.2

let ado_version = ADOVersion::from_type(ado_type.clone()).with_version(version.clone());
ensure!(
ado_version.validate(),
Expand Down
48 changes: 31 additions & 17 deletions contracts/os/andromeda-adodb/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use andromeda_std::{
};
use cosmwasm_std::{ensure, Api, StdResult, Storage};
use cw_storage_plus::Map;
use semver::Version;

/// Stores a mapping from an ADO type/version to its code ID
pub const CODE_ID: Map<&str, u64> = Map::new("code_id");
Expand All @@ -27,29 +28,35 @@ pub fn store_code_id(
) -> Result<(), ContractError> {
let curr_type = ADO_TYPE.may_load(storage, &code_id.to_string())?;
ensure!(
curr_type.is_none() || &curr_type.unwrap() == ado_version,
curr_type.is_none()
|| &curr_type.unwrap_or(ADOVersion::from_string(String::default())) == ado_version,
ContractError::Unauthorized {}
);
ADO_TYPE
.save(storage, &code_id.to_string(), ado_version)
.unwrap();
ADO_TYPE.save(storage, &code_id.to_string(), ado_version)?;
let version = semver::Version::parse(&ado_version.get_version())
.ok()
.ok_or(ContractError::InvalidADOVersion { msg: None })?;
let prerelease = version.pre.parse::<String>().unwrap_or_default();

if prerelease.is_empty() {
LATEST_VERSION
.save(
let current_ado_version = LATEST_VERSION.may_load(storage, &ado_version.get_type())?;

let should_update = current_ado_version
.map(|(ver, _)| {
let current_version = semver::Version::parse(&ver).unwrap_or(Version::new(0, 0, 0));
version > current_version
})
.unwrap_or(true); // If there's no current version, we should update

if should_update {
LATEST_VERSION.save(
storage,
&ado_version.get_type(),
&(ado_version.get_version(), code_id),
)
.unwrap();
)?;
}
}
CODE_ID
.save(storage, ado_version.as_str(), &code_id)
.unwrap();

CODE_ID.save(storage, ado_version.as_str(), &code_id)?;
Ok(())
}

Expand All @@ -60,7 +67,8 @@ pub fn remove_code_id(
) -> Result<(), ContractError> {
let curr_type = ADO_TYPE.may_load(storage, &code_id.to_string())?;
ensure!(
curr_type.is_none() || &curr_type.unwrap() == ado_version,
curr_type.is_none()
|| &curr_type.unwrap_or(ADOVersion::from_string(String::default())) == ado_version,
ContractError::Unauthorized {}
);
ADO_TYPE.remove(storage, &code_id.to_string());
Expand All @@ -69,17 +77,23 @@ pub fn remove_code_id(
// This means that the code_id we're trying to unpublish is also the latest
if version_code.1 == code_id {
let mut penultimate_version = semver::Version::new(0, 0, 0);
let latest_version = semver::Version::parse(&ado_version.get_version()).unwrap();
let latest_version =
semver::Version::parse(&ado_version.get_version()).unwrap_or(Version::new(0, 0, 0));
CODE_ID
.keys(storage, None, None, cosmwasm_std::Order::Descending)
.map(|v| v.unwrap())
.map(|v| v.unwrap_or_default())
// Filter out the keys by type first
.filter(|v| v.starts_with(&ado_version.get_type()))
// We want to get the penultimate version, since it will replace the latest version
.for_each(|v| {
if let Some((_, version)) = v.split_once('@') {
let current_version = semver::Version::parse(version).unwrap();
if penultimate_version < current_version && current_version < latest_version
let current_version =
semver::Version::parse(version).unwrap_or(Version::new(0, 0, 0));

let prerelease = current_version.pre.parse::<String>().unwrap_or_default();
if prerelease.is_empty()
&& penultimate_version < current_version
&& current_version < latest_version
{
penultimate_version = current_version;
};
Expand Down
88 changes: 88 additions & 0 deletions contracts/os/andromeda-adodb/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use andromeda_std::error::ContractError;
use andromeda_std::os::adodb::{ADOVersion, ActionFee, ExecuteMsg, InstantiateMsg, QueryMsg};

use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use rstest::rstest;

#[test]
fn proper_initialization() {
Expand Down Expand Up @@ -747,3 +748,90 @@ fn test_all_ado_types() {
];
assert_eq!(value, expected);
}

#[rstest]
#[case("1.0.0-b.1", "1.0.0-b.2", false, true)] // Beta version increment
#[case("1.0.0-b.1", "1.0.1", true, true)] // Patch increment from beta
#[case("1.0.0-b.1", "1.0.1-b.1", false, true)] // Patch increment with beta
#[case("1.0.0-b.1", "1.1.0", true, true)] // Minor increment from beta
#[case("1.0.0-b.1", "1.1.0-b.1", false, true)] // Minor increment with beta
#[case("1.0.0-b.1", "2.0.0", true, true)] // Major increment from beta
#[case("1.0.0-b.1", "2.0.0-b.1", false, true)] // Major increment with beta
#[case("1.0.0-b.2", "1.0.0-b.1", false, true)] // Lower beta version
#[case("1.0.0", "1.0.1", true, false)] // Regular patch increment
#[case("1.0.0", "1.0.1-b.1", false, false)] // Regular to beta patch
#[case("1.0.0", "1.1.0", true, false)] // Regular minor increment
#[case("1.0.0", "1.1.0-b.1", false, false)] // Regular to beta minor
#[case("1.0.0", "2.0.0", true, false)] // Regular major increment
#[case("1.0.0", "2.0.0-b.1", false, false)] // Regular to beta major
#[case("1.1.0", "1.0.0", false, false)] // Lower version
#[case("1.1.0", "1.1.0-b.1", false, false)] // Regular to beta same version
fn test_version_updates(
#[case] initial_version: &str,
#[case] new_version: &str,
#[case] should_update_latest_version: bool,
#[case] initial_is_beta: bool,
) {
let owner = String::from("owner");
let mut deps = mock_dependencies_custom(&[]);
let env = mock_env();
let info = mock_info(&owner, &[]);

// Initialize contract
instantiate(
deps.as_mut(),
mock_env(),
mock_info(&owner, &[]),
InstantiateMsg {
kernel_address: MOCK_KERNEL_CONTRACT.to_string(),
owner: None,
},
)
.unwrap();

// Publish initial version
let ado_version = ADOVersion::from_type("test_ado").with_version(initial_version);
let msg = ExecuteMsg::Publish {
ado_type: ado_version.get_type(),
version: ado_version.get_version(),
code_id: 1,
action_fees: None,
publisher: Some(owner.clone()),
};
execute(deps.as_mut(), env.clone(), info.clone(), msg).unwrap();

// Publish new version
let new_ado_version = ADOVersion::from_type("test_ado").with_version(new_version);
let msg = ExecuteMsg::Publish {
ado_type: new_ado_version.get_type(),
version: new_ado_version.get_version(),
code_id: 2,
action_fees: None,
publisher: Some(owner.clone()),
};
execute(deps.as_mut(), env.clone(), info, msg).unwrap();

if should_update_latest_version {
let latest = LATEST_VERSION
.load(deps.as_ref().storage, "test_ado")
.unwrap();
assert_eq!(latest.0, new_version);
assert_eq!(latest.1, 2u64);
} else if initial_is_beta {
let latest = LATEST_VERSION
.may_load(deps.as_ref().storage, "test_ado")
.unwrap();
assert!(latest.is_none());
} else {
let latest = LATEST_VERSION
.load(deps.as_ref().storage, "test_ado")
.unwrap();
assert_eq!(latest.0, initial_version);
assert_eq!(latest.1, 1u64);
}

let code_id = CODE_ID
.load(deps.as_ref().storage, new_ado_version.as_str())
.unwrap();
assert_eq!(code_id, 2u64);
}
2 changes: 1 addition & 1 deletion packages/andromeda-testing-e2e/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ anyhow = "1.0.95"
andromeda-non-fungible-tokens = { workspace = true }
andromeda-app = { version = "1.0.0", path = "../andromeda-app" }
andromeda-modules = { version = "2.0.0", path = "../andromeda-modules" }
andromeda-adodb = { version = "1.0.0", path = "../../contracts/os/andromeda-adodb", features = [
andromeda-adodb = { version = "1.1.3-b.1", path = "../../contracts/os/andromeda-adodb", features = [
"testing",
] }
andromeda-kernel = { version = "1.2.0-b.1", path = "../../contracts/os/andromeda-kernel", features = [
Expand Down
2 changes: 1 addition & 1 deletion packages/andromeda-testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ anyhow = "1.0.95"
andromeda-non-fungible-tokens = { workspace = true }
andromeda-app = { version = "1.0.0", path = "../andromeda-app" }
andromeda-modules = { version = "2.0.0", path = "../andromeda-modules" }
andromeda-adodb = { version = "1.0.0", path = "../../contracts/os/andromeda-adodb", features = [
andromeda-adodb = { version = "1.1.3-b.1", path = "../../contracts/os/andromeda-adodb", features = [
"testing",
] }
andromeda-kernel = { version = "1.2.0-b.1", path = "../../contracts/os/andromeda-kernel", features = [
Expand Down
2 changes: 1 addition & 1 deletion packages/deploy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ cw-orch-daemon = "0.24.2"

# OS Contracts
andromeda-kernel = { version = "1.2.0-b.1", path = "../../contracts/os/andromeda-kernel" }
andromeda-adodb = { version = "1.1.1", path = "../../contracts/os/andromeda-adodb" }
andromeda-adodb = { version = "1.1.3-b.1", path = "../../contracts/os/andromeda-adodb" }
andromeda-vfs = { version = "1.1.1", path = "../../contracts/os/andromeda-vfs" }
andromeda-economics = { version = "1.1.1", path = "../../contracts/os/andromeda-economics" }
andromeda-ibc-registry = { version = "1.0.1", path = "../../contracts/os/andromeda-ibc-registry" }
Expand Down

0 comments on commit ebc174d

Please sign in to comment.