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

Change Planner to use optional gas prices field and error if unset #4554

Merged
merged 3 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion crates/bin/pcli/src/command/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde_json::Value;
use penumbra_governance::{
ValidatorVote, ValidatorVoteBody, ValidatorVoteReason, Vote, MAX_VALIDATOR_VOTE_REASON_LENGTH,
};
use penumbra_proto::DomainType;
use penumbra_proto::{view::v1::GasPricesRequest, DomainType};
use penumbra_stake::{
validator,
validator::{Validator, ValidatorToml},
Expand All @@ -22,6 +22,8 @@ use penumbra_stake::{

use crate::App;

use penumbra_fee::FeeTier;

#[derive(Debug, clap::Subcommand)]
pub enum ValidatorCmd {
/// Display the validator identity key derived from this wallet's spend seed.
Expand Down Expand Up @@ -70,6 +72,9 @@ pub enum VoteCmd {
/// key, i.e. when using a separate governance key on another wallet.
#[clap(long, global = true, display_order = 600)]
validator: Option<IdentityKey>,
/// The selected fee tier to multiply the fee amount by.
#[clap(short, long, default_value_t)]
fee_tier: FeeTier,
},
/// Sign a vote on a proposal in your capacity as a validator, for submission elsewhere.
Sign {
Expand Down Expand Up @@ -107,6 +112,9 @@ pub enum DefinitionCmd {
/// definition may be generated using the `pcli validator definition sign` command.
#[clap(long)]
signature: Option<String>,
/// The selected fee tier to multiply the fee amount by.
#[clap(short, long, default_value_t)]
fee_tier: FeeTier,
},
/// Sign a validator definition offline for submission elsewhere.
Sign {
Expand Down Expand Up @@ -229,7 +237,19 @@ impl ValidatorCmd {
file,
source,
signature,
fee_tier,
}) => {
let gas_prices = app
.view
.as_mut()
.context("view service must be initialized")?
.gas_prices(GasPricesRequest {})
.await?
.into_inner()
.gas_prices
.expect("gas prices must be available")
.try_into()?;

let new_validator = read_validator_toml(file)?;

// Sign the validator definition with the wallet's spend key, or instead attach the
Expand Down Expand Up @@ -259,6 +279,8 @@ impl ValidatorCmd {

let plan = Planner::new(OsRng)
.validator_definition(vd)
.set_gas_prices(gas_prices)
.set_fee_tier((*fee_tier).into())
.plan(app.view(), source.into())
.await?;

Expand Down Expand Up @@ -321,7 +343,19 @@ impl ValidatorCmd {
reason,
signature,
validator,
fee_tier,
}) => {
let gas_prices = app
.view
.as_mut()
.context("view service must be initialized")?
.gas_prices(GasPricesRequest {})
.await?
.into_inner()
.gas_prices
.expect("gas prices must be available")
.try_into()?;

let identity_key = validator
.unwrap_or_else(|| IdentityKey(fvk.spend_verification_key().clone().into()));
let governance_key = app.config.governance_key();
Expand Down Expand Up @@ -365,6 +399,8 @@ impl ValidatorCmd {

// Construct a new transaction and include the validator definition.
let plan = Planner::new(OsRng)
.set_gas_prices(gas_prices)
.set_fee_tier((*fee_tier).into())
.validator_vote(vote)
.plan(app.view(), source.into())
.await?;
Expand Down
11 changes: 10 additions & 1 deletion crates/core/app/tests/view_server_can_be_served_on_localhost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use {
penumbra_proto::{
view::v1::{
view_service_client::ViewServiceClient, view_service_server::ViewServiceServer,
StatusRequest, StatusResponse,
GasPricesRequest, StatusRequest, StatusResponse,
},
DomainType,
},
Expand Down Expand Up @@ -133,8 +133,17 @@ async fn view_server_can_be_served_on_localhost() -> anyhow::Result<()> {

// Create a plan spending that note, using the `Planner`.
let plan = {
let gas_prices = view_client
.gas_prices(GasPricesRequest {})
.await?
.into_inner()
.gas_prices
.expect("gas prices must be available")
.try_into()?;

let mut planner = Planner::new(rand_core::OsRng);
planner
.set_gas_prices(gas_prices)
.spend(note.to_owned(), position)
.output(note.value(), test_keys::ADDRESS_1.deref().clone())
.plan(&mut view_client, AddressIndex::default())
Expand Down
12 changes: 8 additions & 4 deletions crates/view/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct Planner<R: RngCore + CryptoRng> {
/// The fee tier to apply to this transaction.
fee_tier: FeeTier,
/// The set of prices used for gas estimation.
gas_prices: GasPrices,
gas_prices: Option<GasPrices>,
/// The transaction parameters to use for the transaction.
transaction_parameters: TransactionParameters,
/// A user-specified change address, if any.
Expand Down Expand Up @@ -105,7 +105,7 @@ impl<R: RngCore + CryptoRng> Planner<R> {
/// Set the current gas prices for fee prediction.
#[instrument(skip(self))]
pub fn set_gas_prices(&mut self, gas_prices: GasPrices) -> &mut Self {
self.gas_prices = gas_prices;
self.gas_prices = Some(gas_prices);
self
}

Expand Down Expand Up @@ -544,7 +544,9 @@ impl<R: RngCore + CryptoRng> Planner<R> {
// Compute an initial fee estimate based on the actions we have so far.
self.action_list.refresh_fee_and_change(
&mut self.rng,
&self.gas_prices,
&self
.gas_prices
.context("planner instances must call set_gas_prices prior to planning")?,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I'm not a huge fan of forcing a separate call rather than doing it automatically, I love how descriptive this error message is!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We alternatively could just change the Planner constructor to require GasPrices or a ViewClient instance it can fetch them from

&self.fee_tier,
&change_address,
);
Expand Down Expand Up @@ -597,7 +599,9 @@ impl<R: RngCore + CryptoRng> Planner<R> {
// Refresh the fee estimate and change outputs.
self.action_list.refresh_fee_and_change(
&mut self.rng,
&self.gas_prices,
&self
.gas_prices
.context("planner instances must call set_gas_prices prior to planning")?,
&self.fee_tier,
&change_address,
);
Expand Down
3 changes: 3 additions & 0 deletions crates/wallet/src/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ where
{
const SWEEP_COUNT: usize = 8;

let gas_prices = view.gas_prices().await?;

let all_notes = view
.notes(NotesRequest {
..Default::default()
Expand Down Expand Up @@ -123,6 +125,7 @@ where
// chunks, ignoring the biggest notes in the remainder.
for group in records.chunks_exact(SWEEP_COUNT) {
let mut planner = Planner::new(&mut rng);
planner.set_gas_prices(gas_prices);

for record in group {
planner.spend(record.note.clone(), record.position);
Expand Down
Loading