templates.test_eval_order

  1import smartpy as sp
  2
  3
  4@sp.module
  5def main():
  6    class Sub(sp.Contract):
  7        @sp.onchain_view()
  8        def my_view(self):
  9            return 42
 10
 11    class Main(sp.Contract):
 12        def __init__(self, sub):
 13            self.data.x = ""
 14            self.data.sub = sub
 15
 16        @sp.private(with_storage="read-write")
 17        def f_unit(self, x):
 18            self.data.x = x
 19            return ()
 20
 21        @sp.private(with_storage="read-write")
 22        def f_int(self, x):
 23            self.data.x = sp.fst(x)
 24            return sp.cast(sp.snd(x), sp.int)
 25
 26        @sp.private(with_storage="read-write")
 27        def f_nat(self, x):
 28            self.data.x = sp.fst(x)
 29            return sp.cast(sp.snd(x), sp.nat)
 30
 31        @sp.private(with_storage="read-write")
 32        def f_string(self, x):
 33            self.data.x = sp.fst(x)
 34            return sp.cast(sp.snd(x), sp.string)
 35
 36        @sp.private(with_storage="read-write")
 37        def f_bytes(self, x):
 38            self.data.x = sp.fst(x)
 39            return sp.cast(sp.snd(x), sp.bytes)
 40
 41        @sp.private(with_storage="read-write")
 42        def f_mutez(self, x):
 43            self.data.x = sp.fst(x)
 44            return sp.cast(sp.snd(x), sp.mutez)
 45
 46        @sp.private(with_storage="read-write")
 47        def f_timestamp(self, x):
 48            self.data.x = sp.fst(x)
 49            return sp.cast(sp.snd(x), sp.timestamp)
 50
 51        @sp.private(with_storage="read-write")
 52        def f_bool(self, x):
 53            self.data.x = sp.fst(x)
 54            return sp.cast(sp.snd(x), sp.bool)
 55
 56        @sp.private(with_storage="read-write")
 57        def f_map_int(self, x):
 58            self.data.x = sp.fst(x)
 59            return sp.cast(sp.snd(x), sp.map[sp.int, sp.int])
 60
 61        @sp.private(with_storage="read-write")
 62        def f_map_nat(self, x):
 63            self.data.x = sp.fst(x)
 64            return sp.cast(sp.snd(x), sp.map[sp.nat, sp.nat])
 65
 66        @sp.private(with_storage="read-write")
 67        def f_set_nat(self, x):
 68            self.data.x = sp.fst(x)
 69            return sp.cast(sp.snd(x), sp.set[sp.nat])
 70
 71        @sp.private(with_storage="read-write")
 72        def f_list_int(self, x):
 73            self.data.x = sp.fst(x)
 74            return sp.cast(sp.snd(x), sp.list[sp.int])
 75
 76        @sp.private(with_storage="read-write")
 77        def f_list_nat(self, x):
 78            self.data.x = sp.fst(x)
 79            return sp.cast(sp.snd(x), sp.list[sp.nat])
 80
 81        # @sp.private(with_storage="read-write")
 82        # def f_pair(self,x):
 83        #     self.data.x = sp.fst(x)
 84        #     return sp.cast(sp.snd(x), sp.pair[sp.nat,sp.nat])
 85
 86        @sp.private(with_storage="read-write")
 87        def f_option(self, x):
 88            self.data.x = sp.fst(x)
 89            return sp.cast(sp.snd(x), sp.option[sp.int])
 90
 91        @sp.private(with_storage="read-write")
 92        def f_option_key_hash(self, x):
 93            self.data.x = sp.fst(x)
 94            return sp.cast(sp.snd(x), sp.option[sp.key_hash])
 95
 96        @sp.private(with_storage="read-write")
 97        def f_lambda(self, x):
 98            self.data.x = sp.fst(x)
 99            return sp.cast(sp.snd(x), sp.lambda_(sp.int, sp.int))
