Skip to content

Commit 1e41c0c

Browse files
committed
feat: validate hash length
1 parent adb3f0e commit 1e41c0c

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/RandomContract.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace AElf.Contracts.RandomContract;
88

9-
public class RandomContract : RandomContractContainer.RandomContractBase
9+
public partial class RandomContract : RandomContractContainer.RandomContractBase
1010
{
1111
// Initializes the contract
1212
public override Empty Initialize(InitializeInput input)
@@ -61,6 +61,8 @@ public override Empty GenerateRandomNumber(GenerateRandomNumberInput input)
6161
{
6262
// Check if the random number is already generated
6363
Assert(State.RandomNumbers[input.Hash] == null, "Random number already generated.");
64+
// Check if the hash length is valid
65+
Assert(input.Hash.Length <= MaxHashLength, "Invalid hash length.");
6466
// Check if the maxValue is valid
6567
Assert(input.MaxValue > 0 && input.MaxValue <= State.MaxValueLimit.Value, "Invalid max value.");
6668
// Check if the random number count is valid

src/RandomContractConstants.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace AElf.Contracts.RandomContract;
2+
3+
public partial class RandomContract
4+
{
5+
private const long MaxHashLength = 100;
6+
}

test/RandomContractTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,17 @@ public async Task GenerateRandomNumberTest()
224224
result.TransactionResult.Status.ShouldBe(TransactionResultStatus.Failed);
225225
result.TransactionResult.Error.ShouldContain("Random number already generated");
226226

227+
var invalidHash = "cdd6b38b763d929355c47fb6f7be5107cdd6b38b763d929355c47fb6f7be5107cdd6b38b763d929355c47fb6f7be510712345";
228+
// Generate random number with invalid hash length and expect an exception
229+
result = await RandomContractStub.GenerateRandomNumber.SendWithExceptionAsync(new GenerateRandomNumberInput
230+
{
231+
Hash = invalidHash,
232+
RandomNumberCount = randomNumberCount,
233+
MaxValue = maxValue
234+
});
235+
result.TransactionResult.Status.ShouldBe(TransactionResultStatus.Failed);
236+
result.TransactionResult.Error.ShouldContain("Invalid hash length");
237+
227238
hash = "OtherHash";
228239
// Generate random number with invalid max value and expect an exception
229240
result = await RandomContractStub.GenerateRandomNumber.SendWithExceptionAsync(new GenerateRandomNumberInput

0 commit comments

Comments
 (0)