Skip to content

Commit 774423d

Browse files
authored
Chain event handler (#444)
* Name refactor, chain event handler * Block processing * Basic block implementation * Test setup * Tests * more tests * More tests * Avoid adding blocks on just action events * More bill chain events * Recourse events * Reject events * Recourse action * Fix review comment * Review fixes
1 parent 2cfca49 commit 774423d

File tree

24 files changed

+1730
-845
lines changed

24 files changed

+1730
-845
lines changed

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,13 @@ impl BillService {
178178
}
179179
}
180180

181-
// send notification to all required recipients
182-
self.notification_service
183-
.send_bill_is_signed_event(&BillChainEvent::new(&bill, &chain, &bill_keys)?)
184-
.await?;
185-
186-
// propagate the bill
187-
let bill_clone = bill.clone();
188-
let self_clone = self.clone();
189-
if let Err(e) = self_clone
190-
.propagate_bill_and_subscribe(
191-
&bill_clone.id,
192-
&bill_clone.drawer.node_id,
193-
&bill_clone.drawee.node_id,
194-
&bill_clone.payee.node_id,
195-
)
181+
// send notification and blocks to all required recipients
182+
if let Err(e) = self
183+
.notification_service
184+
.send_bill_is_signed_event(&BillChainEvent::new(&bill, &chain, &bill_keys, true)?)
196185
.await
197186
{
198-
error!("Error propagating and subscribing to bill: {e}");
187+
error!("Error propagating bill via Nostr {e}");
199188
}
200189

201190
// If we're the drawee, we immediately accept the bill with timestamp increased by 1 sec

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,7 +2173,7 @@ pub mod tests {
21732173
// Request to sell event should be sent
21742174
ctx.notification_service
21752175
.expect_send_offer_to_sell_event()
2176-
.returning(|_, _, _| Ok(()));
2176+
.returning(|_, _| Ok(()));
21772177
let service = get_service(ctx);
21782178

21792179
let res = service
@@ -2258,7 +2258,7 @@ pub mod tests {
22582258
// Request to sell event should be sent
22592259
ctx.notification_service
22602260
.expect_send_bill_is_sold_event()
2261-
.returning(|_, _, _| Ok(()));
2261+
.returning(|_, _| Ok(()));
22622262

22632263
let service = get_service(ctx);
22642264

@@ -2318,7 +2318,7 @@ pub mod tests {
23182318
// Sold event should be sent
23192319
ctx.notification_service
23202320
.expect_send_bill_is_sold_event()
2321-
.returning(|_, _, _| Ok(()));
2321+
.returning(|_, _| Ok(()));
23222322

23232323
let service = get_service(ctx);
23242324

@@ -2351,7 +2351,7 @@ pub mod tests {
23512351
// Request to sell event should be sent
23522352
ctx.notification_service
23532353
.expect_send_bill_is_sold_event()
2354-
.returning(|_, _, _| Ok(()));
2354+
.returning(|_, _| Ok(()));
23552355
let service = get_service(ctx);
23562356

23572357
let res = service
@@ -2579,7 +2579,7 @@ pub mod tests {
25792579
});
25802580
ctx.notification_service
25812581
.expect_send_bill_is_sold_event()
2582-
.returning(|_, _, _| Ok(()));
2582+
.returning(|_, _| Ok(()));
25832583

25842584
let service = get_service(ctx);
25852585

@@ -2625,7 +2625,7 @@ pub mod tests {
26252625
});
26262626
ctx.notification_service
26272627
.expect_send_bill_is_sold_event()
2628-
.returning(|_, _, _| Ok(()));
2628+
.returning(|_, _| Ok(()));
26292629
let service = get_service(ctx);
26302630

26312631
let res = service.check_bills_offer_to_sell_payment().await;
@@ -3227,8 +3227,8 @@ pub mod tests {
32273227
});
32283228
ctx.notification_service
32293229
.expect_send_request_to_action_rejected_event()
3230-
.with(eq("1234"), always(), eq(ActionType::AcceptBill), always())
3231-
.returning(|_, _, _, _| Ok(()));
3230+
.with(always(), eq(ActionType::AcceptBill))
3231+
.returning(|_, _| Ok(()));
32323232

32333233
let service = get_service(ctx);
32343234
let res = service
@@ -3271,8 +3271,8 @@ pub mod tests {
32713271

32723272
ctx.notification_service
32733273
.expect_send_request_to_action_rejected_event()
3274-
.with(eq("1234"), always(), eq(ActionType::BuyBill), always())
3275-
.returning(|_, _, _, _| Ok(()));
3274+
.with(always(), eq(ActionType::BuyBill))
3275+
.returning(|_, _| Ok(()));
32763276
let service = get_service(ctx);
32773277

32783278
let res = service
@@ -3328,8 +3328,8 @@ pub mod tests {
33283328
});
33293329
ctx.notification_service
33303330
.expect_send_request_to_action_rejected_event()
3331-
.with(eq("1234"), always(), eq(ActionType::PayBill), always())
3332-
.returning(|_, _, _, _| Ok(()));
3331+
.with(always(), eq(ActionType::PayBill))
3332+
.returning(|_, _| Ok(()));
33333333
let service = get_service(ctx);
33343334

33353335
let res = service
@@ -3388,8 +3388,8 @@ pub mod tests {
33883388
});
33893389
ctx.notification_service
33903390
.expect_send_request_to_action_rejected_event()
3391-
.with(eq("1234"), always(), eq(ActionType::RecourseBill), always())
3392-
.returning(|_, _, _, _| Ok(()));
3391+
.with(always(), eq(ActionType::RecourseBill))
3392+
.returning(|_, _| Ok(()));
33933393

