Skip to content

Commit

Permalink
Fix competition modules migration
Browse files Browse the repository at this point in the history
  • Loading branch information
ismellike committed Sep 3, 2024
1 parent 5ce69b3 commit 45f4b55
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 26 deletions.
74 changes: 74 additions & 0 deletions packages/arena-interface/src/competition/migrate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::fmt;

use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Addr, Uint128};
use cw_utils::Expiration;

use crate::fees::FeeInformation;

use super::state::{Competition, CompetitionStatus};

/// Used for migration
#[cw_serde]
pub enum CompetitionStatusV182 {
Pending,
Active,
Inactive,
Jailed,
}

impl fmt::Display for CompetitionStatusV182 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CompetitionStatusV182::Pending => write!(f, "Pending"),
CompetitionStatusV182::Jailed => write!(f, "Jailed"),
CompetitionStatusV182::Active => write!(f, "Active"),
CompetitionStatusV182::Inactive => write!(f, "Inactive"),
}
}
}

/// Used for migration
#[cw_serde]
pub struct CompetitionV182<CompetitionExt> {
pub id: Uint128,
pub category_id: Option<Uint128>,
pub admin_dao: Addr,
pub host: Addr,
pub escrow: Option<Addr>,
pub name: String,
pub description: String,
pub start_height: u64,
pub expiration: Expiration,
pub rulesets: Option<Vec<Uint128>>,
pub status: CompetitionStatusV182,
pub extension: CompetitionExt,
pub fees: Option<Vec<FeeInformation<Addr>>>,
pub banner: Option<String>,
}

impl<CompetitionExt: Clone> CompetitionV182<CompetitionExt> {
pub fn into_competition(self, activation_height: u64) -> Competition<CompetitionExt> {
Competition {
id: self.id,
category_id: self.category_id,
admin_dao: self.admin_dao,
host: self.host,
escrow: self.escrow,
name: self.name,
description: self.description,
start_height: self.start_height,
expiration: self.expiration,
rulesets: self.rulesets,
status: match self.status {
CompetitionStatusV182::Pending => CompetitionStatus::Pending,
CompetitionStatusV182::Active => CompetitionStatus::Active { activation_height },
CompetitionStatusV182::Inactive => CompetitionStatus::Inactive,
CompetitionStatusV182::Jailed => CompetitionStatus::Jailed { activation_height },
},
extension: self.extension,
fees: self.fees,
banner: self.banner,
}
}
}
1 change: 1 addition & 0 deletions packages/arena-interface/src/competition/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod migrate;
pub mod msg;
pub mod state;
92 changes: 70 additions & 22 deletions packages/cw-competition-base/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::marker::PhantomData;

use arena_interface::{
competition::{
migrate::CompetitionV182,
msg::{
CompetitionsFilter, EscrowInstantiateInfo, ExecuteBase, HookDirection, InstantiateBase,
MemberStatUpdate, QueryBase, StatMsg, ToCompetitionExt,
Expand Down Expand Up @@ -49,6 +50,24 @@ impl<'a, CompetitionExt: Serialize + Clone + DeserializeOwned>
}
}

pub struct CompetitionV182Indexes<'a, CompetitionExt> {
pub status: MultiIndex<'a, String, CompetitionV182<CompetitionExt>, u128>,
pub category: MultiIndex<'a, u128, CompetitionV182<CompetitionExt>, u128>,
pub host: MultiIndex<'a, String, CompetitionV182<CompetitionExt>, u128>,
}

impl<'a, CompetitionExt: Serialize + Clone + DeserializeOwned>
IndexList<CompetitionV182<CompetitionExt>> for CompetitionV182Indexes<'a, CompetitionExt>
{
fn get_indexes(
&'_ self,
) -> Box<dyn Iterator<Item = &'_ dyn Index<CompetitionV182<CompetitionExt>>> + '_> {
let v: Vec<&dyn Index<CompetitionV182<CompetitionExt>>> =
vec![&self.status, &self.category, &self.host];
Box::new(v.into_iter())
}
}

pub struct CompetitionModuleContract<
'a,
InstantiateExt,
Expand All @@ -65,6 +84,12 @@ pub struct CompetitionModuleContract<
Competition<CompetitionExt>,
CompetitionIndexes<'static, CompetitionExt>,
>,
pub competitions_v182: IndexedMap<
'static,
u128,
CompetitionV182<CompetitionExt>,
CompetitionV182Indexes<'static, CompetitionExt>,
>,
pub competition_evidence: Map<'static, (u128, u128), Evidence>,
pub competition_evidence_count: Map<'static, u128, Uint128>,
pub competition_result: Map<'static, u128, Option<Distribution<Addr>>>,
Expand Down Expand Up @@ -125,6 +150,12 @@ impl<
competitions_category_key,
competitions_host_key,
),
competitions_v182: Self::competitions_v182(
competitions_key,
competitions_status_key,
competitions_category_key,
competitions_host_key,
),
escrows_to_competitions: Map::new(escrows_to_competitions_key),
temp_competition: Item::new(temp_competition_key),
competition_hooks: Map::new(competition_hooks_key),
Expand Down Expand Up @@ -173,6 +204,39 @@ impl<
};
IndexedMap::new(competitions_key, indexes)
}

