Skip to content

Commit

Permalink
add more restrictions for validator metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
longbowlu authored and bmwill committed Mar 25, 2023
1 parent bfb2b4f commit 5679d63
Show file tree
Hide file tree
Showing 7 changed files with 427 additions and 173 deletions.
8 changes: 8 additions & 0 deletions crates/mysten-network/src/multiaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ impl Multiaddr {
self.0.replace(at, by).map(Self)
}

pub fn len(&self) -> usize {
self.0.len()
}

pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

/// Attempts to convert a multiaddr of the form `/[ip4,ip6,dns]/{}/udp/{port}` into an anemo
/// address
pub fn to_anemo_address(&self) -> Result<anemo::types::Address, &'static str> {
Expand Down
33 changes: 29 additions & 4 deletions crates/sui-config/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ use sui_types::{
use sui_types::{SUI_FRAMEWORK_ADDRESS, SUI_SYSTEM_ADDRESS};
use tracing::trace;

const MAX_VALIDATOR_METADATA_LENGTH: usize = 256;

#[derive(Clone, Debug)]
pub struct Genesis {
checkpoint: CertifiedCheckpointSummary,
Expand Down Expand Up @@ -342,29 +344,52 @@ impl GenesisValidatorInfo {
if !self.info.name.is_ascii() {
bail!("name must be ascii");
}
if self.info.name.len() > 128 {
bail!("name must be <= 128 bytes long");
if self.info.name.len() > MAX_VALIDATOR_METADATA_LENGTH {
bail!("name must be <= {MAX_VALIDATOR_METADATA_LENGTH} bytes long");
}

if !self.info.description.is_ascii() {
bail!("description must be ascii");
}
if self.info.description.len() > 150 {
bail!("description must be <= 150 bytes long");
if self.info.description.len() > MAX_VALIDATOR_METADATA_LENGTH {
bail!("description must be <= {MAX_VALIDATOR_METADATA_LENGTH} bytes long");
}

if self.info.image_url.len() > MAX_VALIDATOR_METADATA_LENGTH {
bail!("image url must be <= {MAX_VALIDATOR_METADATA_LENGTH} bytes long");
}

if self.info.project_url.len() > MAX_VALIDATOR_METADATA_LENGTH {
bail!("project url must be <= {MAX_VALIDATOR_METADATA_LENGTH} bytes long");
}

if !self.info.network_address.to_string().is_ascii() {
bail!("network address must be ascii");
}
if self.info.network_address.len() > MAX_VALIDATOR_METADATA_LENGTH {
bail!("network address must be <= {MAX_VALIDATOR_METADATA_LENGTH} bytes long");
}

if !self.info.p2p_address.to_string().is_ascii() {
bail!("p2p address must be ascii");
}
if self.info.p2p_address.len() > MAX_VALIDATOR_METADATA_LENGTH {
bail!("p2p address must be <= {MAX_VALIDATOR_METADATA_LENGTH} bytes long");
}

if !self.info.narwhal_primary_address.to_string().is_ascii() {
bail!("primary address must be ascii");
}
if self.info.narwhal_primary_address.len() > MAX_VALIDATOR_METADATA_LENGTH {
bail!("primary address must be <= {MAX_VALIDATOR_METADATA_LENGTH} bytes long");
}

if !self.info.narwhal_worker_address.to_string().is_ascii() {
bail!("worker address must be ascii");
}
if self.info.narwhal_worker_address.len() > MAX_VALIDATOR_METADATA_LENGTH {
bail!("worker address must be <= {MAX_VALIDATOR_METADATA_LENGTH} bytes long");
}

if let Err(e) = self.info.p2p_address.to_anemo_address() {
bail!("p2p address must be valid anemo address: {e}");
Expand Down
22 changes: 6 additions & 16 deletions crates/sui-framework/docs/sui_system_state_inner.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@
- [Module Specification](#@Module_Specification_1)


<pre><code><b>use</b> <a href="">0x1::ascii</a>;
<b>use</b> <a href="">0x1::option</a>;
<b>use</b> <a href="">0x1::string</a>;
<pre><code><b>use</b> <a href="">0x1::option</a>;
<b>use</b> <a href="">0x2::bag</a>;
<b>use</b> <a href="">0x2::balance</a>;
<b>use</b> <a href="">0x2::coin</a>;
Expand All @@ -75,7 +73,6 @@
<b>use</b> <a href="">0x2::table</a>;
<b>use</b> <a href="">0x2::transfer</a>;
<b>use</b> <a href="">0x2::tx_context</a>;
<b>use</b> <a href="">0x2::url</a>;
<b>use</b> <a href="">0x2::vec_map</a>;
<b>use</b> <a href="">0x2::vec_set</a>;
<b>use</b> <a href="stake_subsidy.md#0x3_stake_subsidy">0x3::stake_subsidy</a>;
Expand Down Expand Up @@ -1203,7 +1200,8 @@ Update a validator's name.
ctx: &TxContext,
) {
<b>let</b> <a href="validator.md#0x3_validator">validator</a> = <a href="validator_set.md#0x3_validator_set_get_validator_mut_with_ctx_including_candidates">validator_set::get_validator_mut_with_ctx_including_candidates</a>(&<b>mut</b> self.validators, ctx);
<a href="validator.md#0x3_validator_update_name">validator::update_name</a>(<a href="validator.md#0x3_validator">validator</a>, <a href="_from_ascii">string::from_ascii</a>(<a href="_string">ascii::string</a>(name)));

<a href="validator.md#0x3_validator_update_name">validator::update_name</a>(<a href="validator.md#0x3_validator">validator</a>, name);
}
</code></pre>

Expand Down Expand Up @@ -1233,7 +1231,7 @@ Update a validator's description
ctx: &TxContext,
) {
<b>let</b> <a href="validator.md#0x3_validator">validator</a> = <a href="validator_set.md#0x3_validator_set_get_validator_mut_with_ctx_including_candidates">validator_set::get_validator_mut_with_ctx_including_candidates</a>(&<b>mut</b> self.validators, ctx);
<a href="validator.md#0x3_validator_update_description">validator::update_description</a>(<a href="validator.md#0x3_validator">validator</a>, <a href="_from_ascii">string::from_ascii</a>(<a href="_string">ascii::string</a>(description)));
<a href="validator.md#0x3_validator_update_description">validator::update_description</a>(<a href="validator.md#0x3_validator">validator</a>, description);
}
</code></pre>

Expand Down Expand Up @@ -1263,7 +1261,7 @@ Update a validator's image url
ctx: &TxContext,
) {
<b>let</b> <a href="validator.md#0x3_validator">validator</a> = <a href="validator_set.md#0x3_validator_set_get_validator_mut_with_ctx_including_candidates">validator_set::get_validator_mut_with_ctx_including_candidates</a>(&<b>mut</b> self.validators, ctx);
<a href="validator.md#0x3_validator_update_image_url">validator::update_image_url</a>(<a href="validator.md#0x3_validator">validator</a>, <a href="_new_unsafe_from_bytes">url::new_unsafe_from_bytes</a>(image_url));
<a href="validator.md#0x3_validator_update_image_url">validator::update_image_url</a>(<a href="validator.md#0x3_validator">validator</a>, image_url);
}
</code></pre>

Expand Down Expand Up @@ -1293,7 +1291,7 @@ Update a validator's project url
ctx: &TxContext,
) {
<b>let</b> <a href="validator.md#0x3_validator">validator</a> = <a href="validator_set.md#0x3_validator_set_get_validator_mut_with_ctx_including_candidates">validator_set::get_validator_mut_with_ctx_including_candidates</a>(&<b>mut</b> self.validators, ctx);
<a href="validator.md#0x3_validator_update_project_url">validator::update_project_url</a>(<a href="validator.md#0x3_validator">validator</a>, <a href="_new_unsafe_from_bytes">url::new_unsafe_from_bytes</a>(project_url));
<a href="validator.md#0x3_validator_update_project_url">validator::update_project_url</a>(<a href="validator.md#0x3_validator">validator</a>, project_url);
}
</code></pre>

Expand Down Expand Up @@ -1324,7 +1322,6 @@ The change will only take effects starting from the next epoch.
ctx: &TxContext,
) {
<b>let</b> <a href="validator.md#0x3_validator">validator</a> = <a href="validator_set.md#0x3_validator_set_get_validator_mut_with_ctx">validator_set::get_validator_mut_with_ctx</a>(&<b>mut</b> self.validators, ctx);
<b>let</b> network_address = <a href="_from_ascii">string::from_ascii</a>(<a href="_string">ascii::string</a>(network_address));
<a href="validator.md#0x3_validator_update_next_epoch_network_address">validator::update_next_epoch_network_address</a>(<a href="validator.md#0x3_validator">validator</a>, network_address);
<b>let</b> <a href="validator.md#0x3_validator">validator</a> :&Validator = <a href="validator.md#0x3_validator">validator</a>; // Force immutability for the following call
<a href="validator_set.md#0x3_validator_set_assert_no_pending_or_actice_duplicates">validator_set::assert_no_pending_or_actice_duplicates</a>(&self.validators, <a href="validator.md#0x3_validator">validator</a>);
Expand Down Expand Up @@ -1357,7 +1354,6 @@ Update candidate validator's network address.
ctx: &TxContext,
) {
<b>let</b> candidate = <a href="validator_set.md#0x3_validator_set_get_validator_mut_with_ctx_including_candidates">validator_set::get_validator_mut_with_ctx_including_candidates</a>(&<b>mut</b> self.validators, ctx);
<b>let</b> network_address = <a href="_from_ascii">string::from_ascii</a>(<a href="_string">ascii::string</a>(network_address));
<a href="validator.md#0x3_validator_update_candidate_network_address">validator::update_candidate_network_address</a>(candidate, network_address);
}
</code></pre>
Expand Down Expand Up @@ -1389,7 +1385,6 @@ The change will only take effects starting from the next epoch.
ctx: &TxContext,
) {
<b>let</b> <a href="validator.md#0x3_validator">validator</a> = <a href="validator_set.md#0x3_validator_set_get_validator_mut_with_ctx">validator_set::get_validator_mut_with_ctx</a>(&<b>mut</b> self.validators, ctx);
<b>let</b> p2p_address = <a href="_from_ascii">string::from_ascii</a>(<a href="_string">ascii::string</a>(p2p_address));
<a href="validator.md#0x3_validator_update_next_epoch_p2p_address">validator::update_next_epoch_p2p_address</a>(<a href="validator.md#0x3_validator">validator</a>, p2p_address);
<b>let</b> <a href="validator.md#0x3_validator">validator</a> :&Validator = <a href="validator.md#0x3_validator">validator</a>; // Force immutability for the following call
<a href="validator_set.md#0x3_validator_set_assert_no_pending_or_actice_duplicates">validator_set::assert_no_pending_or_actice_duplicates</a>(&self.validators, <a href="validator.md#0x3_validator">validator</a>);
Expand Down Expand Up @@ -1422,7 +1417,6 @@ Update candidate validator's p2p address.
ctx: &TxContext,
) {
<b>let</b> candidate = <a href="validator_set.md#0x3_validator_set_get_validator_mut_with_ctx_including_candidates">validator_set::get_validator_mut_with_ctx_including_candidates</a>(&<b>mut</b> self.validators, ctx);
<b>let</b> p2p_address = <a href="_from_ascii">string::from_ascii</a>(<a href="_string">ascii::string</a>(p2p_address));
<a href="validator.md#0x3_validator_update_candidate_p2p_address">validator::update_candidate_p2p_address</a>(candidate, p2p_address);
}
</code></pre>
Expand Down Expand Up @@ -1454,7 +1448,6 @@ The change will only take effects starting from the next epoch.
ctx: &TxContext,
) {
<b>let</b> <a href="validator.md#0x3_validator">validator</a> = <a href="validator_set.md#0x3_validator_set_get_validator_mut_with_ctx">validator_set::get_validator_mut_with_ctx</a>(&<b>mut</b> self.validators, ctx);
<b>let</b> primary_address = <a href="_from_ascii">string::from_ascii</a>(<a href="_string">ascii::string</a>(primary_address));
<a href="validator.md#0x3_validator_update_next_epoch_primary_address">validator::update_next_epoch_primary_address</a>(<a href="validator.md#0x3_validator">validator</a>, primary_address);
}
</code></pre>
Expand Down Expand Up @@ -1485,7 +1478,6 @@ Update candidate validator's narwhal primary address.
ctx: &TxContext,
) {
<b>let</b> candidate = <a href="validator_set.md#0x3_validator_set_get_validator_mut_with_ctx_including_candidates">validator_set::get_validator_mut_with_ctx_including_candidates</a>(&<b>mut</b> self.validators, ctx);
<b>let</b> primary_address = <a href="_from_ascii">string::from_ascii</a>(<a href="_string">ascii::string</a>(primary_address));
<a href="validator.md#0x3_validator_update_candidate_primary_address">validator::update_candidate_primary_address</a>(candidate, primary_address);
}
</code></pre>
Expand Down Expand Up @@ -1517,7 +1509,6 @@ The change will only take effects starting from the next epoch.
ctx: &TxContext,
) {
<b>let</b> <a href="validator.md#0x3_validator">validator</a> = <a href="validator_set.md#0x3_validator_set_get_validator_mut_with_ctx">validator_set::get_validator_mut_with_ctx</a>(&<b>mut</b> self.validators, ctx);
<b>let</b> worker_address = <a href="_from_ascii">string::from_ascii</a>(<a href="_string">ascii::string</a>(worker_address));
<a href="validator.md#0x3_validator_update_next_epoch_worker_address">validator::update_next_epoch_worker_address</a>(<a href="validator.md#0x3_validator">validator</a>, worker_address);
}
</code></pre>
Expand Down Expand Up @@ -1548,7 +1539,6 @@ Update candidate validator's narwhal worker address.
ctx: &TxContext,
) {
<b>let</b> candidate = <a href="validator_set.md#0x3_validator_set_get_validator_mut_with_ctx_including_candidates">validator_set::get_validator_mut_with_ctx_including_candidates</a>(&<b>mut</b> self.validators, ctx);
<b>let</b> worker_address = <a href="_from_ascii">string::from_ascii</a>(<a href="_string">ascii::string</a>(worker_address));
<a href="validator.md#0x3_validator_update_candidate_worker_address">validator::update_candidate_worker_address</a>(candidate, worker_address);
}
</code></pre>
Expand Down
Loading

0 comments on commit 5679d63

Please sign in to comment.