Cartesi: App Specific Rollup / Using the Linux stack as though it was a smart contract

What if we could run an entire Linux stack as though it was a smart contract? · 28 Feb 2024 · 1:17:29

Diego Nahab describes how Cartesi uses a full Linux stack as though it were a smart contract, and how this works as an application specific rollup. Suited to developers curious about building rollup applications with familiar Linux tooling.

Speakers

Topics

From What if we could run an entire Linux stack as though it was a smart contract?, hosted by Ethereum Engineering Group.

Transcript

okay hello and welcome everyone my name is Peter Robinson and today I've got Diego here who's going to tell us um a new way of doing things an altern it's an alternative to ethereum but um should be interesting to get a different perspective on what blockchain is and can be so um over to you Diego H hi so uh hello my name is is Diego neab and I'm with the machine reference unit at caresi and I thought it would be interesting to show you some of our work in making an enti Linux stack work as if it was a smart contract so for those familiar with the

jargon let me give you a context for this talk uh our project centers around a scalability solution for ethereum based blockchains so it's not an alternative to ethereum it's a way to add scalability to ethereum right so this takes the form as an optimistic roll up and the correct behavior is enforced by a fraud prooof mechanism but each application runs on its own chain right so that's where most of the scalability comes from so what sets us apart is that a frog proof mechanism allows a single honest civilian to defeat an entire Army of cheating CBL

and another difference is that our applications run on Linux so the talk will focus on getting the Linux size of things going if you're interested in our dispute resolution algorithm let me know and I can arrange for a colleague to talk about it at a later time I'll describe a simpler dispute uh method just to illustrate the requirements that disputes impose on our stack so unfolding this to those unfamiliar with the jargon here's what an optimistic roll up might look like right so everybody must first agree on a state transition function and an initial state so this is like uh ethereum and the Genesis uh for example state right

now everybody must agree on a sequence of available inputs and in a sense these are all known to the base layer right that you're that you want to add scalability to so when you feed the state and an input to the transition function it's pit out an updated State and a bunch of outputs so you can fold the state the inputs and outputs into a capital letter M and everybody then can run this transition function of chain nobody disagree say on the nth transition up to the end transition then the the outputs up to then can be trusted by base layer somebody disagrees then then can engage with Bas layer to

start a dispute over these results but the premise is that if you set the incentives right nobody will dispute because whoever is cheating will lose and you almost never need to involve uh base layer on the transition function itself this is all very nice and Abstract but let's get more specific right so imagine you're implementing a decentralized exchange now the state would include current balances of all traders in all supported tokens it would also include the outstanding orders in the order books for each uh supported token pair that you can trade right and an input would include say a bunch of

deposits new trade orders and perhaps withdrawal requests the outputs would be what we call vouchers and a voucher would allow a withdrawal to be performed in base layer to move funds from the exchange wallet to the trader wallet this of course would only be allowed once everybody agreed that a given state has been settled now this function a is actually a computer program like a real computer program and implements the state transition function for the exchange and our goal is to let users implement this program pretty much however they like so here is a general idea of how disputes work uh assume two parties

agree disagree somewhere in a sequence of straight transitions between A and B uh we want to find the first transition where they disagree so we ask them through Bas layer if they disagree before the midpoint transition and with the answer we can have this interval of disagreement and we can then go on with this uh binary search if you will until we reach a single state transition now only the honest party will be able to prove their opinion about the state transition remember the state transition function is just software and it stat is just data but the function can be too expensive to run directly on on base layer and the data can be too vast to

actually submit to base layer so we want to transport transplant uh the transition function and its state to a special emulator that can continue disputes still offchain but the thing is that no man is an island and neither is any software so your code usually depends on the code of others and it depends on your choice of programming language the programming languages used by the software components you want to use and these components and programming languages uh depend on an underlying operating system very frequently so finally the operating system itself depends on certain features being supported by the

underlying computer architecture so trying to extricate existing code from its dependency graph is often a futile exercise and frustration right the alternative would be to reinvent the wheel and code everything yourself many wheels in fact and if you want developers to adopt a technology we shouldn't force them to go through this hell right we should just be accommodating now you could argue that if you give developers power they will abuse it right but I don't want to be in the business of telling people how to do their work I want to empower them to do their work however they like and to be as productive as possible so let's look

a little bit in more detail have to to make this happen so borrow nomenclature from the CN C++ standards and there are two parts of these standards uh the first part deals with the fact that processors run code at a very low level of abstraction that disregards uh human con concerns so the freestanding part is a small subset of the standard that simply offers higher level con constructs for humans so stuff like literals Expressions conditionals BAS Bic types function classes you know statements so they just isolate the programming from details of the given processor so you can change the compiler backend to Target any

architecture you like right now the hosted part of the standard is a lot more ambitious this aims to simplify many of the common tasks uh that a programmer would want to perform stuff like thata structures and algorithms but also access to file systems memory management Process Management networking almost every programmer uh of of traditional applications operate at this level so compared to the freestanding world in the hosted world you feel like a superhero but the problem is that much of this added functionality depends on an underlining operating system and even though you are isolated from directly dealing with one the programming language runtime has to deal with

