Javascript Smart Contracts on Bitcoin

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Looks pretty cool. Does a nice job explaining things.

Edit: The link on the last slide doesn't seem to go anywhere so maybe still in early stages --> http://www.bitcoincomputer.io

👍︎︎ 1 👤︎︎ u/mogray5 📅︎︎ Sep 24 2019 🗫︎ replies
Captions
well hello everybody thank you very much for coming I'm Clemens I'm here to talk about the topic that's you know very near and dear to my heart which is computation in Bitcoin it's a topic I've been working on for a pretty long time over three years at this point and so what fascinates me about this topic is that I think Bitcoin can be used for many things that's not being used for today right and in particular I mean actual full applications can be built directly in Bitcoin just like they're built on a theorem today and so I think that's actually kind of great news for everybody who's in Bitcoin and everybody who owns Bitcoin because I'm thinking this makes Bitcoin bit more useful and so very excited about this topic and I previously gave two talks I gave one talk about a year ago and in Tokyo called why bitcoin is turing-complete and what I showed there is basically that the data structures of Bitcoin are sufficiently powerful to simulate the run of a Turing machine right and that's sort of the first prerequisite we'd need if we ever wanted to do full computation on Bitcoin and then a couple of months later I gave a second talk in San Francisco called a smart contract engine for Bitcoin where I sort of made this approach a little bit more concretely and in that talk I explained how we can sort of reuse old ideas in Bitcoin and in the field of colored coins and just like upgrade these ideas slightly and exploit the techniques they're a little bit more to actually build a very powerful from our contracting engine on top of Bitcoin but that projects you know while I think you know many very interesting applications can be built that way it's it's a system that's not super easy to use so it's much more comparable to programming an assembly code and of course today everybody loves to program in these higher-level languages like you know JavaScript and Java and stuff like that and so that's what I've been working on for the past couple of months and so now I'm very happy to be able to present this to you I'm calling this thing the Bitcoin computer and what it allows you to do is to write smart contracts in JavaScript and run them on Bitcoin so let's jump right in overview about what's going to come I'm gonna talk for a little tiny bit about smart contracts and just you know explain what they are and where they from and then they you know the biggest part of the talk is gonna be about you know the Bitcoin computer is the software there that I'm building so I'm gonna explain what that is what it does and how to use it and you know finally I'm gonna wrap up so let's jump right in so it's the shortest possible history of smart contracts so you know we all know that you know Bitcoin got invented in 2009 and you know that was in my opinion really one of the major breakthroughs and computer science in the you know past decades really right because what it allows you to do is run one very important application which are payments run them in a way that doesn't sort of require one central point that everybody needs to trust right and that was something that had been tried in academia that they couldn't really pull it off and so Bitcoin was the first first system that was actually practical that scaled to a large number of users that allows them to send payments between one another you know without requiring trust in a third party and then in my opinion surprisingly early on as early as 2012 when you know the Bitcoin community was very very small in fact some some amazing work was done in my opinion in the in the area of colored coins and there were several you know teams competing on ideas like that and sort of the the general approach or general thinking was okay we have Bitcoin and Bitcoin it showed us how we can build you know payments in this decentralized setting can we use similar ideas to build more applications in just payments right and then sort of colored coins with the first example where a sort of Bitcoin was taken to not just to send money around but to build other things right and these other things were you know mostly tokens that can represent real what efforts and you can send them to your friends or you can collect them or you can trade them if you want to and so this is sort of the second layer here and my my little chart in terms of what you can do with script or writes or those areas we can do just payments middle layer means we can in addition to payments also do you know tokens pretty much and another two years goes by and eventually vitalik boot ream who actually came out of the colored coins project so he contributed to this project and had great ideas there two years later italic released his own cryptocurrency called a theorem and the theorem in terms of the application that I could build you know one went not just one layer up but really sort of to the maximum layer right so every single application that can be built on any computer can now be built on a theorem in a you know decentralized way which has you know many advantages as people know and you know so pretty much since then I don't think there's been a major breakthrough in terms of you know making smart contracts work better for cryptocurrencies so there's been a bunch mostly during the ICO craze of you know the last two years many other coins were launched and you know many of them also have you know churn compete smart contract capabilities but but mostly what they did is they just thing or anything to say they're strongly inspired by a theorem and and sort of you know it less of a breakthrough ideas and you know so but what didn't change at all is sort of the state of Bitcoin the applications you could build in Bitcoin right and so so Bitcoin in a theorem are quite quite different technically so you can't easily transfer the ideas from a thermal two-bit con that just doesn't work right but now I've been sort of you know I'm banging my head against the problem you know like I said for a long time I spend a lot of time university studying problems like this and so I think over the last few years I I got you know you know a few pretty good ideas for how how we can actually build smart contracts over clean Bitcoin and you know that's what I'm here to present today pretty much so what's the Bitcoin computer long story short so you know before I go ahead and explain what I mean by the become computer let's just look at you know what's what's even a computer right and sort of on the on the most abstract level computer has two components has a CPU and the memory right so you know if you look at you know poke about hardware as many other things in a computer but there really the purpose of all the other things is just to keep these these two things alive and working pretty much so what are they good for you know memory is you know just the storage device for data right where you can store your data here and when I say memory there's actually there's a big hierarchy in memory starting from no processor caches to you know hard drives I mean anything that service data here sort of in the second component is you know the CPU that's we're sort of the actual computation happens and or different way of putting it is is we're sort of the data gets updated ok and that's that's a very basic these are the two basic components then any computational system have right so I'm starting from Turing machines and all examples always there's always sort of storage and a way to update that thing and if you don't want to be completely abstract you also have to you know put a you know human in the loop there somewhere every what why have a computer if the humans can't learn anything from them and so so that's why no to two other important computer comes over computers of course the input and output devices and so so in general you know computation can be explained that as you know a user gives an inputs to a computer and then there's something crazy magical happening in here all those computation stuff the result of all this crazy you know magical stuff is that number one the the user gets an output so a user can answer ask a question and the computer can give the user an answer and and also you know the computation can have what's called side effects where sort of the memory changes at state ok so that's very basic computer and then it's actually kind of interesting if we compare this to Bitcoin and Bitcoin will also kind of have these two basic components okay so on the one hand we have you know the blockchain which is a you know data storage device and you know pretty pretty good one actually I'll talk about that and you have the miners whose job it is to update the blockchain okay so in a sense if you want you can view this as you know here's your memory component and here's your CPU component of the you know Bitcoin computer but unfortunately Bitcoin as a computer is quite restricted okay so you can I don't know if you can tell this from the slides these processors are they're really really small okay they they actually can't compute that much and and even more dramatically this output arrow is really I made it thin here because kind of the answers you get back from Bitcoin are are not not you know you know as good as the answers you would get from home computer so in particular really you can only do with Bitcoin as you can send transactions to it and know your computation component the miners will verify is that about a transaction if it's about a transactions gets added to the blockchain if it's not one it will just tell you no all right so there is only limited amount of information that you can get out of Bitcoin and you know in a sense the Bitcoin is is a little bit closer maybe to a storage device then to a full computer but then like I said a theorem got invented and you know on a very basic level you can think of a theorem you know it has a blockchain it has a kind of a similar data structure similar properties to the one in Bitcoin you know the technical level is a little bit different but taking a step back it's very similar but now we gave the miners much more power here ok so I drew them a bit bigger and so so these are now sort of full fetched CPUs that can perform arbitrary computation ok and so now this in terms of functionality works exactly like a normal computer right where a user can send in an input to the device and the device performs the computation updates the state on the blockchain and returns a result ok so my mission is here to you know I've said set myself up to building you know you know turning Bitcoin into something where you can do computation so the mission is to build something like a theorem from you know Bitcoin here but like I said you know problem is that sort of on the computation side your bitcoins a bit weak ish and you know there's nothing I can do about that really so I'm gonna need you know some some help I need another tool and you know the only tool that I can think of really it's just you know a good old computer ok so so now that we have all these things on the screen here let's just compare what's going on ok so computers have been around for a long time very popular devices can do amazing things and if you compare say ethereum 2 to the computer what you see is that the the computation the CPU component of a of a theorem is actually kind of like less efficient then then the one on on your good old laptop right so number one all computation in ethereum is replicated ten thousand times okay if you if you send the command to the theorem Network there's about ten thousand miners and all ten thousand miners will execute your program to you for you okay and they will all compute the same result because of the same program on the same inputs and then they will update the blockchain accordingly okay so just by having the computation run on ten thousand computers instead of just one means increases your cost by a factor of ten thousand but unfortunately even worse than that where these miners have to talk to one another right and and they have this sort of very you know complicated inefficient you know pretty much communication protocol between them which they need to have because of the Byzantine environment that they work in but still it's sort of that adds another you know factor of a hundred or a thousand and inefficient zero so overall you know you know these computers are like a million times more costly to use then then then just the processor and your hard drive ok so I mean that's not Shawn's you know disappointing so why is everybody so excited about crypto and the reason they're so excited is because this storage component of crypto is really very very novel and you know has very many good properties that just the normal hard disk doesn't have okay and so so the examples are if you update your normal hard disk your previous state is pretty much this lost forever right over very very hard to recover if you can recover it at all the blockchain just stores every previous state right so whenever you want to go back in time and then and see what happens you just sort of check into this log that's the blockchain figure out the previous state all right other things is that every single update on a blockchain is cryptographic key sign so you know exactly who performed which update and it's also time-stamped okay these are all features that are not not here and finally the the biggest advantage is that these block chains work really really well in a multi-user environment right and so that that's what why they were invented you wanted multiple people to come together and sort of create you know a shared history of computation of what happened and so blockchains work very well in multi-user environments but but even even more more impressively their work in multi-user environments where everybody hates everybody else and know everybody tries to cheat and so these are very very good devices for storing data and in particular in a you know Byzantine environment okay so coming back to my task here I got got a good old computers there they're amazing you know computing stuff less impressive at storing stuff and we got the block chains that that you know very very good at storing things you know if they're I'm bit couldn't pretty much alike if theorem also has you know DC you know very expensive but at least they can do computation Bitcoin doesn't even have computation okay and so you know once you have it on a slide like this you know it doesn't really seem that hard so let's remove this guy here so the basic idea is you know if if you know Bitcoin shines and storing data and the laptop on your on your desk the device you're looking at right now you know excels at computing stuff and every computer has these two components you know why not just combine these two okay and that's the Bitcoin computer okay it uses a normal computer the computer on your desktop to do computation and then it uses the big contraption to store things okay and you have to have them talk to one another so you remember in the and the old picture of computers I showed you there's the system bus which handles all the communication between the CPU and the memory and so so in the case of the Bitcoin computer we just make sure that we always keep the the data on the computer and sync with the data on the Bitcoin network okay and this is how we bind together the the blockchain is your computer on the screen okay and so so why why would we do this okay so so this you know you know it's it's a very good storage device but it's also you know pretty expensive to use you have to pay for every time you access the database but the the the reason the main reason for doing this is that the the properties you can unlock the properties the special properties that only blockchain have and make them available in a normal computing environment by coupling these components so closely so let me show you one example so this is an example of a single user system I just told you that you know Bitcoin works great in a multi-user environment so what happens if we have two users now and they both have their own computers this is the computer owned by this guy and this the computer by this guy what happens if they both synchronize their computers to the blockchain in that way okay so what happens is this guy can provide an input change the state on his local computer because his his state is completely sync to the state on the blockchain you know this change would propagate to the blockchain and now since you know the second guy here is also synchronized eruption his device will also update right and so if you abstract away all the details in the middle there so these two guys you don't you know have to be bothered with how it works inside so these two guys it really feels like they're just using one computer right and so so in particular these are not just you can't just do this for two people you can do this for four groups of people and eventually for everybody on the planet right and sort of the the promise of Bitcoin computer is that we can use the system and it works as if we were sitting all typing into the same laptop no which is you know great advantages in many many applications we mentioned some of them later but that's what the Bitcoin computer is it's the one global computer that's been running for 10 years and we can all use it together and we can use it for all sorts of business purposes games many different things but actually there's more advantages to this construction okay so I mentioned before that like you know these block chains have properties that that normal computers actually don't have right and then you know one of the most well known property is money okay you can use Bitcoin was created to send money around so so money is you know just pay into the system and by creating the Bitcoin computer we just sort of make make money available in in your normal computing environment right so you can now think of this as a computer that doesn't just store data and and you know process data or update data in addition it can also store money okay and that's a property that doesn't exist in computers today and that's what makes you know the Bitcoin computer a very special computer and very unique computer of course you can have you know money in in you know in current computing environments you can you know use a service like PayPal but money is just not baked into computation from the start and sort of by coupling the blockchain with normal computers and creating the Bitcoin computer we make money available you know easily available for use in computing environments and there's actually more features of the blockchain right so as soon as you have money you have to protect money somehow right so it's you know storage device for money where everybody can see that that doesn't make sense so we're designing of course the Bitcoin computer to store the money just as securely as your money is stored on the on the blockchain right and the reason it's it's stored as securely it's just because it's the exact same way you store money in Bitcoin right now is exactly the same way that the Bitcoin computer stores money but then the way that Bitcoin protects the money is that it ensures that only the owner of that money can send that money somewhere else language is a very sensible thing to do and so we have this notion of the owner of over of a coin and so we can very easily just make this notion of ownership also available in the Bitcoin computer so which is that the second additional property then we're sort of adding to normal computation which is this notion of ownership right and an owner can mean you have a user assigned and and only that user can send that money somewhere else but we're also extending ownership not just to money but also to data okay so in the Bitcoin computer we have a very strong notion of data ownership so if you own a piece of data you're the only one who can delete that piece of data you're the only one who can update that piece of data you're the only one who can modify that data you've complete control over their data nobody can take that away from you okay and so that's sort of the second component that the Bitcoin computer adds to normal computing environments okay and and that's pretty much it so this is to me an you know abstract if you no explanation for what the Bitcoin computer does has a component where does everything at normal computer stood where is it as memory and a CPU but it also has two additional features which are money and ownership okay and then okay there's even more features so of course every update is time-stamped and digitally designed but that's not the focus of our talk but then these are very useful features that will come in handy later okay so now let's make things bit more concrete how how are we going to use the Bitcoin computer and then you know as usually I like to you know just take experience per Asian from what exists so let's you know focus on normal computers at first so we need you know every computer needs some sort of develop environment development environment to make it sort of easily accessible so really you know computers are usually access to applications and we need some some sort of techniques to to build applications way to build applications and normal computer says you write a program in a programming language and we have many great programming languages so I just put it you know a couple of examples here and you know in addition to your programming language you need some sort of you know deployment infrastructure that you know make sure that your program is running on on your computer and then you know keeps running and all these kind of things so so what can we learn from this now that we want to build sort of a programming language and a deployment infrastructure for this you know slightly you know more complicated device here ok so first of all the first thing we learn is that the Bitcoin computer is not not an entirely new computer now entirely new invention it's partially if something that's old an existing in work very well understood which is computation on data and two additional things okay and so in my opinion there's no need for us at all to reinvent the wheel here wheel here you know create a new programming language we should you know just pick one of these programming language that people have put you know decades in a work and to make them you know really you know user friendly and and you know sort of nice nice things to use and you know we're just picking the most most popular programming language that there is today so the programming language that's used to program the Bitcoin computer it's mostly just JavaScript okay and so we're picking JavaScript for two reasons reason number one is according to you know certain studies lately it's the most popular programming language ever all right so apparently around like over 70% of programmers already today know how to use JavaScript and so if we're sort of launching a new smart contract platform now it seems very helpful to use a language that everybody already sort of you know likes to use and you know the advantage is there that you know companies don't need to hire new expensive smart contract developers and you know then then they'll have to train them and explain to them which smart contract right instead they can sort of have their existing employees who are already domain experts right right the smart contracts right and so so that seems you know much much more secure approach and you know the second reason is there's like you know a bunch of people who are very very experienced and in you know working with javascript for many many years and so the probability of of you know making a mistake in your code or introducing a bug or see its utility problem it's much much lower if people program in languages that they've used for years all right so these are all reasons we're using javascript and then there's another reason where javascript is maybe it's not the most efficient language and is probably not the prettiest language but there's one thing that distinguishes javascript is that it runs it's the only program language that runs directly in the web browser right and to me that's that's the difference not in in quantity and not in how fast it is it's a different in quality right it's something where javascript can do something completely different and so so that's the reason why I think javascript can get away with being a little bit you know more or maybe inefficient or awkward to use than other languages because it has this one superpower right and that's running in the web browser and we buy all means want the Bitcoin computer to connect as seamlessly as possible to the internet so that's why we're using javascript okay now that we have Java scripts that should sort of cover what we need to do on on you know just in terms of tradition and computation so now we we just gotta like take care of these two things that we've added to our computing environment money and ownership how to deal with them okay so what do you mean what's what's money its what do you want to do with money in a computing environment so you know really you know at the first point you want to store it somewhere right and then if you look at JavaScript Java scripts and object oriented programming language so that means is sort of the basic entity that that people use our objects which are sort of collections of data and then every object has coupled with it you know a couple of functions that tell you how to update that data yeah which is by the way very close to the distinction between no state and updates again okay but so we have money without a store somewhere the only thing to store stuff in object-oriented programming languages are in the the properties of an object so you know how about we just say every object in our sort of in our smart contracting language can store an amount of money okay so it's just to me very very simple app so explanation for how that works and syntactically what we do is we just sort of add one extra property which pretty much acts like a keyword inside the Bitcoin computer it's called the mount and you can set it to a number and and sort of the meaning of that of that keyword is if you set the amount of an object to a certain number and you deploy that object to the blockchain you know that object will store that amount of Bitcoin okay and of course you can only do that if you own that amount of money but if you do have enough Bitcoin you can just create a object on the blockchain store money in it very easy okay now what's ownership what does ownership mean ownership is like it you know an owner is a person who owns something now you just talked about things to own in object-oriented languages probably like an object okay and so what we just do is we just add a second property to every object it's called underscore on owner and you know you can set it to a public key or list of public keys that specify who is the owner of that of that object okay and so so like I said before the owner of the object the meaning is if you own a piece of date it's like your private property nobody can change your private property if you own a piece of data you're the only person who's allowed to change that thing and nobody can take that away from you okay and you know in a programming environment you just said you know this property of an object to a certain public key deployed to the Bitcoin computer and that will ensure that only the owner has you know permission to update that object okay so and that's all that we need in terms of programming language so you know if you're developer there's a 73% chance that you know now how to program the Bitcoin computer right because all you need to know is just JavaScript we can get our head wrap your head around you know to the properties you know one set to a number and one set to a public key so what do we need in terms of deployment infrastructure right you know turns out also not that much what do you want to do with these objects you want to like you know you want to somehow store them on the blockchain why do you want to or you want to somehow get them into the Bitcoin computer in the first place so so that's where sort of the key word you know computer dot new comes in it's very similar to the to the JavaScript keyword new you know the JavaScript a word new creates a new instance of a class on your local hard drive computer a new creates a new instance of a class on the Bitcoin computer okay very easy use computer is is just the NPM package step that we expose so you can download the computer class from from NPM and create a new instance and then your instance will expose the new function and the second second function has to do with the fact that that were we're trying to make it easy to build these multi user experiences so they kind of work together and the idea is computer out new we'll create a new instance that's stored on the Bitcoin computer and then if a second person is also interested in the same object one so you know call a function for example there they call computer let's sync on that object which sort of creates them you know a local instance of that same file okay so this is sort of creating a new object on the Bitcoin computer and this is to to connect with an existing object okay and then in terms of if you if you're interested in you know learning how to use the Bitcoin computer this is what you need to need to learn okay there's no new language involved there yeah I mean you probably know JavaScript you got to wrap your head around these two properties amount sir as a number which you know specifies an amount of Bitcoin owner stores a pub key which you know you know designates a user of an object and you got something to deploy a smart contract and something to connect an existing smart contract so it's really I think very easy to learn okay so the design process is sort of was was guided by two principles if you want you can call them the golden rules of become computer and I've mentioned them already the first one is is no very important if if you own a piece of data on the Bitcoin computer you're the only person who can ever update that data okay and that is guaranteed you know you're on very you know large amount of force whoever wants to try to take your data away from you it's gonna be very hard if you store it on the Bitcoin computer right and and the reason is that we store data on the Bitcoin computer just using the same techniques and just as a clearly as you know the the Bitcoin blockchain stores money okay and so so that's that's you know maybe you know it's it's an important goal but it's also something that that that is so fundamental to us that it's not even really part of the thing that we're building so this is just guaranteed by the Bitcoin miners right so that's a very very important property and that will always be true and you know the second property just pretty much says that you know what we're building here is a good multi-user environment okay so we talked about the fact that like you know multiple users can somehow connect to the same object that's stored on the blockchain and you know the second golden rule says that all users must have consensus over the current value right and just that just means if you know Joe from Japan looks at an object and and you know who knows sue from Chile looks at the object at every point in time they're seeing the exactly same value there okay and and that that's pretty much exactly what creates the illusion of two people who sit on opposite ends of the planet that they're actually using the same computer okay so let's make this bit more concrete and look at some examples so here's the first example of a smart contract it's a counter class and it implements a counter a counter is a device that can store a number and you can increase that number so the way you implement that in JavaScript is you declare your class counter it stores one property I which is you know of tech numbers or as technically you know flow or typescript whatever you have a constructor to create in your object that constructor initializes the variable I property I to zero you know you got your increase function that increases the value that's stored okay so here's your first smart contract on the Bitcoin computer and as you can see that it's completely vanilla JavaScript class okay and so so this is just sort of the side effect of what we said before like the Bitcoin computer is not some completely new you know computing environment it's just like something existing storage and and you know CPU was two added things so every program that should work on you know the simplified version of a computer which is a normal computer should also just run on the Bitcoin computer right and so in particular it's going to be one of our goals that every single JavaScript program can run without modification on the Bitcoin computer okay and and the advantage there being is that existing code that has already been developed has been in production for years has been tested and you know shown to be bug free you know we want that kind of code to be able to be easily deployed on the on the Bitcoin computer right because this is highly trusted code and again it just you know decreases development costs by a very large factor and mostly development risks right because now we're not building something completed for in a new and different we're in an environment where where everything seems familiar and we can use the own tools that sort of the tools that we've developed for a long time and and just deploy them somewhere else so that's your first smart contract right there let's just see what it does first quick reminder you know what does the class actually like to honor on a just on a normal laptop computer for example so here is your laptop computer you got the CPU and the memory to run a program you know number one you have to somehow have the program on your hard drive so you know today usually this downloaded by the internet there's different ways as well just going to assume you have your program on the on your local computer and here's are just a little tiny remark on my visual notation I'm gonna stop drawing the CPU here right just because it's bit distracting okay but but you should still think of you know this box being an entire computer consisting of you know memory and a CPU okay so now now we have our our program stored in the memory of our computer now the user can call the constructor of this program and so it calls new counter and what it does is it creates a new instance you know in the in the local memory of that computer and and you know initializes the the instance you know sets I to the or as sort of specified in the constructor okay and then the other thing you can do with the program on a normal computer is you can call a function so in this case there was just one function this Inc function so assume the user cause the Inc function and you know what what happens is this you know this instance that we just created from from the class sort of has their property I increased from 0 to 1 no big surprise here so how did how does the same thing work on on the Bitcoin computer right so so again I assume that we have somehow gotten our the the program that we're interested in we've somehow gotten that into our local hard drive ok and now what the user to deploy or and create in an instance and deploy to the Bitcoin computer I told you before what he needs to do is instead of calling new new counter is going to call computer new counter ok which is this keyword that I mentioned before that allows you to sort of create an instance deploy to the off chain so let's see how that works in detail when you call computer out new counter the first step is that an instance is just created on your local hard drive ok just like in the previous case but now the difference is is that that that same object together with the the program that created this object just get stored on the blockchain ok it's just a one-to-one copy alright and that's why we say these two things are now in sync because both store exactly the same state ok but then if you deploy something to develop chain as another nice side effect due to the fact that you know the blockchain is you know sort of has this immutable log we can create an ID for this object which is just the location on the blockchain word you know this information was for a stored and this you know you know makes it very easy to just assign an ID to every object that's stored on the Bitcoin computer right and having these ideas IDs around is also something it's very convenient for programming has been studied in the past and for example in the context of object-oriented databases but it makes for a computing environment where object have unique identities and they behave much more similar to objects in the real world right so you you know the way the Bitcoin computer will work is you know you can't just easily make a copy and nobody notices right that's exactly the strengths of Bitcoin right and so in a sense data stored on a blockchain it's much closer to our normal perception of just you know physical objects pretty much and so that's that's what's going to make you know using the Bitcoin computer just an interesting experience because you really have the feeling that you're shoving shoving you know objects around and then moving from here to here and stuff like this ok so now we got our object and deployed that you know idea signed so but I you know I kept going on about this multi user experience so I I'm sure how that works oh no no before I do that so we've now sort of deployed this object but now we also want to like somehow update this object right and so you know I told you before the way in an object-oriented language the way you you sort of you change your object as you call a function okay and the the design goal is to make the Bitcoin computer work as similar as possible to just the normal computing environment that you you're used to okay so in the previous step when you call computer new what's returned from the call isn't is an instance of the object but it's you know it's what we called a smart object that that knows that it synchronizes the blockchain and kind of knows what to do however all that crumbs and complexities is completely hidden from the programmer and from the outside that object looks exactly the same way like the T counter object would have looked right so it has the exact same functions the same functions have the same input and output behavior so now to use my smart object I can just call the same functions that I would have called on the object that I just created for my local machine okay and you know one that when that happens in step one the counter on the local machine sort of gets increased from value zero to value one but since this is also synced on the blockchain yeah this update is propagated to the blockchain and now the blockchain also reflects the fact that the counter is now at at you know number one but here we're seeing you know an another cool property of the blockchain this is the effect that you know the blockchain logs every previous state right so so we have you know one one piece of data here on the blockchain that represents the current state where the counter is at one but we also have another location on the blockchain that throws the counter being at zero okay and so so this this history of all previous states is insanely useful for many applications like starting from no container shipping where you want to know where your containers have been previously to patient records where you want to know which doctors treated these patients and easily and all that that comes you know very naturally out of the box with the beacon computer and they really just come out of be competing ok so now what we wanted to talk about multi-user things so I see them there's you know a second user over here and he also has its own you know local computer to to participate on an object that's deployed on the blockchain all or you know you know use that object I might say all that this user needs is this ID of the location where the object was first deployed right or just the object ID ok he doesn't even this guy doesn't even need the the actual program all right all that he needs to to you know deal with that object is you know this ID and and really doesn't need the program because you can look up the program because it stored right there on the box chain so say you know this user created the object maybe he'll send you know the ID to this guy by email you know the I be can be ID can be made available on a website so there's many different ways to distributing these ideas but you know as soon as this guy now has the ID if he wants to sort of get its own local copy of that object what he calls is that the second key word that I mentioned before not sync and he passes in that ID and then you know the Bitcoin computer understands he's gonna kind of find that location on the blockchain that corresponds to this ID and just gonna download you know whatever stored right here so he's gonna download you know the the source code of the program and and the state of the program when it was first deployed right which is an instance of the counter object was I equal to zero but the system will also detect that this is not the latest state of the system right because these these two sort of you know versions of the object pretty much are you know tightly connected on the blockchain to his spending relations so it's very easy for the system to determine not only that this is out of date now but also sort of what's the more recent state okay and so the system sort of walks through sort of the entire history of that object until it finds sort of the most recent version and you know while it walks down this history it keeps continuously updating the local copy here until eventually isn't isn't sync okay and now it's probably hard to read but now all three of these instances are all in the same state were were I said to one okay and now so we've seen before an example where this guy calls a function on this object but now this guy has exactly the same copy so he too can call a function on the same object so he can call the ink function what happens is it increases the you know the instance on this local computer to to okay then it increases the blockchain current state on the blockchain is is set to two and it both previous states are recorded and in the third step since this guy's also synched the counter on his local machine also updated to all right and sort of then that's what creates this illusion where you know one user caused a function and another computer on the other opposite end of the world potentially updates almost in real time okay the synchronization is gonna take you know under a few seconds okay so it really very strongly sort of creates this impression that everybody is using the one shared device okay so so you know that's that's what we got here but then so this is a system that works well but like to make get more practical we have to take into consideration that line block chains aren't free to use okay so every time you you store a piece of data on the blockchain you have to pay and the price that you pay depends on the size of the data so we really want to you know practically store as little information as we must however we have to store sufficient amounts of information to ensure that like our second rule is still true and that like from the information that's stored on the blockchain both users must be able to to sort of gain consensus of the state right but here I think we're being a bit bit too verbose so the first thing that you notice is that we're now storing the source code three times right in each of these three locations and that's you know just really completely redundant because from this location can easily find this location so we don't we don't have to store the program and these in and these two two cases down there then the second program as the problem is when you when the second guy joined recall that he had to sort of sync sort of redo the entire computation of the system from from the point where the object was first deployed sort of to the current state okay and so what we're doing here is we're storing all all these intermediate state and so so what the system would need to do is you got a look at the state before in the state afterwards and you have to verify like in colored points that this is a legal update and so in particular you have to verify that this is an update that somehow been induced by this by this you know class here okay that sounds you know a bit hard already so the problem is you know this class might have many many many many functions in general and number one you have to find the the function that sort of might take this state to this state but if the functions take parameters as well prominent it's way harder right because technically you have to guess these parameters as well and I don't even know if that's possible and if it is possible it takes like a lot of work and is very complicated I don't know do any of these things luckily you can escape that problem fairly easily so what what you have to do is in addition to the state you just store the function that was called in addition to the the parameters that were passed into this function right and then if you have it like this and you know that the state before was you know the counter was at zero the state after is the counter is at 1 and I know that you know exactly which function was called the increment function and I know exactly which parameters well in this case none okay then you know to verify that this was a legal update all that I really have to do is you know evaluate this the Inc function in the context of the previous state and you know should spit out the state after and if it didn't you know someone's trying to cheat me and I should should like not not accept that computation or not not not interact with that person but now sort of you know that that sounds fine and that works too but but now again it almost looks a little bit redundant right because why do we even need to store the state right if we're gonna go ahead and like you know evaluate all these functions anyway right we we don't need to store the state we can just have the state be defined by the state here is whatever state you get when you evaluate you know this the sequence of functions okay and so we can make the system actually quite a bit more efficient by just storing these these function calls here ok and so so should think of it this is a very very very efficient encoding actually right because again if you compare this with ethereum what happened here okay a user called a function some computation happened the entire computation happened on his local computer that is like I told you a million times more efficient than any con you know computation then in a block chain would be and and all that got recorded here is just the name of the function that's called and the parameters to that function okay and so in particular the price you pay for that update it's just sort of the size of you know the function name and the parameter so it's just the size of the inputs and the interesting thing is what it's independent of okay number one it's independent of the size of the state right because the state is nowhere stored we're just storing function calls and you know that you know potentially big state is just created on the local machines yeah which you know makes it the cost of running a function is independent from the amount of state you need do you know to store your objects which is amazing which is you know amazing amounts of money you know potential for from money being saved and you know this the second thing that is independent off is you know the computational cost right so just like how many computation steps does it cost right so I told you before in the theorem what happens if you call a function 10000 miners in parallel evaluate every single step of that function right and and hey you don't have to do that you only pay for you know the size of the name and and you know the reason you can do that is because you just performed the function on your computer right in that that almost sounds too good to be true it's a smart contract solution where where you have a fixed cost that's very low in many examples and and you can evaluate an arbitrarily complex function with it so why can we get away with something like that ok and the reason that we can get away is our end goal is all users have consensus over the current value right at the end of the day that's what the users in a theorem care about and that's what they should care about and not they don't care what what the miners think the value is is that the two participants must know right and to guarantee that we just have to make sure that the enough information is stored on the blockchain to be able to recompute that state okay and sort of by by using Bitcoin as a pretty much as a dumb storage device you store all the information that's necessary to sort of recompute that state right and it's really not necessary to have someone else we compute that for you if you can just do it yourself so so this is a system that's that's you know in in in some applications gonna be much much much more cheap to use than then-current smart contract solutions okay and so so this is just a picture that shows you we don't actually have to store that information that captures the current state here if we sort of can completely recompute it okay so you get consensus by either having one location where you guaranteed then that's the state of the world and just read the state of the world but it's also you can also get consensus by just sort of getting consensus on how we arrived at that state and being able to replay that on your local machine okay it's exactly the same guarantees you know just you know cheaper okay so what can we build this way so I just showed you how you know you build a counter and you sort of have this shared state which is just an integer and you know both users can update that state if they want and and that's actually you sort of I'm so actually speaking is an example for many many different things so think of a chat right how would you build a chat program is very similar to there just instead of storing a number you're going to store an area of strings every string represents you know one post to the chat and you just write it very very simple you know JavaScript program that you know it stores an array and it has one function called post and you can post a new string there adds it to the array everybody who sort of finds the route ID of that chat can compute you know every single message that has ever been called there okay or not more examples will be you know any sorts of public lists you know lists says have been popular and encrypted for a brief period of time and but also other things that you know message boards and image boards and sort of in general anything where we where anybody can change the state right all these applications can be built like that okay but there's other examples of application that we really really like to build that we can't build with the techniques that I showed you right now and so in particular you know tokens can't be built like that because like I said anybody can update the state and if anybody can update the state I'll just you know reassigned all tokens to myself and that's not that's not gonna work well and then and then games you also need restrictions for example you need to restrict that like if it's not your turn you can't move right and from all these techniques that I showed you so far there's no way to restrict something like that okay so problem is that everybody can update the Bitcoin computer whenever they want no solutions lucky very easy and in fact it's something that actually the Bitcoin computer excels at right because restricting access is something that's hyper important if you want to do payments right you got to make sure that only the owner of the money can send it the money somewhere else and so we'll just use the same exact techniques to restrict access to objects right to ensure that only the player whose turn it is and change the state of the board for example so let's see how that works so here's an example of a blog okay it contains an area of strings which will sort of store every single blog entry and now here here we're seeing you know what you know one of these two keywords that I mentioned previously right we're taking JavaScript adding you know two new properties one is underscore owner or no to underscore amount there's an example that shows you how underscore owner use okay so underscore owner must be set to a public key otherwise it doesn't work and the convention is that only the user who has the private key for that public key is allowed to update that object okay that's guaranteed by the system itself and doesn't have to be specified by by the programmer so if you want to want to set an owner of an object all you need to do is you just have to assign a public key to that property right and so here we see an example in the in the constructor of the blog object you pass in a pub he called owner you set the owner to that owner and that just means I've just created an object and nobody other than the person who who owns you know the private key for that public key can update that object and you know the second property that we set in constructors we were setting you know other you know blog entries to the empty list nobody's blog so far and you know we have one one function it says you know post a new blog okay and all all that it does is you know any this is exactly the code that every JavaScript developer would naturally write if they were to implement a blog on their normal computer right so you to post to a blog you just you pass in the new blog entry and you just you know push that blog entry to your your every array of of previous blog entries right so that's a very easy way to do to post to a blog and then we have a second function here okay so in the in this function it's actually interesting to note so I told the host or the important thing about this example is that only the owner of the blog can post information there right I don't want other people to post on my blog I want to be the only person wants to post there but in this code we don't have to write anything like you know if you know caller is the owner then execute this call system guarantees that already right the system will always sort of know who is calling this function because calling a function involves signing a message and if the if the person who who calls the function is not the person sort of specified as the owner and we just do nothing we'll just ignore the function call right so very even syntactically very easily can you enforce these these ownership restrictions right and then and then we have a second function here called change owner the purpose of that is you know I'm surprised you need to change the owner and it shows you if I want to like you know give my blog to someone else eventually maybe I get tired of it I want to give it to someone else I can just very simply call a function here pass in a new owner right represented by their public key and just reassign this own property and sort of like here only the owner current owner can change the object and call this function okay but then if you want to it's very easy right the owner just passes in you know here's the new owner change your variable at that point the you know the old owner cannot update the blog anymore only the new new one cat so let's just walk through this as an example real quick so in the first step here we've got user an user B user a calls computer dot new a passes in the class that we're interested in blog but he also passes in the parameters to the constructor okay and so the parameter the constructor is sort of the owner of that object so he passes in his own public key there okay what happens is what I explained to you before you know the source code of the program gets too close to the blockchain and a representation of this this constructor call is stored on the blockchain so that everybody can easily compute the state from there and now you know the owner can call the post function and you know post a new message to their blog right and again everybody can read this data everybody can determine you know what's what's the latest blog post and in this blog okay and so say now you know user B he's the bad guy here he tries to post something on to users Hayes blog you know despite the fact that he's not supposed to okay and he's not supposed to because in the constructor call the the owner was set to user a and we you know we had this little the circle here to make make it clear that you know these classes or these objects are owned by user a now what happens if user B just tries to call a function on that object you know it just system just rejects it okay nothing happens so so there's an and again this is the security provided by the miners and you know by the blockchain nobody can can change the state of an object other than the owner okay but then if you know if user a once user V to be you know he wants to allow him to post something he just calls he change owner function passes and it was Abby's public key this resets the owner of this object and now sort of user beacon post to the same same blog okay and so so here here's another example that you just used the same technique so so these techniques that I've showed you so far there's many many many examples so let's just look at a game real quick okay so this is the tic-tac-toe game and you know I started by just downloading you know JavaScript implementation of Bitcoin I'm sorry of tic-tac-toe from github pretty much any open-source implementation is going to work right because the smart contract that I have to write here is extremely similar to the two the two just a normal code that you would write if you write code for your local machine so what are the differences here you know the tic-tac-toe game of course must store you know the current state of the board but in in addition to that as also stores you know the two players okay and so so we have one property called you know that that keyword owner property that's the pub key and and we also you know have one other property that can also sort of store a pub key and now let's look at the constructor of the Samburu quick so into the constructor of course the constructor will create sort of an empty board but you also pass in two public keys representing the two players called player one player two what we do is we set the owner to player one which just means is you know fair ones now the owner he's the only one who you can update that object which is just a different way of saying it's his turn right so if it's your turn in a game you have the power to change the state of the game and so if you want like want to have this first term we have to set the the owner to pair one and we just store you know player trees probably and the other property and other than that it's a completely normal constructor and does nothing else actually program continues over here we have tumor functions move and is one is one sort of checks the winning condition you know is did one of the pair's win the game it's gonna return either that player or null if the game sort of still in progress that function is completely unchanged that's exactly what I downloaded from github there's nothing to change here the mole function is is you know mostly the same one little difference so the way you update your board you know works you know in the smart contract exactly like a would on a normal computer however since we're dealing with owners we have to you know take care of that and particularly we have to make make here you know make sure that you know if if I made a move then then you know I have to make sure that it's now the other other guys turns and they can move so we gotta reassign the owners here so the way it works as little tiny bit of logic here is first you check is the game one and if the game is one then you set the owner to be the winner of the game because maybe you know the winner wants to use that to claim some reward who knows okay like otherwise if nobody has won you just swap the two players very much right you you just say ok you're not the owner anymore you're now the other player and the other player or not becomes the owner which is just a different way of saying no it's it's the other guys turn right now okay and so so this is an extremely simple implementation or a simple way to turn an existing game into a smart contract that can run trust its Leon a blockchain on this global computer that everybody even can use even if they don't trust each other and so if you can do tic-tac-toe you can do every single game like that okay so every single you know two or a multiplayer game that's at least like turn-based game where there's just one one one guy's turn every time can be built using exactly that technique and as I showed you it's very very simple right you just take your normal game implantation you add these two players here that are passed the players into the constructor and at the end of the move you have to make sure that like the the player whose turn it is is then you then you owner okay a very very large sort of class of examples that that can be built like this so now we can build all these grid like every single game you can think of and especially games will take advantage of the fact that computations no free so you can do you know you know games was you know great graphics and you know great huge landscapes to explore and you can all build them as part of a smart conflict without without having to pay a lot of money so so games is going to be one great applications you can build tokens like that and you know many many many different applications however there's still stuff that that we can't build and like an example would be in exchange or something right so maybe some sort of decentralized token exchange or anything like that and really you know what's missing there at this point you can probably guess it it's you know we need money okay and you can also probably guess that you know solution is not that hard because the Bitcoin computer has money built in from the start so we'll just use you know big kind of money and so so let's think of how we can turn this blog example so in the blog example we have this change owner function here which allows you know the current owner of the blog to reassign in it sort of reassigned ownership to someone else and so say so this is pretty much just like giving away your blog for free so what how would we do it if we wanted to sell the blog okay so to sell the blog first of all we need some sort of way to store money in the system right and so so this is where we have this class you know money here has two properties an amount which stores a you know number of Satoshi's and and an owner which is just a public key and you know you have the travel constructor here that just sets the owner and the amount and you can even forget the same function please don't look at the same function okay as a bug and now so now we have you know one class and you can create instances of that class and you know that instance will represent an amount of money okay and like I said before you have to have enough Bitcoin if you're going to create a an object that stores hundred bitcoins you have to have a hundred bit code right otherwise the system will say sorry you can't you know insufficient funds or whatever right but if you have enough Bitcoin then you can just create any amount of Bitcoin just storage in object wrapped into a normal JavaScript object store it on the Bitcoin computer right and then if you want to buy stuff you know in the Bitcoin computer world that's how you get your money in there right you create an object and you just store the amount of money that you want and now if I wanted to now buy this blog here we have to change you know the the change owner function love it so I change it to a function called sell of course that has to be done before the smart contract is deployed once it's deployed you can't change the code but you could you know now create this you know sellable blog for example and it's again it's a very simple function if you read it any JavaScript developer will immediately understand what it means okay so it's a function you pass in a money object okay and now we've got some some fancy JavaScript syntax going on here but really what it does is it says you know make who was previously the owner of the money make him the owner of the blog right and you know make the guy used to own the blog now own the money so it's just like you know one guy got to block the other guy got the money and you just pass in this object here as a parameter okay and so again that sounds almost too good to be true so how can that work and how can that be secure and that just has to do with you know how the you know Bitcoin computers implemented and essentially with our two golden rules okay so golden rule number one said if you own an object your agreement is needed when that object is updated okay so let's look at which objects change here we're setting the owner of the blog to the owner of the money so clearly the blog instance changes and to execute that function will require agreement of the owner of the blog and you know in practice how that works as he signs you know a message in a Bitcoin transaction we says okay I see you know the function was called and you know this is the object that got passed in and I agree to this update so I'm gonna sign the message okay and then the second object the second object that's changed is we're passing in this object here by reference okay and so so we're passing the object by reference and we're changing that object that was passed in okay and again think of like a physical object being moved from one location into that function okay and now that function changes that object and by the first Golden Rule the owner of that object must agree to that function okay so the way that this would you know get implemented is this call is recorded in one transaction with two inputs and one output and one assigned by the owner of the blog one assigned by the owner of the money it's only you know you can only sort of use that update of post people sign it and if they sign it it's sort of you know their mutual agreement to the smart contract I want to sell you my blog for this amount of money and the guy was a blog knows how much money it is then one was the money knows which blog it is and it's recorded in one transaction one atomic operation where there's no risk where one guy sends the money first and the other one doesn't send the blog okay so so that's just another example of you know some very powerful thing that can be done out of the you know the few features that we've introduced today okay so so that brings me to the end of my talk so what I've presented is you know a novel approach to smart contracts and it's not like you know this is way better than everything that has been done before but I think it's interesting and I think it's different and you know my prediction would be that that there's going to be applications that are where you know a theorem is the go-to tool right and people should build these applications in a theorem but you know the two approaches are quite different so that there will also be other applications where Bitcoin computers you know the more efficient tool to use and so I'm thinking these are sort of you know - pretty complementary products and it's just good to have like you know this new new tool in our Arsenal right and like I said if you're some whatever nostalgic about Bitcoin you can also just be happy that works on Bitcoin okay so besides Bitcoin sort of you know my my whole explanation was very abstract I make very few assumptions about the underlying blockchain so when I say Bitcoin in particular I don't mean any cific bitcoin i mene mene pretty much any bitcoin like currency which you know the ones we're currently supporting are you know you know the BTC bch litecoin and bitcoin sv and hoping to add support for dogecoin soon that's cool so but but then even beyond that sort of the general technique would work for many many different and pretty much just every single blockchain to be honest right and so there's lots of flexibility in terms of the blockchain you want to target you also have locked so lots of flexibility for the programming language that you want to use we're currently sort of building the prototype in JavaScript but the general technique works in every object-oriented programming language again because I'm not exploiting anything that's very particular to JavaScript just talking about like very you know core features that are available in every object-oriented language so the approach works was you know you know many existing programming language and and this is one of the properties that I almost like most it's very easy to learn right and the reason it's really easy to learn is because we have this clear dissection is this is you know the computer that's always been there and these are the two isolated things that we're adding to them right and that approach allows us to use you know very well understood programming language like JavaScript and just adding two very simple features to them all right which make it makes it very easy to use very easy to learn fun experience hopefully and you know also highly secure so thank you very much that's wraps up my talk please go and check out you know www a bit con computer at i/o future free to reach out we're on telegram I'm available email thank you very much
Info
Channel: Clemens Ley
Views: 2,011
Rating: undefined out of 5
Keywords:
Id: CN8NfMBTx0U
Channel Id: undefined
Length: 68min 11sec (4091 seconds)
Published: Fri Sep 20 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.