use bytemuck::{Pod, Zeroable};
use serde::{Deserialize, Serialize};
#[cfg(feature = "wincode")]
use wincode_derive::{SchemaRead, SchemaWrite};
use tape_crypto::address::Address;
use crate::bft::{quorum_above, quorum_below};
use crate::bls::BlsPubkey;
use crate::types::*;
use super::{Member, Peer};
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Pod, Zeroable)]
pub struct NodeMetadata {
pub name: [u8; 32],
pub network_address: NetworkAddress,
pub network_tls: NetworkTlsPubkey,
pub bls_pubkey: BlsPubkey,
}
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq, Zeroable, Pod, Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "wincode", derive(SchemaRead, SchemaWrite))]
pub struct NodePreferences {
pub committee_size: u64,
pub spool_groups: u64,
pub burn_fee_bps: BasisPoints,
pub subsidy_decay_bps: BasisPoints,
pub storage_capacity: StorageUnits,
pub storage_price: Coin<TAPE>,
pub access_threshold: Coin<TAPE>,
pub epoch_duration: EpochDuration,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NodePreferenceAggregationError {
MissingPeer { node: Address },
ZeroWeight,
}
pub fn aggregate_node_preferences(
members: &[Member],
peers: &[Peer],
bounds: NodePreferences,
) -> Result<NodePreferences, NodePreferenceAggregationError> {
let mut total_weight: u64 = 0;
let mut committee_sizes: Vec<(u64, u64)> = Vec::new();
let mut spool_group_counts: Vec<(u64, u64)> = Vec::new();
let mut burn_fee_bps: Vec<(u64, u64)> = Vec::new();
let mut subsidy_decay_bps: Vec<(u64, u64)> = Vec::new();
let mut storage_capacities: Vec<(u64, u64)> = Vec::new();
let mut storage_prices: Vec<(u64, u64)> = Vec::new();
let mut access_thresholds: Vec<(u64, u64)> = Vec::new();
let mut epoch_durations: Vec<(u64, u64)> = Vec::new();
for member in members.iter() {
let peer = peers
.iter()
.find(|p| p.node == member.node)
.ok_or(NodePreferenceAggregationError::MissingPeer { node: member.node })?;
let weight = member.spools;
committee_sizes.push((peer.preferences.committee_size, weight));
spool_group_counts.push((peer.preferences.spool_groups, weight));
burn_fee_bps.push((peer.preferences.burn_fee_bps.0, weight));
subsidy_decay_bps.push((peer.preferences.subsidy_decay_bps.0, weight));
storage_capacities.push((peer.preferences.storage_capacity.0, weight));
storage_prices.push((peer.preferences.storage_price.0, weight));
access_thresholds.push((peer.preferences.access_threshold.0, weight));
epoch_durations.push((peer.preferences.epoch_duration.0, weight));
total_weight = total_weight.saturating_add(weight);
}
if total_weight == 0 {
return Err(NodePreferenceAggregationError::ZeroWeight);
}
Ok(NodePreferences {
committee_size: quorum_above(&committee_sizes, total_weight)
.max(bounds.committee_size),
spool_groups: quorum_above(&spool_group_counts, total_weight)
.max(bounds.spool_groups),
burn_fee_bps: BasisPoints(
quorum_below(&burn_fee_bps, total_weight)
.min(BasisPoints::MAX)
.max(bounds.burn_fee_bps.0),
),
subsidy_decay_bps: BasisPoints(
quorum_below(&subsidy_decay_bps, total_weight)
.min(BasisPoints::MAX)
.max(bounds.subsidy_decay_bps.0),
),
storage_capacity: StorageUnits(
quorum_above(&storage_capacities, total_weight)
.max(bounds.storage_capacity.0),
),
storage_price: TAPE(
quorum_below(&storage_prices, total_weight)
.max(bounds.storage_price.0),
),
access_threshold: TAPE(
quorum_above(&access_thresholds, total_weight)
.max(bounds.access_threshold.0),
),
epoch_duration: EpochDuration(
quorum_above(&epoch_durations, total_weight)
.max(bounds.epoch_duration.0),
),
})
}