New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

ETHLondonUK: Introduction to Solidity and Debugging, with Patrick McCory & Patricio Palladino

ETHGlobalFri, Oct 9, 2020, 12:00 AM

Transcript

cool okay guys so we're just gonna begin I've I got the thumbs up to start yeah awesome grip so welcome to London everyone before I begin who's actually from London here is your Hanabi from the area that's about half the room maybe 60% that's pretty cool so the rasters is from around the world and if you're from Italy you know you could just go outside as well I'm only joking ok so I also want to get another reads of Hans who's done any solidity programming before ok so you're all quite familiar with for then so maybe that's gonna be too easy for you guys maybe I can try to make it a bit harder so what I'm gonna do first is I'm going to give a brief overview about smart contracts and solidity and then we're going to build a very simple contract that's a hollow world that censorship resistant it can never be taken down by a nation-state and everyone likes the censorship resistant hello world okay so yeah you only need Wi-Fi so if you connect to the club quarters and type in the room number 999 you can put any email address or name in and you'll automatically connect so just one last time look for club quarters on your Wi-Fi list and the room is 999 any last name you know is Tosh nakamoto V butter interview wants we can all be be bought butter in today cool so what is a smart contract just to make sure everyone has into a high level view of what we're all you know what I think a smart contract is so the first definition that was really released was from 1994 by Nick Szabo which is a smart contract as a computer a transaction protocol that executes the terms of a contract but for me when I read this definition it doesn't really make any sense to me and I can't really process or computed that well so raise your hand if this makes any sense to anyone in this room cool exactly so a better way to think about it for me is that a smart contract as a trusted third party with public state public that means it doesn't keep any secrets whatsoever if you give this trust a third party your date of birth that date of birth would be visible to everyone in the world now the smart contract gives you some guarantees first when I deploy the third party it's code can never be modified give or take how you design it and if I want to call a function I'd say the vote function it will always execute the code I expect as then if I call vote it will run the vote function now there's no head in black box code there in most cases and also one of the safety features is that it will never stop halfway through so if I call the vote function it will either execute all the code or it will stop and revert and not execute anything shows all or nothing and of course it likes the gossip and I can't keep any secrets whatsoever so that's the best way for me to think about a smart contract I want a third party to coordinate some protocol or action or game whatever and all that third party's doing is really coordinating I'm making sure everyone in this room has a cm set of code and everyone here can execute the CM code on the CM state that's a cute little robot cool now in aetherium you have two types of accounts the first is an externally owned account that's your theorem address and as a public/private key pair I have a private key on my computer I can do digital signatures and I can authorize transactions and of course the public key there theorem account is what I give you guys and you can all send me money now there's three different types of transactions on aetherium the first one's really straightforward value transfer I can sign a transaction where I can send anyone in this room some eath you know that central Lakes is like Bitcoin in a sense but then we can go a bit further so we can create it we can create a contract you know creation transactions what I call that we're in the transaction in the payload we put the byte code of the of the contract we deploy that to the network then this transaction will create the smart contract in its initial state so everyone here can see that I created this smart contract and everyone here gonna get a copy of the byte code and finally an invocation transaction if it is the voting contract that's on the blockchain I sign a transaction where I say I want to call the vote function on this contract with these arguments I sign that I broadcast out to the network and of course in that transaction will process the vote of course they all have the same transaction format but I do in terms of the idea there's three different types now on aetherium the rows of this idea of gas so who's heard of gas before all right guys I do really also he hasn't heard of gas before okay everyone's okay one part is cool cool do you know have the really really hot if you don't want to but on a theory of Methos idea of gas okay so why do we have gas for what are the issues is that when we send this transaction to the network we're sending it to thousands of people in the world we didn't make sure that this you know transaction doesn't do a denial of service attack you know it doesn't do a for loop 10 million times we need some way to measure competition we need some way the limit the amount of competition a single transaction can do so we have a gas unit and I'm going to explain that in a second and there's a gas price so one of the the quirks of this network is that let's just say I want to call the vote function I need to sign a transaction I need a broadcast out to the network and I need to get that in the blockchain who puts it in the block miners know these guys open up roofer work so I need to set a gas price says that when the miner includes my transaction they get rewarded a fee for doing this okay so it's a bit like going to you know the gas station for every gallon of gas I want that causes a dollar and if I use 10 gallons then the man is going to get 10 dollars cool so this is a very simple illustration of how Goss works remember the only a theorem Virtual Machine at the end of the day everything's assembly there's up codes that are executed and every up code has a fixed gas cost so in this case you know grabbing the time stamp will cost 10 gas now we're just going to run through a very simple example of course it's not what up coals look like in real life this is the most prettier way to represent them so am i single transaction first what I'm going to do it's that's the timestamp and the Haas for black bloc 500 now this will cost me 60 gas to run both of those up codes but for those you know steps in the execution now when I went to add them both together I add the timestamp in the blockhouse they'll cost me up to 65 gas then when I fetch the coinbase transaction oh I run out of gas corn beers required 50 gas but my transaction had a gas limit of 80 so because of the gas limit I can't finish this execution so I go too fast the coin base it explodes I get an out of gas exception and everything rolls back and that's exactly a good way that you know think about a theorem transactions if you don't allocate enough gas then it's going to explode and not finish the execution cool and finally what does this look like on the network sir every 12 seconds there's a new block and in this example I've created a smart contract and the contract code is here and it cost me up to 98,000 gas meter oh I'm blocked recreated and no one's using the smart contract and then finally Bob comes along he signs a transaction he brought causes this to the network and of course he's going to call a function on the smart contract so again as an example I've deployed the voting contract then over here Bob may have casted a vote in my contract and that's basically how we'll be proof says in the blockchain okay so now what we're going to do is go through a very simple solidity example of writing hello world so I want everyone to go to this website remixed aetherium dot org okay there's an online IDE where we can either write simple code and test it out now what's important is that on this network we have this aetherium virtual machine so all of the programs are executed in the etherium virtual machine on finally the most important thing to take away for a theorem is that these programs will not do anything in the background you know normally when you write a program you can have a task that's always running in the background you can't do that on aetherium your smart contracts will do anything all Nestor's this transaction unless you send a transaction that's the only time your contract will do any execution so there's no idea there's new background tasks everything's event-based cool so now let's go into our remix example under any questions before I continue is everyone quite happy with that okay cool I remember guys this is the workshops who ask as many questions as you want there's no such thing as a stupid question cool Oh actually the last thing I want to highlight and while you guys load up remix when I first discovered solidity back in 2016 this is the first example you'll see on remix with Zabala program so in this very simple example is a simple voting contract there's a list of proposals so when I create the contract where is the code for that well I've deleted that okay so when you create the condo that is when you create the contract you say there's a certain number of proposals that I want the sent so maybe there's three proposals that people can vote on so when I created I say there's three proposals and all of this gets stored then over here there's the vote function so I want to vote for proposal one so that I'll do is I'll create a transaction where I say I want to go to this voting prutte there's a bollard contract I want to call the vote function and I want to vote for proposal one I proved that in I broadcast it and then it approves as the vote function so as the very first across as the very first example I ever seen for aetherium and over the years has been updated on solidity whoops great cool and also one last note is when you write these smart contracts they tend have 40 lines of code but they hold millions of dollars of assets so if you are going to write smart contracts I assume everyone here will at some point in their lives you have to make sure you have an adversarial mindset because this 40 line code program will be deployed on this public network that anyone in the world can access so if you have a single book in your contract it will get exploited by someone so in this example was the first parody wallet hack in 2017 where they lost 32 million dollars and then this was the DAO hack where the Dow got three end of fifty five million dollars so the Dow was his Kickstarter program the idea was that we deployed this contract anyone in this room could do any have funds and then a haazig into a big pop pocket of money I guess Malik Malik Dow now later on anyone here could submit a proposal if that proposal gets enough votes then it gets funded and the other proposal gets the funding but that contract had a bug where the attacker was it with the jury and 55 million dollars and it's a very so a book that maybe we'll cover later now one interesting thing was that why this black hot hacker was draining the funds the etherium dabs also discovered it and they were also trying to drain the funds see how the hacker and the white hat hackers moved draining the funds at the same time you know trying to see of the day the imparity wallet get hacked again and they lost around 280 million dollars and this was guy by DevOps 199 he accidentally killed it but was interesting there was that yes some people remember this then so in this attack what was quite interesting was that there was a central library contract you know when we program we write dependencies we sat when they import this code and I want to run this code so that a library contract on the add 50 wallet contracts that all depended on the library the issue was that this library contract had a suicide function that anyone could call so DevOps came along he set himself as the owner he then sent in a suicide you know command and destroyed the contract and by destroying the library he also froze all of the other wallets because the functionality no longer existed because the library was deleted I mean it's Ava who Rian's have talks around for attacks well this is a big deal you know you don't want anyone to make a t-shirt for your your smart contract okay and that's really it so when you do write these smart contracts in the future please let go online read as many tutorials as you can and how the brick smart contracts maybe we'll try to break one later if we have time and last thing is it's slow expensive and everyone has to share this resource ID you're gonna see smart contract you're very expensive they execute so you want to minimize what you do in the smart contract and maximize what you do off chin you do want to do everything in the smart contract and that's it so these are little links that I've made so last year I taught a yep quit so in this example with the library all the money was on the fifty contracts here so every contract was a wallet is that a wallet contract one wallet contract too and near the library so in this library's destroyed the wallet and the money's still on the wallet contract but the wallet contract has new functionality so it's frozen it's a stuck there which must be so painful I mean if I were the the victim of this I just be really annoyed that I can see my money there but I can't live dropped yeah yeah yeah yeah well typically but the library obviously is not money in this case but nobody when you do destroy a contract it sends the remaining funds somewhere else yeah go ahead it depends on how you design it so I think we've create two op codes you can do that but obviously this wasn't predicted in time so not yet no I mean what you could do is you could hard fork the network and reinstate the contract and I'd be a believer that you should do that and then in this scenario is verifiable that the funds are locked and it's verifiable that this was deleted and by simply recreating it you would draw the funds I'm quite positive I mean I would opt into that but a lot of people wouldn't which is why it hasn't been done cool any other questions okay awesome so what we're going to do now is is we're going to run through this first tutorial so you want to type in those links each of these so what I did last year was I taught a master's course in MSC on cryptocurrencies and these were the tutorials that I give to the students so they're quite in-depth in terms of a lot of the constructs that you can do in solidity and how to break smart contracts but we'll just run through the first one for now so you don't really have the open it but I'll send these links later on and you can look through the tutorials if you want you know they get incrementally harder so if you think so if the remix I'm doing now if you find this too easy then you can just jump ahead to this one and do this yourself right away this is probably the hardest one and I don't think anyone will get that done in 20 minutes cool so that's this go through the hello world example so now I can sit down and go on remix whoops okay cool so this is the remix IDE okay so what we're going to do is create a new smart contract so if you go up here to the top left you'll see a little plus button you just click that okay top left little plus button and then we can type in the name of the new file so we're going to call this hello world dot so give it a long time awesome okay so again top left hit the little plus button and then you can call your contract whatever you want but I'm going to call it hello world dot Sol cool so you'll notice when you open this new file your first error message will appear which is basically telling you need to be able to inform remix or whatever compiler you're using what version of solidity you want to use so in this case we just type in pragma solidity and I'm gonna make the tax bigger now no point six point one so how do i zoom in see that's how you come on obon I don't know the shortcuts cool okay so the first thing you do is tell remix what version of solidity you want to use so we just type in pragma solidity version zero point six point one I always forget to do this then I always get the error message okay and then next what we're going to do is define our smart contract so we want to call it contract hello world curly bracket curly bracket then I'll just wait a few seconds because I can see people are typing and catching up awesome so now what we're gonna do is the whole point of this hello world is that we want to tell the world you know hello so what we're going to do is first define an event which would be a we're going to call it event message and this is going to take a string which would be another message so what's really nice about aetherium is that when I call a function in a smart contract you can create events okay so if I watch a smart contract I'm just watching it and I want to figure out when some cool event happens I'd you're going to see when you call a function in the contract you can meet the program and made a message to the outside world to notify the outside world there's something cool happened and that's the whole point of an event it makes it really easy for anyone watching the smart contract to be notified when something happens so what we're going to notify the world about is to say hello for now okay so now what we're going to do is create a function called SAN message and it doesn't take any parameters for now okay so we're going to define a function called sad message now you'll notice one aspect here is that we have the keyword public so when I deploy this smart contract to the world you might send it into the blockchain I have to define a function to either be internal you know so no one can execute this except the smart contract or public and as a public function then anyone here can execute the function so if you're familiar with Java or JavaScript this is probably quite similar you know some functions can only be executed by the program other functions can be executed by anyone you know it's external API so here we use public to say it's access over anyone and then we're going to do is emit the event message and for now we're just going to you know hard coded to say hello world whoops cool so obvious is a really boring program but of course it's censorship resistant and no one can take this down because we're all cypher punks so so now that we've written this code I'm just going to prove this out a bit now what we want to do is deploy it run the function and then we can emit hello world to the world so we'll go to the solidity compiler which is this one it should be the second menu down the connect can everyone see that by the way because sometimes remix is a bit can I see your example sometimes remix doesn't set this up yep I thought so cool okay so guys if you don't have the compile option go to the little plug down here the plug-in manager now what we're going to do is activate the solidity compiler and activate deploy and run transactions so there's a second option bottom option shoes just a repeat go to the little plug the plug-in manager activate deploy and run transactions activate solidity compiler you can ignore the rest I never use the rest okay so now we go to the compiler so what you should see is that it should auto compile and already be compiled but I always click is to be double sure cuz I don't trust on a compile I've run into some troubles in the past okay and now we click on is deploy and run transactions so can everyone see this on their screen raise your hand if you cannot see this on your laptop down there can someone Mary can you help the guy behind you just because you're there [Laughter] awesome okay so can everyone see this on their laptops nope Chris can you help that guy there the guy in the blue top cool and okay can anyone else not see this are you okay yep the meet that means jump down quickly so all my helpers are gone just click that one oh okay compiled up there they've dumped hit compile hello world see exactly that's why we don't trust the auto compiler my point proven cool okay so guys what we're going to do now is we're going to deploy hello world so we have to click deploy bomb yeah it does you think it yes yeah so the default cast limit for this transaction is three million gasps but there's our contract will actually cost in practice then we seamless is actually costs two calls about 100k gasps so I'll go through that now actually so guys hit deploy everyone click the word deploy and just click it once now when you go to the bottom right hand side down here you'll see a transaction in the VM and I should have a big green tick it doesn't have a big green tick raise your hand cuz then something went wrong okay there's everyone got a big green tick there cool so click it I mean get cool information about this transaction oh you guys okay okay awesome Coco it works so you'll notice that this is from a certain address that's your because you sign the transaction and you broadcast it we've sent it to the hello world country constructor basically we've created the smart contract so there's a new address for the smart contract I only kid it 3 million guys for this transaction that was the maximum that was a maximum quantity of competition I was willing to pay for I was willing to pay up to 3 million gasps I only used a hundred thousand gasps give her a tick and you'll see down here that well there's nothing interesting yet but we can all see that the contracts created so if you scroll here to deploy contracts just here click the little right arrow next to it okay and you'll see the function sad message okay so again go to the left-hand side go to deployed contracts click the little arrow and you'll see the list of functions okay so is everyone not got to this part yet raise your hand if you're not here yet cool everyone's here so all we have to do is click send message bomb you notice I clinic buying you know the dirt is gone and that we've sent or we've sent the transaction to the network and we should hopefully have emitted the word HelloWorld so click the transaction scroll down to the bottom and we'll see in the logs that there was an event called message so we've notified the world that there was a hello message and the message was hello world and so everyone here is now admitted and notified the world of hello okay to just one last time in the logs you'll find a transaction with hello world that's send message okay we click it we scroll down and we can see the event now this is obviously a really useful feature for more than just hello world if your contracts doing anything interesting whatsoever please omit an event because it makes it really easy for anyone watching your contract the watch for interest and events and to do something in response to this but the issue is that hello world's a bit boring it would be really nice if we could send more than just hello world so let's go back to our contract okay Oh topic the der where is it oh I nobody know the best way to describe that I think that's just the what why would you guys describe the purpose of topic okay okay okay it's a hush of the word event and parameters yep okay I always forget so when you're working okay so basically the point of topic is that when you're writing code in web 3 and you want to watch for an event you normally say this is the event I want the watch for so that's there's a hash of the word off the string you know event in the parameters cool yep so in etherium okay so we're doing ents live let me get that up first so an etherium every transaction will produce a receipt you know the output the finish act that you're finished execution and what we care about the event lives in the receipt so in the blockchain itself there's no record of the event you know we don't store the event when we execute the transaction we get a receipt let me get the event data there now in a theorem as well in the block header there is a Merkle tree of all the transaction receipts so you can verify yourself that this transaction receipt actually exists for this block for this transaction so there's a commitment to a cool okay so any other questions before I continue oh okay we up yes of course I forget them of scoffs exactly it's not too expensive let's say a thousand for now but we can google it yeah yep not very easily events are much easier so normally when you run the transaction the whole point of the return statement is to return to another smart contract or to another function it's very very very difficult to get it out so hard that I would even bother trying yeah yeah yeah that's a very important point is to reiterate that so events are only for humans and mates P is for you and I they are not for other smart contracts if contract a is calling contract B you return a value to that so what you would normally do is returns in this case we're going to return a string and we'll return well return hello world and let's just put this in now okay this is all we need to do a memory cool okay cool yep so memory means that memory means is that this variable will only exist for this transaction it's not stored for long term and actually there's some subtle attack factor I mean now the tag factors so bugs if you mix up stories in memory if I get time off to the next tutorial we can go through one of the bugs is in one of the tutorial sheets if you look good tutorial two I think it's in there of how you can mix that up yep what one so this is hard-coded in the byte code so when I deploy the contract it is hard-coded in the program right now and that's on the blockchain the hash format so this help you mean this hello world here so this is a program so gobi bytecode and that by code will live on the blockchain so hello world is hard-coded into the program yeah so what yep Amos is returned is that was up for a ban I'm pretty sure this for returns so if it says returns you'll to be this statement here effectively equal event so it shouldn't I don't think the API you should say what event is there but I don't remember I don't know I don't think it does no no yeah it doesn't so the returns just means that this function will return a value but again that's only useful for other smart contracts yeah and that's important so the reason for that is if there's a contract surviving on the blockchain that's he has an Oracle service I want the interact with that Oracle so I need the new the return statement for that so I look at the ABI I see the function I see the return statement then I can write a contract that will match that that's one of the purposes why you would have that again is for contracts pious okay any other questions yep yes so if you want to play with this this will live in the blockchain and you half the Senate transaction to the network that invokes this function yep and then anyone can read it message oh not the event no no you can't just call the event Jeff nothing I knew all the events I don't think event from the API go ahead where is the API I don't think I've ever looked to the API on remix before in the compiler I see yeah Oh a B I never used this feature here we go oh it's just copied it of course let's see oh okay there it is oh I didn't know that yeah it's a saying that the event exists thank you for that this region often I look at api's there you go awesome yeah cool okay so awesome I learned something new good job guys okay so now I'm going to quickly move on to one more task and then I'll give it over to the the build build alert crew to go through the debugging okay so what I want to do now is allow anyone in the world to set a message and a minute okay bit like a bulletin board or a broadcaster so this is really easy to do it's just like any programming we're going to give an argument or a promoter so we're going to do string message and of course it gave me up to pre memory in there so this'll only exist for this function call and then we pre message in okay on a gamer just couldn't let's keep hard-coded HelloWorld in there why not that's fun okay so we're just going to modify the function the odd and a new promoter called message with a type string and then we're going to modify the event I'm putting message in there okay very simple very straightforward so now what we're going to do is again we're going to compile it make sure it compiles now in this list and deploy and run that menu make sure you delete the old contract so go to the old contract here and click the little bin make sure you delete that just the avoid running old code there's normally a source of mistakes lived on that plenty of times and then we want to redeploy the country okay now that we've redeployed it we actually have an option now the include a message okay so we're just going to include a message to say this tutorial rocks because it rocks so much and then we're going to hit send message and send that again to your virtual network bomb every school down well click here and we'll all see that this tutorial rocks so now we can enter in around a message and I'll always be admitted there any questions yep yep so there's two ways to create a contract address the first one is typically best so they learnt they tend to be deterministic so the first one is based on your address and the transaction nouns so you don't really have much control there the other one is based on I believe also the sender's address and the byte code does that include the message the doesn't know okay I think that one of the proposals okay okay an assault oh yeah so it's just the bienick code and the assault value next to it so there you have a lot more control it can be more deterministic call so the VIN the transaction field it will be empty I don't think you actually fill that until the contracts created yeah but you can double check the code from I remember that was that you send it to when you first create the contract the transaction won't have the address in the field format I remember yeah down let's bring it back make three years of memory just take this for granted now there any other questions at this point about says everyone completed this part of the tutorial okay what are you stuck on [Music] yep yep yep that's it that's the end o the old contract yep delete the old contract had a little been make sure that's delete it because that AG yep yep and then you can deploy it again so you can redeploy it I'm filling the Massey's to say this tutorial rocks okay so a string is actually a it could be an arbitrary size of bytes so because it'd be an arbitrary size of bytes we typically just say this will exist for this function so it's not a gonna need a primitive native primitives you don't have to worry about memory but if you look at the tutorials the tutorials are very in-depth it looks being the difference between storage and memory on some of the bugs that would occur view if you get them confused okay okay so I want to do one last task because I think I put two or three minutes left so the only problem of this smart contract now is that it doesn't record any history it'll always emit a message and not keep you know any storage or memory around so we're going to try to do is keep a log of all previous messages so I'm kind of remember how to do this myself so we do this string array and there's an old messages okay I'm pretty sure when I pick public I think it's here nope didn't like that cool okay so in the code we're going to create a string array that's denominated by the little curly it know that what he calls in square brackets we're going to make it public to sort the publicly accessible variable and anyone can read it I'm are going to call it old messages okay and then here what we're going to do and I hope this works except done this in a while push message cool okay now what's important is that because this is in storage is as a dynamic array you know there's no fixed size to it it could be any size okay so just adding that new variable string array public all messages and you can also do dot push I see people type in solve with a second yep yep exactly is variable-sized ring yeah so under the hood solidity takes care of you know making sure there's enough bytes 32s cuz I think that's really a recent feature I think is really in the past of a year or two that they've had string and every one I started they didn't have a string and it was really difficult okay so I think everyone stopped typing now so again what we're going to do is we're going to make sure this is compiled we're then going to go in the deploy and run delete the old contract you know always make sure that's deleted and this is empty and we're gonna redeploy the contract you know Bom then we're going to scroll down here and now you're going to see two fields first is the send message function where again we can put in any message we want so we're gonna say if London is gonna be great fun and we can send that to the network you know bomb sent so we just scroll down for a second we'll see the latest transaction we can click it okay and we can clearly see here that if London is going to be great fun now what is important is that this is the very first item in the list you know we've actually storage something now in the network so we go to old messages you'll see that this is blue so from what I remember a blue means is not going to be a transaction it will be a call what I mean by a call is that I've got a local copy of the contract I've got the local step you know because I've got the blockchain I can simply read from it it's audio dock haul and I'll read from the contract I can also do a doc call that run the function locally the make sure it does what acts back before I send it out to the network so doc call allows me to run a function locally and not on the network so I always recommend is that when you actually write your smart contract in your web 3 I typically run the code first before I send a transaction out so I want to make sure it's doing what I expect so if we put in zero here the first entry in the list we do old matches is Bob we get the string it was stored on the network if I put in one as it hopefully blow up yeah it didn't change at all terrible air masses remix invalid oak up code there you go invalid opcode so secured error message but I think everyone gets to the up quit yeah yep nope no because you're not interacting with the network this is local between you and your blockchain no yeah exactly yeah yeah so the motivation is that you you run the function first and you can verify yep that's definitely going to cost my vote it's not gonna feel and then you broadcast that out to the network so you make sure that it works first of course give there's also no thank you did another talk about breaking smart contracts and you there's some sort of bugs around unpredictable execution good so and I don't know how easy this is the access but technically in the block header there is what is it under maybe it's under this there's a commitment to the entire state Oh so in a block header there's a commitment to the new state of the network but I do believe that there's a commitment between every previous state for a transaction in every post it so you can actually look up the state difference and the commitments for that but an easier way it's just the the read the contract yourself it will but I don't think it's gonna be an easy to read for Mom I think I don't think it's very easy I don't think there's a given what's in the block header I don't think there's a stria for that work out the difference I mean obviously can see the byte difference book but my advice would be just to read the contract I mean actually just look to your local call and look at what's up yeah for a while exactly they can't get large and expensive and there's some very very subtle attacks around it so I mean the motivation is that I mean I never use dynamic arrays but the motivation is that you know there's some item you want to add to a list so one example could be a lottery you want the append a new lottery ticket to the list and then at the end of the the lottery izes tell the the smart contract my lottery ticket is 50 no entry 50 and then the contract can look that up yeah exactly yep you should yeah you choose deleted over time so one of the subtle attacks is that you could have a for loop here so what we could do is for you and I equal zero is less than or messages length I plus plus and then we could actually emit every old message you know not the type this in this is just for fun now can anyone tell me what would go wrong here except for my error message because I miss belt it can anyone tell me the issue of this function with this for loop it will consume a lot of gas so this list has the say 50,000 element then actually the issue is that I call the sand function I run through the code I get the here I run this 50,000 times I run out of gas and what happens if you run out of gas everything reverts if everything reverts you can never finish this function execution she actually break the function so dynamic arrays are really dangerous and never do a for loop around a dynamic array that's one of the subtle attack vectors I mean I could go on for hours with all the tech factors but that's one of them any other questions yep no strings are just as big as they can be as far as I'm aware I don't think there's any upper limit except for the gas any other questions cool yep yep so I think here is that well this would be one of the limitations is that if you have an array of 50,000 elements you probably never want to grab everything at once because you're it's just too expensive computationally what people tend to do is use mappings so this is technically like a mapping you know I can access a variable so what you could do is say function let's say emit old message and then we actually put EU went in there okay I and then what we do is we just omit this there's like so what you would do then is do it individually so every new function would emit this whole message so you can still access everything but you just never never try to grab everything in a dynamic array yeah you can do that yeah yeah actually that that in my old voting contract years ago so the thing there so the idea there is that you got have a function that keeps so basically in a solidity which you can access is what is that up code is gas used gas left that's it good they made a keyword so you can actually check how much gas is left in the transaction so you can say if gas is left is greater than 50000 continue Agra dice is doing nice that's to say we have a UNT R checkpoint okay so we do is that we go through for you and I equals 0 as less than no I would never do this this is just be feeling this for fun now it's like oh messages I plus one you could have a function like this for example okay where you just see if the checkpoint else checkpoint equal I then you just stop so you look a bit like that but please never do this in production if you do you never take in this class but there you could do something like that then you could keep recalling that function it eventually get over all the elements although well that's all full of error messages so it's broken go ahead yeah yeah that's basically this idea here but this is like arbitrary number so well oblems is that you'll go in this function I should really fix this you're not annoying me so the idea is that in this function you call it it'll start at 0 because this begins with 0 and it'll start going through every element in the list and every time it goes every time it tries to ill do emit message oh messages I why is this complaining oh oops yep there you go camel kiss Chris is probably laughing right now so never do combo kiss okay cool does it get rid of that I'm done with this okay so what you'll see here is that you go in you'll call this function it'll start from I it'll then start looping through dry still of gas left yep and met a message cars left and made a message events your run low on gas and I was a store the Check Point and finish you can even do ever turn their access you should have a return there this is this is like an orbit renumber normally is that's like five thousand I small take two or five thousand it's not very big you know that could be ten thousand easy thirty thousand could you're storing something but yeah that'll be the idea you leave enough gas left over this store the checkpoint and then return but please never do this no please don't visit for fun this is the is a really good example the highlight gas and the limitation of making sure you have enough gas left so this is only for that purpose you know never ever ever reproduction cool like this yep yep exactly everything here causes gas you know so nor maybe you get to this point though you'll just see if the checkpoint in return so you have thirty thousand gas to do this giving that requirement yep oh yeah yeah awesome so now we're done so we're going to move on to the bit alert act asked so just jump on up guys yeah [Applause] you need us so the idea of this session is to help you a gap a complete development environment for smart contacts and apps and everything is contained in this repository and everything that I'm going to explain is here written in the repository so if you want to go deeper into something or if you meet some part of it or you think that something is too easy for you you can just skip parts of it go there and read by yourself so the first thing that we need to do is clone this repository and install it I can you read this is it big enough okay so I already clone the repository but you have to do that like it's a normal repository on github go here copy this URL git clone paste it enter that's it and then to install this you just have to type NPM install it may take a few minutes so it's way for everyone to install it so let's go back to your right it sarakin yeah it's npm install yeah you need to stall no - yes so if you don't have no - yes install or this npm install for phase you can go to this section on the reports on the repository that says development environment setup and there you can go to this other repository that will teach you how to install everything you can just copy and paste these things from the other repository to check if everything is already installed just paste them and if everything goes works well you should I'm using no 10 but they I will do that because it's the current LT LS LT s version so I would go with 10 but 12 or 13 see lots of work okay yeah so if you go to this other repository that it's linked on the installation section of the one I linked there's instructions to install node CI p gy p but i wouldn't recommend using note well I would use no 10 yeah okay yes everyone is told this repository so okay let's weigh one or two more minutes what and p.m. mr. and NTM is node package manager yeah so most tools from the Athenian development ecosystem are based on no sir yes so even if you than like using node you will probably be using it because that's what most people use you can use on other platforms and languages but you'll probably be very we know she is as there are more to with some more things to use Frank with the fronton there's this more friend than here really one go very deep into that during this session but you can see it's more fronting yeah that's doesn't it I mean you have to run NPM brand em in the front end which will start Parcells bundler shut it anything [Music] okay shall we continue okay so you're gonna be using a bunch of tools on this setup the main one is called bit layer which is a task runner and a development network what does it mean like with wheeler you can do most of the things you need to compile and test and debug your smart contacts so if we run npx builder help video will be executed and you see all the options all the things that you can do with it so if we want to for example compile our smart contacts we just run npx build layer compile oops compile that tip I have already compiled them so nothing happened let's do some visit clean and compile them okay so really has everything you need to test your smart contacts just like remix from the other tutorial video has its own testing blockchain so whenever you run your test it starts up a new blockchain where are your smart contacts are going to be deployed and executed and you're going to be testing testing them from outside develop chain in JavaScript is everyone familiar with JavaScript raise your hand if you are with JavaScript okay so normally you're going to taste your smart contacts from JavaScript and what it's going to happen is that you are going to use any film library to connect to the film network in this case this library is called ether - yes we refers you are going to model your smart contacts in JavaScript and interact with them through it through them so let's see a small example of this go to the paste folder create a new file example - yes here you can write your test you see mocha and chai does everyone know or ratio kind if you know what mocha and chai are okay so let's make half of it mocha is just a test runner for javascript is not something in the film related it's like a super popular JavaScript project which is very easy to use you just have to write these describe methods where you declare your tastes like this this guy is just like a section of your tastes should do something so we describe you shall declare restriction of taste like a bundle of taste that you are going to run and we did you declare one taste you just have to provide a title and a function that will taste anything you want and chai is just an assertion library that you are going to use to actually test things so realistic ladies expect something to be equal to something else and to run the taste you just ran npx viddler taste there's a whole bunch of of tests running here but the ones that we just created is here Satine should do something is anyone here are you following me yep one so one is any value we want so let's say that here it is three we rerun this test and as we are expecting it to be one like ASD should be equal to one this test will fail and we see it here so assertion error expecting ASD to equal one right so this is just a JavaScript test it has nothing to do with you in so far so let's do something with the theorem this repository already has a simple smart contact it's pretty much the same idea that but we explain like a pole where there is a bunch of proposals and people can vote for them and that's pretty much it the only difference is that here you have a donor which is you don't have to read this whole thing we're just gonna go through it so in this my contact you have an owner that it's the person who deployed it who's going to be able to add propyl sighs start the poll and close it so when you deploy this contract the owner will be automatically a assigned to the deployer and the name of the poll will be stored and our state value will is open will be set as false so list it the bad works from JavaScript so this describe we'll use it to test the poll contract and we want to taste that it should deployed correctly what we are going to do is to get a shutter speed model of that contract and we do this we do it this way way remember that everything here is written down in the readme so you can go you can go as many times as you want through this and so you can ask us about these on the disk or okay so this is how we get a contract factory the contract factory is just a JavaScript dog shed that lets us deploy a contract we are using ethers here to get the factory yeah especially yep no it's already checked Levi Bueller yeah yeah pretty similar yeah yep what's the difference well I mean it's both of them are a film libraries you can use any of them for exactly the same things I think ether she is is like we're for this for these kind of projects like a hackathon project we will bring you less problems probably and it's were documented so that's why I chose ether for for this occasion you can use both with vislar because it has a very modular architecture and if there is just like applying in below and you also have wet free place but it's pretty much the same ya know kinda shakes describe I need you can it's already like in the global context you can import it explicitly if you want but just for brevity I'm not doing it here now Norrell aflame this is viddler that it's injecting these things yeah okay so now that we have this pole contact factory we can just do deploy to get a new pole yep yeah but it's like its own blockchain so yeah now the control the contacts get compiled before running the test if this lines just lost the compilation output yeah now we are using no I mean you could but that's another set of libraries of tools we are using bit earlier and Bisleri p.m.

