4

If a contract A is given an address of another contract B, can it duplicate B and produce B' that has the same code?

ronme
  • 1,035
  • 1
  • 9
  • 18

2 Answers2

2

EDIT: Most of the contract code can be copied, but the constructor code is not actually stored on the blockchain, hence cannot be duplicatd. See comments below for a fuller explanation by Tjaden Hess.

ronme
  • 1,035
  • 1
  • 9
  • 18
  • 1
    This is not entirely true. The code can be copied, yes, but this does not mean that code can then be redeployed. The contract creation code is run once at creation, and returns the code that will actually be stored in the contract. The contract also cannot read the storage of another contract, so any state variables will not be copied, including references to other contract on which the original relies. The contract can only be reliably copied if it is extremely simple – Tjaden Hess Oct 22 '16 at 03:35
  • Thank you Tjaden. The question references copying the code, so it's ok not to copy the state. As for contract creation, if the code can retrieve the duplicated contract's code, is there anything that prevents it from prefixing it (and maybe suffixing it as well) with opcodes that will return that contract's code? – ronme Oct 22 '16 at 03:49
  • There are certain constructs, like constant variables and certain memory allocation features that your contract would not have access to. Also, anything that was in the "constructor" function of the contract will not be done. This could include global variables that are used within the code and make it infeasible to use the code without its proper state – Tjaden Hess Oct 22 '16 at 04:51
  • i.e. imagine a contract Double that has a constructor which puts the value uint a = 2 into storage. Then the double(uint b){return a+b} function is called. If you don't have the constructor or storage, there would be no way based on just the code to know what this function does. It will just return 0. But barring this type of example, it is certainly possible to just insert some opcodes – Tjaden Hess Oct 22 '16 at 04:55
  • Tjadeb, this is really helpful. Does that mean constructors are actually a Solidity construct, not an EVM one, run before the actual contract deployment? – ronme Oct 22 '16 at 15:22
  • The constructor code is still run on the EVM; that's how it can write to storage, etc. The code that you send with the contract creation transaction is essentially just the constructor, except that it returns the rest of the (non-constructor) code. So I'd say constructors are built into the EVM, since it makes a distinction between the code that's in the transaction and the code that actually gets stored. – Tjaden Hess Oct 23 '16 at 02:58
  • I see. So since the constructor is not even stored in the blockchain, there is not even a theoretical way one can duplicate a contract and bring the copy to a valid state, right? – ronme Oct 23 '16 at 17:31
  • 1
    Not in the general case, right. You may be able to get around this by using merkel proofs to prove that another contract has a certain state. – Tjaden Hess Oct 23 '16 at 17:34
0

No it can't,if you are using normal solidity a contract could call functions from other contracts, but it cannot read(parse) the other contracts in order to cloning the code. the question why it will be useful to do so? if you want to upgrade the previous code look at this discussion : Upgradeable smart contracts

Badr Bellaj
  • 18,780
  • 4
  • 58
  • 75
  • actually looking at the docs, it seems a contract could read another contract's code, just not using Solidity but assembly instructions. http://solidity.readthedocs.io/en/latest/control-structures.html#example Am I getting this wrong? – ronme Oct 21 '16 at 20:16
  • No, this answer is wrong. – Tjaden Hess Oct 22 '16 at 00:23
  • Take a look at Tjaden's comments to my answer. – ronme Oct 22 '16 at 15:23