Skip to content

Commit

Permalink
More parameterization which gives us the freedom to match things
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed Nov 12, 2024
1 parent 5442af6 commit 32d43fe
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/channel_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ impl ChannelHandler {
}
}

pub fn get_unroll_coin(&self) -> &ChannelHandlerUnrollSpendInfo {
&self.unroll
}

fn make_curried_unroll_puzzle<R: Rng>(
&self,
env: &mut ChannelHandlerEnv<R>,
Expand Down Expand Up @@ -1023,9 +1027,9 @@ impl ChannelHandler {
pub fn get_unroll_coin_transaction<R: Rng>(
&self,
env: &mut ChannelHandlerEnv<R>,
use_unroll: &ChannelHandlerUnrollSpendInfo,
) -> Result<ChannelCoinSpentResult, Error> {
assert!(self.timeout.is_some());
let use_unroll = self.get_finished_unroll_coin();

debug!("channel handler at {}", self.current_state_number);

Expand Down Expand Up @@ -1123,7 +1127,7 @@ impl ChannelHandler {
match state_number.cmp(&self.current_state_number) {
Ordering::Greater => Err(Error::StrErr(format!(
"Reply from the future onchain {} (me {}) vs {}",
state_number, self.current_state_number, self.unroll.coin.state_number
state_number, self.current_state_number, full_coin.coin.state_number
))),
Ordering::Less => {
if our_parity == their_parity {
Expand All @@ -1132,7 +1136,7 @@ impl ChannelHandler {
));
}

self.get_unroll_coin_transaction(env)
self.get_unroll_coin_transaction(env, &full_coin)
}
_ => {
// Timeout
Expand Down Expand Up @@ -1511,8 +1515,8 @@ impl ChannelHandler {
pub fn get_unroll_target<R: Rng>(
&self,
env: &mut ChannelHandlerEnv<R>,
use_unroll: &ChannelHandlerUnrollSpendInfo,
) -> Result<UnrollTarget, Error> {
let use_unroll = self.get_finished_unroll_coin();
let curried_unroll_puzzle = use_unroll
.coin
.make_curried_unroll_puzzle(env, &self.get_aggregate_unroll_public_key())?;
Expand Down
2 changes: 1 addition & 1 deletion src/channel_handler/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ pub struct HandshakeResult {
}

/// The channel handler can use these two items to produce a spend on chain.
#[derive(Default)]
#[derive(Default, Clone)]
pub struct ChannelHandlerUnrollSpendInfo {
/// Contains the half signature, puzzle and conditions needed to spend.
pub coin: UnrollCoin,
Expand Down
6 changes: 4 additions & 2 deletions src/potato_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,8 @@ impl PotatoHandler {
let (env, system_interface) = penv.env();
let player_ch = self.channel_handler()?;
// Channel coin
let pre_unroll_data = player_ch.get_unroll_coin_transaction(env)?;
let finished_unroll_coin = player_ch.get_unroll_coin();
let pre_unroll_data = player_ch.get_unroll_coin_transaction(env, &finished_unroll_coin)?;
debug!(
"unroll to on chain puzzle: {}",
pre_unroll_data.transaction.puzzle.to_hex()
Expand Down Expand Up @@ -1610,7 +1611,8 @@ impl PotatoHandler {
let ch = self.channel_handler()?;
let unroll_target = if let HandshakeState::Finished(_hs) = &self.handshake_state {
let (env, _) = penv.env();
ch.get_unroll_target(env)?
let use_unroll = ch.get_unroll_coin();
ch.get_unroll_target(env, use_unroll)?
} else {
return Err(Error::StrErr(
"go on chain before handshake finished".to_string(),
Expand Down
9 changes: 6 additions & 3 deletions src/tests/simenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ impl<'a, R: Rng> SimulatorEnvironment<'a, R> {
unroll_coin: CoinString,
) -> Result<Vec<CoinString>, Error> {
let player_ch = &mut self.parties.player(player).ch;
let pre_unroll_data = player_ch.get_unroll_coin_transaction(&mut self.env)?;
let finished_unroll_coin = player_ch.get_finished_unroll_coin();
let pre_unroll_data = player_ch.get_unroll_coin_transaction(&mut self.env, &finished_unroll_coin)?;

let run_puzzle = pre_unroll_data
.transaction
Expand Down Expand Up @@ -294,7 +295,8 @@ impl<'a, R: Rng> SimulatorEnvironment<'a, R> {
&ReadableMove::from_nodeptr(readable),
entropy,
)?;
let post_unroll_data = player_ch.get_unroll_coin_transaction(&mut self.env)?;
let finished_unroll_coin = player_ch.get_finished_unroll_coin();
let post_unroll_data = player_ch.get_unroll_coin_transaction(&mut self.env, &finished_unroll_coin)?;
debug!("post_unroll_data {post_unroll_data:?}");
todo!();
}
Expand All @@ -314,11 +316,12 @@ impl<'a, R: Rng> SimulatorEnvironment<'a, R> {
}
}
GameAction::GoOnChain(player) => {
let use_unroll = self.parties.player(*player).ch.get_finished_unroll_coin().clone();
let unroll_target = self
.parties
.player(*player)
.ch
.get_unroll_target(&mut self.env)?;
.get_unroll_target(&mut self.env, &use_unroll)?;
debug!(
"GO ON CHAIN: {} {:?} {:?}",
unroll_target.state_number, unroll_target.my_amount, unroll_target.their_amount
Expand Down

0 comments on commit 32d43fe

Please sign in to comment.