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

Loading player…

J2 PAIN 04 Fredrik Harrysson Running Rust contracts on Kovan with WASM

Ethereum ParisTue, Oct 9, 2018, 12:00 AM

Transcript

all right hello everyone there has been a sort of last-minute schedule change so if you looked at the schedule yesterday you would have seen that it was supposed to be a touch diner here talking about parody but instead you have me so if you expected you times hard to disappoint I couldn't possibly fill her shoes but instead you get me talking about running rust contracts on covin with wasum and that's also the reason why I wasn't lumped together with the wasn't talks yesterday so if you washed them yesterday then you have a little bit more today my name is Frederick I'm a core developer at parody technologies that means I mostly work on the theorem clients but I also do some other stuff and I I spend a lot of my time thinking about how to make blockchain development better both for infrastructure developers and app developers at parity we have these roughly three areas that we operate within one is partnerships with companies and organizations that rely on our software we help large and small companies alike through Enterprise projects and advisory we do what we call core infrastructure development and that's what I'll be talking about here today and finally we're contracted by the web 3 foundation to build the first implementation of poccadot so core infrastructure what does that mean it's mainly any theorem clients that you can use to run maintenance but you can also use it to run a local development network or on test nets or spawn up your own networks but really it's aetherium plus a bunch of other things so we have a thing called secret store that's a distributed key generation mechanism that allows essentially contracts to store and distribute secrets we have private transactions that allow a set of nodes to privately transact and commit and equipted states to chain our pari decline features a pluggable consensus module so that you can replace proof of work with proof o of authority or proof of stake or anything else that you might want for our chain and we recently shipped or actually all of these are kind of recent chips but we have permissioning which is sort of low-level configuration options that lets you set permissions on a network and these are also controlled by smart contracts so you can say that you know only this IP is allowed to connect to the network or only these accounts are allowed to deploy contracts we have a JavaScript library that we call the bonds library which is sort of a high-level reactive declarative library where you just say what data you want from the blockchain and it uses our pub sub-layer to automatically update that data when there's a new block and finally what I'll talk about here today is our was in DM that we're shipping very soon and we also have a bunch of other stuff going on but these ours are my own personal topics of stuff I think is cool so was amande webassembly for those of you who missed the 5 talks yesterday I'll do a very quick recap it's a standard developed by a w3c workgroup that includes Google and Mozilla and a bunch of other people and it has major browser support by now and it's quite well adopted it aims to be a sort of low-level assembly like instructions that they can run in the browser because it runs in a browser it has to be high performance you don't know what this browser is running on it may be some mobile device and we also want a compact representation so small binaries because again we're shipping these things over the internet to devices that we don't know what connection they have LLVM has target support for wasum which is a pretty big thing so there's a bunch of languages that uses LLVM as its back-end for compilers which means that you get like 15 years of compiler optimization for free if you use wasum why wasn't for blockchain so there's the core idea of sort of standing on the shoulder of giants there's been many years of work put into awesome both by compiler teams and by standardization teams etc we could probably get to the same stage with the EVM but it would probably take a couple more years but the main thing is really that it's very easy to make was um fully deterministic so if we compare to like the Java VM the JVM the JVM has garbage collection and has a bunch of other things that make its non deterministic and it would be a very daunting task to make it deterministic whereas with Waze them you kind of get that for free as long as you exclude some operations basically means you don't have to change the VM really at all you just do pass your code through a validation layer and of course a boon of wisdom as well is that it allows smart contracts that are written in any language so typescript C C++ anything that goes through em scripting or LVM or you know Haskell rusts Haskell probably a bit less suitable because it would have a huge runtime and obviously we parody our fans of rust we write rust for basically everything we do and we think rust is a pretty suitable smart contract language that's what I'll kind of show off here today as well so whaza Montcoal ban for those of you that don't know kovin is a test nets its main goal is obviously that you should be able to deploy a contract tested make sure that it works and then be expected that it works the same on maintenance but we also want a little bit more out of code and so it's sort of a test net plus we like to think that we can sort of experiment with features there we want to be able to like ship secrets store and let koban users to experiment with having contracts store and distribute secrets and wasum is another one of these examples where we want to try to ship this see how it works if people can break it or not and if they use it or if it's useful and it's currently targeted at block six and half million which is very soon what's important to notice that EB MN was and will coexist on Kovan so both will be there both beams will be there and you'll be able to call you know an old EVM contract from wasum and vice versa you can obviously also run all of this on a local development network today that's what I'll be doing and we've built some rudimentary tooling around this so we have some libraries for rust and we have some tooling to make this process easy so I'll try to attempt the most dangerous conference trick of all which is a live demo like I briefly contemplated actually writing some code ly but I figured none of you actually want to see me sit here and write code so I'll just walk through the code and then deploy it so we start with this which is an important feature of rust and many other languages have this feature as well but it's something that's absolutely necessary if you want to build something for blockchain and that is to be able to exclude the standard library that comes with your language so by including this little statement we get a completely blank slate and we just get what we explicitly include then another very important feature to to sort of consider here is that we can use whatever package management solution we have in our language already so for us that package management solution is cargo and it uses crate Co so we've built a bunch of libraries like library for hash types and standard library for wasum contracts Stan library for interacting with aetherium the externs and some stuff to derive ABI set cetera and we can just use our package management solution to automatically include these we've published these on crates that I oh and it's it's all sort of standard rust then we get to the actual contract bit so this is a donation contract it's just like a little simple hello world that I wrote up and it doesn't actually do anything very useful but you can see that we include an H 256 which is a hash 256 bit hash we have a concept of an address and then from P was Emma theorem and we include read rights and ER value those kinds of things those are externs they're pull in data from the external environment of this transaction so sender is whoever sent the transaction values how much ether they sent then because all state storage in ethereum is essentially a map of 32 bytes to 32 bytes we don't really have a clean solution for doing state variables but this is our solution for now and we hope we can sort of fix this with some macros and I think this is a good example of the stage we're in we can sort of do stuff but not everything is the way we want it to be so we declared these three keys total donated top donor and owner and they're just like one two three in the first bytes and then we store data in these places later this bit though is sort of what's really clean and needs in the solution we have a trait that fully defines the contract that we want so it has all the types you can declare whether it's a constant whether it's an event and you the little thing on top of donation contract there shows that we automatically derive adjacent ABI from this trait so we have our contract fully defined now we just need to write an implementation for it this is what the implementation looked like we declare structs that is a donation contract instance we implement the donation contract traits for this instance and we have a constructor that gets called when the contract is deployed and only when it's deployed the total donations reads from this key that we previously set and returns the value and so you can also see in the constructor we're actually initializing this value to zero and we're initializing the owner to the sender essentially the deployer we have a top donor that just returns the address of that donor and the donate function reads the state of this from the sender and the value and we can do whatever stuff we want there and then finally we trigger this donation event with the draw function that checks that the sender is the owner and they can withdraw and we have this call that in this case is an ether transfer but it could be a call to other contracts as well and what's neat here as well is that after that last function we actually fully defined our contract interface but we can have whatever helper functions or libraries or whatever our rust code we want outside of this so I put in two helper functions to call out an address from storage and to set the top and donor if the amount is larger than the amount registered then we have this little bit of boilerplate that sort of makes all of this work so the deploy function is what it's called when the contract is deployed and only then it dispatches whatever inputs to the constructor and that's it the call function is what gets called when you do any sort of calls into this contract and again we have a dispatcher that takes the inputs runs it through the contracts and returns whatever value that that function returned so that this is the sort of piping that you need to make your language work with aetherium and because we've learned from our mistakes let's write some tests so what's neat here again is that we can use the exact same testing infrastructure or whatever frameworks we want from our other you know projects and environment so these are just plain standard rust tests we have the P wasn't test library that we import here and that gives us a bunch of tools to be able to manipulate our external environment here and if you look at this test it just says that it should actually donate and tally up correctly so we defined two addresses we can use this extra updates to extra to update the external states setting who the sender is what the value is then we call the constructor we check that it's initialized we call the donate function check that the total donations is actually 500 and then we can update the external state again setting the sender someone else setting the value to something else and then we call donates it should tally up correctly to 750 the top donor should still be the first donor and what's also neat is that we can get like get the log so we can get values from this external environment as well so we can check that there are indeed two log events we could even check that they are what we expect them to be so that's roughly what a contract in rust would look like let's let's actually try to run our tests and it compiles and ran the test says it's okay so I guess we're good good to go now we I can make this a little bit larger now we want to build and so we build a release with the wasum 32 target this is still just plain rust plain cargo we haven't done anything special at all until this point so we build it it produces some was an output and then we do wasum and build so we have this little tool called Waze and build this is something that we built and this is where we get blockchain specifics so so far everything is just plain rust this takes the output the was an output from rust makes it in something that we can deploy on a blockchain so run that and we get yeah this is the ABI that's output by compiling the rust code it's just a standard API it looks like anything else that you would expect that you would get out of solidity or anything else and we can check a little deploy script here that I wrote it just uses plain web three Jas nothing special we set the provider which is a localhost said it's at an account that has some gas in its unlock the accounts pull in the ABI that I just showed pull in the data from the Waze 'm binary and deploy it and hopefully this should work so I have a parity node running here in one of my tabs and we can just run deploy and it's deployed because it's a local development network it just instantly deploys and instantly gets mind so we get an address for this contracts we can move over to the the parody UI look at the contract say we want to watch one paste in that address name it and we need the ABI so I'll just copy paste that I had contracts and you can see we have a contract on our local chain so it has these top donor and top two and total donations functions it shows an event list which is empty and let's let's donate some some money into it so here we we see the two the two other functions donate an oath draw will donate 1337 I feel very generous and unlock and it gets instantly mind again because I'm on their development network let's refresh the contract it shows the top donors each CC which is the accounts I was on and total donations we can flip the switch to show in eath and everything works as expected we have our events and what's important to note here is that this is just a completely plain standard pair DUI we haven't changed anything on the UI all your DAP should work the same way whether it's EVM or Watson VM and it's it's really straightforward and nothing special here so that's it for for the demo I want to say that this is ready for users to start playing and testing and making smart contract and rust or your own favorite language we think this is super cool we really like this but we can't build everything ourselves here we like really wants you guys to test this out to tell us what sucks to tell us what's great and just join us so all code is open source as usual it's available on github we're available to mentor a guide or just chat on riots we have a watercolor channel or a dedicated channel to watch them if you just want to follow that conversation this is a list of repos that might be relevant so parody wasum is the serialization and deserialization of blossom into rust we have the standard library for rust contracts for ethereum externs and we have this repo while the mutants which is like general azam utilities it's not really necessarily related to rust but if you really want to dig into this I'd say start with the tutorial it's a really great sort of step-by-step tutorial that walks through everything I just did here and finally I want to give a shout out to Nicolai Sergei and Alexei who are the phenomenal Russian trios actually made this possible Sergei sitting right over here so if you find him later please buy him a beer that's it for me so thank you very much so the question is if this is e azam or if it's compatible with it it's definitely compatible with it so in one of the awesome talks yesterday the guy said that you know he was and this just was them they literally don't make any change to it at all neither do we so he wasn't it's just was them and this is the same thing there may be some differences in this sort of validation layer that we do but if there are they should be trivial to eliminate and our goal is definitely to be compatible with everything else here why did we pick rusts it's a good question rust stands at a very unique point of being type safe safe against seg faults it has no undefined behavior it has no runtime so it doesn't do dark garbage collection doesn't do reference counting you get very small efficient code with it and we just kind of like it all right there's no further questions there's oh sorry what's the performance comparison to EVM well right now we actually haven't benchmarked all that much we know that we have some low-hanging fruit in terms of binary size it produces binaries that are quite much a lot larger than EVM right now but theoretically it should be much more efficient because we don't we're not restricted to dealing with 256 bit integers we have 64 32 bits and we can do a lot of other things there are a lot of like lower level instructions so theoretically it should be much more efficient but we haven't actually gotten to the point where we're optimizing and benchmarking comparing TVM what's the roadmap that we have in mind so it's getting deployed on Kovan we're hoping we can get a lot of good feedback from that people actually testing this stuff out and using it we have the sort of informal roadmap for writing rusts mark contract specifically we definitely want to not have to write like all the bits in a hash to do storage like we actually want to do variables and and have maps like you would have in solidity we also hope to maybe get down a line of being able to more clearly separate immutable and mutable code using the Russ type system and then obviously it's just sort of performance optimizations making things better on the wasum side the question is if we're we're afraid of consensus failures due to differing implementations of wasum we're not really at that stage yet where we're worrying much about that I mean we're we're deploying this to Kovan if you manage to break it we'll give you a couple thousand bucks and a bug bounty but there wasn't standard is actually quite set and because we're not actually modifying that standard in any way we're just removing things from it it should be relatively easy to agree on like what operators do we exclude and just have a set list of that that all VM implementations rely on so it really should be quite easy to make sure that all implementations are fully deterministic and behave the same way can you deploy a custom private test net yes definitely that's what I've done on my laptop here it's in the the tutorial that shows you exactly how to do that as well all right thank you very much for coming and listening [Applause]

Automatic transcript — names and jargon may be misspelled.