Skip to content

Commit

Permalink
[experimental] add smoke tests testing experimental framework
Browse files Browse the repository at this point in the history
  • Loading branch information
emmazzz authored and bors-libra committed Dec 11, 2021
1 parent 734fb26 commit 39450ea
Show file tree
Hide file tree
Showing 23 changed files with 504 additions and 11 deletions.
18 changes: 18 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 @@ -54,6 +54,7 @@ members = [
"diem-move/diem-framework",
"diem-move/diem-framework/DPN/releases",
"diem-move/diem-framework/experimental/demo-cli",
"diem-move/diem-framework/experimental/releases",
"diem-move/diem-framework/releases",
"diem-move/diem-keygen",
"diem-move/diem-resource-viewer",
Expand Down
2 changes: 1 addition & 1 deletion diem-move/diem-framework/experimental/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ version = "1.5.0"
[addresses]
Std = "0x1"
DiemFramework = "0x1"
BARS = "0x34CD440B72D03907100007D7BC0080A1"
BARS = "0x06505CCD81E562B524D8F656ABD92A15"
DiemRoot = "0xA550C18"
TreasuryCompliance = "0xB1E55ED"
CurrencyInfo = "0xA550C18"
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<�A}_/,��@f��#;P�d�3Ϸ���fh -�
3���T�,�{
L�?%<pS�+�9�w��%�Y��
11 changes: 6 additions & 5 deletions diem-move/diem-framework/experimental/demo-cli/demo_commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
# local setup
export ROOT_KEY="local_root.key"
export TC_KEY="local_root.key"
export BARS_ADDRESS="0x06505CCD81E562B524D8F656ABD92A15"
export JSON_RPC_ENDPOINT="http://0.0.0.0:8080"
export REST_API_ENDPOINT="localhost:8080"
# run a local node
cargo run --bin diem-node -- --lazy --test --open-publishing --genesis-modules diem-move/diem-framework/experimental/releases/artifacts/current --seed 0000000000000000000000000000000000000000000000000000000000000000

cargo run -- --jsonrpc-endpoint $JSON_RPC_ENDPOINT --account-key-path $TC_KEY --account-address 0xB1E55ED create-basic-account 0x34CD440B72D03907100007D7BC0080A1 44f0d42a415212a4ab59749efb6ac9a9
cargo run -- --jsonrpc-endpoint $JSON_RPC_ENDPOINT --account-key-path bars_account.key --account-address 0x34CD440B72D03907100007D7BC0080A1 create-basic-account 0x3132E2B5216A46DFCF8154079954C129 45ecaf56addd68aa8be08e2763c09857
cargo run -- --jsonrpc-endpoint $JSON_RPC_ENDPOINT --account-key-path bars_account.key --account-address 0x34CD440B72D03907100007D7BC0080A1 create-basic-account 0x1A08E8165BB9225702495E8CB6E57E61 5839ada2d75b6c43f194123337a11c5e
cargo run -- --jsonrpc-endpoint $JSON_RPC_ENDPOINT --account-key-path $TC_KEY --account-address 0xB1E55ED create-basic-account $BARS_ADDRESS 8bf3d7c0b381385c06bdfe37e1230cc8
cargo run -- --jsonrpc-endpoint $JSON_RPC_ENDPOINT --account-key-path bars_account.key --account-address $BARS_ADDRESS create-basic-account 0x3132E2B5216A46DFCF8154079954C129 45ecaf56addd68aa8be08e2763c09857
cargo run -- --jsonrpc-endpoint $JSON_RPC_ENDPOINT --account-key-path bars_account.key --account-address $BARS_ADDRESS create-basic-account 0x1A08E8165BB9225702495E8CB6E57E61 5839ada2d75b6c43f194123337a11c5e

# get all the resource types of the newly created account
curl ${REST_API_ENDPOINT}/accounts/0x34CD440B72D03907100007D7BC0080A1/resources | jq '.[] | .type'
curl ${REST_API_ENDPOINT}/accounts/${BARS_ADDRESS}/resources | jq '.[] | .type'
# look at the event
curl "${REST_API_ENDPOINT}/accounts/0xB1E55ED/transactions?start=0&limit=1" | jq

Expand All @@ -32,7 +33,7 @@ curl ${REST_API_ENDPOINT}/accounts/0x3132E2B5216A46DFCF8154079954C129/resources

# register another user
cargo run -- --jsonrpc-endpoint $JSON_RPC_ENDPOINT --account-key-path user_account2.key --account-address 0x1A08E8165BB9225702495E8CB6E57E61 register-bars-user
cargo run -- --jsonrpc-endpoint $JSON_RPC_ENDPOINT --account-key-path bars_account.key --account-address 0x34CD440B72D03907100007D7BC0080A1 mint-bars-nft --creator-addr 0x3132E2B5216A46DFCF8154079954C129 --creator-name "Some Name" --content-uri "www.diem.com" --amount 100
cargo run -- --jsonrpc-endpoint $JSON_RPC_ENDPOINT --account-key-path bars_account.key --account-address $BARS_ADDRESS mint-bars-nft --creator-addr 0x3132E2B5216A46DFCF8154079954C129 --creator-name "Some Name" --content-uri "www.diem.com" --amount 100
# Token data collection
curl ${REST_API_ENDPOINT}/accounts/0x3132E2B5216A46DFCF8154079954C129/resources | jq '.[] | select(.type | contains("TokenDataCollection"))'
# Token gallery
Expand Down
24 changes: 24 additions & 0 deletions diem-move/diem-framework/experimental/releases/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "diem-experimental-framework-releases"
version = "0.1.0"
edition = "2018"
authors = ["Diem Association <[email protected]>"]
description = "Diem Experimental Framework Releases"
repository = "https://github.com/diem/diem"
homepage = "https://diem.com"
license = "Apache-2.0"
publish = false


[dependencies]
move-command-line-common = { path = "../../../../language/move-command-line-common" }
diem-crypto = { path = "../../../../crates/diem-crypto" }
diem-types = { path = "../../../../types" }
diem-workspace-hack = { path = "../../../../crates/diem-workspace-hack" }
move-binary-format = { path = "../../../../language/move-binary-format" }
framework-releases = { path = "../../releases" }

anyhow = "1.0.38"
include_dir = "0.6.0"
bcs = "0.1.2"
once_cell = "1.7.2"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
compiled_package_info:
package_name: DiemExperimental
address_alias_instantiation:
BARS: 34cd440b72d03907100007d7bc0080a1
BARS: 06505ccd81e562b524d8f656abd92a15
CurrencyInfo: 0000000000000000000000000a550c18
DiemFramework: "00000000000000000000000000000001"
DiemRoot: 0000000000000000000000000a550c18
Expand Down Expand Up @@ -175,7 +175,7 @@ compiled_package_info:
? address: "00000000000000000000000000000001"
name: XUS
: DiemFramework
source_digest: C9F77B892DA4815BBFCB31E16010CD8BC02815C65F6EF38384B860C01DA98271
source_digest: C5415C7DFBC3B6BC4AB994BFE0B6B8C580089AD7D664589FF329434BA328B3AD
build_flags:
dev_mode: false
test_mode: false
Expand Down
Binary file not shown.
30 changes: 30 additions & 0 deletions diem-move/diem-framework/experimental/releases/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use framework_releases::{Release, ReleaseFetcher};
use move_binary_format::file_format::CompiledModule;
use once_cell::sync::Lazy;

/// Load the serialized modules from the specified release.
pub fn load_modules_from_release(release_name: &str) -> Result<Vec<Vec<u8>>> {
ReleaseFetcher::new(Release::Experimental, release_name).module_blobs()
}

static CURRENT_MODULE_BLOBS: Lazy<Vec<Vec<u8>>> =
Lazy::new(|| load_modules_from_release("current").unwrap());

static CURRENT_MODULES: Lazy<Vec<CompiledModule>> = Lazy::new(|| {
CURRENT_MODULE_BLOBS
.iter()
.map(|blob| CompiledModule::deserialize(blob).unwrap())
.collect()
});

pub fn current_modules() -> &'static [CompiledModule] {
&CURRENT_MODULES
}

