Skip to content

Commit

Permalink
Merge 73976c2 into c1e5cee
Browse files Browse the repository at this point in the history
  • Loading branch information
guangyuz authored Apr 29, 2021
2 parents c1e5cee + 73976c2 commit 566fcd3
Show file tree
Hide file tree
Showing 76 changed files with 513 additions and 14 deletions.
4 changes: 4 additions & 0 deletions cmd/starcoin/src/dev/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ mod tests;
mod upgrade_module_exe_cmd;
mod upgrade_module_plan_cmd;
mod upgrade_module_proposal_cmd;
mod upgrade_module_proposal_v2_cmd;
mod upgrade_module_queue_cmd;
mod upgrade_module_queue_v2_cmd;

pub use call_contract_cmd::*;
pub use compile_cmd::*;
Expand All @@ -31,4 +33,6 @@ pub use subscribe_cmd::*;
pub use upgrade_module_exe_cmd::*;
pub use upgrade_module_plan_cmd::*;
pub use upgrade_module_proposal_cmd::*;
pub use upgrade_module_proposal_v2_cmd::*;
pub use upgrade_module_queue_cmd::*;
pub use upgrade_module_queue_v2_cmd::*;
12 changes: 8 additions & 4 deletions cmd/starcoin/src/dev/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use starcoin_rpc_api::types::{
use starcoin_rpc_client::{RemoteStateReader, RpcClient};
use starcoin_state_api::AccountStateReader;
use starcoin_transaction_builder::{
build_module_upgrade_plan, build_module_upgrade_proposal, build_module_upgrade_queue,
build_module_upgrade_plan, build_module_upgrade_proposal_v2, build_module_upgrade_queue_v2,
};
use starcoin_types::transaction::{
parse_transaction_argument, ScriptFunction, TransactionArgument,
Expand Down Expand Up @@ -175,8 +175,12 @@ fn test_upgrade_module() {
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 (module_upgrade_proposal, _) = build_module_upgrade_proposal_v2(
&test_upgrade_module_package,
2,
dao_config.min_action_delay,
false,
);

let proposal_txn = _sign_txn_with_association_account_by_rpc_client(
&cli_state,
Expand Down Expand Up @@ -270,7 +274,7 @@ fn test_upgrade_module() {
node_handle.generate_block().unwrap();

// 5. queue
let module_upgrade_queue = build_module_upgrade_queue(association_address(), proposal_id);
let module_upgrade_queue = build_module_upgrade_queue_v2(association_address(), proposal_id);
let queue_txn = sign_txn_with_account_by_rpc_client(
&cli_state,
default_account.address,
Expand Down
142 changes: 142 additions & 0 deletions cmd/starcoin/src/dev/upgrade_module_proposal_v2_cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

use crate::cli_state::CliState;
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_v2;
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;
use std::path::PathBuf;
use structopt::StructOpt;

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

#[structopt(
short = "g",
name = "max-gas-amount",
default_value = "10000000",
help = "max gas used to deploy the module"
)]
max_gas_amount: u64,
#[structopt(
short = "p",
long = "gas-price",
name = "price of gas",
default_value = "1",
help = "gas price used to deploy the module"
)]
gas_price: u64,

#[structopt(
name = "expiration_time",
long = "timeout",
default_value = "3000",
help = "how long(in seconds) the txn stay alive"
)]
expiration_time: u64,

#[structopt(
short = "e",
name = "enforced",
long = "enforced",
help = "enforced upgrade regardless of compatible or not"
)]
enforced: bool,

#[structopt(
short = "b",
name = "blocking-mode",
long = "blocking",
help = "blocking wait txn mined"
)]
blocking: bool,

#[structopt(
short = "m",
name = "module-file",
long = "module",
help = "path for module file, can be empty.",
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 UpgradeModuleProposalV2Command;

impl CommandAction for UpgradeModuleProposalV2Command {
type State = CliState;
type GlobalOpt = StarcoinOpt;
type Opt = UpgradeModuleProposalOpt;
type ReturnItem = (HashValue, HashValue);

fn run(
&self,
ctx: &ExecContext<Self::State, Self::GlobalOpt, Self::Opt>,
) -> 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: Package = bcs_ext::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_v2(
&upgrade_package,
opt.version,
min_action_delay,
opt.enforced,
);
let signed_txn = sign_txn_with_account_by_rpc_client(
cli_state,
sender,
opt.max_gas_amount,
opt.gas_price,
opt.expiration_time,
TransactionPayload::ScriptFunction(module_upgrade_proposal),
)?;
let txn_hash = signed_txn.crypto_hash();
cli_state.client().submit_transaction(signed_txn)?;

println!("txn {:#x} submitted.", txn_hash);

if opt.blocking {
ctx.state().watch_txn(txn_hash)?;
}
Ok((package_hash, txn_hash))
} else {
bail!("file can not be empty.")
}
}
}
109 changes: 109 additions & 0 deletions cmd/starcoin/src/dev/upgrade_module_queue_v2_cmd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (c) The Starcoin Core Contributors
// SPDX-License-Identifier: Apache-2.0

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

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

