Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Season 2 - Fix forging out of season #320

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 17 additions & 11 deletions pallets/ajuna-awesome-avatars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,9 +1117,8 @@ pub mod pallet {
let GlobalConfig { forge, .. } = GlobalConfigs::<T>::get();
ensure!(forge.open, Error::<T>::ForgeClosed);

let (season_id, season) = Self::current_season_with_id()?;
let (leader, sacrifice_ids, sacrifices) =
Self::ensure_for_forge(player, leader_id, sacrifice_ids, &season_id, &season)?;
let (leader, sacrifice_ids, sacrifices, season_id, season) =
Self::ensure_for_forge(player, leader_id, sacrifice_ids)?;

let input_leader = (*leader_id, leader);
let input_sacrifices =
Expand Down Expand Up @@ -1195,6 +1194,13 @@ pub mod pallet {
Ok((current_status.season_id, season))
}

fn season_with_id_for(avatar: &Avatar) -> Result<(SeasonId, SeasonOf<T>), DispatchError> {
let season_id = avatar.season_id;
let season = Self::seasons(&season_id)?;

Ok((season_id, season))
}

fn ensure_ownership(
player: &T::AccountId,
avatar_id: &AvatarIdOf<T>,
Expand Down Expand Up @@ -1252,10 +1258,13 @@ pub mod pallet {
player: &T::AccountId,
leader_id: &AvatarIdOf<T>,
sacrifice_ids: Vec<AvatarIdOf<T>>,
season_id: &SeasonId,
season: &SeasonOf<T>,
) -> Result<(Avatar, Vec<AvatarIdOf<T>>, Vec<Avatar>), DispatchError> {
) -> Result<(Avatar, Vec<AvatarIdOf<T>>, Vec<Avatar>, SeasonId, SeasonOf<T>), DispatchError>
{
let sacrifice_count = sacrifice_ids.len() as u8;

let leader = Self::ensure_ownership(player, leader_id)?;
let (season_id, season) = Self::season_with_id_for(&leader)?;

ensure!(sacrifice_count >= season.min_sacrifices, Error::<T>::TooFewSacrifices);
ensure!(sacrifice_count <= season.max_sacrifices, Error::<T>::TooManySacrifices);
ensure!(!sacrifice_ids.contains(leader_id), Error::<T>::LeaderSacrificed);
Expand All @@ -1267,9 +1276,6 @@ pub mod pallet {
Self::ensure_unlocked(leader_id)?;
Self::ensure_unprepared(leader_id)?;

let leader = Self::ensure_ownership(player, leader_id)?;
ensure!(leader.season_id == *season_id, Error::<T>::IncorrectAvatarSeason);

let deduplicated_sacrifice_ids = {
let mut id_queue = sacrifice_ids.into_iter().collect::<VecDeque<_>>();

Expand All @@ -1287,7 +1293,7 @@ pub mod pallet {
.iter()
.map(|id| {
let avatar = Self::ensure_ownership(player, id)?;
ensure!(avatar.season_id == *season_id, Error::<T>::IncorrectAvatarSeason);
ensure!(avatar.season_id == season_id, Error::<T>::IncorrectAvatarSeason);
ensure!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would assume this check is not needed since, the encoding of an avatar from the same season must be the same any way since in one season you can only mint one type of encoding

avatar.encoding == leader.encoding,
Error::<T>::IncompatibleAvatarVersions
Expand All @@ -1298,7 +1304,7 @@ pub mod pallet {
})
.collect::<Result<Vec<Avatar>, DispatchError>>()?;

Ok((leader, deduplicated_sacrifice_ids, sacrifices))
Ok((leader, deduplicated_sacrifice_ids, sacrifices, season_id, season))
}

fn process_leader_forge_output(
Expand Down
115 changes: 80 additions & 35 deletions pallets/ajuna-awesome-avatars/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2001,31 +2001,49 @@ mod forging {
fn forge_should_reject_out_of_bound_sacrifices() {
let season = Season::default().min_sacrifices(3).max_sacrifices(5);

ExtBuilder::default().seasons(&[(1, season.clone())]).build().execute_with(|| {
run_to_block(season.start);
ExtBuilder::default()
.seasons(&[(1, season.clone())])
.balances(&[(ALICE, 1_000_000)])
.free_mints(&[(ALICE, MintCount::MAX)])
.build()
.execute_with(|| {
run_to_block(season.start);

for i in 0..season.min_sacrifices {
assert_noop!(
AAvatars::forge(
for _ in 0..3 {
assert_ok!(AAvatars::mint(
RuntimeOrigin::signed(ALICE),
H256::default(),
(0..i).map(|_| H256::default()).collect::<Vec<_>>(),
),
Error::<Test>::TooFewSacrifices,
);
}
MintOption {
pack_size: MintPackSize::Six,
payment: MintPayment::Normal,
pack_type: PackType::Material,
}
));
}

for i in (season.max_sacrifices + 1)..(season.max_sacrifices + 5) {
assert_noop!(
AAvatars::forge(
RuntimeOrigin::signed(ALICE),
H256::default(),
(0..i).map(|_| H256::default()).collect::<Vec<_>>(),
),
Error::<Test>::TooManySacrifices,
);
}
});
let owned_avatars = Owners::<Test>::get(ALICE, 1);

for i in 0..season.min_sacrifices {
assert_noop!(
AAvatars::forge(
RuntimeOrigin::signed(ALICE),
owned_avatars[0],
(0..i).map(|i| owned_avatars[i as usize + 1]).collect::<Vec<_>>(),
),
Error::<Test>::TooFewSacrifices,
);
}

for i in (season.max_sacrifices + 1)..(season.max_sacrifices + 5) {
assert_noop!(
AAvatars::forge(
RuntimeOrigin::signed(ALICE),
owned_avatars[0],
(0..i).map(|i| owned_avatars[i as usize + 1]).collect::<Vec<_>>(),
),
Error::<Test>::TooManySacrifices,
);
}
});
}

#[test]
Expand Down Expand Up @@ -2074,20 +2092,47 @@ mod forging {

#[test]
fn forge_should_reject_unknown_season_calls() {
ExtBuilder::default().build().execute_with(|| {
CurrentSeasonStatus::<Test>::mutate(|status| {
status.season_id = 123;
status.active = true;
});
assert_noop!(
AAvatars::forge(
let season = Season::default();

ExtBuilder::default()
.seasons(&[(1, season.clone())])
.balances(&[(ALICE, 1_000_000)])
.free_mints(&[(ALICE, MintCount::MAX)])
.build()
.execute_with(|| {
run_to_block(season.start);

assert_ok!(AAvatars::mint(
RuntimeOrigin::signed(ALICE),
H256::default(),
vec![H256::default()]
),
Error::<Test>::UnknownSeason,
);
});
MintOption {
pack_size: MintPackSize::Six,
payment: MintPayment::Normal,
pack_type: PackType::Material,
}
));

let owned_avatars = Owners::<Test>::get(ALICE, 1);

CurrentSeasonStatus::<Test>::mutate(|status| {
status.season_id = 123;
status.active = true;
});

Avatars::<Test>::mutate(owned_avatars[0], |maybe_avatar| {
if let Some((_, ref mut avatar)) = maybe_avatar {
avatar.season_id = 123;
}
});

assert_noop!(
AAvatars::forge(
RuntimeOrigin::signed(ALICE),
owned_avatars[0],
vec![owned_avatars[1]]
),
Error::<Test>::UnknownSeason,
);
});
}

#[test]
Expand Down