pub fn current_module_blobs() -> &'static [Vec<u8>] {
&CURRENT_MODULE_BLOBS
}
2 changes: 1 addition & 1 deletion sdk/src/transaction_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
use serde::{Deserialize, Serialize};
use std::fmt;

pub use diem_transaction_builder::stdlib;
pub use diem_transaction_builder::{experimental_stdlib, stdlib};
use diem_types::transaction::{ChangeSet, ModuleBundle, Script, ScriptFunction, WriteSetPayload};

pub struct TransactionBuilder {
Expand Down
1 change: 1 addition & 0 deletions testsuite/forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ diem-logger = { path = "../../crates/diem-logger" }
diem-retrier = { path = "../../crates/diem-retrier" }
diem-sdk = { path = "../../sdk" }
diem-secure-storage = { path = "../../secure/storage" }
diem-transaction-builder = { path = "../../sdk/transaction-builder" }
diem-workspace-hack = { version = "0.1", path = "../../crates/diem-workspace-hack" }
transaction-emitter = { path = "../../crates/transaction-emitter" }
11 changes: 10 additions & 1 deletion testsuite/forge/src/interface/chain_info.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::{Coffer, PublicInfo, Result};
use crate::{Coffer, NFTPublicInfo, PublicInfo, Result};
use diem_sdk::{
client::BlockingClient,
transaction_builder::{Currency, TransactionFactory},
Expand Down Expand Up @@ -142,4 +142,13 @@ impl<'t> ChainInfo<'t> {
self.rest_api_url.clone(),
)
}

pub fn into_nft_public_info(self) -> NFTPublicInfo<'t> {
NFTPublicInfo::new(
self.json_rpc_url.clone(),
self.chain_id,
self.rest_api_url.clone(),
self.root_account,
)
}
}
2 changes: 2 additions & 0 deletions testsuite/forge/src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ mod node;
pub use node::*;
mod chain_info;
pub use chain_info::*;
mod nft;
pub use nft::*;

