@@ -44,34 +44,33 @@ pub async fn create_iroh(path: PathBuf) -> Result<IrohNode> {
4444// Ok(user_data.node_id().to_string())
4545// });
4646
47- host_fn ! ( iroh_blob_get_ticket( user_data: IrohNode ; ticket: & str ) -> Vec <u8 > {
48- let node = user_data. get( ) ?;
49- let node = node. lock( ) . unwrap( ) ;
47+ struct Context {
48+ rt : tokio:: runtime:: Handle ,
49+ iroh : IrohNode ,
50+ }
51+
52+ host_fn ! ( iroh_blob_get_ticket( user_data: Context ; ticket: & str ) -> Vec <u8 > {
53+ let ctx = user_data. get( ) ?;
54+ let ctx = ctx. lock( ) . unwrap( ) ;
5055
5156 let ( node_addr, hash, format) = iroh:: ticket:: BlobTicket :: from_str( ticket) . map_err( |_| anyhow!( "invalid ticket" ) ) ?. into_parts( ) ;
5257
5358 if format != iroh:: rpc_protocol:: BlobFormat :: Raw {
5459 return Err ( anyhow!( "can only get raw bytes for now, not HashSequences (collections)" ) ) ;
5560 }
56-
57- // TODO(b5): this crashes execution because we don't have a handle to the original _tokio_ runtime :(
58- let handle = tokio:: runtime:: Handle :: current( ) ;
59- let buf = handle. block_on( async move {
60- let mut stream = node. client( )
61- . blobs
62- . download( iroh:: rpc_protocol:: BlobDownloadRequest {
61+ let client = ctx. iroh. client( ) ;
62+ let buf = ctx. rt. block_on( async move {
63+ let mut stream = client. blobs. download( iroh:: rpc_protocol:: BlobDownloadRequest {
6364 hash,
6465 format,
6566 peer: node_addr,
6667 out: DownloadLocation :: Internal ,
6768 tag: SetTagOption :: Auto ,
68- } )
69- . await ?;
69+ } ) . await ?;
7070 while stream. next( ) . await . is_some( ) { }
7171
72- let buffer = node. client( ) . blobs. read( hash) . await ?. read_to_bytes( ) . await . map_err( |e| anyhow!( "read error: {}" , e) ) ?. to_vec( ) ;
73-
74- anyhow:: Ok ( buffer)
72+ let buffer = client. blobs. read( hash) . await ?. read_to_bytes( ) . await ?;
73+ anyhow:: Ok ( buffer. to_vec( ) )
7574 } ) ?;
7675
7776 Ok ( buf)
@@ -80,15 +79,18 @@ host_fn!(iroh_blob_get_ticket(user_data: IrohNode; ticket: &str) -> Vec<u8> {
8079pub fn add_all_host_functions (
8180 rt : tokio:: runtime:: Handle ,
8281 b : PluginBuilder ,
83- node : IrohNode ,
82+ iroh : IrohNode ,
8483) -> PluginBuilder {
85- let iroh = UserData :: new ( node) ;
84+ let ctx = UserData :: new ( Context {
85+ rt,
86+ iroh,
87+ } ) ;
8688
8789 b. with_function (
8890 "iroh_blob_get_ticket" ,
8991 [ PTR ] ,
9092 [ PTR ] ,
91- iroh . clone ( ) ,
93+ ctx ,
9294 iroh_blob_get_ticket,
9395 )
9496}
0 commit comments