Skip to content

Commit 5ee36dc

Browse files
committed
Big refactoring: introduce ChiaIdentity and GameMoveDetails
1 parent 609ce75 commit 5ee36dc

File tree

9 files changed

+389
-274
lines changed

9 files changed

+389
-274
lines changed

src/channel_handler/game_handler.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::common::types::{
2323
atom_from_clvm, u64_from_atom, usize_from_atom, Aggsig, AllocEncoder, Amount, Error, Hash,
2424
IntoErr, Node,
2525
};
26+
use crate::referee::GameMoveDetails;
2627

2728
pub fn chia_dialect() -> ChiaDialect {
2829
ChiaDialect::new(NO_UNKNOWN_OPS)
@@ -70,24 +71,23 @@ fn get_my_turn_debug_flag(_: &MyTurnInputs) -> bool {
7071
pub struct MyTurnResult {
7172
// Next player's turn game handler.
7273
pub waiting_driver: GameHandler,
73-
pub move_data: Vec<u8>,
7474
pub validation_program: NodePtr,
75-
pub validation_program_hash: Hash,
7675
pub state: NodePtr,
77-
pub max_move_size: usize,
78-
pub mover_share: Amount,
76+
pub game_move: GameMoveDetails,
7977
pub message_parser: Option<MessageHandler>,
8078
}
8179

8280
pub struct TheirTurnInputs<'a> {
8381
pub amount: Amount,
8482
pub last_state: NodePtr,
83+
84+
/// Only needs a couple things from last move.
8585
pub last_move: &'a [u8],
8686
pub last_mover_share: Amount,
87-
pub new_move: &'a [u8],
88-
pub new_validation_info_hash: Hash,
89-
pub new_max_move_size: usize,
90-
pub new_mover_share: Amount,
87+
88+
/// New move is a full move details.
89+
pub new_move: GameMoveDetails,
90+
9191
#[cfg(test)]
9292
pub run_debug: bool,
9393
}
@@ -270,12 +270,14 @@ impl GameHandler {
270270

271271
Ok(MyTurnResult {
272272
waiting_driver: GameHandler::their_driver_from_nodeptr(pl[6]),
273-
move_data,
274273
validation_program: pl[1],
275-
validation_program_hash,
276274
state: pl[3],
277-
max_move_size,
278-
mover_share,
275+
game_move: GameMoveDetails {
276+
move_made: move_data,
277+
validation_info_hash: validation_program_hash,
278+
max_move_size,
279+
mover_share,
280+
},
279281
message_parser,
280282
})
281283
}
@@ -290,12 +292,12 @@ impl GameHandler {
290292
(
291293
Node(inputs.last_state.clone()),
292294
(
293-
Node(allocator.encode_atom(inputs.new_move).into_gen()?),
295+
Node(allocator.encode_atom(&inputs.new_move.move_made).into_gen()?),
294296
(
295-
inputs.new_validation_info_hash.clone(),
297+
inputs.new_move.validation_info_hash.clone(),
296298
(
297-
inputs.new_max_move_size.clone(),
298-
(inputs.new_mover_share.clone(), ()),
299+
inputs.new_move.max_move_size.clone(),
300+
(inputs.new_move.mover_share.clone(), ()),
299301
),
300302
),
301303
),

src/channel_handler/mod.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::channel_handler::types::{
1818
};
1919
use crate::common::constants::{CREATE_COIN, REM};
2020
use crate::common::standard_coin::{
21-
agg_sig_me_message, aggregate_public_keys, partial_signer, private_to_public_key,
21+
ChiaIdentity, agg_sig_me_message, aggregate_public_keys, partial_signer, private_to_public_key,
2222
puzzle_for_pk, puzzle_hash_for_pk, sign_agg_sig_me, standard_solution,
2323
standard_solution_partial, unsafe_sign_partial,
2424
};
@@ -704,13 +704,16 @@ impl ChannelHandler {
704704
let new_game_nonce = self.next_nonce_number;
705705
self.next_nonce_number += 1;
706706

707+
let referee_identity = ChiaIdentity::new(
708+
&mut env.allocator,
709+
self.private_keys.my_referee_private_key.clone()
710+
)?;
707711
self.live_games.push(LiveGame {
708712
game_id: g.game_id.clone(),
709713
referee_maker: Box::new(RefereeMaker::new(
710-
env.allocator,
711714
env.referee_coin_puzzle_hash.clone(),
712715
g,
713-
&self.private_keys.my_referee_private_key,
716+
referee_identity,
714717
&self.their_referee_puzzle_hash,
715718
new_game_nonce,
716719
)?),
@@ -788,7 +791,7 @@ impl ChannelHandler {
788791
referee_maker.my_turn_make_move(env.rng, env.allocator, readable_move)?;
789792

790793
let puzzle_hash = referee_result.puzzle_hash_for_unroll.clone();
791-
let amount = referee_result.mover_share.clone();
794+
let amount = referee_result.details.mover_share.clone();
792795

793796
self.have_potato = false;
794797
self.current_state_number += 1;
@@ -805,10 +808,7 @@ impl ChannelHandler {
805808

806809
Ok(MoveResult {
807810
signatures,
808-
validation_info_hash_peer: referee_result.validation_info_hash.clone(),
809-
move_peer: referee_result.move_made.clone(),
810-
mover_share_peer: referee_result.mover_share.clone(),
811-
max_move_size_peer: referee_result.max_move_size,
811+
game_move: referee_result.details.clone()
812812
})
813813
}
814814

@@ -823,10 +823,7 @@ impl ChannelHandler {
823823
let referee_maker: &mut RefereeMaker = self.live_games[game_idx].referee_maker.borrow_mut();
824824
let their_move_result = referee_maker.their_turn_move_off_chain(
825825
env.allocator,
826-
&move_result.move_peer,
827-
&move_result.validation_info_hash_peer,
828-
move_result.max_move_size_peer,
829-
&move_result.mover_share_peer,
826+
&move_result.game_move
830827
)?;
831828

832829
self.received_potato_verify_signatures(

src/channel_handler/types.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::common::types::{
1010
Aggsig, AllocEncoder, Amount, CoinID, CoinString, Error, GameID, Hash, IntoErr, PrivateKey,
1111
PublicKey, Puzzle, PuzzleHash, Sha256tree, SpecificTransactionBundle, Timeout,
1212
};
13-
use crate::referee::RefereeMaker;
13+
use crate::referee::{GameMoveDetails, RefereeMaker};
1414

1515
#[derive(Default)]
1616
pub struct ChannelHandlerPrivateKeys {
@@ -88,10 +88,7 @@ pub struct ReadableUX(NodePtr);
8888

8989
pub struct MoveResult {
9090
pub signatures: PotatoSignatures,
91-
pub move_peer: Vec<u8>,
92-
pub validation_info_hash_peer: Hash,
93-
pub max_move_size_peer: usize,
94-
pub mover_share_peer: Amount,
91+
pub game_move: GameMoveDetails,
9592
}
9693

9794
pub struct OnChainGameCoin<'a> {

src/common/standard_coin.rs

+39-2
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@ use clvm_tools_rs::classic::clvm::__type_compatibility__::{
1212
Bytes, Stream, UnvalidatedBytesFromType,
1313
};
1414
use clvm_tools_rs::classic::clvm::serialize::{sexp_from_stream, SimpleCreateCLVMObject};
15+
use clvm_tools_rs::compiler::comptypes::map_m;
1516

1617
use clvm_utils::CurriedProgram;
1718

1819
use crate::common::constants::{
1920
A_KW, C_KW, DEFAULT_HIDDEN_PUZZLE_HASH, DEFAULT_PUZZLE_HASH, GROUP_ORDER, ONE, Q_KW,
20-
Q_KW_TREEHASH, TWO,
21+
Q_KW_TREEHASH, TWO, CREATE_COIN
2122
};
2223
use crate::common::types;
2324
use crate::common::types::{
24-
Aggsig, AllocEncoder, CoinCondition, CoinID, Hash, IntoErr, Node, PrivateKey, Program,
25+
Aggsig, AllocEncoder, Amount, CoinCondition, CoinID, Hash, IntoErr, Node, PrivateKey, Program,
2526
PublicKey, Puzzle, PuzzleHash, Sha256Input, Sha256tree, ToQuotedProgram,
2627
};
2728

@@ -389,3 +390,39 @@ pub fn standard_solution_partial(
389390
))
390391
}
391392
}
393+
394+
#[derive(Clone)]
395+
pub struct ChiaIdentity {
396+
pub private_key: PrivateKey,
397+
pub public_key: PublicKey,
398+
pub puzzle: Puzzle,
399+
pub puzzle_hash: PuzzleHash
400+
}
401+
402+
impl ChiaIdentity {
403+
pub fn new(
404+
allocator: &mut AllocEncoder,
405+
private_key: PrivateKey
406+
) -> Result<Self, types::Error> {
407+
let public_key = private_to_public_key(&private_key);
408+
Ok(ChiaIdentity {
409+
private_key,
410+
puzzle: puzzle_for_pk(allocator, &public_key)?,
411+
puzzle_hash: puzzle_hash_for_pk(allocator, &public_key)?,
412+
public_key,
413+
})
414+
}
415+
416+
pub fn standard_solution(
417+
&self,
418+
allocator: &mut AllocEncoder,
419+
targets: &[(PuzzleHash, Amount)]
420+
) -> Result<NodePtr, types::Error> {
421+
let conditions: Vec<Node> =
422+
map_m(|(ph, amt)| {
423+
Ok(Node((CREATE_COIN, (ph.clone(), (amt.clone(), ()))).to_clvm(allocator).into_gen()?))
424+
}, targets)?;
425+
let conditions_converted = conditions.to_clvm(allocator).into_gen()?;
426+
solution_for_conditions(allocator, conditions_converted)
427+
}
428+
}

src/common/types.rs

+3
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,9 @@ impl ToClvm<NodePtr> for Puzzle {
571571
}
572572

573573
impl Puzzle {
574+
pub fn to_program(&self) -> Program {
575+
self.0.clone()
576+
}
574577
pub fn to_nodeptr(&self) -> NodePtr {
575578
self.0.to_nodeptr()
576579
}

0 commit comments

Comments
 (0)