Skip to content

Commit a882fa9

Browse files
committed
feat(slasher): remap from peer id
1 parent 0e96c8a commit a882fa9

3 files changed

Lines changed: 15 additions & 15 deletions

File tree

collator/src/validator/impls/std_impl/session.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl ValidatorSession {
255255
);
256256

257257
// Notify listeners about the own signature
258-
events_scope.receive_signature(self.inner.own_validator_idx, true);
258+
events_scope.receive_signature(self.inner.peer_id.as_bytes(), true);
259259

260260
let mut total_weight = self.inner.own_weight;
261261

@@ -421,7 +421,6 @@ impl ValidatorSession {
421421
};
422422

423423
let validator_info = validators.get(peer_id).expect("peer info out of sync");
424-
let validator_idx = validator_info.validator_idx;
425424

426425
if !validator_info.public_key.verify_raw(&data, &signature) {
427426
tracing::warn!(
@@ -439,14 +438,14 @@ impl ValidatorSession {
439438
metrics::counter!(METRIC_INVALID_SIGNATURES_CACHED_TOTAL).increment(1);
440439

441440
if let Some(scope) = events_scope.upgrade() {
442-
scope.receive_signature(validator_idx, false);
441+
scope.receive_signature(peer_id.as_bytes(), false);
443442
}
444443

445444
break 'stored Default::default();
446445
}
447446

448447
if let Some(scope) = events_scope.upgrade() {
449-
scope.receive_signature(validator_idx, true);
448+
scope.receive_signature(peer_id.as_bytes(), true);
450449
}
451450

452451
total_weight += validator_info.weight;
@@ -699,7 +698,7 @@ impl SessionState {
699698
metrics::counter!(METRIC_INVALID_SIGNATURES_IN_TOTAL).increment(1);
700699

701700
if let Some(scope) = block.events_scope.upgrade() {
702-
scope.receive_signature(validator_info.validator_idx, false);
701+
scope.receive_signature(peer_id.as_bytes(), false);
703702
}
704703

705704
return Err(ValidationError::InvalidSignature);
@@ -740,7 +739,7 @@ impl SessionState {
740739
&& !was_sealed
741740
&& let Some(scope) = block.events_scope.upgrade()
742741
{
743-
scope.receive_signature(validator_info.validator_idx, true);
742+
scope.receive_signature(peer_id.as_bytes(), true);
744743
}
745744

746745
Ok(())

slasher-traits/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ pub use self::validator::{
44
};
55

66
mod validator;
7+
8+
pub type PeerIdInner = [u8; 32];

slasher-traits/src/validator.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use indexmap::IndexMap;
66
use tycho_types::models::{BlockId, IndexedValidatorDescription};
77
use tycho_util::FastHasherState;
88

9+
use crate::PeerIdInner;
10+
911
// TODO: Decide how to be with this collator-defined type
1012
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1113
pub struct ValidationSessionId {
@@ -61,12 +63,9 @@ impl ValidatorEvents {
6163
self.listener
6264
.on_session_started(session_id, first_mc_seqno, own_validator_idx, validators);
6365

64-
let mut remap = IndexMap::<u16, u16, FastHasherState>::with_capacity_and_hasher(
65-
validators.len(),
66-
Default::default(),
67-
);
66+
let mut remap = IndexMap::with_capacity_and_hasher(validators.len(), Default::default());
6867
for (i, validator) in validators.iter().enumerate() {
69-
remap.insert(validator.validator_idx, i as u16);
68+
remap.insert(validator.desc.public_key.0, i as u16);
7069
}
7170

7271
ValidatorSessionScope {
@@ -81,7 +80,7 @@ impl ValidatorEvents {
8180
pub struct ValidatorSessionScope {
8281
recorder: Arc<dyn ValidatorEventsListener>,
8382
session_id: ValidationSessionId,
84-
remap_ids: Arc<IndexMap<u16, u16, FastHasherState>>,
83+
remap_ids: Arc<IndexMap<PeerIdInner, u16, FastHasherState>>,
8584
is_sealed: AtomicBool,
8685
}
8786

@@ -120,7 +119,7 @@ impl Drop for ValidatorSessionScope {
120119
pub struct BlockValidationScope {
121120
recorder: Arc<dyn ValidatorEventsListener>,
122121
session_id: ValidationSessionId,
123-
remap_ids: Arc<IndexMap<u16, u16, FastHasherState>>,
122+
remap_ids: Arc<IndexMap<PeerIdInner, u16, FastHasherState>>,
124123
block_id: BlockId,
125124
signature_slots: Box<[AtomicU8]>,
126125
is_sealed: AtomicBool,
@@ -135,14 +134,14 @@ impl BlockValidationScope {
135134
&self.block_id
136135
}
137136

138-
pub fn receive_signature(&self, validator_idx: u16, is_valid: bool) -> bool {
137+
pub fn receive_signature(&self, peer_id: &PeerIdInner, is_valid: bool) -> bool {
139138
let mask = if is_valid {
140139
ReceivedSignature::VALID_SIGNATURE_BIT
141140
} else {
142141
ReceivedSignature::INVALID_SIGNATURE_BIT
143142
};
144143

145-
let Some(slot_id) = self.remap_ids.get(&validator_idx) else {
144+
let Some(slot_id) = self.remap_ids.get(peer_id) else {
146145
return false;
147146
};
148147

0 commit comments

Comments
 (0)