Skip to content

Commit 68b9474

Browse files
authored
fix: drop connection instead of manual close, enable deferred decryption (#472)
1 parent b4334ad commit 68b9474

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

tlsn/examples/interactive/interactive.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use futures::AsyncWriteExt;
21
use http_body_util::Empty;
32
use hyper::{body::Bytes, Request, StatusCode, Uri};
43
use hyper_util::rt::TokioIo;
@@ -49,6 +48,8 @@ async fn prover<T: AsyncWrite + AsyncRead + Send + Unpin + 'static>(
4948
let server_port = uri.port_u16().unwrap_or(443);
5049

5150
// Create prover and connect to verifier.
51+
//
52+
// Perform the setup phase with the verifier.
5253
let prover = Prover::new(
5354
ProverConfig::builder()
5455
.id(id)
@@ -64,9 +65,18 @@ async fn prover<T: AsyncWrite + AsyncRead + Send + Unpin + 'static>(
6465
let tls_client_socket = tokio::net::TcpStream::connect((server_domain, server_port))
6566
.await
6667
.unwrap();
68+
69+
// Pass server connection into the prover.
6770
let (mpc_tls_connection, prover_fut) =
6871
prover.connect(tls_client_socket.compat()).await.unwrap();
72+
73+
// Grab a controller for the Prover so we can enable deferred decryption.
74+
let ctrl = prover_fut.control();
75+
76+
// Wrap the connection in a TokioIo compatibility layer to use it with hyper.
6977
let mpc_tls_connection = TokioIo::new(mpc_tls_connection.compat());
78+
79+
// Spawn the Prover to run in the background.
7080
let prover_task = tokio::spawn(prover_fut);
7181

7282
// MPC-TLS Handshake.
@@ -75,7 +85,12 @@ async fn prover<T: AsyncWrite + AsyncRead + Send + Unpin + 'static>(
7585
.await
7686
.unwrap();
7787

78-
let connection_task = tokio::spawn(connection.without_shutdown());
88+
// Spawn the connection to run in the background.
89+
tokio::spawn(connection);
90+
91+
// Enable deferred decryption. This speeds up the proving time, but doesn't
92+
// let us see the decrypted data until after the connection is closed.
93+
ctrl.defer_decryption().await.unwrap();
7994

8095
// MPC-TLS: Send Request and wait for Response.
8196
let request = Request::builder()
@@ -90,10 +105,6 @@ async fn prover<T: AsyncWrite + AsyncRead + Send + Unpin + 'static>(
90105

91106
assert!(response.status() == StatusCode::OK);
92107

93-
// Close TLS Connection.
94-
let tls_connection = connection_task.await.unwrap().unwrap().io.into_inner();
95-
tls_connection.compat().close().await.unwrap();
96-
97108
// Create proof for the Verifier.
98109
let mut prover = prover_task.await.unwrap().unwrap().start_prove();
99110
redact_and_reveal_received_data(&mut prover);
@@ -128,6 +139,7 @@ async fn verifier<T: AsyncWrite + AsyncRead + Send + Sync + Unpin + 'static>(
128139
response
129140
.find("BEGIN PUBLIC KEY")
130141
.expect("Expected valid public key in JSON response");
142+
131143
// Check Session info: server name.
132144
assert_eq!(session_info.server_name.as_str(), SERVER_DOMAIN);
133145

0 commit comments

Comments
 (0)