Skip to content

Commit bbb80b2

Browse files
committed
Add my rewritten referee code which has a bit more structure
1 parent 606b915 commit bbb80b2

File tree

1 file changed

+200
-0
lines changed

1 file changed

+200
-0
lines changed

onchain/structured-referee.clsp

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
(include *standard-cl-23*)
2+
(import std.assert)
3+
(import std.curry)
4+
(import std.shatree)
5+
(import std.prefix)
6+
(import std.relops)
7+
(import std.append exposing (append as concat))
8+
(import std.condition_codes)
9+
(import std.match)
10+
(import std.li)
11+
(import onchain.game_codes)
12+
13+
(defmac REFEREE_INFO ()
14+
(q . (@ REFEREE_INFO
15+
(MOVER_PUZZLE_HASH
16+
WAITER_PUZZLE_HASH
17+
TIMEOUT
18+
AMOUNT
19+
MOD_HASH
20+
NONCE
21+
MOVE
22+
MAX_MOVE_SIZE
23+
VALIDATION_INFO_HASH
24+
MOVER_SHARE
25+
PREVIOUS_VALIDATION_INFO_HASH
26+
)
27+
)
28+
)
29+
)
30+
31+
(defun handle-referee-timeout ((REFEREE_INFO))
32+
(list
33+
34+
(list ASSERT_HEIGHT_RELATIVE TIMEOUT)
35+
36+
(i
37+
MOVER_SHARE
38+
(list CREATE_COIN MOVER_PUZZLE_HASH MOVER_SHARE)
39+
(list 1)
40+
)
41+
42+
(i
43+
(- AMOUNT MOVER_SHARE)
44+
(list CREATE_COIN WAITER_PUZZLE_HASH (- AMOUNT MOVER_SHARE))
45+
(list 1)
46+
)
47+
)
48+
)
49+
50+
(defun handle-slash ((REFEREE_INFO) previous_state previous_validation_program mover_puzzle solution evidence)
51+
(assign
52+
53+
previous_validation_program_hash (shatree previous_validation_program)
54+
55+
(assert
56+
(= MOVER_PUZZLE_HASH (shatree mover_puzzle))
57+
(= PREVIOUS_VALIDATION_INFO_HASH
58+
(sha256
59+
previous_validation_program_hash
60+
(shatree previous_state))
61+
)
62+
63+
;; usually returns the conditions verbatim
64+
(c (list REMARK)
65+
(concat
66+
(a previous_validation_program
67+
(c previous_validation_program_hash @))
68+
(a mover_puzzle solution))
69+
)
70+
)
71+
)
72+
)
73+
74+
(defun new-referee-info-with-updates
75+
((REFEREE_INFO)
76+
new_move
77+
new_max_move_size
78+
new_validation_info_hash
79+
new_mover_share
80+
)
81+
(list
82+
WAITER_PUZZLE_HASH
83+
MOVER_PUZZLE_HASH
84+
TIMEOUT
85+
AMOUNT
86+
MOD_HASH
87+
NONCE
88+
new_move
89+
new_max_move_size
90+
new_validation_info_hash
91+
new_mover_share
92+
VALIDATION_INFO_HASH
93+
)
94+
)
95+
96+
(defun handle-move ((REFEREE_INFO) new_move new_validation_info_hash new_mover_share new_max_move_size mover_puzzle solution)
97+
(assign
98+
99+
new_puzzle_hash
100+
(curry_hashes
101+
MOD_HASH
102+
(shatree
103+
(new-referee-info-with-updates
104+
REFEREE_INFO
105+
new_move
106+
new_max_move_size
107+
new_validation_info_hash
108+
new_mover_share
109+
new_max_move_size
110+
)
111+
)
112+
)
113+
114+
conditions (a mover_puzzle solution)
115+
116+
(assert
117+
VALIDATION_INFO_HASH
118+
119+
(<= (strlen MOVE) MAX_MOVE_SIZE)
120+
121+
(<= new_mover_share AMOUNT)
122+
123+
(>= new_mover_share 0)
124+
125+
(logior
126+
(not new_validation_info_hash)
127+
(= 32 (strlen new_validation_info_hash))
128+
)
129+
130+
(= MOVER_PUZZLE_HASH (shatree mover_puzzle))
131+
132+
;; Check that the child output is made
133+
(match
134+
(lambda ((& new_puzzle_hash AMOUNT) (condname arg1 arg2))
135+
(logand (= condname CREATE_COIN) (= arg1 new_puzzle_hash) (= arg2 AMOUNT))
136+
)
137+
conditions
138+
)
139+
140+
(li
141+
(list REMARK new_move new_validation_info_hash new_mover_share new_max_move_size)
142+
(list ASSERT_BEFORE_HEIGHT_RELATIVE TIMEOUT) &rest conditions)
143+
)
144+
)
145+
)
146+
147+
; Adjudicates a two player turn based game
148+
;
149+
; MOVE, VALIDATION_HASH and MOVER_SHARE were all accepted optimistically from the
150+
; last move
151+
;
152+
; Both VALIDATION_HASH values are a sha256 of a validation program hash and the
153+
; shatree of a state
154+
;
155+
; The next validation program hash may be nil which means no futher moves are
156+
; allowed
157+
;
158+
; MOVER_SHARE is how much the mover will get if they fold/accept
159+
; MOD_HASH should be the shatree of referee itself
160+
; NONCE is for anti-replay prevention
161+
;
162+
; If action is timeout args is nil
163+
;
164+
; If action is slash args is (state validation_program mover_puzzle solution
165+
; evidence)
166+
;
167+
; If action is move args is (new_move new_validation_info_hash new_mover_share
168+
; mover_puzzle solution)
169+
;
170+
; validation programs get passed this:
171+
; ((last_move
172+
; next_validation_hash
173+
; my_share
174+
; me_hash
175+
; my_puzzle_hash
176+
; opponent_puzzle_hash
177+
; amount
178+
; timeout
179+
; max_move_size
180+
; referee_hash)
181+
; state
182+
; me
183+
; mover_puzzle
184+
; solution
185+
; evidence
186+
; )
187+
188+
(export (CURRIED_REFEREE_INFO . args)
189+
190+
(if (not args)
191+
(handle-referee-timeout &rest @)
192+
193+
(l (f (r args)))
194+
; slash
195+
(handle-slash &rest @)
196+
197+
; move
198+
(handle-move &rest @)
199+
)
200+
)

0 commit comments

Comments
 (0)