@@ -12,6 +12,8 @@ import {ISortitionModule} from "../../src/arbitration/interfaces/ISortitionModul
1212import {SortitionModuleMock, SortitionModuleBase} from "../../src/test/SortitionModuleMock.sol " ;
1313import {UUPSProxy} from "../../src/proxy/UUPSProxy.sol " ;
1414import {BlockHashRNG} from "../../src/rng/BlockHashRNG.sol " ;
15+ import {RNGWithFallback} from "../../src/rng/RNGWithFallback.sol " ;
16+ import {RNGMock} from "../../src/test/RNGMock.sol " ;
1517import {PNK} from "../../src/token/PNK.sol " ;
1618import {TestERC20} from "../../src/token/TestERC20.sol " ;
1719import {ArbitrableExample, IArbitrableV2} from "../../src/arbitration/arbitrables/ArbitrableExample.sol " ;
@@ -2992,4 +2994,113 @@ contract KlerosCoreTest is Test {
29922994 assertEq (totalCommited, 0 , "totalCommited should be 0 " );
29932995 assertEq (choiceCount, 3 , "choiceCount should be 3 " );
29942996 }
2997+
2998+ function test_RNGFallback () public {
2999+ RNGWithFallback rngFallback;
3000+ uint256 fallbackTimeout = 100 ;
3001+ RNGMock rngMock = new RNGMock ();
3002+ rngFallback = new RNGWithFallback (msg .sender , address (sortitionModule), fallbackTimeout, rngMock);
3003+ assertEq (rngFallback.governor (), msg .sender , "Wrong governor " );
3004+ assertEq (rngFallback.consumer (), address (sortitionModule), "Wrong sortition module address " );
3005+ assertEq (address (rngFallback.rng ()), address (rngMock), "Wrong RNG in fallback contract " );
3006+ assertEq (rngFallback.fallbackTimeoutSeconds (), fallbackTimeout, "Wrong fallback timeout " );
3007+ assertEq (rngFallback.requestTimestamp (), 0 , "Request timestamp should be 0 " );
3008+
3009+ vm.prank (governor);
3010+ sortitionModule.changeRandomNumberGenerator (rngFallback);
3011+ assertEq (address (sortitionModule.rng ()), address (rngFallback), "Wrong RNG address " );
3012+
3013+ vm.prank (staker1);
3014+ core.setStake (GENERAL_COURT, 20000 );
3015+ vm.prank (disputer);
3016+ arbitrable.createDispute {value: feeForJuror * DEFAULT_NB_OF_JURORS}("Action " );
3017+ vm.warp (block .timestamp + minStakingTime);
3018+
3019+ sortitionModule.passPhase (); // Generating
3020+ assertEq (rngFallback.requestTimestamp (), block .timestamp , "Wrong request timestamp " );
3021+
3022+ vm.expectRevert (SortitionModuleBase.RandomNumberNotReady.selector );
3023+ sortitionModule.passPhase ();
3024+
3025+ vm.warp (block .timestamp + fallbackTimeout + 1 );
3026+
3027+ // Pass several blocks too to see that correct block.number is still picked up.
3028+ vm.roll (block .number + 5 );
3029+
3030+ vm.expectEmit (true , true , true , true );
3031+ emit RNGWithFallback.RNGFallback ();
3032+ sortitionModule.passPhase (); // Drawing phase
3033+
3034+ assertEq (sortitionModule.randomNumber (), uint256 (blockhash (block .number - 1 )), "Wrong random number " );
3035+ }
3036+
3037+ function test_RNGFallback_happyPath () public {
3038+ RNGWithFallback rngFallback;
3039+ uint256 fallbackTimeout = 100 ;
3040+ RNGMock rngMock = new RNGMock ();
3041+ rngFallback = new RNGWithFallback (msg .sender , address (sortitionModule), fallbackTimeout, rngMock);
3042+
3043+ vm.prank (governor);
3044+ sortitionModule.changeRandomNumberGenerator (rngFallback);
3045+ assertEq (address (sortitionModule.rng ()), address (rngFallback), "Wrong RNG address " );
3046+
3047+ vm.prank (staker1);
3048+ core.setStake (GENERAL_COURT, 20000 );
3049+ vm.prank (disputer);
3050+ arbitrable.createDispute {value: feeForJuror * DEFAULT_NB_OF_JURORS}("Action " );
3051+ vm.warp (block .timestamp + minStakingTime);
3052+
3053+ assertEq (rngFallback.requestTimestamp (), 0 , "Request timestamp should be 0 " );
3054+
3055+ sortitionModule.passPhase (); // Generating
3056+ assertEq (rngFallback.requestTimestamp (), block .timestamp , "Wrong request timestamp " );
3057+
3058+ rngMock.setRN (123 );
3059+
3060+ sortitionModule.passPhase (); // Drawing phase
3061+ assertEq (sortitionModule.randomNumber (), 123 , "Wrong random number " );
3062+ }
3063+
3064+ function test_RNGFallback_sanityChecks () public {
3065+ RNGWithFallback rngFallback;
3066+ uint256 fallbackTimeout = 100 ;
3067+ RNGMock rngMock = new RNGMock ();
3068+ rngFallback = new RNGWithFallback (msg .sender , address (sortitionModule), fallbackTimeout, rngMock);
3069+
3070+ vm.expectRevert (bytes ("Consumer only " ));
3071+ vm.prank (governor);
3072+ rngFallback.requestRandomness ();
3073+
3074+ vm.expectRevert (bytes ("Consumer only " ));
3075+ vm.prank (governor);
3076+ rngFallback.receiveRandomness ();
3077+
3078+ vm.expectRevert (bytes ("Governor only " ));
3079+ vm.prank (other);
3080+ rngFallback.changeGovernor (other);
3081+ vm.prank (governor);
3082+ rngFallback.changeGovernor (other);
3083+ assertEq (rngFallback.governor (), other, "Wrong governor " );
3084+
3085+ // Change governor back for convenience
3086+ vm.prank (other);
3087+ rngFallback.changeGovernor (governor);
3088+
3089+ vm.expectRevert (bytes ("Governor only " ));
3090+ vm.prank (other);
3091+ rngFallback.changeConsumer (other);
3092+ vm.prank (governor);
3093+ rngFallback.changeConsumer (other);
3094+ assertEq (rngFallback.consumer (), other, "Wrong consumer " );
3095+
3096+ vm.expectRevert (bytes ("Governor only " ));
3097+ vm.prank (other);
3098+ rngFallback.changeFallbackTimeout (5 );
3099+
3100+ vm.prank (governor);
3101+ vm.expectEmit (true , true , true , true );
3102+ emit RNGWithFallback.FallbackTimeoutChanged (5 );
3103+ rngFallback.changeFallbackTimeout (5 );
3104+ assertEq (rngFallback.fallbackTimeoutSeconds (), 5 , "Wrong fallback timeout " );
3105+ }
29953106}
0 commit comments