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

Feat: Adding proc fold, its endpoint and its unit test #13

Merged
merged 14 commits into from
May 10, 2024
81 changes: 81 additions & 0 deletions lib/contracts/core/game.masm
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,87 @@ export.play_raise
# []
end


export.play_fold
# make is_fold of player 1
# inc turn
push.CURRENT_TURN_INDEX exec.account::get_item
# => [0, 0, 0, current_turn_index]

drop drop drop
# => [current_turn_index]

push.9
# 9, current_turn_index
add
# => [current_turn_index + 9]
dup #for getting the get item
dup #for updating the turn
# => [current_turn_index + 9, current_turn_index + 9, current_turn_index + 9]

# getting the is_fold of player
exec.account::get_item
drop drop drop
# => [is_fold, current_turn_index + 9, current_turn_index + 9]

# making sure that is_fold is zero currently
assertz
# => [current_turn_index + 9, current_turn_index + 9]
push.1
# => [1, current_turn_index + 9, current_turn_index + 9]

swap padw drop movup.3
# => [current_turn_index + 9, 0, 0, 0, 1, current_turn_index + 9]
exec.account::set_item
dropw drop drop drop
# => [current_turn_index + 9]

# adding 4 will give the slot of next player
push.4 add
push.1
# => [1, next_turn_index]

while.true

dup push.9 add
# => [next_turn_index + 9, next_turn_index]
exec.account::get_item
drop drop drop
# => [is_fold, next_turn_index]

if.true
# if player has folded
dup push.103 lte
# => [0/1, next_turn_index]

if.true
push.13 add
push.1
# => [1, next_turn_index + 13]
else
push.0
end
else
# checking is fold
# => [next_turn_index]

padw drop
# => [0, 0, 0, next_turn_index]

push.CURRENT_TURN_INDEX # slot of current turn
# => [CURRENT_TURN_INDEX, 0, 0, 0, next_turn_index]

exec.account::set_item
dropw dropw
# => [...]
push.0
end
end
drop
# => [...]
end


# some basic account methods

export.basic_wallet::receive_asset
Expand Down
99 changes: 99 additions & 0 deletions lib/contracts/notes/game/fold.masm
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use.miden::account
use.miden::note
use.miden::contracts::wallets::basic->wallet

const.CURRENT_TURN_INDEX=60

proc.play_fold
push.CURRENT_TURN_INDEX exec.account::get_item
# => [0, 0, 0, current_turn_index]

drop drop drop
# => [current_turn_index]

push.9
# 9, current_turn_index
add
# => [current_turn_index + 9]
dup #for getting the get item
dup #for updating the turn
# => [current_turn_index + 9, current_turn_index + 9, current_turn_index + 9]

# getting the is_fold of player
exec.account::get_item
drop drop drop
# => [is_fold, current_turn_index + 9, current_turn_index + 9]

# making sure that is_fold is zero currently
assertz
# => [current_turn_index + 9, current_turn_index + 9]
push.1
# => [1, current_turn_index + 9, current_turn_index + 9]

swap padw drop movup.3
# => [current_turn_index + 9, 0, 0, 0, 1, current_turn_index + 9]
exec.account::set_item
dropw drop drop drop
# => [current_turn_index + 9]

# adding 4 will give the slot of next player
push.4 add
push.1
# => [1, next_turn_index]

while.true

dup push.9 add
# => [next_turn_index + 9, next_turn_index]
exec.account::get_item
drop drop drop
# => [is_fold, next_turn_index]

if.true
# if player has folded
dup push.103 lte
# => [0/1, next_turn_index]

if.true
push.13 add
push.1
# => [1, next_turn_index + 13]
else
push.0
end
else
# checking is fold
# => [next_turn_index]

padw drop
# => [0, 0, 0, next_turn_index]

push.CURRENT_TURN_INDEX # slot of current turn
# => [CURRENT_TURN_INDEX, 0, 0, 0, next_turn_index]

exec.account::set_item
dropw dropw
# => [...]
push.0
end
end
drop
# => [...]
end

begin
dropw

call.play_fold
# => [...]

dropw
exec.note::get_assets drop mem_loadw
# => [ASSET, ...]

# load the asset and add it to the account
call.wallet::receive_asset
# => [...]

dropw
end
196 changes: 196 additions & 0 deletions lib/contracts/poker/poker.masm
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
use.miden::account

# const.CURRNET_TURN_PLAYER_PUB_KEY_INDEX=63

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can delete this file since we are not using it anywhere.

# index corresponding to the information saved
const.PLAYER_1_INDEX=62 # use this index to find other player pub key slot index which would always be second to the player account id
const.SMALL_BLIND_INDEX=56
const.BIG_BLIND_INDEX=57
const.BUY_IN_INDEX=58
const.NO_OF_PLAYERS_INDEX=59
const.CURRENT_TURN_INDEX=60
const.RAISER_INDEX=61
const.HIGHEST_BET_SLOT_INDEX=63
const.CURRENT_TURN_PLAYER_PUB_KEY_INDEX=64 # need to check it's storage initially small blind public key on slot 62

