Difference between initializer, reinitializer(1), and reinitializer(2) modifiers

What is the difference between initializer, reinitializer(1), and reinitializer(2) modifiers?

These modifiers come from the Initializable contract. Both initialize and reinitialize are meant to execute logic only once when the modified function is called. They're similar to regular constructors but they're meant to be used within upgradeable contracts.

Usually an initializer is used first and then a reinitializer MAY be used in future upgrades if a module that requires initialization is added. The difference between them relies on that initializer allows nesting with other initializers. This is because a contract may initialize multiple contracts in the inheritance tree.

Often, reinitializer(1) is not used because each initialization has a version number that starts in 0 and is updated to 1 with initializer, so you can't reuse version 1. You can theoretically use reinitializer(1) but it'll revert if there are nested initializers.

You can find more details in the API documentation.

Thanks for the clear explanation.

1 Like