Skip to content

Commit 945a756

Browse files
authored
Add get endorsees for plaintext chain (#577)
1 parent ff135d8 commit 945a756

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.4.2
2+
3+
* Add logic to get endorsees from plaintext chain
4+
15
# 0.4.1
26

37
* Add file and pub key to bill to share with external party and add accessors to extract data

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace.package]
2-
version = "0.4.1"
2+
version = "0.4.2"
33
edition = "2024"
44
license = "MIT"
55

crates/bcr-ebill-core/src/blockchain/bill/chain.rs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::block::{
44
BillAcceptBlockData, BillBlock, BillEndorseBlockData, BillIdentParticipantBlockData,
55
BillIssueBlockData, BillMintBlockData, BillOfferToSellBlockData, BillParticipantBlockData,
66
BillRecourseBlockData, BillRejectBlockData, BillRequestRecourseBlockData,
7-
BillRequestToAcceptBlockData, BillRequestToPayBlockData, BillSellBlockData,
7+
BillRequestToAcceptBlockData, BillRequestToPayBlockData, BillSellBlockData, HolderFromBlock,
88
};
99
use super::{BillOpCode, RecourseWaitingForPayment};
1010
use super::{OfferToSellWaitingForPayment, RecoursePaymentInfo};
@@ -44,8 +44,55 @@ impl BillBlockPlaintextWrapper {
4444
Err(Error::BlockInvalid)
4545
}
4646
}
47+
48+
pub fn get_holder(&self) -> Result<Option<HolderFromBlock>> {
49+
match self.block.op_code() {
50+
BillOpCode::Issue => {
51+
let bill: BillIssueBlockData = borsh::from_slice(&self.plaintext_data_bytes)?;
52+
Ok(Some(HolderFromBlock {
53+
holder: bill.payee,
54+
signer: BillParticipantBlockData::Ident(bill.drawer),
55+
signatory: bill.signatory,
56+
}))
57+
}
58+
BillOpCode::Endorse => {
59+
let block: BillEndorseBlockData = borsh::from_slice(&self.plaintext_data_bytes)?;
60+
Ok(Some(HolderFromBlock {
61+
holder: block.endorsee,
62+
signer: block.endorser,
63+
signatory: block.signatory,
64+
}))
65+
}
66+
BillOpCode::Mint => {
67+
let block: BillMintBlockData = borsh::from_slice(&self.plaintext_data_bytes)?;
68+
Ok(Some(HolderFromBlock {
69+
holder: block.endorsee,
70+
signer: block.endorser,
71+
signatory: block.signatory,
72+
}))
73+
}
74+
BillOpCode::Sell => {
75+
let block: BillSellBlockData = borsh::from_slice(&self.plaintext_data_bytes)?;
76+
Ok(Some(HolderFromBlock {
77+
holder: block.buyer,
78+
signer: block.seller,
79+
signatory: block.signatory,
80+
}))
81+
}
82+
BillOpCode::Recourse => {
83+
let block: BillRecourseBlockData = borsh::from_slice(&self.plaintext_data_bytes)?;
84+
Ok(Some(HolderFromBlock {
85+
holder: BillParticipantBlockData::Ident(block.recoursee),
86+
signer: BillParticipantBlockData::Ident(block.recourser),
87+
signatory: block.signatory,
88+
}))
89+
}
90+
_ => Ok(None),
91+
}
92+
}
4793
}
4894

95+
/// Gets bill parties from blocks with their plaintext data
4996
pub fn get_bill_parties_from_chain_with_plaintext(
5097
chain_with_plaintext: &[BillBlockPlaintextWrapper],
5198
) -> Result<BillParties> {
@@ -151,6 +198,26 @@ pub fn get_bill_parties_from_chain_with_plaintext(
151198
})
152199
}
153200

201+
/// Gets endorsees from blocks with their plaintext data
202+
pub fn get_endorsees_from_chain_with_plaintext(
203+
chain_with_plaintext: &[BillBlockPlaintextWrapper],
204+
) -> Vec<BillParticipant> {
205+
let mut result: Vec<BillParticipant> = vec![];
206+
// iterate from the front to the back, collecting all endorsement blocks
207+
for block_wrapper in chain_with_plaintext.iter() {
208+
// we ignore issue blocks, since we are only interested in endorsements
209+
if block_wrapper.block.op_code == BillOpCode::Issue {
210+
continue;
211+
}
212+
if let Ok(Some(holder_from_block)) = block_wrapper.get_holder() {
213+
let holder = holder_from_block.holder;
214+
result.push(holder.into());
215+
}
216+
}
217+
218+
result
219+
}
220+
154221
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, Clone)]
155222
pub struct BillBlockchain {
156223
blocks: Vec<BillBlock>,
@@ -1349,5 +1416,15 @@ mod tests {
13491416
)
13501417
.unwrap()
13511418
);
1419+
1420+
assert_eq!(
1421+
chain.get_endorsees_for_bill(&bill_keys),
1422+
get_endorsees_from_chain_with_plaintext(
1423+
chain
1424+
.get_chain_with_plaintext_block_data(&bill_keys)
1425+
.as_ref()
1426+
.unwrap()
1427+
)
1428+
)
13521429
}
13531430
}

0 commit comments

Comments
 (0)