100
101        # @sp.private(with_storage="read-write")
102        # def f_chest(self,x):
103        #     self.data.x = sp.fst(x)
104        #     sp.result(sp.cast(sp.snd(x), sp.TChest))
105
106        @sp.private(with_storage="read-write")
107        def f_contract(self, x):
108            self.data.x = sp.fst(x)
109            return sp.cast(sp.snd(x), sp.contract[sp.int])
110
111        # @sp.private(with_storage="read-write")
112        # def f_chest_key(self,x):
113        #     self.data.x = sp.fst(x)
114        #     sp.result(sp.cast(sp.snd(x), sp.TChest_key))
115
116        @sp.private(with_storage="read-write")
117        def f_lambda2(self, x):
118            self.data.x = sp.fst(x)
119            return sp.cast(sp.snd(x), sp.lambda_(sp.pair[sp.int, sp.int], sp.int))
120
121        # @sp.private(with_storage="read-write")
122        # def f_address(self,x):
123        #     self.data.x = sp.fst(x)
124        #     return sp.cast(sp.snd(x), sp.address)
125
126        # @sp.private(with_storage="read-write")
127        # def f_ticket(self,x):
128        #     (x1, x2) = x
129        #     self.data.x = x1
130        #     return sp.cast(x2, sp.ticket[sp.int])
131
132        @sp.private(with_storage="read-write")
133        def f_key(self, x):
134            (x1, x2) = x
135            self.data.x = x1
136            return sp.cast(x2, sp.key)
137
138        @sp.private(with_storage="read-write")
139        def f_signature(self, x):
140            (x1, x2) = x
141            self.data.x = x1
142            return sp.cast(x2, sp.signature)
143
144        @sp.entrypoint
145        def test_prim2(self):
146            _ = self.f_map_int(("A", {})).get_opt(self.f_int(("B", 1)))
147            assert self.data.x == "A"
148            _ = self.f_map_int(("A", {})).contains(self.f_int(("B", 1)))
149            assert self.data.x == "A"
150            _ = self.f_lambda(("A", lambda x: x))(self.f_int(("B", 1)))
151            assert self.data.x == "B"
152            _ = self.f_lambda2(("A", lambda _: 0)).apply(self.f_int(("B", 1)))
153            assert self.data.x == "A"
154            _ = sp.cons(self.f_int(("A", 1)), self.f_list_int(("B", [])))
155            assert self.data.x == "A"
156            _ = sp.add_seconds(
157                self.f_timestamp(("A", sp.timestamp(1))), self.f_int(("B", 1))
158            )
159            assert self.data.x == "A"
160            _ = sp.ticket(self.f_int(("A", 1)), self.f_nat(("B", 1)))
161            assert self.data.x == "A"
162            # _ = sp.view("my_view", a_address, b_unit)
163            # assert self.data.x == "A"
164
165            _ = self.f_int(("A", 1)) + self.f_int(("B", 1))
166            assert self.data.x == "A"
167            _ = self.f_int(("A", 1)) * self.f_int(("B", 1))
168            assert self.data.x == "A"
169            _ = self.f_int(("A", 1)) - self.f_int(("B", 1))
170            assert self.data.x == "A"
171            _ = self.f_int(("A", 1)) + self.f_int(("B", 1))
172            assert self.data.x == "A"
173            _ = self.f_nat(("A", 1)) / self.f_nat(("B", 1))
174            assert self.data.x == "A"
175            _ = self.f_int(("A", 1)) == self.f_int(("B", 1))
176            assert self.data.x == "A"
177            _ = self.f_int(("A", 1)) != self.f_int(("B", 1))
178            assert self.data.x == "A"
179            _ = self.f_int(("A", 1)) < self.f_int(("B", 1))
180            assert self.data.x == "A"
181            _ = self.f_int(("A", 1)) <= self.f_int(("B", 1))
182            assert self.data.x == "A"
183            _ = self.f_int(("A", 1)) > self.f_int(("B", 1))
184            assert self.data.x == "A"
185            _ = self.f_int(("A", 1)) >= self.f_int(("B", 1))
186            assert self.data.x == "A"
187            _ = sp.mod(self.f_int(("A", 1)), self.f_int(("B", 1)))
188            assert self.data.x == "A"
189            _ = self.f_nat(("A", 1)) ^ self.f_nat(("B", 1))
190            assert self.data.x == "A"
191            _ = sp.min(self.f_int(("A", 1)), self.f_int(("B", 1)))
192            assert self.data.x == "A"
193            _ = sp.max(self.f_int(("A", 1)), self.f_int(("B", 1)))
194            assert self.data.x == "A"
195            _ = sp.ediv(self.f_int(("A", 1)), self.f_int(("B", 1)))
196            assert self.data.x == "A"
197
198            _ = self.f_bool(("A", False)) or self.f_bool(("B", False))
199            assert self.data.x == "B"
200            _ = self.f_bool(("A", False)) or self.f_bool(("B", True))
201            assert self.data.x == "B"
202            _ = self.f_bool(("A", True)) or self.f_bool(("B", False))
203            assert self.data.x == "A"
204            _ = self.f_bool(("A", True)) or self.f_bool(("B", True))
205            assert self.data.x == "A"
206
207            _ = self.f_bool(("A", False)) and self.f_bool(("B", False))
208            assert self.data.x == "A"
209            _ = self.f_bool(("A", False)) and self.f_bool(("B", True))
210            assert self.data.x == "A"
211            _ = self.f_bool(("A", True)) and self.f_bool(("B", False))
212            assert self.data.x == "B"
213            _ = self.f_bool(("A", True)) and self.f_bool(("B", True))
214            assert self.data.x == "B"
215
216        @sp.entrypoint
217        def test_prim3(self):
218            _ = sp.split_tokens(
219                self.f_mutez(("A", sp.mutez(1))),
220                self.f_nat(("B", 1)),
221                self.f_nat(("C", 1)),
222            )
223            assert self.data.x == "A"
224            _ = sp.split_tokens(sp.mutez(0), self.f_nat(("B", 1)), self.f_nat(("C", 1)))
225            assert self.data.x == "B"
226
227            _ = range(self.f_nat(("A", 1)), self.f_nat(("B", 1)), self.f_nat(("C", 1)))
228            assert (
229                self.data.x == "A"
230            )  # TODO Check if there is still interpreter-compiler inconsistency
231
232            _ = sp.update_map(
233                self.f_int(("A", 1)),
234                self.f_option(("B", sp.Some(1))),
235                self.f_map_int(("C", {})),
236            )
237            assert self.data.x == "A"
238            _ = sp.update_map(
239                0, self.f_option(("B", sp.Some(1))), self.f_map_int(("C", {}))
240            )
241            assert self.data.x == "B"
242
243            _ = sp.get_and_update(
244                self.f_int(("A", 1)),
245                self.f_option(("B", sp.Some(1))),
246                self.f_map_int(("C", {})),
247            )
248            assert self.data.x == "A"
249            _ = sp.get_and_update(
250                0,
251                self.f_option(("B", sp.Some(1))),
252                self.f_map_int(("C", {})),
253            )
254            assert self.data.x == "B"
255
256            _ = (
257                self.f_int(("B", 1))
258                if self.f_bool(("A", True))
259                else self.f_int(("C", 1))
260            )
261            assert self.data.x == "B"
262            _ = 0 if self.f_bool(("A", True)) else self.f_int(("C", 1))
263            assert self.data.x == "A"
264
265            _ = (
266                self.f_int(("B", 1))
267                if self.f_bool(("A", False))
268                else self.f_int(("C", 1))
269            )
270            assert self.data.x == "C"
271            _ = self.f_int(("B", 1)) if self.f_bool(("A", False)) else 0
272            assert self.data.x == "A"
273
274        @sp.entrypoint
275        def test_mprim2(self):
276            _ = self.f_nat(("A", 1)) << self.f_nat(("B", 1))
277            assert self.data.x == "A"
278
279            _ = self.f_nat(("A", 1)) >> self.f_nat(("B", 1))
280            assert self.data.x == "A"
281
282        @sp.entrypoint
283        def test_mprim3(self, p):
284            # sp.cast(p.chest_key, sp.chest_key)
285
286            #     # chest     = sp.chest("0xbcf89e8a80d99cd49690dda59accc4d381c1a3facbacc1a3f9aab5e7ecdc8db6b0928eed84a7e5d9958e96b5b1f3c8ddd9b98dd394f9bafaafad85a2d7cbffeec1e280d5c6ccbab3caddb5ced3c1d39c82cea0cc99b7f788bbca85eeb1badd91fdc8e1e1e9a6cddca7bdef8faaf6acf2aeef8ccbd3d987f6d0ecf9c8c9818c92eb8a81e385fff9e8c4ad96d6e7bad6a480ec83f3cb84bedeac82baafa1b88bfd90968b9ee7b2d8aabef9d8e0d9a9c8a5a8859a8db5e6d496de95cfd2cfc4dcfae3edf9f1d5faee998694ed8aada2a7d4b88aecc8acda98d6f69cfb8df3a5ca80b48d91aa9bbbf68f918998ddc5d3cc82d3f0ce81869fd9edb8b0989dcfa5e9b59db18dd099c7b8bb848586afc7c8888f86d2c6988eeee5e5fbfc9de8a7f19e84ebc2b687049390ebd5cecb86eecfe2bef994f9a7c3eabeabdda0b4b7b59381a8a4c3d7c1b382bcf6e1adb2e3c6ce9ca5e0c0a0beb999f4eacc89cf858fb282b294d99ec2daccd9cdcc98a98b8cb5b0bbf9ed80b0f6ccd2a994f3a1faafbbacf7eebbec9399bfdfd1f0da9ee4aa9dcb8b9c85fcab87bd86b98fd7cc9a8cb2e3cc93ef86df83e5ce9bbda8c7d4cad0e4b893fde8e18b9cf283f9829f9ab8f48da8b0f1b5c8f3a1d2a1a6e5c1fec0abb09eb9b0c2c3ae9f9df6c6a7bad396f7b3b1fc90e490a4f2a5f599fedbc8b3b297a99db3b285a3f0acefe0c8d4cec08195af8098d0f4b8d488bdacd7e8effea5cdaf8dd1ccdbe4fee59eb7e1cebff2eb839b8f81d598abc798d5bdcea59bfcbaf89fbdbbcb8182a3eea1f4f5dccafcb4ddd4ff83d3879edef5d3ca06d2f1146cf4c25faf0e432bf540b4de74fae3cc68e0bc57c700000021cf744e778f4d5e220d4be7310077e6735795b6c90bbe2bdcdd9b686e3c71f833f2")
287
288            #     # a_chest_key  = self.f_chest_key(("A", p.chest_key))
289
290            #     # b_chest      = self.f_chest(("B", chest))
291
292            #     # Time-lock is deprecated.
293            #     # _ = sp.open_chest(a_chest_key, b_chest, self.f_nat(("C", 1)))
294            #     # assert self.data.x == "A"
295            #     # _ = sp.open_chest(p.chest_key, b_chest, self.f_nat(("C", 1)))
296            #     # assert self.data.x == "B"
297
298            assert sp.check_signature(
299                self.f_key(("A", p.key)),
300                self.f_signature(("B", p.signature)),
301                self.f_bytes(("C", sp.bytes("0x00"))),
302            )
303            assert self.data.x == "A"
304            assert sp.check_signature(
305                p.key,
306                self.f_signature(("B", p.signature)),
307                self.f_bytes(("C", sp.bytes("0x00"))),
308            )
309            assert self.data.x == "B"
310
311        @sp.entrypoint
312        def test_expr_other(self):
313            #     _ = self.f_option(("A", sp.Some(1))).unwrap_some(self.f_int(("B", 1)))
314            #     assert self.data.x == "A"
315            #     _ = sp.Some(1).unwrap_some(self.f_int(("B", 1)))
316            #     assert self.data.x == "A"
317
318            _ = self.f_map_int(("A", {1: 2}))[self.f_int(("B", 1))]
319            assert self.data.x == "B"
320            _ = (self.f_int(("A", 1)), self.f_int(("B", 1)))
321            assert self.data.x == "A"
322            _ = sp.record(a=self.f_int(("A", 1)), b=self.f_int(("B", 1)))
323            # assert self.data.x == "A" # TODO order + Check if there is still interpreter-compiler inconsistency
324            _ = [self.f_int(("A", 1)), self.f_int(("B", 1))]
325            assert self.data.x == "A"
326            _ = {self.f_int(("A", 1)), self.f_int(("B", 1))}
327            assert self.data.x == "A"
328            _ = {
329                self.f_int(("A", 1)): self.f_int(("B", 1)),
330                self.f_int(("C", 1)): self.f_int(("D", 1)),
331            }
332            # assert self.data.x == "A" # TODO order
333            _ = {1: self.f_int(("B", 1)), self.f_int(("C", 1)): self.f_int(("D", 1))}
334            # assert self.data.x == "B" # TODO order
335            #     # TODO ESaplingVerifyUpdate
336            #     _ = self.f_list_int(("A", [])).map(self.f_lambda(("B", lambda x: x)))
337            #     assert self.data.x == "A"
338
339            m = {}
340            # self.f_map_nat(("A", m))[self.f_nat(("B", 1))] = len(self.f_map_nat(("C", m)))
341            m[self.f_nat(("B", 1))] = len(self.f_map_nat(("C", m)))
342            assert self.data.x == "B" and m[1] == 0 and len(m) == 1
343            # self.f_map_nat(("A", m))[self.f_nat(("B", 2))] = len(self.f_map_nat(("C", m)))
344            m[self.f_nat(("B", 2))] = len(self.f_map_nat(("C", m)))
345            assert self.data.x == "B" and m[2] == 1 and len(m) == 2
346            s = set()
347            # self.f_set_nat(("A", s)).add(len(self.f_set_nat(("B", s))))
348            s.add(len(self.f_set_nat(("B", s))))
349            assert self.data.x == "B" and s.contains(0) and len(s) == 1
350            # self.f_set_nat(("A", s)).add(len(self.f_set_nat(("B", s))))
351            s.add(len(self.f_set_nat(("B", s))))
352            assert self.data.x == "B" and s.contains(1) and len(s) == 2
353            l = []
354            # self.f_list_nat(("A", l)).push(len(self.f_list_nat(("B", l))))
355            l.push(len(self.f_list_nat(("B", l))))
356            assert self.data.x == "B" and sum(l) == 0 and len(l) == 1
357            # self.f_list_nat(("A", l)).push(len(self.f_list_nat(("B", l))))
358            l.push(len(self.f_list_nat(("B", l))))
359            assert self.data.x == "B" and sum(l) == 1 and len(l) == 2
360
361            _ = sp.create_contract_operation(
362                Sub,
363                self.f_option_key_hash(("A", None)),
364                self.f_mutez(("B", sp.mutez(1))),
365                self.f_unit(("C")),
366            )
367            assert self.data.x == "A"
368            _ = sp.create_contract_operation(
369                Sub, None, self.f_mutez(("B", sp.mutez(1))), self.f_unit(("C"))
370            )
371            assert self.data.x == "B"
372
373            _ = sp.slice(
374                self.f_nat(("A", 1)), self.f_nat(("B", 1)), self.f_string(("C", "abc"))
375            )
376            assert self.data.x == "A"
377            _ = sp.slice(0, self.f_nat(("B", 1)), self.f_string(("C", "abc")))
378            assert self.data.x == "B"
379
380            _ = sp.transfer_operation(
381                self.f_int(("A", 1)),
382                self.f_mutez(("B", sp.mutez(1))),
383                self.f_contract(("C", sp.self_entrypoint("put_int"))),
384            )
385            assert self.data.x == "A"
386            _ = sp.transfer_operation(
387                0,
388                self.f_mutez(("B", sp.mutez(1))),
389                self.f_contract(("C", sp.self_entrypoint("put_int"))),
390            )
391            assert self.data.x == "B"
392
393        @sp.entrypoint
394        def put_int(self, x):
395            sp.cast(x, sp.int)
396
397
398@sp.add_test()
399def test():
400    s = sp.test_scenario("Test_eval_order", main)
401
402    sub = main.Sub()
403    s += sub
404
405    main_ = main.Main(sub.address)
406    s += main_
407
408    main_.test_prim2()
409    main_.test_prim3()
410    main_.test_mprim2()
411
412    # chest_key = sp.chest_key("0xf5d3cec5da988edfbee8be85b4919bc7c5a3f7ecebe7a19ac8e0c3f9c3aac5948de9b9dddab88b93c4fee4d094a3a5e48494c0ae80b4ffbcd9a8b7a2c9f3dfc0e2e18082f3d3ceabf9f1a7c8fcd4dec7b7e091a89e83f8a6ab90dea1bdbd95bff9ed8ac89c88ebeecfe5faaeb8f184e5df9298e5aff3bad5bbc1fcefdbb5b2929df9c6ced9aebefbc49decb4d9e085b1a8c39099818d97e0b3dbf287efb99c87c8eeb38f86d9e9c2bdeccec4abb5ce89bce988aba5a0adf59180898697a7e5d1dfa8d5a6aac8f0f5bd87ecb7aeaedec6a4f7f5bfead9e1cbd3a8a88fddcb939198f0ae94cecdc3e9d3d9c2b28ece8ff487a0e983868ef995b3ee90f082adf8aeddbab2efc2b5fcd5aff1a19bb3acff83b08986d0ffa3eec4afabbec280f4d498b1f2e6de01c192a381d3b8c19dd8edb4d6e689cbccb1b5c396c7abda9bc4ed9edf9ecff7c78bab8fadbbd4f4e7d1d289eace839becdfa5e7d1ddc2b0e9e1e88d87a3e3aae8c6dd889cd980e69af8b992d4b5e28cb691de94c0afe0f3fbde8b85e4eced9ec8dcd986d189e4a8bcd3fda2efbce5cde9d68499eef7bda5a782c3adf0cc84abd0c0ec91988b9a81ad819af19e97eefacea9a9b1ffe2d1bcc1e5d8dc87e89d8a92afd3fcf4d4d2e1d9e4f583c5a583ebb6f19dd7abd3f8f5d6a8ee9bf78faa8aedf0d8b0e6a1a6bf95849192b5f8bfef9581deaebef3e6f390ac99ade490e7feb5d0d589cce2e3d4b9ced493e3fac1da83f697e6c0e68f8295ec97f2c0aae7dcaffaedb5fef283d4a5b4f3fdd8e7a0b0f9c4accce9c2f9e9ba8b8391fad3d0db84cbe9f3d003")
413    alice = sp.test_account("Alice")
414    signature = sp.make_signature(
415        secret_key=alice.secret_key, message=sp.bytes("0x00"), message_format="Raw"
416    )
417    # main_.test_mprim3(sp.record(chest_key=chest_key, key=alice.public_key, signature=signature))
418    main_.test_mprim3(sp.record(key=alice.public_key, signature=signature))
419
420    main_.test_expr_other()