Skip to content

Commit

Permalink
Add my rewritten referee code which has a bit more structure
Browse files Browse the repository at this point in the history
  • Loading branch information
prozacchiwawa committed May 15, 2024
1 parent 606b915 commit bbb80b2
Showing 1 changed file with 200 additions and 0 deletions.
200 changes: 200 additions & 0 deletions onchain/structured-referee.clsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
(include *standard-cl-23*)
(import std.assert)
(import std.curry)
(import std.shatree)
(import std.prefix)
(import std.relops)
(import std.append exposing (append as concat))
(import std.condition_codes)
(import std.match)
(import std.li)
(import onchain.game_codes)

(defmac REFEREE_INFO ()
(q . (@ REFEREE_INFO
(MOVER_PUZZLE_HASH
WAITER_PUZZLE_HASH
TIMEOUT
AMOUNT
MOD_HASH
NONCE
MOVE
MAX_MOVE_SIZE
VALIDATION_INFO_HASH
MOVER_SHARE
PREVIOUS_VALIDATION_INFO_HASH
)
)
)
)

(defun handle-referee-timeout ((REFEREE_INFO))
(list

(list ASSERT_HEIGHT_RELATIVE TIMEOUT)

(i
MOVER_SHARE
(list CREATE_COIN MOVER_PUZZLE_HASH MOVER_SHARE)
(list 1)
)

(i
(- AMOUNT MOVER_SHARE)
(list CREATE_COIN WAITER_PUZZLE_HASH (- AMOUNT MOVER_SHARE))
(list 1)
)
)
)

(defun handle-slash ((REFEREE_INFO) previous_state previous_validation_program mover_puzzle solution evidence)
(assign

previous_validation_program_hash (shatree previous_validation_program)

(assert
(= MOVER_PUZZLE_HASH (shatree mover_puzzle))
(= PREVIOUS_VALIDATION_INFO_HASH
(sha256
previous_validation_program_hash
(shatree previous_state))
)

;; usually returns the conditions verbatim
(c (list REMARK)
(concat
(a previous_validation_program
(c previous_validation_program_hash @))
(a mover_puzzle solution))
)
)
)
)

(defun new-referee-info-with-updates
((REFEREE_INFO)
new_move
new_max_move_size
new_validation_info_hash
new_mover_share
)
(list
WAITER_PUZZLE_HASH
MOVER_PUZZLE_HASH
TIMEOUT
AMOUNT
MOD_HASH
NONCE
new_move
new_max_move_size
new_validation_info_hash
new_mover_share
VALIDATION_INFO_HASH
)
)

(defun handle-move ((REFEREE_INFO) new_move new_validation_info_hash new_mover_share new_max_move_size mover_puzzle solution)
(assign

new_puzzle_hash
(curry_hashes
MOD_HASH
(shatree
(new-referee-info-with-updates
REFEREE_INFO
new_move
new_max_move_size
new_validation_info_hash
new_mover_share
new_max_move_size
)
)
)

conditions (a mover_puzzle solution)

(assert
VALIDATION_INFO_HASH

(<= (strlen MOVE) MAX_MOVE_SIZE)

(<= new_mover_share AMOUNT)

(>= new_mover_share 0)

(logior
(not new_validation_info_hash)
(= 32 (strlen new_validation_info_hash))
)

(= MOVER_PUZZLE_HASH (shatree mover_puzzle))

;; Check that the child output is made
(match
(lambda ((& new_puzzle_hash AMOUNT) (condname arg1 arg2))
(logand (= condname CREATE_COIN) (= arg1 new_puzzle_hash) (= arg2 AMOUNT))
)
conditions
)

(li
(list REMARK new_move new_validation_info_hash new_mover_share new_max_move_size)
(list ASSERT_BEFORE_HEIGHT_RELATIVE TIMEOUT) &rest conditions)
)
)
)

; Adjudicates a two player turn based game
;
; MOVE, VALIDATION_HASH and MOVER_SHARE were all accepted optimistically from the
; last move
;
; Both VALIDATION_HASH values are a sha256 of a validation program hash and the
; shatree of a state
;
; The next validation program hash may be nil which means no futher moves are
; allowed
;
; MOVER_SHARE is how much the mover will get if they fold/accept
; MOD_HASH should be the shatree of referee itself
; NONCE is for anti-replay prevention
;
; If action is timeout args is nil
;
; If action is slash args is (state validation_program mover_puzzle solution
; evidence)
;
; If action is move args is (new_move new_validation_info_hash new_mover_share
; mover_puzzle solution)
;
; validation programs get passed this:
; ((last_move
; next_validation_hash
; my_share
; me_hash
; my_puzzle_hash
; opponent_puzzle_hash
; amount
; timeout
; max_move_size
; referee_hash)
; state
; me
; mover_puzzle
; solution
; evidence
; )

(export (CURRIED_REFEREE_INFO . args)

(if (not args)
(handle-referee-timeout &rest @)

(l (f (r args)))
; slash
(handle-slash &rest @)

; move
(handle-move &rest @)
)
)

0 comments on commit bbb80b2

Please sign in to comment.