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?

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.

8
  • 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 3: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 3: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 4: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 4:55
  • 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

3
  • actually looking at the docs, it seems a contract could read another contract's code, just not using Solidity but assembly instructions. solidity.readthedocs.io/en/latest/… Am I getting this wrong? – ronme Oct 21 '16 at 20:16
  • No, this answer is wrong. – Tjaden Hess Oct 22 '16 at 0:23
  • Take a look at Tjaden's comments to my answer. – ronme Oct 22 '16 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.