Skip to content

Commit

Permalink
Merge pull request #6 from Sajjon/simplify
Browse files Browse the repository at this point in the history
Add RETRY logic | Cleanup
  • Loading branch information
Sajjon authored Jul 3, 2024
2 parents fd047e3 + aed806b commit e4daa7f
Show file tree
Hide file tree
Showing 33 changed files with 1,061 additions and 584 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
tarpaulin-report.*
2 changes: 1 addition & 1 deletion .tarpaulin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ timeout = "2m"
locked = true
all-features = true
jobs = 1
out = ["html"]
out = ["Html"]
46 changes: 46 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in library 'signing'",
"cargo": {
"args": [
"test",
"--no-run",
"--lib",
"--package=signing"
],
"filter": {
"name": "signing",
"kind": "lib"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug integration test 'main'",
"cargo": {
"args": [
"test",
"--no-run",
"--test=main",
"--package=signing"
],
"filter": {
"name": "main",
"kind": "test"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"substate",
"txid",
"txids",
"unsecurified",
"Yubikey"
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::prelude::*;

pub trait IsUseFactorSourceDriversContext {
fn driver_for_factor_source_kind(&self, kind: FactorSourceKind) -> SigningDriver;
fn driver_for_factor_source_kind(&self, kind: FactorSourceKind) -> UseFactorSourceDriver;
}
22 changes: 12 additions & 10 deletions src/drivers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
mod batch_signing_response;
mod batch_tx_batch_key_signing_request;
mod parallel_batch_signing_driver;
mod serial_batch_signing_driver;
mod serial_single_signing_driver;
mod signing_driver;
mod signing_drivers_context;
mod is_use_factor_source_drivers_context;
mod parallel_batch_use_factor_sources_driver;
mod serial_batch_use_factor_source_driver;
mod serial_single_use_factor_source_driver;
mod use_factor_source_client;
mod use_factor_source_driver;

pub use batch_signing_response::*;
pub use batch_tx_batch_key_signing_request::*;
pub use parallel_batch_signing_driver::*;
pub use serial_batch_signing_driver::*;
pub use serial_single_signing_driver::*;
pub use signing_driver::*;
pub use signing_drivers_context::*;
pub use is_use_factor_source_drivers_context::*;
pub use parallel_batch_use_factor_sources_driver::*;
pub use serial_batch_use_factor_source_driver::*;
pub use serial_single_use_factor_source_driver::*;
pub use use_factor_source_client::*;
pub use use_factor_source_driver::*;
65 changes: 0 additions & 65 deletions src/drivers/parallel_batch_signing_driver.rs

This file was deleted.

77 changes: 77 additions & 0 deletions src/drivers/parallel_batch_use_factor_sources_driver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::prelude::*;

/// A collection of factor sources to use to sign, transactions with multiple keys
/// (derivations paths).
pub struct ParallelBatchSigningRequest {
/// Per factor source, a set of transactions to sign, with
/// multiple derivations paths.
pub per_factor_source: IndexMap<FactorSourceID, BatchTXBatchKeySigningRequest>,
/// A collection of transactions which would be invalid if the user skips
/// signing with this factor source.
pub invalid_transactions_if_skipped: IndexSet<InvalidTransactionIfSkipped>,
}
impl ParallelBatchSigningRequest {
pub fn new(
per_factor_source: IndexMap<FactorSourceID, BatchTXBatchKeySigningRequest>,
invalid_transactions_if_skipped: IndexSet<InvalidTransactionIfSkipped>,
) -> Self {
Self {
per_factor_source,
invalid_transactions_if_skipped,
}
}
pub fn factor_source_ids(&self) -> IndexSet<FactorSourceID> {
self.per_factor_source.keys().cloned().collect()
}
}

#[async_trait::async_trait]
pub trait IsUseFactorSourcesDriver {
async fn did_fail_ask_if_retry(&self, factor_source_ids: IndexSet<FactorSourceID>) -> bool;
}

#[async_trait::async_trait]
pub trait IsTestUseFactorSourcesDriver: IsUseFactorSourcesDriver + Sync {
fn simulated_user(&self) -> SimulatedUser;

fn should_simulate_failure(&self, factor_source_ids: IndexSet<FactorSourceID>) -> bool {
self.simulated_user()
.simulate_failure_if_needed(factor_source_ids)
}
}

#[async_trait::async_trait]
impl<T> IsUseFactorSourcesDriver for T
where
T: IsTestUseFactorSourcesDriver,
{
async fn did_fail_ask_if_retry(&self, factor_source_ids: IndexSet<FactorSourceID>) -> bool {
self.simulated_user().retry_if_needed(factor_source_ids)
}
}

/// A driver for a factor source kind which supports *Batch* usage of
/// multiple factor sources in parallel.
///
/// Most FactorSourceKinds does in fact NOT support parallel usage,
/// e.g. signing using multiple factors sources at once, but some do,
/// typically the DeviceFactorSource does, i.e. we can load multiple
/// mnemonics from secure storage in one go and sign with all of them
/// "in parallel".
///
/// This is a bit of a misnomer, as we don't actually use them in parallel,
/// but rather we iterate through all mnemonics and derive public keys/
/// or sign a payload with each of them in sequence
///
/// The user does not have the ability to SKIP a certain factor source,
/// instead either ALL factor sources are used to sign the transactions
/// or none.
///
/// Example of a Parallel Batch Signing Driver is that for DeviceFactorSource.
#[async_trait::async_trait]
pub trait ParallelBatchUseFactorSourcesDriver: IsUseFactorSourcesDriver {
async fn sign(
&self,
request: ParallelBatchSigningRequest,
) -> Result<SignWithFactorSourceOrSourcesOutcome<BatchSigningResponse>>;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;

/// A batch signing request used with a SerialBatchSigningDriver, containing
/// A batch signing request used with a SerialBatchUseFactorSourceDriver, containing
/// a collection of transactions to sign with multiple keys (derivation paths),
/// and a collection of transactions which would be invalid if the user skips
/// signing with this factor source.
Expand Down Expand Up @@ -37,24 +37,9 @@ impl SerialBatchSigningRequest {
/// questions from different security questions factor sources (in fact we
/// might not even even allow multiple SecurityQuestionsFactorSources to be used).
#[async_trait]
pub trait SerialBatchSigningDriver {
pub trait SerialBatchUseFactorSourceDriver: IsUseFactorSourcesDriver {
async fn sign(
&self,
request: SerialBatchSigningRequest,
) -> SignWithFactorSourceOrSourcesOutcome<BatchSigningResponse>;
}

pub struct SerialBatchSigningClient {
driver: Arc<dyn SerialBatchSigningDriver>,
}
impl SerialBatchSigningClient {
pub fn new(driver: Arc<dyn SerialBatchSigningDriver>) -> Self {
Self { driver }
}
pub async fn sign(
&self,
request: SerialBatchSigningRequest,
) -> SignWithFactorSourceOrSourcesOutcome<BatchSigningResponse> {
self.driver.sign(request).await
}
) -> Result<SignWithFactorSourceOrSourcesOutcome<BatchSigningResponse>>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,9 @@ impl SerialSingleSigningRequestPartial {
/// Example of a Serial Single Signing Driver *might* be `Arculus` - we
/// do not yet know.
#[async_trait]
pub trait SerialSingleSigningDriver {
pub trait SerialSingleUseFactorSourceDriver: IsUseFactorSourcesDriver {
async fn sign(
&self,
request: SerialSingleSigningRequestFull,
) -> SignWithFactorSourceOrSourcesOutcome<HDSignature>;
}

pub struct SerialSingleSigningClient {
driver: Arc<dyn SerialSingleSigningDriver>,
}
impl SerialSingleSigningClient {
pub fn new(driver: Arc<dyn SerialSingleSigningDriver>) -> Self {
Self { driver }
}
pub async fn sign(
&self,
request: SerialSingleSigningRequestFull,
) -> SignWithFactorSourceOrSourcesOutcome<HDSignature> {
self.driver.sign(request).await
}
) -> Result<SignWithFactorSourceOrSourcesOutcome<HDSignature>>;
}
Loading

0 comments on commit e4daa7f

Please sign in to comment.