What's new in Solidity
ETHCC 3 · March 2020 · 26:59
Mathias Baumann and Erik Kundt review developments in Solidity.
Speakers
Topics
From ETHCC 3, hosted by Ethereum France.
Transcript
hello hello great yeah hey welcome everybody good afternoon my name is Eric and my name is Matias or online Martin's and we were both working with the theorem foundation on the solidity compiler and we want to go through some nice features that we build recently and so I will quickly drive you through the agenda for today so first of all we have some language features that we want to introduce to you then also some compiler features I want to briefly talk about future development of solidity and if there's any time we also will have a Q&A session and as you can
see there's a QR code for the slides that you can use to follow on your own device or to easier click the links that we're going to show you later okay and I think first of all we are going to start with the language features yeah right into the new inheritance system though new is a bit of a stretch here we added keywords we edit restrictions we made it safer more verbose more explicit and we starting with the override and virtual keywords which you probably know from other programming languages here you see a simple example
306 oh we simply derive three times from token refundable token and time true fundable token it's a very simple example we always call the base function of the previous class or contract in the new version this changes to look like this so you notice the virtual keyboard in the token contract this has to be specified now otherwise you cannot override the function anymore and you will get an error and in the refundable token you've noticed the override keyword this is now required if you want to override a function and as we want to override this function again
we also have to add virtual again so it's not inherited and you always have to space it explicitly and finally in the last contact you see we only specified overwrite so we don't intend to overwrite again here this makes overwriting more safe the base implementation changes you will get an error if you would want to make sure no one ever writes your function you just don't make it virtual all the common features you know from other programming languages as opposed in multiple inheritance it also changed in a similar matter here you have a simple example we show er c77 on contract which
implements two interfaces here C seven seven and here C 20 this changes in post o 602 this the special thing to note here is the override keyword with the two contracts or interfaces that are overridden you must specify every single interface you're overriding and another thing to note here is the virtua is not specified in the interfaces because they're always virtual and it's basically not required anything else no this is basically this for multiple inheritance regarding abstract
constructs contracts we have here a simple example with total supply in missing and definition and the deriving from contract is not implementing it or overwriting it so this has various problems as one there is no limitation for total supply this compiles it will give you no error but no code is generated for total supply and you will get an error once you try to new the token class in oh six oh we changed this so you have to specify abstract in the contract as you see the top line and this will result in the compiler telling
you that token must also be abstract because the implementation is missing for total supply you could of course also just implement total supply and get rid of this regarding interface inheritance a small but really neat detail changed interfaces can now inherit from other interfaces so in this example the RAC 777 is also implementing RC 20 in the skin very easily we specified this way now we're leaving the inheritance topic and look at fallback and how we split it into four they can receive yeah thank you Matthias so let's take a look at a
prio 600 contract so as you can see here you have the fallback function and this function is being called for a non-existing function signature or ether being received so this transaction now is successful and the contract keeps all the ether so and we decided to make the whole mechanic a little bit more explicit so we introduced a receive function and the receive function so there's a new keyword the receive keyword and the receive function is only called whenever there's either being sent via sand or transfer and now with after oh six oh this contract now
reverts if a non-existing function is being called this is the first part of the fallback and receives blood and now coming to the for back part so we have a proxy contract here and this again has a fallback function it's a prio 6o contract and here in a fallback function there's a some inline assembly and you would probably delegate the call to two through the contract itself so again this is also being called for both so for non-existing function signature being found and also if there is any ether being received and here you would with all six after oh six oh there's the new fallback keyword and
again this is also being called for for both for non existing function signature being without our if there's any ether received but here the compiler would now warn about that there's no receive function being implemented and to avoid this warning you could then easily implement the receive function and as you can see here we have a receive function we have the fallback function and then we have a generic function that's being called for both and here you could do whatever you need to do there it's what to mention here that the payable keyword for the fallback function is optional and to wrap up this this new behavior so receive is being
executed on plain ether transfer and fallback is being executed if there's no matching function signature or an empty data field was supplied and there is no receive function as you have seen in the example before coming to the next change read only every length so let's take a look at the pre oh 600 contract this is a very simple example doesn't make probably that much sense but we have a stack and it has an internal array and we have a constructor that you can pass pass the length and a person a pop function and then the push and the pop function you would increment the length and assign the value and for pop you
would decrement the length and retrieve the value so this contract has some issues so first of all I'm the XS 2 dot length looks cheap but deletion is quite expensive also if you would pass in a very high number in for for as the length and into the constructor that currently a desk could lead to joy dang a gigantic array s and then into overlapping storage and making this read-only prevents this multiple access to the storage when we say delete we mean if you decrease a length here by a larger number it can be potentially very expensive but it looks very cheap
on the code and after o6o we made the XS to 2 dot length read-only so we altered the previous example and instead of assigning two lengths directly you would have a loop here and into in this loop the empty push function is being called which has less hidden cost and also there are new functions also push with the value and the pub function which was introduced in a 500 if I remember correctly and this contract behaves the same way as the contract before so and
now I'm very excited about in something that we introduced also recently is try-catch so you would probably know the construct from other programming languages so let's have a look at the next example so first of all we have a contract feet and this has a function called theta this time in the function body there's the revert providing a recent string so and then we have a second contract called consumer this has a feet and rate function and here you want to call data function of the feet and you would probably want to try if hey I want to catch a failure but this one was not possible before so we introduced
try-catch syntax iya so here we change the example and in line 9 you can see there's the try statement and you would call the filter data a function as specifying the return type and if everything goes goes well you would just return the tuple with the value and the flag and if not like in this case because it will always revert providing the reason string there's the catch branch with a signature like the string memory and you could react on the revert providing a recent string let's
change this example a little bit so if there's no reason string being provided or if there's a failing assertion assertion like a division by 0 or something like that there's another branch which allows you to access the low-level data sorry and of course you could combine both yeah next one function call options you may know them as dot value and doubt gas if you want to pass gas or way to an external function call and this example you see how we pass to a way to the data sorry to the SUBSCRIBE function and five gas
we changed the syntax for those because another feature that we introduced required also a new syntax and these to work very well together and this syntax in is also not that nice because it looks like a function call but it really isn't and this is how we made it look now it looks similar to named arguments you specify the value and the gas and Koli break brackets before the call here again we pass it two away and five gas and that's all there is to say about this feature but going straight to the feature that prompted this change is
high level support for create - this is how you would do it before so you want to make you want to make an instance of product at a predictable address so you get the code for product you call the create function in low-level assembly you give them ten way you add the code you get the code lengths and your salt which in this case is creatively colored salt you check if the creation failed or not and reward if it did this is all nice but it has quite some problems actually and it's complicated to do you and yeah the high-level support looks like this so
you use the simple new syntax with previously introduced named arguments kind of syntax in the curly brackets you simply specify the salt and value the guy the way they want to put that you want to pass things noteworthy here is we have now a constructor that is payable we actually needed one before too but the compiler couldn't type check what we did so you would have reverted at runtime now this actually creates a compile time error it's easier to pass arguments now to the constructor and without salt it would just be a normal
create call here now we want to look at some miscellaneous features that we don't have time to elaborate much but just one I mention so we added support for global enums and structs so they don't need to be declared inside a contract anymore and we also added the sliced syntax for a called data arrays we also added a new ASM statement lead which is the high-level equivalent of return basically like there is a low-level return assembly in expansion as well but that will leave the whole function that you're in leave will only leave the current you'll function you'll
assembly function so that's allows you to instructor your code flow more precisely when you're using awareness now when we when I look a bit at a few compile features we also edit starting with the avi encoder version 2 we no longer considered experimental now you still have to activate it manually though and no longer experimental means in this case there's no warning anymore if you use it and there is no longer the experimental string in your byte code additionally the you'll optimizer is now always on when you use the norm of lag because it's also considered to
non-experimental now and you're also worth to mention is that we put a lot of effort into to you wasn't back-end so every solidity construct that is compatible through you'll is also now compatible to you wasn't and it wasn't missus etherium flavor to what assembly and which is a subset of it and we also developed a small tool which is called solidity upgrade so just a quick background some little background about it so in our testing pipeline we have some external projects like gnosis from Zeppelin and colony so far and we're always compiling those contracts with the recent compiler version and of
course we needed to upgrade all those contracts to o6o and as you might have seen all the inheritance changes might lead to a lot of work so we wrote a small tool which is able to update contracts on a textual basis so it's a hybrid solution that works on lips a little teased through and uses it to parse and compile the contracts and then uses some regular expressions to replace them and we have some documentation and the reader Docs and if you if you're interested in this tool then feel free to check out the documentation and also we want to use this opportunity here to ask you if you have any zero 6o compiled
below contracts and you want us to integrate your project into our testing pipeline then please feel free to approach us so the future development as we're a little bit short in time here we just thought we want to mention the different possibilities you could use to take part in further discussions on the development so we have a mailing list which is a Google user group and we're discussing language design topics there please feel free to check out the link and join the group and join the discussions we are very happy for your party to see more participation there
and it's only for language design not for 3d programming questions speaking about language design so we are also very happy to announce the solidity summit which will take place in Berlin at the end of April so just go and check out the link so you can sign up there and also send in proposals for workshops and talks and it's a two-day friendly sized singletrack event with with talks and workshops and yeah we are very happy to see a lot of people there hopefully yeah finally we are very approachable you can always talk to us you can report
issues you have with the compiler on our github issues page or even feature ideas we have a git er chat where we discuss our development related things you always welcome to check in there to ask about what features we plan what if you want to contribute what you could work on we help you to get started with the compiler internals things like that we also have Google hangout meetings twice a week Monday and Wednesday you can always join in listen in talk if you want ask questions project ideas that you want to do in the compiler things like that you always welcome and all these links are also in our
documentation under contribute I think and yeah you're welcome to always visit us in anyway finally I want to reference some other talks gonna happen here from our team chris is gonna talk about metadata and source code repository on day 2 and bagua is talking about how he's fuzzing the solidity compiler to find bugs and make sure it's stable on day 3 finally if we still have time we are open for questions and hopefully answers thank you [Applause] hello and thank you for the presentation
I have many questions I will just ask one and we'll see if other people have questions about the gas semantics for calls that you presented using curly bracket which is a new version if you look at a IP 150 this says that it's a maximum amount of gas but there is no guarantee that that amount of gas will be provided if the coal doesn't contain that much and there is this 63 over 64 or so do you have mechanism with an infinity 6 0.6 or should we do the correct assertion to verify that that amount of gas that we specify is in fact available to be passed to the core now we don't have anything like that so they
would just revert in this case what's right because it's EVM level assertions that only 36 out of 64 and so it would not revert like we are doing it constantly in our contract so it would not be worth if you try to send more than is yeah okay yeah true but I have also a question for the you said for the send and receives this for the receiver callbacks this is only called if you call it or if you send funds by a sent and receive how do you check that it is called by a send and receive and not that it just a call with with empty data
because send and receive his difference and a call with empty data right because of the gas limit or is there no difference that's also fire fair for me and I yeah so recent just one more you talked about the push and pop mechanism for Ras and the fact that lens is read-only one thing that I
used to do maybe it was a bad behavior is just overwrite lens to zero to clean up an entire array so should that do a for loop with as many Popes as element because deals this feels wrong like to to update the lens one by one instead of erasing everything at once yeah that would be the solution in this case may I ask why setting lengths likewise changing lengths is expensive just out of a naive approach I would have thought they just changed the length in the first slot of the of the storage of the memory location it's too far like it's
deleting things is very expensive so and just setting lengths from a high value to a very low value is expensive behind the scenes but you don't see it that's and we wanted to make it more explicit so when you do a loop it also feels more expensive now yeah so when I set the length of an error it actually also another hood does the loop to reset all the values because that was yeah it is libel is definitely an open construction
site and there are still issues to be done there I'm not sure how easy it is to get into the codebase when you start with no knowledge at all of the compiler but it's definitely something we can talk about some other creations yep so first of all thank you for adding exception handling to solidity it's actually really helped us and the second of the question I have is that for us for network a breathability of contracts is really important to make it simple and currently we are using the framework
from open Zeppelin you probably know to make contracts breathable and still use the same storage the question I have is have you guys ever consider dating a language feature to to simplify making contracts a breathable I wasn't part of discussions about that so far but I think there is some talk about that but there are some some critical voices about how exactly this is done okay but exactly for questions like this we have also a language design group to discuss just that yes so maybe this question will fall under this this design group for a great ability we
usually use delegate colors and there is mechanism in solidity to implement calls using the low-level and now if the try-catch it's even easier to do to do simple low-level calls and try to catch them do you expect to add mechanism for easily writing delegates code without reverting to assembly and you repeat the last part of the question will we be able to n-- to perform delegate cold at solidity level without doing that in assembly I don't think there are plans for like yet that just yet but we can always talk about that okay thank you very much thank you
everyone [Applause]