Cartesi 백서

Cartesi

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 5쪽 중 4쪽

by an emulator contract. Alice’s off-chain implementation must match this blockchain implementation down to the order in which the state accesses are logged. As a benefit of this restriction, the blockchain implementation can read and write to the state as if its whole contents were available. During its execution, the reference step function issues a sequence of state accesses to the memory manager. As long as the accesses match Alice’s log, everything works transparently. Formally, as the reference step function performs k0 accesses, the memory manager progressively updates mi =( m0i)0 to (m0i)1, (m0i)2, . . . , until it reaches (m0i)k0 . Access j, for j 2 {1,...,k 0}, contains the following information 1. An operation o0j for the access (read or write); 2. An address a0j for the access; 3. In case of writes, the word w0j to be written. As each access is processed, the memory manager: • Checks that j  k, o0j = oj , and a0j = aj ; • Checks that rj = read (m0i)j1,a j with the siblings. Then, for a read access, the memory manager: • Sets (m0i)j =( mi)j1; • Returns rj to the emulator contract. For writes, the memory manager: • Checks that w0j = wj ; • Sets (m0i)j = write(m0i)j1,a j ,w j ) with the siblings. At any point, if a check fails, Alice loses the dispute. If, however, k = k0 and mi+1 6=( m0i)k0 , Alice wins the dispute. 5.3 Cartesi Machines as one of many primitives Cartesi primitives are made available to Charlie through a functional programming interface. The goal is isolating the primitives from the idiosyncrasies of specific smart contract programming languages and blockchains. The syntax itself is not important. What matters is the semantics associated to each primitive. Charlie can use this interface to build expression DAGs that represent complex composite computations. The computations can involve several machines that exchange data with each other. An empty DAG is first initialized with a call to: dag =d a g ( ) Each DAG vertex corresponds to a primitive. A primitive’s inputs come from the outputs of its children vertices. The primitive’s output can in turn serve as input to one or more primitives. Primitives are divided into two categories. Disputable primitives behave as future values promised to Alice by Bob. Their outputs are set by Bob, and must be accepted or disputed by Alice. Disputable primitives can only appear as internal vertices in the expression DAG. They are the read, write, slice, splice, step, and compute primitives described in sections 5.1 and 5.2. Constant primitives have their outputs set by Charlie. They are are implicitly accepted by both Alice and Bob throughout their interac- tions with Charlie’s DApp. These primitives potentially represent large chunks of data and the availability of their contents has to be guaranteed by Charlie, possibly with help from our data availability primitives. Naturally, word, hash, string, and id literals are constants. Cartesi’s blob, resource, and machine primitives, described below, are also constant. Only constant primitives can appear as DAG leaves. Cartesi supported constant and future types are: constant ::= word | hash | string | id disputable ::= word-future | hash-future Constant primitives accept only constants as input. In contrast, disputable primitives accept both constants and disputables: word-type ::= word | word-future hash-type ::= hash | hash-future Constant primitives Blob primitives represent arbitrary binary data stored in the blockchain. The provided hash gives the output. It must match the root hash for a Merkle tree built from the data, padded with zeros to 264depth bytes: hash = dag :blob{ hash = hash , depth = word , data = string } Resource primitives describe files stored off-chain. Files are assumed to be available from the given uri. The download-size can be used to bound, a priori, the total data transfer requirements. Only the first range-length bytes of the file are considered. This bounds the memory required to map it into the machine state. The provided hash output must match the root hash for a Merkle tree built from the first range-length bytes of the corresponding file, padded with zeros or truncated to 264depth bytes: hash = dag :resource{ hash = hash , depth = word , range-length = word , download-size = word , uri = string } Machine primitives are used to describe Cartesi Machine states. The specification follows the scripting interface described in section 4.1. The only the difference is that backing files are given as paths. Instead, they are resource constants. The provided hash output must correspond to the root Merkle tree hash for the corresponding Cartesi Machine state: 10 V ersion 1.01 hash = dag :machine{ hash = hash , processor = processor , rom = rom , ram = ram , flash0 = drive , ... flash7 = drive , clint = clint , htif = htif } Disputable primitives The compute primitive executes a Cartesi Machine. The initial-state gives the value of m0 and steps the value of n, so that the future value is compute(m0,n ). Steps therefore bounds, a priori, the amount of computation required: hash-future = dag :compute{ initial-state = hash-type , steps = word-type } The Merkle tree manipulation primitives slice, splice, read, and write are available as: hash-future = dag :slice{ root = hash-type , address = word-type , depth = word-type } hash-future = dag :splice{ root = hash-type , address = word-type , depth = word-type , target = hash-type } word-future = dag :read{ root = hash-type , address = word-type } hash-future = dag :write{ root = hash-type , address = word-type , word = word-type } Cartesi also provides a variety of simple primitives that increase the expressive power of expressions. Disputes over such primitives can be settled within the blockchain directly from their inputs. Several binary operations on words have the signature: word-future = dag :bin-op (word-type , word-type ) and mirror the RISC-V ISA.They can be divided into arithmetic: add, sub, mul, mulh, mulhu, mulhsu, div, divu, rem, remu, sll, srl, sra; bitwise: or, and, xor; and comparisons: eq, ne, lt, ltu, ge, geu. Signed integers are represented by two’s complement. Boolean values are returned as words where 1 means true and 0 means false. Conversely, when a Boolean value is expected by a conditional, 0 is considered false and any other value is considered true. Conditionals are available as ternary if primitives whose output is set to if-true if condition is true, and if-false otherwise: word-future = dag :if{ condition = word-type , if-true = word-type , if-false = word-type } The hash primitive builds a 256-bit hash by the concatenation of its 64-bit component words: hash-future = dag :word4(word-type , word-type , word-type , word-type ) To help with Merkle tree construction, hashes can be built from words and from the concatenation of two hashes: hash-future = dag :keccak(word-type ) hash-future = dag :keccak(hash-type , hash-type ) For completeness, hashes can also be tested for equality and used as input for a conditional: word-future = dag :eq(hash-type , hash-type ) word-future = dag :neq(hash-type , hash-type ) hash-future = dag :if{ condition = word-type , if-true = hash-type , if-false = hash-type } DAG and vertex interfaces Charlie must define the identity of the players for the roles of Alice and Bob. Recall that Bob proposes a result and Alice can object or accept it: dag :proposing-role{id = id ,s t a k e= word } dag :objecting-role{id = id ,s t a k e= word } The stake argument gives the price for buying Alice’s or Bob’s position. It measures what is at stake for each of them depending on the results of the computation. Charlie sets this value to facilitate Alice and Bob to delegating their roles (section 6.2). Primitive creation functions return: vertex ::= constant | disputable A DAG may contain multiple disconnected sub-DAGs. To specify or retrieve the root vertex for the DAG, Charlie invokes: dag :root(vertex ) This value can be later obtained by anyone that calls: vertex = dag :root() The primitive for any vertex can be queried: primitive = vertex :primitive() primitive ::= word | hash | string | blob | resource | machine | read | write | slice | splice | add | sub | ... | geu | if | word4 | keccak | keccak-hh | eq-hh | if-hh Likewise, the children can be obtained by name or index: vertex = vertex :child-by-index(word ) vertex = vertex :child-by-name(string ) Together, the primitive and child functions enable the entire sub-DAG reachable from a vertex to be traversed. The DAGs and vertices can change state during their lifetimes: dag-state ::= undefined | proposed | accepted | objected | sustained | overruled vertex-state ::= undefined | proposed | accepted 11 V ersion 1.01 Constant vertices are always in the accepted state. At construction, the DAG and all its disputable vertices are in the undefined state. These states can be obtained from the DAG and vertex objects: dag-state = dag :state() vertex-state = vertex :state() Proposing the value of any vertex changes its state to proposed: vertex :propose-hash(hash ) vertex :propose-word(word ) To start a proposal, Bob first invokes: dag :start-proposal() Then, he proposes the output value for the root vertex. Finally, he completes the proposal with a call to: dag :finish-proposal() This changes the DAG to the proposed state. The value proposed for any vertex can be checked with a call to: word = vertex :proposed-word() hash = vertex :proposed-hash() To accept the proposed root for the DAG, Alice calls: dag :accept() This changes the DAG state to accepted. To object to the root value for the DAG, Alice invokes: dag :start-objection() and then proposes a new, distinct value for the root vertex. There are now two cases to consider. If all immediate children of the root vertex are in the accepted state, the dispute can be settled by the root primitive resolution protocol. To that end, Alice simply calls: dag :finish-objection() This changes the DAG to the objected state while the protocol is completed. If Alice succeeds, the DAG is changed to the sustained state. Otherwise, it is changed to the overruled state. If, however, there are any proposed or undefined children, she must first propose values for all vertices in the sub-DAG reachable from the root. Only then can she call: dag :finish-objection() This changes the DAG to the objected state. Bob must now find a vertex, accessible from the root, for which he accepts all inputs but objects to the output. To specify this vertex, Bob gives its path from the root. He also must specify a distinct value for its output: dag :defend-word(path , word ) dag :defend-hash(path , hash ) path ::= { vertex1, vertex2,. . . , vertexk} If the vertex is undefined, Alice’s dispute is declared ill-formed and Bob wins immediately. If the path is invalid, Bob’s defense is declared ill-formed and Alice wins immediately. Otherwise, the primitive dispute resolution protocol is engaged. With this setup, arbitrarily complex expressions behave just like disputable Cartesi primitives: they have agreed-upon inputs and come equipped with a dispute resolution procedure for their output. Figure 6: Expression DAG corresponding to the decryptor example. Constant vertices corresponding to literal values have been omitted for brevity. V ariable names are shown in gray. An example The following example illustrates the power of the expression DAG interface: d=d a g ( ) decryptor =l i b r a r y { hash = decryptor-machine-hash, dag = d } input =d : r e s o u r c e { hash = input-hash, depth = decryptor-flash-1-depth, uri = http://example.com/charlie/input.drive } key =d : b l o b { hash = password-hash, depth = decryptor-flash-2-depth, data = password } loaded-decryptor =d : s p l i c e { root = d:splice{ root = decryptor, address = decryptor-flash-1-addr, target = input }, address = decryptor-flash-2-addr target = key } executed-decryptor =d : c o m p u t e { initial-state = loaded-decryptor, steps = 10*2^32 } iflags =d : r e a d { root = executed-decryptor, address = machine-processor-iflags-addr, } tohost =d : r e a d { root = executed-decryptor, address = machine-htif-tohost-addr } halted =d : n e q ( d : a n d (iflags,1 ) ,0 ) done =d : e q ( d : s r l ( d : a n d (tohost,d : s r l ( - 1 ,1 6 ) ) ,1 ) ,0 ) success =d : a n d (halted, done) d:root(success) In the example, Charlie defines an expression DAG with root success. The implied computation starts with a decryptor machine, available from an off-line library (not shown). Charlie can install his own library of pre-defined machines in the Cartesi Node, or use a library of machine specifications created and stored remotely by another developer. Either way, the hash constant ensures machines that have been tampered with can be easily identified. Next, input and key flash devices are spliced into the decryptor machine. This loaded- decryptor is run for at most 2*2^32 steps, and results in an executed- decryptor. The corresponding state is then probed. Inspection of the processor’s iflags register tells if the machine is halted. The 12 V ersion 1.01 value stored in the payload of HTIF’s tohost register tells if the machine halted after it was done decrypting. Figure 6 shows the corresponding DAG, with literal values (i.e., strings, words, and hashes) omitted. Charlie oversees Alice’s and Bob’s interaction with the DAG and its vertices. After all, Charlie is responsible for the blockchain DApp component where these objects live. Charlie’s software acting on Alice or Bob’s behalf can issue events that trigger reactions from the Cartesi Nodes representing Alice and Bob. These reactions are also under Charlie’s control, since he is in charge of the off-chain DApp components installed in their nodes. He must, as usual, protect his DApp against attacks by rogue users. Cartesi simplifies part of this process by encapsulating access control in all DAG operations. 6 The Cartesi Node The Cartesi Node is the software and hardware infrastructure that hosts the off-chain components of Cartesi DApps. Each user that wishes to interact with a Cartesi DApp must have a Cartesi Node at his disposal. Cartesi Nodes will initially be made available as Docker containers to be run on a computer under the user’s responsibility. Future plans include their distribution as a multi-platform library that developers can link to an executable for users to install as a self-contained DApp. DApps can include native off-chain components that access the full storage and computational power of the hardware where the node is installed. In addition, all nodes contain a reference implementation of the Cartesi Machine that DApps can control to perform verifiable computations. Finally, nodes contain the infrastructure necessary for the interaction of off-chain and blockchain DApp components. 5 At the core level, the Cartesi platform gives DApp developers the freedom to combine native code, reproducible Cartesi Machines, and the blockchain’s API in any way they see fit. Given the fast pace in which novel applications for blockchain technology appear, this seems to be the only way to avoid restraining developers’ creativity. Nevertheless, we can foresee a variety of common tasks, challenges, and patterns that are likely to arise when using the Cartesi platform. With time, the Cartesi platform will encapsulate these into a set of higher-level interfaces built on top of the core [ Teixeira and Nehab 2019a]. The core includes only facilities for the automated execution and verification of Cartesi Machines. 6.1 Off-chain expression DAGs Off-chain DApp components need to interact with existing block- chain DAGs. After all, the computations implied by such DAGs must be performed off-chain within the Cartesi Node. These inter- actions are mediated by off-chain representations for DAGs, which can be automatically built from a blockchain instance with a call to: off-dag =o f f - d a g (dag ) Off-chain DAGs provide DApps with a high-level interface that completely encapsulates typical use cases. Bounds for the data transfer, memory, and computation requirements for the main sub-DAG can be obtained from: bounds = off-dag :bounds() bounds ::= { data = word, memory = word, compute = word } 5In the case of Ethereum, a light client. To download the data for all constant nodes in the main sub-DAG and verify the computed hashes match the declared hashes: download-status = off-dag :download{timeout = word } download-status ::= accepted | failed | timedout Once download is complete, all vertices in the main sub-DAG can be evaluated with a single call to: off-dag :evaluate() The upload method submits results back to the blockchain. It also isolates DApp developers from the details of any potential dispute: off-dag :upload{fee = word } The fee argument is used for role delegation (section 6.2). Let v be the root for the main sub-DAG. The upload method checks whether the role being played is proposing or objecting. The propos- ing role only acts if v is undefined in the blockchain. In that case, it proposes the value it obtained off-chain for v. The objecting role only acts if a value for v has been proposed to the blockchain. If the value matches what it obtained off-chain, it accepts the value. This is how the overwhelming majority of interactions will play out. If, however, the proposed value for v in the blockchain does not match the value computed off-chain, the objecting role starts a dispute. The ensuing interactions between the blockchain and the Cartesi Nodes of both proposing and objecting roles are automatically handled by Cartesi the platform. With the exception of the compute primitive, disputes can be settled right away. Compute primitives require multiple interactions with the blockchain. If a party cannot guarantee the responsiveness of his Cartesi Node throughout a dispute, he could lose by default judgement. To minimize this risk, Cartesi offers another convenience to DApp developers: the dispute delegation market. 6.2 Dispute delegation market A principal party can delegate potential disputes to a proxy by set- ting the fee argument of the upload method to a non-zero value. This causes Cartesi to advertise the disputed DAG in the dispute delegation market. The advertising principal is notified as soon as a proxy purchases a dispute. Proxy candidates own Cartesi Nodes on which Cartesi’s dispute proxy DApp has been enabled. Users of the proxy DApp are in the business of collecting fees for defending the interests of principal parties that are unwilling to conduct their own disputes. The proxy DApp download an advertised DAG, computes its value off-chain, and checks if it matches the value proposed to the block- chain by the role for sale. If so, it can purchase the role for the stake specified in the DAG. This amount will be returned if and only if the proxy wins the ensuing dispute. In that case, the proxy is also rewarded the fee. If the proxy fails in the dispute, the stake is instead sent to the principal party. Guarantees Cartesi guarantees that an honest party can always win any dispute in which it is involved. This is a strong guarantee, but it is also the only guarantee. In the absence of disputes, the value for the DAG is defined to be whatever the interested parties proposed and accepted. It is therefore perfectly reasonable to act on the accepted value, whether or not it is indeed the true result of the computation defined by the DAG. Any unsatisfied party has the responsibility of contesting the value. Disputed values, however, may or may not be useful. The situation is truly exceptional: At least one of the parties is being dishonest, perhaps even both are. The proper way forward must be decided 13 V ersion 1.01 by the DApp developer. One potentially useful bit of information is whether the objection was overruled or sustained. Note that even this bit is only true when at least one of the parties is honest. Whenever a principal party wishes to delegate a dispute
프로젝트로 돌아가기