one and now here is an unspoken secret when you hear a news that a given block project now supports a given programming language what they mean is that they support something like a freestanding implementation but it just doesn't cut it right I I I I once had to implement a logorithm function because the language didn't support or the implementation of the language didn't support some people like this kind of challenge I in fact do right but I also have that lines to meet right I have two young kids I have other stuff to do so we want to support as many host of programming languages as possible and the way to meet all the resulting dependencies is to actually run an operating system and then Linux comes to mind right away so an OS controls access to all the

resources in a computer for example the memory used by each program each program's time in the processor whatever devices you have plugged in and and this this in fact allows different software components to collaborate uh without getting in each other's way the also provides a bunch of facilities most software has come to rely on for example memory management process and thread management different file systems networking nobody should have to reimplement this stuff just want to use it right we've grown used to it the US also isolates programs from details of different devices that might be connected to a computer H it hides Those Behind drivers and then offer a uniform interface that programmers can use this

is less important to us because we don't intend to hook up different webcams to the blockchain right but it's still good to have the thing is that operating systems also have needs they co-evolved with the underlying Hardware on which they run and they now rely on a bunch of harder features to work properly for example us OS code can do stuff that user programs shouldn't be allowed to do the memory used by Theos is protected from access by user programs in fact even memory used by user programs is protected from other programs so to prevent one program from hugging the processor the US can

unilaterally regain control of the processor these things require uh the architecture to support features such as privilege levels memory protection virtual memory uh timer interrupts and a bunch of other things not all Computing architectures satisfy these needs right notably uh web asent and the evm and pretty much every other blockchain related VM out there do not support these things and they were designed to run over onos not under onos right they were designed with fre standing implementations in mind in contrast pretty much every modern hard Hardware instructions architecture satisfy these needs right they are

pretty complicated pasts I guess you could create your own but you probably do a lousy job at it unless you are one of the few specialist in this field so it's it's actually better to pick an existing one and focus on desired properties so you want an open standard you want uh as molish uh specification you want good software support like you want this thing to already have Linux support have compilers for it and all all these goodies right otherwise you'd have to create them from scratch and the choice that comes to mind is risk five recall that programming language standards at least the C++ ones and and and some others we're divided into two

parts the freestanding part and the hosted part and the same thing happens with risk five specification and other hard architecture specifications they're divided into two parts there's the unprivileged specification which is basically what a compiler backand for freestanding implementations of programming languages would need right but then there's the privilege specification which is in addition all that operating system needs and then indirectly what you would need for a a host of programming language implementation so the parts of the risk five specification these two parts are divided further into extensions and and

different implementations of the architecture can pick and choose which parts and which extensions they want to offer so for example uh 64 bit support integer multiplication and division Atomic operations floating Point compressed instructions these are all separate extensions in the non-privileged part of the standard now the privileged part mandates at least a machine instruction set architecture but it leaves supervisor and virtual memory as option as optional uh Linux distribution have adopted the blue ones shown here as a working minimum and here's another secret so when you hear that a blockchain project supports

risk 5 what they they mean is they support the non-privileged part of the standard and a small subset at that so they can't really run Linux directly they can't even run the Linux kernel directly much less a Linux distributions so the cartz machine is is our risk five architecture and and emulator right it's complete enough to run existing Linux distributions un modified we support all common extensions including floating point and even the hypervisor extension so all you need is a very simple patch to the op SBI firmware and we need one kernel module that supports uh roll up input and output right we have a a driver for

that if you will this this architecture and the emulator itself were designed specifically to support dispute resolution right so the the emulator can be embedded inside other programs uh it has an extensive API and binds to a variety of programming languages so you can control the the the emulator inside your own program uh and and of course you can run the applications will run inside the emulator but the emulator itself can be controlled by a by a node if you will that is that is running the computation and and and it can be stopped and restarted from any instruction cycle and at this point it

can also be Sav to dis and load it back it obviously runs are reproducible right this is something that every every decentralized project needs uh it doesn't matter in which computer you run the emulator or how many times you run a given computation the results are always the same this obviously implies that the emulator can be run in a self-contained mode right so nothing in the outside will affect the result of the computation but more importantly the emulator is fully transparent so you can inspect the entirey of its state from the outside and the emulator can even log what access to the state happened over the execution of each instruction

cycle the entire state of this emulator is mapped to the risk 5 64-bit physical memory address space itself this is a trick we use to be able to identify each piece of the state in a uniform way uh in practi obviously this addressed space is sparsely occupied right it's it's large addressed space not as large as the EVMS right but it's it's still pretty large so it's divided into a bunch of linear memory ranges and the most most obvious uh memory ranges would be the ram right the memory but also flash drive say containing a root file system with uh say some version of the

de daan Linux distribution and your your application program inside your dab inside there's also a special memory range that is used to receive rollup inputs and to send roll up outputs so this is what will be controlled by that Linux driver that I mentioned and there's another one that you can reserve to put your application State uh so this would be like the the the exchange uh the the the the wallets of uh all users and the orders that are pending and the other books and stuff like that right but crucially there is a a shadow memory range that contains all the general purpose registers the floating Point registers the program and cycle counters

and many other control and Status registers right so this this memory range here and this is all the stuff that the emulator needs to do its emulating um so the address space is then divided into 64-bit words and this array of words is then merized now obviously this will end up being a very sparse Merkle tree but it locks the entire of the state into a single state hash so going back to disputes we had narrowed down the disputes to a single application of the transition function big F over M sub I and the honest party must now prove their opinions but f is just some software in

