Skip to content

Commit dce2024

Browse files
committed
fix: moved to the new Randomizer contract, the callback takes now a uint256 instead of uint128
1 parent 32d0548 commit dce2024

File tree

6 files changed

+200
-65
lines changed

6 files changed

+200
-65
lines changed

contracts/deploy/00-home-chain-arbitration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const pnkByChain = new Map<HomeChains, string>([
1515

1616
const randomizerByChain = new Map<HomeChains, string>([
1717
[HomeChains.ARBITRUM_ONE, "0x00"],
18-
[HomeChains.ARBITRUM_GOERLI, "0xE1B6CcAc0BB0355C01A049e78909231Bfa13620B"],
18+
[HomeChains.ARBITRUM_GOERLI, "0x57F7a8aA8291A04B325F3f0d2c4d03353d3Ef25f"],
1919
]);
2020

2121
const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {

contracts/deploy/00-rng.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { HardhatRuntimeEnvironment } from "hardhat/types";
2+
import { DeployFunction } from "hardhat-deploy/types";
3+
import { BigNumber } from "ethers";
4+
import { DisputeKitClassic, RandomizerRNG } from "../typechain-types";
5+
6+
enum HomeChains {
7+
ARBITRUM_ONE = 42161,
8+
ARBITRUM_GOERLI = 421613,
9+
HARDHAT = 31337,
10+
}
11+
12+
const pnkByChain = new Map<HomeChains, string>([
13+
[HomeChains.ARBITRUM_ONE, "0x330bD769382cFc6d50175903434CCC8D206DCAE5"],
14+
[HomeChains.ARBITRUM_GOERLI, "0x4DEeeFD054434bf6721eF39Aa18EfB3fd0D12610"],
15+
]);
16+
17+
const randomizerByChain = new Map<HomeChains, string>([
18+
[HomeChains.ARBITRUM_ONE, "0x00"],
19+
[HomeChains.ARBITRUM_GOERLI, "0x57F7a8aA8291A04B325F3f0d2c4d03353d3Ef25f"],
20+
]);
21+
22+
const deployArbitration: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
23+
const { deployments, getNamedAccounts, getChainId } = hre;
24+
const { deploy, execute } = deployments;
25+
const { AddressZero } = hre.ethers.constants;
26+
const RNG_LOOKAHEAD = 20;
27+
28+
// fallback to hardhat node signers on local network
29+
const deployer = (await getNamedAccounts()).deployer ?? (await hre.ethers.getSigners())[0].address;
30+
const chainId = Number(await getChainId());
31+
console.log("deploying to %s with deployer %s", HomeChains[chainId], deployer);
32+
33+
if (chainId === HomeChains.HARDHAT) {
34+
pnkByChain.set(
35+
HomeChains.HARDHAT,
36+
(
37+
await deploy("PNK", {
38+
from: deployer,
39+
log: true,
40+
})
41+
).address
42+
);
43+
randomizerByChain.set(
44+
HomeChains.HARDHAT,
45+
(
46+
await deploy("RandomizerMock", {
47+
from: deployer,
48+
args: [],
49+
log: true,
50+
})
51+
).address
52+
);
53+
}
54+
55+
const randomizer = randomizerByChain.get(Number(await getChainId())) ?? AddressZero;
56+
const rng = await deploy("RandomizerRNG", {
57+
from: deployer,
58+
args: [randomizer, deployer],
59+
log: true,
60+
});
61+
62+
const disputeKit = (await hre.ethers.getContract("DisputeKitClassic")) as DisputeKitClassic;
63+
await disputeKit.changeRandomNumberGenerator(rng.address, RNG_LOOKAHEAD);
64+
};
65+
66+
deployArbitration.tags = ["HomeChain", "RNG"];
67+
deployArbitration.skip = async ({ getChainId }) => {
68+
const chainId = Number(await getChainId());
69+
return !HomeChains[chainId];
70+
};
71+
72+
export default deployArbitration;

contracts/deployments/arbitrumGoerli/RandomizerRNG.json

Lines changed: 73 additions & 44 deletions
Large diffs are not rendered by default.

contracts/src/rng/IRandomizer.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8;
4+
15
// Randomizer protocol interface
26
interface IRandomizer {
37
function request(uint256 callbackGasLimit) external returns (uint256);

contracts/src/rng/RandomizerRNG.sol

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ import "./IRandomizer.sol";
1010
* https://randomizer.ai/
1111
*/
1212
contract RandomizerRNG is RNG {
13-
uint256 public constant CALLBACK_GAS_LIMIT = 50000;
1413
address public governor; // The address that can withdraw funds.
14+
uint256 public callbackGasLimit = 50000; // Gas limit for the randomizer callback
1515

1616
IRandomizer public randomizer; // Randomizer address.
17-
mapping(uint128 => uint256) public randomNumbers; // randomNumbers[requestID] is the random number for this request id, 0 otherwise.
18-
mapping(address => uint128) public requesterToID; // Maps the requester to his latest request ID.
17+
mapping(uint256 => uint256) public randomNumbers; // randomNumbers[requestID] is the random number for this request id, 0 otherwise.
18+
mapping(address => uint256) public requesterToID; // Maps the requester to his latest request ID.
19+
20+
// ************************************* //
21+
// * Function Modifiers * //
22+
// ************************************* //
1923

2024
modifier onlyByGovernor() {
2125
require(governor == msg.sender, "Governor only");
@@ -31,44 +35,70 @@ contract RandomizerRNG is RNG {
3135
governor = _governor;
3236
}
3337

38+
// ************************ //
39+
// * Governance * //
40+
// ************************ //
41+
3442
/** @dev Changes the governor of the contract.
3543
* @param _governor The new governor.
3644
*/
3745
function changeGovernor(address _governor) external onlyByGovernor {
3846
governor = _governor;
3947
}
4048

49+
/** @dev Change the Randomizer callback gas limit.
50+
* @param _callbackGasLimit the new limit.
51+
*/
52+
function setCallbackGasLimit(uint256 _callbackGasLimit) external onlyByGovernor {
53+
callbackGasLimit = _callbackGasLimit;
54+
}
55+
56+
/** @dev Change the Randomizer address.
57+
* @param _randomizer the new Randomizer address.
58+
*/
59+
function setRandomizer(address _randomizer) external onlyByGovernor {
60+
randomizer = IRandomizer(_randomizer);
61+
}
62+
4163
/**
42-
* @dev Request a random number. The id of the request is tied to the sender.
64+
* @dev Allows the governor to withdraw randomizer funds.
65+
* @param _amount Amount to withdraw in wei.
4366
*/
44-
function requestRandomness(uint256 /*_block*/) external override {
45-
uint128 id = uint128(randomizer.request(CALLBACK_GAS_LIMIT));
46-
requesterToID[msg.sender] = id;
67+
function randomizerWithdraw(uint256 _amount) external onlyByGovernor {
68+
randomizer.clientWithdrawTo(msg.sender, _amount);
4769
}
4870

71+
// ************************************* //
72+
// * State Modifiers * //
73+
// ************************************* //
74+
4975
/**
50-
* @dev Return the random number.
51-
* @return randomNumber The random number or 0 if it is not ready or has not been requested.
76+
* @dev Request a random number. The id of the request is tied to the sender.
5277
*/
53-
function receiveRandomness(uint256 /*_block*/) external view override returns (uint256 randomNumber) {
54-
// Get the latest request ID for this requester.
55-
uint128 id = requesterToID[msg.sender];
56-
randomNumber = randomNumbers[id];
78+
function requestRandomness(uint256 /*_block*/) external override {
79+
uint256 id = randomizer.request(callbackGasLimit);
80+
requesterToID[msg.sender] = id;
5781
}
5882

5983
/**
6084
* @dev Callback function called by the randomizer contract when the random value is generated.
6185
*/
62-
function randomizerCallback(uint128 _id, bytes32 _value) external {
63-
require(msg.sender == address(randomizer), "Caller not Randomizer");
86+
function randomizerCallback(uint256 _id, bytes32 _value) external {
87+
require(msg.sender == address(randomizer), "Randomizer only");
6488
randomNumbers[_id] = uint256(_value);
6589
}
6690

91+
// ************************************* //
92+
// * Public Views * //
93+
// ************************************* //
94+
6795
/**
68-
* @dev Allows the governor to withdraw randomizer funds.
69-
* @param _amount Amount to withdraw in wei.
96+
* @dev Return the random number.
97+
* @return randomNumber The random number or 0 if it is not ready or has not been requested.
7098
*/
71-
function randomizerWithdraw(uint256 _amount) external onlyByGovernor {
72-
randomizer.clientWithdrawTo(msg.sender, _amount);
99+
function receiveRandomness(uint256 /*_block*/) external view override returns (uint256 randomNumber) {
100+
// Get the latest request ID for this requester.
101+
uint256 id = requesterToID[msg.sender];
102+
randomNumber = randomNumbers[id];
73103
}
74104
}

contracts/src/rng/mock/RandomizerMock.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ contract RandomizerMock is IRandomizer {
1515
revert("Not Implemented");
1616
}
1717

18-
function relay(RandomizerRNG _rng, uint128 _id, bytes32 _value) external {
18+
function relay(RandomizerRNG _rng, uint256 _id, bytes32 _value) external {
1919
_rng.randomizerCallback(_id, _value);
2020
}
2121
}

0 commit comments

Comments
 (0)