Skip to content

Commit

Permalink
Validator: register callback for the validator record
Browse files Browse the repository at this point in the history
  • Loading branch information
nibhar committed Nov 25, 2024
1 parent 7491f6c commit afaa762
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
12 changes: 11 additions & 1 deletion validator-network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<<Self::NetworkType as Network>::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(<Self::NetworkType as Network>::PeerId, u64) -> TaggedSigned<ValidatorRecord<<Self::NetworkType as Network>::PeerId>, KeyPair>
+ Send
+ Sync
+ 'static,
);
}
13 changes: 12 additions & 1 deletion validator-network/src/network_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(<Self::NetworkType as Network>::PeerId, u64) -> TaggedSigned<ValidatorRecord<<Self::NetworkType as Network>::PeerId>, KeyPair>
+ Send
+ Sync
+ 'static,
) {
self.network.register_validator_signing_callback(callback)
}
}
20 changes: 18 additions & 2 deletions validator/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}};

Check warning on line 38 in validator/src/validator.rs

View workflow job for this annotation

GitHub Actions / Clippy Report

unused import: `TaggedSignable`

warning: unused import: `TaggedSignable` --> validator/src/validator.rs:38:58 | 38 | use nimiq_utils::{spawn, tagged_signing::{TaggedKeyPair, TaggedSignable, TaggedSigned}}; | ^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
use nimiq_validator_network::{PubsubId, ValidatorNetwork};
use parking_lot::RwLock;
#[cfg(feature = "metrics")]
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit afaa762

Please sign in to comment.