our example it was an exchange and M is just some data right the balance and open orders in the exchange book so we wanted to transplant this software to special machine so we can continue dispute of Shain and of course we will move this to a cesia machine we do this through a template and a template is just a snapshot of a a machine that was agreed upon to settle disputes over a certain application so to create a a template you start from the machine from a machine that is ready to boot we put Linux and the firmware in the machine Ram we set up the program counter to point to the ram we then plug

in a root file system with a dab and in your dab binary and all its dependencies and we set the init script to run the dab right we then run this machine and what will happen is that it will boot Linux Linux will run init in it will run the dab and then the dab will ask for the next input from the driver h and a template is a machine that is stored at exactly this stage yeah in other words it's missing the next input and the depth state but it already embodies the the application transition function the big F function so Bas layer knows the hash of this template the miracle hash of the

state of the machine that is this template right we don't care about most of the state for example the Shadow and the rim are it's are you don't want to look into them right they are not easily understandable they basically contain a booted Linux OS with a program in midf flight but it's basically F it's an embodiment of f right uh the thing is that base L can then splice uh the the M subi into the template to do this all it needs is a few hash operations right then you can find the hash of the machine with the rollup input and the D and the D statec State already patched into their memory ranges like this right

it's a typical merco tree operation to do that now the Cartesian machine has a transition function that is simpler uh than the transition function of the application right it it's at least less expensive to run it's just a single instruction cycle from the emulator right so what we do is we run the machine for as many cycles as it takes to reach a fixed point in practice this fixed point will happen when the application invokes the driver to say that it's done with the with the input and at this point the application State uh the modified application State and the outputs it generated will each be in

their designated memory ranges like here right and uh if you know the hash of the machine at the state at the stage right you can then slice out these Rangers is to recover the results of applying the big F over M sub I right you can recover M sub i+ one now obviously the two parties in the dispute will not agree uh on this fixed point right and and so they will disagree somewhere in the sequence and we just keep doing what we were doing right but now we're doing it instruction by instruction in the cing machine we asked if they disagree before the midpoint

and if we if and if by doing this we can have the interval of this agreement and we keep at this until we reach a single Cartesian machine instruction and now the honest party must prove their opinion about this transition uh but then how right so the problem is that this transition function is to too complicated for Bas layer to implement we needed a bunch of extensions to make the machine useful right so floating Point uh also a a tail B for virtual memory like these things are complicated but without them we wouldn't be able to boot Linux or we wouldn't be able to use a standard Linux distribution or it would be inconvenient to do to do so right so this would prevent developers

from bre migrating their codes to run inside a cesing machine in the first place but I imagine having to implement say floating point to run on Bas layer right and not only that but an implementation that actually matches the one off chain it would be kind of a pain and maybe insane so that's not what we do we instead Define a micro architecture now this has a even simpler transition function right it's only the non-privileged part of risk five so no extensions just the bare minimum to execute the big architecture right so the big uh architecture that we describe with all these

extensions so the microarchitecture state is also mapped to the same address space as the rest of the machine but it has its own shadow with its own tiny registers and flags it has its own small Ram uh so what we do is we compile the transition function of the big machine to run on this much smaller machine and in fact this the compiled big machine already comes preloaded in the template itself right in the in the microarchitecture ram so we can run the big machine directly on the post instruction by instruction or we can run each of these instructions as micr code running inside the machine is the small

machine right the micro architecture micro operation by micro operation so this is the point at which uh our inter start getting nose bleeds because we are too deep in the recursion right it's like that movie Inception so uh a dream inside of a dream inside of a dream right so let us recap we had we started with an application running bad bare metal in the host computer at full speed somebody disagreed on the results and we managed to D narrow down the disagreement of the to the processing of a single rollup input right we then moved to running a single input inside the cartisan machine this is our emulator and the emulator is

running bare metal in the host computer but the program uh is running inside the emulator so the the the two parties will obviously disagree on the result of of that as well and so we can narrow down their disagreement to a single instruction of the big emulator of the cartisan machine we then moved to running that single big instruction inside the micro architecture and we can now narrow down the disagreement to a single micro operation now this micro operation we can prove to base layer and to prove the transition function we have to mirror implementations of this transition function these implementations use basically the same source code uh the

trick is that the offchain implementation can collect the log of all the accesses that it performs at to the state both reads and wres while executing a micro operation right so something like a word was read from a given address uh with a certain value and here's a proof based on what we know to be the current state hash like a Merkle proof or maybe a word was written to another address with a value that we know here's a proof that lets you figure out what the new state hash should be uh so we pass this log to the base layer implementation and it will execute exactly the same logic as the offchain implementation but when it tries to read

or write to the state it consumes and verifies one entry in this log and but only the honest party can provide a log that starts from the correct State hash and is then accepted uh by base layers now there are other possible designs potential designs right for example maybe you don't want to write your application code very carefully so that you can run it in bare metal right after all you have to make sure it is reproducible uh and self-contained and so you wouldn't be able to use native Floy point and otherwise you might end up obtaining

different results in different architectures you could you could just run it inside a ctis machine directly right there'll there'll be an overhead of course but it will automatically be reproducible and self-contained no matter what you do like no matter if you use threads it really doesn't matter uh or maybe you want to reduce the number of stages in this dispute that I described uh so for example you could use ZK to prove a bunch of State transitions directly right and we have a a prototype in which one of our people made compil our program our our our big emulator right code to run on risk zero right so in a sense risk zero can run our micro

