Skip to content

Commit

Permalink
feat: add emily to context (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
matteojug authored Oct 3, 2024
1 parent 2fa9ce6 commit 3b73797
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 92 deletions.
4 changes: 4 additions & 0 deletions signer/src/block_observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ mod tests {
storage.clone(),
test_harness.clone(),
test_harness.clone(),
test_harness.clone(),
);
// There must be at least one signal receiver alive when the block observer
// later tries to send a signal, hence this line.
Expand Down Expand Up @@ -500,6 +501,7 @@ mod tests {
storage.clone(),
test_harness.clone(),
test_harness.clone(),
test_harness.clone(),
);

let mut block_observer = BlockObserver {
Expand Down Expand Up @@ -578,6 +580,7 @@ mod tests {
storage.clone(),
test_harness.clone(),
test_harness.clone(),
test_harness.clone(),
);

let mut block_observer = BlockObserver {
Expand Down Expand Up @@ -647,6 +650,7 @@ mod tests {
storage.clone(),
test_harness.clone(),
test_harness.clone(),
test_harness.clone(),
);

// Now let's create two transactions, one spending to the signers
Expand Down
35 changes: 27 additions & 8 deletions signer/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use url::Url;

use crate::bitcoin::BitcoinInteract;
use crate::config::Settings;
use crate::emily_client::EmilyInteract;
use crate::error::Error;
use crate::stacks::api::StacksInteract;
use crate::storage::DbRead;
Expand Down Expand Up @@ -36,12 +37,14 @@ pub trait Context: Clone + Sync + Send {
fn get_bitcoin_client(&self) -> impl BitcoinInteract + Clone;
/// Get a handler to the Stacks client.
fn get_stacks_client(&self) -> impl StacksInteract + Clone;
/// Get a handle to a Emily client.
fn get_emily_client(&self) -> impl EmilyInteract + Clone;
}

/// Signer context which is passed to different components within the
/// signer binary.
#[derive(Debug, Clone)]
pub struct SignerContext<S, BC, ST> {
pub struct SignerContext<S, BC, ST, EM> {
config: Settings,
// Handle to the app signalling channel. This keeps the channel alive
// for the duration of the program and is used both to send messages
Expand All @@ -60,38 +63,48 @@ pub struct SignerContext<S, BC, ST> {
// count down.
/// Handle to a Stacks-RPC fallback-client.
stacks_client: ST,
// /// Handle to a Emily-API fallback-client.
//emily_client: ApiFallbackClient<EM>,
/// Handle to a Emily-API fallback-client.
emily_client: EM,
// /// Handle to a Blocklist-API fallback-client.
//blocklist_client: ApiFallbackClient<BL>,
}

impl<S, BC, ST> SignerContext<S, BC, ST>
impl<S, BC, ST, EM> SignerContext<S, BC, ST, EM>
where
S: DbRead + DbWrite + Clone + Sync + Send,
BC: for<'a> TryFrom<&'a [Url]> + BitcoinInteract + Clone + 'static,
ST: for<'a> TryFrom<&'a Settings> + StacksInteract + Clone + Sync + Send + 'static,
EM: for<'a> TryFrom<&'a [Url]> + EmilyInteract + Clone + Sync + Send + 'static,
Error: for<'a> From<<BC as TryFrom<&'a [Url]>>::Error>,
Error: for<'a> From<<ST as TryFrom<&'a Settings>>::Error>,
Error: for<'a> From<<EM as TryFrom<&'a [Url]>>::Error>,
{
/// Initializes a new [`SignerContext`], automatically creating clients
/// based on the provided types.
pub fn init(config: Settings, db: S) -> Result<Self, Error> {
let bc = BC::try_from(&config.bitcoin.rpc_endpoints)?;
let st = ST::try_from(&config)?;
let em = EM::try_from(&config.emily.endpoints)?;

Ok(Self::new(config, db, bc, st))
Ok(Self::new(config, db, bc, st, em))
}
}

impl<S, BC, ST> SignerContext<S, BC, ST>
impl<S, BC, ST, EM> SignerContext<S, BC, ST, EM>
where
S: DbRead + DbWrite + Clone + Sync + Send,
BC: BitcoinInteract + Clone,
ST: StacksInteract + Clone + Sync + Send,
EM: EmilyInteract + Clone + Sync + Send,
{
/// Create a new signer context.
pub fn new(config: Settings, db: S, bitcoin_client: BC, stacks_client: ST) -> Self {
pub fn new(
config: Settings,
db: S,
bitcoin_client: BC,
stacks_client: ST,
emily_client: EM,
) -> Self {
// TODO: Decide on the channel capacity and how we should handle slow consumers.
// NOTE: Ideally consumers which require processing time should pull the relevent
// messages into a local VecDequeue and process them in their own time.
Expand All @@ -105,15 +118,17 @@ where
storage: db,
bitcoin_client,
stacks_client,
emily_client,
}
}
}

impl<S, BC, ST> Context for SignerContext<S, BC, ST>
impl<S, BC, ST, EM> Context for SignerContext<S, BC, ST, EM>
where
S: DbRead + DbWrite + Clone + Sync + Send,
BC: BitcoinInteract + Clone,
ST: StacksInteract + Clone + Sync + Send,
EM: EmilyInteract + Clone + Sync + Send,
{
fn config(&self) -> &Settings {
&self.config
Expand Down Expand Up @@ -160,6 +175,10 @@ where
fn get_stacks_client(&self) -> impl StacksInteract + Clone {
self.stacks_client.clone()
}

fn get_emily_client(&self) -> impl EmilyInteract + Clone {
self.emily_client.clone()
}
}

#[cfg(test)]
Expand Down
5 changes: 3 additions & 2 deletions signer/src/emily_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ pub enum EmilyClientError {
}

/// Trait describing the interactions with Emily API.
pub trait EmilyInteract {
#[cfg_attr(any(test, feature = "testing"), mockall::automock())]
pub trait EmilyInteract: Sync + Send {
/// Get pending deposits from Emily.
fn get_deposits(
&self,
) -> impl std::future::Future<Output = Result<Vec<CreateDepositRequest>, Error>>;
) -> impl std::future::Future<Output = Result<Vec<CreateDepositRequest>, Error>> + Send;
}

/// Emily API client.
Expand Down
1 change: 1 addition & 0 deletions signer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
_,
ApiFallbackClient<BitcoinCoreClient>,
ApiFallbackClient<StacksClient>,
ApiFallbackClient<EmilyClient>,
>::init(settings, db)?;

// Run the application components concurrently. We're `join!`ing them
Expand Down
Loading

0 comments on commit 3b73797

Please sign in to comment.