-
Notifications
You must be signed in to change notification settings - Fork 21
add forwarder scheme #558
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
Merged
Merged
add forwarder scheme #558
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,58 @@ | ||
| pragma solidity ^0.4.25; | ||
|
|
||
| import "../controller/ControllerInterface.sol"; | ||
| import "openzeppelin-solidity/contracts/ownership/Ownable.sol"; | ||
|
|
||
| /** | ||
| * @title A scheme to forward a call to a dao. | ||
| * The scheme can unregister itself when its expirationTime reached. | ||
| */ | ||
|
|
||
|
|
||
| contract Forwarder is Ownable { | ||
|
|
||
| Avatar public avatar; | ||
| uint public expirationTime; | ||
|
|
||
| /** | ||
| * @dev forwardCall forward a call to the dao controller | ||
| */ | ||
| function () external onlyOwner { | ||
| // solium-disable-next-line security/no-block-members | ||
| require(expirationTime > now, "expirationTime > now"); | ||
| // solium-disable-next-line security/no-low-level-calls | ||
| bool result = avatar.owner().call(msg.data); | ||
| // solium-disable-next-line security/no-inline-assembly | ||
| assembly { | ||
| // Copy the returned data. | ||
| returndatacopy(0, 0, returndatasize) | ||
|
|
||
| switch result | ||
| // call returns 0 on error. | ||
| case 0 { revert(0, returndatasize) } | ||
| default { return(0, returndatasize) } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @dev initialize | ||
| * @param _avatar the avatar of the dao to forward the call to | ||
| * @param _expirationTime the expirationTime to forwardCall | ||
| */ | ||
| function initialize(Avatar _avatar, uint _expirationTime) external onlyOwner { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here there is basically no "time bomb" as the owner can change it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix 51552e6 |
||
| require(avatar == Avatar(0), "can be called only one time"); | ||
| require(_avatar != Avatar(0), "avatar cannot be zero"); | ||
| avatar = _avatar; | ||
| expirationTime = _expirationTime; | ||
| } | ||
|
|
||
| /** | ||
| * @dev unregisterSelf function | ||
| * @return bool | ||
| */ | ||
| function unregisterSelf() public returns(bool) { | ||
| // solium-disable-next-line security/no-block-members | ||
| require(expirationTime <= now, "expirationTime <= now"); | ||
| return ControllerInterface(avatar.owner()).unregisterSelf(address(avatar)); | ||
| } | ||
| } | ||
This file contains hidden or 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,133 @@ | ||
| const helpers = require('./helpers'); | ||
| const DaoCreator = artifacts.require("./DaoCreator.sol"); | ||
| const ControllerCreator = artifacts.require("./ControllerCreator.sol"); | ||
| const constants = require('./constants'); | ||
| const StandardTokenMock = artifacts.require('./test/StandardTokenMock.sol'); | ||
| var Forwarder = artifacts.require("./Forwarder.sol"); | ||
| var ControllerInterface = artifacts.require("./ControllerInterface.sol"); | ||
|
|
||
| const setup = async function (accounts, | ||
| _expirationTime = 300) | ||
| { | ||
| var testSetup = new helpers.TestSetup(); | ||
| testSetup.biddingToken = await StandardTokenMock.new(accounts[0], web3.utils.toWei('100', "ether")); | ||
| var controllerCreator = await ControllerCreator.new({gas: constants.ARC_GAS_LIMIT}); | ||
| testSetup.daoCreator = await DaoCreator.new(controllerCreator.address,{gas:constants.ARC_GAS_LIMIT}); | ||
| testSetup.org = await helpers.setupOrganization(testSetup.daoCreator,accounts[0],1000,1000); | ||
| testSetup.forwarder = await Forwarder.new(); | ||
|
|
||
| testSetup.expirationTime = (await web3.eth.getBlock("latest")).timestamp + _expirationTime; | ||
|
|
||
| await testSetup.forwarder.initialize(testSetup.org.avatar.address, | ||
| testSetup.expirationTime); | ||
|
|
||
| var permissions = "0x0000001f"; | ||
| await testSetup.daoCreator.setSchemes(testSetup.org.avatar.address, | ||
| [accounts[0],testSetup.forwarder.address], | ||
| [web3.utils.asciiToHex("0"),web3.utils.asciiToHex("0")], | ||
| [permissions,permissions]); | ||
| return testSetup; | ||
| }; | ||
|
|
||
| contract('Forwarder', accounts => { | ||
| it("initialize", async () => { | ||
| let testSetup = await setup(accounts); | ||
|
|
||
| assert.equal(await testSetup.forwarder.avatar(),testSetup.org.avatar.address); | ||
| assert.equal(await testSetup.forwarder.expirationTime(),testSetup.expirationTime); | ||
| }); | ||
|
|
||
| it("cannot initialize twice", async () => { | ||
| let testSetup = await setup(accounts); | ||
| try { | ||
| await testSetup.forwarder.initialize(testSetup.org.avatar.address, | ||
| testSetup.expirationTime); | ||
| assert(false, "cannot initialize twice"); | ||
| } catch(error) { | ||
| helpers.assertVMException(error); | ||
| } | ||
| }); | ||
|
|
||
| it("forwardCall (fallback)", async () => { | ||
| let testSetupA = await setup(accounts); | ||
| let testSetupB = await setup(accounts); | ||
| // transferOwnership of testSetupA.forwarder to testSetupB avatar | ||
| await testSetupA.forwarder.transferOwnership(testSetupB.org.avatar.address); | ||
| //do generic call from testSetupB controller to testSetupA controller to "registerScheme" | ||
| let controllerB = await ControllerInterface.at(await testSetupB.org.avatar.owner()); | ||
| let controllerA = await ControllerInterface.at(await testSetupA.org.avatar.owner()); | ||
| const encodeABI = await new web3.eth.Contract(controllerA.abi). | ||
| methods. | ||
| registerScheme(accounts[1],helpers.NULL_HASH,"0x0000001f",testSetupA.org.avatar.address). | ||
| encodeABI(); | ||
| assert.equal(await controllerA.isSchemeRegistered(accounts[1],testSetupA.org.avatar.address),false); | ||
| await controllerB.genericCall(testSetupA.forwarder.address,encodeABI,testSetupB.org.avatar.address); | ||
| //check that accounts[1] register as scheme at controllerA. | ||
| assert.equal(await controllerA.isSchemeRegistered(accounts[1],testSetupA.org.avatar.address),true); | ||
|
|
||
| }); | ||
|
|
||
| it("forwardCall (fallback) -check expirationTime", async () => { | ||
| let testSetupA = await setup(accounts); | ||
| let testSetupB = await setup(accounts); | ||
| // transferOwnership of testSetupA.forwarder to testSetupB avatar | ||
| await testSetupA.forwarder.transferOwnership(testSetupB.org.avatar.address); | ||
| //do generic call from testSetupB controller to testSetupA controller to "registerScheme" | ||
| let controllerB = await ControllerInterface.at(await testSetupB.org.avatar.owner()); | ||
| let controllerA = await ControllerInterface.at(await testSetupA.org.avatar.owner()); | ||
| const encodeABI = await new web3.eth.Contract(controllerA.abi). | ||
| methods. | ||
| registerScheme(accounts[1],helpers.NULL_HASH,"0x0000001f",testSetupA.org.avatar.address). | ||
| encodeABI(); | ||
| await helpers.increaseTime(301); | ||
|
|
||
| try { | ||
| await controllerB.genericCall(testSetupA.forwarder.address,encodeABI,testSetupB.org.avatar.address); | ||
|
|
||
| assert(false, "expired"); | ||
| } catch(error) { | ||
| helpers.assertVMException(error); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
|
|
||
| it("forwardCall (fallback) is onlyOwner ", async () => { | ||
| let testSetupA = await setup(accounts); | ||
| let testSetupB = await setup(accounts); | ||
| //do generic call from testSetupB controller to testSetupA controller to "registerScheme" | ||
| let controllerB = await ControllerInterface.at(await testSetupB.org.avatar.owner()); | ||
| let controllerA = await ControllerInterface.at(await testSetupA.org.avatar.owner()); | ||
| const encodeABI = await new web3.eth.Contract(controllerA.abi). | ||
| methods. | ||
| registerScheme(accounts[1],helpers.NULL_HASH,"0x0000001f",testSetupA.org.avatar.address). | ||
| encodeABI(); | ||
| try { | ||
| await controllerB.genericCall(testSetupA.forwarder.address,encodeABI,testSetupB.org.avatar.address); | ||
|
|
||
| assert(false, "forwardCall is onlyOwner"); | ||
| } catch(error) { | ||
| helpers.assertVMException(error); | ||
| } | ||
| }); | ||
|
|
||
| it("unregisterSelf", async () => { | ||
| let testSetupA = await setup(accounts); | ||
| let testSetupB = await setup(accounts); | ||
| let controllerA = await ControllerInterface.at(await testSetupA.org.avatar.owner()); | ||
| // transferOwnership of testSetupA.forwarder to testSetupB avatar | ||
| await testSetupA.forwarder.transferOwnership(testSetupB.org.avatar.address); | ||
| assert.equal(await controllerA.isSchemeRegistered(testSetupA.forwarder.address,testSetupA.org.avatar.address),true); | ||
| try { | ||
| await testSetupA.forwarder.unregisterSelf(); | ||
| assert(false, "expirationTime did not passed"); | ||
| } catch(error) { | ||
| helpers.assertVMException(error); | ||
| } | ||
| assert.equal(await controllerA.isSchemeRegistered(testSetupA.forwarder.address,testSetupA.org.avatar.address),true); | ||
| await helpers.increaseTime(301); | ||
| await testSetupA.forwarder.unregisterSelf(); | ||
| assert.equal(await controllerA.isSchemeRegistered(testSetupA.forwarder.address,testSetupA.org.avatar.address),false); | ||
|
|
||
| }); | ||
| }); |
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.
Why Avatar?
Why not make generic and initialize some address.
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.
the avatar is needed to call for unregisterSelf .
we can add another address to make it more generic though I am not sure it is needed.
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.
Not needed. This is OK.