code or maybe you want a different OS right uh just check if it has a a risk five port a bunch of them do right so Nick Nix OS secl4 which is a formally proved operating system kind of a micr coral there's a bunch of others there so it should be possible uh to use those instead of using Linux if there's some interest let us know uh or maybe you want to use a different sequencer for your rollups uh say I don't know espresso or something like that and we we we have kind of a prototype that allows generic IO to get inputs from other sequences so the point here is that your modular we're open uh and so you can just take bits of our stack and

and and have fun and let us know about it in our Discord Channel now what is this all good for um if you if you go to rollup lab. you will see a bunch of ongoing projects from the community so these are all full-blown applications right they include interactions with Bas layer uh and front end in addition to the back end that runs inside the carzing machine now in the past we've had a lot of trouble explaining to people why these count as decentralized blockchain projects right because they see Linux and they think we're crazy uh and then it clicks right and indeed some of these

projects have now been finalist in in ethereum events I'll only mention some like a couple of these projects please go look up the author presentations they do a much better job explaining their Vision than I ever could so for example there is this decentralized bug Bounty program called bugless and the idea is that you put the software that you want tested inside of a container or perhaps a hypervisor gasto as inside a cesi machine now you can pass inputs to it and if somebody manages to crash the software inside a ctis machine then the the the Cartesian machine will issue a voucher that will

allow the person to cash out the price there is no middlemen here right people can be sure that they will get their price so here you see the UI where they tested a known exploit to to a certain version of Lua just for fun the Prototype supports a bunch of programming languages and software packages and I think this project has a lot of potential right they are currently adapting this to allow for bounties on evm Smart contracts for example uh if if they use our hypervisor extension then they could even test the Linux kernel itself or they could use uh this to test Linux system tools right in a decentralized

way so cryptopolis was in fact an eth online 2023 finalist right so top 10ish from around 500ish uh projects it was a oneman project right so Danilo our our Danilo took uh the C++ was part of the original uh 1989 Sim City code and compiled it unmodified to run inside our emulator he then added a no GS binding to control the game programmatically and he came up with a pretty nice way to tie up the game economics to use tokens right so it became an actual economy the whole thing runs as a

rollup uh think about the freedom that he had to use the tools that he wanted to compile this project right he used different tools than the tools I would use right he's very familiar with nodejs and bindings and and and C++ maybe I would use Lua instead of no Jazz but I mean it's it's the thing that he's most familiar with it's a lot of freedom and that's what we are all about so uh now I'll change to uh the the final part of the of the talk in which I'll go and talk about playing with the emulator itself right I'm not going to talk about messing with interactions with the blockchain or talking about front ends I'm an emulator guy I'm going to mess

with the emulator and show what it feels like to to to do that uh Peter I don't know if you want to open for questions before I transition to the to the demo and then we can do more questions yeah yeah I I think that's actually a really good idea um and I know one of the questions was is there a demo or is it just Theory so we've already got the answer for that one um but beyond that one um does anyone have any questions so I do so you know we've seen a a little taste of things already but so is is the idea say that Sim City roll

up as an example so you would play Sim City and what the rollup would prove that Sim City executed correctly is is is so you would you would in in that particular game you would have a wallet with a certain number of tokens that represent the money that you have I don't know if you've played Sim City right so there's an economy you you need money to buy things and make things happen and so the the on ethereum you would have deposits happening periodically like you have in the game right and then you would send a command to the game to the sequencer saying you know I want to build a a building here

in this position and I want to do this other thing here construct roads and and a factory and and and wiring and and all the stuff that you do in Sim City it will grow as inputs to uh to the rollup and then the Cartesian machine with the Sim City code inside would take inputs from the rollup and apply them to the game and evolve the state of the game uh and and produce more money if there's more money to be produced this would be feedback and so it would be possible for you to prove that you know here's a set of uh inputs that I sent that created this thriving City this thriving economy and then you could use that in

competitions you could maybe trade tokens between different cities you know it's it's it's a lot of it's a big space right of what you could do but this is all possible because you know the game logic is is proven right it's running inside a thing that is provable yeah does sense yeah yeah so it sounds like you could do quite complicated um multiplayer onchain games interactions yeah it was generally you know like you want to keep your your game logic offchain and just do critical things on chain all right that's very interesting um does anyone else have any [Music] questions I think

no got a question uh yeah of course you talked about using um other operating systems that run on risk five so for example you gave the example of um free OS um so right so you know we're talking about an emulated um environment you know within the system it would you be able to use an uh a device on the edge that runs Aros that would look like it was inside the rollup and and use it as it collects data and

then and use it as part of that whole uh environment whatever solution you might build so what you're what you're describing is a problem that is is every blockchain has right which is how can you bring data from the the outside world like people used to call this a type of Oracle right so whatever data you want to feed inside you want to feed inside the machine has to be provable uh to Bas layer and so to the extent that these devices uh post signed inputs somewhere to base layer or to the sequencer themselves we could make it look as

though the actual devices are operating inside of a cesan machine right but then there is this problem of making sure the data that that appears there is known at least Pro it provable on on Bas layer right otherwise it has to be self-contained and in a sense those uh those those inputs in the sequencer are a solution for this right so you can imagine that you have uh devices that are posting signed data to the sequencer that is used by the ctis machine and then these can make we can make those appear from inside say Linux or Os or whatever is the OS okay cool

