Skip to content
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

20240813 peer container #10

Merged
merged 16 commits into from
Aug 16, 2024
Merged
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
@@ -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))
@@ -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)
}
@@ -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,
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)]
22 changes: 17 additions & 5 deletions src/outside.rs
Original file line number Diff line number Diff line change
@@ -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,
@@ -456,6 +457,7 @@ impl PotatoHandler {
reward_puzzle_hash: PuzzleHash,
) -> PotatoHandler {
PotatoHandler {
initiator: have_potato,
have_potato: if have_potato {
PotatoState::Present
} else {
@@ -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)
@@ -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)?;
}
_ => {
@@ -667,6 +675,7 @@ impl PotatoHandler {
{
// Haven't got the channel coin yet.
if self.waiting_to_start {
debug!("waiting to start");
return Ok(());
}

@@ -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)
@@ -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));
679 changes: 569 additions & 110 deletions src/peer_container.rs

Large diffs are not rendered by default.

63 changes: 21 additions & 42 deletions src/tests/peer/outside.rs
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ use crate::outside::{
BootstrapTowardGame, BootstrapTowardWallet, FromLocalUI, GameStart, GameType, PacketSender,
PeerEnv, PeerMessage, PotatoHandler, SpendWalletReceiver, ToLocalUI, WalletSpendInterface,
};
use crate::peer_container::{MessagePeerQueue, MessagePipe, WalletBootstrapState};

use crate::common::constants::CREATE_COIN;
use crate::common::standard_coin::standard_solution_partial;
@@ -29,30 +30,6 @@ use crate::common::types::{CoinSpend, Program};
use crate::tests::calpoker::{load_calpoker, test_moves_1};
use crate::tests::simenv::GameAction;

#[allow(dead_code)]
enum NotificationToLocalUI {
OpponentMoved(GameID, ReadableMove),
MessageFromOpponent(GameID, ReadableMove),
GameFinished(GameID, Amount),
GameCancelled(GameID),
ShutdownComplete(CoinString),
GoingOnChain,
}

#[allow(dead_code)]
pub enum WalletBootstrapState {
PartlySigned(Spend),
FullySigned(Spend),
}

#[derive(Default)]
pub struct MessagePipe {
pub my_id: usize,

// PacketSender
pub queue: VecDeque<Vec<u8>>,
}

#[derive(Default)]
struct Pipe {
message_pipe: MessagePipe,
@@ -75,13 +52,6 @@ struct Pipe {
bootstrap_state: Option<WalletBootstrapState>,
}

pub trait MessagePeerQueue {
fn message_pipe(&mut self) -> &mut MessagePipe;
fn get_channel_puzzle_hash(&self) -> Option<PuzzleHash>;
fn set_channel_puzzle_hash(&mut self, ph: Option<PuzzleHash>);
fn get_unfunded_offer(&self) -> Option<SpendBundle>;
}

impl MessagePeerQueue for Pipe {
fn message_pipe(&mut self) -> &mut MessagePipe {
&mut self.message_pipe
@@ -158,7 +128,7 @@ impl ToLocalUI for Pipe {
Ok(())
}
fn game_finished(&mut self, _id: &GameID, _my_share: Amount) -> Result<(), Error> {
todo!();
Ok(())
}
fn game_cancelled(&mut self, _id: &GameID) -> Result<(), Error> {
todo!();
@@ -285,16 +255,6 @@ where

peer.received_message(&mut penv, msg)?;

if let Some(ch) = penv.system_interface.get_channel_puzzle_hash() {
let parent = CoinString::from_parts(&CoinID::default(), &PuzzleHash::default(), &amount);
penv.test_handle_received_channel_puzzle_hash(peer, &parent, &ch)?;
penv.system_interface.set_channel_puzzle_hash(None);
}

if let Some(ufo) = penv.system_interface.get_unfunded_offer() {
penv.test_handle_received_unfunded_offer(peer, &ufo)?;
}

Ok(true)
}

@@ -368,6 +328,25 @@ where

i += 1;

{
let mut env = channel_handler_env(allocator, rng);
let mut penv: TestPeerEnv<P, R> = TestPeerEnv {
env: &mut env,
system_interface: &mut pipes[who],
};

if let Some(ch) = penv.system_interface.get_channel_puzzle_hash() {
let parent =
CoinString::from_parts(&CoinID::default(), &PuzzleHash::default(), &amount);
penv.test_handle_received_channel_puzzle_hash(&mut peers[who], &parent, &ch)?;
penv.system_interface.set_channel_puzzle_hash(None);
}

if let Some(ufo) = penv.system_interface.get_unfunded_offer() {
penv.test_handle_received_unfunded_offer(&mut peers[who], &ufo)?;
}
}

if i >= 10 && i < 12 {
let mut env = channel_handler_env(allocator, rng);
// Ensure that we notify about the channel coin (fake here, but the notification
562 changes: 389 additions & 173 deletions src/tests/peer/outsim.rs

Large diffs are not rendered by default.