@@ -5,7 +5,9 @@ use async_trait::async_trait;
5
5
6
6
use mithril_common:: {
7
7
crypto_helper:: { MKMap , MKMapNode , MKTree , MKTreeNode } ,
8
- entities:: { Beacon , BlockRange , CardanoTransactionsSetProof , TransactionHash } ,
8
+ entities:: {
9
+ Beacon , BlockRange , CardanoTransaction , CardanoTransactionsSetProof , TransactionHash ,
10
+ } ,
9
11
signable_builder:: TransactionRetriever ,
10
12
StdResult ,
11
13
} ;
@@ -37,24 +39,15 @@ impl MithrilProverService {
37
39
transaction_retriever,
38
40
}
39
41
}
40
- }
41
42
42
- #[ async_trait]
43
- impl ProverService for MithrilProverService {
44
- async fn compute_transactions_proofs (
43
+ fn compute_merkle_map_from_transactions (
45
44
& self ,
46
- up_to : & Beacon ,
47
- transaction_hashes : & [ TransactionHash ] ,
48
- ) -> StdResult < Vec < CardanoTransactionsSetProof > > {
49
- let transactions = self . transaction_retriever . get_up_to ( up_to) . await ?;
50
- let mut transactions_to_certify = vec ! [ ] ;
45
+ transactions : & [ CardanoTransaction ] ,
46
+ ) -> StdResult < MKMap < BlockRange , MKMapNode < BlockRange > > > {
51
47
let mut transactions_by_block_ranges: HashMap < BlockRange , Vec < TransactionHash > > =
52
48
HashMap :: new ( ) ;
53
- for transaction in & transactions {
49
+ for transaction in transactions {
54
50
let block_range = BlockRange :: from_block_number ( transaction. block_number ) ;
55
- if transaction_hashes. contains ( & transaction. transaction_hash ) {
56
- transactions_to_certify. push ( ( block_range. clone ( ) , transaction) ) ;
57
- }
58
51
transactions_by_block_ranges
59
52
. entry ( block_range)
60
53
. or_default ( )
@@ -72,26 +65,47 @@ impl ProverService for MithrilProverService {
72
65
) ?
73
66
. as_slice ( ) ,
74
67
)
75
- . with_context ( || "ProverService failed to compute MKHashMap " ) ?;
68
+ . with_context ( || "ProverService failed to compute the merkelized structure that proves ownership of the transaction " ) ?;
76
69
70
+ Ok ( mk_hash_map)
71
+ }
72
+ }
73
+
74
+ #[ async_trait]
75
+ impl ProverService for MithrilProverService {
76
+ async fn compute_transactions_proofs (
77
+ & self ,
78
+ up_to : & Beacon ,
79
+ transaction_hashes : & [ TransactionHash ] ,
80
+ ) -> StdResult < Vec < CardanoTransactionsSetProof > > {
81
+ let transactions = self . transaction_retriever . get_up_to ( up_to) . await ?;
82
+ let mk_map = self . compute_merkle_map_from_transactions ( & transactions) ?;
83
+ let transactions_to_prove = transactions
84
+ . iter ( )
85
+ . filter_map ( |transaction| {
86
+ let block_range = BlockRange :: from_block_number ( transaction. block_number ) ;
87
+ transaction_hashes
88
+ . contains ( & transaction. transaction_hash )
89
+ . then ( || ( block_range, transaction. to_owned ( ) ) )
90
+ } )
91
+ . collect :: < Vec < _ > > ( ) ;
77
92
let mut transaction_hashes_certified = vec ! [ ] ;
78
- for ( _block_range, transaction) in transactions_to_certify {
93
+ for ( _block_range, transaction) in transactions_to_prove {
79
94
let mk_tree_node_transaction_hash: MKTreeNode =
80
95
transaction. transaction_hash . to_owned ( ) . into ( ) ;
81
- if mk_hash_map
96
+ if mk_map
82
97
. compute_proof ( & [ mk_tree_node_transaction_hash] )
83
98
. is_ok ( )
84
99
{
85
100
transaction_hashes_certified. push ( transaction. transaction_hash . to_string ( ) ) ;
86
101
}
87
102
}
88
-
89
103
if !transaction_hashes_certified. is_empty ( ) {
90
104
let mk_leaves: Vec < MKTreeNode > = transaction_hashes_certified
91
105
. iter ( )
92
106
. map ( |h| h. to_owned ( ) . into ( ) )
93
107
. collect ( ) ;
94
- let mk_proof = mk_hash_map . compute_proof ( & mk_leaves) ?;
108
+ let mk_proof = mk_map . compute_proof ( & mk_leaves) ?;
95
109
let transactions_set_proof_batch =
96
110
CardanoTransactionsSetProof :: new ( transaction_hashes_certified, mk_proof) ;
97
111
0 commit comments