thanks yeah I think that that's it so let's get on please do your demo yeah yeah let me see how I do this wish me luck so uh here's a here's an example of of how you can make uh a how you can make a root file system right for to run inside a cesan machine so we actually use Docker for this can you imagine so we say here you know I want to produce a a a file system that is based on risk five right right and here's the base image and we just copy

some of our tools inside we have a dabing package for it and then we just run regular Docker things right we to produce an an image so we're installing busy box which is kind of an embedded version of a bunch of system utilities to make the image small car utilities bash right R tools and we create a a user home directory and that's it right and uh if you if you I already built this right so there's a command line that I can run to to do this for example build let me show you the build

script right so this is a build script that I created uses Docker to build a tar file with the file system and then it uh does some things just to ensure reproducibility and generates an EXT2 file system from the tar file that's all it does right so I could run it hopefully uh so Docker file basic 128 Megs to a. EXT2 it's running Docker and there you go right it's a nxt2 uh file system so I could run uh our emulator let me just clear this

here I could actually run our emulator right and here's the the command line right so this is I'm using our Linux kernel already linked with the with the firmware and uh I'm saying you know use the for the flesh Drive root use this root FS file here which is a file system and be quiet about it and so it just printed a a splash screen and said you know you didn't ask me to do anything but doing anything actually means doing a lot of things right because if I ask it to be verbose uh you can see that this thing actually booted Linux right so there it

is right the the the messages that Linux prints while uh while booting and then it ran our init script right and init script printed the Splash and then said you know I have nothing to do and then power down that's what it is now I can do the same thing but now I'm going to uh ask the machine to show me the state hash at certain points the state hash of the machine right so here's what it did so this is the miracle root of the entire machine State before it executed the first instruction cycle right and here is the state hash when it was done processing right when

it was halted in a sense at this instruction cycle so this thing ran uh I don't know 36 million instructions to do nothing right now I'll show you something a bit more interesting which is I'm going to stop the machine in the middle of the splash screen right uh save it to disk and ask it to compute the hash when it started and when it saved to disk and then I'm going to load the machine from dis and print the hash that it has at that point and then let it go until the end right so here is the hold

up so check this out so this is me asking the machine to stop at this big instruction cycle with a certain number right but also print the initial hash and the final hash but store the machine once it stops store in this directly mid Splash right and then run again the emulator loading from mid Splash and also printing the initial and final hash and you see the initial hash that he printed here was the same as the initial hash that it had printed before of course right and then this is the hash at the midpoint right uh and then the machine exited and it started again PR the hash in the beginning it matches which is reassuring uh and then it goes

on printing the rest of the thing and saying nothing to do and here's the final hash which again matches the final hash that he had if it ran directly does this make sense uh uh yes it does to me yeah awes cool so now I'm going to do something a bit more insane which is I'm going to pretend that uh so let me run it and then I'm going to explain what it is doing so I'm going to pretend that uh we already had uh a dispute we're in the middle of a dispute we narrowed down a certain input for roll up right uh and

then we said oh actually the big instruction cycle that we care about is the cycle a th and then we moved to the microarchitecture and we kept doing the search and figure out all the actually the instruction cycle the micro instruction cycle that I care about is this one right so this is like uh little F uh Little M sub a th000 right and this is fi and and mu sub 6,42 and then I told the machine you know run until exactly that point first running the big instruction Cycles on the host and then switch to the micro instructions and run 600 6,42 of them and and when you're done print the log

of the next instruction and here's of course this is human readable for a very special kind of human right but it's is a is a a visualization of what the state transition log would look like and uh what it's doing here in the log you will see that it's reading the cycle counter right and it found the value that we that we expected there and then you read the H flag to see if it should be halted already the micro micro architecture should be halted and it found that it's a zero so it's not halted so it goes on and he reads the micro instruction program counter right and the value that he finds there is this value here and so

he goes to memory at that address and reads the instruction itself right now the instruction happens to be a store double word a risk five store double word instruction and to execute this instruction first you read the target address for the store and this is the value right uh and you also read the value that you want to store you want to store one you will notice that this address is exactly the address of the halt flag here right so it's writing halt there uh it's writing that the thing is now halted and so basically this is the right to the state saying that the mark architecture is not halted

and then it uh advances PC right to a new value adding four and finally updates the cycle counter to the next cycle counter now this is I I I picked this number because this is exactly the halt the microarchitecture right and I I knew that I would be able to explain what's going on on this one does that make sense again so each each of these operations will be backed by a Merkle proof that on read prove that the value read at this address is what it is and on rights allow the base layer to produce the new state hash once this uh

this this operation has been performs performed does it make sense yep sounds cool now let's stop yeah go ahead h no no that makes sense yeah okay so let's ask the machine to do something so here we are passing it uh to run Echo hello world right this is actually running inside of bash just think about it for a second right it's just printing hello world world but behind it there's a lot going on right

now uh let's have some fun here here I'll show you a different Docker file and this do Docker file uh includes more stuff includes our our tools right but also a device tree compiler JQ which is a way to mess with uh Json uh files uh the the calculator right curl to get data Lua and a few modules xxd to inspect files W get I don't know I don't know if why I have W get and curl but you know just pick whatever you want uh VI I'm a VI guy sorry MX people and a few other things

