Skip to content

Commit 9ace072

Browse files
committed
fix: sign Attestation instead of AttestationData
1 parent c274eb1 commit 9ace072

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

crates/blockchain/src/key_manager.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::collections::HashMap;
22

33
use ethlambda_types::{
4-
attestation::XmssSignature,
5-
primitives::H256,
4+
attestation::{Attestation, XmssSignature},
5+
primitives::{H256, TreeHash},
66
signature::{ValidatorSecretKey, ValidatorSignature},
77
};
88

@@ -51,7 +51,7 @@ impl KeyManager {
5151
self.keys.keys().copied().collect()
5252
}
5353

54-
/// Signs an attestation message for the specified validator.
54+
/// Signs a message for the specified validator.
5555
///
5656
/// # Arguments
5757
///
@@ -68,13 +68,13 @@ impl KeyManager {
6868
/// # Example
6969
///
7070
/// ```ignore
71-
/// let signature = key_manager.sign_attestation(
71+
/// let signature = key_manager.sign_message(
7272
/// validator_id,
7373
/// epoch,
7474
/// &message_hash
7575
/// )?;
7676
/// ```
77-
pub fn sign_attestation(
77+
fn sign_message(
7878
&mut self,
7979
validator_id: u64,
8080
epoch: u32,
@@ -94,8 +94,22 @@ impl KeyManager {
9494
let xmss_sig = XmssSignature::try_from(sig_bytes)
9595
.map_err(|e| KeyManagerError::SignatureConversionError(e.to_string()))?;
9696

97+
tracing::info!(
98+
%validator_id, %epoch, %message, ?xmss_sig,
99+
"Publishing attestation",
100+
);
101+
97102
Ok(xmss_sig)
98103
}
104+
105+
pub fn sign_attestation(
106+
&mut self,
107+
attestation: &Attestation,
108+
) -> Result<XmssSignature, KeyManagerError> {
109+
let message_hash = attestation.tree_hash_root();
110+
let epoch = attestation.data.slot as u32;
111+
self.sign_message(attestation.validator_id, epoch, &message_hash)
112+
}
99113
}
100114

101115
#[cfg(test)]
@@ -110,12 +124,12 @@ mod tests {
110124
}
111125

112126
#[test]
113-
fn test_sign_attestation_validator_not_found() {
127+
fn test_sign_message_validator_not_found() {
114128
let keys = HashMap::new();
115129
let mut key_manager = KeyManager::new(keys);
116130
let message = H256::default();
117131

118-
let result = key_manager.sign_attestation(123, 0, &message);
132+
let result = key_manager.sign_message(123, 0, &message);
119133
assert!(matches!(
120134
result,
121135
Err(KeyManagerError::ValidatorKeyNotFound(123))

crates/blockchain/src/lib.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,6 @@ impl BlockChainServer {
144144
// Produce attestation data once for all validators
145145
let attestation_data = self.store.produce_attestation_data(slot);
146146

147-
// Hash the attestation data for signing
148-
let message_hash = attestation_data.tree_hash_root();
149-
150-
// Epoch for signing
151-
let epoch = slot as u32;
152-
153147
// For each registered validator, produce and publish attestation
154148
for validator_id in self.key_manager.validator_ids() {
155149
// Skip if this validator is the slot proposer
@@ -158,14 +152,16 @@ impl BlockChainServer {
158152
continue;
159153
}
160154

155+
// Hash the attestation data for signing
156+
let attestation = Attestation {
157+
data: attestation_data.clone(),
158+
validator_id,
159+
};
160+
161161
// Sign the attestation
162-
let Ok(signature) = self
163-
.key_manager
164-
.sign_attestation(validator_id, epoch, &message_hash)
165-
.inspect_err(
166-
|err| error!(%slot, %validator_id, %err, "Failed to sign attestation"),
167-
)
168-
else {
162+
let Ok(signature) = self.key_manager.sign_attestation(&attestation).inspect_err(
163+
|err| error!(%slot, %validator_id, %err, "Failed to sign attestation"),
164+
) else {
169165
continue;
170166
};
171167

@@ -218,11 +214,9 @@ impl BlockChainServer {
218214
};
219215

220216
// Sign the proposer's attestation
221-
let message_hash = proposer_attestation.data.tree_hash_root();
222-
let epoch = slot as u32;
223217
let Ok(proposer_signature) = self
224218
.key_manager
225-
.sign_attestation(validator_id, epoch, &message_hash)
219+
.sign_attestation(&proposer_attestation)
226220
.inspect_err(
227221
|err| error!(%slot, %validator_id, %err, "Failed to sign proposer attestation"),
228222
)
@@ -233,9 +227,8 @@ impl BlockChainServer {
233227
// Assemble flat signature list: [attestation_sig_0, ..., attestation_sig_n, proposer_sig]
234228
let mut signatures = attestation_signatures;
235229
signatures.push(proposer_signature);
236-
let block_signatures: BlockSignatures = signatures
237-
.try_into()
238-
.expect("signatures within limit");
230+
let block_signatures: BlockSignatures =
231+
signatures.try_into().expect("signatures within limit");
239232

240233
// Assemble SignedBlockWithAttestation
241234
let signed_block = SignedBlockWithAttestation {

0 commit comments

Comments
 (0)