diff --git a/.github/workflows/ci-pre-land.yml b/.github/workflows/ci-pre-land.yml index af64c45734..634dd8b0cc 100644 --- a/.github/workflows/ci-pre-land.yml +++ b/.github/workflows/ci-pre-land.yml @@ -210,7 +210,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-20.04, macos-10.15, windows-2022] + os: [ubuntu-20.04, macos-11, windows-2022] runs-on: ${{ matrix.os }} needs: - prepare diff --git a/.github/workflows/daily.yml b/.github/workflows/daily.yml index 5901e076e9..75a84815e9 100644 --- a/.github/workflows/daily.yml +++ b/.github/workflows/daily.yml @@ -20,8 +20,9 @@ jobs: # List of ignored RUSTSEC # 1. RUSTSEC-2021-0073 - Not impacted. # 2. RUSTSEC-2021-0072 - Not impacted. + # 3. RUSTSEC-2020-0071 - Not impacted (chronotope/chrono#578). run: | - cargo audit --color never --ignore RUSTSEC-2021-0073 --ignore RUSTSEC-2021-0072 > $AUDIT_SUMMARY_FILE + cargo audit --color never --ignore RUSTSEC-2021-0073 --ignore RUSTSEC-2021-0072 --ignore RUSTSEC-2020-0071 > $AUDIT_SUMMARY_FILE - name: set issue body content if: ${{ failure() }} env: diff --git a/.gitignore b/.gitignore index 13d3db1d03..124e0b7c75 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ **/target **/*.rs.bk .idea/ +**/.vscode # Ignore wallet mnemonic files used for deterministic key derivation *.mnemonic diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a1d579e27f..44c9186fdd 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -135,3 +135,26 @@ able to be built and passes all checks performed by CI. Move uses [GitHub issues](https://github.com/move-language/move/issues) to track bugs. Please include necessary information and instructions to reproduce your issue. + +## Major feature requests + +Please begin by checking the following locations for duplicate requests: +* [Approved feature proposals awaiting implementation](https://github.com/move-language/move/issues?q=is%3Aissue+is%3Aopen+label%3A%22accepted+feature+awaiting+implementation%22) +* [Feature proposals under discussion](https://github.com/move-language/move/issues?q=is%3Aissue+is%3Aopen+label%3A%22proposed+feature+in+discussion%22) +* [Language feature request graveyard](GRAVEYARD.md) + +If your feature is not in any of these locations, please add a new feature request using the following format: + +``` +Sponsor: your_github_id + +## Status: initial proposal + +## Rationale +A detailed description of the problem the proposed feature seeks to solve. This should explain why the problem is important for smart contract programmers and why it is impossible (or unacceptably unpleasant) to solve with the existing language constructs. Examples are strongly recommended. + +## Design +Explain the key decisions to be made in designing the feature. This can be organized as fully fleshed out design, a list of design options with pros and cons, or a list of questions to be answered. A proposed feature should have a very strong/clear rationale, but it is ok if many key design questions are open--the Move community and core contributors can help with this. +``` + +A Move core contributor will either add a `proposed_feature_to_be_discussed` tag and queue the feature for discussion at a future Move community meeting (and change the "Status" to reflect the meeting date), or will request changes that must be made to the issue before it is ready for discussion. diff --git a/Cargo.lock b/Cargo.lock index 0591f52205..774baf6109 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -134,17 +134,170 @@ version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" +[[package]] +name = "ascii-canvas" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8824ecca2e851cec16968d54a01dd372ef8f95b244fb84b84e70128be347c3c6" +dependencies = [ + "term 0.7.0", +] + +[[package]] +name = "assert-json-diff" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e4f2b81832e72834d7518d8487a0396a28cc408186a2e8854c0f98011faf12" +dependencies = [ + "serde 1.0.145", + "serde_json", +] + +[[package]] +name = "async-channel" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2114d64672151c0c5eaa5e131ec84a74f06e1e559830dabba01ca30605d66319" +dependencies = [ + "concurrent-queue", + "event-listener", + "futures-core", +] + +[[package]] +name = "async-executor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "871f9bb5e0a22eeb7e8cf16641feb87c9dc67032ccf8ff49e772eb9941d3a965" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand", + "futures-lite", + "once_cell", + "slab", +] + +[[package]] +name = "async-global-executor" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5262ed948da60dd8956c6c5aca4d4163593dddb7b32d73267c93dab7b2e98940" +dependencies = [ + "async-channel", + "async-executor", + "async-io", + "async-lock", + "blocking", + "futures-lite", + "num_cpus", + "once_cell", +] + +[[package]] +name = "async-io" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5e18f61464ae81cde0a23e713ae8fd299580c54d697a35820cfd0625b8b0e07" +dependencies = [ + "concurrent-queue", + "futures-lite", + "libc", + "log", + "once_cell", + "parking", + "polling", + "slab", + "socket2", + "waker-fn", + "winapi", +] + +[[package]] +name = "async-lock" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e97a171d191782fba31bb902b14ad94e24a68145032b7eedf871ab0bc0d077b6" +dependencies = [ + "event-listener", +] + +[[package]] +name = "async-object-pool" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aeb901c30ebc2fc4ab46395bbfbdba9542c16559d853645d75190c3056caf3bc" +dependencies = [ + "async-std", +] + +[[package]] +name = "async-process" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf2c06e30a24e8c78a3987d07f0930edf76ef35e027e7bdb063fccafdad1f60c" +dependencies = [ + "async-io", + "blocking", + "cfg-if 1.0.0", + "event-listener", + "futures-lite", + "libc", + "once_cell", + "signal-hook", + "winapi", +] + +[[package]] +name = "async-std" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62565bb4402e926b29953c785397c6dc0391b7b446e45008b0049eb43cec6f5d" +dependencies = [ + "async-channel", + "async-global-executor", + "async-io", + "async-lock", + "async-process", + "crossbeam-utils", + "futures-channel", + "futures-core", + "futures-io", + "futures-lite", + "gloo-timers", + "kv-log-macro", + "log", + "memchr", + "once_cell", + "pin-project-lite", + "pin-utils", + "slab", + "wasm-bindgen-futures", +] + +[[package]] +name = "async-task" +version = "4.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a40729d2133846d9ed0ea60a8b9541bccddab49cd30f0715a1da672fe9a2524" + [[package]] name = "async-trait" version = "0.1.48" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "36ea56748e10732c49404c153638a15ec3d6211ec5ff35d9bb20e13b93576adf" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] +[[package]] +name = "atomic-waker" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" + [[package]] name = "atty" version = "0.2.14" @@ -163,9 +316,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7862e21c893d65a1650125d157eaeec691439379a1cee17ee49031b79236ada4" dependencies = [ "proc-macro-error", - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] @@ -180,13 +333,24 @@ version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" +[[package]] +name = "basic-cookies" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb53b6b315f924c7f113b162e53b3901c05fc9966baf84d201dfcc7432a4bb38" +dependencies = [ + "lalrpop", + "lalrpop-util", + "regex", +] + [[package]] name = "bcs" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "510fd83e3eaf7263b06182f3550b4c0af2af42cb36ab8024969ff5ea7fcb2833" +checksum = "8b06b4c1f053002b70e7084ac944c77d58d5d92b2110dbc5e852735e00ad3ccc" dependencies = [ - "serde 1.0.130", + "serde 1.0.145", "thiserror", ] @@ -205,9 +369,9 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3deeecb812ca5300b7d3f66f730cc2ebd3511c3d36c691dd79c165d5b19a26e3" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] @@ -333,6 +497,20 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" +[[package]] +name = "blocking" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6ccb65d468978a086b69884437ded69a90faab3bbe6e67f242173ea728acccc" +dependencies = [ + "async-channel", + "async-task", + "atomic-waker", + "fastrand", + "futures-lite", + "once_cell", +] + [[package]] name = "bstr" version = "0.2.15" @@ -342,7 +520,7 @@ dependencies = [ "lazy_static 1.4.0", "memchr", "regex-automata", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -419,13 +597,19 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" +[[package]] +name = "cache-padded" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1db59621ec70f09c5e9b597b220c7a2b43611f4710dc03ceb8748637775692c" + [[package]] name = "camino" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52d74260d9bf6944e2208aa46841b4b8f0d7ffc0849a06837b2f510337f86b2b" dependencies = [ - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -434,7 +618,7 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cbdb825da8a5df079a43676dbe042702f1707b1109f713a01420fbb4cc71fa27" dependencies = [ - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -446,7 +630,7 @@ dependencies = [ "camino", "cargo-platform", "semver 1.0.4", - "serde 1.0.130", + "serde 1.0.145", "serde_json", ] @@ -465,6 +649,12 @@ dependencies = [ "rustc_version", ] +[[package]] +name = "castaway" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2698f953def977c68f935bb0dfa959375ad4638570e969e2f1e9f433cbf1af6" + [[package]] name = "cc" version = "1.0.67" @@ -508,12 +698,24 @@ dependencies = [ [[package]] name = "chrono-tz" -version = "0.5.3" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2554a3155fec064362507487171dcc4edc3df60cb10f3a1fb10ed8094822b120" +checksum = "58549f1842da3080ce63002102d5bc954c7bc843d4f47818e642abdc36253552" dependencies = [ "chrono", + "chrono-tz-build", + "phf", +] + +[[package]] +name = "chrono-tz-build" +version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db058d493fb2f65f41861bfed7e3fe6335264a9f0f92710cab5bdf01fef09069" +dependencies = [ "parse-zoneinfo", + "phf", + "phf_codegen", ] [[package]] @@ -565,18 +767,9 @@ checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1" dependencies = [ "heck 0.4.0", "proc-macro-error", - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", -] - -[[package]] -name = "cloudabi" -version = "0.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" -dependencies = [ - "bitflags", + "syn 1.0.99", ] [[package]] @@ -586,7 +779,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3362992a0d9f1dd7c3d0e89e0ab2bb540b7a95fea8cd798090e758fda2899b5e" dependencies = [ "codespan-reporting", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -595,7 +788,7 @@ version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" dependencies = [ - "serde 1.0.130", + "serde 1.0.145", "termcolor", "unicode-width", ] @@ -622,6 +815,25 @@ dependencies = [ "itertools 0.10.1", ] +[[package]] +name = "combine" +version = "4.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a604e93b79d1808327a6fca85a6f2d69de66461e7620f5a4cbf5fb4d1d7c948" +dependencies = [ + "bytes", + "memchr", +] + +[[package]] +name = "concurrent-queue" +version = "1.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af4780a44ab5696ea9e28294517f1fffb421a83a25af521333c838635509db9c" +dependencies = [ + "cache-padded", +] + [[package]] name = "config" version = "0.11.0" @@ -631,7 +843,7 @@ dependencies = [ "lazy_static 1.4.0", "nom", "rust-ini", - "serde 1.0.130", + "serde 1.0.145", "serde-hjson", "serde_json", "toml", @@ -666,6 +878,22 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" +[[package]] +name = "core-foundation" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" + [[package]] name = "cpuid-bool" version = "0.1.2" @@ -696,7 +924,7 @@ dependencies = [ "plotters", "rayon", "regex", - "serde 1.0.130", + "serde 1.0.145", "serde_cbor", "serde_derive", "serde_json", @@ -802,7 +1030,7 @@ dependencies = [ "bitflags", "crossterm_winapi 0.8.0", "libc", - "mio", + "mio 0.7.13", "parking_lot 0.11.1", "signal-hook", "signal-hook-mio", @@ -818,7 +1046,7 @@ dependencies = [ "bitflags", "crossterm_winapi 0.9.0", "libc", - "mio", + "mio 0.7.13", "parking_lot 0.11.1", "signal-hook", "signal-hook-mio", @@ -879,7 +1107,7 @@ dependencies = [ "csv-core", "itoa 0.4.7", "ryu", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -891,6 +1119,16 @@ dependencies = [ "memchr", ] +[[package]] +name = "ctor" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f877be4f7c9f246b183111634f75baa039715e3f46ce860677d3b19a69fb229c" +dependencies = [ + "quote 1.0.9", + "syn 1.0.99", +] + [[package]] name = "ctr" version = "0.6.0" @@ -910,6 +1148,37 @@ dependencies = [ "winapi", ] +[[package]] +name = "curl" +version = "0.4.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "509bd11746c7ac09ebd19f0b17782eae80aadee26237658a6b4808afb5c11a22" +dependencies = [ + "curl-sys", + "libc", + "openssl-probe", + "openssl-sys", + "schannel", + "socket2", + "winapi", +] + +[[package]] +name = "curl-sys" +version = "0.4.56+curl-7.83.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6093e169dd4de29e468fa649fbae11cdcd5551c81fe5bf1b0677adad7ef3d26f" +dependencies = [ + "cc", + "libc", + "libnghttp2-sys", + "libz-sys", + "openssl-sys", + "pkg-config", + "vcpkg", + "winapi", +] + [[package]] name = "curve25519-dalek-fiat" version = "0.1.0" @@ -953,15 +1222,26 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "223089cd5a4e4491f0a0dddd9933f9575123160cf96ca2bb56a690046ecf1745" +[[package]] +name = "derivative" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" +dependencies = [ + "proc-macro2 1.0.43", + "quote 1.0.9", + "syn 1.0.99", +] + [[package]] name = "derive_more" version = "0.99.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] @@ -977,7 +1257,7 @@ dependencies = [ "once_cell", "petgraph 0.6.0", "rayon", - "serde 1.0.130", + "serde 1.0.145", "toml", ] @@ -999,6 +1279,7 @@ dependencies = [ "move-cli", "move-core-types", "move-stdlib", + "move-vm-test-utils", "move-vm-types", ] @@ -1026,7 +1307,7 @@ dependencies = [ "rand 0.8.4", "rand_core 0.6.2", "ripemd160", - "serde 1.0.130", + "serde 1.0.145", "serde-name", "serde_bytes", "serde_json", @@ -1044,9 +1325,9 @@ name = "diem-crypto-derive" version = "0.0.3" dependencies = [ "anyhow", - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] @@ -1061,6 +1342,12 @@ dependencies = [ "smallvec", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "difference" version = "2.0.0" @@ -1201,7 +1488,7 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37c66a534cbb46ab4ea03477eae19d5c22c01da8258030280b7bd9d8433fb6ef" dependencies = [ - "serde 1.0.130", + "serde 1.0.145", "signature", ] @@ -1214,7 +1501,7 @@ dependencies = [ "curve25519-dalek-fiat", "ed25519", "rand 0.8.4", - "serde 1.0.130", + "serde 1.0.145", "serde_bytes", "sha2", "zeroize", @@ -1226,12 +1513,30 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +[[package]] +name = "ena" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7402b94a93c24e742487327a7cd839dc9d36fec9de9fb25b09f2dae459f36c3" +dependencies = [ + "log", +] + [[package]] name = "encode_unicode" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" +[[package]] +name = "encoding_rs" +version = "0.8.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b" +dependencies = [ + "cfg-if 1.0.0", +] + [[package]] name = "env_logger" version = "0.8.4" @@ -1261,7 +1566,7 @@ dependencies = [ "hex", "once_cell", "regex", - "serde 1.0.130", + "serde 1.0.145", "serde_json", "sha3 0.10.1", "thiserror", @@ -1309,7 +1614,7 @@ dependencies = [ "rlp", "rlp-derive", "scale-info", - "serde 1.0.130", + "serde 1.0.145", "sha3 0.9.1", "triehash", ] @@ -1349,6 +1654,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f58da196a1fc8f14cb51f373f504efc66c434c577b777c2cd30d6fad16e4822" +[[package]] +name = "event-listener" +version = "2.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" + [[package]] name = "evm" version = "0.33.1" @@ -1366,7 +1677,7 @@ dependencies = [ "primitive-types 0.10.1", "rlp", "scale-info", - "serde 1.0.130", + "serde 1.0.145", "sha3 0.8.2", ] @@ -1380,7 +1691,7 @@ dependencies = [ "parity-scale-codec 2.3.1", "primitive-types 0.10.1", "scale-info", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -1436,7 +1747,7 @@ dependencies = [ "move-to-yul", "once_cell", "regex", - "serde 1.0.130", + "serde 1.0.145", "serde_json", ] @@ -1457,6 +1768,15 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" +[[package]] +name = "fastrand" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a407cfaa3385c4ae6b23e84623d48c2798d06e3e6a1878f7f59f17b3f86499" +dependencies = [ + "instant", +] + [[package]] name = "fiat-crypto" version = "0.1.6" @@ -1499,6 +1819,21 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "form_urlencoded" version = "1.0.1" @@ -1575,6 +1910,21 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b0e06c393068f3a6ef246c75cdca793d6a46347e75286933e5e75fd2fd11582" +[[package]] +name = "futures-lite" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7694489acd39452c77daa48516b894c153f192c3578d5a839b62c58099fcbf48" +dependencies = [ + "fastrand", + "futures-core", + "futures-io", + "memchr", + "parking", + "pin-project-lite", + "waker-fn", +] + [[package]] name = "futures-macro" version = "0.3.16" @@ -1583,9 +1933,9 @@ checksum = "c54913bae956fb8df7f4dc6fc90362aa72e69148e3f39041fbe8742d21e0ac57" dependencies = [ "autocfg", "proc-macro-hack", - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] @@ -1702,6 +2052,18 @@ dependencies = [ "walkdir", ] +[[package]] +name = "gloo-timers" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fb7d06c1c8cc2a29bee7ec961009a0b2caa0793ee4900c2ffb348734ba1c8f9" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "guppy" version = "0.12.6" @@ -1723,7 +2085,7 @@ dependencies = [ "petgraph 0.6.0", "rayon", "semver 1.0.4", - "serde 1.0.130", + "serde 1.0.145", "serde_json", "smallvec", "target-spec", @@ -1740,7 +2102,7 @@ dependencies = [ "cfg-if 1.0.0", "diffus", "semver 1.0.4", - "serde 1.0.130", + "serde 1.0.145", "toml", ] @@ -1750,6 +2112,25 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "92620684d99f750bae383ecb3be3748142d6095760afd5cbcf2261e9a279d780" +[[package]] +name = "h2" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca32592cf21ac7ccab1825cd87f6c9b3d9022c44d086172ed0966bec8af30be" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + [[package]] name = "half" version = "1.7.1" @@ -1836,6 +2217,68 @@ dependencies = [ "digest 0.9.0", ] +[[package]] +name = "http" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75f43d41e26995c17e71ee126451dd3941010b0514a81a9d11f3b341debc2399" +dependencies = [ + "bytes", + "fnv", + "itoa 1.0.1", +] + +[[package]] +name = "http-body" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d5f38f16d184e36f2408a55281cd658ecbd3ca05cce6d6510a176eca393e26d1" +dependencies = [ + "bytes", + "http", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "496ce29bb5a52785b44e0f7ca2847ae0bb839c9bd28f69acac9b99d461c0c04c" + +[[package]] +name = "httpdate" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" + +[[package]] +name = "httpmock" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c159c4fc205e6c1a9b325cb7ec135d13b5f47188ce175dabb76ec847f331d9bd" +dependencies = [ + "assert-json-diff", + "async-object-pool", + "async-trait", + "base64", + "basic-cookies", + "crossbeam-utils", + "form_urlencoded", + "futures-util", + "hyper", + "isahc", + "lazy_static 1.4.0", + "levenshtein", + "log", + "regex", + "serde 1.0.145", + "serde_json", + "serde_regex", + "similar", + "tokio", + "url", +] + [[package]] name = "humansize" version = "1.1.0" @@ -1855,7 +2298,44 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac34a56cfd4acddb469cc7fff187ed5ac36f498ba085caf8bbc725e3ff474058" dependencies = [ "humantime", - "serde 1.0.130", + "serde 1.0.145", +] + +[[package]] +name = "hyper" +version = "0.14.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02c929dc5c39e335a03c405292728118860721b10190d98c2a0f0efd5baafbac" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa 1.0.1", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "hyper-tls" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" +dependencies = [ + "bytes", + "hyper", + "native-tls", + "tokio", + "tokio-native-tls", ] [[package]] @@ -1934,7 +2414,7 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4551f042f3438e64dbd6226b20527fc84a6e1fe65688b58746a2f53623f25f5c" dependencies = [ - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -1943,9 +2423,9 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11d7a9f6330b71fea57921c9b61c47ee6e84f72d394754eff6163ae67e7395eb" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] @@ -2002,12 +2482,45 @@ dependencies = [ "proptest", ] +[[package]] +name = "ipnet" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879d54834c8c76457ef4293a689b2a8c59b076067ad77b15efafbb05f92a592b" + [[package]] name = "is_ci" version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "616cde7c720bb2bb5824a224687d8f77bfd38922027f01d825cd7453be5099fb" +[[package]] +name = "isahc" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "334e04b4d781f436dc315cb1e7515bd96826426345d498149e4bde36b67f8ee9" +dependencies = [ + "async-channel", + "castaway", + "crossbeam-utils", + "curl", + "curl-sys", + "encoding_rs", + "event-listener", + "futures-lite", + "http", + "log", + "mime", + "once_cell", + "polling", + "slab", + "sluice", + "tracing", + "tracing-futures", + "url", + "waker-fn", +] + [[package]] name = "itertools" version = "0.9.0" @@ -2053,6 +2566,47 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67c21572b4949434e4fc1e1978b99c5f77064153c59d998bf13ecd96fb5ecba7" +[[package]] +name = "kv-log-macro" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" +dependencies = [ + "log", +] + +[[package]] +name = "lalrpop" +version = "0.19.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b30455341b0e18f276fa64540aff54deafb54c589de6aca68659c63dd2d5d823" +dependencies = [ + "ascii-canvas", + "atty", + "bit-set", + "diff", + "ena", + "itertools 0.10.1", + "lalrpop-util", + "petgraph 0.6.0", + "pico-args", + "regex", + "regex-syntax", + "string_cache", + "term 0.7.0", + "tiny-keccak", + "unicode-xid 0.2.2", +] + +[[package]] +name = "lalrpop-util" +version = "0.19.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcf796c978e9b4d983414f4caedc9273aa33ee214c5b887bd55fde84c85d2dc4" +dependencies = [ + "regex", +] + [[package]] name = "language-benchmarks" version = "0.1.0" @@ -2084,6 +2638,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "levenshtein" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db13adb97ab515a3691f56e4dbab09283d0b86cb45abd991d8634a9d6f501760" + [[package]] name = "lexical-core" version = "0.7.6" @@ -2099,42 +2659,56 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.112" +version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" [[package]] -name = "linked-hash-map" -version = "0.5.4" +name = "libnghttp2-sys" +version = "0.1.7+1.45.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" +checksum = "57ed28aba195b38d5ff02b9170cbff627e336a20925e43b4945390401c5dc93f" +dependencies = [ + "cc", + "libc", +] [[package]] -name = "lock_api" -version = "0.3.4" +name = "libz-sys" +version = "1.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75" +checksum = "9702761c3935f8cc2f101793272e202c72b99da8f4224a19ddcf1279a6450bbf" dependencies = [ - "scopeguard", + "cc", + "libc", + "pkg-config", + "vcpkg", ] +[[package]] +name = "linked-hash-map" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" + [[package]] name = "lock_api" -version = "0.4.4" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb" +checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b" dependencies = [ "scopeguard", ] [[package]] name = "log" -version = "0.4.14" +version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ "cfg-if 1.0.0", - "serde 1.0.130", + "serde 1.0.145", + "value-bag", ] [[package]] @@ -2145,7 +2719,7 @@ checksum = "c351c75989da23b355226dc188dc2b52538a7f4f218d70fd7393c6b62b110444" dependencies = [ "crossbeam-channel", "log", - "serde 1.0.130", + "serde 1.0.145", "serde_json", ] @@ -2156,7 +2730,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f3734ab1d7d157fc0c45110e06b587c31cd82bea2ccfd6b563cbff0aaeeb1d3" dependencies = [ "bitflags", - "serde 1.0.130", + "serde 1.0.145", "serde_json", "serde_repr", "url", @@ -2198,6 +2772,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "mime" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" + [[package]] name = "mio" version = "0.7.13" @@ -2211,6 +2791,18 @@ dependencies = [ "winapi", ] +[[package]] +name = "mio" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf" +dependencies = [ + "libc", + "log", + "wasi 0.11.0+wasi-snapshot-preview1", + "windows-sys", +] + [[package]] name = "miow" version = "0.3.7" @@ -2249,24 +2841,26 @@ dependencies = [ "datatest-stable", "heck 0.3.2", "log", + "move-binary-format", "move-bytecode-verifier", "move-command-line-common", "move-core-types", "move-model", "move-prover", "move-prover-test-utils", - "serde 1.0.130", + "serde 1.0.145", "tempfile", ] [[package]] name = "move-analyzer" -version = "0.0.0" +version = "1.0.0" dependencies = [ "anyhow", "clap 3.1.8", "codespan-reporting", "crossbeam", + "derivative", "dunce", "im", "lsp-server", @@ -2277,7 +2871,7 @@ dependencies = [ "move-package", "move-symbol-pool", "petgraph 0.5.1", - "serde 1.0.130", + "serde 1.0.145", "serde_json", "tempfile", "url", @@ -2299,6 +2893,7 @@ dependencies = [ "move-prover-test-utils", "move-stdlib", "move-vm-runtime", + "move-vm-test-utils", "move-vm-types", "sha3 0.9.1", "smallvec", @@ -2315,7 +2910,7 @@ dependencies = [ "proptest", "proptest-derive", "ref-cast", - "serde 1.0.130", + "serde 1.0.145", "serde_json", "variant_count", ] @@ -2335,7 +2930,7 @@ dependencies = [ "move-core-types", "move-ir-types", "move-symbol-pool", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -2388,6 +2983,7 @@ dependencies = [ "colored", "datatest-stable", "difference", + "httpmock", "itertools 0.10.1", "move-binary-format", "move-bytecode-source-map", @@ -2399,6 +2995,7 @@ dependencies = [ "move-core-types", "move-coverage", "move-disassembler", + "move-docgen", "move-errmapgen", "move-ir-types", "move-package", @@ -2409,13 +3006,17 @@ dependencies = [ "move-table-extension", "move-unit-test", "move-vm-runtime", + "move-vm-test-utils", "move-vm-types", "once_cell", "read-write-set", "read-write-set-dynamic", - "serde 1.0.130", + "reqwest", + "serde 1.0.145", + "serde_json", "serde_yaml", "tempfile", + "toml_edit", "walkdir", ] @@ -2425,10 +3026,12 @@ version = "0.1.0" dependencies = [ "anyhow", "difference", + "dirs-next", "hex", "move-core-types", "num-bigint 0.4.0", - "serde 1.0.130", + "once_cell", + "serde 1.0.145", "sha2", "walkdir", ] @@ -2484,7 +3087,7 @@ dependencies = [ "rand 0.8.4", "ref-cast", "regex", - "serde 1.0.130", + "serde 1.0.145", "serde_bytes", "serde_json", ] @@ -2505,7 +3108,7 @@ dependencies = [ "move-ir-types", "once_cell", "petgraph 0.5.1", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -2542,7 +3145,7 @@ dependencies = [ "num 0.4.0", "once_cell", "regex", - "serde 1.0.130", + "serde 1.0.145", "tempfile", ] @@ -2559,7 +3162,7 @@ dependencies = [ "move-core-types", "move-model", "move-prover", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -2570,7 +3173,7 @@ dependencies = [ "ethabi", "once_cell", "regex", - "serde 1.0.130", + "serde 1.0.145", "serde_json", ] @@ -2650,7 +3253,7 @@ dependencies = [ "move-core-types", "move-symbol-pool", "once_cell", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -2677,7 +3280,7 @@ dependencies = [ "num 0.4.0", "once_cell", "regex", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -2692,6 +3295,7 @@ dependencies = [ "dirs-next", "evm-exec-utils", "hex", + "itertools 0.10.1", "move-abigen", "move-binary-format", "move-bytecode-source-map", @@ -2708,13 +3312,15 @@ dependencies = [ "petgraph 0.5.1", "ptree", "regex", - "serde 1.0.130", + "reqwest", + "serde 1.0.145", "serde_yaml", "sha2", "tempfile", "termcolor", "toml", "walkdir", + "whoami", ] [[package]] @@ -2748,7 +3354,7 @@ dependencies = [ "once_cell", "pretty", "rand 0.8.4", - "serde 1.0.130", + "serde 1.0.145", "serde_json", "shell-words", "simplelog", @@ -2779,7 +3385,7 @@ dependencies = [ "pretty", "rand 0.8.4", "regex", - "serde 1.0.130", + "serde 1.0.145", "serde_json", "tera", "tokio", @@ -2802,7 +3408,7 @@ dependencies = [ "anyhow", "move-binary-format", "move-core-types", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -2816,7 +3422,7 @@ dependencies = [ "move-bytecode-utils", "move-core-types", "once_cell", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -2846,7 +3452,7 @@ dependencies = [ "once_cell", "paste", "petgraph 0.5.1", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -2865,7 +3471,7 @@ dependencies = [ "move-prover-test-utils", "move-stackless-bytecode", "num 0.4.0", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -2900,7 +3506,7 @@ name = "move-symbol-pool" version = "0.1.0" dependencies = [ "once_cell", - "serde 1.0.130", + "serde 1.0.145", "serde_json", ] @@ -2959,7 +3565,7 @@ dependencies = [ "primitive-types 0.10.1", "rand 0.8.4", "regex", - "serde 1.0.130", + "serde 1.0.145", "serde_json", "sha3 0.9.1", "simplelog", @@ -3042,6 +3648,7 @@ version = "0.1.0" dependencies = [ "anyhow", "move-binary-format", + "move-bytecode-verifier", "move-compiler", "move-core-types", "move-stdlib", @@ -3078,8 +3685,12 @@ name = "move-vm-test-utils" version = "0.1.0" dependencies = [ "anyhow", + "move-binary-format", "move-core-types", "move-table-extension", + "move-vm-types", + "once_cell", + "serde 1.0.145", ] [[package]] @@ -3099,30 +3710,54 @@ dependencies = [ "move-core-types", "once_cell", "proptest", - "serde 1.0.130", + "serde 1.0.145", "smallvec", ] [[package]] name = "named-lock" -version = "0.1.1" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3ab176d4bcfbcb53b8c7c5a25cb2c01674cda33db27064a85a16814c88c1f2d" +checksum = "40a3eb6b7c682b65d1f631ec3176829d72ab450b3aacdd3f719bf220822e59ac" dependencies = [ "libc", "once_cell", - "parking_lot 0.10.2", + "parking_lot 0.12.1", "thiserror", "widestring", "winapi", ] +[[package]] +name = "native-tls" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd7e2f3618557f980e0b17e8856252eee3c97fa12c54dff0ca290fb6266ca4a9" +dependencies = [ + "lazy_static 1.4.0", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + [[package]] name = "nested" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca2b420f638f07fe83056b55ea190bb815f609ec5a35e7017884a10f78839c9e" +[[package]] +name = "new_debug_unreachable" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54" + [[package]] name = "nextest-config" version = "0.1.0" @@ -3131,7 +3766,7 @@ dependencies = [ "camino", "config", "humantime-serde", - "serde 1.0.130", + "serde 1.0.145", "toml", ] @@ -3157,7 +3792,7 @@ dependencies = [ "owo-colors", "quick-junit", "rayon", - "serde 1.0.130", + "serde 1.0.145", "serde_json", "strip-ansi-escapes", "twox-hash", @@ -3169,7 +3804,7 @@ version = "0.1.0" source = "git+https://github.com/diem/diem-devtools?rev=f99a204e3d3f8e503d51d7df42e55c8282b59154#f99a204e3d3f8e503d51d7df42e55c8282b59154" dependencies = [ "camino", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -3370,6 +4005,51 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "openssl" +version = "0.10.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "618febf65336490dfcf20b73f885f5651a0c89c64c2d4a8c3662585a70bf5bd0" +dependencies = [ + "bitflags", + "cfg-if 1.0.0", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b501e44f11665960c7e7fcf062c7d96a14ade4aa98116c004b2e37b5be7d736c" +dependencies = [ + "proc-macro2 1.0.43", + "quote 1.0.9", + "syn 1.0.99", +] + +[[package]] +name = "openssl-probe" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" + +[[package]] +name = "openssl-sys" +version = "0.9.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5f9bd0c2710541a3cda73d6f9ac4f1b240de4ae261065d309dbe73d9dceb42f" +dependencies = [ + "autocfg", + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "ordered-float" version = "2.1.1" @@ -3416,9 +4096,9 @@ checksum = "41db02c8f8731cdd7a72b433c7900cce4bf245465b452c364bfd21f4566ab055" dependencies = [ "Inflector", "proc-macro-error", - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] @@ -3438,7 +4118,7 @@ dependencies = [ "byte-slice-cast", "impl-trait-for-tuples", "parity-scale-codec-derive 2.3.1", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -3452,7 +4132,7 @@ dependencies = [ "byte-slice-cast", "impl-trait-for-tuples", "parity-scale-codec-derive 3.1.2", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -3462,9 +4142,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1557010476e0595c9b568d16dcfb81b93cdeb157612726f5170d31aa707bed27" dependencies = [ "proc-macro-crate", - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] @@ -3474,20 +4154,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c45ed1f39709f5a89338fab50e59816b2e8815f5bb58276e7ddf9afd495f73f8" dependencies = [ "proc-macro-crate", - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] -name = "parking_lot" -version = "0.10.2" +name = "parking" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e" -dependencies = [ - "lock_api 0.3.4", - "parking_lot_core 0.7.2", -] +checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" [[package]] name = "parking_lot" @@ -3496,22 +4172,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" dependencies = [ "instant", - "lock_api 0.4.4", + "lock_api", "parking_lot_core 0.8.3", ] [[package]] -name = "parking_lot_core" -version = "0.7.2" +name = "parking_lot" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" dependencies = [ - "cfg-if 0.1.10", - "cloudabi", - "libc", - "redox_syscall 0.1.57", - "smallvec", - "winapi", + "lock_api", + "parking_lot_core 0.9.3", ] [[package]] @@ -3528,6 +4200,19 @@ dependencies = [ "winapi", ] +[[package]] +name = "parking_lot_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09a279cbf25cb0757810394fbc1e359949b59e348145c643a939a525692e6929" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "redox_syscall 0.2.10", + "smallvec", + "windows-sys", +] + [[package]] name = "parse-zoneinfo" version = "0.3.0" @@ -3585,9 +4270,9 @@ checksum = "99b8db626e31e5b81787b9783425769681b347011cc59471e33ea46d2ea0cf55" dependencies = [ "pest", "pest_meta", - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] @@ -3621,6 +4306,71 @@ dependencies = [ "indexmap", ] +[[package]] +name = "phf" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fabbf1ead8a5bcbc20f5f8b939ee3f5b0f6f281b6ad3468b84656b658b455259" +dependencies = [ + "phf_shared", +] + +[[package]] +name = "phf_codegen" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb1c3a8bc4dd4e5cfce29b44ffc14bedd2ee294559a294e2a4d4c9e9a6a13cd" +dependencies = [ + "phf_generator", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d5285893bb5eb82e6aaf5d59ee909a06a16737a8970984dd7746ba9283498d6" +dependencies = [ + "phf_shared", + "rand 0.8.4", +] + +[[package]] +name = "phf_shared" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6796ad771acdc0123d2a88dc428b5e38ef24456743ddb1744ed628f9815c096" +dependencies = [ + "siphasher", + "uncased", +] + +[[package]] +name = "pico-args" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db8bcd96cb740d03149cbad5518db9fd87126a10ab519c011893b1754134c468" + +[[package]] +name = "pin-project" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78203e83c48cffbe01e4a2d35d566ca4de445d79a85372fc64e378bfc812a260" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "710faf75e1b33345361201d36d04e98ac1ed8909151a017ed384700836104c74" +dependencies = [ + "proc-macro2 1.0.43", + "quote 1.0.9", + "syn 1.0.99", +] + [[package]] name = "pin-project-lite" version = "0.2.7" @@ -3631,7 +4381,13 @@ checksum = "8d31d11c69a6b52a174b42bdc0c30e5e11670f90788b2c471c31c1d17d449443" name = "pin-utils" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "pkg-config" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" [[package]] name = "plotters" @@ -3661,6 +4417,19 @@ dependencies = [ "plotters-backend", ] +[[package]] +name = "polling" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "685404d509889fade3e86fe3a5803bca2ec09b0c0778d5ada6ec8bf7a8de5259" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "log", + "wepoll-ffi", + "winapi", +] + [[package]] name = "polyval" version = "0.4.5" @@ -3687,9 +4456,9 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "597c3287a549da151aca6ada2795ecde089c7527bd5093114e8e0e1c3f0e52b1" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] @@ -3698,6 +4467,12 @@ version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +[[package]] +name = "precomputed-hash" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" + [[package]] name = "pretty" version = "0.10.0" @@ -3729,7 +4504,7 @@ dependencies = [ "csv", "encode_unicode", "lazy_static 1.4.0", - "term", + "term 0.5.2", "unicode-width", ] @@ -3776,9 +4551,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" dependencies = [ "proc-macro-error-attr", - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", "version_check", ] @@ -3788,7 +4563,7 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", "version_check", ] @@ -3816,11 +4591,11 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.28" +version = "1.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c7ed8b8c7b886ea3ed7dde405212185f423ab44682667c8c6dd14aa1d9f6612" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" dependencies = [ - "unicode-xid 0.2.2", + "unicode-ident", ] [[package]] @@ -3871,7 +4646,7 @@ dependencies = [ "move-stackless-bytecode", "num 0.4.0", "plotters", - "serde 1.0.130", + "serde 1.0.145", "serde_json", "simplelog", "z3tracer", @@ -3894,7 +4669,7 @@ dependencies = [ "move-prover", "move-stackless-bytecode", "num 0.4.0", - "serde 1.0.130", + "serde 1.0.145", "serde_json", "simplelog", ] @@ -3910,7 +4685,7 @@ dependencies = [ "config", "directories", "petgraph 0.6.0", - "serde 1.0.130", + "serde 1.0.145", "serde-value", "tint", ] @@ -3961,7 +4736,7 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", ] [[package]] @@ -4182,9 +4957,9 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c38e3aecd2b21cb3959637b883bb3714bc7e43f0268b9a29d3743ee3e55cdd2" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] @@ -4223,6 +4998,43 @@ dependencies = [ "winapi", ] +[[package]] +name = "reqwest" +version = "0.11.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b75aa69a3f06bbcc66ede33af2af253c6f7a86b1ca0033f60c580a27074fbf92" +dependencies = [ + "base64", + "bytes", + "encoding_rs", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "hyper", + "hyper-tls", + "ipnet", + "js-sys", + "lazy_static 1.4.0", + "log", + "mime", + "native-tls", + "percent-encoding", + "pin-project-lite", + "serde 1.0.145", + "serde_json", + "serde_urlencoded", + "tokio", + "tokio-native-tls", + "tower-service", + "url", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", + "winreg", +] + [[package]] name = "ripemd160" version = "0.9.1" @@ -4250,9 +5062,9 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e33d7b2abe0c340d8797fe2907d3f20d3b5ea5908683618bfe80df7f621f672a" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] @@ -4288,6 +5100,12 @@ dependencies = [ "semver 0.9.0", ] +[[package]] +name = "rustversion" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24c8ad4f0c00e1eb5bc7614d236a7f1300e3dbd76b68cac8e06fb00b015ad8d8" + [[package]] name = "rusty-fork" version = "0.3.0" @@ -4335,9 +5153,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baeb2780690380592f86205aa4ee49815feb2acad8c2f59e6dd207148c3f1fcd" dependencies = [ "proc-macro-crate", - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", +] + +[[package]] +name = "schannel" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88d6731146462ea25d9244b2ed5fd1d716d25c52e4d54aa4fb0f3c4e9854dbe2" +dependencies = [ + "lazy_static 1.4.0", + "windows-sys", ] [[package]] @@ -4346,6 +5174,29 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +[[package]] +name = "security-framework" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dc14f172faf8a0194a3aded622712b0de276821addc574fa54fc0a1167e10dc" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0160a13a177a45bfb43ce71c01580998474f556ad854dcbca936dd2841a5c556" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "semver" version = "0.9.0" @@ -4361,7 +5212,7 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "568a8e6258aa33c13358f81fd834adb854c6f7c9468520910a9b1e8fac068012" dependencies = [ - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -4378,9 +5229,9 @@ checksum = "9dad3f759919b92c3068c696c15c3d17238234498bbdcc80f2c469606f948ac8" [[package]] name = "serde" -version = "1.0.130" +version = "1.0.145" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" +checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b" dependencies = [ "serde_derive", ] @@ -4403,7 +5254,7 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "12c47087018ec281d1cdab673d36aea22d816b54d498264029c05d5fa1910da6" dependencies = [ - "serde 1.0.130", + "serde 1.0.145", "thiserror", ] @@ -4414,7 +5265,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "167450ba550f903a2b35a81ba3ca387585189e2430e3df6b94b95f3bec2f26bd" dependencies = [ "once_cell", - "serde 1.0.130", + "serde 1.0.145", "thiserror", ] @@ -4425,7 +5276,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c" dependencies = [ "ordered-float", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -4434,7 +5285,7 @@ version = "0.11.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "16ae07dd2f88a366f15bd0632ba725227018c69a1c8550a927324f8eb8368bb9" dependencies = [ - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -4444,18 +5295,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e18acfa2f90e8b735b2836ab8d538de304cbb6729a7360729ea5a895d15a622" dependencies = [ "half", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] name = "serde_derive" -version = "1.0.130" +version = "1.0.145" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b" +checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] @@ -4466,7 +5317,17 @@ checksum = "bcbd0344bc6533bc7ec56df11d42fb70f1b912351c0825ccb7211b59d8af7cf5" dependencies = [ "itoa 1.0.1", "ryu", - "serde 1.0.130", + "serde 1.0.145", +] + +[[package]] +name = "serde_regex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8136f1a4ea815d7eac4101cfd0b16dc0cb5e1fe1b8609dfd728058656b7badf" +dependencies = [ + "regex", + "serde 1.0.145", ] [[package]] @@ -4475,9 +5336,21 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dc6b7951b17b051f3210b063f12cc17320e2fe30ae05b0fe2a3abb068551c76" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa 1.0.1", + "ryu", + "serde 1.0.145", ] [[package]] @@ -4488,7 +5361,7 @@ checksum = "15654ed4ab61726bf918a39cb8d98a2e2995b002387807fa6ba58fdf7f59bb23" dependencies = [ "dtoa", "linked-hash-map", - "serde 1.0.130", + "serde 1.0.145", "yaml-rust", ] @@ -4603,7 +5476,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29fd5867f1c4f2c5be079aee7a2adf1152ebb04a4bc4d341f504b7dece607ed4" dependencies = [ "libc", - "mio", + "mio 0.7.13", "signal-hook", ] @@ -4622,6 +5495,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f0242b8e50dd9accdd56170e94ca1ebd223b098eb9c83539a6e367d0f36ae68" +[[package]] +name = "similar" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e24979f63a11545f5f2c60141afe249d4f19f84581ea2138065e400941d83d3" + [[package]] name = "simplelog" version = "0.9.0" @@ -4633,6 +5512,12 @@ dependencies = [ "termcolor", ] +[[package]] +name = "siphasher" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bd3e3206899af3f8b12af284fafc038cc1dc2b41d1b89dd17297221c5d225de" + [[package]] name = "sized-chunks" version = "0.6.5" @@ -4658,6 +5543,17 @@ dependencies = [ "deunicode", ] +[[package]] +name = "sluice" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d7400c0eff44aa2fcb5e31a5f24ba9716ed90138769e4977a2ba6014ae63eb5" +dependencies = [ + "async-channel", + "futures-core", + "futures-io", +] + [[package]] name = "smallvec" version = "1.7.0" @@ -4676,6 +5572,16 @@ dependencies = [ "structopt 0.3.25", ] +[[package]] +name = "socket2" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66d72b759436ae32898a2af0a14218dbf55efde3feeb170eb623637db85ee1e0" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "spec-flatten" version = "0.1.0" @@ -4702,6 +5608,19 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "string_cache" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "213494b7a2b503146286049378ce02b482200519accc31872ee8be91fa820a08" +dependencies = [ + "new_debug_unreachable", + "once_cell", + "parking_lot 0.12.1", + "phf_shared", + "precomputed-hash", +] + [[package]] name = "strip-ansi-escapes" version = "0.1.1" @@ -4764,9 +5683,9 @@ checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0" dependencies = [ "heck 0.3.2", "proc-macro-error", - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] @@ -4798,13 +5717,13 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.74" +version = "1.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1873d832550d4588c3dbc20f01361ab00bfe741048f71e3fecf145a7cc18b29c" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "unicode-xid 0.2.2", + "unicode-ident", ] [[package]] @@ -4813,9 +5732,9 @@ version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", "unicode-xid 0.2.2", ] @@ -4838,7 +5757,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fdc03d14ed79a75163d3509ebf1970a2ec67945c5cac68d947d1dddace43cec0" dependencies = [ "cfg-expr", - "serde 1.0.130", + "serde 1.0.145", "target-lexicon", ] @@ -4858,9 +5777,9 @@ dependencies = [ [[package]] name = "tera" -version = "1.7.1" +version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2617ab2fb1de8587a988a761692e59895438bebf404725d4f2123251f60bf23e" +checksum = "7c9783d6ff395ae80cf17ed9a25360e7ba37742a79fa8fddabb073c5c7c8856d" dependencies = [ "chrono", "chrono-tz", @@ -4872,7 +5791,7 @@ dependencies = [ "pest_derive", "rand 0.8.4", "regex", - "serde 1.0.130", + "serde 1.0.145", "serde_json", "slug", "unic-segment", @@ -4889,6 +5808,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "term" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f" +dependencies = [ + "dirs-next", + "rustversion", + "winapi", +] + [[package]] name = "termcolor" version = "1.1.2" @@ -4940,29 +5870,29 @@ checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" [[package]] name = "thiserror" -version = "1.0.26" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93119e4feac1cbe6c798c34d3a53ea0026b0b1de6a120deef895137c0529bfe2" +checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.26" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "060d69a0afe7796bf42e9e2ff91f5ee691fb15c53d38b4b62a9a53eb23164745" +checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] name = "thread_local" -version = "1.1.3" +version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd" +checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180" dependencies = [ "once_cell", ] @@ -5002,7 +5932,7 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" dependencies = [ - "serde 1.0.130", + "serde 1.0.145", "serde_json", ] @@ -5023,33 +5953,57 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" [[package]] name = "tokio" -version = "1.9.0" +version = "1.19.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b7b349f11a7047e6d1276853e612d152f5e8a352c61917887cc2169e2366b4c" +checksum = "c51a52ed6686dd62c320f9b89299e9dfb46f730c7a48e635c19f21d116cb1439" dependencies = [ - "autocfg", "bytes", "libc", "memchr", - "mio", + "mio 0.8.4", "num_cpus", "once_cell", - "parking_lot 0.11.1", + "parking_lot 0.12.1", "pin-project-lite", "signal-hook-registry", + "socket2", "tokio-macros", "winapi", ] [[package]] name = "tokio-macros" -version = "1.3.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54473be61f4ebe4efd09cec9bd5d16fa51d70ea0192213d754d2d500457db110" +checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", +] + +[[package]] +name = "tokio-native-tls" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b" +dependencies = [ + "native-tls", + "tokio", +] + +[[package]] +name = "tokio-util" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc463cd8deddc3770d20f9852143d50bf6094e640b485cb2e189a2099085ff45" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", + "tracing", ] [[package]] @@ -5059,9 +6013,27 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" dependencies = [ "indexmap", - "serde 1.0.130", + "serde 1.0.145", +] + +[[package]] +name = "toml_edit" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5376256e44f2443f8896ac012507c19a012df0fe8758b55246ae51a2279db51f" +dependencies = [ + "combine", + "indexmap", + "itertools 0.10.1", + "serde 1.0.145", ] +[[package]] +name = "tower-service" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6bc1c9ce2b5135ac7f93c72918fc37feb872bdc6a5533a8b85eb4b86bfdae52" + [[package]] name = "tracing" version = "0.1.26" @@ -5069,6 +6041,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09adeb8c97449311ccd28a427f96fb563e7fd31aabf994189879d9da2394b89d" dependencies = [ "cfg-if 1.0.0", + "log", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -5080,18 +6053,29 @@ version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c42e6fa53307c8a17e4ccd4dc81cf5ec38db9209f59b222210375b54ee40d1e2" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] [[package]] name = "tracing-core" -version = "0.1.21" +version = "0.1.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f4ed65637b8390770814083d20756f87bfa2c21bf2f110babdc5438351746e4" +checksum = "7b7358be39f2f274f322d2aaed611acc57f382e8eb1e5b48cb9ae30933495ce7" dependencies = [ - "lazy_static 1.4.0", + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-futures" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97d095ae15e245a057c8e8451bab9b3ee1e1f68e9ba2b4fbc18d0ac5237835f2" +dependencies = [ + "pin-project", + "tracing", ] [[package]] @@ -5107,13 +6091,13 @@ dependencies = [ [[package]] name = "tracing-subscriber" -version = "0.3.3" +version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "245da694cc7fc4729f3f418b304cb57789f1bed2a78c575407ab8a23f53cb4d3" +checksum = "60db860322da191b40952ad9affe65ea23e7dd6a5c442c2c42865810c6ab8e6b" dependencies = [ "ansi_term 0.12.1", - "lazy_static 1.4.0", "matchers", + "once_cell", "regex", "sharded-slab", "smallvec", @@ -5133,6 +6117,12 @@ dependencies = [ "rlp", ] +[[package]] +name = "try-lock" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" + [[package]] name = "trybuild" version = "1.0.53" @@ -5141,7 +6131,7 @@ checksum = "9d664de8ea7e531ad4c0f5a834f20b8cb2b8e6dfe88d05796ee7887518ed67b9" dependencies = [ "glob", "lazy_static 1.4.0", - "serde 1.0.130", + "serde 1.0.145", "serde_json", "termcolor", "toml", @@ -5200,6 +6190,15 @@ dependencies = [ "static_assertions", ] +[[package]] +name = "uncased" +version = "0.9.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09b01702b0fd0b3fadcf98e098780badda8742d4f4a7676615cad90e8ac73622" +dependencies = [ + "version_check", +] + [[package]] name = "unic-char-property" version = "0.9.0" @@ -5259,6 +6258,12 @@ dependencies = [ "matches", ] +[[package]] +name = "unicode-ident" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf" + [[package]] name = "unicode-normalization" version = "0.1.17" @@ -5318,7 +6323,7 @@ dependencies = [ "idna", "matches", "percent-encoding", - "serde 1.0.130", + "serde 1.0.145", ] [[package]] @@ -5327,6 +6332,22 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "936e4b492acfd135421d8dca4b1aa80a7bfc26e702ef3af710e0752684df5372" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + +[[package]] +name = "value-bag" +version = "1.0.0-alpha.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2209b78d1249f7e6f3293657c9779fe31ced465df091bbd433a1cf88e916ec55" +dependencies = [ + "ctor", + "version_check", +] + [[package]] name = "variant_count" version = "1.1.0" @@ -5334,9 +6355,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aae2faf80ac463422992abf4de234731279c058aaf33171ca70277c98406b124" dependencies = [ "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", ] +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "vec_map" version = "0.8.2" @@ -5366,7 +6393,7 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d257817081c7dffcdbab24b9e62d2def62e2ff7d00b1c20062551e6cccc145ff" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", ] @@ -5379,6 +6406,12 @@ dependencies = [ "libc", ] +[[package]] +name = "waker-fn" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" + [[package]] name = "walkdir" version = "2.3.2" @@ -5390,6 +6423,16 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "want" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" +dependencies = [ + "log", + "try-lock", +] + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" @@ -5402,6 +6445,12 @@ version = "0.10.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "wasm-bindgen" version = "0.2.71" @@ -5421,12 +6470,24 @@ dependencies = [ "bumpalo", "lazy_static 1.4.0", "log", - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", "wasm-bindgen-shared", ] +[[package]] +name = "wasm-bindgen-futures" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e67a5806118af01f0d9045915676b22aaebecf4178ae7021bc171dab0b897ab" +dependencies = [ + "cfg-if 1.0.0", + "js-sys", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "wasm-bindgen-macro" version = "0.2.71" @@ -5443,9 +6504,9 @@ version = "0.2.71" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cc053ec74d454df287b9374ee8abb36ffd5acb95ba87da3ba5b7d3fe20eb401e" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -5466,11 +6527,30 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "wepoll-ffi" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d743fdedc5c64377b5fc2bc036b01c7fd642205a0d96356034ae3404d49eb7fb" +dependencies = [ + "cc", +] + +[[package]] +name = "whoami" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524b58fa5a20a2fb3014dd6358b70e6579692a56ef6fce928834e488f42f65e8" +dependencies = [ + "wasm-bindgen", + "web-sys", +] + [[package]] name = "widestring" -version = "0.4.3" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c168940144dd21fd8046987c16a46a33d5fc84eec29ef9dcddc2ac9e31526b7c" +checksum = "17882f045410753661207383517a6f62ec3dbeb6a4ed2acce01f0728238d1983" [[package]] name = "winapi" @@ -5503,6 +6583,58 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-sys" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" +dependencies = [ + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" + +[[package]] +name = "windows_i686_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" + +[[package]] +name = "windows_i686_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.36.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" + +[[package]] +name = "winreg" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d" +dependencies = [ + "winapi", +] + [[package]] name = "wyz" version = "0.2.0" @@ -5539,7 +6671,7 @@ dependencies = [ "nextest-runner", "rayon", "regex", - "serde 1.0.130", + "serde 1.0.145", "serde_json", "supports-color", "toml", @@ -5560,7 +6692,7 @@ dependencies = [ "log", "once_cell", "ouroboros", - "serde 1.0.130", + "serde 1.0.145", "toml", ] @@ -5571,7 +6703,7 @@ dependencies = [ "camino", "guppy", "once_cell", - "serde 1.0.130", + "serde 1.0.145", "toml", "x-core", ] @@ -5622,8 +6754,8 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bdff2024a851a322b08f179173ae2ba620445aef1e838f0c196820eade4ae0c7" dependencies = [ - "proc-macro2 1.0.28", + "proc-macro2 1.0.43", "quote 1.0.9", - "syn 1.0.74", + "syn 1.0.99", "synstructure", ] diff --git a/Cargo.toml b/Cargo.toml index 82231f04a4..9b9068e875 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -95,8 +95,9 @@ default-members = [ "language/tools/move-unit-test", ] -# [profile.release] -# debug = true +# Dependencies that should be kept in sync through the whole workspace +[workspace.dependencies] +bcs = "0.1.4" [profile.bench] debug = true diff --git a/GRAVEYARD.md b/GRAVEYARD.md new file mode 100644 index 0000000000..d947d6e591 --- /dev/null +++ b/GRAVEYARD.md @@ -0,0 +1,54 @@ +# Language feature idea graveyard + +Move is a smart contract language that counts minimalism as one of its guiding principles. Features are often proposed, but not always implemented. This list exists to record feature proposals that did not progress to acceptance. The goal of maintaining this list is to avoid duplicate proposals, and to explain the rationale for not moving forward with an idea that might seem promising at first blush. + +## Pure data structs + +### Decision +We should let adapters decide how to handle this + +### Rationale +Structs being passed to transactions cause safety issues because deserialization can circumvent modular encapsulation. For example, below make is the only way to create S and the program guarantees that field x is always greater zero; however, if S is obtained via deserialization, this constraint can be violated:: +``` +struct S has drop, copy { x: u64 } +public fun make(x: u64) { assert!(x > 0); S{x} } + +fun(entry) foo(s: S/*no guarantee about s.x*/) { … } +``` + +## Native structs + +### Decision +This is obsolete as regular structs can be used from native code, e.g. struct S { native_handle: u128 } + +### Rationale +As Move evolves to an extensible language, native structs are an important tool for extensibility. However, support for native structs has been removed from the VM (though still supported in compiler and file format). + +### Design +Bring back support for native structs in the VM. They can use a simple uniform representation via handles which are managed by the runtime environment (like u128 or u256 for each and every native struct value), so implementation effort should be small. + +## Signer relaxation for `move_to` + +### Decision +Tables and framework-specific workarounds like `ResourceAccount` make this unneccessary. + +### Rationale +In some usage scenarios of Move, it does not make sense to only allow move_to(s, x) with s a signer. For instance, when Move is running on the EVM, storage is not owned and paid for by the account owner, but by the contract caller, which manages the accounts on behalf of owners. In general, the signer requirement allows one to only create new resources at addresses for which the transaction has a signer, which disables the capability to manage resource collections on behalf of others. + +## Design (Option A) +Drop the requirement of passing a signer to move-to all together. For downwards compatibility reasons, the compiler allows passing either a signer or an address. Rationale for this quite radical step is that in-real world contracts (e.g. the Diem Payment Network) more complex access control patterns than enabled by signer/move_to are needed anyway. Moreover, since only modules which declare a resource R can call move_to, preventing publishing without a signer can still be modeled by modular encapsulation. + +### Design (Option B) +Use an attribute on struct declarations to indicate a signer is not required for publishing this struct. + +### Design (Option C) +Have another function which does not require a signer, e.g. move_to_address. This, however, raises questions of adequate naming and methodology (when to use which function, should there be constraints when which function is allowed, etc.) + +### Design (Option D) +Add no new features and ask clients that want this feature (e.g., the Move -> EVM compilation effort) to implement it via design patterns and/or native functions. See SignerCapability in the Starcoin Account code for one example of a design pattern for (effectively) implementing move_to_address without changing the language. + +### Design (Option E) +Support this via a new internal ability that allows framework designers to implement more powerful native functions with the same encapsulation guarantees as bytecodes. For example, the framework designer can define a function `public native fun move_to_address(a: address, t: T)` that can only be invoked on `T`’s declared in the calling module. + +### Design (Option F) +Use tables when there is a need to index storage without signer. diff --git a/README.md b/README.md index 639e1d020a..440da1545f 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ cd ./language/documentation/tutorial/step_1/BasicCoin docker run -v `pwd`:/project move/cli build ``` +Follow the [language/documentation/tutorial](./language/documentation/tutorial/README.md) to set up move for development. + ## Community * Join us on the [Move Discord](https://discord.gg/cPUmhe24Mz). diff --git a/devtools/x-core/Cargo.toml b/devtools/x-core/Cargo.toml index cad7367c3a..b02bb7fe89 100644 --- a/devtools/x-core/Cargo.toml +++ b/devtools/x-core/Cargo.toml @@ -3,7 +3,7 @@ name = "x-core" version = "0.1.0" authors = ["Diem Association "] description = "Core data structures used by x" -edition = "2018" +edition = "2021" publish = false license = "Apache-2.0" @@ -14,7 +14,7 @@ determinator = "0.7.0" guppy = "0.12.3" indoc = "1.0.3" hex = "0.4.3" -log = "0.4.14" +log = "0.4.17" toml = "0.5.8" once_cell = "1.7.2" ouroboros = "0.9.2" diff --git a/devtools/x-lint/Cargo.toml b/devtools/x-lint/Cargo.toml index 4174311b85..69ddfb7c08 100644 --- a/devtools/x-lint/Cargo.toml +++ b/devtools/x-lint/Cargo.toml @@ -3,7 +3,7 @@ name = "x-lint" version = "0.1.0" authors = ["Diem Association "] description = "Lint engine for x" -edition = "2018" +edition = "2021" publish = false license = "Apache-2.0" diff --git a/devtools/x/Cargo.toml b/devtools/x/Cargo.toml index 2d9c9498ea..f4a0ab058f 100644 --- a/devtools/x/Cargo.toml +++ b/devtools/x/Cargo.toml @@ -3,7 +3,7 @@ name = "x" version = "0.1.0" authors = ["Diem Association "] description = "Diem extended cargo tasks" -edition = "2018" +edition = "2021" publish = false license = "Apache-2.0" @@ -23,7 +23,7 @@ env_logger = "0.8.3" log = "0.4.14" chrono = "0.4.19" globset = "0.4.6" -regex = "1.4.3" +regex = "1.5.5" rayon = "1.5.0" nextest-config = { git = "https://github.com/diem/diem-devtools", rev = "f99a204e3d3f8e503d51d7df42e55c8282b59154" } nextest-runner = { git = "https://github.com/diem/diem-devtools", rev = "f99a204e3d3f8e503d51d7df42e55c8282b59154" } diff --git a/devtools/x/src/lint/guppy.rs b/devtools/x/src/lint/guppy.rs index cc7e1534c3..fcb79f77b6 100644 --- a/devtools/x/src/lint/guppy.rs +++ b/devtools/x/src/lint/guppy.rs @@ -14,6 +14,7 @@ use guppy::{ }; use std::{ collections::{BTreeMap, HashMap}, + fmt::Write as FmtWrite, iter, }; use x_core::{WorkspaceStatus, XCoreContext}; @@ -270,9 +271,7 @@ impl<'cfg> ProjectLinter for DirectDepDups<'cfg> { if versions.len() > 1 { let mut msg = format!("duplicate direct dependency '{}':\n", direct_dep); for (version, packages) in versions { - msg.push_str(&format!(" * {} (", version)); - msg.push_str(&packages.join(", ")); - msg.push_str(")\n"); + writeln!(&mut msg, " * {} ({})", version, &packages.join(", ")).unwrap(); } out.write(LintLevel::Error, msg); } @@ -361,12 +360,14 @@ impl<'cfg> PackageLinter for OverlayFeatures<'cfg> { if !overlays.is_empty() { let mut msg = "overlay features enabled by default:\n".to_string(); for (from_feature, to_package, to_feature) in overlays { - msg.push_str(&format!( - " * {} -> {}/{}\n", + writeln!( + &mut msg, + " * {} -> {}/{}", feature_str(from_feature), to_package, feature_str(to_feature) - )); + ) + .unwrap(); } msg.push_str("Use a line in the [features] section instead.\n"); out.write(LintLevel::Error, msg); diff --git a/docker/move-cli/Dockerfile b/docker/move-cli/Dockerfile index dcdb026b87..a3543ff5e6 100644 --- a/docker/move-cli/Dockerfile +++ b/docker/move-cli/Dockerfile @@ -20,6 +20,7 @@ ENV PATH "/opt/cargo/bin:/usr/lib/golang/bin:/opt/bin:${DOTNET_ROOT}:${DOTNET_RO RUN mkdir -p /github/home && \ mkdir -p /opt/cargo/ && \ mkdir -p /opt/git/ && \ + sed -i -e 's/\r$//' /move/scripts/*.sh && \ /move/scripts/dev_setup.sh -t -b -p -y -d -g -n && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* diff --git a/language/benchmarks/Cargo.toml b/language/benchmarks/Cargo.toml index 8398914568..c2becf96cb 100644 --- a/language/benchmarks/Cargo.toml +++ b/language/benchmarks/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] anyhow = "1.0.52" diff --git a/language/benchmarks/src/move_vm.rs b/language/benchmarks/src/move_vm.rs index 72b4c337e0..46ac387218 100644 --- a/language/benchmarks/src/move_vm.rs +++ b/language/benchmarks/src/move_vm.rs @@ -12,7 +12,7 @@ use move_core_types::{ }; use move_vm_runtime::move_vm::MoveVM; use move_vm_test_utils::BlankStorage; -use move_vm_types::gas_schedule::GasStatus; +use move_vm_types::gas::UnmeteredGasMeter; use once_cell::sync::Lazy; use std::path::PathBuf; @@ -27,6 +27,7 @@ pub fn bench(c: &mut Criterion, fun: &str) { let modules = compile_modules(); let move_vm = MoveVM::new(move_stdlib::natives::all_natives( AccountAddress::from_hex_literal("0x1").unwrap(), + move_stdlib::natives::GasParameters::zeros(), )) .unwrap(); execute(c, &move_vm, modules, fun); @@ -65,7 +66,8 @@ fn execute( let storage = BlankStorage::new(); let sender = CORE_CODE_ADDRESS; let mut session = move_vm.new_session(&storage); - let mut gas_status = GasStatus::new_unmetered(); + + // TODO: we may want to use a real gas meter to make benchmarks more realistic. for module in modules { let mut mod_blob = vec![]; @@ -73,7 +75,7 @@ fn execute( .serialize(&mut mod_blob) .expect("Module serialization error"); session - .publish_module(mod_blob, sender, &mut gas_status) + .publish_module(mod_blob, sender, &mut UnmeteredGasMeter) .expect("Module must load"); } @@ -90,7 +92,7 @@ fn execute( fun_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap_or_else(|err| { panic!( diff --git a/language/documentation/book/src/abilities.md b/language/documentation/book/src/abilities.md index 8d3c2f9b5a..44ee4ee5a7 100644 --- a/language/documentation/book/src/abilities.md +++ b/language/documentation/book/src/abilities.md @@ -49,7 +49,7 @@ If a value has `key`, all values contained inside of that value have `store`. Th ## Builtin Types -Most primitive, builtin types have `copy`, `drop`, and `store` with the exception of `signer`, which just has `store` +Most primitive, builtin types have `copy`, `drop`, and `store` with the exception of `signer`, which just has `drop` * `bool`, `u8`, `u64`, `u128`, and `address` all have `copy`, `drop`, and `store`. * `signer` has `drop` diff --git a/language/documentation/book/src/abort-and-assert.md b/language/documentation/book/src/abort-and-assert.md index 87ed86bafb..ce3e87478e 100644 --- a/language/documentation/book/src/abort-and-assert.md +++ b/language/documentation/book/src/abort-and-assert.md @@ -205,4 +205,3 @@ let b = else abort 42; // ^^^^^^^^ `abort 42` has type `bool` ``` -```` diff --git a/language/documentation/book/src/address.md b/language/documentation/book/src/address.md index 73753bcd1a..88d8b3f5e8 100644 --- a/language/documentation/book/src/address.md +++ b/language/documentation/book/src/address.md @@ -16,7 +16,7 @@ literals. To distinguish when an address is being used in an expression context or not, the syntax when using an address differs depending on the context where it's used: -* When an address is used as an expression the address must be prefixed by the `@` character, i.e., `@`[``](./integers.md)` or `@`. +* When an address is used as an expression the address must be prefixed by the `@` character, i.e., [`@`](./integers.md) or `@`. * Outside of expression contexts, the address may be written without the leading `@` character, i.e., [``](./integers.md) or ``. In general, you can think of `@` as an operator that takes an address from being a namespace item to being an expression item. diff --git a/language/documentation/book/src/coding-conventions.md b/language/documentation/book/src/coding-conventions.md index dc305bd5ac..0430369fe4 100644 --- a/language/documentation/book/src/coding-conventions.md +++ b/language/documentation/book/src/coding-conventions.md @@ -4,14 +4,14 @@ This section lays out some basic coding conventions for Move that the Move team ## Naming -- **Module names**: should be lower snake case, e.g., `fixed_point32`, `vector` -- **Type names**: should be camel case if they are not a native type, e.g., `Coin`, `RoleId` -- **Function names**: should be lower snake case, e.g., `destroy_empty` -- **Constant names**: should be upper snake case, e.g., `REQUIRES_CAPABILITY` +- **Module names**: should be lower snake case, e.g., `fixed_point32`, `vector`. +- **Type names**: should be camel case if they are not a native type, e.g., `Coin`, `RoleId`. +- **Function names**: should be lower snake case, e.g., `destroy_empty`. +- **Constant names**: should be upper snake case, e.g., `REQUIRES_CAPABILITY`. - Generic types should be descriptive, or anti-descriptive where appropriate, e.g., `T` or `Element` for the Vector generic type parameter. Most of the time the "main" type in a module should be the same name as the module e.g., `option::Option`, `fixed_point32::FixedPoint32`. -- **Module file names**: should be the same as the module name e.g., `Option.move` +- **Module file names**: should be the same as the module name e.g., `Option.move`. - **Script file names**: should be lower snake case and should match the name of the “main” function in the script. -- **Mixed file names**: If the file contains multiple modules and/or scripts, the file name should be lower_snake_case, where the name does not match any particular module/script inside. +- **Mixed file names**: If the file contains multiple modules and/or scripts, the file name should be lower snake case, where the name does not match any particular module/script inside. ## Imports @@ -19,9 +19,9 @@ This section lays out some basic coding conventions for Move that the Move team - Functions should be imported and used fully qualified from the module in which they are declared, and not imported at the top level. - Types should be imported at the top-level. Where there are name clashes, `as` should be used to rename the type locally as appropriate. -For example, if there is a module +For example, if there is a module: -```move= +```move module 0x1::foo { struct Foo { } const CONST_FOO: u64 = 0; @@ -32,7 +32,7 @@ module 0x1::foo { this would be imported and used as: -```move= +```move module 0x1::bar { use 0x1::foo::{Self, Foo}; @@ -49,7 +49,7 @@ module 0x1::bar { And, if there is a local name-clash when importing two modules: -```move= +```move module other_foo { struct Foo {} ... @@ -58,19 +58,19 @@ module other_foo { module 0x1::importer { use 0x1::other_foo::Foo as OtherFoo; use 0x1::foo::Foo; -.... + ... } ``` ## Comments -- Each module, struct, and public function declaration should be commented -- Move has doc comments `///`, regular single-line comments `//`, block comments `/* */`, and block doc comments `/** */` +- Each module, struct, and public function declaration should be commented. +- Move has doc comments `///`, regular single-line comments `//`, block comments `/* */`, and block doc comments `/** */`. ## Formatting The Move team plans to write an autoformatter to enforce formatting conventions. However, in the meantime: -- Four space indentation should be used except for `script` and `address` blocks whose contents should not be indented -- Lines should be broken if they are longer than 100 characters -- Structs and constants should be declared before all functions in a module +- Four space indentation should be used except for `script` and `address` blocks whose contents should not be indented. +- Lines should be broken if they are longer than 100 characters. +- Structs and constants should be declared before all functions in a module. diff --git a/language/documentation/book/src/functions.md b/language/documentation/book/src/functions.md index 5ced412477..23d3070f97 100644 --- a/language/documentation/book/src/functions.md +++ b/language/documentation/book/src/functions.md @@ -322,13 +322,13 @@ fun zero(): u64 { 0 } Here `: u64` indicates that the function's return type is `u64`. -Using tuples, a function can return multiple values +Using tuples, a function can return multiple values: ```move fun one_two_three(): (u64, u64, u64) { (0, 1, 2) } ``` -If no return type is specified, the function has an implicit return type of unit `()`. These functions are equivalent +If no return type is specified, the function has an implicit return type of unit `()`. These functions are equivalent: ```move fun just_unit(): () { () } @@ -336,9 +336,9 @@ fun just_unit() { () } fun just_unit() { } ``` -`script` functions must have a return type of unit `()` +`script` functions must have a return type of unit `()`: -```move= +```move script { fun do_nothing() { } diff --git a/language/documentation/book/src/global-storage-structure.md b/language/documentation/book/src/global-storage-structure.md index 83cf8a2ea0..dc624e60c9 100644 --- a/language/documentation/book/src/global-storage-structure.md +++ b/language/documentation/book/src/global-storage-structure.md @@ -2,7 +2,7 @@ The purpose of Move programs is to [read from and write to](./global-storage-operators.md) tree-shaped persistent global storage. Programs cannot access the filesystem, network, or any other data outside of this tree. -In pseudocode, the global storage looks something like +In pseudocode, the global storage looks something like: ```move struct GlobalStorage { diff --git a/language/documentation/book/src/integers.md b/language/documentation/book/src/integers.md index a78f1a8668..151901f7e6 100644 --- a/language/documentation/book/src/integers.md +++ b/language/documentation/book/src/integers.md @@ -12,6 +12,8 @@ Move supports three unsigned integer types: `u8`, `u64`, and `u128`. Values of t Literal values for these types are specified either as a sequence of digits (e.g.,`112`) or as hex literals, e.g., `0xFF`. The type of the literal can optionally be added as a suffix, e.g., `112u8`. If the type is not specified, the compiler will try to infer the type from the context where the literal is used. If the type cannot be inferred, it is assumed to be `u64`. +Decimal literals can be separated by underscores for grouping and readability. (e.g.,`1_234_5678`, `1_000u128`). However, hexadecimal literals cannot. + If a literal is too large for its specified (or inferred) size range, an error is reported. ### Examples @@ -21,6 +23,7 @@ If a literal is too large for its specified (or inferred) size range, an error i let explicit_u8 = 1u8; let explicit_u64 = 2u64; let explicit_u128 = 3u128; +let explicit_u64_underscored = 154_322_973u64; // literals with simple inference let simple_u8: u8 = 1; @@ -32,12 +35,12 @@ let complex_u8 = 1; // inferred: u8 // right hand argument to shift must be u8 let _unused = 10 << complex_u8; -let x: u8 = 0; +let x: u8 = 38; let complex_u8 = 2; // inferred: u8 // arguments to `+` must have the same type let _unused = x + complex_u8; -let complex_u128 = 3; // inferred: u128 +let complex_u128 = 133_876; // inferred: u128 // inferred from function argument type function_that_takes_u128(complex_u128); @@ -63,7 +66,6 @@ All arithmetic operations abort instead of behaving in a way that mathematical i | `%` | modular division | The divisor is `0` | `/` | truncating division | The divisor is `0` - ### Bitwise The integer types support the following bitwise operations that treat each number as a series of individual bits, either 0 or 1, instead of as numerical integer values. @@ -73,7 +75,7 @@ Bitwise operations do not abort. | Syntax | Operation | Description |--------|------------|------------ | `&` | bitwise and| Performs a boolean and for each bit pairwise -| `|` | bitwise or | Performs a boolean or for each bit pairwise +| `\|` | bitwise or | Performs a boolean or for each bit pairwise | `^` | bitwise xor| Performs a boolean exclusive or for each bit pairwise ### Bit Shifts diff --git a/language/documentation/book/src/modules-and-scripts.md b/language/documentation/book/src/modules-and-scripts.md index 8705991591..f3580e4ded 100644 --- a/language/documentation/book/src/modules-and-scripts.md +++ b/language/documentation/book/src/modules-and-scripts.md @@ -18,14 +18,14 @@ script { } ``` -A `script` block must start with all of its [use](./uses.md) declarations, followed by any [constants](./constants.md) and (finally) the main +A `script` block must start with all of its [`use`](./uses.md) declarations, followed by any [constants](./constants.md) and (finally) the main [function](./functions.md) declaration. The main function can have any name (i.e., it need not be called `main`), is the only function in a script block, can have any number of arguments, and must not return a value. Here is an example with each of these components: ```move script { - // Import the Debug module published at the named account address std. + // Import the debug module published at the named account address std. use std::debug; const ONE: u64 = 1; @@ -41,7 +41,7 @@ Scripts have very limited power—they cannot declare friends, struct types or a ### Modules -A Module has the following syntax: +A module has the following syntax: ```text module
:: { @@ -54,11 +54,11 @@ where `
` is a valid [named or literal address](./address.md). For example: ```move -module 0x42::Test { +module 0x42::test { struct Example has copy, drop { i: u64 } use std::debug; - friend 0x42::AnotherTest; + friend 0x42::another_test; const ONE: u64 = 1; @@ -70,19 +70,19 @@ module 0x42::Test { } ``` -The `module 0x42::Test` part specifies that the module `Test` will be published under the [account address](./address.md) `0x42` in [global storage](./global-storage-structure.md). +The `module 0x42::test` part specifies that the module `test` will be published under the [account address](./address.md) `0x42` in [global storage](./global-storage-structure.md). Modules can also be declared using [named addresses](./address.md). For example: ```move module test_addr::test { - struct Example has copy, drop { a: address} + struct Example has copy, drop { a: address } use std::debug; friend test_addr::another_test; public fun print() { - let example = Example { a: @test_addr}; + let example = Example { a: @test_addr }; debug::print(&example) } } @@ -92,7 +92,7 @@ Because named addresses only exist at the source language level and during compi named addresses will be fully substituted for their value at the bytecode level. For example if we had the following code: -```move= +```move script { fun example() { my_addr::m::foo(@my_addr); @@ -103,7 +103,7 @@ script { and we compiled it with `my_addr` set to `0xC0FFEE`, then it would be equivalent to the following operationally: -```move= +```move script { fun example() { 0xC0FFEE::m::foo(@0xC0FFEE); @@ -112,7 +112,7 @@ script { ``` However at the source level, these _are not equivalent_—the function -`M::foo` _must_ be accessed through the `MyAddr` named address, and not through +`m::foo` _must_ be accessed through the `my_addr` named address, and not through the numerical value assigned to that address. Module names can start with letters `a` to `z` or letters `A` to `Z`. After the first character, module names can contain underscores `_`, letters `a` to `z`, letters `A` to `Z`, or digits `0` to `9`. @@ -122,10 +122,10 @@ module my_module {} module foo_bar_42 {} ``` -Typically, module names start with an uppercase letter. A module named `my_module` should be stored in a source file named `my_module.move`. +Typically, module names start with an lowercase letter. A module named `my_module` should be stored in a source file named `my_module.move`. All elements inside a `module` block can appear in any order. Fundamentally, a module is a collection of [`types`](./structs-and-resources.md) and [`functions`](./functions.md). -[Uses](./uses.md) import types from other modules. -[Friends](./friends.md) specify a list of trusted modules. -[Constants](./constants.md) define private constants that can be used in the functions of a module. +The [`use`](./uses.md) keyword is used to import types from other modules. +The [`friend`](./friends.md) keyword specifies a list of trusted modules. +The [`const`](./constants.md) keyword defines private constants that can be used in the functions of a module. diff --git a/language/documentation/book/src/packages.md b/language/documentation/book/src/packages.md index 37947cc59d..c4abace5ca 100644 --- a/language/documentation/book/src/packages.md +++ b/language/documentation/book/src/packages.md @@ -122,6 +122,7 @@ individually: ### Declaration Let's say we have a Move module in `example_pkg/sources/A.move` as follows: + ```move module named_addr::A { public fun x(): address { @named_addr } diff --git a/language/documentation/book/src/references.md b/language/documentation/book/src/references.md index eb57079a50..57a5b99e1f 100644 --- a/language/documentation/book/src/references.md +++ b/language/documentation/book/src/references.md @@ -94,7 +94,7 @@ A mutable reference can be used in a context where an immutable reference is exp ```move let x = 7; -let y: &mut u64 = &mut x; +let y: &u64 = &mut x; ``` This works because the under the hood, the compiler inserts `freeze` instructions where they are diff --git a/language/documentation/book/src/signer.md b/language/documentation/book/src/signer.md index 0707ff957a..820963c8b2 100644 --- a/language/documentation/book/src/signer.md +++ b/language/documentation/book/src/signer.md @@ -26,7 +26,7 @@ However, `signer` values are special because they cannot be created via literals instructions--only by the Move VM. Before the VM runs a script with parameters of type `signer`, it will automatically create `signer` values and pass them into the script: -```move= +```move script { use std::signer; fun main(s: signer) { @@ -37,10 +37,10 @@ script { This script will abort with code `0` if the script is sent from any address other than `0x42`. -A transaction script can have an arbitrary number of `signer`s as long as the signers are a prefix -to any other arguments. In other words, all of the signer arguments must come first: +A transaction script can have an arbitrary number of `signer`s as long as the `signer`s are a prefix +to any other arguments. In other words, all of the `signer` arguments must come first: -```move= +```move script { use std::signer; fun main(s1: signer, s2: signer, x: u64, y: u8) { @@ -57,10 +57,10 @@ swap between `s1` and `s2`. The `std::signer` standard library module provides two utility functions over `signer` values: -| Function | Description | -| ------------------------------------------- | ------------------------------------------------------------- | -| `signer::address_of(&signer): address` | Return the `address` wrapped by this `&signer`. | -| `signer::borrow_address(&signer): &address` | Return a reference to the `address` wrapped by this `&signer` | +| Function | Description | +| ------------------------------------------- | ------------------------------------------------------------- | +| `signer::address_of(&signer): address` | Return the `address` wrapped by this `&signer`. | +| `signer::borrow_address(&signer): &address` | Return a reference to the `address` wrapped by this `&signer`. | In addition, the `move_to(&signer, T)` [global storage operator](./global-storage-operators.md) requires a `&signer` argument to publish a resource `T` under `signer.address`'s account. This @@ -70,4 +70,4 @@ ensures that only an authenticated user can elect to publish a resource under th Unlike simple scalar values, `signer` values are not copyable, meaning they cannot be copied (from any operation whether it be through an explicit [`copy`](./variables.md#move-and-copy) instruction -or through a [dereference `*`](./references.md#reference-operators)). +or through a [dereference `*`](./references.md#reading-and-writing-through-references). diff --git a/language/documentation/book/src/standard-library.md b/language/documentation/book/src/standard-library.md index 8f17814977..7dcd8f2251 100644 --- a/language/documentation/book/src/standard-library.md +++ b/language/documentation/book/src/standard-library.md @@ -486,12 +486,10 @@ Used for extension points, should be not used under most circumstances. Construc ## fixed_point32 - The `fixed_point32` module defines a fixed-point numeric type with 32 integer bits and 32 fractional bits. Internally, this is represented as a `u64` integer wrapped in a struct to make a unique `fixed_point32` type. Since the numeric representation is a binary one, some decimal values may not be exactly representable, but it provides more than 9 decimal digits of precision both before and after the decimal point (18 digits total). For comparison, double precision floating-point has less than 16 decimal digits of precision, so you should be careful about using floating-point to convert these values to decimal. ### Types - Represents a fixed-point numeric number with 32 fractional bits. ```move diff --git a/language/documentation/book/src/structs-and-resources.md b/language/documentation/book/src/structs-and-resources.md index ba73d2f1d6..a073a44cd4 100644 --- a/language/documentation/book/src/structs-and-resources.md +++ b/language/documentation/book/src/structs-and-resources.md @@ -32,7 +32,7 @@ module m { Structs cannot be recursive, so the following definition is invalid: -```move= +```move struct Foo { x: Foo } // ^ error! Foo cannot contain Foo ``` @@ -42,7 +42,7 @@ to be used with certain operations (that copy it, drop it, store it in global st a storage schema), structs can be granted [abilities](./abilities.md) by annotating them with `has `: -```move= +```move address 0x2 { module m { struct Foo has copy, drop { x: u64, y: bool } @@ -54,7 +54,7 @@ For more details, see the [annotating structs](./abilities.md#annotating-structs ### Naming -Structs must start with a capital letter `A` to `Z`. After the first letter, constant names can +Structs must start with a capital letter `A` to `Z`. After the first letter, struct names can contain underscores `_`, letters `a` to `z`, letters `A` to `Z`, or digits `0` to `9`. ```move @@ -73,7 +73,7 @@ features. It may or may not be removed later. Values of a struct type can be created (or "packed") by indicating the struct name, followed by value for each field: -```move= +```move address 0x2 { module m { struct Foo has drop { x: u64, y: bool } @@ -102,7 +102,7 @@ This is called sometimes called "field name punning". Struct values can be destroyed by binding or assigning them patterns. -```move= +```move address 0x2 { module m { struct Foo { x: u64, y: bool } @@ -122,6 +122,7 @@ module m { fun example_destroy_foo_wildcard() { let foo = Foo { x: 3, y: false }; let Foo { x, y: _ } = foo; + // only one new binding since y was bound to a wildcard // x: u64 = 3 } @@ -130,6 +131,7 @@ module m { let x: u64; let y: bool; Foo { x, y } = Foo { x: 3, y: false }; + // mutating existing variables x & y // x = 3, y = false } @@ -137,6 +139,7 @@ module m { fun example_foo_ref() { let foo = Foo { x: 3, y: false }; let Foo { x, y } = &foo; + // two new bindings // x: &u64 // y: &bool @@ -145,6 +148,7 @@ module m { fun example_foo_ref_mut() { let foo = Foo { x: 3, y: false }; let Foo { x, y } = &mut foo; + // two new bindings // x: &mut u64 // y: &mut bool @@ -154,9 +158,10 @@ module m { let bar = Bar { foo: Foo { x: 3, y: false } }; let Bar { foo: Foo { x, y } } = bar; // ^ nested pattern + // two new bindings // x: u64 = 3 - // foo_y: bool = false + // y: bool = false } fun example_destroy_baz() { @@ -172,7 +177,7 @@ module m { The `&` and `&mut` operator can be used to create references to structs or fields. These examples include some optional type annotations (e.g., `: &Foo`) to demonstrate the type of operations. -```move= +```move let foo = Foo { x: 3, y: true }; let foo_ref: &Foo = &foo; let y: bool = foo_ref.y; // reading a field via a reference to the struct @@ -182,18 +187,18 @@ let x_ref_mut: &mut u64 = &mut foo.x; *x_ref_mut = 42; // modifying a field via a mutable reference ``` -It is possible to borrow inner fields of nested structs. +It is possible to borrow inner fields of nested structs: -```move= +```move let foo = Foo { x: 3, y: true }; let bar = Bar { foo }; let x_ref = &bar.foo.x; ``` -You can also borrow a field via a reference to a struct. +You can also borrow a field via a reference to a struct: -```move= +```move let foo = Foo { x: 3, y: true }; let foo_ref = &foo; let x_ref = &foo_ref.x; @@ -202,9 +207,9 @@ let x_ref = &foo_ref.x; ### Reading and Writing Fields -If you need to read and copy a field's value, you can then dereference the borrowed field +If you need to read and copy a field's value, you can then dereference the borrowed field: -```move= +```move let foo = Foo { x: 3, y: true }; let bar = Bar { foo: copy foo }; let x: u64 = *&foo.x; @@ -215,37 +220,37 @@ let foo2: Foo = *&bar.foo; If the field is implicitly copyable, the dot operator can be used to read fields of a struct without any borrowing. (Only scalar values with the `copy` ability are implicitly copyable.) -```move= +```move let foo = Foo { x: 3, y: true }; let x = foo.x; // x == 3 let y = foo.y; // y == true ``` -Dot operators can be chained to access nested fields. +Dot operators can be chained to access nested fields: -```move= +```move let baz = Baz { foo: Foo { x: 3, y: true } }; let x = baz.foo.x; // x = 3; ``` However, this is not permitted for fields that contain non-primitive types, such a vector or another -struct +struct: -```move= +```move let foo = Foo { x: 3, y: true }; let bar = Bar { foo }; let foo2: Foo = *&bar.foo; -let foo3: Foo = bar.foo; // error! add an explicit copy with *& +let foo3: Foo = bar.foo; // error! must add an explicit copy with *& ``` The reason behind this design decision is that copying a vector or another struct might be an expensive operation. It is important for a programmer to be aware of this copy and make others aware -with the explicit syntax `*&` +with the explicit syntax `*&`. In addition reading from fields, the dot syntax can be used to modify fields, regardless of the -field being a primitive type or some other struct +field being a primitive type or some other struct. -```move= +```move let foo = Foo { x: 3, y: true }; foo.x = 42; // foo = Foo { x: 42, y: true } foo.y = !foo.y; // foo = Foo { x: 42, y: false } @@ -254,9 +259,9 @@ bar.foo.x = 52; // bar = Bar { foo: Foo { x: 52, y: false } } bar.foo = Foo { x: 62, y: true }; // bar = Bar { foo: Foo { x: 62, y: true } } ``` -The dot syntax also works via a reference to a struct +The dot syntax also works via a reference to a struct: -```move= +```move let foo = Foo { x: 3, y: true }; let foo_ref = &mut foo; foo_ref.x = foo_ref.x + 1; @@ -272,11 +277,11 @@ Most struct operations on a struct type `T` can only be performed inside the mod - The fields of a struct are only accessible inside the module that defines the struct. Following these rules, if you want to modify your struct outside the module, you will need to -provide publis APIs for them. The end of the chapter contains some examples of this. +provide public APIs for them. The end of the chapter contains some examples of this. However, struct _types_ are always visible to another module or script: -```move= +```move // m.move address 0x2 { module m { @@ -289,7 +294,7 @@ module m { } ``` -```move= +```move // n.move address 0x2 { module n { @@ -320,7 +325,7 @@ ephemeral. This means they cannot be copied or dropped. This property can be ver modeling real world resources like money, as you do not want money to be duplicated or get lost in circulation. -```move= +```move address 0x2 { module m { struct Foo { x: u64 } @@ -347,10 +352,10 @@ module m { } ``` -To fix the second example (`fun dropping_resource`), you would need to manually "unpack" the +To fix the second example (`fun destroying_resource1`), you would need to manually "unpack" the resource: -```move= +```move address 0x2 { module m { struct Foo { x: u64 } @@ -370,7 +375,7 @@ If on the other hand, your struct does not represent something valuable, you can `copy` and `drop` to get a struct value that might feel more familiar from other programming languages: -```move= +```move address 0x2 { module m { struct Foo has copy, drop { x: u64 } @@ -394,19 +399,19 @@ module m { Only structs with the `key` ability can be saved directly in [persistent global storage](./global-storage-operators.md). All values stored within those `key` -structs must have the `store` abilities. See the [ability](./abilities] and +structs must have the `store` ability. See the [ability](./abilities) and [global storage](./global-storage-operators.md) chapters for more detail. ## Examples Here are two short examples of how you might use structs to represent valuable data (in the case of -`Coin`) or more classical data (in the case of `Point` and `Circle`) +`Coin`) or more classical data (in the case of `Point` and `Circle`). ### Example 1: Coin -```move= +```move address 0x2 { module m { // We do not want the Coin to be copied because that would be duplicating this "money", @@ -422,7 +427,7 @@ module m { public fun mint(value: u64): Coin { // You would want to gate this function with some form of access control to prevent - // anyone using this module from minting an infinite amount of coins + // anyone using this module from minting an infinite amount of coins. Coin { value } } @@ -457,7 +462,7 @@ module m { ### Example 2: Geometry -```move= +```move address 0x2 { module point { struct Point has copy, drop, store { @@ -497,10 +502,10 @@ module point { } ``` -```move= +```move address 0x2 { module circle { - use 0x2::Point::{Self, Point}; + use 0x2::point::{Self, Point}; struct Circle has copy, drop, store { center: Point, @@ -512,7 +517,7 @@ module circle { } public fun overlaps(c1: &Circle, c2: &Circle): bool { - let d = Point::dist_squared(&c1.center, &c2.center); + let d = point::dist_squared(&c1.center, &c2.center); let r1 = c1.radius; let r2 = c2.radius; d*d <= r1*r1 + 2*r1*r2 + r2*r2 diff --git a/language/documentation/book/src/tuples.md b/language/documentation/book/src/tuples.md index 1bf9d8e147..1f4575b817 100644 --- a/language/documentation/book/src/tuples.md +++ b/language/documentation/book/src/tuples.md @@ -1,13 +1,13 @@ # Tuples and Unit Move does not fully support tuples as one might expect coming from another language with them as a -first-class value. However, in order to support multiple return values, Move has tuple-like +[first-class value](https://en.wikipedia.org/wiki/First-class_citizen). However, in order to support multiple return values, Move has tuple-like expressions. These expressions do not result in a concrete value at runtime (there are no tuples in the bytecode), and as a result they are very limited: they can only appear in expressions (usually in the return position for a function); they cannot be bound to local variables; they cannot be stored in structs; and tuple types cannot be used to instantiate generics. -Similarly, unit `()` is a type created by the Move source language in order to be expression based. +Similarly, [unit `()`](https://en.wikipedia.org/wiki/Unit_type) is a type created by the Move source language in order to be expression based. The unit value `()` does not result in any runtime value. We can consider unit`()` to be an empty tuple, and any restrictions that apply to tuples also apply to unit. @@ -21,7 +21,7 @@ multiple return values are represented using tuples. ## Literals -Tuples are created by a comma separated list of expressions inside of parentheses +Tuples are created by a comma separated list of expressions inside of parentheses. | Syntax | Type | Description | | --------------- | ---------------------------------------------------------------------------- | ------------------------------------------------------------ | @@ -37,19 +37,19 @@ Sometimes, tuples with two elements are called "pairs" and tuples with three ele ### Examples -```move= +```move address 0x42 { module example { // all 3 of these functions are equivalent // when no return type is provided, it is assumed to be `()` - fun returs_unit_1() { } + fun returns_unit_1() { } // there is an implicit () value in empty expression blocks - fun returs_unit_2(): () { } + fun returns_unit_2(): () { } - // explicit version of `returs_unit_1` and `returs_unit_2` - fun returs_unit_3(): () { () } + // explicit version of `returns_unit_1` and `returns_unit_2` + fun returns_unit_3(): () { () } fun returns_3_values(): (u64, bool, address) { @@ -72,7 +72,7 @@ For tuples of any size, they can be destructured in either a `let` binding or in For example: -```move= +```move address 0x42 { module example { // all 3 of these functions are equivalent @@ -107,23 +107,25 @@ For more details, see [Move Variables](./variables.md). ## Subtyping -Along with references, tuples are the only types that have subtyping in Move. Tuples do have +Along with references, tuples are the only types that have [subtyping](https://en.wikipedia.org/wiki/Subtyping) in Move. Tuples do have subtyping only in the sense that subtype with references (in a covariant way). -For example +For example: -```move= +```move let x: &u64 = &0; let y: &mut u64 = &mut 1; // (&u64, &mut u64) is a subtype of (&u64, &u64) -// since &mut u64 is a subtype of &u64 +// since &mut u64 is a subtype of &u64 let (a, b): (&u64, &u64) = (x, y); + // (&mut u64, &mut u64) is a subtype of (&u64, &u64) -// since &mut u64 is a subtype of &u64 +// since &mut u64 is a subtype of &u64 let (c, d): (&u64, &u64) = (y, y); + // error! (&u64, &mut u64) is NOT a subtype of (&mut u64, &mut u64) -// since &u64 is NOT a subtype of &mut u64 +// since &u64 is NOT a subtype of &mut u64 let (e, f): (&mut u64, &mut u64) = (x, y); ``` diff --git a/language/documentation/book/src/unit-testing.md b/language/documentation/book/src/unit-testing.md index 868e3b3c40..77daecd2af 100644 --- a/language/documentation/book/src/unit-testing.md +++ b/language/documentation/book/src/unit-testing.md @@ -113,6 +113,7 @@ $ move -h A simple module using some of the unit testing features is shown in the following example: First create an empty package and change directory into it: + ``` $ move new TestExample; cd TestExample ``` @@ -276,7 +277,7 @@ module 0x1::my_module { } ``` -we would get get the following output when running the tests: +we would get the following output when running the tests: ``` $ move test -g diff --git a/language/documentation/book/src/uses.md b/language/documentation/book/src/uses.md index 87d1914760..5e7edf8d9f 100644 --- a/language/documentation/book/src/uses.md +++ b/language/documentation/book/src/uses.md @@ -252,7 +252,7 @@ module example { Inside a given scope, all aliases introduced by `use` declarations must be unique. -For a module, this means aliases introduced by `use` cannot overla +For a module, this means aliases introduced by `use` cannot overlap ```move= address 0x42 { diff --git a/language/documentation/book/src/variables.md b/language/documentation/book/src/variables.md index b6cbaaf70e..e16738adbc 100644 --- a/language/documentation/book/src/variables.md +++ b/language/documentation/book/src/variables.md @@ -99,7 +99,7 @@ let x: T = e; // "Variable x of type T is initialized to expression e" Some examples of explicit type annotations: -```move= +```move address 0x42 { module example { @@ -315,7 +315,7 @@ fun three(): (u64, u64, u64) { ```move let (x1, _, z1) = three(); let (x2, _y, z2) = three(); -assert!(x1 + z1 == x2 + z2) +assert!(x1 + z1 == x2 + z2, 42); ``` This can be necessary at times as the compiler will error on unused local variables @@ -401,7 +401,7 @@ if (cond) x = 1 else x = 2; The assignment uses the same pattern syntax scheme as `let` bindings: -```move= +```move address 0x42 { module example { struct X { f: u64 } @@ -442,8 +442,7 @@ reference `&mut`. let x = 0; let r = &mut x; *r = 1; -assert!(x == 1, 42) -} +assert!(x == 1, 42); ``` This is particularly useful if either: @@ -469,7 +468,7 @@ This sort of modification is how you modify structs and vectors! ```move let v = vector::empty(); vector::push_back(&mut v, 100); -assert!(*vector::borrow(&v, 0) == 100, 42) +assert!(*vector::borrow(&v, 0) == 100, 42); ``` For more details, see [Move references](./references.md). @@ -566,7 +565,7 @@ explicitly destroyed within its declaring module.) ``` If a final expression is not present in a block---that is, if there is a trailing semicolon `;`, -there is an implicit unit `()` value. Similarly, if the expression block is empty, there is an +there is an implicit [unit `()` value](https://en.wikipedia.org/wiki/Unit_type). Similarly, if the expression block is empty, there is an implicit unit `()` value. ```move diff --git a/language/documentation/book/src/vector.md b/language/documentation/book/src/vector.md index 2bbce46788..ad18a7e919 100644 --- a/language/documentation/book/src/vector.md +++ b/language/documentation/book/src/vector.md @@ -43,14 +43,14 @@ result. These values are so common that specific syntax is provided to make the readable, as opposed to having to use `vector[]` where each individual `u8` value is specified in numeric form. -There are currently two supported types of `vector` literals, byte strings and hex strings. +There are currently two supported types of `vector` literals, *byte strings* and *hex strings*. #### Byte Strings Byte strings are quoted string literals prefixed by a `b`, e.g. `b"Hello!\n"`. These are ASCII encoded strings that allow for escape sequences. Currently, the supported escape -sequences are +sequences are: | Escape Sequence | Description | | --------------- | ---------------------------------------------- | @@ -64,10 +64,10 @@ sequences are #### Hex Strings -Hex strings are quoted string literals prefixed by a `x`, e.g. `x"48656C6C6F210A"` +Hex strings are quoted string literals prefixed by a `x`, e.g. `x"48656C6C6F210A"`. Each byte pair, ranging from `00` to `FF`, is interpreted as hex encoded `u8` value. So each byte -pair corresponds to a single entry in the resulting `vector` +pair corresponds to a single entry in the resulting `vector`. #### Example String Literals @@ -91,24 +91,24 @@ fun byte_and_hex_strings() { `vector` supports the following operations via the `std::vector` module in the Move standard library: -| Function | Description | Aborts? | -| ---------------------------------------------------------- | ------------------------------------------------------------- | ----------------------- | -| `vector::empty(): vector` | Create an empty vector that can store values of type `T` | Never | -| `vector::singleton(t: T): vector` | Create a vector of size 1 containing `t` | Never | -| `vector::push_back(v: &mut vector, t: T)` | Add `t` to the end of `v` | Never | -| `vector::pop_back(v: &mut vector): T` | Remove and return the last element in `v` | If `v` is empty | -| `vector::borrow(v: &vector, i: u64): &T` | Return an immutable reference to the `T` at index `i` | If `i` is not in bounds | -| `vector::borrow_mut(v: &mut vector, i: u64): &mut T` | Return an mutable reference to the `T` at index `i` | If `i` is not in bounds | -| `vector::destroy_empty(v: vector)` | Delete `v` | If `v` is not empty | -| `vector::append(v1: &mut vector, v2: vector)` | Add the elements in `v2` to the end of `v1` | If `i` is not in bounds | -| `vector::contains(v: &vector, e: &T): bool` | Return true if `e` is in the vector `v` | Never | -| `vector::swap(v: &mut vector, i: u64, j: u64)` | Swaps the elements at the `i`th and `j`th indices in the vector `v`.| If `i` or `j` is out of bounds | -| `vector::reverse(v: &mut vector)` | Reverses the order of the elements in the vector `v` in place | Never | -| `vector::index_of(v: &vector, e: &T): bool` | Return `(true, i)` if `e` is in the vector `v` at index `i`. Otherwise, returns `(false, 0)`.| Never | -| `vector::remove(v: &mut vector, i: u64): T` | Remove the `i`th element of the vector `v`, shifting all subsequent elements. This is O(n) and preserves ordering of elements in the vector. | If `i` is out of bounds. | -| `vector::swap_remove(v: &mut vector, i: u64): T` | Swap the `i`th element of the vector `v` with the last element and then pop the vector, This is O(1), but does not preserve ordering of elements in the vector. | If `i` is out of bounds. | - -More operations may be added overtime +| Function | Description | Aborts? | +| ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------ | +| `vector::empty(): vector` | Create an empty vector that can store values of type `T` | Never | +| `vector::singleton(t: T): vector` | Create a vector of size 1 containing `t` | Never | +| `vector::push_back(v: &mut vector, t: T)` | Add `t` to the end of `v` | Never | +| `vector::pop_back(v: &mut vector): T` | Remove and return the last element in `v` | If `v` is empty | +| `vector::borrow(v: &vector, i: u64): &T` | Return an immutable reference to the `T` at index `i` | If `i` is not in bounds | +| `vector::borrow_mut(v: &mut vector, i: u64): &mut T` | Return a mutable reference to the `T` at index `i` | If `i` is not in bounds | +| `vector::destroy_empty(v: vector)` | Delete `v` | If `v` is not empty | +| `vector::append(v1: &mut vector, v2: vector)` | Add the elements in `v2` to the end of `v1` | Never | +| `vector::contains(v: &vector, e: &T): bool` | Return true if `e` is in the vector `v`. Otherwise, returns false | Never | +| `vector::swap(v: &mut vector, i: u64, j: u64)` | Swaps the elements at the `i`th and `j`th indices in the vector `v` | If `i` or `j` is out of bounds | +| `vector::reverse(v: &mut vector)` | Reverses the order of the elements in the vector `v` in place | Never | +| `vector::index_of(v: &vector, e: &T): (bool, u64)` | Return `(true, i)` if `e` is in the vector `v` at index `i`. Otherwise, returns `(false, 0)` | Never | +| `vector::remove(v: &mut vector, i: u64): T` | Remove the `i`th element of the vector `v`, shifting all subsequent elements. This is O(n) and preserves ordering of elements in the vector | If `i` is out of bounds | +| `vector::swap_remove(v: &mut vector, i: u64): T` | Swap the `i`th element of the vector `v` with the last element and then pop the element, This is O(1), but does not preserve ordering of elements in the vector | If `i` is out of bounds | + +More operations may be added over time. ## Example @@ -166,4 +166,4 @@ For more details see the sections on [type abilities](./abilities.md) and [gener As mentioned [above](#destroying-and-copying-vectors), `vector` values can be copied only if the elements can be copied. In that case, the copy must be explicit via a -[`copy`](./variables.md#move-and-copy) or a [dereference `*`](./references.md#reference-operators). +[`copy`](./variables.md#move-and-copy) or a [dereference `*`](./references.md#reading-and-writing-through-references). diff --git a/language/documentation/book/translations/move-book-zh/.gitignore b/language/documentation/book/translations/move-book-zh/.gitignore new file mode 100644 index 0000000000..7585238efe --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/.gitignore @@ -0,0 +1 @@ +book diff --git a/language/documentation/book/translations/move-book-zh/CONTRIBUTING.md b/language/documentation/book/translations/move-book-zh/CONTRIBUTING.md new file mode 100644 index 0000000000..8d7724ea56 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/CONTRIBUTING.md @@ -0,0 +1,74 @@ +# Contributing to Move Book Chinese Version + +感谢您有兴趣为**《Move Book 中文版》**做出贡献!有很多方法可以做出贡献,我们感谢所有这些方式。 + +本翻译项目由 [*Move 中文社区(MoveCC)*](https://github.com/move-cc)[发起](https://github.com/move-language/move/issues/353),并与 [MoveDAO 社区](https://github.com/move-dao)共同初步完善。 + +目前工作仍在进行中! + +欢迎所有对 Move 感兴趣的朋友一起加入到《Move Book 中文版》的翻译工作中。 + +## 提交 PR 的 Commits 格式 + +```text +[move-book-zh] 关于这个 PR 的描述信息 +``` + +## 文档规范 + +请参考:[中文技术文档的写作规范](https://github.com/ruanyf/document-style-guide) + +### 断句 + +本书使用 Markdown 作为源文件,使用 [mdBook](https://github.com/rust-lang/mdBook) 作为渲染引擎。 + +由于中英文有所区别,换行后渲染引擎会自动追加一个空格。为了优化视觉体验,中文一个段落内不必换行,保持中文的段内容为一个物理行。 + +英文是由空格分隔的文本,所以不存在上述问题。 + +### 中英文混排规范 + +文中出现中英文混排时,中文与英文之间需要添加一个空格。如果英文单词结尾,此时单词与标点符号之间不添加空格。 + +### 数字规范 + +数字之间需要添加一个空格,如果数字带有英文单位,那么数字与英文单位之间不能添加空格。如果数字后带有中文单位,需要添加一个空格。 + +```text +正确:如果一个 1s 的帧被划分为 10 个时隙,每个时隙为 100ms。 + +错误:如果一个1s的帧被划分为10个时隙,每个时隙为100ms。 + +错误:如果一个 1 s 的帧被划分为10个时隙,每个时隙为 100 ms。 +``` + +### 逗号问题 + +英文中,没有顿号这种标点符号,逗号常用分隔并列的句子成分或结构。 + +英文中,像 `and` 并列词的前面通常会有一个 `,`,但在中文里表示对象之间的并列关系时,`和`的前面不能带逗号。 + +```text +War, famine, and flood are terrible. +战争, 饥荒和洪水都是很可怕的。 +``` + +### 冒号和逗号的使用场景规范 + +冒号通常用在“问、答、说、指出、宣布、证明、表明、例如”一类动词后面,表示提起下文。 +如果在较短的提示句子中,需要将 `:` 改为 `,`;如果提示内容比较多,则使用 `:` 来提起下文: + +示例1,提起的内容短少: + +```text +十六进制字符串是以 x 为前缀的带引号的字符串文字,例如x'48656C6C6F210A' +``` + +示例2,提起的内容多: + +```text +在这些情况下,vector 的类型是从元素类型或从动态数组的使用上推断出来的。如果无法推断类型或者只是为了更清楚地表示,则可以显式指定类型: + +vector[]: vector +vector[e1, ..., en]: vector +``` diff --git a/language/documentation/book/translations/move-book-zh/README.md b/language/documentation/book/translations/move-book-zh/README.md new file mode 100644 index 0000000000..9de276331c --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/README.md @@ -0,0 +1,73 @@ +## 参与贡献的重要提示 + +《Move Book 中文版》翻译工作还在持续进行中,如果你愿意贡献你的一份力量,欢迎提交 pr 或 issue。 + +为了方便翻译工作的进行,在提交 pr 时请不要删除英文原文的内容! + +在贡献之前请阅读[官方的贡献指南](https://github.com/move-language/move/blob/main/CONTRIBUTING.md)和[《Move Book 中文版》的贡献指南](./CONTRIBUTING.md)。 + +# Move Book 中文版 + +区块链技术的发展经历了两个阶段,比特币(BTC)开启了*区块链1.0时代*,以太坊(ETH)开启了*区块链2.0时代*。 +以太坊的出现为区块链带来了*智能合约*这一关键技术,让区块链不只停留在记账这一单的目的,而是带来更多的应用拓展性。 +遗憾的是,智能合约如同一把双刃剑,在带来众多丰富功能拓展的同时,也容易让智能合约开发者无意间引入不安全的代码,让链的资产受到威胁。 + +编写简单、安全、易部署的智能合约应该是*区块链3.0时代*应该关注的重点,**面向资源编程**的 [Move 语言](https://github.com/move-language/move),无疑给这个问题提供了一个很好的解决方案。 + +本书是 [Move Book](https://move-language.github.io/move/) 的中文版。 + +## 快速开始 + +本书使用 [mdBook](https://rust-lang.github.io/mdBook/) 构建。 + +1. 使用 Cargo 安装 mdBook: + +```shell +cargo install mdbook +``` + +2. 下载 + +```shell +git clone https://github.com/move-language/move.git +``` + +3. 构建并预览 + +```shell +cd language/documentation/book/translations/move-book-zh +mdbook serve --open +``` + +## Contributors ✨ + +[各章节译者](Translators.md) + +Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)): + + + + + + + + + + + + + + + + + + + + + +

zhang

💻 📖 🌍 🚇

ruyisu

💻 🌍 📖 🚇

lshoo

💻 🌍 📖 🤔 👀

Container

💻 🌍 📖 👀

nosalt

💻 🌍 📖 🚇

geometryolife

💻 🌍 📢

666thi

💻 🌍 📢

MagicGordon

💻 🌍 📢

xixifusi1984

💻 🌍 📢

yvvw

💻 🌍 📢

xiaochuan891102

💻 🌍 📢

stephenLee

💻 🌍 📢
+ + + + + diff --git a/language/documentation/book/translations/move-book-zh/Translators.md b/language/documentation/book/translations/move-book-zh/Translators.md new file mode 100644 index 0000000000..35bce4ce50 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/Translators.md @@ -0,0 +1,30 @@ +| | 章节 | 译者 | 校对 | +|----|----------------------------|------------------------------------------------|-------------------------------------------------------------------------------------| +| 0 | Intoduction | Tom | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 1 | Modules and Scripts | [@Kusou1](https://github.com/kusou1) | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 2 | Move Tutorial | loadi、[@leego](https://github.com/leego) | [@Joe Chen](https://github.com/geometryolife) | +| 3 | Integers | Tom | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 4 | Bool | Tom | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 5 | Address | ([@stephenLee](https://github.com/stephenLee)) | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 6 | Vector | ([@stephenLee](https://github.com/stephenLee)) | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 7 | Signer | ([@stephenLee](https://github.com/stephenLee)) | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 8 | References | container | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 9 | Tuples and Unit | container | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 10 | Local Variables and Scopes | @ruyisu | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 11 | Equality | @ruyisu | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 12 | Abort and Assert | @ruyisu | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 13 | Conditionals | [@Kusou1](https://github.com/kusou1) | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 14 | While and Loop | [@Kusou1](https://github.com/kusou1) | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 15 | Functions | @nosalt99 | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 16 | Structs and Resource | @nosalt99 | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 17 | Constants | @nosalt99 | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 18 | Generics | 小川 | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 19 | Type Abilities | 小川 | [@lshoo](https://github.com/lshoo)、[@Joe Chen](https://github.com/geometryolife) | +| 20 | Uses and Aliases | 小川 | [@ruyisu](https://github.com/ruy1su)、[@Joe Chen](https://github.com/geometryolife) | +| 21 | Friends | @xiaochuan891102 | [@ruyisu](https://github.com/ruy1su)、[@Joe Chen](https://github.com/geometryolife) | +| 22 | Packages | @xiaochuan891102 | [@ruyisu](https://github.com/ruy1su)、[@Joe Chen](https://github.com/geometryolife) | +| 23 | Unit Test | [@yvvw](https://github.com/yvvw) | [@ruyisu](https://github.com/ruy1su)、[@Joe Chen](https://github.com/geometryolife) | +| 24 | Global Storage Structure | [@yvvw](https://github.com/yvvw) | [@ruyisu](https://github.com/ruy1su)、[@Joe Chen](https://github.com/geometryolife) | +| 25 | Global Storage Operators | [@yvvw](https://github.com/yvvw) | [@ruyisu](https://github.com/ruy1su)、[@Joe Chen](https://github.com/geometryolife) | +| 26 | Standard Library | @MagicGordon | [@ruyisu](https://github.com/ruy1su)、[@Joe Chen](https://github.com/geometryolife) | +| 27 | Coding Conventions | @MagicGordon | [@ruyisu](https://github.com/ruy1su)、[@Joe Chen](https://github.com/geometryolife) | diff --git a/language/documentation/book/translations/move-book-zh/book.toml b/language/documentation/book/translations/move-book-zh/book.toml new file mode 100644 index 0000000000..1f51aef586 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/book.toml @@ -0,0 +1,7 @@ +[book] +authors = ["The Move Contributors"] +description = "Move book maintained by the Move Chinese contributors." +language = "zh" +multilingual = false +src = "src" +title = "Move Book 中文版" diff --git a/language/documentation/book/translations/move-book-zh/src/SUMMARY.md b/language/documentation/book/translations/move-book-zh/src/SUMMARY.md new file mode 100644 index 0000000000..4b75899b44 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/SUMMARY.md @@ -0,0 +1,45 @@ +# The Move Programming Language + +[引言(Introduction)](introduction.md) + +## Getting Started + +- [模块和脚本(Modules and Scripts)](modules-and-scripts.md) +- [Move 教程(Move Tutorial)](move-tutorial.md) + +## Primitive Types + +- [整数(Integers)](integers.md) +- [布尔类型(Bool)](bool.md) +- [地址(Address)](address.md) +- [向量(Vector)](vector.md) +- [签名(Signer)](signer.md) +- [引用(References)](references.md) +- [元组和单值(Tuples and Unit)](tuples.md) + +## Basic Concepts + +- [局部变量和作用域(Local Variables and Scopes)](variables.md) +- [等式(Equality)](equality.md) +- [中止和断言(Abort and Assert)](abort-and-assert.md) +- [条件语句(Conditionals)](conditionals.md) +- [循环(While and Loop)](loops.md) +- [函数(Functions)](functions.md) +- [结构体和资源(Structs and Resources)](structs-and-resources.md) +- [常量(Constants)](constants.md) +- [泛型(Generics)](generics.md) +- [类型能力(Type Abilities)](abilities.md) +- [导入和别名(Uses and Aliases)](uses.md) +- [友元函数(Friends)](friends.md) +- [程序包(Packages)](packages.md) +- [单元测试(Unit Tests)](unit-testing.md) + +## Global Storage + +- [全局存储结构(Global Storage Structure)](global-storage-structure.md) +- [全局存储操作(Global Storage Operators)](global-storage-operators.md) + +## Reference + +- [标准库(Standard Library)](standard-library.md) +- [Move 编码约定(Coding Conventions)](coding-conventions.md) diff --git a/language/documentation/book/translations/move-book-zh/src/abilities.md b/language/documentation/book/translations/move-book-zh/src/abilities.md new file mode 100644 index 0000000000..440362288e --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/abilities.md @@ -0,0 +1,321 @@ +# 能力 (abilities) + +Abilities are a typing feature in Move that control what actions are permissible for values of a given type. This system grants fine grained control over the "linear" typing behavior of values, as well as if and how values are used in global storage. This is implemented by gating access to certain bytecode instructions so that for a value to be used with the bytecode instruction, it must have the ability required (if one is required at all—not every instruction is gated by an ability). + +能力是 Move 语言中的一种类型特性,用于控制对给定类型的值允许哪些操作。 该系统对值的“线性”类型行为以及值如何在全局存储中使用提供细粒度控制。这是通过对某些字节码指令的进行访问控制来实现的,因此对于要与字节码指令一起使用的值,它必须具有所需的能力(如果需要的话,并非每条指令都由能力控制) + +## 四种能力 (The Four Abilities) + +The four abilities are: + +* [`copy`](#copy) + * Allows values of types with this ability to be copied. +* [`drop`](#drop) + * Allows values of types with this ability to be popped/dropped. +* [`store`](#store) + * Allows values of types with this ability to exist inside a struct in global storage. +* [`key`](#key) + * Allows the type to serve as a key for global storage operations. + +这四种能力分别是: + +* [`copy`](#copy) 复制 + * 允许此类型的值被复制 + +* [`drop`](#drop) 丢弃 + * 允许此类型的值被弹出/丢弃 + +* [`store`](#store) 存储 + * 允许此类型的值存在于全局存储的某个结构体中 + +* [`key`](#key) 键值 + * 允许此类型作为全局存储中的键(具有 `key` 能力的类型才能保存到全局存储中) + + +### `copy` + +The `copy` ability allows values of types with that ability to be copied. It gates the ability to copy values out of local variables with the [`copy`](./variables.md#move-and-copy) operator and to copy values via references with [dereference `*e`](./references.md#reading-and-writing-through-references). + +If a value has `copy`, all values contained inside of that value have `copy`. + +`copy` 能力允许具有此能力的类型的值被复制。 它限制了从本地变量通过 [`copy`](./variables.md#.move-and-copy)能力复制值以及通过 [`dereference *e`](./references.html#reading-and-writing-through-references)复制值这两种情况之外的复制操作。 + +如果一个值具有 `copy` 能力,那么这个值内部的所有值都有 `copy` 能力。 + +### `drop` + +The `drop` ability allows values of types with that ability to be dropped. By dropped, we mean that value is not transferred and is effectively destroyed as the Move program executes. As such, this ability gates the ability to ignore values in a multitude of locations, including: +* not using the value in a local variable or parameter +* not using the value in a [sequence via `;`](./variables.md#expression-blocks) +* overwriting values in variables in [assignments](./variables.md#assignments) +* overwriting values via references when [writing `*e1 = e2`](./references.md#reading-and-writing-through-references). + +If a value has `drop`, all values contained inside of that value have `drop`. + +`drop` 能力允许类型的值被丢弃。丢弃的意思程序执行后值会被有效的销毁而不必被转移。因此,这个能力限制在多个位置忽略使用值的可能性,包括: +* 未被使用的局部变量或者参数 +* 未被使用的 [`sequence` via `;`](./variables.md#expression-blocks)中的值 +* 覆盖[赋值(assignments)](./variables.html#assignments)变量中的值 +* [写入(writing) `*e1 = e2`](https://move-language.github.io/move/references.html#reading-and-writing-through-references) 时通过引用覆盖的值。 + +如果一个值具有 `drop` 能力,那么这个值内部的所有值都有 `drop` 能力。 + +### `store` + +The `store` ability allows values of types with this ability to exist inside of a struct (resource) in global storage, *but* not necessarily as a top-level resource in global storage. This is the only ability that does not directly gate an operation. Instead it gates the existence in global storage when used in tandem with `key`. + +If a value has `store`, all values contained inside of that value have `store` + +`store` 能力允许具有这种能力的类型的值位于[全局存储](./global-storage-operators.html)中的结构体(资源)内, *但不一定是* 全局存储中的顶级资源。这是唯一不直接限制操作的能力。相反,当(`store`)与 `key` 一起使用时,它对全局存储中的可行性进行把关。。 + +如果一个值具有 `store` 能力,那么这个值内部的所有值都有 `store` 能力。 + +### `key` + +The `key` ability allows the type to serve as a key for [global storage operations](./global-storage-operators.md). It gates all global storage operations, so in order for a type to be used with `move_to`, `borrow_global`, `move_from`, etc., the type must have the `key` ability. Note that the operations still must be used in the module where the `key` type is defined (in a sense, the operations are private to the defining module). + +If a value has `key`, all values contained inside of that value have `store`. This is the only ability with this sort of asymmetry. + +`key` 能力允许此类型作为[全局存储](./global-storage-operators.html)中的键。它会限制所有[全局存储](./global-storage-operators.html)中的操作,因此一个类型如果与 `move_to`, `borrow_global`, `move_from` 等一起使用,那么这个类型必须具备 `key` 能力。请注意,这些操作仍然必须在定义 `key` 类型的模块中使用(从某种意义上说,这些操作是此模块的私有操作)。 + +如果有一个值有 `key` 能力,那么这个值包含的所有字段值也都具有 `store` 能力,`key` 能力是唯一一个具有非对称的能力。 + +## Builtin Types (内置类型) + + +Most primitive, builtin types have `copy`, `drop`, and `store` with the exception of `signer`, which just has `store` + +* `bool`, `u8`, `u64`, `u128`, and `address` all have `copy`, `drop`, and `store`. +* `signer` has `drop` + * Cannot be copied and cannot be put into global storage +* `vector` may have `copy`, `drop`, and `store` depending on the abilities of `T`. + * See [Conditional Abilities and Generic Types](#conditional-abilities-and-generic-types) for more details. +* Immutable references `&` and mutable references `&mut` both have `copy` and `drop`. + * This refers to copying and dropping the reference itself, not what they refer to. + * References cannot appear in global storage, hence they do not have `store`. + +None of the primitive types have `key`, meaning none of them can be used directly with the [global storage operations](./global-storage-operators.md). + +几乎所有内置的基本类型具都有 `copy`,`drop`,以及 `store` 能力,`singer` 除外,它只有 `drop` 能力(原文是 `store` 有误,译者注) + +* `bool`, `u8`, `u64`, `u128`, `address` 都具有 `copy`, `drop`, 以及 `store` 能力。 +* `signer` 具有 `drop` 能力。 不能被复制以及不能被存放在全局存储中 +* `vector` 可能具有 `copy`,`drop`,以及`store` 能力,这依赖于 `T` 具有的能力。 查看 [条件能力与泛型类型](#conditional-abilities-and-generic-types)获取详情 +* 不可变引用 `&` 和可变引用 `&mut` 都具有 `copy` 和 `drop` 能力。 + * 这是指复制和删除引用本身,而不是它们所引用的内容。 + * 引用不能出现在全局存储中,因此它们没有 `store` 能力。 + +所有基本类型都没有 `key`,这意味着它们都不能直接用于[全局存储操作](./global-storage-operators.html)。 + +## Annotating Structs (标注结构体) + +To declare that a `struct` has an ability, it is declared with `has ` after the struct name but before the fields. For example: + +要声明一个 `struct` 具有某个能力,它在结构体名称之后, 在字段之前用 `has ` 声明。例如: + +```move +struct Ignorable has drop { f: u64 } +struct Pair has copy, drop, store { x: u64, y: u64 } +``` + +In this case: `Ignorable` has the `drop` ability. `Pair` has `copy`, `drop`, and `store`. + +在这个例子中:`Ignorable` 具有 `drop` 能力。 `Pair` 具有 `copy`、`drop` 和 `store` 能力。 + +All of these abilities have strong guarantees over these gated operations. The operation can be performed on the value only if it has that ability; even if the value is deeply nested inside of some other collection! + +所有这些能力对这些访问操作都有强有力的保证。只有具有该能力,才能对值执行对应的操作;即使该值深层嵌套在其他集合中! + +As such: when declaring a struct’s abilities, certain requirements are placed on the fields. All fields must satisfy these constraints. These rules are necessary so that structs satisfy the reachability rules for the abilities given above. If a struct is declared with the ability... + +* `copy`, all fields must have `copy`. +* `drop`, all fields must have `drop`. +* `store`, all fields must have `store`. +* `key`, all fields must have `store`. + * `key` is the only ability currently that doesn’t require itself. + +因此:在声明结构体的能力时,对字段提出了某些要求。所有字段都必须满足这些约束。这些规则是必要的,以便结构体满足上述功能的可达性规则。如果一个结构被声明为具有某能力: + +* `copy`, 所有的字段必须具有 `copy` 能力。 +* `drop`,所有的字段必须具有 `drop` 能力。 +* `store`,所有的字段必须具有 `store` 能力。 +* `key`,所有的字段必须具有 `store` 能力。`key` 是目前唯一不需要包含自身的能力。 + +例如: + +```move +// A struct without any abilities +struct NoAbilities {} + +struct WantsCopy has copy { + f: NoAbilities, // ERROR 'NoAbilities' does not have 'copy' +} +``` + +and similarly: + +类似的: + +```move +// A struct without any abilities +struct NoAbilities {} + +struct MyResource has key { + f: NoAbilities, // Error 'NoAbilities' does not have 'store' +} +``` + +## Conditional Abilities and Generic Types (条件能力与泛型类型) + +When abilities are annotated on a generic type, not all instances of that type are guaranteed to have that ability. Consider this struct declaration: + +在泛型类型上标注能力时,并非该类型的所有实例都保证具有该能力。考虑这个结构体声明: + +```move +struct Cup has copy, drop, store, key { item: T } +``` + + +It might be very helpful if `Cup` could hold any type, regardless of its abilities. The type system can *see* the type parameter, so it should be able to remove abilities from `Cup` if it *sees* a type parameter that would violate the guarantees for that ability. + +This behavior might sound a bit confusing at first, but it might be more understandable if we think about collection types. We could consider the builtin type `vector` to have the following type declaration: + +如果 `Cup` 可以容纳任何类型,可能会很有帮助,不管它的能力如何。类型系统可以 *看到* 类型参数,因此,如果它 *发现* 一个类型参数违反了对该能力的保证,它应该能够从 `Cup` 中删除能力。 + +这种行为一开始可能听起来有点令人困惑,但如果我们考虑一下集合类型,它可能会更容易理解。我们可以认为内置类型 `Vector` 具有以下类型声明: + +```move +vector has copy, drop, store; +``` + +We want `vector`s to work with any type. We don't want separate `vector` types for different abilities. So what are the rules we would want? Precisely the same that we would want with the field rules above. So, it would be safe to copy a `vector` value only if the inner elements can be copied. It would be safe to ignore a `vector` value only if the inner elements can be ignored/dropped. And, it would be safe to put a `vector` in global storage only if the inner elements can be in global storage. + +To have this extra expressiveness, a type might not have all the abilities it was declared with depending on the instantiation of that type; instead, the abilities a type will have depends on both its declaration **and** its type arguments. For any type, type parameters are pessimistically assumed to be used inside of the struct, so the abilities are only granted if the type parameters meet the requirements described above for fields. Taking `Cup` from above as an example: + +* `Cup` has the ability `copy` only if `T` has `copy`. +* It has `drop` only if `T` has `drop`. +* It has `store` only if `T` has `store`. +* It has `key` only if `T` has `store`. + +我们希望 `vector` 适用于任何类型。我们不希望针对不同的能力使用不同的 `vector` 类型。那么我们想要的规则是什么?与上面的字段规则完全相同。因此,仅当可以复制内部元素时,复制`vector` 值才是安全的。仅当可以忽略/丢弃内部元素时,忽略 `vector` 值才是安全的。而且,仅当内部元素可以在全局存储中时,将向量放入全局存储中才是安全的。 + +为了具有这种额外的表现力,一个类型可能不具备它声明的所有能力,具体取决于该类型的实例化;相反,一个类型的能力取决于它的声明 **和** 它的类型参数。对于任何类型,类型参数都被悲观地假定为在结构体内部使用,因此只有在类型参数满足上述字段要求时才授予这些能力。以上面的 `Cup` 为例: + +* `Cup` 拥有 `copy` 能力 仅当 `T` 拥有 `copy` 能力时。 +* `Cup` 拥有 `drop` 能力 仅当 `T` 拥有 `drop` 能力时。 +* `Cup` 拥有 `store` 能力 仅当 `T` 拥有 `store` 能力时。 +* `Cup` 拥有 `key` 能力 仅当 `T` 拥有 `store` 能力时。 + +Here are examples for this conditional system for each ability: + +以下是每个能力的条件系统的示例: + +### Example: conditional `copy` + +```move +struct NoAbilities {} +struct S has copy, drop { f: bool } +struct Cup has copy, drop, store { item: T } + +fun example(c_x: Cup, c_s: Cup) { + // Valid, 'Cup' has 'copy' because 'u64' has 'copy' + let c_x2 = copy c_x; + // Valid, 'Cup' has 'copy' because 'S' has 'copy' + let c_s2 = copy c_s; +} + +fun invalid(c_account: Cup, c_n: Cup) { + // Invalid, 'Cup' does not have 'copy'. + // Even though 'Cup' was declared with copy, the instance does not have 'copy' + // because 'signer' does not have 'copy' + let c_account2 = copy c_account; + // Invalid, 'Cup' does not have 'copy' + // because 'NoAbilities' does not have 'copy' + let c_n2 = copy c_n; +} +``` + +### Example: conditional `drop` + +```move +struct NoAbilities {} +struct S has copy, drop { f: bool } +struct Cup has copy, drop, store { item: T } + +fun unused() { + Cup { item: true }; // Valid, 'Cup' has 'drop' + Cup { item: S { f: false }}; // Valid, 'Cup' has 'drop' +} + +fun left_in_local(c_account: Cup): u64 { + let c_b = Cup { item: true }; + let c_s = Cup { item: S { f: false }}; + // Valid return: 'c_account', 'c_b', and 'c_s' have values + // but 'Cup', 'Cup', and 'Cup' have 'drop' + 0 +} + +fun invalid_unused() { + // Invalid, Cannot ignore 'Cup' because it does not have 'drop'. + // Even though 'Cup' was declared with 'drop', the instance does not have 'drop' + // because 'NoAbilities' does not have 'drop' + Cup { item: NoAbilities {}}; +} + +fun invalid_left_in_local(): u64 { + let n = Cup { item: NoAbilities {}}; + // Invalid return: 'c_n' has a value + // and 'Cup' does not have 'drop' + 0 +} +``` + +### Example: conditional `store` + +```move +struct Cup has copy, drop, store { item: T } + +// 'MyInnerResource' is declared with 'store' so all fields need 'store' +struct MyInnerResource has store { + yes: Cup, // Valid, 'Cup' has 'store' + // no: Cup, Invalid, 'Cup' does not have 'store' +} + +// 'MyResource' is declared with 'key' so all fields need 'store' +struct MyResource has key { + yes: Cup, // Valid, 'Cup' has 'store' + inner: Cup, // Valid, 'Cup' has 'store' + // no: Cup, Invalid, 'Cup' does not have 'store' +} +``` + +### Example: conditional `key` + +```move +struct NoAbilities {} +struct MyResource has key { f: T } + +fun valid(account: &signer) acquires MyResource { + let addr = signer::address_of(account); + // Valid, 'MyResource' has 'key' + let has_resource = exists>(addr); + if (!has_resource) { + // Valid, 'MyResource' has 'key' + move_to(account, MyResource { f: 0 }) + }; + // Valid, 'MyResource' has 'key' + let r = borrow_global_mut>(addr) + r.f = r.f + 1; +} + +fun invalid(account: &signer) { + // Invalid, 'MyResource' does not have 'key' + let has_it = exists>(addr); + // Invalid, 'MyResource' does not have 'key' + let NoAbilities {} = move_from(addr); + // Invalid, 'MyResource' does not have 'key' + move_to(account, NoAbilities {}); + // Invalid, 'MyResource' does not have 'key' + borrow_global(addr); +} +``` diff --git a/language/documentation/book/translations/move-book-zh/src/abort-and-assert.md b/language/documentation/book/translations/move-book-zh/src/abort-and-assert.md new file mode 100644 index 0000000000..314940ee18 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/abort-and-assert.md @@ -0,0 +1,255 @@ +# 中止和断言 (Abort and Assert) + +[`return`](./functions.md) and `abort` are two control flow constructs that end execution, one for +the current function and one for the entire transaction. + +More information on [`return` can be found in the linked section](./functions.md) + +[`return`](./functions.md) 和 `abort` 是两种结束程序执行的控制流结构。前者针对当前函数,后者针对整个事务。 + + [`return`](./functions.md)的更多信息可以参考链接中的文章。 + +## `abort` 中止 + +`abort` is an expression that takes one argument: an **abort code** of type `u64`. For example: + +`abort` 表达式只接受一个参数: 类型为 `u64` 的**中止代码**。例如: + +```move +abort 42 +``` + +The `abort` expression halts execution the current function and reverts all changes made to global +state by the current transaction. There is no mechanism for "catching" or otherwise handling an `abort`. + +`abort` 表达式会中止执行当前函数并恢复当前事务对全局状态所做的所有更改。Move语言没有“捉捕”或者额外处理`abort`的机制。 + +Luckily, in Move transactions are all or nothing, meaning any changes to global storage are made all +at once only if the transaction succeeds. Because of this transactional commitment of changes, after +an abort there is no need to worry about backing out changes. While this approach is lacking in +flexibility, it is incredibly simple and predictable. + +幸运的是,在Move里事务的计算要么完全执行要么完全不执行。这意味着只有在事务成功时,任何对全局存储状态的改变才会被一并执行。 +由于这种对于所有更改的事务承诺,在 `abort` 之后我们不需要担心去回滚任何更改。尽管这种方法缺少灵活性,它还是非常简单和可预测的。 + + +Similar to [`return`](./functions.md), `abort` is useful for exiting control flow when some condition cannot be met. + +In this example, the function will pop two items off of the vector, but will abort early if the vector does not have two items + +与 [`return`](./functions.md)相似, 在一些条件无法被满足的时候,`abort` 可以被用于退出控制流(control flow)。 + +在以下示例中,目标函数会从vector里弹出两个元素,但是如果vector中并没有两个元素,函数会提前中止。 + +```move= +use std::vector; +fun pop_twice(v: &mut vector): (T, T) { + if (vector::length(v) < 2) abort 42; + + (vector::pop_back(v), vector::pop_back(v)) +} +``` + +This is even more useful deep inside a control-flow construct. For example, this function checks +that all numbers in the vector are less than the specified `bound`. And aborts otherwise + +这在控制流结构的深处甚至会更有用。例如,此函数检查vector中是否所有数字都小于指定的边界(`bound`)。否则函数中止: + +```move= +use std::vector; +fun check_vec(v: &vector, bound: u64) { + let i = 0; + let n = vector::length(v); + while (i < n) { + let cur = *vector::borrow(v, i); + if (cur > bound) abort 42; + i = i + 1; + } +} +``` + +### `assert` 断言 + +`assert` is a builtin, macro-like operation provided by the Move compiler. It takes two arguments, a condition of type `bool` and a code of type `u64` + +`assert` 是 Move 编译器提供的内置的类宏(macro-like)操作。它需要两个参数:一个 `bool` 类型的条件和一个 `u64` 类型的错误状态码(类似HTTP中的StatusCode: 404, 500等,译者注) + +```move +assert!(condition: bool, code: u64) +``` + +Since the operation is a macro, it must be invoked with the `!`. This is to convey that the +arguments to `assert` are call-by-expression. In other words, `assert` is not a normal function and +does not exist at the bytecode level. It is replaced inside the compiler with + +由于该操作是一个宏,因此必须使用 `!` 调用它。这是为了表达 `assert` 的参数属于表达式调用(call-by-expression)。换句话说,`assert` 不是一个正常的函数,并且在字节码(bytecode)级别不存在。它在编译器内部被替换为以下代码: + +```move +if (condition) () else abort code +``` + +`assert` is more commonly used than just `abort` by itself. The `abort` examples above can be rewritten using `assert` + +`assert` 比 `abort` 本身更常用。上面的 `abort` 示例可以使用 `assert` 重写 + +```move= +use std::vector; +fun pop_twice(v: &mut vector): (T, T) { + assert!(vector::length(v) >= 2, 42); // 现在使用'assert' + + (vector::pop_back(v), vector::pop_back(v)) +} +``` + +和 + +```move= +use std::vector; +fun check_vec(v: &vector, bound: u64) { + let i = 0; + let n = vector::length(v); + while (i < n) { + let cur = *vector::borrow(v, i); + assert!(cur <= bound, 42); // 现在使用 'assert' + i = i + 1; + } +} +``` + +Note that because the operation is replaced with this `if-else`, the argument for the `code` is not +always evaluated. For example: + +请注意,因为此操作被替换为 `if-else`,这段 `代码` 的参数不是总是被执行(evaluated)。例如: + +```move +assert!(true, 1 / 0) +``` + +Will not result in an arithmetic error, it is equivalent to + +不会导致算术错误,因为它相当于: + +```move +if (true) () else (1 / 0) +``` + +So the arithmetic expression is never evaluated! + +所以这个算术表达式永远不会被执行(evaluated)! + +### Abort codes in the Move VM (Move虚拟机中的中止代码) + +When using `abort`, it is important to understand how the `u64` code will be used by the VM. + +Normally, after successful execution, the Move VM produces a change-set for the changes made to +global storage (added/removed resources, updates to existing resources, etc). + +当使用 `abort` 时,理解虚拟机将如何使用 `u64` 代码是非常重要的。 + +通常,在成功执行后,Move 虚拟机会为对全局存储(添加/删除资源、更新现有资源等)所做的更改生成一个更改集。 + +If an `abort` is reached, the VM will instead indicate an error. Included in that error will be two +pieces of information: + +- The module that produced the abort (address and name) +- The abort code. + +For example + +如果执行到 `abort` 代码,虚拟机将指示错误。该错误中包含两块信息: + +- 发生中止的模块(地址和名称) +- 错误状态码。 + +例如 + +```move= +address 0x2 { + module example { + public fun aborts() { + abort 42 + } + } +} + +script { + fun always_aborts() { + 0x2::example::aborts() + } +} +``` + +If a transaction, such as the script `always_aborts` above, calls `0x2::example::aborts`, the VM +would produce an error that indicated the module `0x2::example` and the code `42`. + +This can be useful for having multiple aborts being grouped together inside a module. + +In this example, the module has two separate error codes used in multiple functions + +如果一个事务,例如上面的脚本 `always_aborts` 调用了 `0x2::example::aborts`,虚拟机将产生一个指示模块 `0x2::example` 和错误状态码 `42` 的错误。 + +这在一个模块内将多个中止功能组合起来会很有用。 + +在以下示例中,模块有两个单独的错误状态码,用于多个函数 + +```move= +address 0x42 { + module example { + + use std::vector; + + const EMPTY_VECTOR: u64 = 0; + const INDEX_OUT_OF_BOUNDS: u64 = 1; + + // 移动 i 到 j, 移动 j 到 k, 移动 k 到 i + public fun rotate_three(v: &mut vector, i: u64, j: u64, k: u64) { + let n = vector::length(v); + assert!(n > 0, EMPTY_VECTOR); + assert!(i < n, INDEX_OUT_OF_BOUNDS); + assert!(j < n, INDEX_OUT_OF_BOUNDS); + assert!(k < n, INDEX_OUT_OF_BOUNDS); + + vector::swap(v, i, k); + vector::swap(v, j, k); + } + + public fun remove_twice(v: &mut vector, i: u64, j: u64): (T, T) { + let n = vector::length(v); + assert!(n > 0, EMPTY_VECTOR); + assert!(i < n, INDEX_OUT_OF_BOUNDS); + assert!(j < n, INDEX_OUT_OF_BOUNDS); + assert!(i > j, INDEX_OUT_OF_BOUNDS); + + (vector::remove(v, i), vector::remove(v, j)) + } + } +} +``` + +## The type of `abort` (`abort` 的类型) + +The `abort i` expression can have any type! This is because both constructs break from the normal +control flow, so they never need to evaluate to the value of that type. + +The following are not useful, but they will type check + +`abort i` 表达式可以有任何类型!这是因为这两种构造都打破了正常控制流,因此他们永远不需要计算该类型的值。 + +以下的示例不是特别有用,但它们会做类型检查 + +```move +let y: address = abort 0; +``` + +This behavior can be helpful in situations where you have a branching instruction that produces a +value on some branches, but not all. For example: + +在您有一个分支指令,并且这个指令会产生某些分支(不是全部)的值的时候,这种行为会非常有用。例如: + +```move +let b = + if (x == 0) false + else if (x == 1) true + else abort 42; +// ^^^^^^^^ `abort 42` 的类型是 `bool` +``` diff --git a/language/documentation/book/translations/move-book-zh/src/address.md b/language/documentation/book/translations/move-book-zh/src/address.md new file mode 100644 index 0000000000..1a5edc9d99 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/address.md @@ -0,0 +1,58 @@ +# 地址 + +`地址(address)`是 Move 中的内置类型,用于表示全局存储中的的位置(有时称为账户)。`地址(address)` 值是一个 128 位(16 字节)标识符。在一个给定的地址,可以存储两样东西:[模块(Module)](./modules-and-scripts.md)和[资源(Resources)](./structs-and-resources.md)。 + +虽然`地址(address)`在底层是一个 128 位整数,但 Move 语言有意让其不透明 —— 它们不能从整数创建,不支持算术运算,也不能修改。即使可能有一些有趣的程序会使用这种特性(例如,C 中的指针算法实现了类似壁龛(niche)的功能),但 Move 语言不允许这种动态行为,因为它从头开始就被设计为支持静态验证。*(壁龛指安装在墙壁上的小格子或在墙身上留出的作为贮藏设施的空间,最早在宗教上是指排放佛像的小空间,现在多用在家庭装修上,因其不占建筑面积,使用比较方便,深受大家喜爱,Joe 注)* + +你可以通过运行时地址值(`address` 类型的值)来访问该地址处的资源。但*无法*在运行时通过地址值访问模块。 + +## 地址及其语法 + +地址有两种形式:*命名的*或*数值的*。命名地址的语法遵循 Move 命名标识符的规则。数值地址的语法不受十六进制编码值的限制,任何有效的 [`u128` 数值](./integers.md)都可以用作地址值。例如,`42`,`0xCFAE` 和 `2021` 都是合法有效的数值地址字面量(literal)。 + +为了区分何时在表达式上下文中使用地址,使用地址时的语法根据使用地址的上下文而有所不同: + +* 当地址被用作表达式时,地址必须以 `@` 字符为前缀,例如:[`@`](./integers.md) 或 `@`。 +* 在表达式上下文之外,地址可以不带前缀字符 `@`。例如:[``](./integers.md) 或 ``。 + +通常,可以将 `@` 视为将地址从命名空间项变为表达式项的运算符。 + +## 命名地址 + +命名地址是一项特性,它允许在使用地址的任何地方使用标识符代替数值,而不仅仅是在值级别。命名地址被声明并绑定为 Move 包中的顶级元素(模块和脚本之外)或作为参数传递给 Move 编译器。 + +命名地址仅存在于源语言级别,并将在字节码级别完全替代它们的值。因此,模块和模块成员*必须*通过模块的命名地址而不是编译期间分配给命名地址的数值来访问,例如:`use my_addr::foo` *不等于* `use 0x2::foo`,即使 Move 程序编译时将 `my_addr` 设置成 `0x2`。这个区别在[模块和脚本](./modules-and-scripts.md)一节中有更详细的讨论。 + +### 例子 + +```move +let a1: address = @0x1; // 0x00000000000000000000000000000001 的缩写 +let a2: address = @0x42; // 0x00000000000000000000000000000042 的缩写 +let a3: address = @0xDEADBEEF; // 0x000000000000000000000000DEADBEEF 的缩写 +let a4: address = @0x0000000000000000000000000000000A; +let a5: address = @std; // 将命名地址 `std` 的值赋给 `a5` +let a6: address = @66; +let a7: address = @0x42; + +module 66::some_module { // 不在表达式上下文中,所以不需要 @ + use 0x1::other_module; // 不在表达式上下文中,所以不需要 @ + use std::vector; // 使用其他模块时,可以使用命名地址作为命名空间项 + ... +} + +module std::other_module { // 可以使用命名地址作为命名空间项来声明模块 + ... +} +``` + +## 全局存储操作 + +`address` 值主要用来与全局存储操作进行交互。 + +`address` 值与 `exists`、`borrow_global`、`borrow_global_mut` 和 `move_from` [操作(operation)](./global-storage-operators.md)一起使用。 + +唯一*不使用* `address` 的全局存储操作是 `move_to`,它使用了 [`signer`](./signer.md)。 + +## 所有权 + +与 Move 语言内置的其他标量值一样,`address` 值是隐式可复制的,这意味着它们可以在没有显式指令(例如 [`copy`](./variables.md#移动和复制))的情况下复制。 diff --git a/language/documentation/book/translations/move-book-zh/src/bool.md b/language/documentation/book/translations/move-book-zh/src/bool.md new file mode 100644 index 0000000000..d49844ddbe --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/bool.md @@ -0,0 +1,49 @@ +# 布尔类型 (Bool) + +`bool`is Move's primitive type for boolean `true` and `false`values. + +`bool` 是 Move 布尔基本类型,有 `true` 和 `false` 两个值。 + +## 字面量 (Literals) + +Literals for `bool` are either `true` or `false` . + +布尔类型字面值只能是 `true` 或者 `false`中的一个 。 + +## 操作 (Operations) + +### 逻辑运算 (Logical) + +`bool`supports three logical operations: + +| Syntax | Description | Equivalent Expression | +| ------------------------- | ---------------------------- | ------------------------------------------------------------------- | +| `&&` | short-circuiting logical and | `p && q` is equivalent to `if (p) q else false` | +| || | short-circuiting logical or | p || q is equivalent to `if (p) true else q` | +| `!` | logical negation | `!p` is equivalent to `if (p) false else true` | + + +`bool` 支持三种逻辑运算: + +| 句法 | 描述 | Equivalent Expression | +| ------ | ---------------------------- | ----------------------------------------------- | +| `&&` | 短路逻辑与(short-circuiting logical and) | `p && q` 等价于 `if (p) q else false` | +| || | 短路逻辑或(short-circuiting logical or) | `p || q` 等价于 `if (p) true else q` | +| `!` | 逻辑非(logical negation) | `!p` 等价于 `if (p) false else true` | + + +### 控制流 (Control Flow) + +`bool`values are used in several of Move's control-flow constructs: + +布尔值用于 Move 的多个控制流结构中: + +- [`if (bool) { ... }`](./conditionals.html) +- [`while (bool) { .. }`](/loops.html) +- [`assert!(bool, u64)`](./abort-and-assert.html) + +## 所有权 (Ownership) + +As with the other scalar values built-in to the language, boolean values are implicitly copyable, meaning they can be copied without an explicit instruction such as `[copy]().` + +与语言内置的其他标量值一样,布尔值是隐式可复制的,这意味着它们可以在没有明确指令如[`copy`](./variables.md#move-and-copy)的情况下复制。 diff --git a/language/documentation/book/translations/move-book-zh/src/coding-conventions.md b/language/documentation/book/translations/move-book-zh/src/coding-conventions.md new file mode 100644 index 0000000000..0ea7920b8d --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/coding-conventions.md @@ -0,0 +1,76 @@ +# Move 编码约定 + +本节列出了 Move 团队认为有用的一些基本的 Move 编码约定。这些只是建议,如果你喜欢其他格式指南和约定,你可以随时使用它们。 + +## 命名 + +- **模块名称**:应该使用小写的蛇形命名法,例如:`fixed_point32`、`vector`。 +- **类型名称**:如果不是原生数据类型,则应使用驼峰命名法,例如:`Coin`、`RoleId`。 +- **函数名称**:应该使用小写的蛇形命名法,例如:`destroy_empty`。 +- **常量名称**:应该使用大写的蛇形命名法,例如:`REQUIRES_CAPABILITY`。 +- 泛型类型应该具备描述性,当然在适当的情况下也可以是反描述性的,例如:Vector 泛型类型的参数可以是 `T` 或 `Element`。大多数情况下,模块中的“主”类型命名应该与模块名相同,例如:`option::Option`,`fixed_point32::FixedPoint32`。 +- **模块文件名称**:应该与模块名相同,例如:`Option.move`。 +- **脚本文件名称**:应该使用小写的蛇形命名法,并且应该与脚本中的“主”函数名匹配。 +- **混合文件名称**:如果文件包含多个模块和/或脚本,文件命名应该使用小写的蛇形命名法,并且不需要与内部的任何特定模块/脚本名匹配。 + +## 导入 + +- 所有模块的 `use` 语句都应该位于模块的顶部。 +- 函数应该从声明它们的模块中完全限定地导入和使用, 而不是在顶部导入。 +- 类型应该在顶部导入。如果存在名称冲突,应使用 `as` 在本地适当地重命名类型。 + +例如,如果有一个模块: + +```move +module 0x1::foo { + struct Foo { } + const CONST_FOO: u64 = 0; + public fun do_foo(): Foo { Foo{} } + ... +} +``` + +此时将被导入并使用: + +```move +module 0x1::bar { + use 0x1::foo::{Self, Foo}; + + public fun do_bar(x: u64): Foo { + if (x == 10) { + foo::do_foo() + } else { + abort 0 + } + } + ... +} +``` + +并且,如果在导入两个模块时存在本地名称冲突: + +```move +module other_foo { + struct Foo {} + ... +} + +module 0x1::importer { + use 0x1::other_foo::Foo as OtherFoo; + use 0x1::foo::Foo; + ... +} +``` + +## 注释 + +- 每个模块、结构体和公共函数声明都应该有对应的注释。 +- Move 有文档注释 `///`,常规单行注释 `//`,块注释 `/* */`,和块文档注释 `/** */`。 + +## 格式化 + +Move 团队计划编写一个自动格式化程序来执行格式化约定。然而,在此期间: + +- 除 `script` 和 `address` 块外,其他的内容应使用四个空格的缩进。 +- 每行代码,如果超过 100 个字符,应该换行。 +- 结构体和常量应该在模块中的所有函数之前声明。 diff --git a/language/documentation/book/translations/move-book-zh/src/conditionals.md b/language/documentation/book/translations/move-book-zh/src/conditionals.md new file mode 100644 index 0000000000..0edbb8c42d --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/conditionals.md @@ -0,0 +1,78 @@ +# 条件语句 (Conditionals) + +An `if` expression specifies that some code should only be evaluated if a certain condition is true. For example: + +`if` 语句可以用来指定一块代码块,但只在判断条件(condition)为true时才会被执行。例如: + +```move +if (x > 5) x = x - 5 +``` + +The condition must be an expression of type `bool`. + +An `if` expression can optionally include an `else` clause to specify another expression to evaluate when the condition is false. + +条件语句(condition)必须是 `bool` 类型的表达式。 + +`if` 语句可选包含 `else` 子句,以指定当条件(condition)为 false 时要执行的另一个代码块。 + +```move +if (y <= 10) y = y + 1 else y = 10 +``` + +Either the "true" branch or the "false" branch will be evaluated, but not both. Either branch can be a single expression or an expression block. + +The conditional expressions may produce values so that the `if` expression has a result. + +无论是"true"分支还是"false"分支都会被执行,但不会同时执行.其中任何一个分支都可以是单行代码或代码块。条件表达式会产生值,所以 `if` 表达式会有一个结果。 + +```move +let z = if (x < 100) x else 100; +``` + +The expressions in the true and false branches must have compatible types. For example: + +true 和 false 分支的表达式类型必须是一致的,例如: + +```move= +// x和y必须是u64整型 +// x and y must be u64 integers +let maximum: u64 = if (x > y) x else y; + +// 错误!分支的类型不一致 +// (ERROR! branches different types) +let z = if (maximum < 10) 10u8 else 100u64; + +// 错误!分支的类型不一致,false-branch默认是()不是u64 +// ERROR! branches different types, as default false-branch is () not u64 +if (maximum >= 10) maximum; +``` + +If the `else` clause is not specified, the false branch defaults to the unit value. The following are equivalent: + +如果`else`子句未定义,false分支默认为 unit 。下面的例子是相等价的: + +```move +if (condition) true_branch // implied default: else () +if (condition) true_branch else () +``` + +Commonly, [`if` expressions](./conditionals.md) are used in conjunction with expression blocks. + +一般来说, [`if` 表达式](./conditionals.md)与多个表达式块结合使用. + +```move +let maximum = if (x > y) x else y; +if (maximum < 10) { + x = x + 10; + y = y + 10; +} else if (x >= 10 && y >= 10) { + x = x - 10; + y = y - 10; +} +``` + +## 条件语句的语法 (Grammar for Conditionals) + +> *if-expression* → **if (** *expression* **)** *expression* *else-clause**opt* +> *else-clause* → **else** *expression* diff --git a/language/documentation/book/translations/move-book-zh/src/constants.md b/language/documentation/book/translations/move-book-zh/src/constants.md new file mode 100644 index 0000000000..380322e8fc --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/constants.md @@ -0,0 +1,136 @@ +# 常量 (Constants) + +Constants are a way of giving a name to shared, static values inside of a `module` or `script`. + +The constant's must be known at compilation. The constant's value is stored in the compiled module +or script. And each time the constant is used, a new copy of that value is made. + +常量是一种对 `module` 或 `script` 内的共享静态值进行命名的方法(类似变量,但值不变,译者注)。 + +常量必须在编译时知道。常量的值存储在编译模块或脚本中。每次使用该常量时,都会生成该值的新副本。 + +## 声明 (Declaration) + +Constant declarations begin with the `const` keyword, followed by a name, a type, and a value. They +can exist in either a script or module + +常量声明以 `const` 关键字开头,后跟名称、类型和值。他们可以存在于脚本或模块中 + +```text +const : = ; +``` + +例如 + +```move= +script { + + const MY_ERROR_CODE: u64 = 0; + + fun main(input: u64) { + assert!(input > 0, MY_ERROR_CODE); + } + +} + +address 0x42 { + module example { + + const MY_ADDRESS: address = @0x42; + + public fun permissioned(s: &signer) { + assert!(std::signer::address_of(s) == MY_ADDRESS, 0); + } + + } +} +``` + +## 命名 (Naming) + +Constants must start with a capital letter `A` to `Z`. After the first letter, constant names can +contain underscores `_`, letters `a` to `z`, letters `A` to `Z`, or digits `0` to `9`. + +常量必须以大写字母 `A` 到 `Z` 开头。在第一个字母之后,常量名可以包含下划线 `_`、字母 `a` 到 `z`、字母 `A` 到 `Z` 或数字 `0` 到 `9`。 + +```move +const FLAG: bool = false; +const MY_ERROR_CODE: u64 = 0; +const ADDRESS_42: address = @0x42; +``` + +Even though you can use letters `a` to `z` in a constant. The +[general style guidelines](./coding-conventions.md) are to use just uppercase letters `A` to `Z`, +with underscores `_` between each word. + +虽然你可以在常量中使用字母 `a` 到 `z`。但[通用风格指南](./coding-conventions.md) 只使用大写字母 `A` 到 `Z`,每个单词之间有下划线`_`。 + +This naming restriction of starting with `A` to `Z` is in place to give room for future language features. It may or may not be removed later. + +这种以 `A` 到 `Z` 开头的命名限制是为了给未来的语言特性留出空间。此限制未来可能会保留或删除。 + +## 可见性 (Visibility) + +`public` constants are not currently supported. `const` values can be used only in the declaring +module. + +当前不支持 `public` 常量。 `const` 值只能在声明的模块中使用。 + +## 有效表达式 (Valid Expressions) + +Currently, constants are limited to the primitive types `bool`, `u8`, `u64`, `u128`, `address`, and +`vector`. Future support for other `vector` values (besides the "string"-style literals) will come later. + +目前,常量仅限于原始类型 `bool`、`u8`、`u64`、`u128`、`address` 和`vector`。其他 `vector` 值(除了“string”风格的字面量)将在不远的将来获得支持。 + +### 值 (Values) + +Commonly, `const`s are assigned a simple value, or literal, of their type. For example + +通常,`const` (常量)会被分配一个对应类型的简单值或字面量。例如 + +```move +const MY_BOOL: bool = false; +const MY_ADDRESS: address = @0x70DD; +const BYTES: vector = b"hello world"; +const HEX_BYTES: vector = x"DEADBEEF"; +``` + +### 复杂表达式 (Complex Expressions) + + +In addition to literals, constants can include more complex expressions, as long as the compiler is +able to reduce the expression to a value at compile time. + +Currently, equality operations, all boolean operations, all bitwise operations, and all arithmetic +operations can be used. + +除了字面量,常量还可以包含更复杂的表达式,只要编译器能够在编译时将表达式归纳(reduce)为一个值。 + +目前,相等运算、所有布尔运算、所有按位运算和所有算术运算可以使用。 + +```move +const RULE: bool = true && false; +const CAP: u64 = 10 * 100 + 1; +const SHIFTY: u8 = { + (1 << 1) * (1 << 2) * (1 << 3) * (1 << 4) +}; +const HALF_MAX: u128 = 340282366920938463463374607431768211455 / 2; +const EQUAL: bool = 1 == 1; +``` + +If the operation would result in a runtime exception, the compiler will give an error that it is +unable to generate the constant's value + +如果操作会导致运行时异常,编译器会给出无法生成常量值的错误。 + +```move +const DIV_BY_ZERO: u64 = 1 / 0; // error! +const SHIFT_BY_A_LOT: u64 = 1 << 100; // error! +const NEGATIVE_U64: u64 = 0 - 1; // error! +``` + +Note that constants cannot currently refer to other constants. This feature, along with support for +other expressions, will be added in the future. + +请注意,常量当前不能引用其他常量。此功能会在将来和支持其他表达方式一起被补充。 diff --git a/language/documentation/book/translations/move-book-zh/src/creating-coins.md b/language/documentation/book/translations/move-book-zh/src/creating-coins.md new file mode 100644 index 0000000000..1f06df9a00 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/creating-coins.md @@ -0,0 +1,7 @@ +# Move Tutorial + +Please refer to the [Move Tutorial](https://github.com/move-language/move/tree/main/language/documentation/tutorial). + +# Move 教程 + +请参阅 [Move 教程](https://github.com/move-language/move/tree/main/language/documentation/tutorial)。 diff --git a/language/documentation/book/translations/move-book-zh/src/equality.md b/language/documentation/book/translations/move-book-zh/src/equality.md new file mode 100644 index 0000000000..2abe89623f --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/equality.md @@ -0,0 +1,192 @@ + +# 等式 (Equality) + +Move supports two equality operations `==` and `!=` + +Move 支持两种等式操作: `==` 和 `!=` + +## 操作 (Operations) + +| Syntax | Operation | Description | +| ------ | --------- | --------------------------------------------------------------------------- | +| `==` | equal | Returns `true` if the two operands have the same value, `false` otherwise | +| `!=` | not equal | Returns `true` if the two operands have different values, `false` otherwise | + +| 语法 | 操作 | 描述 | +| ------ | --------- | --------------------------------------------------------------------------- | +| `==` | 相等 | 如果两个操作数(operands)值相同,返回 `true` , 否则返回 `false` | +| `!=` | 不相等 | 如果两个操作数(operands)值不相同,返回 `true` , 否则返回 `false` | + +### 类型校验 (Typing) + +Both the equal (`==`) and not-equal (`!=`) operations only work if both operands are the same type. + +只有当左右两个操作数类型相同,相等操作 (`==`) 与不等操作 (`!=`) 才能正常使用。 + +```move +0 == 0; // `true` +1u128 == 2u128; // `false` +b"hello" != x"00"; // `true` +``` + +Equality and non-equality also work over user defined types! + +等式与不等式也可以在用户自定义的类型下使用! + +```move= +address 0x42 { + module example { + struct S has copy, drop { f: u64, s: vector } + + fun always_true(): bool { + let s = S { f: 0, s: b"" }; + // 括号不是必需的,但为了清楚起见在此示例中添加了括号 + (copy s) == s + } + + fun always_false(): bool { + let s = S { f: 0, s: b"" }; + // 括号不是必需的,但为了清楚起见在此示例中添加了括号 + (copy s) != s + } + } +} +``` + +If the operands have different types, there is a type checking error. + +如果两边操作数的类型不同,则会出现类型检测错误。 + +```move +1u8 == 1u128; // 错误! +// ^^^^^ 期望此变量的类型是 'u8' +b"" != 0; // 错误! +// ^ 期望此变量的类型是 'vector' +``` + +### 引用变量的类型校验 (Typing with references) + +When comparing [references](./references.md), the type of the reference (immutable or mutable) does +not matter. This means that you can compare an immutable `&` reference with a mutable one `&mut` of +the same underlying type. + +当比较[引用变量](./references.md)时,引用的类别(不可变更的或可变更的(immutable or mutable))无关紧要。这意味着我们可以拿一个不可变更的 `&` 引用变量和另一个有相同相关类型的可变更的 `&mut ` 引用变量进行比较。 + +```move +let i = &0; +let m = &mut 1; + +i == m; // `false` +m == i; // `false` +m == m; // `true` +i == i; // `true` +``` + +The above is equivalent to applying an explicit freeze to each mutable reference where needed + +在需要时,对每个可变引用使用显式冻结(explicit freeze)的结果与上述情况一致。 + +```move +let i = &0; +let m = &mut 1; + +i == freeze(m); // `false` +freeze(m) == i; // `false` +m == m; // `true` +i == i; // `true` +``` +But again, the underlying type must be the same type + +但同样的,我们需要两边操作数的类型一致 + +```move +let i = &0; +let s = &b""; + +i == s; // 错误! +// ^ 期望此变量的类型是 '&u64' +``` + +## 限制 (Restrictions) + +Both `==` and `!=` consume the value when comparing them. As a result, the type system enforces that +the type must have [`drop`](./abilities.md). Recall that without the [`drop` ability](./abilities.md), +ownership must be transferred by the end of the function, and such values can only be explicitly destroyed +within their declaring module. If these were used directly with either equality `==` or non-equality `!=`, +the value would be destroyed which would break [`drop` ability](./abilities.md) safety guarantees! + +`==` 和 `!=` 会在比较不同变量的时候消耗 (consume)它们所包含的值,所以 Move 的类型系统会强制要求这些类型含有[`drop` 能力](./abilities.md)。回想一下,变量在没有[`drop` 能力](./abilities.md)时,所有权必须在函数结束前进行转移,而且这些值只能在其声明模块中被明确销毁(explicitly destroyed)。如果它们被直接使用于等式 `==` 或不等式 `!=` ,其值会被销毁并且这会打破[`drop` 能力](./abilities.md)的安全保证! + +```move= +address 0x42 { + module example { + struct Coin has store { value: u64 } + fun invalid(c1: Coin, c2: Coin) { + c1 == c2 // 错误! + // ^^ ^^ 这些资源将会被销毁! + } + } +} +``` + + +But, a programmer can _always_ borrow the value first instead of directly comparing the value, and +reference types have the [`drop` ability](./abilities.md). For example + +然而, 程序员 _总是_ 可以优先借用变量的值,而不直接比较它们的值。这样一来,引用变量的类型将会拥有[`drop` 能力](./abilities.md)。例如: + +```move= +address 0x42 { + module example { + struct Coin as store { value: u64 } + fun swap_if_equal(c1: Coin, c2: Coin): (Coin, Coin) { + let are_equal = &c1 == &c2; // 合规范的 + if (are_equal) (c2, c1) else (c1, c2) + } + } +} +``` + +## 避免额外的复制 (Avoid Extra Copies) + +While a programmer _can_ compare any value whose type has [`drop`](./abilities.md), a programmer +should often compare by reference to avoid expensive copies. + +当程序员 _可以_ 比较其类型含有[`drop` 能力](./abilities.md)的任意值时,他们应该尽可能多地使用引用变量来比较,以此来避免昂贵的复制。 + +```move= +let v1: vector = function_that_returns_vector(); +let v2: vector = function_that_returns_vector(); +assert!(copy v1 == copy v2, 42); +// ^^^^ ^^^^ +use_two_vectors(v1, v2); + +let s1: Foo = function_that_returns_large_struct(); +let s2: Foo = function_that_returns_large_struct(); +assert!(copy s1 == copy s2, 42); +// ^^^^ ^^^^ +use_two_foos(s1, s2); +``` + +This code is perfectly acceptable (assuming `Foo` has [`drop`](./abilities.md)), just not efficient. +The highlighted copies can be removed and replaced with borrows + +以上代码是完全可以接受的(假设`Foo`具备[`drop`](./abilities.md)能力),但它不是最有效的写法。突出显示的副本可以删除并替换为借用。 + +```move= +let v1: vector = function_that_returns_vector(); +let v2: vector = function_that_returns_vector(); +assert!(&v1 == &v2, 42); +// ^ ^ +use_two_vectors(v1, v2); + +let s1: Foo = function_that_returns_large_struct(); +let s2: Foo = function_that_returns_large_struct(); +assert!(&s1 == &s2, 42); +// ^ ^ +use_two_foos(s1, s2); +``` + +The efficiency of the `==` itself remains the same, but the `copy`s are removed and thus the program is more efficient. + +`==` 本身的效率还是和之前一样,但是 `copy` 操作被移除后整个程序会比之前更有效率。 diff --git a/language/documentation/book/translations/move-book-zh/src/friends.md b/language/documentation/book/translations/move-book-zh/src/friends.md new file mode 100644 index 0000000000..238dce84f4 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/friends.md @@ -0,0 +1,154 @@ +# 友元函数(Friends) + +The `friend` syntax is used to declare modules that are trusted by the current module. +A trusted module is allowed to call any function defined in the current module that have the `public(friend)` visibility. +For details on function visibilities, please refer to the *Visibility* section in [Functions](./functions.md). + +友元语法用于声明当前模块信任的其它模块。受信任的模块可以调用当前模块中定义的任何具有`公开(友元)`可见性的函数。有关函数可见性的详细信息,请参阅[函数](./functions.md)中的可见性部分。 + +## 友元声明(Friend declaration) + +A module can declare other modules as friends via friend declaration statements, in the format of + +一个模块可以通过友元声明语句将其他模块声明为友元,格式为: + +- `friend ` — friend declaration using fully qualified module name like the example below, or +- `friend —` 使用完全合格的模块名称的友元声明,如下例所示,或 + + ``` + address 0x42 { + module a { + friend 0x42::b; + } + } + ``` + +- `friend ` — friend declaration using a module name alias, where the module alias is introduced via the `use` statement. +- `friend —` 使用模块名称别名的友元声明,其中模块别名是通过use语句引入的。 + + ```move + address 0x42 { + module a { + use 0x42::b; + friend b; + } + } + ``` + +A module may have multiple friend declarations, and the union of all the friend modules forms the friend list. +In the example below, both `0x42::B` and `0x42::C` are considered as friends of `0x42::A`. + +一个模块可能有多个友元声明,所有好友模块的并集形成友元列表。在下面的示例中`,0x42::B`和`0x42::C`都被视为 的友元函数`0x42::A`。 + + ```move + address 0x42 { + module a { + friend 0x42::b; + friend 0x42::c; + } + } + ``` + +Unlike `use` statements, `friend` can only be declared in the module scope and not in the expression block scope. +`friend` declarations may be located anywhere a top-level construct (e.g., `use`, `function`, `struct`, etc.) is allowed. +However, for readability, it is advised to place friend declarations near the beginning of the module definition. + +与`use`语句不同,`friend`只能在模块作用域内声明,而不能在表达式块的作用域内声明。`friend`声明可以位于允许顶层构造的任何位置(例如, `use`, `function,struct`等)是被允许的。但是,为了可读性,建议将友元声明放在模块定义的开头附近。 + +Note that the concept of friendship does not apply to Move scripts: +- A Move script cannot declare `friend` modules as doing so is considered meaningless: there is no mechanism to call the function defined in a script. +- A Move module cannot declare `friend` scripts as well because scripts are ephemeral code snippets that are never published to global storage. + +请注意,友元关系(friendship)的概念不适用于 Move 脚本: +- `Move` 脚本不能声明`friend`模块,因为这样做被认为是无意义的:没有机制可以调用脚本中定义的函数。 +- `Move` 模块也不能声明`friend`脚本,因为脚本是永远不会发布到全局存储的临时代码片段。 + +### 友元声明规则(Friend declaration rules) +Friend declarations are subject to the following rules: +友元声明须遵守以下规则: + +- A module cannot declare itself as a friend +- 一个模块不能将自己声明为友元。 + + ```move= + address 0x42 { + module m { friend Self; // 错误! } + // ^^^^ 不能将自己声明为友元 + } + + address 0x43 { + module m { friend 0x43::M; // 错误! } + // ^^^^^^^ 不能将自己声明为友元 + } + ``` + +- Friend modules must be known by the compiler +- 编译器必须知道友元模块 + + ```move= + address 0x42 { + module m { friend 0x42::nonexistent; // 错误! } + // ^^^^^^^^^^^^^^^^^ 未绑定的模块 '0x42::nonexistent' + } + ``` + + - Friend modules must be within the same account address. (Note: this is not a technical requirement but rather a policy decision which *may* be relaxed later.) + + - 友元模块必须在同一个账号地址内。(注:这不是技术要求,而是以后可能放宽的决策。) + + ```move + address 0x42 { + module m {} + } + + address 0x43 { + module n { friend 0x42::m; // 错误! } + // ^^^^^^^ 不能声明当前地址外的模块作为友元 + } + ``` + +- 友元关系不能创建循环模块依赖关系(Friends relationships cannot create cyclic module dependencies) + +Cycles are not allowed in the friend relationships, e.g., the relation `0x2::a` friends `0x2::b` friends `0x2::c` friends `0x2::a` is not allowed. +More generally, declaring a friend module adds a dependency upon the current module to the friend module (because the purpose is for the friend to call functions in the current module). +If that friend module is already used, either directly or transitively, a cycle of dependencies would be created. + +友元关系中不允许循环,例如 `0x2::a` 友元 `0x2::b` 友元 `0x2::c` 友元`0x2::a`是不允许的。更普遍地,声明一个友元模块会将对当前模块的依赖添加到友元模块(因为目的是让友元调用当前模块中的函数)。如果该友元模块已被直接或传递地使用,则将形成一个依赖循环。 + + ```move + address 0x2 { + module a { + use 0x2::c; + friend 0x2::b; + + public fun a() { + c::c() + } + } + + module b { + friend 0x2::c; // 错误! + // ^^^^^^ 这个友元关系形成了一个依赖循环: '0x2::a' 使用了 '0x2::c' 但'0x2::b' 同时是 '0x2::a'和'0x2::b' 的友元 + } + + module c { + public fun c() {} + } + } +``` + +- The friend list for a module cannot contain duplicates. +- 模块的友元列表不能包含重复项。 + + ```move= + address 0x42 { + module a {} + + module m { + use 0x42::a as aliased_a; + friend 0x42::A; + friend aliased_a; // 错误! + // ^^^^^^^^^ 重复的友元声明 '0x42::a'. 模块内的友元声明必须是唯一的 + } + } + ``` diff --git a/language/documentation/book/translations/move-book-zh/src/functions.md b/language/documentation/book/translations/move-book-zh/src/functions.md new file mode 100644 index 0000000000..e0fb8d2a04 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/functions.md @@ -0,0 +1,607 @@ +# 函数 (Functions) + +Function syntax in Move is shared between module functions and script functions. Functions inside of modules are reusable, whereas script functions are only used once to invoke a transaction. + +Move中的函数语法在模块函数和脚本函数之间是一致的。模块内部的函数可重复使用,而脚本的函数只能被使用一次用来调用事务。 + +# 声明 (Declaration) + +Functions are declared with the `fun` keyword followed by the function name, type parameters, parameters, a return type, acquires annotations, and finally the function body. + +函数使用 `fun` 关键字声明,后跟函数名称、类型参数、参数、返回类型、获取标注(annotation),最后是函数体。 + +```text +fun <[type_parameters: constraint],*>([identifier: type],*): +``` + +例如 + +```move +fun foo(x: u64, y: T1, z: T2): (T2, T1, u64) { (z, y, x) } +``` + +### 可见性 (Visibility) + +Module functions, by default, can only be called within the same module. These internal (sometimes called private) functions cannot be called from other modules or from scripts. + +默认情况下,模块函数只能在同一个模块内调用。这些内部(有时称为私有)函数不能从其他模块或脚本中调用。 + +```move= +address 0x42 { + module m { + fun foo(): u64 { 0 } + fun calls_foo(): u64 { foo() } // valid + } + + module other { + fun calls_m_foo(): u64 { + 0x42::m::foo() // ERROR! + // ^^^^^^^^^^^^ 'foo' is internal to '0x42::m' + } + } +} + +script { + fun calls_m_foo(): u64 { + 0x42::m::foo() // ERROR! +// ^^^^^^^^^^^^ 'foo' is internal to '0x42::m' + } +} +``` + +To allow access from other modules or from scripts, the function must be declared `public` or `public(friend)`. + +要允许从其他模块或脚本访问,该函数必须声明为 `public` 或 `public(friend)`。 + +#### `public` 可见性 (`public` visibility) + +A `public` function can be called by *any* function defined in *any* module or script. As shown in the following example, a `public` function can be called by: +- other functions defined in the same module, +- functions defined in another module, or +- the function defined in a script. + +`public` 函数可以被任何模块或脚本中定义的任何函数调用。如以下示例所示,可以通过以下方式调用 `public` 函数: + +- 在同一模块中定义的其他函数 +- 在另一个模块中定义的函数 +- 在脚本中定义的函数 + +```move= +address 0x42 { + module m { + public fun foo(): u64 { 0 } + fun calls_foo(): u64 { foo() } // valid + } + + module other { + fun calls_m_foo(): u64 { + 0x42::m::foo() // valid + } + } +} + +script { + fun calls_m_foo(): u64 { + 0x42::m::foo() // valid + } +} +``` + +#### `public(friend)` 可见性 (`public(friend)` visibility) + +The `public(friend)` visibility modifier is a more restricted form of the `public` modifier to give more control about where a function can be used. A `public(friend)` function can be called by: +- other functions defined in the same module, or +- functions defined in modules which are explicitly specified in the **friend list** (see [Friends](./friends.md) on how to specify the friend list). + +Note that since we cannot declare a script to be a friend of a module, the functions defined in scripts can never call a `public(friend)` function. + +`public(friend)` 可见性修饰符是一种比 `public` 修饰符限制更严格的形式,可以更好地控制函数的使用位置。 `public(friend)` 函数可以通过以下方式调用: + +- 在同一模块中定义的其他函数,或者在 **friend list** 中明确指定的模块中定义的函数(请参阅 [Friends](./friends.md) 了解如何指定友元(friends)列表)。 + +请注意,由于我们不能将脚本声明为模块的友元关系,因此脚本中定义的函数永远不能调用 `public(friend)` 函数。 + +```move= +address 0x42 { + module m { + friend 0x42::n; // friend declaration + public(friend) fun foo(): u64 { 0 } + fun calls_foo(): u64 { foo() } // valid + } + + module n { + fun calls_m_foo(): u64 { + 0x42::m::foo() // valid + } + } + + module other { + fun calls_m_foo(): u64 { + 0x42::m::foo() // ERROR! + // ^^^^^^^^^^^^ 'foo' can only be called from a 'friend' of module '0x42::m' + } + } +} + +script { + fun calls_m_foo(): u64 { + 0x42::m::foo() // ERROR! +// ^^^^^^^^^^^^ 'foo' can only be called from a 'friend' of module '0x42::m' + } +} +``` + +### `entry` 修饰符 (`entry` modifier) + +The `entry` modifier is designed to allow module functions to be safely and directly invoked much like scripts. This allows module writers to specify which functions can be to begin execution. The module writer then knows that any non-`entry` function will be called from a Move program already in execution. + +Essentially, `entry` functions are the "main" functions of a module, and they specify where Move programs start executing. + +Note though, an `entry` function _can_ still be called by other Move functions. So while they _can_ serve as the start of a Move program, they aren't restricted to that case. + +`entry` 修饰符旨在允许像脚本一样安全直接地调用模块函数。这允许模块编写者指定哪些函数可以成为开始执行的入口。这样模块编写者就知道任何非`entry`函数都是从已经在执行的 Move 程序中被调用的。 + +本质上,`entry` 函数是模块的“main”函数,它们指定 Move 程序开始执行的位置。 + +但请注意,`entry` 函数仍可被其他 Move 函数调用。因此,虽然它们 _可以_ 作为 Move 程序的入口,但它们并不局限于这种用法。 + +例如: + +```move= +address 0x42 { + module m { + public entry fun foo(): u64 { 0 } + fun calls_foo(): u64 { foo() } // valid! + } + + module n { + fun calls_m_foo(): u64 { + 0x42::m::foo() // valid! + } + } + + module other { + public entry fun calls_m_foo(): u64 { + 0x42::m::foo() // valid! + } + } +} + +script { + fun calls_m_foo(): u64 { + 0x42::m::foo() // valid! + } +} +``` + +Even internal functions can be marked as `entry`! This lets you guarantee that the function is called only at the beginning of execution (assuming you do not call it elsewhere in your module) + +甚至内部函数也可以标记为 `entry`!这使你可以保证仅在开始执行时调用该函数(假如你没有在模块中的其他地方调用它) + +```move= +address 0x42 { + module m { + entry fun foo(): u64 { 0 } // valid! entry functions do not have to be public + } + + module n { + fun calls_m_foo(): u64 { + 0x42::m::foo() // ERROR! + // ^^^^^^^^^^^^ 'foo' is internal to '0x42::m' + } + } + + module other { + public entry fun calls_m_foo(): u64 { + 0x42::m::foo() // ERROR! + // ^^^^^^^^^^^^ 'foo' is internal to '0x42::m' + } + } +} + +script { + fun calls_m_foo(): u64 { + 0x42::m::foo() // ERROR! +// ^^^^^^^^^^^^ 'foo' is internal to '0x42::m' + } +} +``` + +### 函数名 (Name) + +Function names can start with letters `a` to `z` or letters `A` to `Z`. After the first character, function names can contain underscores `_`, letters `a` to `z`, letters `A` to `Z`, or digits `0` to `9`. + +函数名称可以以字母 `a` 到 `z` 或字母 `A` 到 `Z` 开头。在第一个字符之后,函数名称可以包含下划线 `_`、字母 `a` 到 `z` 、字母 `A` 到 `Z` 或数字 `0` 到 `9`。 + +```move +fun FOO() {} +fun bar_42() {} +fun _bAZ19() {} +``` + +### 类型参数 (Type Parameters) + +After the name, functions can have type parameters + +函数名后可以有类型参数 + +```move +fun id(x: T): T { x } +fun example(x: T1, y: T2): (T1, T1, T2) { (copy x, x, y) } +``` + +For more details, see [Move generics](./generics.md). + +有关更多详细信息,请参阅 [移动泛型](./generics.md)。 + +### 参数 (Parameters) + +Functions parameters are declared with a local variable name followed by a type annotation + +函数参数使用局部变量名,后跟类型标注的方式进行声明。 + +```move +fun add(x: u64, y: u64): u64 { x + y } +``` + +We read this as `x` has type `u64` + +A function does not have to have any parameters at all. + +(上面代码中的函数参数) 我们读为:`x` 参数的类型是 `u64` 。 + +函数可以没有任何参数。 + +```move +fun useless() { } +``` + +This is very common for functions that create new or empty data structures + +在函数中创建新或空的数据结构是常见的用法。 + +```move= +address 0x42 { + module example { + struct Counter { count: u64 } + + fun new_counter(): Counter { + Counter { count: 0 } + } + + } +} +``` + +### Acquires + +When a function accesses a resource using `move_from`, `borrow_global`, or `borrow_global_mut`, the function must indicate that it `acquires` that resource. This is then used by Move's type system to ensure the references into global storage are safe, specifically that there are no dangling references into global storage. + +当一个函数使用 `move_from`、`borrow_global` 或 `borrow_global_mut` 访问资源时,则该函数必须表明它 `获取(acquires)` 该资源。然后 Move 的类型系统使用它来确保对全局存储的引用是安全的,特别是没有对全局存储的悬垂引用(dangling references)。 + +```move= +address 0x42 { + module example { + + struct Balance has key { value: u64 } + + public fun add_balance(s: &signer, value: u64) { + move_to(s, Balance { value }) + } + + public fun extract_balance(addr: address): u64 acquires Balance { + let Balance { value } = move_from(addr); // acquires needed + value + } + } +} +``` +`acquires` annotations must also be added for transitive calls within the module. Calls to these functions from another module do not need to annotated with these acquires because one module cannot access resources declared in another module--so the annotation is not needed to ensure reference safety. + +`acquires` 标注也必须为模块内有传递性的调用添加。从另一个模块对这些函数的调用不需要使用 `acquires` 进行注释,因为一个模块无法访问在另一个模块中声明的资源——因此不需要用标注来确保引用安全。 + +```move= +address 0x42 { + module example { + + struct Balance has key { value: u64 } + + public fun add_balance(s: &signer, value: u64) { + move_to(s, Balance { value }) + } + + public fun extract_balance(addr: address): u64 acquires Balance { + let Balance { value } = move_from(addr); // acquires needed + value + } + + public fun extract_and_add(sender: address, receiver: &signer) acquires Balance { + let value = extract_balance(sender); // acquires needed here + add_balance(receiver, value) + } + } +} + +address 0x42 { + module other { + fun extract_balance(addr: address): u64 { + 0x42::example::extract_balance(addr) // no acquires needed + } + } +} +``` + +A function can `acquire` as many resources as it needs to + +函数可以根据需要 `acquire` 尽可能多的资源。 + +```move= +address 0x42 { + module example { + use std::vector; + + struct Balance has key { value: u64 } + struct Box has key { items: vector } + + public fun store_two( + addr: address, + item1: Item1, + item2: Item2, + ) acquires Balance, Box { + let balance = borrow_global_mut(addr); // acquires needed + balance.value = balance.value - 2; + let box1 = borrow_global_mut>(addr); // acquires needed + vector::push_back(&mut box1.items, item1); + let box2 = borrow_global_mut>(addr); // acquires needed + vector::push_back(&mut box2.items, item2); + } + } +} +``` + +### 返回类型 (Return type) + +After the parameters, a function specifies its return type. + +在参数之后,函数指定其返回类型。 + +```move +fun zero(): u64 { 0 } +``` + +Here `: u64` indicates that the function's return type is `u64`. + +Using tuples, a function can return multiple values + +这里 `: u64` 表示函数的返回类型是 `u64`。 + +使用元组,一个函数可以返回多个值: + +```move +fun one_two_three(): (u64, u64, u64) { (0, 1, 2) } +``` + +If no return type is specified, the function has an implicit return type of unit `()`. These functions are equivalent + +如果未指定返回类型,则该函数具有隐式返回类型单值 `()`。这些函数是等价的: + +```move +fun just_unit(): () { () } +fun just_unit() { () } +fun just_unit() { } +``` + +`script` functions must have a return type of unit `()` + +`script` 函数的返回类型必须为单值 `()`(不能是任何其他类型,例如 `bool`、`u64` 等,注者注): + +```move +script { + fun do_nothing() { + } +} +``` + +As mentioned in the [tuples section](./tuples.md), these tuple "values" are virtual and do not exist at runtime. So for a function that returns unit `()`, it will not be returning any value at all during execution. + +如[元组部分](./tuples.md)所述,这些元组“值”是虚拟的(virtual),且在运行时不存在。因此,对于返回单值 `()` 的函数,它在执行期间根本不会返回任何值。 + +### Function body (函数体) + +A function's body is an expression block. The return value of the function is the last value in the sequence + +函数体是一个表达式块。函数的返回值是序列中最后一个表达式的值。 + +```move= +fun example(): u64 { + let x = 0; + x = x + 1; + x // returns 'x' +} +``` + +See [the section below for more information on returns](#returning-values) + +请参阅[有关返回值的更多信息](#returning-values) + +For more information on expression blocks, see [Move variables](./variables.md). + +有关表达式块的更多信息,请参阅 [Move variables](./variables.md)。 + +### Native Functions + +Some functions do not have a body specified, and instead have the body provided by the VM. These functions are marked `native`. + +Without modifying the VM source code, a programmer cannot add new native functions. Furthermore, it is the intent that `native` functions are used for either standard library code or for functionality needed for the given Move environment. + +Most `native` functions you will likely see are in standard library code such as `vecto + +有些函数没有函数体,而是由 Move VM 提供的函数体。这些函数被标记为 `native`。 + +如果不修改 Move VM 源代码,程序员就无法添加新的 `native` 函数。此外,`native` 函数的意图是用于标准库代码或 Move 环境所需的基础功能。 + +你看到的大多数 `native` 函数可能都在标准库代码中,例如 `vector` + +```move= +module std::vector { + native public fun empty(): vector; + ... +} +``` + +## 调用 (Calling) + +When calling a function, the name can be specified either through an alias or fully qualified + +调用函数时,名称可以通过别名或完全限定名指定 + +```move= +address 0x42 { + module example { + public fun zero(): u64 { 0 } + } +} + +script { + use 0x42::example::{Self, zero}; + fun call_zero() { + // With the `use` above all of these calls are equivalent + 0x42::example::zero(); + example::zero(); + zero(); + } +} +``` + +When calling a function, an argument must be given for every parameter. + +调用函数时,每个参数必须指定一个值。 + +```move= +address 0x42 { + module example { + public fun takes_none(): u64 { 0 } + public fun takes_one(x: u64): u64 { x } + public fun takes_two(x: u64, y: u64): u64 { x + y } + public fun takes_three(x: u64, y: u64, z: u64): u64 { x + y + z } + } +} + +script { + use 0x42::example; + fun call_all() { + example::takes_none(); + example::takes_one(0); + example::takes_two(0, 1); + example::takes_three(0, 1, 2); + } +} +``` + +Type arguments can be either specified or inferred. Both calls are equivalent. + +函数的类型参数可以被指定或推断出来。以下两个调用是等价的。 + +```move= +address 0x42 { + module example { + public fun id(x: T): T { x } + } +} + +script { + use 0x42::example; + fun call_all() { + example::id(0); + example::id(0); + } +} +``` + +For more details, see [Move generics](./generics.md). + +有关更多详细信息,请参阅 [Move generics](./generics.md)。 + + +## Returning values (返回值) + +The result of a function, its "return value", is the final value of its function body. For example + +一个函数的结果,也就是它的“返回值”,是函数体的最后一个值。例如: + +```move= +fun add(x: u64, y: u64): u64 { + x + y +} +``` + +[As mentioned above](#function-body), the function's body is an [expression block](./variables.md). The expression block can sequence various statements, and the final expression in the block will be be the value of that block + +[如上所述](#function-body),函数体是一个[表达式块](./variables.md)。表达式块中可以有各种各种语句,块中最后一个表达式将是该表达式块的值。 + +```move= +fun double_and_add(x: u64, y: u64): u64 { + let double_x = x * 2; + let double_y = y * 2; + double_x + double_y +} +``` + +The return value here is `double_x + double_y` + +这里的返回值是 `double_x + double_y` + +### `return` 表达式 (`return` expression) + +A function implicitly returns the value that its body evaluates to. However, functions can also use the explicit `return` expression: + +函数隐式返回其函数体计算的值。但是,函数也可以使用显式的 `return` 表达式: + +```move +fun f1(): u64 { return 0 } +fun f2(): u64 { 0 } +``` + + +These two functions are equivalent. In this slightly more involved example, the function subtracts two `u64` values, but returns early with `0` if the second value is too large: + +这两个功能是等价的。在下面这个稍微复杂的示例中,该函数返回两个 `u64` 值相减的结果,但如果第二个值大于第一个值,则提前返回 `0` : + +```move= +fun safe_sub(x: u64, y: u64): u64 { + if (y > x) return 0; + x - y +} +``` + +Note that the body of this function could also have been written as `if (y > x) 0 else x - y`. + +However `return` really shines is in exiting deep within other control flow constructs. In this example, the function iterates through a vector to find the index of a given value: + +请注意,这个函数的函数体也可以写成 `if (y > x) 0 else x - y`。 + +然而,`return` 真正的亮点在于在其他控制流结构的深处退出。在此示例中,函数遍历数组以查找给定值的索引: + +```move= +use std::vector; +use std::option::{Self, Option}; +fun index_of(v: &vector, target: &T): Option { + let i = 0; + let n = vector::length(v); + while (i < n) { + if (vector::borrow(v, i) == target) return option::some(i); + i = i + 1 + }; + + option::none() +} +``` + +Using `return` without an argument is shorthand for `return ()`. That is, the following two functions are equivalent: + +使用不带参数的 `return` 是 `return ()` 的简写。即以下两个函数是等价的: + +```move +fun foo() { return } +fun foo() { return () } +``` diff --git a/language/documentation/book/translations/move-book-zh/src/generics.md b/language/documentation/book/translations/move-book-zh/src/generics.md new file mode 100644 index 0000000000..6e4d858eb6 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/generics.md @@ -0,0 +1,528 @@ +# 泛型 (generics) + +Generics can be used to define functions and structs over different input data types. This language feature is sometimes referred to as *parametric polymorphism*. In Move, we will often use the term generics interchangeably with type parameters and type arguments. + +泛型可用于定义具有不同输入数据类型的函数和结构体。这种语言特性被称为参数多态。在 Move语言中,我们经常将交替使用术语泛型与类型参数和类型参数。 + +Generics are commonly used in library code, such as in vector, to declare code that works over any possible instantiation (that satisfies the specified constraints). In other frameworks, generic code can sometimes be used to interact with global storage many different ways that all still share the same implementation. + +泛型通常用于库代码中,例如 `vector`,以声明适用于任何可实例化(满足指定约束)的代码。在其他框架中,泛型代码有时可用多种不同的方式与全局存储交互,这些方式有着相同的实现。 + + +## 声明类型参数 (Declaring Type Parameters) + +Both functions and structs can take a list of type parameters in their signatures, enclosed by a pair of angle brackets `<...>` . + +函数和结构体都可以在其签名中采用类型参数列表,由一对尖括号括起来 `<...>` 。 + +### 泛型函数 (Generic Functions) + + +Type parameters for functions are placed after the function name and before the (value) parameter list. The following code defines a generic identity function that takes a value of any type and returns that value unchanged. + +函数的类型参数放在函数名称之后和(值)参数列表之前。以下代码定义了一个名为id的泛型函数,该函数接受任何类型的值并返回原值。 + +```move +fun id(x: T): T { + // this type annotation is unnecessary but valid + (x: T) +} +``` + +Once defined, the type parameter `T` can be used in parameter types, return types, and inside the function body. + +一旦定义,类型参数 `T` 就可以在参数类型、返回类型和函数体内使用。 + +### 泛型结构体 (Generic Structs) + +Type parameters for structs are placed after the struct name, and can be used to name the types of the fields. + +结构体的类型参数放在结构名称之后,并可用于命名字段的类型。 + +```move +struct Foo has copy, drop { x: T } + +struct Bar has copy, drop { + x: T1, + y: vector, +} +``` + +[Note that type parameters do not have to be used](#unused-type-parameters) + +请注意,[未使用的类型参数](#unused-type-parameters) + +## 类型参数 (Type Arguments) + +### 调用泛型函数 (Calling Generic Functions) + +When calling a generic function, one can specify the type arguments for the function's type parameters in a list enclosed by a pair of angle brackets. + +调用泛型函数时,可以在由一对尖括号括起来的列表中指定函数类型参数。 + +```move +fun foo() { + let x = id(true); +} +``` + +If you do not specify the type arguments, Move's [type inference](#type-inference) will supply them for you. + +如果您不指定类型参数,Move语言的[类型推断](#type-inference)功能将为您匹配正确的类型 + +### 使用泛型结构 (Using Generic Structs) + +Similarly, one can attach a list of type arguments for the struct's type parameters when constructing or destructing values of generic types. + +类似地,在构造或销毁泛型类型的值时,可以为结构体的类型参数附加一个参数列表。 + +```move +fun foo() { + let foo = Foo { x: true }; + let Foo { x } = foo; +} +``` + +If you do not specify the type arguments, Move's [type inference](#type-inference) will supply them for you. + +如果您不指定类型参数,Move 语言的[类型推断](#type-inference)功能将为您自动补充(supply)。 + +### 类型参数不匹配 (Type Argument Mismatch) + +If you specify the type arguments and they conflict with the actual values supplied, an error will be given + +如果您指定类型参数与实际提供的值不匹配,则会报错 + +```move +fun foo() { + let x = id(true); // error! true is not a u64 +} +``` + +同样地 + +```move +fun foo() { + let foo = Foo { x: 0 }; // error! 0 is not a bool + let Foo
{ x } = foo; // error! bool is incompatible with address +} +``` + +## Type Inference (类型推断) + +In most cases, the Move compiler will be able to infer the type arguments so you don't have to write them down explicitly. Here's what the examples above would look like if we omit the type arguments. + +在大多数情况下,Move 编译器能够推断类型参数,因此您不必显式地写下它们。这是上面例子中省略类型参数写法的示例。 + +```move +fun foo() { + let x = id(true); + // ^ is inferred + + let foo = Foo { x: true }; + // ^ is inferred + + let Foo { x } = foo; + // ^ is inferred +} +``` + +Note: when the compiler is unable to infer the types, you'll need annotate them manually. A common scenario is to call a function with type parameters appearing only at return positions. + +注意:当编译器无法推断类型时,您需要手动标注它们(类型参数)。一个常见的场景是调用一个类型参数只出现在返回位置的函数。 + +```move +address 0x2 { + module m { + using std::vector; + + fun foo() { + // let v = vector::new(); + // ^ The compiler cannot figure out the element type. + + let v = vector::new(); + // ^~~~~ Must annotate manually. + } + } +} +``` + +However, the compiler will be able to infer the type if that return value is used later in that function + +但是,如果稍后在该函数中使用该返回值,编译器将能够推断其类型。 + +```move +address 0x2 { + module m { + using std::vector; + + fun foo() { + let v = vector::new(); + // ^ is inferred + vector::push_back(&mut v, 42); + } + } +} +``` + +## Unused Type Parameters (未使用的类型参数) + +For a struct definition, an unused type parameter is one that +does not appear in any field defined in the struct, but is checked statically at compile time. +Move allows unused type parameters so the following struct definition is valid: + +对于结构体定义,未使用的类型参数是没有出现在结构体中定义的任何字段中,但在编译时会静态检查的类型参数。Move语言允许未使用的类型参数,因此以下结构定义是有效的: + +```move +struct Foo { + foo: u64 +} +``` + +This can be convenient when modeling certain concepts. Here is an example: + +这在对某些概念进行建模时会很方便。这是一个例子: + +```move +address 0x2 { + module m { + // Currency Specifiers + struct Currency1 {} + struct Currency2 {} + + // A generic coin type that can be instantiated using a currency + // specifier type. + // e.g. Coin, Coin etc. + struct Coin has store { + value: u64 + } + + // Write code generically about all currencies + public fun mint_generic(value: u64): Coin { + Coin { value } + } + + // Write code concretely about one currency + public fun mint_concrete(value: u64): Coin { + Coin { value } + } + } +} +``` + +In this example, `struct Coin` is generic on the `Currency` type parameter, +which specifies the currency of the coin and allows code to be written either generically on any currency or +concretely on a specific currency. +This genericity applies even when the `Currency` type parameter does not appear in any of the fields defined in `Coin`. + +在此示例中, `struct Coin` 是类型参数为 `Currency` 的泛型结构体,它指定 `Coin` 的类型参数是 `Currency`,这样就允许代码选择是使用任意类型 `Currency` 或者是指定的具体类型 `Currency` 。即使 `Currency` 类型参数没有出现在定义的任何字段中,这种泛型性也适用结构体 `Coin`。 + +### Phantom Type Parameters + +In the example above, although `struct Coin` asks for the `store` ability, neither `Coin` nor `Coin` will have the `store` ability. +This is because of the rules for [Conditional Abilities and Generic Types](./abilities.md#conditional-abilities-and-generic-types) and the fact that `Currency1` and `Currency2` don't have the `store` ability, despite the fact that they are not even used in the body of `struct Coin`. +This might cause some unpleasant consequences. +For example, we are unable to put `Coin` into a wallet in the global storage. + +在上面的例子中,虽然 `struct Coin` 要求有 `store` 能力,但 `Coin` 和 `Coin` 都没有 `store` 能力。这是因为 [条件能力与泛型类型](./chatper_19_abilities.md#conditional-abilities-and-generic-types)的规则, 而实际上 `Currency1`和 `Currency2` 本身都没有 `store` 能力,尽管它们甚至没有在`struct Coin` 的主体中使用. 这可能会导致一些不好的后果。例如,我们无法将 `Coin` 放入全局存储的一个钱包中。 + +One possible solution would be to add spurious ability annotations to `Currency1` and `Currency2` (i.e., `struct Currency1 has store {}`). +But, this might lead to bugs or security vulnerabilities because it weakens the types with unnecessary ability declarations. +For example, we would never expect a resource in the global storage to have a field in type `Currency1`, but this would be possible with the spurious `store` ability. +Moreover, the spurious annotations would be infectious, requiring many functions generic on the unused type parameter to also include the necessary constraints. + +一种可能的解决方案是向 `Currency1` 和 `Currency2` 添加虚假能力标注(例如:`struct Currency1 has store {}`) 。但是,这可能会导致 bugs 或安全漏洞,因为它削弱了类型安全,声明了不必要的能力。例如,我们永远不会期望全局存储中的资源具有 `Currency1` 类型的字段,但这对于虚假 `store` 能力是可能发生的。 +此外,虚假标注具有传染性,需要在许多未使用类型参数的泛型函数上也引入必要的约束。 + +Phantom type parameters solve this problem. Unused type parameters can be marked as *phantom* type parameters, +which do not participate in the ability derivation for structs. +In this way, arguments to phantom type parameters are not considered when deriving the abilities for generic types, thus avoiding the need for spurious ability annotations. +For this relaxed rule to be sound, Move's type system guarantees that a parameter declared as phantom is either not used at all in the struct definition, or it is only used as an argument to type parameters also declared as phantom. + +Phantom 类型参数解决了这个问题。未使用的类型参数可以标记为 *phantom* 类型参数,不参与结构体的能力推导。这样,在派生泛型类型的能力时,不考虑 phantom type 的参数,从而避免了对虚假能力标注的需要。为了使这个宽松的规则合理,Move 的类型系统保证声明为 `phantom` 的参数要么不在结构定义中使用,要么仅用作声明为 `phantom` 的类型参数的参数。 + +#### 声明 (Declaration) + + +In a struct definition a type parameter can be declared as phantom by adding the `phantom` keyword before its declaration. +If a type parameter is declared as phantom we say it is a phantom type parameter. +When defining a struct, Move's type checker ensures that every phantom type parameter is either not used inside the struct definition +or it is only used as an argument to a phantom type parameter. + +`phantom` 在结构定义中,可以通过在声明之前添加关键字来将类型参数声明为幻影。如果一个类型参数被声明为 phantom,我们就说它是 phantom 类型参数。 +定义结构体时,Move语言的类型检查器确保每个 phantom 类型参数要么不在结构定义中使用,要么仅用作 phantom 类型参数的参数。 + +More formally, if a type is used as an argument to a phantom type parameter we say the type appears in _phantom position_. +With this definition in place, the rule for the correct use of phantom parameters can be specified as follows: **A phantom type parameter can only appear in phantom position**. + +更正式地说,如果将类型用作 phantom 类型参数的输入参数,我们说该类型出现在 _phantom 位置_。有了这个定义,正确使用 phantom 参数的规则可以指定如下: ** phantom 类型参数只能出现在 phantom 位置**。 + +The following two examples show valid uses of phantom parameters. +In the first one, the parameter `T1` is not used at all inside the struct definition. +In the second one, the parameter `T1` is only used as an argument to a phantom type parameter. + +以下两个示例显示了 phantom 参数的 合法用法。在第一个中,`T1` 在结构体定义中根本不使用参数。在第二种情况下,参数 `T1` 仅用作 phantom 类型参数的参数。 + +```move +struct S1 { f: u64 } + ^^ + Ok: T1 does not appear inside the struct definition + + +struct S2 { f: S1 } + ^^ + Ok: T1 appears in phantom position +``` + +The following code shows examples of violations of the rule: + +以下代码展示违反规则的示例: + +```move +struct S1 { f: T } + ^ + Error: Not a phantom position + +struct S2 { f: T } + +struct S3 { f: S2 } + ^ + Error: Not a phantom position +``` + +#### 实例化 (Instantiation) + +When instantiating a struct, the arguments to phantom parameters are excluded when deriving the struct abilities. +For example, consider the following code: + +实例化结构时,派生结构功能时会排除幻影参数的输入参数。例如,考虑以下代码: + +```move +struct S has copy { f: T1 } +struct NoCopy {} +struct HasCopy has copy {} +``` +Consider now the type `S`. Since `S` is defined with `copy` and all non-phantom arguments have copy then `S` also has copy. + +现在考虑类型 `S`。由于 `S` 用 `copy` 定义,且所有非 phantom 参数 具有 `copy` 能力,所以 `S` 也具有 `copy` 能力。 + +#### 具有能力约束的 Phantom 类型参数 (Phantom Type Parameters with Ability Constraints) + +Ability constraints and phantom type parameters are orthogonal features in the sense that phantom parameters can be declared with ability constraints. +When instantiating a phantom type parameter with an ability constraint, the type argument has to satisfy that constraint, even though the parameter is phantom. +For example, the following definition is perfectly valid: + +能力约束和 phantom 类型参数是正交特征,因此 phantom 参数声明时可以用能力进行约束。当使用能力约束实例化一个 phantom 类型参数时,类型参数必须满足该约束,即使参数是。 +例如,以下定义是完全有效的: + +```move +struct S {} +``` + +The usual restrictions apply and `T` can only be instantiated with arguments having `copy`. + +应用(phantom)通常的限制并且 `T` 只能用具有 `copy` 的参数实例化。 + +## 约束 (Constraints) + +In the examples above, we have demonstrated how one can use type parameters to define "unknown" types that can be plugged in by callers at a later time. This however means the type system has little information about the type and has to perform checks in a very conservative way. In some sense, the type system must assume the worst case scenario for an unconstrained generic. Simply put, by default generic type parameters have no [abilities](./abilities.md). + +This is where constraints come into play: they offer a way to specify what properties these unknown types have so the type system can allow operations that would otherwise be unsafe. + +在上面的例子中,我们已经演示了如何使用类型参数来定义“未知”类型,这些类型可以在稍后被调用者插入。然而,这意味着类型系统几乎没有关于类型的信息,并且必须以非常保守的方式执行检查。从某种意义上说,类型系统必须假设不受约束的泛型时的最坏场景。简单地说,默认情况下泛型类型参数没有[能力](./abilities.md)。 + +这就是约束发挥作用的地方:它们提供了一种方法来指定这些未知类型具有哪些属性,因此类型系统可以允许相应的操作,否则会不安全。 + +### 声明约束 (Declaring Constraints) + +Constraints can be imposed on type parameters using the following syntax. + +可以使用以下语法对类型参数施加约束。 + +```move +// T is the name of the type parameter +T: (+ )* +``` + +The `` can be any of the four [abilities](./abilities.md), and a type parameter can be constrained with multiple [abilities](./abilities.md) at once. So all of the following would be valid type parameter declarations + +`` 可以是四种[能力](./abilities.md)中的任何一种,一个类型参数可以同时被多个能力约束。因此,以下所有内容都是有效的类型参数声明 + +```move +T: copy +T: copy + drop +T: copy + drop + store + key +``` + +### 验证约束 (Verifying Constraints) + +Constraints are checked at call sites so the following code won't compile. + +在调用的地方会检查约束,因此以下代码无法编译。 + +```move +struct Foo { x: T } + +struct Bar { x: Foo } +// ^ error! u8 does not have 'key' + +struct Baz { x: Foo } +// ^ error! T does not have 'key' +``` + +```move +struct R {} + +fun unsafe_consume(x: T) { + // error! x does not have 'drop' +} + +fun consume(x: T) { + // valid! + // x will be dropped automatically +} + +fun foo() { + let r = R {}; + consume(r); + // ^ error! R does not have 'drop' +} +``` + +```move +struct R {} + +fun unsafe_double(x: T) { + (copy x, x) + // error! x does not have 'copy' +} + +fun double(x: T) { + (copy x, x) // valid! +} + +fun foo(): (R, R) { + let r = R {}; + double(r) + // ^ error! R does not have copy +} +``` + +For more information, see the abilities section on [conditional abilities and generic types](./abilities.html#conditional-abilities-and-generic-types) + +有关更多信息,请参阅有关[条件能力与泛型类型](./abilities.md#conditional-abilities-and-generic-types) + +## 递归的限制 (Limitations on Recursions) + +### 递归结构 (Recursive Structs) + +Generic structs can not contain fields of the same type, either directly or indirectly, even with different type arguments. All of the following struct definitions are invalid: + +泛型结构不能直接或间接包含相同类型的字段,即使使用不同的类型参数也是如此。以下所有结构定义均无效: + +```move +struct Foo { + x: Foo // error! 'Foo' containing 'Foo' +} + +struct Bar { + x: Bar // error! 'Bar' containing 'Bar' +} + +// error! 'A' and 'B' forming a cycle, which is not allowed either. +struct A { + x: B +} + +struct B { + x: A + y: A +} +``` + +### 高级主题:类型-级别递归 (Advanced Topic: Type-level Recursions) + +Move allows generic functions to be called recursively. However, when used in combination with generic structs, this could create an infinite number of types in certain cases, and allowing this means adding unnecessary complexity to the compiler, vm and other language components. Therefore, such recursions are forbidden. + +Move语言允许递归调用泛型函数。但是,当与泛型结构体结合使用时,在某些情况下可能会创建无限数量的类型,这意味着会给编译器、vm 和其他语言组件增加不必要的复杂性。因此,这种递归是被禁止的。 + +被允许的用法: +```move +address 0x2 { + module m { + struct A {} + + // Finitely many types -- allowed. + // foo -> foo -> foo -> ... is valid + fun foo() { + foo(); + } + + // Finitely many types -- allowed. + // foo -> foo> -> foo> -> ... is valid + fun foo() { + foo>(); + } + } +} +``` + +不被允许的用法: + +```move +address 0x2 { + module m { + struct A {} + + // Infinitely many types -- NOT allowed. + // error! + // foo -> foo> -> foo>> -> ... + fun foo() { + foo>(); + } + } +} + +address 0x2 { + module n { + struct A {} + + // Infinitely many types -- NOT allowed. + // error! + // foo -> bar -> foo> + // -> bar, T2> -> foo, A> + // -> bar, A> -> foo, A>> + // -> ... + fun foo() { + bar(); + } + + fun bar { + foo>(); + } + } +} +``` + +Note, the check for type level recursions is based on a conservative analysis on the call sites and does NOT take control flow or runtime values into account. + +请注意,类型级别递归的检查是基于对调用现场的保守分析,并且不考虑控制流或运行时值。 + +```move +address 0x2 { + module m { + struct A {} + + fun foo(n: u64) { + if (n > 0) { + foo>(n - 1); + }; + } + } +} +``` + + +The function in the example above will technically terminate for any given input and therefore only creating finitely many types, but it is still considered invalid by Move's type system. + +上例中的函数将在技术上给定有限的输入,因此只会创建有限多的类型,但仍然会被 Move 语言的类型系统认为是无效的。 diff --git a/language/documentation/book/translations/move-book-zh/src/global-storage-operators.md b/language/documentation/book/translations/move-book-zh/src/global-storage-operators.md new file mode 100644 index 0000000000..d31b2959f4 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/global-storage-operators.md @@ -0,0 +1,312 @@ +# 全局存储 - 操作(Global Storage - Operators) + +Move programs can create, delete, and update [resources](./structs-and-resources.md) in global storage using the following five instructions: + +Move程序可以使用下面五种指令创建、删除、更新全局存储中的[资源](./structs-and-resources.md): + +| Operation | Description | Aborts? | +---------------------------------------- |---------------------------------------------------------------- |---------------------------------------- | +|`move_to(&signer,T)` | Publish `T` under `signer.address` | If `signer.address` already holds a `T` | +|`move_from(address): T` | Remove `T` from `address` and return it | If `address` does not hold a `T` | +|`borrow_global_mut(address): &mut T` | Return a mutable reference to the `T` stored under `address` | If `address` does not hold a `T` | +|`borrow_global(address): &T` | Return an immutable reference to the `T` stored under `address` | If `address` does not hold a `T` | +|`exists(address): bool` | Return `true` if a `T` is stored under `address` | Never + +| 操作符 | 描述 | 出错 | +---------------------------------------- |------------------------------------------------------ |---------------------------------- | +|`move_to(&signer,T)` | 在 `signer.address` 下发布 `T` | 如果 `signer.address` 已经存在 `T` | +|`move_from(address): T` | 从 `address` 下删除 `T` 并返回 | 如果 `address` 下没有 `T` | +|`borrow_global_mut(address): &mut T` | 返回 `address` 下 `T` 的可变引用 mutable reference | 如果 `address` 下没有 `T` | +|`borrow_global(address): &T` | 返回 `address` 下 `T` 的不可变引用 immutable reference | 如果 `address` 下没有 `T` | +|`exists(address): bool` | 返回 `address` 下的 `T` | 永远不会 | + | + +Each of these instructions is parameterized by a type `T` with the [`key` ability](./abilities.md). However, each type `T` *must be declared in the current module*. This ensures that a resource can only be manipulated via the API exposed by its defining module. The instructions also take either an [`address`](./address.md) or [`&signer`](./signer.md) representing the account address where the resource of type `T` is stored. + +每个指令的参数 `T` 都具有 [`key` 能力](./abilities.md)。然而,类型 `T` *必须在当前模块*中声明。这确保资源只能通过当前模块暴露的 API 来操作。指令在存储 `T` 类型资源的同时,使用 [`address`](./address.md) 或 [`&signer`](./signer.md) 表示账户地址。 + + +## 资源参考(References to resources) + +References to global resources returned by `borrow_global` or `borrow_global_mut` mostly behave like references to local storage: they can be extended, read, and written using ordinary [reference operators](./references.md) and passed as arguments to other function. However, there is one important difference between local and global references: **a function cannot return a reference that points into global storage**. For example, these two functions will each fail to compile: + +`borrow_global` 或 `borrow_global_mut` 指令返回的全局资源引用在大多数情况下类似本地存储的引用:它们可以通过[引用操作](./references.md)进行拓展、读和写,也可以作为其它函数的参数。然而本地引用和全局引用有个重要差异:**函数不能返回指向全局存储的引用**。例如,下面两个函数编译会失败: + + +```move +struct R has key { f: u64 } +// 不能编译 // will not compile +fun ret_direct_resource_ref_bad(a: address): &R { + borrow_global(a) // error! +} +// 也不能编译 // also will not compile +fun ret_resource_field_ref_bad(a: address): &u64 { + &borrow_global(a).f // error! +} +``` + +Move must enforce this restriction to guarantee absence of dangling references to global storage. [This](#reference-safety-for-global-resources) section contains much more detail for the interested reader. + +Move必须强制这种限制来保证全局存储引用不会出现空引用。对于感兴趣的读者,[此节](#全局资源引用安全)包含了更多的细节。 + + +## 使用泛型的全局存储操作(Global storage operators with generics) + +Global storage operations can be applied to generic resources with both instantiated and uninstantiated generic type parameters: + +全局存储操作可以与实例化和未实例化的泛型资源参数使用: + + +```move +struct Container has key { t: T } + +/// 发布用于存储调用者提供 T 类型对象的 Container /// Publish a Container storing a type T of the caller's choosing +fun publish_generic_container(account: &signer, t: T) { + move_to>(account, Container { t }) +} + +/// 发布存储 u64 类型的 Container /// Publish a container storing a u64 +fun publish_instantiated_generic_container(account: &signer, t: u64) { + move_to>(account, Container { t }) +} +``` + +The ability to index into global storage via a type parameter chosen at runtime is a powerful Move feature known as *storage polymorphism*. For more on the design patterns enabled by this feature, see [Move generics](./generics.md). + +能够通过参数类型在运行时中索引全局存储的能力是 Move 的强大特性,该特性称之为*存储多态性*。关于此特性更多的设计模式,请参考[Move泛型](./generics.md)这节。 + +## 示例: `Counter` (Example: `Counter`) + +The simple `Counter` module below exercises each of the five global storage operators. The API exposed by this module allows: + +- Anyone to publish a `Counter` resource under their account +- Anyone to check if a `Counter` exists under any address +- Anyone to read or increment the value of a `Counter` resource under any address +- An account that stores a `Counter` resource to reset it to zero +- An account that stores a `Counter` resource to remove and delete it + +下面简单的 `Counter` 模块使用五种全局存储操作。该模块暴露的API允许: + +- 任何人可以在他们的账户下发布 `Counter` 资源。 +- 任何人可以检查任何地址下是否包含 `Counter`。 +- 任何人可以读或增加任何地址下的 `Counter` 值。 +- 存储 `Counter` 资源的账号可以将其重置为 0。 +- 存储 `Counter` 资源的账号可以删除该对象。 + +```move +address 0x42 { +module counter { + use std::signer; + + /// 包含整数的资源 /// Resource that wraps an integer counter + struct Counter has key { i: u64 } + + /// 给定账户下发布带有 `i` 值的 `Counter` 资源 /// Publish a `Counter` resource with value `i` under the given `account` + public fun publish(account: &signer, i: u64) { + // “打包"(创建)Counter 资源。这是需要授权的操作,只能在声明 `Counter` 资源的此模块内执行。 // "Pack" (create) a Counter resource. This is a privileged operation that can only be done inside the module that declares the `Counter` resource + move_to(account, Counter { i }) + } + + /// 读取 `addr` 地址下 `Counter` 内的值 /// Read the value in the `Counter` resource stored at `addr` + public fun get_count(addr: address): u64 acquires Counter { + borrow_global(addr).i + } + + /// 增加 `addr` 地址下 `Counter` 内的值 /// Increment the value of `addr`'s `Counter` resource + public fun increment(addr: address) acquires Counter { + let c_ref = &mut borrow_global_mut(addr).i; + *c_ref = *c_ref + 1 + } + + /// 将 `account` 的 `Counter` 重置为 0 /// Reset the value of `account`'s `Counter` to 0 + public fun reset(account: &signer) acquires Counter { + let c_ref = &mut borrow_global_mut(signer::address_of(account)).i; + *c_ref = 0 + } + + /// 删除 `account` 的 `Counter` 资源并返回其内值 /// Delete the `Counter` resource under `account` and return its value + public fun delete(account: &signer): u64 acquires Counter { + // 删除 Counter 资源 // remove the Counter resource + let c = move_from(signer::address_of(account)); + // 将 `Counter` 资源“拆”为字段。这是需要授权的操作,只能在声明 `Counter` 资源的此模块内执行。 // "Unpack" the `Counter` resource into its fields. This is a privileged operation that can only be done inside the module that declares the `Counter` resource + let Counter { i } = c; + i + } + + /// 如果 `addr` 下包含 `Counter` 资源,则返回 `true`。 /// Return `true` if `addr` contains a `Counter` resource + public fun exists(addr: address): bool { + exists(addr) + } +} +} +``` + +## `acquires` 函数标注(Annotating functions with `acquires`) + +In the `counter` example, you might have noticed that the `get_count`, `increment`, `reset`, and `delete` functions are annotated with `acquires Counter`. A Move function `m::f` must be annotated with `acquires T` if and only if: + +- The body of `m::f` contains a `move_from`, `borrow_global_mut`, or `borrow_global` instruction, or +- The body of `m::f` invokes a function `m::g` declared in the same module that is annotated with `acquires` + +在 `counter` 例子中,可以注意到 `get_count`、`increment`、`reset` 和 `delete` 方法都使用 `acquires Counter` 进行标注。函数 `m::f` 在且仅在下述情况必须使用 `acquires T` 进行标注: + +- `m::f` 的主体包含 `move_from`、`borrow_global_mut` 或 `borrow_global` 指令调用 +- `m::f` 的主体调用了同模块内被 `acquires` 注解的 `m::g` 的函数 + +For example, the following function inside `Counter` would need an `acquires` annotation: + +例如,下面 `Counter` 内的函数需要使用 `acquires` 标注: + +```move +// 由于 `increment` 使用了 `acquires` 标注,所以函数需要 `acquires` // Needs `acquires` because `increment` is annotated with `acquires` +fun call_increment(addr: address): u64 acquires Counter { + counter::increment(addr) +} +``` + +However, the same function *outside* `Counter` would not need an annotation: + +然而,在 `Counter` *外面*的函数则不需要进行标注: + + +```move +address 0x43 { +module m { + use 0x42::counter; + + // 可以,仅在函数声明在同一模块内时需要标注 // Ok. Only need annotation when resource acquired by callee is declared in the same module + fun call_increment(addr: address): u64 { + counter::increment(addr) + } +} +} +``` + +If a function touches multiple resources, it needs multiple `acquires`: + +如果函数需要多个资源,`acquires` 则需要多个参数: + +```move= +address 0x42 { +module two_resources { + struct R1 has key { f: u64 } + struct R2 has key { g: u64 } + + fun double_acquires(a: address): u64 acquires R1, R2 { + borrow_global(a).f + borrow_global.g + } +} +} +``` + +The `acquires` annotation does not take generic type parameters into account: + +`acquires` 标注不会将泛型类型参数纳入声明中: + + +```move= +address 0x42 { +module m { + struct R has key { t: T } + + // 效果为 `acquires R` 而不是 `acquires R` // `acquires R`, not `acquires R` + fun acquire_generic_resource(a: addr) acquires R { + let _ = borrow_global>(a); + } + + // 效果为 `acquires R` 而不是 `acquiresR` // `acquires R`, not `acquires R + fun acquire_instantiated_generic_resource(a: addr) acquires R { + let _ = borrow_global>(a); + } +} +} +``` + +Finally: redundant `acquires` are not allowed. Adding this function inside `Counter` will result in a compilation error: + +最后:不允许使用不必要的 `acquires`。在 `Counter` 内添加下述方法将会导致编译错误: + + +```move +// 下面代码不会编译,因为函数体没有使用全局存储指令也没调用使用 `acquires` 注解的函数 // This code will not compile because the body of the function does not use a global storage instruction or invoke a function with `acquires` +fun redundant_acquires_bad() acquires Counter {} +``` + +For more information on `acquires`, see [Move functions](./functions.md). + +关于 `acquires` 更多信息,参见 [Move 函数](./functions.md)。 + +## 全局资源引用安全(Reference Safety For Global Resources) + +Move prohibits returning global references and requires the `acquires` annotation to prevent dangling references. This allows Move to live up to its promise of static reference safety (i.e., no dangling references, no `null` or `nil` dereferences) for all [reference](./references.md) types. + +Move 禁止返回全局引用并且需要使用 `acquires` 标注来防止空引用。这使 Move 保证了所有[引用](./references.md)类型的静态引用安全性(例如,没有空引用、不会解引用 `null` 或 `nil` 对象)。 + +This example illustrates how the Move type system uses `acquires` to prevent a dangling reference: + +这个例子展示了 Move 类型系统如何通过使用 `acquires` 来防止空引用: + +```move= +address 0x42 { +module dangling { + struct T has key { f: u64 } + + fun borrow_then_remove_bad(a: address) acquires T { + let t_ref: &mut T = borrow_global_mut(a); + let t = remove_t(a); // 类型系统不允许 t_ref 这种空引用 // type system complains here + // t_ref now dangling! + let uh_oh = *&t_ref.f + } + + fun remove_t(a: address): T acquires T { + move_from(a) + } + +} +} +``` + +In this code, line 6 acquires a reference to the `T` stored at address `a` in global storage. The callee `remove_t` then removes the value, which makes `t_ref` a dangling reference. + +代码中第六行获取了 `a` 地址在全局存储中 `T` 类型资源的引用。`remove_t` 调用删除了该值,使 `t_ref` 变成空引用。 + + +Fortunately, this cannot happen because the type system will reject this program. The `acquires` annotation on `remove_t` lets the type system know that line 7 is dangerous, without having to recheck or introspect the body of `remove_t` separately! + +幸运的是,由于类型系统拒绝编译程序导致这种情况不会发生。`remove_t` 方法的 `acquires` 标注让类型系统知道第七行是危险的,不需要再分析 `remove_t` 的函数体。 + +The restriction on returning global references prevents a similar, but even more insidious problem: + +禁止返回全局引用的限制同时也防止了类似却更隐晦的问题: + +```move= +address 0x42 { +module m1 { + struct T has key {} + + public fun ret_t_ref(a: address): &T acquires T { + borrow_global(a) // 报错 类型系统在这不能继续编译 // error! type system complains here + } + + public fun remove_t(a: address) acquires T { + let T {} = move_from(a); + } +} + +module m2 { + fun borrow_then_remove_bad(a: address) { + let t_ref = m1::ret_t_ref(a); + let t = m1::remove_t(a); // t_ref 为空引用 // t_ref now dangling! + } +} +} +``` + +Line 16 acquires a reference to a global resource `m1::T`, then line 17 removes that same resource, which makes `t_ref` dangle. In this case, `acquires` annotations do not help us because the `borrow_then_remove_bad` function is outside of the `m1` module that declares `T` (recall that `acquires` annotations can only be used for resources declared in the current module). Instead, the type system avoids this problem by preventing the return of a global reference at line 6. + +第十六行获取了全局资源 `m1::T` 类型的引用,然后第十七行删除了同一资源,这使 `t_ref` 变成空引用。在这个例子中,`acquires` 标注没有帮助到我们,因为 `borrow_then_remove_bad` 函数在声明了 `T` 类型(回顾 `acquires` 标注只用在声明此类型的模块内)的 `m1` 模块外。然而禁止返回全局引用的规则使第六行避免了这个问题。 + + +Fancier type systems that would allow returning global references without sacrificing reference safety are possible, and we may consider them in future iterations of Move. We chose the current design because it strikes a good balance between expressivity, annotation burden, and type system complexity. + +允许返回全局引用而尽可能不牺牲引用安全的高级类型系统是可行的,我们将会在 Move 未来的迭代过程中考虑此事。我们选择目前的设计方式是因为它很好的平衡了语言表现力、复杂的标注和复杂的类型系统三者的关系。 diff --git a/language/documentation/book/translations/move-book-zh/src/global-storage-structure.md b/language/documentation/book/translations/move-book-zh/src/global-storage-structure.md new file mode 100644 index 0000000000..763d253d52 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/global-storage-structure.md @@ -0,0 +1,14 @@ +# 全局存储 —— 结构 + +Move 程序的目的是[读取和写入](./global-storage-operators.md)树形的持久全局存储。程序不能访问文件系统、网络或任何此树以外的数据。 + +在伪代码中,全局存储看起来像: + +```move +struct GlobalStorage { + resources: Map<(address, ResourceType), ResourceValue> + modules: Map<(address, ModuleName), ModuleBytecode> +} +``` + +从结构上讲,全局存储是一个[森林(forest)](https://en.wikipedia.org/wiki/Tree_(graph_theory)),这个森林由以账户[地址(`address`)](./address.md)为根的树组成。每个地址可以存储[资源(resource)](./structs-and-resources.md)数据和[模块(module)](./modules-and-scripts.md)代码。如上面的伪代码所示,每个地址(`address`)最多可以存储一个给定类型的资源值,最多可以存储一个给定名称的模块。 diff --git a/language/documentation/book/translations/move-book-zh/src/integers.md b/language/documentation/book/translations/move-book-zh/src/integers.md new file mode 100644 index 0000000000..70fdc4f407 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/integers.md @@ -0,0 +1,215 @@ +# 整数 (Integers) + +Move supports three unsigned integer types: `u8`, `u64`, and `u128`. Values of these types range from 0 to a maximum that depends on the size of the type. + +| Type | Value Range | +| -------------------------------- | ------------------------ | +| Unsigned 8-bit integer, `u8` | 0 to 28 - 1 | +| Unsigned 64-bit integer, `u64` | 0 to 264 - 1 | +| Unsigned 128-bit integer, `u128` | 0 to 2128 - 1 | + + +Move 支持三种无符号整数类型:`u8`、`u64` 和 `u128`。这些类型的值范围从 0 到最大值,最大值的具体取值取决于整数类型。 + +| 类型 | 取值范围 | +|---------------------------|--------------------------| +| 无符号 8位 整数, `u8` | 0 to 28 - 1 | +| 无符号 64位 整数, `u64` | 0 to 264 - 1 | +| 无符号 128位 整数, `u128` | 0 to 2128 - 1 | + + +## 字面值(Literal) + +Literal values for these types are specified either as a sequence of digits (e.g.,`112`) or as hex literals, e.g., `0xFF`. The type of the literal can optionally be added as a suffix, e.g., `112u8`. If the type is not specified, the compiler will try to infer the type from the context where the literal is used. If the type cannot be inferred, it is assumed to be `u64`. + +If a literal is too large for its specified (or inferred) size range, an error is reported. + +(在Move中)这些类型的字面值指定为数字序列(例如:112)或十六进制文字(例如:0xFF), 可以选择将字面值的类型定义为后缀, 例如 `112u8`。如果未指定类型,编译器将尝试从使用字面值的上下文推断类型。如果无法推断类型,则默认为 `u64。 + +如果字面值太大,超出其指定的(或推断的)大小范围,则会报错。 + +### 例如: + +```jsx +// literals with explicit annotations; +let explicit_u8 = 1u8; +let explicit_u64 = 2u64; +let explicit_u128 = 3u128; + +// literals with simple inference +let simple_u8: u8 = 1; +let simple_u64: u64 = 2; +let simple_u128: u128 = 3; + +// literals with more complex inference +let complex_u8 = 1; // inferred: u8 +// right hand argument to shift must be u8 +let _unused = 10 << complex_u8; + +let x: u8 = 0; +let complex_u8 = 2; // inferred: u8 +// arguments to `+` must have the same type +let _unused = x + complex_u8; + +let complex_u128 = 3; // inferred: u128 +// inferred from function argument type +function_that_takes_u128(complex_u128); + +// literals can be written in hex +let hex_u8: u8 = 0x1; +let hex_u64: u64 = 0xCAFE; +let hex_u128: u128 = 0xDEADBEEF; +``` + +## 运算集 (Operations) + +### 算术运算 (Arithmetic) + +Each of these types supports the same set of checked arithmetic operations. For all of these operations, both arguments (the left and right side operands) must be of the same type. If you need to operate over values of different types, you will need to first perform a cast. Similarly, if you expect the result of the operation to be too large for the integer type, perform a cast to a larger size before performing the operation. + +每一种(无符号整数)类型都支持相同算术运算集。对于所有这些运算,两个参数(左侧和右侧操作数)必须是同一类型。如果您需要对不同类型的值进行运算,则需要首先执行强制转换。同样,如果您预计运算结果对于当下整数类型来说太大,请在执行运算之前将之转换为更大的整数类型。 + +All arithmetic operations abort instead of behaving in a way that mathematical integers would not (e.g., overflow, underflow, divide-by-zero). + +| Syntax | Operation | Aborts If | +| ------ | ------------------- | ---------------------------------------- | +| `+` | addition | Result is too large for the integer type | +| `-` | subtraction | Result is less than zero | +| `*` | multiplication | Result is too large for the integer type | +| `%` | modular division | The divisor is `0` | +| `/` | truncating division | The divisor is `0` | + +### [Bitwise](https://move-language.github.io/move/integers.html#bitwise) + +算术运算在遇到异常时将会中止,而不是以上溢、下溢、被零除等数学整数未定义的的方式输出结果。 + +| 句法 | 操作 | 中止条件 | +| ------ | ------------| ---------------------------------------- | +| `+` | 加法 | 结果对于整数类型来说太大了 | +| `-` | 减法 | 结果小于零 | +| `*` | 乘法 | 结果对于整数类型来说太大了 | +| `%` | 取余运算 | 除数为 `0` | +| `/` | 截断除法 | 除数为 `0` | + +### 位运算 (Bitwise) + +The integer types support the following bitwise operations that treat each number as a series of individual bits, either 0 or 1, instead of as numerical integer values. + +Bitwise operations do not abort. + +| Syntax | Operation | Description +|--------|------------|------------ +| `&` | bitwise and| Performs a boolean and for each bit pairwise +| `|` | bitwise or | Performs a boolean or for each bit pairwise +| `^` | bitwise xor| Performs a boolean exclusive or for each bit pairwise + + +整数类型支持下列位运算,即将每个数字视为一系列单独的位:0 或 1,而不是整型数值。 + +位运算不会中止。 + +| 句法 | 操作 | 描述 | +| ------ | ----------- | ----------------------------------------------------- | +| `&` | 按位 和 | 对每个位成对执行布尔值和 | +| `|` | 按位或 | 对每个位成对执行布尔值或 +| `^` | 按位 异与 | 对每个位成对执行布尔异或 | + +### 位移 (Bit shift) + +Similar to the bitwise operations, each integer type supports bit shifts. But unlike the other operations, the righthand side operand (how many bits to shift by) must *always* be a `u8` and need not match the left side operand (the number you are shifting). + +Bit shifts can abort if the number of bits to shift by is greater than or equal to `8`, `64`, or `128` for `u8`, `u64`, and `u128` respectively. + +| Syntax | Operation | Aborts if | +| ------ | ----------- | ------------------------------------------------------------ | +| `<<` | shift left | Number of bits to shift by is greater than the size of the integer type | +| `>>` | shift right | Number of bits to shift by is greater than the size of the integer type | + +与按位运算类似,每种整数类型都支持位移(bit shifts)。但与其他运算不同的是,右侧操作数(要移位多少位)必须始终是 `u8`  并且不需要与左侧操作数类型(您要移位的数字)匹配。 + +如果要移位的位数分别大于或等于 `8`、`64`, `u128` 或 `128` 的 `u8`, `u64`, 则移位可以中止。 + +| 句法 | 操作 | 中止条件 | +| ------ | ----------- | ------------------------------------------------------------ | +| `<<` | 左移 | 要移位的位数大于整数类型的大小 | +| `>>` | 右移 | 要移位的位数大于整数类型的大小 | + +### 比较运算 (Comparisons) + +Integer types are the *only* types in Move that can use the comparison operators. Both arguments need to be of the same type. If you need to compare integers of different types, you will need to [cast](https://move-language.github.io/move/integers.html#casting) one of them first. + +Comparison operations do not abort. + +| Syntax | Operation | +| ------ | ------------------------ | +| `<` | less than | +| `>` | greater than | +| `<=` | less than or equal to | +| `>=` | greater than or equal to | + +整数类型是 Move 中唯一可以使用比较(Comparisons)运算符的类型。两个参数必须是同一类型。如果您需要比较不同类型的整数,则需要先转换其中一个。 + +比较操作不会中止。 + +| 句法 | 操作 | +| ------ | ------------------------ | +| `<` | 小于 | +| `>` | 大于 | +| `<=` | 小于等于 | +| `>=` | 大于等于 | + +### 相等 (Equality) + +Like all types with [`drop`](https://move-language.github.io/move/abilities.html) in Move, all integer types support the ["equal"](https://move-language.github.io/move/equality.html) and ["not equal"](https://move-language.github.io/move/equality.html) operations. Both arguments need to be of the same type. If you need to compare integers of different types, you will need to [cast](https://move-language.github.io/move/integers.html#casting) one of them first. + +Equality operations do not abort. + +| Syntax | Operation | +| ------ | --------- | +| `==` | equal | +| `!=` | not equal | + +For more details see the section on [equality](https://move-language.github.io/move/equality.html) + +与 Move 中的所有具有[`drop`](./chapter_19_abilities.html)能力的类型一样,所有整数类型都支持 ["equal(等于)"](./chapter_11_equality.html) 和 ["not equal(不等于)](./chapter_11_equality.html)运算。两个参数必须是同一类型。如果您需要比较不同类型的整数,则需要先转换其中一个。 + +相等(Equality)运算不会中止。 + +| 句法 | 操作 | +| ------ | --------- | +| `==` | 等于 | +| `!=` | 不等于 | + +更多细节可以参考[相等]([equality](https://move-language.github.io/move/equality.html))章节。 + +## 转换 (Casting) + +Integer types of one size can be cast to integer types of another size. Integers are the only types in Move that support casting. + +Casts *do not* truncate. Casting will abort if the result is too large for the specified type + +| Syntax | Operation | Aborts if | +| ---------- | ---------------------------------------------------- | -------------------------------------- | +| `(e as T)` | Cast integer expression `e` into an integer type `T` | `e` is too large to represent as a `T` | + +Here, the type of `e` must be `u8`, `u64`, or `u128` and `T` must be `u8`, `u64`, or `u128`. + +For example: + +- `(x as u8)` +- `(2u8 as u64)` +- `(1 + 3 as u128)` + +一种大小的整数类型可以转换为另一种大小的整数类型。整数是 Move 中唯一支持强制转换的类型。 + +强制转换不会截断。如果结果对于指定类型来说太大,则转换将中止。 + +| Syntax | 操作 | 中止条件 | +| ---------- | ---------------------------------------------------- | -------------------------------------- | +| `(e as T)` | 将整数表达式 `e` 转换为整数类型 `T` | `e` 太大而不能表示为 `T` | + +## 所有权 (Ownership) + +As with the other scalar values built-in to the language, integer values are implicitly copyable, meaning they can be copied without an explicit instruction such as [`copy`](https://move-language.github.io/move/variables.html#move-and-copy). + +与语言内置的其他标量值一样,整数值是隐式可复制的,这意味着它们可以在没有明确指令如[`copy`](./variables.md#move-and-copy)的情况下复制。 diff --git a/language/documentation/book/translations/move-book-zh/src/introduction.md b/language/documentation/book/translations/move-book-zh/src/introduction.md new file mode 100644 index 0000000000..5e3ea6f79f --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/introduction.md @@ -0,0 +1,47 @@ +# 引言 (Introduction) + +Welcome to Move, a next generation language for secure, sandboxed, and formally verified programming. Its first use case is for the Diem blockchain, where Move provides the foundation for its implementation. Move allows developers to write programs that flexibly manage and transfer assets, while providing the security and protections against attacks on those assets. However, Move has been developed with use cases in mind outside a blockchain context as well. + +欢迎来到Move的世界,Move是一种安全、沙盒式和形式化验证的下一代编程语言,它的第一个用例是 Diem 区块链(当时名字叫Libra, 脸书团队开发的项目, 译者注), Move 为其实现提供了基础。 Move 允许开发人员编写灵活管理和转移数字资产的程序,同时提供安全保护,防止对那些链上资产的攻击。不仅如此,Move 也可用于区块链世界之外的开发场景。 + +Move takes its cue from [Rust](https://www.rust-lang.org/) by using resource types with move (hence the name) semantics as an explicit representation of digital assets, such as currency. + +Move 的诞生从[Rust](https://www.rust-lang.org/)中吸取了灵感,Move也是因为使用具有移动(move)语义的资源类型作为数字资产(例如货币)的显式表示而得名。 + +## Move是为谁准备的?(Who is Move for?) + +Move was designed and created as a secure, verified, yet flexible programming language. The first use of Move is for the implementation of the Diem blockchain. That said, the language is still evolving. Move has the potential to be a language for other blockchains, and even non-blockchain use cases as well. + +Move语言被设计和创建为安全、可验证, 同时兼顾灵活性的编程语言。Move的第一个应用场景是用于Diem区块链的开发。现在,Move语言仍在不断发展中。Move 还有成为其他区块链,甚至非区块链用例开发语言的潜质。 + +Given custom Move modules will not be supported at the [launch](https://diem.com/white-paper/#whats-next) of the Diem Payment Network (DPN), we are targeting an early Move Developer persona. + +鉴于在 Diem 支付网络 (DPN) [启动](https://diem.com/white-paper/#whats-next)时将不支持自定义 Move 模块(custom Move modules),我们的目标是早期的 Move 开发人员。 + +The early Move Developer is one with some programming experience, who wants to begin understanding the core programming language and see examples of its usage. + +早期的 Move 开发人员应该是具有一定编程经验的程序员,他们愿意了解编程语言核心,并探索它的用法。 + +### 爱好者 (Hobbyists) + +Understanding that the capability to create custom modules on the Diem Payment Network will not be available at launch, the hobbyist Move Developer is interested in learning the intricacies of the language. She will understand the basic syntax, the standard libraries available, and write example code that can be executed using the Move CLI. The Move Developer may even want to dig into understanding how the Move Virtual Machine executes the code she writes. + +作为(Move语言)爱好者角色,首先需要明白在Diem支付网络上创建自定义模块(custom modules)是不可能的,其次,你还要对探索这门语言的复杂性保持兴趣。你将了解基本语法、可用的标准库,并编写可以用的Move CLI执行的示例代码。如果可能,你甚至可以去尝试体验Move虚拟机如何执行你自己编写的代码。 + +### 核心贡献者 (Core Contributor) + +Beyond a hobbyist wanting to stay ahead of the curve for the core programming language is someone who may want to [contribute](https://diem.com/en-US/cla-sign/) directly to Move. Whether this includes submitting language improvements or even, in the future, adding core modules available on the Diem Payment Network, the core contributor will understand Move at a deep level. + +核心贡献者指那些超越爱好者并想在核心编程语言方面保持领先,还直接为 Move 做出[贡献](https://diem.com/en-US/cla-sign/)的人。无论是提交语言改进,甚至未来添加 Diem 支付网络上可用的核心模块等,核心贡献者都将深入了解Move。 + +### Move不适用于哪些人?(Who Move is currently not targeting) + +Currently, Move is not targeting developers who wish to create custom modules and contracts for use on the Diem Payment Network. We are also not targeting novice developers who expect a completely polished developer experience even in testing the language. + +目前,Move 并不适用那些希望在在 Diem 支付网络上创建自定义模块和合约的开发人员。我们也不针对期望在测试语言时就能获得完美开发体验的初学开发者。 + +## 从哪里开始?(Where Do I Start?) + +Begin with understanding [modules and scripts](https://move-language.github.io/move/modules-and-scripts.html) and then work through the [Move Tutorial](https://move-language.github.io/move/creating-coins.html). + +你可以从了解模块和脚本([modules and scripts](./modules-and-scripts.html))开始,然后跟随Move教程([Move Tutorial](./move-tutorial.html))进行练习。 diff --git a/language/documentation/book/translations/move-book-zh/src/loops.md b/language/documentation/book/translations/move-book-zh/src/loops.md new file mode 100644 index 0000000000..34626ed284 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/loops.md @@ -0,0 +1,215 @@ +# While and Loop + +Move offers two constructs for looping: `while` and `loop`. + +Move 提供了两种循环结构: `while` and `loop`. + +## `while` 循环 + +The `while` construct repeats the body (an expression of type unit) until the condition (an expression of type `bool`) evaluates to `false`. + +Here is an example of simple `while` loop that computes the sum of the numbers from `1` to `n`: + +`while` 会重复执行结构(一个 `unit` 类型的表达式), 直到条件语句(`bool` 类型的表达式)运算结果为 `false`。 + +下面是一个简单的 `while` 循环的例子,计算从 `1` 到 `n` 数字之和: + +```move +fun sum(n: u64): u64 { + let sum = 0; + let i = 1; + while (i <= n) { + sum = sum + i; + i = i + 1 + }; + + sum +} +``` + +Infinite loops are allowed: + +无限循环是被允许的: + +```move= +fun foo() { + while (true) { } +} +``` + +### `break` + +The `break` expression can be used to exit a loop before the condition evaluates to `false`. For example, this loop uses `break` to find the smallest factor of `n` that's greater than 1: + +`break` 表达式可用于在条件计算结果为 `false` 之前退出循环。例如,这个循环使用 `break` 查找 `n` 大于1的最小因子: + +```move +fun smallest_factor(n: u64): u64 { + // assuming the input is not 0 or 1 + let i = 2; + while (i <= n) { + if (n % i == 0) break; + i = i + 1 + }; + + i +} +``` + +The `break` expression cannot be used outside of a loop. + +`break` 表达式不能在循环之外使用。 + +### `continue` + +The `continue` expression skips the rest of the loop and continues to the next iteration. This loop uses `continue` to compute the sum of `1, 2, ..., n`, except when the number is divisible by 10: + +`continue` 表达式跳过当前循环的剩余部分, 并继续下一轮迭代。下面的例子, 使用 `continue` 去计算 `1, 2, ..., n` 的总和,过滤掉不能被10整除的数: + +```move +fun sum_intermediate(n: u64): u64 { + let sum = 0; + let i = 0; + while (i < n) { + i = i + 1; + if (i % 10 == 0) continue; + sum = sum + i; + }; + + sum +} +``` + +The `continue` expression cannot be used outside of a loop. + +`continue` 表达式不能在循环之外使用。 + +### The type of `break` and `continue` + +`break` and `continue`, much like `return` and `abort`, can have any type. The following examples illustrate where this flexible typing can be helpful: + +`break` and `continue`, 和 `return` and `abort` 很相像, 可以是任何类型。下面的例子说明了这种灵活的类型在那些方面有帮助: + +```move +fun pop_smallest_while_not_equal( + v1: vector, + v2: vector, +): vector { + let result = vector::empty(); + while (!vector::is_empty(&v1) && !vector::is_empty(&v2)) { + let u1 = *vector::borrow(&v1, vector::length(&v1) - 1); + let u2 = *vector::borrow(&v2, vector::length(&v2) - 1); + let popped = + if (u1 < u2) vector::pop_back(&mut v1) + else if (u2 < u1) vector::pop_back(&mut v2) + else break; // Here, `break` has type `u64` + vector::push_back(&mut result, popped); + }; + + result +} + +fun pick( + indexes: vector, + v1: &vector
, + v2: &vector
+): vector
{ + let len1 = vector::length(v1); + let len2 = vector::length(v2); + let result = vector::empty(); + while (!vector::is_empty(&indexes)) { + let index = vector::pop_back(&mut indexes); + let chosen_vector = + if (index < len1) v1 + else if (index < len2) v2 + else continue; // Here, `continue` has type `&vector
` + vector::push_back(&mut result, *vector::borrow(chosen_vector, index)) + }; + + result +} +``` + +## `loop`表达式 + +The `loop` expression repeats the loop body (an expression with type `()`) until it hits a `break` + +Without a `break`, the loop will continue forever + +`loop` 表达式重复循环体(类型为unit()的表达式) ,直到遇到 `break` 为止。 + +(下面的代码中)没有 `break`, 循环将一直执行。 + +```move +fun foo() { + let i = 0; + loop { i = i + 1 } +} +``` + +Here is an example that uses `loop` to write the `sum` function: + +这是一个使用 `loop` 编写 `sum` 函数的示例(可与 `while` 循环比较): + +```move +fun sum(n: u64): u64 { + let sum = 0; + let i = 0; + loop { + i = i + 1; + if (i > n) break; + sum = sum + i + }; + + sum +} +``` + +As you might expect, `continue` can also be used inside a `loop`. Here is `sum_intermediate` from above rewritten using `loop` instead of `while` + +正如你所料, `continue` 也可以在 `loop` 中使用。这是上面的 `sum_intermediate` 使用 `loop` 代替 `while` 重写的: + +```move +fun sum_intermediate(n: u64): u64 { + let sum = 0; + let i = 0; + loop { + i = i + 1; + if (i % 10 == 0) continue; + if (i > n) break; + sum = sum + i + }; + + sum +} +``` + +## `while` and `loop` 的类型 + +Move loops are typed expressions. A `while` expression always has type `()`. + +Move 循环是有类型化的表达式。 `while` 表达式始终具有 `()` 类型。 + +```move +let () = while (i < 10) { i = i + 1 }; +``` + +If a `loop` contains a `break`, the expression has type unit `()` + +如果 `loop` 中包含 `break` , 这个表达式的类型则为 unit `()` + + +```move +(loop { if (i < 10) i = i + 1 else break }: ()); +let () = loop { if (i < 10) i = i + 1 else break }; +``` + +If `loop` does not have a `break`, `loop` can have any type much like `return`, `abort`, `break`, and `continue`. + +如果 `loop` 不包含 `break`, `loop` 可以是任何类型, 就像`return`, `abort`, `break`, 和 `continue`。 + +```move +(loop (): u64); +(loop (): address); +(loop (): &vector>); +``` diff --git a/language/documentation/book/translations/move-book-zh/src/modules-and-scripts.md b/language/documentation/book/translations/move-book-zh/src/modules-and-scripts.md new file mode 100644 index 0000000000..9dbca68ebf --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/modules-and-scripts.md @@ -0,0 +1,119 @@ +# 模块和脚本 + +Move 有两种不同类型的程序:***模块(Module)***和***脚本(Script)***。模块是定义结构类型以及对这些类型进行操作的函数的库。*结构类型*定义了 Move 的[全局存储](./global-storage-structure.md)的模式,*模块函数*定义了更新存储的规则。模块本身也存储在全局存储中。脚本是[可执行文件](https://en.wikipedia.org/wiki/Executable)的入口点,类似于传统语言中的主函数 `main`。脚本通常调用已发布模块的函数来更新全局存储。脚本是临时代码片段,不会发布在全局存储中。 + +一个 Move 源文件(或**编译单元**)可能包含多个模块和脚本。然而,发布模块或执行脚本都是独立的虚拟机(VM)操作。 + +## 语法 + +### 脚本 + +脚本具有以下结构: + +```text +script { + * + * + fun <[type parameters: constraint]*>([identifier: type]*) +} +``` + +一个 `script` 块必须以它的所有 [`use`](./uses.md) 声明开头,然后是[常量(constant)](./constants.md)声明,最后是主[函数](./functions.md)声明。主函数的名称可以是任意的(也就是说,它不一定命名为 `main`),它是脚本块中唯一的函数,可以有任意数量的参数,并且不能有返回值。下面是每个组件的示例: + +```move +script { + // 导入在命名账户地址 std 上发布的 debug 模块。 + use std::debug; + + const ONE: u64 = 1; + + fun main(x: u64) { + let sum = x + ONE; + debug::print(&sum) + } +} +``` + +脚本(Script)的功能非常有限 —— 它们不能声明友元(friend)、结构类型或访问全局存储。他们的主要作用主要是调用*模块函数*。 + +### 模块 + +模块具有以下结构: + +```text +module
:: { + ( | | | | )* +} +``` + +其中 `
` 是一个有效的[命名或字面量地址](./address.md)。 + +例子: + +```move +module 0x42::test { + struct Example has copy, drop { i: u64 } + + use std::debug; + friend 0x42::another_test; + + const ONE: u64 = 1; + + public fun print(x: u64) { + let sum = x + ONE; + let example = Example { i: sum }; + debug::print(&sum) + } +} +``` + +`module 0x42::test` 这部分指定模块 `test` 将在[全局存储](./global-storage-structure.md)的[账户地址](./address.md) `0x42` 下发布。 + +模块也可以使用[命名地址](./address.md)来声明,例如: + +```move +module test_addr::test { + struct Example has copy, drop { a: address } + + use std::debug; + friend test_addr::another_test; + + public fun print() { + let example = Example { a: @test_addr }; + debug::print(&example) + } +} +``` + +因为命名地址只存在于源语言级别和编译期间,所以命名地址将在字节码级别彻底替换它们的值。例如,如果我们有以下代码: + +```move +script { + fun example() { + my_addr::m::foo(@my_addr); + } +} +``` + +我们在把 `my_addr` 设置为 `0xC0FFEE` 的情况下编译它,那么它在操作上等同于以下内容: + +```move +script { + fun example() { + 0xC0FFEE::m::foo(@0xC0FFEE); + } +} +``` + +然而,在源代码级别,这些是*不等价的* —— 函数 `m::foo` *必须*通过 `my_addr` 命名地址来访问,而不是通过分配给该地址的数值来访问。 + +模块名称可以以字母 `a` 到 `z` 或字母 `A` 到 `Z` 开头。在第一个字符之后,模块名可以包含下划线 `_`、字母 `a` 到 `z`、字母 `A` 到 `Z` 或数字 `0` 到 `9`。 + +```move +module my_module {} +module foo_bar_42 {} +``` + +通常,模块名称以小写字母开头。名为 `my_module` 的模块应该存储在名为 `my_module.move` 的源文件中。 + +`module` 块内的所有元素都可以按任意顺序出现。从根本上说,模块是[`类型(type)`](./structs-and-resources.md)和[`函数(function)`](./functions.md)的集合。[`use`](./uses.md) 关键字用来从其他模块导入类型。[`friend`](./friends.md) 关键字指定一个可信的模块列表。[`const`](./constants.md) 关键字定义了可以在模块函数中使用的私有常量。 diff --git a/language/documentation/book/translations/move-book-zh/src/move-tutorial.md b/language/documentation/book/translations/move-book-zh/src/move-tutorial.md new file mode 100644 index 0000000000..209d54950a --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/move-tutorial.md @@ -0,0 +1,970 @@ +# Move 教程(Move Tutorial) + +Welcome to the Move Tutorial! In this tutorial, we are going to go through some steps of developing Move code including design, implementation, unit testing and formal verification of Move modules. + +欢迎来到 Move 语言教程,在本教程中,我们通过一些具体的步骤进行 Move 语言代码的开发,包括 Move 模块的设计、实现、单元测试和形式化验证。 + +There are nine steps in total: + +- [Step 0: Installation](#Step0) +- [Step 1: Writing my first Move module](#Step1) +- [Step 2: Adding unit tests to my first Move module](#Step2) +- [Step 3: Designing my `BasicCoin` module](#Step3) +- [Step 4: Implementing my `BasicCoin` module](#Step4) +- [Step 5: Adding and using unit tests with the `BasicCoin` module](#Step5) +- [Step 6: Making my `BasicCoin` module generic](#Step6) +- [Step 7: Use the Move prover](#Step7) +- [Step 8: Writing formal specifications for the `BasicCoin` module](#Step8) + +整个过程共包含9个步骤: + +- [Step 0: 安装 Move 开发环境](#Step0) +- [Step 1: 编写第一个 Move 模块(Move Module)](#Step1) +- [Step 2: 给模块(Module)添加单元测试](#Step2) +- [Step 3: 设计自己的 `BasicCoin` 模块(Module)](#Step3) +- [Step 4: `BasicCoin` 模块(Module)的实现](#Step4) +- [Step 5: 给 `BasicCoin` 模块添加单元测试](#Step5) +- [Step 6: 使用泛型(generic)编写 `BasicCoin` 模块](#Step6) +- [Step 7: 使用 `Move prover`](#Step7) +- [Step 8: 为 `BasicCoin` 模块编写形式化规范(formal specifications)](#Step8) + +Each step is designed to be self-contained in the corresponding `step_x` folder. For example, if you would +like to skip the contents in step 1 through 4, feel free to jump to step 5 since all the code we have written +before step 5 will be in `step_5` folder. At the end of some steps, we also include +additional material on more advanced topics. + +其中每一步都被设计为自包含的文件夹, 相应名字为 `step_x`。 例如,如果您愿意跳过 `step 1` 到 `step 4` 的内容,可直接跳到 `step 5`,因为所有在 `step 5` 之前的代码均在在`step_5` 文件夹之下。在部分步骤结束时,我们还引入有关更高级主题的附加资料。 + +Now let's get started! + +好了,我们现在开始! + +## Step 0: 安装 Move 开发环境 (Step 0: Installation) + +If you haven't already, open your terminal and clone [the Move repository](https://github.com/move-language/move): + +如果您还没有安装过 Move,首先打开命令终端(terminal) 并clone [Move代码库](https://github.com/move-language/move): + +```bash +git clone https://github.com/move-language/move.git +``` + +Go to the `move` directory and run the `dev_setup.sh` script: + +进入到 `move` 文件夹下,执行 `dev_setup.sh` 脚本: + +```bash +cd move +./scripts/dev_setup.sh -ypt +``` + +Follow the script's prompts in order to install all of Move's dependencies. + +The script adds environment variable definitions to your `~/.profile` file. +Include them by running this command: + +根据脚本命令的提示,按顺序安装 Move 的所有依赖项。 +脚本将会将(move命令所在路径)环境变量写入到 `~/.profile` 文件中。 + +执行如下命令使环境变量生效: + +```bash +source ~/.profile +```` + +Next, install Move's command-line tool by running this commands: + +然后执行如下命令来安装 Move 命令行工具: + +```bash +cargo install --path language/tools/move-cli +``` + +You can check that it is working by running the following command: + +通过如下运行命令可以检查 move 命令是否可正常: + +```bash +move --help +``` +You should see something like this along with a list and description of a number of commands: + +您应该会看到类似这样的内容以及许多命令的列表和描述: + +``` +move-package +Execute a package command. Executed in the current directory or the closest containing Move package + +USAGE: + move [OPTIONS] + +OPTIONS: + --abi Generate ABIs for packages +... +``` + +If you want to find what commands are available and what they do, running +a command or subcommand with the `--help` flag will print documentation. + +如果想了解有支持哪引命令及其作用, 执行命令或子命令时添加 `--help` 标记,此时会打印帮助文档。 + +Before running the next steps, `cd` to the tutorial directory: + +在执行下一步骤之前,请先执行 `cd` 命令进入到教程对应目录下: + +```bash +cd /language/documentation/tutorial +``` + +
+ +Visual Studio Code Move 支持 (Visual Studio Code Move Support) + +There is official Move support for Visual Studio Code. You need to install +the move analyzer first: + +Visual Studio Code 有正式的 Move 语言支持, 您需要先安装 `move analyzer` : + +```bash +cargo install --path language/move-analyzer +``` + +Now you can install the VS extension by opening VS Code, searching for the "move-analyzer" in the Extension Pane, and installing it. More detailed instructions can be found +in the extension's [README](https://github.com/move-language/move/tree/main/language/move-analyzer/editors/code). + +现在您可以打开 VS Code 并安装 Move 扩展插件了,在扩展页面下找到 `move-analyzer` 并安装即可。关于扩展的详细信息可以查看扩展的[README](https://github.com/move-language/move/tree/main/language/move-analyzer/editors/code)。 +
+ +## Step 1: 编写第一个Move模块 (Writing my first Move module) + +Change directory into the [`step_1/BasicCoin`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_1/BasicCoin) directory. +You should see a directory called `sources` -- this is the place where all +the Move code for this package lives. You should also see a +`Move.toml` file as well. This file specifies dependencies and other information about +the package; if you're familiar with Rust and Cargo, the `Move.toml` file +is similar to the `Cargo.toml` file, and the `sources` directory similar to +the `src` directory. + +切换当前目录到[`step_1/BasicCoin`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_1/BasicCoin)下,您将看到 `sources` 子目录 -- 这个包(package)下所有的 Move 代码都在此目录中,同时您还会看到一个 `Move.toml` 文件。该文件指定当前包的依赖列表和其他信息。 +如果您熟悉 `Rust` 和 `Cargo`,那 `Move.toml` 文件类似 `Cargo.toml` 文件, `sources` 目录类似 `src` 目录(它们的作用是一样的) + +Let's take a look at some Move code! Open up +[`sources/FirstModule.move`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_1/BasicCoin/sources/FirstModule.move) in +your editor of choice. The first thing you'll see is this: + +来一起看看 Move 语言代码内容! 用你的编辑器打开[`sources/FirstModule.move`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_1/BasicCoin/sources/FirstModule.move)文件,会看到如下内容: + +``` +// sources/FirstModule.move +module 0xCAFE::BasicCoin { + ... +} +``` + +This is defining a Move +[module](https://move-language.github.io/move/modules-and-scripts.html). Modules are the +building block of Move code, and are defined with a specific address -- the address that the module can be published under. +In this case, the `BasicCoin` module can only be published under `0xCAFE`. + +这是一个 `Move` [module(模块)](./chpater_1_modules-and-scripts.html)的定义。 +模块是 Move 语言的代码块, 并且它使用指定的地址(address)进行定义 -- 模块只能在该地址下发布。 +当前 `BasicCoin` 模块只能被发布在 `0xCAFE` 地址下。 + +Let's now take a look at the next part of this file where we define a [struct](https://move-language.github.io/move/structs-and-resources.html) to represent a `Coin` with a given `value`: + +再看这个文件的下一部分,这里定义了一个具有字段 `value` 的[结构体](./structs-and-resources.html) `Coin`: + +``` +module 0xCAFE::BasicCoin { + struct Coin has key { + value: u64, + } + ... +} +``` + +Looking at the rest of the file, we see a function definition that creates a `Coin` struct and stores it under an account: + +再看文件剩余部分,我们会看到一个函数,它会创建一个 `Coin` 结构体,并将其保存在某个账号(account)下: + +``` +module 0xCAFE::BasicCoin { + struct Coin has key { + value: u64, + } + + public fun mint(account: signer, value: u64) { + move_to(&account, Coin { value }) + } +} +``` + +Let's take a look at this function and what it's saying: +* It takes a [`signer`](https://move-language.github.io/move/signer.html) -- an + unforgeable token that represents control over a particular address, and + a `value` to mint. +* It creates a `Coin` with the given value and stores it under the + `account` using the `move_to` operator. + +Let's make sure it builds! This can be done with the `build` command from within the package folder ([`step_1/BasicCoin`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_1/BasicCoin/)): + +让我们来看看这个函数和它的含义: +* 此函数需要一个[`signer`](./signer.html)参数 -- 表示不可伪造的 token 受此特定地址的控制; 和一个需要铸造的数量参数 `value`。 +* 此函数使用给定的参数值铸造一个 `Coin`,然后通过 `move_to` 操作将其保存在(全局存储中)给定的 `account` 账户下。 + +我们需要确保它真的执行,这可以通过在包文件夹([`step_1/BasicCoin`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_1/BasicCoin/))下的运行 `build` 命令来完成: + +```bash +move build +``` + +
+ +进阶概念及参考引用 (Advanced concepts and references) + +* You can create an empty Move package by calling: + ```bash + move new + ``` +* Move code can also live a number of other places. More information on the + Move package system can be found in the [Move book](https://move-language.github.io/move/packages.html) +* More information on the `Move.toml` file can be found in the [package section of the Move book](https://move-language.github.io/move/packages.html#movetoml). +* Move also supports the idea of [named addresses](https://move-language.github.io/move/address.html#named-addresses), Named addresses are a way to parametrize Move source code so that you can compile the module using different values for `NamedAddr` to get different bytecode that you can deploy, depending on what address(es) you control. They are used quite frequently, and can be defined in the `Move.toml` file in the `[addresses]` section, e.g., + ``` + [addresses] + SomeNamedAddress = "0xC0FFEE" + ``` + +* 你可以通过以下命令创建一个空的 Move 包(move package): + ```bash + move new + ``` +* Move 代码也可以放在其他很多地方, 更多关于 Move 包系统的信息请参阅[Move book](./packages.html) +* 更多关于 `Move.toml` 文件的信息可以参阅[package section of the Move book](./packages.html#movetoml). +* Move语言也支持命名地址的概念([named addresses](./address.html#named-addresses)), 命名地址是一种参数化 Move 源代码的方法, + 就是如果对 `NamedAddr` 使用的不同赋值编译,编译后会获得部署到你控制地址的不同字节码. 这种用法很常见,一般都将地址变量其定义在 `Move.toml` 文件 + 的 `[addresses]` 部分. 例如: + ``` + [addresses] + SomeNamedAddress = "0xC0FFEE" + ``` + +* [Structures](https://move-language.github.io/move/structs-and-resources.html) in Move can be given different + [abilities](https://move-language.github.io/move/abilities.html) that describe what can be done with that type. There are four different abilities: + - `copy`: Allows values of types with this ability to be copied. + - `drop`: Allows values of types with this ability to be popped/dropped. + - `store`: Allows values of types with this ability to exist inside a struct in global storage. + - `key`: Allows the type to serve as a key for global storage operations. + + So in the `BasicCoin` module we are saying that the `Coin` struct can be used as a key + in global storage and, because it has no other abilities, it cannot be + copied, dropped, or stored as a non-key value in storage. So you can't copy + coins, and you also can't lose coins by accident! +* [Functions](https://move-language.github.io/move/functions.html) are default + private, and can also be `public`, + [`public(friend)`](https://move-language.github.io/move/friends.html), or + `public(script)`. The last of these states that this function can be + called from a transaction script. `public(script)` functions can also be + called by other `public(script)` functions. +* `move_to` is one of the [five different global storage operators](https://move-language.github.io/move/global-storage-operators.html). + +* Move [结构体](./chpater_16_structs-and-resources.html)可以通过给类型设定不同的能力[abilities](./chapter_19_abilities.html)让类型下支持对应的行为. 有四种能力: + - `copy`: 允许此类型的值被复制 + - `drop`: 允许此类型的值被弹出/丢弃 + - `store`: 允许此类型的值存在于全局存储的某个结构体中 + - `key`: 允许此类型作为全局存储中的键(具有 `key` 能力的类型才能保存到全局存储中) + + 所以 `BasicCoin` 模块下的 `Coin` 结构体可以用作全局存储(global storage)的键(key), 因为它又不具备其他能力,它不能 + 被拷贝,不能被丢弃, 也不能作为非key来保存在(全局)存储里. 你无法复制 `Coin`,也不会意外弄丢它. +* 函数[Functions](./functions.html)默认是私有的(private), 也可以声明为 `public` [`public(friend)`](https://move-language.github.io/move/friends.html), `public(script)`. 最后一个声明(指 `public(script)`)的函数可以被事务脚本调用。`public(script)` 函数也可以被其他 `public(script)` 函数调用。(注意:在最新版本的 Move中,`public(script)` 已经被废弃,被`public entry` 取代,下同,译者注) +* `move_to` 是[五种不同的全局存储操作](./global-storage-operators.html)之一 + +
+ +## Step 2: 给模块(Module)添加单元测试 (Adding unit tests to my first Move module) + +Now that we've taken a look at our first Move module, we'll take a look at a test to make sure minting works the way we expect it to by changing directory to [`step_2/BasicCoin`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_2/BasicCoin). Unit tests in Move are similar to unit tests in Rust if you're familiar with them -- tests are annotated with `#[test]` and written like normal Move functions. + +You can run the tests with the `move test` command: (原文是 `package test`,应该有误) + +现在我们已经完成了我们的第一个 Move 模块,我们将切换到目录[`step_2/BasicCoin`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_2/BasicCoin)下并完成一个测试,确保铸币按我们预期的方式工作。 +如果你熟悉它们(Move 和 Rust)的话,Move 中的单元测试类似于 Rust 中的单元测试 —— 测试代码使用 `#[test]` 注解,并像编写普通的 Move 函数一样。 + +可以通过 `move test` 命令来执行测试: + +```bash +move test +``` + +Let's now take a look at the contents of the [`FirstModule.move`file](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_2/BasicCoin/sources/FirstModule.move). The first new thing you'll +see is this test: + +现在我们来完成文件[`FirstModule.move`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_2/BasicCoin/sources/FirstModule.move)的具体内容,你将看到的第一个新事项是这个测试: + +``` +module 0xCAFE::BasicCoin { + ... + // Declare a unit test. It takes a signer called `account` with an + // address value of `0xC0FFEE`. + #[test(account = @0xC0FFEE)] + fun test_mint_10(account: signer) acquires Coin { + let addr = signer::address_of(&account); + mint(account, 10); + // Make sure there is a `Coin` resource under `addr` with a value of `10`. + // We can access this resource and its value since we are in the + // same module that defined the `Coin` resource. + assert!(borrow_global(addr).value == 10, 0); + } +} +``` + +This is declaring a unit test called `test_mint_10` that mints a `Coin` struct under the `account` with a `value` of `10`. It is then checking that the minted +coin in storage has the value that is expected with the `assert!` call. If the assertion fails the unit test will fail. + +这里声明了一个命名为 `test_mint_10` 的单元测试,它在 `account` 账户地址下铸造了一个包含 `value` 为 `10`的 `Coin`,然后通过 `assert!` 断言检查已经铸造成功并保存在(全局)存储中的 `Coin` 的值是否与期望值一致。如果断言 `assert` 执行失败,则单元测试失败。 + +
+ +进阶概念及参考练习 (Advanced concepts and exercises) + +* There are a number of test-related annotations that are worth exploring, they can be found + [here](https://github.com/move-language/move/blob/main/language/changes/4-unit-testing.md#testing-annotations-their-meaning-and-usage). + You'll see some of these used in Step 5. +* Before running unit tests, you'll always need to add a dependency on the Move + standard library. This can be done by adding an entry to the `[dependencies]` + section of the `Move.toml`, e.g., + + ```toml + [dependencies] + MoveStdlib = { local = "../../../../move-stdlib/", addr_subst = { "Std" = "0x1" } } + ``` + + Note that you may need to alter the path to point to the `move-stdlib` directory under + `/language`. You can also specify git dependencies. You can read more on Move + package dependencies [here](https://move-language.github.io/move/packages.html#movetoml). + +* 很多测试相关的注解(annotations)都值得仔细探索, 参阅[用法](https://github.com/move-language/move/blob/main/language/changes/4-unit-testing.md#testing-annotations-their-meaning-and-usage)。 在 `Step 5` 中会看到更多用法. + +* 执行测试之前,需要设定Move标准库依赖关系,找到 `Move.toml` 并在 `[dependencies]` 段内进行设定, 例如 + + ```toml + [dependencies] + MoveStdlib = { local = "../../../../move-stdlib/", addr_subst = { "Std" = "0x1" } } + ``` +注意, 需要修改 `/language` 中的内容来匹配实际 `move-stdlib` 所在的目录路径. 也可以用 `git` 方式指定依赖, 关于 Move 包依赖(package denpendices)信息可参阅[package文档](./packages.html#movetoml) + +#### 练习 (Exercises) + +* Change the assertion to `11` so that the test fails. Find a flag that you can pass to the `move test` command that will show you the global state when the test fails. It should look something like this: + +* 将断言值改为 `11` 将导致断言执行失败, 找一个可以传递给 `move test` 命令的标志,当测试失败时它会显示全局状态。看起来像这样: + ``` + ┌── test_mint_10 ────── + │ error[E11001]: test failure + │ ┌─ ./sources/FirstModule.move:24:9 + │ │ + │ 18 │ fun test_mint_10(account: signer) acquires Coin { + │ │ ------------ In this function in 0xcafe::BasicCoin + │ · + │ 24 │ assert!(borrow_global(addr).value == 11, 0); + │ │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Test was not expected to abort but it aborted with 0 here + │ + │ + │ ────── Storage state at point of failure ────── + │ 0xc0ffee: + │ => key 0xcafe::BasicCoin::Coin { + │ value: 10 + │ } + │ + └────────────────── + ``` + +* Find a flag that allows you to gather test coverage information, and then play around with using the `move coverage` command to look at coverage statistics and source coverage. + +* 找一个允许您收集测试覆盖率信息的标志,然后使用 `move coverage` 命令查看覆盖率统计信息和源码覆盖率。 + +
+ +## Step 3: 设计 `BasicCoin` 模块(Module) (Designing my `BasicCoin` module) + +In this section, we are going to design a module implementing a basic coin and balance interface, where coins can be minted and transferred between balances held under different addresses. + +在本节中,我们将设计一个具有基本代币和余额(balance)接口功能的模块,通过他们来实现币的挖矿铸造,不同地址之下钱包的转账。 + +The signatures of the public Move function are the following: + +Move 语言的 `public function` 签名如下: + +``` +/// Publish an empty balance resource under `account`'s address. This function must be called before +/// minting or transferring to the account. +public fun publish_balance(account: &signer) { ... } + +/// Mint `amount` tokens to `mint_addr`. Mint must be approved by the module owner. +public fun mint(module_owner: &signer, mint_addr: address, amount: u64) acquires Balance { ... } + +/// Returns the balance of `owner`. +public fun balance_of(owner: address): u64 acquires Balance { ... } + +/// Transfers `amount` of tokens from `from` to `to`. +public fun transfer(from: &signer, to: address, amount: u64) acquires Balance { ... } +``` + +Next we look at the data structs we need for this module. + +接下来再看本模块所需要各数据结构. + +A Move module doesn't have its own storage. Instead, Move "global storage" (what we call our +blockchain state) is indexed by addresses. Under each address there are Move modules (code) and Move resources (values). + +Move 语言的模块没有自己的数据存储,相反的是 Move 语言提供按地址(addresses) 索引的 **全局存储** (也是就是我们所说的区块链状态(blockchain state)). +每个地址之下包含有 Move 模块(代码)和 Move 资源 (数据)。 + +The global storage looks roughly like this in Rust syntax: + +在 Rust 语法中,全局存储看起来有点像这样: + +```rust +struct GlobalStorage { + resources: Map> + modules: Map> +} +``` + +The Move resource storage under each address is a map from types to values. (An observant reader might observe that this means each address can only have one value of each type.) This conveniently provides us a native mapping indexed by addresses. +In our `BasicCoin` module, we define the following `Balance` resource representing the number of coins each address holds: + +每个地址下的 Move 资源存储是一个类型到数值的映射。(细心的读者也许已经注意到每个地址, 每个类型下只能对应一个具体值)。这方便地为我们提供了一个按地址索引的本地映射。 +在 `BasicCoin` 模块中,定义了每个 `Balance` (钱包,余额)资源表示每个地址下持有的币的数量: + +``` +/// Struct representing the balance of each address. +struct Balance has key { + coin: Coin // same Coin from Step 1 +} +``` + +Roughly the Move blockchain state should look like this: + +区块链状态(`Move blockchain state`)看起来大致如下: + +![](https://raw.githubusercontent.com/move-language/move/main/language/documentation/tutorial/diagrams/move_state.png) + +#### 进阶主题 (Advanced topics) : + +public(script) functions + +Only functions with `public(script)` visibility can be invoked directly in transactions. So if you would like to call the `transfer` method directly from a transaction, you'll want to change its signature to: + +只有`public(script)`可见行的函数才能直接被交易调用,所以如果你要直接在交易内调用`transfer`方法,那么需要将函数签改成如下格式: + +``` +public(script) fun transfer(from: signer, to: address, amount: u64) acquires Balance { ... } +``` +Read more on Move function visibilities [here](https://move-language.github.io/move/functions.html#visibility). + +关于函数可见性的更多信息,请参阅[Move function visibilities](./functions.html#visibility)。 + + +
+与 Ethereum/Solidity 的比较 (Comparison with Ethereum/Solidity) + +In most Ethereum [ERC-20]((https://ethereum.org/en/developers/docs/standards/tokens/erc-20/)) contracts, the balance of each address is stored in a _state variable_ of type mapping(address => uint256). This state variable is stored in the storage of a particular smart contract. + +在大多数以太坊[ERC-20]((https://ethereum.org/en/developers/docs/standards/tokens/erc-20/))智能合约中,各个账户地址下的余额保存在类型为 mapping(address => uint256)的 __状态变量__ 中,此状态变量存储在具体的智能合约内部存储中。 + +The Ethereum blockchain state might look like this: + +以太坊区块链的状态看起来大致如下: + +![](https://raw.githubusercontent.com/move-language/move/main/language/documentation/tutorial/diagrams/solidity_state.png) +
+ +## Step 4: 实现 `BasicCoin` 模块span id="Step4"> (Implementing my `BasicCoin` module) + +We have created a Move package for you in folder `step_4` called `BasicCoin`. The `sources` folder contains source code for all your Move modules in the package, including `BasicCoin.move`. In this section, we will take a closer look at the implementation of the methods inside [`BasicCoin.move`](https://github.com/move-language/move/blob/main/language/documentation/tutorial/step_4/BasicCoin/sources/BasicCoin.move). + +我们已经在 `step_4` 文件夹上创建了名叫 `BasicCoin` 的 Move 包。`sources` 文件夹包含所有的 Move 包(package)的模块源码,包括 `BasicCoin.move`。 在本节中,我们将仔细研究[`BasicCoin.move`](https://github.com/move-language/move/blob/main/language/documentation/tutorial/step_4/BasicCoin/sources/BasicCoin.move)内部方法的实现。 + +### 编译代码 (Compiling our code) + +Let's first try building the code using Move package by running the following command in [`step_4/BasicCoin`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_4/BasicCoin) folder: + +首先尝试在文件夹[`step_4/BasicCoin`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_4/BasicCoin)中运行以下命令,使用 Move 包构建代码: + +```bash +move build +``` + +### 方法的实现 (Implementation of methods) + +Now let's take a closer look at the implementation of the methods inside [`BasicCoin.move`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_4/BasicCoin/sources/BasicCoin.move). + +现在仔细看看[`BasicCoin.move`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_4/BasicCoin/sources/BasicCoin.move)中内部方法的实现。 + +
+ +publish_balance方法 (Method publish_balance) + +This method publishes a `Balance` resource to a given address. Since this resource is needed to receive coins through minting or transferring, `publish_balance` method must be called by a user before they can receive money, including the module owner. + +此方法将 `Balance` 资源发布到指定地址名下。由于此资源需要通过铸造或转账来接收代币,必须由用户先调用方法 `publish_balance` 才能接收钱,包括模块所有者。 + +This method uses a `move_to` operation to publish the resource: + +此方法使用 `move_to` 操作来发布资源: + +``` +let empty_coin = Coin { value: 0 }; +move_to(account, Balance { coin: empty_coin }); +``` + +
+
+ +mint方法 (Method mint)) + +Here we require that `mint` must be approved by the module owner. We enforce this using the assert statement: +`mint` method mints coins to a given account. + +`mint` 方法将代币铸造到指定的帐户。在此我们要求 `mint` 必须得到模块所有者的批准。我们使用 `assert` 语句强制执行此操作: + +``` +assert!(signer::address_of(&module_owner) == MODULE_OWNER, errors::requires_address(ENOT_MODULE_OWNER)); +``` + +Assert statements in Move can be used in this way: `assert!(, );`. This means that if the `` is false, then abort the transaction with ``. Here `MODULE_OWNER` and `ENOT_MODULE_OWNER` are both constants defined at the beginning of the module. And `errors` module defines common error categories we can use. +It is important to note that Move is transactional in its execution -- so if an [abort](https://move-language.github.io/move/abort-and-assert.html) is raised no unwinding of state needs to be performed, as no changes from that transaction will be persisted to the blockchain. + +Move 中的 `assert` 语句可以这样使用:`assert!(, );`。这意味着如果 `` 为假,则使用中止错误码 `` 来终止交易。此处的 `MODULE_OWNER` 和 `ENOT_MODULE_OWNER` 都是在模块开头定义的常量。`errors` 模块定义了我们可以使用的常见错误种类。重点是我们需要注意 Move 在其执行过程中是事务性的-- 因此,如果触发[中止(abort)](./chapter_12_abort-and-assert.html),并不用回退已执行状态的,因为该事务的任何更改都不会持久保存到区块链。 + +We then deposit a coin with value `amount` to the balance of `mint_addr`. + +然后将数量为 `amount` 的代币存入 `mint_addr` 的余额中。 + +``` +deposit(mint_addr, Coin { value: amount }); +``` +
+ +
+ +balance_of方法 (Method balance_of) + +We use `borrow_global`, one of the global storage operators, to read from the global storage. + +我们使用全局存储操作之一的 `borrow_global` 从全局存储中读取资源(数据)。 + +``` +borrow_global(owner).coin.value + | | \ / + resource type address field names +``` +
+ +
+ +transfer方法 (Method transfer) + +This function withdraws tokens from `from`'s balance and deposits the tokens into `to`s balance. We take a closer look at `withdraw` helper function: + +该函数从 `from` 的余额中提取代币并将代币存入 `to` 的余额中。我们仔细研究帮助函数 `withdraw`: + +``` +fun withdraw(addr: address, amount: u64) : Coin acquires Balance { + let balance = balance_of(addr); + assert!(balance >= amount, EINSUFFICIENT_BALANCE); + let balance_ref = &mut borrow_global_mut(addr).coin.value; + *balance_ref = balance - amount; + Coin { value: amount } +} +``` + +At the beginning of the method, we assert that the withdrawing account has enough balance. We then use `borrow_global_mut` to get a mutable reference to the global storage, and `&mut` is used to create a [mutable reference](https://move-language.github.io/move/references.html) to a field of a struct. We then modify the balance through this mutable reference and return a new coin with the withdrawn amount. + +在方法开始,我们断言提款账户有足够的余额。然后我们使用 `borrow_global_mut` 来获得全局存储的可变引用,并用 `&mut` 创建结构体字段的[可变引用](./references.html)。然后我们通过这个可变引用修改余额并返回一个带有提取金额的新代币。 + +
+ +### 练习 (Exercises) + +There are two `TODO`s in our module, left as exercises for the reader: +- Finish implementing the `publish_balance` method. +- Implement the `deposit` method. + +在模块中有两个 TODOs,留给读者练习: +- 完成 `publish_balance` 方法的实现。 +- 实现 `deposit` 方法。 + +The solution to this exercise can be found in [`step_4_sol`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_4_sol) folder. + +此练习的解决方案可以在[`step_4_sol`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_4_sol)文件夹中找到。 + +**额外练习** (**Bonus exercise**) + +- What would happen if we deposit too many tokens to a balance? +- 如果我们在余额中存入太多会发生什么? + + +## Step 5: 在模块 `BasicCoin` 中添加和使用单元测试 (Adding and using unit tests with the `BasicCoin` module) + +In this step we're going to take a look at all the different unit tests we've written to cover the code we wrote in step 4. We're also going to take a look at some tools we can use to help us write tests. + +在这一步中,来看看我们为覆盖在 `step 4` 中编写的代码而编写的所有不同的单元测试。还将看看我们可以用来帮助我们编写测试用例的一些工具。 + +To get started, run the `move test` command in the [`step_5/BasicCoin`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_5/BasicCoin) folder + +首先,请在文件夹 [`step_5/BasicCoin`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_5/BasicCoin)中 运行 `move test` 命令。 + +```bash +move test +``` +You should see something like this: + +您应该看到如下内容: + +``` +INCLUDING DEPENDENCY MoveStdlib +BUILDING BasicCoin +Running Move unit tests +[ PASS ] 0xcafe::BasicCoin::can_withdraw_amount +[ PASS ] 0xcafe::BasicCoin::init_check_balance +[ PASS ] 0xcafe::BasicCoin::init_non_owner +[ PASS ] 0xcafe::BasicCoin::publish_balance_already_exists +[ PASS ] 0xcafe::BasicCoin::publish_balance_has_zero +[ PASS ] 0xcafe::BasicCoin::withdraw_dne +[ PASS ] 0xcafe::BasicCoin::withdraw_too_much +Test result: OK. Total tests: 7; passed: 7; failed: 0 +``` + +Taking a look at the tests in the [`BasicCoin` module](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_5/BasicCoin/sources/BasicCoin.move) we've tried to keep each unit test to testing one particular behavior. + +看看 [`BasicCoin` ](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_5/BasicCoin/sources/BasicCoin.move)模块中的测试,我们试图让每个单元测试都测试一个具体的行为。 + +
+Exercise (练习) + +After taking a look at the tests, try and write a unit test called `balance_of_dne` in the `BasicCoin` module that tests the case where a `Balance` resource doesn't exist under the address that `balance_of` is being called on. It should only be a couple lines! + +在查看测试之后,尝试在 `BasicCoin` 模块中编写一个单元测试 `balance_of_dne`,测试地址没有 `Balance` 资源的情况,调用 `balance_of` 方法的执行结果。它应该只有几行代码。 + +The solution to this exercise can be found in [`step_5_sol`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_5_sol). + +练习的答案可以在[`step_5_sol`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_5_sol)中找到。 + +
+ +## Step 6: `BasicCoin` 模块泛型化(Making my `BasicCoin` module generic) + +In Move, we can use generics to define functions and structs over different input data types. Generics are a great building block for library code. In this section, we are going to make our simple `BasicCoin` module generic so that it can serve as a library module that can be used by other user modules. + +在 Move 语言中,我们可以使用泛型来定义不同输入数据类型的函数和结构体。泛型是库代码的重要组成部分。在本节中,我们将使我们的简单 `BasicCoin` 模块泛型化,以便它可以用作其他用户模块可以使用的模块库。 + +First, we add type parameters to our data structs: + +首先,我们将类型参数添加到我们的数据结构中: + +``` +struct Coin has store { + value: u64 +} + +struct Balance has key { + coin: Coin +} +``` + +We also add type parameters to our methods in the same manner. For example, `withdraw` becomes the following: + +我们还以相同的方式将类型参数添加到我们的方法中。例如,`withdraw` 变成如下: + +``` +fun withdraw(addr: address, amount: u64) : Coin acquires Balance { + let balance = balance_of(addr); + assert!(balance >= amount, EINSUFFICIENT_BALANCE); + let balance_ref = &mut borrow_global_mut>(addr).coin.value; + *balance_ref = balance - amount; + Coin { value: amount } +} +``` + +Take a look at [`step_6/BasicCoin/sources/BasicCoin.move`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_6/BasicCoin/sources/BasicCoin.move) to see the full implementation. + +查看[`step_6/BasicCoin/sources/BasicCoin.move`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_6/BasicCoin/sources/BasicCoin.move)完整的实现。 + +At this point, readers who are familiar with Ethereum might notice that this module serves a similar purpose as the [ERC20 token standard](https://ethereum.org/en/developers/docs/standards/tokens/erc-20/), which provides an interface for implementing fungible tokens in smart contracts. One key advantage of using generics is the ability to reuse code since the generic library module already provides a standard implementation and the instantiating module can provide customizations by wrapping the standard implementation. + +此时,熟悉以太坊的读者可能会注意到,该模块的用途与[ERC20 token standard](https://ethereum.org/en/developers/docs/standards/tokens/erc-20/)类似,后者提供了在智能合约中实现可替代代币的接口。使用泛型的一个关键优势是能够重用代码,因为泛型模块库已经提供了标准实现,并且实例化模块可以通过包装标准实现提供定制化功能。 + +We provide a little module called [`MyOddCoin`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_6/BasicCoin/sources/MyOddCoin.move) that instantiates the `Coin` type and customizes its transfer policy: only odd number of coins can be transferred. We also include two [tests](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_6/BasicCoin/sources/MyOddCoin.move) to test this behavior. You can use the commands you learned in step 2 and step 5 to run the tests. + +我们提供了一个称为[`MyOddCoin`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_6/BasicCoin/sources/MyOddCoin.move)并实例化 `Coin` 类型并自定义其转移策略的小模块:只能转移奇数个代币。其还包括两个 [tests](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_6/BasicCoin/sources/MyOddCoin.move)来测试这种行为。您可以使用在第 2 步和第 5 步中学到的命令来运行测试。 + + +#### 进阶主题 (Advanced topics): + +
+ +phantom 类型参数 (phantom type parameters) + +In definitions of both `Coin` and `Balance`, we declare the type parameter `CoinType` to be phantom because `CoinType` is not used in the struct definition or is only used as a phantom type parameter. + +在 `Coin` 和 `Balance `的定义中,我们将类型参数 `CoinType` 声明为phantom,因为 `CoinType` 没有在结构体定义中使用或仅用作 phantom 类型参数。 + +Read more about phantom type parameters here. + +阅读更多有关 [phantom 类型参数](./generics.md#phantom-type-parameters) 信息. + +
+ +## 进阶步骤 (Advanced steps) + +Before moving on to the next steps, let's make sure you have all the prover dependencies installed. + +在继续下一步之前,确保您已安装所有的验证器依赖项。 + +Try running `boogie /version `. If an error message shows up saying "command not found: boogie", you will have to run the setup script and source your profile: + +尝试运行 `boogie /version` 。如果出现错误消息“找不到命令:boogie”,您将必须运行安装脚本并更新环境配置(`source ~/.profile`): + +```bash +# run the following in move repo root directory +./scripts/dev_setup.sh -yp +source ~/.profile +``` + +## Step 7: 使用Move验证器(Use the Move prover) + +Smart contracts deployed on the blockchain may manipulate high-value assets. As a technique that uses strict mathematical methods to describe behavior and reason correctness of computer systems, formal verification has been used in blockchains to prevent bugs in smart contracts. [The Move prover](https://github.com/move-language/move/blob/main/language/move-prover/doc/user/prover-guide.md) is an evolving formal verification tool for smart contracts written in the Move language. The user can specify functional properties of smart contracts using the [Move Specification Language (MSL)](https://github.com/move-language/move/blob/main/language/move-prover/doc/user/spec-lang.md) and then use the prover to automatically check them statically. To illustrate how the prover is used, we have added the following code snippet to the [BasicCoin.move](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_7/BasicCoin/sources/BasicCoin.move): + +部署在区块链上的智能合约可能会操纵高价值资产。作为一种使用严格的数学方式来描述计算机系统的行为和推理正确性的技术,形式化验证已被用于区块链,以防止智能合约中错误的产生。 [Move验证器](https://github.com/move-language/move/blob/main/language/move-prover/doc/user/prover-guide.md)是一种在进化中、用Move 语言编写的智能合约形式化验证工具。用户可以使用[Move语言规范(Move Specification Language (MSL))](https://github.com/move-language/move/blob/main/language/move-prover/doc/user/spec-lang.md)指定智能合约的功能属性,然后使用验证器自动静态检查它们。 +为了说明如何使用验证器,我们在[BasicCoin.move](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_7/BasicCoin/sources/BasicCoin.move)中添加了以下代码片段: + +``` + spec balance_of { + pragma aborts_if_is_strict; + } +``` + +Informally speaking, the block `spec balance_of {...}` contains the property specification of the method `balance_of`. + +通俗地说,代码块 `spec balance_of {...}` 包含 `balance_of` 方法的属性规范说明。 + +Let's first run the prover using the following command inside [`BasicCoin` directory](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_7/BasicCoin/): + +首先在[`BasicCoin` directory](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_7/BasicCoin/)目录中使用以下命令运行验证器。 + +```bash +move prove +``` + +which outputs the following error information: + +它输出以下错误信息: + +``` +error: abort not covered by any of the `aborts_if` clauses + ┌─ ./sources/BasicCoin.move:38:5 + │ +35 │ borrow_global>(owner).coin.value + │ ------------- 由于执行失败这里发生中止 + · +38 │ ╭ spec balance_of { +39 │ │ pragma aborts_if_is_strict; +40 │ │ } + │ ╰─────^ + │ + = at ./sources/BasicCoin.move:34: balance_of + = owner = 0x29 + = at ./sources/BasicCoin.move:35: balance_of + = 中止 + +Error: exiting with verification errors +``` + +The prover basically tells us that we need to explicitly specify the condition under which the function `balance_of` will abort, which is caused by calling the function `borrow_global` when `owner` does not own the resource `Balance`. To remove this error information, we add an `aborts_if` condition as follows: + +验证器大体上告诉我们,我们需要明确指定函数 `balance_of` 中止的条件,中止原因是 `owner`(函数调用者)在没有资源 `Balance` 的情况下调用 `borrow_global` 函数导致的。要去掉此错误信息,我们添加如下 `aborts_if` 条件: + + +``` + spec balance_of { + pragma aborts_if_is_strict; + aborts_if !exists>(owner); + } +``` + +After adding this condition, try running the `prove` command again to confirm that there are no verification errors: + +添加此条件后,再次尝试运行prove命令,确认没有验证错误: + +```bash +move prove +``` + +Apart from the abort condition, we also want to define the functional properties. In Step 8, we will give more detailed introduction to the prover by specifying properties for the methods defined the `BasicCoin` module. + +除了中止条件,我们还想定义功能属性。在第 8 步中,我们将通过为定义 `BasicCoin` 模块的方法指定属性来更详细地介绍验证器。 + + +## 第 8 步:为 `BasicCoin` 模块编写正式规范(Write formal specifications for the `BasicCoin` module) + +
+ + 取款方法 (Method withdraw) + +The signature of the method `withdraw` is given below: + + 取款(`withdraw`) 方法的签名如下: + +``` +fun withdraw(addr: address, amount: u64) : Coin acquires Balance +``` + +The method withdraws tokens with value `amount` from the address `addr` and returns a created Coin of value `amount`. The method `withdraw` aborts when 1) `addr` does not have the resource `Balance` or 2) the number of tokens in `addr` is smaller than `amount`. We can define conditions like this: + +该方法从地址 `addr` 中提取数量为 `amount` 的代币,然后创建数量为 `amount` 的代币并将其返回。当出现如下情况会中止: + 1) 地址 `addr` 没有资源 `Balance`,或 + 2) 地址 `addr` 中的代币数量小于 `amount` 时,`withdraw` 。 + +我们可以这样定义条件: + +``` + spec withdraw { + let balance = global>(addr).coin.value; + aborts_if !exists>(addr); + aborts_if balance < amount; + } +``` + +As we can see here, a spec block can contain let bindings which introduce names for expressions. `global(address): T` is a built-in function that returns the resource value at `addr`. `balance` is the number of tokens owned by `addr`. `exists(address): bool` is a built-in function that returns true if the resource T exists at address. Two `aborts_if` clauses correspond to the two conditions mentioned above. In general, if a function has more than one `aborts_if` condition, those conditions are or-ed with each other. By default, if a user wants to specify aborts conditions, all possible conditions need to be listed. Otherwise, the prover will generate a verification error. However, if `pragma aborts_if_is_partial` is defined in the spec block, the combined aborts condition (the or-ed individual conditions) only *imply* that the function aborts. The reader can refer to the [MSL](https://github.com/move-language/move/blob/main/language/move-prover/doc/user/spec-lang.md) document for more information. + +正如我们在这里看到的,一个 spec 块可以包含 `let` 绑定,它为表达式引入名称。 +`global(address): T` 是一个返回 `addr` 资源值的内置函数。`balance` 是 `addr` 拥有的代币数量。 +`exists(address): bool` 是一个内置函数,如果指定的地址(address)在(全局存储中)有资源 `T` 则返回 `true` 。 +两个 `aborts_if` 子句对应上述两个条件。通常,如果一个函数有多个 `aborts_if` 条件,这些条件之间是相互对等的。默认情况下,如果用户想要指定中止条件,则需要列出所有可能的条件。否则验证器将产生验证错误。 +但是,如果在 `spec` 代码块中定义了 `pragma aborts_if_is_partial`,则组合中止条件(或对等的单个条件)仅 *暗示* 函数中止。 +读者可以参考 [MSL](https://github.com/move-language/move/blob/main/language/move-prover/doc/user/spec-lang.md) 文档了解更多信息。 + +The next step is to define functional properties, which are described in the two `ensures` clauses below. First, by using the `let post` binding, `balance_post` represents the balance of `addr` after the execution, which should be equal to `balance - amount`. Then, the return value (denoted as `result`) should be a coin with value `amount`. + +下一步是定义功能属性,这些属性在下面的两个 `ensures` 子句中进行了描述。首先,通过使用 `let post` 绑定,`balance_post` 表示地址 `addr` 执行后的余额,应该等于 `balance - amount`。那么,返回值(表示为 `result` )应该是一个价值为 `amount` 的代币。 + +``` + spec withdraw { + let balance = global>(addr).coin.value; + aborts_if !exists>(addr); + aborts_if balance < amount; + + let post balance_post = global>(addr).coin.value; + ensures balance_post == balance - amount; + ensures result == Coin { value: amount }; + } +``` +
+ +
+ + 存款方法 (Method deposit) + +The signature of the method `deposit` is given below: + +存款(`deposit`)方法的签名如下: + +``` +fun deposit(addr: address, check: Coin) acquires Balance +``` + +The method deposits the `check` into `addr`. The specification is defined below: + +该方法将代币 `check` 存入地址 `addr`. 规范定义如下: + +``` + spec deposit { + let balance = global>(addr).coin.value; + let check_value = check.value; + + aborts_if !exists>(addr); + aborts_if balance + check_value > MAX_U64; + + let post balance_post = global>(addr).coin.value; + ensures balance_post == balance + check_value; + } +``` + +`balance` represents the number of tokens in `addr` before execution and `check_value` represents the number of tokens to be deposited. The method would abort if 1) `addr` does not have the resource `Balance` or 2) the sum of `balance` and `check_value` is greater than the maxium value of the type `u64`. The functional property checks that the balance is correctly updated after the execution. + +`balance` 表示 `addr` 执行前的代币数量,`check_value` 表示要存入的代币数量。方法出现如下情况将会中止: + 1) 地址 `addr` 没有 `Balance` 资源, 或 + 2) `balance` 与 `check_value` 之和大于 `u64` 的最大值。 + +该功能属性检查执行后余额是否正确更新。 + + +
+ +
+ + 转账方法 (Method transfer) + +The signature of the method `transfer` is given below: + +转账(`transfer`)方法的签名如下: + +``` +public fun transfer(from: &signer, to: address, amount: u64, _witness: CoinType) acquires Balance +``` + +The method transfers the `amount` of coin from the account of `from` to the address `to`. The specification is given below: + +该方法将数量为 `amount` 的代币从帐户 `from` 转账给地址 `to`。规范如下: + +``` + spec transfer { + let addr_from = signer::address_of(from); + + let balance_from = global>(addr_from).coin.value; + let balance_to = global>(to).coin.value; + let post balance_from_post = global>(addr_from).coin.value; + let post balance_to_post = global>(to).coin.value; + + ensures balance_from_post == balance_from - amount; + ensures balance_to_post == balance_to + amount; + } +``` + +`addr_from` is the address of `from`. Then the balances of `addr_from` and `to` before and after the execution are obtained. +The `ensures` clauses specify that the `amount` number of tokens is deducted from `addr_from` and added to `to`. However, the prover will generate the error information as below: + +`addr_from` 是账户 `from` 的地址,然后获取执行前两个地址 `addr_from` 和 `to` 的余额。 + `ensures` 子句指定从 `addr_from` 减去 `amount` 数量的代币,添加到 `to`。然而,验证器会生成以下错误: + +``` +error: post-condition does not hold + ┌─ ./sources/BasicCoin.move:57:9 + │ +62 │ ensures balance_from_post == balance_from - amount; + │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + │ + ... +``` + +The property is not held when `addr_from` is equal to `to`. As a result, we could add an assertion `assert!(from_addr != to)` in the method to make sure that `addr_from` is not equal to `to`. + +当 `addr_from` 等于 `to` 时,这个属性无效。因此,我们可以在方法中添加一个断言,`assert!(from_addr != to)` 来确保 `addr_from` 不等于 `to`。 + +
+ + +
+ + 练习 (Exercises) + +- Implement the `aborts_if` conditions for the `transfer` method. +- 为` transfer` 方法实现 `aborts_if` 条件。 +- Implement the specification for the `mint` and `publish_balance` method. +- 为 `mint` 和 `publish_balance` 方法实现规范。 + +The solution to this exercise can be found in [`step_8_sol`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_8_sol). + +练习的解答可以在 [`step_8_sol`](https://github.com/move-language/move/tree/main/language/documentation/tutorial/step_8_sol)中找到。 diff --git a/language/documentation/book/translations/move-book-zh/src/overview.md b/language/documentation/book/translations/move-book-zh/src/overview.md new file mode 100644 index 0000000000..d08bf43529 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/overview.md @@ -0,0 +1,200 @@ +--- +id: overview +title: Overview +sidebar_label: Move +--- + +Move is a next generation language for secure, sandboxed, and formally verified programming. Its first use case is for the Diem blockchain, where Move provides the foundation for its implementation. However, Move has been developed with use cases in mind outside a blockchain context as well. + +### Start Here + + + + + + + +### Primitive Types + + + + + + + + + + + +### Basic Concepts + + + + + + + + + + + + + + +### Global Storage + + + + + + +### Reference + + + + + diff --git a/language/documentation/book/translations/move-book-zh/src/packages.md b/language/documentation/book/translations/move-book-zh/src/packages.md new file mode 100644 index 0000000000..4ab185d120 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/packages.md @@ -0,0 +1,348 @@ +# 程序包(packages) + +Packages allow Move programmers to more easily re-use code and share it +across projects. The Move package system allows programmers to easily: +* Define a package containing Move code; +* Parameterize a package by [named addresses](./address.md); +* Import and use packages in other Move code and instantiate named addresses; +* Build packages and generate associated compilation artifacts from packages; and +* Work with a common interface around compiled Move artifacts. + +包允许 `Move` 程序员更轻松地重用代码并在项目之间共享。`Move` 包系统允许程序员轻松地: +* 定义一个包含 `Move`代码的包; +* 通过命名地址参数化包; +* 在其他 `Move` 代码中导入和使用包并实例化命名地址; +* 构建包并从包中生成相关的编译源代码; +* 使用围绕已编译 `Move` 工件的通用接口。 + +## 包布局和清单语法(Package Layout and Manifest Syntax) + +A Move package source directory contains a `Move.toml` package manifest +file along with a set of subdirectories: + +`Move` 包源目录包含一个`Move.toml`包清单文件以及一组子目录: + +``` + a_move_package + ├── Move.toml (required)(需要的) + ├── sources (required)(需要的) + ├── examples (optional, test & dev mode)(可选的,测试 & 开发者模式) + ├── scripts (optional)(可选的) + ├── doc_templates (optional)(可选的) + └── tests (optional, test mode)(可选的,测试模式) +``` + +The directories marked `required` _must_ be present in order for the directory +to be considered a Move package and to be compiled. Optional directories can +be present, and if so will be included in the compilation process. Depending on +the mode that the package is built with (`test` or `dev`), the `tests` and +`examples` directories will be included as well. + +标记为`required` 的目录必须存在才可以将该目录作为 `Move` 包并进行编译。可选目录被视为可存在的,如果存在,将包含在编译过程里。根据使用 (`test`或`dev`)构建包的模式,`tests`和`examples` 目录也将包含在内。 + +The `sources` directory can contain both Move modules and Move scripts (both +transaction scripts and modules containing script functions). The `examples` +directory can hold additional code to be used only for development and/or +tutorial purposes that will not be included when compiled outside `test` or +`dev` mode. + +`sources`目录可以包含 `Move` 模块和 `Move` 脚本(事务脚本和包含脚本函数的模块)。`Example`目录可以保留仅用于开发和/或用作教程目的附加代码,当在 `test` 或者`dev`模式之外时,这些附加代码编译时不会被包括进来。 + +A `scripts` directory is supported so transaction scripts can be separated +from modules if that is desired by the package author. The `scripts` +directory will always be included for compilation if it is present. +Documentation will be built using any documentation templates present in +the `doc_templates` directory. + +`scripts`目录是被支持的,如果包作者需要,事物脚本可以从模块中分离。如果该`scripts`目录存在,则编译时将始终包含该目录。 +Move将使用存在于`doc_templates` 目录的任何模板构建文档。 + + +### 包清单 Move.toml + +The Move package manifest is defined within the `Move.toml` file and has the +following syntax. Optional fields are marked with `*`, `+` denotes +one or more elements: + +Move 包清单在`Move.toml`文件中定义,并具有以下语法。可选字段标有`*`,`+`表示一个或多个元素: +``` + [package] + name = # e.g., "MoveStdlib" + version = ".." # e.g., "0.1.1" + license* = # e.g., "MIT", "GPL", "Apache 2.0" + authors* = [] # e.g., ["Joe Smith (joesmith@noemail.com)", "Jane Smith (janesmith@noemail.com)"] + + [addresses] # (Optional section) Declares named addresses in this package and instantiates named addresses in the package graph + # One or more lines declaring named addresses in the following format + = "_" | "" # e.g., std = "_" or my_addr = "0xC0FFEECAFE" + + [dependencies] # (Optional section) Paths to dependencies and instantiations or renamings of named addresses from each dependency + # One or more lines declaring dependencies in the following format + = { local = , addr_subst* = { ( = ( | ""))+ } } # local dependencies + = { git = , subdir=, rev=, addr_subst* = { ( = ( | ""))+ } } # git dependencies + + [dev-addresses] # (Optional section) Same as [addresses] section, but only included in "dev" and "test" modes + # One or more lines declaring dev named addresses in the following format + = "_" | "" # e.g., std = "_" or my_addr = "0xC0FFEECAFE" + + [dev-dependencies] # (Optional section) Same as [dependencies] section, but only included in "dev" and "test" modes + # One or more lines declaring dev dependencies in the following format + = { local = , addr_subst* = { ( = ( |
))+ } } +``` +An example of a minimal package manifest with one local dependency and one git dependency: + +一个具有局部依赖项和一个 git 依赖项的最小包清单示例: +``` + [package] + name = "AName" + version = "0.0.0" +``` + +An example of a more standard package manifest that also includes the Move +standard library and instantiates the named address `Std` from it with the +address value `0x1`: + +一个包括 Move 标准库并从中使用地址值`0x1`实例化命名地址`Std`的更标准的包清单示例: + +``` + [package] + name = "AName" + version = "0.0.0" + license = "Apache 2.0" + + [addresses] + address_to_be_filled_in = "_" + specified_address = "0xB0B" + + [dependencies] + # Local dependency + LocalDep = { local = "projects/move-awesomeness", addr_subst = { "std" = "0x1" } } + # Git dependency + MoveStdlib = { git = "https://github.com/diem/diem.git", subdir="language/move-stdlib", rev = "56ab033cc403b489e891424a629e76f643d4fb6b" } + + [dev-addresses] # For use when developing this module + address_to_be_filled_in = "0x101010101" + ``` + +Most of the sections in the package manifest are self explanatory, but named +addresses can be a bit difficult to understand so it's worth examining them in +a bit more detail. + +包清单中的大部分段落都是不言自明的,但命名地址可能有点难以理解,因此值得更详细地检查它们。 + +## 编译期间的命名地址(Named Addresses During Compilation) + +Recall that Move has [named addresses](./address.md) and that +named addresses cannot be declared in Move. Because of this, until now +named addresses and their values needed to be passed to the compiler on the +command line. With the Move package system this is no longer needed, and +you can declare named addresses in the package, instantiate other named +addresses in scope, and rename named addresses from other packages within +the Move package system manifest file. Let's go through each of these +individually: + +回想一下,Move 具有命名地址,并且不能在 Move 中声明命名地址。正因为如此,到目前为止,命名地址及其值都需要在命令行上传递给编译器。但使用 Move 包系统时这将不再需要,您可以在包中声明命名地址,实例化范围内的其他命名地址,并从 Move 包系统清单文件中的其他包重命名命名地址,让我们分别来看看这些: + +### 声明(Declaration) +Let's say we have a Move module in `example_pkg/sources/A.move` as follows: + +假设我们有一个Move模块,`example_pkg/sources/A.move`如下所示: + +```move + module named_addr::A { + public fun x(): address { @named_addr } + } +``` + +We could in `example_pkg/Move.toml` declare the named address `named_addr` in +two different ways. The first: + +我们可以用两种不同`example_pkg/Move.toml`的方式声明命名地址`named_addr`。首先: + +``` + [package] + name = "ExamplePkg" + ... + [addresses] + named_addr = "_" +``` + +Declares `named_addr` as a named address in the package `ExamplePkg` and +that _this address can be any valid address value_. Therefore an importing +package can pick the value of the named address `named_addr` to be any address +it wishes. Intuitively you can think of this as parameterizing the package +`ExamplePkg` by the named address `named_addr`, and the package can then be +instantiated later on by an importing package. + +声明`named_addr`为包`ExamplePkg`中的命名地址,并且 _该地址可以是任何有效的地址值_。因此,导入包可以选择命名地址的值作为`named_addr`它希望的任何地址。直观地,您可以将其视为通过命名地址`named_addr`参数化包 `ExamplePkg`,然后稍后通过导入包使包被实例化。 + +`named_addr` can also be declared as: + +`named_addr`也可以声明为: + +``` + [package] + name = "ExamplePkg" + ... + [addresses] + named_addr = "0xCAFE" +``` + +which states that the named address `named_addr` is exactly `0xCAFE` and cannot be +changed. This is useful so other importing packages can use this named +address without needing to worry about the exact value assigned to it. + +这表明命名的地址`named_addr`是准确的`0xCAFE`并且不能更改。这很有用,因此其他导入包可以使用这个命名地址,而无需担心分配给它的确切值。 + +With these two different declaration methods, there are two ways that +information about named addresses can flow in the package graph: +* The former ("unassigned named addresses") allows named address values to flow + from the importation site to the declaration site. +* The latter ("assigned named addresses") allows named address values to flow + from the declaration site upwards in the package graph to usage sites. + +使用这两种不同的声明方法,有关命名地址的信息可以通过两种方式在包图中流动: +* 前者(“未分配的命名地址”)允许命名地址值从进口站点流向申报站点。 +* 后者(“分配的命名地址”)允许命名地址值从包图中的声明站点向上流动到使用站点。 + +With these two methods for flowing named address information throughout the +package graph the rules around scoping and renaming become important to +understand. + +通过这两种在整个包图中流动命名地址信息的方法,了解范围和重命名的规则变得很重要。 + +## 命名地址的作用域和重命名(Scoping and Renaming of Named Addresses) + +A named address `N` in a package `P` is in scope if: +1. It declares a named address `N`; or +2. A package in one of `P`'s transitive dependencies declares the named address + `N` and there is a dependency path in the package graph between between `P` and the + declaring package of `N` with no renaming of `N`. + +在包`P`中的命名地址`N`如果满足以下条件,则在作用域内: + + 1. 它声明了一个命名地址`N`;或者 + 2. `P`的传递依赖项之一中的包声明了命名地址`N`,并且封装图在`P`和没有重命名的声明包`N`之间有一个依赖路径。 + + Additionally, every named address in a package is exported. Because of this and +the above scoping rules each package can be viewed as coming with a set of +named addresses that will be brought into scope when the package is imported, +e.g., if the `ExamplePkg` package was imported, that importation would bring +into scope the `named_addr` named address. Because of this, if `P` imports two +packages `P1` and `P2` both of which declare a named address `N` an issue +arises in `P`: which "`N`" is meant when `N` is referred to in `P`? The one +from `P1` or `P2`? To prevent this ambiguity around which package a named +address is coming from, we enforce that the sets of scopes introduced by all +dependencies in a package are disjoint, and provide a way to _rename named +addresses_ when the package that brings them into scope is imported. + + 此外,包中的每个命名地址都会被导出。由于这个和上面的范围规则,每个包都可以被视为带有一组命名地址,当包被导入时,这些地址将被带入作用域,例如,如果包`ExamplePkg`被导入,则该导入会将命名地址`named_addr`带入作用域。 因此,如果`P`导入两个包`P1`并且`P2`都声明了一个命名地址`N`,在`P`中则会出现以下问题:当`N`被引用于`P`时我们指的是哪个`N`?来自`P1`或来自`P2`的`N`? 为了防止命名地址来自哪个包的这种歧义,我们强制一个包中所有依赖项引入的范围集是不相交的,并提供一种在将命名地址带入范围的包被导入时重命名命名地址的方法。 + +Renaming a named address when importing can be done as follows in our `P`, +`P1`, and `P2` example above: + +导入时重命名一个命名地址可以在我们的`P`,`P1`和`P2`上面的示例中完成: +``` + [package] + name = "P" + ... + [dependencies] + P1 = { local = "some_path_to_P1", addr_subst = { "P1N" = "N" } } + P2 = { local = "some_path_to_P2" } +``` +With this renaming `N` refers to the `N` from `P2` and `P1N` will refer to `N` +coming from `P1`: + +这种重命名`N`指的是`P2`中的`N`并且`P1N`将指 `P1`中的`N`: +``` + module N::A { + public fun x(): address { @P1N } + } +``` +It is important to note that _renaming is not local_: once a named address `N` +has been renamed to `N2` in a package `P` all packages that import `P` will not +see `N` but only `N2` unless `N` is reintroduced from outside of `P`. This is +why rule (2) in the scoping rules at the start of this section specifies a +"dependency path in the package graph between between `P` and the declaring +package of `N` with no renaming of `N`." + +重要的是要注意 _重命名不是局部的_:一旦一个命名地址`N`在一个包`P`中被重命名为`N2`,所有导入`P`的包都不会看到`N`但只会看到`N2`,除非`N`是从`P`外引入的。这就是为什么本节开头的范围规则中的规则 (2) 特别说明了“在`P`和没有重命名的声明包`N` 的封装图中的依赖路径” 。 + +### 实例化(Instantiation) + +Named addresses can be instantiated multiple times across the package graph as +long as it is always with the same value. It is an error if the same named +address (regardless of renaming) is instantiated with differing values across +the package graph. + +只要命名地址始终具有相同的值,就可以在封装图中多次实例化命名地址。如果在整个封装图中使用不同的值实例化相同的命名地址(无论是否重命名),则会出现错误。 + +A Move package can only be compiled if all named addresses resolve to a value. +This presents issues if the package wishes to expose an uninstantiated named +address. This is what the `[dev-addresses]` section solves. This section can +set values for named addresses, but cannot introduce any named addresses. +Additionally, only the `[dev-addresses]` in the root package are included in +`dev` mode. For example a root package with the following manifest would not compile +outside of `dev` mode since `named_addr` would be uninstantiated: + +只有当所有命名地址都解析为一个值时,才能编译 Move 包。如果包希望公开未实例化的命名地址,则会出现问题。这就是`[dev-addresses]`段要解决的问题。此段可以设置命名地址的值,但不能引入任何命名地址。此外, `dev`模式下仅根包中的`[dev-addresses]`会被包括进来。例如,具有以下清单的根包将不会在`dev`模式之外编译,因为`named_addr`不会被实例化: +``` +[package] +name = "ExamplePkg" +... +[addresses] +named_addr = "_" + +[dev-addresses] +named_addr = "0xC0FFEE" +``` +## 用法、源代码和数据结构( Usage, Artifacts, and Data Structures) + +The Move package system comes with a command line option as part of the Move +CLI `move `. Unless a +particular path is provided, all package commands will run in the current working +directory. The full list of commands and flags for the Move CLI can be found by +running `move --help`. + +Move 软件包系统带有一个命令行选项,作为 Move CLI 的一部分move ` ` ``。除非提供特定路径,否则所有包命令都将在当前工作目录中运行。可以通过运行`move --help`找到 Move CLI 的命令和标志的完整列表。 + +### 用法(Usage) + +A package can be compiled either through the Move CLI commands, or as a library +command in Rust with the function `compile_package`. This will create a +`CompiledPackage` that holds the compiled bytecode along with other compilation +artifacts (source maps, documentation, ABIs) in memory. This `CompiledPackage` +can be converted to an `OnDiskPackage` and vice versa -- the latter being the data of +the `CompiledPackage` laid out in the file system in the following format: + +一个包可以通过 Move CLI 命令,或是当作Rust函数`compile_package`的库命令来编译。 这种编译方法将创建一个编译包`CompiledPackage` 保存已编译的字节码以及其他编译内存中的源代码(源映射、文档、ABIs)。这个`CompiledPackage`可以转换为`OnDiskPackage`,反之亦然——后者是文件系统中的编译包 `CompiledPackage`数据,它的格式如下: + +``` +a_move_package +├── Move.toml +... +└── build + ├── + │ ├── BuildInfo.yaml + │ ├── bytecode_modules + │ │ └── *.mv + │ ├── source_maps + │ │ └── *.mvsm + │ ├── bytecode_scripts + │ │ └── *.mv + │ ├── abis + │ │ ├── *.abi + │ │ └── /*.abi + │ └── sources + │ └── *.move + ... + └── + ├── BuildInfo.yaml + ... + └── sources +``` + +See the `move-package` crate for more information on these data structures and +how to use the Move package system as a Rust library. + +有关这些数据结构和如何将 Move 包系统用作 Rust 库的更多信息,请参阅 `move-package` 箱(crate) 。 diff --git a/language/documentation/book/translations/move-book-zh/src/references.md b/language/documentation/book/translations/move-book-zh/src/references.md new file mode 100644 index 0000000000..287287dfc5 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/references.md @@ -0,0 +1,289 @@ +# 引用(references) + +Move has two types of references: immutable `&` and mutable `&mut`. Immutable references are read +only, and cannot modify the underlying value (or any of its fields). Mutable references allow for +modifications via a write through that reference. Move's type system enforces an ownership +discipline that prevents reference errors. + +Move 支持两种类型的引用:不可变引用 `&` 和可变引用 `&mut`。不可变引用是只读的,不能修改相关值(或其任何字段)。可变引用通过写入该引用进行修改。Move的类型系统强制执行所有权规则,以避免引用错误。 + +For more details on the rules of references, see [Structs and Resources](./structs-and-resources.md) + +更多有关引用规则的详细信息,请参阅:[结构和资源](./structs-and-resources.html). + +## 引用运算符 (Reference Operators) + +Move provides operators for creating and extending references as well as converting a mutable +reference to an immutable one. Here and elsewhere, we use the notation `e: T` for "expression `e` +has type `T`". + +Move 提供了用于创建和扩展引用以及将可变引用转换为不可变引用的运算符。在这里和其他地方,我们使用符号 `e: T` 来表示“表达式 `e` 的类型是 `T` ” + +| Syntax | Type | Description | +| ----------- | ----------------------------------------------------- | -------------------------------------------------------------- | +| `&e` | `&T` where `e: T` and `T` is a non-reference type | Create an immutable reference to `e` | +| `&mut e` | `&mut T` where `e: T` and `T` is a non-reference type | Create a mutable reference to `e`. | +| `&e.f` | `&T` where `e.f: T` | Create an immutable reference to field `f` of struct `e`. | +| `&mut e.f` | `&mut T` where `e.f: T` | Create a mutable reference to field `f` of struct`e`. | +| `freeze(e)` | `&T` where `e: &mut T` | Convert the mutable reference `e` into an immutable reference. | + +| 语法 | 类型 | 描述 | +| ------ | ------ |------ | +| `&e` | `&T` 其中 `e: T` 和 `T` 是非引用类型 | 创建一个不可变的引用 `e` +| `&mut e` | `&mut T` 其中 `e: T` 和 `T` 是非引用类型 | 创建一个可变的引用 `e` +| `&e.f` | `&T` 其中 `e.f: T` | 创建结构 `e` 的字段 `f` 的不可变引用 +| `&mut e.f` | `&mut T` 其中`e.f: T` | 创建结构 `e` 的字段 `f` 的可变引用 +| `freeze(e)` | `&T` 其中`e: &mut T` | 将可变引用 `e` 转换为不可变引用 + +The `&e.f` and `&mut e.f` operators can be used both to create a new reference into a struct or to extend an existing reference: + +`&e.f`和`&mut e.f`运算符既可以用于在结构中创建新引用,也可以用于扩展现有引用: + +```move +let s = S { f: 10 }; +let f_ref1: &u64 = &s.f; // works +let s_ref: &S = &s; +let f_ref2: &u64 = &s_ref.f // also works +``` + +A reference expression with multiple fields works as long as both structs are in the same module: + +只要两个结构都在同一个模块中,具有多个字段的引用表达式就可以工作: + +```move +struct A { b: B } +struct B { c : u64 } +fun f(a: &A): &u64 { + &a.b.c +} +``` + +Finally, note that references to references are not allowed: + +最后,请注意,不允许引用"引用"(Move不支持多重引用, 但Rust可以,译者注): + +```move +let x = 7; +let y: &u64 = &x; +let z: &&u64 = &y; // will not compile +``` + +## 通过引用读取和写入 + +Both mutable and immutable references can be read to produce a copy of the referenced value. + +Only mutable references can be written. A write `*x = v` discards the value previously stored in `x` +and updates it with `v`. + +可以读取可变和不可变引用以生成引用值的副本。 + +只能写入可变引用。写入表达式 `*x = v` 会丢弃先前存储在x中的值,并用 `v` 更新。 + +Both operations use the C-like `*` syntax. However, note that a read is an expression, whereas a +write is a mutation that must occur on the left hand side of an equals. + +两种操作都使用类 C `*` 语法。但是请注意,读取是一个表达式,而写入是一个必须发生在等号左侧的改动。 + +| Syntax | Type | Description | +| ---------- | ----------------------------------- | ----------------------------------- | +| `*e` | `T` where `e` is `&T` or `&mut T` | Read the value pointed to by `e` | +| `*e1 = e2` | `()` where `e1: &mut T` and `e2: T` | Update the value in `e1` with `e2`. | + +| 语法 | 类型 | 描述 | +| ------ | ------ |------ | +| `&e` | `T` 其中 `e` 为 `&T` 或 `&mut T` | 读取 `e` 所指向的值 +| `*e1 = e2` | () 其中 `e1: &mut T` 和 `e2: T` | 用 `e2` 更新 `e1` 中的值 + +In order for a reference to be read, the underlying type must have the +[`copy` ability](./abilities.md) as reading the reference creates a new copy of the value. This rule +prevents the copying of resource values: + +为了读取引用,相关类型必须具备[`copy` 能力](./abilities.html),因为读取引用会创建值的新副本。此规则防止复制资源值: + +```move= +fun copy_resource_via_ref_bad(c: Coin) { + let c_ref = &c; + let counterfeit: Coin = *c_ref; // not allowed! + pay(c); + pay(counterfeit); +} +``` + +Dually: in order for a reference to be written to, the underlying type must have the +[`drop` ability](./abilities.md) as writing to the reference will discard (or "drop") the old value. +This rule prevents the destruction of resource values: + +双重性:为了写入引用,相关类型必须具备[`drop` 能力](./abilities.html),因为写入引用将丢弃(或“删除”)旧值。此规则可防止破坏资源值: + +```move= +fun destroy_resource_via_ref_bad(ten_coins: Coin, c: Coin) { + let ref = &mut ten_coins; + *ref = c; // not allowed--would destroy 10 coins! +} +``` + +## `freeze` 推断 (`freeze` inference) + +A mutable reference can be used in a context where an immutable reference is expected: + +可变引用可以在预期不可变引用的上下文中使用: + +```move +let x = 7; +let y: &mut u64 = &mut x; +``` + +This works because the under the hood, the compiler inserts `freeze` instructions where they are +needed. Here are a few more examples of `freeze` inference in action: + +这是因为编译器会在底层需要的地方插入 `freeze` 指令。以下是更多 `freeze` 实际推断行为的示例: + +```move= +fun takes_immut_returns_immut(x: &u64): &u64 { x } + +// freeze inference on return value +fun takes_mut_returns_immut(x: &mut u64): &u64 { x } + +fun expression_examples() { + let x = 0; + let y = 0; + takes_immut_returns_immut(&x); // no inference + takes_immut_returns_immut(&mut x); // inferred freeze(&mut x) + takes_mut_returns_immut(&mut x); // no inference + + assert!(&x == &mut y, 42); // inferred freeze(&mut y) +} + +fun assignment_examples() { + let x = 0; + let y = 0; + let imm_ref: &u64 = &x; + + imm_ref = &x; // no inference + imm_ref = &mut y; // inferred freeze(&mut y) +} +``` + +### 子类型化 (Subtyping) + +With this `freeze` inference, the Move type checker can view `&mut T` as a subtype of `&T`. As shown +above, this means that anywhere for any expression where a `&T` value is used, a `&mut T` value can +also be used. This terminology is used in error messages to concisely indicate that a `&mut T` was +needed where a `&T` was supplied. For example + +通过freeze推断,Move 类型检查器可以将 `&mut T` 视为 `&T` 的子类型。 如上所示,这意味着对于使用 `&T` 值的任何表达式,也可以使用 `&mut T` 值。此术语用于错误消息中,以简明扼要地表明在提供 `&T` 的地方需要 `&mut T` 。例如: + +```move= +address 0x42 { + module example { + fun read_and_assign(store: &mut u64, new_value: &u64) { + *store = *new_value + } + + fun subtype_examples() { + let x: &u64 = &0; + let y: &mut u64 = &mut 1; + + x = &mut 1; // valid + y = &2; // invalid! + + read_and_assign(y, x); // valid + read_and_assign(x, y); // invalid! + } + } +} +``` + +will yield the following error messages + +将产生以下错误消息 + +```text +error: + ┌── example.move:12:9 ─── + │ + 12 │ y = &2; // invalid! + │ ^ Invalid assignment to local 'y' + · + 12 │ y = &2; // invalid! + │ -- The type: '&{integer}' + · + 9 │ let y: &mut u64 = &mut 1; + │ -------- Is not a subtype of: '&mut u64' + │ + +error: + ┌── example.move:15:9 ─── + │ + 15 │ read_and_assign(x, y); // invalid! + │ ^^^^^^^^^^^^^^^^^^^^^ Invalid call of '0x42::example::read_and_assign'. Invalid argument for parameter 'store' + · + 8 │ let x: &u64 = &0; + │ ---- The type: '&u64' + · + 3 │ fun read_and_assign(store: &mut u64, new_value: &u64) { + │ -------- Is not a subtype of: '&mut u64' + │ +``` + +The only other types currently that has subtyping are [tuples](./tuples.md) + +当前唯一具有子类型的其他类型是[tuple(元组)](./tuples.html) + +## 所有权 (Ownership) + +Both mutable and immutable references can always be copied and extended _even if there are existing +copies or extensions of the same reference_: + +_即使同一引用存在现有副本或扩展_,可变引用和不可变引用始终可以被复制和扩展: + +```move +fun reference_copies(s: &mut S) { + let s_copy1 = s; // ok + let s_extension = &mut s.f; // also ok + let s_copy2 = s; // still ok + ... +} +``` + +This might be surprising for programmers familiar with Rust's ownership system, which would reject +the code above. Move's type system is more permissive in its treatment of +[copies](./variables.md#move-and-copy), but equally strict in ensuring unique ownership of mutable +references before writes. + +对于熟悉 Rust 所有权系统的程序员来说,这可能会令人惊讶,因为他们会拒绝上面的代码。Move 的类型系统在处理[副本](./variables.html#move-and-copy)方面更加宽松 ,但在写入前确保可变引用的唯一所有权方面同样严格。 + +### 无法存储引用 (References Cannot Be Stored) + +References and tuples are the _only_ types that cannot be stored as a field value of structs, which +also means that they cannot exist in global storage. All references created during program execution +will be destroyed when a Move program terminates; they are entirely ephemeral. This invariant is +also true for values of types without the `store` [ability](./abilities.md), but note that +references and tuples go a step further by never being allowed in structs in the first place. + +This is another difference between Move and Rust, which allows references to be stored inside of +structs. + +引用和元组是唯一不能存储为结构的字段值的类型,这也意味着它们不能存在于全局存储中。当 Move 程序终止时,程序执行期间创建的所有引用都将被销毁;它们完全是短暂的。这种不变式也适用于没有[`store` 能力](./chatper_19_abilities.html)的类型的值,但请注意,引用和元组更进一步,从一开始就不允许出现在结构中。 + +这是 Move 和 Rust 之间的另一个区别,后者允许将引用存储在结构内。 + +Currently, Move cannot support this because references cannot be +[serialized](https://en.wikipedia.org/wiki/Serialization), but _every Move value must be +serializable_. This requirement comes from Move's +[persistent global storage](./global-storage-structure.md), which needs to serialize values to +persist them across program executions. Structs can be written to global storage, and thus they must +be serializable. + +One could imagine a fancier, more expressive, type system that would allow references to be stored +in structs _and_ ban those structs from existing in global storage. We could perhaps allow +references inside of structs that do not have the `store` [ability](./abilities.md), but that would +not completely solve the problem: Move has a fairly complex system for tracking static reference +safety, and this aspect of the type system would also have to be extended to support storing +references inside of structs. In short, Move's type system (particularly the aspects around +reference safety) would have to expand to support stored references. But it is something we are +keeping an eye on as the language evolves. + +目前,Move 无法支持这一点,因为引用无法被[序列化](https://en.wikipedia.org/wiki/Serialization),但 _每个 Move 值都必须是可序列化的_。这个要求来自于 Move 的 [持久化全局存储](./global-storage-structure.html),它需要在程序执行期间序列化值以持久化它们。结构体可以写入全局存储,因此它们必须是可序列化的。 + +可以想象一种更奇特、更有表现力的类型系统,它允许将引用存储在结构中,并禁止这些结构存在于全局存储中。我们也许可以允许在没有[`store` 能力](./abilities.html)的结构内部使用引用,但这并不能完全解决问题:Move 有一个相当复杂的系统来跟踪静态引用安全性,并且类型系统的这一方面也必须扩展以支持在结构内部存储引用。简而言之,Move 的类型系统(尤其是与引用安全相关的方面)需要扩展以支持存储的引用。随着语言的发展,我们正在关注这一点。 diff --git a/language/documentation/book/translations/move-book-zh/src/signer.md b/language/documentation/book/translations/move-book-zh/src/signer.md new file mode 100644 index 0000000000..2bba515c0a --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/signer.md @@ -0,0 +1,60 @@ +# 签名者 + +`签名者(signer)`是 Move 内置的资源类型。`签名者(signer)`是一种允许持有者代表特定`地址(address)`行使权力的[能力(capability)](https://en.wikipedia.org/wiki/Object-capability_model)。你可以将原生实现(native implementation)视为: + +```move +struct signer has drop { a: address } +``` + +`signer` 有点像 Unix [UID](https://en.wikipedia.org/wiki/User_identifier),因为它表示一个通过 Move *之外*的代码(例如,通过检查加密签名或密码)进行身份验证的用户。 + +## 与 `address` 的比较 + +Move 程序可以使用地址字面量(literal)创建任何`地址(address)`值,而无需特殊许可: + +```move +let a1 = @0x1; +let a2 = @0x2; +// ... 等等,所有其他可能的地址 +``` + +但是,`signer` 值是特殊的,因为它们不能通过字面量或者指令创建 —— 只能通过 Move 虚拟机(VM)创建。在虚拟机运行带有 `signer` 类型参数的脚本之前,它会自动创建 `signer` 值并将它们传递给脚本: + +```move +script { + use std::signer; + fun main(s: signer) { + assert!(signer::address_of(&s) == @0x42, 0); + } +} +``` + +如果脚本是从 `0x42` 以外的任何地址发送的,则此脚本将中止并返回代码 `0`。 + +交易脚本可以有任意数量的 `signer`,只要 `signer` 参数排在其他参数前面。换句话说,所有 `signer` 参数都必须放在第一位。 + +```move +script { + use std::signer; + fun main(s1: signer, s2: signer, x: u64, y: u8) { + // ... + } +} +``` + +这对于实现具有多方权限原子行为的*多重签名脚本(multi-signer scripts)*很有用。例如,上述脚本的扩展可以在 `s1` 和 `s2` 之间执行原子货币交换。 + +## `signer` 操作符 + +`std::signer` 标准库模块为 `signer` 提供了两个实用函数: + +| 函数 | 描述 | +| ------------------------------------------- | ------------------------------------------------------------- | +| `signer::address_of(&signer): address` | 返回由 `&signer` 包装的地址值。 | +| `signer::borrow_address(&signer): &address` | 返回由 `&signer` 包装的地址的引用。 | + +此外,`move_to(&signer, T)` [全局存储](./global-storage-operators.md)操作符需要一个 `&signer` 参数在 `signer.address` 的帐户下发布资源 `T`。这确保了只有经过身份验证的用户才能在其地址下发布资源。 + +## 所有权 + +与简单的标量值不同,`signer` 值是不可复制的,这意味着他们不能被复制(通过任何操作,无论是通过显式 [`copy`](./variables.md#移动和复制)指令还是通过[解引用(dereference)`*`](./references.md#通过引用读取和写入))。 diff --git a/language/documentation/book/translations/move-book-zh/src/standard-library.md b/language/documentation/book/translations/move-book-zh/src/standard-library.md new file mode 100644 index 0000000000..85c8d9f433 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/standard-library.md @@ -0,0 +1,695 @@ +# 标准库(Standard Library) + +The Move standard library exposes interfaces that implement the following functionality: +* [Basic operations on vectors](#vector). +* [Option types and operations on`Option` types](#option). +* [A common error encoding code interface for abort codes](#errors). +* [32-bit precision fixed-point numbers](#fixed_point32). + +Move标准库公开了实现以下功能的接口: +* [向量的基本操作](#向量). +* [Option类型与基本操作](#option). +* [终止码的常见错误编码接口](#errors). +* [32位精确定点数字](#fixed_point32). + +## 向量(vector) + + +The `vector` module defines a number of operations over the primitive +[`vector`](./vector.md) type. The module is published under the +named address `Std` and consists of a number of native functions, as +well as functions defined in Move. The API for this module is as follows. + +`向量`模块在原生类型[`向量`](./vector.md)上定义了许多操作。该模块以命名地址`Std`发布,并由许多原生函数以及在Move中定义的函数组成。此模块的API如下所示: + +### 函数(Functions) + +--------------------------------------------------------------------------- + +Create an empty [`vector`](./vector.md). +The `Element` type can be both a `resource` or `copyable` type. + +创建一个空的[`向量`](./vector.md)。 +`Element`类型可以是`资源`或`可复制`类型。 + +```move + native public fun empty(): vector; +``` + +--------------------------------------------------------------------------- + +Create a vector of length `1` containing the passed in `element`. + +创建一个长度为`1`的vector,并且包含传入的`element`。 + +```move + public fun singleton(e: Element): vector; +``` + +--------------------------------------------------------------------------- + +Destroy (deallocate) the vector `v`. Will abort if `v` is non-empty. +*Note*: The emptiness restriction is due to the fact that `Element` can be a +resource type, and destruction of a non-empty vector would violate +[resource conservation](./structs-and-resources.md). + +销毁(释放)向量`v`。如果`v`非空操作将终止。 +*注意*:空的限制是由于`Element`可以是资源类型,而销毁非空的向量会违反[资源保护机制](./structs-and-resources.md)。 + +```move + native public fun destroy_empty(v: vector); +``` + +--------------------------------------------------------------------------- + +Acquire an [immutable reference](./references.md) to the `i`th element of the vector `v`. Will abort if +the index `i` is out of bounds for the vector `v`. + +获取向量`v`的第`i`个元素的[不可变引用](./references.md)。如果索引`i`超出了向量`v`的范围,操作将会终止。 + +```move + native public fun borrow(v: &vector, i: u64): ∈ +``` + +--------------------------------------------------------------------------- + +Acquire a [mutable reference](./references.md) +to the `i`th element of the vector `v`. Will abort if +the index `i` is out of bounds for the vector `v`. + +获取向量`v`的第`i`个元素的[可变引用](./references.md)。如果索引`i`超出了向量`v`的范围,操作将会终止。 + +```move + native public fun borrow_mut(v: &mut vector, i: u64): &mut Element; +``` + +--------------------------------------------------------------------------- + +Empty and destroy the `other` vector, and push each of the elements in +the `other` vector onto the `lhs` vector in the same order as they occurred in `other`. + +清空并销毁`other`动态数组,并将`other`向量中的每个元素按顺序添加到`lhs`动态数组。 + +```move + public fun append(lhs: &mut vector, other: vector); +``` + +--------------------------------------------------------------------------- + +Push an element `e` of type `Element` onto the end of the vector `v`. May +trigger a resizing of the underlying vector's memory. + +将类型为`Element`的元素`e`添加到向量`v`的末尾。可能触发底层向量内存的大小调整。 + +```move + native public fun push_back(v: &mut vector, e: Element); +``` + +--------------------------------------------------------------------------- + +Pop an element from the end of the vector `v` in-place and return the owned +value. Will abort if `v` is empty. + +从向量`v`的末尾取出一个元素并返回。如果`v`为空将终止操作。 + +```move + native public fun pop_back(v: &mut vector): Element; +``` + +--------------------------------------------------------------------------- + +Remove the element at index `i` in the vector `v` and return the owned value +that was previously stored at `i` in `v`. All elements occurring at indices +greater than `i` will be shifted down by 1. Will abort if `i` is out of bounds +for `v`. + +移除向量`v`中索引`i`处的元素,并返回之前存储在`v`中的`i`处的值。所有下标大于`i`的元素将向前移动1个位置。如果`i`超出了`v`的范围,操作将会终止。 + +```move + public fun remove(v: &mut vector, i: u64): Element; +``` + +--------------------------------------------------------------------------- + +Swap the `i`th element of the vector `v` with the last element and then pop +this element off of the back of the vector and return the owned value that +was previously stored at index `i`. +This operation is O(1), but does not preserve ordering of elements in the vector. +Aborts if the index `i` is out of bounds for the vector `v`. + +将向量`v`的第`i`个元素与最后一个元素交换,然后将这个元素从向量的后面取出,并返回之前存储在索引`i`处的所有元素的值。 +这个操作时间复杂度是O(1),但是不保持向量容器中元素的顺序。 +如果索引`i`超出了向量`v`的边界,则操作终止。 + +```move + public fun swap_remove(v: &mut vector, i: u64): Element; +``` + +--------------------------------------------------------------------------- + +Swap the elements at the `i`'th and `j`'th indices in the vector `v`. Will +abort if either of `i` or `j` are out of bounds for `v`. + +交换向量`v`中下标为第`i`和第`j`的元素。如果`i`或`j`中的任何一个超出了`v`的范围,则操作将终止。 + +```move + native public fun swap(v: &mut vector, i: u64, j: u64); +``` + +--------------------------------------------------------------------------- + +Reverse the order of the elements in the vector `v` in-place. + +将向量v中的元素顺序颠倒。 + +```move + public fun reverse(v: &mut vector); +``` + +--------------------------------------------------------------------------- + +Return the index of the first occurrence of an element in `v` that is +equal to `e`. Returns `(true, index)` if such an element was found, and +`(false, 0)` otherwise. + +返回`v`中第一个与`e`相等的元素的索引。如果找到这样的元素,则返回`(true, index)`,否则返回`(false, 0)`。 + +```move + public fun index_of(v: &vector, e: &Element): (bool, u64); +``` + +--------------------------------------------------------------------------- +Return if an element equal to `e` exists in the vector `v`. + +如果向量`v`中存在等于`e`的元素,则返回true, 否则返回false。 + +```move + public fun contains(v: &vector, e: &Element): bool; +``` + +--------------------------------------------------------------------------- +Return the length of a `vector`. + +返回`向量`的长度。 + +```move + native public fun length(v: &vector): u64; +``` + +--------------------------------------------------------------------------- +Return whether the vector `v` is empty. + +如果向量`v`中没有元素,则返回true, 否则返回false。 + +```move + public fun is_empty(v: &vector): bool; +``` + +--------------------------------------------------------------------------- + +## 选项(option) + +The `option` module defines a generic option type `Option` that represents a +value of type `T` that may, or may not, be present. It is published under the named address `Std`. + +`option`模块定义了一个泛型option类型`Option`,它表示类型为`T`的值可能存在,也可能不存在。它发布在命名地址`Std`下。 + +The Move option type is internally represented as a singleton vector, and may +contain a value of `resource` or `copyable` kind. If you are familiar with option +types in other languages, the Move `Option` behaves similarly to those with a +couple notable exceptions since the option can contain a value of kind `resource`. +Particularly, certain operations such as `get_with_default` and +`destroy_with_default` require that the element type `T` be of `copyable` kind. + +Move option类型在内部表示为一个单例向量,可能包含`资源`或`可复制`类型的值。如果你熟悉其他语言中的option类型,Move `Option`的行为与那些类似,但有几个显著的例外,因为option可以包含一个类型为`资源`的值。 +特别地,某些操作如`get_with_default`和`destroy_with_default`要求元素类型`T`为`可复制`类型。 + +The API for the `option` module is as as follows + +`option`模块的API如下所示: + +### 类型(Types) + +Generic type abstraction of a value that may, or may not, be present. Can contain +a value of either `resource` or `copyable` kind. + +一个值的泛型类型的抽象,可能存在,也可能不存在。它可以包含`资源`或`可复制`类型的值。 + +```move + struct Option; +``` + +### 函数(Functions) + +Create an empty `Option` of that can contain a value of `Element` type. + +创建一个可以包含`Element`类型值的空`Option`。 + +```move + public fun none(): Option; +``` + +--------------------------------------------------------------------------- + +Create a non-empty `Option` type containing a value `e` of type `Element`. + +创建一个非空的`Option`类型,包含类型为`Element`的值`e`。 + +```move + public fun some(e: T): Option; +``` + +--------------------------------------------------------------------------- + +Return an immutable reference to the value inside the option `opt_elem` +Will abort if `opt_elem` does not contain a value. + +返回`opt_elem`内部值的不可变引用,如果`opt_elem`不包含值,则将终止操作。 + +```move + public fun borrow(opt_elem: &Option): ∈ +``` + +--------------------------------------------------------------------------- + +Return a reference to the value inside `opt_elem` if it contains one. If +`opt_elem` does not contain a value the passed in `default_ref` reference will be returned. +Does not abort. + +如果`opt_elem`中包含值,则返回该值的引用。如果`opt_elem`不包含值,将返回传入的`default_ref`引用。不会终止操作。 + +```move + public fun borrow_with_default(opt_elem: &Option, default_ref: &Element): ∈ +``` + +--------------------------------------------------------------------------- + +Return a mutable reference to the value inside `opt_elem`. Will abort if +`opt_elem` does not contain a value. + +返回`opt_elem`内部值的可变引用。如果`opt_elem`不包含值,则操作将终止。 + +```move + public fun borrow_mut(opt_elem: &mut Option): &mut Element; +``` + +--------------------------------------------------------------------------- + +Convert an option value that contains a value to one that is empty in-place by +removing and returning the value stored inside `opt_elem`. +Will abort if `opt_elem` does not contain a value. + +通过删除并返回存储在`opt_elem`中的值,将包含值的`opt_elem`转换为空option类型。 +如果`opt_elem`不包含值,则将终止。 + +```move + public fun extract(opt_elem: &mut Option): Element; +``` + +--------------------------------------------------------------------------- + +Return the value contained inside the option `opt_elem` if it contains one. +Will return the passed in `default` value if `opt_elem` does not contain a +value. The `Element` type that the `Option` type is instantiated with must be +of `copyable` kind in order for this function to be callable. + +如果`opt_elem`中包含值,则返回该值。 +如果`opt_elem`不包含值,将返回传入的`default`值。`default`类型必须是`可复制`类型,这样该函数才能被调用。 + +```move + public fun get_with_default(opt_elem: &Option, default: Element): Element; +``` + +--------------------------------------------------------------------------- + +Convert an empty option `opt_elem` to an option value that contains the value `e`. +Will abort if `opt_elem` already contains a value. + +将空option类型`opt_elem`转换为包含值`e`的option类。 +如果`opt_elem`已经包含值,则操作将终止。 + +```move + public fun fill(opt_elem: &mut Option, e: Element); +``` + +--------------------------------------------------------------------------- + +Swap the value currently contained in `opt_elem` with `new_elem` and return the +previously contained value. Will abort if `opt_elem` does not contain a value. + +将`opt_elem`当前包含的值与`new_elem`交换,并返回先前包含的值。如果`opt_elem`不包含值,则操作将终止。 + +```move + public fun swap(opt_elem: &mut Option, e: Element): Element; +``` + +--------------------------------------------------------------------------- + +Return true if `opt_elem` contains a value equal to the value of `e_ref`. +Otherwise, `false` will be returned. + +如果`opt_elem`包含一个等于`e_ref`的值,则返回`true`。否则,将返回`false`。 + +```move + public fun contains(opt_elem: &Option, e_ref: &Element): bool; +``` + +--------------------------------------------------------------------------- + +Return `true` if `opt_elem` does not contain a value. + +如果`opt_elem`不包含值,则返回`true`。 + +```move + public fun is_none(opt_elem: &Option): bool; +``` + +--------------------------------------------------------------------------- + +Return `true` if `opt_elem` contains a value. + +如果`opt_elem`包含值,则返回`true`。 + +```move + public fun is_some(opt_elem: &Option): bool; +``` + +--------------------------------------------------------------------------- + +Unpack `opt_elem` and return the value that it contained. +Will abort if `opt_elem` does not contain a value. + +解包`opt_elem`并返回它所包含的值。 +如果`opt_elem`不包含值,则操作将终止。 + +```move + public fun destroy_some(opt_elem: Option): Element; +``` + +--------------------------------------------------------------------------- + +Destroys the `opt_elem` value passed in. If `opt_elem` contained a value it +will be returned otherwise, the passed in `default` value will be returned. + +销毁传入的`opt_elem`。如果`opt_elem`包含值,它将被返回,否则将返回传入的`default`值。 + +```move + public fun destroy_with_default(opt_elem: Option, default: Element): Element; +``` + +--------------------------------------------------------------------------- + +Destroys the `opt_elem` value passed in, `opt_elem` must be empty and not +contain a value. Will abort if `opt_elem` contains a value. + +销毁传入的`opt_elem`,`opt_elem`必须为空且不包含值。如果`opt_elem`包含一个值,则会终止操作。 + +```move + public fun destroy_none(opt_elem: Option); +``` + +## 错误(errors) + +Recall that each abort code in Move is represented as an unsigned 64-bit integer. The `errors` module defines a common interface that can be used to "tag" each of these abort codes so that they can represent both the error **category** along with an error **reason**. + +回想一下,Move中的每个终止代码都表示为无符号64位整数。`errors`模块定义了一个通用接口,可用于"标记"每个终止代码,以便它们既可以表示错误**类别**,也可以表示错误**原因**。 + +Error categories are declared as constants in the `errors` module and are globally unique with respect to this module. Error reasons on the other hand are module-specific error codes, and can provide greater detail (perhaps, even a particular _reason_) about the specific error condition. This representation of a category and reason for each error code is done by dividing the abort code into two sections. + +错误类别在`errors`模块中声明为常量,并且对该模块来说是全局唯一的。另一方面,错误原因是特定于模块的错误代码,可以提供关于特定错误条件的更详细的信息(甚至可能是一个特定的_reason_)。每个错误代码的类别和原因的这种表示是通过将终止代码分成两部分来完成的。 + +The lower 8 bits of the abort code hold the *error category*. The remaining 56 bits of the abort code hold the *error reason*. +The reason should be a unique number relative to the module which raised the error and can be used to obtain more information about the error at hand. It should mostly be used for diagnostic purposes as error reasons may change over time if the module is updated. + +| Category | Reason | +|----------|--------| +| 8 bits | 56 bits| + +Since error categories are globally stable, these present the most stable API and should in general be what is used by clients to determine the messages they may present to users (whereas the reason is useful for diagnostic purposes). There are public functions in the `errors` module for creating an abort code of each error category with a specific `reason` number (represented as a `u64`). + +终止代码的较低8位保存*错误类别*。终止代码的其余56位包含*错误原因*。 +原因应该是相对于引发错误的模块的唯一数字,并且可以用来获取关于当前错误的更多信息。它应该主要用于诊断目的,因为如果模块更新,错误原因可能会随着时间的推移而变化。 + +| 类型 | 原因 | +|----------|--------| +| 8 bits | 56 bits| + +由于错误类别是全局稳定的,所以它们提供了稳定的API,通常应该由客户端用来确定它们可能向用户提供的消息(而原因则用于诊断目的)。在`errors`模块中有一些公共函数,用于创建每个错误类别的带有特定`原因`号的终止代码(表示为`u64`)。 + +### 常量(Constants) + +The system is in a state where the performed operation is not allowed. + +系统处于不允许操作的状态。 + +```move + const INVALID_STATE: u8 = 1; +``` + +--------------------------------------------------------------------------- +A specific account address was required to perform an operation, but a different address from what was expected was encounterd. + +执行操作需要一个特定的帐户地址,但遇到的地址与预期的不同。 + +```move + const REQUIRES_ADDRESS: u8 = 2; +``` + +--------------------------------------------------------------------------- +An account did not have the expected role for this operation. Useful for Role Based Access Control (RBAC) error conditions. + +帐户没有此操作的预期角色。用于基于角色访问控制(RBAC)错误。 + +```move + const REQUIRES_ROLE: u8 = 3; +``` + +--------------------------------------------------------------------------- +An account did not not have a required capability. Useful for RBAC error conditions. + +帐户没有所需的能力。用于RBAC错误。 + +```move + const REQUIRES_CAPABILITY: u8 = 4; +``` + +--------------------------------------------------------------------------- +A resource was expected, but did not exist under an address. + +地址下不存在期望的资源。 + +```move + const NOT_PUBLISHED: u8 = 5; +``` + +--------------------------------------------------------------------------- +Attempted to publish a resource under an address where one was already published. + +试图在已发布资源的地址发布资源。 + +```move + const ALREADY_PUBLISHED: u8 = 6; +``` + +--------------------------------------------------------------------------- +An argument provided for an operation was invalid. + +为操作提供的参数无效。 + +```move + const INVALID_ARGUMENT: u8 = 7; +``` + +--------------------------------------------------------------------------- +A limit on a value was exceeded. + +超过了一个值的限制。 + +```move + const LIMIT_EXCEEDED: u8 = 8; +``` + +--------------------------------------------------------------------------- +An internal error (bug) has occurred. + +发生了内部错误(bug)。 + +```move + const INTERNAL: u8 = 10; +``` + +--------------------------------------------------------------------------- +A custom error category for extension points. + +扩展自定义错误类别。 + +```move + const CUSTOM: u8 = 255; +``` + +--------------------------------------------------------------------------- + +### 函数(Functions) + + Should be used in the case where invalid (global) state is encountered. Constructs an abort code with specified `reason` and category `INVALID_STATE`. Will abort if `reason` does not fit in 56 bits. + +在遇到无效(全局)状态的情况下应使用。构造一个具有指定的`reason`和类别`INVALID_STATE`的终止代码。如果`reason`不适合56位,将会终止操作。 + +```move + public fun invalid_state(reason: u64): u64; +``` + +--------------------------------------------------------------------------- +Should be used if an account's address does not match a specific address. Constructs an abort code with specified `reason` and category `REQUIRES_ADDRESS`. Will abort if `reason` does not fit in 56 bits. + +当账户地址与特定地址不匹配时应使用。构造一个具有指定的`reason`和类别`REQUIRES_ADDRESS`的终止代码。如果`reason`不适合56位,将会终止操作。 + +```move + public fun requires_address(reason: u64): u64; +``` + +--------------------------------------------------------------------------- +Should be used if a role did not match a required role when using RBAC. Constructs an abort code with specified `reason` and category `REQUIRES_ROLE`. Will abort if `reason` does not fit in 56 bits. + +在使用RBAC时,角色与所需角色不匹配时应使用。构造一个具有指定的`reason`和类别`REQUIRES_ROLE`的终止代码。如果`reason`不适合56位,将会终止操作。 + +```move + public fun requires_role(reason: u64): u64; +``` + +--------------------------------------------------------------------------- +Should be used if an account did not have a required capability when using RBAC. Constructs an abort code with specified `reason` and category `REQUIRES_CAPABILITY`. Should be Will abort if `reason` does not fit in 56 bits. + +在使用RBAC时,帐户没有必要的能力时应使用。构造一个具有指定的`reason`和类别`REQUIRES_CAPABILITY`的终止代码。如果`reason`不适合56位,将会终止操作。 + +```move + public fun requires_capability(reason: u64): u64; +``` + +--------------------------------------------------------------------------- +Should be used if a resource did not exist where one was expected. Constructs an abort code with specified `reason` and category `NOT_PUBLISHED`. Will abort if `reason` does not fit in 56 bits. + +在需要资源的地方不存在资源时应使用。构造一个具有指定的`reason`和类别`NOT_PUBLISHED`的终止代码。如果`reason`不适合56位,将会终止操作。 + +```move + public fun not_published(reason: u64): u64; +``` + +--------------------------------------------------------------------------- +Should be used if a resource already existed where one was about to be published. Constructs an abort code with specified `reason` and category `ALREADY_PUBLISHED`. Will abort if `reason` does not fit in 56 bits. + +要发布资源的地方已经存在资源时使用。构造一个具有指定的`reason`和类别`ALREADY_PUBLISHED`的终止代码。如果`reason`不适合56位,将会终止操作。 + +```move + public fun already_published(reason: u64): u64; +``` + +--------------------------------------------------------------------------- +Should be used if an invalid argument was passed to a function/operation. Constructs an abort code with specified `reason` and category `INVALID_ARGUMENT`. Will abort if `reason` does not fit in 56 bits. + +当向函数/操作传递无效参数时使用。构造一个具有指定的`reason`和类别`INVALID_ARGUMENT`的终止代码。如果`reason`不适合56位,将会终止操作。 + +```move + public fun invalid_argument(reason: u64): u64; +``` + +--------------------------------------------------------------------------- +Should be used if a limit on a specific value is reached, e.g., subtracting 1 from a value of 0. Constructs an abort code with specified `reason` and category `LIMIT_EXCEEDED`. Will abort if `reason` does not fit in 56 bits. + +当达到特定值的限制时应使用,例如,0减去1。构造一个具有指定的`reason`和类别`LIMIT_EXCEEDED`的终止代码。如果`reason`不适合56位,将会终止操作。 + +```move + public fun limit_exceeded(reason: u64): u64; +``` + +--------------------------------------------------------------------------- +Should be used if an internal error or bug was encountered. Constructs an abort code with specified `reason` and category `INTERNAL`. Will abort if `reason` does not fit in 56 bits. + +在遇到内部错误或错误时使用。构造一个具有指定的`reason`和类别`INTERNAL`的终止代码。如果`reason`不适合56位,将会终止操作。 + +```move + public fun internal(reason: u64): u64; +``` + +--------------------------------------------------------------------------- +Used for extension points, should be not used under most circumstances. Constructs an abort code with specified `reason` and category `CUSTOM`. Will abort if `reason` does not fit in 56 bits. + +用于扩展,大多数情况下不应使用。构造一个具有指定的`reason`和类别`CUSTOM`的终止代码。如果`reason`不适合56位,将会终止操作。 + +```move + public fun custom(reason: u64): u64; +``` + +--------------------------------------------------------------------------- + +## 32位精确定点数字(fixed_point32) + +The `fixed_point32` module defines a fixed-point numeric type with 32 integer bits and 32 fractional bits. Internally, this is represented as a `u64` integer wrapped in a struct to make a unique `fixed_point32` type. Since the numeric representation is a binary one, some decimal values may not be exactly representable, but it provides more than 9 decimal digits of precision both before and after the decimal point (18 digits total). For comparison, double precision floating-point has less than 16 decimal digits of precision, so you should be careful about using floating-point to convert these values to decimal. + +`fixed_point32`模块定义了一个具有32个整数位和32个小数位的定点数值类型。在内部,它被表示为一个`u64`整数,包装在一个结构中,形成一个唯一的`fixed_point32`类型。由于数字表示是二进制的,一些十进制值可能不能完全表示,但它在小数点之前和之后都提供了9位以上的十进制精度(总共18位)。为了进行比较,双精度浮点数的精度小于16位十进制数字,因此在使用浮点数将这些值转换为十进制时应该小心。 + +### 类型(Types) +Represents a fixed-point numeric number with 32 fractional bits. + +表示具有32个小数位的定点数字。 + +```move + struct FixedPoint32; +``` + +### 函数(Functions) + +Multiply a u64 integer by a fixed-point number, truncating any fractional part of the product. This will abort if the product overflows. + +当u64整数乘以定点数,截断乘积的任何小数部分。如果乘积溢出,该操作将终止。 + +```move + public fun multiply_u64(val: u64, multiplier: FixedPoint32): u64; +``` + +--------------------------------------------------------------------------- +Divide a u64 integer by a fixed-point number, truncating any fractional part of the quotient. This will abort if the divisor is zero or if the quotient overflows. + +当u64整数除以定点数,截断商的任何小数部分。如果除数为零或商溢出,该操作将终止。 + +```move + public fun divide_u64(val: u64, divisor: FixedPoint32): u64; +``` + +--------------------------------------------------------------------------- +Create a fixed-point value from a rational number specified by its numerator and denominator. Calling this function should be preferred for using `fixed_point32::create_from_raw_value` which is also available. This will abort if the denominator is zero. It will also abort if the numerator is nonzero and the ratio is not in the range $2^{-32}\ldots2^{32}-1$. When specifying decimal fractions, be careful about rounding errors: if you round to display $N$ digits after the decimal point, you can use a denominator of $10^N$ to avoid numbers where the very small imprecision in the binary representation could change the rounding, e.g., 0.0125 will round down to 0.012 instead of up to 0.013. + +根据分子和分母指定的有理数创建定点值。如果`fixed_point32::create_from_raw_value`函数可用,应优先使用。如果分母为零,该操作将终止。如果分子非零且比值不在$2^{-32}\ldots2^{32}-1$范围内,该操作将终止。指定小数时,请注意四舍五入错误:如果要对小数点后$N$位进行四舍五入,则可以用$10^N$做分母,这样就能避免精确度丢失问题,例如,0.0125将四舍五入到0.012而不是0.013。 + +```move + public fun create_from_rational(numerator: u64, denominator: u64): FixedPoint32; +``` + +--------------------------------------------------------------------------- +Create a fixedpoint value from a raw `u64` value. + +通过`u64`原始值创建一个定点值。 + +```move + public fun create_from_raw_value(value: u64): FixedPoint32; +``` + +--------------------------------------------------------------------------- +Returns `true` if the decimal value of `num` is equal to zero. + +如果`num`的十进制值等于0,则返回`true`。 + +```move + public fun is_zero(num: FixedPoint32): bool; +``` + +--------------------------------------------------------------------------- +Accessor for the raw `u64` value. Other less common operations, such as adding or subtracting `FixedPoint32` values, can be done using the raw values directly. + +获取`u64`原始值的方法。其他不太常见的操作,例如添加或减去`FixedPoint32`值,可以直接使用原始值来完成。 + +```move + public fun get_raw_value(num: FixedPoint32): u64; +``` + +--------------------------------------------------------------------------- diff --git a/language/documentation/book/translations/move-book-zh/src/structs-and-resources.md b/language/documentation/book/translations/move-book-zh/src/structs-and-resources.md new file mode 100644 index 0000000000..9dcf97c958 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/structs-and-resources.md @@ -0,0 +1,591 @@ +# 结构体和资源 + +A _struct_ is a user-defined data structure containing typed fields. Structs can store any +non-reference type, including other structs. + +***结构体***(struct)是包含类型字段的自定义数据结构。结构体可以存储任何非引用类型,包括其他结构。 + +We often refer to struct values as _resources_ if they cannot be copied and cannot be dropped. In +this case, resource values must have ownership transferred by the end of the function. This property +makes resources particularly well served for defining global storage schemas or for representing +important values (such as a token). + +如果结构值无法复制且无法删除,我们通常将其称为***资源***(resource)。在这种情况下,资源值必须在函数结束时转移所有权。这个属性使资源特别适合定义全局存储模式或表示重要的值(如 token)。 + +By default, structs are linear and ephemeral. By this we mean that they: cannot be copied, cannot be +dropped, and cannot be stored in global storage. This means that all values have to have ownership +transferred (linear) and the values must be dealt with by the end of the program's execution +(ephemeral). We can relax this behavior by giving the struct [abilities](./abilities.md) which allow +values to be copied or dropped and also to be stored in global storage or to define global storage +schemas. + +默认情况下,结构体是线性的和临时的。我们的意思是它们:不能被复制,不能被删除,不能被存储在全局存储中。这意味着所有值都必须转移所有权(线性),并且必须在程序执行结束时处理这些值(临时)。我们可以通过赋予结构体允许复制或删除值以及将值存储在全局存储中或定义全局存储模式的[能力](./abilities.md)来放松这种行为(减轻限制)。 + +## 定义结构体 + +Structs must be defined inside a module: + +结构体必须在模块内定义: + +```move +address 0x2 { +module m { + struct Foo { x: u64, y: bool } + struct Bar {} + struct Baz { foo: Foo, } + // ^ 注意:尾随逗号是可以的 +} +} +``` + +Structs cannot be recursive, so the following definition is invalid: + +结构体不能递归(定义),所以下面的定义是无效的: + +```move +struct Foo { x: Foo } +// ^ 错误! Foo 不能包含 Foo +``` + +As mentioned above: by default, a struct declaration is linear and ephemeral. So to allow the value +to be used with certain operations (that copy it, drop it, store it in global storage, or use it as +a storage schema), structs can be granted [abilities](./abilities.md) by annotating them with +`has `: + +如上所述:默认情况下,结构体声明是线性且临时的。因此,为了允许将值用于某些操作(复制、删除、将其存储在全局存储中或将其用作存储模式),可以通过使用 `has ` 标注它们来授予结构体[能力](./abilities.md): + +```move +address 0x2 { +module m { + struct Foo has copy, drop { x: u64, y: bool } +} +} +``` + +For more details, see the [annotating structs](./abilities.md#annotating-structs) section. + +有关更多详细信息,请参阅[标注结构体](./abilities.md#标注结构体)部分。 + +### 命名 + +Structs must start with a capital letter `A` to `Z`. After the first letter, constant names can +contain underscores `_`, letters `a` to `z`, letters `A` to `Z`, or digits `0` to `9`. + +结构体必须以大写字母 `A` 到 `Z` 开头。在第一个字母之后,结构体名称可以包含下划线 `_`、字母 `a` 到 `z`、字母 `A` 到 `Z` 或数字 `0` 到 `9`。 + +```move +struct Foo {} +struct BAR {} +struct B_a_z_4_2 {} +``` + +This naming restriction of starting with `A` to `Z` is in place to give room for future language +features. It may or may not be removed later. + +这种以 `A` 到 `Z` 开头的命名限制是为了给未来的语言特性留出空间。以后可能会也可能不会删除这个限制。 + +## 使用结构体 + +### 创建结构体 + +Values of a struct type can be created (or "packed") by indicating the struct name, followed by +value for each field: + +可以通过写明结构体名称来创建(或“打包”)结构体类型的值,然后是每个字段的值: + +```move +address 0x2 { +module m { + struct Foo has drop { x: u64, y: bool } + struct Baz has drop { foo: Foo } + + fun example() { + let foo = Foo { x: 0, y: false }; + let baz = Baz { foo: foo }; + } +} +} +``` + +If you initialize a struct field with a local variable whose name is the same as the field, you can +use the following shorthand: + +如果使用与字段名相同的局部变量初始化结构体字段,则可以使用以下简写: + +```move +let baz = Baz { foo: foo }; +// 相当于 +let baz = Baz { foo }; +``` + +This is called sometimes called "field name punning". + +这有时称为“字段名双关语”。 + +### 通过模式匹配销毁结构体 + +Struct values can be destroyed by binding or assigning them patterns. + +结构值可以通过绑定或赋值模式来销毁。 + +```move +address 0x2 { +module m { + struct Foo { x: u64, y: bool } + struct Bar { foo: Foo } + struct Baz {} + + fun example_destroy_foo() { + let foo = Foo { x: 3, y: false }; + let Foo { x, y: foo_y } = foo; + // ^ `x: x` 的简写 + + // 两个新绑定 + // x: u64 = 3 + // foo_y: bool = false + } + + fun example_destroy_foo_wildcard() { + let foo = Foo { x: 3, y: false }; + let Foo { x, y: _ } = foo; + + // 由于 y 绑定到通配符,因此只有一个新绑定 + // x: u64 = 3 + } + + fun example_destroy_foo_assignment() { + let x: u64; + let y: bool; + Foo { x, y } = Foo { x: 3, y: false }; + + // 改变现有变量 x 和 y + // x = 3, y = false + } + + fun example_foo_ref() { + let foo = Foo { x: 3, y: false }; + let Foo { x, y } = &foo; + + // 两个新绑定 + // x: &u64 + // y: &bool + } + + fun example_foo_ref_mut() { + let foo = Foo { x: 3, y: false }; + let Foo { x, y } = &mut foo; + + // 两个新绑定 + // x: &mut u64 + // y: &mut bool + } + + fun example_destroy_bar() { + let bar = Bar { foo: Foo { x: 3, y: false } }; + let Bar { foo: Foo { x, y } } = bar; + // ^ 嵌套模式 + + // 两个新绑定 + // x: u64 = 3 + // y: bool = false + } + + fun example_destroy_baz() { + let baz = Baz {}; + let Baz {} = baz; + } +} +} +``` + +### 借用结构体和字段 + +The `&` and `&mut` operator can be used to create references to structs or fields. These examples +include some optional type annotations (e.g., `: &Foo`) to demonstrate the type of operations. + +`&` 和 `&mut` 运算符可用于创建对结构体或字段的引用。这些例子包括一些可选的类型标注(例如,`: &Foo`)来演示操作的类型。 + +```move +let foo = Foo { x: 3, y: true }; +let foo_ref: &Foo = &foo; +let y: bool = foo_ref.y; // 通过对结构体的引用读取字段 +let x_ref: &u64 = &foo.x; + +let x_ref_mut: &mut u64 = &mut foo.x; +*x_ref_mut = 42; // 通过可变引用修改字段 +``` + +It is possible to borrow inner fields of nested structs. + +可以借用嵌套结构体的内部字段: + +```move +let foo = Foo { x: 3, y: true }; +let bar = Bar { foo }; + +let x_ref = &bar.foo.x; +``` + +You can also borrow a field via a reference to a struct. + +你还可以通过对结构体的引用来借用字段: + +```move +let foo = Foo { x: 3, y: true }; +let foo_ref = &foo; +let x_ref = &foo_ref.x; +// 这与 let x_ref = &foo.x 的效果相同 +``` + +### 读写字段 + +If you need to read and copy a field's value, you can then dereference the borrowed field + +如果你需要读取和复制字段的值,则可以解引用借用的字段: + +```move +let foo = Foo { x: 3, y: true }; +let bar = Bar { foo: copy foo }; +let x: u64 = *&foo.x; +let y: bool = *&foo.y; +let foo2: Foo = *&bar.foo; +``` + +If the field is implicitly copyable, the dot operator can be used to read fields of a struct without +any borrowing. (Only scalar values with the `copy` ability are implicitly copyable.) + +如果该字段是隐式可复制的,则点运算符可用于读取结构体的字段而无需任何借用。(只有具有 `copy` 能力的标量值是隐式可复制的。) + +```move +let foo = Foo { x: 3, y: true }; +let x = foo.x; // x == 3 +let y = foo.y; // y == true +``` + +Dot operators can be chained to access nested fields. + +点运算符可以链式访问嵌套字段: + +```move +let baz = Baz { foo: Foo { x: 3, y: true } }; +let x = baz.foo.x; // x = 3; +``` + +However, this is not permitted for fields that contain non-primitive types, such a vector or another +struct + +但是,对于包含非原始类型(例如向量或其他结构体)的字段,这是不允许的: + +```move +let foo = Foo { x: 3, y: true }; +let bar = Bar { foo }; +let foo2: Foo = *&bar.foo; +let foo3: Foo = bar.foo; // 错误!必须使用 *& 添加显式复制 +``` + +The reason behind this design decision is that copying a vector or another struct might be an +expensive operation. It is important for a programmer to be aware of this copy and make others aware +with the explicit syntax `*&` + +In addition reading from fields, the dot syntax can be used to modify fields, regardless of the +field being a primitive type or some other struct + +这个设计决策背后的原因是复制一个向量或另一个结构体可能是一项昂贵的操作。对于程序员来说,了解这个复制(操作)并使用显式语法 `*&` 让其他人意识到是很重要的。 + +除了从字段中读取之外,点语法还可用于修改字段,无论该字段是原始类型还是其他结构体。 + +```move +let foo = Foo { x: 3, y: true }; +foo.x = 42; // foo = Foo { x: 42, y: true } +foo.y = !foo.y; // foo = Foo { x: 42, y: false } +let bar = Bar { foo }; // bar = Bar { foo: Foo { x: 42, y: false } } +bar.foo.x = 52; // bar = Bar { foo: Foo { x: 52, y: false } } +bar.foo = Foo { x: 62, y: true }; // bar = Bar { foo: Foo { x: 62, y: true } } +``` + +The dot syntax also works via a reference to a struct + +点语法也适用于对结构体的引用: + +```move +let foo = Foo { x: 3, y: true }; +let foo_ref = &mut foo; +foo_ref.x = foo_ref.x + 1; +``` + +## 私有结构体操作 + +Most struct operations on a struct type `T` can only be performed inside the module that declares +`T`: + +- Struct types can only be created ("packed"), destroyed ("unpacked") inside the module that defines + the struct. +- The fields of a struct are only accessible inside the module that defines the struct. + +Following these rules, if you want to modify your struct outside the module, you will need to +provide public APIs for them. The end of the chapter contains some examples of this. + +However, struct _types_ are always visible to another module or script: + +大多数对结构体类型 `T` 的结构体操作只能在声明 `T` 的模块内执行: + +- 结构体类型只能在定义结构体的模块内创建(“打包”)、销毁(“解包”)。 +- 结构体的字段只能在定义结构体的模块内部访问。 + +按照这些规则,如果你想在模块之外修改你的结构体,你需要为他们提供公共 API。本章的最后包含了这方面的一些例子。 + +但是,结构体类型始终对其他模块或脚本可见: + +```move +// m.move +address 0x2 { +module m { + struct Foo has drop { x: u64 } + + public fun new_foo(): Foo { + Foo { x: 42 } + } +} +} +``` + +```move +// n.move +address 0x2 { +module n { + use 0x2::m; + + struct Wrapper has drop { + foo: m::Foo + } + + fun f1(foo: m::Foo) { + let x = foo.x; + // ^ 错误!此处无法访问 `foo` 的字段 + } + + fun f2() { + let foo_wrapper = Wrapper { foo: m::new_foo() }; + } +} +} +``` + +Note that structs do not have visibility modifiers (e.g., `public` or `private`). + +请注意,结构体没有可见性修饰符(例如,`public` 或 `private`)。 + +## 所有权 + +As mentioned above in [Defining Structs](#defining-structs), structs are by default linear and +ephemeral. This means they cannot be copied or dropped. This property can be very useful when +modeling real world resources like money, as you do not want money to be duplicated or get lost in +circulation. +正如上面[定义结构体](#定义结构体)中提到的,结构体默认是线性的和临时的。这意味着它们不能被复制或删除。在模拟货币等现实世界资源时,此属性非常有用,因为你不希望货币被复制或在流通中丢失。 + +```move +address 0x2 { +module m { + struct Foo { x: u64 } + + public fun copying_resource() { + let foo = Foo { x: 100 }; + let foo_copy = copy foo; // 错误!“复制”需要“复制”能力 + let foo_ref = &foo; + let another_copy = *foo_ref // 错误!解引用需要“复制”能力 + } + + public fun destroying_resource1() { + let foo = Foo { x: 100 }; + + // 错误!当函数返回时,foo 仍然包含一个值。 + // 这种销毁需要“drop”能力 + } + + public fun destroying_resource2(f: &mut Foo) { + *f = Foo { x: 100 } // 错误!通过写入销毁旧值需要“drop”能力 + } +} +} +``` + +To fix the second example (`fun dropping_resource`), you would need to manually "unpack" the resource: + +要修复第二个示例(`fun destroying_resource1`),你需要手动“解包”资源: + +```move +address 0x2 { +module m { + struct Foo { x: u64 } + + public fun destroying_resource1_fixed() { + let foo = Foo { x: 100 }; + let Foo { x: _ } = foo; + } +} +} +``` + +Recall that you are only able to deconstruct a resource within the module in which it is defined. +This can be leveraged to enforce certain invariants in a system, for example, conservation of money. + +If on the other hand, your struct does not represent something valuable, you can add the abilities +`copy` and `drop` to get a struct value that might feel more familiar from other programming +languages: + +回想一下,你只能在定义资源的模块中解构资源。这可以用来在系统中强制执行某些不变量,例如货币守恒。 + +另一方面,如果你的结构体不代表有价值的东西,你可以添加 `copy` 和 `drop` 能力来获取一个结构值,这感觉可能会与其他编程语言更相似。 + +```move +address 0x2 { +module m { + struct Foo has copy, drop { x: u64 } + + public fun run() { + let foo = Foo { x: 100 }; + let foo_copy = copy foo; + // ^ 此代码复制 foo,而 `let x = foo` 或 + // `let x = move foo` 都移动 foo + + let x = foo.x; // x = 100 + let x_copy = foo_copy.x; // x = 100 + + // 函数返回时 foo 和 foo_copy 都被隐式丢弃 + } +} +} +``` + +## 在全局存储中存储资源 + +Only structs with the `key` ability can be saved directly in +[persistent global storage](./global-storage-operators.md). All values stored within those `key` +structs must have the `store` abilities. See the [ability](./abilities] and +[global storage](./global-storage-operators.md) chapters for more detail. + +只有具有 `key` 能力的结构体才能直接保存在[持久性全局存储](./global-storage-operators.md)中。存储在这些 `key` 结构体中的所有值都必须具有 `store` 能力。有关更多详细信息,请参阅[能力](./abilities.md)和[全局存储](./global-storage-operators.md)章节。 + +## 示例 + +Here are two short examples of how you might use structs to represent valuable data (in the case of +`Coin`) or more classical data (in the case of `Point` and `Circle`) + +这里有两个简短的示例,说明如何使用结构体来表示有价值的数据(在 `Coin` 的情况下)或更经典的数据(在 `Point` 和 `Circle` 的情况下)。 + +### 示例 1:Coin + + + +```move +address 0x2 { +module m { + // 我们不希望钱币(Coin)被复制,因为那会复制这个“钱”, + // 所以我们不会给结构体“copy”能力。 + // 同样,我们不希望程序员销毁钱币,所以我们不会给结构体“drop”能力。 + // 但是,我们*希望*模块的用户能够将这个钱币存储在持久的全局存储中, + // 所以我们授予结构体“store”能力。 + // 这个结构体只会在全局存储内的其他资源中,因此我们不会赋予该结构体“key”能力。 + struct Coin has store { + value: u64, + } + + public fun mint(value: u64): Coin { + // 你可能希望通过某种形式的访问控制来关闭此(铸币)功能,以防止使用此模块的任何人铸造无限数量的钱币。 + Coin { value } + } + + public fun withdraw(coin: &mut Coin, amount: u64): Coin { + assert!(coin.balance >= amount, 1000); + coin.value = coin.value - amount; + Coin { value: amount } + } + + public fun deposit(coin: &mut Coin, other: Coin) { + let Coin { value } = other; + coin.value = coin.value + value; + } + + public fun split(coin: Coin, amount: u64): (Coin, Coin) { + let other = withdraw(&mut coin, amount); + (coin, other) + } + + public fun merge(coin1: Coin, coin2: Coin): Coin { + deposit(&mut coin1, coin2); + coin1 + } + + public fun destroy_zero(coin: Coin) { + let Coin { value } = coin; + assert!(value == 0, 1001); + } +} +} +``` + +### 示例 2:Geometry + +```move +address 0x2 { +module point { + struct Point has copy, drop, store { + x: u64, + y: u64, + } + + public fun new(x: u64, y: u64): Point { + Point { + x, y + } + } + + public fun x(p: &Point): u64 { + p.x + } + + public fun y(p: &Point): u64 { + p.y + } + + fun abs_sub(a: u64, b: u64): u64 { + if (a < b) { + b - a + } + else { + a - b + } + } + + public fun dist_squared(p1: &Point, p2: &Point): u64 { + let dx = abs_sub(p1.x, p2.x); + let dy = abs_sub(p1.y, p2.y); + dx*dx + dy*dy + } +} +} +``` + +```move +address 0x2 { +module circle { + use 0x2::point::{Self, Point}; + + struct Circle has copy, drop, store { + center: Point, + radius: u64, + } + + public fun new(center: Point, radius: u64): Circle { + Circle { center, radius } + } + + public fun overlaps(c1: &Circle, c2: &Circle): bool { + let d = point::dist_squared(&c1.center, &c2.center); + let r1 = c1.radius; + let r2 = c2.radius; + d*d <= r1*r1 + 2*r1*r2 + r2*r2 + } +} +} +``` diff --git a/language/documentation/book/translations/move-book-zh/src/tuples.md b/language/documentation/book/translations/move-book-zh/src/tuples.md new file mode 100644 index 0000000000..b3182e8c2a --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/tuples.md @@ -0,0 +1,117 @@ +# 元组和单值 + +Move 不完全支持元组,因为人们可能期望像来自另一种语言的元组一样将它们作为[头等值(即头等公民)](https://zh.wikipedia.org/wiki/%E9%A0%AD%E7%AD%89%E7%89%A9%E4%BB%B6)。但是,为了支持多个返回值,Move 具有类似元组的表达式。这些表达式在运行时不会产生具体的值(字节码中没有元组),因此它们非常有限:它们只能出现在表达式中(通常在函数的返回位置);它们不能绑定到局部变量;它们不能存储在结构中;元组类型不能用于实例化泛型。 + +类似地,[单值(unit)`()`](https://zh.wikipedia.org/wiki/%E5%8D%95%E5%80%BC%E7%B1%BB%E5%9E%8B) 是 Move 源语言创建的一种以表达式为基础的类型。单值 `()` 不会产生任何运行时值。我们可以认为单值 `()` 是一个空元组,适用于元组的任何限制也适用于单值。 + +考虑到这些限制,在语言中使用元组可能会感觉很奇怪。但其他语言中,元组最常见的用例之一是函数 —— 允许函数返回多个值。一些语言通过强制用户编写包含多个返回值的结构来解决这个问题。但是在 Move 中,您不能将引用放在[结构体](./structs-and-resources.md)中。这需要 Move 支持多个返回值。这些多个返回值都在字节码级别被压入到堆栈中。在源码级别,这些多个返回值使用元组表示。 + +## 字面量 + +元组(tuple)是由括号内以逗号分隔的表达式列表创建的。 + +| 语法 | 类型 | 描述 | +|-----------------|-----------------------------------------------------------------------------|-----------------------------------------| +| `()` | `(): ()` | 单值、空元组或 0 元素元组 | +| `(e1, ..., en)` | `(e1, ..., en): (T1, ..., Tn)` 其中 `e_i: Ti` 满足 `0 < i <= n` and `n > 0` | n 元组、n 元素元组、带有 n 个元素的元组 | + +注意 `(e)` 没有类型 `(e): (t)`,换句话说,没有一个元素的元组。如果括号内只有一个元素,则括号仅用于消除歧义,不带有任何其他特殊含义。 + +有时,具有两个元素的元组称为“二元组(pairs)”,而具有三个元素的元组称为“三元组(triples)”。 + +### 例子 + +```move +address 0x42 { +module example { + // 这三个函数都是等价的 + + // 当没有提供返回类型时,假定为 `()` + fun returns_unit_1() { } + + // 空表达式块中存在隐式 () 值 + fun returns_unit_2(): () { } + + // `returns_unit_1` 和 `returns_unit_2` 的显式版本 + fun returns_unit_3(): () { () } + + + fun returns_3_values(): (u64, bool, address) { + (0, false, @0x42) + } + fun returns_4_values(x: &u64): (&u64, u8, u128, vector) { + (x, 0, 1, b"foobar") + } +} +} +``` + +## 操作 + +目前唯一可以对元组执行的操作是解构(destructuring)。 + +### 解构 + +对于任何大小的元组,它们可以在 `let` 绑定或赋值中被解构。 + +例如: + +```move +address 0x42 { +module example { + // 这三个函数都是等价的 + fun returns_unit() {} + fun returns_2_values(): (bool, bool) { (true, false) } + fun returns_4_values(x: &u64): (&u64, u8, u128, vector) { (x, 0, 1, b"foobar") } + + fun examples(cond: bool) { + let () = (); + let (x, y): (u8, u64) = (0, 1); + let (a, b, c, d) = (@0x0, 0, false, b""); + + () = (); + (x, y) = if (cond) (1, 2) else (3, 4); + (a, b, c, d) = (@0x1, 1, true, b"1"); + } + + fun examples_with_function_calls() { + let () = returns_unit(); + let (x, y): (bool, bool) = returns_2_values(); + let (a, b, c, d) = returns_4_values(&0); + + () = returns_unit(); + (x, y) = returns_2_values(); + (a, b, c, d) = returns_4_values(&1); + } +} +} +``` + +有关更多详细信息,请参阅 [Move 变量](./variables.md)。 + +## 子类型 + +除了引用,元组是唯一在 Move 中具有[子类型(subtyping)](https://zh.wikipedia.org/wiki/%E5%AD%90%E7%B1%BB%E5%9E%8B)的类型。元组只有在具有引用的子类型(以协变方式)的意义上才具有子类型。 + +例如: + +```move +let x: &u64 = &0; +let y: &mut u64 = &mut 1; + +// (&u64, &mut u64) 是 (&u64, &u64) 的子类型 +// 因为 &mut u64 是 &u64 的子类型 +let (a, b): (&u64, &u64) = (x, y); + +// (&mut u64, &mut u64) 是 (&u64, &u64) 的子类型 +// 因为 &mut u64 是 &u64 的子类型 +let (c, d): (&u64, &u64) = (y, y); + +// 错误!(&u64, &mut u64) 不是 (&mut u64, &mut u64) 的子类型 +// 因为 &u64 不是 &mut u64 的子类型 +let (e, f): (&mut u64, &mut u64) = (x, y); +``` + +## 所有权 + +如上所述,元组值在运行时并不真正存在。由于这个原因,目前它们不能存储到局部变量中(但这个功能很可能很快就会出现)。因此,元组目前只能移动,因为复制它们需要先将它们放入局部变量中。 diff --git a/language/documentation/book/translations/move-book-zh/src/unit-testing.md b/language/documentation/book/translations/move-book-zh/src/unit-testing.md new file mode 100644 index 0000000000..1e8f8abe2c --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/unit-testing.md @@ -0,0 +1,367 @@ +# 单元测试 (Unit Tests) + +Unit testing for Move adds three new annotations to the Move source language: + +Move 语言中存在三种单元测试标注: + +* `#[test]` +* `#[test_only]`, and +* `#[expected_failure]`. + +They respectively mark a function as a test, mark a module or module member (`use`, function, or struct) as code to be included for testing only, and mark that a test is expected to fail. These annotations can be placed on a function with any visibility. Whenever a module or module member is annotated as `#[test_only]` or `#[test]`, it will not be included in the compiled bytecode unless it is compiled for testing. + +它们分别把函数、模块或模块成员(`use` 声明,函数 function,或结构体 struct)标记为只用于测试的代码,同时也标记期望失败的测试。这些标注可以用在任何可见性(visibility)函数上。无论何种情况,被标注为 `#[test_only]` 或 `#[test]` 的模块或模块成员除非用于测试,其它情况都不会被编译成字节码。 + +## 测试注解:含义和使用方法(Testing Annotations: Their Meaning and Usage) + +Both the `#[test]` and `#[expected_failure]` annotations can be used either with or without arguments. + +`#[test]` 和 `#[expected_failure]` 两个注解均可以在有、无参数情况下使用。 + +Without arguments, the `#[test]` annotation can only be placed on a function with no parameters. This annotation simply marks this function as a test to be run by the unit testing harness. + +没有参数的 `#[test]` 标记只能用于没有参数的函数。表示该函数作为单元测试函数被运行。 + +``` +#[test] // 正确 // OK +fun this_is_a_test() { ... } + +#[test] // 编译失败,因为函数需要参数 // Will fail to compile since the test takes an argument +fun this_is_not_correct(arg: signer) { ... } +``` + +A test can also be annotated as an `#[expected_failure]`. This annotation marks that the test should is expected to raise an error. You can ensure that a test is aborting with a specific abort code by annotating it with `#[expected_failure(abort_code = )]`, if it then fails with a different abort code or with a non-abort error the test will fail. Only functions that have the `#[test]` annotation can also be annotated as an #`[expected_failure]`. + +测试也可以使用 `#[expected_failure]` 标注,表示该函数会抛出错误。你可以使用 `#[expected_failure(abort_code = )]` 这种方式方式确保此测试会被指定错误码打断,如果抛出不同错误码或没有抛出错误测试将失败。只有被 `#[test]` 标注的函数才能使用 `#[expected_failure]` 标注。 + +``` +#[test] +#[expected_failure] +public fun this_test_will_abort_and_pass() { abort 1 } + +#[test] +#[expected_failure] +public fun test_will_error_and_pass() { 1/0; } + +#[test] +#[expected_failure(abort_code = 0)] +public fun test_will_error_and_fail() { 1/0; } + +#[test, expected_failure] // 可以合并多个属性。测试将会通过。 // Can have multiple in one attribute. This test will pass. +public fun this_other_test_will_abort_and_pass() { abort 1 } +``` + +With arguments, a test annotation takes the form `#[test( =
, ..., =
)]`. If a function is annotated in such a manner, the function's parameters must be a permutation of the parameters <`param_name_1>, ..., `, i.e., the order of these parameters as they occur in the function and their order in the test annotation do not have to be the same, but they must be able to be matched up with each other by name. + +测试标注可以采用 `#[test( =
, ..., =
)]` 这种形式指定参数。如果函数使用这样的标注,函数的参数则必须为 `, ..., ` 的形式。参数在函数中的顺序不必与注解中顺序一致,但必须要能根据参数名匹配。 + +Only parameters with a type of `signer` are supported as test parameters. If a non-`signer` parameter is supplied, the test will result in an error when run. + +只有 `signer` 类型可以用作测试参数。使用非 `signer` 类型参数,测试将会失败。 + +``` +#[test(arg = @0xC0FFEE)] // 正确 // OK +fun this_is_correct_now(arg: signer) { ... } + +#[test(wrong_arg_name = @0xC0FFEE)] // 不正确: 参数名不匹配 // Not correct: arg name doesn't match +fun this_is_incorrect(arg: signer) { ... } + +#[test(a = @0xC0FFEE, b = @0xCAFE)] // 正确,多参数情况下必须为每个参数提供值。 // OK. We support multiple signer arguments, but you must always provide a value for that argument +fun this_works(a: signer, b: signer) { ... } + +// 在某处声明一个命名地址(named address) // somewhere a named address is declared +#[test_only] // 命名地址支持 test-only 注解 // test-only named addresses are supported +address TEST_NAMED_ADDR = @0x1; +... +#[test(arg = @TEST_NAMED_ADDR)] // 支持命名地址! // Named addresses are supported! +fun this_is_correct_now(arg: signer) { ... } +``` + +An expected failure annotation can also take the form `#[expected_failure(abort_code = )]`. If a test function is annotated in such a way, the test must abort with an abort code equal to ``. Any other failure or abort code will result in a test failure. + +预期失败的标注使用 `#[expected_failure(abort_code = )]` 这种形式。如果函数被这样标注,测试错误码必须为 ``。任何其它的错误或错误码都会失败。 + +``` +#[test, expected_failure(abort_code = 1)] // 这个测试会失败 // This test will fail +fun this_test_should_abort_and_fail() { abort 0 } + +#[test] +#[expected_failure(abort_code = 0)] // 这个测试会通过 // This test will pass +fun this_test_should_abort_and_pass_too() { abort 0 } +``` + +A module and any of its members can be declared as test only. In such a case the item will only be included in the compiled Move bytecode when compiled in test mode. Additionally, when compiled outside of test mode, any non-test `use`s of a `#[test_only]` module will raise an error during compilation. + +模块和它的成员可以被声明为仅测试用。这种情况它们只会在测试模式下编译。此外,在非测试模式下,任何被 `#[test_only]` 标记的模块都会在编译时报错。 + +``` +#[test_only] // test only 属性可以用于模块 // test only attributes can be attached to modules +module abc { ... } + +#[test_only] // test only 属性可以用于命名地址 // test only attributes can be attached to named addresses +address ADDR = @0x1; + +#[test_only] // .. 用于 use 声明 // .. to uses +use 0x1::some_other_module; + +#[test_only] // .. 用于结构体 // .. to structs +struct SomeStruct { ... } + +#[test_only] // .. 用于函数。只能在测试函数中调用,但自身不是测试 // .. and functions. Can only be called from test code, but not a test +fun test_only_function(...) { ... } +``` + +## 运行单元测试(Running Unit Tests) + +Unit tests for a Move package can be run with the [`move test` +command](./packages.md). + +使用 [`move test` 命令](./packages.md)运行包中的单元测试。 + +When running tests, every test will either `PASS`, `FAIL`, or `TIMEOUT`. If a test case fails, the location of the failure along with the function name that caused the failure will be reported if possible. You can see an example of this below. + +运行测试的结果包括 `PASS`、`FAIL` 或 `TIMEOUT`。如果测试失败,将会尽可能的提供执行失败的位置及函数名信息。请看下面的例子。 + +A test will be marked as timing out if it exceeds the maximum number of instructions that can be executed for any single test. This bound can be changed using the options below, and its default value is set to 5000 instructions. Additionally, while the result of a test is always deterministic, tests are run in parallel by default, so the ordering of test results in a test run is non-deterministic unless running with only one thread (see `OPTIONS` below). + +任何测试执行超过最大数量指令限制将会标记成超时。可以通过参数调整此限制,默认值为 5000 条指令。此外,虽然测试结果是确定的,但由于测试默认并行执行,所以测试结果的顺序是不确定的,除非使用单线程模式(见下述参数)。 + +There are also a number of options that can be passed to the unit testing binary to fine-tune testing and to help debug failing tests. These can be found using the the help flag: + +存在大量参数细粒度调整测试工具的行为,帮助调试失败的测试。可以通过 help 参数查看。 + +``` +$ move -h +``` + +## 示例(Example) + +A simple module using some of the unit testing features is shown in the following example: + +下面例子展示了一个简单的使用了单元测试特性的模块: + +First create an empty package and change directory into it: + +首先创建一个空 package 进入目录: + +``` +$ move new TestExample; cd TestExample +``` + +Next add the following to the `Move.toml`: + +接下来添加下面内容到 `Move.toml` 文件: + +``` +[dependencies] +MoveStdlib = { git = "https://github.com/diem/diem.git", subdir="language/move-stdlib", rev = "56ab033cc403b489e891424a629e76f643d4fb6b", addr_subst = { "std" = "0x1" } } +``` + +Next add the following module under the `sources` directory: + +接下来在 `sources` 目录下添加下述模块: + +``` +// 文件路径: sources/my_module.move // filename: sources/my_module.move +module 0x1::my_module { + + struct MyCoin has key { value: u64 } + + public fun make_sure_non_zero_coin(coin: MyCoin): MyCoin { + assert!(coin.value > 0, 0); + coin + } + + public fun has_coin(addr: address): bool { + exists(addr) + } + + #[test] + fun make_sure_non_zero_coin_passes() { + let coin = MyCoin { value: 1 }; + let MyCoin { value: _ } = make_sure_non_zero_coin(coin); + } + + #[test] + // 如果不关心错误码也可以使用 #[expected_failure] // Or #[expected_failure] if we don't care about the abort code + #[expected_failure(abort_code = 0)] + fun make_sure_zero_coin_fails() { + let coin = MyCoin { value: 0 }; + let MyCoin { value: _ } = make_sure_non_zero_coin(coin); + } + + #[test_only] // 仅用作测试的帮助方法 // test only helper function + fun publish_coin(account: &signer) { + move_to(account, MyCoin { value: 1 }) + } + + #[test(a = @0x1, b = @0x2)] + fun test_has_coin(a: signer, b: signer) { + publish_coin(&a); + publish_coin(&b); + assert!(has_coin(@0x1), 0); + assert!(has_coin(@0x2), 1); + assert!(!has_coin(@0x3), 1); + } +} +``` + +### 运行测试(Running Tests) + +You can then run these tests with the `move test` command: + +你可以使用 `move test` 命令运行测试。 + +``` +$ move test +BUILDING MoveStdlib +BUILDING TestExample +Running Move unit tests +[ PASS ] 0x1::my_module::make_sure_non_zero_coin_passes +[ PASS ] 0x1::my_module::make_sure_zero_coin_fails +[ PASS ] 0x1::my_module::test_has_coin +Test result: OK. Total tests: 3; passed: 3; failed: 0 +``` + +### 使用测试参数(Using Test Flags) + +#### `-f ` 或 `--filter `(`-f ` or `--filter `) + +This will only run tests whose fully qualified name contains ``. For example if we wanted to only run tests with `"zero_coin"` in their name: + +仅运行名字包含 `` 字符的测试。例如只想运行名字包含 `"zero_coin"` 的测试: + + +``` +$ move test -f zero_coin +CACHED MoveStdlib +BUILDING TestExample +Running Move unit tests +[ PASS ] 0x1::my_module::make_sure_non_zero_coin_passes +[ PASS ] 0x1::my_module::make_sure_zero_coin_fails +Test result: OK. Total tests: 2; passed: 2; failed: 0 +``` + +#### `-i ` 或 `--instructions `(`-i ` or `--instructions `) + +This bounds the number of instructions that can be executed for any one test to ``: + +调整测试指令限制为 ``: + +``` +$ move test -i 0 +CACHED MoveStdlib +BUILDING TestExample +Running Move unit tests +[ TIMEOUT ] 0x1::my_module::make_sure_non_zero_coin_passes +[ TIMEOUT ] 0x1::my_module::make_sure_zero_coin_fails +[ TIMEOUT ] 0x1::my_module::test_has_coin + +Test failures: + +Failures in 0x1::my_module: + +┌── make_sure_non_zero_coin_passes ────── +│ Test timed out +└────────────────── + + +┌── make_sure_zero_coin_fails ────── +│ Test timed out +└────────────────── + + +┌── test_has_coin ────── +│ Test timed out +└────────────────── + +Test result: FAILED. Total tests: 3; passed: 0; failed: 3 +``` + +#### `-s` 或 `--statistics`(`-s` or `--statistics`) + +With these flags you can gather statistics about the tests run and report the runtime and instructions executed for each test. For example, if we wanted to see the statistics for the tests in the example above: + +使用此参数你可以得到每个测试的运行报告及执行指令的统计信息。例如查看上述示例的统计数据: + +``` +$ move test -s +CACHED MoveStdlib +BUILDING TestExample +Running Move unit tests +[ PASS ] 0x1::my_module::make_sure_non_zero_coin_passes +[ PASS ] 0x1::my_module::make_sure_zero_coin_fails +[ PASS ] 0x1::my_module::test_has_coin + +Test Statistics: + +┌────────────────────────────────────────────────┬────────────┬───────────────────────────┐ +│ Test Name │ Time │ Instructions Executed │ +├────────────────────────────────────────────────┼────────────┼───────────────────────────┤ +│ 0x1::my_module::make_sure_non_zero_coin_passes │ 0.009 │ 1 │ +├────────────────────────────────────────────────┼────────────┼───────────────────────────┤ +│ 0x1::my_module::make_sure_zero_coin_fails │ 0.008 │ 1 │ +├────────────────────────────────────────────────┼────────────┼───────────────────────────┤ +│ 0x1::my_module::test_has_coin │ 0.008 │ 1 │ +└────────────────────────────────────────────────┴────────────┴───────────────────────────┘ + +Test result: OK. Total tests: 3; passed: 3; failed: 0 +``` + +#### `-g` 或 `--state-on-error`(`-g` or `--state-on-error`) + +These flags will print the global state for any test failures. e.g., if we added the following (failing) test to the `my_module` example: + +这个参数会在测试失败情况下打印全局状态。如在 `my_module` 模块中添加下述失败测试: + +``` +module 0x1::my_module { + ... + #[test(a = @0x1)] + fun test_has_coin_bad(a: signer) { + publish_coin(&a); + assert!(has_coin(@0x1), 0); + assert!(has_coin(@0x2), 1); + } +} +``` + +we would get get the following output when running the tests: + +当运行测试时我们将得到下面的输出: + +``` +$ move test -g +CACHED MoveStdlib +BUILDING TestExample +Running Move unit tests +[ PASS ] 0x1::my_module::make_sure_non_zero_coin_passes +[ PASS ] 0x1::my_module::make_sure_zero_coin_fails +[ PASS ] 0x1::my_module::test_has_coin +[ FAIL ] 0x1::my_module::test_has_coin_bad + +Test failures: + +Failures in 0x1::my_module: + +┌── test_has_coin_bad ────── +│ error[E11001]: test failure +│ ┌─ /home/tzakian/TestExample/sources/my_module.move:47:10 +│ │ +│ 44 │ fun test_has_coin_bad(a: signer) { +│ │ ----------------- In this function in 0x1::my_module +│ · +│ 47 │ assert!(has_coin(@0x2), 1); +│ │ ^^^^^^^^^^^^^^^^^^^^^^^^^^ Test was not expected to abort but it aborted with 1 here +│ +│ +│ ────── Storage state at point of failure ────── +│ 0x1: +│ => key 0x1::my_module::MyCoin { +│ value: 1 +│ } +│ +└────────────────── + +Test result: FAILED. Total tests: 4; passed: 3; failed: 1 +``` diff --git a/language/documentation/book/translations/move-book-zh/src/uses.md b/language/documentation/book/translations/move-book-zh/src/uses.md new file mode 100644 index 0000000000..79918badf6 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/uses.md @@ -0,0 +1,376 @@ +# 使用与别名(Uses and Aliases) + +The `use` syntax can be used to create aliases to members in other modules. `use` can be used to +create aliases that last either for the entire module, or for a given expression block scope. + +`use` 语法可用于为其他模块中的成员创建别名。 `use` 可用于创建持续整个模块或给定表达式块范围的别名。 + +## 语法(Syntax) +There are several different syntax cases for `use`. Starting with the most simple, we have the +following for creating aliases to other modules + +这里有几种不同的语法案例可供使用。从最简单的开始,我们有以下例子用于为其他模块创建别名 + +```move +use
::; +use
:: as ; +``` +For example +举例 +```move +use std::vector; +use std::vector as V; +``` + +`use std::vector;` introduces an alias `vector` for `std::vector`. This means that anywhere you +would want to use the module name `std::vector` (assuming this `use` is in scope), you could use +`vector` instead. `use std::vector;` is equivalent to `use std::vector as vector;` + +`use std::vector;`为 `std::vector` 引入别名向量。这意味着在任何您想使用模块名称 `std::vector` 的地方(假设此`use`在作用域内),您都可以使用 `vector` 代替使用`std::vector`; + +Similarly `use std::vector as V;` would let you use `V` instead of `std::vector` + +同样使用 `std::vector as V`;会让你使用 `V` 代替 `std::vector` + +```move +use std::vector; +use std::vector as V; + +fun new_vecs(): (vector, vector, vector) { + let v1 = std::vector::empty(); + let v2 = vector::empty(); + let v3 = V::empty(); + (v1, v2, v3) +} +``` +If you want to import a specific module member (such as a function, struct, or constant). You can +use the following syntax. + +如果要导入特定的模块成员(例如函数、结构或常量)。您可以使用以下语法。 +```move +use
::::; +use
:::: as ; +``` +For example +举例 + +```move +use std::vector::empty; +use std::vector::empty as empty_vec; +``` + +This would let you use the function `std::vector::empty` without full qualification. Instead you +could use `empty` and `empty_vec` respectively. Again, `use std::vector::empty;` is equivalent to +`use std::vector::empty as empty;` + +这将允许您在没有前缀限定的情况下使用函数 `std::vector::empty`。相反,您可以分别使用 `empty` 和 `empty_vec`,使用 `std::vector::empty;`使用`empty` 相当于使用`std::vector::empty`; + +```move +use std::vector::empty; +use std::vector::empty as empty_vec; + +fun new_vecs(): (vector, vector, vector) { + let v1 = std::vector::empty(); + let v2 = empty(); + let v3 = empty_vec(); + (v1, v2, v3) +} +``` +If you want to add aliases for multiple module members at once, you can do so with the following +syntax + +如果要一次为多个模块成员添加别名,可以使用以下语法 +```move +use
::::{, as ... }; +``` +For example +举例 + +```move +use std::vector::{push_back, length as len, pop_back}; + +fun swap_last_two(v: &mut vector) { + assert!(len(v) >= 2, 42); + let last = pop_back(v); + let second_to_last = pop_back(v); + push_back(v, last); + push_back(v, second_to_last) +} +``` + +If you need to add an alias to the Module itself in addition to module members, you can do that in a +single `use` using `Self`. `Self` is a member of sorts that refers to the module. + +如果除了模块成员之外,您还需要为模块本身添加别名,您可以使用 `Self` 在一次`use`中完成。 `Self` 是指模块的各种成员。 +```move +use std::vector::{Self, empty}; +For clarity, all of the following are equivalent: +``` +For clarity, all of the following are equivalent: + +为清晰起见,以下所有内容都是等效的: +```move +use std::vector; +use std::vector as vector; +use std::vector::Self; +use std::vector::Self as vector; +use std::vector::{Self}; +use std::vector::{Self as vector}; +``` +If needed, you can have as many aliases for any item as you like + +如果需要,您可以为任何项目设置任意数量的别名 + +```move +use std::vector::{ + Self, + Self as V, + length, + length as len, +}; + +fun pop_twice(v: &mut vector): (T, T) { + // all options available given the `use` above + assert!(vector::length(v) > 1, 42); + assert!(V::length(v) > 1, 42); + assert!(length(v) > 1, 42); + assert!(len(v) > 1, 42); + + (vector::pop_back(v), vector::pop_back(v)) +} +``` + +## 模块内部(Inside a `module`) +Inside of a `module` all `use` declarations are usable regardless of the order of declaration. + +在模块内部,无论声明顺序如何,所有 `use` 声明都是可用的。 +```move +address 0x42 { +module example { + use std::vector; + + fun example(): vector { + let v = empty(); + vector::push_back(&mut v, 0); + vector::push_back(&mut v, 10); + v + } + + use std::vector::empty; +} +} +``` +The aliases declared by `use` in the module usable within that module. + +在该模块中可用的模块中使用声明的别名。 + +Additionally, the aliases introduced cannot conflict with other module members. See +[Uniqueness](#uniqueness) for more details + +此外,引入的别名不能与其他模块成员冲突。有关详细信息,请参阅[唯一性](#uniqueness)。 + +## 表达式内部(Inside an expression) +You can add `use` declarations to the beginning of any expression block + +您可以将 `use` 声明添加到任何表达式块的开头 +```move +address 0x42 { +module example { + + fun example(): vector { + use std::vector::{empty, push_back}; + + let v = empty(); + push_back(&mut v, 0); + push_back(&mut v, 10); + v + } +} +} +``` +As with `let`, the aliases introduced by `use` in an expression block are removed at the end of that +block. + +与 `let` 一样,在表达式块中使用 `use` 引入的别名在该块的末尾被删除。 + +```move +address 0x42 { +module example { + + fun example(): vector { + let result = { + use std::vector::{empty, push_back}; + let v = empty(); + push_back(&mut v, 0); + push_back(&mut v, 10); + v + }; + result + } + +} +} +``` +Attempting to use the alias after the block ends will result in an error + +在块结束后尝试使用别名将导致错误 +```move +fun example(): vector { + let result = { + use std::vector::{empty, push_back}; + let v = empty(); + push_back(&mut v, 0); + push_back(&mut v, 10); + v + }; + let v2 = empty(); // 错误! +// ^^^^^ 未绑定的函数 'empty' + 结果 +} +``` +Any `use` must be the first item in the block. If the `use` comes after any expression or `let`, it +will result in a parsing error + +任何使用都必须是块中的第一项。如果 use 出现在任何表达式或 let 之后,则会导致解析错误 +```move +{ + let x = 0; + use std::vector; // 错误! + let v = vector::empty(); +} +``` + +## 命名规则(Naming rules) +Aliases must follow the same rules as other module members. This means that aliases to structs or +constants must start with `A` to `Z` + +别名必须遵循与其他模块成员相同的规则。这意味着结构或常量的别名必须以 `A` 到 `Z` 开头 +```move +address 0x42 { +module data { + struct S {} + const FLAG: bool = false; + fun foo() {} +} +module example { + use 0x42::data::{ + S as s, // 错误! + FLAG as fLAG, // 错误! + foo as FOO, // 有效 + foo as bar, // 有效 + }; +} +} +``` +## 唯一性(Uniqueness) +Inside a given scope, all aliases introduced by `use` declarations must be unique. + +在给定范围内,所有由 use 声明引入的别名必须是唯一的。 + +For a module, this means aliases introduced by `use` cannot overlap + +对于一个模块,这意味着使用引入的别名不能重复 +```move +address 0x42 { +module example { + + use std::vector::{empty as foo, length as foo}; // ERROR! + // ^^^ duplicate 'foo' + + use std::vector::empty as bar; + + use std::vector::length as bar; // 错误! + // ^^^ 重复的 'bar' + +} +} +``` +And, they cannot overlap with any of the module's other members + +而且,它们不能与模块的任何其他成员重复 +```move +address 0x42 { +module data { + struct S {} +} +module example { + use 0x42::data::S; + + struct S { value: u64 } // ERROR! + // ^ conflicts with alias 'S' above +} +} +``` +Inside of an expression block, they cannot overlap with each other, but they can +[shadow](#shadowing) other aliases or names from an outer scope + +在表达式块内部,它们不能相互重复,但它们可以遮蔽外部作用域中的其他别名或名称 + +## 遮蔽(Shadowing) +`use` aliases inside of an expression block can shadow names (module members or aliases) from the +outer scope. As with shadowing of locals, the shadowing ends at the end of the expression block; + +在表达式块内使用别名可以覆盖外部作用域的名称(模块成员或别名)。当遮蔽局部变量时,遮蔽会在表达式块的末尾结束; +```move +address 0x42 { +module example { + + struct WrappedVector { vec: vector } + + fun empty(): WrappedVector { + WrappedVector { vec: std::vector::empty() } + } + + fun example1(): (WrappedVector, WrappedVector) { + let vec = { + use std::vector::{empty, push_back}; + // 'empty' 现在指向 std::vector::empty + + let v = empty(); + push_back(&mut v, 0); + push_back(&mut v, 1); + push_back(&mut v, 10); + v + }; + // 'empty' 现在指向 Self::empty + + (empty(), WrappedVector { vec }) + } + + fun example2(): (WrappedVector, WrappedVector) { + use std::vector::{empty, push_back}; + let w: WrappedVector = { + use 0x42::example::empty; + empty() + }; + push_back(&mut w.vec, 0); + push_back(&mut w.vec, 1); + push_back(&mut w.vec, 10); + + let vec = empty(); + push_back(&mut vec, 0); + push_back(&mut vec, 1); + push_back(&mut vec, 10); + + (w, WrappedVector { vec }) + } +} +} +``` + +## 未使用的Use或别名(Unused Use or Alias) +An unused `use` will result in an error + +未使用的 `use` 会导致错误 +```move +address 0x42 { +module example { + use std::vector::{empty, push_back}; // ERROR! + // ^^^^^^^^^ 未使用的别名 'push_back' + + fun example(): vector { + empty() + } +} +} +``` diff --git a/language/documentation/book/translations/move-book-zh/src/variables.md b/language/documentation/book/translations/move-book-zh/src/variables.md new file mode 100644 index 0000000000..3625728ef5 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/variables.md @@ -0,0 +1,879 @@ +# 局部变量和作用域(Local Variables and Scopes) + +Local variables in Move are lexically (statically) scoped. New variables are introduced with the +keyword `let`, which will shadow any previous local with the same name. Locals are mutable and can +be updated both directly and via a mutable reference. + +在 Move 语言中,局部变量的解析依赖于词法作用域(lexically scoped)或静态作用域(statically scoped)。使用关键字 `let` 引入新的变量,它将隐藏任何以前的同名局部变量。局部变量是可变的(Rust 中的变量默认不可变,译者注),可以直接更新,也可以通过可变引用更新。 + +## 声明局部变量(Declaring Local Variables) + +### `let` 绑定(`let` bindings) + +Move programs use `let` to bind variable names to values: + +Move 程序使用 `let` 给变量名绑定一个值: + +```move +let x = 1; +let y = x + x: +``` + +`let` can also be used without binding a value to the local. + +`let` 使用时也可以不绑定任何数值给局部变量。 + +```move +let x; +``` + +The local can then be assigned a value later. + +然后可以稍后为局部变量赋一个值。 + +```move +let x; +if (cond) { + x = 1 +} else { + x = 0 +} +``` + +This can be very helpful when trying to extract a value from a loop when a default value cannot be provided. + +当无法提供默认值时,这在尝试从循环中提取值时非常有用。 + +```move +let x; +let cond = true; +let i = 0; +loop { + (x, cond) = foo(i); + if (!cond) break; + i = i + 1; +} +``` + +### 变量必须在使用前赋值(Variables must be assigned before use) + +Move's type system prevents a local variable from being used before it has been assigned. + +Move 的类型系统防止在赋值之前使用局部变量。 + +```move +let x; +x + x // ERROR! +``` + +```move +let x; +if (cond) x = 0; +x + x // ERROR! +``` + +```move +let x; +while (cond) x = 0; +x + x // ERROR! +``` + +### 有效的变量名(Valid variable names) + +Variable names can contain underscores `_`, letters `a` to `z`, letters `A` to `Z`, and digits `0` +to `9`. Variable names must start with either an underscore `_` or a letter `a` through `z`. They +_cannot_ start with uppercase letters. + +变量名可以包含下划线 `_`、小写字母 `a` 到 `z`、大写字母 `A` 到 `Z`、和数字 `0` 到 `9`。变量名必须以下划线 `_` 或者以小写字母`a`到`z`开头。它们*不能*以大写字母开头。 + +```move +// 全部有效 +let x = e; +let _x = e; +let _A = e; +let x0 = e; +let xA = e; +let foobar_123 = e; + +// 全部无效 +let X = e; // ERROR! +let Foo = e; // ERROR! +``` + +### 类型标注(Type annotations) + +The type of a local variable can almost always be inferred by Move's type system. However, Move +allows explicit type annotations that can be useful for readability, clarity, or debuggability. The +syntax for adding a type annotation is: + +局部变量的类型几乎总是可以通过 Move 的类型系统推断出来。但是,Move 允许显式类型标注,这对可读性、清晰性或可调试性很有用。添加类型标注的语法如下: + +```move +let x: T = e; // “T 类型的变量 x 被初始化为表达式 e” +``` + +Some examples of explicit type annotations: + +一些显式类型标注的示例: + +```move +address 0x42 { +module example { + + struct S { f: u64, g: u64 } + + fun annotated() { + let u: u8 = 0; + let b: vector = b"hello"; + let a: address = @0x0; + let (x, y): (&u64, &mut u64) = (&0, &mut 1); + let S { f, g: f2 }: S = S { f: 0, g: 1 }; + } +} +} +``` + +Note that the type annotations must always be to the right of the pattern: + +请注意,类型标注必须始终位于变量模式的右侧: + +```move +let (x: &u64, y: &mut u64) = (&0, &mut 1); // 错误!正确写法是 let (x, y): ... = +``` + +### 何时需要类型标注(When annotations are necessary) + +In some cases, a local type annotation is required if the type system cannot infer the type. This +commonly occurs when the type argument for a generic type cannot be inferred. For example: + +在某些情况下,如果类型系统无法推断类型,则需要局部类型标注。这通常发生在无法推断某个泛型(generic type)的类型参数时,比如: + +```move +let _v1 = vector::empty(); // 错误! +// ^^^^^^^^^^^^^^^ Could not infer this type. Try adding an annotation (无法推断此类型。尝试添加标注) +let v2: vector = vector::empty(); // 没有错误 +``` + +In a rarer case, the type system might not be able to infer a type for divergent code (where all the +following code is unreachable). Both `return` and [`abort`](./abort-and-assert.md) are expressions +and can have any type. A [`loop`](./loops.md) has type `()` if it has a `break`, but if there is no +break out of the `loop`, it could have any type. If these types cannot be inferred, a type +annotation is required. For example, this code: + +在极少数情况下,Move 的类型系统可能无法推断出一段发散式代码(divergent code)的类型(后面所有代码无法访问)。在 Move 语言中,`return` 和 [`abort`](./abort-and-assert.md) 都属于表达式,它们可以返回任何类型。如果一段 [`loop`](./loops.md) 有 `break`,那么它的返回类型是 `()`,但是如果它不包含 `break`,它的返回类型可以是任何类型。如果无法推断出这些类型,那么类型标注是必须的。例如,这段代码: + +```move +let a: u8 = return (); +let b: bool = abort 0; +let c: signer = loop (); + +let x = return (); // ERROR! +// ^ Could not infer this type. Try adding an annotation +let y = abort 0; // ERROR! +// ^ Could not infer this type. Try adding an annotation +let z = loop (); // ERROR! +// ^ Could not infer this type. Try adding an annotation +``` + +Adding type annotations to this code will expose other errors about dead code or unused local +variables, but the example is still helpful for understanding this problem. + +在这段代码中添加类型标注会暴露其他关于死代码或未使用的局部变量的错误,但该示例仍然有助于理解这个问题。 + +### 元组式的多个变量声明(Multiple declarations with tuples) + +`let` can introduce more than one local at a time using tuples. The locals declared inside the +parenthesis are initialized to the corresponding values from the tuple. + +`let` 可以使用元组一次引入多个局部变量。在括号内声明的局部变量会被初始化为元组中的对应值。 + +```move +let () = (); +let (x0, x1) = (0, 1); +let (y0, y1, y2) = (0, 1, 2); +let (z0, z1, z2, z3) = (0, 1, 2, 3); +``` + +The type of the expression must match the arity of the tuple pattern exactly. + +表达式的类型必须与元组模式的数量完全匹配。 + +```move +let (x, y) = (0, 1, 2); // 错误! +let (x, y, z, q) = (0, 1, 2); // 错误! +``` + +You cannot declare more than one local with the same name in a single `let`. + +你不能在单个 `let` 中声明多个具有相同名称的局部变量。 + +```move +let (x, x) = 0; // 错误! +``` + +### 结构体式的多个变量声明(Multiple declarations with structs) + +`let` can also introduce more than one local at a time when destructuring (or matching against) a +struct. In this form, the `let` creates a set of local variables that are initialized to the values +of the fields from a struct. The syntax looks like this: + +`let` 还可以在解构(或匹配)结构体时一次引入多个局部变量。在这种形式中,`let` 创建了一组局部变量,这些变量被初始化为结构体中字段的值。语法如下所示: + +```move +struct T { f1: u64, f2: u64 } +``` + +```move +let T { f1: local1, f2: local2 } = T { f1: 1, f2: 2 }; +// local1: u64 +// local2: u64 +``` + +Here is a more complicated example: + +这是一个更复杂的示例: + +```move +address 0x42 { + module example { + struct X { f: u64 } + struct Y { x1: X, x2: X } + + fun new_x(): X { + X { f: 1 } + } + + fun example() { + let Y { x1: X { f }, x2 } = Y { x1: new_x(), x2: new_x() }; + assert!(f + x2.f == 2, 42); + + let Y { x1: X { f: f1 }, x2: X { f: f2 } } = Y { x1: new_x(), x2: new_x() }; + assert!(f1 + f2 == 2, 42); + } + } +} +``` + +Fields of structs can serve double duty, identifying the field to bind _and_ the name of the +variable. This is sometimes referred to as punning. + +结构体的字段可以起到双重作用:识别要绑定的字段*和*命名变量。这有时被称为双关语。 + +```move +let X { f } = e; +``` + +is equivalent to: + +等价于: + +```move +let X { f: f } = e; +``` + +As shown with tuples, you cannot declare more than one local with the same name in a single `let`. + +如元组所示,您不能在单个 `let` 中声明多个具有相同名称的局部变量。 + +```move +let Y { x1: x, x2: x } = e; // 错误!(两个 x 同名了) +``` + +### 针对引用进行解构(Destructuring against references) + +In the examples above for structs, the bound value in the let was moved, destroying the struct value +and binding its fields. + +在上面的结构体示例中,`let` 中绑定的值被移动了,这销毁了结构体的值并同时绑定了它的字段(到变量)。 + +```move +struct T { f1: u64, f2: u64 } +``` + +```move +let T { f1: local1, f2: local2 } = T { f1: 1, f2: 2 }; +// local1: u64 +// local2: u64 +``` + +In this scenario the struct value `T { f1: 1, f2: 2 }` no longer exists after the `let`. + +If you wish instead to not move and destroy the struct value, you can borrow each of its fields. For +example: + +在这种场景下结构体的值 `T { f1: 1, f2: 2 }` 会在 `let` 后消失。 + +如果您希望不移动和销毁结构体的值,则可以借用其中的每个字段。例如: + +```move +let t = T { f1: 1, f2: 2 }; +let T { f1: local1, f2: local2 } = &t; +// local1: &u64 +// local2: &u64 +``` + +And similarly with mutable references: + +可变引用也类似: + +```move +let t = T { f1: 1, f2: 2 }; +let T { f1: local1, f2: local2 } = &mut t; +// local1: &mut u64 +// local2: &mut u64 +``` + +This behavior can also work with nested structs. + +此行为也适用于嵌套结构体。 + +```move +address 0x42 { + module example { + struct X { f: u64 } + struct Y { x1: X, x2: X } + + fun new_x(): X { + X { f: 1 } + } + + fun example() { + let y = Y { x1: new_x(), x2: new_x() }; + + let Y { x1: X { f }, x2 } = &y; + assert!(*f + x2.f == 2, 42); + + let Y { x1: X { f: f1 }, x2: X { f: f2 } } = &mut y; + *f1 = *f1 + 1; + *f2 = *f2 + 1; + assert!(*f1 + *f2 == 4, 42); + } + } +} +``` + +### 忽略值(Ignoring Values) + +In `let` bindings, it is often helpful to ignore some values. Local variables that start with `_` +will be ignored and not introduce a new variable + +在 `let` 绑定中,忽略某些值通常很有帮助。以 `_` 开头的局部变量将被忽略并且不会引入新变量。 + +```move +fun three(): (u64, u64, u64) { + (0, 1, 2) +} +``` + +```move +let (x1, _, z1) = three(); +let (x2, _y, z2) = three(); +assert!(x1 + z1 == x2 + z2, 42); +``` + +This can be necessary at times as the compiler will error on unused local variables。 + +这有时是必要的,因为编译器会在未使用的局部变量上报错。 + +```move +let (x1, y, z1) = three(); // 错误! +// ^ 未被使用的局部变量 'y' +``` + +### 通用的 `let` 语法(General `let` grammar) + +All of the different structures in `let` can be combined! With that we arrive at this general +grammar for `let` statements: + +`let` 中所有不同的结构体都可以组合!这样,我们就得出了 `let` 语句的通用语法: + +> _let-binding_ → **let** _pattern-or-list_ _type-annotation__opt_ +> _initializer__opt_ > _pattern-or-list_ → _pattern_ | **(** _pattern-list_ **)** > +> _pattern-list_ → _pattern_ **,**_opt_ | _pattern_ **,** _pattern-list_ > +> _type-annotation_ → **:** _type_ _initializer_ → **=** _expression_ + +The general term for the item that introduces the bindings is a _pattern_. The pattern serves to +both destructure data (possibly recursively) and introduce the bindings. The pattern grammar is as +follows: + +引入绑定的项的通用术语是 *模式(pattern)*。该模式用于解构数据(可能是递归的)并引入绑定。模式语法如下: + +> _pattern_ → _local-variable_ | _struct-type_ **{** _field-binding-list_ **}** > +> _field-binding-list_ → _field-binding_ **,**_opt_ | _field-binding_ **,** +> _field-binding-list_ > _field-binding_ → _field_ | _field_ **:** _pattern_ + +A few concrete examples with this grammar applied: + +应用此语法的一些具体示例: + +```move + let (x, y): (u64, u64) = (0, 1); +// ^ local-variable(局部变量) +// ^ pattern(模式) +// ^ local-variable(局部变量) +// ^ pattern(模式) +// ^ pattern-list(模式列表) +// ^^^^ pattern-list(模式列表) +// ^^^^^^ pattern-or-list(模式或列表) +// ^^^^^^^^^^^^ type-annotation(类型标注) +// ^^^^^^^^ initializer(初始化器) +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ let-binding(let 绑定) + + let Foo { f, g: x } = Foo { f: 0, g: 1 }; +// ^^^ struct-type(结构类型) +// ^ field(字段) +// ^ field-binding(字段绑定) +// ^ field(字段) +// ^ local-variable(局部变量) +// ^ pattern(模式) +// ^^^^ field-binding(字段绑定) +// ^^^^^^^ field-binding-list(字段绑定列表) +// ^^^^^^^^^^^^^^^ pattern(模式) +// ^^^^^^^^^^^^^^^ pattern-or-list(模式或列表) +// ^^^^^^^^^^^^^^^^^^^^ initializer(初始化器) +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ let-binding(let 绑定) +``` + +## 变更(Mutations) + +### 赋值(Assignments) + +After the local is introduced (either by `let` or as a function parameter), the local can be +modified via an assignment: + +在引入局部变量后(通过 `let` 或作为函数参数),可以通过赋值来修改局部变量: + +```move +x = e +``` + +Unlike `let` bindings, assignments are expressions. In some languages, assignments return the value +that was assigned, but in Move, the type of any assignment is always `()`. + +与 `let` 绑定不同,赋值是表达式。在某些语言中,赋值会返回被赋予的值,但在 Move 语言中,任何赋值的返回类型始终是 `()`。 + +```move +(x = e: ()) +``` + +Practically, assignments being expressions means that they can be used without adding a new +expression block with braces (`{`...`}`). + +实际上,赋值作为表达式意味着它们可以在不添加带有大括号(`{`...`}`)的新表达式块(expression block)的情况下使用。 + +```move +let x = 0; +if (cond) x = 1 else x = 2; +``` + +The assignment uses the same pattern syntax scheme as `let` bindings: + +赋值使用与 `let` 绑定相同的模式语法方案: + +```move +address 0x42 { +module example { + struct X { f: u64 } + + fun new_x(): X { + X { f: 1 } + } + + // 这个例子会因为存在未使用的变量和赋值而报错。 + fun example() { + let (x, _, z) = (0, 1, 3); + let (x, y, f, g); + + (X { f }, X { f: x }) = (new_x(), new_x()); + assert!(f + x == 2, 42); + + (x, y, z, f, _, g) = (0, 0, 0, 0, 0, 0); + } +} +} +``` + +Note that a local variable can only have one type, so the type of the local cannot change between +assignments. + +注意,一个局部变量只能有一种类型,所以局部变量的类型不能在赋值之间(多次赋值)改变。 + +```move +let x; +x = 0; +x = false; // 错误! +``` + +### 通过引用进行变更(Mutating through a reference) + +In addition to directly modifying a local with assignment, a local can be modified via a mutable +reference `&mut`. + +除了通过赋值直接修改局部变量外,还可以通过可变引用 `&mut` 的方式修改局部变量。 + +```move +let x = 0; +let r = &mut x; +*r = 1; +assert!(x == 1, 42); +``` + +This is particularly useful if either: + +(1) You want to modify different variables depending on some condition. + +这在以下情况下特别有用: + +(1) 你想根据某些条件修改不同的变量。 + +```move +let x = 0; +let y = 1; +let r = if (cond) &mut x else &mut y; +*r = *r + 1; +``` + +(2) You want another function to modify your local value. + +(2) 你想要另一个函数来修改你的局部变量值。 + +```move +let x = 0; +modify_ref(&mut x); +``` + +This sort of modification is how you modify structs and vectors! + +这种修改方法就是你修改结构体和向量的方式! + +```move +let v = vector::empty(); +vector::push_back(&mut v, 100); +assert!(*vector::borrow(&v, 0) == 100, 42); +``` + +For more details, see [Move references](./references.md). + +有关更多详细信息,请参阅 [Move 引用](./references.md)。 + +## 作用域(Scopes) + +Any local declared with `let` is available for any subsequent expression, _within that scope_. +Scopes are declared with expression blocks, `{`...`}`. + +Locals cannot be used outside of the declared scope. + +使用 `let` 声明的任何局部变量都可用于*该作用域内*的任何后续表达式。作用域用表达式块(expression blocks)声明,`{`...`}`。 + +局部变量不能在声明的作用域之外使用。 + +```move +let x = 0; +{ + let y = 1; +}; +x + y // 错误! +// ^ unbound local 'y'(未绑定的局部变量“y”) +``` + +But, locals from an outer scope _can_ be used in a nested scope. + +但是,来自外部作用域的本地变量*可以*在嵌套作用域中使用。 + +```move +{ + let x = 0; + { + let y = x + 1; // 有效的 + } +} +``` + +Locals can be mutated in any scope where they are accessible. That mutation survives with the local, +regardless of the scope that performed the mutation. + +局部变量可以在允许访问的任何作用域内进行变更。无论执行变更的作用域如何,这种变更会跟随局部变量的生命周期。 + +```move +let x = 0; +x = x + 1; +assert!(x == 1, 42); +{ + x = x + 1; + assert!(x == 2, 42); +}; +assert!(x == 2, 42); +``` + +### 表达式块(Expression Blocks) + +An expression block is a series of statements separated by semicolons (`;`). The resulting value of +an expression block is the value of the last expression in the block. + +表达式块是由分号(`;`)分隔的一系列语句。表达式块的结果值是块中最后一个表达式的值。 + +```move +{ let x = 1; let y = 1; x + y } +``` + +In this example, the result of the block is `x + y`. + +A statement can be either a `let` declaration or an expression. Remember that assignments (`x = e`) +are expressions of type `()`. + +在此示例中, 此区块的结果是 `x + y`. + +语句可以是 `let` 声明或表达式。请记住,赋值(`x = e`)是 `()` 类型的表达式。 + +```move +{ let x; let y = 1; x = 1; x + y } +``` + +Function calls are another common expression of type `()`. Function calls that modify data are +commonly used as statements. + +函数调用是 `()` 类型的另一种常见表达方式。修改数据的函数调用通常被用作语句。 + +```move +{ let v = vector::empty(); vector::push_back(&mut v, 1); v } +``` + +This is not just limited to `()` types---any expression can be used as a statement in a sequence! + +这不仅限于 `()` 类型 —— 任何表达式都可以用作序列中的语句! + +```move +{ + let x = 0; + x + 1; // 值会被丢弃 + x + 2; // 值会被丢弃 + b"hello"; // 值会被丢弃 +} +``` + +But! If the expression contains a resource (a value without the `drop` [ability](./abilities.md)), +you will get an error. This is because Move's type system guarantees that any value that is dropped +has the `drop` [ability](./abilities.md). (Ownership must be transferred or the value must be +explicitly destroyed within its declaring module.) + +但是!如果表达式包含资源(没有 `drop` [能力](./abilities.md)的值),你将收到错误消息。这是因为 Move 的类型系统保证任何被删除的值都具有 `drop` [能力](./abilities.md)。(必须转移所有权,或者必须在其声明模块中显式销毁该值。) + +```move +{ + let x = 0; + Coin { value: x }; // 错误! +// ^^^^^^^^^^^^^^^^^ unused value without the `drop` ability(未使用没有 `drop` 能力的值) + x +} +``` + +If a final expression is not present in a block---that is, if there is a trailing semicolon `;`, +there is an implicit unit `()` value. Similarly, if the expression block is empty, there is an +implicit unit `()` value. + +如果块中不存在最终表达式 —— 也就是说,如果有一个尾随分号 `;`,则含有一个隐式的[单值(unit)`()`](https://zh.wikipedia.org/wiki/%E5%8D%95%E5%80%BC%E7%B1%BB%E5%9E%8B)。同样,如果表达式块为空,那么也存在隐式的单值 `()`。 + +```move +// 两者是等价的 +{ x = x + 1; 1 / x; } +{ x = x + 1; 1 / x; () } +``` + +```move +// 两者是等价的 +{ } +{ () } +``` + +An expression block is itself an expression and can be used anyplace an expression is used. (Note: +The body of a function is also an expression block, but the function body cannot be replaced by +another expression.) + +表达式块本身就是一个表达式,可以在任何使用表达式的地方使用。(注意:函数体也是一个表达式块,但函数体不能被另一个表达式替换。) + +```move +let my_vector: vector> = { + let v = vector::empty(); + vector::push_back(&mut v, b"hello"); + vector::push_back(&mut v, b"goodbye"); + v +}; +``` + +(The type annotation is not needed in this example and only added for clarity.) + +(此示例中不需要类型标注,只是为了清楚起见而添加。) + +### 遮蔽(Shadowing) + +If a `let` introduces a local variable with a name already in scope, that previous variable can no +longer be accessed for the rest of this scope. This is called _shadowing_. + +如果一个 `let` 引入了一个名称已经在作用域中的局部变量,则该作用域的剩余部分将无法再访问先前的变量。这称为*遮蔽(shadowing)*。 + +```move +let x = 0; +assert!(x == 0, 42); + +let x = 1; // x 被遮蔽了 +assert!(x == 1, 42); +``` + +When a local is shadowed, it does not need to retain the same type as before. + +当局部变量被遮蔽时,它不需要保留与以前相同的类型。 + +```move +let x = 0; +assert!(x == 0, 42); + +let x = b"hello"; // x 被遮蔽了 +assert!(x == b"hello", 42); +``` + +After a local is shadowed, the value stored in the local still exists, but will no longer be +accessible. This is important to keep in mind with values of types without the +[`drop` ability](./abilities.md), as ownership of the value must be transferred by the end of the +function. + +在局部变量被遮蔽后,存储在局部变量的值仍然存在,但是将不再可访问。对于没有 [`drop` 能力](./abilities.md)的类型的值,请记住这一点很重要,因为值的所有权必须在函数结束时转移。 + +```move +address 0x42 { + module example { + struct Coin has store { value: u64 } + + fun unused_resource(): Coin { + let x = Coin { value: 0 }; // ERROR! +// ^ This local still contains a value without the `drop` ability(这个局部变量仍然包含一个没有 `drop` 能力的值) + x.value = 1; + let x = Coin { value: 10 }; + x +// ^ Invalid return(无效的返回) + } + } +} +``` + +When a local is shadowed inside a scope, the shadowing only remains for that scope. The shadowing is +gone once that scope ends. + +当局部变量在作用域内被遮蔽时,该遮蔽作用仅保留在该作用域内。一旦该作用域结束,遮蔽作用就消失了。 + +```move +let x = 0; +{ + let x = 1; + assert!(x == 1, 42); +}; +assert!(x == 0, 42); +``` + +Remember, locals can change type when they are shadowed. + +请记住,局部变量在被遮蔽时可以更改类型。 + +```move +let x = 0; +{ + let x = b"hello"; + assert!(x = b"hello", 42); +}; +assert!(x == 0, 42); +``` + +## 移动和复制(Move and Copy) + +All local variables in Move can be used in two ways, either by `move` or `copy`. If one or the other +is not specified, the Move compiler is able to infer whether a `copy` or a `move` should be used. +This means that in all of the examples above, a `move` or a `copy` would be inserted by the +compiler. A local variable cannot be used without the use of `move` or `copy`. + +`copy` will likely feel the most familiar coming from other programming languages, as it creates a +new copy of the value inside of the variable to use in that expression. With `copy`, the local +variable can be used more than once. + +Move 中的所有局部变量都可以通过两种方式使用,通过 `move` 或 `copy`。如果未指定其中之一,则 Move 编译器能够推断应该使用 `copy` 还是 `move`。这意味着在上述所有示例中,编译器将插入 `move` 或 `copy`。如果不使用 `move` 或 `copy`,就不能使用局部变量。 + +`copy` 对来自其他编程语言的开发者来说可能会觉得最熟悉,因为它会在变量内部创建一个新的副本值以在该表达式中使用。使用 `copy`,局部变量可以被多次使用。 + +```move +let x = 0; +let y = copy x + 1; +let z = copy x + 2; +``` + +Any value with the `copy` [ability](./abilities.md) can be copied in this way. + +`move` takes the value out of the local variable _without_ copying the data. After a `move` occurs, +the local variable is unavailable. + +任何具有 `copy` [能力](./abilities.md)的值都可以通过这种方式复制。 + +`move` 从局部变量中取出值*而不是*复制数据。`移动(move)`发生后,局部变量将不可用。 + +```move +let x = 1; +let y = move x + 1; +// ------ Local was moved here(局部变量被移动到这里了) +let z = move x + 2; // 错误! +// ^^^^^^ Invalid usage of local 'x'(局部变量“x”的无效使用方式) +y + z +``` + +### 安全性(Safety) + +Move's type system will prevent a value from being used after it is moved. This is the same safety +check described in [`let` declaration](#let-bindings) that prevents local variables from being used +before it is assigned a value. + +Move 的类型系统会阻止一个值在移动后被使用。这与 [`let` 声明](#let-绑定let-bindings)中描述的防止在局部变量被赋值之前使用的安全检查相同。 + + + + + +### 推断(Inference) + +As mentioned above, the Move compiler will infer a `copy` or `move` if one is not indicated. The +algorithm for doing so is quite simple: + +- Any scalar value with the `copy` [ability](./abilities.md) is given a `copy`. +- Any reference (both mutable `&mut` and immutable `&`) is given a `copy`. + - Except under special circumstances where it is made a `move` for predictable borrow checker errors. +- Any other value is given a `move`. + - This means that even though other values might be have the `copy` [ability](./abilities.md), it must be done _explicitly_ by the programmer. + - This is to prevent accidental copies of large data structures. + +如上所述,如果未指明,Move 编译器将推断出 `copy` 还是 `move`。这样做的算法非常简单: + +- 任何带有 `copy` [能力](./abilities.md)的标量值都被赋予了 `copy`。 +- 任何引用(可变的 `&mut` 和不可变的 `&`)都被赋予 `copy`。 + - 除非在可预测的借用检查器错误的特殊情况下,会进行 `move` 操作。 +- 任何其他值都被赋予 `move`。 + - 这意味着即使其他值可能具有 `copy` [能力](./abilities.md),它也必须由程序员*显式*声明。 + - 这是为了防止意外地复制很大的数据结构。 + +例如: + +```move +let s = b"hello"; +let foo = Foo { f: 0 }; +let coin = Coin { value: 0 }; + +let s2 = s; // 移动 +let foo2 = foo; // 移动 +let coin2 = coin; // 移动 + +let x = 0; +let b = false; +let addr = @0x42; +let x_ref = &x; +let coin_ref = &mut coin2; + +let x2 = x; // 复制 +let b2 = b; // 复制 +let addr2 = @0x42; // 复制 +let x_ref2 = x_ref; // 复制 +let coin_ref2 = coin_ref; // 复制 +``` diff --git a/language/documentation/book/translations/move-book-zh/src/vector.md b/language/documentation/book/translations/move-book-zh/src/vector.md new file mode 100644 index 0000000000..286892a280 --- /dev/null +++ b/language/documentation/book/translations/move-book-zh/src/vector.md @@ -0,0 +1,152 @@ +# 向量 + +`vector` 是 Move 提供的唯一原始集合类型。`vector` 是类型为 `T` 的同构集合,可以通过从"末端"推入/弹出(出栈/入栈,译者注)值来增长或缩小。 +*(与 Rust 一样,向量(vector)是一种可以存放任何类型的可变大小的容器,也可称为[动态数组](https://en.wikipedia.org/wiki/Dynamic_array),与 Python 中的[列表(list)](https://computersciencewiki.org/index.php/Lists)不同,译者注)* + +`vector` 可以用任何类型 `T` 实例化。例如,`vector`、`vector
`、`vector<0x42::MyModuel::MyResource>` 和 `vector>` 都是有效的向量类型。 + +## 字面量 + +### 通用 `vector` 字面量 + +任何类型的向量都可以通过 `vector` 字面量创建。 + +| 语法 | 类型 | 描述 | +|-----------------------|-------------------------------------------------------------------------------|-----------------------------------| +| `vector[]` | `vector[]: vector` 其中 `T` 是任何单一的非引用类型 | 一个空向量 | +| `vector[e1, ..., en]` | `vector[e1, ..., en]: vector` where `e_i: T` 满足 `0 < i <= n` and `n > 0` | 带有 `n` 个元素(长度为 n)的向量 | + +在这些情况下,`vector` 的类型是从元素类型或从向量的使用上推断出来的。如果无法推断类型或者只是为了更清楚地表示,则可以显式指定类型: + +```move +vector[]: vector +vector[e1, ..., en]: vector +``` + +#### 向量字面量示例 + +```move +(vector[]: vector); +(vector[0u8, 1u8, 2u8]: vector); +(vector[]: vector); +(vector
[@0x42, @0x100]: vector
); +``` + +### `vector` 字面量 + +Move 中向量的一个常见用例是表示“字节数组”,用 `vector` 表示。这些值通常用于加密目的,例如公钥或哈希结果。这些值非常常见,以至于提供了特定的语法以使值更具可读性,而不是必须使用 `vector[]`,其中每个单独的 `u8` 值都以数字形式指定。 + +目前支持两种类型的 `vector` 字面量,*字节字符串*和*十六进制字符串*。 + +#### 字节字符串 + +字节字符串是带引号的字符串字面量,以 `b` 为前缀,例如,`b"Hello!\n"`。 + +这些是允许转义序列的 ASCII 编码字符串。目前,支持的转义序列如下: + +| 转义序列 | 描述 | +|----------|---------------------------------------------| +| `\n` | 换行 | +| `\r` | 回车 | +| `\t` | 制表符 | +| `\\` | 反斜杠 | +| `\0` | Null | +| `\"` | 引号 | +| `\xHH` | 十六进制进制转义,插入十六进制字节序列 `HH` | + +#### 十六进制字符串 + +十六进制字符串是以 `x` 为前缀的带引号的字符串字面量,例如,`x"48656C6C6F210A"`。 + +每个字节对,范围从 `00` 到 `FF` 都被解析为十六进制编码的 `u8` 值。所以每个字节对对应于结果 `vector` 的单个条目。 + +#### 字符串字面量示例 + +```move +script { + fun byte_and_hex_strings() { + assert!(b"" == x"", 0); + assert!(b"Hello!\n" == x"48656C6C6F210A", 1); + assert!(b"\x48\x65\x6C\x6C\x6F\x21\x0A" == x"48656C6C6F210A", 2); + assert!( + b"\"Hello\tworld!\"\n \r \\Null=\0" == + x"2248656C6C6F09776F726C6421220A200D205C4E756C6C3D00", + 3 + ); + } +} +``` + +## 操作 + +`vector` 通过 Move 标准库里的 `std::vector` 模块支持以下操作: + +| 函数 | 描述 | 中止条件 | +|------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|----------------------| +| `vector::empty(): vector` | 创建一个可以存储 `T` 类型值的空向量 | 永不中止 | +| `vector::singleton(t: T): vector` | 创建一个包含 `t` 的大小为 1 的向量 | 永不中止 | +| `vector::push_back(v: &mut vector, t: T)` | 将 `t` 添加到 `v` 的尾部 | 永不中止 | +| `vector::pop_back(v: &mut vector): T` | 移除并返回 `v` 中的最后一个元素 | 如果 `v` 是空向量 | +| `vector::borrow(v: &vector, i: u64): &T` | 返回在索引 `i` 处对 `T` 的不可变引用 | 如果 `i` 越界 | +| `vector::borrow_mut(v: &mut vector, i: u64): &mut T` | 返回在索引 `i` 处对 `T` 的可变引用 | 如果 `i` 越界 | +| `vector::destroy_empty(v: vector)` | 销毁 `v` 向量 | 如果 `v` 不是空向量 | +| `vector::append(v1: &mut vector, v2: vector)` | 将 `v2` 中的元素添加到 `v1` 的末尾 | 永不中止 | +| `vector::contains(v: &vector, e: &T): bool` | 如果 `e` 在向量 `v` 里返回 true,否则返回 false | 永不中止 | +| `vector::swap(v: &mut vector, i: u64, j: u64)` | 交换向量 `v` 中第 `i` 个和第 `j` 个索引处的元素 | 如果 `i` 或 `j` 越界 | +| `vector::reverse(v: &mut vector)` | 反转向量 `v` 中元素的顺序 | 永不中止 | +| `vector::index_of(v: &vector, e: &T): (bool, u64)` | 如果 `e` 在索引 `i` 处的向量中,则返回 `(true, i)`。否则返回`(false, 0)` | 永不中止 | +| `vector::remove(v: &mut vector, i: u64): T` | 移除向量 `v` 中的第 `i` 个元素,移动所有后续元素。这里的时间复杂度是 O(n),并且保留了向量中元素的顺序 | 如果 `i` 越界 | +| `vector::swap_remove(v: &mut vector, i: u64): T` | 将向量中的第 `i` 个元素与最后一个元素交换,然后弹出该元素。这里的时间复杂度是 O(1),但是不保留向量中的元素顺序 | 如果 `i` 越界 | + +随着时间的推移可能会增加更多操作。 + +## 示例 + +```move +use std::vector; + +let v = vector::empty(); +vector::push_back(&mut v, 5); +vector::push_back(&mut v, 6); + +assert!(*vector::borrow(&v, 0) == 5, 42); +assert!(*vector::borrow(&v, 1) == 6, 42); +assert!(vector::pop_back(&mut v) == 6, 42); +assert!(vector::pop_back(&mut v) == 5, 42); +``` + +## 销毁和复制 `vector` + +`vector` 的某些行为取决于元素类型 `T` 的能力(ability),例如:如果向量中包含不具有 `drop` 能力的元素,那么不能像上面例子中的 `v` 一样隐式丢弃 —— 它们必须用 `vector::destroy_empty` 显式销毁。 + +请注意,除非向量 `vec` 包含零个元素,否则 `vector::destroy_empty` 将在运行时中止: + +```move +fun destroy_any_vector(vec: vector) { + vector::destroy_empty(vec) // 删除此行将导致编译器错误 +} +``` + +但是删除包含带有 `drop` 能力的元素的向量不会发生错误: + +```move +fun destroy_droppable_vector(vec: vector) { + // 有效! + // 不需要明确地做任何事情来销毁向量 +} +``` + +同样,除非元素类型具有 `copy` 能力,否则无法复制向量。换句话说,当且仅当 `T` 具有 `copy` 能力时,`vector` 才具有 `copy` 能力。然而,即使是可复制的向量也永远不会被隐式复制: + +```move +let x = vector::singleton(10); +let y = copy x; // 没有 copy 将导致编译器错误! +``` + +大向量的复制可能很昂贵,因此编译器需要显式 `copy` 以便更容易查看它们发生的位置。 + +有关更多详细信息,请参阅[类型能力](./abilities.md)和[泛型](./generics.md)部分。 + +## 所有权 + +[如上所述](#销毁和复制-vector),`vector` 值只有在元素值可以复制的时候才能复制。在这种情况下,复制必须通过显式 [`copy`](./variables.md#移动和复制) 或者[解引用 `*`](./references.md#通过引用读取和写入)。 diff --git a/language/documentation/examples/diem-framework/crates/cli/Cargo.toml b/language/documentation/examples/diem-framework/crates/cli/Cargo.toml index 7976e01e29..f1223aa3d4 100644 --- a/language/documentation/examples/diem-framework/crates/cli/Cargo.toml +++ b/language/documentation/examples/diem-framework/crates/cli/Cargo.toml @@ -5,17 +5,19 @@ description = "CLI frontend for the Move compiler and VM (with Diem Framework)" authors = ["Diem Association "] license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] anyhow = "1.0.52" -bcs = "0.1.2" clap = { version = "3.1.8", features = ["derive"] } +bcs = "0.1.4" + move-stdlib = { path = "../../../../../move-stdlib" } move-core-types = { path = "../../../../../move-core/types" } move-vm-types = { path = "../../../../../move-vm/types" } move-cli = { path = "../../../../../tools/move-cli" } +move-vm-test-utils = { path = "../../../../../move-vm/test-utils" } diem-framework-natives = { path = "../natives" } diff --git a/language/documentation/examples/diem-framework/crates/cli/src/main.rs b/language/documentation/examples/diem-framework/crates/cli/src/main.rs index f974828c32..ec83cef143 100644 --- a/language/documentation/examples/diem-framework/crates/cli/src/main.rs +++ b/language/documentation/examples/diem-framework/crates/cli/src/main.rs @@ -5,12 +5,10 @@ use anyhow::Result; use clap::Parser; use move_cli::{Command, Move}; -use move_core_types::{ - errmap::ErrorMapping, - gas_schedule::{CostTable, GasCost}, - language_storage::CORE_CODE_ADDRESS, +use move_core_types::{errmap::ErrorMapping, language_storage::CORE_CODE_ADDRESS}; +use move_vm_test_utils::gas_schedule::{ + new_from_instructions, zero_cost_instruction_table, CostTable, }; -use move_vm_types::gas_schedule::{new_from_instructions, zero_cost_instruction_table}; #[derive(Parser)] pub struct DfCli { @@ -28,31 +26,36 @@ pub enum DfCommands { // extra commands available only in df-cli can be added below } -fn cost_table(num_natives: usize) -> CostTable { +fn cost_table() -> CostTable { let instruction_table = zero_cost_instruction_table(); - let native_table = std::iter::repeat_with(|| GasCost::new(0, 0)) - .take(num_natives) - .collect(); - - new_from_instructions(instruction_table, native_table) + new_from_instructions(instruction_table) } fn main() -> Result<()> { // let error_descriptions: ErrorMapping = // bcs::from_bytes(diem_framework_releases::current_error_descriptions())?; - let natives = move_stdlib::natives::all_natives(CORE_CODE_ADDRESS) - .into_iter() - .chain(diem_framework_natives::all_natives(CORE_CODE_ADDRESS)) - .collect::>(); - - let num_natives = natives.len(); + let natives = move_stdlib::natives::all_natives( + CORE_CODE_ADDRESS, + // We may want to switch to a different gas schedule in the future, but for now, + // the all-zero one should be enough. + move_stdlib::natives::GasParameters::zeros(), + ) + .into_iter() + .chain(move_stdlib::natives::nursery_natives( + CORE_CODE_ADDRESS, + // We may want to switch to a different gas schedule in the future, but for now, + // the all-zero one should be enough. + move_stdlib::natives::NurseryGasParameters::zeros(), + )) + .chain(diem_framework_natives::all_natives(CORE_CODE_ADDRESS)) + .collect::>(); let args = DfCli::parse(); match args.cmd { DfCommands::Command(cmd) => move_cli::run_cli( natives, - &cost_table(num_natives), + &cost_table(), // TODO: implement this &ErrorMapping::default(), args.move_args, diff --git a/language/documentation/examples/diem-framework/crates/crypto-derive/Cargo.toml b/language/documentation/examples/diem-framework/crates/crypto-derive/Cargo.toml index efae300ae2..cd5d74589c 100644 --- a/language/documentation/examples/diem-framework/crates/crypto-derive/Cargo.toml +++ b/language/documentation/examples/diem-framework/crates/crypto-derive/Cargo.toml @@ -3,7 +3,7 @@ name = "diem-crypto-derive" version = "0.0.3" authors = ["Diem Association "] publish = false -edition = "2018" +edition = "2021" license = "Apache-2.0" [lib] diff --git a/language/documentation/examples/diem-framework/crates/crypto/Cargo.toml b/language/documentation/examples/diem-framework/crates/crypto/Cargo.toml index ceba097221..2e9031677b 100644 --- a/language/documentation/examples/diem-framework/crates/crypto/Cargo.toml +++ b/language/documentation/examples/diem-framework/crates/crypto/Cargo.toml @@ -3,7 +3,7 @@ name = "diem-crypto" version = "0.0.3" authors = ["Diem Association "] publish = false -edition = "2018" +edition = "2021" license = "Apache-2.0" [dependencies] @@ -30,7 +30,8 @@ tiny-keccak = { version = "2.0.2", features = ["sha3"] } x25519-dalek = { version = "0.1.0", package = "x25519-dalek-fiat", default-features = false, features = ["std"] } aes-gcm = "0.8.0" diem-crypto-derive = { path = "../crypto-derive" } -bcs = "0.1.2" + +bcs = "0.1.4" [dev-dependencies] bitvec = "0.19.4" @@ -41,7 +42,8 @@ ripemd160 = "0.9.1" criterion = "0.3.4" sha3 = "0.9.1" serde_json = "1.0.64" -trybuild = "1.0.41" +# TODO: some tests will fail if this is set to 1.0.63 +trybuild = "=1.0.53" [features] default = ["fiat"] diff --git a/language/documentation/examples/diem-framework/crates/crypto/src/test_utils.rs b/language/documentation/examples/diem-framework/crates/crypto/src/test_utils.rs index 25e9386136..8bc36d2c65 100644 --- a/language/documentation/examples/diem-framework/crates/crypto/src/test_utils.rs +++ b/language/documentation/examples/diem-framework/crates/crypto/src/test_utils.rs @@ -140,7 +140,7 @@ impl ::core::clone::Clone for TestDiemCryptoHasher { fn clone(&self) -> TestDiemCryptoHasher { match *self { TestDiemCryptoHasher(ref __self_0_0) => { - TestDiemCryptoHasher(::core::clone::Clone::clone(&(*__self_0_0))) + TestDiemCryptoHasher(::core::clone::Clone::clone(__self_0_0)) } } } diff --git a/language/documentation/examples/diem-framework/crates/natives/Cargo.toml b/language/documentation/examples/diem-framework/crates/natives/Cargo.toml index f59df07096..cc2867982a 100644 --- a/language/documentation/examples/diem-framework/crates/natives/Cargo.toml +++ b/language/documentation/examples/diem-framework/crates/natives/Cargo.toml @@ -3,7 +3,7 @@ name = "diem-framework-natives" version = "0.0.0" authors = ["Diem Association "] publish = false -edition = "2018" +edition = "2021" license = "Apache-2.0" [dependencies] diff --git a/language/documentation/examples/diem-framework/crates/natives/src/account.rs b/language/documentation/examples/diem-framework/crates/natives/src/account.rs index 00c063fa00..6794e22206 100644 --- a/language/documentation/examples/diem-framework/crates/natives/src/account.rs +++ b/language/documentation/examples/diem-framework/crates/natives/src/account.rs @@ -6,17 +6,13 @@ use move_binary_format::errors::PartialVMResult; use move_core_types::account_address::AccountAddress; use move_vm_runtime::native_functions::NativeContext; use move_vm_types::{ - gas_schedule::NativeCostIndex, - loaded_data::runtime_types::Type, - natives::function::{native_gas, NativeResult}, - pop_arg, - values::Value, + loaded_data::runtime_types::Type, natives::function::NativeResult, pop_arg, values::Value, }; use smallvec::smallvec; use std::collections::VecDeque; pub fn native_create_signer( - context: &mut NativeContext, + _context: &mut NativeContext, ty_args: Vec, mut arguments: VecDeque, ) -> PartialVMResult { @@ -24,20 +20,21 @@ pub fn native_create_signer( debug_assert!(arguments.len() == 1); let address = pop_arg!(arguments, AccountAddress); - let cost = native_gas(context.cost_table(), NativeCostIndex::CREATE_SIGNER, 0); - Ok(NativeResult::ok(cost, smallvec![Value::signer(address)])) + Ok(NativeResult::ok( + 25.into(), + smallvec![Value::signer(address)], + )) } /// NOTE: this function will be deprecated after the Diem v3 release, but must /// remain for replaying old transactions pub fn native_destroy_signer( - context: &mut NativeContext, + _context: &mut NativeContext, ty_args: Vec, arguments: VecDeque, ) -> PartialVMResult { debug_assert!(ty_args.is_empty()); debug_assert!(arguments.len() == 1); - let cost = native_gas(context.cost_table(), NativeCostIndex::DESTROY_SIGNER, 0); - Ok(NativeResult::ok(cost, smallvec![])) + Ok(NativeResult::ok(213.into(), smallvec![])) } diff --git a/language/documentation/examples/diem-framework/crates/natives/src/lib.rs b/language/documentation/examples/diem-framework/crates/natives/src/lib.rs index cc85249f32..bb8471f7cd 100644 --- a/language/documentation/examples/diem-framework/crates/natives/src/lib.rs +++ b/language/documentation/examples/diem-framework/crates/natives/src/lib.rs @@ -5,44 +5,42 @@ pub mod account; pub mod signature; -use move_core_types::{account_address::AccountAddress, identifier::Identifier}; -use move_vm_runtime::native_functions::{NativeFunction, NativeFunctionTable}; +use std::sync::Arc; + +use move_core_types::account_address::AccountAddress; +use move_vm_runtime::native_functions::{ + make_table_from_iter, NativeFunction, NativeFunctionTable, +}; pub fn all_natives(diem_framework_addr: AccountAddress) -> NativeFunctionTable { - const NATIVES: &[(&str, &str, NativeFunction)] = &[ + let natives: [(&str, &str, NativeFunction); 5] = [ // TODO: Remove once/if DPN is moved over to use the core framework ( "DiemAccount", "create_signer", - account::native_create_signer, + Arc::new(account::native_create_signer), ), ( "DiemAccount", "destroy_signer", - account::native_destroy_signer, + Arc::new(account::native_destroy_signer), ), ( "Signature", "ed25519_validate_pubkey", - signature::native_ed25519_publickey_validation, + Arc::new(signature::native_ed25519_publickey_validation), ), ( "Signature", "ed25519_verify", - signature::native_ed25519_signature_verification, + Arc::new(signature::native_ed25519_signature_verification), + ), + ( + "Account", + "create_signer", + Arc::new(account::native_create_signer), ), - ("Account", "create_signer", account::native_create_signer), ]; - NATIVES - .iter() - .cloned() - .map(|(module_name, func_name, func)| { - ( - diem_framework_addr, - Identifier::new(module_name).unwrap(), - Identifier::new(func_name).unwrap(), - func, - ) - }) - .collect() + + make_table_from_iter(diem_framework_addr, natives) } diff --git a/language/documentation/examples/diem-framework/crates/natives/src/signature.rs b/language/documentation/examples/diem-framework/crates/natives/src/signature.rs index 911ba1edde..0cc1311a62 100644 --- a/language/documentation/examples/diem-framework/crates/natives/src/signature.rs +++ b/language/documentation/examples/diem-framework/crates/natives/src/signature.rs @@ -6,17 +6,13 @@ use diem_crypto::{ed25519, traits::*}; use move_binary_format::errors::PartialVMResult; use move_vm_runtime::native_functions::NativeContext; use move_vm_types::{ - gas_schedule::NativeCostIndex, - loaded_data::runtime_types::Type, - natives::function::{native_gas, NativeResult}, - pop_arg, - values::Value, + loaded_data::runtime_types::Type, natives::function::NativeResult, pop_arg, values::Value, }; use smallvec::smallvec; use std::{collections::VecDeque, convert::TryFrom}; pub fn native_ed25519_publickey_validation( - context: &mut NativeContext, + _context: &mut NativeContext, _ty_args: Vec, mut arguments: VecDeque, ) -> PartialVMResult { @@ -25,19 +21,15 @@ pub fn native_ed25519_publickey_validation( let key_bytes = pop_arg!(arguments, Vec); - let cost = native_gas( - context.cost_table(), - NativeCostIndex::ED25519_VALIDATE_KEY, - key_bytes.len(), - ); + let cost = 26 * usize::max(key_bytes.len(), 1) as u64; // This deserialization performs point-on-curve and small subgroup checks let valid = ed25519::Ed25519PublicKey::try_from(&key_bytes[..]).is_ok(); - Ok(NativeResult::ok(cost, smallvec![Value::bool(valid)])) + Ok(NativeResult::ok(cost.into(), smallvec![Value::bool(valid)])) } pub fn native_ed25519_signature_verification( - context: &mut NativeContext, + _context: &mut NativeContext, _ty_args: Vec, mut arguments: VecDeque, ) -> PartialVMResult { @@ -48,28 +40,24 @@ pub fn native_ed25519_signature_verification( let pubkey = pop_arg!(arguments, Vec); let signature = pop_arg!(arguments, Vec); - let cost = native_gas( - context.cost_table(), - NativeCostIndex::ED25519_VERIFY, - msg.len(), - ); + let cost = 62 * usize::max(msg.len(), 1) as u64; let sig = match ed25519::Ed25519Signature::try_from(signature.as_slice()) { Ok(sig) => sig, Err(_) => { - return Ok(NativeResult::ok(cost, smallvec![Value::bool(false)])); + return Ok(NativeResult::ok(cost.into(), smallvec![Value::bool(false)])); } }; let pk = match ed25519::Ed25519PublicKey::try_from(pubkey.as_slice()) { Ok(pk) => pk, Err(_) => { - return Ok(NativeResult::ok(cost, smallvec![Value::bool(false)])); + return Ok(NativeResult::ok(cost.into(), smallvec![Value::bool(false)])); } }; let verify_result = sig.verify_arbitrary_msg(msg.as_slice(), &pk).is_ok(); Ok(NativeResult::ok( - cost, + cost.into(), smallvec![Value::bool(verify_result)], )) } diff --git a/language/documentation/examples/diem-framework/move-packages/DPN/sources/CRSN.move b/language/documentation/examples/diem-framework/move-packages/DPN/sources/CRSN.move index 9fabd60bd1..7880280dcc 100644 --- a/language/documentation/examples/diem-framework/move-packages/DPN/sources/CRSN.move +++ b/language/documentation/examples/diem-framework/move-packages/DPN/sources/CRSN.move @@ -194,8 +194,9 @@ module DiemFramework::CRSN { } spec force_expire { - let addr = signer::address_of(account); - ensures global(addr).min_nonce == old(global(addr)).min_nonce + shift_amount; + // TODO: this ensures may not hold + // let addr = signer::address_of(account); + // ensures global(addr).min_nonce == old(global(addr)).min_nonce + shift_amount; } /// Return whether this address has a CRSN resource published under it. public fun has_crsn(addr: address): bool { diff --git a/language/documentation/examples/experimental/basic-coin/sources/BasicCoin.move b/language/documentation/examples/experimental/basic-coin/sources/BasicCoin.move index a6aa8d2008..07afeca9a6 100644 --- a/language/documentation/examples/experimental/basic-coin/sources/BasicCoin.move +++ b/language/documentation/examples/experimental/basic-coin/sources/BasicCoin.move @@ -1,6 +1,6 @@ /// This module defines a minimal and generic Coin and Balance. module BasicCoin::BasicCoin { - use std::errors; + use std::error; use std::signer; /// Error codes @@ -20,7 +20,7 @@ module BasicCoin::BasicCoin { public fun publish_balance(account: &signer) { let empty_coin = Coin { value: 0 }; - assert!(!exists>(signer::address_of(account)), errors::already_published(EALREADY_HAS_BALANCE)); + assert!(!exists>(signer::address_of(account)), error::already_exists(EALREADY_HAS_BALANCE)); move_to(account, Balance { coin: empty_coin }); } diff --git a/language/documentation/examples/experimental/coin-swap/sources/CoinSwap.move b/language/documentation/examples/experimental/coin-swap/sources/CoinSwap.move index 6d92a57a5b..e97b7112ad 100644 --- a/language/documentation/examples/experimental/coin-swap/sources/CoinSwap.move +++ b/language/documentation/examples/experimental/coin-swap/sources/CoinSwap.move @@ -1,6 +1,6 @@ module CoinSwap::CoinSwap { use std::signer; - use std::errors; + use std::error; use BasicCoin::BasicCoin; use CoinSwap::PoolToken; @@ -27,8 +27,8 @@ module CoinSwap::CoinSwap { // TODO: Alternatively, `struct LiquidityPool` could be refactored to actually hold the coin (e.g., coin1: CoinType1). BasicCoin::publish_balance(coinswap); BasicCoin::publish_balance(coinswap); - assert!(signer::address_of(coinswap) == @CoinSwap, errors::invalid_argument(ECOINSWAP_ADDRESS)); - assert!(!exists>(signer::address_of(coinswap)), errors::already_published(EPOOL)); + assert!(signer::address_of(coinswap) == @CoinSwap, error::invalid_argument(ECOINSWAP_ADDRESS)); + assert!(!exists>(signer::address_of(coinswap)), error::already_exists(EPOOL)); move_to(coinswap, LiquidityPool{coin1, coin2, share}); // Transfer the initial liquidity of CoinType1 and CoinType2 to the pool under @CoinSwap. @@ -53,8 +53,8 @@ module CoinSwap::CoinSwap { witness1: CoinType1, witness2: CoinType2 ) acquires LiquidityPool { - assert!(signer::address_of(coinswap) == @CoinSwap, errors::invalid_argument(ECOINSWAP_ADDRESS)); - assert!(exists>(signer::address_of(coinswap)), errors::not_published(EPOOL)); + assert!(signer::address_of(coinswap) == @CoinSwap, error::invalid_argument(ECOINSWAP_ADDRESS)); + assert!(exists>(signer::address_of(coinswap)), error::not_found(EPOOL)); let pool = borrow_global_mut>(signer::address_of(coinswap)); let coin2 = get_input_price(coin1, pool.coin1, pool.coin2); pool.coin1 = pool.coin1 + coin1; diff --git a/language/documentation/spec/vm.md b/language/documentation/spec/vm.md index 7b4588f1c5..c35dfc5f00 100644 --- a/language/documentation/spec/vm.md +++ b/language/documentation/spec/vm.md @@ -75,7 +75,7 @@ pub fn publish_module( &mut self, module: Vec, sender: AccountAddress, - gas_status: &mut GasStatus, + gas_status: &mut impl GasMeter, ) -> VMResult<()>; ``` @@ -91,7 +91,7 @@ the module](#References-to-Data-and-Code). If the two addresses do not match, an error with `StatusCode::MODULE_ADDRESS_DOES_NOT_MATCH_SENDER` is returned. * Check that the module is not already published: Code is immutable in -Move. An attempt to overwrite an exiting module results in an error with +Move. An attempt to overwrite an existing module results in an error with `StatusCode::DUPLICATE_MODULE_NAME`. * Verify loading: The VM performs [verification](#Verification) of the @@ -120,7 +120,7 @@ pub fn execute_script( ty_args: Vec, args: Vec>, senders: Vec, - gas_status: &mut GasStatus, + gas_status: &mut impl GasMeter, ) -> VMResult<()>; ``` @@ -170,7 +170,7 @@ pub fn execute_script_function( ty_args: Vec, args: Vec>, senders: Vec, - gas_status: &mut GasStatus, + gas_status: &mut impl GasMeter, ) -> VMResult<()>; ``` @@ -201,7 +201,7 @@ pub fn execute_function( function_name: &IdentStr, ty_args: Vec, args: Vec>, - gas_status: &mut GasStatus, + gas_status: &mut impl GasMeter, ) -> VMResult<()>; ``` diff --git a/language/documentation/tutorial/README.md b/language/documentation/tutorial/README.md index 55c09debc8..3c178a7dc7 100644 --- a/language/documentation/tutorial/README.md +++ b/language/documentation/tutorial/README.md @@ -62,8 +62,10 @@ You should see something like this along with a list and description of a number of commands: ``` -move-package -Execute a package command. Executed in the current directory or the closest containing Move package +move-cli 0.1.0 +Diem Association +MoveCLI is the CLI that will be executed by the `move-cli` command The `cmd` argument is added here +rather than in `Move` to make it easier for other crates to extend `move-cli` USAGE: move [OPTIONS] @@ -120,7 +122,7 @@ module 0xCAFE::BasicCoin { This is defining a Move [module](https://move-language.github.io/move/modules-and-scripts.html). Modules are the -building block of Move code, and are defined with a specific address -- the +building blocks of Move code, and are defined with a specific address -- the address that the module can be published under. In this case, the `BasicCoin` module can only be published under `0xCAFE`. @@ -215,7 +217,7 @@ to [`step_2/BasicCoin`](./step_2/BasicCoin). Unit tests in Move are similar to unit tests in Rust if you're familiar with them -- tests are annotated with `#[test]` and written like normal Move functions. -You can run the tests with the `package test` command: +You can run the tests with the `move test` command: ```bash move test @@ -232,7 +234,7 @@ module 0xCAFE::BasicCoin { // address value of `0xC0FFEE`. #[test(account = @0xC0FFEE)] fun test_mint_10(account: signer) acquires Coin { - let addr = signer::address_of(&account); + let addr = 0x1::signer::address_of(&account); mint(account, 10); // Make sure there is a `Coin` resource under `addr` with a value of `10`. // We can access this resource and its value since we are in the @@ -260,7 +262,7 @@ assertion fails the unit test will fail. ```toml [dependencies] - MoveStdlib = { local = "../../../../move-stdlib/", addr_subst = { "Std" = "0x1" } } + MoveStdlib = { local = "../../../../move-stdlib/", addr_subst = { "std" = "0x1" } } ``` Note that you may need to alter the path to point to the `move-stdlib` directory under @@ -273,24 +275,18 @@ assertion fails the unit test will fail. pass to the `move test` command that will show you the global state when the test fails. It should look something like this: ``` - ┌── test_mint_10 ────── - │ error[E11001]: test failure - │ ┌─ ./sources/FirstModule.move:24:9 - │ │ - │ 18 │ fun test_mint_10(account: signer) acquires Coin { - │ │ ------------ In this function in 0xcafe::BasicCoin - │ · - │ 24 │ assert!(borrow_global(addr).value == 11, 0); - │ │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Test was not expected to abort but it aborted with 0 here - │ - │ - │ ────── Storage state at point of failure ────── - │ 0xc0ffee: - │ => key 0xcafe::BasicCoin::Coin { - │ value: 10 - │ } - │ - └────────────────── + ┌── test_mint_10 ────── + │ error[E11001]: test failure + │ ┌─ ./sources/FirstModule.move:24:9 + │ │ + │ 18 │ fun test_mint_10(account: signer) acquires Coin { + │ │ ------------ In this function in 0xcafe::BasicCoin + │ · + │ 24 │ assert!(borrow_global(addr).value == 11, 0); + │ │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Test was not expected to abort but it aborted with 0 here + │ + │ + └────────────────── ``` * Find a flag that allows you to gather test coverage information, and then play around with using the `move coverage` command to look at @@ -413,11 +409,13 @@ assert!(signer::address_of(&module_owner) == MODULE_OWNER, errors::requires_addr ``` Assert statements in Move can be used in this way: `assert!(, );`. This means that if the `` is false, then abort the transaction with ``. Here `MODULE_OWNER` and `ENOT_MODULE_OWNER` are both constants -defined at the beginning of the module. And `errors` module defines common error categories we can use. +defined at the beginning of the module. The standard library's [`error` module] also defines common error categories we can use. It is important to note that Move is transactional in its execution -- so if an [abort](https://move-language.github.io/move/abort-and-assert.html) is raised no unwinding of state needs to be performed, as no changes from that transaction will be persisted to the blockchain. +[`error` module]: https://github.com/move-language/move/blob/main/language/move-stdlib/docs/error.md + We then deposit a coin with value `amount` to the balance of `mint_addr`. ``` deposit(mint_addr, Coin { value: amount }); @@ -571,7 +569,7 @@ source ~/.profile ``` ## Step 7: Use the Move prover -Smart contracts deployed on the blockchain may maniputate high-value assets. As a technique that uses strict +Smart contracts deployed on the blockchain may manipulate high-value assets. As a technique that uses strict mathematical methods to describe behavior and reason correctness of computer systems, formal verification has been used in blockchains to prevent bugs in smart contracts. [ The Move prover](https://github.com/move-language/move/blob/main/language/move-prover/doc/user/prover-guide.md) diff --git a/language/documentation/tutorial/step_4_sol/BasicCoin/sources/BasicCoin.move b/language/documentation/tutorial/step_4_sol/BasicCoin/sources/BasicCoin.move index 6c6cae371e..3b41ce5938 100644 --- a/language/documentation/tutorial/step_4_sol/BasicCoin/sources/BasicCoin.move +++ b/language/documentation/tutorial/step_4_sol/BasicCoin/sources/BasicCoin.move @@ -58,7 +58,7 @@ module NamedAddr::BasicCoin { } /// Deposit `amount` number of tokens to the balance under `addr`. - fun deposit(addr: address, check: Coin) acquires Balance{ + fun deposit(addr: address, check: Coin) acquires Balance { let balance = balance_of(addr); let balance_ref = &mut borrow_global_mut(addr).coin.value; let Coin { value } = check; diff --git a/language/documentation/tutorial/step_5/BasicCoin/sources/BasicCoin.move b/language/documentation/tutorial/step_5/BasicCoin/sources/BasicCoin.move index 488c3ba2d4..f901557178 100644 --- a/language/documentation/tutorial/step_5/BasicCoin/sources/BasicCoin.move +++ b/language/documentation/tutorial/step_5/BasicCoin/sources/BasicCoin.move @@ -91,13 +91,13 @@ module NamedAddr::BasicCoin { } #[test(account = @0x1)] - #[expected_failure(abort_code = 518)] // Can specify an abort code + #[expected_failure(abort_code = 2)] // Can specify an abort code fun publish_balance_already_exists(account: signer) { publish_balance(&account); publish_balance(&account); } - // EXERCISE: Write `balance_dne` test here! + // EXERCISE: Write `balance_of_dne` test here! #[test] #[expected_failure] diff --git a/language/documentation/tutorial/step_5_sol/BasicCoin/sources/BasicCoin.move b/language/documentation/tutorial/step_5_sol/BasicCoin/sources/BasicCoin.move index 496c7721af..a6e31ba552 100644 --- a/language/documentation/tutorial/step_5_sol/BasicCoin/sources/BasicCoin.move +++ b/language/documentation/tutorial/step_5_sol/BasicCoin/sources/BasicCoin.move @@ -91,7 +91,7 @@ module NamedAddr::BasicCoin { } #[test(account = @0x1)] - #[expected_failure(abort_code = 518)] // Can specify an abort code + #[expected_failure(abort_code = 2)] // Can specify an abort code fun publish_balance_already_exists(account: signer) { publish_balance(&account); publish_balance(&account); diff --git a/language/evm/README.md b/language/evm/README.md index 6baba04f3d..883e3bd883 100644 --- a/language/evm/README.md +++ b/language/evm/README.md @@ -1,5 +1,9 @@ # Move-on-EVM +> NOTE: this tree contains an experimental version of Move which runs on the EVM. The programming model is +> different from regular Move. The examples in this directory do not work with the usual Move +> tools and blockchains. + "Move-on-EVM" is a programming model in Move for EVM. In the current model, *each Move EVM contract has its own isolated address space*. This reflects the setup of the EVM most naturally, where storage between contracts cannot be shared apart from via accessor contract functions. Move EVM contracts use attributes to indicate the usage of structs for storage and events, and for functions to be callable from other contracts. It is expected that there is some codegen of Move from these attributes. For example, functions marked as `callable` have a generated API for cross-contract EVM call and delegate invocations. The module [Evm.move](./stdlib/sources/Evm.move) contains the API of a Move contract to the EVM. It encapsulates access to the transaction context and other EVM builtins This directory contains the following sub-directories: diff --git a/language/evm/exec-utils/src/compile.rs b/language/evm/exec-utils/src/compile.rs index 59f4992f5d..a254cec9d3 100644 --- a/language/evm/exec-utils/src/compile.rs +++ b/language/evm/exec-utils/src/compile.rs @@ -114,7 +114,7 @@ pub fn solc_yul(source: &str, return_optimized_yul: bool) -> Result<(Vec, Op } let yul = if return_optimized_yul { Some( - (&out_str[(start_of_yul.unwrap() + OPTIMIZED_YUL_MARKER.len())..start_of_hex.unwrap()]) + out_str[(start_of_yul.unwrap() + OPTIMIZED_YUL_MARKER.len())..start_of_hex.unwrap()] .trim() .to_string(), ) diff --git a/language/evm/extract-ethereum-abi/Cargo.toml b/language/evm/extract-ethereum-abi/Cargo.toml index d40acabe04..7ae124f1be 100644 --- a/language/evm/extract-ethereum-abi/Cargo.toml +++ b/language/evm/extract-ethereum-abi/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" authors = ["Diem Association "] description = "Extract Etherem ABI" publish = false -edition = "2018" +edition = "2021" license = "Apache-2.0" [dependencies] diff --git a/language/evm/move-ethereum-abi/Cargo.toml b/language/evm/move-ethereum-abi/Cargo.toml index c837746ad9..329e78cec5 100644 --- a/language/evm/move-ethereum-abi/Cargo.toml +++ b/language/evm/move-ethereum-abi/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" authors = ["Diem Association "] description = "Move Ethereum ABI" publish = false -edition = "2018" +edition = "2021" license = "Apache-2.0" [dependencies] diff --git a/language/evm/move-to-yul/Cargo.toml b/language/evm/move-to-yul/Cargo.toml index 34f28a7dcf..40b2798ec6 100644 --- a/language/evm/move-to-yul/Cargo.toml +++ b/language/evm/move-to-yul/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" authors = ["Diem Association "] description = "Move Solidity Generator" publish = false -edition = "2018" +edition = "2021" license = "Apache-2.0" [dependencies] diff --git a/language/evm/move-to-yul/src/abi_move_metadata.rs b/language/evm/move-to-yul/src/abi_move_metadata.rs index d709309752..9d1b42908c 100644 --- a/language/evm/move-to-yul/src/abi_move_metadata.rs +++ b/language/evm/move-to-yul/src/abi_move_metadata.rs @@ -26,7 +26,7 @@ pub(crate) fn generate_abi_move_metadata(ctx: &Context, receive: bool, fallback: let st_env = ctx.env.get_struct(key.to_qualified_id()); event_map.insert( st_env.get_identifier().unwrap().to_string(), - from_event_sig(ctx.event_signature_map.borrow().get(&key).unwrap()), + from_event_sig(ctx.event_signature_map.borrow().get(key).unwrap()), ); } @@ -34,7 +34,7 @@ pub(crate) fn generate_abi_move_metadata(ctx: &Context, receive: bool, fallback: let mut func_map = BTreeMap::new(); for (key, (solidity_sig, attr)) in ctx.callable_function_map.borrow().iter() { let fun = ctx.env.get_function(key.to_qualified_id()); - let abi_sig = from_solidity_sig(&solidity_sig, Some(*attr), "function"); + let abi_sig = from_solidity_sig(solidity_sig, Some(*attr), "function"); func_map.insert(fun.get_identifier().to_string(), abi_sig); } diff --git a/language/evm/move-to-yul/src/attributes.rs b/language/evm/move-to-yul/src/attributes.rs index 39780fb803..3a82877a4a 100644 --- a/language/evm/move-to-yul/src/attributes.rs +++ b/language/evm/move-to-yul/src/attributes.rs @@ -147,14 +147,7 @@ pub fn has_attr(env: &GlobalEnv, attrs: &[Attribute], name: &str, simple_flag: b true } }; - attrs.iter().any(|a| match a { - Attribute::Apply(_, s, args) - if is_empty(args) && env.symbol_pool().string(*s).as_str() == name => - { - true - } - _ => false, - }) + attrs.iter().any(|a| matches!(a, Attribute::Apply(_, s, args) if is_empty(args) && env.symbol_pool().string(*s).as_str() == name)) } /// Check whether the module has a `#[evm_contract]` attribute. diff --git a/language/evm/move-to-yul/src/context.rs b/language/evm/move-to-yul/src/context.rs index 7cc5776852..c4671c3841 100644 --- a/language/evm/move-to-yul/src/context.rs +++ b/language/evm/move-to-yul/src/context.rs @@ -354,8 +354,7 @@ impl<'a> Context<'a> { fn get_target_structs(&self, p: impl Fn(&StructEnv) -> bool) -> Vec> { self.env .get_modules() - .map(|m| m.into_structs().filter(|f| p(f))) - .flatten() + .flat_map(|m| m.into_structs().filter(|f| p(f))) .collect() } @@ -608,7 +607,7 @@ impl<'a> Context<'a> { .get_local_name(idx) .display(target.symbol_pool()) .to_string() - .replace("#", "_") + .replace('#', "_") } /// Make name for a result. diff --git a/language/evm/move-to-yul/src/dispatcher_generator.rs b/language/evm/move-to-yul/src/dispatcher_generator.rs index 64c9e5be29..2ecc93d8b8 100644 --- a/language/evm/move-to-yul/src/dispatcher_generator.rs +++ b/language/evm/move-to-yul/src/dispatcher_generator.rs @@ -261,8 +261,8 @@ impl Generator { /// Generate optional receive function. fn optional_receive(&mut self, ctx: &Context, receive: &Option>) -> bool { if let Some(receive) = receive { - ctx.check_no_generics(&receive); - if !attributes::is_payable_fun(&receive) { + ctx.check_no_generics(receive); + if !attributes::is_payable_fun(receive) { ctx.env .error(&receive.get_loc(), "receive function must be payable") } @@ -309,8 +309,8 @@ impl Generator { fallback: &Option>, ) { if let Some(fallback) = fallback { - ctx.check_no_generics(&fallback); - if !attributes::is_payable_fun(&fallback) { + ctx.check_no_generics(fallback); + if !attributes::is_payable_fun(fallback) { self.generate_call_value_check(ctx, REVERT_ERR_NON_PAYABLE_FUN); } let fun_id = &fallback @@ -560,7 +560,7 @@ impl Generator { ); }); head_pos += ty_size; - let memory_func = ctx.memory_store_builtin_fun(&move_ty); + let memory_func = ctx.memory_store_builtin_fun(move_ty); if local_typ_var.len() == 1 { gen.call_builtin( ctx, @@ -1454,7 +1454,7 @@ impl Generator { { let is_static = ty.is_static(); let local_typ_var = vec![ret_var[stack_pos].clone()]; - let memory_func = ctx.memory_load_builtin_fun(&move_ty); + let memory_func = ctx.memory_load_builtin_fun(move_ty); if local_typ_var.len() == 1 { emitln!( ctx.writer, @@ -1474,7 +1474,7 @@ impl Generator { ctx, &ty.clone(), &SignatureDataLocation::Memory, - &move_ty, + move_ty, sub_option.clone() ), local_typ_var[0].clone() @@ -1488,7 +1488,7 @@ impl Generator { ctx, &ty.clone(), &SignatureDataLocation::Memory, - &move_ty, + move_ty, sub_option.clone() ), local_typ_var[0].clone(), @@ -1507,7 +1507,7 @@ impl Generator { ctx, &ty.clone(), &SignatureDataLocation::Memory, - &move_ty, + move_ty, sub_option.clone() ), local_typ_var[0].clone() diff --git a/language/evm/move-to-yul/src/functions.rs b/language/evm/move-to-yul/src/functions.rs index 80a9ecd94b..df46ed2860 100644 --- a/language/evm/move-to-yul/src/functions.rs +++ b/language/evm/move-to-yul/src/functions.rs @@ -562,6 +562,7 @@ impl<'a> FunctionGenerator<'a> { | EventStoreDiverge | OpaqueCallBegin(_, _, _) | OpaqueCallEnd(_, _, _) + | Uninit | Havoc(_) | Stop | TraceGlobalMem(_) => {} @@ -613,6 +614,7 @@ impl<'a> FunctionGenerator<'a> { format!("0x{}", a.to_str_radix(16)) } Constant::ByteArray(_) => "".to_string(), + Constant::AddressArray(_) => "".to_string(), }; if !val_str.is_empty() { emitln!(ctx.writer, "{} := {}", dest, val_str); diff --git a/language/evm/move-to-yul/src/solidity_ty.rs b/language/evm/move-to-yul/src/solidity_ty.rs index 56b608bf22..0cb3799b18 100644 --- a/language/evm/move-to-yul/src/solidity_ty.rs +++ b/language/evm/move-to-yul/src/solidity_ty.rs @@ -398,7 +398,7 @@ impl SolidityType { let error_msg = "illegal type name"; return Err(anyhow!(error_msg)); } - ctx.check_or_create_struct_abi(&trimmed_ty_str) + ctx.check_or_create_struct_abi(trimmed_ty_str) } } diff --git a/language/evm/move-to-yul/src/storage.rs b/language/evm/move-to-yul/src/storage.rs index 4832fb5a17..0643afc2ca 100644 --- a/language/evm/move-to-yul/src/storage.rs +++ b/language/evm/move-to-yul/src/storage.rs @@ -379,7 +379,8 @@ impl Generator { ); // Skip the existence flag and create a pointer. - let make_ptr = self.call_builtin_str( + + self.call_builtin_str( ctx, YulFunction::MakePtr, vec![ @@ -387,8 +388,7 @@ impl Generator { format!("add({}, ${{RESOURCE_EXISTS_FLAG_SIZE}})", base_offset), ] .into_iter(), - ); - make_ptr + ) } /// Returns an expression for checking whether a resource exists. diff --git a/language/evm/move-to-yul/src/vectors.rs b/language/evm/move-to-yul/src/vectors.rs index df3fe9f41d..551330d5b9 100644 --- a/language/evm/move-to-yul/src/vectors.rs +++ b/language/evm/move-to-yul/src/vectors.rs @@ -1051,7 +1051,7 @@ fn define_destroy_empty_fun( /// Generate equality method for the vector type. pub(crate) fn equality_fun(gen: &mut Generator, ctx: &Context, ty: &Type) { - let elem_type = get_elem_type(&ty).unwrap(); + let elem_type = get_elem_type(ty).unwrap(); if ctx.type_allocates_memory(&elem_type) { emitln!( ctx.writer, diff --git a/language/evm/move-to-yul/tests/TestABINative.exp b/language/evm/move-to-yul/tests/TestABINative.exp index e38fd0bc67..dbeb714ea1 100644 --- a/language/evm/move-to-yul/tests/TestABINative.exp +++ b/language/evm/move-to-yul/tests/TestABINative.exp @@ -153,25 +153,20 @@ object "test_A2_M_test_decode_two_bytes1" { $t26 := 1 // $t27 := ==($t25, $t26) $t27 := $Eq($t25, $t26) - // if ($t27) goto L8 else goto L10 + // if ($t27) goto L8 else goto L9 switch $t27 - case 0 { $block := 10 } - default { $block := 9 } + case 0 { $block := 9 } + default { $block := 10 } } case 9 { - // label L8 - // goto L11 - $block := 11 - } - case 10 { - // label L10 + // label L9 // $t28 := 101 $t28 := 101 // abort($t28) $Abort($t28) } - case 11 { - // label L11 + case 10 { + // label L8 // $t29 := borrow_local($t4) $t29 := $MakePtr(false, add($locals, 64)) // $t30 := vector::length($t29) @@ -180,25 +175,20 @@ object "test_A2_M_test_decode_two_bytes1" { $t31 := 1 // $t32 := ==($t30, $t31) $t32 := $Eq($t30, $t31) - // if ($t32) goto L12 else goto L14 + // if ($t32) goto L10 else goto L11 switch $t32 - case 0 { $block := 13 } + case 0 { $block := 11 } default { $block := 12 } } - case 12 { - // label L12 - // goto L15 - $block := 14 - } - case 13 { - // label L14 + case 11 { + // label L11 // $t33 := 102 $t33 := 102 // abort($t33) $Abort($t33) } - case 14 { - // label L15 + case 12 { + // label L10 // $t34 := borrow_local($t3) $t34 := $MakePtr(false, add($locals, 32)) // $t35 := 0 @@ -211,25 +201,20 @@ object "test_A2_M_test_decode_two_bytes1" { $t38 := 42 // $t39 := ==($t37, $t38) $t39 := $Eq($t37, $t38) - // if ($t39) goto L16 else goto L18 + // if ($t39) goto L12 else goto L13 switch $t39 - case 0 { $block := 16 } - default { $block := 15 } - } - case 15 { - // label L16 - // goto L19 - $block := 17 + case 0 { $block := 13 } + default { $block := 14 } } - case 16 { - // label L18 + case 13 { + // label L13 // $t40 := 103 $t40 := 103 // abort($t40) $Abort($t40) } - case 17 { - // label L19 + case 14 { + // label L12 // $t41 := borrow_local($t4) $t41 := $MakePtr(false, add($locals, 64)) // $t42 := 0 @@ -242,25 +227,20 @@ object "test_A2_M_test_decode_two_bytes1" { $t45 := 43 // $t46 := ==($t44, $t45) $t46 := $Eq($t44, $t45) - // if ($t46) goto L20 else goto L22 + // if ($t46) goto L14 else goto L15 switch $t46 - case 0 { $block := 19 } - default { $block := 18 } + case 0 { $block := 15 } + default { $block := 16 } } - case 18 { - // label L20 - // goto L23 - $block := 20 - } - case 19 { - // label L22 + case 15 { + // label L15 // $t47 := 104 $t47 := 104 // abort($t47) $Abort($t47) } - case 20 { - // label L23 + case 16 { + // label L14 // return () $Free($locals, 96) leave @@ -604,7 +584,7 @@ object "test_A2_M_test_decode_two_bytes1" { } } } -===> Test result of M::test_decode_two_bytes1: Succeed(Stopped) (used_gas=66864): [] +===> Test result of M::test_decode_two_bytes1: Succeed(Stopped) (used_gas=65452): [] // test of M::test_decode_two_u8 /* ======================================= @@ -716,48 +696,38 @@ object "test_A2_M_test_decode_two_u8" { $t24 := 42 // $t25 := ==($t22, $t24) $t25 := $Eq($t22, $t24) - // if ($t25) goto L8 else goto L10 + // if ($t25) goto L8 else goto L9 switch $t25 - case 0 { $block := 10 } - default { $block := 9 } + case 0 { $block := 9 } + default { $block := 10 } } case 9 { - // label L8 - // goto L11 - $block := 11 - } - case 10 { - // label L10 + // label L9 // $t26 := 101 $t26 := 101 // abort($t26) $Abort($t26) } - case 11 { - // label L11 + case 10 { + // label L8 // $t27 := 43 $t27 := 43 // $t28 := ==($t23, $t27) $t28 := $Eq($t23, $t27) - // if ($t28) goto L12 else goto L14 + // if ($t28) goto L10 else goto L11 switch $t28 - case 0 { $block := 13 } + case 0 { $block := 11 } default { $block := 12 } } - case 12 { - // label L12 - // goto L15 - $block := 14 - } - case 13 { - // label L14 + case 11 { + // label L11 // $t29 := 102 $t29 := 102 // abort($t29) $Abort($t29) } - case 14 { - // label L15 + case 12 { + // label L10 // return () $Free($locals, 32) leave @@ -1041,7 +1011,7 @@ object "test_A2_M_test_decode_two_u8" { } } } -===> Test result of M::test_decode_two_u8: Succeed(Stopped) (used_gas=64646): [] +===> Test result of M::test_decode_two_u8: Succeed(Stopped) (used_gas=64116): [] // test of M::test_encode_packed_string /* ======================================= @@ -1060,17 +1030,38 @@ object "test_A2_M_test_encode_packed_string" { for {} true {} { switch $block case 2 { - // label L0 - // goto L3 - $block := 5 - } - case 3 { - // label L2 + // label L1 // $t5 := 100 $t5 := 100 // abort($t5) $Abort($t5) } + case 3 { + // label L0 + // $t6 := [49] + $t6 := $Malloc(add(32, $ClosestGreaterPowerOfTwo(1))) + $MemoryStoreU64($t6, 1) + $MemoryStoreU64(add($t6, 8), $ClosestGreaterPowerOfTwo(1)) + copy_literal_string_to_memory_2868747976(add($t6, 32)) + // $t7 := [50] + $t7 := $Malloc(add(32, $ClosestGreaterPowerOfTwo(1))) + $MemoryStoreU64($t7, 1) + $MemoryStoreU64(add($t7, 8), $ClosestGreaterPowerOfTwo(1)) + copy_literal_string_to_memory_4015750317(add($t7, 32)) + // $t8 := M::encode_packed_string($t6, $t7) + $t8 := A2_M_encode_packed_string($t6, $t7) + // $t9 := [49, 50] + $t9 := $Malloc(add(32, $ClosestGreaterPowerOfTwo(2))) + $MemoryStoreU64($t9, 2) + $MemoryStoreU64(add($t9, 8), $ClosestGreaterPowerOfTwo(2)) + copy_literal_string_to_memory_141265791(add($t9, 32)) + // $t10 := ==($t8, $t9) + $t10 := $Eq_$vec$u8$$($t8, $t9) + // if ($t10) goto L2 else goto L3 + switch $t10 + case 0 { $block := 5 } + default { $block := 6 } + } case 4 { // $t0 := [] $t0 := $Malloc(add(32, $ClosestGreaterPowerOfTwo(0))) @@ -1091,51 +1082,20 @@ object "test_A2_M_test_encode_packed_string" { copy_literal_string_to_memory_21418693(add($t3, 32)) // $t4 := ==($t2, $t3) $t4 := $Eq_$vec$u8$$($t2, $t3) - // if ($t4) goto L0 else goto L2 + // if ($t4) goto L0 else goto L1 switch $t4 - case 0 { $block := 3 } - default { $block := 2 } + case 0 { $block := 2 } + default { $block := 3 } } case 5 { // label L3 - // $t6 := [49] - $t6 := $Malloc(add(32, $ClosestGreaterPowerOfTwo(1))) - $MemoryStoreU64($t6, 1) - $MemoryStoreU64(add($t6, 8), $ClosestGreaterPowerOfTwo(1)) - copy_literal_string_to_memory_2868747976(add($t6, 32)) - // $t7 := [50] - $t7 := $Malloc(add(32, $ClosestGreaterPowerOfTwo(1))) - $MemoryStoreU64($t7, 1) - $MemoryStoreU64(add($t7, 8), $ClosestGreaterPowerOfTwo(1)) - copy_literal_string_to_memory_4015750317(add($t7, 32)) - // $t8 := M::encode_packed_string($t6, $t7) - $t8 := A2_M_encode_packed_string($t6, $t7) - // $t9 := [49, 50] - $t9 := $Malloc(add(32, $ClosestGreaterPowerOfTwo(2))) - $MemoryStoreU64($t9, 2) - $MemoryStoreU64(add($t9, 8), $ClosestGreaterPowerOfTwo(2)) - copy_literal_string_to_memory_141265791(add($t9, 32)) - // $t10 := ==($t8, $t9) - $t10 := $Eq_$vec$u8$$($t8, $t9) - // if ($t10) goto L4 else goto L6 - switch $t10 - case 0 { $block := 7 } - default { $block := 6 } - } - case 6 { - // label L4 - // goto L7 - $block := 8 - } - case 7 { - // label L6 // $t11 := 101 $t11 := 101 // abort($t11) $Abort($t11) } - case 8 { - // label L7 + case 6 { + // label L2 // $t12 := [] $t12 := $Malloc(add(32, $ClosestGreaterPowerOfTwo(0))) $MemoryStoreU64($t12, 0) @@ -1155,25 +1115,20 @@ object "test_A2_M_test_encode_packed_string" { copy_literal_string_to_memory_2053440334(add($t15, 32)) // $t16 := ==($t14, $t15) $t16 := $Eq_$vec$u8$$($t14, $t15) - // if ($t16) goto L8 else goto L10 + // if ($t16) goto L4 else goto L5 switch $t16 - case 0 { $block := 10 } - default { $block := 9 } - } - case 9 { - // label L8 - // goto L11 - $block := 11 + case 0 { $block := 7 } + default { $block := 8 } } - case 10 { - // label L10 + case 7 { + // label L5 // $t17 := 102 $t17 := 102 // abort($t17) $Abort($t17) } - case 11 { - // label L11 + case 8 { + // label L4 // $t18 := [97] $t18 := $Malloc(add(32, $ClosestGreaterPowerOfTwo(1))) $MemoryStoreU64($t18, 1) @@ -1200,25 +1155,20 @@ object "test_A2_M_test_encode_packed_string" { copy_literal_string_to_memory_3871831907(add($t23, 32)) // $t24 := ==($t22, $t23) $t24 := $Eq_$vec$u8$$($t22, $t23) - // if ($t24) goto L12 else goto L14 + // if ($t24) goto L6 else goto L7 switch $t24 - case 0 { $block := 13 } - default { $block := 12 } + case 0 { $block := 9 } + default { $block := 10 } } - case 12 { - // label L12 - // goto L15 - $block := 14 - } - case 13 { - // label L14 + case 9 { + // label L7 // $t25 := 103 $t25 := 103 // abort($t25) $Abort($t25) } - case 14 { - // label L15 + case 10 { + // label L6 // $t26 := [116, 101, 115, 116] $t26 := $Malloc(add(32, $ClosestGreaterPowerOfTwo(4))) $MemoryStoreU64($t26, 4) @@ -1238,25 +1188,20 @@ object "test_A2_M_test_encode_packed_string" { copy_literal_string_to_memory_1610556060(add($t29, 32)) // $t30 := ==($t28, $t29) $t30 := $Eq_$vec$u8$$($t28, $t29) - // if ($t30) goto L16 else goto L18 + // if ($t30) goto L8 else goto L9 switch $t30 - case 0 { $block := 16 } - default { $block := 15 } - } - case 15 { - // label L16 - // goto L19 - $block := 17 + case 0 { $block := 11 } + default { $block := 12 } } - case 16 { - // label L18 + case 11 { + // label L9 // $t31 := 104 $t31 := 104 // abort($t31) $Abort($t31) } - case 17 { - // label L19 + case 12 { + // label L8 // return () leave } @@ -1443,7 +1388,7 @@ object "test_A2_M_test_encode_packed_string" { } } } -===> Test result of M::test_encode_packed_string: Succeed(Stopped) (used_gas=10647): [] +===> Test result of M::test_encode_packed_string: Succeed(Stopped) (used_gas=9310): [] // test of M::test_encode_packed_uint16 /* ======================================= @@ -1463,17 +1408,18 @@ object "test_A2_M_test_encode_packed_uint16" { for {} true {} { switch $block case 2 { - // label L0 - // goto L3 - $block := 5 - } - case 3 { - // label L2 + // label L1 // $t9 := 101 $t9 := 101 // abort($t9) $Abort($t9) } + case 3 { + // label L0 + // return () + $Free($locals, 32) + leave + } case 4 { // $t3 := 41 $t3 := 41 @@ -1489,16 +1435,10 @@ object "test_A2_M_test_encode_packed_uint16" { $t7 := 4 // $t8 := ==($t6, $t7) $t8 := $Eq($t6, $t7) - // if ($t8) goto L0 else goto L2 + // if ($t8) goto L0 else goto L1 switch $t8 - case 0 { $block := 3 } - default { $block := 2 } - } - case 5 { - // label L3 - // return () - $Free($locals, 32) - leave + case 0 { $block := 2 } + default { $block := 3 } } } } @@ -1668,7 +1608,7 @@ object "test_A2_M_test_encode_packed_uint16" { } } } -===> Test result of M::test_encode_packed_uint16: Succeed(Stopped) (used_gas=776): [] +===> Test result of M::test_encode_packed_uint16: Succeed(Stopped) (used_gas=693): [] // test of M::test_marshalling_two_bytes1 /* ======================================= @@ -1782,25 +1722,20 @@ object "test_A2_M_test_marshalling_two_bytes1" { $t26 := mload($locals) // $t27 := ==($t26, $t25) $t27 := $Eq_$vec$u8$$($t26, $t25) - // if ($t27) goto L8 else goto L10 + // if ($t27) goto L8 else goto L9 switch $t27 - case 0 { $block := 10 } - default { $block := 9 } + case 0 { $block := 9 } + default { $block := 10 } } case 9 { - // label L8 - // goto L11 - $block := 11 - } - case 10 { - // label L10 + // label L9 // $t28 := 101 $t28 := 101 // abort($t28) $Abort($t28) } - case 11 { - // label L11 + case 10 { + // label L8 // return () $Free($locals, 32) leave @@ -2161,7 +2096,7 @@ object "test_A2_M_test_marshalling_two_bytes1" { } } } -===> Test result of M::test_marshalling_two_bytes1: Succeed(Stopped) (used_gas=66247): [] +===> Test result of M::test_marshalling_two_bytes1: Succeed(Stopped) (used_gas=66026): [] // test of M::test_marshalling_two_u8 /* ======================================= @@ -2273,73 +2208,58 @@ object "test_A2_M_test_marshalling_two_u8" { $t25 := 42 // $t26 := ==($t23, $t25) $t26 := $Eq($t23, $t25) - // if ($t26) goto L8 else goto L10 + // if ($t26) goto L8 else goto L9 switch $t26 - case 0 { $block := 10 } - default { $block := 9 } + case 0 { $block := 9 } + default { $block := 10 } } case 9 { - // label L8 - // goto L11 - $block := 11 - } - case 10 { - // label L10 + // label L9 // $t27 := 101 $t27 := 101 // abort($t27) $Abort($t27) } - case 11 { - // label L11 + case 10 { + // label L8 // $t28 := 43 $t28 := 43 // $t29 := ==($t24, $t28) $t29 := $Eq($t24, $t28) - // if ($t29) goto L12 else goto L14 + // if ($t29) goto L10 else goto L11 switch $t29 - case 0 { $block := 13 } + case 0 { $block := 11 } default { $block := 12 } } - case 12 { - // label L12 - // goto L15 - $block := 14 - } - case 13 { - // label L14 + case 11 { + // label L11 // $t30 := 102 $t30 := 102 // abort($t30) $Abort($t30) } - case 14 { - // label L15 + case 12 { + // label L10 // $t31 := M::encode_two_u8($t23, $t24) $t31 := A2_M_encode_two_u8($t23, $t24) // $t32 := move($t2) $t32 := mload($locals) // $t33 := ==($t32, $t31) $t33 := $Eq_$vec$u8$$($t32, $t31) - // if ($t33) goto L16 else goto L18 + // if ($t33) goto L12 else goto L13 switch $t33 - case 0 { $block := 16 } - default { $block := 15 } - } - case 15 { - // label L16 - // goto L19 - $block := 17 + case 0 { $block := 13 } + default { $block := 14 } } - case 16 { - // label L18 + case 13 { + // label L13 // $t34 := 103 $t34 := 103 // abort($t34) $Abort($t34) } - case 17 { - // label L19 + case 14 { + // label L12 // return () $Free($locals, 32) leave @@ -2681,7 +2601,7 @@ object "test_A2_M_test_marshalling_two_u8" { } } } -===> Test result of M::test_marshalling_two_u8: Succeed(Stopped) (used_gas=66157): [] +===> Test result of M::test_marshalling_two_u8: Succeed(Stopped) (used_gas=65230): [] diff --git a/language/evm/move-to-yul/tests/testsuite.rs b/language/evm/move-to-yul/tests/testsuite.rs index 651eb3c264..f782f09153 100644 --- a/language/evm/move-to-yul/tests/testsuite.rs +++ b/language/evm/move-to-yul/tests/testsuite.rs @@ -19,6 +19,7 @@ use move_to_yul::{generator::Generator, options::Options}; use primitive_types::{H160, U256}; use std::{ collections::BTreeMap, + fmt::Write, path::{Path, PathBuf}, }; @@ -116,16 +117,19 @@ fn run_tests( let mut res = String::new(); res.push_str("!! Unit tests\n\n"); for (fun, source) in test_cases { - res.push_str(&format!( - "// test of {}\n", + writeln!( + &mut res, + "// test of {}", env.get_function(*fun).get_full_name_str() - )); + ) + .unwrap(); res.push_str(source); - res.push_str(&format!( - "===> Test result of {}: {}\n\n", + writeln!( + &mut res, + "===> Test result of {}: {}\n", env.get_function(*fun).get_full_name_str(), execute_test(env, source)? - )); + )?; } Ok(res) } diff --git a/language/extensions/async/move-async-vm/Cargo.toml b/language/extensions/async/move-async-vm/Cargo.toml index 812745d9d4..998e73da46 100644 --- a/language/extensions/async/move-async-vm/Cargo.toml +++ b/language/extensions/async/move-async-vm/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Diem Association "] description = "Wrapper for the Move VM for the asynchronous execution flavor" repository = "https://github.com/diem/move" license = "Apache-2.0" -edition = "2018" +edition = "2021" publish = false [dependencies] @@ -14,15 +14,17 @@ better_any = "0.1.1" walkdir = "2.3.1" itertools = "0.10.0" smallvec = "1.6.1" -bcs = "0.1.2" sha3 = "0.9.1" move-command-line-common = { path = "../../../move-command-line-common" } move-core-types = { path = "../../../move-core/types" } move-compiler = { path = "../../../move-compiler" } move-vm-types = { path = "../../../move-vm/types" } move-vm-runtime = { path = "../../../move-vm/runtime", features = ["debugging"] } +move-vm-test-utils = { path = "../../../move-vm/test-utils" } move-binary-format = { path = "../../../move-binary-format" } +bcs.workspace = true + [dev-dependencies] datatest-stable = "0.1.1" move-prover-test-utils = { path = "../../../move-prover/test-utils" } diff --git a/language/extensions/async/move-async-vm/src/async_vm.rs b/language/extensions/async/move-async-vm/src/async_vm.rs index 6b6cce61fb..72f98c27bb 100644 --- a/language/extensions/async/move-async-vm/src/async_vm.rs +++ b/language/extensions/async/move-async-vm/src/async_vm.rs @@ -11,8 +11,7 @@ use std::{ use move_binary_format::errors::{Location, PartialVMError, PartialVMResult, VMError, VMResult}; use move_core_types::{ account_address::AccountAddress, - effects::{ChangeSet, Event}, - gas_schedule::GasAlgebra, + effects::{ChangeSet, Event, Op}, identifier::Identifier, language_storage::{ModuleId, StructTag, TypeTag}, resolver::MoveResolver, @@ -24,13 +23,16 @@ use move_vm_runtime::{ native_functions::NativeFunction, session::{SerializedReturnValues, Session}, }; -use move_vm_types::{ - gas_schedule::GasStatus, - values::{Reference, Value}, +use move_vm_test_utils::gas_schedule::{Gas, GasStatus}; +use move_vm_types::values::{Reference, Value}; + +use crate::{ + actor_metadata, + actor_metadata::ActorMetadata, + natives, + natives::{AsyncExtension, GasParameters as ActorGasParameters}, }; -use crate::{actor_metadata, actor_metadata::ActorMetadata, natives, natives::AsyncExtension}; - /// Represents an instance of an async VM. pub struct AsyncVM { move_vm: MoveVM, @@ -40,7 +42,12 @@ pub struct AsyncVM { impl AsyncVM { /// Creates a new VM, registering the given natives and actors. - pub fn new(async_lib_addr: AccountAddress, natives: I, actors: A) -> VMResult + pub fn new( + async_lib_addr: AccountAddress, + natives: I, + actors: A, + actor_gas_parameters: ActorGasParameters, + ) -> VMResult where I: IntoIterator, A: IntoIterator, @@ -62,9 +69,9 @@ impl AsyncVM { .collect(); Ok(AsyncVM { move_vm: MoveVM::new( - natives - .into_iter() - .chain(natives::actor_natives(async_lib_addr).into_iter()), + natives.into_iter().chain( + natives::actor_natives(async_lib_addr, actor_gas_parameters).into_iter(), + ), )?, actor_metadata, message_table, @@ -138,7 +145,7 @@ pub struct AsyncSuccess<'r> { pub change_set: ChangeSet, pub events: Vec, pub messages: Vec, - pub gas_used: u64, + pub gas_used: Gas, pub ext: NativeContextExtensions<'r>, } @@ -146,7 +153,7 @@ pub struct AsyncSuccess<'r> { #[derive(Debug, Clone)] pub struct AsyncError { pub error: VMError, - pub gas_used: u64, + pub gas_used: Gas, } /// Result type for operations of an AsyncSession. @@ -183,6 +190,7 @@ impl<'r, 'l, S: MoveResolver> AsyncSession<'r, 'l, S> { .vm_session .get_data_store() .load_resource(actor_addr, &state_type) + .map(|(gv, _)| gv) .map_err(partial_vm_error_to_async)?; if state.exists().map_err(partial_vm_error_to_async)? { return Err(async_extension_error(format!( @@ -193,7 +201,7 @@ impl<'r, 'l, S: MoveResolver> AsyncSession<'r, 'l, S> { } // Execute the initializer. - let gas_before = gas_status.remaining_gas().get(); + let gas_before = gas_status.remaining_gas(); let result = self .vm_session .execute_function_bypass_visibility( @@ -204,7 +212,7 @@ impl<'r, 'l, S: MoveResolver> AsyncSession<'r, 'l, S> { gas_status, ) .and_then(|ret| Ok((ret, self.vm_session.finish_with_extensions()?))); - let gas_used = gas_status.remaining_gas().get() - gas_before; + let gas_used = gas_before.checked_sub(gas_status.remaining_gas()).unwrap(); // Process the result, moving the return value of the initializer function into the // changeset. @@ -227,6 +235,7 @@ impl<'r, 'l, S: MoveResolver> AsyncSession<'r, 'l, S> { actor_addr, actor.state_tag.clone(), return_values.remove(0).0, + false, ) .map_err(partial_vm_error_to_async)?; let async_ext = native_extensions.remove::(); @@ -276,6 +285,7 @@ impl<'r, 'l, S: MoveResolver> AsyncSession<'r, 'l, S> { .vm_session .get_data_store() .load_resource(actor_addr, &state_type) + .map(|(gv, _)| gv) .map_err(partial_vm_error_to_async)?; let actor_state = actor_state_global .borrow_global() @@ -289,13 +299,13 @@ impl<'r, 'l, S: MoveResolver> AsyncSession<'r, 'l, S> { ); // Execute the handler. - let gas_before = gas_status.remaining_gas().get(); + let gas_before = gas_status.remaining_gas(); let result = self .vm_session .execute_function_bypass_visibility(module_id, handler_id, vec![], args, gas_status) .and_then(|ret| Ok((ret, self.vm_session.finish_with_extensions()?))); - let gas_used = gas_status.remaining_gas().get() - gas_before; + let gas_used = gas_before.checked_sub(gas_status.remaining_gas()).unwrap(); // Process the result, moving the mutated value of the handlers first parameter // into the changeset. @@ -319,6 +329,7 @@ impl<'r, 'l, S: MoveResolver> AsyncSession<'r, 'l, S> { actor_addr, actor.state_tag.clone(), mutable_reference_outputs.remove(0).1, + true, ) .map_err(partial_vm_error_to_async)?; } @@ -367,9 +378,18 @@ fn publish_actor_state( actor_addr: AccountAddress, state_tag: StructTag, state: Vec, + is_modify: bool, ) -> PartialVMResult<()> { change_set - .publish_resource(actor_addr, state_tag, state) + .add_resource_op( + actor_addr, + state_tag, + if is_modify { + Op::Modify(state) + } else { + Op::New(state) + }, + ) .map_err(|err| partial_extension_error(format!("cannot publish actor state: {}", err))) } @@ -384,12 +404,15 @@ pub(crate) fn extension_error(msg: impl ToString) -> VMError { fn async_extension_error(msg: impl ToString) -> AsyncError { AsyncError { error: extension_error(msg), - gas_used: 0, + gas_used: 0.into(), } } fn vm_error_to_async(error: VMError) -> AsyncError { - AsyncError { error, gas_used: 0 } + AsyncError { + error, + gas_used: 0.into(), + } } fn partial_vm_error_to_async(error: PartialVMError) -> AsyncError { diff --git a/language/extensions/async/move-async-vm/src/natives.rs b/language/extensions/async/move-async-vm/src/natives.rs index 5b78f372f4..a9a9f4f967 100644 --- a/language/extensions/async/move-async-vm/src/natives.rs +++ b/language/extensions/async/move-async-vm/src/natives.rs @@ -5,26 +5,23 @@ use crate::async_vm::Message; use better_any::{Tid, TidAble}; use move_binary_format::errors::PartialVMResult; -use move_core_types::{account_address::AccountAddress, identifier::Identifier}; +use move_core_types::{ + account_address::AccountAddress, + gas_algebra::{InternalGas, InternalGasPerByte, NumBytes}, + identifier::Identifier, +}; use move_vm_runtime::{ native_functions, native_functions::{NativeContext, NativeFunction}, }; use move_vm_types::{ - gas_schedule::NativeCostIndex, loaded_data::runtime_types::Type, - natives::function::{native_gas, NativeResult}, + natives::function::NativeResult, pop_arg, values::{Value, Vector}, }; use smallvec::smallvec; -use std::collections::VecDeque; - -// TODO: make cost tables extensible; right now we forward to one of the predefined cost indices -// as an approximation. -const SELF_COST_INDEX: NativeCostIndex = NativeCostIndex::LENGTH; -const SEND_COST_INDEX: NativeCostIndex = NativeCostIndex::EMIT_EVENT; -const EPOCH_TIME_INDEX: NativeCostIndex = NativeCostIndex::LENGTH; +use std::{collections::VecDeque, sync::Arc}; /// Environment extension for the Move VM which we pass down to native functions, /// to implement message sending and retrieval of actor address. @@ -36,39 +33,116 @@ pub struct AsyncExtension { pub in_initializer: bool, } +#[derive(Clone, Debug)] +pub struct GasParameters { + pub self_: SelfGasParameters, + pub send: SendGasParameters, + pub virtual_time: VirtualTimeGasParameters, +} + +impl GasParameters { + pub fn zeros() -> Self { + Self { + self_: SelfGasParameters { + base_cost: 0.into(), + }, + send: SendGasParameters { + base_cost: 0.into(), + unit_cost: 0.into(), + }, + virtual_time: VirtualTimeGasParameters { + base_cost: 0.into(), + }, + } + } +} + pub fn actor_natives( async_addr: AccountAddress, + gas_params: GasParameters, ) -> Vec<(AccountAddress, Identifier, Identifier, NativeFunction)> { - const NATIVES: &[(&str, &str, NativeFunction)] = &[ - ("Actor", "self", native_self), - ("Actor", "virtual_time", native_virtual_time), - ("Runtime", "send__0", native_send), - ("Runtime", "send__1", native_send), - ("Runtime", "send__2", native_send), - ("Runtime", "send__3", native_send), - ("Runtime", "send__4", native_send), - ("Runtime", "send__5", native_send), - ("Runtime", "send__6", native_send), - ("Runtime", "send__7", native_send), - ("Runtime", "send__8", native_send), + let natives = [ + ("Actor", "self", make_native_self(gas_params.self_)), + ( + "Actor", + "virtual_time", + make_native_virtual_time(gas_params.virtual_time), + ), + ( + "Runtime", + "send__0", + make_native_send(gas_params.send.clone()), + ), + ( + "Runtime", + "send__1", + make_native_send(gas_params.send.clone()), + ), + ( + "Runtime", + "send__2", + make_native_send(gas_params.send.clone()), + ), + ( + "Runtime", + "send__3", + make_native_send(gas_params.send.clone()), + ), + ( + "Runtime", + "send__4", + make_native_send(gas_params.send.clone()), + ), + ( + "Runtime", + "send__5", + make_native_send(gas_params.send.clone()), + ), + ( + "Runtime", + "send__6", + make_native_send(gas_params.send.clone()), + ), + ( + "Runtime", + "send__7", + make_native_send(gas_params.send.clone()), + ), + ("Runtime", "send__8", make_native_send(gas_params.send)), ]; - native_functions::make_table(async_addr, NATIVES) + native_functions::make_table_from_iter(async_addr, natives) +} + +#[derive(Clone, Debug)] +pub struct SelfGasParameters { + base_cost: InternalGas, } fn native_self( + gas_params: &SelfGasParameters, context: &mut NativeContext, mut _ty_args: Vec, mut _args: VecDeque, ) -> PartialVMResult { - let cost = native_gas(context.cost_table(), SELF_COST_INDEX, 1); let ext = context.extensions().get::(); Ok(NativeResult::ok( - cost, + gas_params.base_cost, smallvec![Value::address(ext.current_actor)], )) } +fn make_native_self(gas_params: SelfGasParameters) -> NativeFunction { + Arc::new(move |context, ty_args, args| native_self(&gas_params, context, ty_args, args)) +} + +#[derive(Clone, Debug)] +pub struct SendGasParameters { + base_cost: InternalGas, + unit_cost: InternalGasPerByte, +} + fn native_send( + gas_params: &SendGasParameters, context: &mut NativeContext, mut _ty_args: Vec, mut args: VecDeque, @@ -82,19 +156,34 @@ fn native_send( let message_hash = pop_arg!(args, u64); let target = pop_arg!(args, AccountAddress); ext.sent.push((target, message_hash, bcs_args)); - let cost = native_gas(context.cost_table(), SEND_COST_INDEX, args.len()); + + let cost = gas_params.base_cost + gas_params.unit_cost * NumBytes::new(args.len() as u64); + Ok(NativeResult::ok(cost, smallvec![])) } +fn make_native_send(gas_params: SendGasParameters) -> NativeFunction { + Arc::new(move |context, ty_args, args| native_send(&gas_params, context, ty_args, args)) +} + +#[derive(Clone, Debug)] +pub struct VirtualTimeGasParameters { + base_cost: InternalGas, +} + fn native_virtual_time( + gas_params: &VirtualTimeGasParameters, context: &mut NativeContext, mut _ty_args: Vec, mut _args: VecDeque, ) -> PartialVMResult { - let cost = native_gas(context.cost_table(), EPOCH_TIME_INDEX, 1); let ext = context.extensions().get::(); Ok(NativeResult::ok( - cost, + gas_params.base_cost, smallvec![Value::u128(ext.virtual_time)], )) } + +fn make_native_virtual_time(gas_params: VirtualTimeGasParameters) -> NativeFunction { + Arc::new(move |context, ty_args, args| native_virtual_time(&gas_params, context, ty_args, args)) +} diff --git a/language/extensions/async/move-async-vm/tests/sources/AccountStateMachine.exp b/language/extensions/async/move-async-vm/tests/sources/AccountStateMachine.exp index da5160c13f..308482370a 100644 --- a/language/extensions/async/move-async-vm/tests/sources/AccountStateMachine.exp +++ b/language/extensions/async/move-async-vm/tests/sources/AccountStateMachine.exp @@ -5,10 +5,10 @@ publishing vector publishing AccountStateMachine actor 0x4 created from 0x3::AccountStateMachine SUCCESS - commit 0x3::AccountStateMachine::AccountStateMachine[0x4] := [00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::AccountStateMachine::AccountStateMachine[0x4] := New("[00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00]") actor 0x5 created from 0x3::AccountStateMachine SUCCESS - commit 0x3::AccountStateMachine::AccountStateMachine[0x5] := [00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::AccountStateMachine::AccountStateMachine[0x5] := New("[00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00]") actor 0x4 handling 0x3::AccountStateMachine::start (hash=0x5438F379BC9E3BCB) SUCCESS sent 0x4 <- 0x4C40DD3C3521A146 argc=1 @@ -20,28 +20,28 @@ actor 0x5 handling 0x3::AccountStateMachine::start (hash=0x5438F379BC9E3BCB) SUCCESS actor 0x4 handling 0x3::AccountStateMachine::deposit (hash=0x4C40DD3C3521A146) SUCCESS - commit 0x3::AccountStateMachine::AccountStateMachine[0x4] := [64, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::AccountStateMachine::AccountStateMachine[0x4] := Modify("[64, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00]") actor 0x5 handling 0x3::AccountStateMachine::deposit (hash=0x4C40DD3C3521A146) SUCCESS - commit 0x3::AccountStateMachine::AccountStateMachine[0x5] := [64, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::AccountStateMachine::AccountStateMachine[0x5] := Modify("[64, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00]") actor 0x4 handling 0x3::AccountStateMachine::xfer (hash=0xF8ECAD16D8E182BB) SUCCESS sent 0x5 <- 0xB32E0FAF108F638 argc=3 - commit 0x3::AccountStateMachine::AccountStateMachine[0x4] := [64, 00, 00, 00, 00, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, 00, 14, 00, 00, 00, 00, 00, 00, 00, A0, 69, 62, 02, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::AccountStateMachine::AccountStateMachine[0x4] := Modify("[64, 00, 00, 00, 00, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, 00, 14, 00, 00, 00, 00, 00, 00, 00, A0, 69, 62, 02, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00]") actor 0x4 handling 0x3::AccountStateMachine::cleanup (hash=0xDD840198DA7DE13E) SUCCESS - commit 0x3::AccountStateMachine::AccountStateMachine[0x4] := [64, 00, 00, 00, 00, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, 00, 14, 00, 00, 00, 00, 00, 00, 00, A0, 69, 62, 02, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::AccountStateMachine::AccountStateMachine[0x4] := Modify("[64, 00, 00, 00, 00, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, 00, 14, 00, 00, 00, 00, 00, 00, 00, A0, 69, 62, 02, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00]") actor 0x5 handling 0x3::AccountStateMachine::cleanup (hash=0xDD840198DA7DE13E) SUCCESS - commit 0x3::AccountStateMachine::AccountStateMachine[0x5] := [64, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::AccountStateMachine::AccountStateMachine[0x5] := Modify("[64, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00]") actor 0x5 handling 0x3::AccountStateMachine::xfer_deposit (hash=0xB32E0FAF108F638) SUCCESS sent 0x4 <- 0xB8229D65C5B58BBA argc=1 - commit 0x3::AccountStateMachine::AccountStateMachine[0x5] := [78, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::AccountStateMachine::AccountStateMachine[0x5] := Modify("[78, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00]") actor 0x4 handling 0x3::AccountStateMachine::xfer_finish (hash=0xB8229D65C5B58BBA) SUCCESS sent 0x4 <- 0x22801C54EE790BE3 argc=0 - commit 0x3::AccountStateMachine::AccountStateMachine[0x4] := [50, 00, 00, 00, 00, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::AccountStateMachine::AccountStateMachine[0x4] := Modify("[50, 00, 00, 00, 00, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, 00]") actor 0x4 handling 0x3::AccountStateMachine::end (hash=0x22801C54EE790BE3) SUCCESS sent 0x4 <- 0x3450A8717C6383FF argc=1 @@ -52,4 +52,4 @@ actor 0x4 handling 0x3::AccountStateMachine::verify (hash=0x3450A8717C6383FF) actor 0x5 handling 0x3::AccountStateMachine::verify (hash=0x3450A8717C6383FF) SUCCESS actor 0x5 handling 0x3::AccountStateMachine::verify (hash=0x3450A8717C6383FF) - FAIL VMError with status ABORTED with sub status 2 at location Module ModuleId { address: 00000000000000000000000000000003, name: Identifier("AccountStateMachine") } and message 0x00000000000000000000000000000003::AccountStateMachine::verify at offset 9 at code offset 9 in function definition 16 + FAIL VMError with status ABORTED with sub status 2 at location Module ModuleId { address: 00000000000000000000000000000003, name: Identifier("AccountStateMachine") } and message 0x00000000000000000000000000000003::AccountStateMachine::verify at offset 7 at code offset 7 in function definition 16 diff --git a/language/extensions/async/move-async-vm/tests/sources/Basic.exp b/language/extensions/async/move-async-vm/tests/sources/Basic.exp index f0b1741af0..fc83136899 100644 --- a/language/extensions/async/move-async-vm/tests/sources/Basic.exp +++ b/language/extensions/async/move-async-vm/tests/sources/Basic.exp @@ -3,30 +3,30 @@ publishing bcs publishing Basic actor 0x4 created from 0x3::Basic SUCCESS - commit 0x3::Basic::Basic[0x4] := [00, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::Basic::Basic[0x4] := New("[00, 00, 00, 00, 00, 00, 00, 00]") actor 0x4 handling 0x3::Basic::start (hash=0xA9C2CD33311F2015) SUCCESS sent 0x4 <- 0x45F84510862E6905 argc=1 actor 0x4 handling 0x3::Basic::count_down (hash=0x45F84510862E6905) SUCCESS sent 0x4 <- 0x45F84510862E6905 argc=1 - commit 0x3::Basic::Basic[0x4] := [01, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::Basic::Basic[0x4] := Modify("[01, 00, 00, 00, 00, 00, 00, 00]") actor 0x4 handling 0x3::Basic::count_down (hash=0x45F84510862E6905) SUCCESS sent 0x4 <- 0x45F84510862E6905 argc=1 - commit 0x3::Basic::Basic[0x4] := [02, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::Basic::Basic[0x4] := Modify("[02, 00, 00, 00, 00, 00, 00, 00]") actor 0x4 handling 0x3::Basic::count_down (hash=0x45F84510862E6905) SUCCESS sent 0x4 <- 0x45F84510862E6905 argc=1 - commit 0x3::Basic::Basic[0x4] := [03, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::Basic::Basic[0x4] := Modify("[03, 00, 00, 00, 00, 00, 00, 00]") actor 0x4 handling 0x3::Basic::count_down (hash=0x45F84510862E6905) SUCCESS sent 0x4 <- 0x45F84510862E6905 argc=1 - commit 0x3::Basic::Basic[0x4] := [04, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::Basic::Basic[0x4] := Modify("[04, 00, 00, 00, 00, 00, 00, 00]") actor 0x4 handling 0x3::Basic::count_down (hash=0x45F84510862E6905) SUCCESS sent 0x4 <- 0x45F84510862E6905 argc=1 - commit 0x3::Basic::Basic[0x4] := [05, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::Basic::Basic[0x4] := Modify("[05, 00, 00, 00, 00, 00, 00, 00]") actor 0x4 handling 0x3::Basic::count_down (hash=0x45F84510862E6905) SUCCESS - commit 0x3::Basic::Basic[0x4] := [05, 00, 00, 00, 00, 00, 00, 00] + commit 0x3::Basic::Basic[0x4] := Modify("[05, 00, 00, 00, 00, 00, 00, 00]") diff --git a/language/extensions/async/move-async-vm/tests/testsuite.rs b/language/extensions/async/move-async-vm/tests/testsuite.rs index f986693e25..a5fbc03a53 100644 --- a/language/extensions/async/move-async-vm/tests/testsuite.rs +++ b/language/extensions/async/move-async-vm/tests/testsuite.rs @@ -8,6 +8,7 @@ use move_async_vm::{ actor_metadata, actor_metadata::ActorMetadata, async_vm::{AsyncResult, AsyncSession, AsyncVM, Message}, + natives::GasParameters as ActorGasParameters, }; use move_binary_format::access::ModuleAccess; use move_command_line_common::testing::EXP_EXT; @@ -17,13 +18,13 @@ use move_compiler::{ }; use move_core_types::{ account_address::AccountAddress, - effects::ChangeSet, + effects::{ChangeSet, Op}, identifier::{IdentStr, Identifier}, language_storage::{ModuleId, StructTag}, resolver::{ModuleResolver, ResourceResolver}, }; use move_prover_test_utils::{baseline_test::verify_or_update_baseline, extract_test_directives}; -use move_vm_types::gas_schedule::GasStatus; +use move_vm_test_utils::gas_schedule::GasStatus; use std::{ cell::RefCell, collections::{BTreeMap, BTreeSet, VecDeque}, @@ -182,25 +183,34 @@ impl Harness { fn commit_changeset(&self, changeset: ChangeSet) { for (addr, change) in changeset.into_inner() { - for (struct_tag, val) in change.into_inner().1 { + for (struct_tag, op) in change.into_inner().1 { self.log(format!( - " commit 0x{}::{}::{}[0x{}] := {}", + " commit 0x{}::{}::{}[0x{}] := {:?}", struct_tag.address.short_str_lossless(), struct_tag.module, struct_tag.module, addr.short_str_lossless(), - val.as_ref() - .map(|b| format!("{:02X?}", b)) - .unwrap_or_else(|| "None".to_string()) + op.as_ref().map(|b| format!("{:02X?}", b)) )); - match val { - Some(v) => { + match op { + Op::New(v) => { + assert!(self + .resource_store + .borrow_mut() + .insert((addr, struct_tag), v) + .is_none()); + } + Op::Modify(v) => { self.resource_store .borrow_mut() - .insert((addr, struct_tag), v); + .insert((addr, struct_tag), v) + .unwrap(); } - None => { - self.resource_store.borrow_mut().remove(&(addr, struct_tag)); + Op::Delete => { + self.resource_store + .borrow_mut() + .remove(&(addr, struct_tag)) + .unwrap(); } } } @@ -237,8 +247,14 @@ impl Harness { resource_store: Default::default(), vm: AsyncVM::new( test_account(), - move_stdlib::natives::all_natives(test_account()), + move_stdlib::natives::all_natives( + test_account(), + // We may want to switch to a different gas schedule in the future, but for now, + // the all-zero one should be enough. + move_stdlib::natives::GasParameters::zeros(), + ), actor_metadata, + ActorGasParameters::zeros(), )?, actor_instances, }; diff --git a/language/extensions/move-table-extension/Cargo.toml b/language/extensions/move-table-extension/Cargo.toml index 36e01fd7ab..1dc6384986 100644 --- a/language/extensions/move-table-extension/Cargo.toml +++ b/language/extensions/move-table-extension/Cargo.toml @@ -5,14 +5,13 @@ authors = ["Diem Association "] description = "Wrapper for the Move VM which coordinates multiple extensions" repository = "https://github.com/diem/move" license = "Apache-2.0" -edition = "2018" +edition = "2021" publish = false [dependencies] anyhow = "1.0.52" better_any = "0.1.1" smallvec = "1.6.1" -bcs = "0.1.2" sha3 = "0.9.1" once_cell = "1.7.2" move-core-types = { path = "../../move-core/types" } @@ -20,6 +19,8 @@ move-vm-types = { path = "../../move-vm/types" } move-vm-runtime = { path = "../../move-vm/runtime", features = ["debugging"] } move-binary-format = { path = "../../move-binary-format" } +bcs.workspace = true + [dev-dependencies] move-stdlib = { path = "../../move-stdlib", features = ["testing"] } move-unit-test = { path = "../../tools/move-unit-test", features = ["table-extension"] } diff --git a/language/extensions/move-table-extension/sources/Table.move b/language/extensions/move-table-extension/sources/Table.move index b29221f65d..8241264370 100644 --- a/language/extensions/move-table-extension/sources/Table.move +++ b/language/extensions/move-table-extension/sources/Table.move @@ -11,7 +11,7 @@ module extensions::table { /// Type of tables struct Table has store { - handle: u128, + handle: address, length: u64, } @@ -98,7 +98,7 @@ module extensions::table { // Primitives which take as an additional type parameter `Box`, so the implementation // can use this to determine serialization layout. - native fun new_table_handle(): u128; + native fun new_table_handle(): address; native fun add_box(table: &mut Table, key: K, val: Box); native fun borrow_box(table: &Table, key: K): &Box; native fun borrow_box_mut(table: &mut Table, key: K): &mut Box; diff --git a/language/extensions/move-table-extension/sources/Table.spec.move b/language/extensions/move-table-extension/sources/Table.spec.move index 1a9698c19b..02470cf1ec 100644 --- a/language/extensions/move-table-extension/sources/Table.spec.move +++ b/language/extensions/move-table-extension/sources/Table.spec.move @@ -4,7 +4,21 @@ spec extensions::table { // Make most of the public API intrinsic. Those functions have custom specifications in the prover. spec Table { - pragma intrinsic; + pragma intrinsic = map, + map_new = new, + map_destroy_empty = destroy_empty, + map_len = length, + map_is_empty = empty, + map_has_key = contains, + map_add_no_override = add, + map_del_must_exist = remove, + map_borrow = borrow, + map_borrow_mut = borrow_mut, + map_spec_get = spec_get, + map_spec_set = spec_add, + map_spec_del = spec_remove, + map_spec_len = spec_len, + map_spec_has_key = spec_contains; } spec new { @@ -45,7 +59,6 @@ spec extensions::table { // Specification functions for tables - spec native fun spec_table(): Table; spec native fun spec_len(t: Table): num; spec native fun spec_contains(t: Table, k: K): bool; spec native fun spec_add(t: Table, k: K, v: V): Table; diff --git a/language/extensions/move-table-extension/src/lib.rs b/language/extensions/move-table-extension/src/lib.rs index df7687eff8..0856913906 100644 --- a/language/extensions/move-table-extension/src/lib.rs +++ b/language/extensions/move-table-extension/src/lib.rs @@ -11,28 +11,29 @@ use better_any::{Tid, TidAble}; use move_binary_format::errors::{PartialVMError, PartialVMResult}; use move_core_types::{ account_address::AccountAddress, - gas_schedule::{GasAlgebra, GasCarrier, InternalGasUnits}, + effects::Op, + gas_algebra::{InternalGas, InternalGasPerByte, NumBytes}, language_storage::TypeTag, value::MoveTypeLayout, vm_status::StatusCode, }; use move_vm_runtime::{ native_functions, - native_functions::{NativeContext, NativeFunctionTable}, + native_functions::{NativeContext, NativeFunction, NativeFunctionTable}, }; use move_vm_types::{ loaded_data::runtime_types::Type, natives::function::NativeResult, pop_arg, - values::{GlobalValue, GlobalValueEffect, Reference, StructRef, Value}, + values::{GlobalValue, Reference, StructRef, Value}, }; use sha3::{Digest, Sha3_256}; use smallvec::smallvec; use std::{ cell::RefCell, collections::{btree_map::Entry, BTreeMap, BTreeSet, VecDeque}, - convert::TryInto, fmt::Display, + sync::Arc, }; // =========================================================================================== @@ -42,7 +43,7 @@ use std::{ /// hash over a transaction hash provided by the environment and a table creation counter /// local to the transaction. #[derive(Copy, Clone, Debug, PartialOrd, Ord, PartialEq, Eq)] -pub struct TableHandle(pub u128); +pub struct TableHandle(pub AccountAddress); impl Display for TableHandle { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -81,7 +82,7 @@ pub struct TableChangeSet { /// A change of a single table. pub struct TableChange { - pub entries: BTreeMap, Option>>, + pub entries: BTreeMap, Op>>, } /// A table resolver which needs to be provided by the environment. This allows to lookup @@ -92,13 +93,6 @@ pub trait TableResolver { handle: &TableHandle, key: &[u8], ) -> Result>, anyhow::Error>; - - fn operation_cost( - &self, - op: TableOperation, - key_size: usize, - val_size: usize, - ) -> InternalGasUnits; } /// A table operation, for supporting cost calculation. @@ -118,7 +112,7 @@ pub enum TableOperation { #[derive(Tid)] pub struct NativeTableContext<'a> { resolver: &'a dyn TableResolver, - txn_hash: u128, + txn_hash: [u8; 32], table_data: RefCell, } @@ -160,7 +154,7 @@ const HANDLE_FIELD_INDEX: usize = 0; impl<'a> NativeTableContext<'a> { /// Create a new instance of a native table context. This must be passed in via an /// extension into VM session functions. - pub fn new(txn_hash: u128, resolver: &'a dyn TableResolver) -> Self { + pub fn new(txn_hash: [u8; 32], resolver: &'a dyn TableResolver) -> Self { Self { resolver, txn_hash, @@ -185,15 +179,23 @@ impl<'a> NativeTableContext<'a> { } = table; let mut entries = BTreeMap::new(); for (key, gv) in content { - match gv.into_effect()? { - GlobalValueEffect::Deleted => { - entries.insert(key, None); + let op = match gv.into_effect() { + Some(op) => op, + None => continue, + }; + + match op { + Op::New(val) => { + let bytes = serialize(&value_layout, &val)?; + entries.insert(key, Op::New(bytes)); } - GlobalValueEffect::Changed(new_val) => { - let new_bytes = serialize(&value_layout, &new_val)?; - entries.insert(key, Some(new_bytes)); + Op::Modify(val) => { + let bytes = serialize(&value_layout, &val)?; + entries.insert(key, Op::Modify(bytes)); + } + Op::Delete => { + entries.insert(key, Op::Delete); } - _ => {} } } if !entries.is_empty() { @@ -218,145 +220,129 @@ impl TableData { key_ty: &Type, value_ty: &Type, ) -> PartialVMResult<&mut Table> { - if let Entry::Vacant(e) = self.tables.entry(handle) { - let key_layout = get_type_layout(context, key_ty)?; - let value_layout = get_type_layout(context, value_ty)?; - let table = Table { - handle, - key_layout, - value_layout, - content: Default::default(), - }; - e.insert(table); - } - Ok(self.tables.get_mut(&handle).unwrap()) + Ok(match self.tables.entry(handle) { + Entry::Vacant(e) => { + let key_layout = get_type_layout(context, key_ty)?; + let value_layout = get_type_layout(context, value_ty)?; + let table = Table { + handle, + key_layout, + value_layout, + content: Default::default(), + }; + e.insert(table) + } + Entry::Occupied(e) => e.into_mut(), + }) } } impl Table { - /// Inserts a value into a table. - fn insert( - &mut self, - context: &NativeTableContext, - key: &Value, - val: Value, - ) -> PartialVMResult<(usize, usize)> { - let (gv_opt, _, _) = self.global_value_if_exists(context, key)?; - if gv_opt.is_some() { - return Err(partial_abort_error( - "table entry already occupied", - ALREADY_EXISTS, - )); - } - let key_bytes = serialize(&self.key_layout, key)?; - let key_size = key_bytes.len(); - // Need to serialize for cost computation - let val_size = serialize(&self.value_layout, &val)?.len(); - self.content - .entry(key_bytes) - .or_insert_with(GlobalValue::none) - .move_to(val)?; - Ok((key_size, val_size)) - } - - /// Borrows a reference to a table (mutable or immutable). - fn borrow_global( - &mut self, - context: &NativeTableContext, - key: &Value, - ) -> PartialVMResult<(Value, usize, usize)> { - let (gv_opt, key_size, val_size) = self.global_value_if_exists(context, key)?; - let gv = gv_opt.ok_or_else(|| partial_abort_error("undefined table entry", NOT_FOUND))?; - let val = gv.borrow_global()?; - Ok((val, key_size, val_size)) - } - - /// Removes an entry from a table. - fn remove( + fn get_or_create_global_value( &mut self, context: &NativeTableContext, - key: &Value, - ) -> PartialVMResult<(Value, usize, usize)> { - let (gv_opt, key_size, val_size) = self.global_value_if_exists(context, key)?; - let gv = gv_opt.ok_or_else(|| partial_abort_error("undefined table entry", NOT_FOUND))?; - let val = gv.move_from()?; - Ok((val, key_size, val_size)) + key: Vec, + ) -> PartialVMResult<(&mut GlobalValue, Option>)> { + Ok(match self.content.entry(key) { + Entry::Vacant(entry) => { + let (gv, loaded) = match context + .resolver + .resolve_table_entry(&self.handle, entry.key()) + .map_err(|err| { + partial_extension_error(format!("remote table resolver failure: {}", err)) + })? { + Some(val_bytes) => { + let val = deserialize(&self.value_layout, &val_bytes)?; + ( + GlobalValue::cached(val)?, + Some(NumBytes::new(val_bytes.len() as u64)), + ) + } + None => (GlobalValue::none(), None), + }; + (entry.insert(gv), Some(loaded)) + } + Entry::Occupied(entry) => (entry.into_mut(), None), + }) } +} - /// Checks whether a key is in the table. - fn contains( - &mut self, - context: &NativeTableContext, - key: &Value, - ) -> PartialVMResult<(Value, usize, usize)> { - let (gv_opt, key_size, val_size) = self.global_value_if_exists(context, key)?; - Ok((Value::bool(gv_opt.is_some()), key_size, val_size)) - } +// ========================================================================================= +// Native Function Implementations - /// Destroys a table. - fn destroy_empty(&mut self, _context: &NativeTableContext) -> PartialVMResult<(usize, usize)> { - Ok((0, 0)) - } +/// Returns all natives for tables. +pub fn table_natives(table_addr: AccountAddress, gas_params: GasParameters) -> NativeFunctionTable { + let natives: [(&str, &str, NativeFunction); 8] = [ + ( + "table", + "new_table_handle", + make_native_new_table_handle(gas_params.new_table_handle), + ), + ( + "table", + "add_box", + make_native_add_box(gas_params.common.clone(), gas_params.add_box), + ), + ( + "table", + "borrow_box", + make_native_borrow_box(gas_params.common.clone(), gas_params.borrow_box.clone()), + ), + ( + "table", + "borrow_box_mut", + make_native_borrow_box(gas_params.common.clone(), gas_params.borrow_box), + ), + ( + "table", + "remove_box", + make_native_remove_box(gas_params.common.clone(), gas_params.remove_box), + ), + ( + "table", + "contains_box", + make_native_contains_box(gas_params.common, gas_params.contains_box), + ), + ( + "table", + "destroy_empty_box", + make_native_destroy_empty_box(gas_params.destroy_empty_box), + ), + ( + "table", + "drop_unchecked_box", + make_native_drop_unchecked_box(gas_params.drop_unchecked_box), + ), + ]; + + native_functions::make_table_from_iter(table_addr, natives) +} - /// Gets the global value of an entry in the table. Attempts to retrieve a value from - /// the resolver if needed. Aborts if the value does not exists. Also returns the size - /// of the key and value (if a value needs to be fetched from remote) for cost computation. - fn global_value_if_exists( - &mut self, - context: &NativeTableContext, - key: &Value, - ) -> PartialVMResult<(Option<&mut GlobalValue>, usize, usize)> { - let key_bytes = serialize(&self.key_layout, key)?; - let key_size = key_bytes.len(); - let mut val_size = 0; - if !self.content.contains_key(&key_bytes) { - // Try to retrieve a value from the remote resolver. - let gv = match context - .resolver - .resolve_table_entry(&self.handle, &key_bytes) - .map_err(|err| { - partial_extension_error(format!("remote table resolver failure: {}", err)) - })? { - Some(val_bytes) => { - val_size = val_bytes.len(); - let val = deserialize(&self.value_layout, &val_bytes)?; - GlobalValue::cached(val)? - } - None => GlobalValue::none(), - }; - self.content.insert(key_bytes.clone(), gv); - } +#[derive(Debug, Clone)] +pub struct CommonGasParameters { + pub load_base: InternalGas, + pub load_per_byte: InternalGasPerByte, + pub load_failure: InternalGas, +} - let gv = self.content.get_mut(&key_bytes).unwrap(); - if gv.exists()? { - Ok((Some(gv), key_size, val_size)) - } else { - Ok((None, key_size, val_size)) - } +impl CommonGasParameters { + fn calculate_load_cost(&self, loaded: Option>) -> InternalGas { + self.load_base + + match loaded { + Some(Some(num_bytes)) => self.load_per_byte * num_bytes, + Some(None) => self.load_failure, + None => 0.into(), + } } } -// ========================================================================================= -// Native Function Implementations - -/// Returns all natives for tables. -pub fn table_natives(table_addr: AccountAddress) -> NativeFunctionTable { - native_functions::make_table( - table_addr, - &[ - ("table", "new_table_handle", native_new_table_handle), - ("table", "add_box", native_add_box), - ("table", "borrow_box", native_borrow_box), - ("table", "borrow_box_mut", native_borrow_box), - ("table", "remove_box", native_remove_box), - ("table", "contains_box", native_contains_box), - ("table", "destroy_empty_box", native_destroy_empty_box), - ("table", "drop_unchecked_box", native_drop_unchecked_box), - ], - ) +#[derive(Debug, Clone)] +pub struct NewTableHandleGasParameters { + pub base: InternalGas, } fn native_new_table_handle( + gas_params: &NewTableHandleGasParameters, context: &mut NativeContext, ty_args: Vec, args: VecDeque, @@ -368,29 +354,45 @@ fn native_new_table_handle( let mut table_data = table_context.table_data.borrow_mut(); // Take the transaction hash provided by the environment, combine it with the # of tables - // produced so far, sha256 this and select 16 bytes from the result. Given the txn hash + // produced so far, sha256 this to produce a unique handle. Given the txn hash // is unique, this should create a unique and deterministic global id. let mut digest = Sha3_256::new(); - Digest::update(&mut digest, table_context.txn_hash.to_be_bytes()); - Digest::update(&mut digest, table_data.new_tables.len().to_be_bytes()); - let bytes: [u8; 16] = digest.finalize()[0..16].try_into().unwrap(); - let id = u128::from_be_bytes(bytes); + let table_len = table_data.new_tables.len() as u32; // cast usize to u32 to ensure same length + Digest::update(&mut digest, table_context.txn_hash); + Digest::update(&mut digest, table_len.to_be_bytes()); + let bytes = digest.finalize().to_vec(); + let handle = AccountAddress::from_bytes(&bytes[0..AccountAddress::LENGTH]) + .map_err(|_| partial_extension_error("Unable to create table handle"))?; let key_type = context.type_to_type_tag(&ty_args[0])?; let value_type = context.type_to_type_tag(&ty_args[1])?; assert!(table_data .new_tables - .insert(TableHandle(id), TableInfo::new(key_type, value_type)) + .insert(TableHandle(handle), TableInfo::new(key_type, value_type)) .is_none()); Ok(NativeResult::ok( - table_context - .resolver - .operation_cost(TableOperation::NewHandle, 0, 0), - smallvec![Value::u128(id)], + gas_params.base, + smallvec![Value::address(handle)], )) } +pub fn make_native_new_table_handle(gas_params: NewTableHandleGasParameters) -> NativeFunction { + Arc::new( + move |context, ty_args, args| -> PartialVMResult { + native_new_table_handle(&gas_params, context, ty_args, args) + }, + ) +} + +#[derive(Debug, Clone)] +pub struct AddBoxGasParameters { + pub base: InternalGas, + pub per_byte_serialized: InternalGasPerByte, +} + fn native_add_box( + common_gas_params: &CommonGasParameters, + gas_params: &AddBoxGasParameters, context: &mut NativeContext, ty_args: Vec, mut args: VecDeque, @@ -405,19 +407,42 @@ fn native_add_box( let key = args.pop_back().unwrap(); let handle = get_table_handle(&pop_arg!(args, StructRef))?; + let mut cost = gas_params.base; + let table = table_data.get_or_create_table(context, handle, &ty_args[0], &ty_args[2])?; - let status = table.insert(table_context, &key, val); - let (key_size, val_size) = status?; - Ok(NativeResult::ok( - table_context - .resolver - .operation_cost(TableOperation::Insert, key_size, val_size), - smallvec![], - )) + let key_bytes = serialize(&table.key_layout, &key)?; + cost += gas_params.per_byte_serialized * NumBytes::new(key_bytes.len() as u64); + + let (gv, loaded) = table.get_or_create_global_value(table_context, key_bytes)?; + cost += common_gas_params.calculate_load_cost(loaded); + + match gv.move_to(val) { + Ok(_) => Ok(NativeResult::ok(cost, smallvec![])), + Err(_) => Ok(NativeResult::err(cost, ALREADY_EXISTS)), + } +} + +pub fn make_native_add_box( + common_gas_params: CommonGasParameters, + gas_params: AddBoxGasParameters, +) -> NativeFunction { + Arc::new( + move |context, ty_args, args| -> PartialVMResult { + native_add_box(&common_gas_params, &gas_params, context, ty_args, args) + }, + ) +} + +#[derive(Debug, Clone)] +pub struct BorrowBoxGasParameters { + pub base: InternalGas, + pub per_byte_serialized: InternalGasPerByte, } fn native_borrow_box( + common_gas_params: &CommonGasParameters, + gas_params: &BorrowBoxGasParameters, context: &mut NativeContext, ty_args: Vec, mut args: VecDeque, @@ -432,17 +457,41 @@ fn native_borrow_box( let handle = get_table_handle(&pop_arg!(args, StructRef))?; let table = table_data.get_or_create_table(context, handle, &ty_args[0], &ty_args[2])?; - let (val, key_size, val_size) = table.borrow_global(table_context, &key)?; - Ok(NativeResult::ok( - table_context - .resolver - .operation_cost(TableOperation::Borrow, key_size, val_size), - smallvec![val], - )) + let mut cost = gas_params.base; + + let key_bytes = serialize(&table.key_layout, &key)?; + cost += gas_params.per_byte_serialized * NumBytes::new(key_bytes.len() as u64); + + let (gv, loaded) = table.get_or_create_global_value(table_context, key_bytes)?; + cost += common_gas_params.calculate_load_cost(loaded); + + match gv.borrow_global() { + Ok(ref_val) => Ok(NativeResult::ok(cost, smallvec![ref_val])), + Err(_) => Ok(NativeResult::err(cost, NOT_FOUND)), + } +} + +pub fn make_native_borrow_box( + common_gas_params: CommonGasParameters, + gas_params: BorrowBoxGasParameters, +) -> NativeFunction { + Arc::new( + move |context, ty_args, args| -> PartialVMResult { + native_borrow_box(&common_gas_params, &gas_params, context, ty_args, args) + }, + ) +} + +#[derive(Debug, Clone)] +pub struct ContainsBoxGasParameters { + pub base: InternalGas, + pub per_byte_serialized: InternalGasPerByte, } fn native_contains_box( + common_gas_params: &CommonGasParameters, + gas_params: &ContainsBoxGasParameters, context: &mut NativeContext, ty_args: Vec, mut args: VecDeque, @@ -457,17 +506,40 @@ fn native_contains_box( let handle = get_table_handle(&pop_arg!(args, StructRef))?; let table = table_data.get_or_create_table(context, handle, &ty_args[0], &ty_args[2])?; - let (val, key_size, val_size) = table.contains(table_context, &key)?; - Ok(NativeResult::ok( - table_context - .resolver - .operation_cost(TableOperation::Contains, key_size, val_size), - smallvec![val], - )) + let mut cost = gas_params.base; + + let key_bytes = serialize(&table.key_layout, &key)?; + cost += gas_params.per_byte_serialized * NumBytes::new(key_bytes.len() as u64); + + let (gv, loaded) = table.get_or_create_global_value(table_context, key_bytes)?; + cost += common_gas_params.calculate_load_cost(loaded); + + let exists = Value::bool(gv.exists()?); + + Ok(NativeResult::ok(cost, smallvec![exists])) +} + +pub fn make_native_contains_box( + common_gas_params: CommonGasParameters, + gas_params: ContainsBoxGasParameters, +) -> NativeFunction { + Arc::new( + move |context, ty_args, args| -> PartialVMResult { + native_contains_box(&common_gas_params, &gas_params, context, ty_args, args) + }, + ) +} + +#[derive(Debug, Clone)] +pub struct RemoveGasParameters { + pub base: InternalGas, + pub per_byte_serialized: InternalGasPerByte, } fn native_remove_box( + common_gas_params: &CommonGasParameters, + gas_params: &RemoveGasParameters, context: &mut NativeContext, ty_args: Vec, mut args: VecDeque, @@ -480,18 +552,41 @@ fn native_remove_box( let key = args.pop_back().unwrap(); let handle = get_table_handle(&pop_arg!(args, StructRef))?; + let table = table_data.get_or_create_table(context, handle, &ty_args[0], &ty_args[2])?; - let (val, key_size, val_size) = table.remove(table_context, &key)?; - Ok(NativeResult::ok( - table_context - .resolver - .operation_cost(TableOperation::Remove, key_size, val_size), - smallvec![val], - )) + let mut cost = gas_params.base; + + let key_bytes = serialize(&table.key_layout, &key)?; + cost += gas_params.per_byte_serialized * NumBytes::new(key_bytes.len() as u64); + + let (gv, loaded) = table.get_or_create_global_value(table_context, key_bytes)?; + cost += common_gas_params.calculate_load_cost(loaded); + + match gv.move_from() { + Ok(val) => Ok(NativeResult::ok(cost, smallvec![val])), + Err(_) => Ok(NativeResult::err(cost, NOT_FOUND)), + } +} + +pub fn make_native_remove_box( + common_gas_params: CommonGasParameters, + gas_params: RemoveGasParameters, +) -> NativeFunction { + Arc::new( + move |context, ty_args, args| -> PartialVMResult { + native_remove_box(&common_gas_params, &gas_params, context, ty_args, args) + }, + ) +} + +#[derive(Debug, Clone)] +pub struct DestroyEmptyBoxGasParameters { + pub base: InternalGas, } fn native_destroy_empty_box( + gas_params: &DestroyEmptyBoxGasParameters, context: &mut NativeContext, ty_args: Vec, mut args: VecDeque, @@ -503,20 +598,29 @@ fn native_destroy_empty_box( let mut table_data = table_context.table_data.borrow_mut(); let handle = get_table_handle(&pop_arg!(args, StructRef))?; - let table = table_data.get_or_create_table(context, handle, &ty_args[0], &ty_args[2])?; - let (key_size, val_size) = table.destroy_empty(table_context)?; + // TODO: Can the following line be removed? + table_data.get_or_create_table(context, handle, &ty_args[0], &ty_args[2])?; assert!(table_data.removed_tables.insert(handle)); - Ok(NativeResult::ok( - table_context - .resolver - .operation_cost(TableOperation::Destroy, key_size, val_size), - smallvec![], - )) + Ok(NativeResult::ok(gas_params.base, smallvec![])) +} + +pub fn make_native_destroy_empty_box(gas_params: DestroyEmptyBoxGasParameters) -> NativeFunction { + Arc::new( + move |context, ty_args, args| -> PartialVMResult { + native_destroy_empty_box(&gas_params, context, ty_args, args) + }, + ) +} + +#[derive(Debug, Clone)] +pub struct DropUncheckedBoxGasParameters { + pub base: InternalGas, } fn native_drop_unchecked_box( + gas_params: &DropUncheckedBoxGasParameters, _context: &mut NativeContext, ty_args: Vec, args: VecDeque, @@ -524,17 +628,70 @@ fn native_drop_unchecked_box( assert_eq!(ty_args.len(), 3); assert_eq!(args.len(), 1); - Ok(NativeResult::ok(InternalGasUnits::new(0_u64), smallvec![])) + Ok(NativeResult::ok(gas_params.base, smallvec![])) +} + +pub fn make_native_drop_unchecked_box(gas_params: DropUncheckedBoxGasParameters) -> NativeFunction { + Arc::new( + move |context, ty_args, args| -> PartialVMResult { + native_drop_unchecked_box(&gas_params, context, ty_args, args) + }, + ) +} + +#[derive(Debug, Clone)] +pub struct GasParameters { + pub common: CommonGasParameters, + pub new_table_handle: NewTableHandleGasParameters, + pub add_box: AddBoxGasParameters, + pub borrow_box: BorrowBoxGasParameters, + pub contains_box: ContainsBoxGasParameters, + pub remove_box: RemoveGasParameters, + pub destroy_empty_box: DestroyEmptyBoxGasParameters, + pub drop_unchecked_box: DropUncheckedBoxGasParameters, +} + +impl GasParameters { + pub fn zeros() -> Self { + Self { + common: CommonGasParameters { + load_base: 0.into(), + load_per_byte: 0.into(), + load_failure: 0.into(), + }, + new_table_handle: NewTableHandleGasParameters { base: 0.into() }, + add_box: AddBoxGasParameters { + base: 0.into(), + per_byte_serialized: 0.into(), + }, + borrow_box: BorrowBoxGasParameters { + base: 0.into(), + per_byte_serialized: 0.into(), + }, + contains_box: ContainsBoxGasParameters { + base: 0.into(), + per_byte_serialized: 0.into(), + }, + remove_box: RemoveGasParameters { + base: 0.into(), + per_byte_serialized: 0.into(), + }, + destroy_empty_box: DestroyEmptyBoxGasParameters { base: 0.into() }, + drop_unchecked_box: DropUncheckedBoxGasParameters { base: 0.into() }, + } + } } // ========================================================================================= // Helpers fn get_table_handle(table: &StructRef) -> PartialVMResult { - let field_ref = table + let handle = table .borrow_field(HANDLE_FIELD_INDEX)? - .value_as::()?; - field_ref.read_ref()?.value_as::().map(TableHandle) + .value_as::()? + .read_ref()? + .value_as::()?; + Ok(TableHandle(handle)) } fn serialize(layout: &MoveTypeLayout, val: &Value) -> PartialVMResult> { @@ -551,12 +708,6 @@ fn partial_extension_error(msg: impl ToString) -> PartialVMError { PartialVMError::new(StatusCode::VM_EXTENSION_ERROR).with_message(msg.to_string()) } -fn partial_abort_error(msg: impl ToString, code: u64) -> PartialVMError { - PartialVMError::new(StatusCode::ABORTED) - .with_message(msg.to_string()) - .with_sub_status(code) -} - fn get_type_layout(context: &NativeContext, ty: &Type) -> PartialVMResult { context .type_to_type_layout(ty)? diff --git a/language/extensions/move-table-extension/tests/move_unit_tests.rs b/language/extensions/move-table-extension/tests/move_unit_tests.rs index d9ed043e8d..5a8773e59c 100644 --- a/language/extensions/move-table-extension/tests/move_unit_tests.rs +++ b/language/extensions/move-table-extension/tests/move_unit_tests.rs @@ -4,17 +4,20 @@ use move_cli::base::test::{run_move_unit_tests, UnitTestResult}; use move_core_types::account_address::AccountAddress; -use move_table_extension::table_natives; +use move_table_extension::{table_natives, GasParameters}; use move_unit_test::UnitTestingConfig; use std::path::PathBuf; use tempfile::tempdir; fn run_tests_for_pkg(path_to_pkg: impl Into) { let pkg_path = path_in_crate(path_to_pkg); - let mut natives = - move_stdlib::natives::all_natives(AccountAddress::from_hex_literal("0x1").unwrap()); + let mut natives = move_stdlib::natives::all_natives( + AccountAddress::from_hex_literal("0x1").unwrap(), + move_stdlib::natives::GasParameters::zeros(), + ); natives.append(&mut table_natives( AccountAddress::from_hex_literal("0x2").unwrap(), + GasParameters::zeros(), )); let res = run_move_unit_tests( &pkg_path, diff --git a/language/move-analyzer/Cargo.toml b/language/move-analyzer/Cargo.toml index 03097e977b..20b6f8ca0c 100644 --- a/language/move-analyzer/Cargo.toml +++ b/language/move-analyzer/Cargo.toml @@ -1,17 +1,17 @@ [package] name = "move-analyzer" -version = "0.0.0" +version = "1.0.0" authors = ["Diem Association "] description = "A language server for Move" -repository = "https://github.com/diem/diem" -homepage = "https://diem.com" +repository = "https://github.com/move-language/move" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] anyhow = "1.0.52" codespan-reporting = "0.11.1" +derivative = "2.2.0" dunce = "1.0.2" im = "15.1.0" lsp-server = "0.5.1" diff --git a/language/move-analyzer/editors/code/.eslintrc.json b/language/move-analyzer/editors/code/.eslintrc.json index 705f4b860a..a6a2d7351c 100644 --- a/language/move-analyzer/editors/code/.eslintrc.json +++ b/language/move-analyzer/editors/code/.eslintrc.json @@ -139,7 +139,6 @@ // * All @typescript-eslint rules: https://github.com/typescript-eslint/typescript-eslint/tree/v4.31.1/packages/eslint-plugin/docs/rules // * The rules included in @typescript-eslint/recommended: https://github.com/typescript-eslint/typescript-eslint/blob/v4.31.1/packages/eslint-plugin/src/configs/recommended.ts // * The rules included in @typescript-eslint/recommended-requiring-type-checking: https://github.com/typescript-eslint/typescript-eslint/blob/v4.31.1/packages/eslint-plugin/src/configs/recommended-requiring-type-checking.ts - "@typescript-eslint/array-type": ["warn", { "default": "generic" }], "@typescript-eslint/ban-tslint-comment": "warn", "@typescript-eslint/brace-style": "warn", "@typescript-eslint/comma-dangle": [ @@ -215,6 +214,7 @@ "@typescript-eslint/type-annotation-spacing": "off", "@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/no-unsafe-assignment": "off", - "@typescript-eslint/restrict-template-expressions": "off" + "@typescript-eslint/restrict-template-expressions": "off", + "@typescript-eslint/array-type": "off" } } diff --git a/language/move-analyzer/editors/code/.vscodeignore b/language/move-analyzer/editors/code/.vscodeignore index c170fc4dee..ddd5d59296 100644 --- a/language/move-analyzer/editors/code/.vscodeignore +++ b/language/move-analyzer/editors/code/.vscodeignore @@ -5,7 +5,7 @@ **/* !images/move.png -!out/src/* +!out/src/**/* !node_modules/**/* !language-configuration.json !move.tmLanguage.json diff --git a/language/move-analyzer/editors/code/README.md b/language/move-analyzer/editors/code/README.md index a43c1c434b..196e36dece 100644 --- a/language/move-analyzer/editors/code/README.md +++ b/language/move-analyzer/editors/code/README.md @@ -86,3 +86,4 @@ Move source file (a file with a `.move` file extension) and: - go to type definition - go to references - type on hover + - outline view showing symbol tree for Move source files diff --git a/language/move-analyzer/editors/code/package-lock.json b/language/move-analyzer/editors/code/package-lock.json index b6db9528ac..11b3500d68 100644 --- a/language/move-analyzer/editors/code/package-lock.json +++ b/language/move-analyzer/editors/code/package-lock.json @@ -1,19 +1,21 @@ { "name": "move-analyzer", - "version": "0.0.9", + "version": "0.0.10", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "move-analyzer", - "version": "0.0.9", + "version": "0.0.10", "license": "Apache-2.0", "dependencies": { "command-exists": "^1.2.9", + "lru-cache": "^4.1.3", "vscode-languageclient": "6.1.4" }, "devDependencies": { "@types/command-exists": "^1.2.0", + "@types/fs-extra": "^9.0.13", "@types/glob": "^7.1.4", "@types/mocha": "^9.0.0", "@types/node": "^14.17.22", @@ -25,6 +27,7 @@ "cross-env": "^7.0.3", "eslint": "^7.32.0", "eslint-plugin-tsdoc": "^0.2.14", + "fs-extra": "10.0.1", "glob": "^7.1.7", "mocha": "^9.1.1", "typescript": "^4.4.4", @@ -208,6 +211,15 @@ "integrity": "sha512-ugsxEJfsCuqMLSuCD4PIJkp5Uk2z6TCMRCgYVuhRo5cYQY3+1xXTQkSlPtkpGHuvWMjS2KTeVQXxkXRACMbM6A==", "dev": true }, + "node_modules/@types/fs-extra": { + "version": "9.0.13", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.13.tgz", + "integrity": "sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/glob": { "version": "7.1.4", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.4.tgz", @@ -280,6 +292,18 @@ } } }, + "node_modules/@typescript-eslint/eslint-plugin/node_modules/lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "dev": true, + "dependencies": { + "yallist": "^4.0.0" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/@typescript-eslint/eslint-plugin/node_modules/semver": { "version": "7.3.5", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.5.tgz", @@ -295,6 +319,12 @@ "node": ">=10" } }, + "node_modules/@typescript-eslint/eslint-plugin/node_modules/yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "dev": true + }, "node_modules/@typescript-eslint/experimental-utils": { "version": "4.33.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/experimental-utils/-/experimental-utils-4.33.0.tgz", @@ -403,6 +433,18 @@ } } }, + "node_modules/@typescript-eslint/typescript-estree/node_modules/lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "dev": true, + "dependencies": { + "yallist": "^4.0.0" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/@typescript-eslint/typescript-estree/node_modules/semver": { "version": "7.3.5", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.5.tgz", @@ -418,6 +460,12 @@ "node": ">=10" } }, + "node_modules/@typescript-eslint/typescript-estree/node_modules/yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "dev": true + }, "node_modules/@typescript-eslint/visitor-keys": { "version": "4.33.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-4.33.0.tgz", @@ -1307,16 +1355,6 @@ "editorconfig": "bin/editorconfig" } }, - "node_modules/editorconfig/node_modules/lru-cache": { - "version": "4.1.5", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz", - "integrity": "sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==", - "dev": true, - "dependencies": { - "pseudomap": "^1.0.2", - "yallist": "^2.1.2" - } - }, "node_modules/editorconfig/node_modules/semver": { "version": "5.7.1", "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", @@ -1326,12 +1364,6 @@ "semver": "bin/semver" } }, - "node_modules/editorconfig/node_modules/yallist": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", - "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=", - "dev": true - }, "node_modules/emoji-regex": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", @@ -1529,6 +1561,18 @@ "node": ">= 4" } }, + "node_modules/eslint/node_modules/lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "dev": true, + "dependencies": { + "yallist": "^4.0.0" + }, + "engines": { + "node": ">=10" + } + }, "node_modules/eslint/node_modules/semver": { "version": "7.3.5", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.5.tgz", @@ -1544,6 +1588,12 @@ "node": ">=10" } }, + "node_modules/eslint/node_modules/yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "dev": true + }, "node_modules/espree": { "version": "7.3.1", "resolved": "https://registry.npmjs.org/espree/-/espree-7.3.1.tgz", @@ -1775,6 +1825,20 @@ "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==", "dev": true }, + "node_modules/fs-extra": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.0.1.tgz", + "integrity": "sha512-NbdoVMZso2Lsrn/QwLXOy6rm0ufY2zEOKCDzJR/0kBsb0E6qed0P3iYK+Ath3BfvXEeu4JhEtXLgILx5psUfag==", + "dev": true, + "dependencies": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "engines": { + "node": ">=12" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -2068,6 +2132,24 @@ "node": ">=10" } }, + "node_modules/hosted-git-info/node_modules/lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "dev": true, + "dependencies": { + "yallist": "^4.0.0" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/hosted-git-info/node_modules/yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "dev": true + }, "node_modules/htmlparser2": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-6.1.0.tgz", @@ -2323,6 +2405,18 @@ "integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=", "dev": true }, + "node_modules/jsonfile": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "dev": true, + "dependencies": { + "universalify": "^2.0.0" + }, + "optionalDependencies": { + "graceful-fs": "^4.1.6" + } + }, "node_modules/keytar": { "version": "7.7.0", "resolved": "https://registry.npmjs.org/keytar/-/keytar-7.7.0.tgz", @@ -2421,6 +2515,7 @@ } }, "node_modules/lru-cache": { +<<<<<<< HEAD "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", @@ -2430,6 +2525,14 @@ }, "engines": { "node": ">=10" +======= + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz", + "integrity": "sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==", + "dependencies": { + "pseudomap": "^1.0.2", + "yallist": "^2.1.2" +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d } }, "node_modules/markdown-it": { @@ -3022,8 +3125,12 @@ "node_modules/pseudomap": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", +<<<<<<< HEAD "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=", "dev": true +======= + "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=" +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d }, "node_modules/pump": { "version": "3.0.0", @@ -3745,6 +3852,18 @@ "integrity": "sha512-hzSoAVtJF+3ZtiFX0VgfFPHEDRm7Y/QPjGyNo4TVdnDTdft3tr8hEkD25a1jC+TjTuE7tkHGKkhwCgs9dgBB2g==", "dev": true }, +<<<<<<< HEAD +======= + "node_modules/universalify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", + "dev": true, + "engines": { + "node": ">= 10.0.0" + } + }, +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d "node_modules/untildify": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/untildify/-/untildify-4.0.0.tgz", @@ -4060,10 +4179,16 @@ } }, "node_modules/yallist": { +<<<<<<< HEAD "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", "dev": true +======= + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", + "integrity": "sha512-ncTzHV7NvsQZkYe1DW7cbDLm0YpzHmZF5r/iyP3ZnQtMiJ+pjzisCiMNI+Sj+xQF5pXhSHxSB3uDbsBTzY/c2A==" +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d }, "node_modules/yargs": { "version": "16.2.0", @@ -4283,6 +4408,18 @@ "integrity": "sha512-ugsxEJfsCuqMLSuCD4PIJkp5Uk2z6TCMRCgYVuhRo5cYQY3+1xXTQkSlPtkpGHuvWMjS2KTeVQXxkXRACMbM6A==", "dev": true }, +<<<<<<< HEAD +======= + "@types/fs-extra": { + "version": "9.0.13", + "resolved": "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.13.tgz", + "integrity": "sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d "@types/glob": { "version": "7.1.4", "resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.4.tgz", @@ -4339,6 +4476,18 @@ "tsutils": "^3.21.0" }, "dependencies": { +<<<<<<< HEAD +======= + "lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "dev": true, + "requires": { + "yallist": "^4.0.0" + } + }, +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d "semver": { "version": "7.3.5", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.5.tgz", @@ -4347,6 +4496,15 @@ "requires": { "lru-cache": "^6.0.0" } +<<<<<<< HEAD +======= + }, + "yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "dev": true +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d } } }, @@ -4407,6 +4565,18 @@ "tsutils": "^3.21.0" }, "dependencies": { +<<<<<<< HEAD +======= + "lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "dev": true, + "requires": { + "yallist": "^4.0.0" + } + }, +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d "semver": { "version": "7.3.5", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.5.tgz", @@ -4415,6 +4585,15 @@ "requires": { "lru-cache": "^6.0.0" } +<<<<<<< HEAD +======= + }, + "yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "dev": true +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d } } }, @@ -5080,6 +5259,7 @@ "sigmund": "^1.0.1" }, "dependencies": { +<<<<<<< HEAD "lru-cache": { "version": "4.1.5", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz", @@ -5090,17 +5270,22 @@ "yallist": "^2.1.2" } }, +======= +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d "semver": { "version": "5.7.1", "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", "dev": true +<<<<<<< HEAD }, "yallist": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", "integrity": "sha1-HBH5IY8HYImkfdUS+TxmmaaoHVI=", "dev": true +======= +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d } } }, @@ -5217,6 +5402,18 @@ "integrity": "sha512-cyFDKrqc/YdcWFniJhzI42+AzS+gNwmUzOSFcRCQYwySuBBBy/KjuxWLZ/FHEH6Moq1NizMOBWyTcv8O4OZIMg==", "dev": true }, +<<<<<<< HEAD +======= + "lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "dev": true, + "requires": { + "yallist": "^4.0.0" + } + }, +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d "semver": { "version": "7.3.5", "resolved": "https://registry.npmjs.org/semver/-/semver-7.3.5.tgz", @@ -5225,6 +5422,15 @@ "requires": { "lru-cache": "^6.0.0" } +<<<<<<< HEAD +======= + }, + "yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "dev": true +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d } } }, @@ -5445,6 +5651,20 @@ "integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==", "dev": true }, +<<<<<<< HEAD +======= + "fs-extra": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.0.1.tgz", + "integrity": "sha512-NbdoVMZso2Lsrn/QwLXOy6rm0ufY2zEOKCDzJR/0kBsb0E6qed0P3iYK+Ath3BfvXEeu4JhEtXLgILx5psUfag==", + "dev": true, + "requires": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + } + }, +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -5667,6 +5887,26 @@ "dev": true, "requires": { "lru-cache": "^6.0.0" +<<<<<<< HEAD +======= + }, + "dependencies": { + "lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "dev": true, + "requires": { + "yallist": "^4.0.0" + } + }, + "yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "dev": true + } +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d } }, "htmlparser2": { @@ -5855,6 +6095,19 @@ "integrity": "sha1-nbe1lJatPzz+8wp1FC0tkwrXJlE=", "dev": true }, +<<<<<<< HEAD +======= + "jsonfile": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "dev": true, + "requires": { + "graceful-fs": "^4.1.6", + "universalify": "^2.0.0" + } + }, +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d "keytar": { "version": "7.7.0", "resolved": "https://registry.npmjs.org/keytar/-/keytar-7.7.0.tgz", @@ -5934,12 +6187,21 @@ } }, "lru-cache": { +<<<<<<< HEAD "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", "dev": true, "requires": { "yallist": "^4.0.0" +======= + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-4.1.5.tgz", + "integrity": "sha512-sWZlbEP2OsHNkXrMl5GYk/jKk70MBng6UU4YI/qGDYbgf6YbP4EvmqISbXCoJiRKs+1bSpFHVgQxvJ17F2li5g==", + "requires": { + "pseudomap": "^1.0.2", + "yallist": "^2.1.2" +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d } }, "markdown-it": { @@ -6411,8 +6673,12 @@ "pseudomap": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/pseudomap/-/pseudomap-1.0.2.tgz", +<<<<<<< HEAD "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=", "dev": true +======= + "integrity": "sha1-8FKijacOYYkX7wqKw0wa5aaChrM=" +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d }, "pump": { "version": "3.0.0", @@ -6947,6 +7213,15 @@ "integrity": "sha512-hzSoAVtJF+3ZtiFX0VgfFPHEDRm7Y/QPjGyNo4TVdnDTdft3tr8hEkD25a1jC+TjTuE7tkHGKkhwCgs9dgBB2g==", "dev": true }, +<<<<<<< HEAD +======= + "universalify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", + "dev": true + }, +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d "untildify": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/untildify/-/untildify-4.0.0.tgz", @@ -7199,10 +7474,16 @@ "dev": true }, "yallist": { +<<<<<<< HEAD "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", "dev": true +======= + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", + "integrity": "sha512-ncTzHV7NvsQZkYe1DW7cbDLm0YpzHmZF5r/iyP3ZnQtMiJ+pjzisCiMNI+Sj+xQF5pXhSHxSB3uDbsBTzY/c2A==" +>>>>>>> c9b5765f816d773618cc12a70a0095f644bbc68d }, "yargs": { "version": "16.2.0", diff --git a/language/move-analyzer/editors/code/package.json b/language/move-analyzer/editors/code/package.json index e2fe995e52..9e54615e57 100644 --- a/language/move-analyzer/editors/code/package.json +++ b/language/move-analyzer/editors/code/package.json @@ -5,7 +5,7 @@ "publisher": "move", "icon": "images/move.png", "license": "Apache-2.0", - "version": "0.0.9", + "version": "0.0.10", "preview": true, "homepage": "https://github.com/move-language/move", "repository": { @@ -26,7 +26,8 @@ ], "main": "./out/src/main.js", "activationEvents": [ - "onLanguage:move" + "onLanguage:move", + "workspaceContains:Move.toml" ], "contributes": { "commands": [ @@ -94,7 +95,7 @@ "test": "node ./out/tests/runTests.js", "dev": "npm run pretest && cross-env mode=dev node ./out/tests/runTests.js", "vscode:prepublish": "npm run pretest", - "package": "vsce package -o move-analyzer.vsix", + "package": "npm run pretest && vsce package -o move-analyzer.vsix", "publish": "npm run pretest && npm run test && vsce publish" }, "extensionDependencies": [ @@ -102,10 +103,12 @@ ], "dependencies": { "command-exists": "^1.2.9", - "vscode-languageclient": "6.1.4" + "vscode-languageclient": "6.1.4", + "lru-cache": "^4.1.3" }, "devDependencies": { "@types/command-exists": "^1.2.0", + "@types/fs-extra": "^9.0.13", "@types/glob": "^7.1.4", "@types/mocha": "^9.0.0", "@types/node": "^14.17.22", @@ -117,6 +120,7 @@ "cross-env": "^7.0.3", "eslint": "^7.32.0", "eslint-plugin-tsdoc": "^0.2.14", + "fs-extra": "10.0.1", "glob": "^7.1.7", "mocha": "^9.1.1", "typescript": "^4.4.4", diff --git a/language/move-analyzer/editors/code/src/commands/lsp_command.ts b/language/move-analyzer/editors/code/src/commands/lsp_command.ts index abc0e7a03d..157551759a 100644 --- a/language/move-analyzer/editors/code/src/commands/lsp_command.ts +++ b/language/move-analyzer/editors/code/src/commands/lsp_command.ts @@ -1,17 +1,60 @@ - -import * as lc from 'vscode-languageclient'; +import type { + DocumentSymbolParams, + SymbolInformation, + DocumentSymbol, + CompletionParams, + CompletionList, + CompletionItem, +} from 'vscode-languageclient'; +import { DocumentSymbolRequest, HoverRequest, CompletionRequest } from 'vscode-languageclient'; import type { Context } from '../context'; /** * An LSP command textDocument/documentSymbol */ -export async function textDocumentDocumentSymbol(context: Readonly, params: lc.DocumentSymbolParams) - : Promise | Array | null> { +export async function textDocumentDocumentSymbol( + context: Readonly, + params: DocumentSymbolParams, +): Promise { + const client = context.getClient(); + if (client === undefined) { + return Promise.reject(new Error('No language client connected.')); + } + + // Send the request to the language client. + return client.sendRequest(DocumentSymbolRequest.type, params); +} + +/** + * An LSP command textDocument/completion + */ +export async function textDocumentCompletion( + context: Readonly, + params: CompletionParams, +): Promise { + const client = context.getClient(); + if (client === undefined) { + return Promise.reject(new Error('No language client connected.')); + } + + // Send the request to the language client. + return client.sendRequest(CompletionRequest.type, params); +} + + +/** + * An LSP command textDocument/hover + */ +export async function textDocumentHover( + context: Readonly, + params: DocumentSymbolParams, +) + : Promise { const client = context.getClient(); if (client === undefined) { return Promise.reject(new Error('No language client connected.')); } // Send the request to the language client. - return client.sendRequest(lc.DocumentSymbolRequest.type, params); + return client.sendRequest(HoverRequest.method, params); } diff --git a/language/move-analyzer/editors/code/src/configuration.ts b/language/move-analyzer/editors/code/src/configuration.ts index cd15550b8d..595a791cf5 100644 --- a/language/move-analyzer/editors/code/src/configuration.ts +++ b/language/move-analyzer/editors/code/src/configuration.ts @@ -35,6 +35,12 @@ export class Configuration { return defaultName; } + if (serverPath === defaultName) { + // If the program set by the user is through PATH, + // it will return directly if specified + return defaultName; + } + if (serverPath.startsWith('~/')) { serverPath = os.homedir() + serverPath.slice('~'.length); } diff --git a/language/move-analyzer/editors/code/src/context.ts b/language/move-analyzer/editors/code/src/context.ts index d9e047792e..335ac76229 100644 --- a/language/move-analyzer/editors/code/src/context.ts +++ b/language/move-analyzer/editors/code/src/context.ts @@ -7,17 +7,18 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; import { log } from './log'; import { sync as commandExistsSync } from 'command-exists'; +import { IndentAction } from 'vscode'; /** Information passed along to each VS Code command defined by this extension. */ export class Context { - private _client: lc.LanguageClient | undefined; + private client: lc.LanguageClient | undefined; private constructor( private readonly extensionContext: Readonly, readonly configuration: Readonly, client: lc.LanguageClient | undefined = undefined, ) { - this._client = client; + this.client = client; } static create( @@ -46,10 +47,42 @@ export class Context { name: Readonly, command: (context: Readonly, ...args: Array) => any, ): void { - const disposable = vscode.commands.registerCommand(`move-analyzer.${name}`, async (...args: Array) - : Promise => { - const ret = await command(this, ...args); - return ret; + const disposable = vscode.commands.registerCommand( + `move-analyzer.${name}`, + async (...args: Array) : Promise => { + const ret = await command(this, ...args); + return ret; + }, + ); + + this.extensionContext.subscriptions.push(disposable); + } + + /** + * Sets up additional language configuration that's impossible to do via a + * separate language-configuration.json file. See [1] for more information. + * + * This code originates from [2](vscode-rust). + * + * [1]: https://github.com/Microsoft/vscode/issues/11514#issuecomment-244707076 + * [2]: https://github.com/rust-lang/vscode-rust/blob/660b412701fe2ea62fad180c40ee4f8a60571c61/src/extension.ts#L287:L287 + */ + configureLanguage(): void { + const disposable = vscode.languages.setLanguageConfiguration('move', { + onEnterRules: [ + { + // Doc single-line comment + // e.g. ///| + beforeText: /^\s*\/{3}.*$/, + action: { indentAction: IndentAction.None, appendText: '/// ' }, + }, + { + // Parent doc single-line comment + // e.g. //!| + beforeText: /^\s*\/{2}!.*$/, + action: { indentAction: IndentAction.None, appendText: '//! ' }, + }, + ], }); this.extensionContext.subscriptions.push(disposable); @@ -66,6 +99,9 @@ export class Context { * * To read more about the messages sent and responses received by this client, such as * "initialize," read [the Language Server Protocol specification](https://microsoft.github.io/language-server-protocol/specifications/specification-current/#initialize). + * + * In order to synchronously wait for the client to be completely ready, + * we need to mark the function as asynchronous **/ async startClient(): Promise { const executable: lc.Executable = { @@ -104,8 +140,11 @@ export class Context { log.info('Starting client...'); const disposable = client.start(); this.extensionContext.subscriptions.push(disposable); - this._client = client; - await this._client.onReady(); + this.client = client; + + // Wait for the Move Language Server initialization to complete, + // especially the first symbol table parsing is completed + await this.client.onReady(); } /** @@ -114,6 +153,6 @@ export class Context { * @returns lc.LanguageClient */ getClient(): lc.LanguageClient | undefined { - return this._client; + return this.client; } } // Context diff --git a/language/move-analyzer/editors/code/src/main.ts b/language/move-analyzer/editors/code/src/main.ts index ad0644f456..64981a2263 100644 --- a/language/move-analyzer/editors/code/src/main.ts +++ b/language/move-analyzer/editors/code/src/main.ts @@ -43,6 +43,9 @@ async function serverVersion(context: Readonly): Promise { * * Activation events for this extension are listed in its `package.json` file, under the key * `"activationEvents"`. + * + * In order to achieve synchronous activation, mark the function as an asynchronous function, + * so that you can wait for the activation to complete by await */ export async function activate(extensionContext: Readonly): Promise { const extension = new Extension(); @@ -65,7 +68,12 @@ export async function activate(extensionContext: Readonly { const mode = process.env['mode'] || 'test'; if (mode === 'dev') { return new Promise((resolve) => { - setTimeout(resolve, 1000 * 60 * 15); // 15分钟休息一下 + setTimeout(resolve, 1000 * 60 * 15); // Development mode, set a timeout of 15 minutes }); } @@ -28,7 +28,7 @@ export async function run(): Promise { color: true, // The default timeout of 2000 miliseconds can sometimes be too quick, since the extension // tests need to launch VS Code first. - timeout: 5000, + timeout: 10000, }); const testsRoot = path.resolve(__dirname, '..'); diff --git a/language/move-analyzer/editors/code/tests/lsp-demo/sources/Completions.move b/language/move-analyzer/editors/code/tests/lsp-demo/sources/Completions.move new file mode 100644 index 0000000000..b0ff2df28f --- /dev/null +++ b/language/move-analyzer/editors/code/tests/lsp-demo/sources/Completions.move @@ -0,0 +1,13 @@ +module Symbols::Completions { + fun add(a: u64, b: u64): u64 { + a + b + } + + fun subtract(a: u64, b: u64): u64 { + a - b + } + + fun divide(a: u64, b: u64): u64 { + a / b + } +} diff --git a/language/move-analyzer/editors/code/tests/lsp-demo/sources/M2.move b/language/move-analyzer/editors/code/tests/lsp-demo/sources/M2.move new file mode 100644 index 0000000000..4d00f275e7 --- /dev/null +++ b/language/move-analyzer/editors/code/tests/lsp-demo/sources/M2.move @@ -0,0 +1,22 @@ +module Symbols::M2 { + + /// Constant containing the answer to the universe + const DOCUMENTED_CONSTANT: u64 = 42; + + /** + This is a multiline docstring + + This docstring has empty lines. + + It uses the ** format instead of /// + */ + fun other_doc_struct(): Symbols::M3::OtherDocStruct { + Symbols::M3::create_other_struct(DOCUMENTED_CONSTANT) + } + + use Symbols::M3::{Self, OtherDocStruct}; + + fun other_doc_struct_import(): OtherDocStruct { + M3::create_other_struct(7) + } +} diff --git a/language/move-analyzer/editors/code/tests/lsp-demo/sources/M3.move b/language/move-analyzer/editors/code/tests/lsp-demo/sources/M3.move new file mode 100644 index 0000000000..1fd363e82e --- /dev/null +++ b/language/move-analyzer/editors/code/tests/lsp-demo/sources/M3.move @@ -0,0 +1,12 @@ +module Symbols::M3 { + + /// Documented struct in another module + struct OtherDocStruct has drop { + some_field: u64, + } + + /// Documented initializer in another module + public fun create_other_struct(v: u64): OtherDocStruct { + OtherDocStruct { some_field: v } + } +} diff --git a/language/move-analyzer/editors/code/tests/lsp.test.ts b/language/move-analyzer/editors/code/tests/lsp.test.ts index fbf0d68827..419ecaee4e 100644 --- a/language/move-analyzer/editors/code/tests/lsp.test.ts +++ b/language/move-analyzer/editors/code/tests/lsp.test.ts @@ -3,13 +3,31 @@ import * as Mocha from 'mocha'; import * as path from 'path'; import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; +import type { MarkupContent } from 'vscode-languageclient'; +import { CompletionItemKind } from 'vscode-languageclient'; + +const isFunctionInCompletionItems = (fnName: string, items: vscode.CompletionItem[]): boolean => { + return ( + items.find((item) => item.label === fnName && item.kind === CompletionItemKind.Function) !== + undefined + ); +}; + +const isKeywordInCompletionItems = (label: string, items: vscode.CompletionItem[]): boolean => { + return ( + items.find((item) => item.label === label && item.kind === CompletionItemKind.Keyword) !== + undefined + ); +}; + +const PRIMITIVE_TYPES = ['u8', 'u64', 'u128', 'bool', 'vector']; Mocha.suite('LSP', () => { Mocha.test('textDocument/documentSymbol', async () => { const ext = vscode.extensions.getExtension('move.move-analyzer'); assert.ok(ext); - await ext.activate(); + await ext.activate(); // Synchronous waiting for activation to complete // 1. get workdir const workDir = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? ''; @@ -49,4 +67,152 @@ Mocha.suite('LSP', () => { assert.deepStrictEqual(syms[0]?.children[3].name, 'this_is_a_test'); assert.deepStrictEqual(syms[0]?.children[3]?.detail, '["test", "expected_failure"]'); }); + + Mocha.test('textDocument/hover for definition in the same module', async () => { + const ext = vscode.extensions.getExtension('move.move-analyzer'); + assert.ok(ext); + + await ext.activate(); // Synchronous waiting for activation to complete + + // 1. get workdir + const workDir = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? ''; + + // 2. open doc + const docs = await vscode.workspace.openTextDocument( + path.join(workDir, 'sources/M2.move'), + ); + await vscode.window.showTextDocument(docs); + + // 3. execute command + const params: lc.HoverParams = { + textDocument: { + uri: docs.uri.toString(), + }, + position: { + line: 12, + character: 8, + }, + }; + + const hoverResult: lc.Hover | undefined = + await vscode.commands.executeCommand( + 'move-analyzer.textDocumentHover', + params, + ); + + assert.ok(hoverResult); + assert.deepStrictEqual((hoverResult.contents as MarkupContent).value, + // eslint-disable-next-line max-len + 'fun Symbols::M2::other_doc_struct(): Symbols::M3::OtherDocStruct\n\n\nThis is a multiline docstring\n\nThis docstring has empty lines.\n\nIt uses the ** format instead of ///\n\n'); + + }); + + Mocha.test('textDocument/hover for definition in an external module', async () => { + const ext = vscode.extensions.getExtension('move.move-analyzer'); + assert.ok(ext); + + await ext.activate(); // Synchronous waiting for activation to complete + + // 1. get workdir + const workDir = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? ''; + + // 2. open doc + const docs = await vscode.workspace.openTextDocument( + path.join(workDir, 'sources/M2.move'), + ); + await vscode.window.showTextDocument(docs); + + // 3. execute command + const params: lc.HoverParams = { + textDocument: { + uri: docs.uri.toString(), + }, + position: { + line: 18, + character: 35, + }, + }; + + const hoverResult: lc.Hover | undefined = + await vscode.commands.executeCommand( + 'move-analyzer.textDocumentHover', + params, + ); + + + assert.ok(hoverResult); + assert.deepStrictEqual((hoverResult.contents as MarkupContent).value, + 'Symbols::M3::OtherDocStruct\n\nDocumented struct in another module\n'); + }); + + Mocha.test('textDocument/completion', async () => { + const ext = vscode.extensions.getExtension('move.move-analyzer'); + assert.ok(ext); + + await ext.activate(); // Synchronous waiting for activation to complete + + // 1. get workdir + const workDir = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? ''; + + // 2. open doc + const docs = await vscode.workspace.openTextDocument( + path.join(workDir, 'sources/Completions.move'), + ); + await vscode.window.showTextDocument(docs); + + // 3. execute command + const params: lc.CompletionParams = { + textDocument: { + uri: docs.uri.toString(), + }, + position: { + line: 12, + character: 1, + }, + }; + + const items = await vscode.commands.executeCommand>( + 'move-analyzer.textDocumentCompletion', + params, + ); + + assert.ok(items); + + // Items should return all functions defined in the file + assert.strictEqual(isFunctionInCompletionItems('add', items), true); + assert.strictEqual(isFunctionInCompletionItems('subtract', items), true); + assert.strictEqual(isFunctionInCompletionItems('divide', items), true); + + // Items also include all primitive types because they are keywords + PRIMITIVE_TYPES.forEach((primitive) => { + assert.strictEqual(isKeywordInCompletionItems(primitive, items), true); + }); + + const colonParams: lc.CompletionParams = { + textDocument: { + uri: docs.uri.toString(), + }, + // The position of the character ":" + position: { + line: 9, + character: 15, + }, + }; + + const itemsOnColon = await vscode.commands.executeCommand>( + 'move-analyzer.textDocumentCompletion', + colonParams, + ); + + assert.ok(itemsOnColon); + + const keywordsOnColon = itemsOnColon.filter(i => i.kind === CompletionItemKind.Keyword); + // Primitive types are the only keywords returned after inserting the colon + assert.strictEqual(keywordsOnColon.length, PRIMITIVE_TYPES.length); + + // Final safety check + PRIMITIVE_TYPES.forEach((primitive) => { + assert.strictEqual(isKeywordInCompletionItems(primitive, keywordsOnColon), true); + }); + }); }); diff --git a/language/move-analyzer/editors/code/tests/runTests.ts b/language/move-analyzer/editors/code/tests/runTests.ts index 597a1812f6..a8cd46f285 100644 --- a/language/move-analyzer/editors/code/tests/runTests.ts +++ b/language/move-analyzer/editors/code/tests/runTests.ts @@ -9,9 +9,16 @@ * https://code.visualstudio.com/api/working-with-extensions/testing-extension#the-test-script */ +import * as os from 'os'; import * as path from 'path'; import * as cp from 'child_process'; -import { runTests, downloadAndUnzipVSCode, resolveCliArgsFromVSCodeExecutablePath } from '@vscode/test-electron'; +import * as fs from 'fs'; +import * as fse from 'fs-extra'; +import { + runTests, + downloadAndUnzipVSCode, + resolveCliArgsFromVSCodeExecutablePath, +} from '@vscode/test-electron'; /** * Launches a VS Code instance to run tests. @@ -19,7 +26,7 @@ import { runTests, downloadAndUnzipVSCode, resolveCliArgsFromVSCodeExecutablePat * This is essentially a TypeScript program that executes the "VS Code Tokenizer Tests" launch * target defined in this repository's `.vscode/launch.json`. */ -async function main(): Promise { +async function runVSCodeTest(vscodeVersion: string): Promise { try { // The `--extensionDevelopmentPath` argument passed to VS Code. This should point to the // directory that contains the extension manifest file, `package.json`. @@ -36,7 +43,6 @@ async function main(): Promise { } // Install vscode and depends extension - const vscodeVersion = '1.64.0'; const vscodeExecutablePath = await downloadAndUnzipVSCode(vscodeVersion); const [cli, ...args] = resolveCliArgsFromVSCodeExecutablePath(vscodeExecutablePath); const newCli = cli ?? 'code'; @@ -45,12 +51,20 @@ async function main(): Promise { stdio: 'inherit', }); + // Because the default vscode userDataDir is too long, + // v1.69.2 will report an error when running test. + // So generate a short + const userDataDir = path.join(os.tmpdir(), 'vscode-test', vscodeVersion); + if (!fs.existsSync(userDataDir)) { + fse.mkdirsSync(userDataDir); + } + // Download VS Code, unzip it, and run the "test suite" program. await runTests({ vscodeExecutablePath: vscodeExecutablePath, extensionDevelopmentPath, extensionTestsPath, - launchArgs: [testWorkspacePath], + launchArgs: [testWorkspacePath, '--user-data-dir', userDataDir], }); } catch (_err: unknown) { console.error('Failed to run tests'); @@ -58,4 +72,9 @@ async function main(): Promise { } } +async function main(): Promise { + await runVSCodeTest('1.64.0'); // Test with vscode v1.64.0 + await runVSCodeTest('1.69.2'); // Test with vscode v1.69.2 +} + void main(); diff --git a/language/move-analyzer/src/bin/move-analyzer.rs b/language/move-analyzer/src/bin/move-analyzer.rs index 8676f96aa3..d99f6032f0 100644 --- a/language/move-analyzer/src/bin/move-analyzer.rs +++ b/language/move-analyzer/src/bin/move-analyzer.rs @@ -15,6 +15,7 @@ use std::{ collections::BTreeMap, path::Path, sync::{Arc, Mutex}, + thread, }; use move_analyzer::{ @@ -27,7 +28,7 @@ use move_symbol_pool::Symbol; use url::Url; #[derive(Parser)] -#[clap(author, version = "1.0.0", about)] +#[clap(author, version, about)] struct Options {} fn main() { @@ -48,10 +49,11 @@ fn main() { ); let (connection, io_threads) = Connection::stdio(); + let symbols = Arc::new(Mutex::new(symbols::Symbolicator::empty_symbols())); let mut context = Context { connection, files: VirtualFileSystem::default(), - symbols: Arc::new(Mutex::new(symbols::Symbolicator::empty_symbols())), + symbols: symbols.clone(), }; let (id, client_response) = context @@ -116,8 +118,7 @@ fn main() { serde_json::from_value(client_response) .expect("could not deserialize client capabilities"); - symbolicator_runner = - symbols::SymbolicatorRunner::new(context.symbols.clone(), diag_sender); + symbolicator_runner = symbols::SymbolicatorRunner::new(symbols.clone(), diag_sender); // If initialization information from the client contains a path to the directory being // opened, try to initialize symbols before sending response to the client. Do not bother @@ -126,11 +127,21 @@ fn main() { // to be available right after the client is initialized. if let Some(uri) = initialize_params.root_uri { if let Some(p) = symbols::SymbolicatorRunner::root_dir(&uri.to_file_path().unwrap()) { - if let Ok((Some(new_symbols), _)) = symbols::Symbolicator::get_symbols(p.as_path()) - { - let mut old_symbols = context.symbols.lock().unwrap(); - (*old_symbols).merge(new_symbols); - } + // need to evaluate in a separate thread to allow for a larger stack size (needed on + // Windows) + thread::Builder::new() + .stack_size(symbols::STACK_SIZE_BYTES) + .spawn(move || { + if let Ok((Some(new_symbols), _)) = + symbols::Symbolicator::get_symbols(p.as_path()) + { + let mut old_symbols = symbols.lock().unwrap(); + (*old_symbols).merge(new_symbols); + } + }) + .unwrap() + .join() + .unwrap(); } } }; @@ -212,7 +223,9 @@ fn main() { fn on_request(context: &Context, request: &Request) { match request.method.as_str() { - lsp_types::request::Completion::METHOD => on_completion_request(context, request), + lsp_types::request::Completion::METHOD => { + on_completion_request(context, request, &context.symbols.lock().unwrap()) + } lsp_types::request::GotoDefinition::METHOD => { symbols::on_go_to_def_request(context, request, &context.symbols.lock().unwrap()); } diff --git a/language/move-analyzer/src/completion.rs b/language/move-analyzer/src/completion.rs index 5c0b97c89b..787ee43f4a 100644 --- a/language/move-analyzer/src/completion.rs +++ b/language/move-analyzer/src/completion.rs @@ -2,15 +2,16 @@ // Copyright (c) The Move Contributors // SPDX-License-Identifier: Apache-2.0 -use crate::context::Context; +use crate::{context::Context, symbols::Symbols}; use lsp_server::Request; use lsp_types::{CompletionItem, CompletionItemKind, CompletionParams, Position}; use move_command_line_common::files::FileHash; use move_compiler::parser::{ - keywords::{BUILTINS, CONTEXTUAL_KEYWORDS, KEYWORDS}, + keywords::{BUILTINS, CONTEXTUAL_KEYWORDS, KEYWORDS, PRIMITIVE_TYPES}, lexer::{Lexer, Tok}, }; -use std::collections::HashSet; +use move_symbol_pool::Symbol; +use std::{collections::HashSet, path::PathBuf}; /// Constructs an `lsp_types::CompletionItem` with the given `label` and `kind`. fn completion_item(label: &str, kind: CompletionItemKind) -> CompletionItem { @@ -31,6 +32,7 @@ fn keywords() -> Vec { KEYWORDS .iter() .chain(CONTEXTUAL_KEYWORDS.iter()) + .chain(PRIMITIVE_TYPES.iter()) .map(|label| { let kind = if label == &"copy" || label == &"move" { CompletionItemKind::Operator @@ -42,6 +44,14 @@ fn keywords() -> Vec { .collect() } +/// Return a list of completion items of Move's primitive types +fn primitive_types() -> Vec { + PRIMITIVE_TYPES + .iter() + .map(|label| completion_item(label, CompletionItemKind::Keyword)) + .collect() +} + /// Return a list of completion items corresponding to each one of Move's builtin functions. fn builtins() -> Vec { BUILTINS @@ -59,7 +69,7 @@ fn builtins() -> Vec { /// server did not initialize with a response indicating it's capable of providing completions. In /// the future, the server should be modified to return semantically valid completion items, not /// simple textual suggestions. -fn identifiers(buffer: &str) -> Vec { +fn identifiers(buffer: &str, symbols: &Symbols, path: &PathBuf) -> Vec { let mut lexer = Lexer::new(buffer, FileHash::new(buffer)); if lexer.advance().is_err() { return vec![]; @@ -83,13 +93,26 @@ fn identifiers(buffer: &str) -> Vec { } } + let mods_opt = symbols.file_mods().get(path); + // The completion item kind "text" indicates that the item is based on simple textual matching, // not any deeper semantic analysis. - let items = ids - .iter() - .map(|label| completion_item(label, CompletionItemKind::Text)) - .collect(); - items + ids.iter() + .map(|label| { + if let Some(mods) = mods_opt { + if mods + .iter() + .any(|m| m.functions().contains_key(&Symbol::from(*label))) + { + completion_item(label, CompletionItemKind::Function) + } else { + completion_item(label, CompletionItemKind::Text) + } + } else { + completion_item(label, CompletionItemKind::Text) + } + }) + .collect() } /// Returns the token corresponding to the "trigger character" that precedes the user's cursor, @@ -122,7 +145,7 @@ fn get_cursor_token(buffer: &str, position: &Position) -> Option { /// Sends the given connection a response to a completion request. /// /// The completions returned depend upon where the user's cursor is positioned. -pub fn on_completion_request(context: &Context, request: &Request) { +pub fn on_completion_request(context: &Context, request: &Request, symbols: &Symbols) { eprintln!("handling completion request"); let parameters = serde_json::from_value::(request.params.clone()) .expect("could not deserialize completion request"); @@ -148,9 +171,7 @@ pub fn on_completion_request(context: &Context, request: &Request) { let mut items = vec![]; match cursor { Some(Tok::Colon) => { - // If the user's cursor is positioned after a single `:`, do not provide any completion - // items at all -- this is a "mis-fire" of the "trigger character" `:`. - return; + items.extend_from_slice(&primitive_types()); } Some(Tok::Period) | Some(Tok::ColonColon) => { // `.` or `::` must be followed by identifiers, which are added to the completion items @@ -165,7 +186,7 @@ pub fn on_completion_request(context: &Context, request: &Request) { } if let Some(buffer) = &buffer { - let identifiers = identifiers(buffer); + let identifiers = identifiers(buffer, symbols, &path); items.extend_from_slice(&identifiers); } diff --git a/language/move-analyzer/src/symbols.rs b/language/move-analyzer/src/symbols.rs index b05e01d7cb..fe1a60a790 100644 --- a/language/move-analyzer/src/symbols.rs +++ b/language/move-analyzer/src/symbols.rs @@ -54,6 +54,7 @@ use crate::{ use anyhow::{anyhow, Result}; use codespan_reporting::files::SimpleFiles; use crossbeam::channel::Sender; +use derivative::*; use im::ordmap::OrdMap; use lsp_server::{Request, RequestId}; use lsp_types::{ @@ -92,6 +93,9 @@ use move_symbol_pool::Symbol; /// Enabling/disabling the language server reporting readiness to support go-to-def and /// go-to-references to the IDE. pub const DEFS_AND_REFS_SUPPORT: bool = true; +// Building Move code requires a larger stack size on Windows (16M has been chosen somewhat +// arbitrarily) +pub const STACK_SIZE_BYTES: usize = 16 * 1024 * 1024; #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Copy)] /// Location of a definition's identifier @@ -123,7 +127,8 @@ pub enum IdentType { ModuleIdent_, /* defining module */ Symbol, /* name */ Vec, /* type args */ - Vec, /* args */ + Vec, /* arg names */ + Vec, /* arg types */ Type, /* ret */ Vec, /* acquires */ ), @@ -144,6 +149,8 @@ pub struct UseDef { def_loc: DefLoc, /// Location of the type definition type_def_loc: Option, + /// Doc string for the relevant identifier/function + doc_string: String, } /// Definition of a struct field @@ -160,16 +167,20 @@ struct StructDef { field_defs: Vec, } -#[derive(Debug, Clone, Ord, PartialOrd, PartialEq, Eq)] -struct FunctionDef { +#[derive(Derivative, Debug, Clone, PartialEq, Eq)] +#[derivative(PartialOrd, Ord)] +pub struct FunctionDef { name: Symbol, start: Position, attrs: Vec, + #[derivative(PartialOrd = "ignore")] + #[derivative(Ord = "ignore")] + ident_type: IdentType, } /// Module-level definitions #[derive(Debug, Clone, Ord, PartialOrd, PartialEq, Eq)] -struct ModuleDefs { +pub struct ModuleDefs { /// File where this module is located fhash: FileHash, /// Location where this module is located @@ -192,6 +203,8 @@ pub struct Symbolicator { files: SimpleFiles, /// A mapping from file hashes to file IDs (used to obtain source file locations) file_id_mapping: HashMap, + // A mapping from file IDs to a split vector of the lines in each file (used to build docstrings) + file_id_to_lines: HashMap>, /// Contains type params where relevant (e.g. when processing function definition) type_params: BTreeMap, /// Current processed module (always set before module processing starts) @@ -203,6 +216,10 @@ pub struct Symbolicator { #[derive(Debug, Clone, Eq, PartialEq)] struct UseDefMap(BTreeMap>); +/// Maps a function name to its usage definition +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct FunctionIdentTypeMap(BTreeMap); + /// Result of the symbolication process pub struct Symbols { /// A map from def locations to all the references (uses) @@ -227,6 +244,12 @@ pub struct SymbolicatorRunner { mtx_cvar: Arc<(Mutex, Condvar)>, } +impl ModuleDefs { + pub fn functions(&self) -> &BTreeMap { + &self.functions + } +} + impl fmt::Display for IdentType { fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result { match self { @@ -240,9 +263,9 @@ impl fmt::Display for IdentType { // IDE independently on how compiler error messages are generated. write!(f, "{}", type_to_ide_string(t)) } - Self::FunctionType(mod_ident, name, type_args, args, ret, acquires) => { + Self::FunctionType(mod_ident, name, type_args, arg_names, arg_types, ret, acquires) => { let type_args_str = if !type_args.is_empty() { - let mut s = "<".to_string(); + let mut s = '<'.to_string(); s.push_str(&type_list_to_ide_string(type_args)); s.push('>'); s @@ -268,7 +291,7 @@ impl fmt::Display for IdentType { mod_ident.module.value(), name, type_args_str, - type_list_to_ide_string(args), + arg_list_to_ide_string(arg_names, arg_types), ret_str, acquires_str ) @@ -277,6 +300,15 @@ impl fmt::Display for IdentType { } } +fn arg_list_to_ide_string(names: &[Symbol], types: &[Type]) -> String { + names + .iter() + .zip(types.iter()) + .map(|(n, t)| format!("{}: {}", n, type_to_ide_string(t))) + .collect::>() + .join(", ") +} + fn type_to_ide_string(sp!(_, t): &Type) -> String { match t { Type_::Unit => "()".to_string(), @@ -324,8 +356,8 @@ fn addr_to_ide_string(addr: &Address) -> String { } } -fn type_list_to_ide_string(items: &[Type]) -> String { - items +fn type_list_to_ide_string(types: &[Type]) -> String { + types .iter() .map(type_to_ide_string) .collect::>() @@ -349,7 +381,7 @@ impl SymbolicatorRunner { let runner = SymbolicatorRunner { mtx_cvar }; thread::Builder::new() - .stack_size(16 * 1024 * 1024) // building Move code requires a larger stack size on Windows + .stack_size(STACK_SIZE_BYTES) .spawn(move || { let (mtx, cvar) = &*thread_mtx_cvar; // Locations opened in the IDE (files or directories) for which manifest file is missing @@ -476,6 +508,7 @@ impl UseDef { use_name: &Symbol, use_type: IdentType, type_def_loc: Option, + doc_string: String, ) -> Self { let def_loc = DefLoc { fhash: def_fhash, @@ -498,6 +531,7 @@ impl UseDef { use_type, def_loc, type_def_loc, + doc_string, } } } @@ -542,6 +576,20 @@ impl UseDefMap { } } +impl FunctionIdentTypeMap { + fn new() -> Self { + Self(BTreeMap::new()) + } + + fn insert(&mut self, key: String, val: IdentType) { + self.0.entry(key).or_insert_with(|| val); + } + + pub fn contains_key(self, key: &String) -> bool { + self.0.contains_key(key) + } +} + impl Symbols { pub fn merge(&mut self, other: Self) { for (k, v) in other.references { @@ -554,6 +602,10 @@ impl Symbols { self.file_name_mapping.extend(other.file_name_mapping); self.file_mods.extend(other.file_mods); } + + pub fn file_mods(&self) -> &BTreeMap> { + &self.file_mods + } } impl Symbolicator { @@ -579,11 +631,14 @@ impl Symbolicator { let source_files = &resolution_graph.file_sources(); let mut files = SimpleFiles::new(); let mut file_id_mapping = HashMap::new(); + let mut file_id_to_lines = HashMap::new(); let mut file_name_mapping = BTreeMap::new(); for (fhash, (fname, source)) in source_files { let id = files.add(*fname, source.clone()); file_id_mapping.insert(*fhash, id); file_name_mapping.insert(*fhash, *fname); + let lines: Vec = source.lines().map(String::from).collect(); + file_id_to_lines.insert(id, lines); } let build_plan = BuildPlan::create(resolution_graph)?; @@ -673,33 +728,41 @@ impl Symbolicator { mod_use_defs.insert(*module_ident, symbols); } - eprintln!("get_symbols loaded file_mods length: {:?}", file_mods.len()); + eprintln!("get_symbols loaded file_mods length: {}", file_mods.len()); let mut symbolicator = Symbolicator { mod_outer_defs, files, file_id_mapping, + file_id_to_lines, type_params: BTreeMap::new(), current_mod: None, }; let mut references = BTreeMap::new(); let mut file_use_defs = BTreeMap::new(); + let mut function_ident_type = FunctionIdentTypeMap::new(); + for (pos, module_ident, module_def) in modules { let mut use_defs = mod_use_defs.remove(module_ident).unwrap(); symbolicator.current_mod = Some(sp(pos, *module_ident)); - symbolicator.mod_symbols(module_def, &mut references, &mut use_defs); + symbolicator.mod_symbols( + module_def, + &mut references, + &mut use_defs, + &mut function_ident_type, + ); let fpath = match source_files.get(&pos.file_hash()) { Some((p, _)) => p, None => continue, }; + let fpath_buffer = dunce::canonicalize(fpath.as_str()) + .unwrap_or_else(|_| PathBuf::from(fpath.as_str())); + file_use_defs - .entry( - dunce::canonicalize(fpath.as_str()) - .unwrap_or_else(|_| PathBuf::from(fpath.as_str())), - ) + .entry(fpath_buffer) .or_insert_with(UseDefMap::new) .extend(use_defs.elements()); } @@ -767,6 +830,7 @@ impl Symbolicator { continue; } }; + structs.insert( *name, StructDef { @@ -787,7 +851,7 @@ impl Symbolicator { constants.insert(*name, name_start); } - for (pos, name, func) in &mod_def.functions { + for (pos, name, fun) in &mod_def.functions { let name_start = match Self::get_start_loc(&pos, files, file_id_mapping) { Some(s) => s, None => { @@ -795,17 +859,44 @@ impl Symbolicator { continue; } }; + + let ident_type = IdentType::FunctionType( + mod_ident.value, + *name, + fun.signature + .type_parameters + .iter() + .map(|t| sp(t.user_specified_name.loc, Type_::Param(t.clone()))) + .collect(), + fun.signature + .parameters + .iter() + .map(|(n, _)| n.value()) + .collect(), + fun.signature + .parameters + .iter() + .map(|(_, t)| t.clone()) + .collect(), + fun.signature.return_type.clone(), + fun.acquires + .iter() + .map(|(k, v)| Self::create_struct_type(*mod_ident, *k, *v, vec![])) + .collect(), + ); + functions.insert( *name, FunctionDef { name: *name, start: name_start, - attrs: func + attrs: fun .attributes .clone() .iter() .map(|(_loc, name, _attr)| name.to_string()) .collect(), + ident_type, }, ); } @@ -814,7 +905,7 @@ impl Symbolicator { let name = mod_ident.value; let fhash = loc.file_hash(); - let start = match Self::get_start_loc(&loc, files, file_id_mapping) { + let start = match Self::get_start_loc(loc, files, file_id_mapping) { Some(s) => s, None => { debug_assert!(false); @@ -853,51 +944,41 @@ impl Symbolicator { mod_def: &ModuleDefinition, references: &mut BTreeMap>, use_defs: &mut UseDefMap, + function_ident_type: &mut FunctionIdentTypeMap, ) { for (pos, name, fun) in &mod_def.functions { // enter self-definition for function name (unwrap safe - done when inserting def) let name_start = Self::get_start_loc(&pos, &self.files, &self.file_id_mapping).unwrap(); - let use_type = IdentType::FunctionType( - self.current_mod.unwrap().value, - *name, - fun.signature - .type_parameters - .iter() - .map(|t| sp(t.user_specified_name.loc, Type_::Param(t.clone()))) - .collect(), - fun.signature - .parameters - .iter() - .map(|(_, t)| t.clone()) - .collect(), - fun.signature.return_type.clone(), - fun.acquires - .iter() - .map(|(k, v)| { - Self::create_struct_type(self.current_mod.unwrap(), *k, *v, vec![]) - }) - .collect(), - ); - let ident_type_def = self.ident_type_def_loc(&use_type); - use_defs.insert( - name_start.line, - UseDef::new( - references, - pos.file_hash(), - name_start, - pos.file_hash(), - name_start, - name, - use_type, - ident_type_def, - ), + let doc_string = self.extract_doc_string(&name_start, &pos.file_hash()); + + let mod_ident = self.current_mod.unwrap(); + + let mod_def = self.mod_outer_defs.get(&mod_ident.value).unwrap(); + let fun_def = mod_def.functions.get(name).unwrap(); + let use_type = fun_def.ident_type.clone(); + + let fun_type_def = self.ident_type_def_loc(&use_type); + let use_def = UseDef::new( + references, + pos.file_hash(), + name_start, + pos.file_hash(), + name_start, + name, + use_type.clone(), + fun_type_def, + doc_string, ); + + use_defs.insert(name_start.line, use_def); self.fun_symbols(fun, references, use_defs); + function_ident_type.insert(name.to_string(), use_type); } for (pos, name, c) in &mod_def.constants { // enter self-definition for const name (unwrap safe - done when inserting def) let name_start = Self::get_start_loc(&pos, &self.files, &self.file_id_mapping).unwrap(); + let doc_string = self.extract_doc_string(&name_start, &pos.file_hash()); let ident_type = IdentType::RegularType(c.signature.clone()); let ident_type_def = self.ident_type_def_loc(&ident_type); use_defs.insert( @@ -911,6 +992,7 @@ impl Symbolicator { name, ident_type, ident_type_def, + doc_string, ), ); } @@ -918,6 +1000,7 @@ impl Symbolicator { for (pos, name, struct_def) in &mod_def.structs { // enter self-definition for struct name (unwrap safe - done when inserting def) let name_start = Self::get_start_loc(&pos, &self.files, &self.file_id_mapping).unwrap(); + let doc_string = self.extract_doc_string(&name_start, &pos.file_hash()); let ident_type = IdentType::RegularType(Self::create_struct_type( self.current_mod.unwrap(), StructName(sp(pos, *name)), @@ -936,6 +1019,7 @@ impl Symbolicator { name, ident_type, ident_type_def, + doc_string, ), ); @@ -963,6 +1047,7 @@ impl Symbolicator { let start = Self::get_start_loc(&fpos, &self.files, &self.file_id_mapping).unwrap(); let ident_type = IdentType::RegularType(t.clone()); let ident_type_def = self.ident_type_def_loc(&ident_type); + let doc_string = self.extract_doc_string(&start, &fpos.file_hash()); use_defs.insert( start.line, UseDef::new( @@ -974,6 +1059,7 @@ impl Symbolicator { fname, ident_type, ident_type_def, + doc_string, ), ); } @@ -1048,6 +1134,74 @@ impl Symbolicator { get_loc(&pos.file_hash(), pos.start(), files, file_id_mapping) } + /// Extracts the docstring (/// or /** ... */) for a given definition by traversing up from the line definition + fn extract_doc_string(&self, name_start: &Position, file_hash: &FileHash) -> String { + let mut doc_string = String::new(); + let file_id = match self.file_id_mapping.get(file_hash) { + None => return doc_string, + Some(v) => v, + }; + + let file_lines = match self.file_id_to_lines.get(file_id) { + None => return doc_string, + Some(v) => v, + }; + + if name_start.line == 0 { + return doc_string; + } + + let mut iter = (name_start.line - 1) as usize; + let mut line_before = file_lines[iter].trim(); + + // Detect the two different types of docstrings + if line_before.starts_with("///") { + while let Some(stripped_line) = line_before.strip_prefix("///") { + doc_string = format!("{}\n{}", stripped_line.trim(), doc_string); + if iter == 0 { + break; + } + iter -= 1; + line_before = file_lines[iter].trim(); + } + } else if line_before.ends_with("*/") { + let mut doc_string_found = false; + line_before = file_lines[iter].strip_suffix("*/").unwrap_or("").trim(); + + // Loop condition is a safe guard. + while !doc_string_found { + // We found the start of the multi-line comment/docstring + if line_before.starts_with("/*") { + let is_doc = line_before.starts_with("/**") && !line_before.starts_with("/***"); + + // Invalid doc_string start prefix so return empty doc string. + if !is_doc { + return String::new(); + } + + line_before = line_before.strip_prefix("/**").unwrap_or("").trim(); + doc_string_found = true; + } + + doc_string = format!("{}\n{}", line_before, doc_string); + + if iter == 0 { + break; + } + + iter -= 1; + line_before = file_lines[iter].trim(); + } + + // No doc_string found - return String::new(); + if !doc_string_found { + return String::new(); + } + } + + doc_string + } + /// Get symbols for a sequence representing function body fn seq_item_symbols( &self, @@ -1226,9 +1380,7 @@ impl Symbolicator { use_defs, exp.ty.clone(), ), - E::ModuleCall(mod_call) => { - self.mod_call_symbols(mod_call, scope, references, use_defs, exp.ty.clone()) - } + E::ModuleCall(mod_call) => self.mod_call_symbols(mod_call, scope, references, use_defs), E::Builtin(builtin_fun, exp) => { use BuiltinFunction_ as BF; match &builtin_fun.value { @@ -1378,21 +1530,16 @@ impl Symbolicator { scope: &mut OrdMap, references: &mut BTreeMap>, use_defs: &mut UseDefMap, - ret_type: Type, ) { - // handle function name - let use_type = IdentType::FunctionType( - mod_call.module.value, - mod_call.name.value(), - mod_call.type_arguments.clone(), - mod_call.parameter_types.clone(), - ret_type, - mod_call - .acquires - .iter() - .map(|(k, v)| Self::create_struct_type(mod_call.module, *k, *v, vec![])) - .collect(), - ); + let mod_ident = mod_call.module; + let mod_def = self.mod_outer_defs.get(&mod_ident.value).unwrap(); + + let fun_def = match mod_def.functions.get(&mod_call.name.value()) { + Some(v) => v, + None => return, + }; + let use_type = fun_def.ident_type.clone(); + self.add_fun_use_def( &mod_call.module, &mod_call.name.value(), @@ -1476,6 +1623,8 @@ impl Symbolicator { Type_::Param(tp.clone()), )); let ident_type_def = self.ident_type_def_loc(&ident_type); + + let doc_string = self.extract_doc_string(&start, &fhash); use_defs.insert( start.line, UseDef::new( @@ -1487,6 +1636,7 @@ impl Symbolicator { &tname, ident_type, ident_type_def, + doc_string, ), ); let exists = tp_scope.insert(tname, DefLoc { fhash, start }); @@ -1546,18 +1696,22 @@ impl Symbolicator { |use_name, name_start, mod_defs| match mod_defs.constants.get(use_name) { Some(def_start) => { let ident_type = IdentType::RegularType(use_type.clone()); + let def_fhash = self.mod_outer_defs.get(&module_ident).unwrap().fhash; + let doc_string = self.extract_doc_string(def_start, &def_fhash); let ident_type_def = self.ident_type_def_loc(&ident_type); + use_defs.insert( name_start.line, UseDef::new( references, use_pos.file_hash(), name_start, - self.mod_outer_defs.get(&module_ident).unwrap().fhash, + def_fhash, *def_start, use_name, ident_type, ident_type_def, + doc_string, ), ); } @@ -1582,17 +1736,20 @@ impl Symbolicator { use_pos, |use_name, name_start, mod_defs| match mod_defs.functions.get(use_name) { Some(func_def) => { + let def_fhash = self.mod_outer_defs.get(&module_ident.value).unwrap().fhash; + let doc_string = self.extract_doc_string(&func_def.start, &def_fhash); use_defs.insert( name_start.line, UseDef::new( references, use_pos.file_hash(), name_start, - self.mod_outer_defs.get(&module_ident.value).unwrap().fhash, + def_fhash, func_def.start, use_name, use_type.clone(), self.ident_type_def_loc(&use_type), + doc_string, ), ); } @@ -1618,18 +1775,22 @@ impl Symbolicator { |use_name, name_start, mod_defs| match mod_defs.structs.get(use_name) { Some(def) => { let ident_type = IdentType::RegularType(use_type.clone()); + let ident_type_def = self.ident_type_def_loc(&ident_type); + let def_fhash = self.mod_outer_defs.get(module_ident).unwrap().fhash; + let doc_string = self.extract_doc_string(&def.name_start, &def_fhash); use_defs.insert( name_start.line, UseDef::new( references, use_pos.file_hash(), name_start, - self.mod_outer_defs.get(module_ident).unwrap().fhash, + def_fhash, def.name_start, use_name, ident_type, ident_type_def, + doc_string, ), ); } @@ -1659,17 +1820,21 @@ impl Symbolicator { if fdef.name == *use_name { let ident_type = IdentType::RegularType(use_type.clone()); let ident_type_def = self.ident_type_def_loc(&ident_type); + + let def_fhash = self.mod_outer_defs.get(module_ident).unwrap().fhash; + let doc_string = self.extract_doc_string(&fdef.start, &def_fhash); use_defs.insert( name_start.line, UseDef::new( references, use_pos.file_hash(), name_start, - self.mod_outer_defs.get(module_ident).unwrap().fhash, + def_fhash, fdef.start, use_name, ident_type, ident_type_def, + doc_string, ), ); } @@ -1697,6 +1862,8 @@ impl Symbolicator { Some(def_loc) => { let ident_type = IdentType::RegularType(id_type.clone()); let ident_type_def = self.ident_type_def_loc(&ident_type); + let doc_string = + self.extract_doc_string(&def_loc.start, &def_loc.fhash); use_defs.insert( name_start.line, UseDef::new( @@ -1708,6 +1875,7 @@ impl Symbolicator { &use_name, ident_type, ident_type_def, + doc_string, ), ); } @@ -1756,6 +1924,8 @@ impl Symbolicator { // in rust) a variable can be re-defined in the same scope replacing the previous // definition + let doc_string = self.extract_doc_string(&name_start, &pos.file_hash()); + // enter self-definition for def name let ident_type = IdentType::RegularType(use_type); let ident_type_def = self.ident_type_def_loc(&ident_type); @@ -1770,6 +1940,7 @@ impl Symbolicator { name, ident_type, ident_type_def, + doc_string, ), ); } @@ -1799,6 +1970,7 @@ impl Symbolicator { }; if let Some(def_loc) = scope.get(use_name) { + let doc_string = self.extract_doc_string(&def_loc.start, &def_loc.fhash); let ident_type = IdentType::RegularType(use_type); let ident_type_def = self.ident_type_def_loc(&ident_type); use_defs.insert( @@ -1812,6 +1984,7 @@ impl Symbolicator { use_name, ident_type, ident_type_def, + doc_string, ), ); } else { @@ -1835,7 +2008,7 @@ impl Symbolicator { fn ident_type_def_loc(&self, ident_type: &IdentType) -> Option { match ident_type { IdentType::RegularType(t) => self.type_def_loc(t), - IdentType::FunctionType(_, _, _, _, ret, _) => self.type_def_loc(ret), + IdentType::FunctionType(_, _, _, _, _, ret, _) => self.type_def_loc(ret), } } @@ -2022,7 +2195,11 @@ pub fn on_hover_request(context: &Context, request: &Request, symbols: &Symbols) |u| { let lang_string = LanguageString { language: "".to_string(), - value: format!("{}", u.use_type), + value: if !u.doc_string.is_empty() { + format!("{}\n\n{}", u.use_type, u.doc_string) + } else { + format!("{}", u.use_type) + }, }; let contents = HoverContents::Scalar(MarkedString::LanguageString(lang_string)); let range = None; @@ -2211,7 +2388,7 @@ fn handle_struct_fields(struct_def: StructDef, fields: &mut Vec) } #[cfg(test)] -fn assert_use_def( +fn assert_use_def_with_doc_string( mod_symbols: &UseDefMap, file_name_mapping: &BTreeMap, use_idx: usize, @@ -2222,6 +2399,7 @@ fn assert_use_def( def_file: &str, type_str: &str, type_def: Option<(u32, u32, &str)>, + doc_string: &str, ) { let uses = mod_symbols.get(use_line).unwrap(); let use_def = uses.iter().nth(use_idx).unwrap(); @@ -2234,6 +2412,8 @@ fn assert_use_def( .as_str() .ends_with(def_file)); assert!(type_str == format!("{}", use_def.use_type)); + + assert!(doc_string == use_def.doc_string); match use_def.type_def_loc { Some(type_def_loc) => { let tdef_line = type_def.unwrap().0; @@ -2251,6 +2431,258 @@ fn assert_use_def( } } +#[cfg(test)] +fn assert_use_def( + mod_symbols: &UseDefMap, + file_name_mapping: &BTreeMap, + use_idx: usize, + use_line: u32, + use_col: u32, + def_line: u32, + def_col: u32, + def_file: &str, + type_str: &str, + type_def: Option<(u32, u32, &str)>, +) { + assert_use_def_with_doc_string( + mod_symbols, + file_name_mapping, + use_idx, + use_line, + use_col, + def_line, + def_col, + def_file, + type_str, + type_def, + "", + ) +} + +#[test] +/// Tests if symbolication + doc_string information for documented Move constructs is constructed correctly. +fn docstring_test() { + let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + + path.push("tests/symbols"); + + let (symbols_opt, _) = Symbolicator::get_symbols(path.as_path()).unwrap(); + let symbols = symbols_opt.unwrap(); + + let mut fpath = path.clone(); + fpath.push("sources/M6.move"); + let cpath = dunce::canonicalize(&fpath).unwrap(); + + let mod_symbols = symbols.file_use_defs.get(&cpath).unwrap(); + + // struct def name + assert_use_def_with_doc_string( + mod_symbols, + &symbols.file_name_mapping, + 0, + 4, + 11, + 4, + 11, + "M6.move", + "Symbols::M6::DocumentedStruct", + Some((4, 11, "M6.move")), + "This is a documented struct\nWith a multi-line docstring\n", + ); + + // const def name + assert_use_def_with_doc_string( + mod_symbols, + &symbols.file_name_mapping, + 0, + 10, + 10, + 10, + 10, + "M6.move", + "u64", + None, + "Constant containing the answer to the universe\n", + ); + + // function def name + assert_use_def_with_doc_string( + mod_symbols, + &symbols.file_name_mapping, + 0, + 14, + 8, + 14, + 8, + "M6.move", + "fun Symbols::M6::unpack(s: Symbols::M6::DocumentedStruct): u64", + None, + "A documented function that unpacks a DocumentedStruct\n", + ); + // param var (unpack function) + assert_use_def_with_doc_string( + mod_symbols, + &symbols.file_name_mapping, + 1, + 14, + 15, + 14, + 15, + "M6.move", + "Symbols::M6::DocumentedStruct", + Some((4, 11, "M6.move")), + "A documented function that unpacks a DocumentedStruct\n", + ); + // struct name in param type (unpack function) + assert_use_def_with_doc_string( + mod_symbols, + &symbols.file_name_mapping, + 2, + 14, + 18, + 4, + 11, + "M6.move", + "Symbols::M6::DocumentedStruct", + Some((4, 11, "M6.move")), + "This is a documented struct\nWith a multi-line docstring\n", + ); + // struct name in unpack (unpack function) + assert_use_def_with_doc_string( + mod_symbols, + &symbols.file_name_mapping, + 0, + 15, + 12, + 4, + 11, + "M6.move", + "Symbols::M6::DocumentedStruct", + Some((4, 11, "M6.move")), + "This is a documented struct\nWith a multi-line docstring\n", + ); + // field name in unpack (unpack function) + assert_use_def_with_doc_string( + mod_symbols, + &symbols.file_name_mapping, + 1, + 15, + 31, + 6, + 8, + "M6.move", + "u64", + None, + "A documented field\n", + ); + // moved var in unpack assignment (unpack function) + assert_use_def_with_doc_string( + mod_symbols, + &symbols.file_name_mapping, + 3, + 15, + 59, + 14, + 15, + "M6.move", + "Symbols::M6::DocumentedStruct", + Some((4, 11, "M6.move")), + "A documented function that unpacks a DocumentedStruct\n", + ); + + // docstring construction for multi-line /** .. */ based strings + assert_use_def_with_doc_string( + mod_symbols, + &symbols.file_name_mapping, + 0, + 26, + 8, + 26, + 8, + "M6.move", + "fun Symbols::M6::other_doc_struct(): Symbols::M7::OtherDocStruct", + Some((3, 11, "M7.move")), + "\nThis is a multiline docstring\n\nThis docstring has empty lines.\n\nIt uses the ** format instead of ///\n\n", + ); + + // docstring construction for single-line /** .. */ based strings + assert_use_def_with_doc_string( + mod_symbols, + &symbols.file_name_mapping, + 0, + 31, + 8, + 31, + 8, + "M6.move", + "fun Symbols::M6::acq(addr: address): u64 acquires Symbols::M6::DocumentedStruct", + None, + "Asterix based single-line docstring\n", + ); + + /* Test doc_string construction for struct/function imported from another module */ + + // other module struct name (other_doc_struct function) + assert_use_def_with_doc_string( + mod_symbols, + &symbols.file_name_mapping, + 1, + 26, + 41, + 3, + 11, + "M7.move", + "Symbols::M7::OtherDocStruct", + Some((3, 11, "M7.move")), + "Documented struct in another module\n", + ); + + // function name in a call (other_doc_struct function) + assert_use_def_with_doc_string( + mod_symbols, + &symbols.file_name_mapping, + 0, + 27, + 21, + 9, + 15, + "M7.move", + "fun Symbols::M7::create_other_struct(v: u64): Symbols::M7::OtherDocStruct", + Some((3, 11, "M7.move")), + "Documented initializer in another module\n", + ); + + // const in param (other_doc_struct function) + assert_use_def_with_doc_string( + mod_symbols, + &symbols.file_name_mapping, + 1, + 27, + 41, + 10, + 10, + "M6.move", + "u64", + None, + "Constant containing the answer to the universe\n", + ); + + // // other documented struct name imported (other_doc_struct_import function) + assert_use_def_with_doc_string( + mod_symbols, + &symbols.file_name_mapping, + 1, + 38, + 35, + 3, + 11, + "M7.move", + "Symbols::M7::OtherDocStruct", + Some((3, 11, "M7.move")), + "Documented struct in another module\n", + ); +} + #[test] /// Tests if symbolication information for specific Move constructs has been constructed correctly. fn symbols_test() { @@ -2303,7 +2735,7 @@ fn symbols_test() { 9, 8, "M1.move", - "fun Symbols::M1::unpack(Symbols::M1::SomeStruct): u64", + "fun Symbols::M1::unpack(s: Symbols::M1::SomeStruct): u64", None, ); // param var (unpack function) @@ -2472,7 +2904,7 @@ fn symbols_test() { 6, 15, "M2.move", - "fun Symbols::M2::some_other_struct(u64): Symbols::M2::SomeOtherStruct", + "fun Symbols::M2::some_other_struct(v: u64): Symbols::M2::SomeOtherStruct", Some((2, 11, "M2.move")), ); // const in param (other_mod_struct function) @@ -2511,7 +2943,7 @@ fn symbols_test() { 34, 8, "M1.move", - "fun Symbols::M1::acq(address): u64 acquires Symbols::M1::SomeStruct", + "fun Symbols::M1::acq(addr: address): u64 acquires Symbols::M1::SomeStruct", None, ); // struct name in acquires (acq function) @@ -2693,7 +3125,7 @@ fn symbols_test() { 61, 8, "M1.move", - "fun Symbols::M1::ret(bool, u64): u64", + "fun Symbols::M1::ret(p1: bool, p2: u64): u64", None, ); // returned value (ret function) diff --git a/language/move-analyzer/tests/symbols/sources/M6.move b/language/move-analyzer/tests/symbols/sources/M6.move new file mode 100644 index 0000000000..aa3427fa8c --- /dev/null +++ b/language/move-analyzer/tests/symbols/sources/M6.move @@ -0,0 +1,42 @@ +module Symbols::M6 { + + /// This is a documented struct + /// With a multi-line docstring + struct DocumentedStruct has key, drop, store { + /// A documented field + documented_field: u64, + } + + /// Constant containing the answer to the universe + const DOCUMENTED_CONSTANT: u64 = 42; + + + /// A documented function that unpacks a DocumentedStruct + fun unpack(s: DocumentedStruct): u64 { + let DocumentedStruct { documented_field: value } = s; + value + } + + /** + This is a multiline docstring + + This docstring has empty lines. + + It uses the ** format instead of /// + */ + fun other_doc_struct(): Symbols::M7::OtherDocStruct { + Symbols::M7::create_other_struct(DOCUMENTED_CONSTANT) + } + + /** Asterix based single-line docstring */ + fun acq(addr: address): u64 acquires DocumentedStruct { + let val = borrow_global(addr); + val.documented_field + } + + use Symbols::M7::{Self, OtherDocStruct}; + + fun other_doc_struct_import(): OtherDocStruct { + M7::create_other_struct(7) + } +} diff --git a/language/move-analyzer/tests/symbols/sources/M7.move b/language/move-analyzer/tests/symbols/sources/M7.move new file mode 100644 index 0000000000..b4165da101 --- /dev/null +++ b/language/move-analyzer/tests/symbols/sources/M7.move @@ -0,0 +1,13 @@ +module Symbols::M7 { + + /// Documented struct in another module + struct OtherDocStruct has drop { + /// Documented field in another module + some_field: u64, + } + + /// Documented initializer in another module + public fun create_other_struct(v: u64): OtherDocStruct { + OtherDocStruct { some_field: v } + } +} diff --git a/language/move-binary-format/Cargo.toml b/language/move-binary-format/Cargo.toml index 0ae756af37..25ea04cdf2 100644 --- a/language/move-binary-format/Cargo.toml +++ b/language/move-binary-format/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = ["crates-io"] -edition = "2018" +edition = "2021" [dependencies] anyhow = "1.0.52" diff --git a/language/move-binary-format/serializer-tests/Cargo.toml b/language/move-binary-format/serializer-tests/Cargo.toml index a7a85baba6..1cd9077992 100644 --- a/language/move-binary-format/serializer-tests/Cargo.toml +++ b/language/move-binary-format/serializer-tests/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dev-dependencies] proptest = "1.0.0" diff --git a/language/move-binary-format/src/check_bounds.rs b/language/move-binary-format/src/check_bounds.rs index dccc77a14a..fdf2c4c313 100644 --- a/language/move-binary-format/src/check_bounds.rs +++ b/language/move-binary-format/src/check_bounds.rs @@ -324,14 +324,28 @@ impl<'a> BoundsChecker<'a> { None => return Ok(()), }; - debug_assert!(function_def.function.into_index() < self.view.function_handles().len()); + if function_def.function.into_index() >= self.view.function_handles().len() { + return Err(verification_error( + StatusCode::INDEX_OUT_OF_BOUNDS, + IndexKind::FunctionDefinition, + function_def_idx as TableIndex, + )); + } let function_handle = &self.view.function_handles()[function_def.function.into_index()]; - - debug_assert!(function_handle.parameters.into_index() < self.view.signatures().len()); + if function_handle.parameters.into_index() >= self.view.signatures().len() { + return Err(verification_error( + StatusCode::INDEX_OUT_OF_BOUNDS, + IndexKind::FunctionDefinition, + function_def_idx as TableIndex, + )); + } let parameters = &self.view.signatures()[function_handle.parameters.into_index()]; // check if the number of parameters + locals is less than u8::MAX - let locals_count = self.get_locals(code_unit)?.len() + parameters.len(); + let locals_count = self + .get_locals(code_unit)? + .len() + .saturating_add(parameters.len()); if locals_count > (u8::MAX as usize) + 1 { return Err(verification_error( @@ -353,7 +367,8 @@ impl<'a> BoundsChecker<'a> { check_bounds_impl(self.view.signatures(), code_unit.locals)?; let locals = self.get_locals(code_unit)?; - let locals_count = locals.len() + parameters.len(); + // Use saturating add for stability; range checked above + let locals_count = locals.len().saturating_add(parameters.len()); // if there are locals check that the type parameters in local signature are in bounds. let type_param_count = type_parameters.len(); diff --git a/language/move-binary-format/src/compatibility.rs b/language/move-binary-format/src/compatibility.rs index 258727dc2f..ea22b1c1ce 100644 --- a/language/move-binary-format/src/compatibility.rs +++ b/language/move-binary-format/src/compatibility.rs @@ -19,10 +19,51 @@ use std::collections::BTreeSet; pub struct Compatibility { /// If false, dependent modules that reference functions or structs in this module may not link pub struct_and_function_linking: bool, - /// If false, attempting to read structs previously published by this module will fail at runtime + /// If false, attempting to read structs previously published by this module will fail at + /// runtime, or worse, may allow to re-interpret representations maliciously. pub struct_layout: bool, } +#[derive(Debug, Clone, Copy)] +pub struct CompatibilityConfig { + /// if false, do not ensure the dependent modules that reference functions or structs in this module can link + pub check_struct_and_function_linking: bool, + /// if false, do not ensure the struct layout capability + pub check_struct_layout: bool, + /// if false, treat `friend` as `private` when `check_struct_and_function_linking`. + /// `check_friend_linking` only makes sense if `check_struct_and_function_linking` is true. + pub check_friend_linking: bool, +} + +impl Default for CompatibilityConfig { + fn default() -> Self { + Self { + check_struct_and_function_linking: true, + check_struct_layout: true, + check_friend_linking: true, + } + } +} + +impl CompatibilityConfig { + pub fn full_check() -> Self { + Self::default() + } + + pub fn no_check() -> Self { + Self { + check_struct_and_function_linking: false, + check_struct_layout: false, + check_friend_linking: false, + } + } + + pub fn need_check_compat(&self) -> bool { + // `check_friend_linking` is part of check_struct_and_function_linking, so ignore it at here. + self.check_struct_and_function_linking || self.check_struct_layout + } +} + impl Compatibility { /// Return true if the two module s compared in the compatiblity check are both linking and /// layout compatible. @@ -31,7 +72,11 @@ impl Compatibility { } /// Return compatibility assessment for `new_module` relative to old module `old_module`. - pub fn check(old_module: &Module, new_module: &Module) -> Compatibility { + pub fn check( + check_friend_linking: bool, + old_module: &Module, + new_module: &Module, + ) -> Compatibility { let mut struct_and_function_linking = true; let mut struct_layout = true; @@ -46,18 +91,11 @@ impl Compatibility { Some(new_struct) => new_struct, None => { // Struct not present in new . Existing modules that depend on this struct will fail to link with the new version of the module. + // Also, struct layout cannot be guaranteed transitively, because after + // removing the struct, it could be re-added later with a different layout. struct_and_function_linking = false; - // Note: we intentionally do *not* label this a layout compatibility violation. - // Existing modules can still successfully read previously published values of - // this struct `Parent::T`. That is, code like the function `foo` in - // ``` - // struct S { t: Parent::T } - // public fun foo(a: addr): S { move_from(addr) } - // ``` - // in module `Child` will continue to run without error. But values of type - // `Parent::T` in `Child` are now "orphaned" in the sense that `Parent` no - // longer exposes any API for reading/writing them. - continue; + struct_layout = false; + break; } }; @@ -73,16 +111,9 @@ impl Compatibility { // Fields changed. Code in this module will fail at runtime if it tries to // read a previously published struct value // TODO: this is a stricter definition than required. We could in principle - // choose to label the following as compatible - // (1) changing the name (but not position or type) of a field. The VM does - // not care about the name of a field (it's purely informational), but - // clients presumably do. - // (2) changing the type of a field to a different, but layout and kind - // compatible type. E.g. `struct S { b: bool }` to `struct S { b: B }` - // where - // B is struct B { some_name: bool }. TODO: does this affect clients? I - // think not--the serialization of the same data with these two types - // will be the same. + // choose that changing the name (but not position or type) of a field is + // compatible. The VM does not care about the name of a field + // (it's purely informational), but clients presumably do. struct_layout = false } } @@ -102,6 +133,9 @@ impl Compatibility { // friend list. But for simplicity, we decided to go to the more restrictive form now and // we may revisit this in the future. for (name, old_func) in &old_module.exposed_functions { + if !check_friend_linking && matches!(old_func.visibility, Visibility::Friend) { + continue; + } let new_func = match new_module.exposed_functions.get(name) { Some(new_func) => new_func, None => { @@ -144,18 +178,17 @@ impl Compatibility { } } - // check friend declarations compatibility - // - // - additions to the list are allowed - // - removals are not allowed - // - // NOTE: we may also relax this checking a bit in the future: we may allow the removal of - // a module removed from the friend list if the module does not call any friend function - // in this module. - let old_friend_module_ids: BTreeSet<_> = old_module.friends.iter().cloned().collect(); - let new_friend_module_ids: BTreeSet<_> = new_module.friends.iter().cloned().collect(); - if !old_friend_module_ids.is_subset(&new_friend_module_ids) { - struct_and_function_linking = false; + if check_friend_linking { + // check friend declarations compatibility + // + // - additions to the list are allowed + // - removals are not allowed + // + let old_friend_module_ids: BTreeSet<_> = old_module.friends.iter().cloned().collect(); + let new_friend_module_ids: BTreeSet<_> = new_module.friends.iter().cloned().collect(); + if !old_friend_module_ids.is_subset(&new_friend_module_ids) { + struct_and_function_linking = false; + } } Compatibility { diff --git a/language/move-binary-format/src/deserializer.rs b/language/move-binary-format/src/deserializer.rs index 3f68235c8d..99762da46f 100644 --- a/language/move-binary-format/src/deserializer.rs +++ b/language/move-binary-format/src/deserializer.rs @@ -988,6 +988,10 @@ fn load_signature_token(cursor: &mut VersionedCursor) -> BinaryLoaderResult { let sh_idx = load_struct_handle_index(cursor)?; let arity = load_type_parameter_count(cursor)?; + if arity == 0 { + return Err(PartialVMError::new(StatusCode::MALFORMED) + .with_message("Struct inst with arity 0".to_string())); + } T::StructInst { sh_idx, arity, diff --git a/language/move-binary-format/src/errors.rs b/language/move-binary-format/src/errors.rs index 9f56314968..50353c46bf 100644 --- a/language/move-binary-format/src/errors.rs +++ b/language/move-binary-format/src/errors.rs @@ -82,10 +82,17 @@ impl VMError { VMStatus::Error(StatusCode::ABORTED) } - // TODO Errors for OUT_OF_GAS do not always have index set (major_status, sub_status, location) if major_status.status_type() == StatusType::Execution => { + let abort_location = match &location { + Location::Script => vm_status::AbortLocation::Script, + Location::Module(id) => vm_status::AbortLocation::Module(id.clone()), + Location::Undefined => { + return VMStatus::Error(major_status); + } + }; + // Errors for OUT_OF_GAS do not always have index set: if it does not, it should already return above. debug_assert!( offsets.len() == 1, "Unexpected offsets. major_status: {:?}\ @@ -97,13 +104,6 @@ impl VMError { location, offsets ); - let abort_location = match location { - Location::Script => vm_status::AbortLocation::Script, - Location::Module(id) => vm_status::AbortLocation::Module(id), - Location::Undefined => { - return VMStatus::Error(major_status); - } - }; let (function, code_offset) = match offsets.pop() { None => { return VMStatus::Error(major_status); diff --git a/language/move-binary-format/src/file_format.rs b/language/move-binary-format/src/file_format.rs index e6a92f591f..75030ae694 100644 --- a/language/move-binary-format/src/file_format.rs +++ b/language/move-binary-format/src/file_format.rs @@ -1559,8 +1559,6 @@ pub enum Bytecode { VecSwap(SignatureIndex), } -pub const NUMBER_OF_NATIVE_FUNCTIONS: usize = 18; - impl ::std::fmt::Debug for Bytecode { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { match self { @@ -1670,11 +1668,16 @@ impl Bytecode { /// Return the successor offsets of this bytecode instruction. pub fn get_successors(pc: CodeOffset, code: &[Bytecode]) -> Vec { assert!( - // The program counter could be added to at most twice and must remain - // within the bounds of the code. - pc <= u16::max_value() - 2 && (pc as usize) < code.len(), + // The program counter must remain within the bounds of the code + pc < u16::MAX && (pc as usize) < code.len(), "Program counter out of bounds" ); + + // Return early to prevent overflow if pc is hiting the end of max number of instructions allowed (u16::MAX). + if pc > u16::max_value() - 2 { + return vec![]; + } + let bytecode = &code[pc as usize]; let mut v = vec![]; diff --git a/language/move-binary-format/src/proptest_types/functions.rs b/language/move-binary-format/src/proptest_types/functions.rs index 26d402b62c..2158d5e6dd 100644 --- a/language/move-binary-format/src/proptest_types/functions.rs +++ b/language/move-binary-format/src/proptest_types/functions.rs @@ -782,6 +782,9 @@ impl BytecodeGen { Bytecode::ImmBorrowLoc(idx.index(locals_signature.len()) as LocalIndex) } BytecodeGen::VecPack((idx, num)) => { + if num > u16::MAX as u64 { + return None; + } let sigs_len = state.signatures.signatures.len(); if sigs_len == 0 { return None; @@ -854,6 +857,9 @@ impl BytecodeGen { Bytecode::VecPopBack(SignatureIndex(sig_idx as TableIndex)) } BytecodeGen::VecUnpack((idx, num)) => { + if num > u16::MAX as u64 { + return None; + } let sigs_len = state.signatures.signatures.len(); if sigs_len == 0 { return None; diff --git a/language/move-binary-format/src/unit_tests/binary_tests.rs b/language/move-binary-format/src/unit_tests/binary_tests.rs index 64353191f4..72338e2c26 100644 --- a/language/move-binary-format/src/unit_tests/binary_tests.rs +++ b/language/move-binary-format/src/unit_tests/binary_tests.rs @@ -2,7 +2,7 @@ // Copyright (c) The Move Contributors // SPDX-License-Identifier: Apache-2.0 -use crate::file_format_common::*; +use crate::{file_format::Bytecode, file_format_common::*}; use proptest::prelude::*; #[test] @@ -14,6 +14,18 @@ fn binary_len() { assert_eq!(binary_data.len(), 100); } +#[test] +fn test_max_number_of_bytecode() { + let mut nops = vec![]; + for _ in 0..u16::MAX - 1 { + nops.push(Bytecode::Nop); + } + nops.push(Bytecode::Ret); + + let result = Bytecode::get_successors(u16::MAX - 1, &nops); + assert!(result.is_empty()); +} + proptest! { #[test] fn vec_to_binary(vec in any::>()) { diff --git a/language/move-binary-format/src/unit_tests/compatibility_tests.rs b/language/move-binary-format/src/unit_tests/compatibility_tests.rs index d6855421e5..9cef907ee2 100644 --- a/language/move-binary-format/src/unit_tests/compatibility_tests.rs +++ b/language/move-binary-format/src/unit_tests/compatibility_tests.rs @@ -82,7 +82,7 @@ const COMPATIBLE: Compatibility = Compatibility { fn deprecated_unchanged_script_visibility() { let script_module = mk_module(Visibility::DEPRECATED_SCRIPT); assert_eq!( - Compatibility::check(&script_module, &script_module,), + Compatibility::check(true, &script_module, &script_module,), COMPATIBLE ); } @@ -93,19 +93,19 @@ fn deprecated_remove_script_visibility() { // script -> private, not allowed let private_module = mk_module(Visibility::Private as u8); assert_eq!( - Compatibility::check(&script_module, &private_module), + Compatibility::check(true, &script_module, &private_module), NON_COMPATIBLE ); // script -> public, not allowed let public_module = mk_module(Visibility::Public as u8); assert_eq!( - Compatibility::check(&script_module, &public_module), + Compatibility::check(true, &script_module, &public_module), NON_COMPATIBLE ); // script -> friend, not allowed let friend_module = mk_module(Visibility::Friend as u8); assert_eq!( - Compatibility::check(&script_module, &friend_module), + Compatibility::check(true, &script_module, &friend_module), NON_COMPATIBLE ); } @@ -116,19 +116,19 @@ fn deprecated_add_script_visibility() { // private -> script, allowed let private_module = mk_module(Visibility::Private as u8); assert_eq!( - Compatibility::check(&private_module, &script_module,), + Compatibility::check(true, &private_module, &script_module,), COMPATIBLE ); // public -> script, not allowed let public_module = mk_module(Visibility::Public as u8); assert_eq!( - Compatibility::check(&public_module, &script_module), + Compatibility::check(true, &public_module, &script_module), NON_COMPATIBLE ); // friend -> script, not allowed let friend_module = mk_module(Visibility::Friend as u8); assert_eq!( - Compatibility::check(&friend_module, &script_module), + Compatibility::check(true, &friend_module, &script_module), NON_COMPATIBLE ); } diff --git a/language/move-binary-format/src/unit_tests/signature_token_tests.rs b/language/move-binary-format/src/unit_tests/signature_token_tests.rs index 900319c2cc..c2e2d82130 100644 --- a/language/move-binary-format/src/unit_tests/signature_token_tests.rs +++ b/language/move-binary-format/src/unit_tests/signature_token_tests.rs @@ -5,7 +5,7 @@ use crate::{ deserializer::load_signature_token_test_entry, file_format::{SignatureToken, StructHandleIndex}, - file_format_common::{BinaryData, SIGNATURE_TOKEN_DEPTH_MAX}, + file_format_common::{BinaryData, SerializedType, SIGNATURE_TOKEN_DEPTH_MAX}, serializer::{serialize_signature_token, serialize_signature_token_unchecked}, }; use std::io::Cursor; @@ -44,3 +44,46 @@ fn serialize_nested_types_too_deep() { load_signature_token_test_entry(cursor).expect_err("deserialization should fail"); } } + +#[test] +fn deserialize_struct_inst_arity_0() { + let cursor = Cursor::new( + [ + SerializedType::STRUCT_INST as u8, + 0x0, /* struct handle idx */ + 0x0, /* arity */ + SerializedType::BOOL as u8, + ] + .as_slice(), + ); + load_signature_token_test_entry(cursor).expect_err("deserialization should fail"); +} + +#[test] +fn deserialize_struct_inst_arity_1() { + let cursor = Cursor::new( + [ + SerializedType::STRUCT_INST as u8, + 0x0, /* struct handle idx */ + 0x1, /* arity */ + SerializedType::BOOL as u8, + ] + .as_slice(), + ); + load_signature_token_test_entry(cursor).expect("deserialization should succeed"); +} + +#[test] +fn deserialize_struct_inst_arity_2() { + let cursor = Cursor::new( + [ + SerializedType::STRUCT_INST as u8, + 0x0, /* struct handle idx */ + 0x2, /* arity */ + SerializedType::BOOL as u8, + SerializedType::BOOL as u8, + ] + .as_slice(), + ); + load_signature_token_test_entry(cursor).expect("deserialization should succeed"); +} diff --git a/language/move-borrow-graph/Cargo.toml b/language/move-borrow-graph/Cargo.toml index 5e2285b2ef..891b9be448 100644 --- a/language/move-borrow-graph/Cargo.toml +++ b/language/move-borrow-graph/Cargo.toml @@ -3,5 +3,5 @@ name = "move-borrow-graph" version = "0.0.1" authors = ["Diem Association "] publish = false -edition = "2018" +edition = "2021" license = "Apache-2.0" diff --git a/language/move-borrow-graph/src/graph.rs b/language/move-borrow-graph/src/graph.rs index 0aa742ad65..1e8a7925d0 100644 --- a/language/move-borrow-graph/src/graph.rs +++ b/language/move-borrow-graph/src/graph.rs @@ -12,7 +12,7 @@ use std::collections::{BTreeMap, BTreeSet}; // Definitions //************************************************************************************************** -#[derive(Clone, Debug, Default, PartialEq)] +#[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct BorrowGraph(BTreeMap>); //************************************************************************************************** diff --git a/language/move-borrow-graph/src/references.rs b/language/move-borrow-graph/src/references.rs index a420d56032..a1d1090f9a 100644 --- a/language/move-borrow-graph/src/references.rs +++ b/language/move-borrow-graph/src/references.rs @@ -229,7 +229,7 @@ impl Debug for BorrowEdge { // Iteration //********************************************************************************************** -impl<'a, Loc: Copy, Lbl: Clone + Ord> IntoIterator for BorrowEdgeSet { +impl IntoIterator for BorrowEdgeSet { type Item = BorrowEdge; type IntoIter = std::collections::btree_set::IntoIter>; diff --git a/language/move-bytecode-verifier/Cargo.toml b/language/move-bytecode-verifier/Cargo.toml index c85640fbeb..5049b4be10 100644 --- a/language/move-bytecode-verifier/Cargo.toml +++ b/language/move-bytecode-verifier/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] anyhow = "1.0.52" diff --git a/language/move-bytecode-verifier/bytecode-verifier-tests/Cargo.toml b/language/move-bytecode-verifier/bytecode-verifier-tests/Cargo.toml index 7ba9a30293..8b256d6d88 100644 --- a/language/move-bytecode-verifier/bytecode-verifier-tests/Cargo.toml +++ b/language/move-bytecode-verifier/bytecode-verifier-tests/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dev-dependencies] petgraph = "0.5.1" diff --git a/language/move-bytecode-verifier/bytecode-verifier-tests/src/unit_tests/code_unit_tests.rs b/language/move-bytecode-verifier/bytecode-verifier-tests/src/unit_tests/code_unit_tests.rs index e8091afb85..01e2921d69 100644 --- a/language/move-bytecode-verifier/bytecode-verifier-tests/src/unit_tests/code_unit_tests.rs +++ b/language/move-bytecode-verifier/bytecode-verifier-tests/src/unit_tests/code_unit_tests.rs @@ -10,7 +10,7 @@ use move_core_types::vm_status::StatusCode; #[test] fn invalid_fallthrough_br_true() { let module = dummy_procedure_module(vec![Bytecode::LdFalse, Bytecode::BrTrue(1)]); - let result = CodeUnitVerifier::verify_module(&module); + let result = CodeUnitVerifier::verify_module(&Default::default(), &module); assert_eq!( result.unwrap_err().major_status(), StatusCode::INVALID_FALL_THROUGH @@ -20,7 +20,7 @@ fn invalid_fallthrough_br_true() { #[test] fn invalid_fallthrough_br_false() { let module = dummy_procedure_module(vec![Bytecode::LdTrue, Bytecode::BrFalse(1)]); - let result = CodeUnitVerifier::verify_module(&module); + let result = CodeUnitVerifier::verify_module(&Default::default(), &module); assert_eq!( result.unwrap_err().major_status(), StatusCode::INVALID_FALL_THROUGH @@ -31,7 +31,7 @@ fn invalid_fallthrough_br_false() { #[test] fn invalid_fallthrough_non_branch() { let module = dummy_procedure_module(vec![Bytecode::LdTrue, Bytecode::Pop]); - let result = CodeUnitVerifier::verify_module(&module); + let result = CodeUnitVerifier::verify_module(&Default::default(), &module); assert_eq!( result.unwrap_err().major_status(), StatusCode::INVALID_FALL_THROUGH @@ -41,20 +41,33 @@ fn invalid_fallthrough_non_branch() { #[test] fn valid_fallthrough_branch() { let module = dummy_procedure_module(vec![Bytecode::Branch(0)]); - let result = CodeUnitVerifier::verify_module(&module); + let result = CodeUnitVerifier::verify_module(&Default::default(), &module); assert!(result.is_ok()); } #[test] fn valid_fallthrough_ret() { let module = dummy_procedure_module(vec![Bytecode::Ret]); - let result = CodeUnitVerifier::verify_module(&module); + let result = CodeUnitVerifier::verify_module(&Default::default(), &module); assert!(result.is_ok()); } #[test] fn valid_fallthrough_abort() { let module = dummy_procedure_module(vec![Bytecode::LdU64(7), Bytecode::Abort]); - let result = CodeUnitVerifier::verify_module(&module); + let result = CodeUnitVerifier::verify_module(&Default::default(), &module); + assert!(result.is_ok()); +} + +#[test] +fn test_max_number_of_bytecode() { + let mut nops = vec![]; + for _ in 0..u16::MAX - 1 { + nops.push(Bytecode::Nop); + } + nops.push(Bytecode::Ret); + let module = dummy_procedure_module(nops); + + let result = CodeUnitVerifier::verify_module(&Default::default(), &module); assert!(result.is_ok()); } diff --git a/language/move-bytecode-verifier/bytecode-verifier-tests/src/unit_tests/control_flow_tests.rs b/language/move-bytecode-verifier/bytecode-verifier-tests/src/unit_tests/control_flow_tests.rs index ddf7796f3d..8a4317d953 100644 --- a/language/move-bytecode-verifier/bytecode-verifier-tests/src/unit_tests/control_flow_tests.rs +++ b/language/move-bytecode-verifier/bytecode-verifier-tests/src/unit_tests/control_flow_tests.rs @@ -8,10 +8,10 @@ use move_binary_format::{ errors::PartialVMResult, file_format::{Bytecode, CompiledModule, FunctionDefinitionIndex, TableIndex}, }; -use move_bytecode_verifier::control_flow; +use move_bytecode_verifier::{control_flow, VerifierConfig}; use move_core_types::vm_status::StatusCode; -fn verify_module(module: &CompiledModule) -> PartialVMResult<()> { +fn verify_module(verifier_config: &VerifierConfig, module: &CompiledModule) -> PartialVMResult<()> { for (idx, function_definition) in module .function_defs() .iter() @@ -19,6 +19,7 @@ fn verify_module(module: &CompiledModule) -> PartialVMResult<()> { .filter(|(_, def)| !def.is_native()) { control_flow::verify( + verifier_config, Some(FunctionDefinitionIndex(idx as TableIndex)), function_definition .code @@ -36,7 +37,7 @@ fn verify_module(module: &CompiledModule) -> PartialVMResult<()> { #[test] fn invalid_fallthrough_br_true() { let module = dummy_procedure_module(vec![Bytecode::LdFalse, Bytecode::BrTrue(1)]); - let result = verify_module(&module); + let result = verify_module(&Default::default(), &module); assert_eq!( result.unwrap_err().major_status(), StatusCode::INVALID_FALL_THROUGH @@ -46,7 +47,7 @@ fn invalid_fallthrough_br_true() { #[test] fn invalid_fallthrough_br_false() { let module = dummy_procedure_module(vec![Bytecode::LdTrue, Bytecode::BrFalse(1)]); - let result = verify_module(&module); + let result = verify_module(&Default::default(), &module); assert_eq!( result.unwrap_err().major_status(), StatusCode::INVALID_FALL_THROUGH @@ -57,7 +58,7 @@ fn invalid_fallthrough_br_false() { #[test] fn invalid_fallthrough_non_branch() { let module = dummy_procedure_module(vec![Bytecode::LdTrue, Bytecode::Pop]); - let result = verify_module(&module); + let result = verify_module(&Default::default(), &module); assert_eq!( result.unwrap_err().major_status(), StatusCode::INVALID_FALL_THROUGH @@ -67,20 +68,61 @@ fn invalid_fallthrough_non_branch() { #[test] fn valid_fallthrough_branch() { let module = dummy_procedure_module(vec![Bytecode::Branch(0)]); - let result = verify_module(&module); + let result = verify_module(&Default::default(), &module); assert!(result.is_ok()); } #[test] fn valid_fallthrough_ret() { let module = dummy_procedure_module(vec![Bytecode::Ret]); - let result = verify_module(&module); + let result = verify_module(&Default::default(), &module); assert!(result.is_ok()); } #[test] fn valid_fallthrough_abort() { let module = dummy_procedure_module(vec![Bytecode::LdU64(7), Bytecode::Abort]); - let result = verify_module(&module); + let result = verify_module(&Default::default(), &module); assert!(result.is_ok()); } + +#[test] +fn nested_loops_max_depth() { + let module = dummy_procedure_module(vec![ + Bytecode::LdFalse, + Bytecode::LdFalse, + Bytecode::BrFalse(1), + Bytecode::BrFalse(0), + Bytecode::Ret, + ]); + let result = verify_module( + &VerifierConfig { + max_loop_depth: Some(2), + }, + &module, + ); + assert!(result.is_ok()); +} + +#[test] +fn nested_loops_exceed_max_depth() { + let module = dummy_procedure_module(vec![ + Bytecode::LdFalse, + Bytecode::LdFalse, + Bytecode::LdFalse, + Bytecode::BrFalse(2), + Bytecode::BrFalse(1), + Bytecode::BrFalse(0), + Bytecode::Ret, + ]); + let result = verify_module( + &VerifierConfig { + max_loop_depth: Some(2), + }, + &module, + ); + assert_eq!( + result.unwrap_err().major_status(), + StatusCode::LOOP_MAX_DEPTH_REACHED + ); +} diff --git a/language/move-bytecode-verifier/bytecode-verifier-tests/src/unit_tests/negative_stack_size_tests.rs b/language/move-bytecode-verifier/bytecode-verifier-tests/src/unit_tests/negative_stack_size_tests.rs index ceacdcea50..c239747e3d 100644 --- a/language/move-bytecode-verifier/bytecode-verifier-tests/src/unit_tests/negative_stack_size_tests.rs +++ b/language/move-bytecode-verifier/bytecode-verifier-tests/src/unit_tests/negative_stack_size_tests.rs @@ -10,7 +10,7 @@ use move_core_types::vm_status::StatusCode; #[test] fn one_pop_no_push() { let module = dummy_procedure_module(vec![Bytecode::Pop, Bytecode::Ret]); - let result = CodeUnitVerifier::verify_module(&module); + let result = CodeUnitVerifier::verify_module(&Default::default(), &module); assert_eq!( result.unwrap_err().major_status(), StatusCode::NEGATIVE_STACK_SIZE_WITHIN_BLOCK @@ -21,7 +21,7 @@ fn one_pop_no_push() { fn one_pop_one_push() { // Height: 0 + (-1 + 1) = 0 would have passed original usage verifier let module = dummy_procedure_module(vec![Bytecode::ReadRef, Bytecode::Ret]); - let result = CodeUnitVerifier::verify_module(&module); + let result = CodeUnitVerifier::verify_module(&Default::default(), &module); assert_eq!( result.unwrap_err().major_status(), StatusCode::NEGATIVE_STACK_SIZE_WITHIN_BLOCK @@ -32,7 +32,7 @@ fn one_pop_one_push() { fn two_pop_one_push() { // Height: 0 + 1 + (-2 + 1) = 0 would have passed original usage verifier let module = dummy_procedure_module(vec![Bytecode::LdU64(0), Bytecode::Add, Bytecode::Ret]); - let result = CodeUnitVerifier::verify_module(&module); + let result = CodeUnitVerifier::verify_module(&Default::default(), &module); assert_eq!( result.unwrap_err().major_status(), StatusCode::NEGATIVE_STACK_SIZE_WITHIN_BLOCK @@ -42,7 +42,7 @@ fn two_pop_one_push() { #[test] fn two_pop_no_push() { let module = dummy_procedure_module(vec![Bytecode::WriteRef, Bytecode::Ret]); - let result = CodeUnitVerifier::verify_module(&module); + let result = CodeUnitVerifier::verify_module(&Default::default(), &module); assert_eq!( result.unwrap_err().major_status(), StatusCode::NEGATIVE_STACK_SIZE_WITHIN_BLOCK diff --git a/language/move-bytecode-verifier/invalid-mutations/Cargo.toml b/language/move-bytecode-verifier/invalid-mutations/Cargo.toml index 5f6fc2617c..ed46af714c 100644 --- a/language/move-bytecode-verifier/invalid-mutations/Cargo.toml +++ b/language/move-bytecode-verifier/invalid-mutations/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "invalid-mutations" version = "0.1.0" -edition = "2018" +edition = "2021" authors = ["Diem Association "] description = "Diem invalid mutations" repository = "https://github.com/diem/diem" diff --git a/language/move-bytecode-verifier/src/absint.rs b/language/move-bytecode-verifier/src/absint.rs index acb4077ced..d43870c011 100644 --- a/language/move-bytecode-verifier/src/absint.rs +++ b/language/move-bytecode-verifier/src/absint.rs @@ -5,6 +5,7 @@ use move_binary_format::{ binary_views::FunctionView, control_flow_graph::{BlockId, ControlFlowGraph}, + errors::PartialVMResult, file_format::{Bytecode, CodeOffset}, }; use std::collections::BTreeMap; @@ -21,36 +22,21 @@ pub enum JoinResult { Unchanged, } -#[derive(Clone)] -pub enum BlockPostcondition { - /// Block not yet analyzed - Unprocessed, - /// Analyzing block was successful - /// TODO might carry post state at some point - Success, - /// Analyzing block resulted in an error - Error(AnalysisError), -} - #[allow(dead_code)] #[derive(Clone)] -pub struct BlockInvariant { +pub struct BlockInvariant { /// Precondition of the block - pub pre: State, - /// Postcondition of the block - pub post: BlockPostcondition, + pre: State, } /// A map from block id's to the pre/post of each block after a fixed point is reached. #[allow(dead_code)] -pub type InvariantMap = - BTreeMap>; +pub type InvariantMap = BTreeMap>; /// Take a pre-state + instruction and mutate it to produce a post-state /// Auxiliary data can be stored in self. pub trait TransferFunctions { type State: AbstractDomain; - type AnalysisError; /// Execute local@instr found at index local@index in the current basic block from pre-state /// local@pre. @@ -68,7 +54,7 @@ pub trait TransferFunctions { instr: &Bytecode, index: CodeOffset, last_index: CodeOffset, - ) -> Result<(), Self::AnalysisError>; + ) -> PartialVMResult<()>; } pub trait AbstractInterpreter: TransferFunctions { @@ -77,17 +63,11 @@ pub trait AbstractInterpreter: TransferFunctions { &mut self, initial_state: Self::State, function_view: &FunctionView, - ) -> InvariantMap { - let mut inv_map: InvariantMap = InvariantMap::new(); + ) -> PartialVMResult<()> { + let mut inv_map = InvariantMap::new(); let entry_block_id = function_view.cfg().entry_block_id(); let mut next_block = Some(entry_block_id); - inv_map.insert( - entry_block_id, - BlockInvariant { - pre: initial_state, - post: BlockPostcondition::Unprocessed, - }, - ); + inv_map.insert(entry_block_id, BlockInvariant { pre: initial_state }); while let Some(block_id) = next_block { let block_invariant = match inv_map.get_mut(&block_id) { @@ -101,17 +81,9 @@ pub trait AbstractInterpreter: TransferFunctions { }; let pre_state = &block_invariant.pre; - let post_state = match self.execute_block(block_id, pre_state, function_view) { - Err(e) => { - block_invariant.post = BlockPostcondition::Error(e); - next_block = function_view.cfg().next_block(block_id); - continue; - } - Ok(s) => { - block_invariant.post = BlockPostcondition::Success; - s - } - }; + // Note: this will stop analysis after the first error occurs, to avoid the risk of + // subsequent crashes + let post_state = self.execute_block(block_id, pre_state, function_view)?; let mut next_block_candidate = function_view.cfg().next_block(block_id); // propagate postcondition of this block to successor blocks @@ -136,8 +108,6 @@ pub trait AbstractInterpreter: TransferFunctions { { next_block_candidate = Some(*successor_block_id); } - // Pre has changed, the post condition is now unknown for the block - next_block_invariant.post = BlockPostcondition::Unprocessed } } } @@ -148,7 +118,6 @@ pub trait AbstractInterpreter: TransferFunctions { *successor_block_id, BlockInvariant { pre: post_state.clone(), - post: BlockPostcondition::Success, }, ); } @@ -156,7 +125,7 @@ pub trait AbstractInterpreter: TransferFunctions { } next_block = next_block_candidate; } - inv_map + Ok(()) } fn execute_block( @@ -164,7 +133,7 @@ pub trait AbstractInterpreter: TransferFunctions { block_id: BlockId, pre_state: &Self::State, function_view: &FunctionView, - ) -> Result { + ) -> PartialVMResult { let mut state_acc = pre_state.clone(); let block_end = function_view.cfg().block_end(block_id); for offset in function_view.cfg().instr_indexes(block_id) { diff --git a/language/move-bytecode-verifier/src/acquires_list_verifier.rs b/language/move-bytecode-verifier/src/acquires_list_verifier.rs index de034ff742..ed410fbfbb 100644 --- a/language/move-bytecode-verifier/src/acquires_list_verifier.rs +++ b/language/move-bytecode-verifier/src/acquires_list_verifier.rs @@ -11,6 +11,8 @@ //! - No missing resources (any resource acquired must be present) //! - No additional resources (no extraneous resources not actually acquired) +use std::collections::{BTreeSet, HashMap}; + use move_binary_format::{ access::ModuleAccess, errors::{PartialVMError, PartialVMResult}, @@ -20,7 +22,6 @@ use move_binary_format::{ }, }; use move_core_types::vm_status::StatusCode; -use std::collections::{BTreeSet, HashMap}; pub(crate) struct AcquiresVerifier<'a> { module: &'a CompiledModule, @@ -101,7 +102,70 @@ impl<'a> AcquiresVerifier<'a> { let si = self.module.struct_instantiation_at(*idx); self.struct_acquire(si.def, offset) } - _ => Ok(()), + + Bytecode::Pop + | Bytecode::BrTrue(_) + | Bytecode::BrFalse(_) + | Bytecode::Abort + | Bytecode::Branch(_) + | Bytecode::Nop + | Bytecode::Ret + | Bytecode::StLoc(_) + | Bytecode::MoveLoc(_) + | Bytecode::CopyLoc(_) + | Bytecode::ImmBorrowLoc(_) + | Bytecode::MutBorrowLoc(_) + | Bytecode::FreezeRef + | Bytecode::MutBorrowField(_) + | Bytecode::MutBorrowFieldGeneric(_) + | Bytecode::ImmBorrowField(_) + | Bytecode::ImmBorrowFieldGeneric(_) + | Bytecode::LdU8(_) + | Bytecode::LdU64(_) + | Bytecode::LdU128(_) + | Bytecode::LdConst(_) + | Bytecode::LdTrue + | Bytecode::LdFalse + | Bytecode::Pack(_) + | Bytecode::PackGeneric(_) + | Bytecode::Unpack(_) + | Bytecode::UnpackGeneric(_) + | Bytecode::ReadRef + | Bytecode::WriteRef + | Bytecode::CastU8 + | Bytecode::CastU64 + | Bytecode::CastU128 + | Bytecode::Add + | Bytecode::Sub + | Bytecode::Mul + | Bytecode::Mod + | Bytecode::Div + | Bytecode::BitOr + | Bytecode::BitAnd + | Bytecode::Xor + | Bytecode::Shl + | Bytecode::Shr + | Bytecode::Or + | Bytecode::And + | Bytecode::Not + | Bytecode::Eq + | Bytecode::Neq + | Bytecode::Lt + | Bytecode::Gt + | Bytecode::Le + | Bytecode::Ge + | Bytecode::Exists(_) + | Bytecode::ExistsGeneric(_) + | Bytecode::MoveTo(_) + | Bytecode::MoveToGeneric(_) + | Bytecode::VecPack(..) + | Bytecode::VecLen(_) + | Bytecode::VecImmBorrow(_) + | Bytecode::VecMutBorrow(_) + | Bytecode::VecPushBack(_) + | Bytecode::VecPopBack(_) + | Bytecode::VecUnpack(..) + | Bytecode::VecSwap(_) => Ok(()), } } diff --git a/language/move-bytecode-verifier/src/code_unit_verifier.rs b/language/move-bytecode-verifier/src/code_unit_verifier.rs index 13b96b20e5..79470e25c1 100644 --- a/language/move-bytecode-verifier/src/code_unit_verifier.rs +++ b/language/move-bytecode-verifier/src/code_unit_verifier.rs @@ -7,7 +7,7 @@ //! abstract_interpreter.rs. CodeUnitVerifier simply orchestrates calls into these two files. use crate::{ acquires_list_verifier::AcquiresVerifier, control_flow, locals_safety, reference_safety, - stack_usage_verifier::StackUsageVerifier, type_safety, + stack_usage_verifier::StackUsageVerifier, type_safety, verifier::VerifierConfig, }; use move_binary_format::{ access::ModuleAccess, @@ -28,26 +28,39 @@ pub struct CodeUnitVerifier<'a> { } impl<'a> CodeUnitVerifier<'a> { - pub fn verify_module(module: &'a CompiledModule) -> VMResult<()> { - Self::verify_module_impl(module).map_err(|e| e.finish(Location::Module(module.self_id()))) + pub fn verify_module( + verifier_config: &VerifierConfig, + module: &'a CompiledModule, + ) -> VMResult<()> { + Self::verify_module_impl(verifier_config, module) + .map_err(|e| e.finish(Location::Module(module.self_id()))) } - fn verify_module_impl(module: &'a CompiledModule) -> PartialVMResult<()> { + fn verify_module_impl( + verifier_config: &VerifierConfig, + module: &'a CompiledModule, + ) -> PartialVMResult<()> { for (idx, function_definition) in module.function_defs().iter().enumerate() { let index = FunctionDefinitionIndex(idx as TableIndex); - Self::verify_function(index, function_definition, module) + Self::verify_function(verifier_config, index, function_definition, module) .map_err(|err| err.at_index(IndexKind::FunctionDefinition, index.0))? } Ok(()) } - pub fn verify_script(module: &'a CompiledScript) -> VMResult<()> { - Self::verify_script_impl(module).map_err(|e| e.finish(Location::Script)) + pub fn verify_script( + verifier_config: &VerifierConfig, + module: &'a CompiledScript, + ) -> VMResult<()> { + Self::verify_script_impl(verifier_config, module).map_err(|e| e.finish(Location::Script)) } - fn verify_script_impl(script: &'a CompiledScript) -> PartialVMResult<()> { + fn verify_script_impl( + verifier_config: &VerifierConfig, + script: &'a CompiledScript, + ) -> PartialVMResult<()> { // create `FunctionView` and `BinaryIndexedView` - control_flow::verify(None, &script.code)?; + control_flow::verify(verifier_config, None, &script.code)?; let function_view = FunctionView::script(script); let resolver = BinaryIndexedView::Script(script); //verify @@ -60,6 +73,7 @@ impl<'a> CodeUnitVerifier<'a> { } fn verify_function( + verifier_config: &VerifierConfig, index: FunctionDefinitionIndex, function_definition: &'a FunctionDefinition, module: &'a CompiledModule, @@ -71,7 +85,7 @@ impl<'a> CodeUnitVerifier<'a> { }; // create `FunctionView` and `BinaryIndexedView` let function_handle = module.function_handle_at(function_definition.function); - control_flow::verify(Some(index), code)?; + control_flow::verify(verifier_config, Some(index), code)?; let function_view = FunctionView::function(module, index, code, function_handle); let resolver = BinaryIndexedView::Module(module); let mut name_def_map = HashMap::new(); diff --git a/language/move-bytecode-verifier/src/control_flow.rs b/language/move-bytecode-verifier/src/control_flow.rs index c2ed834801..784b88e6a4 100644 --- a/language/move-bytecode-verifier/src/control_flow.rs +++ b/language/move-bytecode-verifier/src/control_flow.rs @@ -7,6 +7,7 @@ //! - All forward jumps do not enter into the middle of a loop //! - All "breaks" (forward, loop-exiting jumps) go to the "end" of the loop //! - All "continues" (back jumps in a loop) are only to the current loop +use crate::verifier::VerifierConfig; use move_binary_format::{ errors::{PartialVMError, PartialVMResult}, file_format::{Bytecode, CodeOffset, CodeUnit, FunctionDefinitionIndex}, @@ -15,6 +16,7 @@ use move_core_types::vm_status::StatusCode; use std::{collections::HashSet, convert::TryInto}; pub fn verify( + verifier_config: &VerifierConfig, current_function_opt: Option, code: &CodeUnit, ) -> PartialVMResult<()> { @@ -36,7 +38,7 @@ pub fn verify( code: &code.code, }; let labels = instruction_labels(context); - check_jumps(context, labels) + check_jumps(verifier_config, context, labels) } #[derive(Clone, Copy)] @@ -95,13 +97,23 @@ fn instruction_labels(context: &ControlFlowVerifier) -> Vec
-
include UnsetAbortsIf;
-ensures bitvector.bit_field[bit_index];
-
+ +## Function `unset` +Unset the bit at bit_index in the bitvector regardless of its previous state. - +
public fun unset(bitvector: &mut bit_vector::BitVector, bit_index: u64)
+
-
schema UnsetAbortsIf {
-    bitvector: BitVector;
-    bit_index: u64;
-    aborts_if bit_index >= length(bitvector) with EINDEX;
+
+
+Implementation + + +
public fun unset(bitvector: &mut BitVector, bit_index: u64) {
+    assert!(bit_index < vector::length(&bitvector.bit_field), EINDEX);
+    let x = vector::borrow_mut(&mut bitvector.bit_field, bit_index);
+    *x = false;
 }
 
@@ -241,26 +248,25 @@ Set the bit at bit_index in the bitvector regardless o
- +
+Specification -## Function `unset` -Unset the bit at bit_index in the bitvector regardless of its previous state. - -
public fun unset(bitvector: &mut bit_vector::BitVector, bit_index: u64)
+
include UnsetAbortsIf;
+ensures !bitvector.bit_field[bit_index];
 
-
-Implementation + -
public fun unset(bitvector: &mut BitVector, bit_index: u64) {
-    assert!(bit_index < vector::length(&bitvector.bit_field), EINDEX);
-    let x = vector::borrow_mut(&mut bitvector.bit_field, bit_index);
-    *x = false;
+
+
schema UnsetAbortsIf {
+    bitvector: BitVector;
+    bit_index: u64;
+    aborts_if bit_index >= length(bitvector) with EINDEX;
 }
 
diff --git a/language/move-stdlib/docs/fixed_point32.md b/language/move-stdlib/docs/fixed_point32.md index 27d8c7b3a3..4222ed5cef 100644 --- a/language/move-stdlib/docs/fixed_point32.md +++ b/language/move-stdlib/docs/fixed_point32.md @@ -15,6 +15,12 @@ a 32-bit fractional part. - [Function `create_from_raw_value`](#0x1_fixed_point32_create_from_raw_value) - [Function `get_raw_value`](#0x1_fixed_point32_get_raw_value) - [Function `is_zero`](#0x1_fixed_point32_is_zero) +- [Function `min`](#0x1_fixed_point32_min) +- [Function `max`](#0x1_fixed_point32_max) +- [Function `create_from_u64`](#0x1_fixed_point32_create_from_u64) +- [Function `floor`](#0x1_fixed_point32_floor) +- [Function `ceil`](#0x1_fixed_point32_ceil) +- [Function `round`](#0x1_fixed_point32_round) - [Module Specification](#@Module_Specification_1) @@ -451,6 +457,366 @@ Returns true if the ratio is zero. +
+ + + +## Function `min` + +Returns the smaller of the two FixedPoint32 numbers. + + +
public fun min(num1: fixed_point32::FixedPoint32, num2: fixed_point32::FixedPoint32): fixed_point32::FixedPoint32
+
+ + + +
+Implementation + + +
public fun min(num1: FixedPoint32, num2: FixedPoint32): FixedPoint32 {
+    if (num1.value < num2.value) {
+        num1
+    } else {
+        num2
+    }
+}
+
+ + + +
+ +
+Specification + + + +
pragma opaque;
+aborts_if false;
+ensures result == spec_min(num1, num2);
+
+ + + + + + + +
fun spec_min(num1: FixedPoint32, num2: FixedPoint32): FixedPoint32 {
+   if (num1.value < num2.value) {
+       num1
+   } else {
+       num2
+   }
+}
+
+ + + +
+ + + +## Function `max` + +Returns the larger of the two FixedPoint32 numbers. + + +
public fun max(num1: fixed_point32::FixedPoint32, num2: fixed_point32::FixedPoint32): fixed_point32::FixedPoint32
+
+ + + +
+Implementation + + +
public fun max(num1: FixedPoint32, num2: FixedPoint32): FixedPoint32 {
+    if (num1.value > num2.value) {
+        num1
+    } else {
+        num2
+    }
+}
+
+ + + +
+ +
+Specification + + + +
pragma opaque;
+aborts_if false;
+ensures result == spec_max(num1, num2);
+
+ + + + + + + +
fun spec_max(num1: FixedPoint32, num2: FixedPoint32): FixedPoint32 {
+   if (num1.value > num2.value) {
+       num1
+   } else {
+       num2
+   }
+}
+
+ + + +
+ + + +## Function `create_from_u64` + +Create a fixedpoint value from a u64 value. + + +
public fun create_from_u64(val: u64): fixed_point32::FixedPoint32
+
+ + + +
+Implementation + + +
public fun create_from_u64(val: u64): FixedPoint32 {
+    let value = (val as u128) << 32;
+    assert!(value <= MAX_U64, ERATIO_OUT_OF_RANGE);
+    FixedPoint32{value: (value as u64)}
+}
+
+ + + +
+ +
+Specification + + + +
pragma opaque;
+include CreateFromU64AbortsIf;
+ensures result == spec_create_from_u64(val);
+
+ + + + + + + +
schema CreateFromU64AbortsIf {
+    val: num;
+    let scaled_value = val << 32;
+    aborts_if scaled_value > MAX_U64;
+}
+
+ + + + + + + +
fun spec_create_from_u64(val: num): FixedPoint32 {
+   FixedPoint32 {value: val << 32}
+}
+
+ + + +
+ + + +## Function `floor` + +Returns the largest integer less than or equal to a given number. + + +
public fun floor(num: fixed_point32::FixedPoint32): u64
+
+ + + +
+Implementation + + +
public fun floor(num: FixedPoint32): u64 {
+    num.value >> 32
+}
+
+ + + +
+ +
+Specification + + + +
pragma opaque;
+aborts_if false;
+ensures result == spec_floor(num);
+
+ + + + + + + +
fun spec_floor(val: FixedPoint32): u64 {
+   let fractional = val.value % (1 << 32);
+   if (fractional == 0) {
+       val.value >> 32
+   } else {
+       (val.value - fractional) >> 32
+   }
+}
+
+ + + +
+ + + +## Function `ceil` + +Rounds up the given FixedPoint32 to the next largest integer. + + +
public fun ceil(num: fixed_point32::FixedPoint32): u64
+
+ + + +
+Implementation + + +
public fun ceil(num: FixedPoint32): u64 {
+    let floored_num = floor(num) << 32;
+    if (num.value == floored_num) {
+        return floored_num >> 32
+    };
+    let val = ((floored_num as u128) + (1 << 32));
+    (val >> 32 as u64)
+}
+
+ + + +
+ +
+Specification + + + +
pragma opaque;
+aborts_if false;
+ensures result == spec_ceil(num);
+
+ + + + + + + +
fun spec_ceil(val: FixedPoint32): u64 {
+   let fractional = val.value % (1 << 32);
+   let one = 1 << 32;
+   if (fractional == 0) {
+       val.value >> 32
+   } else {
+       (val.value - fractional + one) >> 32
+   }
+}
+
+ + + +
+ + + +## Function `round` + +Returns the value of a FixedPoint32 to the nearest integer. + + +
public fun round(num: fixed_point32::FixedPoint32): u64
+
+ + + +
+Implementation + + +
public fun round(num: FixedPoint32): u64 {
+    let floored_num = floor(num) << 32;
+    let boundary = floored_num + ((1 << 32) / 2);
+    if (num.value < boundary) {
+        floored_num >> 32
+    } else {
+        ceil(num)
+    }
+}
+
+ + + +
+ +
+Specification + + + +
pragma opaque;
+aborts_if false;
+ensures result == spec_round(num);
+
+ + + + + + + +
fun spec_round(val: FixedPoint32): u64 {
+   let fractional = val.value % (1 << 32);
+   let boundary = (1 << 32) / 2;
+   let one = 1 << 32;
+   if (fractional < boundary) {
+       (val.value - fractional) >> 32
+   } else {
+       (val.value - fractional + one) >> 32
+   }
+}
+
+ + +
diff --git a/language/move-stdlib/docs/overview.md b/language/move-stdlib/docs/overview.md index 4d0b9bd6e8..e8aff3f041 100644 --- a/language/move-stdlib/docs/overview.md +++ b/language/move-stdlib/docs/overview.md @@ -21,6 +21,7 @@ This is the root document for the Move stdlib module documentation. The Move std - [`0x1::option`](option.md#0x1_option) - [`0x1::signer`](signer.md#0x1_signer) - [`0x1::string`](string.md#0x1_string) +- [`0x1::type_name`](type_name.md#0x1_type_name) - [`0x1::vector`](vector.md#0x1_vector) diff --git a/language/move-stdlib/docs/type_name.md b/language/move-stdlib/docs/type_name.md new file mode 100644 index 0000000000..35fd1f9003 --- /dev/null +++ b/language/move-stdlib/docs/type_name.md @@ -0,0 +1,127 @@ + + + +# Module `0x1::type_name` + +Functionality for converting Move types into values. Use with care! + + +- [Struct `TypeName`](#0x1_type_name_TypeName) +- [Function `get`](#0x1_type_name_get) +- [Function `borrow_string`](#0x1_type_name_borrow_string) +- [Function `into_string`](#0x1_type_name_into_string) + + +
use 0x1::ascii;
+
+ + + + + +## Struct `TypeName` + + + +
struct TypeName has copy, drop, store
+
+ + + +
+Fields + + +
+
+name: ascii::String +
+
+ String representation of the type. All types are represented + using their source syntax: + "u8", "u64", "u128", "bool", "address", "vector", "signer" for ground types. + Struct types are represented as fully qualified type names; e.g. + 00000000000000000000000000000001::string::String or + 0000000000000000000000000000000a::module_name1::type_name1<0000000000000000000000000000000a::module_name2::type_name2<u64>> + Addresses are hex-encoded lowercase values of length ADDRESS_LENGTH (16, 20, or 32 depending on the Move platform) +
+
+ + +
+ + + +## Function `get` + +Return a value representation of the type T. + + +
public fun get<T>(): type_name::TypeName
+
+ + + +
+Implementation + + +
public native fun get<T>(): TypeName;
+
+ + + +
+ + + +## Function `borrow_string` + +Get the String representation of self + + +
public fun borrow_string(self: &type_name::TypeName): &ascii::String
+
+ + + +
+Implementation + + +
public fun borrow_string(self: &TypeName): &String {
+    &self.name
+}
+
+ + + +
+ + + +## Function `into_string` + +Convert self into its inner String + + +
public fun into_string(self: type_name::TypeName): ascii::String
+
+ + + +
+Implementation + + +
public fun into_string(self: TypeName): String {
+    self.name
+}
+
+ + + +
+ + +[//]: # ("File containing references which can be used from documentation") diff --git a/language/move-stdlib/docs/vector.md b/language/move-stdlib/docs/vector.md index f63575adb8..5ae076fb22 100644 --- a/language/move-stdlib/docs/vector.md +++ b/language/move-stdlib/docs/vector.md @@ -411,6 +411,7 @@ Return true if the vector v has no elements and ## Function `contains` Return true if e is in the vector v. +Otherwise, returns false.
public fun contains<Element>(v: &vector<Element>, e: &Element): bool
@@ -542,7 +543,7 @@ Aborts if i is out of bounds.
 
 ## Function `swap_remove`
 
-Swap the ith element of the vector v with the last element and then pop the vector.
+Swap the ith element of the vector v with the last element and then pop the element.
 This is O(1), but does not preserve ordering of elements in the vector.
 Aborts if i is out of bounds.
 
diff --git a/language/move-stdlib/sources/bit_vector.move b/language/move-stdlib/sources/bit_vector.move
index 51f7c2fdf6..e89795422c 100644
--- a/language/move-stdlib/sources/bit_vector.move
+++ b/language/move-stdlib/sources/bit_vector.move
@@ -71,9 +71,9 @@ module std::bit_vector {
         let x = vector::borrow_mut(&mut bitvector.bit_field, bit_index);
         *x = false;
     }
-    spec set {
+    spec unset {
         include UnsetAbortsIf;
-        ensures bitvector.bit_field[bit_index];
+        ensures !bitvector.bit_field[bit_index];
     }
     spec schema UnsetAbortsIf {
         bitvector: BitVector;
diff --git a/language/move-stdlib/sources/fixed_point32.move b/language/move-stdlib/sources/fixed_point32.move
index 3a12d246b8..c7e9b70643 100644
--- a/language/move-stdlib/sources/fixed_point32.move
+++ b/language/move-stdlib/sources/fixed_point32.move
@@ -154,6 +154,136 @@ module std::fixed_point32 {
         num.value == 0
     }
 
+    /// Returns the smaller of the two FixedPoint32 numbers.
+    public fun min(num1: FixedPoint32, num2: FixedPoint32): FixedPoint32 {
+        if (num1.value < num2.value) {
+            num1
+        } else {
+            num2
+        }
+    }
+    spec min {
+        pragma opaque;
+        aborts_if false;
+        ensures result == spec_min(num1, num2);
+    }
+    spec fun spec_min(num1: FixedPoint32, num2: FixedPoint32): FixedPoint32 {
+        if (num1.value < num2.value) {
+            num1
+        } else {
+            num2
+        }
+    }
+
+    /// Returns the larger of the two FixedPoint32 numbers.
+    public fun max(num1: FixedPoint32, num2: FixedPoint32): FixedPoint32 {
+        if (num1.value > num2.value) {
+            num1
+        } else {
+            num2
+        }
+    }
+    spec max {
+        pragma opaque;
+        aborts_if false;
+        ensures result == spec_max(num1, num2);
+    }
+    spec fun spec_max(num1: FixedPoint32, num2: FixedPoint32): FixedPoint32 {
+        if (num1.value > num2.value) {
+            num1
+        } else {
+            num2
+        }
+    }
+
+    /// Create a fixedpoint value from a u64 value.
+    public fun create_from_u64(val: u64): FixedPoint32 {
+        let value = (val as u128) << 32;
+        assert!(value <= MAX_U64, ERATIO_OUT_OF_RANGE);
+        FixedPoint32{value: (value as u64)}
+    }
+    spec create_from_u64 {
+        pragma opaque;
+        include CreateFromU64AbortsIf;
+        ensures result == spec_create_from_u64(val);
+    }
+    spec schema CreateFromU64AbortsIf {
+        val: num;
+        let scaled_value = val << 32;
+        aborts_if scaled_value > MAX_U64;
+    }
+    spec fun spec_create_from_u64(val: num): FixedPoint32 {
+        FixedPoint32 {value: val << 32}
+    }
+
+    /// Returns the largest integer less than or equal to a given number.
+    public fun floor(num: FixedPoint32): u64 {
+        num.value >> 32
+    }
+    spec floor {
+        pragma opaque;
+        aborts_if false;
+        ensures result == spec_floor(num);
+    }
+    spec fun spec_floor(val: FixedPoint32): u64 {
+        let fractional = val.value % (1 << 32);
+        if (fractional == 0) {
+            val.value >> 32
+        } else {
+            (val.value - fractional) >> 32
+        }
+    }
+
+    /// Rounds up the given FixedPoint32 to the next largest integer.
+    public fun ceil(num: FixedPoint32): u64 {
+        let floored_num = floor(num) << 32;
+        if (num.value == floored_num) {
+            return floored_num >> 32
+        };
+        let val = ((floored_num as u128) + (1 << 32));
+        (val >> 32 as u64)
+    }
+    spec ceil {
+        pragma opaque;
+        aborts_if false;
+        ensures result == spec_ceil(num);
+    }
+    spec fun spec_ceil(val: FixedPoint32): u64 {
+        let fractional = val.value % (1 << 32);
+        let one = 1 << 32;
+        if (fractional == 0) {
+            val.value >> 32
+        } else {
+            (val.value - fractional + one) >> 32
+        }
+    }
+
+    /// Returns the value of a FixedPoint32 to the nearest integer.
+    public fun round(num: FixedPoint32): u64 {
+        let floored_num = floor(num) << 32;
+        let boundary = floored_num + ((1 << 32) / 2);
+        if (num.value < boundary) {
+            floored_num >> 32
+        } else {
+            ceil(num)
+        }
+    }
+    spec round {
+        pragma opaque;
+        aborts_if false;
+        ensures result == spec_round(num);
+    }
+    spec fun spec_round(val: FixedPoint32): u64 {
+        let fractional = val.value % (1 << 32);
+        let boundary = (1 << 32) / 2;
+        let one = 1 << 32;
+        if (fractional < boundary) {
+            (val.value - fractional) >> 32
+        } else {
+            (val.value - fractional + one) >> 32
+        }
+    }
+
     // **************** SPECIFICATIONS ****************
 
     spec module {} // switch documentation context to module level
diff --git a/language/move-stdlib/sources/type_name.move b/language/move-stdlib/sources/type_name.move
new file mode 100644
index 0000000000..e1d973b03b
--- /dev/null
+++ b/language/move-stdlib/sources/type_name.move
@@ -0,0 +1,28 @@
+/// Functionality for converting Move types into values. Use with care!
+module std::type_name {
+    use std::ascii::String;
+
+    struct TypeName has copy, drop, store {
+        /// String representation of the type. All types are represented
+        /// using their source syntax:
+        /// "u8", "u64", "u128", "bool", "address", "vector", "signer" for ground types.
+        /// Struct types are represented as fully qualified type names; e.g.
+        /// `00000000000000000000000000000001::string::String` or
+        /// `0000000000000000000000000000000a::module_name1::type_name1<0000000000000000000000000000000a::module_name2::type_name2>`
+        /// Addresses are hex-encoded lowercase values of length ADDRESS_LENGTH (16, 20, or 32 depending on the Move platform)
+        name: String
+    }
+
+    /// Return a value representation of the type `T`.
+    public native fun get(): TypeName;
+
+    /// Get the String representation of `self`
+    public fun borrow_string(self: &TypeName): &String {
+        &self.name
+    }
+
+    /// Convert `self` into its inner String
+    public fun into_string(self: TypeName): String {
+        self.name
+    }
+}
diff --git a/language/move-stdlib/sources/vector.move b/language/move-stdlib/sources/vector.move
index b027dbeb79..28989dd1bf 100644
--- a/language/move-stdlib/sources/vector.move
+++ b/language/move-stdlib/sources/vector.move
@@ -101,6 +101,7 @@ module std::vector {
     }
 
     /// Return true if `e` is in the vector `v`.
+    /// Otherwise, returns false.
     public fun contains(v: &vector, e: &Element): bool {
         let i = 0;
         let len = length(v);
@@ -145,7 +146,7 @@ module std::vector {
         pragma intrinsic = true;
     }
 
-    /// Swap the `i`th element of the vector `v` with the last element and then pop the vector.
+    /// Swap the `i`th element of the vector `v` with the last element and then pop the element.
     /// This is O(1), but does not preserve ordering of elements in the vector.
     /// Aborts if `i` is out of bounds.
     public fun swap_remove(v: &mut vector, i: u64): Element {
diff --git a/language/move-stdlib/src/natives/bcs.rs b/language/move-stdlib/src/natives/bcs.rs
index a0d075f8f1..d6094d6891 100644
--- a/language/move-stdlib/src/natives/bcs.rs
+++ b/language/move-stdlib/src/natives/bcs.rs
@@ -2,21 +2,43 @@
 // Copyright (c) The Move Contributors
 // SPDX-License-Identifier: Apache-2.0
 
+use crate::natives::helpers::make_module_natives;
 use move_binary_format::errors::PartialVMResult;
-use move_core_types::vm_status::sub_status::NFE_BCS_SERIALIZATION_FAILURE;
-use move_vm_runtime::native_functions::NativeContext;
+use move_core_types::{
+    gas_algebra::{InternalGas, InternalGasPerByte, NumBytes},
+    vm_status::sub_status::NFE_BCS_SERIALIZATION_FAILURE,
+};
+use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
 use move_vm_types::{
-    gas_schedule::NativeCostIndex,
     loaded_data::runtime_types::Type,
-    natives::function::{native_gas, NativeResult},
+    natives::function::NativeResult,
     pop_arg,
     values::{values_impl::Reference, Value},
 };
 use smallvec::smallvec;
-use std::collections::VecDeque;
+use std::{collections::VecDeque, sync::Arc};
+/***************************************************************************************************
+ * native fun to_bytes
+ *
+ *   gas cost: size_of(val_type) * input_unit_cost +        | get type layout
+ *             size_of(val) * input_unit_cost +             | serialize value
+ *             max(size_of(output), 1) * output_unit_cost
+ *
+ *             If any of the first two steps fails, a partial cost + an additional failure_cost
+ *             will be charged.
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct ToBytesGasParameters {
+    pub per_byte_serialized: InternalGasPerByte,
+    pub legacy_min_output_size: NumBytes,
+    pub failure: InternalGas,
+}
 
 /// Rust implementation of Move's `native public fun to_bytes(&T): vector`
-pub fn native_to_bytes(
+#[inline]
+fn native_to_bytes(
+    gas_params: &ToBytesGasParameters,
     context: &mut NativeContext,
     mut ty_args: Vec,
     mut args: VecDeque,
@@ -24,31 +46,59 @@ pub fn native_to_bytes(
     debug_assert!(ty_args.len() == 1);
     debug_assert!(args.len() == 1);
 
-    let ref_to_val = pop_arg!(args, Reference);
+    let mut cost = 0.into();
 
+    // pop type and value
+    let ref_to_val = pop_arg!(args, Reference);
     let arg_type = ty_args.pop().unwrap();
-    // delegate to the BCS serialization for `Value`
-    let serialized_value_opt = match context.type_to_type_layout(&arg_type)? {
-        None => None,
-        Some(layout) => ref_to_val.read_ref()?.simple_serialize(&layout),
-    };
-    let serialized_value = match serialized_value_opt {
+
+    // get type layout
+    let layout = match context.type_to_type_layout(&arg_type)? {
+        Some(layout) => layout,
         None => {
-            let cost = native_gas(context.cost_table(), NativeCostIndex::BCS_TO_BYTES, 1);
+            cost += gas_params.failure;
             return Ok(NativeResult::err(cost, NFE_BCS_SERIALIZATION_FAILURE));
         }
+    };
+    // serialize value
+    let val = ref_to_val.read_ref()?;
+    let serialized_value = match val.simple_serialize(&layout) {
         Some(serialized_value) => serialized_value,
+        None => {
+            cost += gas_params.failure;
+            return Ok(NativeResult::err(cost, NFE_BCS_SERIALIZATION_FAILURE));
+        }
     };
-
-    // cost is proportional to the size of the serialized value
-    let cost = native_gas(
-        context.cost_table(),
-        NativeCostIndex::BCS_TO_BYTES,
-        serialized_value.len(),
-    );
+    cost += gas_params.per_byte_serialized
+        * std::cmp::max(
+            NumBytes::new(serialized_value.len() as u64),
+            gas_params.legacy_min_output_size,
+        );
 
     Ok(NativeResult::ok(
         cost,
         smallvec![Value::vector_u8(serialized_value)],
     ))
 }
+
+pub fn make_native_to_bytes(gas_params: ToBytesGasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_to_bytes(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * module
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct GasParameters {
+    pub to_bytes: ToBytesGasParameters,
+}
+
+pub fn make_all(gas_params: GasParameters) -> impl Iterator {
+    let natives = [("to_bytes", make_native_to_bytes(gas_params.to_bytes))];
+
+    make_module_natives(natives)
+}
diff --git a/language/move-stdlib/src/natives/debug.rs b/language/move-stdlib/src/natives/debug.rs
index b2c4701854..8da0b396a7 100644
--- a/language/move-stdlib/src/natives/debug.rs
+++ b/language/move-stdlib/src/natives/debug.rs
@@ -2,9 +2,10 @@
 // Copyright (c) The Move Contributors
 // SPDX-License-Identifier: Apache-2.0
 
+use crate::natives::helpers::make_module_natives;
 use move_binary_format::errors::PartialVMResult;
-use move_core_types::gas_schedule::ONE_GAS_UNIT;
-use move_vm_runtime::native_functions::NativeContext;
+use move_core_types::gas_algebra::InternalGas;
+use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
 #[allow(unused_imports)]
 use move_vm_types::values::{values_impl::debug::print_reference, Reference};
 #[allow(unused_imports)]
@@ -12,12 +13,22 @@ use move_vm_types::{
     loaded_data::runtime_types::Type, natives::function::NativeResult, pop_arg, values::Value,
 };
 use smallvec::smallvec;
-use std::collections::VecDeque;
+use std::{collections::VecDeque, sync::Arc};
 
-#[allow(unused_mut)]
-#[allow(unused_variables)]
-pub fn native_print(
-    context: &mut NativeContext,
+/***************************************************************************************************
+ * native fun print
+ *
+ *   gas cost: base_cost
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct PrintGasParameters {
+    pub base_cost: InternalGas,
+}
+
+#[inline]
+fn native_print(
+    gas_params: &PrintGasParameters,
+    _context: &mut NativeContext,
     mut ty_args: Vec,
     mut args: VecDeque,
 ) -> PartialVMResult {
@@ -27,7 +38,7 @@ pub fn native_print(
     // No-op if the feature flag is not present.
     #[cfg(feature = "testing")]
     {
-        let ty = ty_args.pop().unwrap();
+        let _ty = ty_args.pop().unwrap();
         let r = pop_arg!(args, Reference);
 
         let mut buf = String::new();
@@ -35,11 +46,30 @@ pub fn native_print(
         println!("[debug] {}", buf);
     }
 
-    Ok(NativeResult::ok(ONE_GAS_UNIT, smallvec![]))
+    Ok(NativeResult::ok(gas_params.base_cost, smallvec![]))
+}
+
+pub fn make_native_print(gas_params: PrintGasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_print(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * native fun print_stack_trace
+ *
+ *   gas cost: base_cost
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct PrintStackTraceGasParameters {
+    pub base_cost: InternalGas,
 }
 
-#[allow(unused_variables)]
-pub fn native_print_stack_trace(
+#[inline]
+fn native_print_stack_trace(
+    gas_params: &PrintStackTraceGasParameters,
     context: &mut NativeContext,
     ty_args: Vec,
     args: VecDeque,
@@ -54,5 +84,34 @@ pub fn native_print_stack_trace(
         println!("{}", s);
     }
 
-    Ok(NativeResult::ok(ONE_GAS_UNIT, smallvec![]))
+    Ok(NativeResult::ok(gas_params.base_cost, smallvec![]))
+}
+
+pub fn make_native_print_stack_trace(gas_params: PrintStackTraceGasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_print_stack_trace(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * module
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct GasParameters {
+    pub print: PrintGasParameters,
+    pub print_stack_trace: PrintStackTraceGasParameters,
+}
+
+pub fn make_all(gas_params: GasParameters) -> impl Iterator {
+    let natives = [
+        ("print", make_native_print(gas_params.print)),
+        (
+            "print_stack_trace",
+            make_native_print_stack_trace(gas_params.print_stack_trace),
+        ),
+    ];
+
+    make_module_natives(natives)
 }
diff --git a/language/move-stdlib/src/natives/event.rs b/language/move-stdlib/src/natives/event.rs
index 802564c19e..1301574bbd 100644
--- a/language/move-stdlib/src/natives/event.rs
+++ b/language/move-stdlib/src/natives/event.rs
@@ -2,20 +2,31 @@
 // Copyright (c) The Move Contributors
 // SPDX-License-Identifier: Apache-2.0
 
+use crate::natives::helpers::make_module_natives;
 use move_binary_format::errors::PartialVMResult;
-use move_core_types::gas_schedule::GasAlgebra;
-use move_vm_runtime::native_functions::NativeContext;
+use move_core_types::gas_algebra::InternalGasPerAbstractMemoryUnit;
+use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
 use move_vm_types::{
-    gas_schedule::NativeCostIndex,
-    loaded_data::runtime_types::Type,
-    natives::function::{native_gas, NativeResult},
-    pop_arg,
-    values::Value,
+    loaded_data::runtime_types::Type, natives::function::NativeResult, pop_arg, values::Value,
+    views::ValueView,
 };
 use smallvec::smallvec;
-use std::collections::VecDeque;
+use std::{collections::VecDeque, sync::Arc};
 
-pub fn write_to_event_store(
+/***************************************************************************************************
+ * [NURSERY-ONLY] native fun write_to_event_store
+ *
+ *   gas cost: base_cost
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct WriteToEventStoreGasParameters {
+    pub unit_cost: InternalGasPerAbstractMemoryUnit,
+}
+
+#[inline]
+fn native_write_to_event_store(
+    gas_params: &WriteToEventStoreGasParameters,
     context: &mut NativeContext,
     mut ty_args: Vec,
     mut arguments: VecDeque,
@@ -28,11 +39,7 @@ pub fn write_to_event_store(
     let seq_num = pop_arg!(arguments, u64);
     let guid = pop_arg!(arguments, Vec);
 
-    let cost = native_gas(
-        context.cost_table(),
-        NativeCostIndex::EMIT_EVENT,
-        msg.size().get() as usize,
-    );
+    let cost = gas_params.unit_cost * std::cmp::max(msg.legacy_abstract_memory_size(), 1.into());
 
     if !context.save_event(guid, seq_num, ty, msg)? {
         return Ok(NativeResult::err(cost, 0));
@@ -40,3 +47,30 @@ pub fn write_to_event_store(
 
     Ok(NativeResult::ok(cost, smallvec![]))
 }
+
+pub fn make_native_write_to_event_store(
+    gas_params: WriteToEventStoreGasParameters,
+) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_write_to_event_store(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * module
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct GasParameters {
+    pub write_to_event_store: WriteToEventStoreGasParameters,
+}
+
+pub fn make_all(gas_params: GasParameters) -> impl Iterator {
+    let natives = [(
+        "write_to_event_store",
+        make_native_write_to_event_store(gas_params.write_to_event_store),
+    )];
+
+    make_module_natives(natives)
+}
diff --git a/language/move-stdlib/src/natives/hash.rs b/language/move-stdlib/src/natives/hash.rs
index 62e9b8cc4d..e0130aad48 100644
--- a/language/move-stdlib/src/natives/hash.rs
+++ b/language/move-stdlib/src/natives/hash.rs
@@ -2,22 +2,35 @@
 // Copyright (c) The Move Contributors
 // SPDX-License-Identifier: Apache-2.0
 
+use crate::natives::helpers::make_module_natives;
 use move_binary_format::errors::PartialVMResult;
-use move_vm_runtime::native_functions::NativeContext;
+use move_core_types::gas_algebra::{InternalGas, InternalGasPerByte, NumBytes};
+use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
 use move_vm_types::{
-    gas_schedule::NativeCostIndex,
-    loaded_data::runtime_types::Type,
-    natives::function::{native_gas, NativeResult},
-    pop_arg,
-    values::Value,
+    loaded_data::runtime_types::Type, natives::function::NativeResult, pop_arg, values::Value,
 };
 use sha2::{Digest, Sha256};
 use sha3::Sha3_256;
 use smallvec::smallvec;
-use std::collections::VecDeque;
+use std::{collections::VecDeque, sync::Arc};
 
-pub fn native_sha2_256(
-    context: &mut NativeContext,
+/***************************************************************************************************
+ * native fun sha2_256
+ *
+ *   gas cost: base_cost + unit_cost * max(input_length_in_bytes, legacy_min_input_len)
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct Sha2_256GasParameters {
+    pub base: InternalGas,
+    pub per_byte: InternalGasPerByte,
+    pub legacy_min_input_len: NumBytes,
+}
+
+#[inline]
+fn native_sha2_256(
+    gas_params: &Sha2_256GasParameters,
+    _context: &mut NativeContext,
     _ty_args: Vec,
     mut arguments: VecDeque,
 ) -> PartialVMResult {
@@ -26,11 +39,12 @@ pub fn native_sha2_256(
 
     let hash_arg = pop_arg!(arguments, Vec);
 
-    let cost = native_gas(
-        context.cost_table(),
-        NativeCostIndex::SHA2_256,
-        hash_arg.len(),
-    );
+    let cost = gas_params.base
+        + gas_params.per_byte
+            * std::cmp::max(
+                NumBytes::new(hash_arg.len() as u64),
+                gas_params.legacy_min_input_len,
+            );
 
     let hash_vec = Sha256::digest(hash_arg.as_slice()).to_vec();
     Ok(NativeResult::ok(
@@ -39,8 +53,31 @@ pub fn native_sha2_256(
     ))
 }
 
-pub fn native_sha3_256(
-    context: &mut NativeContext,
+pub fn make_native_sha2_256(gas_params: Sha2_256GasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_sha2_256(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * native fun sha3_256
+ *
+ *   gas cost: base_cost + unit_cost * max(input_length_in_bytes, legacy_min_input_len)
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct Sha3_256GasParameters {
+    pub base: InternalGas,
+    pub per_byte: InternalGasPerByte,
+    pub legacy_min_input_len: NumBytes,
+}
+
+#[inline]
+fn native_sha3_256(
+    gas_params: &Sha3_256GasParameters,
+    _context: &mut NativeContext,
     _ty_args: Vec,
     mut arguments: VecDeque,
 ) -> PartialVMResult {
@@ -49,11 +86,12 @@ pub fn native_sha3_256(
 
     let hash_arg = pop_arg!(arguments, Vec);
 
-    let cost = native_gas(
-        context.cost_table(),
-        NativeCostIndex::SHA3_256,
-        hash_arg.len(),
-    );
+    let cost = gas_params.base
+        + gas_params.per_byte
+            * std::cmp::max(
+                NumBytes::new(hash_arg.len() as u64),
+                gas_params.legacy_min_input_len,
+            );
 
     let hash_vec = Sha3_256::digest(hash_arg.as_slice()).to_vec();
     Ok(NativeResult::ok(
@@ -61,3 +99,29 @@ pub fn native_sha3_256(
         smallvec![Value::vector_u8(hash_vec)],
     ))
 }
+
+pub fn make_native_sha3_256(gas_params: Sha3_256GasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_sha3_256(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * module
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct GasParameters {
+    pub sha2_256: Sha2_256GasParameters,
+    pub sha3_256: Sha3_256GasParameters,
+}
+
+pub fn make_all(gas_params: GasParameters) -> impl Iterator {
+    let natives = [
+        ("sha2_256", make_native_sha2_256(gas_params.sha2_256)),
+        ("sha3_256", make_native_sha3_256(gas_params.sha3_256)),
+    ];
+
+    make_module_natives(natives)
+}
diff --git a/language/move-stdlib/src/natives/helpers.rs b/language/move-stdlib/src/natives/helpers.rs
new file mode 100644
index 0000000000..1a8b8a88cf
--- /dev/null
+++ b/language/move-stdlib/src/natives/helpers.rs
@@ -0,0 +1,12 @@
+// Copyright (c) The Move Contributors
+// SPDX-License-Identifier: Apache-2.0
+
+use move_vm_runtime::native_functions::NativeFunction;
+
+pub fn make_module_natives(
+    natives: impl IntoIterator, NativeFunction)>,
+) -> impl Iterator {
+    natives
+        .into_iter()
+        .map(|(func_name, func)| (func_name.into(), func))
+}
diff --git a/language/move-stdlib/src/natives/mod.rs b/language/move-stdlib/src/natives/mod.rs
index e9272c6abf..33aa9a9018 100644
--- a/language/move-stdlib/src/natives/mod.rs
+++ b/language/move-stdlib/src/natives/mod.rs
@@ -3,94 +3,175 @@
 // SPDX-License-Identifier: Apache-2.0
 
 pub mod bcs;
+pub mod debug;
 pub mod event;
 pub mod hash;
 pub mod signer;
 pub mod string;
-pub mod vector;
-
+pub mod type_name;
 #[cfg(feature = "testing")]
 pub mod unit_test;
+pub mod vector;
 
-#[cfg(feature = "testing")]
-pub mod debug;
+mod helpers;
 
 use move_core_types::account_address::AccountAddress;
-use move_vm_runtime::{
-    native_functions,
-    native_functions::{NativeFunction, NativeFunctionTable},
-};
-
-pub fn all_natives(move_std_addr: AccountAddress) -> NativeFunctionTable {
-    const NATIVES: &[(&str, &str, NativeFunction)] = &[
-        ("bcs", "to_bytes", bcs::native_to_bytes),
-        ("event", "write_to_event_store", event::write_to_event_store),
-        ("hash", "sha2_256", hash::native_sha2_256),
-        ("hash", "sha3_256", hash::native_sha3_256),
-        ("signer", "borrow_address", signer::native_borrow_address),
-        ("string", "internal_check_utf8", string::native_check_utf8),
-        (
-            "string",
-            "internal_is_char_boundary",
-            string::native_is_char_boundary,
-        ),
-        ("string", "internal_sub_string", string::native_sub_string),
-        ("string", "internal_index_of", string::native_index_of),
-        ("vector", "length", vector::native_length),
-        ("vector", "empty", vector::native_empty),
-        ("vector", "borrow", vector::native_borrow),
-        ("vector", "borrow_mut", vector::native_borrow),
-        ("vector", "push_back", vector::native_push_back),
-        ("vector", "pop_back", vector::native_pop),
-        ("vector", "destroy_empty", vector::native_destroy_empty),
-        ("vector", "swap", vector::native_swap),
-        #[cfg(feature = "testing")]
-        ("debug", "print", debug::native_print),
-        #[cfg(feature = "testing")]
-        (
-            "debug",
-            "print_stack_trace",
-            debug::native_print_stack_trace,
-        ),
-        #[cfg(feature = "testing")]
-        (
-            "unit_test",
-            "create_signers_for_testing",
-            unit_test::native_create_signers_for_testing,
-        ),
-    ];
-    native_functions::make_table(move_std_addr, NATIVES)
+use move_vm_runtime::native_functions::{make_table_from_iter, NativeFunctionTable};
+
+#[derive(Debug, Clone)]
+pub struct GasParameters {
+    pub bcs: bcs::GasParameters,
+    pub hash: hash::GasParameters,
+    pub signer: signer::GasParameters,
+    pub string: string::GasParameters,
+    pub type_name: type_name::GasParameters,
+    pub vector: vector::GasParameters,
+
+    #[cfg(feature = "testing")]
+    pub unit_test: unit_test::GasParameters,
+}
+
+impl GasParameters {
+    pub fn zeros() -> Self {
+        Self {
+            bcs: bcs::GasParameters {
+                to_bytes: bcs::ToBytesGasParameters {
+                    per_byte_serialized: 0.into(),
+                    legacy_min_output_size: 0.into(),
+                    failure: 0.into(),
+                },
+            },
+
+            hash: hash::GasParameters {
+                sha2_256: hash::Sha2_256GasParameters {
+                    base: 0.into(),
+                    per_byte: 0.into(),
+                    legacy_min_input_len: 0.into(),
+                },
+                sha3_256: hash::Sha3_256GasParameters {
+                    base: 0.into(),
+                    per_byte: 0.into(),
+                    legacy_min_input_len: 0.into(),
+                },
+            },
+            type_name: type_name::GasParameters {
+                get: type_name::GetGasParameters {
+                    base: 0.into(),
+                    per_byte: 0.into(),
+                },
+            },
+            signer: signer::GasParameters {
+                borrow_address: signer::BorrowAddressGasParameters { base: 0.into() },
+            },
+            string: string::GasParameters {
+                check_utf8: string::CheckUtf8GasParameters {
+                    base: 0.into(),
+                    per_byte: 0.into(),
+                },
+                is_char_boundary: string::IsCharBoundaryGasParameters { base: 0.into() },
+                sub_string: string::SubStringGasParameters {
+                    base: 0.into(),
+                    per_byte: 0.into(),
+                },
+                index_of: string::IndexOfGasParameters {
+                    base: 0.into(),
+                    per_byte_pattern: 0.into(),
+                    per_byte_searched: 0.into(),
+                },
+            },
+            vector: vector::GasParameters {
+                empty: vector::EmptyGasParameters { base: 0.into() },
+                length: vector::LengthGasParameters { base: 0.into() },
+                push_back: vector::PushBackGasParameters {
+                    base: 0.into(),
+                    legacy_per_abstract_memory_unit: 0.into(),
+                },
+                borrow: vector::BorrowGasParameters { base: 0.into() },
+                pop_back: vector::PopBackGasParameters { base: 0.into() },
+                destroy_empty: vector::DestroyEmptyGasParameters { base: 0.into() },
+                swap: vector::SwapGasParameters { base: 0.into() },
+            },
+            #[cfg(feature = "testing")]
+            unit_test: unit_test::GasParameters {
+                create_signers_for_testing: unit_test::CreateSignersForTestingGasParameters {
+                    base_cost: 0.into(),
+                    unit_cost: 0.into(),
+                },
+            },
+        }
+    }
 }
 
-pub fn all_natives_old_names(move_std_addr: AccountAddress) -> NativeFunctionTable {
-    const NATIVES: &[(&str, &str, NativeFunction)] = &[
-        ("BCS", "to_bytes", bcs::native_to_bytes),
-        ("Event", "write_to_event_store", event::write_to_event_store),
-        ("Hash", "sha2_256", hash::native_sha2_256),
-        ("Hash", "sha3_256", hash::native_sha3_256),
-        ("Signer", "borrow_address", signer::native_borrow_address),
-        ("Vector", "length", vector::native_length),
-        ("Vector", "empty", vector::native_empty),
-        ("Vector", "borrow", vector::native_borrow),
-        ("Vector", "borrow_mut", vector::native_borrow),
-        ("Vector", "push_back", vector::native_push_back),
-        ("Vector", "pop_back", vector::native_pop),
-        ("Vector", "destroy_empty", vector::native_destroy_empty),
-        ("Vector", "swap", vector::native_swap),
-        #[cfg(feature = "testing")]
-        ("Debug", "print", debug::native_print),
-        #[cfg(feature = "testing")]
-        (
-            "Debug",
-            "print_stack_trace",
-            debug::native_print_stack_trace,
-        ),
-        #[cfg(feature = "testing")]
-        (
-            "UnitTest",
-            "create_signers_for_testing",
-            unit_test::native_create_signers_for_testing,
-        ),
-    ];
-    native_functions::make_table(move_std_addr, NATIVES)
+pub fn all_natives(
+    move_std_addr: AccountAddress,
+    gas_params: GasParameters,
+) -> NativeFunctionTable {
+    let mut natives = vec![];
+
+    macro_rules! add_natives {
+        ($module_name: expr, $natives: expr) => {
+            natives.extend(
+                $natives.map(|(func_name, func)| ($module_name.to_string(), func_name, func)),
+            );
+        };
+    }
+
+    add_natives!("bcs", bcs::make_all(gas_params.bcs));
+    add_natives!("hash", hash::make_all(gas_params.hash));
+    add_natives!("signer", signer::make_all(gas_params.signer));
+    add_natives!("string", string::make_all(gas_params.string));
+    add_natives!("type_name", type_name::make_all(gas_params.type_name));
+    add_natives!("vector", vector::make_all(gas_params.vector));
+    #[cfg(feature = "testing")]
+    {
+        add_natives!("unit_test", unit_test::make_all(gas_params.unit_test));
+    }
+
+    make_table_from_iter(move_std_addr, natives)
+}
+
+#[derive(Debug, Clone)]
+pub struct NurseryGasParameters {
+    event: event::GasParameters,
+    debug: debug::GasParameters,
+}
+
+impl NurseryGasParameters {
+    pub fn zeros() -> Self {
+        Self {
+            event: event::GasParameters {
+                write_to_event_store: event::WriteToEventStoreGasParameters {
+                    unit_cost: 0.into(),
+                },
+            },
+            debug: debug::GasParameters {
+                print: debug::PrintGasParameters {
+                    base_cost: 0.into(),
+                },
+                print_stack_trace: debug::PrintStackTraceGasParameters {
+                    base_cost: 0.into(),
+                },
+            },
+        }
+    }
+}
+
+pub fn nursery_natives(
+    move_std_addr: AccountAddress,
+    gas_params: NurseryGasParameters,
+) -> NativeFunctionTable {
+    let mut natives = vec![];
+
+    macro_rules! add_natives {
+        ($module_name: expr, $natives: expr) => {
+            natives.extend(
+                $natives.map(|(func_name, func)| ($module_name.to_string(), func_name, func)),
+            );
+        };
+    }
+
+    add_natives!("event", event::make_all(gas_params.event));
+    add_natives!("debug", debug::make_all(gas_params.debug));
+
+    make_table_from_iter(move_std_addr, natives)
 }
diff --git a/language/move-stdlib/src/natives/signer.rs b/language/move-stdlib/src/natives/signer.rs
index b9da26847f..f6ae868f04 100644
--- a/language/move-stdlib/src/natives/signer.rs
+++ b/language/move-stdlib/src/natives/signer.rs
@@ -2,20 +2,34 @@
 // Copyright (c) The Move Contributors
 // SPDX-License-Identifier: Apache-2.0
 
+use crate::natives::helpers::make_module_natives;
 use move_binary_format::errors::PartialVMResult;
-use move_vm_runtime::native_functions::NativeContext;
+use move_core_types::gas_algebra::InternalGas;
+use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
 use move_vm_types::{
-    gas_schedule::NativeCostIndex,
     loaded_data::runtime_types::Type,
-    natives::function::{native_gas, NativeResult},
+    natives::function::NativeResult,
     pop_arg,
     values::{values_impl::SignerRef, Value},
 };
 use smallvec::smallvec;
-use std::collections::VecDeque;
+use std::{collections::VecDeque, sync::Arc};
 
-pub fn native_borrow_address(
-    context: &mut NativeContext,
+/***************************************************************************************************
+ * native fun borrow_address
+ *
+ *   gas cost: base_cost
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct BorrowAddressGasParameters {
+    pub base: InternalGas,
+}
+
+#[inline]
+fn native_borrow_address(
+    gas_params: &BorrowAddressGasParameters,
+    _context: &mut NativeContext,
     _ty_args: Vec,
     mut arguments: VecDeque,
 ) -> PartialVMResult {
@@ -23,10 +37,34 @@ pub fn native_borrow_address(
     debug_assert!(arguments.len() == 1);
 
     let signer_reference = pop_arg!(arguments, SignerRef);
-    let cost = native_gas(context.cost_table(), NativeCostIndex::SIGNER_BORROW, 1);
 
     Ok(NativeResult::ok(
-        cost,
+        gas_params.base,
         smallvec![signer_reference.borrow_signer()?],
     ))
 }
+
+pub fn make_native_borrow_address(gas_params: BorrowAddressGasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_borrow_address(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * module
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct GasParameters {
+    pub borrow_address: BorrowAddressGasParameters,
+}
+
+pub fn make_all(gas_params: GasParameters) -> impl Iterator {
+    let natives = [(
+        "borrow_address",
+        make_native_borrow_address(gas_params.borrow_address),
+    )];
+
+    make_module_natives(natives)
+}
diff --git a/language/move-stdlib/src/natives/string.rs b/language/move-stdlib/src/natives/string.rs
index 0266fe2654..24b1074dc0 100644
--- a/language/move-stdlib/src/natives/string.rs
+++ b/language/move-stdlib/src/natives/string.rs
@@ -4,17 +4,17 @@
 
 //! Implementation of native functions for utf8 strings.
 
+use crate::natives::helpers::make_module_natives;
 use move_binary_format::errors::PartialVMResult;
-use move_vm_runtime::native_functions::NativeContext;
+use move_core_types::gas_algebra::{InternalGas, InternalGasPerByte, NumBytes};
+use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
 use move_vm_types::{
-    gas_schedule::NativeCostIndex,
     loaded_data::runtime_types::Type,
-    natives::function::{native_gas, NativeResult},
+    natives::function::NativeResult,
     pop_arg,
     values::{Value, VectorRef},
 };
-
-use std::collections::VecDeque;
+use std::{collections::VecDeque, sync::Arc};
 
 // The implementation approach delegates all utf8 handling to Rust.
 // This is possible without copying of bytes because (a) we can
@@ -24,8 +24,21 @@ use std::collections::VecDeque;
 // create a `&str` view on the bytes without a copy. Once we have this
 // view, we can call ut8 functions like length, substring, etc.
 
-pub fn native_check_utf8(
-    context: &mut NativeContext,
+/***************************************************************************************************
+ * native fun internal_check_utf8
+ *
+ *   gas cost: base_cost + unit_cost * length_in_bytes
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct CheckUtf8GasParameters {
+    pub base: InternalGas,
+    pub per_byte: InternalGasPerByte,
+}
+
+fn native_check_utf8(
+    gas_params: &CheckUtf8GasParameters,
+    _context: &mut NativeContext,
     _ty_args: Vec,
     mut args: VecDeque,
 ) -> PartialVMResult {
@@ -34,12 +47,34 @@ pub fn native_check_utf8(
     let s_ref = s_arg.as_bytes_ref();
     let ok = std::str::from_utf8(s_ref.as_slice()).is_ok();
     // TODO: extensible native cost tables
-    let cost = native_gas(context.cost_table(), NativeCostIndex::PUSH_BACK, 1);
+
+    let cost = gas_params.base + gas_params.per_byte * NumBytes::new(s_ref.as_slice().len() as u64);
+
     NativeResult::map_partial_vm_result_one(cost, Ok(Value::bool(ok)))
 }
 
-pub fn native_is_char_boundary(
-    context: &mut NativeContext,
+pub fn make_native_check_utf8(gas_params: CheckUtf8GasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_check_utf8(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * native fun internal_is_char_boundary
+ *
+ *   gas cost: base_cost
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct IsCharBoundaryGasParameters {
+    pub base: InternalGas,
+}
+
+fn native_is_char_boundary(
+    gas_params: &IsCharBoundaryGasParameters,
+    _context: &mut NativeContext,
     _ty_args: Vec,
     mut args: VecDeque,
 ) -> PartialVMResult {
@@ -51,33 +86,80 @@ pub fn native_is_char_boundary(
         // This is safe because we guarantee the bytes to be utf8.
         std::str::from_utf8_unchecked(s_ref.as_slice()).is_char_boundary(i as usize)
     };
-    // TODO: extensible native cost tables
-    let cost = native_gas(context.cost_table(), NativeCostIndex::PUSH_BACK, 1);
-    NativeResult::map_partial_vm_result_one(cost, Ok(Value::bool(ok)))
+    NativeResult::map_partial_vm_result_one(gas_params.base, Ok(Value::bool(ok)))
+}
+
+pub fn make_native_is_char_boundary(gas_params: IsCharBoundaryGasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_is_char_boundary(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * native fun internal_sub_string
+ *
+ *   gas cost: base_cost + unit_cost * sub_string_length_in_bytes
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct SubStringGasParameters {
+    pub base: InternalGas,
+    pub per_byte: InternalGasPerByte,
 }
 
-pub fn native_sub_string(
-    context: &mut NativeContext,
+fn native_sub_string(
+    gas_params: &SubStringGasParameters,
+    _context: &mut NativeContext,
     _ty_args: Vec,
     mut args: VecDeque,
 ) -> PartialVMResult {
     debug_assert!(args.len() == 3);
     let j = pop_arg!(args, u64) as usize;
     let i = pop_arg!(args, u64) as usize;
+
+    if j < i {
+        // TODO: what abort code should we use here?
+        return Ok(NativeResult::err(gas_params.base, 1));
+    }
+
     let s_arg = pop_arg!(args, VectorRef);
     let s_ref = s_arg.as_bytes_ref();
     let s_str = unsafe {
         // This is safe because we guarantee the bytes to be utf8.
         std::str::from_utf8_unchecked(s_ref.as_slice())
     };
-    let v = Value::vector_u8((&s_str[i..j]).as_bytes().iter().cloned());
-    // TODO: extensible native cost tables
-    let cost = native_gas(context.cost_table(), NativeCostIndex::PUSH_BACK, 1);
+    let v = Value::vector_u8(s_str[i..j].as_bytes().iter().cloned());
+
+    let cost = gas_params.base + gas_params.per_byte * NumBytes::new((j - i) as u64);
     NativeResult::map_partial_vm_result_one(cost, Ok(v))
 }
 
-pub fn native_index_of(
-    context: &mut NativeContext,
+pub fn make_native_sub_string(gas_params: SubStringGasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_sub_string(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * native fun internal_index_of
+ *
+ *   gas cost: base_cost + unit_cost * bytes_searched
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct IndexOfGasParameters {
+    pub base: InternalGas,
+    pub per_byte_pattern: InternalGasPerByte,
+    pub per_byte_searched: InternalGasPerByte,
+}
+
+fn native_index_of(
+    gas_params: &IndexOfGasParameters,
+    _context: &mut NativeContext,
     _ty_args: Vec,
     mut args: VecDeque,
 ) -> PartialVMResult {
@@ -92,7 +174,52 @@ pub fn native_index_of(
         Some(size) => size,
         None => s_str.len(),
     };
-    // TODO: extensible native cost tables
-    let cost = native_gas(context.cost_table(), NativeCostIndex::LENGTH, 1);
+    // TODO(Gas): What is the algorithm used for the search?
+    //            Ideally it should be something like KMP with O(n) time complexity...
+    let cost = gas_params.base
+        + gas_params.per_byte_pattern * NumBytes::new(r_str.len() as u64)
+        + gas_params.per_byte_searched * NumBytes::new(pos as u64);
     NativeResult::map_partial_vm_result_one(cost, Ok(Value::u64(pos as u64)))
 }
+
+pub fn make_native_index_of(gas_params: IndexOfGasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_index_of(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * module
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct GasParameters {
+    pub check_utf8: CheckUtf8GasParameters,
+    pub is_char_boundary: IsCharBoundaryGasParameters,
+    pub sub_string: SubStringGasParameters,
+    pub index_of: IndexOfGasParameters,
+}
+
+pub fn make_all(gas_params: GasParameters) -> impl Iterator {
+    let natives = [
+        (
+            "internal_check_utf8",
+            make_native_check_utf8(gas_params.check_utf8),
+        ),
+        (
+            "internal_is_char_boundary",
+            make_native_is_char_boundary(gas_params.is_char_boundary),
+        ),
+        (
+            "internal_sub_string",
+            make_native_sub_string(gas_params.sub_string),
+        ),
+        (
+            "internal_index_of",
+            make_native_index_of(gas_params.index_of),
+        ),
+    ];
+
+    make_module_natives(natives)
+}
diff --git a/language/move-stdlib/src/natives/type_name.rs b/language/move-stdlib/src/natives/type_name.rs
new file mode 100644
index 0000000000..6808289f20
--- /dev/null
+++ b/language/move-stdlib/src/natives/type_name.rs
@@ -0,0 +1,58 @@
+// Copyright (c) The Move Contributors
+// SPDX-License-Identifier: Apache-2.0
+
+use move_binary_format::errors::PartialVMResult;
+use move_core_types::gas_algebra::{InternalGas, InternalGasPerByte, NumBytes};
+use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
+use move_vm_types::{
+    loaded_data::runtime_types::Type,
+    natives::function::NativeResult,
+    values::{Struct, Value},
+};
+
+use smallvec::smallvec;
+use std::{collections::VecDeque, sync::Arc};
+
+#[derive(Debug, Clone)]
+pub struct GetGasParameters {
+    pub base: InternalGas,
+    pub per_byte: InternalGasPerByte,
+}
+
+fn native_get(
+    gas_params: &GetGasParameters,
+    context: &mut NativeContext,
+    ty_args: Vec,
+    arguments: VecDeque,
+) -> PartialVMResult {
+    debug_assert_eq!(ty_args.len(), 1);
+    debug_assert!(arguments.is_empty());
+
+    let type_tag = context.type_to_type_tag(&ty_args[0])?;
+    let type_name = type_tag.to_canonical_string();
+    // make a std::string::String
+    let string_val = Value::struct_(Struct::pack(vec![Value::vector_u8(
+        type_name.as_bytes().to_vec(),
+    )]));
+    // make a std::type_name::TypeName
+    let type_name_val = Value::struct_(Struct::pack(vec![string_val]));
+
+    let cost = gas_params.base + gas_params.per_byte * NumBytes::new(type_name.len() as u64);
+
+    Ok(NativeResult::ok(cost, smallvec![type_name_val]))
+}
+
+pub fn make_native_get(gas_params: GetGasParameters) -> NativeFunction {
+    Arc::new(move |context, ty_args, args| native_get(&gas_params, context, ty_args, args))
+}
+
+#[derive(Debug, Clone)]
+pub struct GasParameters {
+    pub get: GetGasParameters,
+}
+
+pub fn make_all(gas_params: GasParameters) -> impl Iterator {
+    let natives = [("get", make_native_get(gas_params.get))];
+
+    crate::natives::helpers::make_module_natives(natives)
+}
diff --git a/language/move-stdlib/src/natives/unit_test.rs b/language/move-stdlib/src/natives/unit_test.rs
index e68ae3454c..31fe78d549 100644
--- a/language/move-stdlib/src/natives/unit_test.rs
+++ b/language/move-stdlib/src/natives/unit_test.rs
@@ -2,17 +2,25 @@
 // Copyright (c) The Move Contributors
 // SPDX-License-Identifier: Apache-2.0
 
+use crate::natives::helpers::make_module_natives;
 use move_binary_format::errors::PartialVMResult;
-use move_core_types::gas_schedule::ONE_GAS_UNIT;
-use move_vm_runtime::native_functions::NativeContext;
+use move_core_types::{
+    account_address::AccountAddress,
+    gas_algebra::{InternalGas, InternalGasPerArg, NumArgs},
+};
+use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
 use move_vm_types::{
     loaded_data::runtime_types::Type, natives::function::NativeResult, pop_arg, values::Value,
 };
 use smallvec::smallvec;
-use std::collections::VecDeque;
-
-use move_core_types::account_address::AccountAddress;
+use std::{collections::VecDeque, sync::Arc};
 
+/***************************************************************************************************
+ * native fun create_signers_for_testing
+ *
+ *   gas cost: base_cost + unit_cost * num_of_signers
+ *
+ **************************************************************************************************/
 fn to_le_bytes(i: u64) -> [u8; AccountAddress::LENGTH] {
     let bytes = i.to_le_bytes();
     let mut result = [0u8; AccountAddress::LENGTH];
@@ -20,7 +28,14 @@ fn to_le_bytes(i: u64) -> [u8; AccountAddress::LENGTH] {
     result
 }
 
-pub fn native_create_signers_for_testing(
+#[derive(Debug, Clone)]
+pub struct CreateSignersForTestingGasParameters {
+    pub base_cost: InternalGas,
+    pub unit_cost: InternalGasPerArg,
+}
+
+fn native_create_signers_for_testing(
+    gas_params: &CreateSignersForTestingGasParameters,
     _context: &mut NativeContext,
     ty_args: Vec,
     mut args: VecDeque,
@@ -33,5 +48,34 @@ pub fn native_create_signers_for_testing(
         (0..num_signers).map(|i| Value::signer(AccountAddress::new(to_le_bytes(i)))),
     );
 
-    Ok(NativeResult::ok(ONE_GAS_UNIT, smallvec![signers]))
+    let cost = gas_params.base_cost + gas_params.unit_cost * NumArgs::new(num_signers);
+
+    Ok(NativeResult::ok(cost, smallvec![signers]))
+}
+
+pub fn make_native_create_signers_for_testing(
+    gas_params: CreateSignersForTestingGasParameters,
+) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_create_signers_for_testing(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * module
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct GasParameters {
+    pub create_signers_for_testing: CreateSignersForTestingGasParameters,
+}
+
+pub fn make_all(gas_params: GasParameters) -> impl Iterator {
+    let natives = [(
+        "create_signers_for_testing",
+        make_native_create_signers_for_testing(gas_params.create_signers_for_testing),
+    )];
+
+    make_module_natives(natives)
 }
diff --git a/language/move-stdlib/src/natives/vector.rs b/language/move-stdlib/src/natives/vector.rs
index d312b6cc32..de71a7d6c1 100644
--- a/language/move-stdlib/src/natives/vector.rs
+++ b/language/move-stdlib/src/natives/vector.rs
@@ -2,33 +2,67 @@
 // Copyright (c) The Move Contributors
 // SPDX-License-Identifier: Apache-2.0
 
+use crate::natives::helpers::make_module_natives;
 use move_binary_format::errors::{PartialVMError, PartialVMResult};
-use move_core_types::{gas_schedule::GasAlgebra, vm_status::StatusCode};
-use move_vm_runtime::native_functions::NativeContext;
+use move_core_types::{
+    gas_algebra::{InternalGas, InternalGasPerAbstractMemoryUnit},
+    vm_status::StatusCode,
+};
+use move_vm_runtime::native_functions::{NativeContext, NativeFunction};
 use move_vm_types::{
-    gas_schedule::NativeCostIndex,
     loaded_data::runtime_types::Type,
-    natives::function::{native_gas, NativeResult},
+    natives::function::NativeResult,
     pop_arg,
     values::{Value, Vector, VectorRef},
+    views::ValueView,
 };
+use std::{collections::VecDeque, sync::Arc};
 
-use std::collections::VecDeque;
+/***************************************************************************************************
+ * native fun empty
+ *
+ *   gas cost: base_cost
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct EmptyGasParameters {
+    pub base: InternalGas,
+}
 
 pub fn native_empty(
-    context: &mut NativeContext,
+    gas_params: &EmptyGasParameters,
+    _context: &mut NativeContext,
     ty_args: Vec,
     args: VecDeque,
 ) -> PartialVMResult {
     debug_assert!(ty_args.len() == 1);
     debug_assert!(args.is_empty());
 
-    let cost = native_gas(context.cost_table(), NativeCostIndex::EMPTY, 1);
-    NativeResult::map_partial_vm_result_one(cost, Vector::empty(&ty_args[0]))
+    NativeResult::map_partial_vm_result_one(gas_params.base, Vector::empty(&ty_args[0]))
+}
+
+pub fn make_native_empty(gas_params: EmptyGasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_empty(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * native fun length
+ *
+ *   gas cost: base_cost
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct LengthGasParameters {
+    pub base: InternalGas,
 }
 
 pub fn native_length(
-    context: &mut NativeContext,
+    gas_params: &LengthGasParameters,
+    _context: &mut NativeContext,
     ty_args: Vec,
     mut args: VecDeque,
 ) -> PartialVMResult {
@@ -36,12 +70,32 @@ pub fn native_length(
     debug_assert!(args.len() == 1);
 
     let r = pop_arg!(args, VectorRef);
-    let cost = native_gas(context.cost_table(), NativeCostIndex::LENGTH, 1);
-    NativeResult::map_partial_vm_result_one(cost, r.len(&ty_args[0]))
+    NativeResult::map_partial_vm_result_one(gas_params.base, r.len(&ty_args[0]))
+}
+
+pub fn make_native_length(gas_params: LengthGasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_length(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * native fun push_back
+ *
+ *   gas cost: base_cost + legacy_unit_cost * max(1, size_of(val))
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct PushBackGasParameters {
+    pub base: InternalGas,
+    pub legacy_per_abstract_memory_unit: InternalGasPerAbstractMemoryUnit,
 }
 
 pub fn native_push_back(
-    context: &mut NativeContext,
+    gas_params: &PushBackGasParameters,
+    _context: &mut NativeContext,
     ty_args: Vec,
     mut args: VecDeque,
 ) -> PartialVMResult {
@@ -50,16 +104,38 @@ pub fn native_push_back(
 
     let e = args.pop_back().unwrap();
     let r = pop_arg!(args, VectorRef);
-    let cost = native_gas(
-        context.cost_table(),
-        NativeCostIndex::PUSH_BACK,
-        e.size().get() as usize,
-    );
+
+    let mut cost = gas_params.base;
+    if gas_params.legacy_per_abstract_memory_unit != 0.into() {
+        cost += gas_params.legacy_per_abstract_memory_unit
+            * std::cmp::max(e.legacy_abstract_memory_size(), 1.into());
+    }
+
     NativeResult::map_partial_vm_result_empty(cost, r.push_back(e, &ty_args[0]))
 }
 
+pub fn make_native_push_back(gas_params: PushBackGasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_push_back(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * native fun borrow
+ *
+ *   gas cost: base_cost
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct BorrowGasParameters {
+    pub base: InternalGas,
+}
+
 pub fn native_borrow(
-    context: &mut NativeContext,
+    gas_params: &BorrowGasParameters,
+    _context: &mut NativeContext,
     ty_args: Vec,
     mut args: VecDeque,
 ) -> PartialVMResult {
@@ -68,16 +144,35 @@ pub fn native_borrow(
 
     let idx = pop_arg!(args, u64) as usize;
     let r = pop_arg!(args, VectorRef);
-    let cost = native_gas(context.cost_table(), NativeCostIndex::BORROW, 1);
     NativeResult::map_partial_vm_result_one(
-        cost,
+        gas_params.base,
         r.borrow_elem(idx, &ty_args[0])
             .map_err(native_error_to_abort),
     )
 }
 
-pub fn native_pop(
-    context: &mut NativeContext,
+pub fn make_native_borrow(gas_params: BorrowGasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_borrow(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * native fun pop
+ *
+ *   gas cost: base_cost
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct PopBackGasParameters {
+    pub base: InternalGas,
+}
+
+pub fn native_pop_back(
+    gas_params: &PopBackGasParameters,
+    _context: &mut NativeContext,
     ty_args: Vec,
     mut args: VecDeque,
 ) -> PartialVMResult {
@@ -85,12 +180,34 @@ pub fn native_pop(
     debug_assert!(args.len() == 1);
 
     let r = pop_arg!(args, VectorRef);
-    let cost = native_gas(context.cost_table(), NativeCostIndex::POP_BACK, 1);
-    NativeResult::map_partial_vm_result_one(cost, r.pop(&ty_args[0]).map_err(native_error_to_abort))
+    NativeResult::map_partial_vm_result_one(
+        gas_params.base,
+        r.pop(&ty_args[0]).map_err(native_error_to_abort),
+    )
+}
+
+pub fn make_native_pop_back(gas_params: PopBackGasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_pop_back(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * native fun destroy_empty
+ *
+ *   gas cost: base_cost
+ *
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct DestroyEmptyGasParameters {
+    pub base: InternalGas,
 }
 
 pub fn native_destroy_empty(
-    context: &mut NativeContext,
+    gas_params: &DestroyEmptyGasParameters,
+    _context: &mut NativeContext,
     ty_args: Vec,
     mut args: VecDeque,
 ) -> PartialVMResult {
@@ -98,15 +215,31 @@ pub fn native_destroy_empty(
     debug_assert!(args.len() == 1);
 
     let v = pop_arg!(args, Vector);
-    let cost = native_gas(context.cost_table(), NativeCostIndex::DESTROY_EMPTY, 1);
     NativeResult::map_partial_vm_result_empty(
-        cost,
+        gas_params.base,
         v.destroy_empty(&ty_args[0]).map_err(native_error_to_abort),
     )
 }
 
+pub fn make_native_destroy_empty(gas_params: DestroyEmptyGasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_destroy_empty(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
+/***************************************************************************************************
+ * native fun swap
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct SwapGasParameters {
+    pub base: InternalGas,
+}
+
 pub fn native_swap(
-    context: &mut NativeContext,
+    gas_params: &SwapGasParameters,
+    _context: &mut NativeContext,
     ty_args: Vec,
     mut args: VecDeque,
 ) -> PartialVMResult {
@@ -116,14 +249,21 @@ pub fn native_swap(
     let idx2 = pop_arg!(args, u64) as usize;
     let idx1 = pop_arg!(args, u64) as usize;
     let r = pop_arg!(args, VectorRef);
-    let cost = native_gas(context.cost_table(), NativeCostIndex::SWAP, 1);
     NativeResult::map_partial_vm_result_empty(
-        cost,
+        gas_params.base,
         r.swap(idx1, idx2, &ty_args[0])
             .map_err(native_error_to_abort),
     )
 }
 
+pub fn make_native_swap(gas_params: SwapGasParameters) -> NativeFunction {
+    Arc::new(
+        move |context, ty_args, args| -> PartialVMResult {
+            native_swap(&gas_params, context, ty_args, args)
+        },
+    )
+}
+
 fn native_error_to_abort(err: PartialVMError) -> PartialVMError {
     let (major_status, sub_status_opt, message_opt, exec_state_opt, indices, offsets) =
         err.all_data();
@@ -145,3 +285,35 @@ fn native_error_to_abort(err: PartialVMError) -> PartialVMError {
     };
     new_err.at_indices(indices).at_code_offsets(offsets)
 }
+
+/***************************************************************************************************
+ * module
+ **************************************************************************************************/
+#[derive(Debug, Clone)]
+pub struct GasParameters {
+    pub empty: EmptyGasParameters,
+    pub length: LengthGasParameters,
+    pub push_back: PushBackGasParameters,
+    pub borrow: BorrowGasParameters,
+    pub pop_back: PopBackGasParameters,
+    pub destroy_empty: DestroyEmptyGasParameters,
+    pub swap: SwapGasParameters,
+}
+
+pub fn make_all(gas_params: GasParameters) -> impl Iterator {
+    let natives = [
+        ("empty", make_native_empty(gas_params.empty)),
+        ("length", make_native_length(gas_params.length)),
+        ("push_back", make_native_push_back(gas_params.push_back)),
+        ("borrow", make_native_borrow(gas_params.borrow.clone())),
+        ("borrow_mut", make_native_borrow(gas_params.borrow)),
+        ("pop_back", make_native_pop_back(gas_params.pop_back)),
+        (
+            "destroy_empty",
+            make_native_destroy_empty(gas_params.destroy_empty),
+        ),
+        ("swap", make_native_swap(gas_params.swap)),
+    ];
+
+    make_module_natives(natives)
+}
diff --git a/language/move-stdlib/tests/fixedpoint32_tests.move b/language/move-stdlib/tests/fixedpoint32_tests.move
index ece9704098..3ec7574ceb 100644
--- a/language/move-stdlib/tests/fixedpoint32_tests.move
+++ b/language/move-stdlib/tests/fixedpoint32_tests.move
@@ -106,4 +106,83 @@ module std::fixed_point32_tests {
         let one = fixed_point32::get_raw_value(f);
         assert!(one == 4294967296, 0); // 0x1.00000000
     }
+
+    #[test]
+    fun min_can_return_smaller_fixed_point_number() {
+        let one = fixed_point32::create_from_rational(1, 1);
+        let two = fixed_point32::create_from_rational(2, 1);
+        let smaller_number1 = fixed_point32::min(one, two);
+        let val1 = fixed_point32::get_raw_value(smaller_number1);
+        assert!(val1 == 4294967296, 0);  // 0x1.00000000
+        let smaller_number2 = fixed_point32::min(two, one);
+        let val2 = fixed_point32::get_raw_value(smaller_number2);
+        assert!(val2 == 4294967296, 0);  // 0x1.00000000
+    }
+
+    #[test]
+    fun max_can_return_larger_fixed_point_number() {
+        let one = fixed_point32::create_from_rational(1, 1);
+        let two = fixed_point32::create_from_rational(2, 1);
+        let larger_number1 = fixed_point32::max(one, two);
+        let larger_number2 = fixed_point32::max(two, one);
+        let val1 = fixed_point32::get_raw_value(larger_number1);
+        assert!(val1 == 8589934592, 0);  // 0x2.00000000
+        let val2 = fixed_point32::get_raw_value(larger_number2);
+        assert!(val2 == 8589934592, 0);  // 0x2.00000000
+    }
+
+    #[test]
+    fun floor_can_return_the_correct_number_zero() {
+        let point_five = fixed_point32::create_from_rational(1, 2);
+        let val = fixed_point32::floor(point_five);
+        assert!(val == 0, 0);
+    }
+
+    #[test]
+    fun create_from_u64_create_correct_fixed_point_number() {
+        let one = fixed_point32::create_from_u64(1);
+        let val = fixed_point32::get_raw_value(one);
+        assert!(val == 4294967296, 0);
+    }
+
+    #[test]
+    #[expected_failure(abort_code = 0x20005)]
+    fun create_from_u64_throw_error_when_number_too_large() {
+        fixed_point32::create_from_u64(4294967296); // (u64 >> 32) + 1
+    }
+
+    #[test]
+    fun floor_can_return_the_correct_number_one() {
+        let three_point_five = fixed_point32::create_from_rational(7, 2); // 3.5
+        let val = fixed_point32::floor(three_point_five);
+        assert!(val == 3, 0);
+    }
+
+    #[test]
+    fun ceil_can_round_up_correctly() {
+        let point_five = fixed_point32::create_from_rational(1, 2); // 0.5
+        let val = fixed_point32::ceil(point_five);
+        assert!(val == 1, 0);
+    }
+
+    #[test]
+    fun ceil_will_not_change_if_number_already_integer() {
+        let one = fixed_point32::create_from_rational(1, 1); // 0.5
+        let val = fixed_point32::ceil(one);
+        assert!(val == 1, 0);
+    }
+
+    #[test]
+    fun round_can_round_up_correctly() {
+        let point_five = fixed_point32::create_from_rational(1, 2); // 0.5
+        let val = fixed_point32::round(point_five);
+        assert!(val == 1, 0);
+    }
+
+    #[test]
+    fun round_can_round_down_correctly() {
+        let num = fixed_point32::create_from_rational(499, 1000); // 0.499
+        let val = fixed_point32::round(num);
+        assert!(val == 0, 0);
+    }
 }
diff --git a/language/move-stdlib/tests/move_unit_test.rs b/language/move-stdlib/tests/move_unit_test.rs
index 187e244d83..4255c48980 100644
--- a/language/move-stdlib/tests/move_unit_test.rs
+++ b/language/move-stdlib/tests/move_unit_test.rs
@@ -4,12 +4,27 @@
 
 use move_cli::base::test::{run_move_unit_tests, UnitTestResult};
 use move_core_types::account_address::AccountAddress;
-use move_stdlib::{natives::all_natives, path_in_crate};
+use move_stdlib::{
+    natives::{all_natives, nursery_natives, GasParameters, NurseryGasParameters},
+    path_in_crate,
+};
 use move_unit_test::UnitTestingConfig;
 use tempfile::tempdir;
 
-fn run_tests_for_pkg(path_to_pkg: impl Into) {
+fn run_tests_for_pkg(path_to_pkg: impl Into, include_nursery_natives: bool) {
     let pkg_path = path_in_crate(path_to_pkg);
+
+    let mut natives = all_natives(
+        AccountAddress::from_hex_literal("0x1").unwrap(),
+        GasParameters::zeros(),
+    );
+    if include_nursery_natives {
+        natives.extend(nursery_natives(
+            AccountAddress::from_hex_literal("0x1").unwrap(),
+            NurseryGasParameters::zeros(),
+        ))
+    }
+
     let result = run_move_unit_tests(
         &pkg_path,
         move_package::BuildConfig {
@@ -18,7 +33,7 @@ fn run_tests_for_pkg(path_to_pkg: impl Into) {
             ..Default::default()
         },
         UnitTestingConfig::default_with_bound(Some(100_000)),
-        all_natives(AccountAddress::from_hex_literal("0x1").unwrap()),
+        natives,
         /* compute_coverage */ false,
         &mut std::io::stdout(),
     )
@@ -30,6 +45,6 @@ fn run_tests_for_pkg(path_to_pkg: impl Into) {
 
 #[test]
 fn move_unit_tests() {
-    run_tests_for_pkg(".");
-    run_tests_for_pkg("nursery");
+    run_tests_for_pkg(".", false);
+    run_tests_for_pkg("nursery", true);
 }
diff --git a/language/move-stdlib/tests/type_name_tests.move b/language/move-stdlib/tests/type_name_tests.move
new file mode 100644
index 0000000000..c8ed2febc0
--- /dev/null
+++ b/language/move-stdlib/tests/type_name_tests.move
@@ -0,0 +1,38 @@
+// note: intentionally using 0xa here to test non-0x1 module addresses
+module 0xA::type_name_tests {
+    #[test_only]
+    use std::type_name::{get, into_string};
+    #[test_only]
+    use std::ascii::string;
+
+    struct TestStruct {}
+
+    struct TestGenerics { }
+
+    #[test]
+    fun test_ground_types() {
+        assert!(into_string(get()) == string(b"u8"), 0);
+        assert!(into_string(get()) == string(b"u64"), 0);
+        assert!(into_string(get()) == string(b"u128"), 0);
+        assert!(into_string(get
()) == string(b"address"), 0); + assert!(into_string(get()) == string(b"signer"), 0); + assert!(into_string(get>()) == string(b"vector"), 0) + } + + // Note: these tests assume a 16 byte address length, and will fail on platforms where addresses are 20 or 32 bytes + #[test] + fun test_structs() { + assert!(into_string(get()) == string(b"0000000000000000000000000000000a::type_name_tests::TestStruct"), 0); + assert!(into_string(get()) == string(b"00000000000000000000000000000001::ascii::String"), 0); + assert!(into_string(get>()) == string(b"00000000000000000000000000000001::option::Option"), 0); + assert!(into_string(get()) == string(b"00000000000000000000000000000001::string::String"), 0); + } + + // Note: these tests assume a 16 byte address length, and will fail on platforms where addresses are 20 or 32 bytes + #[test] + fun test_generics() { + assert!(into_string(get>()) == string(b"0000000000000000000000000000000a::type_name_tests::TestGenerics<00000000000000000000000000000001::string::String>"), 0); + assert!(into_string(get>>()) == string(b"vector<0000000000000000000000000000000a::type_name_tests::TestGenerics>"), 0); + assert!(into_string(get>>()) == string(b"00000000000000000000000000000001::option::Option<0000000000000000000000000000000a::type_name_tests::TestGenerics>"), 0); + } +} diff --git a/language/move-symbol-pool/Cargo.toml b/language/move-symbol-pool/Cargo.toml index 6cc5b14748..db132c8990 100644 --- a/language/move-symbol-pool/Cargo.toml +++ b/language/move-symbol-pool/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] once_cell = "1.7.2" diff --git a/language/move-vm/integration-tests/Cargo.toml b/language/move-vm/integration-tests/Cargo.toml index 67b82cf73e..e62c8fe7c9 100644 --- a/language/move-vm/integration-tests/Cargo.toml +++ b/language/move-vm/integration-tests/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -17,6 +17,7 @@ tempfile = "3.2.0" move-core-types = {path = "../../move-core/types" } move-binary-format = { path = "../../move-binary-format" } +move-bytecode-verifier = { path = "../../move-bytecode-verifier" } move-compiler = { path = "../../move-compiler" } move-vm-runtime = { path = "../runtime" } move-vm-types = { path = "../types" } diff --git a/language/move-vm/integration-tests/src/tests/bad_entry_point_tests.rs b/language/move-vm/integration-tests/src/tests/bad_entry_point_tests.rs index 1d22c8a4e9..1287861869 100644 --- a/language/move-vm/integration-tests/src/tests/bad_entry_point_tests.rs +++ b/language/move-vm/integration-tests/src/tests/bad_entry_point_tests.rs @@ -12,7 +12,7 @@ use move_core_types::{ }; use move_vm_runtime::move_vm::MoveVM; use move_vm_test_utils::{BlankStorage, InMemoryStorage}; -use move_vm_types::gas_schedule::GasStatus; +use move_vm_types::gas::UnmeteredGasMeter; const TEST_ADDR: AccountAddress = AccountAddress::new([42; AccountAddress::LENGTH]); @@ -24,7 +24,6 @@ fn call_non_existent_module() { let mut sess = vm.new_session(&storage); let module_id = ModuleId::new(TEST_ADDR, Identifier::new("M").unwrap()); let fun_name = Identifier::new("foo").unwrap(); - let mut gas_status = GasStatus::new_unmetered(); let err = sess .execute_function_bypass_visibility( @@ -32,7 +31,7 @@ fn call_non_existent_module() { &fun_name, vec![], serialize_values(&vec![MoveValue::Signer(TEST_ADDR)]), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap_err(); @@ -59,7 +58,6 @@ fn call_non_existent_function() { let mut sess = vm.new_session(&storage); let fun_name = Identifier::new("foo").unwrap(); - let mut gas_status = GasStatus::new_unmetered(); let err = sess .execute_function_bypass_visibility( @@ -67,7 +65,7 @@ fn call_non_existent_function() { &fun_name, vec![], serialize_values(&vec![MoveValue::Signer(TEST_ADDR)]), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap_err(); diff --git a/language/move-vm/integration-tests/src/tests/bad_storage_tests.rs b/language/move-vm/integration-tests/src/tests/bad_storage_tests.rs index a1c94b4041..533d007d00 100644 --- a/language/move-vm/integration-tests/src/tests/bad_storage_tests.rs +++ b/language/move-vm/integration-tests/src/tests/bad_storage_tests.rs @@ -6,7 +6,7 @@ use crate::compiler::{as_module, as_script, compile_units}; use move_binary_format::errors::{Location, PartialVMError, VMError}; use move_core_types::{ account_address::AccountAddress, - effects::ChangeSet, + effects::{ChangeSet, Op}, identifier::Identifier, language_storage::{ModuleId, StructTag}, resolver::{ModuleResolver, ResourceResolver}, @@ -15,7 +15,7 @@ use move_core_types::{ }; use move_vm_runtime::move_vm::MoveVM; use move_vm_test_utils::{DeltaStorage, InMemoryStorage}; -use move_vm_types::gas_schedule::GasStatus; +use move_vm_types::gas::UnmeteredGasMeter; const TEST_ADDR: AccountAddress = AccountAddress::new([42; AccountAddress::LENGTH]); @@ -86,11 +86,10 @@ fn test_malformed_resource() { let vm = MoveVM::new(move_stdlib::natives::all_natives( AccountAddress::from_hex_literal("0x1").unwrap(), + move_stdlib::natives::GasParameters::zeros(), )) .unwrap(); - let mut gas_status = GasStatus::new_unmetered(); - // Execute the first script to publish a resource Foo. let mut script_blob = vec![]; s1.serialize(&mut script_blob).unwrap(); @@ -99,7 +98,7 @@ fn test_malformed_resource() { script_blob, vec![], vec![MoveValue::Signer(TEST_ADDR).simple_serialize().unwrap()], - &mut gas_status, + &mut UnmeteredGasMeter, ) .map(|_| ()) .unwrap(); @@ -117,7 +116,7 @@ fn test_malformed_resource() { script_blob.clone(), vec![], vec![MoveValue::Signer(TEST_ADDR).simple_serialize().unwrap()], - &mut gas_status, + &mut UnmeteredGasMeter, ) .map(|_| ()) .unwrap(); @@ -144,7 +143,7 @@ fn test_malformed_resource() { script_blob, vec![], vec![MoveValue::Signer(TEST_ADDR).simple_serialize().unwrap()], - &mut gas_status, + &mut UnmeteredGasMeter, ) .map(|_| ()) .unwrap_err(); @@ -172,8 +171,6 @@ fn test_malformed_module() { let module_id = ModuleId::new(TEST_ADDR, Identifier::new("M").unwrap()); let fun_name = Identifier::new("foo").unwrap(); - let mut gas_status = GasStatus::new_unmetered(); - // Publish M and call M::foo. No errors should be thrown. { let mut storage = InMemoryStorage::new(); @@ -185,7 +182,7 @@ fn test_malformed_module() { &fun_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap(); } @@ -211,7 +208,7 @@ fn test_malformed_module() { &fun_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap_err(); assert_eq!(err.status_type(), StatusType::InvariantViolation); @@ -231,7 +228,6 @@ fn test_unverifiable_module() { let mut units = compile_units(&code).unwrap(); let m = as_module(units.pop().unwrap()); - let mut gas_status = GasStatus::new_unmetered(); let module_id = ModuleId::new(TEST_ADDR, Identifier::new("M").unwrap()); let fun_name = Identifier::new("foo").unwrap(); @@ -251,7 +247,7 @@ fn test_unverifiable_module() { &fun_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap(); } @@ -276,7 +272,7 @@ fn test_unverifiable_module() { &fun_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap_err(); @@ -308,8 +304,6 @@ fn test_missing_module_dependency() { let mut blob_n = vec![]; n.serialize(&mut blob_n).unwrap(); - let mut gas_status = GasStatus::new_unmetered(); - let module_id = ModuleId::new(TEST_ADDR, Identifier::new("N").unwrap()); let fun_name = Identifier::new("bar").unwrap(); @@ -328,7 +322,7 @@ fn test_missing_module_dependency() { &fun_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap(); } @@ -348,7 +342,7 @@ fn test_missing_module_dependency() { &fun_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap_err(); @@ -357,7 +351,7 @@ fn test_missing_module_dependency() { } #[test] -fn test_malformed_module_denpency() { +fn test_malformed_module_dependency() { // Compile two modules M, N where N depends on M. let code = r#" module {{ADDR}}::M { @@ -380,8 +374,6 @@ fn test_malformed_module_denpency() { let mut blob_n = vec![]; n.serialize(&mut blob_n).unwrap(); - let mut gas_status = GasStatus::new_unmetered(); - let module_id = ModuleId::new(TEST_ADDR, Identifier::new("N").unwrap()); let fun_name = Identifier::new("bar").unwrap(); @@ -400,7 +392,7 @@ fn test_malformed_module_denpency() { &fun_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap(); } @@ -426,7 +418,7 @@ fn test_malformed_module_denpency() { &fun_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap_err(); @@ -456,8 +448,6 @@ fn test_unverifiable_module_dependency() { let mut blob_n = vec![]; n.serialize(&mut blob_n).unwrap(); - let mut gas_status = GasStatus::new_unmetered(); - let module_id = ModuleId::new(TEST_ADDR, Identifier::new("N").unwrap()); let fun_name = Identifier::new("bar").unwrap(); @@ -479,7 +469,7 @@ fn test_unverifiable_module_dependency() { &fun_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap(); } @@ -505,7 +495,7 @@ fn test_unverifiable_module_dependency() { &fun_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap_err(); @@ -549,7 +539,6 @@ const LIST_OF_ERROR_CODES: &[StatusCode] = &[ #[test] fn test_storage_returns_bogus_error_when_loading_module() { - let mut gas_status = GasStatus::new_unmetered(); let module_id = ModuleId::new(TEST_ADDR, Identifier::new("N").unwrap()); let fun_name = Identifier::new("bar").unwrap(); @@ -566,7 +555,7 @@ fn test_storage_returns_bogus_error_when_loading_module() { &fun_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap_err(); @@ -576,8 +565,6 @@ fn test_storage_returns_bogus_error_when_loading_module() { #[test] fn test_storage_returns_bogus_error_when_loading_resource() { - let mut gas_status = GasStatus::new_unmetered(); - let code = r#" address std { module signer { @@ -611,8 +598,8 @@ fn test_storage_returns_bogus_error_when_loading_resource() { m.serialize(&mut m_blob).unwrap(); s.serialize(&mut s_blob).unwrap(); let mut delta = ChangeSet::new(); - delta.publish_module(m.self_id(), m_blob).unwrap(); - delta.publish_module(s.self_id(), s_blob).unwrap(); + delta.add_module_op(m.self_id(), Op::New(m_blob)).unwrap(); + delta.add_module_op(s.self_id(), Op::New(s_blob)).unwrap(); let m_id = m.self_id(); let foo_name = Identifier::new("foo").unwrap(); @@ -626,6 +613,7 @@ fn test_storage_returns_bogus_error_when_loading_resource() { let vm = MoveVM::new(move_stdlib::natives::all_natives( AccountAddress::from_hex_literal("0x1").unwrap(), + move_stdlib::natives::GasParameters::zeros(), )) .unwrap(); let mut sess = vm.new_session(&storage); @@ -635,7 +623,7 @@ fn test_storage_returns_bogus_error_when_loading_resource() { &foo_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap(); @@ -645,7 +633,7 @@ fn test_storage_returns_bogus_error_when_loading_resource() { &bar_name, vec![], serialize_values(&vec![MoveValue::Signer(TEST_ADDR)]), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap_err(); diff --git a/language/move-vm/integration-tests/src/tests/exec_func_effects_tests.rs b/language/move-vm/integration-tests/src/tests/exec_func_effects_tests.rs index 205cc79f70..db8c403265 100644 --- a/language/move-vm/integration-tests/src/tests/exec_func_effects_tests.rs +++ b/language/move-vm/integration-tests/src/tests/exec_func_effects_tests.rs @@ -9,7 +9,6 @@ use move_binary_format::errors::VMResult; use move_core_types::{ account_address::AccountAddress, effects::{ChangeSet, Event}, - gas_schedule::GasAlgebra, identifier::Identifier, language_storage::ModuleId, value::{serialize_values, MoveValue}, @@ -17,7 +16,7 @@ use move_core_types::{ }; use move_vm_runtime::{move_vm::MoveVM, session::SerializedReturnValues}; use move_vm_test_utils::InMemoryStorage; -use move_vm_types::gas_schedule::GasStatus; +use move_vm_types::gas::UnmeteredGasMeter; const TEST_ADDR: AccountAddress = AccountAddress::new([42; AccountAddress::LENGTH]); const TEST_MODULE_ID: &str = "M"; @@ -39,7 +38,6 @@ fn fail_arg_deserialize() { for value in values { for name in FUN_NAMES { let err = run(&mod_code, name, value.clone()) - .1 .map(|_| ()) .expect_err("Should have failed to deserialize non-u64 type to u64"); assert_eq!( @@ -54,7 +52,7 @@ fn fail_arg_deserialize() { #[test] fn mutref_output_success() { let mod_code = setup_module(); - let (_gas_used, result) = run(&mod_code, USE_MUTREF_LABEL, MoveValue::U64(1)); + let result = run(&mod_code, USE_MUTREF_LABEL, MoveValue::U64(1)); let (_, _, ret_values) = result.unwrap(); assert_eq!(1, ret_values.mutable_reference_outputs.len()); let parsed = parse_u64_arg(&ret_values.mutable_reference_outputs.first().unwrap().1); @@ -84,32 +82,26 @@ fn run( module: &ModuleCode, fun_name: &str, arg_val0: MoveValue, -) -> ( - /* gas used */ u64, - VMResult<(ChangeSet, Vec, SerializedReturnValues)>, -) { +) -> VMResult<(ChangeSet, Vec, SerializedReturnValues)> { let module_id = &module.0; let modules = vec![module.clone()]; let (vm, storage) = setup_vm(&modules); let mut session = vm.new_session(&storage); let fun_name = Identifier::new(fun_name).unwrap(); - let mut gas_status = GasStatus::new_unmetered(); - let gas_start = gas_status.remaining_gas().get(); - let res = session + + session .execute_function_bypass_visibility( module_id, &fun_name, vec![], serialize_values(&vec![arg_val0]), - &mut gas_status, + &mut UnmeteredGasMeter, ) .and_then(|ret_values| { let (change_set, events) = session.finish()?; Ok((change_set, events, ret_values)) - }); - let gas_used = gas_start - gas_status.remaining_gas().get(); - (gas_used, res) + }) } type ModuleCode = (ModuleId, String); @@ -121,9 +113,9 @@ fn setup_vm(modules: &[ModuleCode]) -> (MoveVM, InMemoryStorage) { (MoveVM::new(vec![]).unwrap(), storage) } -fn compile_modules(mut storage: &mut InMemoryStorage, modules: &[ModuleCode]) { +fn compile_modules(storage: &mut InMemoryStorage, modules: &[ModuleCode]) { modules.iter().for_each(|(id, code)| { - compile_module(&mut storage, id, code); + compile_module(storage, id, code); }); } diff --git a/language/move-vm/integration-tests/src/tests/function_arg_tests.rs b/language/move-vm/integration-tests/src/tests/function_arg_tests.rs index 52fbb910e5..a882cea324 100644 --- a/language/move-vm/integration-tests/src/tests/function_arg_tests.rs +++ b/language/move-vm/integration-tests/src/tests/function_arg_tests.rs @@ -13,7 +13,7 @@ use move_core_types::{ }; use move_vm_runtime::move_vm::MoveVM; use move_vm_test_utils::InMemoryStorage; -use move_vm_types::gas_schedule::GasStatus; +use move_vm_types::gas::UnmeteredGasMeter; const TEST_ADDR: AccountAddress = AccountAddress::new([42; AccountAddress::LENGTH]); @@ -60,14 +60,19 @@ fn run( let mut sess = vm.new_session(&storage); let fun_name = Identifier::new("foo").unwrap(); - let mut gas_status = GasStatus::new_unmetered(); let args: Vec<_> = args .into_iter() .map(|val| val.simple_serialize().unwrap()) .collect(); - sess.execute_function_bypass_visibility(&module_id, &fun_name, ty_args, args, &mut gas_status)?; + sess.execute_function_bypass_visibility( + &module_id, + &fun_name, + ty_args, + args, + &mut UnmeteredGasMeter, + )?; Ok(()) } diff --git a/language/move-vm/integration-tests/src/tests/loader_tests.rs b/language/move-vm/integration-tests/src/tests/loader_tests.rs index 9b5b0a7ab6..018c6b141b 100644 --- a/language/move-vm/integration-tests/src/tests/loader_tests.rs +++ b/language/move-vm/integration-tests/src/tests/loader_tests.rs @@ -11,7 +11,8 @@ use move_core_types::{ }; use move_vm_runtime::move_vm::MoveVM; use move_vm_test_utils::InMemoryStorage; -use move_vm_types::gas_schedule::GasStatus; +use move_vm_types::gas::UnmeteredGasMeter; + use std::{path::PathBuf, sync::Arc, thread}; const WORKING_ACCOUNT: AccountAddress = @@ -56,14 +57,14 @@ impl Adapter { fn publish_modules(&mut self, modules: Vec) { let mut session = self.vm.new_session(&self.store); - let mut gas_status = GasStatus::new_unmetered(); + for module in modules { let mut binary = vec![]; module .serialize(&mut binary) .unwrap_or_else(|_| panic!("failure in module serialization: {:#?}", module)); session - .publish_module(binary, WORKING_ACCOUNT, &mut gas_status) + .publish_module(binary, WORKING_ACCOUNT, &mut UnmeteredGasMeter) .unwrap_or_else(|_| panic!("failure publishing module: {:#?}", module)); } let (changeset, _) = session.finish().expect("failure getting write set"); @@ -85,7 +86,6 @@ impl Adapter { let vm = self.vm.clone(); let data_store = self.store.clone(); children.push(thread::spawn(move || { - let mut gas_status = GasStatus::new_unmetered(); let mut session = vm.new_session(&data_store); session .execute_function_bypass_visibility( @@ -93,7 +93,7 @@ impl Adapter { &name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap_or_else(|_| { panic!("Failure executing {:?}::{:?}", module_id, name) @@ -107,7 +107,6 @@ impl Adapter { } fn call_function(&self, module: &ModuleId, name: &IdentStr) { - let mut gas_status = GasStatus::new_unmetered(); let mut session = self.vm.new_session(&self.store); session .execute_function_bypass_visibility( @@ -115,7 +114,7 @@ impl Adapter { name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap_or_else(|_| panic!("Failure executing {:?}::{:?}", module, name)); } diff --git a/language/move-vm/integration-tests/src/tests/mod.rs b/language/move-vm/integration-tests/src/tests/mod.rs index 596ebe0b7c..d055e07784 100644 --- a/language/move-vm/integration-tests/src/tests/mod.rs +++ b/language/move-vm/integration-tests/src/tests/mod.rs @@ -8,4 +8,5 @@ mod exec_func_effects_tests; mod function_arg_tests; mod loader_tests; mod mutated_accounts_tests; +mod nested_loop_tests; mod return_value_tests; diff --git a/language/move-vm/integration-tests/src/tests/mutated_accounts_tests.rs b/language/move-vm/integration-tests/src/tests/mutated_accounts_tests.rs index 661b7cab08..ad9d9188d6 100644 --- a/language/move-vm/integration-tests/src/tests/mutated_accounts_tests.rs +++ b/language/move-vm/integration-tests/src/tests/mutated_accounts_tests.rs @@ -11,7 +11,7 @@ use move_core_types::{ }; use move_vm_runtime::move_vm::MoveVM; use move_vm_test_utils::InMemoryStorage; -use move_vm_types::gas_schedule::GasStatus; +use move_vm_types::gas::UnmeteredGasMeter; const TEST_ADDR: AccountAddress = AccountAddress::new([42; AccountAddress::LENGTH]); @@ -46,8 +46,6 @@ fn mutated_accounts() { let vm = MoveVM::new(vec![]).unwrap(); let mut sess = vm.new_session(&storage); - let mut gas_status = GasStatus::new_unmetered(); - let publish = Identifier::new("publish").unwrap(); let flip = Identifier::new("flip").unwrap(); let get = Identifier::new("get").unwrap(); @@ -59,7 +57,7 @@ fn mutated_accounts() { &publish, vec![], serialize_values(&vec![MoveValue::Signer(account1)]), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap(); @@ -73,7 +71,7 @@ fn mutated_accounts() { &get, vec![], serialize_values(&vec![MoveValue::Address(account1)]), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap(); @@ -84,7 +82,7 @@ fn mutated_accounts() { &flip, vec![], serialize_values(&vec![MoveValue::Address(account1)]), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap(); assert_eq!(sess.num_mutated_accounts(&TEST_ADDR), 2); @@ -98,7 +96,7 @@ fn mutated_accounts() { &get, vec![], serialize_values(&vec![MoveValue::Address(account1)]), - &mut gas_status, + &mut UnmeteredGasMeter, ) .unwrap(); diff --git a/language/move-vm/integration-tests/src/tests/nested_loop_tests.rs b/language/move-vm/integration-tests/src/tests/nested_loop_tests.rs new file mode 100644 index 0000000000..ab55a946a2 --- /dev/null +++ b/language/move-vm/integration-tests/src/tests/nested_loop_tests.rs @@ -0,0 +1,141 @@ +// Copyright (c) The Move Contributors +// SPDX-License-Identifier: Apache-2.0 + +use crate::compiler::{as_module, as_script, compile_units}; +use move_bytecode_verifier::VerifierConfig; +use move_core_types::account_address::AccountAddress; +use move_vm_runtime::move_vm::MoveVM; +use move_vm_test_utils::InMemoryStorage; +use move_vm_types::gas::UnmeteredGasMeter; + +const TEST_ADDR: AccountAddress = AccountAddress::new([42; AccountAddress::LENGTH]); + +#[test] +fn test_publish_module_with_nested_loops() { + // Compile the modules and scripts. + // TODO: find a better way to include the Signer module. + let code = r#" + module {{ADDR}}::M { + fun foo() { + let i = 0; + while (i < 10) { + let j = 0; + while (j < 10) { + j = j + 1; + }; + i = i + 1; + }; + } + } + "#; + let code = code.replace("{{ADDR}}", &format!("0x{}", TEST_ADDR)); + let mut units = compile_units(&code).unwrap(); + + let m = as_module(units.pop().unwrap()); + let mut m_blob = vec![]; + m.serialize(&mut m_blob).unwrap(); + + // Should succeed with max_loop_depth = 2 + { + let storage = InMemoryStorage::new(); + let vm = MoveVM::new_with_verifier_config( + move_stdlib::natives::all_natives( + AccountAddress::from_hex_literal("0x1").unwrap(), + move_stdlib::natives::GasParameters::zeros(), + ), + VerifierConfig { + max_loop_depth: Some(2), + }, + ) + .unwrap(); + + let mut sess = vm.new_session(&storage); + sess.publish_module(m_blob.clone(), TEST_ADDR, &mut UnmeteredGasMeter) + .unwrap(); + } + + // Should fail with max_loop_depth = 1 + { + let storage = InMemoryStorage::new(); + let vm = MoveVM::new_with_verifier_config( + move_stdlib::natives::all_natives( + AccountAddress::from_hex_literal("0x1").unwrap(), + move_stdlib::natives::GasParameters::zeros(), + ), + VerifierConfig { + max_loop_depth: Some(1), + }, + ) + .unwrap(); + + let mut sess = vm.new_session(&storage); + sess.publish_module(m_blob, TEST_ADDR, &mut UnmeteredGasMeter) + .unwrap_err(); + } +} + +#[test] +fn test_run_script_with_nested_loops() { + // Compile the modules and scripts. + // TODO: find a better way to include the Signer module. + let code = r#" + script { + fun main() { + let i = 0; + while (i < 10) { + let j = 0; + while (j < 10) { + j = j + 1; + }; + i = i + 1; + }; + } + } + "#; + let code = code.replace("{{ADDR}}", &format!("0x{}", TEST_ADDR)); + let mut units = compile_units(&code).unwrap(); + + let s = as_script(units.pop().unwrap()); + let mut s_blob: Vec = vec![]; + s.serialize(&mut s_blob).unwrap(); + + // Should succeed with max_loop_depth = 2 + { + let storage = InMemoryStorage::new(); + let vm = MoveVM::new_with_verifier_config( + move_stdlib::natives::all_natives( + AccountAddress::from_hex_literal("0x1").unwrap(), + move_stdlib::natives::GasParameters::zeros(), + ), + VerifierConfig { + max_loop_depth: Some(2), + }, + ) + .unwrap(); + + let mut sess = vm.new_session(&storage); + let args: Vec> = vec![]; + sess.execute_script(s_blob.clone(), vec![], args, &mut UnmeteredGasMeter) + .unwrap(); + } + + // Should fail with max_loop_depth = 1 + { + let storage = InMemoryStorage::new(); + let vm = MoveVM::new_with_verifier_config( + move_stdlib::natives::all_natives( + AccountAddress::from_hex_literal("0x1").unwrap(), + move_stdlib::natives::GasParameters::zeros(), + ), + VerifierConfig { + max_loop_depth: Some(1), + }, + ) + .unwrap(); + + let mut sess = vm.new_session(&storage); + let args: Vec> = vec![]; + sess.execute_script(s_blob, vec![], args, &mut UnmeteredGasMeter) + .unwrap_err(); + } +} diff --git a/language/move-vm/integration-tests/src/tests/return_value_tests.rs b/language/move-vm/integration-tests/src/tests/return_value_tests.rs index 230541c48a..841054cb8d 100644 --- a/language/move-vm/integration-tests/src/tests/return_value_tests.rs +++ b/language/move-vm/integration-tests/src/tests/return_value_tests.rs @@ -12,7 +12,7 @@ use move_core_types::{ }; use move_vm_runtime::{move_vm::MoveVM, session::SerializedReturnValues}; use move_vm_test_utils::InMemoryStorage; -use move_vm_types::gas_schedule::GasStatus; +use move_vm_types::gas::UnmeteredGasMeter; const TEST_ADDR: AccountAddress = AccountAddress::new([42; AccountAddress::LENGTH]); @@ -51,7 +51,6 @@ fn run( let mut sess = vm.new_session(&storage); let fun_name = Identifier::new("foo").unwrap(); - let mut gas_status = GasStatus::new_unmetered(); let args: Vec<_> = args .into_iter() @@ -66,7 +65,7 @@ fn run( &fun_name, ty_args, args, - &mut gas_status, + &mut UnmeteredGasMeter, )?; Ok(return_values diff --git a/language/move-vm/runtime/Cargo.toml b/language/move-vm/runtime/Cargo.toml index df81c5a70e..fb28ca5964 100644 --- a/language/move-vm/runtime/Cargo.toml +++ b/language/move-vm/runtime/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -39,3 +39,4 @@ failpoints = ["fail/failpoints"] debugging = [] testing = [] stacktrace = [] +lazy_natives = [] diff --git a/language/move-vm/runtime/src/data_cache.rs b/language/move-vm/runtime/src/data_cache.rs index bbfcb15767..3b41363c4c 100644 --- a/language/move-vm/runtime/src/data_cache.rs +++ b/language/move-vm/runtime/src/data_cache.rs @@ -7,7 +7,8 @@ use crate::loader::Loader; use move_binary_format::errors::*; use move_core_types::{ account_address::AccountAddress, - effects::{AccountChangeSet, ChangeSet, Event}, + effects::{AccountChangeSet, ChangeSet, Event, Op}, + gas_algebra::NumBytes, identifier::Identifier, language_storage::{ModuleId, TypeTag}, resolver::MoveResolver, @@ -17,13 +18,13 @@ use move_core_types::{ use move_vm_types::{ data_store::DataStore, loaded_data::runtime_types::Type, - values::{GlobalValue, GlobalValueEffect, Value}, + values::{GlobalValue, Value}, }; use std::collections::btree_map::BTreeMap; pub struct AccountDataCache { data_map: BTreeMap, - module_map: BTreeMap>, + module_map: BTreeMap, bool)>, } impl AccountDataCache { @@ -36,7 +37,7 @@ impl AccountDataCache { } /// Transaction data cache. Keep updates within a transaction so they can all be published at -/// once when the transaction succeeeds. +/// once when the transaction succeeds. /// /// It also provides an implementation for the opcodes that refer to storage and gives the /// proper guarantees of reference lifetime. @@ -75,37 +76,53 @@ impl<'r, 'l, S: MoveResolver> TransactionDataCache<'r, 'l, S> { let mut change_set = ChangeSet::new(); for (addr, account_data_cache) in self.account_map.into_iter() { let mut modules = BTreeMap::new(); - for (module_name, module_blob) in account_data_cache.module_map { - modules.insert(module_name, Some(module_blob)); + for (module_name, (module_blob, is_republishing)) in account_data_cache.module_map { + let op = if is_republishing { + Op::Modify(module_blob) + } else { + Op::New(module_blob) + }; + modules.insert(module_name, op); } let mut resources = BTreeMap::new(); for (ty, (layout, gv)) in account_data_cache.data_map { - match gv.into_effect()? { - GlobalValueEffect::None => (), - GlobalValueEffect::Deleted => { - let struct_tag = match self.loader.type_to_type_tag(&ty)? { - TypeTag::Struct(struct_tag) => struct_tag, - _ => return Err(PartialVMError::new(StatusCode::INTERNAL_TYPE_ERROR)), - }; - resources.insert(struct_tag, None); + let op = match gv.into_effect() { + Some(op) => op, + None => continue, + }; + + let struct_tag = match self.loader.type_to_type_tag(&ty)? { + TypeTag::Struct(struct_tag) => struct_tag, + _ => return Err(PartialVMError::new(StatusCode::INTERNAL_TYPE_ERROR)), + }; + + match op { + Op::New(val) => { + let resource_blob = val + .simple_serialize(&layout) + .ok_or_else(|| PartialVMError::new(StatusCode::INTERNAL_TYPE_ERROR))?; + resources.insert(struct_tag, Op::New(resource_blob)); } - GlobalValueEffect::Changed(val) => { - let struct_tag = match self.loader.type_to_type_tag(&ty)? { - TypeTag::Struct(struct_tag) => struct_tag, - _ => return Err(PartialVMError::new(StatusCode::INTERNAL_TYPE_ERROR)), - }; + Op::Modify(val) => { let resource_blob = val .simple_serialize(&layout) .ok_or_else(|| PartialVMError::new(StatusCode::INTERNAL_TYPE_ERROR))?; - resources.insert(struct_tag, Some(resource_blob)); + resources.insert(struct_tag, Op::Modify(resource_blob)); + } + Op::Delete => { + resources.insert(struct_tag, Op::Delete); } } } - change_set.publish_or_overwrite_account_change_set( - addr, - AccountChangeSet::from_modules_resources(modules, resources), - ); + if !modules.is_empty() || !resources.is_empty() { + change_set + .add_account_changeset( + addr, + AccountChangeSet::from_modules_resources(modules, resources), + ) + .expect("accounts should be unique"); + } } let mut events = vec![]; @@ -153,11 +170,12 @@ impl<'r, 'l, S: MoveResolver> DataStore for TransactionDataCache<'r, 'l, S> { &mut self, addr: AccountAddress, ty: &Type, - ) -> PartialVMResult<&mut GlobalValue> { + ) -> PartialVMResult<(&mut GlobalValue, Option>)> { let account_cache = Self::get_mut_or_insert_with(&mut self.account_map, &addr, || { (addr, AccountDataCache::new()) }); + let mut load_res = None; if !account_cache.data_map.contains_key(ty) { let ty_tag = match self.loader.type_to_type_tag(ty)? { TypeTag::Struct(s_tag) => s_tag, @@ -167,10 +185,12 @@ impl<'r, 'l, S: MoveResolver> DataStore for TransactionDataCache<'r, 'l, S> { return Err(PartialVMError::new(StatusCode::INTERNAL_TYPE_ERROR)) } }; + // TODO(Gas): Shall we charge for this? let ty_layout = self.loader.type_to_type_layout(ty)?; let gv = match self.remote.get_resource(&addr, &ty_tag) { Ok(Some(blob)) => { + load_res = Some(Some(NumBytes::new(blob.len() as u64))); let val = match Value::simple_deserialize(&blob, &ty_layout) { Some(val) => val, None => { @@ -185,7 +205,10 @@ impl<'r, 'l, S: MoveResolver> DataStore for TransactionDataCache<'r, 'l, S> { GlobalValue::cached(val)? } - Ok(None) => GlobalValue::none(), + Ok(None) => { + load_res = Some(None); + GlobalValue::none() + } Err(err) => { let msg = format!("Unexpected storage error: {:?}", err); return Err( @@ -198,16 +221,19 @@ impl<'r, 'l, S: MoveResolver> DataStore for TransactionDataCache<'r, 'l, S> { account_cache.data_map.insert(ty.clone(), (ty_layout, gv)); } - Ok(account_cache - .data_map - .get_mut(ty) - .map(|(_ty_layout, gv)| gv) - .expect("global value must exist")) + Ok(( + account_cache + .data_map + .get_mut(ty) + .map(|(_ty_layout, gv)| gv) + .expect("global value must exist"), + load_res, + )) } fn load_module(&self, module_id: &ModuleId) -> VMResult> { if let Some(account_cache) = self.account_map.get(module_id.address()) { - if let Some(blob) = account_cache.module_map.get(module_id.name()) { + if let Some((blob, _is_republishing)) = account_cache.module_map.get(module_id.name()) { return Ok(blob.clone()); } } @@ -227,7 +253,12 @@ impl<'r, 'l, S: MoveResolver> DataStore for TransactionDataCache<'r, 'l, S> { } } - fn publish_module(&mut self, module_id: &ModuleId, blob: Vec) -> VMResult<()> { + fn publish_module( + &mut self, + module_id: &ModuleId, + blob: Vec, + is_republishing: bool, + ) -> VMResult<()> { let account_cache = Self::get_mut_or_insert_with(&mut self.account_map, module_id.address(), || { (*module_id.address(), AccountDataCache::new()) @@ -235,7 +266,7 @@ impl<'r, 'l, S: MoveResolver> DataStore for TransactionDataCache<'r, 'l, S> { account_cache .module_map - .insert(module_id.name().to_owned(), blob); + .insert(module_id.name().to_owned(), (blob, is_republishing)); Ok(()) } diff --git a/language/move-vm/runtime/src/interpreter.rs b/language/move-vm/runtime/src/interpreter.rs index d79722992d..cb4c0b4653 100644 --- a/language/move-vm/runtime/src/interpreter.rs +++ b/language/move-vm/runtime/src/interpreter.rs @@ -11,25 +11,26 @@ use fail::fail_point; use move_binary_format::{ errors::*, file_format::{Bytecode, FunctionHandleIndex, FunctionInstantiationIndex}, - file_format_common::Opcodes, }; use move_core_types::{ account_address::AccountAddress, - gas_schedule::{AbstractMemorySize, GasAlgebra, GasCarrier}, + gas_algebra::{NumArgs, NumBytes}, + language_storage::TypeTag, vm_status::{StatusCode, StatusType}, }; use move_vm_types::{ data_store::DataStore, - gas_schedule::GasStatus, + gas::{GasMeter, SimpleInstruction}, loaded_data::runtime_types::Type, values::{ self, GlobalValue, IntegerValue, Locals, Reference, Struct, StructRef, VMValueCast, Value, Vector, VectorRef, }, + views::TypeView, }; use crate::native_extensions::NativeContextExtensions; -use std::{cmp::min, collections::VecDeque, fmt::Write, mem, sync::Arc}; +use std::{cmp::min, collections::VecDeque, fmt::Write, sync::Arc}; use tracing::error; macro_rules! debug_write { @@ -68,6 +69,17 @@ pub(crate) struct Interpreter { call_stack: CallStack, } +struct TypeWithLoader<'a, 'b> { + ty: &'a Type, + loader: &'b Loader, +} + +impl<'a, 'b> TypeView for TypeWithLoader<'a, 'b> { + fn to_type_tag(&self) -> TypeTag { + self.loader.type_to_type_tag(self.ty).unwrap() + } +} + impl Interpreter { /// Entrypoint into the interpreter. All external calls need to be routed through this /// function. @@ -76,42 +88,16 @@ impl Interpreter { ty_args: Vec, args: Vec, data_store: &mut impl DataStore, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, extensions: &mut NativeContextExtensions, loader: &Loader, ) -> VMResult> { - // We count the intrinsic cost of the transaction here, since that needs to also cover the - // setup of the function. - let mut interp = Self::new(); - interp.execute( - loader, data_store, gas_status, extensions, function, ty_args, args, - ) - } - - /// Create a new instance of an `Interpreter` in the context of a transaction with a - /// given module cache and gas schedule. - fn new() -> Self { Interpreter { operand_stack: Stack::new(), call_stack: CallStack::new(), } - } - - /// Internal execution entry point. - fn execute( - &mut self, - loader: &Loader, - data_store: &mut impl DataStore, - gas_status: &mut GasStatus, - extensions: &mut NativeContextExtensions, - function: Arc, - ty_args: Vec, - args: Vec, - ) -> VMResult> { - // No unwinding of the call stack and value stack need to be done here -- the context will - // take care of that. - self.execute_main( - loader, data_store, gas_status, extensions, function, ty_args, args, + .execute_main( + loader, data_store, gas_meter, extensions, function, ty_args, args, ) } @@ -121,13 +107,11 @@ impl Interpreter { /// function represented by the frame. Control comes back to this function on return or /// on call. When that happens the frame is changes to a new one (call) or to the one /// at the top of the stack (return). If the call stack is empty execution is completed. - // REVIEW: create account will be removed in favor of a native function (no opcode) and - // we can simplify this code quite a bit. fn execute_main( - &mut self, + mut self, loader: &Loader, data_store: &mut impl DataStore, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, extensions: &mut NativeContextExtensions, function: Arc, ty_args: Vec, @@ -143,34 +127,47 @@ impl Interpreter { let mut current_frame = Frame::new(function, ty_args, locals); loop { let resolver = current_frame.resolver(loader); - let exit_code = current_frame //self - .execute_code(&resolver, self, data_store, gas_status) - .map_err(|err| self.maybe_core_dump(err, ¤t_frame))?; + let exit_code = + current_frame //self + .execute_code(&resolver, &mut self, data_store, gas_meter) + .map_err(|err| self.maybe_core_dump(err, ¤t_frame))?; match exit_code { ExitCode::Return => { if let Some(frame) = self.call_stack.pop() { + // Note: the caller will find the callee's return values at the top of the shared operand stack current_frame = frame; current_frame.pc += 1; // advance past the Call instruction in the caller } else { - return Ok(mem::take(&mut self.operand_stack.0)); + // end of execution. `self` should no longer be used afterward + return Ok(self.operand_stack.0); } } ExitCode::Call(fh_idx) => { - gas_status - .charge_instr_with_size(Opcodes::CALL, AbstractMemorySize::new(1)) - .map_err(|e| set_err_info!(current_frame, e))?; let func = resolver.function_from_handle(fh_idx); - gas_status - .charge_instr_with_size( - Opcodes::CALL, - AbstractMemorySize::new(func.arg_count() as GasCarrier), + + // Charge gas + let module_id = func + .module_id() + .ok_or_else(|| { + PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR) + .with_message("Failed to get native function module id".to_string()) + }) + .map_err(|e| set_err_info!(current_frame, e))?; + gas_meter + .charge_call( + module_id, + func.name(), + self.operand_stack + .last_n(func.arg_count()) + .map_err(|e| set_err_info!(current_frame, e))?, ) .map_err(|e| set_err_info!(current_frame, e))?; + if func.is_native() { self.call_native( &resolver, data_store, - gas_status, + gas_meter, extensions, func, vec![], @@ -186,29 +183,38 @@ impl Interpreter { let err = set_err_info!(frame, err); self.maybe_core_dump(err, &frame) })?; + // Note: the caller will find the the callee's return values at the top of the shared operand stack current_frame = frame; } ExitCode::CallGeneric(idx) => { - let arity = resolver.type_params_count(idx); - gas_status - .charge_instr_with_size( - Opcodes::CALL_GENERIC, - AbstractMemorySize::new((arity + 1) as GasCarrier), - ) - .map_err(|e| set_err_info!(current_frame, e))?; + // TODO(Gas): We should charge gas as we do type substitution... let ty_args = resolver .instantiate_generic_function(idx, current_frame.ty_args()) .map_err(|e| set_err_info!(current_frame, e))?; let func = resolver.function_from_instantiation(idx); - gas_status - .charge_instr_with_size( - Opcodes::CALL_GENERIC, - AbstractMemorySize::new(func.arg_count() as GasCarrier), + + // Charge gas + let module_id = func + .module_id() + .ok_or_else(|| { + PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR) + .with_message("Failed to get native function module id".to_string()) + }) + .map_err(|e| set_err_info!(current_frame, e))?; + gas_meter + .charge_call_generic( + module_id, + func.name(), + ty_args.iter().map(|ty| TypeWithLoader { ty, loader }), + self.operand_stack + .last_n(func.arg_count()) + .map_err(|e| set_err_info!(current_frame, e))?, ) .map_err(|e| set_err_info!(current_frame, e))?; + if func.is_native() { self.call_native( - &resolver, data_store, gas_status, extensions, func, ty_args, + &resolver, data_store, gas_meter, extensions, func, ty_args, )?; current_frame.pc += 1; // advance past the Call instruction in the caller continue; @@ -251,7 +257,7 @@ impl Interpreter { &mut self, resolver: &Resolver, data_store: &mut dyn DataStore, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, extensions: &mut NativeContextExtensions, function: Arc, ty_args: Vec, @@ -260,7 +266,7 @@ impl Interpreter { self.call_native_impl( resolver, data_store, - gas_status, + gas_meter, extensions, function.clone(), ty_args, @@ -281,24 +287,39 @@ impl Interpreter { &mut self, resolver: &Resolver, data_store: &mut dyn DataStore, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, extensions: &mut NativeContextExtensions, function: Arc, ty_args: Vec, ) -> PartialVMResult<()> { - let mut arguments = VecDeque::new(); + let return_type_count = function.return_type_count(); + let mut args = VecDeque::new(); let expected_args = function.arg_count(); for _ in 0..expected_args { - arguments.push_front(self.operand_stack.pop()?); + args.push_front(self.operand_stack.pop()?); } - let mut native_context = - NativeContext::new(self, data_store, gas_status, resolver, extensions); + let mut native_context = NativeContext::new(self, data_store, resolver, extensions); let native_function = function.get_native()?; - let result = native_function(&mut native_context, ty_args, arguments)?; - gas_status.deduct_gas(result.cost)?; + + let result = native_function(&mut native_context, ty_args, args)?; + gas_meter.charge_native_function(result.cost)?; + let return_values = result .result .map_err(|code| PartialVMError::new(StatusCode::ABORTED).with_sub_status(code))?; + // Paranoid check to protect us against incorrect native function implementations. A native function that + // returns a different number of values than its declared types will trigger this check + if return_values.len() != return_type_count { + return Err( + PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR).with_message( + "Arity mismatch: return value count does not match return type count" + .to_string(), + ), + ); + } + // Put return values on the top of the operand stack, where the caller will find them. + // This is one of only two times the operand stack is shared across call stack frames; the other is in handling + // the Return instruction for normal calls for value in return_values { self.operand_stack.push(value)?; } @@ -340,14 +361,20 @@ impl Interpreter { self.binop(|lhs, rhs| Ok(Value::bool(f(lhs, rhs)?))) } - /// Load a resource from the data store. + /// Loads a resource from the data store and return the number of bytes read from the storage. fn load_resource<'b>( + gas_meter: &mut impl GasMeter, data_store: &'b mut impl DataStore, addr: AccountAddress, ty: &Type, ) -> PartialVMResult<&'b mut GlobalValue> { match data_store.load_resource(addr, ty) { - Ok(gv) => Ok(gv), + Ok((gv, load_res)) => { + if let Some(loaded) = load_res { + gas_meter.charge_load_resource(loaded)?; + } + Ok(gv) + } Err(e) => { error!( "[VM] error loading resource at ({}, {:?}): {:?} from data store", @@ -361,54 +388,105 @@ impl Interpreter { /// BorrowGlobal (mutable and not) opcode. fn borrow_global( &mut self, + is_mut: bool, + is_generic: bool, + loader: &Loader, + gas_meter: &mut impl GasMeter, data_store: &mut impl DataStore, addr: AccountAddress, ty: &Type, - ) -> PartialVMResult> { - let g = Self::load_resource(data_store, addr, ty)?.borrow_global()?; - let size = g.size(); - self.operand_stack.push(g)?; - Ok(size) + ) -> PartialVMResult<()> { + let res = Self::load_resource(gas_meter, data_store, addr, ty)?.borrow_global(); + gas_meter.charge_borrow_global( + is_mut, + is_generic, + TypeWithLoader { ty, loader }, + res.is_ok(), + )?; + self.operand_stack.push(res?)?; + Ok(()) } /// Exists opcode. fn exists( &mut self, + is_generic: bool, + loader: &Loader, + gas_meter: &mut impl GasMeter, data_store: &mut impl DataStore, addr: AccountAddress, ty: &Type, - ) -> PartialVMResult> { - let gv = Self::load_resource(data_store, addr, ty)?; - let mem_size = gv.size(); + ) -> PartialVMResult<()> { + let gv = Self::load_resource(gas_meter, data_store, addr, ty)?; let exists = gv.exists()?; + gas_meter.charge_exists(is_generic, TypeWithLoader { ty, loader }, exists)?; self.operand_stack.push(Value::bool(exists))?; - Ok(mem_size) + Ok(()) } /// MoveFrom opcode. fn move_from( &mut self, + is_generic: bool, + loader: &Loader, + gas_meter: &mut impl GasMeter, data_store: &mut impl DataStore, addr: AccountAddress, ty: &Type, - ) -> PartialVMResult> { - let resource = Self::load_resource(data_store, addr, ty)?.move_from()?; - let size = resource.size(); + ) -> PartialVMResult<()> { + let resource = match Self::load_resource(gas_meter, data_store, addr, ty)?.move_from() { + Ok(resource) => { + gas_meter.charge_move_from( + is_generic, + TypeWithLoader { ty, loader }, + Some(&resource), + )?; + resource + } + Err(err) => { + let val: Option<&Value> = None; + gas_meter.charge_move_from(is_generic, TypeWithLoader { ty, loader }, val)?; + return Err(err); + } + }; self.operand_stack.push(resource)?; - Ok(size) + Ok(()) } /// MoveTo opcode. fn move_to( &mut self, + is_generic: bool, + loader: &Loader, + gas_meter: &mut impl GasMeter, data_store: &mut impl DataStore, addr: AccountAddress, ty: &Type, resource: Value, - ) -> PartialVMResult> { - let size = resource.size(); - Self::load_resource(data_store, addr, ty)?.move_to(resource)?; - Ok(size) + ) -> PartialVMResult<()> { + let gv = Self::load_resource(gas_meter, data_store, addr, ty)?; + // NOTE(Gas): To maintain backward compatibility, we need to charge gas after attempting + // the move_to operation. + match gv.move_to(resource) { + Ok(()) => { + gas_meter.charge_move_to( + is_generic, + TypeWithLoader { ty, loader }, + gv.view().unwrap(), + true, + )?; + Ok(()) + } + Err((err, resource)) => { + gas_meter.charge_move_to( + is_generic, + TypeWithLoader { ty, loader }, + &resource, + false, + )?; + Err(err) + } + } } // @@ -650,6 +728,14 @@ impl Stack { let args = self.0.split_off(remaining_stack_size); Ok(args) } + + fn last_n(&self, n: usize) -> PartialVMResult> { + if self.0.len() < n { + return Err(PartialVMError::new(StatusCode::EMPTY_VALUE_STACK) + .with_message("Failed to get last n arguments on the argument stack".to_string())); + } + Ok(self.0[(self.0.len() - n)..].iter()) + } } /// A call stack. @@ -720,9 +806,9 @@ impl Frame { resolver: &Resolver, interpreter: &mut Interpreter, data_store: &mut impl DataStore, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, ) -> VMResult { - self.execute_code_impl(resolver, interpreter, data_store, gas_status) + self.execute_code_impl(resolver, interpreter, data_store, gas_meter) .map_err(|e| { e.at_code_offset(self.function.index(), self.pc) .finish(self.location()) @@ -734,8 +820,19 @@ impl Frame { resolver: &Resolver, interpreter: &mut Interpreter, data_store: &mut impl DataStore, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, ) -> PartialVMResult { + use SimpleInstruction as S; + + macro_rules! make_ty { + ($ty: expr) => { + TypeWithLoader { + ty: $ty, + loader: resolver.loader(), + } + }; + } + let code = self.function.code(); loop { for instruction in &code[self.pc as usize..] { @@ -758,50 +855,47 @@ impl Frame { match instruction { Bytecode::Pop => { - gas_status.charge_instr(Opcodes::POP)?; + gas_meter.charge_simple_instr(S::Pop)?; interpreter.operand_stack.pop()?; } Bytecode::Ret => { - gas_status.charge_instr(Opcodes::RET)?; + gas_meter.charge_simple_instr(S::Ret)?; return Ok(ExitCode::Return); } Bytecode::BrTrue(offset) => { - gas_status.charge_instr(Opcodes::BR_TRUE)?; + gas_meter.charge_simple_instr(S::BrTrue)?; if interpreter.operand_stack.pop_as::()? { self.pc = *offset; break; } } Bytecode::BrFalse(offset) => { - gas_status.charge_instr(Opcodes::BR_FALSE)?; + gas_meter.charge_simple_instr(S::BrFalse)?; if !interpreter.operand_stack.pop_as::()? { self.pc = *offset; break; } } Bytecode::Branch(offset) => { - gas_status.charge_instr(Opcodes::BRANCH)?; + gas_meter.charge_simple_instr(S::Branch)?; self.pc = *offset; break; } Bytecode::LdU8(int_const) => { - gas_status.charge_instr(Opcodes::LD_U8)?; + gas_meter.charge_simple_instr(S::LdU8)?; interpreter.operand_stack.push(Value::u8(*int_const))?; } Bytecode::LdU64(int_const) => { - gas_status.charge_instr(Opcodes::LD_U64)?; + gas_meter.charge_simple_instr(S::LdU64)?; interpreter.operand_stack.push(Value::u64(*int_const))?; } Bytecode::LdU128(int_const) => { - gas_status.charge_instr(Opcodes::LD_U128)?; + gas_meter.charge_simple_instr(S::LdU128)?; interpreter.operand_stack.push(Value::u128(*int_const))?; } Bytecode::LdConst(idx) => { let constant = resolver.constant_at(*idx); - gas_status.charge_instr_with_size( - Opcodes::LD_CONST, - AbstractMemorySize::new(constant.data.len() as GasCarrier), - )?; + gas_meter.charge_ld_const(NumBytes::new(constant.data.len() as u64))?; interpreter.operand_stack.push( Value::deserialize_constant(constant).ok_or_else(|| { PartialVMError::new(StatusCode::VERIFIER_INVARIANT_VIOLATION) @@ -813,28 +907,28 @@ impl Frame { )? } Bytecode::LdTrue => { - gas_status.charge_instr(Opcodes::LD_TRUE)?; + gas_meter.charge_simple_instr(S::LdTrue)?; interpreter.operand_stack.push(Value::bool(true))?; } Bytecode::LdFalse => { - gas_status.charge_instr(Opcodes::LD_FALSE)?; + gas_meter.charge_simple_instr(S::LdFalse)?; interpreter.operand_stack.push(Value::bool(false))?; } Bytecode::CopyLoc(idx) => { + // TODO(Gas): We should charge gas before copying the value. let local = self.locals.copy_loc(*idx as usize)?; - gas_status.charge_instr_with_size(Opcodes::COPY_LOC, local.size())?; + gas_meter.charge_copy_loc(&local)?; interpreter.operand_stack.push(local)?; } Bytecode::MoveLoc(idx) => { let local = self.locals.move_loc(*idx as usize)?; - gas_status.charge_instr_with_size(Opcodes::MOVE_LOC, local.size())?; + gas_meter.charge_move_loc(&local)?; interpreter.operand_stack.push(local)?; } Bytecode::StLoc(idx) => { let value_to_store = interpreter.operand_stack.pop()?; - gas_status - .charge_instr_with_size(Opcodes::ST_LOC, value_to_store.size())?; + gas_meter.charge_store_loc(&value_to_store)?; self.locals.store_loc(*idx as usize, value_to_store)?; } Bytecode::Call(idx) => { @@ -844,21 +938,21 @@ impl Frame { return Ok(ExitCode::CallGeneric(*idx)); } Bytecode::MutBorrowLoc(idx) | Bytecode::ImmBorrowLoc(idx) => { - let opcode = match instruction { - Bytecode::MutBorrowLoc(_) => Opcodes::MUT_BORROW_LOC, - _ => Opcodes::IMM_BORROW_LOC, + let instr = match instruction { + Bytecode::MutBorrowLoc(_) => S::MutBorrowLoc, + _ => S::ImmBorrowLoc, }; - gas_status.charge_instr(opcode)?; + gas_meter.charge_simple_instr(instr)?; interpreter .operand_stack .push(self.locals.borrow_loc(*idx as usize)?)?; } Bytecode::ImmBorrowField(fh_idx) | Bytecode::MutBorrowField(fh_idx) => { - let opcode = match instruction { - Bytecode::MutBorrowField(_) => Opcodes::MUT_BORROW_FIELD, - _ => Opcodes::IMM_BORROW_FIELD, + let instr = match instruction { + Bytecode::MutBorrowField(_) => S::MutBorrowField, + _ => S::ImmBorrowField, }; - gas_status.charge_instr(opcode)?; + gas_meter.charge_simple_instr(instr)?; let reference = interpreter.operand_stack.pop_as::()?; let offset = resolver.field_offset(*fh_idx); @@ -867,11 +961,11 @@ impl Frame { } Bytecode::ImmBorrowFieldGeneric(fi_idx) | Bytecode::MutBorrowFieldGeneric(fi_idx) => { - let opcode = match instruction { - Bytecode::MutBorrowField(_) => Opcodes::MUT_BORROW_FIELD_GENERIC, - _ => Opcodes::IMM_BORROW_FIELD_GENERIC, + let instr = match instruction { + Bytecode::MutBorrowField(_) => S::MutBorrowFieldGeneric, + _ => S::ImmBorrowFieldGeneric, }; - gas_status.charge_instr(opcode)?; + gas_meter.charge_simple_instr(instr)?; let reference = interpreter.operand_stack.pop_as::()?; let offset = resolver.field_instantiation_offset(*fi_idx); @@ -880,87 +974,75 @@ impl Frame { } Bytecode::Pack(sd_idx) => { let field_count = resolver.field_count(*sd_idx); + gas_meter.charge_pack( + false, + interpreter.operand_stack.last_n(field_count as usize)?, + )?; let args = interpreter.operand_stack.popn(field_count)?; - let size = args.iter().fold( - AbstractMemorySize::new(GasCarrier::from(field_count)), - |acc, v| acc.add(v.size()), - ); - gas_status.charge_instr_with_size(Opcodes::PACK, size)?; interpreter .operand_stack .push(Value::struct_(Struct::pack(args)))?; } Bytecode::PackGeneric(si_idx) => { let field_count = resolver.field_instantiation_count(*si_idx); + gas_meter.charge_pack( + true, + interpreter.operand_stack.last_n(field_count as usize)?, + )?; let args = interpreter.operand_stack.popn(field_count)?; - let size = args.iter().fold( - AbstractMemorySize::new(GasCarrier::from(field_count)), - |acc, v| acc.add(v.size()), - ); - gas_status.charge_instr_with_size(Opcodes::PACK_GENERIC, size)?; interpreter .operand_stack .push(Value::struct_(Struct::pack(args)))?; } - Bytecode::Unpack(sd_idx) => { - let field_count = resolver.field_count(*sd_idx); + Bytecode::Unpack(_sd_idx) => { let struct_ = interpreter.operand_stack.pop_as::()?; - gas_status.charge_instr_with_size( - Opcodes::UNPACK, - AbstractMemorySize::new(GasCarrier::from(field_count)), - )?; - // TODO: Whether or not we want this gas metering in the loop is - // questionable. However, if we don't have it in the loop we could wind up - // doing a fair bit of work before charging for it. + + gas_meter.charge_unpack(false, struct_.field_views())?; + for value in struct_.unpack()? { - gas_status.charge_instr_with_size(Opcodes::UNPACK, value.size())?; interpreter.operand_stack.push(value)?; } } - Bytecode::UnpackGeneric(si_idx) => { - let field_count = resolver.field_instantiation_count(*si_idx); + Bytecode::UnpackGeneric(_si_idx) => { let struct_ = interpreter.operand_stack.pop_as::()?; - gas_status.charge_instr_with_size( - Opcodes::UNPACK_GENERIC, - AbstractMemorySize::new(GasCarrier::from(field_count)), - )?; + + gas_meter.charge_unpack(true, struct_.field_views())?; + // TODO: Whether or not we want this gas metering in the loop is // questionable. However, if we don't have it in the loop we could wind up // doing a fair bit of work before charging for it. for value in struct_.unpack()? { - gas_status - .charge_instr_with_size(Opcodes::UNPACK_GENERIC, value.size())?; interpreter.operand_stack.push(value)?; } } Bytecode::ReadRef => { let reference = interpreter.operand_stack.pop_as::()?; + gas_meter.charge_read_ref(reference.value_view())?; let value = reference.read_ref()?; - gas_status.charge_instr_with_size(Opcodes::READ_REF, value.size())?; interpreter.operand_stack.push(value)?; } Bytecode::WriteRef => { let reference = interpreter.operand_stack.pop_as::()?; let value = interpreter.operand_stack.pop()?; - gas_status.charge_instr_with_size(Opcodes::WRITE_REF, value.size())?; + gas_meter.charge_write_ref(&value)?; reference.write_ref(value)?; } Bytecode::CastU8 => { - gas_status.charge_instr(Opcodes::CAST_U8)?; + gas_meter.charge_simple_instr(S::CastU8)?; let integer_value = interpreter.operand_stack.pop_as::()?; interpreter .operand_stack .push(Value::u8(integer_value.cast_u8()?))?; } Bytecode::CastU64 => { - gas_status.charge_instr(Opcodes::CAST_U64)?; + gas_meter.charge_simple_instr(S::CastU64)?; let integer_value = interpreter.operand_stack.pop_as::()?; interpreter .operand_stack .push(Value::u64(integer_value.cast_u64()?))?; } Bytecode::CastU128 => { - gas_status.charge_instr(Opcodes::CAST_U128)?; + gas_meter.charge_simple_instr(S::CastU128)?; let integer_value = interpreter.operand_stack.pop_as::()?; interpreter .operand_stack @@ -968,39 +1050,39 @@ impl Frame { } // Arithmetic Operations Bytecode::Add => { - gas_status.charge_instr(Opcodes::ADD)?; + gas_meter.charge_simple_instr(S::Add)?; interpreter.binop_int(IntegerValue::add_checked)? } Bytecode::Sub => { - gas_status.charge_instr(Opcodes::SUB)?; + gas_meter.charge_simple_instr(S::Sub)?; interpreter.binop_int(IntegerValue::sub_checked)? } Bytecode::Mul => { - gas_status.charge_instr(Opcodes::MUL)?; + gas_meter.charge_simple_instr(S::Mul)?; interpreter.binop_int(IntegerValue::mul_checked)? } Bytecode::Mod => { - gas_status.charge_instr(Opcodes::MOD)?; + gas_meter.charge_simple_instr(S::Mod)?; interpreter.binop_int(IntegerValue::rem_checked)? } Bytecode::Div => { - gas_status.charge_instr(Opcodes::DIV)?; + gas_meter.charge_simple_instr(S::Div)?; interpreter.binop_int(IntegerValue::div_checked)? } Bytecode::BitOr => { - gas_status.charge_instr(Opcodes::BIT_OR)?; + gas_meter.charge_simple_instr(S::BitOr)?; interpreter.binop_int(IntegerValue::bit_or)? } Bytecode::BitAnd => { - gas_status.charge_instr(Opcodes::BIT_AND)?; + gas_meter.charge_simple_instr(S::BitAnd)?; interpreter.binop_int(IntegerValue::bit_and)? } Bytecode::Xor => { - gas_status.charge_instr(Opcodes::XOR)?; + gas_meter.charge_simple_instr(S::Xor)?; interpreter.binop_int(IntegerValue::bit_xor)? } Bytecode::Shl => { - gas_status.charge_instr(Opcodes::SHL)?; + gas_meter.charge_simple_instr(S::Shl)?; let rhs = interpreter.operand_stack.pop_as::()?; let lhs = interpreter.operand_stack.pop_as::()?; interpreter @@ -1008,7 +1090,7 @@ impl Frame { .push(lhs.shl_checked(rhs)?.into_value())?; } Bytecode::Shr => { - gas_status.charge_instr(Opcodes::SHR)?; + gas_meter.charge_simple_instr(S::Shr)?; let rhs = interpreter.operand_stack.pop_as::()?; let lhs = interpreter.operand_stack.pop_as::()?; interpreter @@ -1016,31 +1098,31 @@ impl Frame { .push(lhs.shr_checked(rhs)?.into_value())?; } Bytecode::Or => { - gas_status.charge_instr(Opcodes::OR)?; + gas_meter.charge_simple_instr(S::Or)?; interpreter.binop_bool(|l, r| Ok(l || r))? } Bytecode::And => { - gas_status.charge_instr(Opcodes::AND)?; + gas_meter.charge_simple_instr(S::And)?; interpreter.binop_bool(|l, r| Ok(l && r))? } Bytecode::Lt => { - gas_status.charge_instr(Opcodes::LT)?; + gas_meter.charge_simple_instr(S::Lt)?; interpreter.binop_bool(IntegerValue::lt)? } Bytecode::Gt => { - gas_status.charge_instr(Opcodes::GT)?; + gas_meter.charge_simple_instr(S::Gt)?; interpreter.binop_bool(IntegerValue::gt)? } Bytecode::Le => { - gas_status.charge_instr(Opcodes::LE)?; + gas_meter.charge_simple_instr(S::Le)?; interpreter.binop_bool(IntegerValue::le)? } Bytecode::Ge => { - gas_status.charge_instr(Opcodes::GE)?; + gas_meter.charge_simple_instr(S::Ge)?; interpreter.binop_bool(IntegerValue::ge)? } Bytecode::Abort => { - gas_status.charge_instr(Opcodes::ABORT)?; + gas_meter.charge_simple_instr(S::Abort)?; let error_code = interpreter.operand_stack.pop_as::()?; let error = PartialVMError::new(StatusCode::ABORTED) .with_sub_status(error_code) @@ -1058,8 +1140,7 @@ impl Frame { Bytecode::Eq => { let lhs = interpreter.operand_stack.pop()?; let rhs = interpreter.operand_stack.pop()?; - gas_status - .charge_instr_with_size(Opcodes::EQ, lhs.size().add(rhs.size()))?; + gas_meter.charge_eq(&lhs, &rhs)?; interpreter .operand_stack .push(Value::bool(lhs.equals(&rhs)?))?; @@ -1067,53 +1148,87 @@ impl Frame { Bytecode::Neq => { let lhs = interpreter.operand_stack.pop()?; let rhs = interpreter.operand_stack.pop()?; - gas_status - .charge_instr_with_size(Opcodes::NEQ, lhs.size().add(rhs.size()))?; + gas_meter.charge_neq(&lhs, &rhs)?; interpreter .operand_stack .push(Value::bool(!lhs.equals(&rhs)?))?; } Bytecode::MutBorrowGlobal(sd_idx) | Bytecode::ImmBorrowGlobal(sd_idx) => { + let is_mut = matches!(instruction, Bytecode::MutBorrowGlobal(_)); let addr = interpreter.operand_stack.pop_as::()?; let ty = resolver.get_struct_type(*sd_idx); - let size = interpreter.borrow_global(data_store, addr, &ty)?; - gas_status.charge_instr_with_size(Opcodes::MUT_BORROW_GLOBAL, size)?; + interpreter.borrow_global( + is_mut, + false, + resolver.loader(), + gas_meter, + data_store, + addr, + &ty, + )?; } Bytecode::MutBorrowGlobalGeneric(si_idx) | Bytecode::ImmBorrowGlobalGeneric(si_idx) => { + let is_mut = matches!(instruction, Bytecode::MutBorrowGlobalGeneric(_)); let addr = interpreter.operand_stack.pop_as::()?; let ty = resolver.instantiate_generic_type(*si_idx, self.ty_args())?; - let size = interpreter.borrow_global(data_store, addr, &ty)?; - gas_status - .charge_instr_with_size(Opcodes::MUT_BORROW_GLOBAL_GENERIC, size)?; + interpreter.borrow_global( + is_mut, + true, + resolver.loader(), + gas_meter, + data_store, + addr, + &ty, + )?; } Bytecode::Exists(sd_idx) => { let addr = interpreter.operand_stack.pop_as::()?; let ty = resolver.get_struct_type(*sd_idx); - let size = interpreter.exists(data_store, addr, &ty)?; - gas_status.charge_instr_with_size(Opcodes::EXISTS, size)?; + interpreter.exists( + false, + resolver.loader(), + gas_meter, + data_store, + addr, + &ty, + )?; } Bytecode::ExistsGeneric(si_idx) => { let addr = interpreter.operand_stack.pop_as::()?; let ty = resolver.instantiate_generic_type(*si_idx, self.ty_args())?; - let size = interpreter.exists(data_store, addr, &ty)?; - gas_status.charge_instr_with_size(Opcodes::EXISTS_GENERIC, size)?; + interpreter.exists( + true, + resolver.loader(), + gas_meter, + data_store, + addr, + &ty, + )?; } Bytecode::MoveFrom(sd_idx) => { let addr = interpreter.operand_stack.pop_as::()?; let ty = resolver.get_struct_type(*sd_idx); - let size = interpreter.move_from(data_store, addr, &ty)?; - // TODO: Have this calculate before pulling in the data based upon - // the size of the data that we are about to read in. - gas_status.charge_instr_with_size(Opcodes::MOVE_FROM, size)?; + interpreter.move_from( + false, + resolver.loader(), + gas_meter, + data_store, + addr, + &ty, + )?; } Bytecode::MoveFromGeneric(si_idx) => { let addr = interpreter.operand_stack.pop_as::()?; let ty = resolver.instantiate_generic_type(*si_idx, self.ty_args())?; - let size = interpreter.move_from(data_store, addr, &ty)?; - // TODO: Have this calculate before pulling in the data based upon - // the size of the data that we are about to read in. - gas_status.charge_instr_with_size(Opcodes::MOVE_FROM_GENERIC, size)?; + interpreter.move_from( + true, + resolver.loader(), + gas_meter, + data_store, + addr, + &ty, + )?; } Bytecode::MoveTo(sd_idx) => { let resource = interpreter.operand_stack.pop()?; @@ -1125,8 +1240,15 @@ impl Frame { .value_as::()?; let ty = resolver.get_struct_type(*sd_idx); // REVIEW: Can we simplify Interpreter::move_to? - let size = interpreter.move_to(data_store, addr, &ty, resource)?; - gas_status.charge_instr_with_size(Opcodes::MOVE_TO, size)?; + interpreter.move_to( + false, + resolver.loader(), + gas_meter, + data_store, + addr, + &ty, + resource, + )?; } Bytecode::MoveToGeneric(si_idx) => { let resource = interpreter.operand_stack.pop()?; @@ -1137,75 +1259,84 @@ impl Frame { .read_ref()? .value_as::()?; let ty = resolver.instantiate_generic_type(*si_idx, self.ty_args())?; - let size = interpreter.move_to(data_store, addr, &ty, resource)?; - gas_status.charge_instr_with_size(Opcodes::MOVE_TO_GENERIC, size)?; + interpreter.move_to( + true, + resolver.loader(), + gas_meter, + data_store, + addr, + &ty, + resource, + )?; } Bytecode::FreezeRef => { - gas_status.charge_instr(Opcodes::FREEZE_REF)?; + gas_meter.charge_simple_instr(S::FreezeRef)?; // FreezeRef should just be a null op as we don't distinguish between mut // and immut ref at runtime. } Bytecode::Not => { - gas_status.charge_instr(Opcodes::NOT)?; + gas_meter.charge_simple_instr(S::Not)?; let value = !interpreter.operand_stack.pop_as::()?; interpreter.operand_stack.push(Value::bool(value))?; } Bytecode::Nop => { - gas_status.charge_instr(Opcodes::NOP)?; + gas_meter.charge_simple_instr(S::Nop)?; } Bytecode::VecPack(si, num) => { - let elements = interpreter.operand_stack.popn(*num as u16)?; - let size = AbstractMemorySize::new(*num); - gas_status.charge_instr_with_size(Opcodes::VEC_PACK, size)?; - let value = Vector::pack( - &resolver.instantiate_single_type(*si, self.ty_args())?, - elements, + let ty = resolver.instantiate_single_type(*si, self.ty_args())?; + gas_meter.charge_vec_pack( + make_ty!(&ty), + interpreter.operand_stack.last_n(*num as usize)?, )?; + let elements = interpreter.operand_stack.popn(*num as u16)?; + let value = Vector::pack(&ty, elements)?; interpreter.operand_stack.push(value)?; } Bytecode::VecLen(si) => { let vec_ref = interpreter.operand_stack.pop_as::()?; - gas_status.charge_instr(Opcodes::VEC_LEN)?; - let vec_ty_arg = &resolver.instantiate_single_type(*si, self.ty_args())?; - let value = vec_ref.len(vec_ty_arg)?; + let ty = &resolver.instantiate_single_type(*si, self.ty_args())?; + gas_meter.charge_vec_len(TypeWithLoader { + ty, + loader: resolver.loader(), + })?; + let value = vec_ref.len(ty)?; interpreter.operand_stack.push(value)?; } Bytecode::VecImmBorrow(si) => { let idx = interpreter.operand_stack.pop_as::()? as usize; let vec_ref = interpreter.operand_stack.pop_as::()?; - gas_status.charge_instr(Opcodes::VEC_IMM_BORROW)?; - let vec_ty_arg = &resolver.instantiate_single_type(*si, self.ty_args())?; - let value = vec_ref.borrow_elem(idx, vec_ty_arg)?; - interpreter.operand_stack.push(value)?; + let ty = resolver.instantiate_single_type(*si, self.ty_args())?; + let res = vec_ref.borrow_elem(idx, &ty); + gas_meter.charge_vec_borrow(false, make_ty!(&ty), res.is_ok())?; + interpreter.operand_stack.push(res?)?; } Bytecode::VecMutBorrow(si) => { let idx = interpreter.operand_stack.pop_as::()? as usize; let vec_ref = interpreter.operand_stack.pop_as::()?; - gas_status.charge_instr(Opcodes::VEC_MUT_BORROW)?; - let vec_ty_arg = &resolver.instantiate_single_type(*si, self.ty_args())?; - let value = vec_ref.borrow_elem(idx, vec_ty_arg)?; - interpreter.operand_stack.push(value)?; + let ty = &resolver.instantiate_single_type(*si, self.ty_args())?; + let res = vec_ref.borrow_elem(idx, ty); + gas_meter.charge_vec_borrow(true, make_ty!(ty), res.is_ok())?; + interpreter.operand_stack.push(res?)?; } Bytecode::VecPushBack(si) => { let elem = interpreter.operand_stack.pop()?; let vec_ref = interpreter.operand_stack.pop_as::()?; - gas_status.charge_instr_with_size(Opcodes::VEC_PUSH_BACK, elem.size())?; - let vec_ty_arg = &resolver.instantiate_single_type(*si, self.ty_args())?; - vec_ref.push_back(elem, vec_ty_arg)?; + let ty = &resolver.instantiate_single_type(*si, self.ty_args())?; + gas_meter.charge_vec_push_back(make_ty!(ty), &elem)?; + vec_ref.push_back(elem, ty)?; } Bytecode::VecPopBack(si) => { let vec_ref = interpreter.operand_stack.pop_as::()?; - gas_status.charge_instr(Opcodes::VEC_POP_BACK)?; - let vec_ty_arg = &resolver.instantiate_single_type(*si, self.ty_args())?; - let value = vec_ref.pop(vec_ty_arg)?; - interpreter.operand_stack.push(value)?; + let ty = &resolver.instantiate_single_type(*si, self.ty_args())?; + let res = vec_ref.pop(ty); + gas_meter.charge_vec_pop_back(make_ty!(ty), res.as_ref().ok())?; + interpreter.operand_stack.push(res?)?; } Bytecode::VecUnpack(si, num) => { let vec_val = interpreter.operand_stack.pop_as::()?; - let size = AbstractMemorySize::new(*num); - gas_status.charge_instr_with_size(Opcodes::VEC_UNPACK, size)?; - let vec_ty_arg = &resolver.instantiate_single_type(*si, self.ty_args())?; - let elements = vec_val.unpack(vec_ty_arg, *num)?; + let ty = &resolver.instantiate_single_type(*si, self.ty_args())?; + gas_meter.charge_vec_unpack(make_ty!(ty), NumArgs::new(*num))?; + let elements = vec_val.unpack(ty, *num)?; for value in elements { interpreter.operand_stack.push(value)?; } @@ -1214,9 +1345,9 @@ impl Frame { let idx2 = interpreter.operand_stack.pop_as::()? as usize; let idx1 = interpreter.operand_stack.pop_as::()? as usize; let vec_ref = interpreter.operand_stack.pop_as::()?; - gas_status.charge_instr(Opcodes::VEC_SWAP)?; - let vec_ty_arg = &resolver.instantiate_single_type(*si, self.ty_args())?; - vec_ref.swap(idx1, idx2, vec_ty_arg)?; + let ty = &resolver.instantiate_single_type(*si, self.ty_args())?; + gas_meter.charge_vec_swap(make_ty!(ty))?; + vec_ref.swap(idx1, idx2, ty)?; } } // invariant: advance to pc +1 is iff instruction at pc executed without aborting diff --git a/language/move-vm/runtime/src/loader.rs b/language/move-vm/runtime/src/loader.rs index e7028b0a25..8fb40bf2b4 100644 --- a/language/move-vm/runtime/src/loader.rs +++ b/language/move-vm/runtime/src/loader.rs @@ -4,7 +4,7 @@ use crate::{ logging::expect_no_verification_errors, - native_functions::{NativeFunction, NativeFunctions}, + native_functions::{NativeFunction, NativeFunctions, UnboxedNativeFunction}, session::LoadedFunctionInstantiation, }; use move_binary_format::{ @@ -20,11 +20,12 @@ use move_binary_format::{ }, IndexKind, }; -use move_bytecode_verifier::{self, cyclic_dependencies, dependencies}; +use move_bytecode_verifier::{self, cyclic_dependencies, dependencies, VerifierConfig}; use move_core_types::{ identifier::{IdentStr, Identifier}, language_storage::{ModuleId, StructTag, TypeTag}, - value::{MoveStructLayout, MoveTypeLayout}, + metadata::Metadata, + value::{MoveFieldLayout, MoveStructLayout, MoveTypeLayout}, vm_status::StatusCode, }; use move_vm_types::{ @@ -216,12 +217,20 @@ impl ModuleCache { idx: StructDefinitionIndex, ) -> StructType { let struct_handle = module.struct_handle_at(struct_def.struct_handle); + let field_names = match &struct_def.field_information { + StructFieldInformation::Native => vec![], + StructFieldInformation::Declared(field_info) => field_info + .iter() + .map(|f| module.identifier_at(f.name).to_owned()) + .collect(), + }; let abilities = struct_handle.abilities; let name = module.identifier_at(struct_handle.name).to_owned(); let type_parameters = struct_handle.type_parameters.clone(); let module = module.self_id(); StructType { fields: vec![], + field_names, abilities, type_parameters, name, @@ -441,16 +450,108 @@ pub(crate) struct Loader { module_cache: RwLock, type_cache: RwLock, natives: NativeFunctions, + + // The below field supports a hack to workaround well-known issues with the + // loader cache. This cache is not designed to support module upgrade or deletion. + // This leads to situations where the cache does not reflect the state of storage: + // + // 1. On module upgrade, the upgraded module is in storage, but the old one still in the cache. + // 2. On an abandoned code publishing transaction, the cache may contain a module which was + // never committed to storage by the adapter. + // + // The solution is to add a flag to Loader marking it as 'invalidated'. For scenario (1), + // the VM sets the flag itself. For scenario (2), a public API allows the adapter to set + // the flag. + // + // If the cache is invalidated, it can (and must) still be used until there are no more + // sessions alive which are derived from a VM with this loader. This is because there are + // internal data structures derived from the loader which can become inconsistent. Therefore + // the adapter must explicitly call a function to flush the invalidated loader. + // + // This code (the loader) needs a complete refactoring. The new loader should + // + // - support upgrade and deletion of modules, while still preserving max cache lookup + // performance. This is essential for a cache like this in a multi-tenant execution + // environment. + // - should delegate lifetime ownership to the adapter. Code loading (including verification) + // is a major execution bottleneck. We should be able to reuse a cache for the lifetime of + // the adapter/node, not just a VM or even session (as effectively today). + invalidated: RwLock, + + // Collects the cache hits on module loads. This information can be read and reset by + // an adapter to reason about read/write conflicts of code publishing transactions and + // other transactions. + module_cache_hits: RwLock>, + + verifier_config: VerifierConfig, } impl Loader { - pub(crate) fn new(natives: NativeFunctions) -> Self { + pub(crate) fn new(natives: NativeFunctions, verifier_config: VerifierConfig) -> Self { Self { scripts: RwLock::new(ScriptCache::new()), module_cache: RwLock::new(ModuleCache::new()), type_cache: RwLock::new(TypeCache::new()), natives, + invalidated: RwLock::new(false), + module_cache_hits: RwLock::new(BTreeSet::new()), + verifier_config, + } + } + + /// Gets and clears module cache hits. A cache hit may also be caused indirectly by + /// loading a function or a type. This not only returns the direct hit, but also + /// indirect ones, that is all dependencies. + pub(crate) fn get_and_clear_module_cache_hits(&self) -> BTreeSet { + let mut result = BTreeSet::new(); + let hits: BTreeSet = std::mem::take(&mut self.module_cache_hits.write()); + for id in hits { + self.transitive_dep_closure(&id, &mut result) } + result + } + + fn transitive_dep_closure(&self, id: &ModuleId, visited: &mut BTreeSet) { + if !visited.insert(id.clone()) { + return; + } + let deps = self + .module_cache + .read() + .modules + .get(id) + .unwrap() + .module + .immediate_dependencies(); + for dep in deps { + self.transitive_dep_closure(&dep, visited) + } + } + + /// Flush this cache if it is marked as invalidated. + pub(crate) fn flush_if_invalidated(&self) { + let mut invalidated = self.invalidated.write(); + if *invalidated { + *self.scripts.write() = ScriptCache::new(); + *self.module_cache.write() = ModuleCache::new(); + *self.type_cache.write() = TypeCache::new(); + *invalidated = false; + } + } + + /// Mark this cache as invalidated. + pub(crate) fn mark_as_invalid(&self) { + *self.invalidated.write() = true; + } + + /// Copies metadata out of a modules bytecode if available. + pub(crate) fn get_metadata(&self, module: ModuleId, key: &[u8]) -> Option { + let cache = self.module_cache.read(); + cache + .modules + .get(&module) + .and_then(|module| module.module.metadata.iter().find(|md| md.key == key)) + .cloned() } // @@ -545,7 +646,7 @@ impl Loader { // Script verification steps. // See `verify_module()` for module verification steps. fn verify_script(&self, script: &CompiledScript) -> VMResult<()> { - move_bytecode_verifier::verify_script(script) + move_bytecode_verifier::verify_script_with_config(&self.verifier_config, script) } fn verify_script_dependencies( @@ -670,7 +771,7 @@ impl Loader { // module will NOT show up in `module_cache`. In the module republishing case, it means // that the old module is still in the `module_cache`, unless a new Loader is created, // which means that a new MoveVM instance needs to be created. - move_bytecode_verifier::verify_module(module)?; + move_bytecode_verifier::verify_module_with_config(&self.verifier_config, module)?; self.check_natives(module)?; let mut visited = BTreeSet::new(); @@ -740,31 +841,34 @@ impl Loader { ) } - // All native functions must be known to the loader + // All native functions must be known to the loader, unless we are compiling with feature + // `lazy_natives`. fn check_natives(&self, module: &CompiledModule) -> VMResult<()> { fn check_natives_impl(loader: &Loader, module: &CompiledModule) -> PartialVMResult<()> { - for (idx, native_function) in module - .function_defs() - .iter() - .filter(|fdv| fdv.is_native()) - .enumerate() - { - let fh = module.function_handle_at(native_function.function); - let mh = module.module_handle_at(fh.module); - loader - .natives - .resolve( - module.address_identifier_at(mh.address), - module.identifier_at(mh.name).as_str(), - module.identifier_at(fh.name).as_str(), - ) - .ok_or_else(|| { - verification_error( - StatusCode::MISSING_DEPENDENCY, - IndexKind::FunctionHandle, - idx as TableIndex, + if !cfg!(feature = "lazy_natives") { + for (idx, native_function) in module + .function_defs() + .iter() + .filter(|fdv| fdv.is_native()) + .enumerate() + { + let fh = module.function_handle_at(native_function.function); + let mh = module.module_handle_at(fh.module); + loader + .natives + .resolve( + module.address_identifier_at(mh.address), + module.identifier_at(mh.name).as_str(), + module.identifier_at(fh.name).as_str(), ) - })?; + .ok_or_else(|| { + verification_error( + StatusCode::MISSING_DEPENDENCY, + IndexKind::FunctionHandle, + idx as TableIndex, + ) + })?; + } } // TODO: fix check and error code if we leave something around for native structs. // For now this generates the only error test cases care about... @@ -844,6 +948,7 @@ impl Loader { ) -> VMResult> { // if the module is already in the code cache, load the cached version if let Some(cached) = self.module_cache.read().module_at(id) { + self.module_cache_hits.write().insert(id.clone()); return Ok(cached); } @@ -895,7 +1000,8 @@ impl Loader { .map_err(expect_no_verification_errors)?; // bytecode verifier checks that can be performed with the module itself - move_bytecode_verifier::verify_module(&module).map_err(expect_no_verification_errors)?; + move_bytecode_verifier::verify_module_with_config(&self.verifier_config, &module) + .map_err(expect_no_verification_errors)?; self.check_natives(&module) .map_err(expect_no_verification_errors)?; Ok(module) @@ -1250,6 +1356,7 @@ impl<'a> Resolver<'a> { Ok(instantiation) } + #[allow(unused)] pub(crate) fn type_params_count(&self, idx: FunctionInstantiationIndex) -> usize { let func_inst = match &self.binary { BinaryType::Module(module) => module.function_instantiation_at(idx.0), @@ -1341,6 +1448,13 @@ impl<'a> Resolver<'a> { self.loader.type_to_type_layout(ty) } + pub(crate) fn type_to_fully_annotated_layout( + &self, + ty: &Type, + ) -> PartialVMResult { + self.loader.type_to_fully_annotated_layout(ty) + } + // get the loader pub(crate) fn loader(&self) -> &Loader { self.loader @@ -1744,7 +1858,7 @@ impl Script { let type_parameters = script.type_parameters.clone(); // TODO: main does not have a name. Revisit. let name = Identifier::new("main").unwrap(); - let native = None; // Script entries cannot be native + let (native, def_is_native) = (None, false); // Script entries cannot be native let main: Arc = Arc::new(Function { file_format_version: script.version(), index: FunctionDefinitionIndex(0), @@ -1754,6 +1868,7 @@ impl Script { locals, type_parameters, native, + def_is_native, scope, name, }); @@ -1845,6 +1960,7 @@ pub(crate) struct Function { locals: Signature, type_parameters: Vec, native: Option, + def_is_native: bool, scope: Scope, name: Identifier, } @@ -1859,14 +1975,17 @@ impl Function { let handle = module.function_handle_at(def.function); let name = module.identifier_at(handle.name).to_owned(); let module_id = module.self_id(); - let native = if def.is_native() { - natives.resolve( - module_id.address(), - module_id.name().as_str(), - name.as_str(), + let (native, def_is_native) = if def.is_native() { + ( + natives.resolve( + module_id.address(), + module_id.name().as_str(), + name.as_str(), + ), + true, ) } else { - None + (None, false) }; let scope = Scope::Module(module_id); let parameters = module.signature_at(handle.parameters).clone(); @@ -1896,6 +2015,7 @@ impl Function { locals, type_parameters, native, + def_is_native, scope, name, } @@ -1938,6 +2058,10 @@ impl Function { self.parameters.len() } + pub(crate) fn return_type_count(&self) -> usize { + self.return_.len() + } + pub(crate) fn name(&self) -> &str { self.name.as_str() } @@ -1968,14 +2092,24 @@ impl Function { } pub(crate) fn is_native(&self) -> bool { - self.native.is_some() + self.def_is_native } - pub(crate) fn get_native(&self) -> PartialVMResult { - self.native.ok_or_else(|| { - PartialVMError::new(StatusCode::UNREACHABLE) - .with_message("Missing Native Function".to_string()) - }) + pub(crate) fn get_native(&self) -> PartialVMResult<&UnboxedNativeFunction> { + if cfg!(feature = "lazy_natives") { + // If lazy_natives is configured, this is a MISSING_DEPENDENCY error, as we skip + // checking those at module loading time. + self.native.as_deref().ok_or_else(|| { + PartialVMError::new(StatusCode::MISSING_DEPENDENCY) + .with_message(format!("Missing Native Function `{}`", self.name)) + }) + } else { + // Otherwise this error should not happen, hence UNREACHABLE + self.native.as_deref().ok_or_else(|| { + PartialVMError::new(StatusCode::UNREACHABLE) + .with_message("Missing Native Function".to_string()) + }) + } } } @@ -2036,6 +2170,7 @@ struct FieldInstantiation { struct StructInfo { struct_tag: Option, struct_layout: Option, + annotated_struct_layout: Option, } impl StructInfo { @@ -2043,6 +2178,7 @@ impl StructInfo { Self { struct_tag: None, struct_layout: None, + annotated_struct_layout: None, } } } @@ -2188,12 +2324,101 @@ impl Loader { }) } + fn struct_gidx_to_fully_annotated_layout( + &self, + gidx: CachedStructIndex, + ty_args: &[Type], + depth: usize, + ) -> PartialVMResult { + if let Some(struct_map) = self.type_cache.read().structs.get(&gidx) { + if let Some(struct_info) = struct_map.get(ty_args) { + if let Some(layout) = &struct_info.annotated_struct_layout { + return Ok(layout.clone()); + } + } + } + + let struct_type = self.module_cache.read().struct_at(gidx); + if struct_type.fields.len() != struct_type.field_names.len() { + return Err( + PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR).with_message( + "Field types did not match the length of field names in loaded struct" + .to_owned(), + ), + ); + } + let struct_tag = self.struct_gidx_to_type_tag(gidx, ty_args)?; + let field_layouts = struct_type + .field_names + .iter() + .zip(&struct_type.fields) + .map(|(n, ty)| { + let ty = ty.subst(ty_args)?; + let l = self.type_to_fully_annotated_layout_impl(&ty, depth + 1)?; + Ok(MoveFieldLayout::new(n.clone(), l)) + }) + .collect::>>()?; + let struct_layout = MoveStructLayout::with_types(struct_tag, field_layouts); + + self.type_cache + .write() + .structs + .entry(gidx) + .or_insert_with(HashMap::new) + .entry(ty_args.to_vec()) + .or_insert_with(StructInfo::new) + .annotated_struct_layout = Some(struct_layout.clone()); + + Ok(struct_layout) + } + + fn type_to_fully_annotated_layout_impl( + &self, + ty: &Type, + depth: usize, + ) -> PartialVMResult { + if depth > VALUE_DEPTH_MAX { + return Err(PartialVMError::new(StatusCode::VM_MAX_VALUE_DEPTH_REACHED)); + } + Ok(match ty { + Type::Bool => MoveTypeLayout::Bool, + Type::U8 => MoveTypeLayout::U8, + Type::U64 => MoveTypeLayout::U64, + Type::U128 => MoveTypeLayout::U128, + Type::Address => MoveTypeLayout::Address, + Type::Signer => MoveTypeLayout::Signer, + Type::Vector(ty) => MoveTypeLayout::Vector(Box::new( + self.type_to_fully_annotated_layout_impl(ty, depth + 1)?, + )), + Type::Struct(gidx) => MoveTypeLayout::Struct( + self.struct_gidx_to_fully_annotated_layout(*gidx, &[], depth)?, + ), + Type::StructInstantiation(gidx, ty_args) => MoveTypeLayout::Struct( + self.struct_gidx_to_fully_annotated_layout(*gidx, ty_args, depth)?, + ), + Type::Reference(_) | Type::MutableReference(_) | Type::TyParam(_) => { + return Err( + PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR) + .with_message(format!("no type layout for {:?}", ty)), + ) + } + }) + } + pub(crate) fn type_to_type_tag(&self, ty: &Type) -> PartialVMResult { self.type_to_type_tag_impl(ty) } + pub(crate) fn type_to_type_layout(&self, ty: &Type) -> PartialVMResult { self.type_to_type_layout_impl(ty, 1) } + + pub(crate) fn type_to_fully_annotated_layout( + &self, + ty: &Type, + ) -> PartialVMResult { + self.type_to_fully_annotated_layout_impl(ty, 1) + } } // Public APIs for external uses. @@ -2207,4 +2432,14 @@ impl Loader { self.type_to_type_layout(&ty) .map_err(|e| e.finish(Location::Undefined)) } + + pub(crate) fn get_fully_annotated_type_layout( + &self, + type_tag: &TypeTag, + move_storage: &impl DataStore, + ) -> VMResult { + let ty = self.load_type(type_tag, move_storage)?; + self.type_to_fully_annotated_layout(&ty) + .map_err(|e| e.finish(Location::Undefined)) + } } diff --git a/language/move-vm/runtime/src/move_vm.rs b/language/move-vm/runtime/src/move_vm.rs index 4127164a48..75153a8cf3 100644 --- a/language/move-vm/runtime/src/move_vm.rs +++ b/language/move-vm/runtime/src/move_vm.rs @@ -2,7 +2,7 @@ // Copyright (c) The Move Contributors // SPDX-License-Identifier: Apache-2.0 -use std::sync::Arc; +use std::{collections::BTreeSet, sync::Arc}; use crate::{ data_cache::TransactionDataCache, native_extensions::NativeContextExtensions, @@ -12,9 +12,10 @@ use move_binary_format::{ errors::{Location, VMResult}, CompiledModule, }; +use move_bytecode_verifier::VerifierConfig; use move_core_types::{ account_address::AccountAddress, identifier::Identifier, language_storage::ModuleId, - resolver::MoveResolver, + metadata::Metadata, resolver::MoveResolver, }; pub struct MoveVM { @@ -24,9 +25,17 @@ pub struct MoveVM { impl MoveVM { pub fn new( natives: impl IntoIterator, + ) -> VMResult { + Self::new_with_verifier_config(natives, VerifierConfig::default()) + } + + pub fn new_with_verifier_config( + natives: impl IntoIterator, + verifier_config: VerifierConfig, ) -> VMResult { Ok(Self { - runtime: VMRuntime::new(natives).map_err(|err| err.finish(Location::Undefined))?, + runtime: VMRuntime::new(natives, verifier_config) + .map_err(|err| err.finish(Location::Undefined))?, }) } @@ -71,4 +80,46 @@ impl MoveVM { ) .map(|arc_module| arc_module.arc_module()) } + + /// Allows the adapter to announce to the VM that the code loading cache should be considered + /// outdated. This can happen if the adapter executed a particular code publishing transaction + /// but decided to not commit the result to the data store. Because the code cache currently + /// does not support deletion, the cache will, incorrectly, still contain this module. + /// TODO: new loader architecture + pub fn mark_loader_cache_as_invalid(&self) { + self.runtime.loader().mark_as_invalid() + } + + /// If the loader cache has been invalidated (either by the above call or by internal logic) + /// flush it so it is valid again. Notice that should only be called if there are no + /// outstanding sessions created from this VM. + /// TODO: new loader architecture + pub fn flush_loader_cache_if_invalidated(&self) { + self.runtime.loader().flush_if_invalidated() + } + + /// Gets and clears module cache hits. This is hack which allows the adapter to see module + /// reads if executing multiple transactions in a VM. Without this, the adapter only sees + /// the first load of a module. + /// TODO: new loader architecture + pub fn get_and_clear_module_cache_hits(&self) -> BTreeSet { + self.runtime.loader().get_and_clear_module_cache_hits() + } + + /// Attempts to discover metadata in a given module with given key. Availability + /// of this data may depend on multiple aspects. In general, no hard assumptions of + /// availability should be made, but typically, one can expect that + /// the modules which have been involved in the execution of the last session are available. + /// + /// This is called by an adapter to extract, for example, debug information out of + /// the metadata section of the code for post mortem analysis. Notice that because + /// of ownership of the underlying binary representation of modules hidden behind an rwlock, + /// this actually has to hand back a copy of the associated metadata, so metadata should + /// be organized keeping this in mind. + /// + /// TODO: in the new loader architecture, as the loader is visible to the adapter, one would + /// call this directly via the loader instead of the VM. + pub fn get_module_metadata(&self, module: ModuleId, key: &[u8]) -> Option { + self.runtime.loader().get_metadata(module, key) + } } diff --git a/language/move-vm/runtime/src/native_functions.rs b/language/move-vm/runtime/src/native_functions.rs index bc383f430c..d88ab52261 100644 --- a/language/move-vm/runtime/src/native_functions.rs +++ b/language/move-vm/runtime/src/native_functions.rs @@ -8,23 +8,27 @@ use crate::{ use move_binary_format::errors::{ExecutionState, PartialVMError, PartialVMResult}; use move_core_types::{ account_address::AccountAddress, - gas_schedule::CostTable, identifier::Identifier, language_storage::TypeTag, value::MoveTypeLayout, vm_status::{StatusCode, StatusType}, }; use move_vm_types::{ - data_store::DataStore, gas_schedule::GasStatus, loaded_data::runtime_types::Type, - natives::function::NativeResult, values::Value, + data_store::DataStore, loaded_data::runtime_types::Type, natives::function::NativeResult, + values::Value, }; use std::{ collections::{HashMap, VecDeque}, fmt::Write, + sync::Arc, }; -pub type NativeFunction = - fn(&mut NativeContext, Vec, VecDeque) -> PartialVMResult; +pub type UnboxedNativeFunction = dyn Fn(&mut NativeContext, Vec, VecDeque) -> PartialVMResult + + Send + + Sync + + 'static; + +pub type NativeFunction = Arc; pub type NativeFunctionTable = Vec<(AccountAddress, Identifier, Identifier, NativeFunction)>; @@ -88,7 +92,6 @@ impl NativeFunctions { pub struct NativeContext<'a, 'b> { interpreter: &'a mut Interpreter, data_store: &'a mut dyn DataStore, - gas_status: &'a GasStatus<'a>, resolver: &'a Resolver<'a>, extensions: &'a mut NativeContextExtensions<'b>, } @@ -97,14 +100,12 @@ impl<'a, 'b> NativeContext<'a, 'b> { pub(crate) fn new( interpreter: &'a mut Interpreter, data_store: &'a mut dyn DataStore, - gas_status: &'a mut GasStatus, resolver: &'a Resolver<'a>, extensions: &'a mut NativeContextExtensions<'b>, ) -> Self { Self { interpreter, data_store, - gas_status, resolver, extensions, } @@ -117,10 +118,6 @@ impl<'a, 'b> NativeContext<'a, 'b> { .debug_print_stack_trace(buf, self.resolver.loader()) } - pub fn cost_table(&self) -> &CostTable { - self.gas_status.cost_table() - } - pub fn save_event( &mut self, guid: Vec, @@ -151,6 +148,17 @@ impl<'a, 'b> NativeContext<'a, 'b> { } } + pub fn type_to_fully_annotated_layout( + &self, + ty: &Type, + ) -> PartialVMResult> { + match self.resolver.type_to_fully_annotated_layout(ty) { + Ok(ty_layout) => Ok(Some(ty_layout)), + Err(e) if e.major_status().status_type() == StatusType::InvariantViolation => Err(e), + Err(_) => Ok(None), + } + } + pub fn extensions(&self) -> &NativeContextExtensions<'b> { self.extensions } diff --git a/language/move-vm/runtime/src/runtime.rs b/language/move-vm/runtime/src/runtime.rs index d861ce27c2..a638675e5c 100644 --- a/language/move-vm/runtime/src/runtime.rs +++ b/language/move-vm/runtime/src/runtime.rs @@ -12,12 +12,12 @@ use crate::{ }; use move_binary_format::{ access::ModuleAccess, - compatibility::Compatibility, + compatibility::{Compatibility, CompatibilityConfig}, errors::{verification_error, Location, PartialVMError, PartialVMResult, VMResult}, file_format::LocalIndex, normalized, CompiledModule, IndexKind, }; -use move_bytecode_verifier::script_signature; +use move_bytecode_verifier::{script_signature, VerifierConfig}; use move_core_types::{ account_address::AccountAddress, identifier::{IdentStr, Identifier}, @@ -28,7 +28,7 @@ use move_core_types::{ }; use move_vm_types::{ data_store::DataStore, - gas_schedule::GasStatus, + gas::GasMeter, loaded_data::runtime_types::Type, values::{Locals, Reference, VMValueCast, Value}, }; @@ -43,18 +43,15 @@ pub(crate) struct VMRuntime { impl VMRuntime { pub(crate) fn new( natives: impl IntoIterator, + verifier_config: VerifierConfig, ) -> PartialVMResult { Ok(VMRuntime { - loader: Loader::new(NativeFunctions::new(natives)?), + loader: Loader::new(NativeFunctions::new(natives)?, verifier_config), }) } pub fn new_session<'r, S: MoveResolver>(&self, remote: &'r S) -> Session<'r, '_, S> { - Session { - runtime: self, - data_cache: TransactionDataCache::new(remote, &self.loader), - native_extensions: NativeContextExtensions::default(), - } + self.new_session_with_extensions(remote, NativeContextExtensions::default()) } pub fn new_session_with_extensions<'r, S: MoveResolver>( @@ -74,8 +71,8 @@ impl VMRuntime { modules: Vec>, sender: AccountAddress, data_store: &mut impl DataStore, - _gas_status: &mut GasStatus, - compat_check: bool, + _gas_meter: &mut impl GasMeter, + compat_config: CompatibilityConfig, ) -> VMResult<()> { // deserialize the modules. Perform bounds check. After this indexes can be // used with the `[]` operator @@ -115,13 +112,24 @@ impl VMRuntime { // changing the bytecode format to include an `is_upgradable` flag in the CompiledModule. for module in &compiled_modules { let module_id = module.self_id(); - if data_store.exists_module(&module_id)? && compat_check { + + if data_store.exists_module(&module_id)? && compat_config.need_check_compat() { let old_module_ref = self.loader.load_module(&module_id, data_store)?; let old_module = old_module_ref.module(); let old_m = normalized::Module::new(old_module); let new_m = normalized::Module::new(module); - let compat = Compatibility::check(&old_m, &new_m); - if !compat.is_fully_compatible() { + let compat = + Compatibility::check(compat_config.check_friend_linking, &old_m, &new_m); + + if compat_config.check_struct_and_function_linking + && !compat.struct_and_function_linking + { + return Err(PartialVMError::new( + StatusCode::BACKWARD_INCOMPATIBLE_MODULE_UPDATE, + ) + .finish(Location::Undefined)); + } + if compat_config.check_struct_layout && !compat.struct_layout { return Err(PartialVMError::new( StatusCode::BACKWARD_INCOMPATIBLE_MODULE_UPDATE, ) @@ -193,7 +201,13 @@ impl VMRuntime { // All modules verified, publish them to data cache for (module, blob) in compiled_modules.into_iter().zip(modules.into_iter()) { - data_store.publish_module(&module.self_id(), blob)?; + let is_republishing = data_store.exists_module(&module.self_id())?; + if is_republishing { + // This is an upgrade, so invalidate the loader cache, which still contains the + // old module. + self.loader.mark_as_invalid(); + } + data_store.publish_module(&module.self_id(), blob, is_republishing)?; } Ok(()) } @@ -318,7 +332,7 @@ impl VMRuntime { return_types: Vec, serialized_args: Vec>, data_store: &mut impl DataStore, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, extensions: &mut NativeContextExtensions, ) -> VMResult { let arg_types = param_types @@ -348,7 +362,7 @@ impl VMRuntime { ty_args, deserialized_args, data_store, - gas_status, + gas_meter, extensions, &self.loader, )?; @@ -383,7 +397,7 @@ impl VMRuntime { ty_args: Vec, serialized_args: Vec>, data_store: &mut impl DataStore, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, extensions: &mut NativeContextExtensions, bypass_declared_entry_check: bool, ) -> VMResult { @@ -435,7 +449,7 @@ impl VMRuntime { return_, serialized_args, data_store, - gas_status, + gas_meter, extensions, ) } @@ -447,7 +461,7 @@ impl VMRuntime { ty_args: Vec, serialized_args: Vec>, data_store: &mut impl DataStore, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, extensions: &mut NativeContextExtensions, ) -> VMResult { // load the script, perform verification @@ -469,7 +483,7 @@ impl VMRuntime { return_, serialized_args, data_store, - gas_status, + gas_meter, extensions, ) } diff --git a/language/move-vm/runtime/src/session.rs b/language/move-vm/runtime/src/session.rs index f2aa3ea13d..598e55ec78 100644 --- a/language/move-vm/runtime/src/session.rs +++ b/language/move-vm/runtime/src/session.rs @@ -7,6 +7,7 @@ use crate::{ runtime::VMRuntime, }; use move_binary_format::{ + compatibility::CompatibilityConfig, errors::*, file_format::{AbilitySet, LocalIndex}, }; @@ -20,7 +21,7 @@ use move_core_types::{ }; use move_vm_types::{ data_store::DataStore, - gas_schedule::GasStatus, + gas::GasMeter, loaded_data::runtime_types::{CachedStructIndex, StructType, Type}, }; use std::{borrow::Borrow, sync::Arc}; @@ -74,7 +75,7 @@ impl<'r, 'l, S: MoveResolver> Session<'r, 'l, S> { function_name: &IdentStr, ty_args: Vec, args: Vec>, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, ) -> VMResult { let bypass_declared_entry_check = false; self.runtime.execute_function( @@ -83,7 +84,7 @@ impl<'r, 'l, S: MoveResolver> Session<'r, 'l, S> { ty_args, args, &mut self.data_cache, - gas_status, + gas_meter, &mut self.native_extensions, bypass_declared_entry_check, ) @@ -96,7 +97,7 @@ impl<'r, 'l, S: MoveResolver> Session<'r, 'l, S> { function_name: &IdentStr, ty_args: Vec, args: Vec>, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, ) -> VMResult { let bypass_declared_entry_check = true; self.runtime.execute_function( @@ -105,7 +106,7 @@ impl<'r, 'l, S: MoveResolver> Session<'r, 'l, S> { ty_args, args, &mut self.data_cache, - gas_status, + gas_meter, &mut self.native_extensions, bypass_declared_entry_check, ) @@ -132,14 +133,14 @@ impl<'r, 'l, S: MoveResolver> Session<'r, 'l, S> { script: impl Borrow<[u8]>, ty_args: Vec, args: Vec>, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, ) -> VMResult { self.runtime.execute_script( script, ty_args, args, &mut self.data_cache, - gas_status, + gas_meter, &mut self.native_extensions, ) } @@ -161,9 +162,9 @@ impl<'r, 'l, S: MoveResolver> Session<'r, 'l, S> { &mut self, module: Vec, sender: AccountAddress, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, ) -> VMResult<()> { - self.publish_module_bundle(vec![module], sender, gas_status) + self.publish_module_bundle(vec![module], sender, gas_meter) } /// Publish a series of modules. @@ -185,21 +186,47 @@ impl<'r, 'l, S: MoveResolver> Session<'r, 'l, S> { &mut self, modules: Vec>, sender: AccountAddress, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, ) -> VMResult<()> { - self.runtime - .publish_module_bundle(modules, sender, &mut self.data_cache, gas_status, true) + self.runtime.publish_module_bundle( + modules, + sender, + &mut self.data_cache, + gas_meter, + CompatibilityConfig::full_check(), + ) + } + + /// Same like `publish_module_bundle` but with a custom compatibility check. + pub fn publish_module_bundle_with_compat_config( + &mut self, + modules: Vec>, + sender: AccountAddress, + gas_meter: &mut impl GasMeter, + compat_config: CompatibilityConfig, + ) -> VMResult<()> { + self.runtime.publish_module_bundle( + modules, + sender, + &mut self.data_cache, + gas_meter, + compat_config, + ) } - /// Same like `publish_module_bundle` but relaxes compatibility checks. pub fn publish_module_bundle_relax_compatibility( &mut self, modules: Vec>, sender: AccountAddress, - gas_status: &mut GasStatus, + gas_meter: &mut impl GasMeter, ) -> VMResult<()> { - self.runtime - .publish_module_bundle(modules, sender, &mut self.data_cache, gas_status, false) + self.runtime.publish_module_bundle( + modules, + sender, + &mut self.data_cache, + gas_meter, + CompatibilityConfig::no_check(), + ) } pub fn num_mutated_accounts(&self, sender: &AccountAddress) -> u64 { @@ -271,6 +298,12 @@ impl<'r, 'l, S: MoveResolver> Session<'r, 'l, S> { .get_type_layout(type_tag, &self.data_cache) } + pub fn get_fully_annotated_type_layout(&self, type_tag: &TypeTag) -> VMResult { + self.runtime + .loader() + .get_fully_annotated_type_layout(type_tag, &self.data_cache) + } + /// Fetch a struct type from cache, if the index is in bounds /// Helpful when paired with load_type, or any other API that returns 'Type' pub fn get_struct_type(&self, index: CachedStructIndex) -> Option> { diff --git a/language/move-vm/runtime/src/tracing.rs b/language/move-vm/runtime/src/tracing.rs index 0a0c6abc03..b016879c9b 100644 --- a/language/move-vm/runtime/src/tracing.rs +++ b/language/move-vm/runtime/src/tracing.rs @@ -95,7 +95,7 @@ macro_rules! trace { ($function_desc:expr, $locals:expr, $pc:expr, $instr:tt, $resolver:expr, $interp:expr) => { // Only include this code in debug releases #[cfg(any(debug_assertions, feature = "debugging"))] - crate::tracing::trace( + $crate::tracing::trace( &$function_desc, $locals, $pc, diff --git a/language/move-vm/runtime/src/unit_tests/vm_arguments_tests.rs b/language/move-vm/runtime/src/unit_tests/vm_arguments_tests.rs index 60f2064fdc..45823b0b3b 100644 --- a/language/move-vm/runtime/src/unit_tests/vm_arguments_tests.rs +++ b/language/move-vm/runtime/src/unit_tests/vm_arguments_tests.rs @@ -23,7 +23,7 @@ use move_core_types::{ value::{serialize_values, MoveValue}, vm_status::{StatusCode, StatusType}, }; -use move_vm_types::gas_schedule::GasStatus; +use move_vm_types::gas::UnmeteredGasMeter; // make a script with a given signature for main. fn make_script(parameters: Signature) -> Vec { @@ -286,13 +286,12 @@ fn call_script_with_args_ty_args_signers( let move_vm = MoveVM::new(vec![]).unwrap(); let remote_view = RemoteStore::new(); let mut session = move_vm.new_session(&remote_view); - let mut gas_status = GasStatus::new_unmetered(); session .execute_script( script, ty_args, combine_signers_and_args(signers, non_signer_args), - &mut gas_status, + &mut UnmeteredGasMeter, ) .map(|_| ()) } @@ -313,13 +312,12 @@ fn call_script_function_with_args_ty_args_signers( let id = module.self_id(); remote_view.add_module(module); let mut session = move_vm.new_session(&remote_view); - let mut gas_status = GasStatus::new_unmetered(); session.execute_function_bypass_visibility( &id, function_name.as_ident_str(), ty_args, combine_signers_and_args(signers, non_signer_args), - &mut gas_status, + &mut UnmeteredGasMeter, )?; Ok(()) } @@ -786,7 +784,6 @@ fn call_missing_item() { let function_name = IdentStr::new("foo").unwrap(); // mising module let move_vm = MoveVM::new(vec![]).unwrap(); - let mut gas_status = GasStatus::new_unmetered(); let mut remote_view = RemoteStore::new(); let mut session = move_vm.new_session(&remote_view); let error = session @@ -795,7 +792,7 @@ fn call_missing_item() { function_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .err() .unwrap(); @@ -812,7 +809,7 @@ fn call_missing_item() { function_name, vec![], Vec::>::new(), - &mut gas_status, + &mut UnmeteredGasMeter, ) .err() .unwrap(); diff --git a/language/move-vm/test-utils/Cargo.toml b/language/move-vm/test-utils/Cargo.toml index 4d391dadc6..23426b6f56 100644 --- a/language/move-vm/test-utils/Cargo.toml +++ b/language/move-vm/test-utils/Cargo.toml @@ -7,14 +7,18 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] anyhow = "1.0.52" +once_cell = "1.7.2" +serde = { version = "1.0.124", features = ["derive", "rc"] } +move-binary-format = { path = "../../move-binary-format" } move-core-types = {path = "../../move-core/types" } +move-vm-types = { path = "../types" } move-table-extension = { path = "../../extensions/move-table-extension", optional = true } [features] diff --git a/language/move-vm/types/src/gas_schedule.rs b/language/move-vm/test-utils/src/gas_schedule.rs similarity index 50% rename from language/move-vm/types/src/gas_schedule.rs rename to language/move-vm/test-utils/src/gas_schedule.rs index 030304e6cd..802f8c9286 100644 --- a/language/move-vm/types/src/gas_schedule.rs +++ b/language/move-vm/test-utils/src/gas_schedule.rs @@ -8,26 +8,99 @@ //! operations or other native operations; the cost of each native operation will be returned by the //! native function itself. use move_binary_format::{ - errors::{Location, PartialVMError, PartialVMResult, VMResult}, + errors::{PartialVMError, PartialVMResult}, file_format::{ Bytecode, ConstantPoolIndex, FieldHandleIndex, FieldInstantiationIndex, FunctionHandleIndex, FunctionInstantiationIndex, SignatureIndex, - StructDefInstantiationIndex, StructDefinitionIndex, NUMBER_OF_NATIVE_FUNCTIONS, + StructDefInstantiationIndex, StructDefinitionIndex, }, file_format_common::{instruction_key, Opcodes}, }; use move_core_types::{ - gas_schedule::{ - AbstractMemorySize, CostTable, GasAlgebra, GasCarrier, GasConstants, GasCost, GasUnits, - InternalGasUnits, + gas_algebra::{ + AbstractMemorySize, GasQuantity, InternalGas, InternalGasPerAbstractMemoryUnit, + InternalGasUnit, NumArgs, NumBytes, ToUnit, ToUnitFractional, }, + language_storage::ModuleId, vm_status::StatusCode, }; +use move_vm_types::{ + gas::{GasMeter, SimpleInstruction}, + views::{TypeView, ValueView}, +}; use once_cell::sync::Lazy; -use std::cmp::max; +use serde::{Deserialize, Serialize}; +use std::{ + ops::{Add, Mul}, + u64, +}; + +pub enum GasUnit {} + +pub type Gas = GasQuantity; + +impl ToUnit for GasUnit { + const MULTIPLIER: u64 = 1000; +} + +impl ToUnitFractional for InternalGasUnit { + const NOMINATOR: u64 = 1; + const DENOMINATOR: u64 = 1000; +} + +/// The size in bytes for a non-string or address constant on the stack +pub const CONST_SIZE: AbstractMemorySize = AbstractMemorySize::new(16); + +/// The size in bytes for a reference on the stack +pub const REFERENCE_SIZE: AbstractMemorySize = AbstractMemorySize::new(8); + +/// The size of a struct in bytes +pub const STRUCT_SIZE: AbstractMemorySize = AbstractMemorySize::new(2); -static ZERO_COST_SCHEDULE: Lazy = - Lazy::new(|| zero_cost_schedule(NUMBER_OF_NATIVE_FUNCTIONS)); +/// For exists checks on data that doesn't exists this is the multiplier that is used. +pub const MIN_EXISTS_DATA_SIZE: AbstractMemorySize = AbstractMemorySize::new(100); + +/// The cost tables, keyed by the serialized form of the bytecode instruction. We use the +/// serialized form as opposed to the instruction enum itself as the key since this will be the +/// on-chain representation of bytecode instructions in the future. +#[derive(Clone, Debug, Serialize, PartialEq, Eq, Deserialize)] +pub struct CostTable { + pub instruction_table: Vec, +} + +impl CostTable { + #[inline] + pub fn instruction_cost(&self, instr_index: u8) -> &GasCost { + debug_assert!(instr_index > 0 && instr_index <= (self.instruction_table.len() as u8)); + &self.instruction_table[(instr_index - 1) as usize] + } +} + +/// The `GasCost` tracks: +/// - instruction cost: how much time/computational power is needed to perform the instruction +/// - memory cost: how much memory is required for the instruction, and storage overhead +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +pub struct GasCost { + pub instruction_gas: u64, + pub memory_gas: u64, +} + +impl GasCost { + pub fn new(instruction_gas: u64, memory_gas: u64) -> Self { + Self { + instruction_gas, + memory_gas, + } + } + + /// Convert a GasCost to a total gas charge in `InternalGas`. + #[inline] + pub fn total(&self) -> u64 { + self.instruction_gas.add(self.memory_gas) + } +} + +static ZERO_COST_SCHEDULE: Lazy = Lazy::new(zero_cost_schedule); /// The Move VM implementation of state for gas metering. /// @@ -37,7 +110,7 @@ static ZERO_COST_SCHEDULE: Lazy = /// Every client must use an instance of this type to interact with the Move VM. pub struct GasStatus<'a> { cost_table: &'a CostTable, - gas_left: InternalGasUnits, + gas_left: InternalGas, charge: bool, } @@ -46,9 +119,9 @@ impl<'a> GasStatus<'a> { /// /// Charge for every operation and fail when there is no more gas to pay for operations. /// This is the instantiation that must be used when executing a user script. - pub fn new(cost_table: &'a CostTable, gas_left: GasUnits) -> Self { + pub fn new(cost_table: &'a CostTable, gas_left: Gas) -> Self { Self { - gas_left: cost_table.gas_constants.to_internal_units(gas_left), + gas_left: gas_left.to_unit(), cost_table, charge: true, } @@ -60,7 +133,7 @@ impl<'a> GasStatus<'a> { /// code that does not have to charge the user. pub fn new_unmetered() -> Self { Self { - gas_left: InternalGasUnits::new(0), + gas_left: InternalGas::new(0), cost_table: &ZERO_COST_SCHEDULE, charge: false, } @@ -72,72 +145,367 @@ impl<'a> GasStatus<'a> { } /// Return the gas left. - pub fn remaining_gas(&self) -> GasUnits { - self.cost_table - .gas_constants - .to_external_units(self.gas_left) + pub fn remaining_gas(&self) -> Gas { + self.gas_left.to_unit_round_down() } /// Charge a given amount of gas and fail if not enough gas units are left. - pub fn deduct_gas(&mut self, amount: InternalGasUnits) -> PartialVMResult<()> { + pub fn deduct_gas(&mut self, amount: InternalGas) -> PartialVMResult<()> { if !self.charge { return Ok(()); } - if self - .gas_left - .app(&amount, |curr_gas, gas_amt| curr_gas >= gas_amt) - { - self.gas_left = self.gas_left.sub(amount); - Ok(()) - } else { - // Zero out the internal gas state - self.gas_left = InternalGasUnits::new(0); - Err(PartialVMError::new(StatusCode::OUT_OF_GAS)) + + match self.gas_left.checked_sub(amount) { + Some(gas_left) => { + self.gas_left = gas_left; + Ok(()) + } + None => { + self.gas_left = InternalGas::new(0); + Err(PartialVMError::new(StatusCode::OUT_OF_GAS)) + } } } + fn charge_instr(&mut self, opcode: Opcodes) -> PartialVMResult<()> { + self.deduct_gas( + self.cost_table + .instruction_cost(opcode as u8) + .total() + .into(), + ) + } + /// Charge an instruction over data with a given size and fail if not enough gas units are left. - pub fn charge_instr_with_size( + fn charge_instr_with_size( &mut self, opcode: Opcodes, - size: AbstractMemorySize, + size: AbstractMemorySize, ) -> PartialVMResult<()> { // Make sure that the size is always non-zero - let size = size.map(|x| std::cmp::max(1, x)); - debug_assert!(size.get() > 0); + let size = std::cmp::max(1.into(), size); + debug_assert!(size > 0.into()); self.deduct_gas( - self.cost_table - .instruction_cost(opcode as u8) - .total() - .mul(size), + InternalGasPerAbstractMemoryUnit::new( + self.cost_table.instruction_cost(opcode as u8).total(), + ) + .mul(size), ) } + pub fn set_metering(&mut self, enabled: bool) { + self.charge = enabled + } +} + +fn get_simple_instruction_opcode(instr: SimpleInstruction) -> Opcodes { + use Opcodes::*; + use SimpleInstruction::*; + + match instr { + Nop => NOP, + Ret => RET, + + BrTrue => BR_TRUE, + BrFalse => BR_FALSE, + Branch => BRANCH, + + Pop => POP, + LdU8 => LD_U8, + LdU64 => LD_U64, + LdU128 => LD_U128, + LdTrue => LD_TRUE, + LdFalse => LD_FALSE, + + FreezeRef => FREEZE_REF, + MutBorrowLoc => MUT_BORROW_LOC, + ImmBorrowLoc => IMM_BORROW_LOC, + ImmBorrowField => IMM_BORROW_FIELD, + MutBorrowField => MUT_BORROW_FIELD, + ImmBorrowFieldGeneric => IMM_BORROW_FIELD_GENERIC, + MutBorrowFieldGeneric => MUT_BORROW_FIELD_GENERIC, + + CastU8 => CAST_U8, + CastU64 => CAST_U64, + CastU128 => CAST_U128, + + Add => ADD, + Sub => SUB, + Mul => MUL, + Mod => MOD, + Div => DIV, + + BitOr => BIT_OR, + BitAnd => BIT_AND, + Xor => XOR, + Shl => SHL, + Shr => SHR, + + Or => OR, + And => AND, + Not => NOT, + + Lt => LT, + Gt => GT, + Le => LE, + Ge => GE, + + Abort => ABORT, + } +} + +impl<'b> GasMeter for GasStatus<'b> { /// Charge an instruction and fail if not enough gas units are left. - pub fn charge_instr(&mut self, opcode: Opcodes) -> PartialVMResult<()> { - self.deduct_gas(self.cost_table.instruction_cost(opcode as u8).total()) + fn charge_simple_instr(&mut self, instr: SimpleInstruction) -> PartialVMResult<()> { + self.charge_instr(get_simple_instruction_opcode(instr)) } - /// Charge gas related to the overall size of a transaction and fail if not enough - /// gas units are left. - pub fn charge_intrinsic_gas( + fn charge_native_function(&mut self, amount: InternalGas) -> PartialVMResult<()> { + self.deduct_gas(amount) + } + + fn charge_call( &mut self, - intrinsic_cost: AbstractMemorySize, - ) -> VMResult<()> { - let cost = calculate_intrinsic_gas(intrinsic_cost, &self.cost_table.gas_constants); - self.deduct_gas(cost) - .map_err(|e| e.finish(Location::Undefined)) + _module_id: &ModuleId, + _func_name: &str, + args: impl ExactSizeIterator, + ) -> PartialVMResult<()> { + self.charge_instr_with_size(Opcodes::CALL, (args.len() as u64 + 1).into()) } - pub fn set_metering(&mut self, enabled: bool) { - self.charge = enabled + fn charge_call_generic( + &mut self, + _module_id: &ModuleId, + _func_name: &str, + ty_args: impl ExactSizeIterator, + args: impl ExactSizeIterator, + ) -> PartialVMResult<()> { + self.charge_instr_with_size( + Opcodes::CALL_GENERIC, + ((ty_args.len() + args.len() + 1) as u64).into(), + ) + } + + fn charge_ld_const(&mut self, size: NumBytes) -> PartialVMResult<()> { + self.charge_instr_with_size(Opcodes::LD_CONST, u64::from(size).into()) + } + + fn charge_copy_loc(&mut self, val: impl ValueView) -> PartialVMResult<()> { + self.charge_instr_with_size(Opcodes::COPY_LOC, val.legacy_abstract_memory_size()) + } + + fn charge_move_loc(&mut self, val: impl ValueView) -> PartialVMResult<()> { + self.charge_instr_with_size(Opcodes::MOVE_LOC, val.legacy_abstract_memory_size()) + } + + fn charge_store_loc(&mut self, val: impl ValueView) -> PartialVMResult<()> { + self.charge_instr_with_size(Opcodes::ST_LOC, val.legacy_abstract_memory_size()) + } + + fn charge_pack( + &mut self, + is_generic: bool, + args: impl ExactSizeIterator, + ) -> PartialVMResult<()> { + let field_count = AbstractMemorySize::new(args.len() as u64); + self.charge_instr_with_size( + if is_generic { + Opcodes::PACK_GENERIC + } else { + Opcodes::PACK + }, + args.fold(field_count, |acc, val| { + acc + val.legacy_abstract_memory_size() + }), + ) + } + + fn charge_unpack( + &mut self, + is_generic: bool, + args: impl ExactSizeIterator, + ) -> PartialVMResult<()> { + let field_count = AbstractMemorySize::new(args.len() as u64); + self.charge_instr_with_size( + if is_generic { + Opcodes::UNPACK_GENERIC + } else { + Opcodes::UNPACK + }, + args.fold(field_count, |acc, val| { + acc + val.legacy_abstract_memory_size() + }), + ) + } + + fn charge_read_ref(&mut self, ref_val: impl ValueView) -> PartialVMResult<()> { + self.charge_instr_with_size(Opcodes::READ_REF, ref_val.legacy_abstract_memory_size()) + } + + fn charge_write_ref(&mut self, val: impl ValueView) -> PartialVMResult<()> { + self.charge_instr_with_size(Opcodes::WRITE_REF, val.legacy_abstract_memory_size()) + } + + fn charge_eq(&mut self, lhs: impl ValueView, rhs: impl ValueView) -> PartialVMResult<()> { + self.charge_instr_with_size( + Opcodes::EQ, + lhs.legacy_abstract_memory_size() + rhs.legacy_abstract_memory_size(), + ) + } + + fn charge_neq(&mut self, lhs: impl ValueView, rhs: impl ValueView) -> PartialVMResult<()> { + self.charge_instr_with_size( + Opcodes::NEQ, + lhs.legacy_abstract_memory_size() + rhs.legacy_abstract_memory_size(), + ) + } + + fn charge_load_resource(&mut self, _loaded: Option) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_borrow_global( + &mut self, + is_mut: bool, + is_generic: bool, + _ty: impl TypeView, + is_success: bool, + ) -> PartialVMResult<()> { + use Opcodes::*; + + if is_success { + let op = match (is_mut, is_generic) { + (false, false) => IMM_BORROW_GLOBAL, + (false, true) => IMM_BORROW_GLOBAL_GENERIC, + (true, false) => MUT_BORROW_GLOBAL, + (true, true) => MUT_BORROW_GLOBAL_GENERIC, + }; + + self.charge_instr_with_size(op, REFERENCE_SIZE)?; + } + + Ok(()) + } + + fn charge_exists( + &mut self, + is_generic: bool, + _ty: impl TypeView, + // TODO(Gas): see if we can get rid of this param + exists: bool, + ) -> PartialVMResult<()> { + use Opcodes::*; + + let op = if is_generic { EXISTS_GENERIC } else { EXISTS }; + self.charge_instr_with_size( + op, + if exists { + REFERENCE_SIZE + } else { + MIN_EXISTS_DATA_SIZE + }, + ) + } + + fn charge_move_from( + &mut self, + is_generic: bool, + _ty: impl TypeView, + val: Option, + ) -> PartialVMResult<()> { + use Opcodes::*; + + if let Some(val) = val { + let op = if is_generic { + MOVE_FROM_GENERIC + } else { + MOVE_FROM + }; + + self.charge_instr_with_size(op, val.legacy_abstract_memory_size())?; + } + + Ok(()) + } + + fn charge_move_to( + &mut self, + is_generic: bool, + _ty: impl TypeView, + val: impl ValueView, + is_success: bool, + ) -> PartialVMResult<()> { + use Opcodes::*; + + let op = if is_generic { MOVE_TO_GENERIC } else { MOVE_TO }; + + if is_success { + self.charge_instr_with_size(op, val.legacy_abstract_memory_size())?; + } + + Ok(()) + } + + fn charge_vec_pack<'a>( + &mut self, + _ty: impl TypeView + 'a, + args: impl ExactSizeIterator, + ) -> PartialVMResult<()> { + self.charge_instr_with_size(Opcodes::VEC_PACK, (args.len() as u64).into()) + } + + fn charge_vec_len(&mut self, _ty: impl TypeView) -> PartialVMResult<()> { + self.charge_instr(Opcodes::VEC_LEN) + } + + fn charge_vec_borrow( + &mut self, + is_mut: bool, + _ty: impl TypeView, + _is_success: bool, + ) -> PartialVMResult<()> { + use Opcodes::*; + + self.charge_instr(if is_mut { + VEC_MUT_BORROW + } else { + VEC_IMM_BORROW + }) + } + + fn charge_vec_push_back( + &mut self, + _ty: impl TypeView, + val: impl ValueView, + ) -> PartialVMResult<()> { + self.charge_instr_with_size(Opcodes::VEC_PUSH_BACK, val.legacy_abstract_memory_size()) + } + + fn charge_vec_pop_back( + &mut self, + _ty: impl TypeView, + _val: Option, + ) -> PartialVMResult<()> { + self.charge_instr(Opcodes::VEC_POP_BACK) + } + + fn charge_vec_unpack( + &mut self, + _ty: impl TypeView, + expect_num_elements: NumArgs, + ) -> PartialVMResult<()> { + self.charge_instr_with_size( + Opcodes::VEC_PUSH_BACK, + u64::from(expect_num_elements).into(), + ) + } + + fn charge_vec_swap(&mut self, _ty: impl TypeView) -> PartialVMResult<()> { + self.charge_instr(Opcodes::VEC_SWAP) } } -pub fn new_from_instructions( - mut instrs: Vec<(Bytecode, GasCost)>, - native_table: Vec, -) -> CostTable { +pub fn new_from_instructions(mut instrs: Vec<(Bytecode, GasCost)>) -> CostTable { instrs.sort_by_key(|cost| instruction_key(&cost.0)); if cfg!(debug_assertions) { @@ -157,11 +525,7 @@ pub fn new_from_instructions( .into_iter() .map(|(_, cost)| cost) .collect::>(); - CostTable { - instruction_table, - native_table, - gas_constants: GasConstants::default(), - } + CostTable { instruction_table } } pub fn zero_cost_instruction_table() -> Vec<(Bytecode, GasCost)> { @@ -280,24 +644,18 @@ pub fn zero_cost_instruction_table() -> Vec<(Bytecode, GasCost)> { // Only used for genesis and for tests where we need a cost table and // don't have a genesis storage state. -pub fn zero_cost_schedule(num_of_native_funcs: usize) -> CostTable { +pub fn zero_cost_schedule() -> CostTable { // The actual costs for the instructions in this table _DO NOT MATTER_. This is only used // for genesis and testing, and for these cases we don't need to worry // about the actual gas for instructions. The only thing we care about is having an entry // in the gas schedule for each instruction. let instrs = zero_cost_instruction_table(); - // length of native_table vector should be at least 18 due to the fact that there's a - // builtin native function cost EMIT_EVENT which indexed 17 in the vector - let num_of_native_funcs = max(num_of_native_funcs, 18); - let native_table = (0..num_of_native_funcs) - .map(|_| GasCost::new(0, 0)) - .collect::>(); - new_from_instructions(instrs, native_table) + new_from_instructions(instrs) } pub fn bytecode_instruction_costs() -> Vec<(Bytecode, GasCost)> { use Bytecode::*; - return vec![ + vec![ (MoveTo(StructDefinitionIndex::new(0)), GasCost::new(13, 1)), ( MoveToGeneric(StructDefInstantiationIndex::new(0)), @@ -408,7 +766,7 @@ pub fn bytecode_instruction_costs() -> Vec<(Bytecode, GasCost)> { (VecPopBack(SignatureIndex::new(0)), GasCost::new(227, 1)), (VecUnpack(SignatureIndex::new(0), 0), GasCost::new(572, 1)), (VecSwap(SignatureIndex::new(0)), GasCost::new(1436, 1)), - ]; + ] } pub static INITIAL_COST_SCHEDULE: Lazy = Lazy::new(|| { @@ -416,79 +774,5 @@ pub static INITIAL_COST_SCHEDULE: Lazy = Lazy::new(|| { // Note that the DiemVM is expecting the table sorted by instruction order. instrs.sort_by_key(|cost| instruction_key(&cost.0)); - use NativeCostIndex as N; - - let mut native_table = vec![ - (N::SHA2_256, GasCost::new(21, 1)), - (N::SHA3_256, GasCost::new(64, 1)), - (N::ED25519_VERIFY, GasCost::new(61, 1)), - (N::ED25519_THRESHOLD_VERIFY, GasCost::new(3351, 1)), - (N::BCS_TO_BYTES, GasCost::new(181, 1)), - (N::LENGTH, GasCost::new(98, 1)), - (N::EMPTY, GasCost::new(84, 1)), - (N::BORROW, GasCost::new(1334, 1)), - (N::BORROW_MUT, GasCost::new(1902, 1)), - (N::PUSH_BACK, GasCost::new(53, 1)), - (N::POP_BACK, GasCost::new(227, 1)), - (N::DESTROY_EMPTY, GasCost::new(572, 1)), - (N::SWAP, GasCost::new(1436, 1)), - (N::ED25519_VALIDATE_KEY, GasCost::new(26, 1)), - (N::SIGNER_BORROW, GasCost::new(353, 1)), - (N::CREATE_SIGNER, GasCost::new(24, 1)), - (N::DESTROY_SIGNER, GasCost::new(212, 1)), - (N::EMIT_EVENT, GasCost::new(52, 1)), - ]; - native_table.sort_by_key(|cost| cost.0 as u64); - let raw_native_table = native_table - .into_iter() - .map(|(_, cost)| cost) - .collect::>(); - new_from_instructions(instrs, raw_native_table) + new_from_instructions(instrs) }); - -/// Calculate the intrinsic gas for the transaction based upon its size in bytes/words. -pub fn calculate_intrinsic_gas( - transaction_size: AbstractMemorySize, - gas_constants: &GasConstants, -) -> InternalGasUnits { - let min_transaction_fee = gas_constants.min_transaction_gas_units; - - if transaction_size.get() > gas_constants.large_transaction_cutoff.get() { - let excess = transaction_size.sub(gas_constants.large_transaction_cutoff); - min_transaction_fee.add(gas_constants.intrinsic_gas_per_byte.mul(excess)) - } else { - min_transaction_fee.unitary_cast() - } -} - -// TODO: need to refactor native gas calculation so it is extensible. Currently we -// have hardcoded here the stdlib natives. -#[allow(non_camel_case_types)] -#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)] -#[repr(u8)] -pub enum NativeCostIndex { - SHA2_256 = 0, - SHA3_256 = 1, - ED25519_VERIFY = 2, - ED25519_THRESHOLD_VERIFY = 3, - BCS_TO_BYTES = 4, - LENGTH = 5, - EMPTY = 6, - BORROW = 7, - BORROW_MUT = 8, - PUSH_BACK = 9, - POP_BACK = 10, - DESTROY_EMPTY = 11, - SWAP = 12, - ED25519_VALIDATE_KEY = 13, - SIGNER_BORROW = 14, - CREATE_SIGNER = 15, - DESTROY_SIGNER = 16, - EMIT_EVENT = 17, -} - -impl From for u8 { - fn from(index: NativeCostIndex) -> Self { - index as u8 - } -} diff --git a/language/move-vm/test-utils/src/lib.rs b/language/move-vm/test-utils/src/lib.rs index 6b65217408..a56aba81a8 100644 --- a/language/move-vm/test-utils/src/lib.rs +++ b/language/move-vm/test-utils/src/lib.rs @@ -6,4 +6,5 @@ mod storage; +pub mod gas_schedule; pub use storage::{BlankStorage, DeltaStorage, InMemoryStorage}; diff --git a/language/move-vm/test-utils/src/storage.rs b/language/move-vm/test-utils/src/storage.rs index f30da18a95..103cf2cc42 100644 --- a/language/move-vm/test-utils/src/storage.rs +++ b/language/move-vm/test-utils/src/storage.rs @@ -2,21 +2,24 @@ // Copyright (c) The Move Contributors // SPDX-License-Identifier: Apache-2.0 -#[allow(unused)] -use anyhow::{anyhow, format_err, Error, Result}; -#[allow(unused)] -use move_core_types::gas_schedule::{GasAlgebra, GasCarrier, InternalGasUnits}; +use anyhow::{bail, Result}; use move_core_types::{ account_address::AccountAddress, - effects::{AccountChangeSet, ChangeSet}, + effects::{AccountChangeSet, ChangeSet, Op}, identifier::Identifier, language_storage::{ModuleId, StructTag}, resolver::{ModuleResolver, MoveResolver, ResourceResolver}, }; -use std::collections::{btree_map, BTreeMap}; +use std::{ + collections::{btree_map, BTreeMap}, + fmt::Debug, +}; #[cfg(feature = "table-extension")] -use move_table_extension::{TableChangeSet, TableHandle, TableOperation, TableResolver}; +use { + anyhow::Error, + move_table_extension::{TableChangeSet, TableHandle, TableResolver}, +}; /// A dummy storage containing no modules or resources. #[derive(Debug, Clone)] @@ -57,19 +60,10 @@ impl TableResolver for BlankStorage { ) -> Result>, Error> { Ok(None) } - - fn operation_cost( - &self, - _op: TableOperation, - _key_size: usize, - _val_size: usize, - ) -> InternalGasUnits { - InternalGasUnits::new(1) - } } -// A storage adapter created by stacking a change set on top of an existing storage backend. -/// The new storage can be used for additional computations without modifying the base. +/// A storage adapter created by stacking a change set on top of an existing storage backend. +/// This can be used for additional computations without modifying the base. #[derive(Debug, Clone)] pub struct DeltaStorage<'a, 'b, S> { base: &'a S, @@ -82,7 +76,7 @@ impl<'a, 'b, S: ModuleResolver> ModuleResolver for DeltaStorage<'a, 'b, S> { fn get_module(&self, module_id: &ModuleId) -> Result>, Self::Error> { if let Some(account_storage) = self.delta.accounts().get(module_id.address()) { if let Some(blob_opt) = account_storage.modules().get(module_id.name()) { - return Ok(blob_opt.clone()); + return Ok(blob_opt.clone().ok()); } } @@ -100,7 +94,7 @@ impl<'a, 'b, S: ResourceResolver> ResourceResolver for DeltaStorage<'a, 'b, S> { ) -> Result>, S::Error> { if let Some(account_storage) = self.delta.accounts().get(address) { if let Some(blob_opt) = account_storage.resources().get(tag) { - return Ok(blob_opt.clone()); + return Ok(blob_opt.clone().ok()); } } @@ -118,16 +112,6 @@ impl<'a, 'b, S: TableResolver> TableResolver for DeltaStorage<'a, 'b, S> { // TODO: No support for table deltas self.base.resolve_table_entry(handle, key) } - - fn operation_cost( - &self, - op: TableOperation, - key_size: usize, - val_size: usize, - ) -> InternalGasUnits { - // TODO: No support for table deltas - self.base.operation_cost(op, key_size, val_size) - } } impl<'a, 'b, S: MoveResolver> DeltaStorage<'a, 'b, S> { @@ -151,49 +135,60 @@ pub struct InMemoryStorage { tables: BTreeMap, Vec>>, } -fn apply_changes( - tree: &mut BTreeMap, - changes: impl IntoIterator)>, - make_err: F, -) -> std::result::Result<(), E> +fn apply_changes( + map: &mut BTreeMap, + changes: impl IntoIterator)>, +) -> Result<()> where - K: Ord, - F: FnOnce(K) -> E, + K: Ord + Debug, { - for (k, v_opt) in changes.into_iter() { - match (tree.entry(k), v_opt) { - (btree_map::Entry::Vacant(entry), None) => return Err(make_err(entry.into_key())), - (btree_map::Entry::Vacant(entry), Some(v)) => { - entry.insert(v); + use btree_map::Entry::*; + use Op::*; + + for (k, op) in changes.into_iter() { + match (map.entry(k), op) { + (Occupied(entry), New(_)) => { + bail!( + "Failed to apply changes -- key {:?} already exists", + entry.key() + ) } - (btree_map::Entry::Occupied(entry), None) => { + (Occupied(entry), Delete) => { entry.remove(); } - (btree_map::Entry::Occupied(entry), Some(v)) => { - *entry.into_mut() = v; + (Occupied(entry), Modify(val)) => { + *entry.into_mut() = val; + } + (Vacant(entry), New(val)) => { + entry.insert(val); } + (Vacant(entry), Delete | Modify(_)) => bail!( + "Failed to apply changes -- key {:?} does not exist", + entry.key() + ), } } Ok(()) } +fn get_or_insert(map: &mut BTreeMap, key: K, make_val: F) -> &mut V +where + K: Ord, + F: FnOnce() -> V, +{ + use btree_map::Entry::*; + + match map.entry(key) { + Occupied(entry) => entry.into_mut(), + Vacant(entry) => entry.insert(make_val()), + } +} + impl InMemoryAccountStorage { fn apply(&mut self, account_changeset: AccountChangeSet) -> Result<()> { let (modules, resources) = account_changeset.into_inner(); - apply_changes(&mut self.modules, modules, |module_name| { - format_err!( - "Failed to delete module {}: module does not exist.", - module_name - ) - })?; - - apply_changes(&mut self.resources, resources, |struct_tag| { - format_err!( - "Failed to delete resource {}: resource does not exist.", - struct_tag - ) - })?; - + apply_changes(&mut self.modules, modules)?; + apply_changes(&mut self.resources, resources)?; Ok(()) } @@ -225,7 +220,7 @@ impl InMemoryStorage { } #[cfg(feature = "table-extension")] - self.apply_table(table_changes); + self.apply_table(table_changes)?; Ok(()) } @@ -239,7 +234,7 @@ impl InMemoryStorage { } #[cfg(feature = "table-extension")] - fn apply_table(&mut self, changes: TableChangeSet) { + fn apply_table(&mut self, changes: TableChangeSet) -> Result<()> { let TableChangeSet { new_tables, removed_tables, @@ -258,14 +253,9 @@ impl InMemoryStorage { "inconsistent table change set: stale table handle" ); let table = self.tables.get_mut(&h).unwrap(); - for (key, val) in c.entries { - if let Some(v) = val { - table.insert(key, v); - } else { - table.remove(&key); - } - } + apply_changes(table, c.entries)?; } + Ok(()) } pub fn new() -> Self { @@ -277,14 +267,10 @@ impl InMemoryStorage { } pub fn publish_or_overwrite_module(&mut self, module_id: ModuleId, blob: Vec) { - let mut delta = ChangeSet::new(); - delta.publish_module(module_id, blob).unwrap(); - self.apply_extended( - delta, - #[cfg(feature = "table-extension")] - TableChangeSet::default(), - ) - .unwrap(); + let account = get_or_insert(&mut self.accounts, *module_id.address(), || { + InMemoryAccountStorage::new() + }); + account.modules.insert(module_id.name().to_owned(), blob); } pub fn publish_or_overwrite_resource( @@ -293,14 +279,8 @@ impl InMemoryStorage { struct_tag: StructTag, blob: Vec, ) { - let mut delta = ChangeSet::new(); - delta.publish_resource(addr, struct_tag, blob).unwrap(); - self.apply_extended( - delta, - #[cfg(feature = "table-extension")] - TableChangeSet::default(), - ) - .unwrap(); + let account = get_or_insert(&mut self.accounts, addr, InMemoryAccountStorage::new); + account.resources.insert(struct_tag, blob); } } @@ -339,13 +319,4 @@ impl TableResolver for InMemoryStorage { ) -> std::result::Result>, Error> { Ok(self.tables.get(handle).and_then(|t| t.get(key).cloned())) } - - fn operation_cost( - &self, - _op: TableOperation, - _key_size: usize, - _val_size: usize, - ) -> InternalGasUnits { - InternalGasUnits::new(1) - } } diff --git a/language/move-vm/transactional-tests/Cargo.toml b/language/move-vm/transactional-tests/Cargo.toml index 03777ae5d3..d29d297039 100644 --- a/language/move-vm/transactional-tests/Cargo.toml +++ b/language/move-vm/transactional-tests/Cargo.toml @@ -3,7 +3,7 @@ name = "move-vm-transactional-tests" version = "0.1.0" authors = ["Diem Association "] publish = false -edition = "2018" +edition = "2021" license = "Apache-2.0" [dev-dependencies] diff --git a/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_fn.exp b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_fn.exp new file mode 100644 index 0000000000..544ac1fda8 --- /dev/null +++ b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_fn.exp @@ -0,0 +1,10 @@ +processed 5 tasks + +task 4 'publish'. lines 39-46: +Error: Unable to publish module '00000000000000000000000000000042::A'. Got VMError: { + major_status: CYCLIC_MODULE_DEPENDENCY, + sub_status: None, + location: 0x42::A, + indices: [], + offsets: [], +} diff --git a/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_fn.mvir b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_fn.mvir new file mode 100644 index 0000000000..c8e1d23410 --- /dev/null +++ b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_fn.mvir @@ -0,0 +1,46 @@ +//# publish +module 0x42.A { + public foo(): u64 { + label b0: + return 5; + } +} + +//# publish +module 0x43.B { + import 0x42.A; + public foo(): u64 { + label b0: + return A.foo(); + } +} + +//# publish +module 0x44.C { + import 0x43.B; + public foo(): u64 { + label b0: + return B.foo(); + } +} + +//# run +import 0x44.C; + +main() { + let f: u64; +label b0: + f = C.foo(); + assert(move(f) == 5, 42); + return; +} + + +//# publish +module 0x42.A { + import 0x44.C; + public foo(): u64 { + label b0: + return C.foo(); + } +} diff --git a/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_friend.exp b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_friend.exp new file mode 100644 index 0000000000..9e20a832f5 --- /dev/null +++ b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_friend.exp @@ -0,0 +1,10 @@ +processed 4 tasks + +task 3 'publish'. lines 15-18: +Error: Unable to publish module '00000000000000000000000000000042::A'. Got VMError: { + major_status: CYCLIC_MODULE_FRIENDSHIP, + sub_status: None, + location: 0x42::A, + indices: [], + offsets: [], +} diff --git a/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_friend.mvir b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_friend.mvir new file mode 100644 index 0000000000..f007fe9ce5 --- /dev/null +++ b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_friend.mvir @@ -0,0 +1,18 @@ +//# publish +module 0x42.A { +} + +//# publish +module 0x42.B { + friend 0x42.A; +} + +//# publish +module 0x42.C { + friend 0x42.B; +} + +//# publish +module 0x42.A { + friend 0x42.C; +} diff --git a/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_struct.exp b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_struct.exp new file mode 100644 index 0000000000..e74030f976 --- /dev/null +++ b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_struct.exp @@ -0,0 +1,10 @@ +processed 4 tasks + +task 3 'publish'. lines 24-33: +Error: Unable to publish module '00000000000000000000000000000042::A'. Got VMError: { + major_status: CYCLIC_MODULE_DEPENDENCY, + sub_status: None, + location: 0x42::A, + indices: [], + offsets: [], +} diff --git a/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_struct.mvir b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_struct.mvir new file mode 100644 index 0000000000..18b9bc1071 --- /dev/null +++ b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_cyclic_dependencies_struct.mvir @@ -0,0 +1,33 @@ +//# publish +module 0x42.A { + struct A has store{ + f: u64, + } +} + +//# publish +module 0x43.B { + import 0x42.A; + struct B has store{ + a: A.A, + } +} + +//# publish +module 0x44.C { + import 0x43.B; + struct C has store{ + b: B.B, + } +} + +//# publish +module 0x42.A { + import 0x44.C; + struct A has store{ + f: u64, + } + struct A2 has store{ + c: C.C, + } +} diff --git a/language/move-vm/transactional-tests/tests/module_publishing/republish_module_incompatible_friend_fn_skip_check_friend_linking.exp b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_incompatible_friend_fn_skip_check_friend_linking.exp new file mode 100644 index 0000000000..256b0148cf --- /dev/null +++ b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_incompatible_friend_fn_skip_check_friend_linking.exp @@ -0,0 +1,10 @@ +processed 5 tasks + +task 4 'run'. lines 39-39: +Error: Function execution failed with VMError: { + major_status: UNEXPECTED_VERIFIER_ERROR, + sub_status: None, + location: 0x42::N, + indices: [(FunctionHandle, 1)], + offsets: [], +} diff --git a/language/move-vm/transactional-tests/tests/module_publishing/republish_module_incompatible_friend_fn_skip_check_friend_linking.mvir b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_incompatible_friend_fn_skip_check_friend_linking.mvir new file mode 100644 index 0000000000..88af6c2f07 --- /dev/null +++ b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_incompatible_friend_fn_skip_check_friend_linking.mvir @@ -0,0 +1,39 @@ +//# publish +module 0x42.N { + public foo() { + label b0: + return; + } +} + +//# publish +module 0x42.M { + friend 0x42.N; + public(friend) foo() { + label b0: + return; + } +} + +//# publish +module 0x42.N { + import 0x42.M; + public foo() { + label b0: + M.foo(); + return; + } +} + +//# publish --skip-check-friend-linking +module 0x42.M { + friend 0x42.N; + // `--skip-check-friend-linking` allow make `friend` visibility more restrictive + foo() { + label b0: + return; + } +} + +/// `--skip-check-friend-linking` upgrade will break the caller. +//# run 0x42::N::foo diff --git a/language/move-vm/transactional-tests/tests/module_publishing/republish_module_incompatible_layout_struct.exp b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_incompatible_layout_struct.exp new file mode 100644 index 0000000000..9d0cee140f --- /dev/null +++ b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_incompatible_layout_struct.exp @@ -0,0 +1,10 @@ +processed 2 tasks + +task 1 'publish'. lines 7-13: +Error: Unable to publish module '00000000000000000000000000000042::Duplicate'. Got VMError: { + major_status: BACKWARD_INCOMPATIBLE_MODULE_UPDATE, + sub_status: None, + location: undefined, + indices: [], + offsets: [], +} diff --git a/language/move-vm/transactional-tests/tests/module_publishing/republish_module_incompatible_layout_struct.mvir b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_incompatible_layout_struct.mvir new file mode 100644 index 0000000000..96b26cc629 --- /dev/null +++ b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_incompatible_layout_struct.mvir @@ -0,0 +1,13 @@ +//# publish +module 0x42.Duplicate { + struct U { f: u64 } + struct T { f: Self.U } +} + +//# publish +module 0x42.Duplicate { + struct U { f: u64 } + struct U2 { f: u64 } + struct T { f: Self.U2 } + // cannot change fields type +} diff --git a/language/move-vm/transactional-tests/tests/module_publishing/republish_module_skip_compatible_linking.exp b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_skip_compatible_linking.exp new file mode 100644 index 0000000000..ddb83526e7 --- /dev/null +++ b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_skip_compatible_linking.exp @@ -0,0 +1,10 @@ +processed 6 tasks + +task 5 'run'. lines 69-69: +Error: Function execution failed with VMError: { + major_status: UNEXPECTED_VERIFIER_ERROR, + sub_status: None, + location: 0x43::B, + indices: [(FunctionHandle, 2)], + offsets: [], +} diff --git a/language/move-vm/transactional-tests/tests/module_publishing/republish_module_skip_compatible_linking.mvir b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_skip_compatible_linking.mvir new file mode 100644 index 0000000000..3fcfdfc2e0 --- /dev/null +++ b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_skip_compatible_linking.mvir @@ -0,0 +1,69 @@ +//# publish +module 0x42.A { + struct A has store,drop { + f: u64, + } + public new(f: u64): Self.A { + label b0: + return A { f: move(f) }; + } + public f(a: &Self.A) :u64 { + label b0: + return *&move(a).A::f; + } +} + +//# publish +module 0x43.B { + import 0x42.A; + public f(a: &A.A) :u64 { + label b0: + return A.f(move(a)); + } + + public m(){ + let a: A.A; + let f: u64; + label b0: + a = A.new(1); + f = Self.f(&a); + assert(move(f) == 1, 42); + return; + } +} + +//# publish +module 0x44.C { + import 0x43.B; + + public entry m(){ + label b0: + B.m(); + return; + } +} + +//# run 0x44::C::m + + +//# publish --skip-check-struct-and-function-linking +module 0x42.A { + struct A has store,drop { + f: u64, + } + struct A1 has store,drop { + f: u64, + } + public new(f: u64): Self.A { + label b0: + return A { f: move(f) }; + } + // change the origin function signature + public f(a: &Self.A1) :u64 { + label b0: + return *&move(a).A1::f; + } +} + + +//# run 0x44::C::m diff --git a/language/move-vm/transactional-tests/tests/module_publishing/republish_module_skip_compatible_linking_hack_struct.exp b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_skip_compatible_linking_hack_struct.exp new file mode 100644 index 0000000000..f390bbc348 --- /dev/null +++ b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_skip_compatible_linking_hack_struct.exp @@ -0,0 +1,28 @@ +processed 5 tasks + +task 2 'publish'. lines 18-23: +Error: Unable to publish module '00000000000000000000000000000042::A'. Got VMError: { + major_status: BACKWARD_INCOMPATIBLE_MODULE_UPDATE, + sub_status: None, + location: undefined, + indices: [], + offsets: [], +} + +task 3 'publish'. lines 25-45: +Error: Unable to publish module '00000000000000000000000000000042::A'. Got VMError: { + major_status: BACKWARD_INCOMPATIBLE_MODULE_UPDATE, + sub_status: None, + location: undefined, + indices: [], + offsets: [], +} + +task 4 'run'. lines 47-47: +Error: Function execution failed with VMError: { + major_status: FUNCTION_RESOLUTION_FAILURE, + sub_status: None, + location: undefined, + indices: [], + offsets: [], +} diff --git a/language/move-vm/transactional-tests/tests/module_publishing/republish_module_skip_compatible_linking_hack_struct.mvir b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_skip_compatible_linking_hack_struct.mvir new file mode 100644 index 0000000000..7aaaff24b9 --- /dev/null +++ b/language/move-vm/transactional-tests/tests/module_publishing/republish_module_skip_compatible_linking_hack_struct.mvir @@ -0,0 +1,47 @@ +//# publish +module 0x42.A { + struct A has store,drop { + f: u64, + } + struct K has key, store,drop { + a: Self.A, + } + public entry make_save_k(sender: signer){ + label b0: + move_to(&sender, K { a: A { f: 100 } }); + return; + } +} + +//# run 0x42::A::make_save_k --signers 0x42 + +//# publish --skip-check-struct-and-function-linking +module 0x42.A { + struct A has store,drop { + f: u64, + } +} + +//# publish +module 0x42.A { + struct A has store,drop { + f: u64, + } + struct B has store,drop { + f: u64, + } + struct K has key, store,drop { + a: Self.B, + } + public entry take_check_k() acquires K { + let a: Self.B; + let f: u64; + label b0: + K { a } = move_from(0x42); + B { f } = move(a); + assert(move(f) == 100, 42); + return; + } +} + +//# run 0x42::A::take_check_k diff --git a/language/move-vm/types/Cargo.toml b/language/move-vm/types/Cargo.toml index 1ac48f4893..8a89a92feb 100644 --- a/language/move-vm/types/Cargo.toml +++ b/language/move-vm/types/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] once_cell = "1.7.2" @@ -15,7 +15,8 @@ proptest = { version = "1.0.0", optional = true } serde = { version = "1.0.124", features = ["derive", "rc"] } smallvec = "1.6.1" -bcs = "0.1.2" +bcs.workspace = true + move-core-types = { path = "../../move-core/types" } move-binary-format = { path = "../../move-binary-format" } diff --git a/language/move-vm/types/src/data_store.rs b/language/move-vm/types/src/data_store.rs index 7f0b6d4035..06992e8adb 100644 --- a/language/move-vm/types/src/data_store.rs +++ b/language/move-vm/types/src/data_store.rs @@ -8,7 +8,8 @@ use crate::{ }; use move_binary_format::errors::{PartialVMResult, VMResult}; use move_core_types::{ - account_address::AccountAddress, language_storage::ModuleId, value::MoveTypeLayout, + account_address::AccountAddress, gas_algebra::NumBytes, language_storage::ModuleId, + value::MoveTypeLayout, }; /// Provide an implementation for bytecodes related to data with a given data store. @@ -28,13 +29,18 @@ pub trait DataStore { &mut self, addr: AccountAddress, ty: &Type, - ) -> PartialVMResult<&mut GlobalValue>; + ) -> PartialVMResult<(&mut GlobalValue, Option>)>; /// Get the serialized format of a `CompiledModule` given a `ModuleId`. fn load_module(&self, module_id: &ModuleId) -> VMResult>; /// Publish a module. - fn publish_module(&mut self, module_id: &ModuleId, blob: Vec) -> VMResult<()>; + fn publish_module( + &mut self, + module_id: &ModuleId, + blob: Vec, + is_republishing: bool, + ) -> VMResult<()>; /// Check if this module exists. fn exists_module(&self, module_id: &ModuleId) -> VMResult; diff --git a/language/move-vm/types/src/gas.rs b/language/move-vm/types/src/gas.rs new file mode 100644 index 0000000000..0db5cdd9ce --- /dev/null +++ b/language/move-vm/types/src/gas.rs @@ -0,0 +1,369 @@ +// Copyright (c) The Move Contributors +// SPDX-License-Identifier: Apache-2.0 + +use crate::views::{TypeView, ValueView}; +use move_binary_format::errors::PartialVMResult; +use move_core_types::{ + gas_algebra::{InternalGas, NumArgs, NumBytes}, + language_storage::ModuleId, +}; + +/// Enum of instructions that do not need extra information for gas metering. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum SimpleInstruction { + Nop, + Ret, + + BrTrue, + BrFalse, + Branch, + + Pop, + LdU8, + LdU64, + LdU128, + LdTrue, + LdFalse, + + FreezeRef, + MutBorrowLoc, + ImmBorrowLoc, + ImmBorrowField, + MutBorrowField, + ImmBorrowFieldGeneric, + MutBorrowFieldGeneric, + + CastU8, + CastU64, + CastU128, + + Add, + Sub, + Mul, + Mod, + Div, + + BitOr, + BitAnd, + Xor, + Shl, + Shr, + + Or, + And, + Not, + + Lt, + Gt, + Le, + Ge, + + Abort, +} + +/// Trait that defines a generic gas meter interface, allowing clients of the Move VM to implement +/// their own metering scheme. +pub trait GasMeter { + /// Charge an instruction and fail if not enough gas units are left. + fn charge_simple_instr(&mut self, instr: SimpleInstruction) -> PartialVMResult<()>; + + fn charge_call( + &mut self, + module_id: &ModuleId, + func_name: &str, + args: impl ExactSizeIterator, + ) -> PartialVMResult<()>; + + fn charge_call_generic( + &mut self, + module_id: &ModuleId, + func_name: &str, + ty_args: impl ExactSizeIterator, + args: impl ExactSizeIterator, + ) -> PartialVMResult<()>; + + fn charge_ld_const(&mut self, size: NumBytes) -> PartialVMResult<()>; + + fn charge_copy_loc(&mut self, val: impl ValueView) -> PartialVMResult<()>; + + fn charge_move_loc(&mut self, val: impl ValueView) -> PartialVMResult<()>; + + fn charge_store_loc(&mut self, val: impl ValueView) -> PartialVMResult<()>; + + fn charge_pack( + &mut self, + is_generic: bool, + args: impl ExactSizeIterator, + ) -> PartialVMResult<()>; + + fn charge_unpack( + &mut self, + is_generic: bool, + args: impl ExactSizeIterator, + ) -> PartialVMResult<()>; + + fn charge_read_ref(&mut self, val: impl ValueView) -> PartialVMResult<()>; + + fn charge_write_ref(&mut self, val: impl ValueView) -> PartialVMResult<()>; + + fn charge_eq(&mut self, lhs: impl ValueView, rhs: impl ValueView) -> PartialVMResult<()>; + + fn charge_neq(&mut self, lhs: impl ValueView, rhs: impl ValueView) -> PartialVMResult<()>; + + fn charge_borrow_global( + &mut self, + is_mut: bool, + is_generic: bool, + ty: impl TypeView, + is_success: bool, + ) -> PartialVMResult<()>; + + fn charge_exists( + &mut self, + is_generic: bool, + ty: impl TypeView, + // TODO(Gas): see if we can get rid of this param + exists: bool, + ) -> PartialVMResult<()>; + + fn charge_move_from( + &mut self, + is_generic: bool, + ty: impl TypeView, + val: Option, + ) -> PartialVMResult<()>; + + fn charge_move_to( + &mut self, + is_generic: bool, + ty: impl TypeView, + val: impl ValueView, + is_success: bool, + ) -> PartialVMResult<()>; + + fn charge_vec_pack<'a>( + &mut self, + ty: impl TypeView + 'a, + args: impl ExactSizeIterator, + ) -> PartialVMResult<()>; + + fn charge_vec_len(&mut self, ty: impl TypeView) -> PartialVMResult<()>; + + fn charge_vec_borrow( + &mut self, + is_mut: bool, + ty: impl TypeView, + is_success: bool, + ) -> PartialVMResult<()>; + + fn charge_vec_push_back( + &mut self, + ty: impl TypeView, + val: impl ValueView, + ) -> PartialVMResult<()>; + + fn charge_vec_pop_back( + &mut self, + ty: impl TypeView, + val: Option, + ) -> PartialVMResult<()>; + + // TODO(Gas): Expose the elements + fn charge_vec_unpack( + &mut self, + ty: impl TypeView, + expect_num_elements: NumArgs, + ) -> PartialVMResult<()>; + + // TODO(Gas): Expose the two elements + fn charge_vec_swap(&mut self, ty: impl TypeView) -> PartialVMResult<()>; + + /// Charges for loading a resource from storage. This is only called when the resource is not + /// cached. + /// - `Some(n)` means `n` bytes are loaded. + /// - `None` means a load operation is performed but the resource does not exist. + /// + /// WARNING: This can be dangerous if you execute multiple user transactions in the same + /// session -- identical transactions can have different gas costs. Use at your own risk. + fn charge_load_resource(&mut self, loaded: Option) -> PartialVMResult<()>; + + /// Charge for executing a native function. + /// The cost is calculated returned by the native function implementation. + /// Should fail if not enough gas units are left. + /// + /// In the future, we may want to remove this and directly pass a reference to the GasMeter + /// instance to the native functions to allow gas to be deducted during computation. + fn charge_native_function(&mut self, amount: InternalGas) -> PartialVMResult<()>; +} + +/// A dummy gas meter that does not meter anything. +/// Charge operations will always succeed. +pub struct UnmeteredGasMeter; + +impl GasMeter for UnmeteredGasMeter { + fn charge_simple_instr(&mut self, _instr: SimpleInstruction) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_call( + &mut self, + _module_id: &ModuleId, + _func_name: &str, + _args: impl IntoIterator, + ) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_call_generic( + &mut self, + _module_id: &ModuleId, + _func_name: &str, + _ty_args: impl ExactSizeIterator, + _args: impl ExactSizeIterator, + ) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_ld_const(&mut self, _size: NumBytes) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_copy_loc(&mut self, _val: impl ValueView) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_move_loc(&mut self, _val: impl ValueView) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_store_loc(&mut self, _val: impl ValueView) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_pack( + &mut self, + _is_generic: bool, + _args: impl ExactSizeIterator, + ) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_unpack( + &mut self, + _is_generic: bool, + _args: impl ExactSizeIterator, + ) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_read_ref(&mut self, _val: impl ValueView) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_write_ref(&mut self, _val: impl ValueView) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_eq(&mut self, _lhs: impl ValueView, _rhs: impl ValueView) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_neq(&mut self, _lhs: impl ValueView, _rhs: impl ValueView) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_borrow_global( + &mut self, + _is_mut: bool, + _is_generic: bool, + _ty: impl TypeView, + _is_success: bool, + ) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_exists( + &mut self, + _is_generic: bool, + _ty: impl TypeView, + _exists: bool, + ) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_move_from( + &mut self, + _is_generic: bool, + _ty: impl TypeView, + _val: Option, + ) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_move_to( + &mut self, + _is_generic: bool, + _ty: impl TypeView, + _val: impl ValueView, + _is_success: bool, + ) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_vec_pack<'a>( + &mut self, + _ty: impl TypeView + 'a, + _args: impl ExactSizeIterator, + ) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_vec_len(&mut self, _ty: impl TypeView) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_vec_borrow( + &mut self, + _is_mut: bool, + _ty: impl TypeView, + _is_success: bool, + ) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_vec_push_back( + &mut self, + _ty: impl TypeView, + _val: impl ValueView, + ) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_vec_pop_back( + &mut self, + _ty: impl TypeView, + _val: Option, + ) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_vec_unpack( + &mut self, + _ty: impl TypeView, + _expect_num_elements: NumArgs, + ) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_vec_swap(&mut self, _ty: impl TypeView) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_load_resource(&mut self, _loaded: Option) -> PartialVMResult<()> { + Ok(()) + } + + fn charge_native_function(&mut self, _amount: InternalGas) -> PartialVMResult<()> { + Ok(()) + } +} diff --git a/language/move-vm/types/src/lib.rs b/language/move-vm/types/src/lib.rs index e857e9335d..e90386c0dd 100644 --- a/language/move-vm/types/src/lib.rs +++ b/language/move-vm/types/src/lib.rs @@ -23,10 +23,11 @@ macro_rules! debug_writeln { } pub mod data_store; -pub mod gas_schedule; +pub mod gas; pub mod loaded_data; pub mod natives; pub mod values; +pub mod views; #[cfg(test)] mod unit_tests; diff --git a/language/move-vm/types/src/loaded_data/runtime_types.rs b/language/move-vm/types/src/loaded_data/runtime_types.rs index fcbb050ebe..d445bf020b 100644 --- a/language/move-vm/types/src/loaded_data/runtime_types.rs +++ b/language/move-vm/types/src/loaded_data/runtime_types.rs @@ -6,13 +6,17 @@ use move_binary_format::{ errors::{PartialVMError, PartialVMResult}, file_format::{AbilitySet, StructDefinitionIndex, StructTypeParameter}, }; -use move_core_types::{identifier::Identifier, language_storage::ModuleId, vm_status::StatusCode}; +use move_core_types::{ + gas_algebra::AbstractMemorySize, identifier::Identifier, language_storage::ModuleId, + vm_status::StatusCode, +}; pub const TYPE_DEPTH_MAX: usize = 256; #[derive(Debug, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct StructType { pub fields: Vec, + pub field_names: Vec, pub abilities: AbilitySet, pub type_parameters: Vec, pub name: Identifier, @@ -98,4 +102,26 @@ impl Type { 1, ) } + + #[allow(deprecated)] + const LEGACY_BASE_MEMORY_SIZE: AbstractMemorySize = AbstractMemorySize::new(1); + + /// Returns the abstract memory size the data structure occupies. + /// + /// This kept only for legacy reasons. + /// New applications should not use this. + pub fn size(&self) -> AbstractMemorySize { + use Type::*; + + match self { + TyParam(_) | Bool | U8 | U64 | U128 | Address | Signer => Self::LEGACY_BASE_MEMORY_SIZE, + Vector(ty) | Reference(ty) | MutableReference(ty) => { + Self::LEGACY_BASE_MEMORY_SIZE + ty.size() + } + Struct(_) => Self::LEGACY_BASE_MEMORY_SIZE, + StructInstantiation(_, tys) => tys + .iter() + .fold(Self::LEGACY_BASE_MEMORY_SIZE, |acc, ty| acc + ty.size()), + } + } } diff --git a/language/move-vm/types/src/natives/function.rs b/language/move-vm/types/src/natives/function.rs index f7ea55c88e..d6bf71a280 100644 --- a/language/move-vm/types/src/natives/function.rs +++ b/language/move-vm/types/src/natives/function.rs @@ -18,13 +18,10 @@ //! function. use crate::values::Value; -use move_core_types::gas_schedule::{ - AbstractMemorySize, CostTable, GasAlgebra, GasCarrier, InternalGasUnits, -}; use smallvec::{smallvec, SmallVec}; pub use move_binary_format::errors::{PartialVMError, PartialVMResult}; -pub use move_core_types::vm_status::StatusCode; +pub use move_core_types::{gas_algebra::InternalGas, vm_status::StatusCode}; /// Result of a native function execution requires charges for execution cost. /// @@ -36,15 +33,14 @@ pub use move_core_types::vm_status::StatusCode; /// Errors (typically user errors and aborts) that are logically part of the function execution /// must be expressed in a `NativeResult` with a cost and a VMStatus. pub struct NativeResult { - /// The cost for running that function, whether successfully or not. - pub cost: InternalGasUnits, /// Result of execution. This is either the return values or the error to report. + pub cost: InternalGas, pub result: Result, u64>, } impl NativeResult { /// Return values of a successful execution. - pub fn ok(cost: InternalGasUnits, values: SmallVec<[Value; 1]>) -> Self { + pub fn ok(cost: InternalGas, values: SmallVec<[Value; 1]>) -> Self { NativeResult { cost, result: Ok(values), @@ -55,7 +51,7 @@ impl NativeResult { /// failure of the VM which would raise a `PartialVMError` error directly. /// The only thing the funciton can specify is its abort code, as if it had invoked the `Abort` /// bytecode instruction - pub fn err(cost: InternalGasUnits, abort_code: u64) -> Self { + pub fn err(cost: InternalGas, abort_code: u64) -> Self { NativeResult { cost, result: Err(abort_code), @@ -64,7 +60,7 @@ impl NativeResult { /// Convert a PartialVMResult<()> into a PartialVMResult pub fn map_partial_vm_result_empty( - cost: InternalGasUnits, + cost: InternalGas, res: PartialVMResult<()>, ) -> PartialVMResult { let result = match res { @@ -85,7 +81,7 @@ impl NativeResult { /// Convert a PartialVMResult into a PartialVMResult pub fn map_partial_vm_result_one( - cost: InternalGasUnits, + cost: InternalGas, res: PartialVMResult, ) -> PartialVMResult { let result = match res { @@ -105,19 +101,6 @@ impl NativeResult { } } -/// Return the native gas entry in `CostTable` for the given key. -/// The key is the specific native function index known to `CostTable`. -pub fn native_gas( - table: &CostTable, - native_table_idx: impl Into, - size: usize, -) -> InternalGasUnits { - let gas_amt = table.native_cost(native_table_idx.into()); - let memory_size = AbstractMemorySize::new(std::cmp::max(1, size) as GasCarrier); - debug_assert!(memory_size.get() > 0); - gas_amt.total().mul(memory_size) -} - /// Return the argument at the top of the stack. /// /// Arguments are passed to a native as a stack with first arg at the bottom of the stack. diff --git a/language/move-vm/types/src/values/value_tests.rs b/language/move-vm/types/src/values/value_tests.rs index a1a32db309..ddfe9453fe 100644 --- a/language/move-vm/types/src/values/value_tests.rs +++ b/language/move-vm/types/src/values/value_tests.rs @@ -2,8 +2,9 @@ // Copyright (c) The Move Contributors // SPDX-License-Identifier: Apache-2.0 -use crate::values::*; +use crate::{loaded_data::runtime_types::Type, values::*, views::*}; use move_binary_format::errors::*; +use move_core_types::account_address::AccountAddress; #[test] fn locals() -> PartialVMResult<()> { @@ -121,3 +122,78 @@ fn global_value_non_struct() -> PartialVMResult<()> { Ok(()) } + +#[test] +fn leagacy_ref_abstract_memory_size_consistency() -> PartialVMResult<()> { + let mut locals = Locals::new(10); + + locals.store_loc(0, Value::u128(0))?; + let r = locals.borrow_loc(0)?; + assert_eq!(r.legacy_abstract_memory_size(), r.legacy_size()); + + locals.store_loc(1, Value::vector_u8([1, 2, 3]))?; + let r = locals.borrow_loc(1)?; + assert_eq!(r.legacy_abstract_memory_size(), r.legacy_size()); + + let r: VectorRef = r.value_as()?; + let r = r.borrow_elem(0, &Type::U8)?; + assert_eq!(r.legacy_abstract_memory_size(), r.legacy_size()); + + locals.store_loc(2, Value::struct_(Struct::pack([])))?; + let r: Reference = locals.borrow_loc(2)?.value_as()?; + assert_eq!(r.legacy_abstract_memory_size(), r.legacy_size()); + + Ok(()) +} + +#[test] +fn legacy_struct_abstract_memory_size_consistenty() -> PartialVMResult<()> { + let structs = [ + Struct::pack([]), + Struct::pack([Value::struct_(Struct::pack([Value::u8(0), Value::u64(0)]))]), + ]; + + for s in &structs { + assert_eq!(s.legacy_abstract_memory_size(), s.legacy_size()); + } + + Ok(()) +} + +#[test] +fn legacy_val_abstract_memory_size_consistency() -> PartialVMResult<()> { + let vals = [ + Value::u8(0), + Value::u64(0), + Value::u128(0), + Value::bool(true), + Value::address(AccountAddress::ZERO), + Value::vector_u8([0, 1, 2]), + Value::vector_u64([]), + Value::vector_u128([1, 2, 3, 4]), + Value::struct_(Struct::pack([])), + Value::struct_(Struct::pack([Value::u8(0), Value::bool(false)])), + Value::vector_for_testing_only([]), + Value::vector_for_testing_only([Value::u8(0), Value::u8(1)]), + ]; + + let mut locals = Locals::new(vals.len()); + for (idx, val) in vals.iter().enumerate() { + locals.store_loc(idx, val.copy_value()?)?; + + let val_size_new = val.legacy_abstract_memory_size(); + let val_size_old = val.legacy_size(); + + assert_eq!(val_size_new, val_size_old); + + let val_size_through_ref = locals + .borrow_loc(idx)? + .value_as::()? + .value_view() + .legacy_abstract_memory_size(); + + assert_eq!(val_size_through_ref, val_size_old) + } + + Ok(()) +} diff --git a/language/move-vm/types/src/values/values_impl.rs b/language/move-vm/types/src/values/values_impl.rs index 4ffbe3d560..97ad688c3a 100644 --- a/language/move-vm/types/src/values/values_impl.rs +++ b/language/move-vm/types/src/values/values_impl.rs @@ -2,17 +2,18 @@ // Copyright (c) The Move Contributors // SPDX-License-Identifier: Apache-2.0 -use crate::loaded_data::runtime_types::Type; +use crate::{ + loaded_data::runtime_types::Type, + views::{ValueView, ValueVisitor}, +}; use move_binary_format::{ errors::*, file_format::{Constant, SignatureToken}, }; use move_core_types::{ account_address::AccountAddress, - gas_schedule::{ - AbstractMemorySize, GasAlgebra, GasCarrier, CONST_SIZE, MIN_EXISTS_DATA_SIZE, - REFERENCE_SIZE, STRUCT_SIZE, - }, + effects::Op, + gas_algebra::AbstractMemorySize, value::{MoveStructLayout, MoveTypeLayout}, vm_status::{sub_status::NFE_VECTOR_ERROR_BASE, StatusCode}, }; @@ -20,8 +21,6 @@ use std::{ cell::RefCell, fmt::{self, Debug, Display}, iter, - mem::size_of, - ops::Add, rc::Rc, }; @@ -30,7 +29,7 @@ use std::{ * Internal Types * * Internal representation of the Move value calculus. These types are abstractions - * over the concrete Move concepts and may carry additonal information that is not + * over the concrete Move concepts and may carry additional information that is not * defined by the language, but required by the implementation. * **************************************************************************************/ @@ -109,25 +108,6 @@ enum ReferenceImpl { ContainerRef(ContainerRef), } -// A reference to a signer. Clients can attempt a cast to this struct if they are -// expecting a Signer on the stack or as an argument. -#[derive(Debug)] -pub struct SignerRef(ContainerRef); - -// A reference to a vector. This is an alias for a ContainerRef for now but we may change -// it once Containers are restructured. -// It's used from vector native functions to get a reference to a vector and operate on that. -// There is an impl for VectorRef which implements the API private to this module. -#[derive(Debug)] -pub struct VectorRef(ContainerRef); - -// A vector. This is an alias for a Container for now but we may change -// it once Containers are restructured. -// It's used from vector native functions to get a vector and operate on that. -// There is an impl for Vector which implements the API private to this module. -#[derive(Debug)] -pub struct Vector(Container); - /*************************************************************************************** * * Public Types @@ -143,25 +123,11 @@ pub struct Vector(Container); * internal invariants are violated. * **************************************************************************************/ - -/// A reference to a Move struct that allows you to take a reference to one of its fields. -#[derive(Debug)] -pub struct StructRef(ContainerRef); - -/// A generic Move reference that offers two functionalities: read_ref & write_ref. -#[derive(Debug)] -pub struct Reference(ReferenceImpl); - /// A Move value -- a wrapper around `ValueImpl` which can be created only through valid /// means. #[derive(Debug)] pub struct Value(ValueImpl); -/// The locals for a function frame. It allows values to be read, written or taken -/// reference from. -#[derive(Debug)] -pub struct Locals(Rc>>); - /// An integer value in Move. #[derive(Debug)] pub enum IntegerValue { @@ -176,6 +142,33 @@ pub struct Struct { fields: Vec, } +// A vector. This is an alias for a Container for now but we may change +// it once Containers are restructured. +// It's used from vector native functions to get a vector and operate on that. +// There is an impl for Vector which implements the API private to this module. +#[derive(Debug)] +pub struct Vector(Container); + +/// A reference to a Move struct that allows you to take a reference to one of its fields. +#[derive(Debug)] +pub struct StructRef(ContainerRef); + +/// A generic Move reference that offers two functionalities: read_ref & write_ref. +#[derive(Debug)] +pub struct Reference(ReferenceImpl); + +// A reference to a signer. Clients can attempt a cast to this struct if they are +// expecting a Signer on the stack or as an argument. +#[derive(Debug)] +pub struct SignerRef(ContainerRef); + +// A reference to a vector. This is an alias for a ContainerRef for now but we may change +// it once Containers are restructured. +// It's used from vector native functions to get a reference to a vector and operate on that. +// There is an impl for VectorRef which implements the API private to this module. +#[derive(Debug)] +pub struct VectorRef(ContainerRef); + /// A special "slot" in global storage that can hold a resource. It also keeps track of the status /// of the resource relative to the global state, which is necessary to compute the effects to emit /// at the end of transaction execution. @@ -200,15 +193,10 @@ enum GlobalValueImpl { #[derive(Debug)] pub struct GlobalValue(GlobalValueImpl); -/// Simple enum for the change state of a GlobalValue, used by `into_effect`. -pub enum GlobalValueEffect { - /// There was no value, or the value was not changed - None, - /// The value was removed - Deleted, - /// Updated with a new value - Changed(T), -} +/// The locals for a function frame. It allows values to be read, written or taken +/// reference from. +#[derive(Debug)] +pub struct Locals(Rc>>); /*************************************************************************************** * @@ -429,7 +417,7 @@ impl Value { * * Equality tests of Move values. Errors are raised when types mismatch. * - * It is intented to NOT use or even implement the standard library traits Eq and + * It is intended to NOT use or even implement the standard library traits Eq and * Partial Eq due to: * 1. They do not allow errors to be returned. * 2. They can be invoked without the user being noticed thanks to operator @@ -618,7 +606,7 @@ impl IndexedRef { fn read_ref(self) -> PartialVMResult { use Container::*; - let res = match &*self.container_ref.container() { + let res = match self.container_ref.container() { Locals(r) | Vec(r) | Struct(r) => r.borrow()[self.idx].copy_value()?, VecU8(r) => ValueImpl::U8(r.borrow()[self.idx]), VecU64(r) => ValueImpl::U64(r.borrow()[self.idx]), @@ -1634,7 +1622,7 @@ impl VectorRef { Ok(Value(self.0.borrow_elem(idx)?)) } - /// Returns a Refcell reference to the underlying vector of a `&vector` value. + /// Returns a RefCell reference to the underlying vector of a `&vector` value. pub fn as_bytes_ref(&self) -> std::cell::Ref<'_, Vec> { let c = self.0.container(); match c { @@ -1825,101 +1813,116 @@ impl Vector { /*************************************************************************************** * - * Gas + * Abstract Memory Size * - * Abstract memory sizes of the VM values. + * TODO(Gas): This is the oldest implementation of abstract memory size. + * It is now kept only as a reference impl, which is used to ensure + * the new implementation is fully backward compatible. + * We should be able to get this removed after we use the new impl + * for a while and gain enough confidence in that. * **************************************************************************************/ +/// The size in bytes for a non-string or address constant on the stack +pub(crate) const LEGACY_CONST_SIZE: AbstractMemorySize = AbstractMemorySize::new(16); + +/// The size in bytes for a reference on the stack +pub(crate) const LEGACY_REFERENCE_SIZE: AbstractMemorySize = AbstractMemorySize::new(8); + +/// The size of a struct in bytes +pub(crate) const LEGACY_STRUCT_SIZE: AbstractMemorySize = AbstractMemorySize::new(2); + impl Container { - fn size(&self) -> AbstractMemorySize { + #[cfg(test)] + fn legacy_size(&self) -> AbstractMemorySize { match self { - Self::Locals(r) | Self::Vec(r) | Self::Struct(r) => Struct::size_impl(&*r.borrow()), - Self::VecU8(r) => AbstractMemorySize::new((r.borrow().len() * size_of::()) as u64), + Self::Locals(r) | Self::Vec(r) | Self::Struct(r) => { + Struct::legacy_size_impl(&*r.borrow()) + } + Self::VecU8(r) => { + AbstractMemorySize::new((r.borrow().len() * std::mem::size_of::()) as u64) + } Self::VecU64(r) => { - AbstractMemorySize::new((r.borrow().len() * size_of::()) as u64) + AbstractMemorySize::new((r.borrow().len() * std::mem::size_of::()) as u64) } Self::VecU128(r) => { - AbstractMemorySize::new((r.borrow().len() * size_of::()) as u64) + AbstractMemorySize::new((r.borrow().len() * std::mem::size_of::()) as u64) } Self::VecBool(r) => { - AbstractMemorySize::new((r.borrow().len() * size_of::()) as u64) - } - Self::VecAddress(r) => { - AbstractMemorySize::new((r.borrow().len() * size_of::()) as u64) + AbstractMemorySize::new((r.borrow().len() * std::mem::size_of::()) as u64) } + Self::VecAddress(r) => AbstractMemorySize::new( + (r.borrow().len() * std::mem::size_of::()) as u64, + ), } } } impl ContainerRef { - fn size(&self) -> AbstractMemorySize { - REFERENCE_SIZE + #[cfg(test)] + fn legacy_size(&self) -> AbstractMemorySize { + LEGACY_REFERENCE_SIZE } } impl IndexedRef { - fn size(&self) -> AbstractMemorySize { - REFERENCE_SIZE + #[cfg(test)] + fn legacy_size(&self) -> AbstractMemorySize { + LEGACY_REFERENCE_SIZE } } impl ValueImpl { - fn size(&self) -> AbstractMemorySize { + #[cfg(test)] + fn legacy_size(&self) -> AbstractMemorySize { use ValueImpl::*; match self { - Invalid | U8(_) | U64(_) | U128(_) | Bool(_) => CONST_SIZE, + Invalid | U8(_) | U64(_) | U128(_) | Bool(_) => LEGACY_CONST_SIZE, Address(_) => AbstractMemorySize::new(AccountAddress::LENGTH as u64), - ContainerRef(r) => r.size(), - IndexedRef(r) => r.size(), + ContainerRef(r) => r.legacy_size(), + IndexedRef(r) => r.legacy_size(), // TODO: in case the borrow fails the VM will panic. - Container(c) => c.size(), + Container(c) => c.legacy_size(), } } } impl Struct { - fn size_impl(fields: &[ValueImpl]) -> AbstractMemorySize { + #[cfg(test)] + fn legacy_size_impl(fields: &[ValueImpl]) -> AbstractMemorySize { fields .iter() - .fold(STRUCT_SIZE, |acc, v| acc.map2(v.size(), Add::add)) + .fold(LEGACY_STRUCT_SIZE, |acc, v| acc + v.legacy_size()) } - pub fn size(&self) -> AbstractMemorySize { - Self::size_impl(&self.fields) + #[cfg(test)] + pub(crate) fn legacy_size(&self) -> AbstractMemorySize { + Self::legacy_size_impl(&self.fields) } } impl Value { - pub fn size(&self) -> AbstractMemorySize { - self.0.size() + #[cfg(test)] + pub(crate) fn legacy_size(&self) -> AbstractMemorySize { + self.0.legacy_size() } } impl ReferenceImpl { - fn size(&self) -> AbstractMemorySize { + #[cfg(test)] + fn legacy_size(&self) -> AbstractMemorySize { match self { - Self::ContainerRef(r) => r.size(), - Self::IndexedRef(r) => r.size(), + Self::ContainerRef(r) => r.legacy_size(), + Self::IndexedRef(r) => r.legacy_size(), } } } impl Reference { - pub fn size(&self) -> AbstractMemorySize { - self.0.size() - } -} - -impl GlobalValue { - pub fn size(&self) -> AbstractMemorySize { - // REVIEW: this doesn't seem quite right. Consider changing it to - // a constant positive size or better, something proportional to the size of the value. - match &self.0 { - GlobalValueImpl::Fresh { .. } | GlobalValueImpl::Cached { .. } => REFERENCE_SIZE, - GlobalValueImpl::Deleted | GlobalValueImpl::None => MIN_EXISTS_DATA_SIZE, - } + #[cfg(test)] + pub(crate) fn legacy_size(&self) -> AbstractMemorySize { + self.0.legacy_size() } } @@ -1953,26 +1956,31 @@ impl Struct { **************************************************************************************/ #[allow(clippy::unnecessary_wraps)] impl GlobalValueImpl { - fn cached(val: ValueImpl, status: GlobalDataStatus) -> PartialVMResult { + fn cached( + val: ValueImpl, + status: GlobalDataStatus, + ) -> Result { match val { ValueImpl::Container(Container::Struct(fields)) => { let status = Rc::new(RefCell::new(status)); Ok(Self::Cached { fields, status }) } - _ => Err( + val => Err(( PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR) .with_message("failed to publish cached: not a resource".to_string()), - ), + val, + )), } } - fn fresh(val: ValueImpl) -> PartialVMResult { + fn fresh(val: ValueImpl) -> Result { match val { ValueImpl::Container(Container::Struct(fields)) => Ok(Self::Fresh { fields }), - _ => Err( + val => Err(( PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR) .with_message("failed to publish fresh: not a resource".to_string()), - ), + val, + )), } } @@ -1999,10 +2007,13 @@ impl GlobalValueImpl { Ok(ValueImpl::Container(Container::Struct(fields))) } - fn move_to(&mut self, val: ValueImpl) -> PartialVMResult<()> { + fn move_to(&mut self, val: ValueImpl) -> Result<(), (PartialVMError, ValueImpl)> { match self { Self::Fresh { .. } | Self::Cached { .. } => { - return Err(PartialVMError::new(StatusCode::RESOURCE_ALREADY_EXISTS)) + return Err(( + PartialVMError::new(StatusCode::RESOURCE_ALREADY_EXISTS), + val, + )) } Self::None => *self = Self::fresh(val)?, Self::Deleted => *self = Self::cached(val, GlobalDataStatus::Dirty)?, @@ -2030,20 +2041,20 @@ impl GlobalValueImpl { } } - fn into_effect(self) -> PartialVMResult> { - Ok(match self { - Self::None => GlobalValueEffect::None, - Self::Deleted => GlobalValueEffect::Deleted, + fn into_effect(self) -> Option> { + match self { + Self::None => None, + Self::Deleted => Some(Op::Delete), Self::Fresh { fields } => { - GlobalValueEffect::Changed(ValueImpl::Container(Container::Struct(fields))) + Some(Op::New(ValueImpl::Container(Container::Struct(fields)))) } Self::Cached { fields, status } => match &*status.borrow() { GlobalDataStatus::Dirty => { - GlobalValueEffect::Changed(ValueImpl::Container(Container::Struct(fields))) + Some(Op::Modify(ValueImpl::Container(Container::Struct(fields)))) } - GlobalDataStatus::Clean => GlobalValueEffect::None, + GlobalDataStatus::Clean => None, }, - }) + } } fn is_mutated(&self) -> bool { @@ -2065,18 +2076,19 @@ impl GlobalValue { } pub fn cached(val: Value) -> PartialVMResult { - Ok(Self(GlobalValueImpl::cached( - val.0, - GlobalDataStatus::Clean, - )?)) + Ok(Self( + GlobalValueImpl::cached(val.0, GlobalDataStatus::Clean).map_err(|(err, _val)| err)?, + )) } pub fn move_from(&mut self) -> PartialVMResult { Ok(Value(self.0.move_from()?)) } - pub fn move_to(&mut self, val: Value) -> PartialVMResult<()> { - self.0.move_to(val.0) + pub fn move_to(&mut self, val: Value) -> Result<(), (PartialVMError, Value)> { + self.0 + .move_to(val.0) + .map_err(|(err, val)| (err, Value(val))) } pub fn borrow_global(&self) -> PartialVMResult { @@ -2087,12 +2099,8 @@ impl GlobalValue { self.0.exists() } - pub fn into_effect(self) -> PartialVMResult> { - Ok(match self.0.into_effect()? { - GlobalValueEffect::None => GlobalValueEffect::None, - GlobalValueEffect::Deleted => GlobalValueEffect::Deleted, - GlobalValueEffect::Changed(v) => GlobalValueEffect::Changed(Value(v)), - }) + pub fn into_effect(self) -> Option> { + self.0.into_effect().map(|op| op.map(Value)) } pub fn is_mutated(&self) -> bool { @@ -2631,7 +2639,7 @@ impl<'d, 'a> serde::de::Visitor<'d> for StructFieldVisitor<'a> { * * Constants * -* Implementation of deseserialization of constant data into a runtime value +* Implementation of deserialization of constant data into a runtime value * **************************************************************************************/ @@ -2661,6 +2669,229 @@ impl Value { } } +/*************************************************************************************** +* +* Views +* +**************************************************************************************/ +impl Container { + fn visit_impl(&self, visitor: &mut impl ValueVisitor, depth: usize) { + use Container::*; + + match self { + Locals(_) => unreachable!("Should not ba able to visit a Locals container directly"), + Vec(r) => { + let r = r.borrow(); + if visitor.visit_vec(depth, r.len()) { + for val in r.iter() { + val.visit_impl(visitor, depth + 1); + } + } + } + Struct(r) => { + let r = r.borrow(); + if visitor.visit_struct(depth, r.len()) { + for val in r.iter() { + val.visit_impl(visitor, depth + 1); + } + } + } + VecU8(r) => visitor.visit_vec_u8(depth, &*r.borrow()), + VecU64(r) => visitor.visit_vec_u64(depth, &*r.borrow()), + VecU128(r) => visitor.visit_vec_u128(depth, &*r.borrow()), + VecBool(r) => visitor.visit_vec_bool(depth, &*r.borrow()), + VecAddress(r) => visitor.visit_vec_address(depth, &*r.borrow()), + } + } + + fn visit_indexed(&self, visitor: &mut impl ValueVisitor, depth: usize, idx: usize) { + use Container::*; + + match self { + Locals(r) | Vec(r) | Struct(r) => r.borrow()[idx].visit_impl(visitor, depth + 1), + VecU8(vals) => visitor.visit_u8(depth + 1, vals.borrow()[idx]), + VecU64(vals) => visitor.visit_u64(depth + 1, vals.borrow()[idx]), + VecU128(vals) => visitor.visit_u128(depth + 1, vals.borrow()[idx]), + VecBool(vals) => visitor.visit_bool(depth + 1, vals.borrow()[idx]), + VecAddress(vals) => visitor.visit_address(depth + 1, vals.borrow()[idx]), + } + } +} + +impl ContainerRef { + fn visit_impl(&self, visitor: &mut impl ValueVisitor, depth: usize) { + use ContainerRef::*; + + let (container, is_global) = match self { + Local(container) => (container, false), + Global { container, .. } => (container, false), + }; + + if visitor.visit_ref(depth, is_global) { + container.visit_impl(visitor, depth + 1); + } + } +} + +impl IndexedRef { + fn visit_impl(&self, visitor: &mut impl ValueVisitor, depth: usize) { + use ContainerRef::*; + + let (container, is_global) = match &self.container_ref { + Local(container) => (container, false), + Global { container, .. } => (container, false), + }; + + if visitor.visit_ref(depth, is_global) { + container.visit_indexed(visitor, depth, self.idx) + } + } +} + +impl ValueImpl { + fn visit_impl(&self, visitor: &mut impl ValueVisitor, depth: usize) { + use ValueImpl::*; + + match self { + Invalid => unreachable!("Should not be able to visit an invalid value"), + + U8(val) => visitor.visit_u8(depth, *val), + U64(val) => visitor.visit_u64(depth, *val), + U128(val) => visitor.visit_u128(depth, *val), + Bool(val) => visitor.visit_bool(depth, *val), + Address(val) => visitor.visit_address(depth, *val), + + Container(c) => c.visit_impl(visitor, depth), + + ContainerRef(r) => r.visit_impl(visitor, depth), + IndexedRef(r) => r.visit_impl(visitor, depth), + } + } +} + +impl ValueView for ValueImpl { + fn visit(&self, visitor: &mut impl ValueVisitor) { + self.visit_impl(visitor, 0) + } +} + +impl ValueView for Value { + fn visit(&self, visitor: &mut impl ValueVisitor) { + self.0.visit(visitor) + } +} + +impl ValueView for Struct { + fn visit(&self, visitor: &mut impl ValueVisitor) { + if visitor.visit_struct(0, self.fields.len()) { + for val in self.fields.iter() { + val.visit_impl(visitor, 1); + } + } + } +} + +impl ValueView for Vector { + fn visit(&self, visitor: &mut impl ValueVisitor) { + self.0.visit_impl(visitor, 0) + } +} + +impl ValueView for IntegerValue { + fn visit(&self, visitor: &mut impl ValueVisitor) { + use IntegerValue::*; + + match self { + U8(val) => visitor.visit_u8(0, *val), + U64(val) => visitor.visit_u64(0, *val), + U128(val) => visitor.visit_u128(0, *val), + } + } +} + +impl ValueView for Reference { + fn visit(&self, visitor: &mut impl ValueVisitor) { + use ReferenceImpl::*; + + match &self.0 { + ContainerRef(r) => r.visit_impl(visitor, 0), + IndexedRef(r) => r.visit_impl(visitor, 0), + } + } +} + +impl ValueView for VectorRef { + fn visit(&self, visitor: &mut impl ValueVisitor) { + self.0.visit_impl(visitor, 0) + } +} + +impl ValueView for StructRef { + fn visit(&self, visitor: &mut impl ValueVisitor) { + self.0.visit_impl(visitor, 0) + } +} + +impl ValueView for SignerRef { + fn visit(&self, visitor: &mut impl ValueVisitor) { + self.0.visit_impl(visitor, 0) + } +} + +// Note: We may want to add more helpers to retrieve value views behind references here. + +impl Struct { + #[allow(clippy::needless_lifetimes)] + pub fn field_views<'a>(&'a self) -> impl ExactSizeIterator { + self.fields.iter() + } +} + +impl Reference { + #[allow(clippy::needless_lifetimes)] + pub fn value_view<'a>(&'a self) -> impl ValueView + 'a { + struct ValueBehindRef<'b>(&'b ReferenceImpl); + + impl<'b> ValueView for ValueBehindRef<'b> { + fn visit(&self, visitor: &mut impl ValueVisitor) { + use ReferenceImpl::*; + + match self.0 { + ContainerRef(r) => r.container().visit_impl(visitor, 0), + IndexedRef(r) => r.container_ref.container().visit_indexed(visitor, 0, r.idx), + } + } + } + + ValueBehindRef(&self.0) + } +} + +impl GlobalValue { + #[allow(clippy::needless_lifetimes)] + pub fn view<'a>(&'a self) -> Option { + use GlobalValueImpl as G; + + struct Wrapper<'b>(&'b Rc>>); + + impl<'b> ValueView for Wrapper<'b> { + fn visit(&self, visitor: &mut impl ValueVisitor) { + let r = self.0.borrow(); + if visitor.visit_struct(0, r.len()) { + for val in r.iter() { + val.visit_impl(visitor, 1); + } + } + } + } + + match &self.0 { + G::None | G::Deleted => None, + G::Cached { fields, .. } | G::Fresh { fields } => Some(Wrapper(fields)), + } + } +} + /*************************************************************************************** * * Prop Testing diff --git a/language/move-vm/types/src/views.rs b/language/move-vm/types/src/views.rs new file mode 100644 index 0000000000..4f49cab7eb --- /dev/null +++ b/language/move-vm/types/src/views.rs @@ -0,0 +1,170 @@ +// Copyright (c) The Move Contributors +// SPDX-License-Identifier: Apache-2.0 + +use move_core_types::{ + account_address::AccountAddress, gas_algebra::AbstractMemorySize, language_storage::TypeTag, +}; +use std::mem::size_of; + +/// Trait that provides an abstract view into a Move type. +/// +/// This is used to expose certain info to clients (e.g. the gas meter), +/// usually in a lazily evaluated fashion. +pub trait TypeView { + /// Returns the `TypeTag` (fully qualified name) of the type. + fn to_type_tag(&self) -> TypeTag; +} + +/// Trait that provides an abstract view into a Move Value. +/// +/// This is used to expose certain info to clients (e.g. the gas meter), +/// usually in a lazily evaluated fashion. +pub trait ValueView { + fn visit(&self, visitor: &mut impl ValueVisitor); + + /// Returns the abstract memory size of the value. + /// + /// The concept of abstract memory size is not well-defined and is only kept for backward compatibility. + /// New applications should avoid using this. + /// + /// TODO(Gas): Encourage clients to replicate this in their own repo and get this removed once + /// they are done. + fn legacy_abstract_memory_size(&self) -> AbstractMemorySize { + use crate::values::{LEGACY_CONST_SIZE, LEGACY_REFERENCE_SIZE, LEGACY_STRUCT_SIZE}; + + struct Acc(AbstractMemorySize); + + impl ValueVisitor for Acc { + fn visit_u8(&mut self, _depth: usize, _val: u8) { + self.0 += LEGACY_CONST_SIZE; + } + + fn visit_u64(&mut self, _depth: usize, _val: u64) { + self.0 += LEGACY_CONST_SIZE; + } + + fn visit_u128(&mut self, _depth: usize, _val: u128) { + self.0 += LEGACY_CONST_SIZE; + } + + fn visit_bool(&mut self, _depth: usize, _val: bool) { + self.0 += LEGACY_CONST_SIZE; + } + + fn visit_address(&mut self, _depth: usize, _val: AccountAddress) { + self.0 += AbstractMemorySize::new(AccountAddress::LENGTH as u64); + } + + fn visit_struct(&mut self, _depth: usize, _len: usize) -> bool { + self.0 += LEGACY_STRUCT_SIZE; + true + } + + fn visit_vec(&mut self, _depth: usize, _len: usize) -> bool { + self.0 += LEGACY_STRUCT_SIZE; + true + } + + fn visit_vec_u8(&mut self, _depth: usize, vals: &[u8]) { + self.0 += ((size_of::() * vals.len()) as u64).into(); + } + + fn visit_vec_u64(&mut self, _depth: usize, vals: &[u64]) { + self.0 += ((size_of::() * vals.len()) as u64).into(); + } + + fn visit_vec_u128(&mut self, _depth: usize, vals: &[u128]) { + self.0 += ((size_of::() * vals.len()) as u64).into(); + } + + fn visit_vec_bool(&mut self, _depth: usize, vals: &[bool]) { + self.0 += ((size_of::() * vals.len()) as u64).into(); + } + + fn visit_vec_address(&mut self, _depth: usize, vals: &[AccountAddress]) { + self.0 += ((size_of::() * vals.len()) as u64).into(); + } + + fn visit_ref(&mut self, _depth: usize, _is_global: bool) -> bool { + self.0 += LEGACY_REFERENCE_SIZE; + false + } + } + + let mut acc = Acc(0.into()); + self.visit(&mut acc); + + acc.0 + } +} + +/// Trait that defines a visitor that could be used to traverse a value recursively. +pub trait ValueVisitor { + fn visit_u8(&mut self, depth: usize, val: u8); + fn visit_u64(&mut self, depth: usize, val: u64); + fn visit_u128(&mut self, depth: usize, val: u128); + fn visit_bool(&mut self, depth: usize, val: bool); + fn visit_address(&mut self, depth: usize, val: AccountAddress); + + fn visit_struct(&mut self, depth: usize, len: usize) -> bool; + fn visit_vec(&mut self, depth: usize, len: usize) -> bool; + + fn visit_ref(&mut self, depth: usize, is_global: bool) -> bool; + + fn visit_vec_u8(&mut self, depth: usize, vals: &[u8]) { + self.visit_vec(depth, vals.len()); + for val in vals { + self.visit_u8(depth + 1, *val); + } + } + + fn visit_vec_u64(&mut self, depth: usize, vals: &[u64]) { + self.visit_vec(depth, vals.len()); + for val in vals { + self.visit_u64(depth + 1, *val); + } + } + + fn visit_vec_u128(&mut self, depth: usize, vals: &[u128]) { + self.visit_vec(depth, vals.len()); + for val in vals { + self.visit_u128(depth + 1, *val); + } + } + + fn visit_vec_bool(&mut self, depth: usize, vals: &[bool]) { + self.visit_vec(depth, vals.len()); + for val in vals { + self.visit_bool(depth + 1, *val); + } + } + + fn visit_vec_address(&mut self, depth: usize, vals: &[AccountAddress]) { + self.visit_vec(depth, vals.len()); + for val in vals { + self.visit_address(depth + 1, *val); + } + } +} + +impl ValueView for &T +where + T: ValueView, +{ + fn legacy_abstract_memory_size(&self) -> AbstractMemorySize { + ::legacy_abstract_memory_size(*self) + } + + fn visit(&self, visitor: &mut impl ValueVisitor) { + ::visit(*self, visitor) + } +} + +impl TypeView for &T +where + T: TypeView, +{ + fn to_type_tag(&self) -> TypeTag { + ::to_type_tag(*self) + } +} diff --git a/language/testing-infra/module-generation/Cargo.toml b/language/testing-infra/module-generation/Cargo.toml index 3b4e4611c8..c3b1cb5459 100644 --- a/language/testing-infra/module-generation/Cargo.toml +++ b/language/testing-infra/module-generation/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] rand = "0.8.3" diff --git a/language/testing-infra/test-generation/Cargo.toml b/language/testing-infra/test-generation/Cargo.toml index 83ac8f35ff..7e1366721c 100644 --- a/language/testing-infra/test-generation/Cargo.toml +++ b/language/testing-infra/test-generation/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] rand = "0.8.3" @@ -18,7 +18,7 @@ hex = "0.4.3" getrandom = "0.2.2" crossbeam-channel = "0.5.0" tracing = "0.1.26" -tracing-subscriber = { version = "0.3.3", features = ["env-filter"] } +tracing-subscriber = { version = "0.3.15", features = ["env-filter"] } once_cell = "1.7.2" move-bytecode-verifier = { path = "../../move-bytecode-verifier" } diff --git a/language/testing-infra/test-generation/src/abstract_state.rs b/language/testing-infra/test-generation/src/abstract_state.rs index 2f374971b7..f0c179c5d2 100644 --- a/language/testing-infra/test-generation/src/abstract_state.rs +++ b/language/testing-infra/test-generation/src/abstract_state.rs @@ -19,14 +19,14 @@ use std::{ /// The BorrowState denotes whether a local is `Available` or /// has been moved and is `Unavailable`. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum BorrowState { Available, Unavailable, } /// This models a value on the stack or in the locals -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct AbstractValue { /// Represents the type of the value pub token: SignatureToken, @@ -36,7 +36,7 @@ pub struct AbstractValue { } /// This models the mutability of a reference -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum Mutability { /// Represents a mutable reference Mutable, @@ -118,7 +118,7 @@ impl AbstractValue { } } -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct CallGraph { calls: HashMap>, max_function_handle_index: usize, diff --git a/language/testing-infra/test-generation/src/borrow_graph.rs b/language/testing-infra/test-generation/src/borrow_graph.rs index 1684efde3b..df1ea3fde6 100644 --- a/language/testing-infra/test-generation/src/borrow_graph.rs +++ b/language/testing-infra/test-generation/src/borrow_graph.rs @@ -23,7 +23,7 @@ type Edge = (PartitionID, PartitionID, Path, EdgeType); /// The `EdgeType` is either weak or strong. A weak edge represents imprecise information /// on the path along which the borrow takes place. A strong edge is precise. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum EdgeType { Weak, Strong, @@ -31,7 +31,7 @@ pub enum EdgeType { /// The `BorrowGraph` stores information sufficient to determine whether the instruction /// of a bytecode instruction that interacts with references is memory safe. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct BorrowGraph { /// All of the partitions that make up the graph partitions: Vec, diff --git a/language/testing-infra/test-generation/src/bytecode_generator.rs b/language/testing-infra/test-generation/src/bytecode_generator.rs index d28f4765f0..ec0d5b1d00 100644 --- a/language/testing-infra/test-generation/src/bytecode_generator.rs +++ b/language/testing-infra/test-generation/src/bytecode_generator.rs @@ -116,7 +116,7 @@ enum StackEffect { } /// Context containing information about a function -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct FunctionGenerationContext { pub function_handle_index: FunctionHandleIndex, pub starting_call_height: usize, @@ -387,23 +387,23 @@ impl<'a> BytecodeGenerator<'a> { } BytecodeType::ConstantPoolIndex(instruction) => { // Select a random address from the module's address pool - Self::index_or_none(&module.constant_pool, &mut self.rng) + Self::index_or_none(&module.constant_pool, self.rng) .map(|x| instruction(ConstantPoolIndex::new(x))) } BytecodeType::StructIndex(instruction) => { // Select a random struct definition and local signature - Self::index_or_none(&module.struct_defs, &mut self.rng) + Self::index_or_none(&module.struct_defs, self.rng) .map(|x| instruction(StructDefinitionIndex::new(x))) } BytecodeType::FieldHandleIndex(instruction) => { // Select a field definition from the module's field definitions - Self::index_or_none(&module.field_handles, &mut self.rng) + Self::index_or_none(&module.field_handles, self.rng) .map(|x| instruction(FieldHandleIndex::new(x))) } BytecodeType::FunctionIndex(instruction) => { // Select a random function handle and local signature let callable_fns = &state.call_graph.can_call(fn_context.function_handle_index); - Self::index_or_none(callable_fns, &mut self.rng) + Self::index_or_none(callable_fns, self.rng) .and_then(|handle_idx| { Self::call_stack_backpressure( &state, @@ -415,17 +415,17 @@ impl<'a> BytecodeGenerator<'a> { } BytecodeType::StructInstantiationIndex(instruction) => { // Select a field definition from the module's field definitions - Self::index_or_none(&module.struct_def_instantiations, &mut self.rng) + Self::index_or_none(&module.struct_def_instantiations, self.rng) .map(|x| instruction(StructDefInstantiationIndex::new(x))) } BytecodeType::FunctionInstantiationIndex(instruction) => { // Select a field definition from the module's field definitions - Self::index_or_none(&module.function_instantiations, &mut self.rng) + Self::index_or_none(&module.function_instantiations, self.rng) .map(|x| instruction(FunctionInstantiationIndex::new(x))) } BytecodeType::FieldInstantiationIndex(instruction) => { // Select a field definition from the module's field definitions - Self::index_or_none(&module.field_instantiations, &mut self.rng) + Self::index_or_none(&module.field_instantiations, self.rng) .map(|x| instruction(FieldInstantiationIndex::new(x))) } }; @@ -729,7 +729,7 @@ impl<'a> BytecodeGenerator<'a> { // generation range. debug_assert!(number_of_blocks > 0); let mut cfg = CFG::new( - &mut self.rng, + self.rng, locals, &module.signatures[fh.parameters.0 as usize], number_of_blocks, diff --git a/language/testing-infra/test-generation/src/lib.rs b/language/testing-infra/test-generation/src/lib.rs index 49958e432c..44ff4f8c4e 100644 --- a/language/testing-infra/test-generation/src/lib.rs +++ b/language/testing-infra/test-generation/src/lib.rs @@ -28,7 +28,7 @@ use move_bytecode_verifier::verify_module; use move_compiler::{compiled_unit::AnnotatedCompiledUnit, Compiler}; use move_core_types::{ account_address::AccountAddress, - effects::ChangeSet, + effects::{ChangeSet, Op}, language_storage::TypeTag, resolver::MoveResolver, value::MoveValue, @@ -36,7 +36,7 @@ use move_core_types::{ }; use move_vm_runtime::move_vm::MoveVM; use move_vm_test_utils::{DeltaStorage, InMemoryStorage}; -use move_vm_types::gas_schedule::GasStatus; +use move_vm_types::gas::UnmeteredGasMeter; use once_cell::sync::Lazy; use rand::{rngs::StdRng, Rng, SeedableRng}; use std::{fs, io::Write, panic, thread}; @@ -126,23 +126,25 @@ fn execute_function_in_module( { let vm = MoveVM::new(move_stdlib::natives::all_natives( AccountAddress::from_hex_literal("0x1").unwrap(), + move_stdlib::natives::GasParameters::zeros(), )) .unwrap(); let mut changeset = ChangeSet::new(); let mut blob = vec![]; module.serialize(&mut blob).unwrap(); - changeset.publish_or_overwrite_module(module_id.clone(), blob); + changeset + .add_module_op(module_id.clone(), Op::New(blob)) + .unwrap(); let delta_storage = DeltaStorage::new(storage, &changeset); let mut sess = vm.new_session(&delta_storage); - let mut gas_status = GasStatus::new_unmetered(); sess.execute_function_bypass_visibility( &module_id, entry_name, ty_args, args, - &mut gas_status, + &mut UnmeteredGasMeter, )?; Ok(()) @@ -189,7 +191,7 @@ fn seed(seed: Option) -> [u8; 32] { array } -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum Status { VerificationFailure, ExecutionFailure, diff --git a/language/testing-infra/test-generation/src/transitions.rs b/language/testing-infra/test-generation/src/transitions.rs index cd969fefca..6b19fdff3a 100644 --- a/language/testing-infra/test-generation/src/transitions.rs +++ b/language/testing-infra/test-generation/src/transitions.rs @@ -1350,16 +1350,16 @@ macro_rules! state_never { #[macro_export] macro_rules! state_stack_bin_op { (#left) => { - Box::new(move |state| stack_bin_op(state, crate::transitions::StackBinOpResult::Left)) + Box::new(move |state| stack_bin_op(state, $crate::transitions::StackBinOpResult::Left)) }; (#right) => { - Box::new(move |state| stack_bin_op(state, crate::transitions::StackBinOpResult::Right)) + Box::new(move |state| stack_bin_op(state, $crate::transitions::StackBinOpResult::Right)) }; () => { state_stack_bin_op!(#left) }; ($e: expr) => { - Box::new(move |state| stack_bin_op(state, crate::transitions::StackBinOpResult::Other($e))) + Box::new(move |state| stack_bin_op(state, $crate::transitions::StackBinOpResult::Other($e))) } } diff --git a/language/testing-infra/transactional-test-runner/Cargo.toml b/language/testing-infra/transactional-test-runner/Cargo.toml index 798ecae63d..cb99bbcf4d 100644 --- a/language/testing-infra/transactional-test-runner/Cargo.toml +++ b/language/testing-infra/transactional-test-runner/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] anyhow = "1.0.52" diff --git a/language/testing-infra/transactional-test-runner/src/framework.rs b/language/testing-infra/transactional-test-runner/src/framework.rs index 7ed6121700..4b357d7e30 100644 --- a/language/testing-infra/transactional-test-runner/src/framework.rs +++ b/language/testing-infra/transactional-test-runner/src/framework.rs @@ -41,7 +41,7 @@ use move_vm_runtime::session::SerializedReturnValues; use rayon::iter::Either; use std::{ collections::{BTreeMap, BTreeSet, VecDeque}, - fmt::Debug, + fmt::{Debug, Write as FmtWrite}, io::Write, path::Path, }; @@ -667,11 +667,13 @@ where .collect::>(); assert!(!tasks.is_empty()); let num_tasks = tasks.len(); - output.push_str(&format!( - "processed {} task{}\n", + writeln!( + &mut output, + "processed {} task{}", num_tasks, if num_tasks > 1 { "s" } else { "" } - )); + ) + .unwrap(); let first_task = tasks.pop_front().unwrap(); let init_opt = match &first_task.command { @@ -687,7 +689,7 @@ where let (mut adapter, result_opt) = Adapter::init(default_syntax, fully_compiled_program_opt, init_opt); if let Some(result) = result_opt { - output.push_str(&format!("\ninit:\n{}\n", result)) + writeln!(output, "\ninit:\n{}", result)?; } for task in tasks { handle_known_task(&mut output, &mut adapter, task); @@ -720,10 +722,13 @@ fn handle_known_task<'a, Adapter: MoveTestAdapter<'a>>( Err(e) => format!("Error: {}", e), }; assert!(!result_string.is_empty()); - output.push_str(&format!( - "\ntask {} '{}'. lines {}-{}:\n{}\n", + + writeln!( + output, + "\ntask {} '{}'. lines {}-{}:\n{}", task_number, task_name, start_line, stop_line, result_string - )); + ) + .unwrap(); } fn handle_expected_output(test_path: &Path, output: impl AsRef) -> Result<()> { @@ -742,7 +747,7 @@ fn handle_expected_output(test_path: &Path, output: impl AsRef) -> Result<( let expected_output = std::fs::read_to_string(&exp_path) .unwrap() .replace("\r\n", "\n") - .replace("\r", "\n"); + .replace('\r', "\n"); if output != expected_output { let msg = format!( "Expected errors differ from actual errors:\n{}", diff --git a/language/testing-infra/transactional-test-runner/src/vm_test_harness.rs b/language/testing-infra/transactional-test-runner/src/vm_test_harness.rs index 1e1fcfd7cd..518b070c19 100644 --- a/language/testing-infra/transactional-test-runner/src/vm_test_harness.rs +++ b/language/testing-infra/transactional-test-runner/src/vm_test_harness.rs @@ -9,7 +9,9 @@ use crate::{ tasks::{EmptyCommand, InitCommand, SyntaxChoice, TaskInput}, }; use anyhow::{anyhow, Result}; +use clap::Parser; use move_binary_format::{ + compatibility::CompatibilityConfig, errors::{Location, VMError, VMResult}, file_format::CompiledScript, CompiledModule, @@ -34,8 +36,7 @@ use move_vm_runtime::{ move_vm::MoveVM, session::{SerializedReturnValues, Session}, }; -use move_vm_test_utils::InMemoryStorage; -use move_vm_types::gas_schedule::GasStatus; +use move_vm_test_utils::{gas_schedule::GasStatus, InMemoryStorage}; use once_cell::sync::Lazy; const STD_ADDR: AccountAddress = AccountAddress::ONE; @@ -68,9 +69,22 @@ pub fn view_resource_in_move_storage( } } +#[derive(Debug, Parser)] +pub struct AdapterPublishArgs { + #[clap(long)] + /// is skip the struct_and_function_linking compatibility check + pub skip_check_struct_and_function_linking: bool, + #[clap(long)] + /// is skip the struct_layout compatibility check + pub skip_check_struct_layout: bool, + #[clap(long)] + /// is skip the check friend link, if true, treat `friend` as `private` + pub skip_check_friend_linking: bool, +} + impl<'a> MoveTestAdapter<'a> for SimpleVMTestAdapter<'a> { type ExtraInitArgs = EmptyCommand; - type ExtraPublishArgs = EmptyCommand; + type ExtraPublishArgs = AdapterPublishArgs; type ExtraValueArgs = (); type ExtraRunArgs = EmptyCommand; type Subcommand = EmptyCommand; @@ -148,7 +162,7 @@ impl<'a> MoveTestAdapter<'a> for SimpleVMTestAdapter<'a> { module: CompiledModule, _named_addr_opt: Option, gas_budget: Option, - _extra_args: Self::ExtraPublishArgs, + extra_args: Self::ExtraPublishArgs, ) -> Result<(Option, CompiledModule)> { let mut module_bytes = vec![]; module.serialize(&mut module_bytes)?; @@ -156,7 +170,19 @@ impl<'a> MoveTestAdapter<'a> for SimpleVMTestAdapter<'a> { let id = module.self_id(); let sender = *id.address(); match self.perform_session_action(gas_budget, |session, gas_status| { - session.publish_module(module_bytes, sender, gas_status) + let compat_config = CompatibilityConfig { + check_struct_and_function_linking: !extra_args + .skip_check_struct_and_function_linking, + check_struct_layout: !extra_args.skip_check_struct_layout, + check_friend_linking: !extra_args.skip_check_friend_linking, + }; + + session.publish_module_bundle_with_compat_config( + vec![module_bytes], + sender, + gas_status, + compat_config, + ) }) { Ok(()) => Ok((None, module)), Err(e) => Err(anyhow!( @@ -292,10 +318,15 @@ impl<'a> SimpleVMTestAdapter<'a> { f: impl FnOnce(&mut Session, &mut GasStatus) -> VMResult, ) -> VMResult { // start session - let vm = MoveVM::new(move_stdlib::natives::all_natives(STD_ADDR)).unwrap(); + let vm = MoveVM::new(move_stdlib::natives::all_natives( + STD_ADDR, + // TODO: come up with a suitable gas schedule + move_stdlib::natives::GasParameters::zeros(), + )) + .unwrap(); let (mut session, mut gas_status) = { let gas_status = move_cli::sandbox::utils::get_gas_status( - &move_vm_types::gas_schedule::INITIAL_COST_SCHEDULE, + &move_vm_test_utils::gas_schedule::INITIAL_COST_SCHEDULE, gas_budget, ) .unwrap(); diff --git a/language/tools/move-bytecode-utils/Cargo.toml b/language/tools/move-bytecode-utils/Cargo.toml index f42c6d15ec..fbf52faf22 100644 --- a/language/tools/move-bytecode-utils/Cargo.toml +++ b/language/tools/move-bytecode-utils/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] anyhow = "1.0.52" diff --git a/language/tools/move-bytecode-viewer/Cargo.toml b/language/tools/move-bytecode-viewer/Cargo.toml index 82e2ab8d68..43b921483c 100644 --- a/language/tools/move-bytecode-viewer/Cargo.toml +++ b/language/tools/move-bytecode-viewer/Cargo.toml @@ -5,12 +5,12 @@ authors = ["Diem Association "] description = "Explore Move bytecode and how the source code compiles to it" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] clap = { version = "3.1.8", features = ["derive"] } anyhow = "1.0.52" -regex = "1.1.9" +regex = "1.5.5" tui = "0.17.0" crossterm = "0.21" diff --git a/language/tools/move-bytecode-viewer/src/bytecode_viewer.rs b/language/tools/move-bytecode-viewer/src/bytecode_viewer.rs index 7279cab34d..4abbcabfdb 100644 --- a/language/tools/move-bytecode-viewer/src/bytecode_viewer.rs +++ b/language/tools/move-bytecode-viewer/src/bytecode_viewer.rs @@ -107,6 +107,6 @@ impl LeftScreen for BytecodeViewer<'_> { } fn backing_string(&self) -> String { - self.lines.join("\n").replace("\t", " ") + self.lines.join("\n").replace('\t', " ") } } diff --git a/language/tools/move-cli/Cargo.toml b/language/tools/move-cli/Cargo.toml index feb3bad0df..7802ba99b7 100644 --- a/language/tools/move-cli/Cargo.toml +++ b/language/tools/move-cli/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] anyhow = "1.0.52" @@ -21,9 +21,15 @@ tempfile = "3.2.0" walkdir = "2.3.1" codespan-reporting = "0.11.1" itertools = "0.10.0" -bcs = "0.1.2" +serde_json = "1.0" +toml_edit = { version = "0.14.3", features = ["easy"] } +reqwest = { version = "0.11.1", features = ["blocking", "json"] } + +bcs.workspace = true + move-bytecode-verifier = { path = "../../move-bytecode-verifier" } move-disassembler = { path = "../move-disassembler" } +move-docgen = { path = "../../move-prover/move-docgen" } move-command-line-common = { path = "../../move-command-line-common" } move-bytecode-utils = { path = "../move-bytecode-utils" } move-coverage = { path = "../move-coverage" } @@ -35,6 +41,7 @@ move-table-extension = { path = "../../extensions/move-table-extension", optiona move-symbol-pool = { path = "../../move-symbol-pool" } move-vm-types = { path = "../../move-vm/types" } move-vm-runtime = { path = "../../move-vm/runtime", features = ["debugging"] } +move-vm-test-utils = { path = "../../move-vm/test-utils" } read-write-set = { path = "../read-write-set" } read-write-set-dynamic = { path = "../read-write-set/dynamic" } move-resource-viewer = { path = "../move-resource-viewer" } @@ -48,6 +55,7 @@ move-bytecode-viewer = { path = "../move-bytecode-viewer" } [dev-dependencies] datatest-stable = "0.1.1" +httpmock = "0.6.6" [[bin]] name = "move" diff --git a/language/tools/move-cli/README.md b/language/tools/move-cli/README.md index 093a411f5b..7dae2bdb45 100644 --- a/language/tools/move-cli/README.md +++ b/language/tools/move-cli/README.md @@ -125,11 +125,11 @@ directory: ```toml [addresses] -Std = "0x1" # Specify and assign 0x1 to the named address "Std" +std = "0x1" # Specify and assign 0x1 to the named address "std" [dependencies] -MoveNursery = { git = "https://github.com/move-language/move.git", subdir = "language/move-stdlib/nursery", rev = "d45f20a" } -# ^ ^ ^ ^ +MoveNursery = { git = "https://github.com/move-language/move.git", subdir = "language/move-stdlib/nursery", rev = "main" } +# ^ ^ ^ ^ # Git dependency Git clone URL Subdir under git repo (optional) Git revision to use ``` @@ -137,7 +137,7 @@ Now let's try running the script -- the very first time may take some time since ```shell $ move sandbox run sources/debug_script.move --signers 0xf -[debug] (&) { 0000000000000000000000000000000F } +[debug] (&) { 0000000000000000000000000000000f } ``` The `--signers 0xf` argument indicates which account address(es) have signed @@ -316,7 +316,7 @@ changes first. We can do this by passing the `--dry-run` flag: $ move sandbox run sources/test_script.move --signers 0xf -v --dry-run Compiling transaction script... Changed resource(s) under 1 address(es): - Changed 1 resource(s) under address 0000000000000000000000000000000F: + Changed 1 resource(s) under address 0000000000000000000000000000000f: Added type 0x2::Test::Resource: [10, 0, 0, 0, 0, 0, 0, 0] (wrote 40 bytes) Wrote 40 bytes of resource ID's and data key 0x2::Test::Resource { @@ -332,7 +332,7 @@ changes by removing the `--dry-run` flag: $ move sandbox run sources/test_script.move --signers 0xf -v Compiling transaction script... Changed resource(s) under 1 address(es): - Changed 1 resource(s) under address 0000000000000000000000000000000F: + Changed 1 resource(s) under address 0000000000000000000000000000000f: Added type 0x2::Test::Resource: [10, 0, 0, 0, 0, 0, 0, 0] (wrote 40 bytes) Wrote 40 bytes of resource ID's and data key 0x2::Test::Resource { @@ -346,7 +346,7 @@ We can inspect the newly published resource using `move sandbox view` since the change has been committed: ```shell -$ move sandbox view storage/0x0000000000000000000000000000000F/resources/0x00000000000000000000000000000002::Test::Resource.bcs +$ move sandbox view storage/0x0000000000000000000000000000000f/resources/0x00000000000000000000000000000002::Test::Resource.bcs key 0x2::Test::Resource { i: 10 } @@ -360,12 +360,12 @@ can be done using the `move sandbox clean` command which will remove the `storage` and `build` directories: ```shell -$ move sandbox view storage/0x0000000000000000000000000000000F/resources/0x00000000000000000000000000000002::Test::Resource.bcs +$ move sandbox view storage/0x0000000000000000000000000000000f/resources/0x00000000000000000000000000000002::Test::Resource.bcs resource 0x2::Test::Resource { i: 10 } $ move sandbox clean -$ move sandbox view storage/0x0000000000000000000000000000000F/resources/0x00000000000000000000000000000002::Test::Resource.bcs +$ move sandbox view storage/0x0000000000000000000000000000000f/resources/0x00000000000000000000000000000002::Test::Resource.bcs Error: `move sandbox view ` must point to a valid file under storage ``` @@ -412,7 +412,7 @@ build sandbox publish sandbox view storage/0x00000000000000000000000000000002/modules/Test.mv sandbox run sources/test_script.move --signers 0xf -v -sandbox view storage/0x0000000000000000000000000000000F/resources/0x00000000000000000000000000000002::Test::Resource.bcs +sandbox view storage/0x0000000000000000000000000000000f/resources/0x00000000000000000000000000000002::Test::Resource.bcs ``` We can then use the `move sandbox test` command and point it at the `readme` directory to run each of these @@ -441,7 +441,7 @@ in the `args.txt` file: ```shell $ cat readme/args.exp Command `sandbox run sources/debug_script.move --signers 0xf`: -[debug] (&) { 0000000000000000000000000000000F } +[debug] (&) { 0000000000000000000000000000000f } Command `sandbox run sources/debug_script.move --signers 0xf --mode bare`: ... ``` diff --git a/language/tools/move-cli/src/base/disassemble.rs b/language/tools/move-cli/src/base/disassemble.rs index e47eb65829..db7f4eadf5 100644 --- a/language/tools/move-cli/src/base/disassemble.rs +++ b/language/tools/move-cli/src/base/disassemble.rs @@ -38,6 +38,7 @@ impl Disassemble { .unwrap_or(package.compiled_package_info.package_name.as_str()); match package .get_module_by_name(needle_package, &module_or_script_name) + .or_else(|_| package.get_script_by_name(needle_package, &module_or_script_name)) .ok() { None => anyhow::bail!( diff --git a/language/tools/move-cli/src/base/docgen.rs b/language/tools/move-cli/src/base/docgen.rs new file mode 100644 index 0000000000..a458cb9c3a --- /dev/null +++ b/language/tools/move-cli/src/base/docgen.rs @@ -0,0 +1,125 @@ +// Copyright (c) The Move Contributors +// SPDX-License-Identifier: Apache-2.0 + +use super::reroot_path; +use clap::*; +use move_docgen::DocgenOptions; +use move_package::{BuildConfig, ModelConfig}; +use std::{fs, path::PathBuf}; + +/// Generate javadoc style documentation for Move packages +#[derive(Parser)] +#[clap(name = "docgen")] +pub struct Docgen { + /// The level where we start sectioning. Often markdown sections are rendered with + /// unnecessary large section fonts, setting this value high reduces the size + #[clap(long = "section-level-start", value_name = "HEADER_LEVEL")] + pub section_level_start: Option, + /// Whether to exclude private functions in the generated docs + #[clap(long = "exclude-private-fun")] + pub exclude_private_fun: bool, + /// Whether to exclude specifications in the generated docs + #[clap(long = "exclude-specs")] + pub exclude_specs: bool, + /// Whether to put specifications in the same section as a declaration or put them all + /// into an independent section + #[clap(long = "independent-specs")] + pub independent_specs: bool, + /// Whether to exclude Move implementations + #[clap(long = "exclude-impl")] + pub exclude_impl: bool, + /// Max depth to which sections are displayed in table-of-contents + #[clap(long = "toc-depth", value_name = "DEPTH")] + pub toc_depth: Option, + /// Do not use collapsed sections (
) for impl and specs + #[clap(long = "no-collapsed-sections")] + pub no_collapsed_sections: bool, + /// In which directory to store output + #[clap(long = "output-directory", value_name = "PATH")] + pub output_directory: Option, + /// A template for documentation generation. Can be multiple + #[clap(long = "template", short = 't', value_name = "FILE")] + pub template: Vec, + /// An optional file containing reference definitions. The content of this file will + /// be added to each generated markdown doc + #[clap(long = "references-file", value_name = "FILE")] + pub references_file: Option, + /// Whether to include dependency diagrams in the generated docs + #[clap(long = "include-dep-diagrams")] + pub include_dep_diagrams: bool, + /// Whether to include call diagrams in the generated docs + #[clap(long = "include-call-diagrams")] + pub include_call_diagrams: bool, + /// If this is being compiled relative to a different place where it will be stored (output directory) + #[clap(long = "compile-relative-to-output-dir")] + pub compile_relative_to_output_dir: bool, +} + +impl Docgen { + /// Calling the Docgen + pub fn execute(self, path: Option, config: BuildConfig) -> anyhow::Result<()> { + let model = config.move_model_for_package( + &reroot_path(path).unwrap(), + ModelConfig { + all_files_as_targets: false, + target_filter: None, + }, + )?; + + let mut options = DocgenOptions::default(); + + if !self.template.is_empty() { + options.root_doc_templates = self.template; + } + if self.section_level_start.is_some() { + options.section_level_start = self.section_level_start.unwrap(); + } + if self.exclude_private_fun { + options.include_private_fun = false; + } + if self.exclude_specs { + options.include_specs = false; + } + if self.independent_specs { + options.specs_inlined = false; + } + if self.exclude_impl { + options.include_impl = false; + } + if self.toc_depth.is_some() { + options.toc_depth = self.toc_depth.unwrap(); + } + if self.no_collapsed_sections { + options.collapsed_sections = false; + } + if self.output_directory.is_some() { + options.output_directory = self.output_directory.unwrap(); + } + if self.references_file.is_some() { + options.references_file = self.references_file; + } + if self.compile_relative_to_output_dir { + options.compile_relative_to_output_dir = true; + } + + // We are using the full namespace, since we already use `Docgen` here. + // Docgen is the most suitable name for both: this Docgen subcommand, + // and the actual move_docgen::Docgen. + let generator = move_docgen::Docgen::new(&model, &options); + + for (file, content) in generator.gen() { + let path = PathBuf::from(&file); + fs::create_dir_all(path.parent().unwrap())?; + fs::write(path.as_path(), content)?; + println!("Generated {:?}", path); + } + + anyhow::ensure!( + !model.has_errors(), + "Errors encountered while generating documentation!" + ); + + println!("\nDocumentation generation successful!"); + Ok(()) + } +} diff --git a/language/tools/move-cli/src/base/mod.rs b/language/tools/move-cli/src/base/mod.rs index f0583c21a4..349844c262 100644 --- a/language/tools/move-cli/src/base/mod.rs +++ b/language/tools/move-cli/src/base/mod.rs @@ -4,8 +4,11 @@ pub mod build; pub mod coverage; pub mod disassemble; +pub mod docgen; pub mod errmap; pub mod info; +pub mod movey_login; +pub mod movey_upload; pub mod new; pub mod prove; pub mod test; diff --git a/language/tools/move-cli/src/base/movey_login.rs b/language/tools/move-cli/src/base/movey_login.rs new file mode 100644 index 0000000000..bcb0a84851 --- /dev/null +++ b/language/tools/move-cli/src/base/movey_login.rs @@ -0,0 +1,227 @@ +// Copyright (c) The Move Contributors +// SPDX-License-Identifier: Apache-2.0 + +use crate::utils::movey_credential::read_credential_file; +use anyhow::{bail, Result}; +use clap::Parser; +use move_command_line_common::{ + env::MOVE_HOME, + movey_constants::{MOVEY_CREDENTIAL_PATH, MOVEY_URL}, +}; +use std::{fs, fs::File, io, path::PathBuf}; +use toml_edit::easy::{map::Map, Value}; + +#[derive(Parser)] +#[clap(name = "movey-login")] +pub struct MoveyLogin; + +impl MoveyLogin { + pub fn execute(self) -> Result<()> { + println!( + "Please paste the API Token found on {}/settings/tokens below", + MOVEY_URL + ); + let mut line = String::new(); + loop { + match io::stdin().read_line(&mut line) { + Ok(_) => { + line = line.trim().to_string(); + if !line.is_empty() { + break; + } + println!("Invalid API Token. Try again!"); + } + Err(err) => { + bail!("Error reading file: {}", err); + } + } + } + Self::save_credential(line, MOVE_HOME.clone())?; + println!("Token for Movey saved."); + Ok(()) + } + + pub fn save_credential(token: String, move_home: String) -> Result<()> { + fs::create_dir_all(&move_home)?; + let credential_path = move_home + MOVEY_CREDENTIAL_PATH; + let credential_file = PathBuf::from(&credential_path); + if !credential_file.exists() { + create_credential_file(&credential_path)?; + } + + let mut toml: Value = read_credential_file(&credential_path)?; + // only update token key, keep the rest of the file intact + if let Some(registry) = toml.as_table_mut().unwrap().get_mut("registry") { + if let Some(toml_token) = registry.as_table_mut().unwrap().get_mut("token") { + *toml_token = Value::String(token); + } else { + registry + .as_table_mut() + .unwrap() + .insert(String::from("token"), Value::String(token)); + } + } else { + let mut value = Map::new(); + value.insert(String::from("token"), Value::String(token)); + toml.as_table_mut() + .unwrap() + .insert(String::from("registry"), Value::Table(value)); + } + + let new_contents = toml.to_string(); + fs::write(credential_file, new_contents).expect("Unable to write file"); + Ok(()) + } +} + +#[cfg(unix)] +fn create_credential_file(credential_path: &str) -> Result<()> { + use std::os::unix::fs::PermissionsExt; + let credential_file = File::create(&credential_path)?; + + let mut perms = credential_file.metadata()?.permissions(); + perms.set_mode(0o600); + credential_file.set_permissions(perms)?; + Ok(()) +} + +#[cfg(windows)] +#[allow(unused)] +fn create_credential_file(credential_path: &str) -> Result<()> { + let windows_path = credential_path.replace("/", "\\"); + File::create(&windows_path)?; + Ok(()) +} + +#[cfg(not(any(unix, windows)))] +#[allow(unused)] +fn create_credential_file(credential_path: &str) -> Result<()> { + bail!("OS not supported") +} + +#[cfg(test)] +mod tests { + use super::*; + use std::env; + + fn setup_move_home(test_path: &str) -> (String, String) { + let cwd = env::current_dir().unwrap(); + let mut move_home: String = String::from(cwd.to_string_lossy()); + if !test_path.is_empty() { + move_home.push_str(test_path); + } else { + move_home.push_str("/test"); + } + let credential_path = move_home.clone() + MOVEY_CREDENTIAL_PATH; + (move_home, credential_path) + } + + fn clean_up(move_home: &str) { + let _ = fs::remove_dir_all(move_home); + } + + #[test] + fn save_credential_works_if_no_credential_file_exists() { + let (move_home, credential_path) = + setup_move_home("/save_credential_works_if_no_credential_file_exists"); + let _ = fs::remove_dir_all(&move_home); + MoveyLogin::save_credential(String::from("test_token"), move_home.clone()).unwrap(); + + let contents = fs::read_to_string(&credential_path).expect("Unable to read file"); + let mut toml: Value = contents.parse().unwrap(); + let registry = toml.as_table_mut().unwrap().get_mut("registry").unwrap(); + let token = registry.as_table_mut().unwrap().get_mut("token").unwrap(); + assert!(token.to_string().contains("test_token")); + + clean_up(&move_home); + } + + #[test] + fn save_credential_works_if_empty_credential_file_exists() { + let (move_home, credential_path) = + setup_move_home("/save_credential_works_if_empty_credential_file_exists"); + + let _ = fs::remove_dir_all(&move_home); + fs::create_dir_all(&move_home).unwrap(); + File::create(&credential_path).unwrap(); + + let contents = fs::read_to_string(&credential_path).expect("Unable to read file"); + let mut toml: Value = contents.parse().unwrap(); + assert!(toml.as_table_mut().unwrap().get_mut("registry").is_none()); + + MoveyLogin::save_credential(String::from("test_token"), move_home.clone()).unwrap(); + + let contents = fs::read_to_string(&credential_path).expect("Unable to read file"); + let mut toml: Value = contents.parse().unwrap(); + let registry = toml.as_table_mut().unwrap().get_mut("registry").unwrap(); + let token = registry.as_table_mut().unwrap().get_mut("token").unwrap(); + assert!(token.to_string().contains("test_token")); + + clean_up(&move_home); + } + + #[test] + fn save_credential_works_if_token_field_exists() { + let (move_home, credential_path) = + setup_move_home("/save_credential_works_if_token_field_exists"); + + let _ = fs::remove_dir_all(&move_home); + fs::create_dir_all(&move_home).unwrap(); + File::create(&credential_path).unwrap(); + + let old_content = + String::from("[registry]\ntoken = \"old_test_token\"\nversion = \"0.0.0\"\n"); + fs::write(&credential_path, old_content).expect("Unable to write file"); + + let contents = fs::read_to_string(&credential_path).expect("Unable to read file"); + let mut toml: Value = contents.parse().unwrap(); + let registry = toml.as_table_mut().unwrap().get_mut("registry").unwrap(); + let token = registry.as_table_mut().unwrap().get_mut("token").unwrap(); + assert!(token.to_string().contains("old_test_token")); + assert!(!token.to_string().contains("new_world")); + + MoveyLogin::save_credential(String::from("new_world"), move_home.clone()).unwrap(); + + let contents = fs::read_to_string(&credential_path).expect("Unable to read file"); + let mut toml: Value = contents.parse().unwrap(); + let registry = toml.as_table_mut().unwrap().get_mut("registry").unwrap(); + let token = registry.as_table_mut().unwrap().get_mut("token").unwrap(); + assert!(token.to_string().contains("new_world")); + assert!(!token.to_string().contains("old_test_token")); + let version = registry.as_table_mut().unwrap().get_mut("version").unwrap(); + assert!(version.to_string().contains("0.0.0")); + + clean_up(&move_home); + } + + #[test] + fn save_credential_works_if_empty_token_field_exists() { + let (move_home, credential_path) = + setup_move_home("/save_credential_works_if_empty_token_field_exists"); + + let _ = fs::remove_dir_all(&move_home); + fs::create_dir_all(&move_home).unwrap(); + File::create(&credential_path).unwrap(); + + let old_content = String::from("[registry]\ntoken = \"\"\nversion = \"0.0.0\"\n"); + fs::write(&credential_path, old_content).expect("Unable to write file"); + + let contents = fs::read_to_string(&credential_path).expect("Unable to read file"); + let mut toml: Value = contents.parse().unwrap(); + let registry = toml.as_table_mut().unwrap().get_mut("registry").unwrap(); + let token = registry.as_table_mut().unwrap().get_mut("token").unwrap(); + assert!(!token.to_string().contains("test_token")); + + MoveyLogin::save_credential(String::from("test_token"), move_home.clone()).unwrap(); + + let contents = fs::read_to_string(&credential_path).expect("Unable to read file"); + let mut toml: Value = contents.parse().unwrap(); + let registry = toml.as_table_mut().unwrap().get_mut("registry").unwrap(); + let token = registry.as_table_mut().unwrap().get_mut("token").unwrap(); + assert!(token.to_string().contains("test_token")); + let version = registry.as_table_mut().unwrap().get_mut("version").unwrap(); + assert!(version.to_string().contains("0.0.0")); + + clean_up(&move_home); + } +} diff --git a/language/tools/move-cli/src/base/movey_upload.rs b/language/tools/move-cli/src/base/movey_upload.rs new file mode 100644 index 0000000000..eb344a86d7 --- /dev/null +++ b/language/tools/move-cli/src/base/movey_upload.rs @@ -0,0 +1,134 @@ +// Copyright (c) The Move Contributors +// SPDX-License-Identifier: Apache-2.0 + +use crate::utils::movey_credential; +use anyhow::bail; +use clap::*; +use move_command_line_common::env::MOVE_HOME; +use reqwest::blocking::Client; +use std::{env, fs::File, path::PathBuf, process::Command}; + +// Metadata that will be collected by Movey +#[derive(serde::Serialize, Default)] +pub struct MoveyUploadRequest { + github_repo_url: String, + total_files: usize, + token: String, + subdir: String, +} + +/// Upload the package metadata to Movey.net. +#[derive(Parser)] +#[clap(name = "movey-upload")] +pub struct MoveyUpload; + +impl MoveyUpload { + pub fn execute(self, path: Option) -> anyhow::Result<()> { + if let Some(path) = path { + if path.exists() && path.is_dir() { + let _ = env::set_current_dir(&path); + } else { + bail!("invalid directory") + } + } + // make sure it's a Move project + let move_toml = File::open("Move.toml"); + if move_toml.is_err() { + bail!("Move.toml not found") + } + let metadata = move_toml.unwrap().metadata()?; + if metadata.len() == 0 { + bail!("Move.toml not found") + } + + // use git command to get the repository url + let mut movey_upload_request: MoveyUploadRequest = Default::default(); + let mut output = Command::new("git") + .current_dir(".") + .args(&["remote", "-v"]) + .output() + .unwrap(); + if !output.status.success() || output.stdout.is_empty() { + bail!("invalid git repository") + } + + let lines = String::from_utf8_lossy(output.stdout.as_slice()); + let lines = lines.split('\n'); + for line in lines { + if line.contains("github.com") { + let tokens: Vec<&str> = line.split(&['\t', ' '][..]).collect(); + if tokens.len() != 3 { + bail!("invalid remote url") + } + // convert ssh url to https + let https_url = if tokens[1].starts_with("git@github.com") { + tokens[1].replace(':', "/").replace("git@", "https://") + } else { + String::from(tokens[1]) + }; + movey_upload_request.github_repo_url = if https_url.ends_with(".git") { + https_url[..https_url.len() - 4].to_string() + } else { + https_url + }; + } + } + + // use git command to get the subdir if move package is not on the top level + output = Command::new("git") + .current_dir(".") + .args(&["rev-parse", "--show-prefix"]) + .output() + .unwrap(); + let subdir = String::from_utf8_lossy(output.stdout.as_slice()); + movey_upload_request.subdir = String::from(subdir); + + // use git command to count total files + output = Command::new("git") + .current_dir(".") + .args(&["ls-files"]) + .output() + .unwrap(); + let tracked_files = String::from_utf8_lossy(output.stdout.as_slice()); + let tracked_files: Vec<&str> = tracked_files.split('\n').collect(); + let mut total_files = tracked_files.len(); + for file_path in tracked_files { + if file_path.is_empty() { + total_files -= 1; + continue; + } + } + movey_upload_request.total_files = total_files; + movey_upload_request.token = movey_credential::get_registry_api_token(&MOVE_HOME)?; + let movey_url = movey_credential::get_movey_url(&MOVE_HOME); + match movey_url { + Ok(url) => { + let client = Client::new(); + let response = client + .post(&format!("{}/api/v1/packages/upload", &url)) + .json(&movey_upload_request) + .send(); + match response { + Ok(response) => { + if response.status().is_success() { + println!( + "Your package has been successfully uploaded to Movey at {}/packages/{}.", + url, + response.text()? + ); + } else if response.status().is_client_error() { + bail!("{}", response.text()?) + } else if response.status().is_server_error() { + bail!("An unexpected error occurred. Please try again later"); + } + } + Err(_) => { + bail!("An unexpected error occurred. Please try again later"); + } + } + } + Err(_) => bail!("An unexpected error occurred. Please try again later"), + } + Ok(()) + } +} diff --git a/language/tools/move-cli/src/base/new.rs b/language/tools/move-cli/src/base/new.rs index c1996ed5a7..6dbfbeaf1d 100644 --- a/language/tools/move-cli/src/base/new.rs +++ b/language/tools/move-cli/src/base/new.rs @@ -34,6 +34,7 @@ impl New { "0.0.0", [(MOVE_STDLIB_PACKAGE_NAME, MOVE_STDLIB_PACKAGE_PATH)], [(MOVE_STDLIB_ADDR_NAME, MOVE_STDLIB_ADDR_VALUE)], + "", ) } @@ -43,6 +44,7 @@ impl New { version: &str, deps: impl IntoIterator, addrs: impl IntoIterator, + custom: &str, // anything else that needs to end up being in Move.toml (or empty string) ) -> anyhow::Result<()> { // TODO warn on build config flags let Self { name } = self; @@ -76,6 +78,9 @@ version = \"{version}\" for (addr_name, addr_val) in addrs { writeln!(w, "{addr_name} = \"{addr_val}\"")?; } + if !custom.is_empty() { + writeln!(w, "{}", custom)?; + } Ok(()) } } diff --git a/language/tools/move-cli/src/base/test.rs b/language/tools/move-cli/src/base/test.rs index f5e4a48ee1..8a1aae20b3 100644 --- a/language/tools/move-cli/src/base/test.rs +++ b/language/tools/move-cli/src/base/test.rs @@ -37,13 +37,8 @@ compile_error!("Unsupported OS, currently we only support windows and unix famil #[clap(name = "test")] pub struct Test { /// Bound the number of instructions that can be executed by any one test. - #[clap( - name = "instructions", - default_value = "5000", - short = 'i', - long = "instructions" - )] - pub instruction_execution_bound: u64, + #[clap(name = "instructions", short = 'i', long = "instructions")] + pub instruction_execution_bound: Option, /// A filter string to determine which unit tests to run. A unit test will be run only if it /// contains this string in its fully qualified (::::) name. #[clap(name = "filter", short = 'f', long = "filter")] @@ -65,6 +60,11 @@ pub struct Test { /// Show the storage state at the end of execution of a failing test #[clap(name = "global_state_on_error", short = 'g', long = "state_on_error")] pub report_storage_on_error: bool, + + /// Ignore compiler's warning, and continue run tests + #[clap(name = "ignore_compile_warnings", long = "ignore_compile_warnings")] + pub ignore_compile_warnings: bool, + /// Use the stackless bytecode interpreter to run the tests and cross check its results with /// the execution result from Move VM. #[clap(long = "stackless")] @@ -98,6 +98,7 @@ impl Test { num_threads, report_statistics, report_storage_on_error, + ignore_compile_warnings, check_stackless_vm, verbose_mode, compute_coverage, @@ -113,7 +114,7 @@ impl Test { report_storage_on_error, check_stackless_vm, verbose: verbose_mode, - + ignore_compile_warnings, #[cfg(feature = "evm-backend")] evm, @@ -137,7 +138,7 @@ impl Test { } /// Encapsulates the possible returned states when running unit tests on a move package. -#[derive(PartialEq, Debug)] +#[derive(PartialEq, Eq, Debug)] pub enum UnitTestResult { Success, Failure, @@ -200,7 +201,13 @@ pub fn run_move_unit_tests( let (mut compiler, cfgir) = compiler.into_ast(); let compilation_env = compiler.compilation_env(); let built_test_plan = construct_test_plan(compilation_env, Some(root_package), &cfgir); - if let Err(diags) = compilation_env.check_diags_at_or_above_severity(Severity::Warning) { + if let Err(diags) = compilation_env.check_diags_at_or_above_severity( + if unit_test_config.ignore_compile_warnings { + Severity::NonblockingError + } else { + Severity::Warning + }, + ) { diagnostics::report_diagnostics(&files, diags); } diff --git a/language/tools/move-cli/src/lib.rs b/language/tools/move-cli/src/lib.rs index 2fa155bac3..7bea681310 100644 --- a/language/tools/move-cli/src/lib.rs +++ b/language/tools/move-cli/src/lib.rs @@ -3,14 +3,16 @@ // SPDX-License-Identifier: Apache-2.0 use base::{ - build::Build, coverage::Coverage, disassemble::Disassemble, errmap::Errmap, info::Info, - new::New, prove::Prove, test::Test, + build::Build, coverage::Coverage, disassemble::Disassemble, docgen::Docgen, errmap::Errmap, + info::Info, movey_login::MoveyLogin, movey_upload::MoveyUpload, new::New, prove::Prove, + test::Test, }; use move_package::BuildConfig; pub mod base; pub mod experimental; pub mod sandbox; +pub mod utils; /// Default directory where saved Move resources live pub const DEFAULT_STORAGE_DIR: &str = "storage"; @@ -24,10 +26,10 @@ const BCS_EXTENSION: &str = "bcs"; use anyhow::Result; use clap::Parser; use move_core_types::{ - account_address::AccountAddress, errmap::ErrorMapping, gas_schedule::CostTable, - identifier::Identifier, + account_address::AccountAddress, errmap::ErrorMapping, identifier::Identifier, }; use move_vm_runtime::native_functions::NativeFunction; +use move_vm_test_utils::gas_schedule::CostTable; use std::path::PathBuf; type NativeFunctionRecord = (AccountAddress, Identifier, Identifier, NativeFunction); @@ -65,8 +67,10 @@ pub enum Command { Build(Build), Coverage(Coverage), Disassemble(Disassemble), + Docgen(Docgen), Errmap(Errmap), Info(Info), + MoveyUpload(MoveyUpload), New(New), Prove(Prove), Test(Test), @@ -90,6 +94,8 @@ pub enum Command { #[clap(subcommand)] cmd: experimental::cli::ExperimentalCommand, }, + #[clap(name = "movey-login")] + MoveyLogin(MoveyLogin), } pub fn run_cli( @@ -99,12 +105,17 @@ pub fn run_cli( move_args: Move, cmd: Command, ) -> Result<()> { + // TODO: right now, the gas metering story for move-cli (as a library) is a bit of a mess. + // 1. It's still using the old CostTable. + // 2. The CostTable only affects sandbox runs, but not unit tests, which use a unit cost table. match cmd { Command::Build(c) => c.execute(move_args.package_path, move_args.build_config), Command::Coverage(c) => c.execute(move_args.package_path, move_args.build_config), Command::Disassemble(c) => c.execute(move_args.package_path, move_args.build_config), + Command::Docgen(c) => c.execute(move_args.package_path, move_args.build_config), Command::Errmap(c) => c.execute(move_args.package_path, move_args.build_config), Command::Info(c) => c.execute(move_args.package_path, move_args.build_config), + Command::MoveyUpload(c) => c.execute(move_args.package_path), Command::New(c) => c.execute_with_defaults(move_args.package_path), Command::Prove(c) => c.execute(move_args.package_path, move_args.build_config), Command::Test(c) => c.execute(move_args.package_path, move_args.build_config, natives), @@ -116,6 +127,7 @@ pub fn run_cli( &storage_dir, ), Command::Experimental { storage_dir, cmd } => cmd.handle_command(&move_args, &storage_dir), + Command::MoveyLogin(c) => c.execute(), } } diff --git a/language/tools/move-cli/src/main.rs b/language/tools/move-cli/src/main.rs index 1bef3d6190..f2f29cc402 100644 --- a/language/tools/move-cli/src/main.rs +++ b/language/tools/move-cli/src/main.rs @@ -4,13 +4,16 @@ use anyhow::Result; use move_core_types::{account_address::AccountAddress, errmap::ErrorMapping}; +use move_stdlib::natives::{all_natives, nursery_natives, GasParameters, NurseryGasParameters}; fn main() -> Result<()> { let error_descriptions: ErrorMapping = bcs::from_bytes(move_stdlib::error_descriptions())?; - let cost_table = &move_vm_types::gas_schedule::INITIAL_COST_SCHEDULE; - move_cli::move_cli( - move_stdlib::natives::all_natives(AccountAddress::from_hex_literal("0x1").unwrap()), - cost_table, - &error_descriptions, - ) + let cost_table = &move_vm_test_utils::gas_schedule::INITIAL_COST_SCHEDULE; + let addr = AccountAddress::from_hex_literal("0x1").unwrap(); + let natives = all_natives(addr, GasParameters::zeros()) + .into_iter() + .chain(nursery_natives(addr, NurseryGasParameters::zeros())) + .collect(); + + move_cli::move_cli(natives, cost_table, &error_descriptions) } diff --git a/language/tools/move-cli/src/sandbox/cli.rs b/language/tools/move-cli/src/sandbox/cli.rs index a338613a58..3c897739b5 100644 --- a/language/tools/move-cli/src/sandbox/cli.rs +++ b/language/tools/move-cli/src/sandbox/cli.rs @@ -12,10 +12,11 @@ use crate::{ use anyhow::Result; use clap::Parser; use move_core_types::{ - errmap::ErrorMapping, gas_schedule::CostTable, language_storage::TypeTag, parser, + errmap::ErrorMapping, language_storage::TypeTag, parser, transaction_argument::TransactionArgument, }; use move_package::compilation::package_layout::CompiledPackageLayout; +use move_vm_test_utils::gas_schedule::CostTable; use std::{ fs, path::{Path, PathBuf}, diff --git a/language/tools/move-cli/src/sandbox/commands/publish.rs b/language/tools/move-cli/src/sandbox/commands/publish.rs index a1b226ff9e..9d8fee606f 100644 --- a/language/tools/move-cli/src/sandbox/commands/publish.rs +++ b/language/tools/move-cli/src/sandbox/commands/publish.rs @@ -10,10 +10,11 @@ use crate::{ NativeFunctionRecord, }; use anyhow::{bail, Result}; +use move_binary_format::errors::Location; use move_command_line_common::env::get_bytecode_version_from_env; -use move_core_types::gas_schedule::CostTable; use move_package::compilation::compiled_package::CompiledPackage; use move_vm_runtime::move_vm::MoveVM; +use move_vm_test_utils::gas_schedule::CostTable; use std::collections::BTreeMap; pub fn publish( @@ -115,8 +116,18 @@ pub fn publish( let res = session.publish_module_bundle(module_bytes_vec, sender, &mut gas_status); if let Err(err) = res { - // TODO (mengxu): explain publish errors in multi-module publishing println!("Invalid multi-module publishing: {}", err); + if let Location::Module(module_id) = err.location() { + // find the module where error occures and explain + if let Some(unit) = modules_to_publish + .into_iter() + .find(|&x| x.unit.name().as_str() == module_id.name().as_str()) + { + explain_publish_error(err, state, unit)? + } else { + println!("Unable to locate the module in the multi-module publishing error"); + } + } has_error = true; } } @@ -141,11 +152,13 @@ pub fn publish( let (changeset, events) = session.finish().map_err(|e| e.into_vm_status())?; assert!(events.is_empty()); if verbose { - explain_publish_changeset(&changeset, state); + explain_publish_changeset(&changeset); } let modules: Vec<_> = changeset .into_modules() - .map(|(module_id, blob_opt)| (module_id, blob_opt.expect("must be non-deletion"))) + .map(|(module_id, blob_opt)| { + (module_id, blob_opt.ok().expect("must be non-deletion")) + }) .collect(); state.save_modules(&modules)?; } diff --git a/language/tools/move-cli/src/sandbox/commands/run.rs b/language/tools/move-cli/src/sandbox/commands/run.rs index 8b4077c2c8..17d0d07937 100644 --- a/language/tools/move-cli/src/sandbox/commands/run.rs +++ b/language/tools/move-cli/src/sandbox/commands/run.rs @@ -15,7 +15,6 @@ use move_command_line_common::env::get_bytecode_version_from_env; use move_core_types::{ account_address::AccountAddress, errmap::ErrorMapping, - gas_schedule::CostTable, identifier::IdentStr, language_storage::TypeTag, transaction_argument::{convert_txn_args, TransactionArgument}, @@ -23,6 +22,7 @@ use move_core_types::{ }; use move_package::compilation::compiled_package::CompiledPackage; use move_vm_runtime::move_vm::MoveVM; +use move_vm_test_utils::gas_schedule::CostTable; use std::{fs, path::Path}; pub fn run( diff --git a/language/tools/move-cli/src/sandbox/commands/test.rs b/language/tools/move-cli/src/sandbox/commands/test.rs index 897a8f8f82..e7c3a8a426 100644 --- a/language/tools/move-cli/src/sandbox/commands/test.rs +++ b/language/tools/move-cli/src/sandbox/commands/test.rs @@ -20,6 +20,7 @@ use move_package::{ use std::{ collections::{BTreeMap, HashMap}, env, + fmt::Write as FmtWrite, fs::{self, File}, io::{self, BufRead, Write}, path::{Path, PathBuf}, @@ -223,7 +224,7 @@ pub fn run_one( for args_line in args_file { let args_line = args_line?; - if let Some(external_cmd) = args_line.strip_prefix(">") { + if let Some(external_cmd) = args_line.strip_prefix('>') { let external_cmd = external_cmd.trim_start(); let mut cmd_iter = external_cmd.split_ascii_whitespace(); @@ -238,7 +239,7 @@ pub fn run_one( } let cmd_output = command.output()?; - output += &format!("External Command `{}`:\n", external_cmd); + writeln!(&mut output, "External Command `{}`:", external_cmd)?; output += std::str::from_utf8(&cmd_output.stdout)?; output += std::str::from_utf8(&cmd_output.stderr)?; @@ -269,7 +270,7 @@ pub fn run_one( } let cmd_output = cli_command_template().args(args_iter).output()?; - output += &format!("Command `{}`:\n", args_line); + writeln!(&mut output, "Command `{}`:", args_line)?; output += std::str::from_utf8(&cmd_output.stdout)?; output += std::str::from_utf8(&cmd_output.stderr)?; } diff --git a/language/tools/move-cli/src/sandbox/utils/mod.rs b/language/tools/move-cli/src/sandbox/utils/mod.rs index 38b582bc01..f768dca003 100644 --- a/language/tools/move-cli/src/sandbox/utils/mod.rs +++ b/language/tools/move-cli/src/sandbox/utils/mod.rs @@ -21,9 +21,8 @@ use move_compiler::{ }; use move_core_types::{ account_address::AccountAddress, - effects::{ChangeSet, Event}, + effects::{ChangeSet, Event, Op}, errmap::ErrorMapping, - gas_schedule::{GasAlgebra, GasUnits}, language_storage::{ModuleId, TypeTag}, transaction_argument::TransactionArgument, vm_status::{AbortLocation, StatusCode, VMStatus}, @@ -31,7 +30,7 @@ use move_core_types::{ use move_ir_types::location::Loc; use move_package::compilation::compiled_package::CompiledUnitWithSource; use move_resource_viewer::{AnnotatedMoveStruct, MoveValueAnnotator}; -use move_vm_types::gas_schedule::GasStatus; +use move_vm_test_utils::gas_schedule::Gas; use std::{ collections::{BTreeMap, HashMap}, fs, @@ -42,19 +41,18 @@ pub mod on_disk_state_view; pub mod package_context; use move_bytecode_utils::module_cache::GetModule; -use move_core_types::gas_schedule::CostTable; +use move_vm_test_utils::gas_schedule::{CostTable, GasStatus}; pub use on_disk_state_view::*; pub use package_context::*; pub fn get_gas_status(cost_table: &CostTable, gas_budget: Option) -> Result { let gas_status = if let Some(gas_budget) = gas_budget { - let max_gas_budget = u64::MAX - .checked_div(cost_table.gas_constants.gas_unit_scaling_factor) - .unwrap(); + // TODO(Gas): This should not be hardcoded. + let max_gas_budget = u64::MAX.checked_div(1000).unwrap(); if gas_budget >= max_gas_budget { bail!("Gas budget set too high; maximum is {}", max_gas_budget) } - GasStatus::new(cost_table, GasUnits::new(gas_budget)) + GasStatus::new(cost_table, Gas::new(gas_budget)) } else { // no budget specified. Disable gas metering GasStatus::new_unmetered() @@ -69,29 +67,34 @@ pub(crate) fn module(unit: &CompiledUnit) -> Result<&CompiledModule> { } } -pub(crate) fn explain_publish_changeset(changeset: &ChangeSet, state: &OnDiskStateView) { +pub(crate) fn explain_publish_changeset(changeset: &ChangeSet) { // publish effects should contain no resources assert!(changeset.resources().next().is_none()); // total bytes written across all accounts let mut total_bytes_written = 0; - for (addr, name, blob_opt) in changeset.modules() { - if let Some(module_bytes) = blob_opt { - let bytes_written = addr.len() + name.len() + module_bytes.len(); - total_bytes_written += bytes_written; - let module_id = ModuleId::new(addr, name.clone()); - if state.has_module(&module_id) { + for (addr, name, blob_op) in changeset.modules() { + match blob_op { + Op::New(module_bytes) => { + let bytes_written = addr.len() + name.len() + module_bytes.len(); + total_bytes_written += bytes_written; + let module_id = ModuleId::new(addr, name.clone()); println!( - "Updating an existing module {} (wrote {:?} bytes)", + "Publishing a new module {} (wrote {:?} bytes)", module_id, bytes_written ); - } else { + } + Op::Modify(module_bytes) => { + let bytes_written = addr.len() + name.len() + module_bytes.len(); + total_bytes_written += bytes_written; + let module_id = ModuleId::new(addr, name.clone()); println!( - "Publishing a new module {} (wrote {:?} bytes)", + "Updating an existing module {} (wrote {:?} bytes)", module_id, bytes_written ); } - } else { - panic!("Deleting a module is not supported") + Op::Delete => { + panic!("Deleting a module is not supported") + } } } println!( @@ -181,42 +184,39 @@ pub(crate) fn explain_execution_effects( account.resources().len(), addr ); - for (struct_tag, write_opt) in account.resources() { + for (struct_tag, write_op) in account.resources() { print!(" "); let mut bytes_to_write = struct_tag.access_vector().len(); - match write_opt { - Some(blob) => { + match write_op { + Op::New(blob) => { + bytes_to_write += blob.len(); + println!( + "Added type {}: {:?} (wrote {:?} bytes)", + struct_tag, blob, bytes_to_write + ); + // Print new resource + let resource = + MoveValueAnnotator::new(state).view_resource(struct_tag, blob)?; + print_struct_with_indent(&resource, 6) + } + Op::Modify(blob) => { bytes_to_write += blob.len(); - if state + println!( + "Changed type {}: {:?} (wrote {:?} bytes)", + struct_tag, blob, bytes_to_write + ); + // Print resource diff + let resource_data = state .get_resource_bytes(*addr, struct_tag.clone())? - .is_some() - { - println!( - "Changed type {}: {:?} (wrote {:?} bytes)", - struct_tag, blob, bytes_to_write - ); - // Print resource diff - let resource_data = state - .get_resource_bytes(*addr, struct_tag.clone())? - .unwrap(); - let resource_old = MoveValueAnnotator::new(state) - .view_resource(struct_tag, &resource_data)?; - let resource_new = - MoveValueAnnotator::new(state).view_resource(struct_tag, blob)?; + .unwrap(); + let resource_old = + MoveValueAnnotator::new(state).view_resource(struct_tag, &resource_data)?; + let resource_new = + MoveValueAnnotator::new(state).view_resource(struct_tag, blob)?; - print_struct_diff_with_indent(&resource_old, &resource_new, 8) - } else { - println!( - "Added type {}: {:?} (wrote {:?} bytes)", - struct_tag, blob, bytes_to_write - ); - // Print new resource - let resource = - MoveValueAnnotator::new(state).view_resource(struct_tag, blob)?; - print_struct_with_indent(&resource, 6) - } + print_struct_diff_with_indent(&resource_old, &resource_new, 8) } - None => { + Op::Delete => { println!( "Deleted type {} (wrote {:?} bytes)", struct_tag, bytes_to_write @@ -254,10 +254,12 @@ pub(crate) fn maybe_commit_effects( // shouldn't contain modules if commit { for (addr, account) in changeset.into_inner() { - for (struct_tag, blob_opt) in account.into_resources() { - match blob_opt { - Some(blob) => state.save_resource(addr, struct_tag, &blob)?, - None => state.delete_resource(addr, struct_tag)?, + for (struct_tag, blob_op) in account.into_resources() { + match blob_op { + Op::New(blob) | Op::Modify(blob) => { + state.save_resource(addr, struct_tag, &blob)? + } + Op::Delete => state.delete_resource(addr, struct_tag)?, } } } @@ -345,7 +347,7 @@ pub(crate) fn explain_publish_error( let old_module = state.get_module_by_id(&module_id)?.unwrap(); let old_api = normalized::Module::new(&old_module); let new_api = normalized::Module::new(module); - let compat = Compatibility::check(&old_api, &new_api); + let compat = Compatibility::check(true, &old_api, &new_api); // the only way we get this error code is compatibility checking failed, so assert here assert!(!compat.is_fully_compatible()); diff --git a/language/tools/move-cli/src/sandbox/utils/on_disk_state_view.rs b/language/tools/move-cli/src/sandbox/utils/on_disk_state_view.rs index bb3cb5d4db..9bd7246a02 100644 --- a/language/tools/move-cli/src/sandbox/utils/on_disk_state_view.rs +++ b/language/tools/move-cli/src/sandbox/utils/on_disk_state_view.rs @@ -138,7 +138,7 @@ impl OnDiskStateView { return None; } let name = Identifier::new(p.file_stem().unwrap().to_str().unwrap()).unwrap(); - match p.parent().map(|parent| parent.parent()).flatten() { + match p.parent().and_then(|parent| parent.parent()) { Some(parent) => { let addr = AccountAddress::from_hex_literal(parent.file_stem().unwrap().to_str().unwrap()) diff --git a/language/tools/move-cli/src/utils/mod.rs b/language/tools/move-cli/src/utils/mod.rs new file mode 100644 index 0000000000..e304f0120b --- /dev/null +++ b/language/tools/move-cli/src/utils/mod.rs @@ -0,0 +1,4 @@ +// Copyright (c) The Move Contributors +// SPDX-License-Identifier: Apache-2.0 + +pub mod movey_credential; diff --git a/language/tools/move-cli/src/utils/movey_credential.rs b/language/tools/move-cli/src/utils/movey_credential.rs new file mode 100644 index 0000000000..7025b94d27 --- /dev/null +++ b/language/tools/move-cli/src/utils/movey_credential.rs @@ -0,0 +1,192 @@ +// Copyright (c) The Move Contributors +// SPDX-License-Identifier: Apache-2.0 + +use anyhow::{bail, Context, Result}; +use move_command_line_common::movey_constants::{MOVEY_CREDENTIAL_PATH, MOVEY_URL}; +use std::fs; +use toml_edit::easy::Value; + +pub fn get_registry_api_token(move_home: &str) -> Result { + if let Ok(content) = get_api_token(move_home) { + Ok(content) + } else { + bail!( + "There seems to be an error with your Movey API token. \ + Please run `move movey-login` and follow the instructions." + ) + } +} + +pub fn get_api_token(move_home: &str) -> Result { + let credential_path = format!("{}{}", move_home, MOVEY_CREDENTIAL_PATH); + let mut toml: Value = read_credential_file(&credential_path)?; + let token = get_registry_field(&mut toml, "token")?; + Ok(token.to_string().replace('\"', "")) +} + +pub fn get_movey_url(move_home: &str) -> Result { + let credential_path = format!("{}{}", move_home, MOVEY_CREDENTIAL_PATH); + let contents = fs::read_to_string(&credential_path)?; + let mut toml: Value = contents.parse()?; + + let movey_url = get_registry_field(&mut toml, "url"); + if let Ok(url) = movey_url { + Ok(url.to_string().replace('\"', "")) + } else { + Ok(MOVEY_URL.to_string()) + } +} + +fn get_registry_field<'a>(toml: &'a mut Value, field: &'a str) -> Result<&'a mut Value> { + let registry = toml + .as_table_mut() + .context(format!("Error parsing {}", MOVEY_CREDENTIAL_PATH))? + .get_mut("registry") + .context(format!("Error parsing {}", MOVEY_CREDENTIAL_PATH))?; + let value = registry + .as_table_mut() + .context("Error parsing registry table")? + .get_mut(field) + .context("Error parsing token")?; + Ok(value) +} + +pub fn read_credential_file(credential_path: &str) -> Result { + let content = match fs::read_to_string(&credential_path) { + Ok(content) => content, + Err(error) => bail!("Error reading input: {}", error), + }; + content.parse().map_err(|e| { + anyhow::Error::from(e).context(format!( + "could not parse input at {} as TOML", + &credential_path + )) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::{env, fs::File}; + + fn setup_move_home(test_path: &str) -> (String, String) { + let cwd = env::current_dir().unwrap(); + let mut move_home: String = String::from(cwd.to_string_lossy()); + move_home.push_str(test_path); + let credential_path = move_home.clone() + MOVEY_CREDENTIAL_PATH; + + (move_home, credential_path) + } + + fn clean_up(move_home: &str) { + let _ = fs::remove_dir_all(move_home); + } + + #[test] + fn get_api_token_works() { + let test_path = String::from("/get_api_token_works"); + let (move_home, credential_path) = setup_move_home(&test_path); + let _ = fs::create_dir_all(&move_home); + File::create(&credential_path).unwrap(); + + let content = r#" + [registry] + token = "test-token" + "#; + fs::write(&credential_path, content).unwrap(); + + let token = get_registry_api_token(&move_home).unwrap(); + assert!(token.contains("test-token")); + + clean_up(&move_home) + } + + #[test] + fn get_api_token_fails_if_there_is_no_move_home_directory() { + let test_path = String::from("/get_api_token_fails_if_there_is_no_move_home_directory"); + let (move_home, _) = setup_move_home(&test_path); + let _ = fs::remove_dir_all(&move_home); + + let token = get_registry_api_token(&move_home); + assert!(token.is_err()); + + clean_up(&move_home) + } + + #[test] + fn get_api_token_fails_if_there_is_no_credential_file() { + let test_path = String::from("/get_api_token_fails_if_there_is_no_credential_file"); + let (move_home, _) = setup_move_home(&test_path); + let _ = fs::remove_dir_all(&move_home); + fs::create_dir_all(&move_home).unwrap(); + + let token = get_registry_api_token(&move_home); + assert!(token.is_err()); + + clean_up(&move_home) + } + + #[test] + fn get_api_token_fails_if_credential_file_is_in_wrong_format() { + let test_path = String::from("/get_api_token_fails_if_credential_file_is_in_wrong_format"); + let (move_home, credential_path) = setup_move_home(&test_path); + let _ = fs::remove_dir_all(&move_home); + fs::create_dir_all(&move_home).unwrap(); + File::create(&credential_path).unwrap(); + + let missing_double_quote = r#" + [registry] + token = test-token + "#; + fs::write(&credential_path, missing_double_quote).unwrap(); + let token = get_registry_api_token(&move_home); + assert!(token.is_err()); + + let wrong_token_field = r#" + [registry] + tokens = "test-token" + "#; + fs::write(&credential_path, wrong_token_field).unwrap(); + let token = get_registry_api_token(&move_home); + assert!(token.is_err()); + + clean_up(&move_home) + } + + #[test] + fn get_movey_url_works() { + let test_path = String::from("/get_movey_url_works"); + let (move_home, credential_path) = setup_move_home(&test_path); + let _ = fs::create_dir_all(&move_home); + File::create(&credential_path).unwrap(); + let content = r#" + [registry] + token = "test-token" + url = "test-url" + "#; + fs::write(&credential_path, content).unwrap(); + + let url = get_movey_url(&move_home).unwrap(); + assert_eq!(url, "test-url"); + + clean_up(&move_home) + } + + #[test] + fn get_movey_url_returns_default_url_if_url_field_not_existed() { + let test_path = String::from("/get_movey_url_returns_default_url_if_url_field_not_existed"); + let (move_home, credential_path) = setup_move_home(&test_path); + let _ = fs::create_dir_all(&move_home); + File::create(&credential_path).unwrap(); + let content = r#" + [registry] + token = "test-token" + "#; + fs::write(&credential_path, content).unwrap(); + + let url = get_movey_url(&move_home).unwrap(); + assert_eq!(url, MOVEY_URL); + + clean_up(&move_home) + } +} diff --git a/language/tools/move-cli/tests/build_tests/build_with_warnings/Move.toml b/language/tools/move-cli/tests/build_tests/build_with_warnings/Move.toml new file mode 100644 index 0000000000..db3f9fb558 --- /dev/null +++ b/language/tools/move-cli/tests/build_tests/build_with_warnings/Move.toml @@ -0,0 +1,3 @@ +[package] +name = "Test" +version = "0.0.0" diff --git a/language/tools/move-cli/tests/build_tests/build_with_warnings/args.exp b/language/tools/move-cli/tests/build_tests/build_with_warnings/args.exp new file mode 100644 index 0000000000..bdc8f71c25 --- /dev/null +++ b/language/tools/move-cli/tests/build_tests/build_with_warnings/args.exp @@ -0,0 +1,25 @@ +Command `build`: +BUILDING Test +warning[W09002]: unused variable + ┌─ ./sources/m.move:2:16 + │ +2 │ public fun foo(x: u64): u64 { + │ ^ Unused parameter 'x'. Consider removing or prefixing with an underscore: '_x' + +Command `disassemble --package Test --name m`: +// Move bytecode v5 +module 42.m { + + +public foo(x: u64): u64 { +B0: + 0: LdU64(2) + 1: Ret +} +} +warning[W09002]: unused variable + ┌─ ./sources/m.move:2:16 + │ +2 │ public fun foo(x: u64): u64 { + │ ^ Unused parameter 'x'. Consider removing or prefixing with an underscore: '_x' + diff --git a/language/tools/move-cli/tests/build_tests/build_with_warnings/args.txt b/language/tools/move-cli/tests/build_tests/build_with_warnings/args.txt new file mode 100644 index 0000000000..aacafa1ba2 --- /dev/null +++ b/language/tools/move-cli/tests/build_tests/build_with_warnings/args.txt @@ -0,0 +1,2 @@ +build +disassemble --package Test --name m diff --git a/language/tools/move-cli/tests/build_tests/build_with_warnings/modules/m.move b/language/tools/move-cli/tests/build_tests/build_with_warnings/modules/m.move new file mode 100644 index 0000000000..c892ad41ec --- /dev/null +++ b/language/tools/move-cli/tests/build_tests/build_with_warnings/modules/m.move @@ -0,0 +1,5 @@ +module 0x42::m { +public fun foo(x: u64): u64 { + 1 + 1 +} +} diff --git a/language/tools/move-cli/tests/build_tests/build_with_warnings/sources/m.move b/language/tools/move-cli/tests/build_tests/build_with_warnings/sources/m.move new file mode 100644 index 0000000000..c892ad41ec --- /dev/null +++ b/language/tools/move-cli/tests/build_tests/build_with_warnings/sources/m.move @@ -0,0 +1,5 @@ +module 0x42::m { +public fun foo(x: u64): u64 { + 1 + 1 +} +} diff --git a/language/tools/move-cli/tests/build_tests/disassemble_script/Move.toml b/language/tools/move-cli/tests/build_tests/disassemble_script/Move.toml new file mode 100644 index 0000000000..241b837f1b --- /dev/null +++ b/language/tools/move-cli/tests/build_tests/disassemble_script/Move.toml @@ -0,0 +1,3 @@ +[package] +name = "disassemble_script" +version = "0.0.0" diff --git a/language/tools/move-cli/tests/build_tests/disassemble_script/args.exp b/language/tools/move-cli/tests/build_tests/disassemble_script/args.exp new file mode 100644 index 0000000000..0b0fc0653a --- /dev/null +++ b/language/tools/move-cli/tests/build_tests/disassemble_script/args.exp @@ -0,0 +1,11 @@ +Command `disassemble --name main`: +// Move bytecode v5 +script { + + +main() { +L0: x: u64 +B0: + 0: Ret +} +} diff --git a/language/tools/move-cli/tests/build_tests/disassemble_script/args.txt b/language/tools/move-cli/tests/build_tests/disassemble_script/args.txt new file mode 100644 index 0000000000..87f6ca1466 --- /dev/null +++ b/language/tools/move-cli/tests/build_tests/disassemble_script/args.txt @@ -0,0 +1 @@ +disassemble --name main diff --git a/language/tools/move-cli/tests/build_tests/disassemble_script/sources/me.move b/language/tools/move-cli/tests/build_tests/disassemble_script/sources/me.move new file mode 100644 index 0000000000..6aa5426fd2 --- /dev/null +++ b/language/tools/move-cli/tests/build_tests/disassemble_script/sources/me.move @@ -0,0 +1,7 @@ +//# run +script { + fun main() { + let x = 420; + assert!(x == 420, 0); + } +} diff --git a/language/tools/move-cli/tests/build_tests/simple_build_with_docs/args.exp b/language/tools/move-cli/tests/build_tests/simple_build_with_docs/args.exp new file mode 100644 index 0000000000..47903a6fc6 --- /dev/null +++ b/language/tools/move-cli/tests/build_tests/simple_build_with_docs/args.exp @@ -0,0 +1,16 @@ +Command `new --path . Foo`: +Command `build`: +INCLUDING DEPENDENCY MoveStdlib +BUILDING Foo +Command `docgen --template template.md --exclude-impl --exclude-private-fun --exclude-specs --include-call-diagrams --include-dep-diagrams --independent-specs --no-collapsed-sections --output-directory doc --references-file template.md --section-level-start 3 --toc-depth 3`: +Generated "doc/template.md" +Generated "doc/Foo.md" + +Documentation generation successful! +External Command `grep documentation doc/Foo.md`: +Test documentation comment +External Command `cat doc/template.md`: +This is a test template + + +This is a test template diff --git a/language/tools/move-cli/tests/build_tests/simple_build_with_docs/args.txt b/language/tools/move-cli/tests/build_tests/simple_build_with_docs/args.txt new file mode 100644 index 0000000000..549f96d661 --- /dev/null +++ b/language/tools/move-cli/tests/build_tests/simple_build_with_docs/args.txt @@ -0,0 +1,5 @@ +new --path . Foo +build +docgen --template template.md --exclude-impl --exclude-private-fun --exclude-specs --include-call-diagrams --include-dep-diagrams --independent-specs --no-collapsed-sections --output-directory doc --references-file template.md --section-level-start 3 --toc-depth 3 +> grep documentation doc/Foo.md +> cat doc/template.md diff --git a/language/tools/move-cli/tests/build_tests/simple_build_with_docs/sources/Foo.move b/language/tools/move-cli/tests/build_tests/simple_build_with_docs/sources/Foo.move new file mode 100644 index 0000000000..6af3654843 --- /dev/null +++ b/language/tools/move-cli/tests/build_tests/simple_build_with_docs/sources/Foo.move @@ -0,0 +1,5 @@ +module 0x1::Foo { + /// Test documentation comment + public fun foo() { + } +} diff --git a/language/tools/move-cli/tests/build_tests/simple_build_with_docs/template.md b/language/tools/move-cli/tests/build_tests/simple_build_with_docs/template.md new file mode 100644 index 0000000000..a9130f91a8 --- /dev/null +++ b/language/tools/move-cli/tests/build_tests/simple_build_with_docs/template.md @@ -0,0 +1 @@ +This is a test template diff --git a/language/tools/move-cli/tests/cli_tests.rs b/language/tools/move-cli/tests/cli_tests.rs index b77954862b..9504a94e8b 100644 --- a/language/tools/move-cli/tests/cli_tests.rs +++ b/language/tools/move-cli/tests/cli_tests.rs @@ -2,9 +2,24 @@ // Copyright (c) The Move Contributors // SPDX-License-Identifier: Apache-2.0 +use httpmock::{prelude::*, Mock}; use move_cli::sandbox::commands::test; - -use std::path::PathBuf; +use move_command_line_common::{ + files, + movey_constants::{MOVEY_CREDENTIAL_PATH, MOVEY_URL}, +}; +use serde_json::json; +#[cfg(unix)] +use std::fs::File; +#[cfg(unix)] +use std::os::unix::fs::PermissionsExt; +use std::{ + env, fs, + io::Write, + path::PathBuf, + process::{Command, Stdio}, +}; +use toml_edit::easy::Value; pub const CLI_METATEST_PATH: [&str; 3] = ["tests", "metatests", "args.txt"]; @@ -39,17 +54,324 @@ fn run_metatest() { fn cross_process_locking_git_deps() { let cli_exe = env!("CARGO_BIN_EXE_move"); let handle = std::thread::spawn(move || { - std::process::Command::new(cli_exe) + Command::new(cli_exe) .current_dir("./tests/cross_process_tests/Package1") .args(["package", "build"]) .output() .expect("Package1 failed"); }); let cli_exe = env!("CARGO_BIN_EXE_move").to_string(); - std::process::Command::new(cli_exe) + Command::new(cli_exe) .current_dir("./tests/cross_process_tests/Package2") .args(["package", "build"]) .output() .expect("Package2 failed"); handle.join().unwrap(); } + +const UPLOAD_PACKAGE_PATH: &str = "./tests/upload_tests"; +#[test] +fn upload_package_to_movey_works() { + let package_path = format!("{}/valid_package1", UPLOAD_PACKAGE_PATH); + init_git(&package_path, true); + let server = MockServer::start(); + let server_mock = mock_movey_upload_with_response_body_and_status_code(&server, 200, None); + init_stub_registry_file(&package_path, &server.base_url()); + let relative_package_path = PathBuf::from(&package_path); + let absolute_package_path = + files::path_to_string(&relative_package_path.canonicalize().unwrap()).unwrap(); + + let cli_exe = env!("CARGO_BIN_EXE_move"); + let output = Command::new(cli_exe) + .env("MOVE_HOME", &absolute_package_path) + .current_dir(&absolute_package_path) + .args(["movey-upload"]) + .output() + .unwrap(); + + server_mock.assert(); + assert!(output.status.success()); + let output = String::from_utf8_lossy(output.stdout.as_slice()).to_string(); + assert!( + output.contains("Your package has been successfully uploaded to Movey"), + "{}", + output + ); + + clean_up(&absolute_package_path); +} + +#[test] +fn upload_package_to_movey_prints_error_message_if_server_respond_4xx() { + let package_path = format!("{}/valid_package2", UPLOAD_PACKAGE_PATH); + init_git(&package_path, true); + let server = MockServer::start(); + let server_mock = mock_movey_upload_with_response_body_and_status_code( + &server, + 400, + Some("Invalid Api token"), + ); + init_stub_registry_file(&package_path, &server.base_url()); + let relative_package_path = PathBuf::from(&package_path); + let absolute_package_path = + files::path_to_string(&relative_package_path.canonicalize().unwrap()).unwrap(); + + let cli_exe = env!("CARGO_BIN_EXE_move"); + let output = Command::new(cli_exe) + .env("MOVE_HOME", &absolute_package_path) + .current_dir(&absolute_package_path) + .args(["movey-upload"]) + .output() + .unwrap(); + + server_mock.assert(); + assert!(!output.status.success()); + let output = String::from_utf8_lossy(output.stderr.as_slice()).to_string(); + assert!(output.contains("Error: Invalid Api token"), "{}", output); + + clean_up(&absolute_package_path); +} + +#[test] +fn upload_package_to_movey_prints_hardcoded_error_message_if_server_respond_5xx() { + let package_path = format!("{}/valid_package3", UPLOAD_PACKAGE_PATH); + init_git(&package_path, true); + let server = MockServer::start(); + let server_mock = mock_movey_upload_with_response_body_and_status_code( + &server, + 500, + Some("Invalid Api token"), + ); + init_stub_registry_file(&package_path, &server.base_url()); + let relative_package_path = PathBuf::from(&package_path); + let absolute_package_path = + files::path_to_string(&relative_package_path.canonicalize().unwrap()).unwrap(); + + let cli_exe = env!("CARGO_BIN_EXE_move"); + let output = Command::new(cli_exe) + .env("MOVE_HOME", &absolute_package_path) + .current_dir(&absolute_package_path) + .args(["movey-upload"]) + .output() + .unwrap(); + + server_mock.assert(); + assert!(!output.status.success()); + let output = String::from_utf8_lossy(output.stderr.as_slice()).to_string(); + assert!( + output.contains("Error: An unexpected error occurred. Please try again later"), + "{}", + output + ); + + clean_up(&absolute_package_path); +} + +#[test] +fn upload_package_to_movey_with_no_remote_should_panic() { + let package_path = format!("{}/no_git_remote_package", UPLOAD_PACKAGE_PATH); + init_git(&package_path, false); + + let cli_exe = env!("CARGO_BIN_EXE_move"); + let output = Command::new(cli_exe) + .current_dir(&package_path) + .args(["movey-upload"]) + .output() + .unwrap(); + + assert!(!output.status.success()); + let error = String::from_utf8_lossy(output.stderr.as_slice()).to_string(); + assert!(error.contains("invalid git repository")); + + clean_up(&package_path); +} + +// is_valid == true: all git commands are run +// is_valid == false: missing git remote add command +fn init_git(package_path: &str, is_valid: bool) { + Command::new("git") + .current_dir(package_path) + .args(&["init"]) + .output() + .unwrap(); + Command::new("git") + .current_dir(package_path) + .args(&["add", "."]) + .output() + .unwrap(); + if is_valid { + Command::new("git") + .current_dir(package_path) + .args(&[ + "remote", + "add", + "test-origin", + "git@github.com:move-language/move.git", + ]) + .output() + .unwrap(); + Command::new("git") + .current_dir(package_path) + .args(&["config", "user.email", "\"you@example.com\""]) + .output() + .unwrap(); + Command::new("git") + .current_dir(package_path) + .args(&["config", "user.name", "\"Your Name\""]) + .output() + .unwrap(); + Command::new("git") + .current_dir(package_path) + .args(&["commit", "--allow-empty", "-m", "initial commit"]) + .output() + .unwrap(); + } +} +#[test] +fn save_credential_works() { + let cli_exe = env!("CARGO_BIN_EXE_move"); + let (move_home, credential_path) = setup_move_home("/save_credential_works"); + assert!(fs::read_to_string(&credential_path).is_err()); + + match Command::new(cli_exe) + .env("MOVE_HOME", &move_home) + .current_dir(".") + .args(["movey-login"]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + { + Ok(child) => { + let token = "test_token"; + child + .stdin + .as_ref() + .unwrap() + .write_all(token.as_bytes()) + .unwrap(); + match child.wait_with_output() { + Ok(output) => { + assert!(String::from_utf8_lossy(&output.stdout).contains(&format!( + "Please paste the API Token found on {}/settings/tokens below", + MOVEY_URL + ))); + Ok(()) + } + Err(error) => Err(error), + } + } + Err(error) => Err(error), + } + .unwrap(); + + let contents = fs::read_to_string(&credential_path).expect("Unable to read file"); + let mut toml: Value = contents.parse().unwrap(); + let registry = toml.as_table_mut().unwrap().get_mut("registry").unwrap(); + let token = registry.as_table_mut().unwrap().get_mut("token").unwrap(); + assert!(token.to_string().contains("test_token")); + + let _ = fs::remove_dir_all(move_home); +} + +#[cfg(unix)] +#[test] +fn save_credential_fails_if_undeletable_credential_file_exists() { + let cli_exe = env!("CARGO_BIN_EXE_move"); + let (move_home, credential_path) = + setup_move_home("/save_credential_fails_if_undeletable_credential_file_exists"); + let file = File::create(&credential_path).unwrap(); + let mut perms = file.metadata().unwrap().permissions(); + perms.set_mode(0o000); + file.set_permissions(perms).unwrap(); + + match std::process::Command::new(cli_exe) + .env("MOVE_HOME", &move_home) + .current_dir(".") + .args(["movey-login"]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + { + Ok(child) => { + let token = "test_token"; + child + .stdin + .as_ref() + .unwrap() + .write_all(token.as_bytes()) + .unwrap(); + match child.wait_with_output() { + Ok(output) => { + assert!(String::from_utf8_lossy(&output.stdout).contains(&format!( + "Please paste the API Token found on {}/settings/tokens below", + MOVEY_URL + ))); + assert!(String::from_utf8_lossy(&output.stderr) + .contains("Error: Error reading input: Permission denied (os error 13)")); + Ok(()) + } + Err(error) => Err(error), + } + } + Err(error) => Err(error), + } + .unwrap(); + + let mut perms = file.metadata().unwrap().permissions(); + perms.set_mode(0o600); + file.set_permissions(perms).unwrap(); + let _ = fs::remove_file(&credential_path); + + let _ = fs::remove_dir_all(move_home); +} + +fn setup_move_home(test_path: &str) -> (String, String) { + let cwd = env::current_dir().unwrap(); + let mut move_home: String = String::from(cwd.to_string_lossy()); + move_home.push_str(test_path); + let _ = fs::remove_dir_all(&move_home); + fs::create_dir_all(&move_home).unwrap(); + let credential_path = move_home.clone() + MOVEY_CREDENTIAL_PATH; + (move_home, credential_path) +} + +fn clean_up(package_path: &str) { + fs::remove_dir_all(format!("{}/.git", package_path)).unwrap(); + let credential_path = format!("{}{}", package_path, MOVEY_CREDENTIAL_PATH); + let _ = fs::remove_file(&credential_path); +} + +// create a dummy move_credential.toml file for testing +fn init_stub_registry_file(package_path: &str, base_url: &str) { + let credential_path = format!("{}{}", package_path, MOVEY_CREDENTIAL_PATH); + let content = format!( + r#" + [registry] + token = "test-token" + url = "{}" + "#, + base_url + ); + fs::write(credential_path, content).expect("Unable to write file"); +} + +// create a mock server to check if the request is sent or not, also returns a stub response for testing +fn mock_movey_upload_with_response_body_and_status_code<'a>( + server: &'a MockServer, + status_code: u16, + response_body: Option<&str>, +) -> Mock<'a> { + server.mock(|when, then| { + when.method(POST) + .path("/api/v1/packages/upload") + .header("content-type", "application/json") + .json_body(json!({ + "github_repo_url": "https://github.com/move-language/move", + "total_files": 2, + "token": "test-token", + "subdir": '\n' + })); + then.status(status_code).body(response_body.unwrap_or("")); + }) +} diff --git a/language/tools/move-cli/tests/sandbox_tests/cyclic_multi_module_publish/args.exp b/language/tools/move-cli/tests/sandbox_tests/cyclic_multi_module_publish/args.exp index 31617308da..f56f36ddf7 100644 --- a/language/tools/move-cli/tests/sandbox_tests/cyclic_multi_module_publish/args.exp +++ b/language/tools/move-cli/tests/sandbox_tests/cyclic_multi_module_publish/args.exp @@ -6,3 +6,4 @@ Wrote 175 bytes of module ID's and code Command `-p p2 sandbox publish --bundle --override-ordering A --override-ordering C -v`: Found 3 modules Invalid multi-module publishing: VMError with status INVALID_FRIEND_DECL_WITH_MODULES_IN_DEPENDENCIES at location Module ModuleId { address: 00000000000000000000000000000003, name: Identifier("C") } and message At least one module, 00000000000000000000000000000003::A, appears in both the dependency set and the friend set +Publishing failed with unexpected error INVALID_FRIEND_DECL_WITH_MODULES_IN_DEPENDENCIES diff --git a/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/Move.toml b/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/Move.toml new file mode 100644 index 0000000000..25d4f10fdf --- /dev/null +++ b/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/Move.toml @@ -0,0 +1,3 @@ +[package] +name = "verify_native_functions_in_publish" +version = "0.0.0" diff --git a/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/args.exp b/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/args.exp new file mode 100644 index 0000000000..71489b99ba --- /dev/null +++ b/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/args.exp @@ -0,0 +1,8 @@ +Command `sandbox publish --bundle --override-ordering M --override-ordering N`: +Invalid multi-module publishing: VMError with status MISSING_DEPENDENCY at location Module ModuleId { address: 00000000000000000000000000000042, name: Identifier("M") } at index 0 for function handle +error[E02007]: invalid 'fun' declaration + ┌─ ./sources/example.move:3:16 + │ +3 │ native fun create_nothing(); + │ ^^^^^^^^^^^^^^ Missing implementation for the native function M::create_nothing + diff --git a/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/args.txt b/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/args.txt new file mode 100644 index 0000000000..0a739f8bcc --- /dev/null +++ b/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/args.txt @@ -0,0 +1 @@ +sandbox publish --bundle --override-ordering M --override-ordering N diff --git a/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/sources/example.move b/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/sources/example.move new file mode 100644 index 0000000000..916813c8af --- /dev/null +++ b/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/sources/example.move @@ -0,0 +1,4 @@ +module 0x42::M { + native fun create_signer(addr: address): signer; + native fun create_nothing(); +} diff --git a/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/sources/example_another.move b/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/sources/example_another.move new file mode 100644 index 0000000000..62eccfb87e --- /dev/null +++ b/language/tools/move-cli/tests/sandbox_tests/verify_native_functions_in_multi_module_publish/sources/example_another.move @@ -0,0 +1,4 @@ +module 0x42::N { + native fun create_signer(addr: address): signer; + native fun create_nothing(); +} diff --git a/language/tools/move-cli/tests/upload_tests/no_git_remote_package/Move.toml b/language/tools/move-cli/tests/upload_tests/no_git_remote_package/Move.toml new file mode 100644 index 0000000000..637d99d854 --- /dev/null +++ b/language/tools/move-cli/tests/upload_tests/no_git_remote_package/Move.toml @@ -0,0 +1,6 @@ +[package] +name = "Package1" +version = "0.0.0" + +[addresses] +Std = "0x1" diff --git a/language/tools/move-cli/tests/upload_tests/no_git_remote_package/sources/Dummy.move b/language/tools/move-cli/tests/upload_tests/no_git_remote_package/sources/Dummy.move new file mode 100644 index 0000000000..45646f1b13 --- /dev/null +++ b/language/tools/move-cli/tests/upload_tests/no_git_remote_package/sources/Dummy.move @@ -0,0 +1 @@ +module 0x1::Dummy {} diff --git a/language/tools/move-cli/tests/upload_tests/valid_package1/Move.toml b/language/tools/move-cli/tests/upload_tests/valid_package1/Move.toml new file mode 100644 index 0000000000..637d99d854 --- /dev/null +++ b/language/tools/move-cli/tests/upload_tests/valid_package1/Move.toml @@ -0,0 +1,6 @@ +[package] +name = "Package1" +version = "0.0.0" + +[addresses] +Std = "0x1" diff --git a/language/tools/move-cli/tests/upload_tests/valid_package1/sources/Dummy.move b/language/tools/move-cli/tests/upload_tests/valid_package1/sources/Dummy.move new file mode 100644 index 0000000000..45646f1b13 --- /dev/null +++ b/language/tools/move-cli/tests/upload_tests/valid_package1/sources/Dummy.move @@ -0,0 +1 @@ +module 0x1::Dummy {} diff --git a/language/tools/move-cli/tests/upload_tests/valid_package2/Move.toml b/language/tools/move-cli/tests/upload_tests/valid_package2/Move.toml new file mode 100644 index 0000000000..637d99d854 --- /dev/null +++ b/language/tools/move-cli/tests/upload_tests/valid_package2/Move.toml @@ -0,0 +1,6 @@ +[package] +name = "Package1" +version = "0.0.0" + +[addresses] +Std = "0x1" diff --git a/language/tools/move-cli/tests/upload_tests/valid_package2/sources/Dummy.move b/language/tools/move-cli/tests/upload_tests/valid_package2/sources/Dummy.move new file mode 100644 index 0000000000..45646f1b13 --- /dev/null +++ b/language/tools/move-cli/tests/upload_tests/valid_package2/sources/Dummy.move @@ -0,0 +1 @@ +module 0x1::Dummy {} diff --git a/language/tools/move-cli/tests/upload_tests/valid_package3/Move.toml b/language/tools/move-cli/tests/upload_tests/valid_package3/Move.toml new file mode 100644 index 0000000000..637d99d854 --- /dev/null +++ b/language/tools/move-cli/tests/upload_tests/valid_package3/Move.toml @@ -0,0 +1,6 @@ +[package] +name = "Package1" +version = "0.0.0" + +[addresses] +Std = "0x1" diff --git a/language/tools/move-cli/tests/upload_tests/valid_package3/sources/Dummy.move b/language/tools/move-cli/tests/upload_tests/valid_package3/sources/Dummy.move new file mode 100644 index 0000000000..45646f1b13 --- /dev/null +++ b/language/tools/move-cli/tests/upload_tests/valid_package3/sources/Dummy.move @@ -0,0 +1 @@ +module 0x1::Dummy {} diff --git a/language/tools/move-coverage/Cargo.toml b/language/tools/move-coverage/Cargo.toml index 89fcaaec5a..65698f2d96 100644 --- a/language/tools/move-coverage/Cargo.toml +++ b/language/tools/move-coverage/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] once_cell = "1.7.2" @@ -18,7 +18,8 @@ anyhow = "1.0.52" codespan = { version = "0.11.1", features = ["serialization"] } colored = "2.0.0" -bcs = "0.1.2" +bcs.workspace = true + move-command-line-common = { path = "../../move-command-line-common" } move-core-types = { path = "../../move-core/types" } move-ir-types = { path = "../../move-ir/types" } diff --git a/language/tools/move-disassembler/Cargo.toml b/language/tools/move-disassembler/Cargo.toml index ec4587992a..3553844ebd 100644 --- a/language/tools/move-disassembler/Cargo.toml +++ b/language/tools/move-disassembler/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Diem Association "] description = "Print a human-readable version of Move bytecode (.mv files)" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] anyhow = "1.0.52" diff --git a/language/tools/move-explain/Cargo.toml b/language/tools/move-explain/Cargo.toml index 8f9742396a..030f86c352 100644 --- a/language/tools/move-explain/Cargo.toml +++ b/language/tools/move-explain/Cargo.toml @@ -7,13 +7,14 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] clap = { version = "3.1.8", features = ["derive"] } move-command-line-common = { path = "../../move-command-line-common" } move-core-types = { path = "../../move-core/types" } -bcs = "0.1.2" + +bcs.workspace = true [features] default = [] diff --git a/language/tools/move-package/Cargo.toml b/language/tools/move-package/Cargo.toml index 3fbd69206d..9cfdfa525e 100644 --- a/language/tools/move-package/Cargo.toml +++ b/language/tools/move-package/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Diem Association "] description = "Package and build system for Move code" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] toml = "0.5.8" @@ -14,7 +14,6 @@ petgraph = "0.5.1" anyhow = "1.0.52" walkdir = "2.3.1" clap = { version = "3.1.8", features = ["derive"] } -bcs = "0.1.2" colored = "2.0.0" serde_yaml = "0.8.17" tempfile = "3.2.0" @@ -22,8 +21,11 @@ sha2 = "0.9.3" regex = "1.1.9" ptree = "0.4.0" once_cell = "1.7.2" -named-lock = "0.1.1" +named-lock = "0.2.0" dirs-next = "2.0.0" +itertools = "0.10.0" + +bcs.workspace = true move-binary-format = { path = "../../move-binary-format" } move-compiler = { path = "../../move-compiler" } @@ -40,6 +42,9 @@ move-to-yul = { path = "../../evm/move-to-yul", optional = true } evm-exec-utils = { path = "../../evm/exec-utils", optional = true } termcolor = { version = "1.1.2", optional = true } hex = { version = "0.4.3", optional = true } +reqwest = { version = "0.11.1", features = ["blocking", "json"] } + +whoami = { version = "1.2.1" } [dev-dependencies] datatest-stable = "0.1.1" diff --git a/language/tools/move-package/src/compilation/compiled_package.rs b/language/tools/move-package/src/compilation/compiled_package.rs index a2c3786a8f..6c68488f65 100644 --- a/language/tools/move-package/src/compilation/compiled_package.rs +++ b/language/tools/move-package/src/compilation/compiled_package.rs @@ -430,7 +430,9 @@ impl CompiledPackage { self.deps_compiled_units .iter() - .filter(|(dep_package, _)| dep_package.as_str() == package_name) + .filter(|(dep_package, unit)| { + dep_package.as_str() == package_name && matches!(unit.unit, CompiledUnit::Module(_)) + }) .map(|(_, unit)| unit) .find(|unit| unit.unit.name().as_str() == module_name) .ok_or_else(|| { @@ -442,6 +444,31 @@ impl CompiledPackage { }) } + pub fn get_script_by_name( + &self, + package_name: &str, + script_name: &str, + ) -> Result<&CompiledUnitWithSource> { + if self.compiled_package_info.package_name.as_str() == package_name { + return self.get_script_by_name_from_root(script_name); + } + + self.deps_compiled_units + .iter() + .filter(|(dep_package, unit)| { + dep_package.as_str() == package_name && matches!(unit.unit, CompiledUnit::Script(_)) + }) + .map(|(_, unit)| unit) + .find(|unit| unit.unit.name().as_str() == script_name) + .ok_or_else(|| { + anyhow::format_err!( + "Unable to find script with name '{}' in package {}", + script_name, + self.compiled_package_info.package_name + ) + }) + } + pub fn get_module_by_name_from_root( &self, module_name: &str, @@ -457,6 +484,21 @@ impl CompiledPackage { }) } + pub fn get_script_by_name_from_root( + &self, + script_name: &str, + ) -> Result<&CompiledUnitWithSource> { + self.scripts() + .find(|unit| unit.unit.name().as_str() == script_name) + .ok_or_else(|| { + anyhow::format_err!( + "Unable to find script with name '{}' in package {}", + script_name, + self.compiled_package_info.package_name + ) + }) + } + pub fn scripts(&self) -> impl Iterator { self.root_compiled_units .iter() diff --git a/language/tools/move-package/src/lib.rs b/language/tools/move-package/src/lib.rs index 61011bb8e9..798a7fafbb 100644 --- a/language/tools/move-package/src/lib.rs +++ b/language/tools/move-package/src/lib.rs @@ -5,6 +5,7 @@ mod package_lock; pub mod compilation; +pub mod package_hooks; pub mod resolution; pub mod source_package; @@ -89,7 +90,7 @@ impl Architecture { } } -#[derive(Debug, Parser, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd)] +#[derive(Debug, Parser, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Default)] #[clap(author, version, about)] pub struct BuildConfig { /// Compile in 'dev' mode. The 'dev-addresses' and 'dev-dependencies' fields will be used if @@ -129,22 +130,10 @@ pub struct BuildConfig { /// Only fetch dependency repos to MOVE_HOME #[clap(long = "fetch-deps-only", global = true)] pub fetch_deps_only: bool, -} -impl Default for BuildConfig { - fn default() -> Self { - Self { - dev_mode: false, - test_mode: false, - generate_docs: false, - generate_abis: false, - install_dir: None, - force_recompilation: false, - additional_named_addresses: BTreeMap::new(), - architecture: None, - fetch_deps_only: false, - } - } + /// Skip fetching latest git dependencies + #[clap(long = "skip-fetch-latest-git-deps", global = true)] + pub skip_fetch_latest_git_deps: bool, } #[derive(Debug, Clone, Eq, PartialEq, PartialOrd)] diff --git a/language/tools/move-package/src/package_hooks.rs b/language/tools/move-package/src/package_hooks.rs new file mode 100644 index 0000000000..9d6f338926 --- /dev/null +++ b/language/tools/move-package/src/package_hooks.rs @@ -0,0 +1,66 @@ +// Copyright (c) The Move Contributors +// SPDX-License-Identifier: Apache-2.0 + +use crate::source_package::parsed_manifest::CustomDepInfo; +use anyhow::bail; +use move_symbol_pool::Symbol; +use once_cell::sync::Lazy; +use std::sync::Mutex; + +// TODO: remove static hooks and refactor this crate for better customizability + +/// A trait providing hooks to customize the package system for a particular Move application. +/// An instance of the trait can be registered globally. +pub trait PackageHooks { + /// Returns custom fields allowed in `PackageInfo`. + fn custom_package_info_fields(&self) -> Vec; + + /// Returns a custom key for dependencies, if available. This is the string used + /// in dependencies `{ = value, address = addr }. + fn custom_dependency_key(&self) -> Option; + + /// A resolver for custom dependencies in the manifest. This is called to download the + /// dependency from the dependency into the `info.local_path` location, similar as with git + /// dependencies. + fn resolve_custom_dependency( + &self, + dep_name: Symbol, + info: &CustomDepInfo, + ) -> anyhow::Result<()>; +} +static HOOKS: Lazy>>> = + Lazy::new(|| Mutex::new(None)); + +/// Registers package hooks for the process in which the package system is used. +pub fn register_package_hooks(hooks: Box) { + *HOOKS.lock().unwrap() = Some(hooks) +} + +/// Calls any registered hook to resolve a node dependency. Bails if none is registered. +pub(crate) fn resolve_custom_dependency( + dep_name: Symbol, + info: &CustomDepInfo, +) -> anyhow::Result<()> { + if let Some(hooks) = &*HOOKS.lock().unwrap() { + hooks.resolve_custom_dependency(dep_name, info) + } else { + bail!("use of unsupported custom dependency in package manifest") + } +} + +pub(crate) fn custom_dependency_key() -> Option { + if let Some(hooks) = &*HOOKS.lock().unwrap() { + hooks.custom_dependency_key() + } else { + None + } +} + +/// Calls any registered hook to return custom package fields. +pub(crate) fn custom_package_info_fields() -> Vec { + if let Some(hooks) = &*HOOKS.lock().unwrap() { + hooks.custom_package_info_fields() + } else { + vec![] + } +} diff --git a/language/tools/move-package/src/package_lock.rs b/language/tools/move-package/src/package_lock.rs index 010a9da382..b275bf792d 100644 --- a/language/tools/move-package/src/package_lock.rs +++ b/language/tools/move-package/src/package_lock.rs @@ -5,11 +5,14 @@ use named_lock::{NamedLock, NamedLockGuard}; use once_cell::sync::Lazy; use std::sync::{Mutex, MutexGuard}; +use whoami::username; const PACKAGE_LOCK_NAME: &str = "move_pkg_lock"; static PACKAGE_THREAD_MUTEX: Lazy> = Lazy::new(|| Mutex::new(())); -static PACKAGE_PROCESS_MUTEX: Lazy = - Lazy::new(|| NamedLock::create(PACKAGE_LOCK_NAME).unwrap()); +static PACKAGE_PROCESS_MUTEX: Lazy = Lazy::new(|| { + let user_lock_file = format!("{}_{}", PACKAGE_LOCK_NAME, username()); + NamedLock::create(user_lock_file.as_str()).unwrap() +}); /// The package lock is a lock held across threads and processes. This lock is held to ensure that /// the Move package manager has a consistent (read: serial) view of the file system. Without this diff --git a/language/tools/move-package/src/resolution/resolution_graph.rs b/language/tools/move-package/src/resolution/resolution_graph.rs index 295e0e948f..1bf8974b78 100644 --- a/language/tools/move-package/src/resolution/resolution_graph.rs +++ b/language/tools/move-package/src/resolution/resolution_graph.rs @@ -3,6 +3,7 @@ // SPDX-License-Identifier: Apache-2.0 use crate::{ + package_hooks, resolution::digest::compute_digest, source_package::{ layout::SourcePackageLayout, @@ -25,7 +26,7 @@ use std::{ collections::{BTreeMap, BTreeSet}, fs, path::{Path, PathBuf}, - process::Command, + process::{Command, Stdio}, rc::Rc, }; @@ -300,12 +301,7 @@ impl ResolvingGraph { is_root_package: bool, ) -> Result<()> { let package_name = &package.package.name; - for (name, addr_opt) in package - .addresses - .clone() - .unwrap_or_else(BTreeMap::new) - .into_iter() - { + for (name, addr_opt) in package.addresses.clone().unwrap_or_default().into_iter() { match resolution_table.get(&name) { Some(other) => { other.unify(addr_opt).with_context(|| { @@ -337,7 +333,7 @@ impl ResolvingGraph { for (name, addr) in package .dev_address_assignments .clone() - .unwrap_or_else(BTreeMap::new) + .unwrap_or_default() .into_iter() { match resolution_table.get(&name) { @@ -392,7 +388,11 @@ impl ResolvingGraph { dep: Dependency, root_path: PathBuf, ) -> Result<(Renaming, ResolvingTable)> { - Self::download_and_update_if_repo(dep_name_in_pkg, &dep)?; + Self::download_and_update_if_remote( + dep_name_in_pkg, + &dep, + self.build_options.skip_fetch_latest_git_deps, + )?; let (dep_package, dep_package_dir) = Self::parse_package_manifest(&dep, &dep_name_in_pkg, root_path) .with_context(|| format!("While processing dependency '{}'", dep_name_in_pkg))?; @@ -530,20 +530,29 @@ impl ResolvingGraph { }; for (dep_name, dep) in manifest.dependencies.iter().chain(additional_deps.iter()) { - Self::download_and_update_if_repo(*dep_name, dep)?; + Self::download_and_update_if_remote( + *dep_name, + dep, + build_options.skip_fetch_latest_git_deps, + )?; let (dep_manifest, _) = Self::parse_package_manifest(dep, dep_name, root_path.to_path_buf()) .with_context(|| format!("While processing dependency '{}'", *dep_name))?; // download dependencies of dependencies - Self::download_dependency_repos(&dep_manifest, &build_options, root_path)?; + Self::download_dependency_repos(&dep_manifest, build_options, root_path)?; } Ok(()) } - fn download_and_update_if_repo(dep_name: PackageName, dep: &Dependency) -> Result<()> { + fn download_and_update_if_remote( + dep_name: PackageName, + dep: &Dependency, + skip_fetch_latest_git_deps: bool, + ) -> Result<()> { if let Some(git_info) = &dep.git_info { if !git_info.download_to.exists() { + // If the cached folder does not exist, download and clone accordingly Command::new("git") .args([ "clone", @@ -569,8 +578,83 @@ impl ResolvingGraph { dep_name ) })?; + } else if !skip_fetch_latest_git_deps { + let git_path = &git_info.download_to.display().to_string(); + + // Check first that it isn't a git rev (if it doesn't work, just continue with the fetch) + if let Ok(rev) = Command::new("git") + .args(["-C", git_path, "rev-parse", "--verify", &git_info.git_rev]) + .output() + { + if let Ok(parsable_version) = String::from_utf8(rev.stdout) { + // If it's exactly the same, then it's a git rev + if parsable_version + .trim() + .starts_with(git_info.git_rev.as_str()) + { + return Ok(()); + } + } + } + + // If the current folder exists, do a fetch and reset to ensure that the branch + // is up to date + // NOTE: this means that you must run the package system with a working network connection + let status = Command::new("git") + .args([ + "-C", + git_path, + "fetch", + "origin", + ]) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .map_err(|_| { + anyhow::anyhow!( + "Failed to fetch latest Git state for package '{}', to skip set --skip-fetch-latest-git-deps", + dep_name + ) + })?; + + if !status.success() { + return Err(anyhow::anyhow!( + "Failed to fetch to latest Git state for package '{}', to skip set --skip-fetch-latest-git-deps | Exit status: {}", + dep_name, + status + )); + } + let status = Command::new("git") + .args([ + "-C", + git_path, + "reset", + "--hard", + &format!("origin/{}", git_info.git_rev) + ]) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .map_err(|_| { + anyhow::anyhow!( + "Failed to reset to latest Git state '{}' for package '{}', to skip set --skip-fetch-latest-git-deps", + &git_info.git_rev, + dep_name + ) + })?; + if !status.success() { + return Err(anyhow::anyhow!( + "Failed to reset to latest Git state '{}' for package '{}', to skip set --skip-fetch-latest-git-deps | Exit status: {}", + &git_info.git_rev, + dep_name, + status + )); + } } } + if let Some(node_info) = &dep.node_info { + package_hooks::resolve_custom_dependency(dep_name, node_info)? + } Ok(()) } } diff --git a/language/tools/move-package/src/source_package/manifest_parser.rs b/language/tools/move-package/src/source_package/manifest_parser.rs index 69f17cce68..bc438ca7b2 100644 --- a/language/tools/move-package/src/source_package/manifest_parser.rs +++ b/language/tools/move-package/src/source_package/manifest_parser.rs @@ -2,8 +2,9 @@ // Copyright (c) The Move Contributors // SPDX-License-Identifier: Apache-2.0 -use crate::{source_package::parsed_manifest as PM, Architecture}; +use crate::{package_hooks, source_package::parsed_manifest as PM, Architecture}; use anyhow::{bail, format_err, Context, Result}; +use move_command_line_common::env::MOVE_HOME; use move_core_types::account_address::{AccountAddress, AccountAddressParseError}; use move_symbol_pool::symbol::Symbol; use std::{ @@ -79,13 +80,13 @@ pub fn parse_source_manifest(tval: TV) -> Result { .map(parse_dependencies) .transpose() .context("Error parsing '[dependencies]' section of manifest")? - .unwrap_or_else(BTreeMap::new); + .unwrap_or_default(); let dev_dependencies = table .remove(DEV_DEPENDENCY_NAME) .map(parse_dependencies) .transpose() .context("Error parsing '[dev-dependencies]' section of manifest")? - .unwrap_or_else(BTreeMap::new); + .unwrap_or_default(); Ok(PM::SourceManifest { package, addresses, @@ -109,7 +110,12 @@ pub fn parse_package_info(tval: TV) -> Result { match tval { TV::Table(mut table) => { check_for_required_field_names(&table, &["name", "version"])?; - warn_if_unknown_field_names(&table, &["name", "version", "authors", "license"]); + let hook_names = package_hooks::custom_package_info_fields(); + let known_names = ["name", "version", "authors", "license"] + .into_iter() + .chain(hook_names.iter().map(|s| s.as_str())) + .collect::>(); + warn_if_unknown_field_names(&table, known_names.as_slice()); let name = table .remove("name") .ok_or_else(|| format_err!("'name' is a required field but was not found",))?; @@ -144,12 +150,22 @@ pub fn parse_package_info(tval: TV) -> Result { .collect::>()? } }; + // Turn the remaining entries into custom properties. For those which are not + // supported (also in the presence of hooks) we have warned above. + let mut custom_properties: BTreeMap = Default::default(); + for (name, val) in table { + let val_str = val + .as_str() + .ok_or_else(|| format_err!("Field `{}` value must be a string", name))?; + custom_properties.insert(Symbol::from(name), val_str.to_owned()); + } Ok(PM::PackageInfo { name, version, authors, license, + custom_properties, }) } x => bail!( @@ -165,8 +181,8 @@ pub fn parse_dependencies(tval: TV) -> Result { TV::Table(table) => { let mut deps = BTreeMap::new(); for (dep_name, dep) in table.into_iter() { - let dep_name_ident = PM::PackageName::from(dep_name); - let dep = parse_dependency(dep)?; + let dep_name_ident = PM::PackageName::from(dep_name.clone()); + let dep = parse_dependency(&dep_name, dep)?; deps.insert(dep_name_ident, dep); } Ok(deps) @@ -290,21 +306,24 @@ fn parse_address_literal(address_str: &str) -> Result Result { +fn parse_dependency(dep_name: &str, tval: TV) -> Result { match tval { TV::Table(mut table) => { - warn_if_unknown_field_names( - &table, - &[ - "addr_subst", - "version", - "local", - "digest", - "git", - "rev", - "subdir", - ], - ); + let mut known_fields = vec![ + "addr_subst", + "version", + "local", + "digest", + "git", + "rev", + "subdir", + "address", + ]; + let custom_key_opt = &package_hooks::custom_dependency_key(); + if let Some(key) = custom_key_opt { + known_fields.push(key.as_ref()) + } + warn_if_unknown_field_names(&table, known_fields.as_slice()); let subst = table .remove("addr_subst") .map(parse_substitution) @@ -312,8 +331,17 @@ fn parse_dependency(tval: TV) -> Result { let version = table.remove("version").map(parse_version).transpose()?; let digest = table.remove("digest").map(parse_digest).transpose()?; let mut git_info = None; - match (table.remove("local"), table.remove("git")) { - (Some(local), None) => { + let mut node_info = None; + match ( + table.remove("local"), + table.remove("git"), + if let Some(key) = custom_key_opt { + table.remove(key) + } else { + None + }, + ) { + (Some(local), None, None) => { let local_str = local .as_str() .ok_or_else(|| format_err!("Local source path not a string"))?; @@ -324,19 +352,11 @@ fn parse_dependency(tval: TV) -> Result { digest, local: local_path, git_info, + node_info, }) } - (None, Some(git)) => { - // Look to see if a MOVE_HOME has been set. Otherwise default to $HOME - let move_home = std::env::var("MOVE_HOME").unwrap_or_else(|_| { - format!( - "{}/.move", - dirs_next::home_dir() - .expect("user's home directory not found") - .to_str() - .unwrap() - ) - }); + (None, Some(git), None) => { + let move_home = MOVE_HOME.clone(); let rev_name = match table.remove("rev") { None => bail!("Git revision not supplied for dependency"), Some(r) => Symbol::from( @@ -345,13 +365,12 @@ fn parse_dependency(tval: TV) -> Result { ), }; // Downloaded packages are of the form _ + let git_url = git + .as_str() + .ok_or_else(|| anyhow::anyhow!("Git URL not a string"))?; let local_path = PathBuf::from(move_home).join(format!( "{}_{}", - regex::Regex::new(r"/|:|\.|@").unwrap().replace_all( - git.as_str() - .ok_or_else(|| anyhow::anyhow!("Git URL not a string"))?, - "_" - ), + url_to_file_name(git_url), rev_name.replace('/', "__") )); let subdir = PathBuf::from(match table.remove("subdir") { @@ -362,10 +381,7 @@ fn parse_dependency(tval: TV) -> Result { .to_string(), }); git_info = Some(PM::GitInfo { - git_url: Symbol::from( - git.as_str() - .ok_or_else(|| format_err!("Git url not a string"))?, - ), + git_url: Symbol::from(git_url), git_rev: rev_name, subdir: subdir.clone(), download_to: local_path.clone(), @@ -377,13 +393,56 @@ fn parse_dependency(tval: TV) -> Result { digest, local: local_path.join(subdir), git_info, + node_info, }) } - (Some(_), Some(_)) => { - bail!("both 'local' and 'git' paths specified for dependency.") + (None, None, Some(custom_key)) => { + let package_name = Symbol::from(dep_name); + let address = match table.remove("address") { + None => bail!("Address not supplied for 'node' dependency"), + Some(r) => Symbol::from( + r.as_str() + .ok_or_else(|| format_err!("Node address not a string"))?, + ), + }; + // Downloaded packages are of the form _
_ + let node_url = custom_key + .as_str() + .ok_or_else(|| anyhow::anyhow!("Git URL not a string"))?; + let local_path = PathBuf::from(MOVE_HOME.clone()).join(format!( + "{}_{}_{}", + url_to_file_name(node_url), + address, + package_name + )); + node_info = Some(PM::CustomDepInfo { + node_url: Symbol::from(node_url), + package_address: address, + package_name, + download_to: local_path.clone(), + }); + Ok(PM::Dependency { + subst, + version, + digest, + local: local_path, + git_info, + node_info, + }) } - (None, None) => { - bail!("both 'local' and 'git' paths not specified for dependency.") + _ => { + let mut keys = vec!["local", "git"]; + if let Some(k) = custom_key_opt { + keys.push(k.as_str()) + } + let keys = keys + .into_iter() + .map(|s| format!("'{}'", s)) + .collect::>(); + bail!( + "must provide exactly one of {} for dependency.", + keys.join(" or ") + ) } } } @@ -391,6 +450,13 @@ fn parse_dependency(tval: TV) -> Result { } } +fn url_to_file_name(url: &str) -> String { + regex::Regex::new(r"/|:|\.|@") + .unwrap() + .replace_all(url, "_") + .to_string() +} + fn parse_substitution(tval: TV) -> Result { match tval { TV::Table(table) => { diff --git a/language/tools/move-package/src/source_package/parsed_manifest.rs b/language/tools/move-package/src/source_package/parsed_manifest.rs index f491fa1dc8..606fca7009 100644 --- a/language/tools/move-package/src/source_package/parsed_manifest.rs +++ b/language/tools/move-package/src/source_package/parsed_manifest.rs @@ -34,6 +34,7 @@ pub struct PackageInfo { pub version: Version, pub authors: Vec, pub license: Option, + pub custom_properties: BTreeMap, } #[derive(Debug, Clone, Eq, PartialEq)] @@ -43,6 +44,7 @@ pub struct Dependency { pub version: Option, pub digest: Option, pub git_info: Option, + pub node_info: Option, } #[derive(Debug, Clone, Eq, PartialEq)] @@ -58,6 +60,19 @@ pub struct GitInfo { pub download_to: PathBuf, } +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct CustomDepInfo { + /// The url of the node to download from + pub node_url: Symbol, + /// The address where the package is published. The representation depends + /// on the registered node resolver. + pub package_address: Symbol, + /// The address where the package is published. + pub package_name: Symbol, + /// Where the package is downloaded to. + pub download_to: PathBuf, +} + #[derive(Default, Debug, Clone, Eq, PartialEq)] pub struct BuildInfo { pub language_version: Option, diff --git a/language/tools/move-package/tests/test_runner.rs b/language/tools/move-package/tests/test_runner.rs index 18ee00e373..3f72c5ec26 100644 --- a/language/tools/move-package/tests/test_runner.rs +++ b/language/tools/move-package/tests/test_runner.rs @@ -2,15 +2,22 @@ // Copyright (c) The Move Contributors // SPDX-License-Identifier: Apache-2.0 +use anyhow::bail; use move_command_line_common::testing::{ add_update_baseline_fix, format_diff, read_env_update_baseline, EXP_EXT, }; use move_package::{ compilation::{build_plan::BuildPlan, model_builder::ModelBuilder}, + package_hooks, + package_hooks::PackageHooks, resolution::resolution_graph as RG, - source_package::{manifest_parser as MP, parsed_manifest::PackageDigest}, + source_package::{ + manifest_parser as MP, + parsed_manifest::{CustomDepInfo, PackageDigest}, + }, BuildConfig, ModelConfig, }; +use move_symbol_pool::Symbol; use std::{ ffi::OsStr, fs, @@ -22,6 +29,7 @@ const COMPILE_EXT: &str = "compile"; const MODEL_EXT: &str = "model"; pub fn run_test(path: &Path) -> datatest_stable::Result<()> { + package_hooks::register_package_hooks(Box::new(TestHooks())); let update_baseline = read_env_update_baseline(); if path .components() @@ -124,4 +132,31 @@ pub fn run_test(path: &Path) -> datatest_stable::Result<()> { Ok(()) } +/// Some dummy hooks for testing the hook mechanism +struct TestHooks(); + +impl PackageHooks for TestHooks { + fn custom_package_info_fields(&self) -> Vec { + vec!["test_hooks_field".to_owned()] + } + + fn custom_dependency_key(&self) -> Option { + Some("custom".to_owned()) + } + + fn resolve_custom_dependency( + &self, + dep_name: Symbol, + info: &CustomDepInfo, + ) -> anyhow::Result<()> { + bail!( + "TestHooks resolve dep {} = {} {} {}", + dep_name, + info.node_url, + info.package_name, + info.package_address + ) + } +} + datatest_stable::harness!(run_test, "tests/test_sources", r".*\.toml$"); diff --git a/language/tools/move-package/tests/test_sources/compilation/basic_no_deps/Move.exp b/language/tools/move-package/tests/test_sources/compilation/basic_no_deps/Move.exp index 9974fc0647..1722fc807b 100644 --- a/language/tools/move-package/tests/test_sources/compilation/basic_no_deps/Move.exp +++ b/language/tools/move-package/tests/test_sources/compilation/basic_no_deps/Move.exp @@ -16,5 +16,6 @@ CompiledPackageInfo { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, } diff --git a/language/tools/move-package/tests/test_sources/compilation/basic_no_deps_address_assigned/Move.exp b/language/tools/move-package/tests/test_sources/compilation/basic_no_deps_address_assigned/Move.exp index 3862f486ac..c000418d5e 100644 --- a/language/tools/move-package/tests/test_sources/compilation/basic_no_deps_address_assigned/Move.exp +++ b/language/tools/move-package/tests/test_sources/compilation/basic_no_deps_address_assigned/Move.exp @@ -18,5 +18,6 @@ CompiledPackageInfo { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, } diff --git a/language/tools/move-package/tests/test_sources/compilation/basic_no_deps_address_not_assigned_with_dev_assignment/Move.exp b/language/tools/move-package/tests/test_sources/compilation/basic_no_deps_address_not_assigned_with_dev_assignment/Move.exp index f8c5b1d503..661d318a28 100644 --- a/language/tools/move-package/tests/test_sources/compilation/basic_no_deps_address_not_assigned_with_dev_assignment/Move.exp +++ b/language/tools/move-package/tests/test_sources/compilation/basic_no_deps_address_not_assigned_with_dev_assignment/Move.exp @@ -18,5 +18,6 @@ CompiledPackageInfo { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, } diff --git a/language/tools/move-package/tests/test_sources/compilation/basic_no_deps_test_mode/Move.exp b/language/tools/move-package/tests/test_sources/compilation/basic_no_deps_test_mode/Move.exp index af0fe4123f..b4be3ab9f3 100644 --- a/language/tools/move-package/tests/test_sources/compilation/basic_no_deps_test_mode/Move.exp +++ b/language/tools/move-package/tests/test_sources/compilation/basic_no_deps_test_mode/Move.exp @@ -18,5 +18,6 @@ CompiledPackageInfo { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, } diff --git a/language/tools/move-package/tests/test_sources/compilation/diamond_problem_backflow_resolution/Move.exp b/language/tools/move-package/tests/test_sources/compilation/diamond_problem_backflow_resolution/Move.exp index 9d61982e1b..edeeaa45ca 100644 --- a/language/tools/move-package/tests/test_sources/compilation/diamond_problem_backflow_resolution/Move.exp +++ b/language/tools/move-package/tests/test_sources/compilation/diamond_problem_backflow_resolution/Move.exp @@ -19,5 +19,6 @@ CompiledPackageInfo { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, } diff --git a/language/tools/move-package/tests/test_sources/compilation/diamond_problem_no_conflict/Move.exp b/language/tools/move-package/tests/test_sources/compilation/diamond_problem_no_conflict/Move.exp index 9d61982e1b..edeeaa45ca 100644 --- a/language/tools/move-package/tests/test_sources/compilation/diamond_problem_no_conflict/Move.exp +++ b/language/tools/move-package/tests/test_sources/compilation/diamond_problem_no_conflict/Move.exp @@ -19,5 +19,6 @@ CompiledPackageInfo { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, } diff --git a/language/tools/move-package/tests/test_sources/compilation/multiple_deps_rename/Move.exp b/language/tools/move-package/tests/test_sources/compilation/multiple_deps_rename/Move.exp index c95634f0ae..3ddf5572bf 100644 --- a/language/tools/move-package/tests/test_sources/compilation/multiple_deps_rename/Move.exp +++ b/language/tools/move-package/tests/test_sources/compilation/multiple_deps_rename/Move.exp @@ -20,5 +20,6 @@ CompiledPackageInfo { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, } diff --git a/language/tools/move-package/tests/test_sources/compilation/multiple_deps_rename_one/Move.exp b/language/tools/move-package/tests/test_sources/compilation/multiple_deps_rename_one/Move.exp index 81b4a35692..9250da1cd6 100644 --- a/language/tools/move-package/tests/test_sources/compilation/multiple_deps_rename_one/Move.exp +++ b/language/tools/move-package/tests/test_sources/compilation/multiple_deps_rename_one/Move.exp @@ -20,5 +20,6 @@ CompiledPackageInfo { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, } diff --git a/language/tools/move-package/tests/test_sources/compilation/one_dep/Move.exp b/language/tools/move-package/tests/test_sources/compilation/one_dep/Move.exp index baed78ba25..4c9731e1f0 100644 --- a/language/tools/move-package/tests/test_sources/compilation/one_dep/Move.exp +++ b/language/tools/move-package/tests/test_sources/compilation/one_dep/Move.exp @@ -18,5 +18,6 @@ CompiledPackageInfo { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, } diff --git a/language/tools/move-package/tests/test_sources/compilation/one_dep_assigned_address/Move.exp b/language/tools/move-package/tests/test_sources/compilation/one_dep_assigned_address/Move.exp index c21c42001e..ad6c5c7190 100644 --- a/language/tools/move-package/tests/test_sources/compilation/one_dep_assigned_address/Move.exp +++ b/language/tools/move-package/tests/test_sources/compilation/one_dep_assigned_address/Move.exp @@ -18,5 +18,6 @@ CompiledPackageInfo { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, } diff --git a/language/tools/move-package/tests/test_sources/compilation/one_dep_renamed/Move.exp b/language/tools/move-package/tests/test_sources/compilation/one_dep_renamed/Move.exp index baed78ba25..4c9731e1f0 100644 --- a/language/tools/move-package/tests/test_sources/compilation/one_dep_renamed/Move.exp +++ b/language/tools/move-package/tests/test_sources/compilation/one_dep_renamed/Move.exp @@ -18,5 +18,6 @@ CompiledPackageInfo { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, } diff --git a/language/tools/move-package/tests/test_sources/compilation/one_dep_with_scripts/Move.exp b/language/tools/move-package/tests/test_sources/compilation/one_dep_with_scripts/Move.exp index baed78ba25..4c9731e1f0 100644 --- a/language/tools/move-package/tests/test_sources/compilation/one_dep_with_scripts/Move.exp +++ b/language/tools/move-package/tests/test_sources/compilation/one_dep_with_scripts/Move.exp @@ -18,5 +18,6 @@ CompiledPackageInfo { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, } diff --git a/language/tools/move-package/tests/test_sources/compilation/test_symlinks/Move.exp b/language/tools/move-package/tests/test_sources/compilation/test_symlinks/Move.exp index 3862f486ac..c000418d5e 100644 --- a/language/tools/move-package/tests/test_sources/compilation/test_symlinks/Move.exp +++ b/language/tools/move-package/tests/test_sources/compilation/test_symlinks/Move.exp @@ -18,5 +18,6 @@ CompiledPackageInfo { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, } diff --git a/language/tools/move-package/tests/test_sources/parsing/invalid_identifier_package_name/Move.exp b/language/tools/move-package/tests/test_sources/parsing/invalid_identifier_package_name/Move.exp index 0aa3f6aa02..bd137f194d 100644 --- a/language/tools/move-package/tests/test_sources/parsing/invalid_identifier_package_name/Move.exp +++ b/language/tools/move-package/tests/test_sources/parsing/invalid_identifier_package_name/Move.exp @@ -12,6 +12,7 @@ ResolutionGraph { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, root_package: SourceManifest { package: PackageInfo { @@ -23,6 +24,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -46,6 +48,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, diff --git a/language/tools/move-package/tests/test_sources/parsing/minimal_manifest/Move.exp b/language/tools/move-package/tests/test_sources/parsing/minimal_manifest/Move.exp index 539c3cdd96..dc8b7e4951 100644 --- a/language/tools/move-package/tests/test_sources/parsing/minimal_manifest/Move.exp +++ b/language/tools/move-package/tests/test_sources/parsing/minimal_manifest/Move.exp @@ -12,6 +12,7 @@ ResolutionGraph { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, root_package: SourceManifest { package: PackageInfo { @@ -23,6 +24,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -46,6 +48,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, diff --git a/language/tools/move-package/tests/test_sources/parsing/no_path_set_for_dependency/Move.exp b/language/tools/move-package/tests/test_sources/parsing/no_path_set_for_dependency/Move.exp index 53e2632f8f..8c3567f859 100644 --- a/language/tools/move-package/tests/test_sources/parsing/no_path_set_for_dependency/Move.exp +++ b/language/tools/move-package/tests/test_sources/parsing/no_path_set_for_dependency/Move.exp @@ -1 +1 @@ -Error parsing '[dependencies]' section of manifest: both 'local' and 'git' paths not specified for dependency. +Error parsing '[dependencies]' section of manifest: must provide exactly one of 'local' or 'git' or 'custom' for dependency. diff --git a/language/tools/move-package/tests/test_sources/resolution/basic_no_deps/Move.exp b/language/tools/move-package/tests/test_sources/resolution/basic_no_deps/Move.exp index 9187edcc25..5d27cc1fe4 100644 --- a/language/tools/move-package/tests/test_sources/resolution/basic_no_deps/Move.exp +++ b/language/tools/move-package/tests/test_sources/resolution/basic_no_deps/Move.exp @@ -12,6 +12,7 @@ ResolutionGraph { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, root_package: SourceManifest { package: PackageInfo { @@ -23,6 +24,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -46,6 +48,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, diff --git a/language/tools/move-package/tests/test_sources/resolution/basic_no_deps_address_assigned/Move.exp b/language/tools/move-package/tests/test_sources/resolution/basic_no_deps_address_assigned/Move.exp index 9d053f3d14..c534947764 100644 --- a/language/tools/move-package/tests/test_sources/resolution/basic_no_deps_address_assigned/Move.exp +++ b/language/tools/move-package/tests/test_sources/resolution/basic_no_deps_address_assigned/Move.exp @@ -12,6 +12,7 @@ ResolutionGraph { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, root_package: SourceManifest { package: PackageInfo { @@ -23,6 +24,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -52,6 +54,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { diff --git a/language/tools/move-package/tests/test_sources/resolution/basic_no_deps_address_not_assigned_with_dev_assignment/Move.exp b/language/tools/move-package/tests/test_sources/resolution/basic_no_deps_address_not_assigned_with_dev_assignment/Move.exp index 105824f328..02ca66b3a1 100644 --- a/language/tools/move-package/tests/test_sources/resolution/basic_no_deps_address_not_assigned_with_dev_assignment/Move.exp +++ b/language/tools/move-package/tests/test_sources/resolution/basic_no_deps_address_not_assigned_with_dev_assignment/Move.exp @@ -12,6 +12,7 @@ ResolutionGraph { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, root_package: SourceManifest { package: PackageInfo { @@ -23,6 +24,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -54,6 +56,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { diff --git a/language/tools/move-package/tests/test_sources/resolution/dep_good_digest/Move.exp b/language/tools/move-package/tests/test_sources/resolution/dep_good_digest/Move.exp index ab4d241291..c8adbaa752 100644 --- a/language/tools/move-package/tests/test_sources/resolution/dep_good_digest/Move.exp +++ b/language/tools/move-package/tests/test_sources/resolution/dep_good_digest/Move.exp @@ -12,6 +12,7 @@ ResolutionGraph { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, root_package: SourceManifest { package: PackageInfo { @@ -23,6 +24,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -48,6 +50,7 @@ ResolutionGraph { "6A88B7888D6049EB0121900E22B6FA2C0E702F042C8C8D4FD62AD5C990B9F9A8", ), git_info: None, + node_info: None, }, }, dev_dependencies: {}, @@ -79,6 +82,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -109,6 +113,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -134,6 +139,7 @@ ResolutionGraph { "6A88B7888D6049EB0121900E22B6FA2C0E702F042C8C8D4FD62AD5C990B9F9A8", ), git_info: None, + node_info: None, }, }, dev_dependencies: {}, diff --git a/language/tools/move-package/tests/test_sources/resolution/diamond_problem_backflow_resolution/Move.exp b/language/tools/move-package/tests/test_sources/resolution/diamond_problem_backflow_resolution/Move.exp index e55d032f09..e7f2e139ac 100644 --- a/language/tools/move-package/tests/test_sources/resolution/diamond_problem_backflow_resolution/Move.exp +++ b/language/tools/move-package/tests/test_sources/resolution/diamond_problem_backflow_resolution/Move.exp @@ -12,6 +12,7 @@ ResolutionGraph { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, root_package: SourceManifest { package: PackageInfo { @@ -23,6 +24,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -34,6 +36,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, "B": Dependency { local: "./deps_only/B", @@ -47,6 +50,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, @@ -106,6 +110,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -123,6 +128,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, @@ -151,6 +157,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -168,6 +175,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, @@ -196,6 +204,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -226,6 +235,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -237,6 +247,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, "B": Dependency { local: "./deps_only/B", @@ -250,6 +261,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, diff --git a/language/tools/move-package/tests/test_sources/resolution/diamond_problem_no_conflict/Move.exp b/language/tools/move-package/tests/test_sources/resolution/diamond_problem_no_conflict/Move.exp index ada69c02bf..19d5e0a453 100644 --- a/language/tools/move-package/tests/test_sources/resolution/diamond_problem_no_conflict/Move.exp +++ b/language/tools/move-package/tests/test_sources/resolution/diamond_problem_no_conflict/Move.exp @@ -12,6 +12,7 @@ ResolutionGraph { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, root_package: SourceManifest { package: PackageInfo { @@ -23,6 +24,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -40,6 +42,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, "B": Dependency { local: "./deps_only/B", @@ -53,6 +56,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, @@ -112,6 +116,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -129,6 +134,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, @@ -157,6 +163,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -174,6 +181,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, @@ -202,6 +210,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -232,6 +241,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -249,6 +259,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, "B": Dependency { local: "./deps_only/B", @@ -262,6 +273,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, diff --git a/language/tools/move-package/tests/test_sources/resolution/multiple_deps_rename/Move.exp b/language/tools/move-package/tests/test_sources/resolution/multiple_deps_rename/Move.exp index 9d098030d4..bea1ff066b 100644 --- a/language/tools/move-package/tests/test_sources/resolution/multiple_deps_rename/Move.exp +++ b/language/tools/move-package/tests/test_sources/resolution/multiple_deps_rename/Move.exp @@ -12,6 +12,7 @@ ResolutionGraph { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, root_package: SourceManifest { package: PackageInfo { @@ -23,6 +24,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -40,6 +42,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, "D": Dependency { local: "./deps_only/D", @@ -53,6 +56,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, @@ -94,6 +98,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -126,6 +131,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -158,6 +164,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -175,6 +182,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, "D": Dependency { local: "./deps_only/D", @@ -188,6 +196,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, diff --git a/language/tools/move-package/tests/test_sources/resolution/one_dep/Move.exp b/language/tools/move-package/tests/test_sources/resolution/one_dep/Move.exp index 747f0ec048..0f2d399fea 100644 --- a/language/tools/move-package/tests/test_sources/resolution/one_dep/Move.exp +++ b/language/tools/move-package/tests/test_sources/resolution/one_dep/Move.exp @@ -12,6 +12,7 @@ ResolutionGraph { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, root_package: SourceManifest { package: PackageInfo { @@ -23,6 +24,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -46,6 +48,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, @@ -77,6 +80,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -107,6 +111,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -130,6 +135,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, diff --git a/language/tools/move-package/tests/test_sources/resolution/one_dep_assigned_address/Move.exp b/language/tools/move-package/tests/test_sources/resolution/one_dep_assigned_address/Move.exp index a4ea059b17..38d5123c49 100644 --- a/language/tools/move-package/tests/test_sources/resolution/one_dep_assigned_address/Move.exp +++ b/language/tools/move-package/tests/test_sources/resolution/one_dep_assigned_address/Move.exp @@ -12,6 +12,7 @@ ResolutionGraph { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, root_package: SourceManifest { package: PackageInfo { @@ -23,6 +24,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -40,6 +42,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, @@ -71,6 +74,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -103,6 +107,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: None, dev_address_assignments: None, @@ -120,6 +125,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, diff --git a/language/tools/move-package/tests/test_sources/resolution/one_dep_multiple_of_same_name/Move.exp b/language/tools/move-package/tests/test_sources/resolution/one_dep_multiple_of_same_name/Move.exp index f3c17bf94b..75f7ac92bf 100644 --- a/language/tools/move-package/tests/test_sources/resolution/one_dep_multiple_of_same_name/Move.exp +++ b/language/tools/move-package/tests/test_sources/resolution/one_dep_multiple_of_same_name/Move.exp @@ -12,6 +12,7 @@ ResolutionGraph { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, root_package: SourceManifest { package: PackageInfo { @@ -23,6 +24,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -46,6 +48,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, @@ -77,6 +80,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -107,6 +111,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -130,6 +135,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, diff --git a/language/tools/move-package/tests/test_sources/resolution/one_dep_reassigned_address/Move.exp b/language/tools/move-package/tests/test_sources/resolution/one_dep_reassigned_address/Move.exp index 1fb3159e68..af1f30c513 100644 --- a/language/tools/move-package/tests/test_sources/resolution/one_dep_reassigned_address/Move.exp +++ b/language/tools/move-package/tests/test_sources/resolution/one_dep_reassigned_address/Move.exp @@ -12,6 +12,7 @@ ResolutionGraph { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, root_package: SourceManifest { package: PackageInfo { @@ -23,6 +24,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -46,6 +48,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, @@ -77,6 +80,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -109,6 +113,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -132,6 +137,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, diff --git a/language/tools/move-package/tests/test_sources/resolution/one_dep_unification_across_local_renamings/Move.exp b/language/tools/move-package/tests/test_sources/resolution/one_dep_unification_across_local_renamings/Move.exp index d1270b23de..5d2dc898c0 100644 --- a/language/tools/move-package/tests/test_sources/resolution/one_dep_unification_across_local_renamings/Move.exp +++ b/language/tools/move-package/tests/test_sources/resolution/one_dep_unification_across_local_renamings/Move.exp @@ -12,6 +12,7 @@ ResolutionGraph { additional_named_addresses: {}, architecture: None, fetch_deps_only: false, + skip_fetch_latest_git_deps: false, }, root_package: SourceManifest { package: PackageInfo { @@ -23,6 +24,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -46,6 +48,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, @@ -77,6 +80,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -107,6 +111,7 @@ ResolutionGraph { ), authors: [], license: None, + custom_properties: {}, }, addresses: Some( { @@ -130,6 +135,7 @@ ResolutionGraph { version: None, digest: None, git_info: None, + node_info: None, }, }, dev_dependencies: {}, diff --git a/language/tools/move-package/tests/test_sources/resolution/package_hooks/Move.exp b/language/tools/move-package/tests/test_sources/resolution/package_hooks/Move.exp new file mode 100644 index 0000000000..9ff0f0e380 --- /dev/null +++ b/language/tools/move-package/tests/test_sources/resolution/package_hooks/Move.exp @@ -0,0 +1 @@ +Unable to resolve packages for package 'test': While resolving dependency 'Pkg' in package 'test': TestHooks resolve dep Pkg = localhost:8080 Pkg 0x1 diff --git a/language/tools/move-package/tests/test_sources/resolution/package_hooks/Move.toml b/language/tools/move-package/tests/test_sources/resolution/package_hooks/Move.toml new file mode 100644 index 0000000000..44b7370a10 --- /dev/null +++ b/language/tools/move-package/tests/test_sources/resolution/package_hooks/Move.toml @@ -0,0 +1,6 @@ +[package] +name = "test" +version = "0.0.0" + +[dependencies] +Pkg = { custom = "localhost:8080", address = "0x1", package = "framework" } diff --git a/language/tools/move-resource-viewer/Cargo.toml b/language/tools/move-resource-viewer/Cargo.toml index c4dcb2bb15..d860d9ac7f 100644 --- a/language/tools/move-resource-viewer/Cargo.toml +++ b/language/tools/move-resource-viewer/Cargo.toml @@ -7,15 +7,16 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] -bcs = "0.1.2" move-core-types = { path = "../../move-core/types" } move-binary-format = { path = "../../move-binary-format" } move-bytecode-utils = { path = "../move-bytecode-utils" } serde = { version = "1.0.124", features = ["derive", "rc"] } +bcs.workspace = true + anyhow = "1.0.52" once_cell = "1.7.2" hex = "0.4.3" diff --git a/language/tools/move-unit-test/Cargo.toml b/language/tools/move-unit-test/Cargo.toml index 8b4a964dc1..bf1bed8167 100644 --- a/language/tools/move-unit-test/Cargo.toml +++ b/language/tools/move-unit-test/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] anyhow = "1.0.52" @@ -16,7 +16,7 @@ clap = { version = "3.1.8", features = ["derive"] } codespan-reporting = "0.11.1" colored = "2.0.0" rayon = "1.5.0" -regex = "1.1.9" +regex = "1.5.5" once_cell = "1.7.2" itertools = "0.10.1" diff --git a/language/tools/move-unit-test/src/cargo_runner.rs b/language/tools/move-unit-test/src/cargo_runner.rs index c17428d39d..0307f98f8b 100644 --- a/language/tools/move-unit-test/src/cargo_runner.rs +++ b/language/tools/move-unit-test/src/cargo_runner.rs @@ -26,7 +26,7 @@ pub fn run_tests_with_config_and_filter( let sources = get_files(root_path, source_pattern); let deps = dep_root .map(|root| get_files(root, r".*\.move$")) - .unwrap_or_else(Vec::new); + .unwrap_or_default(); config.source_files = sources; config.dep_files = deps; diff --git a/language/tools/move-unit-test/src/extensions.rs b/language/tools/move-unit-test/src/extensions.rs index 21673ea8bc..afadd25898 100644 --- a/language/tools/move-unit-test/src/extensions.rs +++ b/language/tools/move-unit-test/src/extensions.rs @@ -61,7 +61,7 @@ pub(crate) fn print_change_sets(_w: &mut W, mut extensions: NativeCont #[cfg(feature = "table-extension")] fn create_table_extension(extensions: &mut NativeContextExtensions) { - extensions.add(NativeTableContext::new(0, &*DUMMY_RESOLVER)); + extensions.add(NativeTableContext::new([0u8; 32], &*DUMMY_RESOLVER)); } #[cfg(feature = "table-extension")] diff --git a/language/tools/move-unit-test/src/lib.rs b/language/tools/move-unit-test/src/lib.rs index 1a5453a404..6c7757bcb7 100644 --- a/language/tools/move-unit-test/src/lib.rs +++ b/language/tools/move-unit-test/src/lib.rs @@ -26,17 +26,15 @@ use std::{ sync::Mutex, }; +/// The default value bounding the number of instructions executed in a test. +const DEFAULT_EXECUTION_BOUND: u64 = 100_000; + #[derive(Debug, Parser, Clone)] #[clap(author, version, about)] pub struct UnitTestingConfig { /// Bound the number of instructions that can be executed by any one test. - #[clap( - name = "instructions", - default_value = "5000", - short = 'i', - long = "instructions" - )] - pub instruction_execution_bound: u64, + #[clap(name = "instructions", short = 'i', long = "instructions")] + pub instruction_execution_bound: Option, /// A filter string to determine which unit tests to run #[clap(name = "filter", short = 'f', long = "filter")] @@ -81,6 +79,10 @@ pub struct UnitTestingConfig { )] pub report_stacktrace_on_abort: bool, + /// Ignore compiler's warning, and continue run tests + #[clap(name = "ignore_compile_warnings", long = "ignore_compile_warnings")] + pub ignore_compile_warnings: bool, + /// Named address mapping #[clap( name = "NAMED_ADDRESSES", @@ -108,6 +110,10 @@ pub struct UnitTestingConfig { #[clap(short = 'v', long = "verbose")] pub verbose: bool, + /// Whether the test output need to be printed out. + #[clap(short = 'v', long = "verbose")] + pub report_writeset: bool, + /// Use the EVM-based execution backend. /// Does not work with --stackless. #[cfg(feature = "evm-backend")] @@ -127,18 +133,20 @@ impl UnitTestingConfig { /// Create a unit testing config for use with `register_move_unit_tests` pub fn default_with_bound(bound: Option) -> Self { Self { - instruction_execution_bound: bound.unwrap_or(5000), + instruction_execution_bound: bound.or(Some(DEFAULT_EXECUTION_BOUND)), filter: None, num_threads: 8, report_statistics: false, report_storage_on_error: false, report_stacktrace_on_abort: false, + ignore_compile_warnings: false, source_files: vec![], dep_files: vec![], check_stackless_vm: false, verbose: false, list: false, named_address_values: vec![], + report_writeset: false, #[cfg(feature = "evm-backend")] evm: false, @@ -173,7 +181,13 @@ impl UnitTestingConfig { let compilation_env = compiler.compilation_env(); let test_plan = unit_test::plan_builder::construct_test_plan(compilation_env, None, &cfgir); - if let Err(diags) = compilation_env.check_diags_at_or_above_severity(Severity::Warning) { + if let Err(diags) = + compilation_env.check_diags_at_or_above_severity(if self.ignore_compile_warnings { + Severity::NonblockingError + } else { + Severity::Warning + }) + { diagnostics::report_diagnostics(&files, diags); } @@ -225,7 +239,8 @@ impl UnitTestingConfig { writeln!(shared_writer.lock().unwrap(), "Running Move unit tests")?; let mut test_runner = TestRunner::new( - self.instruction_execution_bound, + self.instruction_execution_bound + .unwrap_or(DEFAULT_EXECUTION_BOUND), self.num_threads, self.check_stackless_vm, self.verbose, @@ -234,6 +249,7 @@ impl UnitTestingConfig { test_plan, native_function_table, verify_and_create_named_address_mapping(self.named_address_values.clone()).unwrap(), + self.report_writeset, #[cfg(feature = "evm-backend")] self.evm, ) @@ -248,9 +264,13 @@ impl UnitTestingConfig { test_results.report_statistics(&shared_writer)?; } - let all_tests_passed = test_results.summarize(&shared_writer)?; + if self.report_writeset { + test_results.report_goldens(&shared_writer)?; + } + + let ok = test_results.summarize(&shared_writer)?; let writer = shared_writer.into_inner().unwrap(); - Ok((writer, all_tests_passed)) + Ok((writer, ok)) } } diff --git a/language/tools/move-unit-test/src/test_reporter.rs b/language/tools/move-unit-test/src/test_reporter.rs index 348222d658..102c4786bd 100644 --- a/language/tools/move-unit-test/src/test_reporter.rs +++ b/language/tools/move-unit-test/src/test_reporter.rs @@ -12,7 +12,7 @@ use move_binary_format::{ use move_command_line_common::files::FileHash; use move_compiler::{ diagnostics::{self, Diagnostic}, - unit_test::{ModuleTestPlan, TestPlan}, + unit_test::{ModuleTestPlan, TestName, TestPlan}, }; use move_core_types::{effects::ChangeSet, language_storage::ModuleId}; use move_ir_types::location::Loc; @@ -70,6 +70,7 @@ pub struct TestRunInfo { pub struct TestStatistics { passed: BTreeMap>, failed: BTreeMap>, + output: BTreeMap>, } #[derive(Debug, Clone)] @@ -369,6 +370,7 @@ impl TestStatistics { Self { passed: BTreeMap::new(), failed: BTreeMap::new(), + output: BTreeMap::new(), } } @@ -386,6 +388,13 @@ impl TestStatistics { .insert(test_info); } + pub fn test_output(&mut self, test_name: TestName, test_plan: &ModuleTestPlan, output: String) { + self.output + .entry(test_plan.module_id.clone()) + .or_insert_with(BTreeMap::new) + .insert(test_name, output); + } + pub fn combine(mut self, other: Self) -> Self { for (module_id, test_result) in other.passed { let entry = self.passed.entry(module_id).or_default(); @@ -395,6 +404,10 @@ impl TestStatistics { let entry = self.failed.entry(module_id).or_default(); entry.extend(test_result.into_iter()); } + for (module_id, test_output) in other.output { + let entry = self.output.entry(module_id).or_default(); + entry.extend(test_output.into_iter()); + } self } } @@ -407,6 +420,21 @@ impl TestResults { } } + pub fn report_goldens(&self, writer: &Mutex) -> Result<()> { + for (module_name, test_outputs) in self.final_statistics.output.iter() { + for (test_name, write_set) in test_outputs.iter() { + writeln!( + writer.lock().unwrap(), + "{}::{}", + format_module_id(module_name), + test_name + )?; + writeln!(writer.lock().unwrap(), "Output: {}", write_set)?; + } + } + Ok(()) + } + pub fn report_statistics(&self, writer: &Mutex) -> Result<()> { writeln!(writer.lock().unwrap(), "\nTest Statistics:\n")?; @@ -528,7 +556,7 @@ impl TestResults { "│ {}", test_failure .render_error(&self.test_plan) - .replace("\n", "\n│ ") + .replace('\n', "\n│ ") )?; writeln!(writer.lock().unwrap(), "└──────────────────\n")?; } diff --git a/language/tools/move-unit-test/src/test_runner.rs b/language/tools/move-unit-test/src/test_runner.rs index dd2e6635f4..a86e4b68fa 100644 --- a/language/tools/move-unit-test/src/test_runner.rs +++ b/language/tools/move-unit-test/src/test_runner.rs @@ -17,8 +17,7 @@ use move_compiler::{ }; use move_core_types::{ account_address::AccountAddress, - effects::ChangeSet, - gas_schedule::{CostTable, GasAlgebra, GasCost, GasUnits}, + effects::{ChangeSet, Op}, identifier::IdentStr, value::serialize_values, vm_status::StatusCode, @@ -34,8 +33,10 @@ use move_stackless_bytecode_interpreter::{ StacklessBytecodeInterpreter, }; use move_vm_runtime::{move_vm::MoveVM, native_functions::NativeFunctionTable}; -use move_vm_test_utils::InMemoryStorage; -use move_vm_types::gas_schedule::{zero_cost_schedule, GasStatus}; +use move_vm_test_utils::{ + gas_schedule::{zero_cost_schedule, CostTable, Gas, GasCost, GasStatus}, + InMemoryStorage, +}; use rayon::prelude::*; use std::{collections::BTreeMap, io::Write, marker::Send, sync::Mutex, time::Instant}; @@ -62,6 +63,7 @@ pub struct SharedTestingConfig { named_address_values: BTreeMap, check_stackless_vm: bool, verbose: bool, + record_writeset: bool, #[cfg(feature = "evm-backend")] evm: bool, @@ -75,14 +77,11 @@ pub struct TestRunner { /// A gas schedule where every instruction has a cost of "1". This is used to bound execution of a /// test to a certain number of ticks. -fn unit_cost_table(num_of_native_funcs: usize) -> CostTable { - let mut cost_schedule = zero_cost_schedule(num_of_native_funcs); +fn unit_cost_table() -> CostTable { + let mut cost_schedule = zero_cost_schedule(); cost_schedule.instruction_table.iter_mut().for_each(|cost| { *cost = GasCost::new(1, 1); }); - cost_schedule.native_table.iter_mut().for_each(|cost| { - *cost = GasCost::new(1, 1); - }); cost_schedule } @@ -118,12 +117,12 @@ fn print_resources_and_extensions( for (account_addr, account_state) in cs.accounts() { writeln!(&mut buf, "0x{}:", account_addr.short_str_lossless())?; - for (tag, resource_opt) in account_state.resources() { - if let Some(resource) = resource_opt { + for (tag, resource_op) in account_state.resources() { + if let Op::New(resource) | Op::Modify(resource) = resource_op { writeln!( &mut buf, "\t{}", - format!("=> {}", annotator.view_resource(tag, resource)?).replace("\n", "\n\t") + format!("=> {}", annotator.view_resource(tag, resource)?).replace('\n', "\n\t") )?; } } @@ -142,8 +141,11 @@ impl TestRunner { save_storage_state_on_failure: bool, report_stacktrace_on_abort: bool, tests: TestPlan, + // TODO: maybe we should require the clients to always pass in a list of native functions so + // we don't have to make assumptions about their gas parameters. native_function_table: Option, named_address_values: BTreeMap, + record_writeset: bool, #[cfg(feature = "evm-backend")] evm: bool, ) -> Result { let source_files = tests @@ -154,9 +156,11 @@ impl TestRunner { let modules = tests.module_info.values().map(|info| &info.module); let starting_storage_state = setup_test_storage(modules)?; let native_function_table = native_function_table.unwrap_or_else(|| { - move_stdlib::natives::all_natives(AccountAddress::from_hex_literal("0x1").unwrap()) + move_stdlib::natives::all_natives( + AccountAddress::from_hex_literal("0x1").unwrap(), + move_stdlib::natives::GasParameters::zeros(), + ) }); - let num_of_native_funcs = native_function_table.len(); Ok(Self { testing_config: SharedTestingConfig { save_storage_state_on_failure, @@ -164,11 +168,17 @@ impl TestRunner { starting_storage_state, execution_bound, native_function_table, - cost_table: unit_cost_table(num_of_native_funcs), + // TODO: our current implementation uses a unit cost table to prevent programs from + // running indefinitely. This should probably be done in a different way, like halting + // after executing a certain number of instructions or setting a timer. + // + // From the API standpoint, we should let the client specify the cost table. + cost_table: unit_cost_table(), source_files, check_stackless_vm, verbose, named_address_values, + record_writeset, #[cfg(feature = "evm-backend")] evm, }, @@ -202,7 +212,11 @@ impl TestRunner { let tests = std::mem::take(&mut module_test.tests); module_test.tests = tests .into_iter() - .filter(|(test_name, _)| test_name.as_str().contains(test_name_slice)) + .filter(|(test_name, _)| { + let full_name = + format!("{}::{}", module_id.name().as_str(), test_name.as_str()); + full_name.contains(test_name_slice) + }) .collect(); } } @@ -266,7 +280,7 @@ impl SharedTestingConfig { let extensions = extensions::new_extensions(); let mut session = move_vm.new_session_with_extensions(&self.starting_storage_state, extensions); - let mut gas_meter = GasStatus::new(&self.cost_table, GasUnits::new(self.execution_bound)); + let mut gas_meter = GasStatus::new(&self.cost_table, Gas::new(self.execution_bound)); // TODO: collect VM logs if the verbose flag (i.e, `self.verbose`) is set let now = Instant::now(); @@ -291,7 +305,12 @@ impl SharedTestingConfig { let test_run_info = TestRunInfo::new( function_name.to_string(), now.elapsed(), - self.execution_bound - gas_meter.remaining_gas().get(), + // TODO(Gas): This doesn't look quite right... + // We're not computing the number of instructions executed even with a unit gas schedule. + Gas::new(self.execution_bound) + .checked_sub(gas_meter.remaining_gas()) + .unwrap() + .into(), ); match session.finish_with_extensions() { Ok((cs, _, extensions)) => (Ok(cs), Ok(extensions), return_result, test_run_info), @@ -390,6 +409,15 @@ impl SharedTestingConfig { for (function_name, test_info) in &test_plan.tests { let (cs_result, ext_result, exec_result, test_run_info) = self.execute_via_move_vm(test_plan, function_name, test_info); + + if self.record_writeset { + stats.test_output( + function_name.to_string(), + test_plan, + format!("{:?}", cs_result), + ); + } + if self.check_stackless_vm { let (stackless_vm_change_set, stackless_vm_result, _, prop_check_result) = self .execute_via_stackless_vm( diff --git a/language/tools/move-unit-test/tests/move_unit_test_testsuite.rs b/language/tools/move-unit-test/tests/move_unit_test_testsuite.rs index 43c68624c9..750be32ebb 100644 --- a/language/tools/move-unit-test/tests/move_unit_test_testsuite.rs +++ b/language/tools/move-unit-test/tests/move_unit_test_testsuite.rs @@ -82,12 +82,13 @@ fn run_test_impl(path: &Path) -> anyhow::Result<()> { let source_files = vec![path.to_str().unwrap().to_owned()]; let unit_test_config = UnitTestingConfig { num_threads: 1, - instruction_execution_bound: 1000, + instruction_execution_bound: Some(1000), source_files, dep_files: move_stdlib::move_stdlib_files(), named_address_values: move_stdlib::move_stdlib_named_addresses() .into_iter() .collect(), + report_writeset: true, ..UnitTestingConfig::default_with_bound(None) }; diff --git a/language/tools/move-unit-test/tests/test_sources/address_args.exp b/language/tools/move-unit-test/tests/test_sources/address_args.exp index f259c8e260..88ad6f1f21 100644 --- a/language/tools/move-unit-test/tests/test_sources/address_args.exp +++ b/language/tools/move-unit-test/tests/test_sources/address_args.exp @@ -2,4 +2,10 @@ Running Move unit tests [ PASS ] 0x1::M::correct_address [ PASS ] 0x1::M::correct_addresses [ PASS ] 0x1::M::wrong_address +0x1::M::correct_address +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::correct_addresses +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::wrong_address +Output: Ok(ChangeSet { accounts: {} }) Test result: OK. Total tests: 3; passed: 3; failed: 0 diff --git a/language/tools/move-unit-test/tests/test_sources/arithmetic_errors.exp b/language/tools/move-unit-test/tests/test_sources/arithmetic_errors.exp index bdcc00dec3..9c7dd55395 100644 --- a/language/tools/move-unit-test/tests/test_sources/arithmetic_errors.exp +++ b/language/tools/move-unit-test/tests/test_sources/arithmetic_errors.exp @@ -3,4 +3,12 @@ Running Move unit tests [ PASS ] 0x1::M::u64_div_by_zero [ PASS ] 0x1::M::u64_mul_overflow [ PASS ] 0x1::M::u64_sub_underflow +0x1::M::u64_add_overflow +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::u64_div_by_zero +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::u64_mul_overflow +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::u64_sub_underflow +Output: Ok(ChangeSet { accounts: {} }) Test result: OK. Total tests: 4; passed: 4; failed: 0 diff --git a/language/tools/move-unit-test/tests/test_sources/construct_data.exp b/language/tools/move-unit-test/tests/test_sources/construct_data.exp index 06604b2134..5f250ba28f 100644 --- a/language/tools/move-unit-test/tests/test_sources/construct_data.exp +++ b/language/tools/move-unit-test/tests/test_sources/construct_data.exp @@ -1,4 +1,8 @@ Running Move unit tests [ PASS ] 0x1::M::make_sure_not_other_number [ PASS ] 0x1::M::make_sure_number_matches +0x1::M::make_sure_not_other_number +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::make_sure_number_matches +Output: Ok(ChangeSet { accounts: {} }) Test result: OK. Total tests: 2; passed: 2; failed: 0 diff --git a/language/tools/move-unit-test/tests/test_sources/cross_module_aborts.exp b/language/tools/move-unit-test/tests/test_sources/cross_module_aborts.exp index 0ee0586aff..36467faf8c 100644 --- a/language/tools/move-unit-test/tests/test_sources/cross_module_aborts.exp +++ b/language/tools/move-unit-test/tests/test_sources/cross_module_aborts.exp @@ -1,6 +1,10 @@ Running Move unit tests [ FAIL ] 0x1::B::failing_test [ PASS ] 0x1::M::dummy_test +0x1::B::failing_test +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::dummy_test +Output: Ok(ChangeSet { accounts: {} }) Test failures: diff --git a/language/tools/move-unit-test/tests/test_sources/do_nothing.exp b/language/tools/move-unit-test/tests/test_sources/do_nothing.exp index 3a1e8fabff..dbeabdb66d 100644 --- a/language/tools/move-unit-test/tests/test_sources/do_nothing.exp +++ b/language/tools/move-unit-test/tests/test_sources/do_nothing.exp @@ -1,3 +1,5 @@ Running Move unit tests [ PASS ] 0x1::M::do_nothing +0x1::M::do_nothing +Output: Ok(ChangeSet { accounts: {} }) Test result: OK. Total tests: 1; passed: 1; failed: 0 diff --git a/language/tools/move-unit-test/tests/test_sources/expected_abort_no_abort.exp b/language/tools/move-unit-test/tests/test_sources/expected_abort_no_abort.exp index 01394de8cc..e29a55de43 100644 --- a/language/tools/move-unit-test/tests/test_sources/expected_abort_no_abort.exp +++ b/language/tools/move-unit-test/tests/test_sources/expected_abort_no_abort.exp @@ -1,6 +1,10 @@ Running Move unit tests [ FAIL ] 0x1::M::fail [ FAIL ] 0x1::M::fail_with_code +0x1::M::fail +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::fail_with_code +Output: Ok(ChangeSet { accounts: {} }) Test failures: diff --git a/language/tools/move-unit-test/tests/test_sources/missing_data.exp b/language/tools/move-unit-test/tests/test_sources/missing_data.exp index 7e56b41765..5ce4a58f6a 100644 --- a/language/tools/move-unit-test/tests/test_sources/missing_data.exp +++ b/language/tools/move-unit-test/tests/test_sources/missing_data.exp @@ -1,5 +1,7 @@ Running Move unit tests [ FAIL ] 0x1::MissingData::missing_data +0x1::MissingData::missing_data +Output: Ok(ChangeSet { accounts: {} }) Test failures: diff --git a/language/tools/move-unit-test/tests/test_sources/native_abort.exp b/language/tools/move-unit-test/tests/test_sources/native_abort.exp index e11aa80a9a..bbfcd48de2 100644 --- a/language/tools/move-unit-test/tests/test_sources/native_abort.exp +++ b/language/tools/move-unit-test/tests/test_sources/native_abort.exp @@ -2,6 +2,12 @@ Running Move unit tests [ PASS ] 0x1::A::native_abort_good_right_code [ FAIL ] 0x1::A::native_abort_good_wrong_code [ FAIL ] 0x1::A::native_abort_unexpected_abort +0x1::A::native_abort_good_right_code +Output: Ok(ChangeSet { accounts: {} }) +0x1::A::native_abort_good_wrong_code +Output: Ok(ChangeSet { accounts: {} }) +0x1::A::native_abort_unexpected_abort +Output: Ok(ChangeSet { accounts: {} }) Test failures: diff --git a/language/tools/move-unit-test/tests/test_sources/native_signer_creation.exp b/language/tools/move-unit-test/tests/test_sources/native_signer_creation.exp index 62d2830dea..67a20c72c8 100644 --- a/language/tools/move-unit-test/tests/test_sources/native_signer_creation.exp +++ b/language/tools/move-unit-test/tests/test_sources/native_signer_creation.exp @@ -2,6 +2,12 @@ Running Move unit tests [ PASS ] 0x1::M::test_determinisim [ FAIL ] 0x1::M::test_doesnt_exist [ PASS ] 0x1::M::test_exists +0x1::M::test_determinisim +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::test_doesnt_exist +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 01000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 02000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 03000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 04000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 05000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 06000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 07000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 08000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 09000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }} }) +0x1::M::test_exists +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 01000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 02000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 03000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 04000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 05000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 06000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 07000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 08000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 09000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }} }) Test failures: diff --git a/language/tools/move-unit-test/tests/test_sources/native_signer_creation.storage.exp b/language/tools/move-unit-test/tests/test_sources/native_signer_creation.storage.exp index 48e5b317a1..14d060d7f1 100644 --- a/language/tools/move-unit-test/tests/test_sources/native_signer_creation.storage.exp +++ b/language/tools/move-unit-test/tests/test_sources/native_signer_creation.storage.exp @@ -2,6 +2,12 @@ Running Move unit tests [ PASS ] 0x1::M::test_determinisim [ FAIL ] 0x1::M::test_doesnt_exist [ PASS ] 0x1::M::test_exists +0x1::M::test_determinisim +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::test_doesnt_exist +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 01000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 02000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 03000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 04000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 05000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 06000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 07000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 08000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 09000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }} }) +0x1::M::test_exists +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 01000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 02000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 03000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 04000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 05000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 06000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 07000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 08000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }, 09000000000000000000000000000000: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("M"), name: Identifier("A"), type_params: [] }: New([0])} }} }) Test failures: diff --git a/language/tools/move-unit-test/tests/test_sources/non_exsistent_native.exp b/language/tools/move-unit-test/tests/test_sources/non_exsistent_native.exp index 06031139e8..2e02ba23e4 100644 --- a/language/tools/move-unit-test/tests/test_sources/non_exsistent_native.exp +++ b/language/tools/move-unit-test/tests/test_sources/non_exsistent_native.exp @@ -1,5 +1,7 @@ Running Move unit tests [ FAIL ] 0x1::M::non_existent_native +0x1::M::non_existent_native +Output: Ok(ChangeSet { accounts: {} }) Test failures: diff --git a/language/tools/move-unit-test/tests/test_sources/proposal_test.exp b/language/tools/move-unit-test/tests/test_sources/proposal_test.exp index 4027a55905..d200fb4699 100644 --- a/language/tools/move-unit-test/tests/test_sources/proposal_test.exp +++ b/language/tools/move-unit-test/tests/test_sources/proposal_test.exp @@ -5,6 +5,18 @@ Running Move unit tests [ PASS ] 0x1::Module::tests_b [ PASS ] 0x1::Module::tests_c [ FAIL ] 0x1::Module::tests_d +0x1::Module::other_module_aborts +Output: Ok(ChangeSet { accounts: {} }) +0x1::Module::tests_a +Output: Ok(ChangeSet { accounts: {} }) +0x1::Module::tests_aborts +Output: Ok(ChangeSet { accounts: {} }) +0x1::Module::tests_b +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("A"), type_params: [] }: New([5, 0, 0, 0, 0, 0, 0, 0])} }, 00000000000000000000000000000002: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("A"), type_params: [] }: New([5, 0, 0, 0, 0, 0, 0, 0])} }} }) +0x1::Module::tests_c +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("A"), type_params: [] }: New([5, 0, 0, 0, 0, 0, 0, 0])} }, 00000000000000000000000000000002: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("A"), type_params: [] }: New([6, 0, 0, 0, 0, 0, 0, 0])} }} }) +0x1::Module::tests_d +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("B"), type_params: [U64] }: New([5, 0, 0, 0, 0, 0, 0, 0])} }, 00000000000000000000000000000002: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("B"), type_params: [Bool] }: New([6, 0, 0, 0, 0, 0, 0, 0]), StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("B"), type_params: [U64] }: New([5, 0, 0, 0, 0, 0, 0, 0]), StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("B"), type_params: [Struct(StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("C"), type_params: [U64] })] }: New([5, 0, 0, 0, 0, 0, 0, 0])} }} }) Test failures: diff --git a/language/tools/move-unit-test/tests/test_sources/proposal_test.storage.exp b/language/tools/move-unit-test/tests/test_sources/proposal_test.storage.exp index 3b5df580d8..ff5689abb5 100644 --- a/language/tools/move-unit-test/tests/test_sources/proposal_test.storage.exp +++ b/language/tools/move-unit-test/tests/test_sources/proposal_test.storage.exp @@ -5,6 +5,18 @@ Running Move unit tests [ PASS ] 0x1::Module::tests_b [ PASS ] 0x1::Module::tests_c [ FAIL ] 0x1::Module::tests_d +0x1::Module::other_module_aborts +Output: Ok(ChangeSet { accounts: {} }) +0x1::Module::tests_a +Output: Ok(ChangeSet { accounts: {} }) +0x1::Module::tests_aborts +Output: Ok(ChangeSet { accounts: {} }) +0x1::Module::tests_b +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("A"), type_params: [] }: New([5, 0, 0, 0, 0, 0, 0, 0])} }, 00000000000000000000000000000002: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("A"), type_params: [] }: New([5, 0, 0, 0, 0, 0, 0, 0])} }} }) +0x1::Module::tests_c +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("A"), type_params: [] }: New([5, 0, 0, 0, 0, 0, 0, 0])} }, 00000000000000000000000000000002: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("A"), type_params: [] }: New([6, 0, 0, 0, 0, 0, 0, 0])} }} }) +0x1::Module::tests_d +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("B"), type_params: [U64] }: New([5, 0, 0, 0, 0, 0, 0, 0])} }, 00000000000000000000000000000002: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("B"), type_params: [Bool] }: New([6, 0, 0, 0, 0, 0, 0, 0]), StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("B"), type_params: [U64] }: New([5, 0, 0, 0, 0, 0, 0, 0]), StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("B"), type_params: [Struct(StructTag { address: 00000000000000000000000000000001, module: Identifier("Module"), name: Identifier("C"), type_params: [U64] })] }: New([5, 0, 0, 0, 0, 0, 0, 0])} }} }) Test failures: diff --git a/language/tools/move-unit-test/tests/test_sources/signer_args.exp b/language/tools/move-unit-test/tests/test_sources/signer_args.exp index 7c57f92e44..ca25c51ca4 100644 --- a/language/tools/move-unit-test/tests/test_sources/signer_args.exp +++ b/language/tools/move-unit-test/tests/test_sources/signer_args.exp @@ -5,6 +5,18 @@ Running Move unit tests [ FAIL ] 0x1::M::single_signer_fail [ PASS ] 0x1::M::single_signer_pass [ PASS ] 0x1::M::test_correct_signer_arg_addrs +0x1::M::multi_signer_fail +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::multi_signer_pass +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::multi_signer_pass_expected_failure +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::single_signer_fail +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::single_signer_pass +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::test_correct_signer_arg_addrs +Output: Ok(ChangeSet { accounts: {} }) Test failures: diff --git a/language/tools/move-unit-test/tests/test_sources/storage_on_error_empty_and_non_empty.exp b/language/tools/move-unit-test/tests/test_sources/storage_on_error_empty_and_non_empty.exp index 6e7d05a3df..f67c12579f 100644 --- a/language/tools/move-unit-test/tests/test_sources/storage_on_error_empty_and_non_empty.exp +++ b/language/tools/move-unit-test/tests/test_sources/storage_on_error_empty_and_non_empty.exp @@ -4,6 +4,16 @@ Running Move unit tests [ PASS ] 0x1::A::c [ FAIL ] 0x1::A::x [ FAIL ] 0x1::A::y +0x1::A::a +Output: Ok(ChangeSet { accounts: {} }) +0x1::A::b +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("A"), name: Identifier("A"), type_params: [] }: New([0])} }} }) +0x1::A::c +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("A"), name: Identifier("A"), type_params: [] }: New([0])} }} }) +0x1::A::x +Output: Ok(ChangeSet { accounts: {} }) +0x1::A::y +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("A"), name: Identifier("A"), type_params: [] }: New([0])} }} }) Test failures: diff --git a/language/tools/move-unit-test/tests/test_sources/storage_on_error_empty_and_non_empty.storage.exp b/language/tools/move-unit-test/tests/test_sources/storage_on_error_empty_and_non_empty.storage.exp index 3f1b3bc5bd..fb552c7d08 100644 --- a/language/tools/move-unit-test/tests/test_sources/storage_on_error_empty_and_non_empty.storage.exp +++ b/language/tools/move-unit-test/tests/test_sources/storage_on_error_empty_and_non_empty.storage.exp @@ -4,6 +4,16 @@ Running Move unit tests [ PASS ] 0x1::A::c [ FAIL ] 0x1::A::x [ FAIL ] 0x1::A::y +0x1::A::a +Output: Ok(ChangeSet { accounts: {} }) +0x1::A::b +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("A"), name: Identifier("A"), type_params: [] }: New([0])} }} }) +0x1::A::c +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("A"), name: Identifier("A"), type_params: [] }: New([0])} }} }) +0x1::A::x +Output: Ok(ChangeSet { accounts: {} }) +0x1::A::y +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("A"), name: Identifier("A"), type_params: [] }: New([0])} }} }) Test failures: diff --git a/language/tools/move-unit-test/tests/test_sources/storage_test.exp b/language/tools/move-unit-test/tests/test_sources/storage_test.exp index b17c243ad4..96f7bb05b2 100644 --- a/language/tools/move-unit-test/tests/test_sources/storage_test.exp +++ b/language/tools/move-unit-test/tests/test_sources/storage_test.exp @@ -1,4 +1,8 @@ Running Move unit tests [ PASS ] 0x1::B::tests_a [ PASS ] 0x1::B::tests_b +0x1::B::tests_a +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("B"), name: Identifier("A"), type_params: [U64] }: New([5, 0, 0, 0, 0, 0, 0, 0])} }, 00000000000000000000000000000002: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("B"), name: Identifier("A"), type_params: [Bool] }: New([0])} }} }) +0x1::B::tests_b +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("B"), name: Identifier("A"), type_params: [U64] }: New([5, 0, 0, 0, 0, 0, 0, 0])} }, 00000000000000000000000000000002: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("B"), name: Identifier("A"), type_params: [U64] }: New([6, 0, 0, 0, 0, 0, 0, 0])} }} }) Test result: OK. Total tests: 2; passed: 2; failed: 0 diff --git a/language/tools/move-unit-test/tests/test_sources/storage_test.storage.exp b/language/tools/move-unit-test/tests/test_sources/storage_test.storage.exp index b17c243ad4..96f7bb05b2 100644 --- a/language/tools/move-unit-test/tests/test_sources/storage_test.storage.exp +++ b/language/tools/move-unit-test/tests/test_sources/storage_test.storage.exp @@ -1,4 +1,8 @@ Running Move unit tests [ PASS ] 0x1::B::tests_a [ PASS ] 0x1::B::tests_b +0x1::B::tests_a +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("B"), name: Identifier("A"), type_params: [U64] }: New([5, 0, 0, 0, 0, 0, 0, 0])} }, 00000000000000000000000000000002: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("B"), name: Identifier("A"), type_params: [Bool] }: New([0])} }} }) +0x1::B::tests_b +Output: Ok(ChangeSet { accounts: {00000000000000000000000000000001: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("B"), name: Identifier("A"), type_params: [U64] }: New([5, 0, 0, 0, 0, 0, 0, 0])} }, 00000000000000000000000000000002: AccountChangeSet { modules: {}, resources: {StructTag { address: 00000000000000000000000000000001, module: Identifier("B"), name: Identifier("A"), type_params: [U64] }: New([6, 0, 0, 0, 0, 0, 0, 0])} }} }) Test result: OK. Total tests: 2; passed: 2; failed: 0 diff --git a/language/tools/move-unit-test/tests/test_sources/timeout.exp b/language/tools/move-unit-test/tests/test_sources/timeout.exp index 6a5fd52a2e..b43022ad87 100644 --- a/language/tools/move-unit-test/tests/test_sources/timeout.exp +++ b/language/tools/move-unit-test/tests/test_sources/timeout.exp @@ -4,6 +4,16 @@ Running Move unit tests [ PASS ] 0x1::M::no_timeout_while_loop [ TIMEOUT ] 0x1::M::timeout_fail [ TIMEOUT ] 0x1::M::timeout_fail_with_expected_failure +0x1::M::no_timeout +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::no_timeout_fail +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::no_timeout_while_loop +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::timeout_fail +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::timeout_fail_with_expected_failure +Output: Ok(ChangeSet { accounts: {} }) Test failures: diff --git a/language/tools/move-unit-test/tests/test_sources/unexpected_abort.exp b/language/tools/move-unit-test/tests/test_sources/unexpected_abort.exp index 8082e4139f..c253692746 100644 --- a/language/tools/move-unit-test/tests/test_sources/unexpected_abort.exp +++ b/language/tools/move-unit-test/tests/test_sources/unexpected_abort.exp @@ -4,6 +4,16 @@ Running Move unit tests [ FAIL ] 0x1::M::unexpected_abort [ FAIL ] 0x1::M::unexpected_abort_in_other_function [ FAIL ] 0x1::M::wrong_abort_code +0x1::M::correct_abort_code +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::just_test_failure +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::unexpected_abort +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::unexpected_abort_in_other_function +Output: Ok(ChangeSet { accounts: {} }) +0x1::M::wrong_abort_code +Output: Ok(ChangeSet { accounts: {} }) Test failures: diff --git a/language/tools/move-unit-test/tests/test_sources/use_unit_test_module.exp b/language/tools/move-unit-test/tests/test_sources/use_unit_test_module.exp index fd927d6fda..f148ff28d1 100644 --- a/language/tools/move-unit-test/tests/test_sources/use_unit_test_module.exp +++ b/language/tools/move-unit-test/tests/test_sources/use_unit_test_module.exp @@ -1,3 +1,5 @@ Running Move unit tests [ PASS ] 0x1::M::poison_call +0x1::M::poison_call +Output: Ok(ChangeSet { accounts: {} }) Test result: OK. Total tests: 1; passed: 1; failed: 0 diff --git a/language/tools/read-write-set/Cargo.toml b/language/tools/read-write-set/Cargo.toml index fda28ac073..790f684e0a 100644 --- a/language/tools/read-write-set/Cargo.toml +++ b/language/tools/read-write-set/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] anyhow = "1.0.52" diff --git a/language/tools/read-write-set/dynamic/Cargo.toml b/language/tools/read-write-set/dynamic/Cargo.toml index 1ff84db9dd..206ca47818 100644 --- a/language/tools/read-write-set/dynamic/Cargo.toml +++ b/language/tools/read-write-set/dynamic/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = false -edition = "2018" +edition = "2021" [dependencies] anyhow = "1.0.52" diff --git a/language/tools/read-write-set/src/lib.rs b/language/tools/read-write-set/src/lib.rs index c33608c778..f0bc4c30a9 100644 --- a/language/tools/read-write-set/src/lib.rs +++ b/language/tools/read-write-set/src/lib.rs @@ -59,14 +59,11 @@ impl ReadWriteSetAnalysis { /// Return an overapproximation access paths read/written by `module`::`fun`. /// Returns `None` if the function or module does not exist. pub fn get_summary(&self, module: &ModuleId, fun: &IdentStr) -> Option<&ReadWriteSetState> { - self.get_function_env(module, fun) - .map(|fenv| { - self.targets - .get_data(&fenv.get_qualified_id(), &FunctionVariant::Baseline) - .map(|data| data.annotations.get::()) - .flatten() - }) - .flatten() + self.get_function_env(module, fun).and_then(|fenv| { + self.targets + .get_data(&fenv.get_qualified_id(), &FunctionVariant::Baseline) + .and_then(|data| data.annotations.get::()) + }) } /// Returns the FunctionEnv for `module`::`fun` @@ -95,8 +92,7 @@ impl ReadWriteSetAnalysis { func.get_identifier(), self.targets .get_data(&func.get_qualified_id(), &FunctionVariant::Baseline) - .map(|data| data.annotations.get::()) - .flatten() + .and_then(|data| data.annotations.get::()) .unwrap() .normalize(&self.env), ); diff --git a/language/tools/read-write-set/types/Cargo.toml b/language/tools/read-write-set/types/Cargo.toml index 4ed24a5322..fd2e99385d 100644 --- a/language/tools/read-write-set/types/Cargo.toml +++ b/language/tools/read-write-set/types/Cargo.toml @@ -7,7 +7,7 @@ repository = "https://github.com/diem/diem" homepage = "https://diem.com" license = "Apache-2.0" publish = ["crates-io"] -edition = "2018" +edition = "2021" [dependencies] anyhow = "1.0.52" diff --git a/rust-toolchain b/rust-toolchain index 4d5fde5bd1..9405730420 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -1.60.0 +1.64.0 diff --git a/rustfmt.toml b/rustfmt.toml index 80eeb4054d..55797efb6b 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,2 +1,2 @@ -edition = "2018" +edition = "2021" use_field_init_shorthand = true diff --git a/scripts/dev_setup.sh b/scripts/dev_setup.sh index b4c225e5ff..495547b557 100755 --- a/scripts/dev_setup.sh +++ b/scripts/dev_setup.sh @@ -15,10 +15,10 @@ # fast fail. set -eo pipefail -Z3_VERSION=4.8.13 +Z3_VERSION=4.11.0 CVC5_VERSION=0.0.3 -DOTNET_VERSION=5.0 -BOOGIE_VERSION=2.9.6 +DOTNET_VERSION=6.0 +BOOGIE_VERSION=2.15.8 SOLC_VERSION="v0.8.11+commit.d7f03943" SCRIPT_PATH="$(cd "$(dirname "$0")" >/dev/null 2>&1 && pwd)" @@ -279,6 +279,12 @@ function install_boogie { echo "Boogie $BOOGIE_VERSION already installed" else "${DOTNET_INSTALL_DIR}dotnet" tool update --tool-path "${DOTNET_INSTALL_DIR}tools/" Boogie --version $BOOGIE_VERSION + # If a higher version of boogie is installed, we can not install required version with `dotnet tool update` command above + # Print a tip here, since incompatible version of boogie might cause move-prover stuck forever + if [[ $? != 0 ]]; then + echo "failed to install boogie ${BOOGIE_VERSION}, if there is a more updated boogie installed, please consider uninstall it with" + echo "${DOTNET_INSTALL_DIR}dotnet tool uninstall --tool-path ${DOTNET_INSTALL_DIR}/tools Boogie" + fi fi } @@ -294,7 +300,7 @@ function install_z3 { return fi if [[ "$(uname)" == "Linux" ]]; then - Z3_PKG="z3-$Z3_VERSION-x64-glibc-2.28" + Z3_PKG="z3-$Z3_VERSION-x64-glibc-2.31" elif [[ "$(uname)" == "Darwin" ]]; then Z3_PKG="z3-$Z3_VERSION-x64-osx-10.16" else @@ -306,7 +312,7 @@ function install_z3 { mkdir -p "$TMPFILE"/ ( cd "$TMPFILE" || exit - curl -LOs "https://github.com/junkil-park/z3/releases/download/z3-$Z3_VERSION/$Z3_PKG.zip" + curl -LOs "https://github.com/Z3Prover/z3/releases/download/z3-$Z3_VERSION/$Z3_PKG.zip" unzip -q "$Z3_PKG.zip" cp "$Z3_PKG/bin/z3" "${INSTALL_DIR}" chmod +x "${INSTALL_DIR}z3" diff --git a/x.toml b/x.toml index 00744f3ff9..009c2e7ea8 100644 --- a/x.toml +++ b/x.toml @@ -30,7 +30,7 @@ features = [ "s3" ] allowed = [ # Deriving Arbitrary often causes this warning to show up. "clippy::unit_arg", - "clippy::eval-order-dependence", + "clippy::mixed_read_write_in_expression", "clippy::new-without-default", "clippy::rc_buffer", "clippy::upper_case_acronyms", @@ -41,13 +41,6 @@ allowed = [ "clippy::needless-collect", "clippy::enum-variant-names", "clippy::self-named-constructors", - # TODO: those below should probably be fixed and removed - "clippy::derivable-impls", - "clippy::single-char-pattern", - "clippy::map-flatten", - "clippy::unwrap_or_else_default", - "clippy::needless_borrow", - "clippy::needless_late_init", ] warn = [ "clippy::wildcard_dependencies",