/// A wrapper around a usize in order to represent an opaque version of a Node.
///
Expand Down
170 changes: 170 additions & 0 deletions testsuite/forge/src/interface/nft.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use super::Test;
use crate::{CoreContext, Result, TestReport};
use diem_sdk::{
client::BlockingClient,
crypto::{ed25519::Ed25519PrivateKey, PrivateKey, Uniform},
move_types::account_address::AccountAddress,
transaction_builder::TransactionFactory,
types::{
account_config::xus_tag, chain_id::ChainId, transaction::authenticator::AuthenticationKey,
LocalAccount,
},
};
use diem_transaction_builder::experimental_stdlib;
use rand::{rngs::StdRng, SeedableRng};

/// The testing interface which defines a test written from the perspective of the a public user of
/// the NFT network in a "testnet" like environment where there exists a source for minting NFTs
/// and a means of creating new accounts.
pub trait NFTPublicUsageTest: Test {
/// Executes the test against the given context.
fn run<'t>(&self, ctx: &mut NFTPublicUsageContext<'t>) -> Result<()>;
}

pub struct NFTPublicUsageContext<'t> {
core: CoreContext,
public_info: NFTPublicInfo<'t>,
pub report: &'t mut TestReport,
}

impl<'t> NFTPublicUsageContext<'t> {
pub fn new(
core: CoreContext,
public_info: NFTPublicInfo<'t>,
report: &'t mut TestReport,
) -> Self {
Self {
core,
public_info,
report,
}
}

pub fn client(&self) -> BlockingClient {
BlockingClient::new(&self.public_info.json_rpc_url)
}

pub fn url(&self) -> &str {
&self.public_info.json_rpc_url
}

pub fn rest_api_url(&self) -> &str {
&self.public_info.rest_api_url
}

pub fn core(&self) -> &CoreContext {
&self.core
}

pub fn rng(&mut self) -> &mut ::rand::rngs::StdRng {
self.core.rng()
}

pub fn random_account(&mut self) -> LocalAccount {
LocalAccount::generate(self.core.rng())
}

pub fn chain_id(&self) -> ChainId {
self.public_info.chain_id
}

pub fn transaction_factory(&self) -> TransactionFactory {
TransactionFactory::new(self.chain_id())
}

pub fn mint_bars(&mut self, address: AccountAddress, amount: u64) -> Result<()> {
let mint_nft_txn = self.public_info.bars_account.sign_with_transaction_builder(
self.transaction_factory().payload(
experimental_stdlib::encode_mint_bars_script_function(
address,
"test_artist".as_bytes().to_vec(),
"test_url".as_bytes().to_vec(),
amount,
),
),
);
self.public_info.json_rpc_client.submit(&mint_nft_txn)?;
self.public_info
.json_rpc_client
.wait_for_signed_transaction(&mint_nft_txn, None, None)?;
Ok(())
}

pub fn create_user_account(&mut self, auth_key: AuthenticationKey) -> Result<()> {
let create_account_txn = self.public_info.bars_account.sign_with_transaction_builder(
self.transaction_factory().payload(
experimental_stdlib::encode_create_account_script_function(
xus_tag(),
auth_key.derived_address(),
auth_key.prefix().to_vec(),
),
),
);
self.public_info
.json_rpc_client
.submit(&create_account_txn)?;
self.public_info
.json_rpc_client
.wait_for_signed_transaction(&create_account_txn, None, None)?;
Ok(())
}
}