33943394
let service = get_service(ctx);
33953395

@@ -3449,7 +3449,7 @@ pub mod tests {
34493449
});
34503450
ctx.notification_service
34513451
.expect_send_bill_recourse_paid_event()
3452-
.returning(|_, _, _| Ok(()));
3452+
.returning(|_, _| Ok(()));
34533453

34543454
let service = get_service(ctx);
34553455

@@ -3513,7 +3513,7 @@ pub mod tests {
35133513
});
35143514
ctx.notification_service
35153515
.expect_send_bill_recourse_paid_event()
3516-
.returning(|_, _, _| Ok(()));
3516+
.returning(|_, _| Ok(()));
35173517
let service = get_service(ctx);
35183518

35193519
let res = service.check_bills_in_recourse_payment().await;
@@ -3588,7 +3588,7 @@ pub mod tests {
35883588
// Request to recourse event should be sent
35893589
ctx.notification_service
35903590
.expect_send_recourse_action_event()
3591-
.returning(|_, _, _, _| Ok(()));
3591+
.returning(|_, _, _| Ok(()));
35923592
let service = get_service(ctx);
35933593

35943594
let res = service
@@ -3675,7 +3675,7 @@ pub mod tests {
36753675
// Request to recourse event should be sent
36763676
ctx.notification_service
36773677
.expect_send_recourse_action_event()
3678-
.returning(|_, _, _, _| Ok(()));
3678+
.returning(|_, _, _| Ok(()));
36793679
let service = get_service(ctx);
36803680

36813681
let res = service
@@ -3737,7 +3737,7 @@ pub mod tests {
37373737
// Recourse paid event should be sent
37383738
ctx.notification_service
37393739
.expect_send_bill_recourse_paid_event()
3740-
.returning(|_, _, _| Ok(()));
3740+
.returning(|_, _| Ok(()));
37413741

37423742
let service = get_service(ctx);
37433743

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

Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ use super::Result;
22
use super::service::BillService;
33
use crate::service::bill_service::{BillAction, BillServiceApi};
44
use bcr_ebill_core::{
5-
blockchain::{
6-
Blockchain,
7-
bill::{OfferToSellWaitingForPayment, RecourseWaitingForPayment},
8-
},
5+
blockchain::bill::{OfferToSellWaitingForPayment, RecourseWaitingForPayment},
96
company::{Company, CompanyKeys},
107
contact::IdentityPublicData,
118
identity::{Identity, IdentityWithAll},
129
util::BcrKeys,
1310
};
14-
use log::{error, info};
11+
use log::info;
1512
use std::collections::HashMap;
1613

1714
impl BillService {
@@ -74,7 +71,7 @@ impl BillService {
7471
if let Some(signer_identity) =
7572
IdentityPublicData::new(identity.identity.clone())
7673
{
77-
let chain = self
74+
let _ = self
7875
.execute_bill_action(
7976
bill_id,
8077
BillAction::Recourse(
@@ -88,23 +85,6 @@ impl BillService {
8885
now,
8986
)
9087
.await?;
91-
92-
if let Err(e) = self
93-
.propagate_block(bill_id, chain.get_latest_block())
94-
.await
95-
{
96-
error!("Error propagating block: {e}");
97-
}
98-
99-
if let Err(e) = self
100-
.propagate_bill_for_node_id(
101-
bill_id,
102-
&payment_info.recoursee.node_id,
103-
)
104-
.await
105-
{
106-
error!("Error propagating bill for node_id: {e}");
107-
}
10888
}
10989
return Ok(()); // return early
11090
}
@@ -121,7 +101,7 @@ impl BillService {
121101
.iter()
122102
.any(|s| s == &identity.identity.node_id)
123103
{
124-
let chain = self
104+
let _ = self
125105
.execute_bill_action(
126106
bill_id,
127107
BillAction::Recourse(self.extend_bill_chain_identity_data_from_contacts_or_identity(
@@ -136,23 +116,6 @@ impl BillService {
136116
now,
137117
)
138118
.await?;
139-
140-
if let Err(e) = self
141-
.propagate_block(bill_id, chain.get_latest_block())
142-
.await
143-
{
144-
error!("Error propagating block: {e}");
145-
}
146-
147-
if let Err(e) = self
148-
.propagate_bill_for_node_id(
149-
bill_id,
150-
&payment_info.recoursee.node_id,
151-
)
152-
.await
153-
{
154-
error!("Error propagating bill for node_id: {e}");
155-
}
156119
}
157120
}
158121
}
@@ -185,7 +148,7 @@ impl BillService {
185148
if let Some(signer_identity) =
186149
IdentityPublicData::new(identity.identity.clone())
187150
{
188-
let chain = self
151+
let _ = self
189152
.execute_bill_action(
190153
bill_id,
191154
BillAction::Sell(
@@ -202,20 +165,6 @@ impl BillService {
202165
now,
203166
)
204167
.await?;
205-
206-
if let Err(e) = self
207-
.propagate_block(bill_id, chain.get_latest_block())
208-
.await
209-
{
210-
error!("Error propagating block: {e}");
211-
}
212-
213-
if let Err(e) = self
214-
.propagate_bill_for_node_id(bill_id, &payment_info.buyer.node_id)
215-
.await
216-
{
217-
error!("Error propagating bill for node_id: {e}");
218-
}
219168
}
220169
return Ok(()); // return early
221170
}
@@ -231,7 +180,7 @@ impl BillService {
231180
.iter()
232181
.any(|s| s == &identity.identity.node_id)
233182
{
234-
let chain = self
183+
let _ = self
235184
.execute_bill_action(
236185
bill_id,
237186
BillAction::Sell(
@@ -250,20 +199,6 @@ impl BillService {
250199
now,
251200
)
252201
.await?;
253-
254-
if let Err(e) = self
255-
.propagate_block(bill_id, chain.get_latest_block())
256-
.await
257-
{
258-
error!("Error propagating block: {e}");
259-
}
260-
261-
if let Err(e) = self
262-
.propagate_bill_for_node_id(bill_id, &payment_info.buyer.node_id)
263-
.await
264-
{
265-
error!("Error propagating bill for node_id: {e}");
266-
}
267202
}
268203
}
269204
}

0 commit comments

Comments
 (0)