1
1
//! Application server and client for managing MMR accumulator operations via async message passing.
2
2
3
- use std:: path:: PathBuf ;
3
+ use std:: { path:: PathBuf , sync :: Arc } ;
4
4
5
+ use accumulators:: hasher:: stark_blake:: StarkBlakeHasher ;
5
6
use bitcoin:: { block:: Header as BlockHeader , Txid } ;
6
- use raito_bitcoin_client:: BitcoinClient ;
7
7
use tokio:: sync:: { broadcast, mpsc, oneshot} ;
8
8
use tracing:: { error, info} ;
9
9
@@ -14,6 +14,8 @@ use raito_spv_mmr::{
14
14
} ;
15
15
use raito_spv_verify:: TransactionInclusionProof ;
16
16
17
+ use crate :: mmr_store:: MMRStore ;
18
+
17
19
/// Request sent to the application server via the API channel
18
20
pub struct ApiRequest {
19
21
/// The body of the API request containing the specific operation
@@ -31,8 +33,6 @@ pub enum ApiRequestBody {
31
33
/// Get MMR sparse roots for a given chain height (optional)
32
34
/// The chain height is the number of blocks in the MMR minus one
33
35
GetSparseRoots ( Option < u32 > ) ,
34
- /// Add a new block header to the MMR
35
- AddBlock ( BlockHeader ) ,
36
36
/// Generate an inclusion proof for a block at the given height and chain height (optional)
37
37
GenerateBlockProof ( ( u32 , Option < u32 > ) ) ,
38
38
/// Get a Bitcoin block header by height
@@ -43,8 +43,6 @@ pub enum ApiRequestBody {
43
43
44
44
/// Response body for API requests containing the result data
45
45
pub enum ApiResponseBody {
46
- /// Empty response
47
- Empty ,
48
46
/// Response containing the current block count
49
47
GetBlockCount ( u32 ) ,
50
48
/// Response containing the sparse roots for a given block count
@@ -99,7 +97,12 @@ impl AppServer {
99
97
info ! ( "App server started" ) ;
100
98
101
99
// We need to specify mmr_id to have deterministic keys in the database
102
- let mut mmr = BlockMMR :: from_file ( & self . config . db_path , "blocks" ) . await ?;
100
+ let mmr_id = Some ( "blocks" . to_string ( ) ) ;
101
+ let store = Arc :: new (
102
+ MMRStore :: multiple_concurrent_readers ( & self . config . db_path , mmr_id. clone ( ) ) . await ?,
103
+ ) ;
104
+ let hasher = Arc :: new ( StarkBlakeHasher :: default ( ) ) ;
105
+ let mmr = BlockMMR :: new ( store, hasher, mmr_id) ;
103
106
104
107
loop {
105
108
tokio:: select! {
@@ -117,30 +120,9 @@ impl AppServer {
117
120
let res = mmr. generate_proof( block_height, chain_height) . await . map( |proof| ApiResponseBody :: GenerateBlockProof ( proof) ) ;
118
121
req. tx_response. send( res) . map_err( |_| anyhow:: anyhow!( "Failed to send response to GenerateBlockProof request" ) ) ?;
119
122
}
120
- ApiRequestBody :: AddBlock ( block_header) => {
121
- // This is a local-only method, so we treat errors differently here
122
- mmr. add_block_header( & block_header) . await ?;
123
- let res = Ok ( ApiResponseBody :: Empty ) ;
124
- req. tx_response. send( res) . map_err( |_| anyhow:: anyhow!( "Failed to send response to AddBlock request" ) ) ?;
125
- }
126
123
ApiRequestBody :: GetBlockHeader ( block_height) => {
127
- let res = async {
128
- let bitcoin_client = BitcoinClient :: new(
129
- self . config. bitcoin_rpc_url. clone( ) ,
130
- self . config. bitcoin_rpc_userpwd. clone( ) ,
131
- ) ?;
132
-
133
- let ( block_header, _block_hash) = bitcoin_client
134
- . get_block_header_by_height( block_height)
135
- . await ?;
136
-
137
- Ok ( ApiResponseBody :: GetBlockHeader ( block_header) )
138
- }
139
- . await ;
140
-
141
- req. tx_response
142
- . send( res)
143
- . map_err( |_| anyhow:: anyhow!( "Failed to send response to GetBlockHeader request" ) ) ?;
124
+ let res = mmr. get_block_headers( block_height, 1 ) . await . map( |block_headers| ApiResponseBody :: GetBlockHeader ( block_headers[ 0 ] ) ) ;
125
+ req. tx_response. send( res) . map_err( |_| anyhow:: anyhow!( "Failed to send response to GetBlockHeader request" ) ) ?;
144
126
}
145
127
ApiRequestBody :: GetTransactionProof ( txid) => {
146
128
let res = fetch_transaction_proof(
@@ -227,17 +209,6 @@ impl AppClient {
227
209
. await
228
210
}
229
211
230
- pub async fn add_block ( & self , block_header : BlockHeader ) -> Result < ( ) , anyhow:: Error > {
231
- self . send_request (
232
- ApiRequestBody :: AddBlock ( block_header) ,
233
- |response| match response {
234
- ApiResponseBody :: Empty => Some ( ( ) ) ,
235
- _ => None ,
236
- } ,
237
- )
238
- . await
239
- }
240
-
241
212
pub async fn generate_block_proof (
242
213
& self ,
243
214
block_height : u32 ,
0 commit comments