From 81287365804bed91593c5df5bf8a64e2968072f4 Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt Date: Mon, 25 Mar 2019 05:15:23 -0300 Subject: [PATCH] remove delegationOf from abstract/interface --- contracts/democracy/delegation/Delegation.sol | 37 +------------- .../delegation/DelegationAbstract.sol | 19 ------- .../democracy/delegation/DelegationBase.sol | 49 ++++++++++++++----- test/delegation.js | 8 +-- 4 files changed, 42 insertions(+), 71 deletions(-) diff --git a/contracts/democracy/delegation/Delegation.sol b/contracts/democracy/delegation/Delegation.sol index 8b4bae3..2dd056e 100644 --- a/contracts/democracy/delegation/Delegation.sol +++ b/contracts/democracy/delegation/Delegation.sol @@ -18,10 +18,7 @@ interface Delegation { * @param _who What address to lookup. * @return The address `_who` choosen delegate to. */ - function delegatedTo(address _who) - external - view - returns (address directDelegate); + function delegatedTo(address _who) external view returns (address); /** * @notice Reads `_who` configured delegation at block number `_block` or @@ -30,36 +27,6 @@ interface Delegation { * @param _block Block number of what height in history. * @return The address `_who` choosen delegate to. */ - function delegatedToAt( - address _who, - uint _block - ) - external - view - returns (address directDelegate); - - /** - * @notice Reads the final delegate of `_who` at block number `_block`. - * @param _who Address to lookup. - * @return Final delegate address. - */ - function delegationOf(address _who) - external - view - returns(address finalDelegate); - - /** - * @notice Reads the final delegate of `_who` at block number `_block`. - * @param _who Address to lookup. - * @param _block From what block. - * @return Final delegate address. - */ - function delegationOfAt( - address _who, - uint _block - ) - external - view - returns(address finalDelegate); + function delegatedToAt(address _who, uint _block) external view returns (address); } \ No newline at end of file diff --git a/contracts/democracy/delegation/DelegationAbstract.sol b/contracts/democracy/delegation/DelegationAbstract.sol index 3340750..81ab583 100644 --- a/contracts/democracy/delegation/DelegationAbstract.sol +++ b/contracts/democracy/delegation/DelegationAbstract.sol @@ -105,23 +105,4 @@ contract DelegationAbstract is InstanceAbstract, Delegation { return _who; } } - /** - * @notice Reads the final delegate of `_who` at block number `_block`. - * @param _who Address to lookup. - * @param _block From what block. - * @return Final delegate address. - */ - function findDelegationOfAt( - address _who, - uint _block - ) - internal - view - returns(address finalDelegate) - { - finalDelegate = findDelegatedToAt(_who, _block); - if (finalDelegate != _who) { //_who is delegating to someone? - finalDelegate = findDelegationOfAt(finalDelegate, _block); //yes, load the delegation of _who delegation - } - } } \ No newline at end of file diff --git a/contracts/democracy/delegation/DelegationBase.sol b/contracts/democracy/delegation/DelegationBase.sol index 7b07ba3..55158cb 100644 --- a/contracts/democracy/delegation/DelegationBase.sol +++ b/contracts/democracy/delegation/DelegationBase.sol @@ -40,19 +40,6 @@ contract DelegationBase is DelegationAbstract { return findDelegatedToAt(_who, block.number); } - /** - * @notice Reads the final delegate of `_who` at block number `_block`. - * @param _who Address to lookup. - * @return Final delegate address. - */ - function delegationOf(address _who) - external - view - returns(address) - { - return findDelegationOfAt(_who, block.number); - } - /** * @notice Reads `_who` configured delegation at block number `_block` in this level, * or from parent level if `_who` never defined/defined to parent address. @@ -70,6 +57,19 @@ contract DelegationBase is DelegationAbstract { { return findDelegatedToAt(_who, _block); } + + /** + * @notice Reads the final delegate of `_who` at block number `_block`. + * @param _who Address to lookup. + * @return Final delegate address. + */ + function delegationOf(address _who) + external + view + returns(address) + { + return findDelegationOfAt(_who, block.number); + } /** * @notice Reads the final delegate of `_who` at block number `_block`. @@ -88,4 +88,27 @@ contract DelegationBase is DelegationAbstract { return findDelegationOfAt(_who, _block); } + /** + * @notice Reads the final delegate of `_who` at block number `_block`. + * @param _who Address to lookup. + * @param _block From what block. + * @return Final delegate address. + */ + function findDelegationOfAt( + address _who, + uint _block + ) + internal + view + returns(address finalDelegate) + { + finalDelegate = _who; + address last; + do { + last = finalDelegate; + finalDelegate = findDelegatedToAt(last, _block); + } while (finalDelegate != last); + + } + } \ No newline at end of file diff --git a/test/delegation.js b/test/delegation.js index cd34573..25d0825 100644 --- a/test/delegation.js +++ b/test/delegation.js @@ -2,7 +2,7 @@ const utils = require("../utils/testUtils") const DefaultDelegation = require('Embark/contracts/DefaultDelegation'); const DelegationFactory = require('Embark/contracts/DelegationFactory'); -const Delegation = require('Embark/contracts/Delegation'); +const DelegationBase = require('Embark/contracts/DelegationBase'); config({ contracts: { @@ -58,7 +58,7 @@ contract("DelegationBase", function() { it("creates headless delegation", async function () { let result = await DelegationFactory.methods.createDelegation(utils.zeroAddress).send(); - var NoDefaultDelegation = new web3.eth.Contract(Delegation._jsonInterface, result.events.InstanceCreated.returnValues[0]); + var NoDefaultDelegation = new web3.eth.Contract(DelegationBase._jsonInterface, result.events.InstanceCreated.returnValues[0]); result = await NoDefaultDelegation.methods.delegatedTo(accounts[0]).call() assert.equal(result, accounts[0]) result = await NoDefaultDelegation.methods.delegationOf(accounts[0]).call() @@ -67,7 +67,7 @@ contract("DelegationBase", function() { it("creates root delegation", async function () { let result = await DelegationFactory.methods.createDelegation(DefaultDelegation._address).send(); - RootDelegation = new web3.eth.Contract(Delegation._jsonInterface, result.events.InstanceCreated.returnValues[0]); + RootDelegation = new web3.eth.Contract(DelegationBase._jsonInterface, result.events.InstanceCreated.returnValues[0]); defaultDelegate = await DefaultDelegation.methods.defaultDelegate().call(); }) @@ -132,7 +132,7 @@ contract("DelegationBase", function() { it("creates child delegation", async function () { let result = await DelegationFactory.methods.createDelegation(RootDelegation._address).send(); - ChildDelegation = new web3.eth.Contract(Delegation._jsonInterface, result.events.InstanceCreated.returnValues[0]); + ChildDelegation = new web3.eth.Contract(DelegationBase._jsonInterface, result.events.InstanceCreated.returnValues[0]); }) it("Child Delegate to Default", async function () {