What's New in Solidity

ETHOnline 2021 · 11 Oct 2021 · 28:52

Harikrishnan Mulackal presents the two big features that followed Solidity 0.8.0. Starting from a simple price and quantity example, the talk shows why plain integers lack type safety, why structs cost extra gas, and how stack and memory differ in the EVM. It then introduces user-defined value types as a zero-cost abstraction, and the roadmap for operators and fixed-point support.

Speakers

Topics

From ETHOnline 2021, hosted by ETHGlobal.

Transcript

And our next speaker is Harry and Harry's going to be talking about all the new features in the latest update for Solidity. So, we'll just give it a second for everything to work out on the AV side and Harry, whenever you're ready, turn on your camera and we'll get the slide started from our end. Here we go. Hello, can you hear me well? Uh yes, everything is good. I know we're doing the slides on on on our end, so just give me one more second to pull that up and we can Sure.

get that work. I'm sorry, this one is on my end. I I can go present it, but I'm trying to see if I can actually make it a presentation full screen mode, but um let's see if that works. There we go.

Okay so just say next whenever uh Uh great. Yeah, so I'll say next whenever I want to move. Okay, great. So, my talk is about two important abstractions in Solidity. And as you might already know, the 0.8.0 version was a very important release for us. Uh because it introduced a major feature that is safe map by default. But since then, we have followed up with two other major features that we believe has a similar potential.

I'm not going to say what the two features are right now, but rather I want to slowly introduce a problem and then try to convince you why we implemented this feature and how we can solve certain problems our new So, next. So, let's dive straight into our toy problem. Okay. Suppose you want to write a smart contract that sells some object. The object has a price and there is a limited quantity of the object available.

And the problem is extremely simple. The problem is that a user wants to buy some quantity of the object and you want to compute the total price. So next. Uh the solution the very first solution for this is the following. So, you write a function which is total price that takes two unsigned integers, here quantity and price. And then you multiply it. It's very simple and perfectly valid code.

But what is the problem here? The problem is that you can notice that the price and quantity are supposed to be two different things. For example, you're not supposed to assign a price to a quantity or vice versa. But in our code, we can because both of them are unsigned integers. So, from a type safety perspective we can do much better. So, in our example, the type only represents the underlying data representation and not how the data should be interpreted.

So, to repeat, we would like to have two different types for quantity and price. But, we already have a solution for this in the language. Let's look at another solution. So next. The solution number two is just using structs. So, we have two structs here, quantity and price. They both have a single element. Uh and now you can try the same function, but the arguments are now structs instead of elementary value type.

Uh so, the code is much more type safe than the previous one. And you can no longer assign quantity to price or vice versa. That would be a type error. But, now this has some other problems. Uh so, structs are a wonderful abstraction, but it comes with a cost. I will actually explain the cost in detail in a bit, but let's just say that it's not as efficient as the first example.

So, note that a struct is a reference type. What this means is that it is a pointer towards a value in call data, memory, or storage. So, in this example, it is in memory. So, you can't see it clearly because we you have to explicitly specify it. You can see quantity memory Q Q and price memory P. So, remember that I mentioned this is not as efficient.

So, let's forget about the problem for a bit and talk about something more fundamental. So next. So, we will briefly talk about the difference between stack and memory in the Ethereum virtual machine. So, a stack is simply a data structure. The Ethereum virtual machine is a stack-based machine, which means that you mainly do operations using a stack. Speaking of operations, the main operation in a stack is to push a value to the top of the stack.

And then there are a bunch of operations that allows you to rearrange the stack as well as doing various computations. So, for example, if you have two elements on the stack, you can add them by using the add op code. And what that does is what that does is that the two elements will be removed and replaced by the result. And let's now talk about memory. So, memory is a temporary location in the Ethereum virtual machine, where you can store things and read them later.

The main takeaway from this description should be that stack is much more fundamental than memory. So, it's important because there is often this misconception that all values in Solidity are stored in memory. That is not true. In Solidity, you have to explicitly say memory when you need to store or read something to and from memory. So, for example, local variables are stored in stack and not memory. Because often they don't have a memory specifier.

