Skip to content

Commit

Permalink
staking: check length in TryFrom<FundingStreams>
Browse files Browse the repository at this point in the history
  • Loading branch information
erwanor committed Feb 7, 2024
1 parent 953f22d commit fafa3c2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl ActionHandler for validator::Definition {
anyhow::bail!("validator description must be less than 280 characters")
}

if self.validator.funding_streams.funding_streams.len() > 8 {
if self.validator.funding_streams.len() > 8 {
anyhow::bail!("validators can declare at most 8 funding streams")
}

Expand Down
10 changes: 10 additions & 0 deletions crates/core/component/stake/src/funding_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ impl TryFrom<pb::FundingStream> for FundingStream {
/// [`FundingStream`]s, and cannot exceed 10000bps (100%). This property is guaranteed by the
/// `TryFrom<Vec<FundingStream>` implementation for [`FundingStreams`], which checks the sum, and is
/// the only way to build a non-empty [`FundingStreams`].
///
/// Similarly, it's not possible to build a [`FundingStreams`] with more than 8 funding streams.
#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct FundingStreams {
funding_streams: Vec<FundingStream>,
Expand All @@ -182,12 +184,20 @@ impl FundingStreams {
pub fn iter(&self) -> impl Iterator<Item = &FundingStream> {
self.funding_streams.iter()
}

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

impl TryFrom<Vec<FundingStream>> for FundingStreams {
type Error = anyhow::Error;

fn try_from(funding_streams: Vec<FundingStream>) -> Result<Self, Self::Error> {
if funding_streams.len() > 8 {
anyhow::bail!("validators can declare at most 8 funding streams");
}

if funding_streams.iter().map(|fs| fs.rate_bps()).sum::<u16>() > 10_000 {
anyhow::bail!("sum of funding rates exceeds 100% (10,000bps)");
}
Expand Down

0 comments on commit fafa3c2

Please sign in to comment.