Skip to content

Commit 4d430c2

Browse files
Merge pull request #79 from richardkiss/channel_handler_types
Fragment channel handler types into multiple small files
2 parents e535298 + cdc9d99 commit 4d430c2

20 files changed

+1204
-1075
lines changed

src/channel_handler/types.rs

-1,075
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use crate::referee::RefereeOnChainTransaction;
2+
3+
#[derive(Debug)]
4+
pub enum AcceptTransactionState {
5+
Determined(Box<RefereeOnChainTransaction>),
6+
Waiting,
7+
Finished,
8+
}
+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
use std::rc::Rc;
2+
3+
use clvm_traits::ToClvm;
4+
5+
use log::debug;
6+
7+
use rand::prelude::*;
8+
9+
use crate::channel_handler::types::{prepend_rem_conditions, ChannelHandlerEnv, UnrollCoin};
10+
use crate::common::constants::CREATE_COIN;
11+
use crate::common::standard_coin::standard_solution_partial;
12+
use crate::common::types::{
13+
Aggsig, Amount, BrokenOutCoinSpendInfo, CoinID, CoinString, Error, GameID, IntoErr, Node,
14+
PrivateKey, Program, PublicKey, Sha256tree, Spend,
15+
};
16+
17+
/// Describes all aspects of the channel coin spend.
18+
/// Allows the user to get the solution, conditions, quoted condition program
19+
/// and signature for the channel coin spend.
20+
pub struct ChannelCoin {
21+
state_channel_coin: CoinString,
22+
}
23+
24+
impl ChannelCoin {
25+
pub fn new(state_channel_coin: CoinString) -> Self {
26+
ChannelCoin { state_channel_coin }
27+
}
28+
29+
pub fn coin_string(&self) -> &CoinString {
30+
&self.state_channel_coin
31+
}
32+
pub fn to_coin_id(&self) -> CoinID {
33+
self.state_channel_coin.to_coin_id()
34+
}
35+
36+
pub fn get_solution_and_signature_from_conditions<R: Rng>(
37+
&self,
38+
env: &mut ChannelHandlerEnv<R>,
39+
private_key: &PrivateKey,
40+
aggregate_public_key: &PublicKey,
41+
conditions: Rc<Program>,
42+
) -> Result<BrokenOutCoinSpendInfo, Error> {
43+
debug!("STATE CONDITONS: {conditions:?}");
44+
let conditions_nodeptr = conditions.to_nodeptr(env.allocator)?;
45+
let spend = standard_solution_partial(
46+
env.allocator,
47+
private_key,
48+
&self.state_channel_coin.to_coin_id(),
49+
conditions_nodeptr,
50+
aggregate_public_key,
51+
&env.agg_sig_me_additional_data,
52+
true,
53+
)?;
54+
Ok(spend)
55+
}
56+
57+
pub fn get_solution_and_signature<R: Rng>(
58+
&self,
59+
env: &mut ChannelHandlerEnv<R>,
60+
private_key: &PrivateKey,
61+
aggregate_channel_public_key: &PublicKey,
62+
aggregate_unroll_public_key: &PublicKey,
63+
amount: &Amount,
64+
unroll_coin: &UnrollCoin,
65+
) -> Result<BrokenOutCoinSpendInfo, Error> {
66+
debug!(
67+
"making solution for channel coin with unroll state {}",
68+
unroll_coin.state_number
69+
);
70+
let unroll_puzzle =
71+
unroll_coin.make_curried_unroll_puzzle(env, aggregate_unroll_public_key)?;
72+
let unroll_puzzle_hash = Node(unroll_puzzle).sha256tree(env.allocator);
73+
let create_conditions = vec![Node(
74+
(
75+
CREATE_COIN,
76+
(unroll_puzzle_hash.clone(), (amount.clone(), ())),
77+
)
78+
.to_clvm(env.allocator)
79+
.into_gen()?,
80+
)];
81+
let create_conditions_obj = create_conditions.to_clvm(env.allocator).into_gen()?;
82+
let create_conditions_with_rem =
83+
prepend_rem_conditions(env, unroll_coin.state_number, create_conditions_obj)?;
84+
let ccrem_program = Program::from_nodeptr(env.allocator, create_conditions_with_rem)?;
85+
self.get_solution_and_signature_from_conditions(
86+
env,
87+
private_key,
88+
aggregate_channel_public_key,
89+
Rc::new(ccrem_program),
90+
)
91+
}
92+
}
93+
94+
#[derive(Clone, Debug)]
95+
pub struct ChannelCoinSpentResult {
96+
pub transaction: Spend,
97+
pub timeout: bool,
98+
pub games_canceled: Vec<GameID>,
99+
}
100+
101+
#[derive(Clone, Debug)]
102+
pub struct ChannelCoinSpendInfo {
103+
pub solution: Rc<Program>,
104+
pub conditions: Rc<Program>,
105+
pub aggsig: Aggsig,
106+
}
107+
108+
pub struct ChannelCoinInfo {
109+
pub coin: ChannelCoin,
110+
pub amount: Amount,
111+
// Used in unrolling.
112+
pub spend: Spend,
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use rand::prelude::*;
2+
3+
use rand::distributions::Standard;
4+
5+
use crate::channel_handler::types::{PotatoSignatures, UnrollCoin};
6+
use crate::common::types::{
7+
Aggsig, AllocEncoder, Amount, CoinID, Hash, PrivateKey, PublicKey, Puzzle, PuzzleHash,
8+
Sha256tree, Timeout,
9+
};
10+
11+
#[derive(Clone)]
12+
pub struct ChannelHandlerPrivateKeys {
13+
pub my_channel_coin_private_key: PrivateKey,
14+
pub my_unroll_coin_private_key: PrivateKey,
15+
pub my_referee_private_key: PrivateKey,
16+
}
17+
18+
impl Distribution<ChannelHandlerPrivateKeys> for Standard {
19+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> ChannelHandlerPrivateKeys {
20+
ChannelHandlerPrivateKeys {
21+
my_channel_coin_private_key: rng.gen(),
22+
my_unroll_coin_private_key: rng.gen(),
23+
my_referee_private_key: rng.gen(),
24+
}
25+
}
26+
}
27+
28+
pub struct ChannelHandlerInitiationData {
29+
pub launcher_coin_id: CoinID,
30+
pub we_start_with_potato: bool,
31+
pub their_channel_pubkey: PublicKey,
32+
pub their_unroll_pubkey: PublicKey,
33+
pub their_referee_puzzle_hash: PuzzleHash,
34+
pub my_contribution: Amount,
35+
pub their_contribution: Amount,
36+
pub unroll_advance_timeout: Timeout,
37+
}
38+
39+
#[derive(Clone)]
40+
pub struct ChannelHandlerInitiationResult {
41+
pub channel_puzzle_hash_up: PuzzleHash,
42+
pub my_initial_channel_half_signature_peer: Aggsig,
43+
}
44+
45+
/// The channel handler can use these two items to produce a spend on chain.
46+
#[derive(Default, Clone)]
47+
pub struct ChannelHandlerUnrollSpendInfo {
48+
/// Contains the half signature, puzzle and conditions needed to spend.
49+
pub coin: UnrollCoin,
50+
/// Contains the other half of the signature.
51+
pub signatures: PotatoSignatures,
52+
}
53+
54+
pub struct ChannelHandlerEnv<'a, R: Rng> {
55+
pub allocator: &'a mut AllocEncoder,
56+
pub rng: &'a mut R,
57+
pub unroll_metapuzzle: Puzzle,
58+
pub unroll_puzzle: Puzzle,
59+
60+
pub referee_coin_puzzle: Puzzle,
61+
pub referee_coin_puzzle_hash: PuzzleHash,
62+
63+
pub standard_puzzle: Puzzle,
64+
65+
pub agg_sig_me_additional_data: Hash,
66+
}
67+
68+
impl<'a, R: Rng> ChannelHandlerEnv<'a, R> {
69+
pub fn new(
70+
allocator: &'a mut AllocEncoder,
71+
rng: &'a mut R,
72+
unroll_metapuzzle: Puzzle,
73+
unroll_puzzle: Puzzle,
74+
referee_coin_puzzle: Puzzle,
75+
standard_puzzle: Puzzle,
76+
agg_sig_me_additional_data: Hash,
77+
) -> ChannelHandlerEnv<'a, R> {
78+
let referee_coin_puzzle_hash = referee_coin_puzzle.sha256tree(allocator);
79+
ChannelHandlerEnv {
80+
allocator,
81+
rng,
82+
referee_coin_puzzle,
83+
referee_coin_puzzle_hash,
84+
unroll_metapuzzle,
85+
unroll_puzzle,
86+
standard_puzzle,
87+
agg_sig_me_additional_data,
88+
}
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use crate::common::types::CoinString;
2+
3+
pub struct CoinDataForReward {
4+
pub coin_string: CoinString,
5+
// parent: CoinID,
6+
// puzzle_hash: PuzzleHash,
7+
// amount: Amount,
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use crate::common::types::{Amount, PuzzleHash};
2+
3+
#[derive(Debug)]
4+
pub enum CoinIdentificationByPuzzleHash {
5+
Reward(PuzzleHash, Amount),
6+
Game(PuzzleHash, Amount),
7+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use crate::channel_handler::types::OnChainGameCoin;
2+
use crate::common::types::{Amount, CoinSpend, CoinString, GameID, PuzzleHash};
3+
use crate::referee::TheirTurnCoinSpentResult;
4+
5+
#[derive(Debug)]
6+
pub enum CoinSpentInformation {
7+
OurReward(PuzzleHash, Amount),
8+
OurSpend(PuzzleHash, Amount),
9+
TheirSpend(TheirTurnCoinSpentResult),
10+
Expected(PuzzleHash, Amount),
11+
}
12+
13+
#[derive(Debug, Clone)]
14+
pub struct CoinSpentMoveUp {
15+
pub game_id: GameID,
16+
pub spend_before_game_coin: CoinSpend,
17+
pub after_update_game_coin: CoinString,
18+
}
19+
20+
#[derive(Debug, Clone)]
21+
pub struct CoinSpentAccept {
22+
pub game_id: GameID,
23+
pub spend: CoinSpend,
24+
pub reward_coin: CoinString,
25+
}
26+
27+
// Disposition
28+
#[derive(Debug, Clone)]
29+
pub enum CoinSpentDisposition {
30+
CancelledUX(Vec<GameID>),
31+
Move(CoinSpentMoveUp),
32+
Accept(CoinSpentAccept),
33+
}
34+
35+
#[derive(Debug, Clone)]
36+
pub struct CoinSpentResult {
37+
pub my_clean_reward_coin_string_up: CoinString,
38+
// New coins that now exist.
39+
pub new_game_coins_on_chain: Vec<OnChainGameCoin>,
40+
pub disposition: Option<CoinSpentDisposition>,
41+
}

src/channel_handler/types/evidence.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use clvm_traits::{ClvmEncoder, ToClvm, ToClvmError};
2+
use clvmr::allocator::NodePtr;
3+
4+
use crate::common::types::AllocEncoder;
5+
6+
#[derive(Debug, Clone)]
7+
pub struct Evidence(NodePtr);
8+
9+
impl Evidence {
10+
pub fn from_nodeptr(n: NodePtr) -> Evidence {
11+
Evidence(n)
12+
}
13+
14+
pub fn nil(allocator: &mut AllocEncoder) -> Evidence {
15+
Evidence(allocator.allocator().nil())
16+
}
17+
18+
pub fn to_nodeptr(&self) -> NodePtr {
19+
self.0
20+
}
21+
}
22+
23+
impl<E: ClvmEncoder<Node = NodePtr>> ToClvm<E> for Evidence {
24+
fn to_clvm(&self, _encoder: &mut E) -> Result<NodePtr, ToClvmError> {
25+
Ok(self.0)
26+
}
27+
}

0 commit comments

Comments
 (0)