Skip to content

Commit 1e1d5ca

Browse files
authored
add mint state to bill (#523)
1 parent 216b30f commit 1e1d5ca

File tree

20 files changed

+330
-76
lines changed

20 files changed

+330
-76
lines changed

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# 0.3.13
22

3-
* Add default mint configuration options
4-
* `default_mint_url`
5-
* `default_mint_node_id`
3+
* Minting
4+
* Add default mint configuration options
5+
* `default_mint_url`
6+
* `default_mint_node_id`
7+
* Add minting status flag to bill
8+
* Add endpoint to fetch minting status for a bill
69
* Implement `request_to_mint`
710
* Change bitcoin addresses and descriptor to p2wpkh
811
* Suppress logging from crates we don't control

crates/bcr-ebill-api/src/data/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub use bcr_ebill_core::bill;
22
pub use bcr_ebill_core::company;
33
pub use bcr_ebill_core::contact;
44
pub use bcr_ebill_core::identity;
5+
pub use bcr_ebill_core::mint;
56
pub use bcr_ebill_core::notification;
67

78
pub use bcr_ebill_core::File;

crates/bcr-ebill-api/src/service/bill_service/data_fetching.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::util;
33
use super::service::BillService;
44
use super::{Error, Result};
55
use bcr_ebill_core::ValidationError;
6+
use bcr_ebill_core::bill::BillMintStatus;
67
use bcr_ebill_core::bill::validation::get_expiration_deadline_base_for_req_to_pay;
78
use bcr_ebill_core::blockchain::bill::block::NodeId;
89
use bcr_ebill_core::constants::RECOURSE_DEADLINE_SECONDS;
@@ -157,6 +158,10 @@ impl BillService {
157158
Some(ref endorsee) => endorsee,
158159
};
159160

161+
let has_mint_requests = self
162+
.mint_store
163+
.exists_for_bill(current_identity_node_id, &bill.id)
164+
.await?;
160165
let mut paid = false;
161166
let mut requested_to_pay = false;
162167
let mut rejected_to_pay = false;
@@ -473,6 +478,7 @@ impl BillService {
473478
request_to_recourse_timed_out,
474479
rejected_request_to_recourse,
475480
},
481+
mint: BillMintStatus { has_mint_requests },
476482
redeemed_funds_available,
477483
has_requested_funds,
478484
};

crates/bcr-ebill-api/src/service/bill_service/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::data::{
77
},
88
contact::BillParticipant,
99
identity::{Identity, IdentityWithAll},
10+
mint::MintRequestState,
1011
};
1112
use crate::util::BcrKeys;
1213
use async_trait::async_trait;
@@ -179,6 +180,14 @@ pub trait BillServiceApi: ServiceTraitBounds {
179180
current_identity_node_id: &str,
180181
) -> Result<Vec<Endorsement>>;
181182

183+
/// Returns the mint state for a given bill
184+
async fn get_mint_state(
185+
&self,
186+
bill_id: &str,
187+
current_identity_node_id: &str,
188+
) -> Result<Vec<MintRequestState>>;
189+
190+
/// Clear the bill cache
182191
async fn clear_bill_cache(&self) -> Result<()>;
183192
}
184193

