diff --git a/Cargo.lock b/Cargo.lock index 673da833b82d5..246eeff4ca8bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4536,6 +4536,20 @@ dependencies = [ "workspace-hack", ] +[[package]] +name = "move-examples" +version = "0.1.0" +dependencies = [ + "aptos-vm", + "aptos-workspace-hack", + "framework", + "move-compiler", + "move-package", + "move-stdlib", + "move-unit-test", + "structopt", +] + [[package]] name = "move-ir-compiler" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index f593bcc7cd758..2c358afa697f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ members = [ "aptos-move/framework/experimental/releases", "aptos-move/framework/releases", "aptos-move/genesis-viewer", + "aptos-move/move-examples", "aptos-move/mvhashmap", "aptos-move/parallel-executor", "aptos-move/transaction-builder-generator", diff --git a/aptos-move/move-examples/Cargo.toml b/aptos-move/move-examples/Cargo.toml new file mode 100644 index 0000000000000..c1f738d765692 --- /dev/null +++ b/aptos-move/move-examples/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "move-examples" +version = "0.1.0" +authors = ["Aptos Foundation "] +repository = "https://github.com/aptos-core/aptos-labs" +homepage = "https://aptoslabs.com" +license = "Apache-2.0" +publish = false +edition = "2018" + +[dependencies] +aptos-vm = { path = "../aptos-vm" } +aptos-workspace-hack = { path = "../../crates/aptos-workspace-hack" } +framework = { path = "../framework" } +structopt = "0.3.21" +move-compiler = { git = "https://github.com/diem/move", rev = "98ed299a7e3a9223019c9bdf4dd92fea9faef860" } +move-package = { git = "https://github.com/diem/move", rev = "98ed299a7e3a9223019c9bdf4dd92fea9faef860" } +move-stdlib = { git = "https://github.com/diem/move", rev = "98ed299a7e3a9223019c9bdf4dd92fea9faef860" } +move-unit-test = { git = "https://github.com/diem/move", rev = "98ed299a7e3a9223019c9bdf4dd92fea9faef860" } diff --git a/aptos-move/move-examples/Move.toml b/aptos-move/move-examples/Move.toml new file mode 100644 index 0000000000000..0d2d0ae4b99e3 --- /dev/null +++ b/aptos-move/move-examples/Move.toml @@ -0,0 +1,9 @@ +[package] +name = "Examples" +version = "0.0.0" + +[addresses] +HelloBlockchain = "0xe110" + +[dependencies] +AptosFramework = { local = "../framework/aptos-framework" } diff --git a/aptos-move/move-examples/sources/HelloBlockchain.move b/aptos-move/move-examples/sources/HelloBlockchain.move new file mode 100644 index 0000000000000..a1e6bfc492e6a --- /dev/null +++ b/aptos-move/move-examples/sources/HelloBlockchain.move @@ -0,0 +1,55 @@ +module HelloBlockchain::Message { + use Std::ASCII; + use Std::Errors; + use Std::Event; + use Std::Signer; + + struct MessageHolder has key { + message: ASCII::String, + message_change_events: Event::EventHandle, + } + + struct MessageChangeEvent has drop, store { + from_message: ASCII::String, + to_message: ASCII::String, + } + + /// There is no message present + const ENO_MESSAGE: u64 = 0; + + public fun get_message(addr: address): ASCII::String acquires MessageHolder { + assert!(exists(addr), Errors::not_published(ENO_MESSAGE)); + *&borrow_global(addr).message + } + + public(script) fun set_message(account: signer, message_bytes: vector) + acquires MessageHolder { + let message = ASCII::string(message_bytes); + let account_addr = Signer::address_of(&account); + if (!exists(account_addr)) { + move_to(&account, MessageHolder { + message, + message_change_events: Event::new_event_handle(&account), + }) + } else { + let old_message_holder = borrow_global_mut(account_addr); + let from_message = *&old_message_holder.message; + Event::emit_event(&mut old_message_holder.message_change_events, MessageChangeEvent { + from_message, + to_message: copy message, + }); + old_message_holder.message = message; + } + } + + #[test(account = @0x1)] + public(script) fun sender_can_set_message(account: signer) acquires MessageHolder { + let addr = Signer::address_of(&account); + set_message(account, b"Hello, Blockchain"); + + assert!( + get_message(addr) == ASCII::string(b"Hello, Blockchain"), + ENO_MESSAGE + ); + } +} diff --git a/aptos-move/move-examples/sources/HelloBlockchainTest.move b/aptos-move/move-examples/sources/HelloBlockchainTest.move new file mode 100644 index 0000000000000..152d571d43400 --- /dev/null +++ b/aptos-move/move-examples/sources/HelloBlockchainTest.move @@ -0,0 +1,25 @@ +#[test_only] +module HelloBlockchain::MessageTests { + use Std::Signer; + use Std::UnitTest; + use Std::Vector; + use Std::ASCII; + + use HelloBlockchain::Message; + + fun get_account(): signer { + Vector::pop_back(&mut UnitTest::create_signers_for_testing(1)) + } + + #[test] + public(script) fun sender_can_set_message() { + let account = get_account(); + let addr = Signer::address_of(&account); + Message::set_message(account, b"Hello, Blockchain"); + + assert!( + Message::get_message(addr) == ASCII::string(b"Hello, Blockchain"), + 0 + ); + } +} diff --git a/aptos-move/move-examples/src/main.rs b/aptos-move/move-examples/src/main.rs new file mode 100644 index 0000000000000..1a76dd281facb --- /dev/null +++ b/aptos-move/move-examples/src/main.rs @@ -0,0 +1,32 @@ +// Copyright (c) The Aptos Foundation +// SPDX-License-Identifier: Apache-2.0 + +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +#[structopt(about = "Lightweight Move package builder")] +struct Args { + input_path: std::path::PathBuf, + #[structopt( + long, + short = "o", + about = "Optional output path, defaults to input_path/out" + )] + output_path: Option, +} + +fn main() { + let args = Args::from_args(); + + let build_config = move_package::BuildConfig { + dev_mode: false, + generate_abis: false, + generate_docs: true, + install_dir: args.output_path, + ..Default::default() + }; + + build_config + .compile_package(&args.input_path, &mut std::io::stdout()) + .unwrap(); +} diff --git a/aptos-move/move-examples/tests/move_unit_tests.rs b/aptos-move/move-examples/tests/move_unit_tests.rs new file mode 100644 index 0000000000000..0c291bafb2579 --- /dev/null +++ b/aptos-move/move-examples/tests/move_unit_tests.rs @@ -0,0 +1,27 @@ +// Copyright (c) The Aptos Foundation +// SPDX-License-Identifier: Apache-2.0 + +use aptos_vm::natives::aptos_natives; +use framework::diem_framework_named_addresses; +use move_compiler::shared::NumericalAddress; +use move_unit_test::UnitTestingConfig; + +#[test] +fn move_unit_tests() { + let mut named_addresses = diem_framework_named_addresses(); + named_addresses.insert( + "HelloBlockchain".to_owned(), + NumericalAddress::parse_str("0xe110").unwrap(), + ); + + let config = + UnitTestingConfig::default_with_bound(Some(100_000)).with_named_addresses(named_addresses); + + move_unit_test::cargo_runner::run_tests_with_config_and_filter( + config, + ".", + r"sources/.*\.move$", + Some(&move_stdlib::move_stdlib_modules_full_path()), + Some(aptos_natives()), + ); +} diff --git a/x.toml b/x.toml index 0cba01f5fc6ca..20f904f2608ac 100644 --- a/x.toml +++ b/x.toml @@ -184,6 +184,7 @@ members = [ "language-e2e-tests", "language-e2e-testsuite", "memsocket", + "move-examples", "offchain", "smoke-test", "transaction-emitter",