|
| 1 | +use futures::future::{try_select, Either}; |
| 2 | + |
| 3 | +// send our own commitment reveal message only after we have received the prelude. |
| 4 | + |
| 5 | +// everything that needs the challenge has to wait until we have received their commitment reveal message. |
| 6 | + |
| 7 | +use ufotofu::local_nb::{BulkConsumer, BulkProducer}; |
| 8 | +use willow_encoding::Encodable; |
| 9 | + |
| 10 | +use crate::{ChallengeHash, CommitmentReveal}; |
| 11 | + |
| 12 | +use super::{ |
| 13 | + receive_prelude::{receive_prelude, ReceivePreludeError, ReceivedPrelude}, |
| 14 | + send_prelude::send_prelude, |
| 15 | +}; |
| 16 | + |
| 17 | +/// An error which only occurs during the initial phase of a WGPS session. |
| 18 | +pub enum ExecutePreludeError<E> { |
| 19 | + /// There was a problem receiving their prelude. |
| 20 | + ReceiveError(ReceivePreludeError<E>), |
| 21 | + /// There was a problem sending our prelude. |
| 22 | + SendError(E), |
| 23 | +} |
| 24 | + |
| 25 | +impl<E> From<ReceivePreludeError<E>> for ExecutePreludeError<E> { |
| 26 | + fn from(value: ReceivePreludeError<E>) -> Self { |
| 27 | + ExecutePreludeError::ReceiveError(value) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl<E> From<E> for ExecutePreludeError<E> { |
| 32 | + fn from(value: E) -> Self { |
| 33 | + ExecutePreludeError::SendError(value) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl<E: core::fmt::Display> core::fmt::Display for ExecutePreludeError<E> { |
| 38 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 39 | + match self { |
| 40 | + ExecutePreludeError::ReceiveError(receive_prelude_error) => { |
| 41 | + write!(f, "{}", receive_prelude_error) |
| 42 | + } |
| 43 | + ExecutePreludeError::SendError(error) => write!(f, "{}", error), |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// Given a consumer and producer, send a max payload size and commitment, and wait for the other side's corresponding `ReceivedPrelude`. Then send a `CommitmentReveal` message, before finally returning the received prelude. |
| 49 | +/// |
| 50 | +/// Attention: waiting for the other peer's prelude means that we delay sending our first messages, even though technically we would be allowed to do that. PRs welcome. |
| 51 | +pub(crate) async fn execute_prelude< |
| 52 | + const CHALLENGE_LENGTH: usize, |
| 53 | + const CHALLENGE_HASH_LENGTH: usize, |
| 54 | + CH: ChallengeHash<CHALLENGE_LENGTH, CHALLENGE_HASH_LENGTH>, |
| 55 | + E, |
| 56 | + C: BulkConsumer<Item = u8, Error = E>, |
| 57 | + P: BulkProducer<Item = u8, Error = E>, |
| 58 | +>( |
| 59 | + max_payload_power: u8, |
| 60 | + our_nonce: [u8; CHALLENGE_LENGTH], |
| 61 | + consumer: &mut C, |
| 62 | + producer: &mut P, |
| 63 | +) -> Result<ReceivedPrelude<CHALLENGE_HASH_LENGTH>, ExecutePreludeError<E>> { |
| 64 | + let commitment = CH::hash(our_nonce); |
| 65 | + |
| 66 | + let receive_fut = Box::pin(receive_prelude::<CHALLENGE_HASH_LENGTH, _>(producer)); |
| 67 | + let send_fut = Box::pin(send_prelude(max_payload_power, commitment, consumer)); |
| 68 | + |
| 69 | + let (received_prelude, ()) = match try_select(receive_fut, send_fut).await { |
| 70 | + Ok(Either::Left((received, send_fut))) => (received, send_fut.await?), |
| 71 | + Ok(Either::Right(((), receive_fut))) => (receive_fut.await?, ()), |
| 72 | + Err(Either::Left((error, _))) => return Err(error.into()), |
| 73 | + Err(Either::Right((error, _))) => return Err(error.into()), |
| 74 | + }; |
| 75 | + |
| 76 | + let msg = CommitmentReveal { nonce: our_nonce }; |
| 77 | + msg.encode(consumer).await?; |
| 78 | + |
| 79 | + Ok(received_prelude) |
| 80 | +} |
0 commit comments