From b74329f9704ff95405d161d897cf86cdf9a824d3 Mon Sep 17 00:00:00 2001 From: Gabriel Coutinho de Paula Date: Mon, 6 Jan 2025 09:51:23 -0300 Subject: [PATCH] wip --- .dockerignore | 67 ++- Cargo.lock | 152 +++---- justfile | 13 +- .../cartesi-machine-sys/Cargo.toml | 3 + .../cartesi-machine-sys/build.rs | 389 ++++++++++-------- prt/measure_constants/Makefile | 2 +- prt/tests/rollups/dave/node.lua | 2 +- prt/tests/rollups/justfile | 3 + prt/tests/rollups/prt_rollups.lua | 222 +++++----- test/Dockerfile | 108 ++++- test/programs/build_anvil_state.sh | 5 +- 11 files changed, 568 insertions(+), 398 deletions(-) diff --git a/.dockerignore b/.dockerignore index eac6e322..7accd31e 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,22 +1,31 @@ +**/.git +**/.github +**/.venv +**/.vscode +**/.DS_Store + **/target/ + +**/_state/ **/snapshots/ **/compute_data/ **/rollups_data/ -**/.git -**/.github -**/.venv -**/.vscode +**/out/ +**/cache/ + +test/programs/linux.bin +test/programs/rootfs.ext2 +test/programs/**/machine-image/ +test/programs/**/addresses +test/programs/**/anvil_state.json +test/programs/**/_anvil.log -cartesi-rollups/contracts/**/daveconsensus.rs +cartesi-rollups/contracts/bindings-rs/src/contract/ +prt/contracts/bindings-rs/src/contract/ prt/tests/compute/outputs/ prt/tests/compute/pixels/ -prt/contracts/**/leaftournament.rs -prt/contracts/**/nonleaftournament.rs -prt/contracts/**/nonroottournament.rs -prt/contracts/**/roottournament.rs -prt/contracts/**/tournament.rs machine/emulator/**/*.o machine/emulator/**/*.swp @@ -30,12 +39,42 @@ machine/emulator/**/*.bin machine/emulator/**/*.md machine/emulator/**/*.deb +machine/emulator/src/*.pb.h +machine/emulator/src/*.pb.cc +machine/emulator/src/*.ext2 +machine/emulator/src/*.bin +machine/emulator/src/*.gcno +machine/emulator/src/*.gcov +machine/emulator/src/*.gcda +machine/emulator/src/*.profraw +machine/emulator/src/compile_flags.txt +machine/emulator/src/coverage* +machine/emulator/src/jsonrpc-discover.cpp +machine/emulator/src/machine-c-version.h + machine/emulator/build machine/emulator/third-party/downloads machine/emulator/src/cartesi-machine-client machine/emulator/src/cartesi-machine-server machine/emulator/src/cartesi-machine-hash -machine/emulator/doc/html -machine/emulator/doc/api.md -machine/emulator/doc/html/ -machine/emulator/doc/xml/ +machine/emulator/doc/ + +machine/emulator/**/*.clang-tidy +machine/emulator/**/*.a +machine/emulator/**/*.lib +machine/emulator/**/*.wasm +machine/emulator/**/*.tar.gz +machine/emulator/**/*.raw +machine/emulator/**/*.so +machine/emulator/**/*.dll +machine/emulator/**/*.exe +machine/emulator/**/*.dylib +machine/emulator/**/*.dtb +machine/emulator/**/*.tmp +machine/emulator/**/*.ld +machine/emulator/**/*.elf +machine/emulator/**/*.bin + +machine/emulator/uarch/uarch-pristine-hash.c +machine/emulator/uarch/uarch-pristine-ram.c +machine/emulator/uarch/compute-uarch-pristine-hash diff --git a/Cargo.lock b/Cargo.lock index 67f45ca1..c850b64f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -184,9 +184,9 @@ dependencies = [ [[package]] name = "alloy-core" -version = "0.8.15" +version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c618bd382f0bc2ac26a7e4bfae01c9b015ca8f21b37ca40059ae35a7e62b3dc6" +checksum = "5e3fdddfc89197319b1be19875a70ced62a72bebb67e2276dad688cd59f40e70" dependencies = [ "alloy-dyn-abi", "alloy-json-abi", @@ -197,9 +197,9 @@ dependencies = [ [[package]] name = "alloy-dyn-abi" -version = "0.8.15" +version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41056bde53ae10ffbbf11618efbe1e0290859e5eab0fe9ef82ebdb62f12a866f" +checksum = "b0d2ea4d7f220a19c1f8c98822026d1d26a4b75a72e1a7308d02bab1f77c9a00" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -307,9 +307,9 @@ dependencies = [ [[package]] name = "alloy-json-abi" -version = "0.8.15" +version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c357da577dfb56998d01f574d81ad7a1958d248740a7981b205d69d65a7da404" +checksum = "e79c6b4bcc1067a7394b5b2aec7da1bd829c8c476b796c73eb14da34392a07a7" dependencies = [ "alloy-primitives", "alloy-sol-type-parser", @@ -435,9 +435,9 @@ dependencies = [ [[package]] name = "alloy-primitives" -version = "0.8.15" +version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6259a506ab13e1d658796c31e6e39d2e2ee89243bcc505ddc613b35732e0a430" +checksum = "0540fd0355d400b59633c27bd4b42173e59943f28e9d3376b77a24771d432d04" dependencies = [ "alloy-rlp", "bytes", @@ -555,7 +555,7 @@ checksum = "5a833d97bf8a5f0f878daf2c8451fff7de7f9de38baa5a45d936ec718d81255a" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -734,23 +734,23 @@ dependencies = [ [[package]] name = "alloy-sol-macro" -version = "0.8.15" +version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9d64f851d95619233f74b310f12bcf16e0cbc27ee3762b6115c14a84809280a" +checksum = "c6d1a14b4a9f6078ad9132775a2ebb465b06b387d60f7413ddc86d7bf7453408" dependencies = [ "alloy-sol-macro-expander", "alloy-sol-macro-input", "proc-macro-error2", "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] name = "alloy-sol-macro-expander" -version = "0.8.15" +version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bf7ed1574b699f48bf17caab4e6e54c6d12bc3c006ab33d58b1e227c1c3559f" +checksum = "4436b4b96d265eb17daea26eb31525c3076d024d10901e446790afbd2f7eeaf5" dependencies = [ "alloy-json-abi", "alloy-sol-macro-input", @@ -760,16 +760,16 @@ dependencies = [ "proc-macro-error2", "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", "syn-solidity", "tiny-keccak", ] [[package]] name = "alloy-sol-macro-input" -version = "0.8.15" +version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c02997ccef5f34f9c099277d4145f183b422938ed5322dc57a089fe9b9ad9ee" +checksum = "e5f58698a18b96faa8513519de112b79a96010b4ff84264ce54a217c52a8e98b" dependencies = [ "alloy-json-abi", "const-hex", @@ -778,15 +778,15 @@ dependencies = [ "proc-macro2", "quote", "serde_json", - "syn 2.0.91", + "syn 2.0.94", "syn-solidity", ] [[package]] name = "alloy-sol-type-parser" -version = "0.8.15" +version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce13ff37285b0870d0a0746992a4ae48efaf34b766ae4c2640fa15e5305f8e73" +checksum = "1f3d6d2c490f650c5abd65a9a583b09a8c8931c265d3a55b18a8e349dd6d9d84" dependencies = [ "serde", "winnow", @@ -794,9 +794,9 @@ dependencies = [ [[package]] name = "alloy-sol-types" -version = "0.8.15" +version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1174cafd6c6d810711b4e00383037bdb458efc4fe3dbafafa16567e0320c54d8" +checksum = "c766e4979fc19d70057150befe8e3ea3f0c4cbc6839b8eaaa250803451692305" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -876,9 +876,9 @@ dependencies = [ [[package]] name = "alloy-trie" -version = "0.7.7" +version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e428104b2445a4f929030891b3dbf8c94433a8349ba6480946bf6af7975c2f6" +checksum = "6917c79e837aa7b77b7a6dae9f89cbe15313ac161c4d3cfaf8909ef21f3d22d8" dependencies = [ "alloy-primitives", "alloy-rlp", @@ -1086,7 +1086,7 @@ checksum = "3b43422f69d8ff38f95f1b2bb76517c91589a924d1559a0e935d7c8ce0274c11" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -1108,7 +1108,7 @@ checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -1119,7 +1119,7 @@ checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -1130,7 +1130,7 @@ checksum = "3c87f3f15e7794432337fc718554eaa4dc8f04c9677a950ffe366f20a162ae42" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -1191,7 +1191,7 @@ dependencies = [ "regex", "rustc-hash 1.1.0", "shlex", - "syn 2.0.91", + "syn 2.0.94", "which", ] @@ -1392,9 +1392,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.5" +version = "1.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c31a0499c1dc64f458ad13872de75c0eb7e3fdb0e67964610c914b034fc5956e" +checksum = "8d6dbb628b8f8555f86d0323c2eb39e3ec81901f4b83e091db8a6a76d316a333" dependencies = [ "shlex", ] @@ -1462,7 +1462,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -1628,7 +1628,7 @@ checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", "unicode-xid", ] @@ -1661,7 +1661,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -1908,7 +1908,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -1979,9 +1979,9 @@ checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" [[package]] name = "glob" -version = "0.3.1" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" [[package]] name = "group" @@ -2310,7 +2310,7 @@ checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -2351,7 +2351,7 @@ checksum = "a0eb5a3343abf848c0984fe4604b2b105da9539376e24fc0a3b0007411ae4fd9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -2664,14 +2664,14 @@ checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] name = "nybbles" -version = "0.3.0" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55a62e678a89501192cc5ebf47dcbc656b608ae5e1c61c9251fe35230f119fe3" +checksum = "a3409fc85ac27b27d971ea7cd1aabafd2eefa6de7e481c8d4f707225c117e81a" dependencies = [ "alloy-rlp", "const-hex", @@ -2718,7 +2718,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -2828,7 +2828,7 @@ checksum = "3c0f5fad0874fc7abcd4d750e76917eaebbecaa2c20bde22e1dbeeba8beb758c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -2875,7 +2875,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033" dependencies = [ "proc-macro2", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -2917,7 +2917,7 @@ dependencies = [ "proc-macro-error-attr2", "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -3102,9 +3102,9 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "reqwest" -version = "0.12.9" +version = "0.12.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a77c62af46e79de0a562e1a9849205ffcb7fc1238876e9bd743357570e04046f" +checksum = "43e734407157c3c2034e0258f5e4473ddb361b1e85f95a66690d67264d7cd1da" dependencies = [ "base64", "bytes", @@ -3137,6 +3137,7 @@ dependencies = [ "tokio", "tokio-native-tls", "tokio-rustls", + "tower", "tower-service", "url", "wasm-bindgen", @@ -3408,9 +3409,9 @@ dependencies = [ [[package]] name = "rustversion" -version = "1.0.18" +version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e819f2bc632f285be6d7cd36e25940d45b2391dd6d9b939e79de557f7014248" +checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" [[package]] name = "rusty-fork" @@ -3519,22 +3520,22 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.216" +version = "1.0.217" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e" +checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.216" +version = "1.0.217" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e" +checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -3709,7 +3710,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -3731,9 +3732,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.91" +version = "2.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d53cbcb5a243bd33b7858b1d7f4aca2153490815872d86d955d6ea29f743c035" +checksum = "987bc0be1cdea8b10216bd06e2ca407d40b9543468fafd3ddfb02f36e77f71f3" dependencies = [ "proc-macro2", "quote", @@ -3742,14 +3743,14 @@ dependencies = [ [[package]] name = "syn-solidity" -version = "0.8.15" +version = "0.8.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "219389c1ebe89f8333df8bdfb871f6631c552ff399c23cac02480b6088aad8f0" +checksum = "c74af950d86ec0f5b2ae2d7f1590bbfbcf4603a0a15742d8f98132ac4fe3efd4" dependencies = [ "paste", "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -3769,7 +3770,7 @@ checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -3817,7 +3818,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -3828,7 +3829,7 @@ checksum = "7b50fa271071aae2e6ee85f842e2e28ba8cd2c5fb67f11fcb1fd70b276f9e7d4" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -3900,7 +3901,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -3975,6 +3976,7 @@ dependencies = [ "futures-util", "pin-project-lite", "sync_wrapper", + "tokio", "tower-layer", "tower-service", ] @@ -4010,7 +4012,7 @@ checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -4168,7 +4170,7 @@ dependencies = [ "log", "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", "wasm-bindgen-shared", ] @@ -4203,7 +4205,7 @@ checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -4383,9 +4385,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.6.20" +version = "0.6.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b" +checksum = "e6f5bb5257f2407a5425c6e749bfd9692192a73e70a6060516ac04f889087d68" dependencies = [ "memchr", ] @@ -4431,7 +4433,7 @@ checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", "synstructure", ] @@ -4453,7 +4455,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -4473,7 +4475,7 @@ checksum = "595eed982f7d355beb85837f651fa22e90b3c044842dc7f2c2842c086f295808" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", "synstructure", ] @@ -4494,7 +4496,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] [[package]] @@ -4516,5 +4518,5 @@ checksum = "6eafa6dfb17584ea3e2bd6e76e0cc15ad7af12b09abdd1ca55961bed9b1063c6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.91", + "syn 2.0.94", ] diff --git a/justfile b/justfile index eaa27c4b..eacb1ccd 100644 --- a/justfile +++ b/justfile @@ -22,12 +22,15 @@ check-rust-workspace: bind cargo check test-rust-workspace: bind cargo test -build-rust-workspace: bind - cargo build -build-release-rust-workspace: bind - cargo build --release +build-rust-workspace *ARGS: bind + cargo build {{ARGS}} +build-release-rust-workspace *ARGS: bind + cargo build --release {{ARGS}} build: build-smart-contracts build-prt bind build-rust-workspace +clean-emulator: + make -C machine/emulator clean depclean distclean + hello: - echo "Hello" + echo $(echo "Hello") diff --git a/machine/rust-bindings/cartesi-machine-sys/Cargo.toml b/machine/rust-bindings/cartesi-machine-sys/Cargo.toml index fbb4eb3e..363ded2b 100644 --- a/machine/rust-bindings/cartesi-machine-sys/Cargo.toml +++ b/machine/rust-bindings/cartesi-machine-sys/Cargo.toml @@ -23,6 +23,9 @@ build_uarch = [] # requires setting env vars UARCH_PRISTINE_HASH_PATH and UARCH_PRISTINE_RAM_PATH copy_uarch = [] +# requires setting env vars LIBCARTESI_PATH and INCLUDECARTESI_PATH +external_cartesi = [] + default = ["download_uarch"] diff --git a/machine/rust-bindings/cartesi-machine-sys/build.rs b/machine/rust-bindings/cartesi-machine-sys/build.rs index 5dda2d56..b484ea6e 100644 --- a/machine/rust-bindings/cartesi-machine-sys/build.rs +++ b/machine/rust-bindings/cartesi-machine-sys/build.rs @@ -1,28 +1,38 @@ -use std::{ - env, fs, - path::{Path, PathBuf}, - process::{Command, Stdio}, -}; - -#[cfg(all(feature = "build_uarch", feature = "copy_uarch",))] -compile_error!("Features `build_uarch` and `copy_uarch` are mutually exclusive"); - -#[cfg(all(feature = "build_uarch", feature = "download_uarch"))] -compile_error!("Features `build_uarch` and `download_uarch` are mutually exclusive"); - -#[cfg(all(feature = "copy_uarch", feature = "download_uarch"))] -compile_error!("Features `copy_uarch`, and `download_uarch` are mutually exclusive"); - -#[cfg(not(any( - feature = "copy_uarch", - feature = "download_uarch", - feature = "build_uarch" -)))] -compile_error!("At least one of `build_uarch`, `copy_uarch`, and `download_uarch` must be set"); - -use bytes::Bytes; -use std::fs::OpenOptions; -use std::io::{self, Read, Write}; +// (c) Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) + +use std::{env, path::PathBuf, process::Command}; + +mod feature_checks { + #[cfg(all(feature = "build_uarch", feature = "copy_uarch",))] + compile_error!("Features `build_uarch` and `copy_uarch` are mutually exclusive"); + + #[cfg(all(feature = "build_uarch", feature = "download_uarch"))] + compile_error!("Features `build_uarch` and `download_uarch` are mutually exclusive"); + + #[cfg(all(feature = "copy_uarch", feature = "download_uarch"))] + compile_error!("Features `copy_uarch`, and `download_uarch` are mutually exclusive"); + + #[cfg(not(any( + feature = "copy_uarch", + feature = "download_uarch", + feature = "build_uarch", + feature = "external_cartesi", + )))] + compile_error!("At least one of `build_uarch`, `copy_uarch`, `download_uarch`, and `external_cartesi` must be set"); + + // #[cfg(all( + // feature = "external_cartesi", + // any( + // feature = "copy_uarch", + // feature = "download_uarch", + // feature = "build_uarch" + // ) + // ))] + // compile_error!( + // "`external_cartesi` is mutually exclusive with `build_uarch`, `copy_uarch`, or `download_uarch`" + // ); +} fn main() { let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); @@ -33,77 +43,24 @@ fn main() { // path. .canonicalize() .expect("cannot canonicalize path"); - let libdir_path = machine_dir_path.join("src"); - // clean build artifacts and start from scratch + // Clean build artifacts and start from scratch // clean(&machine_dir_path); - // - // Get uarch - // - + // tell Cargo where to look for libraries cfg_if::cfg_if! { - if #[cfg(feature = "build_uarch")] { - () - } else if #[cfg(feature = "copy_uarch")] { - let uarch_path = machine_dir_path.join("uarch"); - copy_uarch(&uarch_path) - } else if #[cfg(feature = "download_uarch")] { - download_uarch(&machine_dir_path); + if #[cfg(feature = "external_cartesi")] { + let libpath = + env::var("LIBCARTESI_PATH") + .map(PathBuf::from) + .unwrap_or_else(|_| machine_dir_path.join("src")); + println!("cargo:rustc-link-search={}", libpath.to_str().unwrap()); } else { - panic!("Internal error, no way specified to get uarch"); + build_cm::build(&machine_dir_path, &out_path); + println!("cargo:rustc-link-search={}", out_path.to_str().unwrap()); } } - // - // Build and link emulator - // - - // build dependencies - Command::new("make") - .args(["submodules"]) - .current_dir(&machine_dir_path) - .status() - .expect("Failed to run setup `make submodules`"); - Command::new("make") - .args(["bundle-boost"]) - .current_dir(&machine_dir_path) - .status() - .expect("Failed to run `make bundle-boost`"); - - // build `libcartesi.a`, release, no `libslirp` - Command::new("make") - .args([ - "-C", - "src", - "release=yes", - "slirp=no", - "libcartesi.a", - // "libcartesi_jsonrpc.a", - ]) - .current_dir(&machine_dir_path) - .status() - .expect("Failed to build `libcartesi.a` and/or `libcartesi_jsonrpc.a`"); - - // copy `libcartesi.a` to OUT_DIR - let libcartesi_path = libdir_path.join("libcartesi.a"); - let libcartesi_new_path = out_path.join("libcartesi.a"); - fs::copy(&libcartesi_path, &libcartesi_new_path).unwrap_or_else(|_| { - panic!( - "Failed to copy `libcartesi.a` {:?} to OUT_DIR {:?}", - libcartesi_path, libcartesi_new_path - ) - }); - - // copy `libcartesi_jsonrpc.a` to OUT_DIR - // let libcartesi_jsonrpc_path = libdir_path.join("libcartesi_jsonrpc.a"); - // let libcartesi_jsonrpc_new_path = out_path.join("libcartesi_jsonrpc.a"); - // fs::copy(libcartesi_jsonrpc_path, libcartesi_jsonrpc_new_path) - // .expect("Failed to move `libcartesi_jsonrpc.a` to OUT_DIR"); - - // tell Cargo where to look for libraries - println!("cargo:rustc-link-search={}", out_path.to_str().unwrap()); - // static link cfg_if::cfg_if! { if #[cfg(feature = "remote_machine")] { @@ -117,21 +74,34 @@ fn main() { // Generate bindings // + #[allow(clippy::needless_late_init)] + let include_path; + cfg_if::cfg_if! { + if #[cfg(feature = "external_cartesi")] { + include_path = env::var("INCLUDECARTESI_PATH") + .map(PathBuf::from) + .unwrap_or_else(|_| machine_dir_path.join("src")); + + } else { + include_path = machine_dir_path.join("src"); + } + }; + // machine api let machine_bindings = bindgen::Builder::default() - .header(libdir_path.join("machine-c-api.h").to_str().unwrap()) + .header(include_path.join("machine-c-api.h").to_str().unwrap()) .generate() .expect("Unable to generate machine bindings"); // htif constants let htif = bindgen::Builder::default() - .header(libdir_path.join("htif-defines.h").to_str().unwrap()) + .header(include_path.join("htif-defines.h").to_str().unwrap()) .generate() .expect("Unable to generate htif bindings"); // pma constants let pma = bindgen::Builder::default() - .header(libdir_path.join("pma-defines.h").to_str().unwrap()) + .header(include_path.join("pma-defines.h").to_str().unwrap()) .generate() .expect("Unable to generate pma bindings"); @@ -154,95 +124,174 @@ fn main() { println!("cargo::rerun-if-env-changed=UARCH_PRISTINE_RAM_PATH"); } -#[cfg(feature = "copy_uarch")] -fn copy_uarch(uarch_path: &PathBuf) { - let uarch_pristine_hash_path = - env::var("UARCH_PRISTINE_HASH_PATH").expect("`UARCH_PRISTINE_HASH_PATH` not set"); - let uarch_pristine_ram_path = - env::var("UARCH_PRISTINE_RAM_PATH").expect("`UARCH_PRISTINE_RAM_PATH` not set"); - - fs::copy( - uarch_pristine_hash_path, - uarch_path.join("uarch-pristine-hash.c").to_str().unwrap(), - ) - .expect("Failed to move `uarch-pristine-hash.c` to `uarch/`"); - - fs::copy( - uarch_pristine_ram_path, - uarch_path.join("uarch-pristine-ram.c").to_str().unwrap(), - ) - .expect("Failed to move `uarch-pristine-ram.c` to `uarch/`"); -} - -#[cfg(feature = "download_uarch")] -fn download_uarch(machine_dir_path: &Path) { - // apply git patche for 0.18.1 - let patch_file = machine_dir_path.join("add-generated-files.diff"); +#[cfg(not(feature = "external_cartesi"))] +mod build_cm { + use std::{fs, path::Path, process::Command}; + + pub fn build(machine_dir_path: &Path, out_path: &Path) { + // Get uarch + cfg_if::cfg_if! { + if #[cfg(feature = "build_uarch")] { + () + } else if #[cfg(feature = "copy_uarch")] { + let uarch_path = machine_dir_path.join("uarch"); + copy_uarch::copy(&uarch_path) + } else if #[cfg(feature = "download_uarch")] { + download_uarch::download(machine_dir_path); + } else { + panic!("Internal error, no way specified to get uarch"); + } + } - download_git_patch(&patch_file, "v0.18.1"); - apply_git_patch(&patch_file, machine_dir_path); -} + // Build and link emulator + + // build dependencies + Command::new("make") + .args(["submodules"]) + .current_dir(machine_dir_path) + .status() + .expect("Failed to run setup `make submodules`"); + Command::new("make") + .args(["bundle-boost"]) + .current_dir(machine_dir_path) + .status() + .expect("Failed to run `make bundle-boost`"); + + // build `libcartesi.a` and `libcartesi_jsonrpc.a`, release, no `libslirp` + Command::new("make") + .args([ + "-C", + "src", + "release=yes", + "slirp=no", + "libcartesi.a", + "libcartesi_jsonrpc.a", + ]) + .current_dir(machine_dir_path) + .status() + .expect("Failed to build `libcartesi.a` and/or `libcartesi_jsonrpc.a`"); + + // copy `libcartesi.a` to OUT_DIR + let libcartesi_path = machine_dir_path.join("src").join("libcartesi.a"); + let libcartesi_new_path = out_path.join("libcartesi.a"); + fs::copy(&libcartesi_path, &libcartesi_new_path).unwrap_or_else(|_| { + panic!( + "Failed to copy `libcartesi.a` {:?} to OUT_DIR {:?}", + libcartesi_path, libcartesi_new_path + ) + }); + + // copy `libcartesi_jsonrpc.a` to OUT_DIR + let libcartesi_jsonrpc_path = machine_dir_path.join("src").join("libcartesi_jsonrpc.a"); + let libcartesi_jsonrpc_new_path = out_path.join("libcartesi_jsonrpc.a"); + fs::copy(libcartesi_jsonrpc_path, libcartesi_jsonrpc_new_path) + .expect("Failed to move `libcartesi_jsonrpc.a` to OUT_DIR"); + } -fn download_git_patch(patch_file: &Path, target_tag: &str) { - let emulator_git_url = "https://github.com/cartesi/machine-emulator"; + #[cfg(feature = "copy_uarch")] + mod copy_uarch { + use std::{env, fs, path::Path}; + + fn copy(uarch_path: &Path) { + let uarch_pristine_hash_path = + env::var("UARCH_PRISTINE_HASH_PATH").expect("`UARCH_PRISTINE_HASH_PATH` not set"); + let uarch_pristine_ram_path = + env::var("UARCH_PRISTINE_RAM_PATH").expect("`UARCH_PRISTINE_RAM_PATH` not set"); + + fs::copy( + uarch_pristine_hash_path, + uarch_path.join("uarch-pristine-hash.c").to_str().unwrap(), + ) + .expect("Failed to move `uarch-pristine-hash.c` to `uarch/`"); + + fs::copy( + uarch_pristine_ram_path, + uarch_path.join("uarch-pristine-ram.c").to_str().unwrap(), + ) + .expect("Failed to move `uarch-pristine-ram.c` to `uarch/`"); + } + } - let patch_url = format!( - "{}/releases/download/{}/add-generated-files.diff", - emulator_git_url, target_tag, - ); + #[cfg(feature = "download_uarch")] + mod download_uarch { + use bytes::Bytes; + use std::{ + fs::{self, OpenOptions}, + io::{self, Read, Write}, + path::Path, + process::{Command, Stdio}, + }; + + pub fn download(machine_dir_path: &Path) { + // apply git patche for 0.18.1 + let patch_file = machine_dir_path.join("add-generated-files.diff"); + + download_git_patch(&patch_file, "v0.18.1"); + apply_git_patch(&patch_file, machine_dir_path); + } - // get - let diff_data = reqwest::blocking::get(patch_url) - .expect("error downloading diff of generated files") - .bytes() - .expect("error getting diff request body"); + fn download_git_patch(patch_file: &Path, target_tag: &str) { + let emulator_git_url = "https://github.com/cartesi/machine-emulator"; - // write to file - write_bytes_to_file(patch_file.to_str().unwrap(), diff_data) - .expect("failed to write `add-generated-files.diff`"); -} + let patch_url = format!( + "{}/releases/download/{}/add-generated-files.diff", + emulator_git_url, target_tag, + ); -fn apply_git_patch(patch_file: &Path, target_dir: &Path) { - // Open the patch file - let mut patch = fs::File::open(patch_file).expect("fail to open patch file"); - - // Create a command to run `patch -Np0` - let mut cmd = Command::new("patch") - .arg("-Np0") - .stdin(Stdio::piped()) - .current_dir(target_dir) - .spawn() - .expect("fail to spawn patch command"); - - // Write the contents of the patch file to the command's stdin - if let Some(ref mut stdin) = cmd.stdin { - let mut buffer = Vec::new(); - patch - .read_to_end(&mut buffer) - .expect("fail to read patch content"); - stdin - .write_all(&buffer) - .expect("fail to write patch to pipe"); - } + // get + let diff_data = reqwest::blocking::get(patch_url) + .expect("error downloading diff of generated files") + .bytes() + .expect("error getting diff request body"); - // Wait for the command to complete - let status = cmd.wait().expect("fail to wait for patch command"); + // write to file + write_bytes_to_file(patch_file.to_str().unwrap(), diff_data) + .expect("failed to write `add-generated-files.diff`"); + } - if !status.success() { - eprintln!("Patch command failed with status: {:?}", status); - } -} + fn apply_git_patch(patch_file: &Path, target_dir: &Path) { + // Open the patch file + let mut patch = fs::File::open(patch_file).expect("fail to open patch file"); + + // Create a command to run `patch -Np0` + let mut cmd = Command::new("patch") + .arg("-Np0") + .stdin(Stdio::piped()) + .current_dir(target_dir) + .spawn() + .expect("fail to spawn patch command"); + + // Write the contents of the patch file to the command's stdin + if let Some(ref mut stdin) = cmd.stdin { + let mut buffer = Vec::new(); + patch + .read_to_end(&mut buffer) + .expect("fail to read patch content"); + stdin + .write_all(&buffer) + .expect("fail to write patch to pipe"); + } + + // Wait for the command to complete + let status = cmd.wait().expect("fail to wait for patch command"); + + if !status.success() { + eprintln!("Patch command failed with status: {:?}", status); + } + } -fn write_bytes_to_file(path: &str, data: Bytes) -> io::Result<()> { - let mut file = OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(path) - .unwrap_or_else(|_| panic!("failed to open file {}", path)); + fn write_bytes_to_file(path: &str, data: Bytes) -> io::Result<()> { + let mut file = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(path) + .unwrap_or_else(|_| panic!("failed to open file {}", path)); - file.write_all(&data)?; - file.flush() // Ensure all data is written to disk + file.write_all(&data)?; + file.flush() // Ensure all data is written to disk + } + } } #[allow(dead_code)] diff --git a/prt/measure_constants/Makefile b/prt/measure_constants/Makefile index 145019e2..de7da1aa 100644 --- a/prt/measure_constants/Makefile +++ b/prt/measure_constants/Makefile @@ -1,4 +1,4 @@ -LIBNAME = chronos + LUADIR=$(shell pkg-config --cflags-only-I lua) COPT = -O2 -DNDEBUG diff --git a/prt/tests/rollups/dave/node.lua b/prt/tests/rollups/dave/node.lua index b08cb878..6b859c9b 100644 --- a/prt/tests/rollups/dave/node.lua +++ b/prt/tests/rollups/dave/node.lua @@ -5,7 +5,7 @@ local function start_dave_node(machine_path, db_path, sleep_duration, verbosity, [[sh -c "echo $$ ; exec env MACHINE_PATH='%s' STATE_DIR='%s' \ SLEEP_DURATION=%d RUST_BACKTRACE='%s' \ RUST_LOG='none',cartesi_prt_core='%s',rollups_compute_runner='%s',rollups_epoch_manager='%s' \ - cargo run --bin dave-rollups > dave.log 2>&1"]], + ../../target/debug/dave-rollups > dave.log 2>&1"]], machine_path, db_path, sleep_duration, trace_level, verbosity, verbosity, verbosity) local reader = io.popen(cmd) diff --git a/prt/tests/rollups/justfile b/prt/tests/rollups/justfile index c6b688a1..c4fe986f 100644 --- a/prt/tests/rollups/justfile +++ b/prt/tests/rollups/justfile @@ -1,5 +1,8 @@ +# run PRT rollups echo test test-echo: + rm -rf _state MACHINE_PATH="../../../test/programs/echo" lua prt_rollups.lua +# read logs from PRT Rollups node, run in separate terminal after `test-echo` read-node-logs: tail -f dave.log diff --git a/prt/tests/rollups/prt_rollups.lua b/prt/tests/rollups/prt_rollups.lua index 1f90ec17..6e521a88 100755 --- a/prt/tests/rollups/prt_rollups.lua +++ b/prt/tests/rollups/prt_rollups.lua @@ -55,119 +55,119 @@ local ROOT_LEAFS_QUERY = [[sqlite3 ./_state/compute_path/%s/db 'select level,base_cycle,compute_leaf_index,repetitions,HEX(compute_leaf) from compute_leafs where level=0 ORDER BY compute_leaf_index ASC']] local function build_root_commitment_from_db(machine_path, root_tournament) - local builder = MerkleBuilder:new() - local machine = Machine:new_from_path(machine_path) - local initial_state = machine:state() - - local handle = io.popen(string.format(ROOT_LEAFS_QUERY, root_tournament)) - assert(handle) - local rows = handle:read "*a" - handle:close() - - if rows:find "Error" then - error(string.format("Read leafs failed:\n%s", rows)) - end - - -- Iterate over each line in the input data - for line in rows:gmatch("[^\n]+") do - local _, _, _, repetitions, compute_leaf = line:match( - "([^|]+)|([^|]+)|([^|]+)|([^|]+)|([^|]+)") - -- Convert values to appropriate types - repetitions = tonumber(repetitions) - compute_leaf = Hash:from_digest_hex("0x" .. compute_leaf) - - builder:add(compute_leaf, repetitions) - end - - return builder:build(initial_state.root_hash) + local builder = MerkleBuilder:new() + local machine = Machine:new_from_path(machine_path) + local initial_state = machine:state() + + local handle = io.popen(string.format(ROOT_LEAFS_QUERY, root_tournament)) + assert(handle) + local rows = handle:read "*a" + handle:close() + + if rows:find "Error" then + error(string.format("Read leafs failed:\n%s", rows)) + end + + -- Iterate over each line in the input data + for line in rows:gmatch("[^\n]+") do + local _, _, _, repetitions, compute_leaf = line:match( + "([^|]+)|([^|]+)|([^|]+)|([^|]+)|([^|]+)") + -- Convert values to appropriate types + repetitions = tonumber(repetitions) + compute_leaf = Hash:from_digest_hex("0x" .. compute_leaf) + + builder:add(compute_leaf, repetitions) + end + + return builder:build(initial_state.root_hash) end local INPUTS_QUERY = [[sqlite3 ./_state/compute_path/%s/db 'select HEX(input) from inputs ORDER BY input_index ASC']] local function get_inputs_from_db(root_tournament) - local handle = io.popen(string.format(INPUTS_QUERY, root_tournament)) - assert(handle) - local rows = handle:read "*a" - handle:close() - - if rows:find "Error" then - error(string.format("Read inputs failed:\n%s", rows)) - end - - local inputs = {} - -- Iterate over each line in the input data - for line in rows:gmatch("[^\n]+") do - local input = line:match("([^|]+)") - table.insert(inputs, "0x" .. input) - end - - return inputs + local handle = io.popen(string.format(INPUTS_QUERY, root_tournament)) + assert(handle) + local rows = handle:read "*a" + handle:close() + + if rows:find "Error" then + error(string.format("Read inputs failed:\n%s", rows)) + end + + local inputs = {} + -- Iterate over each line in the input data + for line in rows:gmatch("[^\n]+") do + local input = line:match("([^|]+)") + table.insert(inputs, "0x" .. input) + end + + return inputs end -- Function to setup players local function setup_players(root_tournament, machine_path) - local player_coroutines = {} - local player_index = 1 - print("Calculating root commitment...") - local root_commitment = build_root_commitment_from_db(machine_path, root_tournament) - local inputs = get_inputs_from_db(root_tournament) - - if FAKE_COMMITMENT_COUNT > 0 then - print(string.format("Setting up dishonest player with %d fake commitments", FAKE_COMMITMENT_COUNT)) - local scoped_require = new_scoped_require(_ENV) - local start_sybil = scoped_require "runners.sybil_runner" - player_coroutines[player_index] = start_sybil(player_index + 1, machine_path, root_commitment, root_tournament, - FAKE_COMMITMENT_COUNT, inputs) - player_index = player_index + 1 - end - - if IDLE_PLAYER_COUNT > 0 then - print(string.format("Setting up %d idle players", IDLE_PLAYER_COUNT)) - local scoped_require = new_scoped_require(_ENV) - local start_idle = scoped_require "runners.idle_runner" - for _ = 1, IDLE_PLAYER_COUNT do - player_coroutines[player_index] = start_idle(player_index + 1, machine_path, root_tournament) - player_index = player_index + 1 + local player_coroutines = {} + local player_index = 1 + print("Calculating root commitment...") + local root_commitment = build_root_commitment_from_db(machine_path, root_tournament) + local inputs = get_inputs_from_db(root_tournament) + + if FAKE_COMMITMENT_COUNT > 0 then + print(string.format("Setting up dishonest player with %d fake commitments", FAKE_COMMITMENT_COUNT)) + local scoped_require = new_scoped_require(_ENV) + local start_sybil = scoped_require "runners.sybil_runner" + player_coroutines[player_index] = start_sybil(player_index + 1, machine_path, root_commitment, root_tournament, + FAKE_COMMITMENT_COUNT, inputs) + player_index = player_index + 1 end - end - return player_coroutines + if IDLE_PLAYER_COUNT > 0 then + print(string.format("Setting up %d idle players", IDLE_PLAYER_COUNT)) + local scoped_require = new_scoped_require(_ENV) + local start_idle = scoped_require "runners.idle_runner" + for _ = 1, IDLE_PLAYER_COUNT do + player_coroutines[player_index] = start_idle(player_index + 1, machine_path, root_tournament) + player_index = player_index + 1 + end + end + + return player_coroutines end -- Function to run players local function run_players(player_coroutines) - while true do - local idle = true - local has_live_coroutine = false - for i, c in ipairs(player_coroutines) do - if c then - local success, ret = coroutine.resume(c) - local status = coroutine.status(c) - - if status == "dead" then - player_coroutines[i] = false - end - if not success then - print(string.format("coroutine %d fail to resume with error: %s", i, ret)) - elseif ret then - has_live_coroutine = true - idle = idle and ret.idle + while true do + local idle = true + local has_live_coroutine = false + for i, c in ipairs(player_coroutines) do + if c then + local success, ret = coroutine.resume(c) + local status = coroutine.status(c) + + if status == "dead" then + player_coroutines[i] = false + end + if not success then + print(string.format("coroutine %d fail to resume with error: %s", i, ret)) + elseif ret then + has_live_coroutine = true + idle = idle and ret.idle + end + end end - end - end - if not has_live_coroutine then - print("No active players, ending attack...") - break - end + if not has_live_coroutine then + print("No active players, ending attack...") + break + end - if idle then - print(string.format("All players idle, fastforward blockchain for %d seconds...", FAST_FORWARD_TIME)) - blockchain_utils.advance_time(FAST_FORWARD_TIME, blockchain_constants.endpoint) + if idle then + print(string.format("All players idle, fastforward blockchain for %d seconds...", FAST_FORWARD_TIME)) + blockchain_utils.advance_time(FAST_FORWARD_TIME, blockchain_constants.endpoint) + end + time.sleep(SLEEP_TIME) end - time.sleep(SLEEP_TIME) - end end -- Main Execution @@ -196,27 +196,27 @@ print("Hello from Dave rollups lua prototype!") local input_index = 1 while true do - local sealed_epochs = reader:read_epochs_sealed(CONSENSUS_ADDRESS) + local sealed_epochs = reader:read_epochs_sealed(CONSENSUS_ADDRESS) - if #sealed_epochs > 0 then - local last_sealed_epoch = sealed_epochs[#sealed_epochs] - for _ = input_index, input_index + 2 do - sender:tx_add_input(INPUT_BOX_ADDRESS, APP_ADDRESS, ECHO_MSG) - end + if #sealed_epochs > 0 then + local last_sealed_epoch = sealed_epochs[#sealed_epochs] + for _ = input_index, input_index + 2 do + sender:tx_add_input(INPUT_BOX_ADDRESS, APP_ADDRESS, ECHO_MSG) + end - -- react to last sealed epoch - local root_tournament = sealed_epochs[#sealed_epochs].tournament - local work_path = string.format("./_state/compute_path/%s", root_tournament) - if helper.exists(work_path) then - print(string.format("sybil player attacking epoch %d", - last_sealed_epoch.epoch_number)) - local epoch_machine_path = string.format("./_state/snapshots/%d/0", last_sealed_epoch.epoch_number) - local player_coroutines = setup_players(root_tournament, epoch_machine_path) - run_players(player_coroutines) + -- react to last sealed epoch + local root_tournament = sealed_epochs[#sealed_epochs].tournament + local work_path = string.format("./_state/compute_path/%s", root_tournament) + if helper.exists(work_path) then + print(string.format("sybil player attacking epoch %d", + last_sealed_epoch.epoch_number)) + local epoch_machine_path = string.format("./_state/snapshots/%d/0", last_sealed_epoch.epoch_number) + local player_coroutines = setup_players(root_tournament, epoch_machine_path) + run_players(player_coroutines) + end end - end - -- blockchain_utils.advance_time(EPOCH_TIME, blockchain_constants.endpoint) - time.sleep(SLEEP_TIME) + -- blockchain_utils.advance_time(EPOCH_TIME, blockchain_constants.endpoint) + time.sleep(SLEEP_TIME) end print("Good-bye, world!") diff --git a/test/Dockerfile b/test/Dockerfile index f35749df..5b27a9d2 100644 --- a/test/Dockerfile +++ b/test/Dockerfile @@ -1,43 +1,111 @@ -FROM rust:1.83 AS base +# syntax=docker.io/docker/dockerfile-upstream:1.12.1-labs +# to work wiht an unreleased verion, build machine docker image locally +# (i.e. cartesi/machine-emulator:latest), and change tag. +FROM cartesi/machine-emulator:0.18.1 AS machine + +FROM rust:1.83 + +# Install `just` RUN \ --mount=type=cache,target=$CARGO_HOME/registry,sharing=locked \ --mount=type=cache,target=$CARGO_HOME/git,sharing=locked \ + --mount=type=cache,target=just-target,sharing=locked \ cargo install just + +# Install `foundry` +ENV SVM_ROOT=/root/.svm RUN \ --mount=type=cache,target=$CARGO_HOME/registry,sharing=locked \ --mount=type=cache,target=$CARGO_HOME/git,sharing=locked \ + --mount=type=cache,target=foundry-target,sharing=locked \ cargo install --git https://github.com/foundry-rs/foundry \ - anvil cast chisel forge --locked - + --profile release --locked --target-dir foundry-target \ + anvil cast chisel forge +# Install general dependencies ENV DEBIAN_FRONTEND=noninteractive RUN \ - --mount=target=/var/lib/apt/lists,type=cache,sharing=locked \ - --mount=target=/var/cache/apt,type=cache,sharing=locked \ - rm -f /etc/apt/apt.conf.d/docker-clean \ - apt-get update && \ - apt-get install -y --no-install-recommends \ - build-essential git wget libboost1.81-dev liblua5.4-dev libslirp-dev lua5.4 \ - libclang-dev docker.io - -WORKDIR /emulator -COPY machine/emulator . -RUN make -RUN make install PREFIX=/usr/local - -WORKDIR /app -COPY . . + apt-get update && apt-get install -y --no-install-recommends \ + build-essential git wget \ + libslirp-dev \ + liblua5.4-dev \ + lua5.4 \ + libclang-dev \ + xxd jq; \ + rm -rf /var/cache/apt; -ENV SVM_ROOT=/root/.svm +# Install cartesi machine +COPY \ + --from=machine \ + /usr/lib/libcartesi-0.18.so /usr/lib/libcartesi.a \ + /usr/lib/ +COPY \ + --from=machine \ + /usr/bin/cartesi-machine \ + /usr/bin/ +COPY \ + --from=machine \ + /usr/include/cartesi-machine/ \ + /usr/include/cartesi-machine/ +COPY \ + --from=machine \ + /usr/share/lua/ \ + /usr/share/lua/ +COPY \ + --from=machine \ + /usr/lib/lua/ \ + /usr/lib/lua/ + +# for Rust bindings `cartesi-machine-sys` +ENV LIBCARTESI_PATH="/usr/lib/" +ENV INCLUDECARTESI_PATH="/usr/include/cartesi-machine/" + + +WORKDIR /dave + + +COPY test/programs/ test/programs/ +RUN just -f test/programs/justfile download-deps + +COPY prt/contracts/ prt/contracts/ +COPY cartesi-rollups/contracts/ cartesi-rollups/contracts/ +COPY machine/step/ machine/step/ +COPY justfile justfile RUN \ --mount=type=cache,target=$SVM_ROOT,sharing=locked \ + --mount=type=cache,target=cartesi-rollups/contracts/out,sharing=locked \ + --mount=type=cache,target=cartesi-rollups/contracts/cache,sharing=locked \ + --mount=type=cache,target=prt/contracts/out,sharing=locked \ + --mount=type=cache,target=prt/contracts/cache,sharing=locked \ just build-smart-contracts && just bind +RUN \ + --mount=type=cache,target=$SVM_ROOT,sharing=locked \ + --mount=type=cache,target=cartesi-rollups/contracts/out,sharing=locked \ + --mount=type=cache,target=cartesi-rollups/contracts/cache,sharing=locked \ + --mount=type=cache,target=prt/contracts/out,sharing=locked \ + --mount=type=cache,target=prt/contracts/cache,sharing=locked \ + just -f test/programs/justfile build-echo + + +COPY \ + --exclude=test/programs \ + --exclude=prt/contracts \ + --exclude=machine/step \ + --exclude=justfile \ + --exclude=cartesi-rollups/contracts \ + . . + RUN \ --mount=type=cache,target=target,sharing=locked \ --mount=type=cache,target=$CARGO_HOME/registry,sharing=locked \ --mount=type=cache,target=$CARGO_HOME/git,sharing=locked \ --mount=type=cache,target=$SVM_ROOT,sharing=locked \ - just build-rust-workspace + --mount=type=cache,target=cartesi-rollups/contracts/out,sharing=locked \ + --mount=type=cache,target=cartesi-rollups/contracts/cache,sharing=locked \ + --mount=type=cache,target=prt/contracts/out,sharing=locked \ + --mount=type=cache,target=prt/contracts/cache,sharing=locked \ + RUSTFLAGS="-lslirp" \ + just build-rust-workspace --features external_cartesi diff --git a/test/programs/build_anvil_state.sh b/test/programs/build_anvil_state.sh index 0e6f513c..1466bb50 100755 --- a/test/programs/build_anvil_state.sh +++ b/test/programs/build_anvil_state.sh @@ -2,6 +2,8 @@ set -euo pipefail program_path=$1 +mkdir -p $program_path + # start anvil with dump state rm -f $program_path/anvil_state.json anvil --dump-state $program_path/anvil_state.json > $program_path/_anvil.log 2>&1 & @@ -27,4 +29,5 @@ jq -r '.transactions[] | select(.transactionType=="CREATE") | select(.contractNa # # kill anvil, thus dumping its state, to be loaded later by tests -kill "$anvil_pid" +kill -INT "$anvil_pid" +wait $anvil_pid