So, if we go back to uh to the first example, the values are actually stored in stack. And in the second example, the values are stored in memory. But this still doesn't explain why one is more efficient than the other in terms of gas. Let's now try to see why that is true. Uh next. So, as I said before, the most fundamental operation that you can do with the stack is putting a value in it.

So, you can do this with a push operation. The operation is very cheap and only costs three gas. So, if you consume the value, for example, uh when you want to add it with something already something that is already in the stack, you don't need to pay anything extra for consuming it. You only pay for the addition. So, consuming doesn't have an extra cost as long as the element is at the right location. But if it's not at the right location in the stack, you can use some stack operations to move it to the right location before consuming it.

And each of these stack operations would typically cost three gas. But in our best-case scenario, the cost for putting something in stack is just three gas. And we hope that the value value is at the right place, so that we don't have to do any more operations to move it around. Now, let's see how to do the same things with memory. So, if you want to put a value in memory, you do it using the operation MSTORE. So, MSTORE of AB will store the value B at the location starting from A.

So, remember that all operations have to go through the stack. So, to do MSTORE of A, {comma} B, you have to first put uh the values A and B in the stack. So, you have to start with B and then A. So, we have already discussed the cost for doing this. So, to put B in stack, it costs three gas. It is a push operation. Similarly, to put A in stack, it costs three gas. And now the memory MSTORE instruction has the cost three, which is fixed, and at least another three, which can depending which which can vary depending on how big uh A gets.

So, the more memory used, it can get more and more expensive after a certain point. So, in total, you have to pay at least 12 gas for this operation. And similarly, if you want to read a value from memory, you do this using the operation MLOAD, MLOAD of A. Again, you need to start by putting the value in stack, which costs three gas. And afterwards, the MLOAD instruction, which costs three gas. So, that is a total of six gas.

So, as you can see here, just to write and read from memory, you need to spend at least 18 gas. Whereas, this can be done with just three gas in stack. So, stack operations can be around six times cheaper than memory. So, can you go back to slides? Great. Now, I hope that answers my statement, not efficient. Now, let's go to the real solution.

So, what if Solidity had a way to create an abstraction, but without having pay an additional cost for it. Abstractions are amazing, but we don't want to pay more gas for it. So, let me introduce you to user-defined value types. So, it is what we call as a zero-cost abstraction. It can be used starting from version 0.8.9, which was released exactly 9 days before.

So, it allows you to create an alias type while having additional type safety. The syntax looks like type U is V, where U is the name of the new type, and V is an elementary value type. That means V can be an address, a uint, a an int8, etc. So, how do we solve the same problem using user-defined value types? So next.

So, here is an example on how to write a contract to fix solve the toy problem. So, here you define a new type quantity and price. You You see the syntax type quantity is uint and type price is uint. Note that these types are distinct, and therefore, you cannot just assign one to another. That would be a type error. So, this example also introduces how we allow conversions to and from the underlying type, which is by using the function wrap and unwrap.

So, quantity.unwrap can be used to convert from quantity to uint, and quantity.wrap can be used to convert from uint to quantity. This can of course be generalized. So, if you replace you int to you int eight everywhere, then wrap and unwrap will correspondingly change. So next A very important thing to note about this is that the types are fully backwards compatible. What does that even mean?

So, what this means is that even when you have to implement a contract that needs to follow a standard which was written a long time ago, you you can still use the user-defined value types to interface with it or rewrite it. For example, to uh the ERC-20 standard requires the type for the amount of token to be you into 56. But, you can define a new type with the name decimal 18, which is a zero-cost abstraction over you into 56.

And you can replace you into 56 with decimal 18 whenever you think whenever you think that's more appropriate. And it would be completely backwards compatible. So, here I have two examples of interfaces. And how And uh they are supposed to represent some of the subset of the ERC-20 standard. And both of them would work exactly the same. But, in my opinion, the first one is much more clearer because it makes it clearer that the value is supposed to actually represent a number with 18 decimals, which is the case for most tokens.

