Resources

People often ask me "How did you learn how to hack?" The answer: by reading. This page is a collection of the blog posts and other articles that I have accumulated over the years of my journey. Enjoy!

C3 Linearization in Solidity- 1135

bytes032Posted 2 Years Ago
  • Inheritance is straight forward until its not. What do you do when a diamond is created from inheritance. For instance, if we have contract A, contract B & C which inherit from contract A and contract D which inherits from B & C. Weird problem to have. The solution to this in Solidity is C3, which is also used by Python.
  • The algorithm guarantees that the method of resolution is uniform throughout the inheritance hierarchy. Additionally, it's expected that method calls and attribute lookups work in this scenario as well.
  • The goal of the Linearization is to make this a linear hierarchy. As a result, the inheritance flow will turn into [D,C,B,A]. Crazy enough, C is now a direct descendant of B! Although this is not the EXACT interruption that you would expect, it's better than the program not compiling.
  • Constructors for the parent contracts are always executed in the C3 order. Changing the order of constructor execution within the child constructor doesn't effect this at all. Additionally, a parent with a constructor and no arguments is automatically invoked, even if it's not explicitly invoked by the child. There is an example in the code of this for the Ownable contract.
  • Overall, a pretty interesting article on how inheritance works in Solidity. Probably been a few bugs in the past as a result of this.