mirror of
https://github.com/sigp/lighthouse.git
synced 2026-05-07 00:42:42 +00:00
Add Signer to validator client
This commit is contained in:
47
eth2/block_producer/src/test_utils/beacon_node.rs
Normal file
47
eth2/block_producer/src/test_utils/beacon_node.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use crate::traits::{BeaconNode, BeaconNodeError};
|
||||
use std::sync::RwLock;
|
||||
use types::BeaconBlock;
|
||||
|
||||
type ProduceResult = Result<Option<BeaconBlock>, BeaconNodeError>;
|
||||
type PublishResult = Result<bool, BeaconNodeError>;
|
||||
|
||||
/// A test-only struct used to simulate a Beacon Node.
|
||||
#[derive(Default)]
|
||||
pub struct TestBeaconNode {
|
||||
pub produce_input: RwLock<Option<u64>>,
|
||||
pub produce_result: RwLock<Option<ProduceResult>>,
|
||||
pub publish_input: RwLock<Option<BeaconBlock>>,
|
||||
pub publish_result: RwLock<Option<PublishResult>>,
|
||||
}
|
||||
|
||||
impl TestBeaconNode {
|
||||
/// Set the result to be returned when `produce_beacon_block` is called.
|
||||
pub fn set_next_produce_result(&self, result: ProduceResult) {
|
||||
*self.produce_result.write().unwrap() = Some(result);
|
||||
}
|
||||
|
||||
/// Set the result to be returned when `publish_beacon_block` is called.
|
||||
pub fn set_next_publish_result(&self, result: PublishResult) {
|
||||
*self.publish_result.write().unwrap() = Some(result);
|
||||
}
|
||||
}
|
||||
|
||||
impl BeaconNode for TestBeaconNode {
|
||||
/// Returns the value specified by the `set_next_produce_result`.
|
||||
fn produce_beacon_block(&self, slot: u64) -> ProduceResult {
|
||||
*self.produce_input.write().unwrap() = Some(slot);
|
||||
match *self.produce_result.read().unwrap() {
|
||||
Some(ref r) => r.clone(),
|
||||
None => panic!("TestBeaconNode: produce_result == None"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the value specified by the `set_next_publish_result`.
|
||||
fn publish_beacon_block(&self, block: BeaconBlock) -> PublishResult {
|
||||
*self.publish_input.write().unwrap() = Some(block);
|
||||
match *self.publish_result.read().unwrap() {
|
||||
Some(ref r) => r.clone(),
|
||||
None => panic!("TestBeaconNode: publish_result == None"),
|
||||
}
|
||||
}
|
||||
}
|
||||
14
eth2/block_producer/src/test_utils/epoch_map.rs
Normal file
14
eth2/block_producer/src/test_utils/epoch_map.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use crate::{DutiesReader, DutiesReaderError};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub type TestEpochMap = HashMap<u64, u64>;
|
||||
|
||||
impl DutiesReader for TestEpochMap {
|
||||
fn is_block_production_slot(&self, epoch: u64, slot: u64) -> Result<bool, DutiesReaderError> {
|
||||
match self.get(&epoch) {
|
||||
Some(s) if *s == slot => Ok(true),
|
||||
Some(s) if *s != slot => Ok(false),
|
||||
_ => Err(DutiesReaderError::UnknownEpoch),
|
||||
}
|
||||
}
|
||||
}
|
||||
7
eth2/block_producer/src/test_utils/mod.rs
Normal file
7
eth2/block_producer/src/test_utils/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
mod beacon_node;
|
||||
mod epoch_map;
|
||||
mod signer;
|
||||
|
||||
pub use self::beacon_node::TestBeaconNode;
|
||||
pub use self::epoch_map::TestEpochMap;
|
||||
pub use self::signer::TestSigner;
|
||||
31
eth2/block_producer/src/test_utils/signer.rs
Normal file
31
eth2/block_producer/src/test_utils/signer.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use crate::traits::Signer;
|
||||
use std::sync::RwLock;
|
||||
use types::{Keypair, Signature};
|
||||
|
||||
/// A test-only struct used to simulate a Beacon Node.
|
||||
pub struct TestSigner {
|
||||
keypair: Keypair,
|
||||
should_sign: RwLock<bool>,
|
||||
}
|
||||
|
||||
impl TestSigner {
|
||||
/// Produce a new TestSigner with signing enabled by default.
|
||||
pub fn new(keypair: Keypair) -> Self {
|
||||
Self {
|
||||
keypair,
|
||||
should_sign: RwLock::new(true),
|
||||
}
|
||||
}
|
||||
|
||||
/// If set to `false`, the service will refuse to sign all messages. Otherwise, all messages
|
||||
/// will be signed.
|
||||
pub fn enable_signing(&self, enabled: bool) {
|
||||
*self.should_sign.write().unwrap() = enabled;
|
||||
}
|
||||
}
|
||||
|
||||
impl Signer for TestSigner {
|
||||
fn bls_sign(&self, message: &[u8]) -> Option<Signature> {
|
||||
Some(Signature::new(message, &self.keypair.sk))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user