Skip to content

Commit

Permalink
pcli: add validator template-definition and fetch-definition commands
Browse files Browse the repository at this point in the history
Closes penumbra-zone#648

Along the way to this, I noticed that we have a lot of overlapping "validator"
structures, and maybe we should try to streamline those later?
  • Loading branch information
hdevalence authored and avahowell committed Apr 19, 2022
1 parent 82d9fe4 commit 3e649ff
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pcli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ penumbra-wallet = { path = "../wallet" }
# Penumbra dependencies
ark-ff = { git = "https://github.com/penumbra-zone/algebra", branch = "ours" }
decaf377 = { git = "https://github.com/penumbra-zone/decaf377" }
tendermint = { git = "https://github.com/penumbra-zone/tendermint-rs.git", branch = "master" }
# External dependencies
ed25519-consensus = "2"
futures = "0.3"
async-stream = "0.2"
bincode = "1.3.3"
Expand Down
109 changes: 107 additions & 2 deletions pcli/src/command/validator.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::fs::File;
use std::{fs::File, io::Write};

use anyhow::{Context, Result};
use futures::TryStreamExt;
use penumbra_proto::{stake::Validator as ProtoValidator, Message};
use penumbra_stake::{IdentityKey, Validator, ValidatorDefinition};
use penumbra_stake::{
FundingStream, FundingStreams, IdentityKey, Validator, ValidatorDefinition, ValidatorInfo,
};
use rand_core::OsRng;
use structopt::StructOpt;

Expand All @@ -24,13 +27,32 @@ pub enum ValidatorCmd {
#[structopt(long)]
source: Option<u64>,
},
/// Generates a template validator definition for editing.
///
/// The validator identity field will be prepopulated with the validator
/// identity key derived from this wallet's seed phrase.
TemplateDefinition {
/// The JSON file to write the template to.
#[structopt(long)]
file: String,
},
/// Fetches a validator's current definition and saves it to a file.
FetchDefinition {
/// The JSON file to write the template to.
#[structopt(long)]
file: String,
/// The identity key of the validator to fetch.
identity_key: String,
},
}

impl ValidatorCmd {
pub fn needs_sync(&self) -> bool {
match self {
ValidatorCmd::Identity => false,
ValidatorCmd::UploadDefinition { .. } => true,
ValidatorCmd::TemplateDefinition { .. } => false,
ValidatorCmd::FetchDefinition { .. } => false,
}
}

Expand Down Expand Up @@ -82,6 +104,89 @@ impl ValidatorCmd {
state.commit()?;
println!("Uploaded validator definition");
}
ValidatorCmd::TemplateDefinition { file } => {
let (_label, address) = state.wallet().address_by_index(0)?;
let identity_key = IdentityKey(
state
.wallet()
.full_viewing_key()
.spend_verification_key()
.clone(),
);
// Generate a random consensus key.
let consensus_key =
tendermint::PrivateKey::Ed25519(ed25519_consensus::SigningKey::new(OsRng))
.public_key();

let template = Validator {
identity_key,
consensus_key,
name: String::new(),
website: String::new(),
description: String::new(),
funding_streams: FundingStreams::try_from(vec![FundingStream {
address,
rate_bps: 100,
}])?,
sequence_number: 0,
};

File::create(file)
.with_context(|| format!("cannot create file {:?}", file))?
.write_all(&serde_json::to_vec_pretty(&template)?)
.context("could not write file")?;
}
ValidatorCmd::FetchDefinition { file, identity_key } => {
let identity_key = identity_key.parse::<IdentityKey>()?;

/*
use penumbra_proto::client::specific::ValidatorStatusRequest;
let mut client = opt.specific_client().await?;
let status: ValidatorStatus = client
.validator_status(ValidatorStatusRequest {
chain_id: "".to_string(), // TODO: fill in
identity_key: Some(identity_key.into()),
})
.await?
.into_inner()
.try_into()?;
// why isn't the validator definition part of the status?
// why do we have all these different validator messages?
// do we need them?
status.state.
*/

// Intsead just download everything
let mut client = opt.oblivious_client().await?;

use penumbra_proto::client::oblivious::ValidatorInfoRequest;
let validators = client
.validator_info(ValidatorInfoRequest {
show_inactive: true,
chain_id: state.chain_id().unwrap_or_default(),
})
.await?
.into_inner()
.try_collect::<Vec<_>>()
.await?
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<ValidatorInfo>, _>>()?;

let validator = validators
.iter()
.map(|info| &info.validator)
.find(|v| v.identity_key == identity_key)
.cloned()
.ok_or_else(|| anyhow::anyhow!("Could not find validator {}", identity_key))?;

File::create(file)
.with_context(|| format!("cannot create file {:?}", file))?
.write_all(&serde_json::to_vec_pretty(&validator)?)
.context("could not write file")?;
}
}

Ok(())
Expand Down

0 comments on commit 3e649ff

Please sign in to comment.