Skip to content

Commit dbcfd1c

Browse files
committed
refactor: inline validator public key calls
1 parent b65c7de commit dbcfd1c

6 files changed

Lines changed: 34 additions & 45 deletions

File tree

crates/blockchain/src/aggregation.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::collections::{HashMap, HashSet};
2020
use std::time::{Duration, Instant, SystemTime};
2121

2222
use ethlambda_crypto::aggregate_mixed;
23-
use ethlambda_crypto::signature::{ValidatorPubkeys, ValidatorPublicKey, ValidatorSignature};
23+
use ethlambda_crypto::signature::{ValidatorPublicKey, ValidatorSignature};
2424
use ethlambda_storage::Store;
2525
use ethlambda_types::{
2626
ShortRoot,
@@ -425,7 +425,7 @@ fn resolve_job(
425425
let Some(validator) = validators.get(*vid as usize) else {
426426
continue;
427427
};
428-
let Ok(pubkey) = validator.get_attestation_pubkey() else {
428+
let Ok(pubkey) = ValidatorPublicKey::from_bytes(&validator.attestation_pubkey) else {
429429
continue;
430430
};
431431
raw_by_id.insert(*vid, (pubkey, sig.clone()));
@@ -492,7 +492,10 @@ fn resolve_child_pubkeys(
492492
let participant_ids: Vec<u64> = proof.participant_indices().collect();
493493
let child_pubkeys: Vec<ValidatorPublicKey> = participant_ids
494494
.iter()
495-
.filter_map(|&vid| validators.get(vid as usize)?.get_attestation_pubkey().ok())
495+
.filter_map(|&vid| {
496+
let v = validators.get(vid as usize)?;
497+
ValidatorPublicKey::from_bytes(&v.attestation_pubkey).ok()
498+
})
496499
.collect();
497500
if child_pubkeys.len() != participant_ids.len() {
498501
warn!(

crates/blockchain/src/block_builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::{
1515
time::Instant,
1616
};
1717

18-
use ethlambda_crypto::{aggregate_proofs, signature::ValidatorPubkeys};
18+
use ethlambda_crypto::{aggregate_proofs, signature::ValidatorPublicKey};
1919
use ethlambda_state_transition::{
2020
attestation_data_matches_chain, justified_slots_ops, process_block, process_slots,
2121
slot_is_justifiable_after,
@@ -657,11 +657,11 @@ fn compact_attestations(
657657
let pubkeys = proof
658658
.participant_indices()
659659
.map(|vid| {
660-
head_state
660+
let validator = head_state
661661
.validators
662662
.get(vid as usize)
663-
.ok_or(StoreError::InvalidValidatorIndex)?
664-
.get_attestation_pubkey()
663+
.ok_or(StoreError::InvalidValidatorIndex)?;
664+
ValidatorPublicKey::from_bytes(&validator.attestation_pubkey)
665665
.map_err(|_| StoreError::PubkeyDecodingFailed(vid))
666666
})
667667
.collect::<Result<Vec<_>, _>>()?;

crates/blockchain/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::{HashMap, HashSet, VecDeque};
22
use std::time::{Duration, Instant, SystemTime};
33

4-
use ethlambda_crypto::signature::{ValidatorPubkeys, ValidatorPublicKey, ValidatorSignature};
4+
use ethlambda_crypto::signature::{ValidatorPublicKey, ValidatorSignature};
55
use ethlambda_network_api::{BlockChainToP2PRef, InitP2P};
66
use ethlambda_state_transition::is_proposer;
77
use ethlambda_storage::{ALL_TABLES, Store};
@@ -763,7 +763,10 @@ impl BlockChainServer {
763763
// Decode the proposer's proposal pubkey once and reuse it both for the
764764
// singleton single-message aggregate wrap and for the multi-message
765765
// aggregate merge inputs.
766-
let Ok(proposer_pubkey) = proposer_validator.get_proposal_pubkey().inspect_err(
766+
let Ok(proposer_pubkey) = ValidatorPublicKey::from_bytes(
767+
&proposer_validator.proposal_pubkey,
768+
)
769+
.inspect_err(
767770
|err| error!(%slot, %validator_id, %err, "Failed to decode proposer proposal pubkey"),
768771
) else {
769772
metrics::inc_block_building_failures();
@@ -802,7 +805,7 @@ impl BlockChainServer {
802805
resolve_failed = true;
803806
break;
804807
};
805-
match validator.get_attestation_pubkey() {
808+
match ValidatorPublicKey::from_bytes(&validator.attestation_pubkey) {
806809
Ok(pk) => pubkeys.push(pk),
807810
Err(err) => {
808811
error!(%slot, %validator_id, vid, %err, "Failed to decode attestation pubkey");

crates/blockchain/src/reaggregate.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
2525
use std::collections::HashSet;
2626

27-
use ethlambda_crypto::signature::{ValidatorPubkeys, ValidatorPublicKey};
27+
use ethlambda_crypto::signature::ValidatorPublicKey;
2828
use ethlambda_storage::Store;
2929
use ethlambda_types::{
3030
attestation::{
@@ -83,7 +83,9 @@ pub fn reaggregate_from_block(
8383
warn!(vid, "Reaggregation aborted: participant out of range");
8484
return Vec::new();
8585
}
86-
let Ok(pk) = validators[vid as usize].get_attestation_pubkey() else {
86+
let Ok(pk) =
87+
ValidatorPublicKey::from_bytes(&validators[vid as usize].attestation_pubkey)
88+
else {
8789
warn!(vid, "Reaggregation aborted: bad attestation pubkey");
8890
return Vec::new();
8991
};
@@ -94,7 +96,8 @@ pub fn reaggregate_from_block(
9496
if block.proposer_index >= num_validators {
9597
return Vec::new();
9698
}
97-
let Ok(proposer_pubkey) = validators[block.proposer_index as usize].get_proposal_pubkey()
99+
let Ok(proposer_pubkey) =
100+
ValidatorPublicKey::from_bytes(&validators[block.proposer_index as usize].proposal_pubkey)
98101
else {
99102
return Vec::new();
100103
};
@@ -161,7 +164,9 @@ pub fn reaggregate_from_block(
161164
bad = true;
162165
break;
163166
}
164-
match validators[vid as usize].get_attestation_pubkey() {
167+
match ValidatorPublicKey::from_bytes(
168+
&validators[vid as usize].attestation_pubkey,
169+
) {
165170
Ok(pk) => pubkeys.push(pk),
166171
Err(_) => {
167172
bad = true;

crates/blockchain/src/store.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::{HashMap, HashSet};
22

3-
use ethlambda_crypto::signature::{ValidatorPubkeys, ValidatorPublicKey, ValidatorSignature};
3+
use ethlambda_crypto::signature::{ValidatorPublicKey, ValidatorSignature};
44
use ethlambda_state_transition::{is_proposer, slot_is_justifiable_after};
55
use ethlambda_storage::{ForkCheckpoints, Store};
66
use ethlambda_types::{
@@ -418,9 +418,10 @@ pub fn on_gossip_attestation(
418418
if validator_id >= target_state.validators.len() as u64 {
419419
return Err(StoreError::InvalidValidatorIndex);
420420
}
421-
let validator_pubkey = target_state.validators[validator_id as usize]
422-
.get_attestation_pubkey()
423-
.map_err(|_| StoreError::PubkeyDecodingFailed(validator_id))?;
421+
let validator_pubkey = ValidatorPublicKey::from_bytes(
422+
&target_state.validators[validator_id as usize].attestation_pubkey,
423+
)
424+
.map_err(|_| StoreError::PubkeyDecodingFailed(validator_id))?;
424425

425426
// Verify the validator's XMSS signature
426427
let slot: u32 = attestation.data.slot.try_into().expect("slot exceeds u32");
@@ -513,8 +514,7 @@ fn on_gossip_aggregated_attestation_core(
513514
let pubkeys: Vec<_> = participant_indices
514515
.iter()
515516
.map(|&vid| {
516-
validators[vid as usize]
517-
.get_attestation_pubkey()
517+
ValidatorPublicKey::from_bytes(&validators[vid as usize].attestation_pubkey)
518518
.map_err(|_| StoreError::PubkeyDecodingFailed(vid))
519519
})
520520
.collect::<Result<_, _>>()?;
@@ -1142,8 +1142,7 @@ pub fn verify_block_signatures(
11421142
let validator = validators
11431143
.get(vid as usize)
11441144
.ok_or(StoreError::InvalidValidatorIndex)?;
1145-
let pk = validator
1146-
.get_attestation_pubkey()
1145+
let pk = ValidatorPublicKey::from_bytes(&validator.attestation_pubkey)
11471146
.map_err(|_| StoreError::PubkeyDecodingFailed(vid))?;
11481147
pubkeys.push(pk);
11491148
}
@@ -1156,8 +1155,7 @@ pub fn verify_block_signatures(
11561155
let proposer_validator = validators
11571156
.get(block.proposer_index as usize)
11581157
.ok_or(StoreError::InvalidValidatorIndex)?;
1159-
let proposer_pubkey = proposer_validator
1160-
.get_proposal_pubkey()
1158+
let proposer_pubkey = ValidatorPublicKey::from_bytes(&proposer_validator.proposal_pubkey)
11611159
.map_err(|_| StoreError::PubkeyDecodingFailed(block.proposer_index))?;
11621160
pubkeys_per_component.push(vec![proposer_pubkey]);
11631161
let block_slot_u32 =

crates/common/crypto/src/signature.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use std::ops::Range;
55

6-
use ethlambda_types::{primitives::H256, state::Validator};
6+
use ethlambda_types::primitives::H256;
77
use leansig::{
88
serialization::Serializable,
99
signature::{SignatureScheme, SignatureSchemeSecretKey as _, SigningError},
@@ -122,26 +122,6 @@ impl ValidatorSecretKey {
122122
}
123123
}
124124

125-
/// Leansig-backed public-key access for [`Validator`].
126-
///
127-
/// `Validator` lives in `ethlambda-types`, which stays leansig-free, so these
128-
/// helpers can't be inherent methods there. Import this trait to call
129-
/// `validator.get_attestation_pubkey()` / `get_proposal_pubkey()` as before.
130-
pub trait ValidatorPubkeys {
131-
fn get_attestation_pubkey(&self) -> Result<ValidatorPublicKey, SignatureParseError>;
132-
fn get_proposal_pubkey(&self) -> Result<ValidatorPublicKey, SignatureParseError>;
133-
}
134-
135-
impl ValidatorPubkeys for Validator {
136-
fn get_attestation_pubkey(&self) -> Result<ValidatorPublicKey, SignatureParseError> {
137-
ValidatorPublicKey::from_bytes(&self.attestation_pubkey)
138-
}
139-
140-
fn get_proposal_pubkey(&self) -> Result<ValidatorPublicKey, SignatureParseError> {
141-
ValidatorPublicKey::from_bytes(&self.proposal_pubkey)
142-
}
143-
}
144-
145125
#[cfg(test)]
146126
mod tests {
147127
use super::*;

0 commit comments

Comments
 (0)