OpenZeppelin / openzeppelin-labs

A space for the community to interact and exchange ideas on the OpenZeppelin platform. Do not use in production!
https://openzeppelin.com/
MIT License
376 stars 116 forks source link

Initializer contracts by modifying solidity code #127

Closed spalladino closed 6 years ago

spalladino commented 6 years ago

This is an alternative implementation for initializer contracts. While the experiment on initializer_contracts works at the bytecode level, this one attempts to achieve the same results by modifying the Solidity source code.

The goal is, given a contract:

Implementation-wise, the idea is to work with the AST, and use the source locations to perform changes directly to the original text, using primitives similar to the source-code-fixer from Solium.

The upside here is that we don't depend that the compiler outputs bytecode with a certain format, gives us more flexibility for the changes we want to implement, and we know that all contracts can be verified (since they are generated from valid Solidity).

The downside is that rewriting the source for all recursive dependencies can be cumbersome, and we end up more tied to Solidity as a language (though the bytecode version also depends on the output of the Solidity compiler, as other compilers may generate incompatible bytecode).

The code provided here only works with a single contract without inheritance, which is the simplest case to implement.