const fn competitions_v182(
competitions_key: &'static str,
competitions_status_key: &'static str,
competitions_category_key: &'static str,
competitions_host_key: &'static str,
) -> IndexedMap<
'static,
u128,
CompetitionV182<CompetitionExt>,
CompetitionV182Indexes<'static, CompetitionExt>,
> {
let indexes = CompetitionV182Indexes {
status: MultiIndex::new(
|_x, d: &CompetitionV182<CompetitionExt>| d.status.to_string(),
competitions_key,
competitions_status_key,
),
category: MultiIndex::new(
|_x, d: &CompetitionV182<CompetitionExt>| {
d.category_id.unwrap_or(Uint128::zero()).u128()
},
competitions_key,
competitions_category_key,
),
host: MultiIndex::new(
|_x, d: &CompetitionV182<CompetitionExt>| d.host.to_string(),
competitions_key,
competitions_host_key,
),
};
IndexedMap::new(competitions_key, indexes)
}
}

impl<
Expand Down Expand Up @@ -1483,7 +1547,7 @@ impl<
// Competition status 'Active' and 'Jailed' now store the activation height for the payment registry
// Not too many, so we can just do it in the migration
let competition_range = self
.competitions
.competitions_v182
.idx
.status
.prefix(
Expand All @@ -1495,7 +1559,7 @@ impl<
.range(deps.storage, None, None, cosmwasm_std::Order::Descending)
.collect::<StdResult<Vec<_>>>()?;
let competition_range_jailed = self
.competitions
.competitions_v182
.idx
.status
.prefix(
Expand All @@ -1508,34 +1572,18 @@ impl<
.collect::<StdResult<Vec<_>>>()?;

for (competition_id, competition) in competition_range {
let new_competition = Competition {
status: CompetitionStatus::Active {
activation_height: env.block.height,
},
..competition.clone()
};

self.competitions.replace(
self.competitions.save(
deps.storage,
competition_id,
Some(&new_competition),
Some(&competition),
&competition.into_competition(env.block.height),
)?;
}

for (competition_id, competition) in competition_range_jailed {
let new_competition = Competition {
status: CompetitionStatus::Jailed {
activation_height: env.block.height,
},
..competition.clone()
};

self.competitions.replace(
self.competitions.save(
deps.storage,
competition_id,
Some(&new_competition),
Some(&competition),
&competition.into_competition(env.block.height),
)?;
}

Expand Down
17 changes: 16 additions & 1 deletion scripts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use arena::Arena;
use cw_orch::{anyhow, prelude::*};
use orch_interface::{
arena_competition_enrollment::ArenaCompetitionEnrollmentContract,
arena_core::ArenaCoreContract, arena_token_gateway::ArenaTokenGatewayContract,
arena_core::ArenaCoreContract, arena_league_module::ArenaLeagueModuleContract,
arena_token_gateway::ArenaTokenGatewayContract,
arena_tournament_module::ArenaTournamentModuleContract,
arena_wager_module::ArenaWagerModuleContract,
};
use std::env;

Expand Down Expand Up @@ -42,6 +44,7 @@ enum DeployComponent {
Tournament,
Enrollment,
TokenGateway,
CompetitionModules,
}

fn parse_command(args: &[String]) -> Command {
Expand All @@ -61,6 +64,7 @@ fn parse_command(args: &[String]) -> Command {
"tournament" => DeployComponent::Tournament,
"enrollment" => DeployComponent::Enrollment,
"token_gateway" => DeployComponent::TokenGateway,
"competition_modules" => DeployComponent::CompetitionModules,
_ => return Command::Unknown,
};

Expand All @@ -79,6 +83,7 @@ fn deploy(network: Network, component: DeployComponent) -> anyhow::Result<()> {
DeployComponent::Tournament => deploy_tournament(daemon)?,
DeployComponent::Enrollment => deploy_enrollment(daemon)?,
DeployComponent::TokenGateway => deploy_token_gateway(daemon)?,
DeployComponent::CompetitionModules => deploy_competition_modules(daemon)?,
}

Ok(())
Expand Down Expand Up @@ -114,6 +119,16 @@ fn deploy_token_gateway(daemon: Daemon) -> anyhow::Result<()> {
Ok(())
}

fn deploy_competition_modules(daemon: Daemon) -> anyhow::Result<()> {
let wager_module = ArenaWagerModuleContract::new(daemon.clone());
wager_module.upload()?;
let league_module = ArenaLeagueModuleContract::new(daemon.clone());
league_module.upload()?;
let tournament_module = ArenaTournamentModuleContract::new(daemon);
tournament_module.upload()?;
Ok(())
}

mod arena;
mod dao_dao;
#[cfg(test)]
Expand Down
6 changes: 3 additions & 3 deletions scripts/state.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"arena_competition_enrollment": 6403,
"arena_core": 6440,
"arena_escrow": 6398,
"arena_league_module": 6401,
"arena_league_module": 6443,
"arena_payment_registry": 6405,
"arena_token_gateway": 6441,
"arena_tournament_module": 6402,
"arena_wager_module": 6400
"arena_tournament_module": 6444,
"arena_wager_module": 6442
},
"default": {}
}
Expand Down

0 comments on commit 45f4b55

Please sign in to comment.