Skip to content

Commit

Permalink
Fix #261 Failure on migration when EventsMode::CES to EventsMode::CES (
Browse files Browse the repository at this point in the history
…#263)

* Version bump 1.5.1 + tests

* Fix #261 Failure on migration when EventsMode::CES to EventsMode::CES

* Add and remove some tests

* small change

* small change
  • Loading branch information
gRoussac authored Jan 4, 2024
1 parent e7bcbfb commit ab6e697
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 104 deletions.
8 changes: 3 additions & 5 deletions contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ version = "1.5.1"
edition = "2018"

[dependencies]
casper-contract = { version = "3.0.0", default-features = false, features = [
"test-support",
], optional = true }
casper-types = "3.0.0"
casper-contract = { version = "3.0.0", default-features = false, optional = true }
casper-types = { version = "3.0.0", default-features = false }
serde = { version = "1.0.80", default-features = false }
serde_json = { version = "1.0.59", default-features = false }
serde-json-wasm = { version = "0.5.1", default-features = false }
base16 = { version = "0.2.1", default-features = false }
casper-event-standard = { version = "0.4.0", default-features = false }
casper-event-standard = { version = "0.4.1", default-features = false }
hex = { version = "0.4.3", default-features = false }

[[bin]]
Expand Down
62 changes: 42 additions & 20 deletions contract/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2001,27 +2001,50 @@ pub extern "C" fn migrate() {

runtime::put_key(RLO_MFLAG, storage::new_uref(false).into());

let events_mode: EventsMode = utils::get_optional_named_arg_with_user_errors::<u8>(
ARG_EVENTS_MODE,
NFTCoreError::InvalidEventsMode,
)
.unwrap_or(EventsMode::NoEvents as u8)
.try_into()
.unwrap_or_revert();

match events_mode {
EventsMode::NoEvents => {}
EventsMode::CES => {
// Initialize events structures.
utils::init_events();
// Emit Migration event.
casper_event_standard::emit(Migration::new());
let optional_events_mode: Option<u8> = runtime::get_named_arg::<Option<u8>>(ARG_EVENTS_MODE);
let current_events_mode: EventsMode = runtime::get_key(EVENTS_MODE)
.and_then(|_| {
utils::get_stored_value_with_user_errors::<u8>(
EVENTS_MODE,
NFTCoreError::MissingEventsMode,
NFTCoreError::InvalidEventsMode,
)
.try_into()
.ok()
})
.unwrap_or(EventsMode::NoEvents);

if let Some(optional_events_mode) = optional_events_mode {
let requested_events_mode: EventsMode = optional_events_mode
.try_into()
.unwrap_or_revert_with(NFTCoreError::InvalidEventsMode);
match (current_events_mode, requested_events_mode) {
(EventsMode::CES, EventsMode::CES) => casper_event_standard::emit(Migration::new()),
(_, EventsMode::CES) => {
// Initialize events structures.
utils::init_events();
casper_event_standard::emit(Migration::new());
}
(_, EventsMode::CEP47) => record_cep47_event_dictionary(CEP47Event::Migrate),
(_, _) => {}
}
runtime::put_key(EVENTS_MODE, storage::new_uref(optional_events_mode).into());
} else {
match current_events_mode {
EventsMode::CEP47 => record_cep47_event_dictionary(CEP47Event::Migrate),
EventsMode::CES => casper_event_standard::emit(Migration::new()),
_ => {
// Store "no events" mode in case it was never stored like version < 1.2
if !runtime::has_key(EVENTS_MODE) {
runtime::put_key(
EVENTS_MODE,
storage::new_uref(EventsMode::NoEvents as u8).into(),
)
}
}
}
EventsMode::CEP47 => record_cep47_event_dictionary(CEP47Event::Migrate),
}

runtime::put_key(EVENTS_MODE, storage::new_uref(events_mode as u8).into());

let acl_package_mode: bool = utils::get_optional_named_arg_with_user_errors::<bool>(
ARG_ACL_PACKAGE_MODE,
NFTCoreError::InvalidACLPackageMode,
Expand Down Expand Up @@ -2823,8 +2846,7 @@ fn migrate_contract(access_key_name: String, package_key_name: String) {
let events_mode = utils::get_optional_named_arg_with_user_errors::<u8>(
ARG_EVENTS_MODE,
NFTCoreError::InvalidEventsMode,
)
.unwrap_or(0u8);
);

let acl_package_mode: bool = utils::get_optional_named_arg_with_user_errors(
ARG_ACL_PACKAGE_MODE,
Expand Down
2 changes: 1 addition & 1 deletion contract/src/modalities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ impl TryFrom<u8> for NamedKeyConventionMode {
}

#[repr(u8)]
#[derive(PartialEq, Eq)]
#[derive(PartialEq, Eq, Clone, Copy)]
#[allow(clippy::upper_case_acronyms)]
pub enum EventsMode {
NoEvents = 0,
Expand Down
6 changes: 2 additions & 4 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ name = "tests"
version = "1.5.1"
edition = "2018"
[dependencies]
casper-engine-test-support = { version = "5.0.0", default-features = false, features = [
"test-support",
] }
casper-engine-test-support = { version = "5.0.0", default-features = false }
contract = { path = "../contract", default-features = false }
casper-execution-engine = { version = "5.0.0", default-features = false }
casper-types = "3.0.0"
casper-types = { version = "3.0.0", default-features = false }
serde = { version = "1.0.80", default-features = false }
serde_json = { version = "1.0.59", default-features = false }
once_cell = "1"
Expand Down
Loading

0 comments on commit ab6e697

Please sign in to comment.