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

building wasm and finishing shutdown functionality #38

Draft
wants to merge 15 commits into
base: 20240920-accept-and-shutdown
Choose a base branch
from
Draft
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
18 changes: 17 additions & 1 deletion src/common/standard_coin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::fs::read_to_string;

use log::debug;
Expand Down Expand Up @@ -30,6 +32,16 @@ use crate::common::types::{
ToQuotedProgram,
};

thread_local! {
pub static PRESET_FILES: RefCell<HashMap<String, String>> = RefCell::default();
}

pub fn wasm_deposit_file(name: &str, data: &str) {
PRESET_FILES.with(|p| {
p.borrow_mut().insert(name.to_string(), data.to_string());
});
}

pub fn hex_to_sexp(
allocator: &mut AllocEncoder,
hex_data: String,
Expand All @@ -41,7 +53,11 @@ pub fn hex_to_sexp(
}

pub fn read_hex_puzzle(allocator: &mut AllocEncoder, name: &str) -> Result<Puzzle, types::Error> {
let hex_data = read_to_string(name).into_gen()?;
let hex_data = if let Some(data) = PRESET_FILES.with(|p| p.borrow().get(name).cloned()) {
data
} else {
read_to_string(name).into_gen()?
};
let hex_sexp = hex_to_sexp(allocator, hex_data)?;
Puzzle::from_nodeptr(allocator, hex_sexp)
}
Expand Down
36 changes: 36 additions & 0 deletions src/common/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ impl std::fmt::Debug for CoinString {
}

impl CoinString {
pub fn from_bytes(bytes: &[u8]) -> CoinString {
CoinString(bytes.to_vec())
}

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

pub fn from_parts(parent: &CoinID, puzzle_hash: &PuzzleHash, amount: &Amount) -> CoinString {
let mut allocator = AllocEncoder::new();
let amount_clvm = amount.to_clvm(&mut allocator).unwrap();
Expand Down Expand Up @@ -365,6 +373,10 @@ impl GameID {
pub fn from_bytes(s: &[u8]) -> GameID {
GameID(s.to_vec())
}

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

impl ToClvm<NodePtr> for GameID {
Expand Down Expand Up @@ -565,9 +577,27 @@ pub enum Error {
BlsErr(chia_bls::Error),
BsonErr(bson::de::Error),
JsonErr(serde_json::Error),
HexErr(hex::FromHexError),
Channel(String),
}

#[derive(Serialize, Deserialize)]
struct SerializedError {
error: String,
}

impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
SerializedError {
error: format!("{self:?}"),
}
.serialize(serializer)
}
}

#[derive(Clone, Debug)]
pub struct Node(pub NodePtr);

Expand Down Expand Up @@ -845,6 +875,12 @@ impl ErrToError for serde_json::Error {
}
}

impl ErrToError for hex::FromHexError {
fn into_gen(self) -> Error {
Error::HexErr(self)
}
}

pub trait IntoErr<X> {
fn into_gen(self) -> Result<X, Error>;
}
Expand Down
31 changes: 24 additions & 7 deletions src/peer_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ struct SynchronousGameCradleState {
raw_game_messages: VecDeque<(GameID, Vec<u8>)>,
game_messages: VecDeque<(GameID, ReadableMove)>,
game_finished: VecDeque<(GameID, Amount)>,
shutdown: Option<CoinString>,
identity: ChiaIdentity,
}

Expand All @@ -247,7 +248,11 @@ impl PacketSender for SynchronousGameCradleState {

impl WalletSpendInterface for SynchronousGameCradleState {
/// Enqueue an outbound transaction.
fn spend_transaction_and_add_fee(&mut self, _bundle: &Spend) -> Result<(), Error> {
fn spend_transaction_and_add_fee(
&mut self,
_bundle: &Spend,
_parent: Option<&CoinString>,
) -> Result<(), Error> {
todo!();
}
/// Coin should report its lifecycle until it gets spent, then should be
Expand Down Expand Up @@ -301,6 +306,7 @@ impl SynchronousGameCradle {
channel_puzzle_hash: None,
funding_coin: None,
unfunded_offer: None,
shutdown: None,
},
peer: PotatoHandler::new(
config.have_potato,
Expand Down Expand Up @@ -341,7 +347,12 @@ impl ToLocalUI for SynchronousGameCradleState {
Ok(())
}

fn opponent_moved(&mut self, id: &GameID, readable: ReadableMove) -> Result<(), Error> {
fn opponent_moved(
&mut self,
_allocator: &mut AllocEncoder,
id: &GameID,
readable: ReadableMove,
) -> Result<(), Error> {
self.opponent_moves.push_back((id.clone(), readable));
Ok(())
}
Expand All @@ -350,7 +361,12 @@ impl ToLocalUI for SynchronousGameCradleState {
.push_back((id.clone(), readable.to_vec()));
Ok(())
}
fn game_message(&mut self, id: &GameID, readable: ReadableMove) -> Result<(), Error> {
fn game_message(
&mut self,
_allocator: &mut AllocEncoder,
id: &GameID,
readable: ReadableMove,
) -> Result<(), Error> {
self.game_messages.push_back((id.clone(), readable.clone()));
Ok(())
}
Expand All @@ -361,8 +377,9 @@ impl ToLocalUI for SynchronousGameCradleState {
fn game_cancelled(&mut self, _id: &GameID) -> Result<(), Error> {
todo!();
}
fn shutdown_complete(&mut self, _reward_coin_string: &CoinString) -> Result<(), Error> {
todo!();
fn shutdown_complete(&mut self, reward_coin_string: &CoinString) -> Result<(), Error> {
self.shutdown = Some(reward_coin_string.clone());
Ok(())
}
fn going_on_chain(&mut self) -> Result<(), Error> {
todo!();
Expand Down Expand Up @@ -679,12 +696,12 @@ impl GameCradle for SynchronousGameCradle {
}

if let Some((id, msg)) = self.state.game_messages.pop_front() {
local_ui.game_message(&id, msg)?;
local_ui.game_message(allocator, &id, msg)?;
return Ok(result);
}

if let Some((id, readable)) = self.state.opponent_moves.pop_front() {
local_ui.opponent_moved(&id, readable)?;
local_ui.opponent_moved(allocator, &id, readable)?;
result.continue_on = true;
return Ok(result);
}
Expand Down
Loading
Loading