|
1 | 1 | # Cartesi RISC-V Solidity Emulator |
2 | 2 |
|
3 | | -The Cartesi RISC-V Solidity Emulator is the on-chain host implementation of the Cartesi Machine Specification. The libraries and contracts are written in Solidity, and the testing scripts are written in Solidity (with the help of [Foundry](https://github.com/foundry-rs/foundry)). **_Do not recommend running `forge` command directly, as there are many scripts and templates working together. Please use `make` instead._** |
| 3 | +On-chain Solidity implementation of the [Cartesi Machine](https://github.com/cartesi/machine-emulator) state transition function. The core libraries (`UArchStep`, `UArchReset`, `SendCmioResponse`) are transpiled directly from the emulator's C++ sources to guarantee bit-exact equivalence. |
4 | 4 |
|
5 | | -For Cartesi's design to work, this implementation must have the exact transition function as the off-chain [Cartesi RISC-V Emulator](https://github.com/cartesi/machine-emulator), meaning that if given the same initial state (s[i]) both implementation's step functions should reach a bit by bit consistent state s[i + 1]. |
| 5 | +> Use `make` for all build and test operations -- code generation must run before compilation. |
6 | 6 |
|
7 | | -Since the cost of storing a full Cartesi Machine state within the blockchain is prohibitive, all machine states are represented in the blockchain as cryptographic hashes. The contents of those states and memory represented by those hashes are only known off-chain. |
| 7 | +## Quick start |
8 | 8 |
|
9 | | -Cartesi uses Merkle tree operations and properties to ensure that the blockchain has the ability to correctly verify a state transition without having full state-access. However, the RISC-V Solidity emulator abstracts these operations away and acts as if it knows the full contents of a machine state - it uses the Memory Manager interface to fetch or write any necessary words to memory. |
| 9 | +Set up your environment, then build and test: |
10 | 10 |
|
11 | | -## AccessLogs |
| 11 | + export EMULATOR_DIR=../machine-emulator # path to machine-emulator checkout |
| 12 | + export SED=gsed # macOS only (brew install gnu-sed) |
12 | 13 |
|
13 | | -The `AccessLogs.Context` struct is consumed by the RISC-V Solidity emulator as if the entire state content was available - since the off and on-chain emulators match down to the order in which accesses are logged. When a dispute arises, Alice packs her off-chain state access log referent to the disagreement step in an struct `AccessLogs.Context`, which will guide the execution of a `step` (i.e state transition function). |
14 | | - |
15 | | -The `AccessLogs` library implements the RISC-V Solidity emulator all necessary read and write operations. |
| 14 | + make submodules |
| 15 | + make build |
| 16 | + make test-all |
16 | 17 |
|
17 | | -It also makes sure that all accesses performed by the `step` function match the ones provided by Alice and are consistent with the Merkle proofs provided by her. If that is not the case, Alice loses the dispute. |
| 18 | +## Prerequisites |
18 | 19 |
|
19 | | -## Step function |
| 20 | + make env-check # verify everything is installed |
20 | 21 |
|
21 | | -`MetaStep.step` is the top-level state transition function, it is meant to take the machine from state s[i] to state[i + 1], using the `AccessLogs` as an assistant. It receives a `counter` and an `AccessLogs.Context` - the latter populated with the access log generated by the emulator off-chain - and returns a `UArchStepStatus` signaling the result of its execution. Internally it runs one micro-step via `UArchStep.step` and, every `1 << LOG2_CYCLES_TO_RESET` cycles, also runs `UArchReset.reset` to reset the micro-architecture state. |
| 22 | +- [Foundry](https://book.getfoundry.sh/) 1.4.3 |
| 23 | +- GNU Make >= 3.81 |
| 24 | +- Lua 5.4, LuaRocks, LPEG |
| 25 | +- GNU sed (gsed on macOS) |
| 26 | +- GPP >= 2.27 |
22 | 27 |
|
23 | | -During a `MetaStep.step` execution, every necessary read or write (be it to memory, registers etc) is processed and verified by the `AccessLogs`. |
| 28 | +<details> |
| 29 | +<summary>macOS (Homebrew)</summary> |
24 | 30 |
|
25 | | -## Execute Instruction |
| 31 | + brew install lua luarocks gpp gnu-sed |
| 32 | + luarocks install lpeg |
| 33 | + curl -L https://foundry.paradigm.xyz | bash |
| 34 | + foundryup -i v1.4.3 |
26 | 35 |
|
27 | | -The `UArchStep` library implements a single micro-architecture instruction step (`UArchStep.step`), and is invoked by `MetaStep.step`. It consists of the machine instruction logic, such as decoding, executing, opcode matching and etc. The Solidity implementation is converted from the Cpp implementation directly through a translator script. This is to assure that the implementations in two languages are identical. Yet the low level differences in two languages are wrapped in the Compatibility Layer. |
| 36 | +Add to your shell profile: |
28 | 37 |
|
29 | | -## Compatibility Layer |
| 38 | + eval "$(luarocks path)" |
30 | 39 |
|
31 | | -The `EmulatorCompat` contract is taking care of all the differences in the two implementations. Ranging from programming languages (Cpp versus Solidity) to architectural differences (RISC-V versus EVM). |
| 40 | +</details> |
32 | 41 |
|
33 | | -## Getting Started |
| 42 | +<details> |
| 43 | +<summary>macOS (MacPorts)</summary> |
34 | 44 |
|
35 | | -Run `make help` for a list of target options. Here are some of them: |
| 45 | + sudo port install lua54 luarocks gpp gsed |
| 46 | + luarocks install lpeg |
| 47 | + curl -L https://foundry.paradigm.xyz | bash |
| 48 | + foundryup -i v1.4.3 |
36 | 49 |
|
37 | | -``` |
38 | | -Cleaning targets: |
39 | | - clean - clean the cache artifacts and generated files |
40 | | -Generic targets: |
41 | | -* all - build solidity code. To build from a clean clone, run: make submodules all |
42 | | - build - build solidity code |
43 | | - generate-step - generate solidity-step code from cpp |
44 | | - generate-reset - generate solidity-reset code from cpp |
45 | | - generate-send-cmio-response - generate solidity-send-cmio-response code from cpp |
46 | | - generate-constants - generate solidity-constants code by querying the cartesi machine |
47 | | - generate-mock - generate mock library code |
48 | | - generate-prod - generate production library code |
49 | | - generate-replay - generate replay tests |
50 | | - pretest - download necessary files for tests |
51 | | - submodules - initialize and update git submodules |
52 | | - fmt - format solidity sources with forge fmt |
53 | | - test-all - test all |
54 | | - test-mock - test binary files with mock library |
55 | | - test-prod - test production code |
56 | | - test-replay - test log files |
57 | | -Coverage targets: |
58 | | - coverage-mock - generate coverage info for mock library tests |
59 | | - coverage-prod - generate coverage info for production code tests |
60 | | - coverage-report - aggregate coverage info and generate html report |
61 | | -``` |
| 50 | +Add to your shell profile: |
62 | 51 |
|
63 | | -### Prerequisite |
| 52 | + eval "$(luarocks path)" |
| 53 | + export SED=gsed |
64 | 54 |
|
65 | | -- Build [Cartesi docker image](https://github.com/cartesi/machine-emulator#getting-started) |
| 55 | +</details> |
66 | 56 |
|
67 | | -### Requirements |
| 57 | +<details> |
| 58 | +<summary>Linux (Ubuntu/Debian)</summary> |
68 | 59 |
|
69 | | -- Foundry 1.4.3 |
70 | | -- GNU Make >= 3.81 |
71 | | -- GNU sed >= 4.9 |
72 | | -- GPP >= 2.27 |
| 60 | + sudo apt install lua5.4 liblua5.4-dev luarocks gpp |
| 61 | + sudo luarocks install lpeg |
| 62 | + curl -L https://foundry.paradigm.xyz | bash |
| 63 | + foundryup -i v1.4.3 |
73 | 64 |
|
74 | | -Different version of tools maybe working but is not guaranteed. |
| 65 | +Add to your shell profile: |
75 | 66 |
|
76 | | -### Install |
| 67 | + eval "$(luarocks path)" |
77 | 68 |
|
78 | | -Install dependencies and build: |
| 69 | +</details> |
79 | 70 |
|
80 | | - make submodules all |
| 71 | +## Testing |
81 | 72 |
|
82 | | -### Run tests |
| 73 | + make test-transpiler # transpiler unit tests (fast) |
| 74 | + make test-mock # rv64i instruction tests against mock AccessLogs |
| 75 | + make test-prod # production AccessLogs integration tests |
| 76 | + make test-replay # replay 56 emulator step logs with full Merkle verification (~1 min) |
| 77 | + make test-all # all of the above |
83 | 78 |
|
84 | | -There are two types of tests that can be run on the Solidity Emulator. |
| 79 | +### Using local emulator test data |
85 | 80 |
|
86 | | -1. Load binary rv64i test programs in Solidity and verify the execution result |
87 | | -2. Collect step logs and proofs of a test program and replay them in Solidity, verify all accesses and proofs. |
| 81 | +By default, test data (binaries and JSON step logs) is downloaded from a |
| 82 | +GitHub release. When developing against a local emulator build, generate and |
| 83 | +use local test data instead: |
88 | 84 |
|
89 | | -Run all tests: |
| 85 | + # one-time: generate step logs in the emulator repo |
| 86 | + make -C $EMULATOR_DIR/tests test-generate-uarch-logs |
90 | 87 |
|
| 88 | + # use them here |
| 89 | + make local-dep |
91 | 90 | make test-all |
92 | 91 |
|
93 | | -(**_target test-replay is very time consuming_**) |
| 92 | +This ensures the constants and test data come from the same emulator build. |
94 | 93 |
|
95 | | -## Contributing |
| 94 | +### Coverage |
| 95 | + |
| 96 | + make coverage-mock |
| 97 | + make coverage-prod |
| 98 | + make coverage-report # html report in coverage/ |
| 99 | + |
| 100 | +## Code generation |
| 101 | + |
| 102 | +The build step runs these automatically; you only need them when |
| 103 | +iterating on a specific piece: |
| 104 | + |
| 105 | + make generate-step # uarch-step.cpp -> UArchStep.sol |
| 106 | + make generate-reset # uarch-reset-state.cpp -> UArchReset.sol |
| 107 | + make generate-send-cmio-response # send-cmio-response.cpp -> SendCmioResponse.sol |
| 108 | + make generate-constants # EmulatorConstants.sol (from emulator Lua bindings) |
| 109 | + make generate-mock # mock AccessLogs library |
| 110 | + make generate-prod # production AccessLogs library |
| 111 | + |
| 112 | +## Architecture |
| 113 | + |
| 114 | +This implementation must produce the exact same state transition as the |
| 115 | +off-chain emulator: given initial state s[i], both must reach s[i+1] bit for |
| 116 | +bit. Machine states live on-chain as Merkle tree root hashes; their contents |
| 117 | +are only known off-chain. Merkle proofs let the blockchain verify individual |
| 118 | +transitions without storing the full state. |
96 | 119 |
|
97 | | -Thank you for your interest in Cartesi! Head over to our [Contributing Guidelines](CONTRIBUTING.md) for instructions on how to sign our Contributors Agreement and get started with Cartesi! |
| 120 | +### Components |
| 121 | + |
| 122 | +**MetaStep** -- Entry point. Runs one micro-step via `UArchStep.step` and |
| 123 | +periodically resets the micro-architecture via `UArchReset.reset`. |
| 124 | + |
| 125 | +**UArchStep** -- One micro-architecture instruction step (transpiled from |
| 126 | +`uarch-step.cpp`). Decodes and executes RISC-V opcodes. |
| 127 | + |
| 128 | +**UArchReset** -- Resets micro-architecture state (transpiled from |
| 129 | +`uarch-reset-state.cpp`). |
| 130 | + |
| 131 | +**SendCmioResponse** -- Handles CMIO yield responses (transpiled from |
| 132 | +`send-cmio-response.cpp`). |
| 133 | + |
| 134 | +**AccessLogs** -- Provides Merkle-verified read/write access to machine state. |
| 135 | +During a dispute, the claimant supplies an access log with sibling hashes; |
| 136 | +every read and write is checked against the Merkle root. Inconsistencies mean |
| 137 | +the claimant loses. |
| 138 | + |
| 139 | +**EmulatorCompat** -- Bridges C++/Solidity differences (endianness, type |
| 140 | +widths, calling conventions). |
| 141 | + |
| 142 | +**EmulatorConstants** -- Addresses, sizes, and hashes auto-generated from the |
| 143 | +emulator's Lua bindings. |
| 144 | + |
| 145 | +## Contributing |
98 | 146 |
|
99 | | -Please note we have a [Code of Conduct](CODE_OF_CONDUCT.md), please follow it in all your interactions with the project. |
| 147 | +See [Contributing Guidelines](CONTRIBUTING.md) and our |
| 148 | +[Code of Conduct](CODE_OF_CONDUCT.md). |
100 | 149 |
|
101 | 150 | ## License |
102 | 151 |
|
103 | | -The machine-solidity-step repository and all contributions are licensed under |
104 | | -[APACHE 2.0](https://www.apache.org/licenses/LICENSE-2.0). Please review our [LICENSE](LICENSE) file. |
| 152 | +[Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0). See [LICENSE](LICENSE). |
0 commit comments