Skip to content

Commit

Permalink
Merge fcff033 into 194cade
Browse files Browse the repository at this point in the history
  • Loading branch information
nanne007 authored Mar 22, 2021
2 parents 194cade + fcff033 commit 063dcb9
Show file tree
Hide file tree
Showing 26 changed files with 278 additions and 273 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@ use crate::StarcoinOpt;
use anyhow::{format_err, Result};
use scmd::{CommandAction, ExecContext};
use starcoin_crypto::hash::HashValue;
use starcoin_rpc_api::types::FunctionIdView;
use starcoin_rpc_client::RemoteStateReader;
use starcoin_state_api::AccountStateReader;
use starcoin_transaction_builder::compiled_transaction_script;
use starcoin_transaction_builder::StdlibScript;
use starcoin_types::transaction::{
parse_transaction_argument, RawUserTransaction, Script, TransactionArgument,
parse_transaction_argument, RawUserTransaction, TransactionArgument,
};
use starcoin_vm_types::account_address::AccountAddress;
use starcoin_vm_types::genesis_config::StdlibVersion;
use starcoin_vm_types::transaction::ScriptFunction;
use starcoin_vm_types::{language_storage::TypeTag, parser::parse_type_tag};
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[structopt(name = "execute-builtin")]
pub struct ExecuteBuiltInScriptOpt {
#[structopt(name = "execute-function")]
pub struct ExecuteScriptFunctionOpt {
#[structopt(short = "s")]
/// if `sender` is absent, use default account.
sender: Option<AccountAddress>,

#[structopt(long = "script", name = "script-name")]
/// builtin script name to execute
script_name: StdlibScript,
#[structopt(long = "function", name = "script-function")]
/// script function to execute, example: 0x1::TransferScripts::peer_to_peer
script_function: FunctionIdView,

#[structopt(
short = "t",
Expand Down Expand Up @@ -75,12 +74,12 @@ pub struct ExecuteBuiltInScriptOpt {
blocking: bool,
}

pub struct ExecuteBuildInCommand;
pub struct ExecuteScriptFunctionCmd;

impl CommandAction for ExecuteBuildInCommand {
impl CommandAction for ExecuteScriptFunctionCmd {
type State = CliState;
type GlobalOpt = StarcoinOpt;
type Opt = ExecuteBuiltInScriptOpt;
type Opt = ExecuteScriptFunctionOpt;
type ReturnItem = HashValue;

fn run(
Expand All @@ -100,16 +99,18 @@ impl CommandAction for ExecuteBuildInCommand {
})?;
let expiration_time = opt.expiration_time + node_info.now_seconds;

let bytecode =
compiled_transaction_script(StdlibVersion::Latest, opt.script_name).into_vec();

let type_tags = opt.type_tags.clone().unwrap_or_default();
let args = opt.args.clone().unwrap_or_default();

let script_txn = RawUserTransaction::new_script(
let script_function = opt.script_function.clone().0;
let script_txn = RawUserTransaction::new_script_function(
sender.address,
account_resource.sequence_number(),
Script::new(bytecode, type_tags, args),
ScriptFunction::new(
script_function.module,
script_function.function,
type_tags,
args,
),
opt.max_gas_amount,
opt.gas_price,
expiration_time,
Expand Down
4 changes: 2 additions & 2 deletions cmd/starcoin/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod accept_token_cmd;
mod change_password_cmd;
mod create_cmd;
mod default_cmd;
mod execute_builtin_script_cmd;
mod execute_script_function_cmd;
mod export_cmd;
mod import_cmd;
mod list_cmd;
Expand All @@ -19,7 +19,7 @@ pub use accept_token_cmd::*;
pub use change_password_cmd::*;
pub use create_cmd::*;
pub use default_cmd::*;
pub use execute_builtin_script_cmd::*;
pub use execute_script_function_cmd::*;
pub use export_cmd::*;
pub use import_cmd::*;
pub use list_cmd::*;
Expand Down
22 changes: 6 additions & 16 deletions cmd/starcoin/src/dev/call_contract_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,24 @@ use crate::StarcoinOpt;
use anyhow::Result;
use scmd::{CommandAction, ExecContext};
use starcoin_rpc_api::types::{
AnnotatedMoveValueView, ContractCall, TransactionArgumentView, TypeTagView,
AnnotatedMoveValueView, ContractCall, FunctionIdView, TransactionArgumentView, TypeTagView,
};
use starcoin_vm_types::account_address::AccountAddress;
use structopt::StructOpt;

/// Call Contract command
/// Some examples:
/// ``` shell
/// # 0x1::Block::get_current_block_number()
/// dev call --module-address 0x1 --module-name Block --func-name get_current_block_number
/// dev call --function 0x1::Block::current_block_number
/// # 0x1::Account::balance<0x1::STC::STC>(0x726098b70ba8aa2cc172af19af8804)
/// dev call --func-name balance --module-address 0x1 --module-name Account -t 0x1::STC::STC --arg 0x726098b70ba8aa2cc172af19af8804
/// dev call --function 0x1::Account::balance -t 0x1::STC::STC --arg 0x726098b70ba8aa2cc172af19af8804
/// ```
#[derive(Debug, StructOpt)]
#[structopt(name = "call")]
pub struct CallContractOpt {
#[structopt(
long = "module-address",
name = "module address",
help = "hex encoded string, like 0x0, 0x1"
)]
module_address: AccountAddress,
#[structopt(long)]
module_name: String,
#[structopt(long)]
func_name: String,
/// function to execute, example: 0x1::Block::current_block_number
function: FunctionIdView,
#[structopt(
short = "t",
long = "type_tag",
Expand Down Expand Up @@ -63,9 +55,7 @@ impl CommandAction for CallContractCommand {
let opt = ctx.opt();

let call = ContractCall {
module_address: opt.module_address,
module_name: opt.module_name.clone(),
func: opt.func_name.clone(),
function_id: opt.function.clone(),
type_args: opt.type_tags.clone().unwrap_or_default(),
args: opt.args.clone().unwrap_or_default(),
};
Expand Down
116 changes: 63 additions & 53 deletions cmd/starcoin/src/dev/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
use crate::cli_state::CliState;
use crate::view::{ExecuteResultView, ExecutionOutputView};
use crate::StarcoinOpt;
use anyhow::{bail, format_err, Result};
use anyhow::{bail, Result};
use scmd::{CommandAction, ExecContext};
use starcoin_config::temp_path;
use starcoin_dev::playground;
use starcoin_move_compiler::{
compile_source_string_no_report, errors, load_bytecode_file, CompiledUnit, MOVE_EXTENSION,
};
use starcoin_rpc_api::types::{DryRunTransactionRequest, StrView, TransactionVMStatus};
use starcoin_rpc_api::types::{
DryRunTransactionRequest, FunctionIdView, StrView, TransactionVMStatus,
};
use starcoin_rpc_client::RemoteStateReader;
use starcoin_state_api::AccountStateReader;
use starcoin_transaction_builder::{compiled_transaction_script, StdlibScript};
use starcoin_types::transaction::{
parse_transaction_argument, DryRunTransaction, Module, RawUserTransaction, Script,
TransactionArgument,
parse_transaction_argument, DryRunTransaction, Module, Package, RawUserTransaction, Script,
ScriptFunction, TransactionArgument, TransactionPayload,
};
use starcoin_vm_types::account_address::AccountAddress;
use starcoin_vm_types::genesis_config::StdlibVersion;
use starcoin_vm_types::{language_storage::TypeTag, parser::parse_type_tag};
use std::path::PathBuf;
use stdlib::restore_stdlib_in_dir;
Expand Down Expand Up @@ -84,14 +84,14 @@ pub struct ExecuteOpt {
/// Whether dry-run in local cli or remote node.
local_mode: bool,

#[structopt(long = "script", name = "builtin-script")]
/// builtin script name to execute
script_name: Option<StdlibScript>,
#[structopt(long = "function", name = "script-function")]
/// script function to execute, example: 0x1::TransferScripts::peer_to_peer
script_function: Option<FunctionIdView>,

#[structopt(
name = "move_file",
parse(from_os_str),
required_unless = "builtin-script"
required_unless = "script-function"
)]
/// bytecode file or move script source file
move_file: Option<PathBuf>,
Expand All @@ -114,22 +114,17 @@ impl CommandAction for ExecuteCommand {
ctx: &ExecContext<Self::State, Self::GlobalOpt, Self::Opt>,
) -> Result<Self::ReturnItem> {
let opt = ctx.opt();
let client = ctx.state().client();
let sender = if let Some(sender) = ctx.opt().sender {
sender
} else {
ctx.state().default_account()?.address
};
let type_tags = opt.type_tags.clone().unwrap_or_default();
let args = opt.args.clone().unwrap_or_default();

let (bytecode, is_script) = if let Some(builtin_script) = opt.script_name.as_ref() {
let code =
compiled_transaction_script(StdlibVersion::Latest, *builtin_script).into_vec();
(code, true)
} else {
let move_file_path = ctx
.opt()
.move_file
.clone()
.ok_or_else(|| format_err!("expect a move file path"))?;
let script_function_id = opt.script_function.clone().map(|id| id.0);
let bytedata = if let Some(move_file_path) = ctx.opt().move_file.as_ref() {
let ext = move_file_path
.as_path()
.extension()
Expand Down Expand Up @@ -162,54 +157,69 @@ impl CommandAction for ExecuteCommand {
CompiledUnit::Module { .. } => false,
CompiledUnit::Script { .. } => true,
};
(compile_unit.serialize(), is_script)
Some((compile_unit.serialize(), is_script))
} else {
load_bytecode_file(move_file_path.as_path())?
Some(load_bytecode_file(move_file_path.as_path())?)
}
} else {
None
};
let txn_payload = match (bytedata, script_function_id) {
// package deploy
(Some((bytecode, false)), function_id) => {
let module_init_script_function = function_id
.map(|id| ScriptFunction::new(id.module, id.function, type_tags, args));
let package =
Package::new(vec![Module::new(bytecode)], module_init_script_function)?;
TransactionPayload::Package(package)
}
// script
(Some((bytecode, true)), None) => {
let script = Script::new(bytecode, type_tags, args);
TransactionPayload::Script(script)
}
(Some((_bytecode, true)), Some(_)) => {
bail!("should only provide script function or script file, not both");
}
// script function
(None, Some(function_id)) => {
let script_function =
ScriptFunction::new(function_id.module, function_id.function, type_tags, args);
TransactionPayload::ScriptFunction(script_function)
}
(None, None) => {
bail!("this should not happen, bug here!");
}
};

let type_tags = opt.type_tags.clone();
let args = opt.args.clone();

let client = ctx.state().client();
let node_info = client.node_info()?;
let chain_state_reader = RemoteStateReader::new(client)?;
let account_state_reader = AccountStateReader::new(&chain_state_reader);
let account_resource = account_state_reader.get_account_resource(&sender)?;
let raw_txn = {
let account_resource = {
let chain_state_reader = RemoteStateReader::new(client)?;
let account_state_reader = AccountStateReader::new(&chain_state_reader);
account_state_reader.get_account_resource(&sender)?
};

if account_resource.is_none() {
bail!("address {} not exists on chain", &sender);
}
let account_resource = account_resource.unwrap();
if account_resource.is_none() {
bail!("address {} not exists on chain", &sender);
}
let account_resource = account_resource.unwrap();

let expiration_time = opt.expiration_time + node_info.now_seconds;
let script_txn = if is_script {
RawUserTransaction::new_script(
sender,
account_resource.sequence_number(),
Script::new(
bytecode,
type_tags.unwrap_or_default(),
args.unwrap_or_default(),
),
opt.max_gas_amount,
opt.gas_price,
expiration_time,
ctx.state().net().chain_id(),
)
} else {
RawUserTransaction::new_module(
let expiration_time = {
let node_info = client.node_info()?;
opt.expiration_time + node_info.now_seconds
};
RawUserTransaction::new_with_default_gas_token(
sender,
account_resource.sequence_number(),
Module::new(bytecode),
txn_payload,
opt.max_gas_amount,
opt.gas_price,
expiration_time,
ctx.state().net().chain_id(),
)
};

let signed_txn = client.account_sign_txn(script_txn)?;
let signed_txn = client.account_sign_txn(raw_txn)?;
let txn_hash = signed_txn.id();
let output = if opt.local_mode {
let state_view = RemoteStateReader::new(client)?;
Expand Down
9 changes: 5 additions & 4 deletions cmd/starcoin/src/dev/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use anyhow::{format_err, Result};
use starcoin_config::NodeConfig;
use starcoin_logger::prelude::*;
use starcoin_node::NodeHandle;
use starcoin_rpc_api::types::{AnnotatedMoveValueView, ContractCall, TransactionVMStatus};
use starcoin_rpc_api::types::{
AnnotatedMoveValueView, ContractCall, FunctionIdView, TransactionVMStatus,
};
use starcoin_rpc_client::{RemoteStateReader, RpcClient};
use starcoin_state_api::AccountStateReader;
use starcoin_transaction_builder::{
Expand All @@ -25,6 +27,7 @@ use starcoin_vm_types::{
transaction::Package,
};
use starcoin_vm_types::{language_storage::TypeTag, parser::parse_type_tag};
use std::str::FromStr;
use std::sync::Arc;
use std::{thread::sleep, time::Duration};
use test_helper::executor::compile_module_with_address;
Expand Down Expand Up @@ -350,9 +353,7 @@ fn test_upgrade_module() {

// 9. verify
let call = ContractCall {
module_address: genesis_address(),
module_name: "TestModule".to_string(),
func: "is_test".to_string(),
function_id: FunctionIdView::from_str("0x1::TestModule::is_test").unwrap(),
type_args: Vec::new(),
args: Vec::new(),
};
Expand Down
2 changes: 1 addition & 1 deletion cmd/starcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn add_command(
.subcommand(account::UnlockCommand)
.subcommand(account::ExportCommand)
.subcommand(account::ImportCommand)
.subcommand(account::ExecuteBuildInCommand)
.subcommand(account::ExecuteScriptFunctionCmd)
.subcommand(account::LockCommand)
.subcommand(account::ChangePasswordCmd)
.subcommand(account::DefaultCommand),
Expand Down
2 changes: 1 addition & 1 deletion config/example/halley/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ default_global_api_quota = "2000/s"
apis = "chain,dev,miner,node,state,txpool,contract"

[rpc.ipc]
apis = "account,chain,debug,dev,miner,network_manager,node_manager,node,pubsub,state,sync_manager,txpool,contract"
apis = "account,chain,debug,miner,network_manager,node_manager,node,pubsub,state,sync_manager,txpool,contract"

[rpc.tcp]
apis = "chain,dev,miner,node,state,txpool,contract"
Expand Down
2 changes: 1 addition & 1 deletion config/example/proxima/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ default_global_api_quota = "2000/s"
apis = "chain,dev,miner,node,state,txpool,contract"

[rpc.ipc]
apis = "account,chain,debug,dev,miner,network_manager,node_manager,node,pubsub,state,sync_manager,txpool,contract"
apis = "account,chain,debug,miner,network_manager,node_manager,node,pubsub,state,sync_manager,txpool,contract"

[rpc.tcp]
apis = "chain,dev,miner,node,state,txpool,contract"
Expand Down
Loading

0 comments on commit 063dcb9

Please sign in to comment.