...by Daniel Szego
quote
"Simplicity is the ultimate sophistication."
Leonardo da Vinci

Tuesday, April 3, 2018

Solidity Tips and Tricks - private variable and delegatecall


Delegatecall means in solidity that the contract is executed under the context of the calling contract. Pay attention however because that means that even the private variables can be set from a different contract if you call them with delegatecall. As an example, in the following code, calling delegateCallTest will change the private variable n for contract D, even if the variable is marked as private and variable setting is executed in contract E.

contract D{
    uint private n = 0;
    
    function getN () returns (uint) {
        return n;
    }
    
    function delegateCallTest(address _e, uint _n) {
        _e.delegatecall(bytes4(sha3("setN(uint256)")), _n);
    }
}

contract E{
    uint public n = 0;
    
    function setN(uint _n) {
        n = _n;
    }
}