# this procedure just stores the cards as items in the account storage
export.receive_cards
exec.account::set_item

# prepare stack for return
movup.4 drop movup.4 drop movup.4 drop
end

# it helps to check that the intended player is calling the function
export.poker_auth_tx_rpo_falcon512
# Get commitments to created notes
exec.tx::get_output_notes_hash
# => [OUTPUT_NOTES_HASH, ...]

# Get commitments to consumed notes
exec.tx::get_input_notes_hash
# => [INPUT_NOTES_HASH, OUTPUT_NOTES_HASH, ...]

# Get current nonce of the account and pad
exec.account::get_nonce push.0.0.0
# => [0, 0, 0, nonce, INPUT_NOTES_HASH, OUTPUT_NOTES_HASH, ...]

# Get current AccountID and pad
exec.account::get_id push.0.0.0
# => [0, 0, 0, account_id, 0, 0, 0, nonce, INPUT_NOTES_HASH, OUTPUT_NOTES_HASH, ...]

# Compute the message to be signed
# M = h(OUTPUT_NOTES_HASH, h(INPUT_NOTES_HASH, h(0, 0, 0, account_id, 0, 0, 0, nonce)))
hmerge hmerge hmerge

# Get public key from account storage at pos 0 and verify signature
push.CURRENT_TURN_PLAYER_PUB_KEY_INDEX exec.account::get_item
# => [PUB_KEY, M]

# Verify the signature against the public key and the message. The procedure gets as
# inputs the hash of the public key and the hash of the message via the operand
# stack. The signature is provided via the advice stack. The signature is valid if and
# only if the procedure returns.
exec.rpo_falcon512::verify
# => []

# Update the nonce
push.1 exec.account::incr_nonce
# => []
end

# should be executed using transcripts
export.play_call
# when user wants to just call and match the bet
# fetch the current bet we need to 2 to the player_1_index

push.PLAYER_1_INDEX.2 add exec.account::get_item
# => [0, 0, 0, current bet] since we are currently storing everything in word
drop drop drop
# => [current bet]
push.HIGHEST_BET_SLOT_INDEX exec.account::get_item
# => [0, 0, 0, highest bet, current bet]
drop drop drop
sub
# => [highest bet - current bet] subtract this amount from player balance
# => [difference]
# using current turn index found player index
const.CURRENT_PLAYER_INDEX=100
push.CURRENT_PLAYER_INDEX.4 # At 4th we are saving using balance
exec.account::get_item
# => [balance, difference]
sub
# => [balance - difference]
push.CURRENT_PLAYER_INDEX.4
exec.account::set_item
end

export.play_check
# update the turn
# balance, points will remain same
push.CURRENT_TURN_INDEX exec.account::get_item
# [0, 0, 0, current turn index]
drop
drop
drop
# [current turn index]

push.8
add
push.44
mod
# currnt_turn_index + 8

dup
# currnt_turn_index + 8, currnt_turn_index + 8

mem_store.0
#currnt_turn_index + 8

while
# now we know that this is the index we need to use as the next player index
# currnt_turn_index + 8
push.4
# 4, currnt_turn_index + 8
add
exec::account::get_item
drop drop drop
# [is fold]
if
# checking is fold
mem_load.0 # player ka index
push.CURRENT_TURN_INDEX # slot (player ka index )
exec::account::set_item
push.0
else
push.8
add
end
end

export.play_raise
# update the turn
# raise the amount of current bet

end

export.play_fold
# make is_fold of player 1
# inc turn
push.CURRENT_TURN_INDEX
# [0, 0, 0, current_turn_index]

drop drop drop
# [current_turn_index]

push.4
# 4, current_turn_index
add
# [4+current_turn_index]
dup #for getting the get item
dup #for updating the turn
# [4+current_turn_index, 4+current_turn_index, 4+current_turn_index]

# getting the is_fold of player
exec::account::get_item
drop drop drop
#[is_fold, 4+current_turn_index, 4+current_turn_index]

# making sure that is_fold is zero currently
dup
push.0
equal

#[is_fold, 4+current_turn_index, 4+current_turn_index]
swap
exec::account::set_item
#[4+current_turn_index]

# adding 1 will give the slot of next player
push.1
add
push.44
mod

#[next_turn_index]

while

push.4
# 4, next_turn_index
add
exec::account::get_item
drop drop drop
# [is fold]
if
# checking is fold
mem_load.0 # player ka index
push.CURRENT_TURN_INDEX # slot (player ka index )
exec::account::set_item
push.0
else
push.8
add
end

end
4 changes: 2 additions & 2 deletions lib/src/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ fn construct_game_constructor_storage(
];
player_pub_keys.extend(player_slots);

slot_index += 8; // since the mid 9 elements would cover the player stats and initially all those values are zero
slot_index += 13; // since the mid 9 elements would cover the player stats and initially all those values are zero
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you hardcode this different in constant, And name it something suitable.

}

// merghe player_id with card_suit
// merge player_id with card_suit
game_info.push(auth_slot);
game_info.extend(cards);
game_info.extend(game_stats);
Expand Down
Loading
Loading