Skip to content

20240813 peer container #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/common/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,9 @@ impl<X: ToClvm<NodePtr>> Sha256tree for X {
pub struct Program(pub Vec<u8>);

impl Program {
pub fn to_nodeptr(&self, allocator: &mut AllocEncoder) -> Result<NodePtr, Error> {
clvmr::serde::node_from_bytes(allocator.allocator(), &self.0).into_gen()
}
pub fn from_nodeptr(allocator: &mut AllocEncoder, n: NodePtr) -> Result<Program, Error> {
let bytes = clvmr::serde::node_to_bytes(allocator.allocator(), n).into_gen()?;
Ok(Program(bytes))
Expand All @@ -640,6 +643,10 @@ impl Program {
Program(by.to_vec())
}

pub fn bytes(&self) -> &[u8] {
&self.0
}

pub fn to_hex(&self) -> String {
hex::encode(&self.0)
}
Expand Down Expand Up @@ -724,6 +731,14 @@ impl Timeout {
}
}

impl Add for Timeout {
type Output = Timeout;

fn add(self, rhs: Self) -> Timeout {
Timeout::new(self.0 + rhs.0)
}
}

impl ToClvm<NodePtr> for Timeout {
fn to_clvm(
&self,
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ pub mod channel_handler;
mod common;
mod log;
pub mod outside;
/// Provides as simple as possible a full blockchain interface that can be spoken
/// with via a trait interface that's either local and synchronous or over a pipe.
pub mod peer_container;
mod referee;

#[cfg(test)]
Expand Down
22 changes: 17 additions & 5 deletions src/outside.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ enum GameAction {
/// the message through to the channel handler.
#[allow(dead_code)]
pub struct PotatoHandler {
initiator: bool,
have_potato: PotatoState,

handshake_state: HandshakeState,
Expand Down Expand Up @@ -456,6 +457,7 @@ impl PotatoHandler {
reward_puzzle_hash: PuzzleHash,
) -> PotatoHandler {
PotatoHandler {
initiator: have_potato,
have_potato: if have_potato {
PotatoState::Present
} else {
Expand Down Expand Up @@ -488,6 +490,10 @@ impl PotatoHandler {
}
}

pub fn is_initiator(&self) -> bool {
self.initiator
}

pub fn channel_handler(&self) -> Result<&ChannelHandler, Error> {
if let Some(ch) = &self.channel_handler {
Ok(ch)
Expand Down Expand Up @@ -608,11 +614,13 @@ impl PotatoHandler {
}
self.update_channel_coin_after_receive(penv, &spend_info)?;
}
PeerMessage::Accept(game_id, _amount, sigs) => {
PeerMessage::Accept(game_id, amount, sigs) => {
let spend_info = {
let (env, _) = penv.env();
ch.received_potato_accept(env, &sigs, &game_id)?
};
let (env, system_interface) = penv.env();
let result = ch.received_potato_accept(env, &sigs, &game_id)?;
system_interface.game_finished(&game_id, amount)?;
Ok(result)
}?;
self.update_channel_coin_after_receive(penv, &spend_info)?;
}
_ => {
Expand Down Expand Up @@ -667,6 +675,7 @@ impl PotatoHandler {
{
// Haven't got the channel coin yet.
if self.waiting_to_start {
debug!("waiting to start");
return Ok(());
}

Expand Down Expand Up @@ -767,9 +776,10 @@ impl PotatoHandler {
let (_, system_interface) = penv.env();
system_interface.send_message(&PeerMessage::Accept(
game_id.clone(),
amount,
amount.clone(),
sigs,
))?;
system_interface.game_finished(&game_id, amount)?;
self.have_potato = PotatoState::Absent;

Ok(true)
Expand Down Expand Up @@ -1203,6 +1213,8 @@ impl PotatoHandler {
match msg_envelope {
PeerMessage::HandshakeF { bundle } => {
self.channel_finished_transaction = Some(bundle.clone());
let (_, system_interface) = penv.env();
system_interface.received_channel_offer(&bundle)?;
}
PeerMessage::RequestPotato(_) => {
assert!(matches!(self.have_potato, PotatoState::Present));
Expand Down
Loading
Loading