@@ -4,7 +4,7 @@ use super::block::{
4
4
BillAcceptBlockData , BillBlock , BillEndorseBlockData , BillIdentParticipantBlockData ,
5
5
BillIssueBlockData , BillMintBlockData , BillOfferToSellBlockData , BillParticipantBlockData ,
6
6
BillRecourseBlockData , BillRejectBlockData , BillRequestRecourseBlockData ,
7
- BillRequestToAcceptBlockData , BillRequestToPayBlockData , BillSellBlockData ,
7
+ BillRequestToAcceptBlockData , BillRequestToPayBlockData , BillSellBlockData , HolderFromBlock ,
8
8
} ;
9
9
use super :: { BillOpCode , RecourseWaitingForPayment } ;
10
10
use super :: { OfferToSellWaitingForPayment , RecoursePaymentInfo } ;
@@ -44,8 +44,55 @@ impl BillBlockPlaintextWrapper {
44
44
Err ( Error :: BlockInvalid )
45
45
}
46
46
}
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
+ }
47
93
}
48
94
95
+ /// Gets bill parties from blocks with their plaintext data
49
96
pub fn get_bill_parties_from_chain_with_plaintext (
50
97
chain_with_plaintext : & [ BillBlockPlaintextWrapper ] ,
51
98
) -> Result < BillParties > {
@@ -151,6 +198,26 @@ pub fn get_bill_parties_from_chain_with_plaintext(
151
198
} )
152
199
}
153
200
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
+
154
221
#[ derive( BorshSerialize , BorshDeserialize , Serialize , Deserialize , Debug , Clone ) ]
155
222
pub struct BillBlockchain {
156
223
blocks : Vec < BillBlock > ,
@@ -1349,5 +1416,15 @@ mod tests {
1349
1416
)
1350
1417
. unwrap( )
1351
1418
) ;
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
+ )
1352
1429
}
1353
1430
}
0 commit comments