Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.4.2

* Add logic to get endorsees from plaintext chain

# 0.4.1

* Add file and pub key to bill to share with external party and add accessors to extract data
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace.package]
version = "0.4.1"
version = "0.4.2"
edition = "2024"
license = "MIT"

Expand Down
79 changes: 78 additions & 1 deletion crates/bcr-ebill-core/src/blockchain/bill/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
BillAcceptBlockData, BillBlock, BillEndorseBlockData, BillIdentParticipantBlockData,
BillIssueBlockData, BillMintBlockData, BillOfferToSellBlockData, BillParticipantBlockData,
BillRecourseBlockData, BillRejectBlockData, BillRequestRecourseBlockData,
BillRequestToAcceptBlockData, BillRequestToPayBlockData, BillSellBlockData,
BillRequestToAcceptBlockData, BillRequestToPayBlockData, BillSellBlockData, HolderFromBlock,
};
use super::{BillOpCode, RecourseWaitingForPayment};
use super::{OfferToSellWaitingForPayment, RecoursePaymentInfo};
Expand Down Expand Up @@ -44,8 +44,55 @@
Err(Error::BlockInvalid)
}
}

pub fn get_holder(&self) -> Result<Option<HolderFromBlock>> {
match self.block.op_code() {
BillOpCode::Issue => {
let bill: BillIssueBlockData = borsh::from_slice(&self.plaintext_data_bytes)?;
Ok(Some(HolderFromBlock {
holder: bill.payee,
signer: BillParticipantBlockData::Ident(bill.drawer),
signatory: bill.signatory,
}))

Check warning on line 56 in crates/bcr-ebill-core/src/blockchain/bill/chain.rs

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L51 - L56 were not covered by tests
}
BillOpCode::Endorse => {
let block: BillEndorseBlockData = borsh::from_slice(&self.plaintext_data_bytes)?;
Ok(Some(HolderFromBlock {
holder: block.endorsee,
signer: block.endorser,
signatory: block.signatory,
}))
}
BillOpCode::Mint => {
let block: BillMintBlockData = borsh::from_slice(&self.plaintext_data_bytes)?;
Ok(Some(HolderFromBlock {
holder: block.endorsee,
signer: block.endorser,
signatory: block.signatory,
}))
}
BillOpCode::Sell => {
let block: BillSellBlockData = borsh::from_slice(&self.plaintext_data_bytes)?;
Ok(Some(HolderFromBlock {
holder: block.buyer,
signer: block.seller,
signatory: block.signatory,
}))
}
BillOpCode::Recourse => {
let block: BillRecourseBlockData = borsh::from_slice(&self.plaintext_data_bytes)?;
Ok(Some(HolderFromBlock {
holder: BillParticipantBlockData::Ident(block.recoursee),
signer: BillParticipantBlockData::Ident(block.recourser),
signatory: block.signatory,
}))
}
_ => Ok(None),
}
}
}

/// Gets bill parties from blocks with their plaintext data
pub fn get_bill_parties_from_chain_with_plaintext(
chain_with_plaintext: &[BillBlockPlaintextWrapper],
) -> Result<BillParties> {
Expand Down Expand Up @@ -151,6 +198,26 @@
})
}

/// Gets endorsees from blocks with their plaintext data
pub fn get_endorsees_from_chain_with_plaintext(
chain_with_plaintext: &[BillBlockPlaintextWrapper],
) -> Vec<BillParticipant> {
let mut result: Vec<BillParticipant> = vec![];
// iterate from the front to the back, collecting all endorsement blocks
for block_wrapper in chain_with_plaintext.iter() {
// we ignore issue blocks, since we are only interested in endorsements
if block_wrapper.block.op_code == BillOpCode::Issue {
Comment thread
zupzup marked this conversation as resolved.
continue;
}
if let Ok(Some(holder_from_block)) = block_wrapper.get_holder() {
let holder = holder_from_block.holder;
result.push(holder.into());
}
}

result
Comment thread
zupzup marked this conversation as resolved.
}

#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, Clone)]
pub struct BillBlockchain {
blocks: Vec<BillBlock>,
Expand Down Expand Up @@ -1349,5 +1416,15 @@
)
.unwrap()
);

assert_eq!(
chain.get_endorsees_for_bill(&bill_keys),
get_endorsees_from_chain_with_plaintext(
chain
.get_chain_with_plaintext_block_data(&bill_keys)
.as_ref()
.unwrap()
)
)
}
}
Loading