-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow a specified address to disable/enable the Exchange #1467
Merged
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8c7a34a
Add Freezable contract
m-chrzan bf96502
Enable freezing of the exchange
m-chrzan 2975bb1
Move Freezable to new baklava directory
m-chrzan a756aa1
Merge branch 'master' into m-chrzan/toggle-exchange
m-chrzan 4a546f8
Add natspecs
m-chrzan 906d050
Add comment
m-chrzan 8b345f6
Merge branch 'master' into m-chrzan/toggle-exchange
m-chrzan cf91eeb
Appease the linter
m-chrzan b4db704
Merge branch 'master' into m-chrzan/toggle-exchange
m-chrzan 7df038d
Order arguments correctly
m-chrzan 5fb6d03
Merge branch 'master' into m-chrzan/toggle-exchange
m-chrzan fce5c0c
Merge branch 'master' into m-chrzan/toggle-exchange
m-chrzan f2231e1
Merge branch 'master' into m-chrzan/toggle-exchange
m-chrzan 0f93508
Merge branch 'master' into m-chrzan/toggle-exchange
m-chrzan e4b9d63
Remove non-throwing modifier
m-chrzan 2308c48
Merge branch 'master' into m-chrzan/toggle-exchange
m-chrzan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
pragma solidity ^0.5.3; | ||
|
||
|
||
contract Freezable { | ||
bool public frozen; | ||
address public freezer; | ||
|
||
// onlyFreezer functions can only be called by the specified `freezer` address | ||
modifier onlyFreezer() { | ||
require(msg.sender == freezer); | ||
_; | ||
} | ||
|
||
// onlyWhenNotFrozen functions can only be called when `frozen` is false, otherwise they are | ||
// noops. | ||
modifier onlyWhenNotFrozen() { | ||
if (!frozen) { | ||
_; | ||
} | ||
} | ||
|
||
// onlyWhenNotFrozenOrThrow functions can only be called when `frozen` is false, otherwise they | ||
// will revert. | ||
modifier onlyWhenNotFrozenOrThrow() { | ||
require(!frozen); | ||
_; | ||
} | ||
|
||
/** | ||
* @notice Sets the address that is allowed to freeze/unfreeze the contract. | ||
* @param _freezer The address that is allowed to freeze/unfree the contract | ||
* @dev This function is `internal` and leaves its permissioning up to the inheriting contract. | ||
*/ | ||
function _setFreezer(address _freezer) internal { | ||
freezer = _freezer; | ||
} | ||
|
||
/** | ||
* @notice Freezes the contract, disabling `onlyWhenNotFrozen` functions. | ||
*/ | ||
function freeze() external onlyFreezer { | ||
frozen = true; | ||
} | ||
|
||
/** | ||
* @notice Unreezes the contract, enabling `onlyWhenNotFrozen` functions. | ||
*/ | ||
function unfreeze() external onlyFreezer { | ||
frozen = false; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/protocol/contracts/baklava/test/FreezableTest.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
pragma solidity ^0.5.8; | ||
|
||
import "../Freezable.sol"; | ||
|
||
|
||
contract FreezableTest is Freezable { | ||
event FunctionCalled(); | ||
|
||
function setFreezer(address _freezer) external { | ||
_setFreezer(_freezer); | ||
} | ||
|
||
function freezableFunction() external onlyWhenNotFrozen { | ||
emit FunctionCalled(); | ||
} | ||
|
||
function freezableFunctionWithThrow() external onlyWhenNotFrozenOrThrow { | ||
emit FunctionCalled(); | ||
} | ||
|
||
function nonfreezableFunction() external { | ||
emit FunctionCalled(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { assertSameAddress, assertLogMatches2, assertRevert } from '@celo/protocol/lib/test-utils' | ||
import { FreezableTestInstance } from 'types' | ||
|
||
contract('Freezable', (accounts: string[]) => { | ||
const FreezableTest = artifacts.require('FreezableTest') | ||
let freezableTest: FreezableTestInstance | ||
|
||
beforeEach(async () => { | ||
freezableTest = await FreezableTest.new() | ||
freezableTest.setFreezer(accounts[0]) | ||
}) | ||
|
||
describe('_setFreezer', () => { | ||
it('should allow owner to change the freezer', async () => { | ||
// _setFreezer is internal in Freezable, FreezableTest wraps around it with setFreezer | ||
await freezableTest.setFreezer(accounts[1]) | ||
const freezer = await freezableTest.freezer() | ||
assertSameAddress(freezer, accounts[1]) | ||
}) | ||
}) | ||
|
||
describe('when the contract is not frozen', () => { | ||
it('should allow freezable functions to be called', async () => { | ||
const resp = await freezableTest.freezableFunction() | ||
assert.equal(resp.logs.length, 1) | ||
const log = resp.logs[0] | ||
assertLogMatches2(log, { | ||
event: 'FunctionCalled', | ||
args: {}, | ||
}) | ||
}) | ||
|
||
it('should allow freezable functions that throw to be called', async () => { | ||
const resp = await freezableTest.freezableFunctionWithThrow() | ||
assert.equal(resp.logs.length, 1) | ||
const log = resp.logs[0] | ||
assertLogMatches2(log, { | ||
event: 'FunctionCalled', | ||
args: {}, | ||
}) | ||
}) | ||
|
||
it('should allow non-freezable functions to be called', async () => { | ||
const resp = await freezableTest.nonfreezableFunction() | ||
assert.equal(resp.logs.length, 1) | ||
const log = resp.logs[0] | ||
assertLogMatches2(log, { | ||
event: 'FunctionCalled', | ||
args: {}, | ||
}) | ||
}) | ||
}) | ||
|
||
describe('freeze', () => { | ||
it('should allow freezer to freeze the contract', async () => { | ||
await freezableTest.freeze() | ||
const frozen = await freezableTest.frozen() | ||
assert.isTrue(frozen) | ||
}) | ||
|
||
it('should not allow a non-freezer to freeze the contract', async () => { | ||
await assertRevert(freezableTest.freeze({ from: accounts[1] })) | ||
}) | ||
}) | ||
|
||
describe('unfreeze', () => { | ||
beforeEach(async () => { | ||
freezableTest.freeze() | ||
}) | ||
|
||
it('should allow freezer to unfreeze the contract', async () => { | ||
await freezableTest.unfreeze() | ||
const frozen = await freezableTest.frozen() | ||
assert.isFalse(frozen) | ||
}) | ||
|
||
it('should not allow a non-freezer to unfreeze the contract', async () => { | ||
await assertRevert(freezableTest.freeze({ from: accounts[1] })) | ||
}) | ||
}) | ||
|
||
describe('when the contract is frozen', () => { | ||
beforeEach(async () => { | ||
await freezableTest.freeze() | ||
}) | ||
|
||
it('should not run a freezable function', async () => { | ||
const resp = await freezableTest.freezableFunction() | ||
assert.equal(resp.logs.length, 0) | ||
}) | ||
|
||
it('should revert a freezable function that throws', async () => { | ||
await assertRevert(freezableTest.freezableFunctionWithThrow()) | ||
}) | ||
|
||
it('should not affect a non-freezable function', async () => { | ||
const resp = await freezableTest.nonfreezableFunction() | ||
assert.equal(resp.logs.length, 1) | ||
const log = resp.logs[0] | ||
assertLogMatches2(log, { | ||
event: 'FunctionCalled', | ||
args: {}, | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary? I would consider cutting this function and renaming
onlyWhenNotFrozenOrThrow
toonlyWhenNotFrozen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should have cleared this up - this is already looking forward to the next change, where
distributeEpochPayments
will need to be frozen. By not reverting when that function is frozen, I think we'll be able to contain this temporary change for Baklava to smart contracts.I could see an argument for reverting being the default semantic, happy to rename this one to
onlyWhenNotFrozenNoThrow
.WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure we don't want to revert when we freeze epoch rewards for TGCSO? Reverting would prevent any proof-of-stake action from happening in
Finalize
, e.g. validator score changes, payments, etc.