diff --git a/validator-network/src/lib.rs b/validator-network/src/lib.rs index c737c5338c..b881aaa9f4 100644 --- a/validator-network/src/lib.rs +++ b/validator-network/src/lib.rs @@ -7,9 +7,10 @@ use futures::stream::BoxStream; use nimiq_keys::{Address, KeyPair}; use nimiq_network_interface::{ network::{CloseReason, MsgAcceptance, Network, SubscribeEvents, Topic}, - request::{Message, Request, RequestCommon}, + request::{Message, Request, RequestCommon}, validator_record::ValidatorRecord, }; use nimiq_primitives::slots_allocation::Validators; +use nimiq_utils::tagged_signing::TaggedSigned; pub use crate::error::NetworkError; @@ -97,4 +98,13 @@ pub trait ValidatorNetwork: Send + Sync { /// Returns the network peer ID for the given `validator_id` if it is known. fn get_peer_id(&self, validator_id: u16) -> Option<::PeerId>; + + /// Registers a callback to produce a signed ValidatorRecord from a given peer_id and timestamp. + fn register_validator_signing_callback( + &self, + callback: impl Fn(::PeerId, u64) -> TaggedSigned::PeerId>, KeyPair> + + Send + + Sync + + 'static, + ); } diff --git a/validator-network/src/network_impl.rs b/validator-network/src/network_impl.rs index f09381c763..5642ee15fc 100644 --- a/validator-network/src/network_impl.rs +++ b/validator-network/src/network_impl.rs @@ -11,7 +11,7 @@ use nimiq_network_interface::{ }; use nimiq_primitives::slots_allocation::{Validator, Validators}; use nimiq_serde::{Deserialize, Serialize}; -use nimiq_utils::{spawn, stream::FuturesUnordered}; +use nimiq_utils::{spawn, stream::FuturesUnordered, tagged_signing::TaggedSigned}; use parking_lot::RwLock; use time::OffsetDateTime; @@ -547,4 +547,15 @@ where self.get_validator_cache(validator_id) .potentially_outdated_peer_id() } + + /// Registers a callback to produce a signed ValidatorRecord from a given peer_id and timestamp. + fn register_validator_signing_callback( + &self, + callback: impl Fn(::PeerId, u64) -> TaggedSigned::PeerId>, KeyPair> + + Send + + Sync + + 'static, + ) { + self.network.register_validator_signing_callback(callback) + } } diff --git a/validator/src/validator.rs b/validator/src/validator.rs index dde7147ffd..3d551a469e 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -31,11 +31,11 @@ use nimiq_mempool::config::MempoolConfig; use nimiq_mempool_task::MempoolTask; use nimiq_network_interface::{ network::{MsgAcceptance, Network, NetworkEvent, SubscribeEvents}, - request::request_handler, + request::request_handler, validator_record::ValidatorRecord, }; use nimiq_primitives::{coin::Coin, policy::Policy}; use nimiq_transaction_builder::TransactionBuilder; -use nimiq_utils::spawn; +use nimiq_utils::{spawn, tagged_signing::{TaggedKeyPair, TaggedSignable, TaggedSigned}}; use nimiq_validator_network::{PubsubId, ValidatorNetwork}; use parking_lot::RwLock; #[cfg(feature = "metrics")] @@ -366,6 +366,22 @@ where // Inform the network about the current validator ID. self.network.set_validator_id(*self.slot_band.read()); + let key = self.signing_key(); + let validator_address = self.validator_address(); + + self.network.register_validator_signing_callback(move |peer_id, timestamp| { + let record = ValidatorRecord { + timestamp, + peer_id, + validator_address: validator_address.clone(), + }; + + let signature = key.tagged_sign(&record); + + TaggedSigned::new(record, signature) + }); + + // Set the elected validators of the current epoch in the network as well. self.network.set_validators(validators);