Skip to content

Commit

Permalink
Merge branch 'mcdaan/McDaan-validator-alias' (#2911)
Browse files Browse the repository at this point in the history
* mcdaan/McDaan-validator-alias:
  test
  change to validator name (from validator alias)
  fix tests
  Changelog
  generating localnet files
  add validator alias field to metadata
  • Loading branch information
brentstone committed May 8, 2024
2 parents 580cf3a + 294519c commit 4c1bbe5
Show file tree
Hide file tree
Showing 24 changed files with 165 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/2911-validator-alias.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Add a validator alias field to validator metadata
([\#2911](https://github.com/anoma/namada/pull/2911))
30 changes: 30 additions & 0 deletions crates/apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3235,6 +3235,7 @@ pub mod args {
pub const USE_DEVICE: ArgFlag = flag("use-device");
pub const VALIDATOR: Arg<WalletAddress> = arg("validator");
pub const VALIDATOR_OPT: ArgOpt<WalletAddress> = VALIDATOR.opt();
pub const VALIDATOR_NAME_OPT: ArgOpt<String> = arg_opt("name");
pub const VALIDATOR_ACCOUNT_KEY: ArgOpt<WalletPublicKey> =
arg_opt("account-key");
pub const VALIDATOR_ACCOUNT_KEYS: ArgMulti<WalletPublicKey, GlobStar> =
Expand Down Expand Up @@ -4386,6 +4387,7 @@ pub mod args {
website: self.website,
discord_handle: self.discord_handle,
avatar: self.avatar,
name: self.name,
unsafe_dont_encrypt: self.unsafe_dont_encrypt,
tx_code_path: self.tx_code_path.to_path_buf(),
})
Expand All @@ -4409,6 +4411,7 @@ pub mod args {
let website = WEBSITE_OPT.parse(matches);
let discord_handle = DISCORD_OPT.parse(matches);
let avatar = AVATAR_OPT.parse(matches);
let name = VALIDATOR_NAME_OPT.parse(matches);
let unsafe_dont_encrypt = UNSAFE_DONT_ENCRYPT.parse(matches);
let tx_code_path = PathBuf::from(TX_BECOME_VALIDATOR_WASM);
Self {
Expand All @@ -4426,6 +4429,7 @@ pub mod args {
website,
discord_handle,
avatar,
name,
unsafe_dont_encrypt,
tx_code_path,
}
Expand Down Expand Up @@ -4475,6 +4479,10 @@ pub mod args {
.arg(WEBSITE_OPT.def().help("The validator's website."))
.arg(DISCORD_OPT.def().help("The validator's discord handle."))
.arg(AVATAR_OPT.def().help("The validator's avatar."))
.arg(VALIDATOR_NAME_OPT.def().help(
"The validator's name, used to identify the validator in \
online services.",
))
.arg(VALIDATOR_CODE_PATH.def().help(
"The path to the validity predicate WASM code to be used \
for the validator account. Uses the default validator VP \
Expand Down Expand Up @@ -4517,6 +4525,7 @@ pub mod args {
website: self.website,
discord_handle: self.discord_handle,
avatar: self.avatar,
name: self.name,
validator_vp_code_path: self
.validator_vp_code_path
.to_path_buf(),
Expand Down Expand Up @@ -4548,6 +4557,7 @@ pub mod args {
let website = WEBSITE_OPT.parse(matches);
let discord_handle = DISCORD_OPT.parse(matches);
let avatar = AVATAR_OPT.parse(matches);
let name = VALIDATOR_NAME_OPT.parse(matches);
let validator_vp_code_path = VALIDATOR_CODE_PATH
.parse(matches)
.unwrap_or_else(|| PathBuf::from(VP_USER_WASM));
Expand All @@ -4572,6 +4582,7 @@ pub mod args {
website,
discord_handle,
avatar,
name,
validator_vp_code_path,
unsafe_dont_encrypt,
tx_init_account_code_path,
Expand Down Expand Up @@ -4625,6 +4636,10 @@ pub mod args {
.arg(WEBSITE_OPT.def().help("The validator's website."))
.arg(DISCORD_OPT.def().help("The validator's discord handle."))
.arg(AVATAR_OPT.def().help("The validator's avatar."))
.arg(VALIDATOR_NAME_OPT.def().help(
"The validator's alias, used to identify the validator in \
online services.",
))
.arg(VALIDATOR_CODE_PATH.def().help(
"The path to the validity predicate WASM code to be used \
for the validator account. Uses the default validator VP \
Expand Down Expand Up @@ -5733,6 +5748,7 @@ pub mod args {
website: self.website,
discord_handle: self.discord_handle,
avatar: self.avatar,
name: self.name,
commission_rate: self.commission_rate,
tx_code_path: self.tx_code_path.to_path_buf(),
})
Expand All @@ -5748,6 +5764,7 @@ pub mod args {
let website = WEBSITE_OPT.parse(matches);
let discord_handle = DISCORD_OPT.parse(matches);
let avatar = AVATAR_OPT.parse(matches);
let name = VALIDATOR_NAME_OPT.parse(matches);
let commission_rate = COMMISSION_RATE_OPT.parse(matches);
let tx_code_path = PathBuf::from(TX_CHANGE_METADATA_WASM);
Self {
Expand All @@ -5758,6 +5775,7 @@ pub mod args {
website,
discord_handle,
avatar,
name,
commission_rate,
tx_code_path,
}
Expand Down Expand Up @@ -5790,6 +5808,11 @@ pub mod args {
"The desired new validator avatar url. To remove the \
existing avatar, pass an empty string to this argument.",
))
.arg(VALIDATOR_NAME_OPT.def().help(
"The desired new validator name, used to identify the \
validator in online services. To remove the existing \
validator alias, pass an empty string to this argument.",
))
.arg(
COMMISSION_RATE_OPT
.def()
Expand Down Expand Up @@ -7421,6 +7444,7 @@ pub mod args {
pub website: Option<String>,
pub discord_handle: Option<String>,
pub avatar: Option<String>,
pub name: Option<String>,
pub address: EstablishedAddress,
pub tx_path: PathBuf,
}
Expand All @@ -7441,6 +7465,7 @@ pub mod args {
let website = WEBSITE_OPT.parse(matches);
let discord_handle = DISCORD_OPT.parse(matches);
let avatar = AVATAR_OPT.parse(matches);
let name = VALIDATOR_NAME_OPT.parse(matches);
let address = RAW_ADDRESS_ESTABLISHED.parse(matches);
let tx_path = PATH.parse(matches);
Self {
Expand All @@ -7456,6 +7481,7 @@ pub mod args {
website,
discord_handle,
avatar,
name,
tx_path,
address,
}
Expand Down Expand Up @@ -7516,6 +7542,10 @@ pub mod args {
.arg(AVATAR_OPT.def().help(
"The validator's avatar. This is an optional parameter.",
))
.arg(VALIDATOR_NAME_OPT.def().help(
"The validator's name, used to identify the validator in \
online services. This is an optional parameter.",
))
}
}

Expand Down
12 changes: 9 additions & 3 deletions crates/apps/src/lib/client/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,13 +1298,19 @@ pub async fn query_and_print_metadata(
website,
discord_handle,
avatar,
name,
}) => {
display_line!(
context.io(),
"Validator {} metadata:\nEmail: {}",
validator.encode(),
email
"Validator {} metadata:",
validator.encode()
);
if let Some(name) = name {
display_line!(context.io(), "Validator name: {}", name);
} else {
display_line!(context.io(), "No validator name");
}
display_line!(context.io(), "Email: {}", email);
if let Some(description) = description {
display_line!(context.io(), "Description: {}", description);
} else {
Expand Down
2 changes: 2 additions & 0 deletions crates/apps/src/lib/client/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ pub async fn submit_init_validator(
description,
discord_handle,
avatar,
name,
validator_vp_code_path,
unsafe_dont_encrypt,
tx_init_account_code_path,
Expand Down Expand Up @@ -729,6 +730,7 @@ pub async fn submit_init_validator(
website,
discord_handle,
avatar,
name,
tx_code_path: tx_become_validator_code_path,
unsafe_dont_encrypt,
},
Expand Down
2 changes: 2 additions & 0 deletions crates/apps/src/lib/client/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ pub fn init_genesis_validator(
website,
discord_handle,
avatar,
name,
tx_path,
address,
}: args::InitGenesisValidator,
Expand Down Expand Up @@ -888,6 +889,7 @@ pub fn init_genesis_validator(
website,
discord_handle,
avatar,
name,
},
&validator_wallet,
);
Expand Down
1 change: 1 addition & 0 deletions crates/apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ pub fn make_dev_genesis(
website: None,
discord_handle: None,
avatar: None,
name: None,
},
net_address: SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
Expand Down
4 changes: 4 additions & 0 deletions crates/apps/src/lib/config/genesis/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ pub struct GenesisValidatorData {
pub website: Option<String>,
pub discord_handle: Option<String>,
pub avatar: Option<String>,
pub name: Option<String>,
}

/// Panics if given `txs.validator_accounts` is not empty, because validator
Expand Down Expand Up @@ -284,6 +285,7 @@ pub fn init_validator(
website,
discord_handle,
avatar,
name,
}: GenesisValidatorData,
validator_wallet: &ValidatorWallet,
) -> (Address, UnsignedTransactions) {
Expand Down Expand Up @@ -318,6 +320,7 @@ pub fn init_validator(
website,
discord_handle,
avatar,
name,
},
};
let unsigned_validator_addr =
Expand Down Expand Up @@ -630,6 +633,7 @@ impl TxToSign for ValidatorAccountTx<SignedPk> {
website: self.metadata.website.clone(),
discord_handle: self.metadata.discord_handle.clone(),
avatar: self.metadata.avatar.clone(),
name: self.metadata.name.clone(),
},
)
}
Expand Down
4 changes: 4 additions & 0 deletions crates/light_sdk/src/transaction/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ impl BecomeValidator {
website: Option<String>,
discord_handle: Option<String>,
avatar: Option<String>,
name: Option<String>,
args: GlobalArgs,
) -> Self {
let update_account = namada_sdk::tx::data::pos::BecomeValidator {
Expand All @@ -170,6 +171,7 @@ impl BecomeValidator {
website,
discord_handle,
avatar,
name,
};

Self(transaction::build_tx(
Expand Down Expand Up @@ -461,6 +463,7 @@ impl ChangeMetaData {
website: Option<String>,
discord_handle: Option<String>,
avatar: Option<String>,
name: Option<String>,
commission_rate: Option<Dec>,
args: GlobalArgs,
) -> Self {
Expand All @@ -471,6 +474,7 @@ impl ChangeMetaData {
website,
discord_handle,
avatar,
name,
commission_rate,
};

Expand Down
5 changes: 5 additions & 0 deletions crates/proof_of_stake/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use namada_storage::collections::lazy_map::{self, Collectable, LazyMap};
use namada_storage::{StorageRead, StorageWrite};
pub use namada_trans_token as token;
pub use parameters::{OwnedPosParams, PosParams};
use storage::write_validator_name;
use types::{into_tm_voting_power, DelegationEpochs};

use crate::queries::{find_bonds, has_bonds};
Expand Down Expand Up @@ -2680,6 +2681,7 @@ pub fn change_validator_metadata<S>(
website: Option<String>,
discord_handle: Option<String>,
avatar: Option<String>,
name: Option<String>,
commission_rate: Option<Dec>,
current_epoch: Epoch,
) -> namada_storage::Result<()>
Expand All @@ -2701,6 +2703,9 @@ where
if let Some(avatar) = avatar {
write_validator_avatar(storage, validator, &avatar)?;
}
if let Some(name) = name {
write_validator_name(storage, validator, &name)?;
}
if let Some(commission_rate) = commission_rate {
change_validator_commission_rate(
storage,
Expand Down
31 changes: 31 additions & 0 deletions crates/proof_of_stake/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,34 @@ where
}
}

/// Read PoS validator's name.
pub fn read_validator_name<S>(
storage: &S,
validator: &Address,
) -> namada_storage::Result<Option<String>>
where
S: StorageRead,
{
storage.read(&storage_key::validator_name_key(validator))
}

/// Write PoS validator's name. If the provided arg is an empty
/// string, remove the data.
pub fn write_validator_name<S>(
storage: &mut S,
validator: &Address,
validator_name: &String,
) -> namada_storage::Result<()>
where
S: StorageRead + StorageWrite,
{
let key = storage_key::validator_name_key(validator);
if validator_name.is_empty() {
storage.delete(&key)
} else {
storage.write(&key, validator_name)
}
}
/// Write validator's metadata.
pub fn write_validator_metadata<S>(
storage: &mut S,
Expand All @@ -918,6 +946,9 @@ where
if let Some(avatar) = metadata.avatar.as_ref() {
write_validator_avatar(storage, validator, avatar)?;
}
if let Some(name) = metadata.name.as_ref() {
write_validator_name(storage, validator, name)?;
}
Ok(())
}

Expand Down
9 changes: 9 additions & 0 deletions crates/proof_of_stake/src/storage_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const VALIDATOR_DESCRIPTION_KEY: &str = "description";
const VALIDATOR_WEBSITE_KEY: &str = "website";
const VALIDATOR_DISCORD_KEY: &str = "discord_handle";
const VALIDATOR_AVATAR_KEY: &str = "avatar";
const VALIDATOR_NAME_KEY: &str = "name";
const LIVENESS_PREFIX: &str = "liveness";
const LIVENESS_MISSED_VOTES: &str = "missed_votes";
const LIVENESS_MISSED_VOTES_SUM: &str = "sum_missed_votes";
Expand Down Expand Up @@ -277,6 +278,7 @@ pub fn is_validator_metadata_key(key: &Key) -> Option<&Address> {
| VALIDATOR_WEBSITE_KEY
| VALIDATOR_DISCORD_KEY
| VALIDATOR_AVATAR_KEY
| VALIDATOR_NAME_KEY
) =>
{
Some(validator)
Expand Down Expand Up @@ -1028,6 +1030,13 @@ pub fn validator_avatar_key(validator: &Address) -> Key {
.expect("Cannot obtain a storage key")
}

/// Storage key for a validator's name
pub fn validator_name_key(validator: &Address) -> Key {
validator_prefix(validator)
.push(&VALIDATOR_NAME_KEY.to_owned())
.expect("Cannot obtain a storage key")
}

/// Storage prefix for the liveness data of the cosnensus validator set.
pub fn liveness_data_prefix() -> Key {
Key::from(ADDRESS.to_db_key())
Expand Down
3 changes: 3 additions & 0 deletions crates/proof_of_stake/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ pub struct ValidatorMetaData {
/// URL that points to a picture (e.g. PNG),
/// identifying the validator
pub avatar: Option<String>,
/// Validator's name
pub name: Option<String>,
}

#[cfg(any(test, feature = "testing"))]
Expand All @@ -402,6 +404,7 @@ impl Default for ValidatorMetaData {
website: Default::default(),
discord_handle: Default::default(),
avatar: Default::default(),
name: Default::default(),
}
}
}
Expand Down
Loading

0 comments on commit 4c1bbe5

Please sign in to comment.