Skip to content

Dev networks - DO NOT MERGE #1688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mithril-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] }
mithril-build-script = { path = "../internal/mithril-build-script", version = "=0.2" }

[features]
default = ["rug-backend"]
default = ["rug-backend", "allow_skip_signer_certification"]

# Full feature set
full = ["fs", "test_tools"]
Expand Down
92 changes: 84 additions & 8 deletions mithril-common/src/chain_reader/pallas_chain_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ impl PallasChainReader {
.with_context(|| "PallasChainReader failed to get a client")
}

/// Check if the client already exists.
#[allow(dead_code)]
fn has_client(&self) -> bool {
self.client.is_some()
}

/// Drops the client by aborting the connection and setting it to `None`.
fn drop_client(&mut self) {
if let Some(client) = self.client.take() {
tokio::spawn(async move {
let _ = client.abort().await;
});
}
self.client = None;
}

/// Intersects the point of the chain with the given point.
async fn find_intersect_point(&mut self, point: &RawCardanoPoint) -> StdResult<()> {
let logger = self.logger.clone();
Expand Down Expand Up @@ -99,11 +115,7 @@ impl PallasChainReader {

impl Drop for PallasChainReader {
fn drop(&mut self) {
if let Some(client) = self.client.take() {
tokio::spawn(async move {
let _ = client.abort().await;
});
}
self.drop_client();
}
}

Expand All @@ -118,11 +130,18 @@ impl ChainBlockReader for PallasChainReader {
let chainsync = client.chainsync();

let next = match chainsync.has_agency() {
true => chainsync.request_next().await?,
false => chainsync.recv_while_must_reply().await?,
true => chainsync.request_next().await,
false => chainsync.recv_while_must_reply().await,
};

self.process_chain_block_next_action(next).await
match next {
Ok(next) => self.process_chain_block_next_action(next).await,
Err(err) => {
self.drop_client();

return Err(err.into());
}
}
}
}

Expand Down Expand Up @@ -375,4 +394,61 @@ mod tests {
_ => panic!("Unexpected chain block action"),
}
}

/* #[tokio::test]
async fn get_client_is_dropped_when_error_returned_from_server() {
let socket_path = create_temp_dir("get_client_is_dropped_when_error_returned_from_server")
.join("node.socket");
let known_point = get_fake_specific_point();
let known_point_clone = known_point.clone();
let server = setup_server(
socket_path.clone(),
ServerAction::RollForward,
HasAgency::No,
)
.await;
let socket_path_clone = socket_path.clone();
let client = tokio::spawn(async move {
let mut chain_reader = PallasChainReader::new(
socket_path_clone.as_path(),
CardanoNetwork::TestNet(10),
TestLogger::stdout(),
);

chain_reader
.set_chain_point(&RawCardanoPoint::from(known_point_clone))
.await
.unwrap();

// forces the client to change the chainsync server agency state
let client = chain_reader.get_client().await.unwrap();
client.chainsync().request_next().await.unwrap();

chain_reader.get_next_chain_block().await.unwrap().unwrap();

chain_reader
});

let (_, client_res) = tokio::join!(server, client);
let chain_reader = client_res.expect("Client failed to return chain reader");
let server = setup_server(
socket_path.clone(),
ServerAction::RollForward,
HasAgency::No,
)
.await;
let client = tokio::spawn(async move {
let mut chain_reader = chain_reader;

println!("RES 123");
let res = chain_reader.get_next_chain_block().await;
println!("RES 456");

res
});
let (_, client_res) = tokio::join!(server, client);
let chain_block = client_res
.unwrap()
.expect_err("Server should have returned an error");
} */
}
Loading