Skip to content

Commit

Permalink
Big refactoring: introduce ChiaIdentity and GameMoveDetails
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed May 17, 2024
1 parent 609ce75 commit 5ee36dc
Show file tree
Hide file tree
Showing 9 changed files with 389 additions and 274 deletions.
34 changes: 18 additions & 16 deletions src/channel_handler/game_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::common::types::{
atom_from_clvm, u64_from_atom, usize_from_atom, Aggsig, AllocEncoder, Amount, Error, Hash,
IntoErr, Node,
};
use crate::referee::GameMoveDetails;

pub fn chia_dialect() -> ChiaDialect {
ChiaDialect::new(NO_UNKNOWN_OPS)
Expand Down Expand Up @@ -70,24 +71,23 @@ fn get_my_turn_debug_flag(_: &MyTurnInputs) -> bool {
pub struct MyTurnResult {
// Next player's turn game handler.
pub waiting_driver: GameHandler,
pub move_data: Vec<u8>,
pub validation_program: NodePtr,
pub validation_program_hash: Hash,
pub state: NodePtr,
pub max_move_size: usize,
pub mover_share: Amount,
pub game_move: GameMoveDetails,
pub message_parser: Option<MessageHandler>,
}

pub struct TheirTurnInputs<'a> {
pub amount: Amount,
pub last_state: NodePtr,

/// Only needs a couple things from last move.
pub last_move: &'a [u8],
pub last_mover_share: Amount,
pub new_move: &'a [u8],
pub new_validation_info_hash: Hash,
pub new_max_move_size: usize,
pub new_mover_share: Amount,

/// New move is a full move details.
pub new_move: GameMoveDetails,

#[cfg(test)]
pub run_debug: bool,
}
Expand Down Expand Up @@ -270,12 +270,14 @@ impl GameHandler {

Ok(MyTurnResult {
waiting_driver: GameHandler::their_driver_from_nodeptr(pl[6]),
move_data,
validation_program: pl[1],
validation_program_hash,
state: pl[3],
max_move_size,
mover_share,
game_move: GameMoveDetails {
move_made: move_data,
validation_info_hash: validation_program_hash,
max_move_size,
mover_share,
},
message_parser,
})
}
Expand All @@ -290,12 +292,12 @@ impl GameHandler {
(
Node(inputs.last_state.clone()),
(
Node(allocator.encode_atom(inputs.new_move).into_gen()?),
Node(allocator.encode_atom(&inputs.new_move.move_made).into_gen()?),
(
inputs.new_validation_info_hash.clone(),
inputs.new_move.validation_info_hash.clone(),
(
inputs.new_max_move_size.clone(),
(inputs.new_mover_share.clone(), ()),
inputs.new_move.max_move_size.clone(),
(inputs.new_move.mover_share.clone(), ()),
),
),
),
Expand Down
21 changes: 9 additions & 12 deletions src/channel_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::channel_handler::types::{
};
use crate::common::constants::{CREATE_COIN, REM};
use crate::common::standard_coin::{
agg_sig_me_message, aggregate_public_keys, partial_signer, private_to_public_key,
ChiaIdentity, agg_sig_me_message, aggregate_public_keys, partial_signer, private_to_public_key,
puzzle_for_pk, puzzle_hash_for_pk, sign_agg_sig_me, standard_solution,
standard_solution_partial, unsafe_sign_partial,
};
Expand Down Expand Up @@ -704,13 +704,16 @@ impl ChannelHandler {
let new_game_nonce = self.next_nonce_number;
self.next_nonce_number += 1;

let referee_identity = ChiaIdentity::new(
&mut env.allocator,
self.private_keys.my_referee_private_key.clone()
)?;
self.live_games.push(LiveGame {
game_id: g.game_id.clone(),
referee_maker: Box::new(RefereeMaker::new(
env.allocator,
env.referee_coin_puzzle_hash.clone(),
g,
&self.private_keys.my_referee_private_key,
referee_identity,
&self.their_referee_puzzle_hash,
new_game_nonce,
)?),
Expand Down Expand Up @@ -788,7 +791,7 @@ impl ChannelHandler {
referee_maker.my_turn_make_move(env.rng, env.allocator, readable_move)?;

let puzzle_hash = referee_result.puzzle_hash_for_unroll.clone();
let amount = referee_result.mover_share.clone();
let amount = referee_result.details.mover_share.clone();

self.have_potato = false;
self.current_state_number += 1;
Expand All @@ -805,10 +808,7 @@ impl ChannelHandler {

Ok(MoveResult {
signatures,
validation_info_hash_peer: referee_result.validation_info_hash.clone(),
move_peer: referee_result.move_made.clone(),
mover_share_peer: referee_result.mover_share.clone(),
max_move_size_peer: referee_result.max_move_size,
game_move: referee_result.details.clone()
})
}

Expand All @@ -823,10 +823,7 @@ impl ChannelHandler {
let referee_maker: &mut RefereeMaker = self.live_games[game_idx].referee_maker.borrow_mut();
let their_move_result = referee_maker.their_turn_move_off_chain(
env.allocator,
&move_result.move_peer,
&move_result.validation_info_hash_peer,
move_result.max_move_size_peer,
&move_result.mover_share_peer,
&move_result.game_move
)?;

self.received_potato_verify_signatures(
Expand Down
7 changes: 2 additions & 5 deletions src/channel_handler/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::common::types::{
Aggsig, AllocEncoder, Amount, CoinID, CoinString, Error, GameID, Hash, IntoErr, PrivateKey,
PublicKey, Puzzle, PuzzleHash, Sha256tree, SpecificTransactionBundle, Timeout,
};
use crate::referee::RefereeMaker;
use crate::referee::{GameMoveDetails, RefereeMaker};

#[derive(Default)]
pub struct ChannelHandlerPrivateKeys {
Expand Down Expand Up @@ -88,10 +88,7 @@ pub struct ReadableUX(NodePtr);

pub struct MoveResult {
pub signatures: PotatoSignatures,
pub move_peer: Vec<u8>,
pub validation_info_hash_peer: Hash,
pub max_move_size_peer: usize,
pub mover_share_peer: Amount,
pub game_move: GameMoveDetails,
}

pub struct OnChainGameCoin<'a> {
Expand Down
41 changes: 39 additions & 2 deletions src/common/standard_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ use clvm_tools_rs::classic::clvm::__type_compatibility__::{
Bytes, Stream, UnvalidatedBytesFromType,
};
use clvm_tools_rs::classic::clvm::serialize::{sexp_from_stream, SimpleCreateCLVMObject};
use clvm_tools_rs::compiler::comptypes::map_m;

use clvm_utils::CurriedProgram;

use crate::common::constants::{
A_KW, C_KW, DEFAULT_HIDDEN_PUZZLE_HASH, DEFAULT_PUZZLE_HASH, GROUP_ORDER, ONE, Q_KW,
Q_KW_TREEHASH, TWO,
Q_KW_TREEHASH, TWO, CREATE_COIN
};
use crate::common::types;
use crate::common::types::{
Aggsig, AllocEncoder, CoinCondition, CoinID, Hash, IntoErr, Node, PrivateKey, Program,
Aggsig, AllocEncoder, Amount, CoinCondition, CoinID, Hash, IntoErr, Node, PrivateKey, Program,
PublicKey, Puzzle, PuzzleHash, Sha256Input, Sha256tree, ToQuotedProgram,
};

Expand Down Expand Up @@ -389,3 +390,39 @@ pub fn standard_solution_partial(
))
}
}

#[derive(Clone)]
pub struct ChiaIdentity {
pub private_key: PrivateKey,
pub public_key: PublicKey,
pub puzzle: Puzzle,
pub puzzle_hash: PuzzleHash
}

impl ChiaIdentity {
pub fn new(
allocator: &mut AllocEncoder,
private_key: PrivateKey
) -> Result<Self, types::Error> {
let public_key = private_to_public_key(&private_key);
Ok(ChiaIdentity {
private_key,
puzzle: puzzle_for_pk(allocator, &public_key)?,
puzzle_hash: puzzle_hash_for_pk(allocator, &public_key)?,
public_key,
})
}

pub fn standard_solution(
&self,
allocator: &mut AllocEncoder,
targets: &[(PuzzleHash, Amount)]
) -> Result<NodePtr, types::Error> {
let conditions: Vec<Node> =
map_m(|(ph, amt)| {
Ok(Node((CREATE_COIN, (ph.clone(), (amt.clone(), ()))).to_clvm(allocator).into_gen()?))
}, targets)?;
let conditions_converted = conditions.to_clvm(allocator).into_gen()?;
solution_for_conditions(allocator, conditions_converted)
}
}
3 changes: 3 additions & 0 deletions src/common/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,9 @@ impl ToClvm<NodePtr> for Puzzle {
}

impl Puzzle {
pub fn to_program(&self) -> Program {
self.0.clone()
}
pub fn to_nodeptr(&self) -> NodePtr {
self.0.to_nodeptr()
}
Expand Down
Loading

0 comments on commit 5ee36dc

Please sign in to comment.