Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib & cmd]Stdlib Version #1603

Merged
merged 13 commits into from
Nov 10, 2020
Merged
52 changes: 38 additions & 14 deletions cmd/starcoin/src/dev/sign_txn_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,25 @@ use starcoin_vm_types::account_config::association_address;
use starcoin_vm_types::transaction::{SignedUserTransaction, TransactionPayload};
use starcoin_vm_types::{genesis_config::ChainNetwork, on_chain_config::DaoConfig};

pub fn sign_txn_with_association_account_by_rpc_client(
pub fn sign_txn_with_account_by_rpc_client(
cli_state: &CliState,
addr: AccountAddress,
max_gas_amount: u64,
gas_price: u64,
expiration_time: u64,
payload: TransactionPayload,
) -> Result<SignedUserTransaction> {
sign_txn_by_rpc_client(
cli_state,
max_gas_amount,
gas_price,
expiration_time,
payload,
Some(addr),
)
}

pub fn _sign_txn_with_association_account_by_rpc_client(
tiangong3624749 marked this conversation as resolved.
Show resolved Hide resolved
cli_state: &CliState,
max_gas_amount: u64,
gas_price: u64,
Expand All @@ -28,7 +46,7 @@ pub fn sign_txn_with_association_account_by_rpc_client(
)
}

pub fn sign_txn_with_default_account_by_rpc_client(
pub fn _sign_txn_with_default_account_by_rpc_client(
cli_state: &CliState,
max_gas_amount: u64,
gas_price: u64,
Expand Down Expand Up @@ -103,7 +121,9 @@ mod tests {
};
use starcoin_types::transaction::{parse_transaction_argument, Script, TransactionArgument};
use starcoin_vm_types::{
account_config::AccountResource, genesis_config::StdlibVersion, transaction::Package,
account_config::{association_address, genesis_address, AccountResource},
genesis_config::StdlibVersion,
transaction::Package,
vm_status::KeptVMStatus,
};
use starcoin_vm_types::{language_storage::TypeTag, parser::parse_type_tag};
Expand Down Expand Up @@ -158,16 +178,17 @@ mod tests {
}
"#;
let test_upgrade_module =
compile_module_with_address(association_address(), test_upgrade_module_source);
compile_module_with_address(genesis_address(), test_upgrade_module_source);
let test_upgrade_module_package = Package::new_with_module(test_upgrade_module).unwrap();

let dao_config = config.net().genesis_config().dao_config;
let (module_upgrade_proposal, _) = build_module_upgrade_proposal(
&test_upgrade_module_package,
2,
dao_config.min_action_delay,
);

let proposal_txn = sign_txn_with_association_account_by_rpc_client(
let proposal_txn = _sign_txn_with_association_account_by_rpc_client(
&cli_state,
1_000_000,
1,
Expand All @@ -188,14 +209,14 @@ mod tests {
.chain_get_transaction_info(proposal_txn_id)
.unwrap()
.unwrap();
info!("txn status : {:?}", proposal_txn_info);
assert_eq!(proposal_txn_info.status(), &KeptVMStatus::Executed);

// 2. transfer
cli_state
.client()
.sleep(dao_config.voting_period / 2 * 1000)
.unwrap();
// cli_state.client().sleep(3_600_1000).unwrap();
let default_account = cli_state.default_account().unwrap();
// unlock default account
cli_state
Expand Down Expand Up @@ -245,6 +266,7 @@ mod tests {
assert_eq!(transfer_txn_info.status(), &KeptVMStatus::Executed);

// 3. vote
let proposal_id = 0;
let vote_code =
compiled_transaction_script(StdlibVersion::Latest, StdlibScript::CastVote).into_vec();
let mut type_tags: Vec<TypeTag> = Vec::new();
Expand All @@ -254,7 +276,7 @@ mod tests {
type_tags.push(module);
let mut args: Vec<TransactionArgument> = Vec::new();
let arg_1 = parse_transaction_argument("0x0000000000000000000000000a550c18").unwrap();
let arg_2 = parse_transaction_argument("0").unwrap();
let arg_2 = parse_transaction_argument(&format!("{}", proposal_id)).unwrap();
let arg_3 = parse_transaction_argument("true").unwrap();
let arg_4 =
parse_transaction_argument(&format!("{}u128", transfer_amount * 90 / 100)).unwrap();
Expand Down Expand Up @@ -294,10 +316,10 @@ mod tests {
node_handle.generate_block().unwrap();

// 5. queue
let proposal_id = 0;
let module_upgrade_queue = build_module_upgrade_queue(proposal_id);
let queue_txn = sign_txn_with_default_account_by_rpc_client(
let module_upgrade_queue = build_module_upgrade_queue(association_address(), proposal_id);
let queue_txn = sign_txn_with_account_by_rpc_client(
&cli_state,
default_account.address,
1_000_000,
1,
3_000,
Expand All @@ -316,6 +338,7 @@ mod tests {
.chain_get_transaction_info(queue_txn_id)
.unwrap()
.unwrap();
info!("queue txn info : {:?}", queue_txn_info);
assert_eq!(queue_txn_info.status(), &KeptVMStatus::Executed);

// 6. sleep
Expand All @@ -326,9 +349,10 @@ mod tests {
node_handle.generate_block().unwrap();

// 7. plan
let module_upgrade_plan = build_module_upgrade_plan(proposal_id);
let plan_txn = sign_txn_with_default_account_by_rpc_client(
let module_upgrade_plan = build_module_upgrade_plan(association_address(), proposal_id);
let plan_txn = sign_txn_with_account_by_rpc_client(
&cli_state,
default_account.address,
1_000_000,
1,
3_000,
Expand All @@ -350,7 +374,7 @@ mod tests {
assert_eq!(plan_txn_info.status(), &KeptVMStatus::Executed);

// 8. exe package
let package_txn = sign_txn_with_association_account_by_rpc_client(
let package_txn = _sign_txn_with_association_account_by_rpc_client(
&cli_state,
1_000_000,
1,
Expand All @@ -374,7 +398,7 @@ mod tests {

// 9. verify
let call = ContractCall {
module_address: association_address(),
module_address: genesis_address(),
module_name: "TestModule".to_string(),
func: "is_test".to_string(),
type_args: Vec::new(),
Expand Down
15 changes: 13 additions & 2 deletions cmd/starcoin/src/dev/upgrade_module_exe_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// SPDX-License-Identifier: Apache-2.0

use crate::cli_state::CliState;
use crate::dev::sign_txn_helper::sign_txn_with_association_account_by_rpc_client;
use crate::dev::sign_txn_helper::sign_txn_with_account_by_rpc_client;
use crate::StarcoinOpt;
use anyhow::{bail, Result};
use scmd::{CommandAction, ExecContext};
use starcoin_crypto::hash::{HashValue, PlainCryptoHash};
use starcoin_vm_types::account_address::AccountAddress;
use starcoin_vm_types::transaction::TransactionPayload;
use std::fs::File;
use std::io::Read;
Expand All @@ -16,6 +17,10 @@ use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "module_exe")]
pub struct UpgradeModuleExeOpt {
#[structopt(short = "s", long)]
/// hex encoded string, like 0x1, 0x12
sender: Option<AccountAddress>,

#[structopt(
short = "g",
name = "max-gas-amount",
Expand Down Expand Up @@ -71,13 +76,19 @@ impl CommandAction for UpgradeModuleExeCommand {
) -> Result<Self::ReturnItem> {
let opt = ctx.opt();
let cli_state = ctx.state();
let sender = if let Some(sender) = ctx.opt().sender {
sender
} else {
ctx.state().default_account()?.address
};
if let Some(module_file) = &opt.module_file {
let mut bytes = vec![];
File::open(module_file)?.read_to_end(&mut bytes)?;
let upgrade_package = scs::from_bytes(&bytes)?;

let signed_txn = sign_txn_with_association_account_by_rpc_client(
let signed_txn = sign_txn_with_account_by_rpc_client(
cli_state,
sender,
opt.max_gas_amount,
opt.gas_price,
opt.expiration_time,
Expand Down
26 changes: 23 additions & 3 deletions cmd/starcoin/src/dev/upgrade_module_plan_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@
// SPDX-License-Identifier: Apache-2.0

use crate::cli_state::CliState;
use crate::dev::sign_txn_helper::sign_txn_with_default_account_by_rpc_client;
use crate::dev::sign_txn_helper::sign_txn_with_account_by_rpc_client;
use crate::StarcoinOpt;
use anyhow::{bail, Result};
use scmd::{CommandAction, ExecContext};
use starcoin_crypto::hash::{HashValue, PlainCryptoHash};
use starcoin_transaction_builder::build_module_upgrade_plan;
use starcoin_vm_types::account_address::AccountAddress;
use starcoin_vm_types::transaction::TransactionPayload;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[structopt(name = "module_plan")]
pub struct UpgradeModulePlanOpt {
#[structopt(short = "s", long)]
/// hex encoded string, like 0x1, 0x12
sender: Option<AccountAddress>,

#[structopt(
short = "g",
name = "max-gas-amount",
Expand Down Expand Up @@ -45,6 +50,10 @@ pub struct UpgradeModulePlanOpt {
)]
blocking: bool,

#[structopt(short = "a", name = "proposer-address", long = "proposer_address")]
/// hex encoded string, like 0x1, 0x12
proposer_address: Option<AccountAddress>,

#[structopt(
short = "m",
name = "module-proposal-id",
Expand All @@ -68,9 +77,20 @@ impl CommandAction for UpgradeModulePlanCommand {
) -> Result<Self::ReturnItem> {
let opt = ctx.opt();
let cli_state = ctx.state();
let module_upgrade_plan = build_module_upgrade_plan(opt.proposal_id);
let signed_txn = sign_txn_with_default_account_by_rpc_client(
let sender = if let Some(sender) = ctx.opt().sender {
sender
} else {
ctx.state().default_account()?.address
};
let proposer_address = if let Some(address) = ctx.opt().proposer_address {
address
} else {
sender
};
let module_upgrade_plan = build_module_upgrade_plan(proposer_address, opt.proposal_id);
let signed_txn = sign_txn_with_account_by_rpc_client(
cli_state,
sender,
opt.max_gas_amount,
opt.gas_price,
opt.expiration_time,
Expand Down
37 changes: 31 additions & 6 deletions cmd/starcoin/src/dev/upgrade_module_proposal_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// SPDX-License-Identifier: Apache-2.0

use crate::cli_state::CliState;
use crate::dev::sign_txn_helper::{
get_dao_config, sign_txn_with_association_account_by_rpc_client,
};
use crate::dev::sign_txn_helper::{get_dao_config, sign_txn_with_account_by_rpc_client};
use crate::StarcoinOpt;
use anyhow::{bail, Result};
use scmd::{CommandAction, ExecContext};
use starcoin_crypto::hash::{HashValue, PlainCryptoHash};
use starcoin_logger::prelude::*;
use starcoin_transaction_builder::build_module_upgrade_proposal;
use starcoin_types::transaction::Package;
use starcoin_vm_types::account_address::AccountAddress;
use starcoin_vm_types::transaction::TransactionPayload;
use std::fs::File;
use std::io::Read;
Expand All @@ -19,6 +20,10 @@ use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "module_proposal")]
pub struct UpgradeModuleProposalOpt {
#[structopt(short = "s", long)]
/// hex encoded string, like 0x1, 0x12
sender: Option<AccountAddress>,

#[structopt(
short = "g",
name = "max-gas-amount",
Expand All @@ -42,6 +47,7 @@ pub struct UpgradeModuleProposalOpt {
help = "how long(in seconds) the txn stay alive"
)]
expiration_time: u64,

#[structopt(
short = "b",
name = "blocking-mode",
Expand All @@ -58,6 +64,15 @@ pub struct UpgradeModuleProposalOpt {
parse(from_os_str)
)]
module_file: Option<PathBuf>,

#[structopt(
short = "v",
name = "module-version",
long = "module_version",
default_value = "1",
help = "module version"
)]
version: u64,
}

pub struct UpgradeModuleProposalCommand;
Expand All @@ -74,16 +89,26 @@ impl CommandAction for UpgradeModuleProposalCommand {
) -> Result<Self::ReturnItem> {
let opt = ctx.opt();
let cli_state = ctx.state();
let sender = if let Some(sender) = ctx.opt().sender {
sender
} else {
ctx.state().default_account()?.address
};
if let Some(module_file) = &opt.module_file {
let mut bytes = vec![];
File::open(module_file)?.read_to_end(&mut bytes)?;
let upgrade_package = scs::from_bytes(&bytes)?;
let upgrade_package: Package = scs::from_bytes(&bytes)?;
info!(
"upgrade package address : {:?}",
upgrade_package.package_address()
);

let min_action_delay = get_dao_config(cli_state)?.min_action_delay;
let (module_upgrade_proposal, package_hash) =
build_module_upgrade_proposal(&upgrade_package, min_action_delay);
let signed_txn = sign_txn_with_association_account_by_rpc_client(
build_module_upgrade_proposal(&upgrade_package, opt.version, min_action_delay);
let signed_txn = sign_txn_with_account_by_rpc_client(
cli_state,
sender,
opt.max_gas_amount,
opt.gas_price,
opt.expiration_time,
Expand Down
Loading