Feature to forbid to override methods and to derive subcontracts

ametenameten Member Posts: 7
Is there plan to add feature into Solidity to forbid to override methods and to derive subcontracts? Namely, I am talking about the same feature as available with keywords 'final' in Java and 'sealed' in c# applied to methods and classes.

Comments

  • prubypruby Member Posts: 8
    What do you want to accomplish? While it might help indicate your intended use within your own codebase, this isn't going to have any impact on the actual network. Inheritance doesn't exist at that layer, and if people want to copy your bytecode they will.
  • ametenameten Member Posts: 7
    Probably I am trying to solve my problem with a wrong means. Sometimes knowledge about an interface of a contract is not enough. I would like to be sure in implementation.

    // my code
    contract Base {
    function addFunds() public {
    //adds funds
    }
    }

    // my code
    contract DoSomething {
    function doSomething(address _assumedBase) {
    // I would like to be sure that address is of Base contract and
    // is not of Derived contract.
    Base base = Base(_assumedBase);
    base.addFunds();
    }
    }

    //third party code, they use source code of Base contract
    contract Derived is Base {
    function addFunds() {
    // removes funds
    }
    }
  • chrisethchriseth Member Posts: 170 ✭✭✭
    Features like "final" and "sealed" won't help you there. If someone wants to steal your funds, they only need to implement a function with the signature "addFunds()" - no inheritance relation is required for that.
    The only way to make this work in the contract - i.e. in a way that anyone can provide any address to doSomething and the contract only executes if the given address has the correct implementation - is to check that the code stored at that address is the right one. This is possible in the EVM (not yet in Solidity) but still, you perhaps might also want to check some values in storage which is not really possible.
    Probably a better way is to either do these checks from outside or let the DoSomething contract itself create the Base contract using 'x = new Base()'.
  • prubypruby Member Posts: 8
    Sounds like you just want to define a method and use it in a child. Use the "internal" option on the method to do that. https://github.com/ethereum/wiki/wiki/Solidity-Tutorial#visibility-specifiers

    When you compile the contracts, the parents get merged in to the child implementation by the compiler. It doesn't deploy the parents independently to the network. Inheritance only works within a single script, not on the wider network and it doesn't provide a means of subverting your security. Anyone who deploys a derived contract will only modify their own storage.

    If you need multiple contracts to trust each other more than the general public, use something like this (untrusted):

    /*
    Set up the ability to appoint a manager for this contract.
    Only the manager can call functions with the managerOnly modifier.
    */
    contract managed {
    address manager;
    mapping(address => bool) delegates;

    function managed() {
    manager = msg.sender;
    }

    modifier managerOnly { if (msg.sender == manager) _ }
    modifier delegatesOnly { if (delegates[msg.sender]) _ }

    function setManager(address newManager) managerOnly {
    manager = newManager;
    }

    function addDelegate(address newDelegate) managerOnly {
    delegates[newDelegate] = true;
    }

    function removeDelegate(address newDelegate) managerOnly {
    delete delegates[newDelegate];
    }
    }

    contract ParentContract is managed {
    function doSomethingSensitive() delegatesOnly returns (bool) {
    return true;
    }
    }
    You can then instruct your contracts as to which other contracts or addresses to accept particular method calls from.

    Cheers,

    Tim
Sign In or Register to comment.