right and I already uh and and then copy some Lu files inside the create create a user right da and copy some L files in there I already have this uh this roll up this this root file system built here right uh so I'm just going to go and uh uh run it to show you so and there you go so now instead of using bash I'm using Lua right and printing hello world uh but I could I could show you something else which is this right so this is another Docker

file now this one is based on just like people that use Docker do right so I'm just using a different base image here here's one that includes nodejs I'm not a node guy but Danilo is right you might be uh and I'm just taking that base image and installing our tools uh and I can now I already have this uh this root file system built here so I can actually run it thing is that node uh takes some time just to initialize right but still acceptable right this initialization would already be in the template so you

wouldn't pay for the initialization itself right but still it's pretty decent and now I printed hello world using node the thing is just do whatever you want pick whatever tool you you want to get the job done right now obviously when you are prototyping uh you can run the emulator in an interactive mode and this will not be reproduced of course but allows you to have a lot of fun right so uh here I'm inside emulator and I shared so what I did was I said you know share the current

directory uh and mount it at MNT here add Network support make it interactive with enhanced terminal uh so this is the the listings right that we had outside it's it's showing what's in the outside world right but you can also do uh curl you know you have Network support this is what this is what Google uses as an index. HTML uh and you can even VI you could change this you could do whatever you want right it's it's prototyping uh

and you can also uh run in interactive mode and you can actually compile this emulator to run inside of a browser and we have some crazy people that that did that in in in the foundation and so I don't know you can I'm going to suck at this so I don't even know what the what the keyboards are anymore what the I don't know what how do I shoot okay at least I can shoot so this is running Doom some some violence here anyway so this is just running it inside the inside the browser in

interactive mode right uh and and one application for this technology is you could collect a log of all the key strokes that were generated and the times at which you fed them to the emulator and then you can use this uh as a rollup input and and verify a high score for a game for example does that make sense yeah assuming that the game's deterministic I guess it does um it is because it's inside the CZ machine right the the only thing that would not be deterministic is the user input but if you collect the log at exactly the instruction Cycles at which you're

feeding those to the machine the whole process becomes deterministic yeah that makes sense yeah so uh let's now create one of those templates right that I talked about uh so first let me show you uh a shell here script this is a shell script that I'm going to run inside a cising machine right and it's using our utility rollup saying you know all right give me the next input and I'm just piping it to JQ so that it pretty prints this this utility Returns the next rollup input as a Json file so that you can mess with it and here I'm just

printing it just to show you guys right and so the the the command that I'm going to run is this here and uh and here's what we did right so we copied the script itself directly to the init file that we have inside uh and then we asked it to store a template and I asked it to give me the the hash right uh of the template so this is what uh bass layer wouldn't be aware of you would be aware of this hash of the template right but now what I'm going to do is I have some some rollup inputs

that I'm going to feed directly to the machine right because I'm a machine guy I I I like to be ble to do everything without connecting to the outside world so I I created some rollup inputs that I'm going to feed directly uh to this um to this thing and so here's the here's the command let me show you and it's doing its thing so let me show you first the the command here so this is where it begins right I so we loaded the template right and here's the hash of the template and then it patched it patched together the input and it might have patched the state as well uh but here we don't have a state to carry

over but it patched the input so it changed the state hash of course right so uh Bas layer will be able to obtain this hash and this is the hash of the machine that it supposed to run uh to do disputes over that input Index right and so here is it runs the program inside and J JQ just prints the Json that that rollup command uh generated and you see this is the payload this is input zero at that point the machine has this state right and it patches the new input so it changes the state and here's the new input and then it at at this point the machine has the state it patches the new input it has

the state and so this is sort of what would happen uh during a dispute does this make sense just showing the inputs from the inside yeah okay yeah now we're not doing anything with this right so let's uh let's show a different uh a different script right now this script is using the same rollup accept but it's selecting just the payload right to in inside this variable and then it prints the variable but it also

passes the same thing to the application the calculator application right so I'm now going to run this on a few Expressions right as though the inputs contain the expressions and so you will see that I'm using a different different set of inputs here uh than I was using before but here's what this looks like so the input was 2 to the 2000 and this is what BC produced right then two to 200 then 2 to 20 right just performing some computation you can uh imagine that you know if people really care about the value of this computation in instead of just printing it we would

actually output a notice that is provable to base layer or or even a voucher you know you can do whatever you want right but this is just showing how it's easy to just do simple computations now uh I'm going to go one step further and show you another advantage of of uning using a full operating system which is uh let me show you this um this is a Lu application right and the L application just loaded a package for Json manipulation uh it then loaded a very simple library to deal with rollup

inputs got an environment variable the address to figure out what the address is of a HTTP server that we're going to run inside and now applications will use that HTTP server to get the inputs instead of using that rollup utility command line utility or and instead of talking to the driver directly you just use an HTTP server I'll show you in the command line how this works but what it's doing is just using a an HTTP post to that URL saying accept and this means give me the next input then it takes the response which is going to be a a l table representation of that Json and just removes X encoding because the

server uses X encoding right because the payloads could be binary and uh and then it just encodes that into a string without the the the X encoding right it's very simple uh program but to run it what I'll do is is this here let me show you okay so let me show you the command itself I'm saying okay append to the enip script the address of the server then run our server the server is the one that is going to get inputs from the

