Skip to content

Commit

Permalink
Use ValidatorPublicKey instead of ValidatorName
Browse files Browse the repository at this point in the history
  • Loading branch information
deuszx committed Feb 19, 2025
1 parent 1505022 commit 2061f04
Show file tree
Hide file tree
Showing 37 changed files with 199 additions and 200 deletions.
4 changes: 2 additions & 2 deletions linera-base/src/crypto/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};

use super::{
le_bytes_to_u64_array, u64_array_to_le_bytes, BcsHashable, BcsSignable, CryptoError,
CryptoHash, HasTypeName, Hashable,
CryptoHash, HasTypeName, Hashable, ValidatorPublicKey, ValidatorSignature,
};
use crate::{doc_scalar, identifiers::Owner};

Expand Down Expand Up @@ -330,7 +330,7 @@ impl Ed25519Signature {
pub fn verify_batch<'a, 'de, T, I>(value: &'a T, votes: I) -> Result<(), CryptoError>
where
T: BcsSignable<'de>,
I: IntoIterator<Item = (&'a Ed25519PublicKey, &'a Ed25519Signature)>,
I: IntoIterator<Item = (&'a ValidatorPublicKey, &'a ValidatorSignature)>,
{
Ed25519Signature::verify_batch_internal(value, votes).map_err(|error| {
CryptoError::InvalidSignature {
Expand Down
6 changes: 3 additions & 3 deletions linera-chain/src/certificate/confirmed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// SPDX-License-Identifier: Apache-2.0

use linera_base::{
crypto::ValidatorSignature,
crypto::{ValidatorPublicKey, ValidatorSignature},
data_types::Round,
hashed::Hashed,
identifiers::{BlobId, ChainId, MessageId},
};
use linera_execution::committee::{Epoch, ValidatorName};
use linera_execution::committee::Epoch;
use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize};

use super::{generic::GenericCertificate, Certificate};
Expand Down Expand Up @@ -92,7 +92,7 @@ impl<'de> Deserialize<'de> for GenericCertificate<ConfirmedBlock> {
struct Helper {
value: Hashed<ConfirmedBlock>,
round: Round,
signatures: Vec<(ValidatorName, ValidatorSignature)>,
signatures: Vec<(ValidatorPublicKey, ValidatorSignature)>,
}

let helper = Helper::deserialize(deserializer)?;
Expand Down
26 changes: 16 additions & 10 deletions linera-chain/src/certificate/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use custom_debug_derive::Debug;
use linera_base::{
crypto::{CryptoHash, ValidatorSignature},
crypto::{CryptoHash, ValidatorPublicKey, ValidatorSignature},
data_types::Round,
hashed::Hashed,
};
use linera_execution::committee::{Committee, ValidatorName};
use linera_execution::committee::Committee;

use super::CertificateValue;
use crate::{data_types::LiteValue, ChainError};
Expand All @@ -18,14 +18,14 @@ use crate::{data_types::LiteValue, ChainError};
pub struct GenericCertificate<T> {
value: Hashed<T>,
pub round: Round,
signatures: Vec<(ValidatorName, ValidatorSignature)>,
signatures: Vec<(ValidatorPublicKey, ValidatorSignature)>,
}

impl<T> GenericCertificate<T> {
pub fn new(
value: Hashed<T>,
round: Round,
mut signatures: Vec<(ValidatorName, ValidatorSignature)>,
mut signatures: Vec<(ValidatorPublicKey, ValidatorSignature)>,
) -> Self {
signatures.sort_by_key(|&(validator_name, _)| validator_name);

Expand Down Expand Up @@ -61,25 +61,31 @@ impl<T> GenericCertificate<T> {
self.value.hash()
}

pub fn destructure(self) -> (Hashed<T>, Round, Vec<(ValidatorName, ValidatorSignature)>) {
pub fn destructure(
self,
) -> (
Hashed<T>,
Round,
Vec<(ValidatorPublicKey, ValidatorSignature)>,
) {
(self.value, self.round, self.signatures)
}

pub fn signatures(&self) -> &Vec<(ValidatorName, ValidatorSignature)> {
pub fn signatures(&self) -> &Vec<(ValidatorPublicKey, ValidatorSignature)> {
&self.signatures
}

#[cfg(with_testing)]
pub fn signatures_mut(&mut self) -> &mut Vec<(ValidatorName, ValidatorSignature)> {
pub fn signatures_mut(&mut self) -> &mut Vec<(ValidatorPublicKey, ValidatorSignature)> {
&mut self.signatures
}

/// Adds a signature to the certificate's list of signatures
/// It's the responsibility of the caller to not insert duplicates
pub fn add_signature(
&mut self,
signature: (ValidatorName, ValidatorSignature),
) -> &Vec<(ValidatorName, ValidatorSignature)> {
signature: (ValidatorPublicKey, ValidatorSignature),
) -> &Vec<(ValidatorPublicKey, ValidatorSignature)> {
let index = self
.signatures
.binary_search_by(|(name, _)| name.cmp(&signature.0))
Expand All @@ -89,7 +95,7 @@ impl<T> GenericCertificate<T> {
}

/// Returns whether the validator is among the signatories of this certificate.
pub fn is_signed_by(&self, validator_name: &ValidatorName) -> bool {
pub fn is_signed_by(&self, validator_name: &ValidatorPublicKey) -> bool {
self.signatures
.binary_search_by(|(name, _)| name.cmp(validator_name))
.is_ok()
Expand Down
12 changes: 8 additions & 4 deletions linera-chain/src/certificate/lite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

use std::borrow::Cow;

use linera_base::{crypto::ValidatorSignature, data_types::Round, hashed::Hashed};
use linera_execution::committee::{Committee, ValidatorName};
use linera_base::{
crypto::{ValidatorPublicKey, ValidatorSignature},
data_types::Round,
hashed::Hashed,
};
use linera_execution::committee::Committee;
use serde::{Deserialize, Serialize};

use super::{CertificateValue, GenericCertificate};
Expand All @@ -23,14 +27,14 @@ pub struct LiteCertificate<'a> {
/// The round in which the value was certified.
pub round: Round,
/// Signatures on the value.
pub signatures: Cow<'a, [(ValidatorName, ValidatorSignature)]>,
pub signatures: Cow<'a, [(ValidatorPublicKey, ValidatorSignature)]>,
}

impl<'a> LiteCertificate<'a> {
pub fn new(
value: LiteValue,
round: Round,
mut signatures: Vec<(ValidatorName, ValidatorSignature)>,
mut signatures: Vec<(ValidatorPublicKey, ValidatorSignature)>,
) -> Self {
signatures.sort_by_key(|&(validator_name, _)| validator_name);

Expand Down
6 changes: 3 additions & 3 deletions linera-chain/src/certificate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use std::collections::BTreeSet;

pub use generic::GenericCertificate;
use linera_base::{
crypto::ValidatorSignature,
crypto::{ValidatorPublicKey, ValidatorSignature},
data_types::{BlockHeight, Round},
identifiers::{BlobId, ChainId},
};
use linera_execution::committee::{Epoch, ValidatorName};
use linera_execution::committee::Epoch;
pub use lite::LiteCertificate;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -76,7 +76,7 @@ impl Certificate {
}
}

pub fn signatures(&self) -> &Vec<(ValidatorName, ValidatorSignature)> {
pub fn signatures(&self) -> &Vec<(ValidatorPublicKey, ValidatorSignature)> {
match self {
Certificate::Validated(cert) => cert.signatures(),
Certificate::Confirmed(cert) => cert.signatures(),
Expand Down
9 changes: 6 additions & 3 deletions linera-chain/src/certificate/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use linera_base::{crypto::ValidatorSignature, data_types::Round, hashed::Hashed};
use linera_execution::committee::ValidatorName;
use linera_base::{
crypto::{ValidatorPublicKey, ValidatorSignature},
data_types::Round,
hashed::Hashed,
};
use serde::{
ser::{Serialize, SerializeStruct, Serializer},
Deserialize, Deserializer,
Expand Down Expand Up @@ -49,7 +52,7 @@ impl<'de> Deserialize<'de> for GenericCertificate<Timeout> {
struct Inner {
value: Hashed<Timeout>,
round: Round,
signatures: Vec<(ValidatorName, ValidatorSignature)>,
signatures: Vec<(ValidatorPublicKey, ValidatorSignature)>,
}
let inner = Inner::deserialize(deserializer)?;
if !crate::data_types::is_strictly_ordered(&inner.signatures) {
Expand Down
8 changes: 5 additions & 3 deletions linera-chain/src/certificate/validated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
// SPDX-License-Identifier: Apache-2.0

use linera_base::{
crypto::ValidatorSignature, data_types::Round, hashed::Hashed, identifiers::BlobId,
crypto::{ValidatorPublicKey, ValidatorSignature},
data_types::Round,
hashed::Hashed,
identifiers::BlobId,
};
use linera_execution::committee::ValidatorName;
use serde::{
ser::{Serialize, SerializeStruct, Serializer},
Deserialize, Deserializer,
Expand Down Expand Up @@ -67,7 +69,7 @@ impl<'de> Deserialize<'de> for GenericCertificate<ValidatedBlock> {
struct Inner {
value: Hashed<ValidatedBlock>,
round: Round,
signatures: Vec<(ValidatorName, ValidatorSignature)>,
signatures: Vec<(ValidatorPublicKey, ValidatorSignature)>,
}
let inner = Inner::deserialize(deserializer)?;
if !crate::data_types::is_strictly_ordered(&inner.signatures) {
Expand Down
8 changes: 4 additions & 4 deletions linera-chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
use async_graphql::SimpleObject;
use futures::stream::{self, StreamExt, TryStreamExt};
use linera_base::{
crypto::CryptoHash,
crypto::{CryptoHash, ValidatorPublicKey},
data_types::{
Amount, ArithmeticError, BlockHeight, OracleResponse, Timestamp, UserApplicationDescription,
},
Expand All @@ -23,7 +23,7 @@ use linera_base::{
ownership::ChainOwnership,
};
use linera_execution::{
committee::{Committee, Epoch, ValidatorName},
committee::{Committee, Epoch},
system::OpenChainConfig,
ExecutionOutcome, ExecutionRuntimeContext, ExecutionStateView, Message, MessageContext,
Operation, OperationContext, Query, QueryContext, QueryOutcome, RawExecutionOutcome,
Expand Down Expand Up @@ -212,7 +212,7 @@ where
/// Sender chain and height of all certified blocks known as a receiver (local ordering).
pub received_log: LogView<C, ChainAndHeight>,
/// The number of `received_log` entries we have synchronized, for each validator.
pub received_certificate_trackers: RegisterView<C, HashMap<ValidatorName, u64>>,
pub received_certificate_trackers: RegisterView<C, HashMap<ValidatorPublicKey, u64>>,

/// Mailboxes used to receive messages indexed by their origin.
pub inboxes: ReentrantCollectionView<C, Origin, InboxStateView<C>>,
Expand Down Expand Up @@ -549,7 +549,7 @@ where
/// Updates the `received_log` trackers.
pub fn update_received_certificate_trackers(
&mut self,
new_trackers: BTreeMap<ValidatorName, u64>,
new_trackers: BTreeMap<ValidatorPublicKey, u64>,
) {
for (name, tracker) in new_trackers {
self.received_certificate_trackers
Expand Down
26 changes: 13 additions & 13 deletions linera-chain/src/data_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use linera_base::{
bcs,
crypto::{
AccountPublicKey, AccountSecretKey, AccountSignature, BcsHashable, BcsSignable,
CryptoError, CryptoHash, ValidatorSecretKey, ValidatorSignature,
CryptoError, CryptoHash, ValidatorPublicKey, ValidatorSecretKey, ValidatorSignature,
},
data_types::{Amount, BlockHeight, Event, OracleResponse, Round, Timestamp},
doc_scalar, ensure,
Expand All @@ -23,7 +23,7 @@ use linera_base::{
},
};
use linera_execution::{
committee::{Committee, Epoch, ValidatorName},
committee::{Committee, Epoch},
system::OpenChainConfig,
Message, MessageKind, Operation, SystemMessage, SystemOperation,
};
Expand Down Expand Up @@ -424,7 +424,7 @@ struct VoteValue(CryptoHash, Round, CertificateKind);
pub struct Vote<T> {
pub value: Hashed<T>,
pub round: Round,
pub validator: ValidatorName,
pub validator: ValidatorPublicKey,
pub signature: ValidatorSignature,
}

Expand All @@ -439,7 +439,7 @@ impl<T> Vote<T> {
Self {
value,
round,
validator: ValidatorName(key_pair.public()),
validator: key_pair.public(),
signature,
}
}
Expand Down Expand Up @@ -469,7 +469,7 @@ impl<T> Vote<T> {
pub struct LiteVote {
pub value: LiteValue,
pub round: Round,
pub validator: ValidatorName,
pub validator: ValidatorPublicKey,
pub signature: ValidatorSignature,
}

Expand Down Expand Up @@ -816,22 +816,22 @@ impl LiteVote {
Self {
value,
round,
validator: ValidatorName(key_pair.public()),
validator: key_pair.public(),
signature,
}
}

/// Verifies the signature in the vote.
pub fn check(&self) -> Result<(), ChainError> {
let hash_and_round = VoteValue(self.value.value_hash, self.round, self.value.kind);
Ok(self.signature.check(&hash_and_round, self.validator.0)?)
Ok(self.signature.check(&hash_and_round, self.validator)?)
}
}

pub struct SignatureAggregator<'a, T> {
committee: &'a Committee,
weight: u64,
used_validators: HashSet<ValidatorName>,
used_validators: HashSet<ValidatorPublicKey>,
partial: GenericCertificate<T>,
}

Expand All @@ -851,14 +851,14 @@ impl<'a, T> SignatureAggregator<'a, T> {
/// of `check` below. Returns an error if the signed value cannot be aggregated.
pub fn append(
&mut self,
validator: ValidatorName,
validator: ValidatorPublicKey,
signature: ValidatorSignature,
) -> Result<Option<GenericCertificate<T>>, ChainError>
where
T: CertificateValue,
{
let hash_and_round = VoteValue(self.partial.hash(), self.partial.round, T::KIND);
signature.check(&hash_and_round, validator.0)?;
signature.check(&hash_and_round, validator)?;
// Check that each validator only appears once.
ensure!(
!self.used_validators.contains(&validator),
Expand All @@ -883,7 +883,7 @@ impl<'a, T> SignatureAggregator<'a, T> {

// Checks if the array slice is strictly ordered. That means that if the array
// has duplicates, this will return False, even if the array is sorted
pub(crate) fn is_strictly_ordered(values: &[(ValidatorName, ValidatorSignature)]) -> bool {
pub(crate) fn is_strictly_ordered(values: &[(ValidatorPublicKey, ValidatorSignature)]) -> bool {
values.windows(2).all(|pair| pair[0].0 < pair[1].0)
}

Expand All @@ -892,7 +892,7 @@ pub(crate) fn check_signatures(
value_hash: CryptoHash,
certificate_kind: CertificateKind,
round: Round,
signatures: &[(ValidatorName, ValidatorSignature)],
signatures: &[(ValidatorPublicKey, ValidatorSignature)],
committee: &Committee,
) -> Result<(), ChainError> {
// Check the quorum.
Expand All @@ -916,7 +916,7 @@ pub(crate) fn check_signatures(
);
// All that is left is checking signatures!
let hash_and_round = VoteValue(value_hash, round, certificate_kind);
ValidatorSignature::verify_batch(&hash_and_round, signatures.iter().map(|(v, s)| (&v.0, s)))?;
ValidatorSignature::verify_batch(&hash_and_round, signatures.iter().map(|(v, s)| (v, s)))?;
Ok(())
}

Expand Down
6 changes: 3 additions & 3 deletions linera-chain/src/unit_tests/chain_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use linera_base::{
ownership::ChainOwnership,
};
use linera_execution::{
committee::{Committee, Epoch, ValidatorName, ValidatorState},
committee::{Committee, Epoch, ValidatorState},
system::{OpenChainConfig, Recipient},
test_utils::{ExpectedCall, MockApplication},
ExecutionError, ExecutionRuntimeConfig, ExecutionRuntimeContext, Message, MessageKind,
Expand Down Expand Up @@ -93,7 +93,7 @@ fn make_admin_message_id(height: BlockHeight) -> MessageId {
}

fn make_open_chain_config() -> OpenChainConfig {
let committee = Committee::make_simple(vec![ValidatorPublicKey::test_key(1).into()]);
let committee = Committee::make_simple(vec![ValidatorPublicKey::test_key(1)]);
OpenChainConfig {
ownership: ChainOwnership::single(AccountPublicKey::test_key(0).into()),
admin_id: admin_id(),
Expand All @@ -120,7 +120,7 @@ async fn test_block_size_limit() {
Epoch(0),
Committee::new(
BTreeMap::from([(
ValidatorName(ValidatorPublicKey::test_key(1)),
ValidatorPublicKey::test_key(1),
ValidatorState {
network_address: ValidatorPublicKey::test_key(1).to_string(),
votes: 1,
Expand Down
Loading

0 comments on commit 2061f04

Please sign in to comment.