2

I'm trying to change the value of a variable in a contract that is in the blockchain. I've deducted its code and is something like this:

pragma solidity ^0.4.8;
contract Trial {
   address public owner;
   address public person;
   uint initialEther;
   function Trial(address _person) payable {
       owner = msg.sender;
       person = _person;
       initialEther = msg.value;
   }
   modifier only_person() {
       if (msg.sender!=person && tx.origin!=person) throw;
       _;
   }


   function() payable {}  

   bytes4 public signature = bytes4(sha3("libraryFunction()"));
   bool variableToBeChanged = false;
   function libraryInvocation(address libraryAddress) only_person {
       bool doSomething = libraryAddress.delegatecall(signature);
       if (variableToBeChanged) {
           .....
      }
   }
}

Suppose that I have the right signature of the library function, what I'm trying to do is to change the value of "variableToBeChanged" in order to execute the code inside the if. Probably there is a way to create a library with a function with a proper name and inserting assembler code in some way to change the value of the variable. But it's like using an atomic bomb to kill an ant. I'm looking for the simpler way to do this. I also know this contract is not safe, I'm trying to understand if this is possible and I want to understand how risky this can be for a contract.

Pietro
  • 33
  • 1
  • 9

1 Answers1

4

Without a function changing the variable it is not possible. Once the contract code is deployed, it is final, no change is possible. You need to deploy a new contract.

You could control with your contract which wallet is allowed to call the function by checking the msg.sender attribute for a specific wallet address.

address owner = 0x2FD4...;
if(msg.sender != owner) throw;
        //code afterwards)
Simon
  • 605
  • 1
  • 6
  • 18
  • I don't think so. I know for sure that in a contract like this it is possible. For example, suppose to create a library like this : https://paste.ofcode.org/8bgCLXjdHb7cNY4ETM72bn. This would change the value of some variables. – Pietro Apr 24 '17 at 12:29
  • 1
    I do not know how this works. But you cannot change a contract after deployment. You need functions to change variables. – Simon Apr 24 '17 at 12:57
  • It works because of the delegatedcall that is used for dynamic contracts. In this way contracts can be updated. – Pietro Apr 24 '17 at 15:44
  • I just noticed an error in that code. There is a bool that should be bytes32 in the return of the load – Pietro Apr 24 '17 at 17:45