@ -97,52 +97,64 @@ pub fn get_coinsupply(uri: http::Uri, no_cert: bool) -> Result<Coinsupply, Strin
let mut rt = tokio ::runtime ::Runtime ::new ( ) . map_err ( | e | e . to_string ( ) ) ? ;
rt . block_on ( get_coinsupply_info ( & uri , no_cert ) ) . map_err ( | e | e . to_string ( ) )
// tokio::runtime::current_thread::Runtime::new().unwrap().block_on(runner)
}
async fn get_block_range < F : 'static + std ::marker ::Send > ( uri : & http ::Uri , start_height : u64 , end_height : u64 , no_cert : bool , pool : ThreadPool , c : F )
-> Result < ( ) , Box < dyn std ::error ::Error > >
async fn get_block_range < F : 'static + std ::marker ::Send > (
uri : & http ::Uri ,
start_height : u64 ,
end_height : u64 ,
no_cert : bool ,
pool : ThreadPool ,
c : F
) -> Result < ( ) , Box < dyn std ::error ::Error > >
where F : Fn ( & [ u8 ] , u64 ) {
let mut client = get_client ( uri , no_cert ) . await ? ;
let bs = BlockId { height : start_height , hash : vec ! ( ) } ;
let be = BlockId { height : end_height , hash : vec ! ( ) } ;
let bs = BlockId { height : start_height , hash : vec ! [ ] } ;
let be = BlockId { height : end_height , hash : vec ! [ ] } ;
let request = Request ::new ( BlockRange { start : Some ( bs ) , end : Some ( be ) } ) ;
let request = Request ::new ( BlockRange { start : Some ( bs ) , end : Some ( be ) } ) ;
// Channel where the blocks are sent. A None signifies end of all blocks
let ( tx , rx ) = channel ::< Option < CompactBlock > > ( ) ;
// Channel that the processor signals it is done, so the method can return
let ( ftx , frx ) = channel ( ) ;
// The processor runs on a different thread, so that the network calls don't
// block on this
pool . execute ( move | | {
while let Some ( block ) = rx . recv ( ) . unwrap ( ) {
use prost ::Message ;
let mut encoded_buf = vec ! [ ] ;
block . encode ( & mut encoded_buf ) . unwrap ( ) ;
c ( & encoded_buf , block . height ) ;
}
ftx . send ( Ok ( ( ) ) ) . unwrap ( ) ;
} ) ;
let ( tx , rx ) = channel ::< Option < CompactBlock > > ( ) ;
let ( ftx , frx ) = channel ( ) ;
pool . execute ( move | | {
while let Ok ( Some ( block ) ) = rx . recv ( ) {
use prost ::Message ;
let mut encoded_buf = vec ! [ ] ;
if let Err ( e ) = block . encode ( & mut encoded_buf ) {
eprintln ! ( "Error encoding block: {:?}" , e ) ;
break ;
}
c ( & encoded_buf , block . height ) ;
}
if let Err ( e ) = ftx . send ( Ok ( ( ) ) ) {
eprintln ! ( "Error sending completion signal: {:?}" , e ) ;
}
} ) ;
let mut response = client . get_block_range ( request ) . await ? . into_inner ( ) ;
//println!("{:?}", response);
while let Some ( block ) = response . message ( ) . await ? {
tx . send ( Some ( block ) ) . unwrap ( ) ;
if let Err ( e ) = tx . send ( Some ( block ) ) {
eprintln ! ( "Error sending block to channel: {:?}" , e ) ;
break ;
}
}
if let Err ( e ) = tx . send ( None ) {
eprintln ! ( "Error sending end signal to channel: {:?}" , e ) ;
}
tx . send ( None ) . unwrap ( ) ;
// Wait for the processor to exit
frx . iter ( ) . take ( 1 ) . collect ::< Result < Vec < ( ) > , String > > ( ) ? ;
Ok ( ( ) )
}
pub fn fetch_blocks < F : 'static + std ::marker ::Send > ( uri : & http ::Uri , start_height : u64 , end_height : u64 , no_cert : bool , pool : ThreadPool , c : F ) -> Result < ( ) , String >
where F : Fn ( & [ u8 ] , u64 ) {
@ -151,7 +163,6 @@ pub fn fetch_blocks<F : 'static + std::marker::Send>(uri: &http::Uri, start_heig
Err ( e ) = > {
let es = format ! ( "Error creating runtime {:?}" , e ) ;
error ! ( "{}" , es ) ;
eprintln ! ( "{}" , e ) ;
return Err ( es ) ;
}
} ;
@ -161,31 +172,49 @@ pub fn fetch_blocks<F : 'static + std::marker::Send>(uri: &http::Uri, start_heig
Err ( e ) = > {
let e = format ! ( "Error fetching blocks {:?}" , e ) ;
error ! ( "{}" , e ) ;
eprintln ! ( "{}" , e ) ;
Err ( e )
}
}
}
// get_address_txids GRPC call
async fn get_address_txids < F : 'static + std ::marker ::Send > ( uri : & http ::Uri , address : String ,
start_height : u64 , end_height : u64 , no_cert : bool , c : F ) -> Result < ( ) , Box < dyn std ::error ::Error > >
where F : Fn ( & [ u8 ] , u64 ) {
async fn get_address_txids < F : 'static + std ::marker ::Send > (
uri : & http ::Uri ,
address : String ,
start_height : u64 ,
end_height : u64 ,
no_cert : bool ,
c : F
) -> Result < ( ) , Box < dyn std ::error ::Error > >
where F : Fn ( & [ u8 ] , u64 ) {
let mut client = match get_client ( uri , no_cert ) . await {
Ok ( client ) = > client ,
Err ( e ) = > {
eprintln ! ( "Error creating client: {:?}" , e ) ;
return Err ( e . into ( ) ) ;
}
} ;
let mut client = get_client ( uri , no_cert ) . await ? ;
let start = Some ( BlockId { height : start_height , hash : vec ! ( ) } ) ;
let end = Some ( BlockId { height : end_height , hash : vec ! ( ) } ) ;
let end = Some ( BlockId { height : end_height , hash : vec ! ( ) } ) ;
let request = Request ::new ( TransparentAddressBlockFilter { address , range : Some ( BlockRange { start , end } ) } ) ;
let request = Request ::new ( TransparentAddressBlockFilter { address , range : Some ( BlockRange { start , end } ) } ) ;
let maybe_response = match client . get_address_txids ( request ) . await {
Ok ( response ) = > response ,
Err ( e ) = > {
eprintln ! ( "Error getting address txids: {:?}" , e ) ;
return Err ( e . into ( ) ) ;
}
} ;
let maybe_response = client . get_address_txids ( request ) . await ? ;
let mut response = maybe_response . into_inner ( ) ;
while let Some ( tx ) = response . message ( ) . await ? {
c ( & tx . data , tx . height ) ;
}
Ok ( ( ) )
}
@ -223,16 +252,21 @@ where
Ok ( ( ) )
}
pub fn fetch_transparent_txids < F : 'static + std ::marker ::Send > ( uri : & http ::Uri , address : String ,
start_height : u64 , end_height : u64 , no_cert : bool , c : F ) -> Result < ( ) , String >
where F : Fn ( & [ u8 ] , u64 ) {
pub fn fetch_transparent_txids < F : 'static + std ::marker ::Send > (
uri : & http ::Uri ,
address : String ,
start_height : u64 ,
end_height : u64 ,
no_cert : bool ,
c : F
) -> Result < ( ) , String >
where F : Fn ( & [ u8 ] , u64 ) {
let mut rt = match tokio ::runtime ::Runtime ::new ( ) {
Ok ( r ) = > r ,
Err ( e ) = > {
let e = format ! ( "Error creating runtime {:?}" , e ) ;
error ! ( "{}" , e ) ;
eprintln ! ( "{}" , e ) ;
return Err ( e ) ;
}
} ;
@ -242,13 +276,11 @@ pub fn fetch_transparent_txids<F : 'static + std::marker::Send>(uri: &http::Uri,
Err ( e ) = > {
let e = format ! ( "Error with get_address_txids runtime {:?}" , e ) ;
error ! ( "{}" , e ) ;
eprintln ! ( "{}" , e ) ;
Err ( e )
return Err ( e )
}
}
}
// get_transaction GRPC call
async fn get_transaction ( uri : & http ::Uri , txid : TxId , no_cert : bool )
-> Result < RawTransaction , Box < dyn std ::error ::Error > > {
@ -266,7 +298,6 @@ pub fn fetch_full_tx(uri: &http::Uri, txid: TxId, no_cert: bool) -> Result<Vec<u
Err ( e ) = > {
let errstr = format ! ( "Error creating runtime {}" , e . to_string ( ) ) ;
error ! ( "{}" , errstr ) ;
eprintln ! ( "{}" , errstr ) ;
return Err ( errstr ) ;
}
} ;
@ -276,7 +307,6 @@ pub fn fetch_full_tx(uri: &http::Uri, txid: TxId, no_cert: bool) -> Result<Vec<u
Err ( e ) = > {
let errstr = format ! ( "Error in get_transaction runtime {}" , e . to_string ( ) ) ;
error ! ( "{}" , errstr ) ;
eprintln ! ( "{}" , errstr ) ;
Err ( errstr )
}
}