Counterfactual: State Channels Made Simple
EDCON 2019 · April 2019 · 14:51
Liam Horne introduces Counterfactual as open-source infrastructure for generalized state channels. Using payment and game examples, he explains signed off-chain updates, counterfactual contract execution and on-chain dispute resolution, then connects the contracts, protocol, node software and developer APIs needed to build instant applications.
Speakers
Topics
From EDCON 2019, hosted by EDCON.
Transcript
all right everyone so next up we have the co-founder of elf for Liam horn he's gonna be telling us about an awesome awesome layer to technology state channels but particularly what they call counterfactual so welcome Liam Claire come on now all right um hey that is that TV show the slides are now it should okay that's fine I'll just look after all right so um yeah I'm gonna talk about counterfactual I only have 15 minutes so this is gonna be like a super streamlined Li 5 just get the basics across talk but the goal I suppose have a clicker as well probably this right the goal is to just explain how how it all works and why
state channels are interesting to build on top of so quick note my name is Liam Horne I'm the co-founder of a company called l4 we do all kinds of things in this ecosystem we've been doing a lot of research on state channels as I'm about to talk about we've also started an organization called efj global which runs hackathons all across the world I'm we've you know in various ways been involved in different projects in this space trying to help push forward this web 3 vision however as I'm alluding to I'm also a core developer on the counterfactual project which I'm about to introduce to you as a separate standalone open-source project that we've spearheaded outside of our company so what am I going to talk about I'm gonna explain first of all what generalised eight channels are because
in my conversations with a lot of you over the past little while there's all kinds of little nuances that are not quite gotten so I'm going to show them almost dead simple explanation explain what counterfactual is as a means of making it real today and then describe why this stuff is pretty cool in general and again I 15 minutes so it'll be pretty quick so the backgrounds so you've probably familiar with this concept of a payment channel which I'm going to show kind of pictorially what that looks like and I'm gonna show you what a generalised a channel is from this basic primitive so you've all you've all seen this a bazillion times presumably a payment channel is something where you have a blockchain and what you do is you have some number of people let's say to that deposit some amount of state at which in aetherium
could be eath or a token let's just say eath into a contract and they record how much they initially put in a an index 0 bound speed index 0 and over time they message each other by signing messages with both of them signing every message saying what the balances are at some other incremental update so the balance of a and B at index 1 and index 2 and an x3 and the idea is what they're doing is they're sending money back and forth to each other by signing updates of what the latest state is between them and then whenever they're done they're finished doing this off chain they go back to the contract and they put the last signed update the enth 1 and the contract and pays them out accordingly so there's a
pretty standard concept this is what a payment channel is is how lightning works and this is that the basic idea a state channel in the basic way that you look at it is very very similar it's the same setup you have a blockchain you have some deposit and you have two people messaging sending back and forth but the difference is instead of just signing a Balby bail you're actually signing arbitrary state updates so it could be a tic-tac-toe board where you're signing the board with X here and then the board with oh here and then the board with X here and so on and so forth and the difference is that the it's the same idea you're assigning state updates but instead of a bell B bell at state but the contract on chain has some logic that says whatever the last signed update is run this function and figure
out who got the money so tic-tac-toe it says figure out if there's a cross or you know diagonal or up and down whatever the whatever the line of sequential X rows is and figure who the winner is and send that person money there's a little bit of extra logic that's sort of state channel is a generalized state channel is a little bit more interesting it's basically saying we have this ability to go off chain and sign updates of things what if we would look maximally generalize that so it's not just a bytes array or not just a Balby bell but something more complicated and what if we could sign it in different orders to pertain to like a representation of something different than just one app specifically so this is what we do in a general eye State Channel we start with a basic set up
Alice deposits Bob deposits but instead as time goes on we sign updates that let you install entirely new applications with some amount of the deposit you put into the into the contract so in this case you might say we put five in Alice had put five Bob put in five and we want to play a tic-tac-toe but also we wanted to play chess at the same time with the same money so five maybe like maybe two and a half goes into take that oh two and a half goes in the chess and you split the money in multiple applications that can be played in parallel that's the basic idea is instead of just like fixating your contract to one application allow you to reuse the collateral the deposit you put in the contract for multiple
applications and that's that's really it that's that's a general I say channel and of course the same idea applies if you ever want to get your money back out you can just send the money back out from the contract to your regular account but the core idea is that in the off chain world we want to maximize the amount of potential things you can do at the same time such that you're not limited by any unnecessary constraints so that's the bets basically it whenever we say all this crazy stuff about generalized eight channels it's really just describing the idea of reusing your deposit from multiple applications so if you didn't quite get all the details of that I recommend you watch this video l for adventure slash layer two it's a talk from Def Con last year which
describes how this is a really useful technique and it also compares a two plasma in a very nice visual you know not too technical way so I recommend watching this if you haven't got the full depths of plasma burst a channels yet so great so I just told you what general I say channel is what is counter factual as I mentioned earlier it's an open source project which is in a very very critical point we're not a company that has you know released a token we're not a company with a business model trying to sell you our product we're literally just a handful of developers passionate about this technique and think that it at scale and in production can lead to decentralized applications being much more usable and much more user-friendly for everyday people to use which hopefully if that's the case then
this industry can thrive so we're just trying to build this project it's like any other initiative of a group of people we have a vision and a mission our vision is the future we're DAPs don't look any different than regular apps you go to our website use the application and it should be just as fast and easy to use as any centralized application and the mission of the of the project especially they build all the tools that you need to make this a reality so it's pretty straightforward so so what is kind of factual what have we done so far again I want to get across the basic idea of the project and how you can basically map your mind around it so I'm going to go through all the components and the way to look at it is that we're doing three different layers of software and it looks like
this the first is we're building a very easy to understand application library for developers of decentralized applications to use to take advantage of state channels so in the same way that a developer that wants to make a regular application might use web 3gs or ethers ojs with state channels you need to use a slightly different set of API calls and different developer library which we're building the library obviously needs to work in such a way that you know Alice and Bob can communicate to update you know the state of wherever they're doing so we had to devise a whole set of protocols to show you and additionally they need to be running software that understands those protocols locally which I'll show you what that looks like as well and of
course the way that all of this stuff works is that you have the guarantee that it's Stressless because you know at any given point in time you can go to the chain to get your money out and so we've written it as well a full implementation of a state resolutions kind of adjudication contract set that's that you can go to the chain to get your money out no matter what the scenario is so the e li5 what does where did all these pieces actually mean and what are we as a product how we actually approaching this the goal from day one was to basically take this this image of Alice and Bob messaging each other and research discussions and papers that were produced and actually build production level software that you know can run on computers and so what we've done initially is we've built this the
client notes software that I described written entirely in typescript so they can run in the browser or in any kind of node environment that will basically be the case that it'll run inside of your wallet if you go to any page that happens to use the developer API so this is software that Alice and Bob you and your counterparty and the stay channel might run that is basically built entirely for this situation that's gonna enable you to use a state channel application basically it has things like making API calls to open a channel to update the state of a channel to take action to progress some particular part of the state on install application all these kinds of things
that you kind of saw from the diagram earlier of what generally state channels do and that's the first piece of software that we've written and that's now you know easily portable in multiple environments secondly because it's always a little bit confusing is we've had to devise this entire protocol of a secure transport layer between Alice and Bob that describes exactly what you need to send your counterparty to ensure that you can install an application securely or undersell an application securely such that you can actually rely on this software working so and again I'm gonna point to away you can see learn more about each of these different pieces in a bit so what is actually inside the protocol in the protocol that is essentially all of the specific commitments that you need to sign to
guarantee that if you were to go to the chain you'd get your money out so in-state channels the way that this works is if I say to my counterparty hey Alice I want to install the game of tic-tac-toe I use the protocol which is implemented by the client node software to say hey Alice please sign the specific message that says you and I agree to play tic-tac-toe four five eats each and what we're doing is I'm signing him transaction that could go to the chain I'm asking Alice to countersign that transaction such that both of us having signed it makes it legitimate and actually executable on the chain if we were to submit it but we never actually submit it and again the protocol describes precisely what these commitments look like and the commitment is just the transaction that you sign
that could go to the Shane and so both users always have in their possession these turns out these transactions which again the software is managing and storing for you finally when I say application library I literally mean we have libraries that you can import into your application let's say it's tic-tac-toe again going this example that you can make it such that if you're building a UI with say a tic-tac-toe board that has a you know a click handler in the corner for placing your X you can simply write a function call that looks like this you know place the piece and take that hope to ward get an object that represents the state channel application in this case take that to' take an action which is place X in this spot in this corner wait for the promise to resolve which is behind the
scenes using all the client note software I refer to ensuring that you and your counterparty assigning the precisely correct commitments using the protocol I described and then wait for the promise to resolve and then update the UI of your app so if you're running at a centralized application that's instant off change transactions for arbitrary smart contract logic the end result is that you should be able to use a very dead simple like five year old level API like this so that's it it's that that's that's really the way I want you to kind of understand how this all pieces together it's a very straightforward when you look at it this way it's an application library so developers can build state channel based applications it's a protocol that guarantees correctness for all of the operations I've described installing arbitrary off trained applications that
you can update the node software that runs in the browser or in any other node environment - you know I'll show you in a second we have a demo environment where we have run running in Heroku that operates as the hub and there's a lot more interesting stuff you can do with that but the most important promising part is that this is just simple software that works in any node environment and of course all the on chain contracts that you could use if you need to get your money out so so that that's that's really the basics of it and again all of this software works today so if you want to start building state channel applications it's already ready for you to use over the next little while we're gonna bring a lot more time in documentation and showing more example applications in ways you
can get started but this is kind of where we're at and if you want to see a demo because I don't have a lot of time right now to go through all the details you can go to playground counterfactual calm where you'll see an example where we're wrapping all this client node software in the browser as well as the Heroku one I mentioned that's running in the cloud that allows you to create an account deposit some money in open up arbitrary applications tic-tac-toe we have a dice rolling game all these things built using the developer API and then basically play these applications so one transaction to deposit your money in one transaction to deposit your money or withdraw your money out and in the meantime you're using off chain applications that are a hundred percent secured by the blockchain written in and completely isolated in
without developer libraries running using the client note software which is in the browser here using the protocol suite device and a really cool thing and all of us live for this is that this website at the moment kind of wraps the client note software very soon in the next few months we're gonna have a build ready of meta mask that'll also run this client note software so you won't even need to navigate to this page you can just navigate to any website at all that uses the developer library and be able to use to a channel applications so if you want to learn more all of this code is open source it's it's it's it's scarcely documented there's gonna be a lot more documentation as I alluded to there's links on this site to our website where you can see more dogs and
the specifications of the protocol of the note software of the contracts all the details are there and we're also very actively you know looking for contributors and people that want to help out or at least ask questions and so if you do have any questions or you want to chat with us we also have an open discord channel where you can come in ask you know any pertinent you might have or maybe you want to build an application then we can help you with it so you can just come here for that and yeah it's there's anything that you didn't quite understand I've given a similar talk and a lot more depth with less time constraints you can go to L for adventure slash ed con and you can watch that talk and there's a lot of general documentation online to learn more so yeah thats counterfactual and
we're ready to start building say channel apps thanks [Applause]