Skip to content

Commit 9686c5d

Browse files
committed
update WeightingConfig to match record program
1 parent b503648 commit 9686c5d

File tree

2 files changed

+51
-52
lines changed

2 files changed

+51
-52
lines changed

gossip/src/push_active_set.rs

Lines changed: 30 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub enum WeightingMode {
2828
// alpha in [1.0, 2.0], smoothed over time, scaled up by 1,000,000 to avoid floating-point math
2929
Dynamic {
3030
alpha: u64, // current alpha (fixed-point, 1,000,000–2,000,000)
31-
filter_k: u64, // default: 611,000
31+
filter_k: u64, // default: 611,015
3232
tc_ms: u64, // IIR time-constant (ms)
3333
},
3434
}
@@ -101,8 +101,8 @@ impl PushActiveSet {
101101
}
102102

103103
pub(crate) fn apply_cfg(&mut self, cfg: &WeightingConfig) {
104-
info!("greg: apply_cfg");
105-
match (&mut self.mode, WeightingConfigTyped::from(cfg)) {
104+
let config_type = WeightingConfigTyped::from(cfg);
105+
match (&mut self.mode, config_type) {
106106
(WeightingMode::Static, WeightingConfigTyped::Static) => (),
107107
(current_mode, WeightingConfigTyped::Static) => {
108108
// Dynamic -> Static: Switch mode
@@ -115,7 +115,6 @@ impl PushActiveSet {
115115
},
116116
WeightingConfigTyped::Dynamic { tc },
117117
) => {
118-
info!("greg: apply_cfg dynamic -> dynamic");
119118
// Dynamic -> Dynamic: Update parameters if needed
120119
let new_tc_ms = match tc {
121120
TimeConstant::Value(ms) => ms,
@@ -127,10 +126,9 @@ impl PushActiveSet {
127126
info!("Recomputed filter K = {} (tc_ms = {})", *filter_k, *tc_ms);
128127
}
129128
}
130-
(WeightingMode::Static, WeightingConfigTyped::Dynamic { .. }) => {
131-
// Static -> Dynamic: Switch mode
132-
info!("Switching mode: Static -> Dynamic");
133-
self.mode = WeightingMode::from(WeightingConfigTyped::from(cfg));
129+
(current_mode, WeightingConfigTyped::Dynamic { .. }) => {
130+
info!("Switching mode: {:?} -> Dynamic", current_mode);
131+
self.mode = WeightingMode::from(config_type);
134132
if let WeightingMode::Dynamic {
135133
filter_k, tc_ms, ..
136134
} = self.mode
@@ -771,16 +769,21 @@ mod tests {
771769
}
772770
}
773771

772+
#[test]
773+
fn test_record_size() {
774+
assert_eq!(
775+
bincode::serialized_size(&WeightingConfig::default()).unwrap(),
776+
58
777+
);
778+
}
779+
774780
#[test]
775781
fn test_apply_cfg_static_to_static() {
776782
// Static -> Static: No change
777783
let mut active_set = PushActiveSet::new(WeightingMode::Static);
778784
assert_eq!(active_set.mode, WeightingMode::Static);
779785

780-
active_set.apply_cfg(&WeightingConfig {
781-
weighting_mode: WEIGHTING_MODE_STATIC,
782-
tc_ms: 0,
783-
});
786+
active_set.apply_cfg(&WeightingConfig::new_for_test(WEIGHTING_MODE_STATIC, 0));
784787
assert_eq!(active_set.mode, WeightingMode::Static);
785788
}
786789

@@ -790,10 +793,7 @@ mod tests {
790793
let mut active_set = PushActiveSet::new_dynamic();
791794
assert!(matches!(active_set.mode, WeightingMode::Dynamic { .. }));
792795

793-
active_set.apply_cfg(&WeightingConfig {
794-
weighting_mode: WEIGHTING_MODE_STATIC,
795-
tc_ms: 0,
796-
});
796+
active_set.apply_cfg(&WeightingConfig::new_for_test(WEIGHTING_MODE_STATIC, 0));
797797
assert_eq!(active_set.mode, WeightingMode::Static);
798798
}
799799

@@ -803,10 +803,7 @@ mod tests {
803803
let mut active_set = PushActiveSet::new(WeightingMode::Static);
804804
assert_eq!(active_set.mode, WeightingMode::Static);
805805

806-
let config = WeightingConfig {
807-
weighting_mode: WEIGHTING_MODE_DYNAMIC,
808-
tc_ms: 0,
809-
};
806+
let config = WeightingConfig::new_for_test(WEIGHTING_MODE_DYNAMIC, 0);
810807
active_set.apply_cfg(&config);
811808

812809
match active_set.mode {
@@ -832,10 +829,7 @@ mod tests {
832829
let mut active_set = PushActiveSet::new_dynamic();
833830
let original_mode = active_set.mode;
834831

835-
let config = WeightingConfig {
836-
weighting_mode: WEIGHTING_MODE_DYNAMIC,
837-
tc_ms: 0,
838-
};
832+
let config = WeightingConfig::new_for_test(WEIGHTING_MODE_DYNAMIC, 0);
839833
active_set.apply_cfg(&config);
840834

841835
// Mode should be unchanged since tc is the same
@@ -849,10 +843,7 @@ mod tests {
849843

850844
// Change to a different tc value
851845
let new_tc_ms = 45_000;
852-
let config = WeightingConfig {
853-
weighting_mode: WEIGHTING_MODE_DYNAMIC,
854-
tc_ms: new_tc_ms,
855-
};
846+
let config = WeightingConfig::new_for_test(WEIGHTING_MODE_DYNAMIC, new_tc_ms);
856847
active_set.apply_cfg(&config);
857848

858849
match active_set.mode {
@@ -878,37 +869,31 @@ mod tests {
878869
let mut active_set = PushActiveSet::new(WeightingMode::Static);
879870

880871
// Static -> Dynamic
881-
active_set.apply_cfg(&WeightingConfig {
882-
weighting_mode: WEIGHTING_MODE_DYNAMIC,
883-
tc_ms: 20_000,
884-
});
872+
active_set.apply_cfg(&WeightingConfig::new_for_test(
873+
WEIGHTING_MODE_DYNAMIC,
874+
20_000,
875+
));
885876
assert!(matches!(
886877
active_set.mode,
887878
WeightingMode::Dynamic { tc_ms: 20_000, .. }
888879
));
889880

890881
// Dynamic -> Dynamic (change tc)
891-
active_set.apply_cfg(&WeightingConfig {
892-
weighting_mode: WEIGHTING_MODE_DYNAMIC,
893-
tc_ms: 40_000,
894-
});
882+
active_set.apply_cfg(&WeightingConfig::new_for_test(
883+
WEIGHTING_MODE_DYNAMIC,
884+
40_000,
885+
));
895886
assert!(matches!(
896887
active_set.mode,
897888
WeightingMode::Dynamic { tc_ms: 40_000, .. }
898889
));
899890

900891
// Dynamic -> Static
901-
active_set.apply_cfg(&WeightingConfig {
902-
weighting_mode: WEIGHTING_MODE_STATIC,
903-
tc_ms: 0,
904-
});
892+
active_set.apply_cfg(&WeightingConfig::new_for_test(WEIGHTING_MODE_STATIC, 0));
905893
assert_eq!(active_set.mode, WeightingMode::Static);
906894

907895
// Static -> Dynamic (with default tc)
908-
active_set.apply_cfg(&WeightingConfig {
909-
weighting_mode: WEIGHTING_MODE_DYNAMIC,
910-
tc_ms: 0,
911-
});
896+
active_set.apply_cfg(&WeightingConfig::new_for_test(WEIGHTING_MODE_DYNAMIC, 0));
912897
assert!(
913898
matches!(active_set.mode, WeightingMode::Dynamic { tc_ms, .. } if tc_ms == DEFAULT_TC_MS)
914899
);
@@ -922,10 +907,7 @@ mod tests {
922907
let test_cases = [10_000, 30_000, 60_000, 120_000];
923908

924909
for tc_ms in test_cases {
925-
let config = WeightingConfig {
926-
weighting_mode: WEIGHTING_MODE_DYNAMIC,
927-
tc_ms,
928-
};
910+
let config = WeightingConfig::new_for_test(WEIGHTING_MODE_DYNAMIC, tc_ms);
929911
active_set.apply_cfg(&config);
930912

931913
let expected_filter_k = lpf::compute_k(REFRESH_PUSH_ACTIVE_SET_INTERVAL_MS, tc_ms);

gossip/src/stake_weighting_config.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ pub enum TimeConstant {
88
Default,
99
}
1010

11-
#[derive(Deserialize, Debug)]
11+
/// Actual on-chain state that controls the weighting of gossip nodes
12+
#[derive(Serialize, Deserialize, Debug, Default)]
1213
#[repr(C)]
1314
pub struct WeightingConfig {
14-
pub weighting_mode: u8,
15-
pub tc_ms: u64,
15+
_version: u8, // This is part of Record program header
16+
_authority: [u8; 32], // This is part of Record program header
17+
pub weighting_mode: u8, // 0 = Static, 1 = Dynamic
18+
pub tc_ms: u64, // IIR time constant in milliseconds
19+
_future_use: [u8; 16], // Reserved for future use
1620
}
1721

1822
pub const WEIGHTING_MODE_STATIC: u8 = 0;
@@ -41,8 +45,21 @@ impl From<&WeightingConfig> for WeightingConfigTyped {
4145
}
4246
}
4347

48+
impl WeightingConfig {
49+
#[cfg(test)]
50+
pub fn new_for_test(weighting_mode: u8, tc_ms: u64) -> Self {
51+
Self {
52+
_version: 0,
53+
_authority: [0; 32],
54+
weighting_mode,
55+
tc_ms,
56+
_future_use: [0; 16],
57+
}
58+
}
59+
}
60+
4461
mod weighting_config_control_pubkey {
45-
solana_pubkey::declare_id!("gosWyfqaAmMhdrSM9srCsV6DihYg7Ze56SvJLwZbNSP");
62+
solana_pubkey::declare_id!("goSwVUizoqNYKEaaiTjkgdN2RgLpvsTvFt1MEVGibY9");
4663
}
4764

4865
pub(crate) fn get_gossip_config_from_account(bank: &Bank) -> Option<WeightingConfig> {

0 commit comments

Comments
 (0)