Skip to content

Commit

Permalink
[move-examples] a small framework for testing and building move
Browse files Browse the repository at this point in the history
To expedite onboarding into smart contracts, this is a small set that
allows us to have our own demo modules, test them, and build them as a
precursor to either the user adopting move-cli, borrowing our code for
their own CI/CD, or exploring something altogether differently.
  • Loading branch information
davidiw authored and aptos-bot committed Mar 12, 2022
1 parent 595cc9f commit 640a670
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 19 additions & 0 deletions aptos-move/move-examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "move-examples"
version = "0.1.0"
authors = ["Aptos Foundation <[email protected]>"]
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" }
9 changes: 9 additions & 0 deletions aptos-move/move-examples/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "Examples"
version = "0.0.0"

[addresses]
HelloBlockchain = "0xe110"

[dependencies]
AptosFramework = { local = "../framework/aptos-framework" }
55 changes: 55 additions & 0 deletions aptos-move/move-examples/sources/HelloBlockchain.move
Original file line number Diff line number Diff line change
@@ -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<MessageChangeEvent>,
}

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<MessageHolder>(addr), Errors::not_published(ENO_MESSAGE));
*&borrow_global<MessageHolder>(addr).message
}

public(script) fun set_message(account: signer, message_bytes: vector<u8>)
acquires MessageHolder {
let message = ASCII::string(message_bytes);
let account_addr = Signer::address_of(&account);
if (!exists<MessageHolder>(account_addr)) {
move_to(&account, MessageHolder {
message,
message_change_events: Event::new_event_handle<MessageChangeEvent>(&account),
})
} else {
let old_message_holder = borrow_global_mut<MessageHolder>(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
);
}
}
25 changes: 25 additions & 0 deletions aptos-move/move-examples/sources/HelloBlockchainTest.move
Original file line number Diff line number Diff line change
@@ -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
);
}
}
32 changes: 32 additions & 0 deletions aptos-move/move-examples/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<std::path::PathBuf>,
}

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();
}
27 changes: 27 additions & 0 deletions aptos-move/move-examples/tests/move_unit_tests.rs
Original file line number Diff line number Diff line change
@@ -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()),
);
}
1 change: 1 addition & 0 deletions x.toml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ members = [
"language-e2e-tests",
"language-e2e-testsuite",
"memsocket",
"move-examples",
"offchain",
"smoke-test",
"transaction-emitter",
Expand Down

0 comments on commit 640a670

Please sign in to comment.