device driver and pass it down to whoever request them using HTTP right it's almost like a bridge between having to deal with devices toal with HTTP which is something that most programming languages can do and so it allows us to not have to implement bindings to our our driver to every programming language out there right and the server will be listening at this address and then I'm going to run Lua and run our d uh there and what is happening is you're going to see some diagnostics from the HPD server itself is saying sure I'm running the Lu script that you asked me for and oh wait I received a a post to the Finish entry

point and and then and then the Lua program is now outputting this right it's that Json right uh and the payload is this is input zero and then it does the same the the server reports that he received another request and and Lua prints the the next request this is input one and so on and so forth right does this make sense I could have used know JS to do that I could have used uh Python and people have done that right are you guys still with me on this yeah we've got a thumbs up from Mark and I'm still here and um I'm

watching watching and um yep absorbing yeah yeah it's it's a lot and now I'm going to do something very very crazy which which is I am going to show a prototype of that exchange example that I mentioned and to do that uh let me show you um so this is the directory of the exchange that the Prototype that that I wrote with with the help of a friend and you know it's it has a CPP here that is just a C++ code some includes you know like this is a a Json Library uh and

that's it right but I I will uh I will show you a Docker file that I used to build this to run on the host so I'm going to run this bare metal in the machine right and but I want to run it on Linux so I'm going to run it inside Docker right because I'm on a Mac and uh so this is this is a Docker file for theost post and what it does it's it creates a multi-stage it's a multi-stage Docker so it creates a an image that is just to build the application install gcc2 inside it right and then it copies the ladex source code there and makes a host executable right and then it

creates an image that is the one we'll actually use uh and it just copies the compiled host to slome slab right so so um so I'm going to now run this uh the resulting Docker file uh I mean I could build the docker file let me see if this works yeah so uh I'm going to now run the exchange and the command line is going to be a bit crazy but I'll I'll explain it uh here okay so here's the here's the command

line that I ran right so I I I said okay run this application here from the image uh lumad deex right share the the current directory in a shared uh Mount point and move the working directory there make it make it the working directory right and now I'm when I run this application uh it's going to use a uh an image file name that I'm calling Lambda just like Amazon Lambda right this is the state of the application you will notice that first I am ER brazing it if it exists and then I'm creating it a new with zeros so this is the initial state

of the dab right and uh it starts zeroed but when I run this application I'm asking it to initialize its Lambda state right and I'm saying you know what pick inputs from these files here uh I I put the I created inputs to The Exchange already right and metadata for these inputs and I'm saying you know feed inputs from from zero to five to this and then it starts running and the DAP is reporting okay here's the the virtual start this is something that I'm going to explain later on but it's basically saying okay here's the first input it's

an erc20 deposit uh successful deposit with this token to this Trader right this this is the sender will be a Trader uh and here's the amount so this is something that would be received an input that would be received from an nc20 portal to the sequencer and it would be only be issued if the same token amount had been already deposited to the xch change wallet in base layer and now you're just telling the the the application that you know this this deposit has happened and then it just publishes a notice which is a verifiable output from the machine saying you know Trader such

and such now has this token this amount added to the deposit uh there's another deposit for a different uh token right this I one of them is ctsi the other one is usdt it I didn't didn't have time to put the translation here this is the sender here's the amount uh and it outputs the notice and then he receives another input which is actually a a new order so this is a buy order from uh and it will have received will have been received from a a a contract owned by this Trader here right so this is a trade order of buy a th000 uh

ctsi at 15 cents per usdt which is a pretty good deal at this point uh and then and then a new order arrives this is from a different Trader this is actually a a a sell order right for a c amount and then it's outputting notices that it closed some trades right it closed uh a few of these trades and then a new order arrives and then it uh some it closes again right um and then there's an withdrawal right and the if withdrawal uh generates a voucher which is this uh transfer that the whoever

asks for withdrawal there's a logic inside this code that will check if they have enough money inside the the wallet uh and if it is if if they have it would issue a a withdrawal right and and then there's a notice so basically this is like the the simple workings of what an exchange would look like does that make sense uh now what I'm going to do now is I'm going to take the exact same uh source code I'm going to change the library that he uses to get inputs rather than getting inputs from files you will now get inputs from directly

from the Linux driver and I'm going to compile it to run inside the emulator right so uh if you look at uh this this here uh I'm using now a cross compiler Builder uh you I could actually build it inside the risk 5 emulator directly uh but I I prefer cross- compiling because it's faster so I set up a cross Builder here uh I added basically our Linux kernel headers but also the cross compiler to risk 5 uh gcc2 again uh added the user copy lumad deex there asked it to build the

binary and then I created an image now that is a risk five image and just the regular stuff that I did the that I usually do just now I actually copy the the emulator version of that exchange to the depth folder right and I already built this here uh let me just run it and here check what I'm doing right so uh I created the Lambda State uh I killed it right created an empty one

with zeros this is a different file than the other file just because I want to have both files available and and then I'm running the Cartesian machine right and I'm passing uh this command line here asking it to initialize the Lambda uh and what is cool is this line here I want you guys to take a look at this line here what I'm saying is you know add another flash drive but this flash drive uh I want to use the label Lambda and I want to have it associated with this image file name which is the the Lambda State for the exchange right which is the the state of the

