Skip to content

Commit

Permalink
Draw Beacon: General Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
asselstine committed Oct 6, 2021
1 parent 2c2879f commit 2e6b95f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 46 deletions.
48 changes: 24 additions & 24 deletions contracts/DrawBeacon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 ============ */
Expand Down Expand Up @@ -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 ============ */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -444,7 +444,7 @@ contract DrawBeacon is IDrawBeacon, Ownable {

drawHistory = _newDrawHistory;

emit DrawHistoryTransferred(_newDrawHistory);
emit DrawHistoryUpdated(_newDrawHistory);

return _newDrawHistory;
}
Expand Down
14 changes: 5 additions & 9 deletions contracts/interfaces/IDrawBeacon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions contracts/test/DrawBeaconHarness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions deploy/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ module.exports = async (hardhat) => {
});
displayResult('PrizeDistributionHistory', tsunamiDrawSettindsHistoryResult);

const rngTimout = 3600

cyan('\nDeploying DrawBeacon...');
const drawBeaconResult = await deploy('DrawBeacon', {
from: deployer,
Expand All @@ -158,6 +160,7 @@ module.exports = async (hardhat) => {
1,
parseInt('' + new Date().getTime() / 1000),
120, // 2 minute intervals
rngTimout
],
});

Expand Down
25 changes: 14 additions & 11 deletions test/DrawBeacon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('DrawBeacon', () => {

let beaconPeriodStart = now();
const beaconPeriodSeconds = 1000;
const rngTimeout = 2000
const nextDrawId = 1;

const halfTime = beaconPeriodSeconds / 2;
Expand Down Expand Up @@ -59,6 +60,7 @@ describe('DrawBeacon', () => {
nextDrawId,
beaconPeriodStart,
beaconPeriodSeconds,
rngTimeout
);
});

Expand All @@ -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 () => {
Expand All @@ -103,6 +103,7 @@ describe('DrawBeacon', () => {
nextDrawId,
0,
beaconPeriodSeconds,
rngTimeout
),
).to.be.revertedWith('DrawBeacon/beacon-period-greater-than-zero');
});
Expand All @@ -116,6 +117,7 @@ describe('DrawBeacon', () => {
nextDrawId,
beaconPeriodStart,
beaconPeriodSeconds,
rngTimeout
),
).to.be.revertedWith('DrawBeacon/rng-not-zero');
});
Expand All @@ -129,6 +131,7 @@ describe('DrawBeacon', () => {
0,
beaconPeriodStart,
beaconPeriodSeconds,
rngTimeout
),
).to.be.revertedWith('DrawBeacon/next-draw-id-gte-one');
});
Expand Down Expand Up @@ -192,6 +195,7 @@ describe('DrawBeacon', () => {
nextDrawId,
beaconPeriodStart,
beaconPeriodSeconds,
rngTimeout
);
});

Expand Down Expand Up @@ -268,7 +272,7 @@ describe('DrawBeacon', () => {

await expect(drawBeacon.cancelDraw())
.to.emit(drawBeacon, 'DrawCancelled')
.withArgs(wallet.address, 11, 1);
.withArgs(11, 1);
});
});

Expand Down Expand Up @@ -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);
});
Expand All @@ -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);
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2e6b95f

Please sign in to comment.