Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

Commit

Permalink
Problem: dev-utils cannot generate genesis.json for testnet
Browse files Browse the repository at this point in the history
Solution: Added a new option in genesis generation command
(`unbonded_address`). If `unbonded_address` is provided, all other
staking addresses will be marked as `Bonded` except for the one provided
in `unbonded_address`, which will be marked as `UnbondedFromGenesis`.
If `unbonded_address` is not provided, everything will work the same.
  • Loading branch information
devashishdxt committed May 8, 2020
1 parent ae5271c commit 8a78e79
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
33 changes: 31 additions & 2 deletions dev-utils/src/commands/genesis_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,22 @@ pub enum GenesisCommand {
help = "Replace Tendermint genesis.json file in place"
)]
in_place: bool,

#[structopt(
name = "no_backup",
short,
long,
help = "Don't create backup file when modify in place, default is creating backup file genesis.bak.json in the same directory"
)]
no_backup: bool,

#[structopt(
name = "unbonded_address",
short,
long,
help = "If unbonded address is provided, all other staking addresses will be `Bonded` from genesis except from `unbonded_address`"
)]
unbonded_address: Option<String>,
},
}

Expand All @@ -68,11 +77,13 @@ impl GenesisCommand {
genesis_dev_config_path,
in_place,
no_backup,
unbonded_address,
} => generate_genesis_command(
tendermint_genesis_path,
genesis_dev_config_path,
*in_place,
*no_backup,
unbonded_address,
)
.map(|_| ()),
}
Expand All @@ -84,6 +95,7 @@ fn generate_genesis_command(
genesis_dev_config_path: &PathBuf,
in_place: bool,
no_backup: bool,
unbonded_address: &Option<String>,
) -> Result<()> {
let tendermint_genesis_path = match tendermint_genesis_path {
Some(path) => path.clone(),
Expand Down Expand Up @@ -132,7 +144,8 @@ fn generate_genesis_command(
.duration_since(Time::unix_epoch())
.expect("invalid genesis time")
.as_secs();
let (app_hash, app_state, validators) = generate_genesis(&genesis_dev_config, genesis_time)?;
let (app_hash, app_state, validators) =
generate_genesis(&genesis_dev_config, genesis_time, unbonded_address)?;

let app_hash = serde_json::to_value(app_hash).chain(|| {
(
Expand Down Expand Up @@ -205,15 +218,31 @@ fn find_tendermint_path_from_home() -> Option<PathBuf> {
pub fn generate_genesis(
genesis_dev_config: &GenesisDevConfig,
genesis_time: Timespec,
unbonded_address: &Option<String>,
) -> Result<(String, InitConfig, Vec<TendermintValidator>)> {
let mut dist: BTreeMap<RedeemAddress, (StakedStateDestination, Coin)> = BTreeMap::new();

let unbonded_address = unbonded_address
.as_ref()
.map(|address| {
RedeemAddress::from_str(address)
.chain(|| (ErrorKind::InvalidInput, "Invalid unbonded address provided"))
})
.transpose()?;

for (address, amount) in genesis_dev_config.distribution.iter() {
let dest = if genesis_dev_config.council_nodes.contains_key(&address) {
let dest = if let Some(ref unbonded_address) = unbonded_address {
if address == unbonded_address {
StakedStateDestination::UnbondedFromGenesis
} else {
StakedStateDestination::Bonded
}
} else if genesis_dev_config.council_nodes.contains_key(&address) {
StakedStateDestination::Bonded
} else {
StakedStateDestination::UnbondedFromGenesis
};

dist.insert(*address, (dest, *amount));
}
let constant_fee = Milli::from_str(&genesis_dev_config.initial_fee_policy.base_fee)
Expand Down
1 change: 1 addition & 0 deletions dev-utils/src/commands/init_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ impl InitCommand {
.duration_since(Time::unix_epoch())
.unwrap()
.as_secs(),
&None,
)
.unwrap();
self.app_hash = result.0;
Expand Down

0 comments on commit 8a78e79

Please sign in to comment.