Skip to content

Commit

Permalink
Merge pull request #2789 from planetarium/release/1.17.2
Browse files Browse the repository at this point in the history
Throw exception when duplicate product id generated
  • Loading branch information
ipdae authored Aug 29, 2024
2 parents 6c05b1f + 693411a commit 4336b64
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
34 changes: 34 additions & 0 deletions .Lib9c.Tests/Action/RegisterProductTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,40 @@ public void Execute_Throw_ArgumentOutOfRangeException()
Assert.Throws<ArgumentOutOfRangeException>(() => action.Execute(new ActionContext()));
}

[Fact]
public void Execute_Throw_DuplicateOrderIdException()
{
var asset = 3 * RuneHelper.DailyRewardRune;
var context = new ActionContext();
var random = new TestRandom();
var productId = random.GenerateRandomGuid();
_initialState = _initialState
.SetAvatarState(AvatarAddress, _avatarState)
.SetLegacyState(Product.DeriveAddress(productId), List.Empty)
.MintAsset(context, AvatarAddress, asset);
var action = new RegisterProduct
{
AvatarAddress = AvatarAddress,
RegisterInfos = new List<IRegisterInfo>
{
new AssetInfo
{
AvatarAddress = AvatarAddress,
Asset = asset,
Price = 1 * Gold,
Type = ProductType.FungibleAssetValue,
},
},
};
Assert.Throws<DuplicateOrderIdException>(() => action.Execute(new ActionContext
{
BlockIndex = 1L,
PreviousState = _initialState,
RandomSeed = 0,
Signer = _agentAddress,
}));
}

public class ValidateMember
{
public IEnumerable<IRegisterInfo> RegisterInfos { get; set; }
Expand Down
17 changes: 15 additions & 2 deletions Lib9c/Action/RegisterProduct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ public static IWorld Register(IActionContext context, IRegisterInfo info, Avatar
}

Guid productId = random.GenerateRandomGuid();
var productAddress = Product.DeriveAddress(productId);
// 중복된 ProductId가 발급되면 상태를 덮어씌우는 현상을 방지하기위해 예외발생
if (states.TryGetLegacyState(productAddress, out IValue v) && v is not Null)
{
// FIXME 클라이언트 배포를 회피하기위해 기존 오류를 사용했으나 정규배포때 별도 예외로 구분ㅐ
throw new DuplicateOrderIdException("already registered id.");
}
var product = new ItemProduct
{
ProductId = productId,
Expand All @@ -218,8 +225,7 @@ public static IWorld Register(IActionContext context, IRegisterInfo info, Avatar
SellerAvatarAddress = registerInfo.AvatarAddress,
};
productsState.ProductIds.Add(productId);
states = states.SetLegacyState(Product.DeriveAddress(productId),
product.Serialize());
states = states.SetLegacyState(productAddress, product.Serialize());
break;
}
}
Expand All @@ -229,6 +235,13 @@ public static IWorld Register(IActionContext context, IRegisterInfo info, Avatar
{
Guid productId = random.GenerateRandomGuid();
Address productAddress = Product.DeriveAddress(productId);
// 중복된 ProductId가 발급되면 상태를 덮어씌우는 현상을 방지하기위해 예외발생
if (states.TryGetLegacyState(productAddress, out IValue v) && v is not Null)
{
// FIXME 클라이언트 배포를 회피하기위해 기존 오류를 사용했으나 정규배포때 별도 예외로 구분ㅐ
throw new DuplicateOrderIdException("already registered id.");
}

FungibleAssetValue asset = assetInfo.Asset;
var product = new FavProduct
{
Expand Down

0 comments on commit 4336b64

Please sign in to comment.