Skip to content

Commit

Permalink
Merge pull request #2208 from moreal/limit-claim-data-size
Browse files Browse the repository at this point in the history
Limit ClaimItems.ClaimData's size to 100
  • Loading branch information
moreal authored Nov 7, 2023
2 parents 0411bb7 + d15fa76 commit b96db26
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .Lib9c.Tests/Action/ClaimItemsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Lib9c.Tests.Action
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Libplanet.Action.State;
using Libplanet.Crypto;
Expand Down Expand Up @@ -182,6 +183,26 @@ public void Execute_WithMultipleRecipients()
Assert.Equal(1, inventory2.Items.First(x => x.item.Id == _itemIds[2]).count);
}

[Fact]
public void Execute_WithIncorrectClaimData()
{
var fungibleAssetValues = _currencies.Select(currency => currency * 1).ToList();

var action = new ClaimItems(Enumerable.Repeat(0, 101)
.Select(_ => (new PrivateKey().ToAddress(),
(IReadOnlyList<FungibleAssetValue>)fungibleAssetValues))
.ToImmutableList());

Assert.Throws<ArgumentOutOfRangeException>("ClaimData", () =>
action.Execute(new ActionContext
{
PreviousState = _initialState,
Signer = _signerAddress,
BlockIndex = 0,
RandomSeed = 0,
}));
}

private IAccount GenerateAvatar(IAccount state, out Address avatarAddress)
{
var address = new PrivateKey().ToAddress();
Expand Down
9 changes: 9 additions & 0 deletions Lib9c/Action/ClaimItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Nekoyume.Action
public class ClaimItems : GameAction, IClaimItems
{
private const string ActionTypeText = "claim_items";
private const int MaxClaimDataCount = 100;

public IReadOnlyList<(Address address, IReadOnlyList<FungibleAssetValue> fungibleAssetValues)> ClaimData { get; private set; }

Expand Down Expand Up @@ -57,6 +58,14 @@ public override IAccount Execute(IActionContext context)
{
context.UseGas(1);

if (ClaimData.Count > MaxClaimDataCount)
{
throw new ArgumentOutOfRangeException(
nameof(ClaimData),
ClaimData.Count,
$"ClaimData should be less than {MaxClaimDataCount}");
}

var states = context.PreviousState;
var random = context.GetRandom();
var itemSheet = states.GetSheets(containItemSheet: true).GetItemSheet();
Expand Down

0 comments on commit b96db26

Please sign in to comment.