diff --git a/examples/n2c-miniprotocols/src/main.rs b/examples/n2c-miniprotocols/src/main.rs index 24db263..de784c3 100644 --- a/examples/n2c-miniprotocols/src/main.rs +++ b/examples/n2c-miniprotocols/src/main.rs @@ -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(); diff --git a/pallas-network/src/miniprotocols/chainsync/client.rs b/pallas-network/src/miniprotocols/chainsync/client.rs index f7eb54c..9507b3d 100644 --- a/pallas-network/src/miniprotocols/chainsync/client.rs +++ b/pallas-network/src/miniprotocols/chainsync/client.rs @@ -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, 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