Skip to content

Commit adb3f0e

Browse files
committed
feat: validate random number count and prevent duplicate generation
1 parent f9bf06b commit adb3f0e

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/RandomContract.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ public override Empty SetMaxRandomNumberCount(Int32Value input)
5959

6060
public override Empty GenerateRandomNumber(GenerateRandomNumberInput input)
6161
{
62+
// Check if the random number is already generated
63+
Assert(State.RandomNumbers[input.Hash] == null, "Random number already generated.");
6264
// Check if the maxValue is valid
6365
Assert(input.MaxValue > 0 && input.MaxValue <= State.MaxValueLimit.Value, "Invalid max value.");
6466
// Check if the random number count is valid
65-
Assert(input.RandomNumberCount > 0 && input.RandomNumberCount <= State.MaxRandomNumberCount.Value, "Invalid random number count.");
67+
Assert(
68+
input.RandomNumberCount > 0 && input.RandomNumberCount <= State.MaxRandomNumberCount.Value &&
69+
input.RandomNumberCount <= input.MaxValue, "Invalid random number count.");
6670
// Get a random hash and check if it is available
6771
var randomHash = State.ConsensusContract.GetRandomHash.Call(new Int64Value
6872
{

src/RandomContract.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
<RootNamespace>AElf.Contracts.RandomContract</RootNamespace>
55
<IsContract>true</IsContract>
66
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
7-
<AssemblyVersion>1.0.0.0</AssemblyVersion>
87
</PropertyGroup>
98
<PropertyGroup>
109
<ObjPath>$(MSBuildProjectDirectory)/$(BaseIntermediateOutputPath)$(Configuration)/$(TargetFramework)/</ObjPath>

test/RandomContractTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public async Task GenerateRandomNumberTest()
186186
// Generate 100 random number
187187
randomNumberCount = 10;
188188
maxValue = 100;
189+
hash = "Test";
189190
result = await RandomContractStub.GenerateRandomNumber.SendAsync(new GenerateRandomNumberInput
190191
{
191192
Hash = hash,
@@ -213,6 +214,17 @@ public async Task GenerateRandomNumberTest()
213214
randomSet.Add(randomNumber);
214215
}
215216

217+
// Generate random number with same hash and expect an exception
218+
result = await RandomContractStub.GenerateRandomNumber.SendWithExceptionAsync(new GenerateRandomNumberInput
219+
{
220+
Hash = hash,
221+
RandomNumberCount = randomNumberCount,
222+
MaxValue = maxValue
223+
});
224+
result.TransactionResult.Status.ShouldBe(TransactionResultStatus.Failed);
225+
result.TransactionResult.Error.ShouldContain("Random number already generated");
226+
227+
hash = "OtherHash";
216228
// Generate random number with invalid max value and expect an exception
217229
result = await RandomContractStub.GenerateRandomNumber.SendWithExceptionAsync(new GenerateRandomNumberInput
218230
{
@@ -246,6 +258,15 @@ public async Task GenerateRandomNumberTest()
246258
{
247259
Hash = hash,
248260
RandomNumberCount = maxRandomNumberCount + 1,
261+
MaxValue = maxValueLimit
262+
});
263+
result.TransactionResult.Status.ShouldBe(TransactionResultStatus.Failed);
264+
result.TransactionResult.Error.ShouldContain("Invalid random number count");
265+
266+
result = await RandomContractStub.GenerateRandomNumber.SendWithExceptionAsync(new GenerateRandomNumberInput
267+
{
268+
Hash = hash,
269+
RandomNumberCount = maxValue + 1,
249270
MaxValue = maxValue
250271
});
251272
result.TransactionResult.Status.ShouldBe(TransactionResultStatus.Failed);

0 commit comments

Comments
 (0)