Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Func #7

Merged
merged 6 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/signing/petition_types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod factor_list_kind;
mod petition_factors_types;
mod petition_for_entity;
mod petition_for_factors_types;
mod petition_for_transaction;
mod petitions;
mod petitions_status;

pub use factor_list_kind::*;
pub use petition_factors_types::*;
pub use petition_for_entity::*;
pub use petition_for_factors_types::*;
pub(crate) use petition_for_transaction::*;
pub(crate) use petitions::*;
pub(crate) use petitions_status::*;
18 changes: 0 additions & 18 deletions src/signing/petition_types/petition_factors_types/mod.rs

This file was deleted.

This file was deleted.

36 changes: 19 additions & 17 deletions src/signing/petition_types/petition_for_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ impl PetitionForEntity {
/// Returns `true` if signatures requirement has been fulfilled, either by
/// override factors or by threshold factors
pub fn has_signatures_requirement_been_fulfilled(&self) -> bool {
self.status() == PetitionFactorsStatus::Finished(PetitionFactorsStatusFinished::Success)
self.status() == PetitionForFactorsStatus::Finished(PetitionFactorsStatusFinished::Success)
}

/// Returns `true` if the transaction of this petition already has failed due
/// to too many factors neglected
pub fn has_failed(&self) -> bool {
self.status() == PetitionFactorsStatus::Finished(PetitionFactorsStatusFinished::Fail)
self.status() == PetitionForFactorsStatus::Finished(PetitionFactorsStatusFinished::Fail)
}

fn map_list_then_form_union<F, T>(&self, map: F) -> IndexSet<T>
Expand Down Expand Up @@ -172,9 +172,9 @@ impl PetitionForEntity {
) -> bool {
assert!(self.references_any_factor_source(&factor_source_ids));
match self.status() {
PetitionFactorsStatus::Finished(PetitionFactorsStatusFinished::Fail) => true,
PetitionFactorsStatus::Finished(PetitionFactorsStatusFinished::Success) => false,
PetitionFactorsStatus::InProgress => false,
PetitionForFactorsStatus::Finished(PetitionFactorsStatusFinished::Fail) => true,
PetitionForFactorsStatus::Finished(PetitionFactorsStatusFinished::Success) => false,
PetitionForFactorsStatus::InProgress => false,
}
}

Expand All @@ -184,7 +184,7 @@ impl PetitionForEntity {
) -> Option<InvalidTransactionIfNeglected> {
let status_if_neglected = self.status_if_neglected_factors(factor_source_ids);
match status_if_neglected {
PetitionFactorsStatus::Finished(finished_reason) => match finished_reason {
PetitionForFactorsStatus::Finished(finished_reason) => match finished_reason {
PetitionFactorsStatusFinished::Fail => {
let intent_hash = self.intent_hash.clone();
Some(InvalidTransactionIfNeglected::new(
Expand All @@ -194,14 +194,14 @@ impl PetitionForEntity {
}
PetitionFactorsStatusFinished::Success => None,
},
PetitionFactorsStatus::InProgress => None,
PetitionForFactorsStatus::InProgress => None,
}
}

pub fn status_if_neglected_factors(
&self,
factor_source_ids: IndexSet<FactorSourceIDFromHash>,
) -> PetitionFactorsStatus {
) -> PetitionForFactorsStatus {
let simulation = self.clone();
for factor_source_id in factor_source_ids.iter() {
simulation.neglect_if_referenced(NeglectedFactor::new(
Expand Down Expand Up @@ -232,9 +232,9 @@ impl PetitionForEntity {
self.both_void(|p| p.neglect_if_referenced(neglected.clone()));
}

pub fn status(&self) -> PetitionFactorsStatus {
use PetitionFactorsStatus::*;
pub fn status(&self) -> PetitionForFactorsStatus {
use PetitionFactorsStatusFinished::*;
use PetitionForFactorsStatus::*;

self.both(
|p| p.status(),
Expand All @@ -252,12 +252,14 @@ impl PetitionForEntity {
(Some(threshold), None) => threshold,
(None, Some(r#override)) => r#override,
(Some(threshold), Some(r#override)) => match (threshold, r#override) {
(InProgress, InProgress) => PetitionFactorsStatus::InProgress,
(Finished(Fail), InProgress) => PetitionFactorsStatus::InProgress,
(InProgress, Finished(Fail)) => PetitionFactorsStatus::InProgress,
(Finished(Fail), Finished(Fail)) => PetitionFactorsStatus::Finished(Fail),
(Finished(Success), _) => PetitionFactorsStatus::Finished(Success),
(_, Finished(Success)) => PetitionFactorsStatus::Finished(Success),
(InProgress, InProgress) => PetitionForFactorsStatus::InProgress,
(Finished(Fail), InProgress) => PetitionForFactorsStatus::InProgress,
(InProgress, Finished(Fail)) => PetitionForFactorsStatus::InProgress,
(Finished(Fail), Finished(Fail)) => {
PetitionForFactorsStatus::Finished(Fail)
}
(Finished(Success), _) => PetitionForFactorsStatus::Finished(Success),
(_, Finished(Success)) => PetitionForFactorsStatus::Finished(Success),
},
}
},
Expand Down Expand Up @@ -461,7 +463,7 @@ mod tests {

#[test]
fn debug() {
pretty_assertions::assert_eq!(format!("{:?}", Sut::sample()), "intent_hash: TXID(\"dedede\"), entity: acco_Grace, \"threshold_factors PetitionForFactors(input: PetitionFactorsInput(factors: {\\n factor_source_id: Device:00, derivation_path: 0/A/tx/6,\\n factor_source_id: Arculus:03, derivation_path: 0/A/tx/6,\\n factor_source_id: Yubikey:05, derivation_path: 0/A/tx/6,\\n}), state_snapshot: signatures: \\\"\\\", neglected: \\\"\\\")\"\"override_factors PetitionForFactors(input: PetitionFactorsInput(factors: {\\n factor_source_id: Ledger:01, derivation_path: 0/A/tx/6,\\n factor_source_id: Arculus:04, derivation_path: 0/A/tx/6,\\n}), state_snapshot: signatures: \\\"\\\", neglected: \\\"\\\")\"");
pretty_assertions::assert_eq!(format!("{:?}", Sut::sample()), "intent_hash: TXID(\"dedede\"), entity: acco_Grace, \"threshold_factors PetitionForFactors(input: PetitionForFactorsInput(factors: {\\n factor_source_id: Device:00, derivation_path: 0/A/tx/6,\\n factor_source_id: Arculus:03, derivation_path: 0/A/tx/6,\\n factor_source_id: Yubikey:05, derivation_path: 0/A/tx/6,\\n}), state_snapshot: signatures: \\\"\\\", neglected: \\\"\\\")\"\"override_factors PetitionForFactors(input: PetitionForFactorsInput(factors: {\\n factor_source_id: Ledger:01, derivation_path: 0/A/tx/6,\\n factor_source_id: Arculus:04, derivation_path: 0/A/tx/6,\\n}), state_snapshot: signatures: \\\"\\\", neglected: \\\"\\\")\"");
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::prelude::*;

/// A trait for types which reference a factor source.
pub(crate) trait FactorSourceReferencing: std::hash::Hash + PartialEq + Eq + Clone {
fn factor_source_id(&self) -> FactorSourceIDFromHash;
}
Expand Down
7 changes: 7 additions & 0 deletions src/signing/petition_types/petition_for_factors_types/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod factor_source_referencing;
mod neglected_factor_instance;
mod petition_for_factors;

pub(crate) use factor_source_referencing::*;
pub use neglected_factor_instance::*;
pub(crate) use petition_for_factors::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[allow(clippy::module_inception)]
mod petition_for_factors;

mod petition_for_factors_input;
mod petition_for_factors_state;
mod petition_for_factors_state_snapshot;
mod petition_for_factors_status;
mod petition_for_factors_sub_state;

use petition_for_factors_input::*;
use petition_for_factors_state::*;
use petition_for_factors_state_snapshot::*;
use petition_for_factors_sub_state::*;

pub(crate) use petition_for_factors::*;
pub(crate) use petition_for_factors_status::*;
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ pub struct PetitionForFactors {
pub factor_list_kind: FactorListKind,

/// Factors to sign with and the required number of them.
pub(crate) input: PetitionFactorsInput,
state: RefCell<PetitionFactorsState>,
pub(crate) input: PetitionForFactorsInput,
state: RefCell<PetitionForFactorsState>,
}

impl HasSampleValues for PetitionForFactors {
fn sample() -> Self {
Self::new(FactorListKind::Threshold, PetitionFactorsInput::sample())
Self::new(FactorListKind::Threshold, PetitionForFactorsInput::sample())
}

fn sample_other() -> Self {
Self::new(
FactorListKind::Override,
PetitionFactorsInput::sample_other(),
PetitionForFactorsInput::sample_other(),
)
}
}

impl PetitionForFactors {
pub fn new(factor_list_kind: FactorListKind, input: PetitionFactorsInput) -> Self {
pub fn new(factor_list_kind: FactorListKind, input: PetitionForFactorsInput) -> Self {
Self {
factor_list_kind,
input,
state: RefCell::new(PetitionFactorsState::new()),
state: RefCell::new(PetitionForFactorsState::new()),
}
}

Expand All @@ -55,12 +55,12 @@ impl PetitionForFactors {
}
Some(Self::new(
FactorListKind::Threshold,
PetitionFactorsInput::new_threshold(IndexSet::from_iter(factors), threshold),
PetitionForFactorsInput::new_threshold(IndexSet::from_iter(factors), threshold),
))
}

pub fn new_unsecurified(factor: HierarchicalDeterministicFactorInstance) -> Self {
Self::new_threshold(vec![factor], 1).unwrap() // define as 1/1 threshold factor, which is a good definition.
Self::new_threshold(vec![factor], 1).expect("Factors is not empty") // define as 1/1 threshold factor, which is a good definition.
}

pub fn new_override(factors: Vec<HierarchicalDeterministicFactorInstance>) -> Option<Self> {
Expand All @@ -69,7 +69,7 @@ impl PetitionForFactors {
}
Some(Self::new(
FactorListKind::Override,
PetitionFactorsInput::new_override(IndexSet::from_iter(factors)),
PetitionForFactorsInput::new_override(IndexSet::from_iter(factors)),
))
}

Expand Down Expand Up @@ -150,7 +150,7 @@ impl PetitionForFactors {
self.input.reference_factor_source_with_id(factor_source_id)
}

fn state_snapshot(&self) -> PetitionFactorsStateSnapshot {
fn state_snapshot(&self) -> PetitionForFactorsStateSnapshot {
self.state.borrow().snapshot()
}

Expand All @@ -170,7 +170,7 @@ impl PetitionForFactors {
is_finished_with_fail
}

fn finished_with(&self) -> Option<PetitionFactorsStatusFinished> {
fn get_finished_with(&self) -> Option<PetitionFactorsStatusFinished> {
if self.is_finished_successfully() {
Some(PetitionFactorsStatusFinished::Success)
} else if self.is_finished_with_fail() {
Expand All @@ -180,11 +180,11 @@ impl PetitionForFactors {
}
}

pub fn status(&self) -> PetitionFactorsStatus {
if let Some(finished_state) = self.finished_with() {
return PetitionFactorsStatus::Finished(finished_state);
pub fn status(&self) -> PetitionForFactorsStatus {
if let Some(finished_state) = self.get_finished_with() {
return PetitionForFactorsStatus::Finished(finished_state);
}
PetitionFactorsStatus::InProgress
PetitionForFactorsStatus::InProgress
}

pub fn debug_str(&self) -> String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use super::*;
use crate::prelude::*;

/// The input passed to a PetitionsForFactors
#[derive(Clone, PartialEq, Eq, derive_more::Debug)]
#[debug("PetitionFactorsInput(factors: {:#?})", self.factors)]
pub struct PetitionFactorsInput {
#[debug("PetitionForFactorsInput(factors: {:#?})", self.factors)]
pub struct PetitionForFactorsInput {
/// Factors to sign with.
pub(super) factors: IndexSet<HierarchicalDeterministicFactorInstance>,

/// Number of required factors to sign with.
pub(super) required: i8,
}

impl HasSampleValues for PetitionFactorsInput {
impl HasSampleValues for PetitionForFactorsInput {
fn sample() -> Self {
Self::new(
IndexSet::from_iter([
Expand All @@ -30,7 +31,7 @@ impl HasSampleValues for PetitionFactorsInput {
}
}

impl PetitionFactorsInput {
impl PetitionForFactorsInput {
pub(super) fn new(
factors: IndexSet<HierarchicalDeterministicFactorInstance>,
required: i8,
Expand Down Expand Up @@ -62,19 +63,19 @@ impl PetitionFactorsInput {
self.factors.len() as i8
}

fn remaining_factors_until_success(&self, snapshot: PetitionFactorsStateSnapshot) -> i8 {
fn remaining_factors_until_success(&self, snapshot: PetitionForFactorsStateSnapshot) -> i8 {
self.required - snapshot.signed_count()
}

pub(super) fn is_fulfilled_by(&self, snapshot: PetitionFactorsStateSnapshot) -> bool {
pub(super) fn is_fulfilled_by(&self, snapshot: PetitionForFactorsStateSnapshot) -> bool {
self.remaining_factors_until_success(snapshot) <= 0
}

fn factors_left_to_prompt(&self, snapshot: PetitionFactorsStateSnapshot) -> i8 {
fn factors_left_to_prompt(&self, snapshot: PetitionForFactorsStateSnapshot) -> i8 {
self.factors_count() - snapshot.prompted_count()
}

pub(super) fn is_failure_with(&self, snapshot: PetitionFactorsStateSnapshot) -> bool {
pub(super) fn is_failure_with(&self, snapshot: PetitionForFactorsStateSnapshot) -> bool {
let signed_or_pending =
self.factors_left_to_prompt(snapshot.clone()) + snapshot.signed_count();
let is_failure_with = signed_or_pending < self.required;
Expand Down
Loading
Loading