Skip to content

Commit

Permalink
feat(network): add an extra ergonomic method for n2c chainsync (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quantumplation committed Apr 16, 2024
1 parent 97a32c9 commit 1724f6a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/n2c-miniprotocols/src/main.rs
Expand Up @@ -96,7 +96,7 @@ async fn do_chainsync(client: &mut NodeClient) {
info!("intersected point is {:?}", point);

loop {
let next = client.chainsync().request_next().await.unwrap();
let next = client.chainsync().request_or_await_next().await.unwrap();
match next {
chainsync::NextResponse::RollForward(h, _) => {
let block_number = MultiEraBlock::decode(&h).unwrap().number();
Expand Down
14 changes: 14 additions & 0 deletions pallas-network/src/miniprotocols/chainsync/client.rs
Expand Up @@ -280,6 +280,20 @@ where
self.recv_while_can_await().await
}

/// Either requests the next block, or waits for one to become available.
///
/// # Errors
///
/// Returns an error if the message cannot be sent, or if the inbound
/// message is invalid
pub async fn request_or_await_next(&mut self) -> Result<NextResponse<O>, ClientError> {
if self.has_agency() {
self.request_next().await
} else {
self.recv_while_must_reply().await
}
}

/// Attempt to intersect the chain at its origin (genesis block)
///
/// # Errors
Expand Down

0 comments on commit 1724f6a

Please sign in to comment.