Re-organise Alice state to be more coherent with the msg sequence

This commit is contained in:
Franck Royer 2021-02-05 16:40:11 +11:00
parent eefb1b3b16
commit a48823a665
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 38 additions and 47 deletions

View File

@ -63,47 +63,34 @@ impl Behaviour {
pub fn run(&mut self, bob: PeerId, state0: State0) {
self.inner
.do_protocol_listener(bob, move |mut substream| async move {
let message1 = state0.next_message();
let state1 = {
let message0 = serde_cbor::from_slice::<Message0>(
&substream.read_message(BUF_SIZE).await?,
)
.context("failed to deserialize message0")?;
state0.receive(message0)?
};
let message0 =
serde_cbor::from_slice::<Message0>(&substream.read_message(BUF_SIZE).await?)
.context("failed to deserialize message0")?;
let state1 = state0.receive(message0)?;
substream
.write_message(
&serde_cbor::to_vec(&message1).context("failed to serialize message1")?,
&serde_cbor::to_vec(&state1.next_message())
.context("failed to serialize message1")?,
)
.await?;
let state2 = {
let message2 = serde_cbor::from_slice::<Message2>(
&substream.read_message(BUF_SIZE).await?,
)
.context("failed to deserialize message2")?;
state1.receive(message2)
};
let message2 =
serde_cbor::from_slice::<Message2>(&substream.read_message(BUF_SIZE).await?)
.context("failed to deserialize message2")?;
let state2 = state1.receive(message2);
{
let message3 = state2.next_message();
substream
.write_message(
&serde_cbor::to_vec(&message3)
.context("failed to serialize message3")?,
)
.await?;
}
let state3 = {
let message4 = serde_cbor::from_slice::<Message4>(
&substream.read_message(BUF_SIZE).await?,
substream
.write_message(
&serde_cbor::to_vec(&state2.next_message())
.context("failed to serialize message3")?,
)
.context("failed to deserialize message4")?;
state2.receive(message4)?
};
.await?;
let message4 =
serde_cbor::from_slice::<Message4>(&substream.read_message(BUF_SIZE).await?)
.context("failed to deserialize message4")?;
let state3 = state2.receive(message4)?;
Ok(state3)
})

View File

@ -133,20 +133,6 @@ impl State0 {
}
}
pub fn next_message(&self) -> Message1 {
Message1 {
A: self.a.public(),
S_a_monero: monero::PublicKey::from_private_key(&monero::PrivateKey {
scalar: self.s_a.into_ed25519(),
}),
S_a_bitcoin: self.s_a.into_secp256k1().into(),
dleq_proof_s_a: self.dleq_proof_s_a.clone(),
v_a: self.v_a,
redeem_address: self.redeem_address.clone(),
punish_address: self.punish_address.clone(),
}
}
pub fn receive(self, msg: Message0) -> Result<State1> {
msg.dleq_proof_s_b.verify(
msg.S_b_bitcoin.clone().into(),
@ -165,6 +151,8 @@ impl State0 {
S_b_monero: msg.S_b_monero,
S_b_bitcoin: msg.S_b_bitcoin,
v,
v_a: self.v_a,
dleq_proof_s_a: self.dleq_proof_s_a,
btc: self.btc,
xmr: self.xmr,
cancel_timelock: self.cancel_timelock,
@ -184,6 +172,8 @@ pub struct State1 {
S_b_monero: monero::PublicKey,
S_b_bitcoin: bitcoin::PublicKey,
v: monero::PrivateViewKey,
v_a: monero::PrivateViewKey,
dleq_proof_s_a: cross_curve_dleq::Proof,
#[serde(with = "::bitcoin::util::amount::serde::as_sat")]
btc: bitcoin::Amount,
xmr: monero::Amount,
@ -195,6 +185,20 @@ pub struct State1 {
}
impl State1 {
pub fn next_message(&self) -> Message1 {
Message1 {
A: self.a.public(),
S_a_monero: monero::PublicKey::from_private_key(&monero::PrivateKey {
scalar: self.s_a.into_ed25519(),
}),
S_a_bitcoin: self.s_a.into_secp256k1().into(),
dleq_proof_s_a: self.dleq_proof_s_a.clone(),
v_a: self.v_a,
redeem_address: self.redeem_address.clone(),
punish_address: self.punish_address.clone(),
}
}
pub fn receive(self, msg: Message2) -> State2 {
State2 {
a: self.a,