Not Quite Water Under The Bridge Review of cross chain bridge hacks Jan Gorzny
Ethereum Denver·Sun, Oct 9, 2022, 12:00 AM
Speaker
Transcript
[Applause] looks like it's all good and you can hear me good okay so hi my name is john gorsni i'm from kwan snap i'm here presenting evm to evm chain bridges the good the bad and the ugly i apologize if you came here for the other talk which is uh water under the bridge regarding hacks about these bridges that talk will be given but it will be given on saturday by my colleague martin instead of myself we switch this in part because of just the logistics but also because this is a better introduction to the topic so if you don't know what we're talking about for that talk this one might help you a little bit so this presentation was created by colleagues at quan stamp alex and martinette who unfortunately couldn't be here today so i'm presenting on their behalf so in this talk we're going to give a very rough design overview of three um chains that train that bridge assets primarily to polygon i mean these uh chain these bridges may chain other assets to other chains but we're going to be using as an example transferring dye to the polygon network and just exploring how these systems work because not all bridges are created equally and maybe people don't know that we certainly didn't or at least i didn't and so we're just going to talk about how they work we're going to deal detail some specific acid bridging mechanisms and we're going to talk about the fee structures a little bit of these bridges we'll also highlight some notable ux features and some other technical things that these bridges implement so as i've already mentioned we are going to be talking about taking dye from the ethereum main net onto the polygon main net and we're going to have a couple options for this the first option is to use the polygon native bridge right this is the bridge that polygon themselves implemented to get acids from the ethereum main net onto the polygon network but since polygons open source and there's you know a lot of other decentralization going on naturally other people have also tried to solve this problem so we're also going to look at two other example bridges multi-chain and hop exchange we didn't pick these for any particular reason other than we came across them we thought they were interesting and they were open source so we could take a look at them so let's start with the polygon native bridge this is the one that was implemented and developed by polygon so unsurprisingly it's run by polygon validators the source is available online and there's a url there if you do want to go check it out what this bridge does is it takes real dye on the ethereum maintenance has some specific contract that you know and love for dye and then it maps it to a so-called pos die on the polygon main chain so those at those contract addresses are not necessarily the same as we see here on the bottom but the idea is to maintain a mapping between dye on polygon and die on ethereum in a one-to-one sort of mapping this requires some sort of a state sync mechanism to keep these numbers always in check because obviously you don't want them to be askew so we're going to look at that state-sync mechanism and just as a note it does take about 5-10 minutes to bridge assets from one chain to the other if there's a large amount of die being transferred it might take up to 24 hours so the first use case is well we have dion ethereum we want to see what happens if we can put it onto the polygon chain so what we do is we call a function through the ui and essentially we say we want to put 15 dye from ethereum onto polygon transaction that occurs on etherscan is something that looks like this the details are not particularly you know involved it looks like we've transferred 15 die as you can see there and then on the other hand on the other end sorry what happens is that a state sync transaction occurs on the polygon chain so this is a bit odd because what is a state sync transaction this is not something that's particularly common on ethereum in fact it's not something that's native to ethereum and instead it's a type of transaction that the polygon validators sort of put onto the polygon chain this is a modification that they perform to the polygon consensus so they create this thing effectively creating dye on the polygon chain and they did this because we locked dye on the ethereum contract for this bridge and so we lock it on ethereum and then we get some new stuff on polygon and if we want to go the opposite direction we'll have to do the sort of reverse of that but let's take a quick look at the technical end of this because while the first transaction was really sort of straightforward we just sent 15 dies somewhere which is what you'd expect this transaction is a little bit more involved in particular we see that this transaction is to and from the zero address which is a bit odd because normally you can't really get transactions you know coming out of the zero address in ethereum and yet here it's happening and this is because of that modification to the consensus algorithm that polygon has done in order to run their network moreover we submitted a request for 15 die and so if we look at this transaction which has um our address on it on the bottom we also notice some other stuff has happened there's something called heath that's present that seems a bit odd at first glance am i getting free stuff that would be great um but no that's actually not the case and we'll actually talk about what happens there a little bit later in the talk but it's ultimately an artifact of the technical features that this bridge particular supports and we will like i said we will talk about what that is exactly but for now we did do some stuff somehow 15 die has been created on polygons we're pretty happy we're almost done with our our transaction and if we take a look at exactly what happened well in the ui on the ethereum and we called a deposit for three through some ui we sent the right address of the token that we wanted to create on the polygon chain so we called deposit for passing in the parameter of the die contract on the ethereum main net along with the value that we want to transfer ie 15. and then we locked some tokens as part of that transaction there was a method called a lock token somewhere in there and then what it did was the contract that handles the ethereum sorry the polygon bridge did something that says let's event let's omit this thing called a state sync event and so that's what this sort of sync state thing is and so they omit this event on ethereum and then the polygon validators watch for and process this event as part of their job and when they see one of these events they can then interact with the polygon chain in particular they will say okay we saw a state sync on ethereum for 15 die let's now invoke deposit from the die contract on the polygon network and give this person that is my address uh the corresponding amount of dye on polygon so pretty straightforward uses events to track if we want to do the opposite say i'm bored with polygon it was a great experience but i want to take it off what do i got to do well in this case it's not quite the same this time what we do is we call a draw function on the ui for polygon and then we have to wait for something called a checkpoint and a checkpoint is essentially just a time delay so that the polygon verifiers or validators can make sure that in the time in some suitable time frame after this function is called uh you know this function or these coins are not spent anywhere else essentially just making sure that in sort of the near future i'm not double spending this die and that people sort of acknowledge that i am in fact you know we're drawing this on the ethereum end of it uh eventually we are able to call an exit function from the contract corresponding to the polygon bridge on ethereum and in particular i have to pay for the gas fees for that so if i want to draw my my uh dye that i've now sort of sent back to the ethereum main chain while i got to make an ethereum main chain call naturally that cost gas right why that's important is because we'll see some other changes or some other bridges that don't necessarily do that so in particular the steps for taking our dye out of polygon and back onto ethereum so we call some withdrawal function we waited for some checkpoint this checkpoint allowed some time to pass some validators to compute some block hashes to make a merkle proof that sort of says yeah we actually did do this burn on the polygon network and then eventually we can call exit on the ethereum main net um except this proof of burn that validators have created verify that you know we in fact did that and then get our tokens back there's also some other notable features of this chain that we didn't really talk about yet we have alluded to some of them uh the first one is polygon minted tokens so typically uh what sort of traditionally has happened in the ethereum ecospaces oh i've got a new token i want to do something with it i want to create an erc20 token to sort of handle it i'm going to deploy first on ethereum it makes a lot of sense a polygon actually lets you sort of do the opposite where you say okay i'm going to deploy first on a layer two solution in particular polygon uh and you can sort of treat the polygon network as sort of the original source of truth for this uh token which is kind of neat for details there's a there's a link there you can click on there's also this other thing called the fx portal and this is actually related to that heat that we talked about and we'll see it a little bit more uh later as we talk about another exchange or sorry another bridge um but this sort of allows a cross-chain tunneling mechanism for other bridges and hop exchange actually does take advantage of this so in summary well how does this bridge work well it's mostly baked into the validators not surprising polygon built this thing they have validators might as well put them to use it maintains a token mapping between assets on either chain and finality depends on other events in this in the ethereum context or checkpoints in the polygon context in terms of the user experience to send stuff i only had to make one transaction after the send tokens obviously there was an approval that probably needed to be made but ignoring the approval it was just one transaction on the other hand taking it out was two transactions i had to make a call on the polygon main polygon chain and the ethereum chain some notable features as we already discussed the fx portal which we'll go into detail about and some polygon minted assets being a possibility so let's take a look at another one just to compare because the idea is to compare them and see how they're different so this one the next one we're going to take a look at for no particular reason is multi-chain formerly known as any swap so multi-chain supports a bunch of chains not just polygon and ethereum it supports 25 different chains of various kinds the code is open sourced so we will able to take a look for concept but we you know so we are familiar with taking a look at code but this is this is not a security review we're just you know looking at the code to see how it works and seeing what's interesting about it to compare the time it takes about 10 to 30 minutes to send die from ethereum to polygon and again if you're sending large amounts it might take you up to a day this is just to make sure that things work the way it should if we want to make a deposit what we're going to do is we're going to make some sort of a call that allows us to send guy to something on the ethereum main chain which is fine we're going to send 15 again in this example on the other end we're going to receive something so if we take a look we have actually sent die and on some contract somewhere we instead got 15 any die which is not the same as real dye it's supposed to be pegged one to one in in some sense and the idea is that we're going to use any dye or any token more generally as an intermediary for these tokens and so i will send dye get any dye and on the other end i will convert any dye into dye if there's sort of a market for that so this is the other end on the polygon side we take oranida and we want to put that into um pos die which is the real dion polygon and so what we did was we call swap out any underlying and so we took some dye on the ethereum main net we burned it we created some any dye and then we bridge the any dye and then on the on the polygon side we'll take the any dye we'll burn that and we'll receive real pos die so uh the multi-chain nodes do this they burn or they create the transaction on the polygon side and uh if on the other end on polygon we get any die but there's not enough pos die because everyone's holding on to the ps die then we're sort of stuck with any die which is not necessarily a problem because i can either swap that for any die later when that becomes an option i.e when someone's willing to sell their their any die for this or sorry their pos die for this any die or i could just bridge it back to the ethereum main neck and main net and take back my die as i want going going the other direction taking my coins now off of polygon and putting it back onto ethereum uh the nice one nice part about this bridge is it's totally symmetric now i'm saying well i have any dye i want to get my dye back on the other chain and so these any dye sort of intermediaries allow the process to be totally symmetric as long as there is this ability to swap any diet to real die or any token in general to whatever the real token is and this does use an amm for this um the good news is too this is also you know very effective for other chains that do similar things like bsc so in summary um we did it has a more universal sort of token uh transfer system uh it uses these and tokens as intermediary and the finality is all based on events being emitted on the appropriate chain uh for the ux it's a little bit more streamlined there's one transaction in either direction and the relayer charges some fees um along the way i don't know if you're paying close enough attention i didn't highlight it we actually got 14.1 instead of 15 die on that chain we can also do polygon minted assets with this bridge which is kind of nice and it doesn't quite yet support cross-change contract calls for the general case so the last one we want to take out take a look at in the last five minutes is hop exchange so hop exchange typically operates between ethereum and various layer two roll-ups in particular like polygon gnosis arbitrarily or optimism uh code once again open source we will take a look and see how this functions under the hood at a very quick glance and it does have you know some similar aspects to multi-chain but it's a little bit more tailored towards layer twos rather than arbitrary layer ones so if we want to take our die once again from ethereum we've got lots of dye on ethereum we want to put it onto polygon uh we're gonna try this experiment one one last time to see what the differences are we're gonna make this function called ascend to l2 which you know may have been done through ui and here we're actually going to send 40 die instead of 15. and the reasons for this will become a little bit clearer a little bit later what happens well hop exchange uses that fx portal that allows other bridges to sort of use the same state sync transactions that we talked about earlier for uh the polygon native bridge and so we actually have hop exchange creating something called an h die so this is something similar to the any die that we saw for the multi swap but it's now sort of h token for whatever the token is and so this is also what that h eth was so these state sync events are sort of summarizing and aggregating a bunch of transactions and so when we saw h ether earlier it's not that we were getting it just because the validator said well a whole bunch of people were transferring a whole bunch of assets let's do it all at once and the ones that happen to come with an h sort of as a prefix come through the hop exchange protocol and so here we do see we have our 48th being transferred but there are also some other things going on in the state sync which is again from zero to zero okay so uh so what did we do we called sent to l2 it swapped some dye into some age dye using nam and then uh we're going to actually make the call on polygon's crosstrain contract so not hop exchanges contract we're going to do it through um polygon now it might be hop exchange calling polygon but ultimately we're using the polygon bridge contract in some way in particular requirements of this fx base root tunnel thing whatever that is obviously there's a lot more details we could talk about but i've only got 20 minutes uh and then eventually that contract makes a call that says okay let me swap out h die on the polygon chain to pos die which is sort of the real die on on polygon and we're all really happy that we got her die in the end so almost the same but you know a little bit different so we've seen now three different ways of doing this we're not done though anyways let's try to get our money back and this is one of the bigger differences in the hop exchange protocol we want to take our 40 each back remember we sent 40 not 15 this time we try to do this and it looks like we're going to get about 3.
6 east or sorry 3.6 die uh that's a bit odd we wanted 40. so what's happening here well here the ux is changing a little bit instead of me having to call an exit call on the ethereum contract for this bridge and pay gas fees the hop exchange will send me my die right away and so what i'm doing is essentially paying for this gas fee up front and having someone else make that call for me and this is why i'm losing a whole bunch of my money in this transaction at this point it's about 91 of my funds loss but that's only because i'm making a very small transfer the amount of transfer the amount you transfer doesn't really change the gas cost and so if you only send a couple you're going to lose a lot of that to gas because a lot of things uh if you send a lot it's not so bad okay so you can see we did lose 91 of it so um we already talked about this i'm getting the signal to wrap up so just note uses h tokens not any not any tokens the finality is mixed depending on the chain that goes on um and we only have to do one transaction on either direction but we pay for gases gas fees in different ways okay so in conclusion a lot of bridges they all kind of do the same thing but there's a lot of little subtle things in between them so if you're looking to build one or build on one you should probably understand how it works because maybe things are different good news is they all did work we didn't find any issues obviously it wasn't security review but you know it worked that's good that's what you want to see and yeah there's some nice mechanisms so if you have any questions please come find me after or come talk to me or anyone at quan snap about this we're also hiring so please come see us at booth p25 whenever that opens or on the top build floor at the mirrors art gallery thank you [Applause]
Automatic transcript — names and jargon may be misspelled.