Based on joeykrug's questions on Github: Is there any reason we need to store multiple copies of code if the same contracts use the same code? For example, if a contract is deployed and another contract creates it a bunch of times, why does it store the code multiple times instead of just once?


For now it's probably for simplicity of implementation - due to the fact that keeping one copy properly would require reference counting. Since contracts can be destroyed using the SUICIDE opcode, if multiple contracts share the same code but one destroys itself and the shared single copy of the code is removed from the local storage then it would break the node's consensus, as it would no longer be able to execute any of those other still-alive contracts.

I imagine in the future the main clients will implement reference counting for these types of objects and store a single copy shared across all active instances.

As a note: this isn't as straightforward as it seems either way, due to the fact that when you create a contract, you send a transaction to the ZERO address that runs some EVM code that returns the FINAL contract code. Therefore a set of EVM instructions need to run before the final code that is to be committed to the contract address is generated. Hence to compare this "final code" return to the database of all code hashes on in the node's state would be an extra (computational) step in the creation of a contract.

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.