here middle UVM is like the testing level to a bloke eh yeah it goes to the compilation output there's like a default path to look for those artifacts but you can say any path that you want right right it's actually depression right then you can use the exact same things to actually deploy it to maintenance wait that's because it's easier like chai because it's easier you could just do that without child but it has like a very nice API to test things what you do need is a testing network and that's the main reason that you want to use builder or something like that because you don't want to test these things in a real network yeah yeah there is a config file the first everything that you need we are never not gonna go through that because it's a lot of data and it's already set up for you a that going back to these so pol has a deploy function that will take the same arguments that the constructor so here we have to pass it the name of the poll right so we just do that from JavaScript and what we're going to test if is that the actual name is using a smart contact and how do we do this it's just by getting the name from the contract and comparing it with a known value so we can get the name by doing a spoiled odd name so this point of shape is going to have the same functions that the solidity contact craft but in JavaScript just like a one-to-one model between solidity and JavaScript there is very handy to test these things also they would think of doing the test in JavaScript is that you can reuse that code on the front end of your application so here we use char just like for convenience to be equal whole number one in a way no not really because we already have one here why do I need because wait yeah name is Agnes icing function because it has to go to the blockchain ask for it to execute the name grater and then when that value returns they promise that this name represents reserves or something like that I mean I think functions are a little bit hard in JavaScript but most of the times when you call a function in a smart contact you need any weight okay I'm going to add this dot only here so we are only gonna run this test yeah it's just created an example she is think what's MPX yeah MPX Bigler test even know well midlife starts its own note right before running the tests and stops now it's called Billy Revere it's like an alternative to ganache with some smart contact debugging functionality though it's what I'm gonna show you next yeah yeah so you can go to its website here this like a section of useful links and there's the video documentation but in general if you clone this repository and read the readme you very much very understanding then now because we have very a very short time to explain a lot of things but you should be able to clone this repository and use it as the base boilerplate for your project just by reading there with me and a little bit of the test files a little bit of the contracts and there's also like a tiny front-end that you can use to base your front-end okay so if we go back here what we are doing is deploying a poll with a given name and then getting this name and comparing to the same value that we pass and as everything is going okay we are just getting a contact passing but what we want to show you is how to debug contacts because that can be a very frustrating experience and for this I'm just gonna break this contract on purpose how do we do this do you want me to go back to a it's fine here different end connects ethers to like a light blockchain that's running locally like to run that one you can do MPX viddler node and these starts like a yeah like a local Ganesh thing with some debugging output enable like here I mean it's not between an intersection but you'll get some interesting output and then you can point your meta mask or whatever wallet to you want to that yep everything is all these things are explained explained there so okay let's break this smart contact this is way to do this is just rather revert function coil just like this this will make that instruction fail and everything with a well revert and let's do that so what's gonna happen is that this line there is trying to deploy the contract is going to fail so when we run our tests we should see an error there so we see like this test on red that's because it failed and the interesting part is that here you have a stack trace of solidity and may embed in a JavaScript exception so mocha we know how to show that and that's that's what Bolivian does whenever you have secured a contract it knows which contradicts its executing is not just like blindly executing the opcode and it can shows you exactly where your test failed yeah so if one is you of your test fails you just have to go there and figure out why this of course it's here because we just studied it yep yeah that's because I I need this only here just to make things faster also we can pass revert a string which will be available in there in the error message but it's not very useful there is no things like string concatenation in solidity so we can do things like this this doesn't exist in solidity and the reason the reason why they don't exist is because these are quite expensive operations like string operations can get expensive pretty fast like if a string is to be concatenating two of them will run out of gas so great reasons are cool but not so much like you'll only get like a fixed Ravel reason there they're good for testing you can do things like making sure that a transaction actually meets the river reason that you want like we could test for these three and that's something very common there are many examples of these in the actual test of the poll contract in this repository but for now let's just run it like this so it's instead of say intense action roberta without reason we should get a very rare okay so now we get this kind of weirdo message that it says VM execution VM exception while processing transaction and there we were reason but you still get the solidity stack trace here it's just a single frame because it's only the constructor but you'll get the entire stack trace but still this is useful but is this useful to figure out which line of the contract failed but not why so because you can actually output a values from there so one of the things that build a house is console.log just like JavaScript for usage you just have to import this contract that it's called Namie claps - builder that console.log and then just call it like if it were JavaScript so we could do something like console of deploying the poll with name name and get values soft of the contract so this is how you can figure or try to figure out why your contact is failing which is kind of the same that you do in JavaScript or any other language you just output parts of the state of the program - yep you can do that in bricks like the previous thing that we saw but nothing builder right so for now the the twist that you have for the bag in our console oh and stack traces so if we rerun this test with the console log on it you need you should see it here so the plane the pole with name port number one yep can you repeat because we still have to implement that yeah that's like a ton of work specially like things like the code in the state can be extremely hard and we haven't had enough time to implement it now because mostly because we were trying to get the basics working things like stack traces and console log wearing the thing before bindler and we just recently launched these things yeah and then the other thing that you can use to debug your transactions is to enable like a very verbose build layer a mode so to do this you have to go to build layer than dot config dot yes which has a ton of things but we can ignore most of them for now and they yeah yep the I mean everything here is on a real blockchain it's like a simulated blockchain the riddler starts and stops automatically but yes there is a console contract it's not exactly a contract itself solidity library those are treated a little bit differently but yes you can actually go to the console contract like this and you won't understand anything because this is a little bit of EVM magic it's not like actually printing anything it's really that they takes these things and print them but everything in s my contact is written in solidity yes so going back to the verbose output that you can enable you just have to go to the config here most of the data if the confi is in this module exports of shape and here we have network configuration it's like a section of the network a middle area VM is the one we are using it's like is this special automatic network well and here we can you we can do something like logging enable true and when we rerun the test we are going to get lots of output for every transaction and gold that we do so instead of just seeing the shava skip execution of the test we are also going to be able to trace every transaction and cold okay so do you need more time okay so if we rerun this you're going to see a trace of every coil into the blockchain so first we do this call eath account to get the actual accounts that the node has then this net version we can ignore it it's just like an internally first thing thing to figure out how to set it up set itself up properly and then we have this if estimate gasps where is going on here is the reverse is trying to make it an section but if has to know how much grass to use and it does it by calling this if gas method a of the node which will run or simulate the transaction figure out how much grass it it takes to run it and then maybe improve that approximation a little bit to be further it's not going to fail and return that number but as aware constructor method is so with failing the if sentence section is so with this also failing because when the node tries to simulate this transaction if we fail and we are going to we are going to see this here so whatever is the name of the contact that is being deployed who is sending the entire that instruction how many is that instruction has discuss it's just zero the console.log I mean any coil to console.log will be displayed and I saw the solidity stack trace right so if we change the the contact I need to make it stop failing and we rerun the test it's going to pass and we are going to see the other things so here estimate gas was successful then it says maybe each sentence section pretty much the same one but here it use sanga's and it was mine in a blog and then there's another this a bunch of things that we first us to actually confirmed that little section has run successfully and then finally there's this earth coil that it's gonna call Paul name like the function name of Paul and you get some data like who sent it which was the oil contract and that's it I mean if you have more things like another code to pull name or record to another function you are gonna see more if Coy's or if sentence structures depending on whether if it change the state or not of the contract so if your according to a function that changes the state of the contract it says we'll know that it has to send a transaction and that's what its gonna do if it doesn't change the contact it's gonna do a call that is what path we explained of running it section locally and returning the return data of that a simulated transaction yeah sure you can do that yeah so to do that you are going to use the Kinect method of ethers here you can pass it I think an address or what is called a signer Sanger's are either subtraction for account but we first have to get them so we can do these your sickness and then do something like sign there one let's rerun it these apps Thanks so yeah it was them from these unfriendlies right so things like this are explained on the readme so if you want like to go to the details of how to do all these things and many more you can read that any questions so far first and then think of me yes yeah it's actually it's like a binary search of of gas like you first run with a random value the transaction like is simulated not like actually run date instruction see if it fails if you'd face increase the gas if it facing proceed if we pass by a little bit lower gas and just like that yeah I mean you could do something clever or more clever but it's very to be whether you are going to get like get a right estimation then you need faster yeah yeah yeah the thing is that you're gonna have like different contour flows for a written section depending on the actual state of the contact so let's say that you have an if if state variable equals equals 1 yeah so it's gonna depend on the on the state which is an actual problem for IVA for production applications because sometimes if your contract is being used like a lot and you may run into the situation where a gas estimation is not correct for the time where your transaction gets mind like you may smell the gas do this dry run of the transaction and then this the state of the contract changes before your transaction gets mind because of another transaction getting mind before it and that's that can be a real problem so what you normally do is multiply the gas estimation by like I know 10% more with something like that but most libraries like B layer or for other frameworks already do that for you you may waive for a very long time yeah so one of the one of the reasons to use like a simulated testing network is that you can mind that section immediately so whenever we D be M receive certain sexual intersection it might say new look and so you don't have to wait like forever for your tests to run you could actually run them against like even main it if you are willing to spend some money on them but that's gonna be super slow because you need to wait for ten sections to be mined so to me okay so we are running out of time so I invite you to basically repository if you comment and read through the way through it with me which has sections explaining all of these things a more even things like setting up your research to decode a prepared explanation of ethers or if you don't know how to work with mocha and chai there's always I know five minutes tutorial that it's like just a like an actual test with lots of explanations on what all these things are why describe it's they are why it's there without being imported and things like that and then after the first explanation you are gonna find another thing other things installed like waffles chai matches this is a library that's like very handy a assertion functions to a chai so you can test for things like we were treasons or you can also test that a given event is emitted Amit yeah in certain section its paradigms and things like that this more info how to debug the transactions it's pretty much the same that I but with more deep more details some explanation of how to deploy your contacts because so far we've been deploying them to Whistler ABM but that Network is both automatically created and destroyed so this will be useful for things like testing your your front-end or actually deploy you need but the script that will help you deploy to a real Network there is a bunch of networks to try a yes my contacts on be the IBM can also be used like persistently you subscribe to run npx midler node this is gonna start a chase on RPC server that a written sectional every knows how to use every wallet and library knows how to use but there's also a bunch of testing networks live and actually maintained and more realistic or similar to may need north side called Rothstein : wrinkly ungodly you can use pretty much any of these or lifting work and here there's an explanation of how to use them and finally there's like a super simple front end bit with react he doesn't have much functionality you just kind of get like an overview of the state of the of the poll and so and like very basic voting functionality but it's it already has things like connecting your contract to the network setting up ethers a fixed like that that can be very cumbersome to get bright the first time so take a look at it most I think all of it it's contained in a single file just explanation for all the things that it does and that's pretty much it enjoy the hackathon showing the disk or server we are going to be there this a bit layer channel you can make any question that you want about building or about this tutorial so feel free to shine and ask whatever you want thank you [Applause]

Automatic transcript — names and jargon may be misspelled.