Skip to content

Commit

Permalink
refactor: move more methods out of ValidatorSigner (#12500)
Browse files Browse the repository at this point in the history
This PR finalises the effort started in #12422: all unnecessary methods
are moved out of `ValidatorSigner` to the client code.
  • Loading branch information
pugachAG authored Nov 22, 2024
1 parent d12b9d9 commit af0c1f2
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 281 deletions.
11 changes: 3 additions & 8 deletions chain/client/src/client_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,15 +968,10 @@ impl ClientActorInner {
debug!(target: "client", "Sending announce account for {}", signer.validator_id());
self.last_validator_announce_time = Some(now);

let signature =
signer.sign_account_announce(signer.validator_id(), &self.node_id, &next_epoch_id);
let announce_account =
AnnounceAccount::new(signer.as_ref(), self.node_id.clone(), next_epoch_id);
self.network_adapter.send(PeerManagerMessageRequest::NetworkRequests(
NetworkRequests::AnnounceAccount(AnnounceAccount {
account_id: signer.validator_id().clone(),
peer_id: self.node_id.clone(),
epoch_id: next_epoch_id,
signature,
}),
NetworkRequests::AnnounceAccount(announce_account),
));
}
}
Expand Down
8 changes: 5 additions & 3 deletions chain/client/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,14 @@ impl InfoHelper {
},
extra_info: serde_json::to_string(&extra_telemetry_info(client_config)).unwrap(),
};

let mut json = serde_json::to_value(info).expect("Telemetry must serialize to JSON");
// Sign telemetry if there is a signer present.
if let Some(signer) = signer {
signer.sign_telemetry(&info)
} else {
serde_json::to_value(&info).expect("Telemetry must serialize to json")
let content = serde_json::to_string(&json).expect("Telemetry must serialize to JSON");
json["signature"] = signer.sign_bytes(content.as_bytes()).to_string().into();
}
json
}

