Skip to content

Commit

Permalink
Merge pull request #2386 from planetarium/fix/mint-assets
Browse files Browse the repository at this point in the history
AvatarState ctor returns null for invalid bencoded values
  • Loading branch information
OnedgeLee authored Feb 8, 2024
2 parents efcbc4a + e9984c1 commit 900fa7e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Lib9c/Action/Garages/GarageUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public static FungibleItemGarage ConvertToFungibleItemGarage(
{
if (garageState is null || garageState is Null)
{
throw new StateNullException(garageAddr);
throw new StateNullException(ReservedAddresses.LegacyAccount, garageAddr);
}

var garage = new FungibleItemGarage(garageState);
Expand Down
35 changes: 21 additions & 14 deletions Lib9c/Action/MintAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Libplanet.Action.State;
using Libplanet.Crypto;
using Libplanet.Types.Assets;
using Nekoyume.Exceptions;
using Nekoyume.Model;
using Nekoyume.Model.Item;
using Nekoyume.Model.Mail;
Expand Down Expand Up @@ -82,31 +83,37 @@ public override IWorld Execute(IActionContext context)

if (items is { } itemsNotNull)
{
AvatarState avatarState = state.GetAvatarState(recipient);
MaterialItemSheet itemSheet = state.GetSheet<MaterialItemSheet>();
if (itemSheet is null || itemSheet.OrderedList is null)
if (state.GetAvatarState(recipient) is AvatarState recipientAvatarState)
{
throw new InvalidOperationException();
}
MaterialItemSheet itemSheet = state.GetSheet<MaterialItemSheet>();
if (itemSheet is null || itemSheet.OrderedList is null)
{
throw new InvalidOperationException();
}

foreach (MaterialItemSheet.Row row in itemSheet.OrderedList)
{
if (row.ItemId.Equals(itemsNotNull.Id))
foreach (MaterialItemSheet.Row row in itemSheet.OrderedList)
{
Material item = ItemFactory.CreateMaterial(row);
avatarState.inventory.AddFungibleItem(item, itemsNotNull.Count);
if (row.ItemId.Equals(itemsNotNull.Id))
{
Material item = ItemFactory.CreateMaterial(row);
recipientAvatarState.inventory.AddFungibleItem(item, itemsNotNull.Count);
}
}
}

state = state.SetAvatarState(recipient, avatarState, false, true, false, false);
fivs.Add(itemsNotNull);
state = state.SetAvatarState(recipient, recipientAvatarState, false, true, false, false);
fivs.Add(itemsNotNull);
}
else
{
throw new StateNullException(Addresses.Avatar, recipient);
}
}
}

IRandom rng = context.GetRandom();
foreach (var recipient in mailRecords.Keys)
{
if (state.GetAvatarState(recipient) is { } recipientAvatarState)
if (state.GetAvatarState(recipient) is AvatarState recipientAvatarState)
{
(List<FungibleAssetValue> favs, List<FungibleItemValue> fivs) = mailRecords[recipient];
List<(Address recipient, FungibleAssetValue v)> mailFavs =
Expand Down
6 changes: 3 additions & 3 deletions Lib9c/Action/Stake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public override IWorld Execute(IActionContext context)
Log.Debug("{AddressesHex}Stake exec started", addressesHex);
if (!states.TryGetSheet<StakePolicySheet>(out var stakePolicySheet))
{
throw new StateNullException(Addresses.GetSheetAddress<StakePolicySheet>());
throw new StateNullException(ReservedAddresses.LegacyAccount, Addresses.GetSheetAddress<StakePolicySheet>());
}

var currentStakeRegularRewardSheetAddr = Addresses.GetSheetAddress(
Expand All @@ -82,7 +82,7 @@ public override IWorld Execute(IActionContext context)
currentStakeRegularRewardSheetAddr,
out var stakeRegularRewardSheet))
{
throw new StateNullException(currentStakeRegularRewardSheetAddr);
throw new StateNullException(ReservedAddresses.LegacyAccount, currentStakeRegularRewardSheetAddr);
}

var minimumRequiredGold = stakeRegularRewardSheet.OrderedRows.Min(x => x.RequiredGold);
Expand Down Expand Up @@ -115,7 +115,7 @@ public override IWorld Execute(IActionContext context)
// NOTE: Cannot withdraw staking.
if (Amount == 0)
{
throw new StateNullException(stakeStateAddress);
throw new StateNullException(ReservedAddresses.LegacyAccount, stakeStateAddress);
}

// NOTE: Contract a new staking.
Expand Down
3 changes: 2 additions & 1 deletion Lib9c/Exceptions/StateNullException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ public StateNullException(
}

public StateNullException(
Address accountAddress,
Address address,
Exception? innerException = null)
: base($"State is null or Null.Value: {address}", innerException)
: base($"State is null or Null.Value: {accountAddress}.{address}", innerException)
{
}

Expand Down
22 changes: 22 additions & 0 deletions Lib9c/Module/AvatarModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ public static AvatarState GetAvatarState(this IWorldState worldState, Address ad
avatarDict
);

return null;
}
catch (KeyNotFoundException e)
{
Log.Error(
e,
"Invalid avatar state ({AvatarAddress}): {SerializedAvatar}",
address.ToHex(),
avatarDict
);

return null;
}
}
Expand All @@ -115,6 +126,17 @@ public static AvatarState GetAvatarState(this IWorldState worldState, Address ad
avatarList
);

return null;
}
catch (IndexOutOfRangeException e)
{
Log.Error(
e,
"Invalid avatar state ({AvatarAddress}): {SerializedAvatar}",
address.ToHex(),
avatarList
);

return null;
}
}
Expand Down

0 comments on commit 900fa7e

Please sign in to comment.