pub struct NFTPublicInfo<'t> {
json_rpc_url: String,
chain_id: ChainId,
rest_api_url: String,
json_rpc_client: BlockingClient,
root_account: &'t mut LocalAccount,
bars_account: LocalAccount,
}

impl<'t> NFTPublicInfo<'t> {
pub fn new(
json_rpc_url: String,
chain_id: ChainId,
rest_api_url: String,
root_account: &'t mut LocalAccount,
) -> Self {
let bars_key = Ed25519PrivateKey::generate(&mut StdRng::from_seed([1; 32]));
let bars_address = AuthenticationKey::ed25519(&bars_key.public_key()).derived_address();
Self {
json_rpc_url: json_rpc_url.clone(),
chain_id,
rest_api_url,
json_rpc_client: BlockingClient::new(json_rpc_url),
root_account,
bars_account: LocalAccount::new(bars_address, bars_key, 0),
}
}

pub fn init_nft_environment(&mut self) -> Result<()> {
// send a transaction to initialize nft
let init_nft_txn = self.root_account.sign_with_transaction_builder(
TransactionFactory::new(self.chain_id)
.payload(experimental_stdlib::encode_nft_initialize_script_function()),
);
self.json_rpc_client.submit(&init_nft_txn)?;
self.json_rpc_client
.wait_for_signed_transaction(&init_nft_txn, None, None)?;

// create an account for bars
let bars_account_creation_txn = self.root_account.sign_with_transaction_builder(
TransactionFactory::new(self.chain_id).payload(
experimental_stdlib::encode_create_account_script_function(
xus_tag(),
self.bars_account.address(),
self.bars_account.authentication_key().prefix().to_vec(),
),
),
);
self.json_rpc_client.submit(&bars_account_creation_txn)?;
self.json_rpc_client
.wait_for_signed_transaction(&bars_account_creation_txn, None, None)?;

Ok(())
}
}
Loading

0 comments on commit 39450ea

Please sign in to comment.