# Conquering the Nonce by Mike Garland (Devcon5)

- Channel: [Devcon](https://streameth.org/devcon)
- Date: 2020-10-09
- Duration: 15:08
- Topics: blockchain, smart contracts, distributed ledger, ethereum
- Watch: https://streameth.org/watch/yt-mMjrB0rMfjo
- YouTube: https://www.youtube.com/watch?v=mMjrB0rMfjo

## Description

Visit the https://archive.devcon.org/ to gain access to the entire library of Devcon talks with the ease of filtering, playlists, personalized suggestions, decentralized access on IPFS and more.
https://archive.devcon.org/archive/watch/5/conquering-the-nonce

The nonce - just a single, incrementing integer that lets you send your transactions to the Eth network. Simple, right? Our experience working with hundreds of dApp developers tells a very different story - thousands of failing RPC requests, transactions that never get mined, and dozens of hours spent debugging with nothing more to show than a fragile work around. Overlooked and under considered, nonces have been the single biggest pain point for tons of Ethereum developers.

In this talk we will share all of the techniques and best practices we’ve gathered in our hundreds of hours helping DEXs, dApps, and more solve exactly this issue. A few thoughtful design choices and properly handled nonces can lead to better products and significantly higher quality of life for developers.

Speaker(s): Mike Garland
Skill level: intermediate
Track: Developer Infrastructure
Keywords: technical

Follow us: https://twitter.com/efdevcon, https://twitter.com/ethereum
Learn more about devcon: https://www.devcon.org/
Learn more about ethereum: https://ethereum.org/ 

Devcon is the Ethereum conference for developers, researchers, thinkers, and makers. 
Devcon 5 was held in Osaka, Japan on Oct 8 - 11, 2019.
Devcon is organized and presented by the Ethereum Foundation, with the support of our sponsors. To find out more, please visit https://ethereum.foundation/

## Transcript

[Applause] hey everyone my name is Maxime that they talk super excited to talk about this make some of the most frustrating things to attract developers a lot of people building on etherion being on set but really not the case totally will affect through the day background nonsense what they are probably cause start to notice all those frustrations out before you started no no worries if you don't we've been very quiet building things for about two years in the lefthand space but no PR marketing we empower us a deeper structure for a bunch of awesome companies like auger 0s for bikinis building all the tools on top of that evil is really developers focus on the things that matter building either a key your own green spaces and so to have really been working let me have to work with some awesome folks doing that now let's start with onset flavor there will be a lot of nice space funds in this presentation this nonsense talk about the theory mountains they're really two things we're talking about I don't know long is nonsense for miners this is the way they will block Awards incrementing this not stupid the graphical puzzle proof of concept foremost you really with the other thing is applications grand users when you're submitting transactions I mean this is given with a nonce is basically the number of transactions that are confirmed from the address and they are submitting them from if they stood the way that you're in with the Semites that it works that's what we're going to talk about today I don't know too much about the my excitement that's spent a lot of time with developers during a stuff on the application side and then either side that's that's look again so you sort of it for single word announces number one you're starting from scratch super easy you're not sincere oh that's great real straightforward so then those get to much more difficult PSD permit my one it's a counter straightforward you all know if I don't have the rules that started its estimator what happens and things go wrong so if your boss is too low you know anyway it's not a big deal the node will reject it your request will fail it'll say knots to Lois you very easily remember submit where it's interesting in the soup and on system hi your suppress actually basically sit around the men pool it won't get mind so that so basically your mouse counter matches up which will out of cases is hard to do because you as a human you're not sitting there he announces their happenings your application test adjusts all the other problem is a starts to give the two main purposes for us as you know digging line exists in the first place what is for or even transactions the others to prevent certain kind of tanked so you're really a simple example Lucido smart contract my social network I'm gonna be able to do things like create users having friend people deep in accounts if you want unless let's get ended these are creating account they find somebody sometime later on they delete their account all the right is time what can happen if you don't have proper ring and really honest is a way that this is ensure the appearing the world initially mentioned you know the right side everything's gonna with you know how it feels once you've number three of the user into account when you expect one possibly from here then there's the attack side so single-pane example witnesses in all strands to survive nonsense a mousetrap a bottle we need optimistic grab that transaction and send it as many times as he wants because there's nothing that distinguish should be leisure to a transaction from the bat one and so the outcome here is basically on replay it's half can basically just repeat this transaction over and over until I lost have more money and Bob has profited massively with Hudson's Alex submissive that same original transaction with announced let's say number seven when the liner gets Alice's actual transaction and the counterfeit one doesn't really matter which one is mine both of them will result in the same thing just that obvious we need from Alice but when the second something to be mind it see that the nonsence still seven excessive on speed eight now so that connection fails and you can't kind of like repeat this motion attack or over so you know all these things need us to where he does they're not secret formula nasa's so you saw the question is this your personality if so you're not some zero you're going to go no problems there we've not either keeping track of your thoughts if you know how many transactions you've sense you're also good to go so just an expanded transactions awesome you're not saying it's not no worries there's backup plan you can call something like get some is actually pending the result is you're nuts super simple it's a repeat your applications while functioning you're often and probably will cross the drug as it turns out a lot can go wrong so as I started to get here let's let's go in on one of these in that step so come to a you know the number of transactions you have that's supposed to be your knots that's where knots means so you should know that house turns out it's not actually all the time probably isn't a lot of times it's been fail on you three years that most applications are sending multiple transactions at a time so they intended for multiple transactions to appear in a single block when you do that they're submitting them with in commencing analysis let's say 0 to n in this case now suppose what happens if one of those nonsense fails so the same transaction transaction the gospel we go matter was earlier we're not there's something else that's too high it hangs and since there's no transaction that's succeeded with nonce one at this point all your other transactions are having effectively done you might ask for mine so there are a couple of ways this can happen some that are failures so they're not like the most common way this happens is also transactions one one high in X for instance certain traits product passed so failure in this case means a failure if we properly bypass the mind or even some basis for outright failures so as a river from our around earlier you go back a plan you can call get turns at impending snow worries expand we have a plan problem is this also fails the huge chunk of the time for completely different reasons so that's sort of an architectural reason most apps we're building out here you need to scale beyond the single node we do any multiple nodes in the webserver world usable a balancer nice and straightforward turns out we do that we have a theory on thanks for it to break pretty quickly a lot of people we talk to when they're really under in-house infrastructure this is exactly they do a lot of third party API know things in this way it makes managing ounces bucket are so pretty table here fire got bunch of transactions theories getting routed into one of the many knows low bouncer and oh my you to call pendant it might not what happens is this also gets routed to only one with three nodes and I have three different options depending on where exactly my career gets travel to so this is kinda getting consistency problem here is that none of these mountains are correct should be three two and all these cases we actually battering this isn't a race condition so you know eventually these transactions will propagate to the other nodes - little bouncer so if you wait retry eventually it might work so that's one way of handling this but it's not super super well functioning for applications that have got like latency concerns so then there's concern here that makes me this is more difficult to deal with again the parry actually define anything differently this takes a lot of people a long time to find out and when they do it's usually after probably a week of really really frustrating debugging so yeah when they talk about endings basically anything in the mempool so you spit a transaction it hasn't been mined yet it's considered ending the therapy there's actually this idea of like a virtual ending block so it seems that the new thanks we'll be buying in the next block of network shows only a subset of the things that gets consider spending which is that you might actually end up with the wrong note the wrong house prepared when you would have gotten the wrapper from fungi so go beyond our table here that's where Perry said you get zero in all cases so again things are not working there are other things that are going on here that you know biggest again more complicated things like race conditions there's no way show up everywhere if you submit a transaction than all X mounts or get your next encounter very quickly you don't actually agree on so you know things are possibly happening it's a pretty complicated problem and so things break apart and eventually fix so you know we've got two points of contention a few steps that theoretically work but only sometimes but actually not this is the one case like the only mounts to be able to be really amounted about all the time is with non zero you can start at a common area so that it gets complicated but there are a lot of other things that go wrong so the things we talked about already there's also problems with multiple services managing the same address when nodes we start the menfolk and get cleared which can affect nonces changing the organization's have a huge impact on this so there's a lot to worry about as a developer when it comes to this single bitsy claim we released it gets very good so what we do like how do we actually start the analyst problem one thing is wait for you to point out they don't think that's the right solution I'm guessing a lot of people in this room have issues they're building right now they have users they need a solution for this in short-term and waiting for is not going to do that so let's start on the right without us except one it's really know your infrastructure it doesn't seem like alcohol like this or I want the traditional ways of handling this should instruct urban actually do so you know you know is your in-house system good balancing those is the API abusing doing that it is you can still make this work it's just going to look harder and you need to have this in the back your mind when you're building things out things that are in Hana kotti any losses the other thing is to do the excess depending State so nonces are effectively the way that Henning State become this design state so being able to see what spending must drop to all these kind of things is gonna be really really helpful some services don't allow you can do that if you're we don't manage to probably pendency or good shape and then are you writing any parity so I want to say for people who are using parity is a super quick switch that use parity next nonsence to get transaction pending behaves like you would expect it to so work the same way that some are doing transaction this basically understand your new space so today you're doing with those transactions ordering might matter the number of transactions transactions throughput might matter lates of all these kinds of things so you need to come up understanding you space to know how you're gonna Bridget gnosis and the one what kind of Lea had this consider respecting earlier on it's more a contract development I'd ever trade-offs in doing this I think a lot of people don't end up getting announced problems until things are deployed they have users users start complaining or they're throwing developers do suppose I think through these problems early cool so extraordinary solutions number one is mutating analysis state is super super comfortable that way you can minimize your dependency on these these big guy calls that tend to have their own set of problems we store knots as a single atomic counter putting something like Redis all your services can access it increments it you're going pretty well and then we can you to when you're reserving things you can use those API calls to get the kind of see things to be done with the next is these know up fillers so this way of handling they kind of like hanging transactions problem so let's say you submit five transactions all once week romantic nonsense thanks for going well initially they're gonna mind successfully and then eventually one fails let me talk about before the next two in hang what we can do this basically greatest called Anila zeroing transfer to are going to address submit back with the same knots that the transaction that fail there's a really quick way of jump-starting the system again this is pretty space dependent because it means that your transactions can't be dependent on ordering let me talk about earlier but if you don't have that apartment that's a really solid way to keep things have well-functioning so other things just quickly running out list this is a 20-minute talk make transactions experience with would've heard things about if we're ring is really important for you you basically patch so much of operations into a single transaction as our contract kind of distribute them effectively using one transaction for address per block this is basically ideal like you submit one transaction and you wait for it to be mine until you submit another transaction that's not gonna work for a lot of people to do is very slow but you care about minimizing failures much more than maybe my some latency than this might work for each case it also gave you a nice day addresses so one of the things that's how big here's a bunch of services using the same address which are hauling phonetic nonsense which makes this world much more to deal with you can isolate each service to use its own address you can kind of make place before much easier for yourself we're not things from stopping on each other and then resets us were super super happy to help this is something we helped with online ad space and sitting basis for a lot of our users again like a lot of this is super you space dependent we're happy to think through these things with you so let's go you're running into those problems we're not the kind of talk for you a lot of that spaces we've we talk through and then finally open source source talk behind you will this week that are tackling exactly this problem which is really lovely so we got bloom earlier really cool stuff in tribute pay attention and hopefully we can all solve these problems together Thanks [Applause]