Whereas in the second case, it's not obvious what value uh is supposed to represent in terms of decimals. So, effectively, the feature allows you to write code that is much more clearer and easy to read. So next So, there are of course some open questions about how we improve this feature. And one of them is about defining operators.

So, user-defined value types does not have any operators right now, but we would like to have them in the future. So, as you can see in this example, which is again about a token and how we have a type that represents a value in 18 decimals. So, the type decimal 18. So, we need a syntax to create the operator plus equal to because if you look at the function uh mint, you have the line balance of user plus equal to value, but we don't have a plus equal to right now, but we would like to have them.

You can participate in the discussion uh where we propose this feature by going to the links. We would like We would love to hear your opinion on this. I will leave a link to my slides later if you want to check out the links. So, now that we have solved our first problem, let's forget about it and talk about a completely new problem. So, the new problem is also very simple. Uh next.

So, the new problem is the following. So, how can we tell a user why a transaction failed? So, suppose you have a simple contract that can only be used by the owner. So typically So, it's typical to have a modifier that checks the sender and reverts if the sender is not the same as the owner. You can see the contract here where you have a immutable variable owner, which would be set to the message.sender during the contract initialization.

And then you have a modifier only owner, which would check if the message.sender is same as the owner which was saved during the constructor. And if it's not the same, the transaction would revert. For example, if you look at the withdraw function, it can only be called by called if the modifier only owner is true. So, I have a comment here which I said don't do this, but this is a typical This is a very typical in smart contracts.

And why did I say this? So, that's mainly because this is a very large revert string. It's 40 bytes. It's more than 40 bytes. And large revert string add to higher deploy cost for the contract as well as higher gas cost for reverting transactions. The deploy cost here can be especially important if your contract is getting near the contract size limit. But what if we had a way to create a high-level error object? Next.

And that is exactly what custom errors do. So, it was introduced in the version 0.8.4. And syntax is very similar to how we do how we define events. So, you can see the error only owner. And also notice how we use NatSpec comments to describe it. If you listen to the talk about source verification by Franzi that happened just before, you will see some of our goals about how we want to use NatSpec comments to improve the user experience for contracts contract interactions.

So, instead of a require statement, you replace it by an if followed by revert only honor. Next. So, here is a short summary of the before and after. In the first example, that's how you do it classically using a string. And in the second example, we have a contract uh of use customer as and the second example is better because it has lower cost for rewarding transactions as well as cheaper contract deploy cost or smaller bytecode.

And next. So, let's get to the next level of uh revert messages. Sometimes, the revert reason uh sometimes you want the revert reason to have more information than a very simple static string. In the past, if you wanted to convey a structured message back to the user, you would have to do something like the following. Uh the context is that we have a token like contract and if we have a call to the transfer function and if there is insufficient balance, we want to revert and tell that to the user. Of course, we can always revert with saying, "Okay, insufficient balance." But what we want

to do more than that. So, if you really want to have a proper error message, you could do something that looks like this. So, what we have here is a function you went to STR that converts a integer to string. And then you concatenate all these things on chain. I think I have a bug here, but yeah, whatever. It doesn't matter. Uh so, first of all, this you went to string conversion is uh very doing it on chain is paying more gas than you're supposed to do. I mean, it's just unfortunately

expensive. So, when I mean expensive, it's not as expensive as a storage read or storage right, but it's completely unnecessary in my opinion. Things don't really belong in smart contracts, so you should try to really minimize them. And the second reason is that whenever you have a function call in Solidity, each argument has to be evaluated first. I mean, so if you have f of AB and if A and B are function call, you have to evaluate A and B before you evaluate F.

So, in this case, ABI.encodePacked is a function call. And that means that even if the condition for require is true, the revert string has to be still evaluated. So, this means that you're just wasting gas for every single call even if even if the call is completely correct. So next. So, let's see how to solve the same problem by using custom errors. So, here is an example that combines, you know, multiple things from this talk.

