Skip to content

Commit

Permalink
Change exitTimeExpiry to exitPeriodLen for clarity
Browse files Browse the repository at this point in the history
* Added more comments to initExit.js and finalizeExit.js
* Formatted code into blocks for clarity
  • Loading branch information
nigel-heeral authored and skmgoldin committed Jul 26, 2018
1 parent 17ba024 commit 5fb6d3a
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 95 deletions.
2 changes: 1 addition & 1 deletion conf/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"voteQuorum": 50,
"pVoteQuorum": 50,
"exitTimeDelay": 600,
"exitTimeExpiry": 600
"exitPeriodLen": 600
},
"name": "The TestChain Registry",
"token": {
Expand Down
6 changes: 3 additions & 3 deletions contracts/Parameterizer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ contract Parameterizer {
// type of majority out of 100 necessary for proposal success in parameterizer
set("pVoteQuorum", _parameters[11]);

// minimum length of time between initExit & finalizeExit
// minimum length of time user has to wait to exit the registry
set("exitTimeDelay", _parameters[12]);

// maximum length of time between initExit & finalizeExit
set("exitTimeExpiry", _parameters[13]);
// maximum length of time user can wait to exit the registry
set("exitPeriodLen", _parameters[13]);
}

// -----------------------
Expand Down
32 changes: 6 additions & 26 deletions contracts/Registry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,6 @@ contract Registry {
emit _Withdrawal(_listingHash, _amount, listing.unstakedDeposit, msg.sender);
}

/**
@dev Allows the owner of a listingHash to remove the listingHash from the whitelist
Returns all tokens to the owner of the listingHash
@param _listingHash A listingHash msg.sender is the owner of.
*/
// function exit(bytes32 _listingHash) external {
// Listing storage listing = listings[_listingHash];

// require(msg.sender == listing.owner);
// require(isWhitelisted(_listingHash));

// // Cannot exit during ongoing challenge
// require(listing.challengeID == 0 || challenges[listing.challengeID].resolved);

// // Remove listingHash & return tokens
// resetListing(_listingHash);
// emit _ListingWithdrawn(_listingHash);
// }


/**
@dev Initialize an exit timer for a listing to leave the whitelist
@param _listingHash A listing hash msg.sender is the owner of
Expand All @@ -168,14 +148,14 @@ contract Registry {

// Cannot exit during ongoing challenge
require(listing.challengeID == 0 || challenges[listing.challengeID].resolved);
// Ensure that you either never called initExit() or your expiry time is up
// Ensure that you either never called initExit() or exitPeriodLen passed
require(listing.exitTime == 0 || now >
listing.exitTime.add(parameterizer.get("exitTimeExpiry")));
listing.exitTime.add(parameterizer.get("exitPeriodLen")));

// Set when the listing may be removed from the whitelist
listing.exitTime = now.add(parameterizer.get("exitTimeDelay"));
emit _ExitInitialized(_listingHash, listing.exitTime,
listing.exitTime.add(parameterizer.get("exitTimeExpiry")), msg.sender);
listing.exitTime.add(parameterizer.get("exitPeriodLen")), msg.sender);
}

/**
Expand All @@ -195,10 +175,10 @@ contract Registry {
require(listing.exitTime > 0);

// Get the time when the exit is no longer valid
uint timeExpired = listing.exitTime.add(parameterizer.get("exitTimeExpiry"));
uint timeExpired = listing.exitTime.add(parameterizer.get("exitPeriodLen"));

// Time to exit has to be after exit delay but before the exit expiry time
require((listing.exitTime < now) && (now < timeExpired));
// Time to exit has to be after exit delay but before the exitPeriodLen is over
require(listing.exitTime < now && now < timeExpired);

resetListing(_listingHash);
emit _ListingWithdrawn(_listingHash, msg.sender);
Expand Down
2 changes: 1 addition & 1 deletion test/ParameterizerFactory/newParameterizerBYOToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ contract('ParameterizerFactory', (accounts) => {
paramConfig.voteQuorum,
paramConfig.pVoteQuorum,
paramConfig.exitTimeDelay,
paramConfig.exitTimeExpiry,
paramConfig.exitPeriodLen,
];
const parameterizerReceipt = await parameterizerFactory
.newParameterizerBYOToken(token.address, parameters, { from: accounts[0] });
Expand Down
2 changes: 1 addition & 1 deletion test/ParameterizerFactory/newParameterizerWithToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ contract('ParameterizerFactory', (accounts) => {
paramConfig.voteQuorum,
paramConfig.pVoteQuorum,
paramConfig.exitTimeDelay,
paramConfig.exitTimeExpiry,
paramConfig.exitPeriodLen,
];
const parameterizerReceipt = await parameterizerFactory.newParameterizerWithToken(
tokenParams.supply,
Expand Down
2 changes: 1 addition & 1 deletion test/RegistryFactory/newRegistryBYOToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ contract('RegistryFactory', (accounts) => {
paramConfig.voteQuorum,
paramConfig.pVoteQuorum,
paramConfig.exitTimeDelay,
paramConfig.exitTimeExpiry,
paramConfig.exitPeriodLen,
];

// new registry using factory/proxy
Expand Down
2 changes: 1 addition & 1 deletion test/RegistryFactory/newRegistryWithToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ contract('RegistryFactory', (accounts) => {
paramConfig.voteQuorum,
paramConfig.pVoteQuorum,
paramConfig.exitTimeDelay,
paramConfig.exitTimeExpiry,
paramConfig.exitPeriodLen,
];

// new registry using factory/proxy
Expand Down
69 changes: 31 additions & 38 deletions test/registry/finalizeExit.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,38 @@ contract('Registry', (accounts) => {
});

it('should allow a listing to exit when no challenge exists', async () => {
// Adding an application to the whitelist
const listing = utils.getListingHash('google.com');

const initialApplicantTokenHoldings = await token.balanceOf.call(applicant);

await utils.addToWhitelist(listing, paramConfig.minDeposit, applicant, registry);

const isWhitelisted = await registry.isWhitelisted.call(listing);
assert.strictEqual(isWhitelisted, true, 'the listing was not added to the registry');

// Exiting the whitelist
await registry.initExit(listing, { from: applicant });

await utils.increaseTime(paramConfig.exitTimeDelay + 1);
await registry.finalizeExit(listing, { from: applicant });

const isWhitelistedAfterExit = await registry.isWhitelisted.call(listing);
assert.strictEqual(isWhitelistedAfterExit, false, 'the listing was not removed on exit');

const finalApplicantTokenHoldings = await token.balanceOf.call(applicant);
assert.strictEqual(
initialApplicantTokenHoldings.toString(10),
finalApplicantTokenHoldings.toString(10),
'the applicant\'s tokens were not returned to them after exiting the registry',
);

// Make sure resetListing(), called in finalizeExit() correctly removed the listing
const listingStruct = await registry.listings.call(listing);
assert.strictEqual(listingStruct[5].toString(), '0', 'exit time did not reset');
});

it('should not allow a listing to finalize exit when exit was not initialized', async () => {
// Adding an application to the whitelist
const listing = utils.getListingHash('youtube.com');

const initialApplicantTokenHoldings = await token.balanceOf.call(applicant);

await utils.addToWhitelist(listing, paramConfig.minDeposit, applicant, registry);

const isWhitelisted = await registry.isWhitelisted.call(listing);
assert.strictEqual(isWhitelisted, true, 'the listing was not added to the registry');

// Trying to finalize an exit without ever calling initExit()
try {
await registry.finalizeExit(listing, { from: applicant });
assert(false, 'exit succeeded when it should have failed due to exit not being initialized');
Expand All @@ -78,26 +73,26 @@ contract('Registry', (accounts) => {
initialApplicantTokenHoldings.sub(paramConfig.minDeposit).toString(),
'the applicant\'s tokens were returned in spite of failing to exit',
);

// Make sure the listing did not successfully initialize exit
const listingStruct = await registry.listings.call(listing);
assert.strictEqual(listingStruct[5].toString(), '0', 'exit time was initialized');
assert.strictEqual(listingStruct[5].toString(), '0', 'exit time was initialized even though initExit() was never called');
});

it('should not allow a listing to finalize exit when time is not up', async () => {
it('should not allow a listing to finalize exit during the waiting period', async () => {
// Adding an application to the whitelist
const listing = utils.getListingHash('hangouts.com');

const initialApplicantTokenHoldings = await token.balanceOf.call(applicant);

await utils.addToWhitelist(listing, paramConfig.minDeposit, applicant, registry);

const isWhitelisted = await registry.isWhitelisted.call(listing);
assert.strictEqual(isWhitelisted, true, 'the listing was not added to the registry');

await registry.initExit(listing, { from: applicant });
// blockTimestamp is used to calculate when the applicant's exit time is up
const blockTimestamp = new BigNumber(await utils.getBlockTimestamp());
// Trying to finalize exit during waiting period
try {
await registry.finalizeExit(listing, { from: applicant });
assert(false, 'exit succeeded when it should have failed due to time not being up');
assert(false, 'exit succeeded when it should have failed becuase the user called finalizeExit before the delay period was over');
} catch (err) {
const errMsg = err.toString();
assert(utils.isEVMException(err), errMsg);
Expand All @@ -111,28 +106,29 @@ contract('Registry', (accounts) => {
initialApplicantTokenHoldings.sub(paramConfig.minDeposit).toString(),
'the applicant\'s tokens were returned in spite of failing to exit',
);
// Make sure exitTimeDelay was correctly set
const listingStruct = await registry.listings.call(listing);
assert.strictEqual(listingStruct[5].toString(), blockTimestamp.add(paramConfig.exitTimeDelay).toString(), 'exit time was not initialized');
});

it('should not allow a listing to finalize an exit when a challenge does exist', async () => {
// Adding an application to the whitelist
const listing = utils.getListingHash('520.com');

const initialApplicantTokenHoldings = await token.balanceOf.call(applicant);

await utils.addToWhitelist(listing, paramConfig.minDeposit, applicant, registry);

const isWhitelisted = await registry.isWhitelisted.call(listing);
assert.strictEqual(isWhitelisted, true, 'the listing was not added to the registry');

await registry.initExit(listing, { from: applicant });
// blockTimestamp is used to calculate when the applicant's exit time is up
const blockTimestamp = new BigNumber(await utils.getBlockTimestamp());
await utils.increaseTime(paramConfig.exitTimeDelay + 1);
// Challenge the listing
await registry.challenge(listing, '', { from: challenger });
// Make an assertion to prove that challenge does exit
// Assert that challenge does exit
const initialListingStruct = await registry.listings.call(listing);
assert.notStrictEqual(initialListingStruct[4].toString(), '0', 'Challenge was never created');
// Trying to finalize an exit while there is an ongoing challenge
try {
await registry.finalizeExit(listing, { from: applicant });
assert(false, 'exit succeeded when it should have failed');
Expand All @@ -157,27 +153,25 @@ contract('Registry', (accounts) => {
assert.strictEqual(listingStruct[5].toString(), blockTimestamp.add(paramConfig.exitTimeDelay).toString(), 'exit time was not initialized');
});

it('should not allow a listing to finalize an exit when exitTimeExpiry has elapsed', async () => {
it('should not allow a listing to finalize an exit when exitPeriodLen has elapsed', async () => {
// Adding an application to the whitelist
const listing = utils.getListingHash('620-200.com');

const initialApplicantTokenHoldings = await token.balanceOf.call(applicant);
await utils.addToWhitelist(listing, paramConfig.minDeposit, applicant, registry);

const isWhitelisted = await registry.isWhitelisted.call(listing);
assert.strictEqual(isWhitelisted, true, 'the listing was not added to the registry');

// Initialize exit and advance time passed exitPeriodLen
await registry.initExit(listing, { from: applicant });

// blockTimestamp is used to calculate when the applicant's exit time is up
const blockTimestamp = new BigNumber(await utils.getBlockTimestamp());

await utils.increaseTime(paramConfig.exitTimeDelay + 1);
await utils.increaseTime(paramConfig.exitTimeExpiry + 1);
await utils.increaseTime(paramConfig.exitPeriodLen + 1);
const listingStruct = await registry.listings.call(listing);

try {
await registry.finalizeExit(listing, { from: applicant });
assert(false, 'exit succeeded when it should have failed since exitTimeExpiry elapsed');
assert(false, 'exit succeeded when it should have failed since exitPeriodLen elapsed');
} catch (err) {
const errMsg = err.toString();
assert(utils.isEVMException(err), errMsg);
Expand All @@ -186,7 +180,7 @@ contract('Registry', (accounts) => {
assert.strictEqual(
isWhitelistedAfterExit,
true,
'the listing was able to exit since exitTimeExpiry elapsed',
'the listing was able to exit since exitPeriodLen elapsed',
);
const finalApplicantTokenHoldings = await token.balanceOf.call(applicant);
assert.strictEqual(
Expand All @@ -199,28 +193,27 @@ contract('Registry', (accounts) => {
});

it('should allow a listing to finalize after re-initializing a previous exit', async () => {
// Add an application to the whitelsit
const listing = utils.getListingHash('720-300.com');

const initialApplicantTokenHoldings = await token.balanceOf.call(applicant);

await utils.addToWhitelist(listing, paramConfig.minDeposit, applicant, registry);

const isWhitelisted = await registry.isWhitelisted.call(listing);
assert.strictEqual(isWhitelisted, true, 'the listing was not added to the registry');

// Initialize exit and fast forward past expiry date
// Initialize exit and fast forward past exitPeriodLen
await registry.initExit(listing, { from: applicant });
await utils.increaseTime(paramConfig.exitTimeDelay + 1);
await utils.increaseTime(paramConfig.exitTimeExpiry + 1);
await utils.increaseTime(paramConfig.exitPeriodLen + 1);
// finalizeExit should fail since exitPeriodLen has passed
try {
await registry.finalizeExit(listing, { from: applicant });
assert(false, 'exit succeeded when it should have failed since exitTimeExpiry elapsed');
assert(false, 'exit succeeded when it should have failed since exitPeriodLen elapsed');
} catch (err) {
const errMsg = err.toString();
assert(utils.isEVMException(err), errMsg);
}

// Re-initialize the exit and finalize the exit before the expiry time
// Re-initialize the exit and finalize exit
await registry.initExit(listing, { from: applicant });
await utils.increaseTime(paramConfig.exitTimeDelay + 1);
await registry.finalizeExit(listing, { from: applicant });
Expand All @@ -229,7 +222,7 @@ contract('Registry', (accounts) => {
assert.strictEqual(
isWhitelistedAfterExit,
false,
'the listing was not able to exit even though exitTimeExpiry did not elapse',
'the listing was not able to exit even though exitPeriodLen did not elapse',
);
const finalApplicantTokenHoldings = await token.balanceOf.call(applicant);
assert.strictEqual(
Expand Down
Loading

0 comments on commit 5fb6d3a

Please sign in to comment.