forked from massalabs/massa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mock for Selector and start for pool.
- Loading branch information
1 parent
d437ac5
commit a13d61e
Showing
18 changed files
with
160 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,4 @@ pub use types::*; | |
|
||
//TODO: readd | ||
//#[cfg(feature = "testing")] | ||
pub mod test_exports; | ||
pub mod test_exports; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,4 @@ impl Default for FactoryConfig { | |
max_block_gas: MAX_GAS_PER_BLOCK, | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
|
||
mod config; | ||
pub use config::*; | ||
pub use config::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,4 @@ mod run; | |
pub use run::start_factory; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
mod scenarios; | ||
mod scenarios; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod config; | ||
|
||
pub use config::*; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,125 @@ | ||
use std::sync::{ | ||
mpsc::{self, Receiver}, | ||
Arc, Mutex, | ||
}; | ||
|
||
use anyhow::Result; | ||
use massa_models::{api::IndexedSlot, Address, Slot}; | ||
|
||
use crate::{CycleInfo, SelectorController, Selection}; | ||
|
||
/// All events that can be sent by the selector to your callbacks. | ||
pub enum MockSelectorControllerMessage { | ||
/// Feed a new cycle info to the selector | ||
FeedCycle { | ||
/// cycle infos | ||
cycle_info: CycleInfo, | ||
}, | ||
/// Get a list of slots where address has been chosen to produce a block and a list where he is chosen for the endorsements. | ||
/// Look from the start slot to the end slot. | ||
GetAddressSelections { | ||
/// Address to search | ||
address: Address, | ||
/// Start of the search range | ||
start: Slot, | ||
/// End of the search range | ||
end: Slot, | ||
/// Receiver to send the result to | ||
response_tx: mpsc::Sender<(Vec<Slot>, Vec<IndexedSlot>)>, | ||
}, | ||
/// Get the producer for a block at a specific slot | ||
GetProducer { | ||
/// Slot to search | ||
slot: Slot, | ||
/// Receiver to send the result to | ||
response_tx: mpsc::Sender<Result<Address>>, | ||
}, | ||
/// Get the selection for a block at a specific slot | ||
GetSelection { | ||
/// Slot to search | ||
slot: Slot, | ||
/// Receiver to send the result to | ||
response_tx: mpsc::Sender<Result<Selection>>, | ||
} | ||
} | ||
|
||
/// Mock implementation of the SelectorController trait. | ||
/// This mock will be called by the others modules and you will receive events in the receiver. | ||
/// You can choose to manage them how you want. | ||
#[derive(Clone)] | ||
pub struct MockSelectorController(Arc<Mutex<mpsc::Sender<MockSelectorControllerMessage>>>); | ||
|
||
impl MockSelectorController { | ||
/// Create a new pair (mock execution controller, mpsc receiver for emitted messages) | ||
/// Note that unbounded mpsc channels are used | ||
pub fn new_with_receiver() -> ( | ||
Box<dyn SelectorController>, | ||
Receiver<MockSelectorControllerMessage>, | ||
) { | ||
let (tx, rx) = mpsc::channel(); | ||
( | ||
Box::new(MockSelectorController(Arc::new(Mutex::new(tx)))), | ||
rx, | ||
) | ||
} | ||
} | ||
|
||
impl SelectorController for MockSelectorController { | ||
fn feed_cycle(&self, cycle_info: CycleInfo) { | ||
self.0 | ||
.lock() | ||
.unwrap() | ||
.send(MockSelectorControllerMessage::FeedCycle { cycle_info }) | ||
.unwrap(); | ||
} | ||
|
||
fn get_address_selections( | ||
&self, | ||
address: &Address, | ||
start: Slot, | ||
end: Slot, | ||
) -> (Vec<Slot>, Vec<IndexedSlot>) { | ||
let (response_tx, response_rx) = mpsc::channel(); | ||
self.0 | ||
.lock() | ||
.unwrap() | ||
.send(MockSelectorControllerMessage::GetAddressSelections { | ||
address: *address, | ||
start, | ||
end, | ||
response_tx, | ||
}) | ||
.unwrap(); | ||
response_rx.recv().unwrap() | ||
} | ||
|
||
fn get_producer(&self, slot: Slot) -> Result<Address> { | ||
let (response_tx, response_rx) = mpsc::channel(); | ||
self.0 | ||
.lock() | ||
.unwrap() | ||
.send(MockSelectorControllerMessage::GetProducer { | ||
slot, | ||
response_tx, | ||
}) | ||
.unwrap(); | ||
response_rx.recv().unwrap() | ||
} | ||
|
||
fn get_selection(&self, slot: Slot) -> Result<Selection> { | ||
let (response_tx, response_rx) = mpsc::channel(); | ||
self.0 | ||
.lock() | ||
.unwrap() | ||
.send(MockSelectorControllerMessage::GetSelection { | ||
slot, | ||
response_tx, | ||
}) | ||
.unwrap(); | ||
response_rx.recv().unwrap() | ||
} | ||
|
||
fn clone_box(&self) -> Box<dyn SelectorController> { | ||
Box::new(self.clone()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ mod bootstrap; | |
mod mock; | ||
|
||
pub use bootstrap::*; | ||
pub use mock::*; | ||
pub use mock::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
mod tools; | ||
|
||
pub use tools::*; | ||
pub use tools::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters