-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add RestartHeaviestFork to Gossip #34161
Changes from 11 commits
a2e4a5e
ed573d9
e2ad04d
30540d0
35f9c3e
0fc9e08
62f13a0
dbd5159
7c244a7
8aedf03
a976ca4
2e2b84e
7c338b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
use { | ||
crate::crds_value::new_rand_timestamp, | ||
crate::crds_value::{new_rand_timestamp, sanitize_wallclock}, | ||
bv::BitVec, | ||
itertools::Itertools, | ||
rand::Rng, | ||
|
@@ -29,6 +29,16 @@ pub enum RestartLastVotedForkSlotsError { | |
LastVotedForkEmpty, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, AbiExample, Debug)] | ||
pub struct RestartHeaviestFork { | ||
pub from: Pubkey, | ||
pub wallclock: u64, | ||
pub last_slot: Slot, | ||
pub last_slot_hash: Hash, | ||
pub observed_stake: u64, | ||
pub shred_version: u16, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, AbiExample, AbiEnumVisitor)] | ||
enum SlotsOffsets { | ||
RunLengthEncoding(RunLengthEncoding), | ||
|
@@ -48,6 +58,7 @@ struct RawOffsets(BitVec<u8>); | |
|
||
impl Sanitize for RestartLastVotedForkSlots { | ||
fn sanitize(&self) -> std::result::Result<(), SanitizeError> { | ||
sanitize_wallclock(self.wallclock)?; | ||
self.last_voted_hash.sanitize() | ||
} | ||
} | ||
|
@@ -122,6 +133,45 @@ impl RestartLastVotedForkSlots { | |
} | ||
} | ||
|
||
impl Sanitize for RestartHeaviestFork { | ||
fn sanitize(&self) -> Result<(), SanitizeError> { | ||
sanitize_wallclock(self.wallclock)?; | ||
self.last_slot_hash.sanitize() | ||
} | ||
} | ||
|
||
impl RestartHeaviestFork { | ||
pub fn new( | ||
from: Pubkey, | ||
now: u64, | ||
last_slot: Slot, | ||
last_slot_hash: Hash, | ||
observed_stake: u64, | ||
shred_version: u16, | ||
) -> Self { | ||
Self { | ||
from, | ||
wallclock: now, | ||
last_slot, | ||
last_slot_hash, | ||
observed_stake, | ||
shred_version, | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this? all fields are public. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed. |
||
|
||
pub fn new_rand<R: Rng>(rng: &mut R, pubkey: Option<Pubkey>) -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need to be Can you go over all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is used in tests, and I don't see any benchmarks needing it now. So I changed it to pub(crate). |
||
let pubkey = pubkey.unwrap_or_else(solana_sdk::pubkey::new_rand); | ||
Self::new( | ||
pubkey, | ||
new_rand_timestamp(rng), | ||
rng.gen_range(0..1000), | ||
Hash::new_unique(), | ||
rng.gen_range(1..u64::MAX), | ||
1, | ||
) | ||
} | ||
} | ||
|
||
impl RunLengthEncoding { | ||
fn new(bits: &BitVec<u8>) -> Self { | ||
let encoded = (0..bits.len()) | ||
|
@@ -317,4 +367,22 @@ mod test { | |
let range: Vec<Slot> = make_rand_slots(&mut rng).take(large_length).collect(); | ||
check_run_length_encoding(range); | ||
} | ||
|
||
#[test] | ||
fn test_restart_heaviest_fork() { | ||
let keypair = Keypair::new(); | ||
let slot = 53; | ||
let mut fork = RestartHeaviestFork::new( | ||
keypair.pubkey(), | ||
timestamp(), | ||
slot, | ||
Hash::default(), | ||
800_000, | ||
1, | ||
); | ||
assert_eq!(fork.sanitize(), Ok(())); | ||
assert_eq!(fork.observed_stake, 800_000); | ||
fork.wallclock = crate::crds_value::MAX_WALLCLOCK; | ||
assert_eq!(fork.sanitize(), Err(SanitizeError::ValueOutOfBounds)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we sanitize the wallclock like the other crds values?
also probably that should be done for the previous restart crds value as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.