Skip to content

Commit 337cce4

Browse files
authored
Test bill block data validation, add recourse reason to recourse block (#464)
1 parent a11ad77 commit 337cce4

File tree

15 files changed

+528
-141
lines changed

15 files changed

+528
-141
lines changed

CHANGELOG.md

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

33
* Add in-depth tests for bill validation
4+
* Add recourse reason to `Recourse` block data
5+
* (breaks existing persisted bills, if they had a recourse block)
46

57
# 0.3.3
68

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,17 @@ impl BillService {
129129
timestamp,
130130
)?
131131
}
132-
BillAction::Recourse(recoursee, sum, currency) => {
132+
BillAction::Recourse(recoursee, sum, currency, recourse_reason) => {
133+
let reason = match *recourse_reason {
134+
RecourseReason::Accept => BillRecourseReasonBlockData::Accept,
135+
RecourseReason::Pay(_, _) => BillRecourseReasonBlockData::Pay,
136+
};
133137
let block_data = BillRecourseBlockData {
134138
recourser: signer_public_data.clone().into(),
135139
recoursee: recoursee.clone().into(),
136140
sum: *sum,
137141
currency: currency.to_owned(),
142+
recourse_reason: reason,
138143
signatory: signing_keys.signatory_identity,
139144
signing_timestamp: timestamp,
140145
signing_address: signer_public_data.postal_address.clone(),

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,9 @@ pub mod tests {
166166
persistence,
167167
service::company_service::tests::get_baseline_company_data,
168168
tests::tests::{
169-
TEST_BILL_ID, TEST_PRIVATE_KEY_SECP, TEST_PUB_KEY_SECP, empty_address,
170-
empty_identity_public_data, identity_public_data_only_node_id, init_test_cfg,
169+
TEST_BILL_ID, TEST_PRIVATE_KEY_SECP, TEST_PUB_KEY_SECP, VALID_PAYMENT_ADDRESS_TESTNET,
170+
empty_address, empty_identity_public_data, identity_public_data_only_node_id,
171+
init_test_cfg,
171172
},
172173
util,
173174
};
@@ -2468,7 +2469,7 @@ pub mod tests {
24682469
buyer,
24692470
15000,
24702471
"sat".to_string(),
2471-
"tb1qteyk7pfvvql2r2zrsu4h4xpvju0nz7ykvguyk0".to_string(),
2472+
VALID_PAYMENT_ADDRESS_TESTNET.to_string(),
24722473
),
24732474
&IdentityPublicData::new(identity.identity.clone()).unwrap(),
24742475
&identity.key_pair,
@@ -2528,7 +2529,7 @@ pub mod tests {
25282529
buyer,
25292530
15000,
25302531
"sat".to_string(),
2531-
"tb1qteyk7pfvvql2r2zrsu4h4xpvju0nz7ykvguyk0".to_string(),
2532+
VALID_PAYMENT_ADDRESS_TESTNET.to_string(),
25322533
),
25332534
&IdentityPublicData::new(identity.identity.clone()).unwrap(),
25342535
&identity.key_pair,
@@ -2560,7 +2561,7 @@ pub mod tests {
25602561
identity_public_data_only_node_id(BcrKeys::new().get_public_key()),
25612562
15000,
25622563
"sat".to_string(),
2563-
"tb1qteyk7pfvvql2r2zrsu4h4xpvju0nz7ykvguyk0".to_string(),
2564+
VALID_PAYMENT_ADDRESS_TESTNET.to_string(),
25642565
),
25652566
&IdentityPublicData::new(identity.identity.clone()).unwrap(),
25662567
&identity.key_pair,
@@ -2588,7 +2589,7 @@ pub mod tests {
25882589
identity_public_data_only_node_id(BcrKeys::new().get_public_key()),
25892590
15000,
25902591
"sat".to_string(),
2591-
"tb1qteyk7pfvvql2r2zrsu4h4xpvju0nz7ykvguyk0".to_string(),
2592+
VALID_PAYMENT_ADDRESS_TESTNET.to_string(),
25922593
),
25932594
&IdentityPublicData::new(identity.identity.clone()).unwrap(),
25942595
&identity.key_pair,
@@ -3099,7 +3100,7 @@ pub mod tests {
30993100
seller: endorse_endorsee.clone().into(),
31003101
currency: "sat".to_string(),
31013102
sum: 15000,
3102-
payment_address: "tb1qteyk7pfvvql2r2zrsu4h4xpvju0nz7ykvguyk0".to_string(),
3103+
payment_address: VALID_PAYMENT_ADDRESS_TESTNET.to_string(),
31033104
signatory: None,
31043105
signing_timestamp: now + 2,
31053106
signing_address: empty_address(),
@@ -3285,7 +3286,7 @@ pub mod tests {
32853286
seller: endorse_endorsee.clone().into(),
32863287
currency: "sat".to_string(),
32873288
sum: 15000,
3288-
payment_address: "tb1qteyk7pfvvql2r2zrsu4h4xpvju0nz7ykvguyk0".to_string(),
3289+
payment_address: VALID_PAYMENT_ADDRESS_TESTNET.to_string(),
32893290
signatory: None,
32903291
signing_timestamp: now + 2,
32913292
signing_address: empty_address(),
@@ -3979,7 +3980,12 @@ pub mod tests {
39793980
let res = service
39803981
.execute_bill_action(
39813982
TEST_BILL_ID,
3982-
BillAction::Recourse(recoursee, 15000, "sat".to_string()),
3983+
BillAction::Recourse(
3984+
recoursee,
3985+
15000,
3986+
"sat".to_string(),
3987+
RecourseReason::Pay(15000, "sat".into()),
3988+
),
39833989
&IdentityPublicData::new(identity.identity.clone()).unwrap(),
39843990
&identity.key_pair,
39853991
1731593928,

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ use super::Result;
22
use super::service::BillService;
33
use crate::service::bill_service::{BillAction, BillServiceApi};
44
use bcr_ebill_core::{
5+
bill::RecourseReason,
56
blockchain::{
67
Blockchain,
7-
bill::{BillOpCode, OfferToSellWaitingForPayment, RecourseWaitingForPayment},
8+
bill::{
9+
BillOpCode, OfferToSellWaitingForPayment, RecourseWaitingForPayment,
10+
block::BillRecourseReasonBlockData,
11+
},
812
},
913
company::{Company, CompanyKeys},
1014
contact::IdentityPublicData,
@@ -88,6 +92,13 @@ impl BillService {
8892
if let Some(signer_identity) =
8993
IdentityPublicData::new(identity.identity.clone())
9094
{
95+
let reason = match payment_info.reason {
96+
BillRecourseReasonBlockData::Pay => RecourseReason::Pay(
97+
payment_info.sum,
98+
payment_info.currency.clone(),
99+
),
100+
BillRecourseReasonBlockData::Accept => RecourseReason::Accept,
101+
};
91102
let _ = self
92103
.execute_bill_action(
93104
bill_id,
@@ -97,7 +108,7 @@ impl BillService {
97108
&identity.identity,
98109
&contacts
99110
)
100-
.await, payment_info.sum, payment_info.currency),
111+
.await, payment_info.sum, payment_info.currency, reason),
101112
&signer_identity,
102113
&identity.key_pair,
103114
now,
@@ -119,6 +130,13 @@ impl BillService {
119130
.iter()
120131
.any(|s| s == &identity.identity.node_id)
121132
{
133+
let reason = match payment_info.reason {
134+
BillRecourseReasonBlockData::Pay => RecourseReason::Pay(
135+
payment_info.sum,
136+
payment_info.currency.clone(),
137+
),
138+
BillRecourseReasonBlockData::Accept => RecourseReason::Accept,
139+
};
122140
let _ = self
123141
.execute_bill_action(
124142
bill_id,
@@ -127,7 +145,7 @@ impl BillService {
127145
&identity.identity,
128146
&contacts
129147
)
130-
.await, payment_info.sum, payment_info.currency),
148+
.await, payment_info.sum, payment_info.currency, reason),
131149
// signer identity (company)
132150
&IdentityPublicData::from(recourser_company.0.clone()),
133151
// signer keys (company keys)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl BillService {
5656
.send_recourse_action_event(&chain_event, action_type, recoursee)
5757
.await?;
5858
}
59-
BillAction::Recourse(recoursee, _, _) => {
59+
BillAction::Recourse(recoursee, _, _, _) => {
6060
self.notification_service
6161
.send_bill_recourse_paid_event(&chain_event, recoursee)
6262
.await?;

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::{
1010
MockBillChainStoreApiMock, MockBillStoreApiMock, MockCompanyChainStoreApiMock,
1111
MockCompanyStoreApiMock, MockContactStoreApiMock, MockFileUploadStoreApiMock,
1212
MockIdentityChainStoreApiMock, MockIdentityStoreApiMock, MockNotificationService,
13-
TEST_BILL_ID, TEST_PRIVATE_KEY_SECP, TEST_PUB_KEY_SECP, empty_address,
14-
empty_bitcredit_bill, empty_identity, empty_identity_public_data,
13+
TEST_BILL_ID, TEST_PRIVATE_KEY_SECP, TEST_PUB_KEY_SECP, VALID_PAYMENT_ADDRESS_TESTNET,
14+
empty_address, empty_bitcredit_bill, empty_identity, empty_identity_public_data,
1515
identity_public_data_only_node_id,
1616
},
1717
util,
@@ -312,6 +312,7 @@ pub fn recourse_block(
312312
recoursee: recoursee.to_owned().into(),
313313
sum: 15000,
314314
currency: "sat".to_string(),
315+
recourse_reason: BillRecourseReasonBlockData::Pay,
315316
signatory: None,
316317
signing_timestamp: first_block.timestamp + 1,
317318
signing_address: empty_address(),
@@ -414,7 +415,7 @@ pub fn offer_to_sell_block(
414415
buyer: buyer.to_owned().into(),
415416
currency: "sat".to_string(),
416417
sum: 15000,
417-
payment_address: "tb1qteyk7pfvvql2r2zrsu4h4xpvju0nz7ykvguyk0".to_string(),
418+
payment_address: VALID_PAYMENT_ADDRESS_TESTNET.to_string(),
418419
signatory: None,
419420
signing_timestamp: timestamp,
420421
signing_address: empty_address(),
@@ -463,7 +464,7 @@ pub fn sell_block(id: &str, first_block: &BillBlock, buyer: &IdentityPublicData)
463464
.into(),
464465
buyer: buyer.to_owned().into(),
465466
currency: "sat".to_string(),
466-
payment_address: "tb1qteyk7pfvvql2r2zrsu4h4xpvju0nz7ykvguyk0".to_string(),
467+
payment_address: VALID_PAYMENT_ADDRESS_TESTNET.to_string(),
467468
sum: 15000,
468469
signatory: None,
469470
signing_timestamp: first_block.timestamp + 1,

crates/bcr-ebill-api/src/service/notification_service/default_service.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ mod tests {
511511
use bcr_ebill_core::blockchain::Blockchain;
512512
use bcr_ebill_core::blockchain::bill::block::{
513513
BillAcceptBlockData, BillOfferToSellBlockData, BillRecourseBlockData,
514-
BillRequestToAcceptBlockData, BillRequestToPayBlockData,
514+
BillRecourseReasonBlockData, BillRequestToAcceptBlockData, BillRequestToPayBlockData,
515515
};
516516
use bcr_ebill_core::blockchain::bill::{BillBlock, BillBlockchain};
517517
use bcr_ebill_core::util::date::now;
@@ -1468,7 +1468,8 @@ mod tests {
14681468
recourser: payee.clone().into(),
14691469
recoursee: recoursee.clone().into(),
14701470
sum: 100,
1471-
currency: "USD".to_string(),
1471+
currency: "sat".to_string(),
1472+
recourse_reason: BillRecourseReasonBlockData::Pay,
14721473
signatory: None,
14731474
signing_timestamp: timestamp,
14741475
signing_address: PostalAddress::default(),

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,4 +412,6 @@ pub mod tests {
412412

413413
pub const TEST_NODE_ID_SECP_AS_NPUB_HEX: &str =
414414
"205b8dec12bc9e879f5b517aa32192a2550e88adcee3e54ec2c7294802568fef";
415+
416+
pub const VALID_PAYMENT_ADDRESS_TESTNET: &str = "tb1qteyk7pfvvql2r2zrsu4h4xpvju0nz7ykvguyk0";
415417
}

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,22 @@ pub mod validation;
1010

1111
#[derive(Debug, Clone, PartialEq, Eq)]
1212
pub enum BillAction {
13-
Accept,
1413
RequestAcceptance,
15-
RequestToPay(String), // currency
16-
RequestRecourse(IdentityPublicData, RecourseReason), // recoursee, recourse reason
17-
Recourse(IdentityPublicData, u64, String), // recoursee, sum, currency
18-
Mint(IdentityPublicData, u64, String), // mint, sum, currency
19-
OfferToSell(IdentityPublicData, u64, String), // buyer, sum, currency
20-
Sell(IdentityPublicData, u64, String, String), // buyer, sum, currency, payment_address
21-
Endorse(IdentityPublicData), // endorsee
14+
Accept,
15+
// currency
16+
RequestToPay(String),
17+
// buyer, sum, currency
18+
OfferToSell(IdentityPublicData, u64, String),
19+
// buyer, sum, currency, payment_address
20+
Sell(IdentityPublicData, u64, String, String),
21+
// endorsee
22+
Endorse(IdentityPublicData),
23+
// recoursee, recourse reason
24+
RequestRecourse(IdentityPublicData, RecourseReason),
25+
// recoursee, sum, currency reason/
26+
Recourse(IdentityPublicData, u64, String, RecourseReason),
27+
// mint, sum, currency
28+
Mint(IdentityPublicData, u64, String),
2229
RejectAcceptance,
2330
RejectPayment,
2431
RejectBuying,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub fn validate_bill_action(
180180
}
181181
};
182182
}
183-
BillAction::Recourse(recoursee, sum, currency) => {
183+
BillAction::Recourse(recoursee, sum, currency, _reason) => {
184184
// not waiting for req to pay
185185
bill_waiting_for_req_to_pay(blockchain, maturity_date, timestamp, is_paid)?;
186186
// not waiting for offer to sell

0 commit comments

Comments
 (0)