#[structopt(
short = "g",
name = "max-gas-amount",
default_value = "10000000",
help = "max gas used to deploy the module"
)]
max_gas_amount: u64,
#[structopt(
short = "p",
long = "gas-price",
name = "price of gas",
default_value = "1",
help = "gas price used to deploy the module"
)]
gas_price: u64,

#[structopt(
name = "expiration_time",
long = "timeout",
default_value = "3000",
help = "how long(in seconds) the txn stay alive"
)]
expiration_time: u64,
#[structopt(
short = "b",
name = "blocking-mode",
long = "blocking",
help = "blocking wait txn mined"
)]
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",
long = "module",
help = "proposal id."
)]
proposal_id: u64,
}

pub struct UpgradeModuleQueueV2Command;

impl CommandAction for UpgradeModuleQueueV2Command {
type State = CliState;
type GlobalOpt = StarcoinOpt;
type Opt = UpgradeModuleQueueOpt;
type ReturnItem = HashValue;

fn run(
&self,
ctx: &ExecContext<Self::State, Self::GlobalOpt, Self::Opt>,
) -> 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
};
let proposer_address = if let Some(address) = ctx.opt().proposer_address {
address
} else {
sender
};
let module_upgrade_queue = build_module_upgrade_queue_v2(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,
TransactionPayload::ScriptFunction(module_upgrade_queue),
)?;
let txn_hash = signed_txn.id();
cli_state.client().submit_transaction(signed_txn)?;

println!("txn {:#x} submitted.", txn_hash);

if opt.blocking {
ctx.state().watch_txn(txn_hash)?;
}
Ok(txn_hash)
}
}
2 changes: 2 additions & 0 deletions cmd/starcoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ pub fn add_command(
.subcommand(dev::GenerateMultisigTxnCommand)
.subcommand(dev::ExecuteMultiSignedTxnCommand)
.subcommand(dev::UpgradeModuleProposalCommand)
.subcommand(dev::UpgradeModuleProposalV2Command)
.subcommand(dev::UpgradeModulePlanCommand)
.subcommand(dev::UpgradeModuleQueueCommand)
.subcommand(dev::UpgradeModuleQueueV2Command)
.subcommand(dev::UpgradeModuleExeCommand)
.subcommand(dev::CallContractCommand)
.subcommand(
Expand Down
29 changes: 26 additions & 3 deletions developer.starcoin.org/content/cli/upgrade_module_with_dao.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,52 @@ starcoin% account unlock <account address>
```bash
starcoin% dev module_proposal -s <account address> -m <module path> -v <version>
```
如果stdlib当前版本 >= 2, 改为下面命令:

```bash
starcoin% dev module_proposal_v2 -s <account address> -m <module path> -v <version> -e false
```
其中,参数 -m 表示升级包的路径;-v 表示新的版本号;参数 -e 表示是否跳过 module 兼容性检查强制升级,false 表示不跳过兼容性检查。

4. 查询提议状态:

```bash
starcoin% dev call --module-address <module address> --module-name Dao --func-name proposal_state -t 0x1::STC::STC -t 0x1::UpgradeModuleDaoProposal::UpgradeModule --arg <proposal address> --arg <proposal number>
```
如果stdlib当前版本 >= 2, 改为下面命令:

```bash
starcoin% dev call --module-address <module address> --module-name Dao --func-name proposal_state -t 0x1::STC::STC -t 0x1::UpgradeModuleDaoProposal::UpgradeModuleV2 --arg <proposal address> --arg <proposal number>
```

5. 任何人都可以给状态为 ACTIVE 的提议投赞成或者反对票:
```bash
starcoin% dev execute -s <account address> -b --script cast_vote -t 0x1::STC::STC -t 0x1::UpgradeModuleDaoProposal::UpgradeModule --arg <proposal address> --arg <proposal number> --arg <agree> --arg <votes>u128
```
如果stdlib当前版本 >= 2, 改为下面命令:
```bash
starcoin% dev execute -s <account address> -b --script cast_vote -t 0x1::STC::STC -t 0x1::UpgradeModuleDaoProposal::UpgradeModuleV2 --arg <proposal address> --arg <proposal number> --arg <agree> --arg <votes>u128
```

6. 任何人都可以将状态为 AGREED 的提议放入更新队列:
```bash
starcoin% dev module_queue -s <account address> -a <proposal address> -m <proposal number>
```
如果stdlib当前版本 >= 2, 改为下面命令:
```bash
starcoin% dev module_queue_v2 -s <account address> -a <proposal address> -m <proposal number>
```

7. 任何人都可以为状态为 QUEUED 的提议提交更新计划:
执行公示期满后,状态从 QUEUED 变为 EXECUTABLE。

7. 任何人都可以为状态为 EXECUTABLE 的提议提交更新计划:
```bash
starcoin% dev module_plan -s <account address> -a <proposal address> -m <proposal number>
```

8. 如果提议的状态为 EXECUTABLE,任何人都可以更新对应的Module:
8. 如果提议的状态为 EXTRACTED,任何人都可以更新对应的Module:
```bash
starcoin% dev module_exe -s <account address> -m <module path>
```
```

9. 最后不要忘记取回押金、终结提案。具体可参考上一节 onchain config 的修改。
Loading

0 comments on commit 566fcd3

Please sign in to comment.