The first thing is the user defined value type decimal 18. So, we have a token again. And we have a we have an error insufficient balance. So, remember that I told you that custom errors are similar to events. You can have arguments. So, here you have a revert uh with the address of the user, the current balance, and how much is required for the transaction to succeed. It looks very similar to how you emit an event.

So, this is a much cheaper and much more idiomatic approach to having much more structured error than using strings. So next So, custom errors is a feature is a compiler feature, but it wouldn't be complete without tooling support. So, several of the popular tools actually support custom errors. And we are hoping to see better support.

So, currently block explorers like either scan doesn't automatically decode custom errors. And we hope this is something that would be supported in the near future, but all these like uh all these like tools do support it. So next So, that's pretty much my talk. My name is Harry, and I hope I was able to convince you why these two major features, that is uh user-defined value types and custom errors are useful, and also why we need them.

I would love to see smart contracts using these features, and I hope I was able to influence you. You can find my slides in the following link. I can now answer some questions. You can also find my contact in the link if you want to follow up. And if you want to reach out to the compiler team, you can find the details in the link. So questions. Hi, that was a great talk. Um I'm just kind of sourcing everybody. I'm looking at all the chats to see if there are more questions. I think overall people are just excited about the the the gas efficiencies here, of course. Um this is kind of the most obvious uh uh thing that you can highlight here along

with those idiomatic abstractions. Um So, if anybody has quite um we're just getting comments that people are loving this. So, Richie, if you have any questions for RA, we can ask that, too. Maybe just a more future forward-looking question will be what are some of these other abstractions that you're thinking about in the back burner or in possible next releases? Like, what does that look like? And and sort of how do you think about the future of the language? Yeah, so one something that we wanted to do was uh having some support for fixed-point types.

And we think that user-defined value types is like the first step towards it. So, right now our thinking process is that we don't want like proper fixed-point types in Solidity because there is there is no single way to implement it. If you look at the implementation, there are like multiple implementation, and each one has their own advantages and disadvantages. So, if something lands in the compiler, it has to be, you know, somewhat uh the implementation, but there is no other implementation. So, we think it's something that library should do, and we think we want to better support it by using uh user-defined value types, and that's why operators would like be the

next step in user-defined value types uh improvements. And I think that would unlock uh much more abstract code, much unlock that would let you write much more abstract code, and also safer, also efficient code. Yeah, that's the number one priority right now. Got you. Um in terms of I mean, you kind of talked about some of the integrations that are still um like on track to be actually uh on there from like the libraries to Etherscan and what have you. Um what is it that they should keep in mind as they think about integrating it or is there any Obviously, I feel like you already addressed that there's no backwards incompatibility. Uh or sorry, there's no incompatibility here as

people think about it. It's It is equivalent. It's just a high-level abstraction, but for developers on this side looking to just with quickly switch over to these abstract types, do you have any advice or comments on what they should keep in mind? Uh not really. I think it's fairly straightforward to like uh switch. And we purposefully designed uh it to keep it simple. And if you have comments, if you're using the feature and want to see some changes, we are always open for comments. So, the way we implement features is by starting with like a minimal version that will like do most of the things, and we would slowly add

more features to make it stronger. So, if you have, you know, comments, feel free to reach out to us. As a matter of fact, the feature user-defined value types was uh greatly influenced by one of our uh user feedback. So, it your feedback really matters, and it would like influence the language. Awesome. And the way people can get involved is is it the GitHub, or what is the official resource? So, you can find all our information in soliditylang.org, but you can also come to our GitHub chat uh look at open issues in our GitHub channel um or in the Matrix channel as well.

But, the go-to go-to link is soliditylang.org. Awesome. Well, I couple people were asking for a link to the slides. We just posted that, so that also has the link at the end of the presentation. So, thank you so much, Harry, for that amazing intro for the demo of what's new. And uh hope you have a good rest of the weekend. Thanks. Bye. All right. With that, we are ready for our next talk. So,