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

Add dev environment setup doc. #40

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
60 changes: 60 additions & 0 deletions DEV_SETUP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

# Setting up git

First, you'll need to set up commit signing.


`bash

# install gpg on macos
brew install gpg

# Set git to sign all commits by default
git config --global commit.gpgsign true

# Find the verified email address for your GitHub account here:
https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-personal-account-on-github/managing-email-preferences/verifying-your-email-address

# Create a new gpg key -- make sure to use you verified github email
gpg --full-generate-key
# https://docs.github.com/en/authentication/managing-commit-signature-verification/generating-a-new-gpg-key

# Get the ID for your GPG secret key
# In the example below, it is '3AA5C34371567BD2'

$ gpg --list-secret-keys --keyid-format=long
/Users/<you>/.gnupg/secring.gpg
------------------------------------
sec 4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
uid You <[email protected]>
ssb 4096R/4BB6D45482678BE3 2016-03-10

# Print the GPG public key in ASCII armor format
gpg --armor --export <YOUR_ID e.g. 3AA5C34371567BD2>

# Add your GPG signing key to your github account
https://docs.github.com/en/authentication/managing-commit-signature-verification/adding-a-gpg-key-to-your-github-account#adding-a-gpg-key
`

# Setting up your development environment

Now that you have git signing set up, let's set up the repo.

## Check out the repo

`bash
$ cd
$ git clone [email protected]:Chia-Network/chia-gaming.git
$ cd chia-gaming
`

## Install Rust
`
brew install llvm maturin
rustup toolchain install nightly
rustup default nightly
`

## Check the build
`cargo test
`
51 changes: 29 additions & 22 deletions src/channel_handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use log::debug;

use rand::prelude::*;

use clvm_tools_rs::classic::clvm_tools::binutils::disassemble;
use clvm_traits::ToClvm;
use clvmr::allocator::NodePtr;

Expand Down Expand Up @@ -912,68 +913,74 @@ impl ChannelHandler {
})
}

/// Uses the channel coin key to post standard format coin generation to the
/// real blockchain via a Spend.
pub fn send_potato_clean_shutdown<R: Rng>(
pub fn state_channel_coin_solution_and_signature<R: Rng>(
&self,
env: &mut ChannelHandlerEnv<R>,
conditions: NodePtr,
) -> Result<Spend, Error> {
assert!(self.have_potato);
) -> Result<(NodePtr, Aggsig), Error> {
let aggregate_public_key = self.get_aggregate_channel_public_key();
let spend = self.state_channel_coin();

let channel_coin_spend = spend.get_solution_and_signature_from_conditions(
env,
&self.private_keys.my_channel_coin_private_key,
&aggregate_public_key,
conditions,
)?;

Ok(Spend {
solution: Program::from_nodeptr(env.allocator, channel_coin_spend.solution)?,
signature: channel_coin_spend.signature,
puzzle: puzzle_for_pk(env.allocator, &aggregate_public_key)?,
})
Ok((channel_coin_spend.solution, channel_coin_spend.signature))
}

pub fn state_channel_coin_solution_and_signature<R: Rng>(
/// Uses the channel coin key to post standard format coin generation to the
/// real blockchain via a Spend.
pub fn send_potato_clean_shutdown<R: Rng>(
&self,
env: &mut ChannelHandlerEnv<R>,
conditions: NodePtr,
) -> Result<(NodePtr, Aggsig), Error> {
) -> Result<Spend, Error> {
debug!("SEND_POTATO_CLEAN_SHUTDOWN");
assert!(self.have_potato);
let aggregate_public_key = self.get_aggregate_channel_public_key();
let spend = self.state_channel_coin();

let channel_coin_spend = spend.get_solution_and_signature_from_conditions(
env,
&self.private_keys.my_channel_coin_private_key,
&aggregate_public_key,
conditions,
)?;

Ok((channel_coin_spend.solution, channel_coin_spend.signature))
debug!(
"send_potato_clean_shutdown {}",
disassemble(env.allocator.allocator(), channel_coin_spend.solution, None)
);

Ok(Spend {
solution: Program::from_nodeptr(env.allocator, channel_coin_spend.solution)?,
signature: channel_coin_spend.signature,
puzzle: puzzle_for_pk(env.allocator, &aggregate_public_key)?,
})
}

pub fn received_potato_clean_shutdown<R: Rng>(
&self,
env: &mut ChannelHandlerEnv<R>,
their_channel_half_signature: &Aggsig,
conditions: NodePtr,
) -> Result<Spend, Error> {
let aggregate_public_key = self.get_aggregate_channel_public_key();

) -> Result<BrokenOutCoinSpendInfo, Error> {
debug!("RECEIVED_POTATO_CLEAN_SHUTDOWN");
assert!(!self.have_potato);
let channel_spend = self.verify_channel_coin_from_peer_signatures(
env,
their_channel_half_signature,
conditions,
)?;

Ok(Spend {
solution: Program::from_nodeptr(env.allocator, channel_spend.solution)?,
signature: channel_spend.signature,
puzzle: puzzle_for_pk(env.allocator, &aggregate_public_key)?,
})
debug!(
"received_potato_clean_shutdown {}",
disassemble(env.allocator.allocator(), channel_spend.solution, None)
);

Ok(channel_spend)
}

fn break_out_conditions_for_spent_coin<R: Rng>(
Expand Down
21 changes: 20 additions & 1 deletion src/common/standard_coin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::cell::RefCell;
use std::collections::HashMap;
use std::fs::read_to_string;

use log::debug;
use num_bigint::{BigInt, Sign};

use chia_bls;
Expand Down Expand Up @@ -29,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 @@ -40,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 Expand Up @@ -430,6 +447,7 @@ pub fn standard_solution_partial(
let quoted_conds = conditions.to_quoted_program(allocator)?;
let quoted_conds_hash = quoted_conds.sha256tree(allocator);
let solution = solution_for_conditions(allocator, conditions)?;
debug!("standard signing with parent coin {parent_coin:?}");
let coin_agg_sig_me_message = agg_sig_me_message(
quoted_conds_hash.bytes(),
parent_coin,
Expand All @@ -454,6 +472,7 @@ pub fn standard_solution_partial(
for cond in conds.iter() {
match cond {
CoinCondition::CreateCoin(_, _) => {
debug!("adding signature based on create coin: {aggregate_public_key:?} {coin_agg_sig_me_message:?}");
add_signature(
&mut aggregated_signature,
if partial {
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