Skip to content

Commit 3cdd83e

Browse files
Merge pull request #10 from Chia-Network/20240813-peer-container
20240813 peer container
2 parents 64689ea + d99c8ff commit 3cdd83e

File tree

6 files changed

+1014
-330
lines changed

6 files changed

+1014
-330
lines changed

src/common/types.rs

+15
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,9 @@ impl<X: ToClvm<NodePtr>> Sha256tree for X {
631631
pub struct Program(pub Vec<u8>);
632632

633633
impl Program {
634+
pub fn to_nodeptr(&self, allocator: &mut AllocEncoder) -> Result<NodePtr, Error> {
635+
clvmr::serde::node_from_bytes(allocator.allocator(), &self.0).into_gen()
636+
}
634637
pub fn from_nodeptr(allocator: &mut AllocEncoder, n: NodePtr) -> Result<Program, Error> {
635638
let bytes = clvmr::serde::node_to_bytes(allocator.allocator(), n).into_gen()?;
636639
Ok(Program(bytes))
@@ -640,6 +643,10 @@ impl Program {
640643
Program(by.to_vec())
641644
}
642645

646+
pub fn bytes(&self) -> &[u8] {
647+
&self.0
648+
}
649+
643650
pub fn to_hex(&self) -> String {
644651
hex::encode(&self.0)
645652
}
@@ -724,6 +731,14 @@ impl Timeout {
724731
}
725732
}
726733

734+
impl Add for Timeout {
735+
type Output = Timeout;
736+
737+
fn add(self, rhs: Self) -> Timeout {
738+
Timeout::new(self.0 + rhs.0)
739+
}
740+
}
741+
727742
impl ToClvm<NodePtr> for Timeout {
728743
fn to_clvm(
729744
&self,

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ pub mod channel_handler;
22
mod common;
33
mod log;
44
pub mod outside;
5+
/// Provides as simple as possible a full blockchain interface that can be spoken
6+
/// with via a trait interface that's either local and synchronous or over a pipe.
7+
pub mod peer_container;
58
mod referee;
69

710
#[cfg(test)]

src/outside.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ enum GameAction {
383383
/// the message through to the channel handler.
384384
#[allow(dead_code)]
385385
pub struct PotatoHandler {
386+
initiator: bool,
386387
have_potato: PotatoState,
387388

388389
handshake_state: HandshakeState,
@@ -456,6 +457,7 @@ impl PotatoHandler {
456457
reward_puzzle_hash: PuzzleHash,
457458
) -> PotatoHandler {
458459
PotatoHandler {
460+
initiator: have_potato,
459461
have_potato: if have_potato {
460462
PotatoState::Present
461463
} else {
@@ -488,6 +490,10 @@ impl PotatoHandler {
488490
}
489491
}
490492

493+
pub fn is_initiator(&self) -> bool {
494+
self.initiator
495+
}
496+
491497
pub fn channel_handler(&self) -> Result<&ChannelHandler, Error> {
492498
if let Some(ch) = &self.channel_handler {
493499
Ok(ch)
@@ -608,11 +614,13 @@ impl PotatoHandler {
608614
}
609615
self.update_channel_coin_after_receive(penv, &spend_info)?;
610616
}
611-
PeerMessage::Accept(game_id, _amount, sigs) => {
617+
PeerMessage::Accept(game_id, amount, sigs) => {
612618
let spend_info = {
613-
let (env, _) = penv.env();
614-
ch.received_potato_accept(env, &sigs, &game_id)?
615-
};
619+
let (env, system_interface) = penv.env();
620+
let result = ch.received_potato_accept(env, &sigs, &game_id)?;
621+
system_interface.game_finished(&game_id, amount)?;
622+
Ok(result)
623+
}?;
616624
self.update_channel_coin_after_receive(penv, &spend_info)?;
617625
}
618626
_ => {
@@ -667,6 +675,7 @@ impl PotatoHandler {
667675
{
668676
// Haven't got the channel coin yet.
669677
if self.waiting_to_start {
678+
debug!("waiting to start");
670679
return Ok(());
671680
}
672681

@@ -767,9 +776,10 @@ impl PotatoHandler {
767776
let (_, system_interface) = penv.env();
768777
system_interface.send_message(&PeerMessage::Accept(
769778
game_id.clone(),
770-
amount,
779+
amount.clone(),
771780
sigs,
772781
))?;
782+
system_interface.game_finished(&game_id, amount)?;
773783
self.have_potato = PotatoState::Absent;
774784

775785
Ok(true)
@@ -1203,6 +1213,8 @@ impl PotatoHandler {
12031213
match msg_envelope {
12041214
PeerMessage::HandshakeF { bundle } => {
12051215
self.channel_finished_transaction = Some(bundle.clone());
1216+
let (_, system_interface) = penv.env();
1217+
system_interface.received_channel_offer(&bundle)?;
12061218
}
12071219
PeerMessage::RequestPotato(_) => {
12081220
assert!(matches!(self.have_potato, PotatoState::Present));

0 commit comments

Comments
 (0)