|
1 | 1 | import { expect } from "chai";
|
2 | 2 | import { deployments, ethers, network } from "hardhat";
|
3 |
| -import { IncrementalNG, BlockHashRNG, ChainlinkRNG, ChainlinkVRFCoordinatorV2Mock } from "../../typechain-types"; |
| 3 | +import { |
| 4 | + IncrementalNG, |
| 5 | + BlockHashRNG, |
| 6 | + ChainlinkRNG, |
| 7 | + ChainlinkVRFCoordinatorV2Mock, |
| 8 | + RandomizerRNG, |
| 9 | + RandomizerMock, |
| 10 | +} from "../../typechain-types"; |
4 | 11 |
|
5 | 12 | const initialNg = 424242;
|
6 | 13 | const abiCoder = ethers.AbiCoder.defaultAbiCoder();
|
@@ -75,4 +82,98 @@ describe("ChainlinkRNG", async () => {
|
75 | 82 | const rn = await rng.receiveRandomness(0);
|
76 | 83 | expect(rn).to.equal(expectedRn);
|
77 | 84 | });
|
| 85 | + |
| 86 | + it("Should return only the last random number when multiple requests are made", async () => { |
| 87 | + // First request |
| 88 | + let tx = await rng.requestRandomness(0); |
| 89 | + const requestId1 = 1; |
| 90 | + await expect(tx).to.emit(rng, "RequestSent").withArgs(requestId1); |
| 91 | + |
| 92 | + // Second request |
| 93 | + tx = await rng.requestRandomness(0); |
| 94 | + const requestId2 = 2; |
| 95 | + await expect(tx).to.emit(rng, "RequestSent").withArgs(requestId2); |
| 96 | + |
| 97 | + // Generate expected random numbers |
| 98 | + const expectedRn1 = BigInt( |
| 99 | + ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(["uint256", "uint256"], [requestId1, 0])) |
| 100 | + ); |
| 101 | + const expectedRn2 = BigInt( |
| 102 | + ethers.keccak256(ethers.AbiCoder.defaultAbiCoder().encode(["uint256", "uint256"], [requestId2, 0])) |
| 103 | + ); |
| 104 | + expect(expectedRn1).to.not.equal(expectedRn2, "Random numbers should be different"); |
| 105 | + |
| 106 | + // Fulfill first request |
| 107 | + tx = await vrfCoordinator.fulfillRandomWords(requestId1, rng.target, []); |
| 108 | + await expect(tx).to.emit(rng, "RequestFulfilled").withArgs(requestId1, expectedRn1); |
| 109 | + |
| 110 | + // Fulfill second request |
| 111 | + tx = await vrfCoordinator.fulfillRandomWords(requestId2, rng.target, []); |
| 112 | + await expect(tx).to.emit(rng, "RequestFulfilled").withArgs(requestId2, expectedRn2); |
| 113 | + |
| 114 | + // Should return only the last random number |
| 115 | + const rn = await rng.receiveRandomness(0); |
| 116 | + expect(rn).to.equal(expectedRn2); |
| 117 | + }); |
| 118 | +}); |
| 119 | + |
| 120 | +describe("RandomizerRNG", async () => { |
| 121 | + let rng: RandomizerRNG; |
| 122 | + let randomizer: RandomizerMock; |
| 123 | + |
| 124 | + beforeEach("Setup", async () => { |
| 125 | + await deployments.fixture(["RandomizerRNG"], { |
| 126 | + fallbackToGlobal: true, |
| 127 | + keepExistingDeployments: false, |
| 128 | + }); |
| 129 | + rng = (await ethers.getContract("RandomizerRNG")) as RandomizerRNG; |
| 130 | + randomizer = (await ethers.getContract("RandomizerOracle")) as RandomizerMock; |
| 131 | + }); |
| 132 | + |
| 133 | + it("Should return a non-zero random number", async () => { |
| 134 | + const randomBytes = ethers.randomBytes(32); |
| 135 | + const expectedRn = BigInt(ethers.hexlify(randomBytes)); |
| 136 | + const requestId = 1; |
| 137 | + |
| 138 | + let tx = await rng.requestRandomness(0); |
| 139 | + await expect(tx).to.emit(rng, "RequestSent").withArgs(requestId); |
| 140 | + |
| 141 | + tx = await randomizer.relay(rng.target, requestId, randomBytes); |
| 142 | + await expect(tx).to.emit(rng, "RequestFulfilled").withArgs(requestId, expectedRn); |
| 143 | + |
| 144 | + const rn = await rng.receiveRandomness(0); |
| 145 | + expect(rn).to.equal(expectedRn); |
| 146 | + }); |
| 147 | + |
| 148 | + it("Should return only the last random number when multiple requests are made", async () => { |
| 149 | + // First request |
| 150 | + let tx = await rng.requestRandomness(0); |
| 151 | + const requestId1 = 1; |
| 152 | + await expect(tx).to.emit(rng, "RequestSent").withArgs(requestId1); |
| 153 | + |
| 154 | + // Second request |
| 155 | + tx = await rng.requestRandomness(0); |
| 156 | + const requestId2 = 2; |
| 157 | + await expect(tx).to.emit(rng, "RequestSent").withArgs(requestId2); |
| 158 | + |
| 159 | + // Generate random bytes and expected numbers for both requests |
| 160 | + const randomBytes1 = ethers.randomBytes(32); |
| 161 | + const randomBytes2 = ethers.randomBytes(32); |
| 162 | + const expectedRn1 = BigInt(ethers.hexlify(randomBytes1)); |
| 163 | + const expectedRn2 = BigInt(ethers.hexlify(randomBytes2)); |
| 164 | + |
| 165 | + expect(expectedRn1).to.not.equal(expectedRn2, "Random numbers should be different"); |
| 166 | + |
| 167 | + // Fulfill first request |
| 168 | + tx = await randomizer.relay(rng.target, requestId1, randomBytes1); |
| 169 | + await expect(tx).to.emit(rng, "RequestFulfilled").withArgs(requestId1, expectedRn1); |
| 170 | + |
| 171 | + // Fulfill second request |
| 172 | + tx = await randomizer.relay(rng.target, requestId2, randomBytes2); |
| 173 | + await expect(tx).to.emit(rng, "RequestFulfilled").withArgs(requestId2, expectedRn2); |
| 174 | + |
| 175 | + // Should return only the last random number |
| 176 | + const rn = await rng.receiveRandomness(0); |
| 177 | + expect(rn).to.equal(expectedRn2); |
| 178 | + }); |
78 | 179 | });
|
0 commit comments