Proxy — Transparent Proxy Pattern & UUPS

Eszymi
Coinmonks
3 min readOct 25, 2022

--

To better understanding proxy patterns, you should know how works proxy. If you don’t know, here is my previous post explaining it. Ok, now let’s focus on the main subject of this post.

Proxy selector clashing

All functions from the public interface of the smart contracts identified by 4-bytes identifier. How to get this identifier, I wrote in my previous post about delegatecall.

It’s only 4 bytes, so it is (2³)⁴ = 4096 combinations (because 1 byte = 8 bits, and a bit could be set to “1” or “0”, so there are only two options). As we see, the number of possibility of our selector is small and when we will define 4097 we have 100% that minimum both of the function have the same selector. But it doesn’t have to be so many defined functions to have one selector that fit to two functions in the same time. This problem is called proxy selector clashing.

Why in the name of this problem do we see the word “proxy”, if it could happen even in a common smart contract that doesn’t use proxy? The answer is simply, in this common smart contract we won’t have this kind of problem. It’s because, new version of Solidity compiler checks if every function has different identifier, if no then during the compilation we will see an error.

So if compiler protects us from this problem, why we bother of it? The key is the structure of the proxy. When we create the proxy or the logic of the proxy, we compile only one of them. So the compiler doesn’t know that there is another contract, and it is not able to protect us. Therefor, we have to protect us by ourselves.

Proxy Patterns

So the proxy selector clashing is the problem that needs two separate smart contracts, because in one of them we won’t have it thanks to the compiler. Let’s imagine a situation where proxy’s and logic’s contract have defined functions with the same name and take the same kind of argument. So if we would like to call this function, there will be a problem which of this function it should use. Normally, it will use the function defined in the proxy’s contract, because connection with the logic is by the fallback, which is called when no other function call to the sendvalue. So, we will never able to call this function from the logic’s contract. Of course, we could someway write our proxy contract to use this function inside the logic, but then we won’t be able to use this one in proxy. Moreover, this solution is complicated and inflexible. Therefor, there are other solutions.

Transparent Proxy Pattern

This pattern use msg.sender as a key which function will be used: this from proxy or this from logic. If msg.sender is the address of the admin of the proxy, then no function will be called by the delegatcall. If msg.sender is not the equal to the address of the admin, then proxy use only delegatcall and no other function defined in the proxy. This simply solution separate logic from the proxy and thanks than we don’t have a proxy selector clashing.

UUPS (Universal Upgradeable Proxy Standard)

UUPS works similar to the Transparent Proxy Pattern. We use msg.sender as a key in the same way how in the previously explained pattern. The only difference is where we put the function to upgrade the logic’s contract: in the proxy or in the logic. In Transparent Proxy Pattern, the function to upgrade is in the proxy’s contract, and the way how to change the logic looks the same for all logic’s contract.

It is changed in UUPS. The function to upgrade to new version is implemented in the logic’s contract, so the mechanism of upgrading could change during the time. Moreover, if the new version of the logic won’t have the upgrading mechanism, the whole project will be immutable and won’t be able to change. Therefor, if you would like to use this pattern, you should be very careful to accidentally won’t take from yourself the option to upgrade out.

I hope you find this post useful. If you have any idea, how could I make my posts better, let my know. I am always ready to learn. You can connect with me on LinkedIn and Telegram.

If you would like to talk with me about this or any other topic I wrote, feel free. I’m open to conversation.

Happy learning!

New to trading? Try crypto trading bots or copy trading

--

--