From 2e6b95f16f7d62f871f71d16a219d53c2f00b44e Mon Sep 17 00:00:00 2001 From: Brendan Asselstine Date: Tue, 5 Oct 2021 21:05:07 -0700 Subject: [PATCH] Draw Beacon: General Updates --- contracts/DrawBeacon.sol | 48 ++++++++++++++-------------- contracts/interfaces/IDrawBeacon.sol | 14 +++----- contracts/test/DrawBeaconHarness.sol | 5 +-- deploy/deploy.js | 3 ++ test/DrawBeacon.test.ts | 25 ++++++++------- 5 files changed, 49 insertions(+), 46 deletions(-) diff --git a/contracts/DrawBeacon.sol b/contracts/DrawBeacon.sol index 2d01f2d9..489c18e0 100644 --- a/contracts/DrawBeacon.sol +++ b/contracts/DrawBeacon.sol @@ -74,18 +74,12 @@ contract DrawBeacon is IDrawBeacon, Ownable { /** * @notice Emit when the DrawBeacon is deployed. - * @param drawHistory Address of the draw history to push draws to. - * @param rng Address of RNG service. * @param nextDrawId Draw ID at which the DrawBeacon should start. Can't be inferior to 1. * @param beaconPeriodStartedAt Timestamp when beacon period starts. - * @param beaconPeriodSeconds Minimum seconds between draw period. */ event Deployed( - IDrawHistory indexed drawHistory, - RNGInterface indexed rng, uint32 nextDrawId, - uint64 beaconPeriodStartedAt, - uint32 beaconPeriodSeconds + uint64 beaconPeriodStartedAt ); /* ============ Modifiers ============ */ @@ -124,26 +118,23 @@ contract DrawBeacon is IDrawBeacon, Ownable { RNGInterface _rng, uint32 _nextDrawId, uint64 _beaconPeriodStart, - uint32 _beaconPeriodSeconds + uint32 _beaconPeriodSeconds, + uint32 _rngTimeout ) Ownable(_owner) { require(_beaconPeriodStart > 0, "DrawBeacon/beacon-period-greater-than-zero"); require(address(_rng) != address(0), "DrawBeacon/rng-not-zero"); - rng = _rng; + require(_nextDrawId >= 1, "DrawBeacon/next-draw-id-gte-one"); - _setBeaconPeriodSeconds(_beaconPeriodSeconds); beaconPeriodStartedAt = _beaconPeriodStart; - - _setDrawHistory(_drawHistory); - - // 30 min timeout - _setRngTimeout(1800); - - require(_nextDrawId >= 1, "DrawBeacon/next-draw-id-gte-one"); nextDrawId = _nextDrawId; - emit Deployed(_drawHistory, _rng, _nextDrawId, _beaconPeriodStart, _beaconPeriodSeconds); + _setBeaconPeriodSeconds(_beaconPeriodSeconds); + _setDrawHistory(_drawHistory); + _setRngService(_rng); + _setRngTimeout(_rngTimeout); - emit BeaconPeriodStarted(msg.sender, _beaconPeriodStart); + emit Deployed(_nextDrawId, _beaconPeriodStart); + emit BeaconPeriodStarted(_beaconPeriodStart); } /* ============ Public Functions ============ */ @@ -220,7 +211,7 @@ contract DrawBeacon is IDrawBeacon, Ownable { uint32 requestId = rngRequest.id; uint32 lockBlock = rngRequest.lockBlock; delete rngRequest; - emit DrawCancelled(msg.sender, requestId, lockBlock); + emit DrawCancelled(requestId, lockBlock); } /// @inheritdoc IDrawBeacon @@ -254,8 +245,8 @@ contract DrawBeacon is IDrawBeacon, Ownable { // Reset the rngReqeust state so Beacon period can start again. delete rngRequest; - emit DrawCompleted(msg.sender, randomNumber); - emit BeaconPeriodStarted(msg.sender, nextBeaconPeriodStartedAt); + emit DrawCompleted(randomNumber); + emit BeaconPeriodStarted(nextBeaconPeriodStartedAt); } /// @inheritdoc IDrawBeacon @@ -329,7 +320,7 @@ contract DrawBeacon is IDrawBeacon, Ownable { rngRequest.lockBlock = lockBlock; rngRequest.requestedAt = _currentTime(); - emit DrawStarted(msg.sender, requestId, lockBlock); + emit DrawStarted(requestId, lockBlock); } /// @inheritdoc IDrawBeacon @@ -353,6 +344,15 @@ contract DrawBeacon is IDrawBeacon, Ownable { override onlyOwner requireDrawNotStarted + { + _setRngService(_rngService); + } + + /** + * @notice Sets the RNG service that the Prize Strategy is connected to + * @param _rngService The address of the new RNG service interface + */ + function _setRngService(RNGInterface _rngService) internal { rng = _rngService; emit RngServiceUpdated(_rngService); @@ -444,7 +444,7 @@ contract DrawBeacon is IDrawBeacon, Ownable { drawHistory = _newDrawHistory; - emit DrawHistoryTransferred(_newDrawHistory); + emit DrawHistoryUpdated(_newDrawHistory); return _newDrawHistory; } diff --git a/contracts/interfaces/IDrawBeacon.sol b/contracts/interfaces/IDrawBeacon.sol index 8ce762e8..99637374 100644 --- a/contracts/interfaces/IDrawBeacon.sol +++ b/contracts/interfaces/IDrawBeacon.sol @@ -11,37 +11,33 @@ interface IDrawBeacon { * @notice Emit when a new DrawHistory has been set. * @param newDrawHistory The new DrawHistory address */ - event DrawHistoryTransferred(IDrawHistory indexed newDrawHistory); + event DrawHistoryUpdated(IDrawHistory indexed newDrawHistory); /** * @notice Emit when a draw has opened. - * @param operator User address responsible for opening draw * @param startedAt Start timestamp */ - event BeaconPeriodStarted(address indexed operator, uint64 indexed startedAt); + event BeaconPeriodStarted(uint64 indexed startedAt); /** * @notice Emit when a draw has started. - * @param operator User address responsible for starting draw * @param rngRequestId draw id * @param rngLockBlock Block when draw becomes invalid */ - event DrawStarted(address indexed operator, uint32 indexed rngRequestId, uint32 rngLockBlock); + event DrawStarted(uint32 indexed rngRequestId, uint32 rngLockBlock); /** * @notice Emit when a draw has been cancelled. - * @param operator User address responsible for cancelling draw * @param rngRequestId draw id * @param rngLockBlock Block when draw becomes invalid */ - event DrawCancelled(address indexed operator, uint32 indexed rngRequestId, uint32 rngLockBlock); + event DrawCancelled(uint32 indexed rngRequestId, uint32 rngLockBlock); /** * @notice Emit when a draw has been completed. - * @param operator User address responsible for completing draw * @param randomNumber Random number generated from draw */ - event DrawCompleted(address indexed operator, uint256 randomNumber); + event DrawCompleted(uint256 randomNumber); /** * @notice Emit when a RNG service address is set. diff --git a/contracts/test/DrawBeaconHarness.sol b/contracts/test/DrawBeaconHarness.sol index a7a4ff03..34caa4a0 100644 --- a/contracts/test/DrawBeaconHarness.sol +++ b/contracts/test/DrawBeaconHarness.sol @@ -14,8 +14,9 @@ contract DrawBeaconHarness is DrawBeacon { RNGInterface _rng, uint32 _nextDrawId, uint64 _beaconPeriodStart, - uint32 _drawPeriodSeconds - ) DrawBeacon(_owner, _drawHistory, _rng, _nextDrawId, _beaconPeriodStart, _drawPeriodSeconds) {} + uint32 _drawPeriodSeconds, + uint32 _rngTimeout + ) DrawBeacon(_owner, _drawHistory, _rng, _nextDrawId, _beaconPeriodStart, _drawPeriodSeconds, _rngTimeout) {} uint64 internal time; diff --git a/deploy/deploy.js b/deploy/deploy.js index 3ddc8fc5..bd1ddc17 100644 --- a/deploy/deploy.js +++ b/deploy/deploy.js @@ -148,6 +148,8 @@ module.exports = async (hardhat) => { }); displayResult('PrizeDistributionHistory', tsunamiDrawSettindsHistoryResult); + const rngTimout = 3600 + cyan('\nDeploying DrawBeacon...'); const drawBeaconResult = await deploy('DrawBeacon', { from: deployer, @@ -158,6 +160,7 @@ module.exports = async (hardhat) => { 1, parseInt('' + new Date().getTime() / 1000), 120, // 2 minute intervals + rngTimout ], }); diff --git a/test/DrawBeacon.test.ts b/test/DrawBeacon.test.ts index 02bf493b..14b2fbe6 100644 --- a/test/DrawBeacon.test.ts +++ b/test/DrawBeacon.test.ts @@ -23,6 +23,7 @@ describe('DrawBeacon', () => { let beaconPeriodStart = now(); const beaconPeriodSeconds = 1000; + const rngTimeout = 2000 const nextDrawId = 1; const halfTime = beaconPeriodSeconds / 2; @@ -59,6 +60,7 @@ describe('DrawBeacon', () => { nextDrawId, beaconPeriodStart, beaconPeriodSeconds, + rngTimeout ); }); @@ -71,21 +73,19 @@ describe('DrawBeacon', () => { nextDrawId, beaconPeriodStart, beaconPeriodSeconds, + rngTimeout ); await expect(drawBeacon2.deployTransaction) .to.emit(drawBeacon2, 'Deployed') .withArgs( - drawHistory.address, - rng.address, nextDrawId, - beaconPeriodStart, - beaconPeriodSeconds, + beaconPeriodStart ); await expect(drawBeacon2.deployTransaction) .to.emit(drawBeacon2, 'BeaconPeriodStarted') - .withArgs(wallet.address, beaconPeriodStart); + .withArgs(beaconPeriodStart); }); it('should set the params', async () => { @@ -103,6 +103,7 @@ describe('DrawBeacon', () => { nextDrawId, 0, beaconPeriodSeconds, + rngTimeout ), ).to.be.revertedWith('DrawBeacon/beacon-period-greater-than-zero'); }); @@ -116,6 +117,7 @@ describe('DrawBeacon', () => { nextDrawId, beaconPeriodStart, beaconPeriodSeconds, + rngTimeout ), ).to.be.revertedWith('DrawBeacon/rng-not-zero'); }); @@ -129,6 +131,7 @@ describe('DrawBeacon', () => { 0, beaconPeriodStart, beaconPeriodSeconds, + rngTimeout ), ).to.be.revertedWith('DrawBeacon/next-draw-id-gte-one'); }); @@ -192,6 +195,7 @@ describe('DrawBeacon', () => { nextDrawId, beaconPeriodStart, beaconPeriodSeconds, + rngTimeout ); }); @@ -268,7 +272,7 @@ describe('DrawBeacon', () => { await expect(drawBeacon.cancelDraw()) .to.emit(drawBeacon, 'DrawCancelled') - .withArgs(wallet.address, 11, 1); + .withArgs(11, 1); }); }); @@ -319,11 +323,10 @@ describe('DrawBeacon', () => { expect(await drawBeacon.completeDraw()) .to.emit(drawBeacon, 'DrawCompleted') .withArgs( - wallet.address, '0x6c00000000000000000000000000000000000000000000000000000000000000', ) .and.to.emit(drawBeacon, 'BeaconPeriodStarted') - .withArgs(wallet.address, nextStartTime); + .withArgs(nextStartTime); expect(await drawBeacon.getBeaconPeriodStartedAt()).to.equal(nextStartTime); }); @@ -334,7 +337,7 @@ describe('DrawBeacon', () => { describe('setDrawHistory()', () => { it('should allow the owner to set the draw history', async () => { await expect(drawBeacon.setDrawHistory(wallet2.address)) - .to.emit(drawBeacon, 'DrawHistoryTransferred') + .to.emit(drawBeacon, 'DrawHistoryUpdated') .withArgs(wallet2.address); expect(await drawBeacon.getDrawHistory()).to.equal(wallet2.address); @@ -385,7 +388,7 @@ describe('DrawBeacon', () => { 'DrawBeacon/rng-timeout-gt-60-secs', ); - expect(await drawBeacon.getRngTimeout()).to.equal(1800); + expect(await drawBeacon.getRngTimeout()).to.equal(rngTimeout); }); it('should allow the owner to set the rngTimeout above 60', async () => { await expect(drawBeacon.setRngTimeout(100)) @@ -515,7 +518,7 @@ describe('DrawBeacon', () => { expect(await drawBeacon.getLastRngRequestId()).to.equal(0); }); it('should get the getRngTimeout', async () => { - expect(await drawBeacon.getRngTimeout()).to.equal(1800); + expect(await drawBeacon.getRngTimeout()).to.equal(rngTimeout); }); it('should get the getRngService', async () => { expect(await drawBeacon.getRngService()).to.equal(rng.address);