exchange uh I don't want it to Mount because it's going to be a raw file system a raw uh flash drive of raw device but I want it to be shared and what shared means is that uh changes that happen from inside the emulator to this uh flash drive will actually be reflect reflected in the image file on the outside of the emulator right if you don't put shared here then everything happens as a copy on right and and no changes are made to say the root file system if somebody changes the root file system inside the emulator those changes are not reflected but on this thing here on this file on this drive the changes will be reflected on the on the backing

files and then I I tell it to run the same inputs that that I I ran the exchange bare met uh but run it inside the ctis machine and it goes and it produces the same messages right and if we look here let me see if this is true what do you know the hashes of the state of the exchange are exactly the same running inside and running outside now this is kind of baffling if you think about it and we create this this stch demo just to shock people in our group right so this state is actually

contain in a bunch of C++ containers so multi Maps uh unordered Maps uh uh STD arrays they are the native containers right and the same containers are being used inside and outside in a way that makes the state match bit by bit right and uh uh in fact I can then uh inspect the the state from the outside to to see what's inside it right so I instead of uh running changes to the state I can do what's going on here yeah so uh I I now just ran the program on the

host using that doer container that I had right uh and I said okay here are a few queries that I had produced already ready and I said you know just I know that the the files are the same right but just to make a point here I'm using the state that was produced from inside the emulator but I'm running this outside the emulator to inspect this state and there's a query there for the wallet of Trader such and such and there's a report generated that says oh you know this this wallet has two entries and one entry is a this token here and the quantity is an I don't know 97,500 and there's another token uh and the quantity is 70 right and and then

there's another query which is a like a book query and says so give me the the the trader trading book the order book for ctsi and usdt at a depth of 10 and then the report says oh here here's here are the orders right there's a a book entry uh there's a uh Buy an outstanding buy order of 500 from this Trader and then there's an an outstanding sell order uh for this quantity by this order Trader you know and you can imagine that if you're working on a back end on a front end you would then use something like this to get all the data from the

from the exchange and put it on on some UI and populate it somewhere you know and uh yeah that's uh that's all I got for today and let me know if you guys have any uh any doubt and uh if you have any questions hit me well go ahead Mark I was just gonna say that was uh that was very interesting dieo it's kind of it's gonna take a bit for me to digest and uh and and think through but yeah it's quite fascinating thank you yeah you're

welcome so in one of the some a few slides ago there were multiple um trades that were happening you know someone was transferring and someone was doing this someone was doing that were each of those line items separate transactions was the overall a transaction that then could be um proved so it was so yeah so so there are two different ways of doing this uh in the world of rollups right usually you you use what it's called a sequencer and this is a way for people to bundle a bunch of different transactions inside

of a single base layer transaction and and aggregate aggregate them together right some people used to call them aggregators but but now they're called sequencers but the idea is that you can aggregate a bunch of transactions in a single input and then you could have code inside the exchange and the Cartesian machine that go over an aggregate inputs that has already been ordered by the sequencer which is why it's called a sequencer now and and then verify each one and and and process each one of them so it could be a single transaction that contains multiple say deposit withdrawal uh uh trade orders you know in a single one but here this for for Simplicity here I did each one as an individual uh transaction just

because it's more convenient but but you also because of of of a fear of censorship uh in sequencers you always also have a way of of going straight to base layer and saying no here's my order I don't I don't trust this sequencer anymore I want to actually go to base layer and put an order and so that would be like a single order uh that would appear as a as a non-aggregated input okay all right that that makes a lot of sense um now there would it would need to still verify the signatures right if you do aggregate order you you need to to verify signatures and there are aggregate methods of verifying signatures of the whole thing very

easily but I didn't do any of those things for this prototype okay fair enough fair enough um are there any other questions for there I go none none from me okay well look thank you for a um really interesting talk certainly different way of thinking about things I think that um certainly yeah makes us all think um yeah yeah which is good did you um did did you did you put those extra slides at the end yeah yeah all right good let's flip to them so there's the

store um buy your t-shirts there anyway next next slide all right um so we've got a whole lot of talks coming up there are actually another two after this as well that have recently appeared but um yeah so Frank is going to talk about his ideas of using a vrf so a verifiable random function in conjunction um with a sequencer so to allow you to have Fair sequencing so to prove that you're doing Fair sequencing which seems pretty interesting um push protocol it's like a m me in um system Sandra is going to talk about um the the EIP 72 7251 um we've got another talk about um

well the economics of ZK on ethereum so what makes sense what doesn't make sense I can't remember for the life of me what the next one is in early may but then the miday one is um the people from layer zero are going to do a super deep diet on V2 um so yeah all right one more slide please okay if you're watching this here today live um please um you know watch it on YouTube or you know rewatch it and we'll make the slides available on YouTube as well um in the description um if you want to discuss um join the slack workor space um and if

you want to join the um these talks live so you can ask tricky questions of the speakers please feel free to you know like join up to the Meetup and RSVP it'll be great to have you along um so look Diego Thank you very much for putting together such a great talk and live demos as well that's um you know it always takes a lot of guts to do a demo so um thank you very much all right thank you everyone and we uh at CES we have a Discord right you just just Google caresi and you will find our Discord and you can go there there's a machine uh Channel where uh the low-level people like me live and

just ask a away all right that sounds awesome all right look um everyone have a great week or two actually three weeks um so talk to everyone later um bye- bye all right Care thank you very much for your talk byebye

Related talks