fn log_chain_processing_info(&mut self, client: &crate::Client, epoch_id: &EpochId) {
Expand Down
14 changes: 2 additions & 12 deletions chain/network/src/network_protocol/testonly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,8 @@ pub fn make_peer_info<R: Rng>(rng: &mut R) -> PeerInfo {

pub fn make_announce_account<R: Rng>(rng: &mut R) -> AnnounceAccount {
let peer_id = make_peer_id(rng);
let validator_signer = make_validator_signer(rng);
let signature = validator_signer.sign_account_announce(
validator_signer.validator_id(),
&peer_id,
&EpochId::default(),
);
AnnounceAccount {
account_id: validator_signer.validator_id().clone(),
peer_id: peer_id,
epoch_id: EpochId::default(),
signature,
}
let validator_signer = ValidatorSigner::InMemory(make_validator_signer(rng));
AnnounceAccount::new(&validator_signer, peer_id, EpochId::default())
}

pub fn make_partial_edge<R: Rng>(rng: &mut R) -> PartialEdgeInfo {
Expand Down
2 changes: 1 addition & 1 deletion chain/network/src/peer_manager/tests/tier1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn make_block_approval(rng: &mut Rng, signer: &ValidatorSigner) -> Approval {
let inner = ApprovalInner::Endorsement(data::make_hash(rng));
let target_height = rng.gen_range(0..100000);
Approval {
signature: signer.sign_approval(&inner, target_height),
signature: signer.sign_bytes(&Approval::get_data_for_sig(&inner, target_height)),
account_id: signer.validator_id().clone(),
target_height,
inner,
Expand Down
23 changes: 9 additions & 14 deletions core/primitives/src/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ impl Approval {
signer: &ValidatorSigner,
) -> Self {
let inner = ApprovalInner::new(&parent_hash, parent_height, target_height);
let signature = signer.sign_approval(&inner, target_height);

let signature = signer.sign_bytes(&Approval::get_data_for_sig(&inner, target_height));
Approval { inner, target_height, signature, account_id: signer.validator_id().clone() }
}

Expand Down Expand Up @@ -991,20 +992,14 @@ impl BlockHeader {
where
T: BorshSerialize + ?Sized,
{
let hash = BlockHeader::compute_hash(
prev_hash,
&borsh::to_vec(&inner_lite).expect("Failed to serialize"),
&borsh::to_vec(&inner_rest).expect("Failed to serialize"),
);
match signature_source {
SignatureSource::Signer(signer) => signer.sign_block_header_parts(
prev_hash,
&borsh::to_vec(&inner_lite).expect("Failed to serialize"),
&borsh::to_vec(&inner_rest).expect("Failed to serialize"),
),
SignatureSource::Signature(signature) => {
let hash = BlockHeader::compute_hash(
prev_hash,
&borsh::to_vec(&inner_lite).expect("Failed to serialize"),
&borsh::to_vec(&inner_rest).expect("Failed to serialize"),
);
(hash, signature)
}
SignatureSource::Signer(signer) => (hash, signer.sign_bytes(hash.as_ref())),
SignatureSource::Signature(signature) => (hash, signature),
}
}

Expand Down
3 changes: 2 additions & 1 deletion core/primitives/src/challenge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ impl Challenge {
}

pub fn produce(body: ChallengeBody, signer: &ValidatorSigner) -> Self {
let (hash, signature) = signer.sign_challenge(&body);
let hash = CryptoHash::hash_borsh(&body);
let signature = signer.sign_bytes(hash.as_ref());
Self { body, account_id: signer.validator_id().clone(), signature, hash }
}
}
Expand Down
21 changes: 16 additions & 5 deletions core/primitives/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::hash::CryptoHash;
use crate::types::{AccountId, EpochId};
use crate::validator_signer::ValidatorSigner;
use borsh::{BorshDeserialize, BorshSerialize};
use near_crypto::{PublicKey, Signature};
use near_schema_checker_lib::ProtocolSchema;
Expand Down Expand Up @@ -67,17 +68,27 @@ pub struct AnnounceAccount {
}

impl AnnounceAccount {
pub fn new(signer: &ValidatorSigner, peer_id: PeerId, epoch_id: EpochId) -> Self {
let signature = Self::sign(signer, &peer_id, &epoch_id);
Self { account_id: signer.validator_id().clone(), peer_id: peer_id, epoch_id, signature }
}

pub fn hash(&self) -> CryptoHash {
Self::build_header_hash(&self.account_id, &self.peer_id, &self.epoch_id)
}

fn sign(signer: &ValidatorSigner, peer_id: &PeerId, epoch_id: &EpochId) -> Signature {
let hash = Self::build_header_hash(signer.validator_id(), peer_id, epoch_id);
signer.sign_bytes(hash.as_ref())
}

/// We hash only (account_id, peer_id, epoch_id). There is no need hash the signature
/// as it's uniquely determined the triple.
pub fn build_header_hash(
fn build_header_hash(
account_id: &AccountId,
peer_id: &PeerId,
epoch_id: &EpochId,
) -> CryptoHash {
CryptoHash::hash_borsh((account_id, peer_id, epoch_id))
}

pub fn hash(&self) -> CryptoHash {
AnnounceAccount::build_header_hash(&self.account_id, &self.peer_id, &self.epoch_id)
}
}
6 changes: 3 additions & 3 deletions core/primitives/src/sharding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ impl ShardChunkHeaderV2 {
prev_validator_proposals,
};
let hash = Self::compute_hash(&inner);
let signature = signer.sign_chunk_hash(&hash);
let signature = signer.sign_bytes(hash.as_ref());
Self { inner, height_included: 0, signature, hash }
}
}
Expand Down Expand Up @@ -331,7 +331,7 @@ impl ShardChunkHeaderV3 {

pub fn from_inner(inner: ShardChunkHeaderInner, signer: &ValidatorSigner) -> Self {
let hash = Self::compute_hash(&inner);
let signature = signer.sign_chunk_hash(&hash);
let signature = signer.sign_bytes(hash.as_ref());
Self { inner, height_included: 0, signature, hash }
}
}
Expand Down Expand Up @@ -690,7 +690,7 @@ impl ShardChunkHeaderV1 {
prev_validator_proposals,
};
let hash = Self::compute_hash(&inner);
let signature = signer.sign_chunk_hash(&hash);
let signature = signer.sign_bytes(hash.as_ref());
Self { inner, height_included: 0, signature, hash }
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/primitives/src/stateless_validation/chunk_endorsement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl ChunkEndorsement {
epoch_id,
height_created: chunk_header.height_created(),
};
let signature = signer.sign_chunk_endorsement(&inner);
let metadata_signature = signer.sign_chunk_endorsement_metadata(&metadata);
let signature = signer.sign_bytes(&borsh::to_vec(&inner).unwrap());
let metadata_signature = signer.sign_bytes(&borsh::to_vec(&metadata).unwrap());
let endorsement = ChunkEndorsementV2 { inner, signature, metadata, metadata_signature };
ChunkEndorsement::V2(endorsement)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl PartialEncodedStateWitness {
part,
encoded_length,
);
let signature = signer.sign_partial_encoded_state_witness(&inner);
let signature = signer.sign_bytes(&borsh::to_vec(&inner).unwrap());
Self { inner, signature }
}

Expand Down
3 changes: 2 additions & 1 deletion core/primitives/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,12 @@ impl BlockHeader {
}

pub fn resign(&mut self, signer: &ValidatorSigner) {
let (hash, signature) = signer.sign_block_header_parts(
let hash = BlockHeader::compute_hash(
*self.prev_hash(),
&self.inner_lite_bytes(),
&self.inner_rest_bytes(),
);
let signature = signer.sign_bytes(hash.as_ref());
match self {
BlockHeader::BlockHeaderV1(header) => {
let header = Arc::make_mut(header);
Expand Down
Loading

0 comments on commit af0c1f2

Please sign in to comment.