crates/bcr-ebill-api/src/service/bill_service/service.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use bcr_ebill_core::constants::{
3636
};
3737
use bcr_ebill_core::contact::{BillAnonParticipant, BillParticipant, Contact};
3838
use bcr_ebill_core::identity::{IdentityType, IdentityWithAll};
39-
use bcr_ebill_core::mint::MintRequestStatus;
39+
use bcr_ebill_core::mint::{MintRequestState, MintRequestStatus};
4040
use bcr_ebill_core::notification::ActionType;
4141
use bcr_ebill_core::util::currency;
4242
use bcr_ebill_core::{ServiceTraitBounds, Validate, ValidationError};
@@ -696,8 +696,8 @@ impl BillServiceApi for BillService {
696696
matches!(
697697
rtm.status,
698698
MintRequestStatus::Pending
699-
| MintRequestStatus::Offered { .. }
700-
| MintRequestStatus::Accepted { .. }
699+
| MintRequestStatus::Offered
700+
| MintRequestStatus::Accepted
701701
)
702702
}) {
703703
return Err(Error::Validation(
@@ -1100,6 +1100,24 @@ impl BillServiceApi for BillService {
11001100
Ok(result)
11011101
}
11021102

1103+
async fn get_mint_state(
1104+
&self,
1105+
bill_id: &str,
1106+
current_identity_node_id: &str,
1107+
) -> Result<Vec<MintRequestState>> {
1108+
let requests = self
1109+
.mint_store
1110+
.get_requests_for_bill(current_identity_node_id, bill_id)
1111+
.await?;
1112+
Ok(requests
1113+
.into_iter()
1114+
.map(|req| MintRequestState {
1115+
request: req,
1116+
offer: None,
1117+
})
1118+
.collect())
1119+
}
1120+
11031121
async fn clear_bill_cache(&self) -> Result<()> {
11041122
self.store.clear_bill_cache().await?;
11051123
Ok(())

crates/bcr-ebill-api/src/service/bill_service/test_utils.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use crate::{
1919
};
2020
use bcr_ebill_core::{
2121
bill::{
22-
BillAcceptanceStatus, BillData, BillParticipants, BillPaymentStatus, BillRecourseStatus,
23-
BillSellStatus, BillStatus,
22+
BillAcceptanceStatus, BillData, BillMintStatus, BillParticipants, BillPaymentStatus,
23+
BillRecourseStatus, BillSellStatus, BillStatus,
2424
},
2525
blockchain::{
2626
Blockchain,
@@ -131,6 +131,9 @@ pub fn get_baseline_cached_bill(id: String) -> BitcreditBillResult {
131131
request_to_recourse_timed_out: false,
132132
rejected_request_to_recourse: false,
133133
},
134+
mint: BillMintStatus {
135+
has_mint_requests: false,
136+
},
134137
redeemed_funds_available: false,
135138
has_requested_funds: false,
136139
},
@@ -247,6 +250,9 @@ pub fn get_service(mut ctx: MockBillContext) -> BillService {
247250
ctx.identity_store
248251
.expect_get_full()
249252
.returning(|| Ok(get_baseline_identity()));
253+
ctx.mint_store
254+
.expect_exists_for_bill()
255+
.returning(|_, _| Ok(false));
250256
BillService::new(
251257
Arc::new(ctx.bill_store),
252258
Arc::new(ctx.bill_blockchain_store),

crates/bcr-ebill-api/src/tests/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,18 @@ pub mod tests {
5858

5959
#[async_trait]
6060
impl MintStoreApi for MintStore {
61+
async fn exists_for_bill(&self, requester_node_id: &str, bill_id: &str) -> Result<bool>;
6162
async fn get_requests(
6263
&self,
6364
requester_node_id: &str,
6465
bill_id: &str,
6566
mint_node_id: &str,
6667
) -> Result<Vec<MintRequest>>;
68+
async fn get_requests_for_bill(
69+
&self,
70+
requester_node_id: &str,
71+
bill_id: &str,
72+
) -> Result<Vec<MintRequest>>;
6773
async fn add_request(
6874
&self,
6975
requester_node_id: &str,

crates/bcr-ebill-core/src/bill/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ pub struct BillStatus {
175175
pub payment: BillPaymentStatus,
176176
pub sell: BillSellStatus,
177177
pub recourse: BillRecourseStatus,
178+
pub mint: BillMintStatus,
178179
pub redeemed_funds_available: bool,
179180
pub has_requested_funds: bool,
180181
}
@@ -215,6 +216,11 @@ pub struct BillRecourseStatus {
215216
pub rejected_request_to_recourse: bool,
216217
}
217218

219+
#[derive(Debug, Clone)]
220+
pub struct BillMintStatus {
221+
pub has_mint_requests: bool,
222+
}
223+
218224
#[derive(Debug, Clone)]
219225
pub struct BillData {
220226
pub language: String,
Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
1+
/// A request to mint
12
#[derive(Debug, Clone)]
23
pub struct MintRequest {
4+
/// The requester
35
pub requester_node_id: String,
6+
/// The bill to request to mint
47
pub bill_id: String,
8+
/// The mint to be requested against
59
pub mint_node_id: String,
10+
/// The id returned from the mint
611
pub mint_request_id: String,
12+
/// The time of the request
713
pub timestamp: u64,
14+
/// The status of the request to mint
815
pub status: MintRequestStatus,
916
}
1017

1118
#[derive(Debug, Clone)]
1219
pub enum MintRequestStatus {
20+
/// Waiting for an answer from the mint
1321
Pending,
22+
/// Denied by the mint
1423
Denied,
15-
Offered {
16-
keyset_id: String,
17-
expiration_timestamp: u64,
18-
discounted: u64,
19-
},
20-
Accepted {
21-
keyset_id: String,
22-
},
23-
Rejected {
24-
timestamp: u64,
25-
},
24+
/// Offer was made
25+
Offered,
26+
/// Offer was accepted
27+
Accepted,
28+
/// The offer was rejected by the requester
29+
Rejected,
30+
/// The request was cancelled by the requester
31+
Cancelled,
32+
/// The offer expired
33+
Expired,
34+
}
35+
36+
/// An offer from a mint as a response to a request to mint
37+
#[derive(Debug, Clone)]
38+
pub struct MintOffer {}
39+
40+
#[derive(Debug, Clone)]
41+
pub struct MintRequestState {
42+
/// There always is a request
43+
pub request: MintRequest,
44+
/// There might be an offer
45+
pub offer: Option<MintOffer>,
2646
}

crates/bcr-ebill-persistence/src/db/bill.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::collections::HashSet;
22

33
use super::surreal::{Bindings, SurrealWrapper};
4-
use super::{FileDb, PostalAddressDb, Result};
4+
use super::{BillIdDb, FileDb, PostalAddressDb, Result};
55
use crate::constants::{DB_BILL_ID, DB_IDS, DB_OP_CODE, DB_TABLE, DB_TIMESTAMP};
66
use crate::{Error, bill::BillStoreApi};
77
use async_trait::async_trait;
88
use bcr_ebill_core::ServiceTraitBounds;
99
use bcr_ebill_core::bill::{
10-
BillAcceptanceStatus, BillCurrentWaitingState, BillData, BillParticipants, BillPaymentStatus,
11-
BillRecourseStatus, BillSellStatus, BillStatus, BillWaitingForPaymentState,
10+
BillAcceptanceStatus, BillCurrentWaitingState, BillData, BillMintStatus, BillParticipants,
11+
BillPaymentStatus, BillRecourseStatus, BillSellStatus, BillStatus, BillWaitingForPaymentState,
1212
BillWaitingForRecourseState, BillWaitingForSellState, BitcreditBillResult,
1313
};
1414
use bcr_ebill_core::constants::{PAYMENT_DEADLINE_SECONDS, RECOURSE_DEADLINE_SECONDS};
@@ -445,6 +445,7 @@ pub struct BillStatusDb {
445445
pub payment: BillPaymentStatusDb,
446446
pub sell: BillSellStatusDb,
447447
pub recourse: BillRecourseStatusDb,
448+
pub mint: BillMintStatusDb,
448449
pub redeemed_funds_available: bool,
449450
pub has_requested_funds: bool,
450451
}
@@ -456,6 +457,7 @@ impl From<BillStatusDb> for BillStatus {
456457
payment: value.payment.into(),
457458
sell: value.sell.into(),
458459
recourse: value.recourse.into(),
460+
mint: value.mint.into(),
459461
redeemed_funds_available: value.redeemed_funds_available,
460462
has_requested_funds: value.has_requested_funds,
461463
}
@@ -469,6 +471,7 @@ impl From<&BillStatus> for BillStatusDb {
469471
payment: (&value.payment).into(),
470472
sell: (&value.sell).into(),
471473
recourse: (&value.recourse).into(),
474+
mint: (&value.mint).into(),
472475
redeemed_funds_available: value.redeemed_funds_available,
473476
has_requested_funds: value.has_requested_funds,
474477
}
@@ -607,6 +610,27 @@ impl From<&BillRecourseStatus> for BillRecourseStatusDb {
607610
}
608611
}
609612

613+
#[derive(Debug, Clone, Serialize, Deserialize)]
614+
pub struct BillMintStatusDb {
615+
pub has_mint_requests: bool,
616+
}
617+
618+
impl From<BillMintStatusDb> for BillMintStatus {
619+
fn from(value: BillMintStatusDb) -> Self {
620+
Self {
621+
has_mint_requests: value.has_mint_requests,
622+
}
623+
}
624+
}
625+
626+
impl From<&BillMintStatus> for BillMintStatusDb {
627+
fn from(value: &BillMintStatus) -> Self {
628+
Self {
629+
has_mint_requests: value.has_mint_requests,
630+
}
631+
}
632+
}
633+
610634
#[derive(Debug, Clone, Serialize, Deserialize)]
611635
pub struct BillDataDb {
612636
pub language: String,
@@ -783,11 +807,6 @@ pub struct BillPaidDb {
783807
pub payment_address: String,
784808
}
785809

786-
#[derive(Debug, Clone, Serialize, Deserialize)]
787-
pub struct BillIdDb {
788-
pub bill_id: String,
789-
}
790-
791810
#[derive(Debug, Clone, Serialize, Deserialize)]
792811
pub struct BillKeysDb {
793812
#[serde(skip_serializing_if = "Option::is_none")]

0 commit comments

Comments
 (0)