Exemple #1
0
 def test_push_preserves_name(self):
     i = Interpreter()
     i.execute('PUSH @a int 1 ; PUSH @b int 2 ; SWAP')
     fst = i.ctx.stack[0]  # type: StackItem
     sec = i.ctx.stack[1]  # type: StackItem
     self.assertEqual("a", fst.name)
     self.assertEqual("b", sec.name)
Exemple #2
0
class OpcodeTestbig_map_magic_14(TestCase):
    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)

    def test_opcode_big_map_magic_14(self):
        res = self.i.execute(
            f'INCLUDE "{abspath("opcodes/contracts/big_map_magic.tz")}"')
        self.assertTrue(res['success'])

        res = self.i.execute(
            'RUN (Right (Right (Right (Right { "1" })))) (Left (Pair { Elt "1" "one" } { Elt "2" "two" }))'
        )
        self.assertTrue(res['success'])

        exp_val_expr = michelson_to_micheline('(Left (Pair 0 1))')
        exp_val = parse_expression(exp_val_expr,
                                   res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)

        big_map_diff = [{
            'action': 'alloc',
            'big_map': '1',
            'key_type': {
                'prim': 'string'
            },
            'value_type': {
                'prim': 'string'
            }
        }, {
            'action': 'update',
            'big_map': '1',
            'key_hash':
            'exprtaPfHX9A3HW7vujsuarwHDXwJYu9hJZvuqUCyoeHLRLPXyDQjW',
            'key': {
                'string': '2'
            },
            'value': {
                'string': 'two'
            }
        }, {
            'action': 'alloc',
            'big_map': '0',
            'key_type': {
                'prim': 'string'
            },
            'value_type': {
                'prim': 'string'
            }
        }, {
            'action': 'update',
            'big_map': '0',
            'key_hash':
            'exprvHK69NiDmfXXw2Gx5x9meBfRp3CBMZ1QjM3UdqoNfUzK3nSpnL',
            'key': {
                'string': '1'
            }
        }]
        self.assertCountEqual(big_map_diff, res['result']['big_map_diff'])
Exemple #3
0
    def interpret(self,
                  storage,
                  source=None,
                  sender=None,
                  amount=None,
                  balance=None,
                  chain_id=None,
                  now=None):
        """ Run code in the builtin REPL (WARNING! Not recommended for critical tasks).

        :param storage: Python object
        :param source: patch SOURCE
        :param sender: patch SENDER
        :param amount: patch AMOUNT
        :param balance: patch BALANCE
        :param chain_id: patch CHAIN_ID
        :param now: patch NOW
        :rtype: ContractCallResult
        """
        i = Interpreter()
        i.execute(self.contract.text)

        if source is None:
            source = self.key.public_key_hash()
        if sender is None:
            sender = source
        if amount is None:
            amount = 0
        if balance is None:
            balance = 0

        patch_map = {
            'SOURCE': source,
            'SENDER': sender,
            'AMOUNT': amount,
            'BALANCE': balance,
            'CHAIN_ID': chain_id,
            'NOW': now
        }
        for instr, value in patch_map.items():
            if value is not None:
                value = f'"{value}"' if isinstance(value, str) else value
                i.execute(f'PATCH {instr} {value}')

        s_expr = micheline_to_michelson(self.contract.storage.encode(storage),
                                        inline=True,
                                        wrap=True)
        p_expr = micheline_to_michelson(self.parameters['value'],
                                        inline=True,
                                        wrap=True)
        res = i.execute(
            f'RUN %{self.parameters["entrypoint"]} {p_expr} {s_expr}')

        return ContractCallResult.from_repl_result(res,
                                                   parameters=self.parameters,
                                                   contract=self.contract)
Exemple #4
0
class OpcodeTestassert_cmpneq_21(TestCase):
    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=False)  # disable exceptions

    def test_opcode_assert_cmpneq_21(self):
        res = self.i.execute(
            f'INCLUDE "{abspath("opcodes/contracts/assert_cmpneq.tz")}"')
        self.assertTrue(res['success'])

        res = self.i.execute('RUN (Pair -1 -1) Unit')
        self.assertEqual(False, res['success'])
Exemple #5
0
class OpcodeTestslices_4(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=False)  # disable exceptions

    def test_opcode_slices_4(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/slices.tz")}"')
        self.assertTrue(res['success'])

        res = self.i.execute('RUN (Pair 0xe009ab79e8b84ef0 "spsig1PPUFZucuAQybs5wsqsNQ68QNgFaBnVKMFaoZZfi1BtNnuCAWnmL9wVy5HfHkR6AeodjVGxpBVVSYcJKyMURn6K1yknYLm") "sppk7dBPqMPjDjXgKbb5f7V3PuKUrA4Zuwc3c3H7XqQerqPUWbK7Hna"')
        self.assertEqual(False, res['success'])
Exemple #6
0
class OpcodeTestmul_overflow_8(TestCase):
    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=False)  # disable exceptions

    def test_opcode_mul_overflow_8(self):
        res = self.i.execute(
            f'INCLUDE "{abspath("opcodes/contracts/mul_overflow.tz")}"')
        self.assertTrue(res['success'])

        res = self.i.execute('RUN (Left Unit) Unit')
        self.assertEqual(False, res['success'])
class OpcodeTestcheck_signature_3(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=False)  # disable exceptions

    def test_opcode_check_signature_3(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/check_signature.tz")}"')
        self.assertTrue(res['success'])

        res = self.i.execute('RUN "edpkuBknW28nW72KG6RoHtYW7p12T6GKc7nAbwYX5m8Wd9sDVC9yav" (Pair "edsigu3QszDjUpeqYqbvhyRxMpVFamEnvm9FYnt7YiiNt9nmjYfh8ZTbsybZ5WnBkhA7zfHsRVyuTnRsGLR6fNHt1Up1FxgyRtF" "abcd")')
        self.assertEqual(False, res['success'])
class OpcodeTestshifts_7(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=False)  # disable exceptions

    def test_opcode_shifts_7(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/shifts.tz")}"')
        self.assertTrue(res['success'])

        res = self.i.execute('RUN (Right (Pair 123 257)) None')
        self.assertEqual(False, res['success'])
class OpcodeTestpackunpack_1(TestCase):
    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=False)  # disable exceptions

    def test_opcode_packunpack_1(self):
        res = self.i.execute(
            f'INCLUDE "{abspath("opcodes/contracts/packunpack.tz")}"')
        self.assertTrue(res['success'])

        res = self.i.execute(
            'RUN (Pair (Pair (Pair "toto" {3;7;9;1}) {1;2;3}) 0x05070707070100000004746f746f0200000008000300070009000102000000060001000200030004) Unit'
        )
        self.assertEqual(False, res['success'])
Exemple #10
0
class OpcodeTestslices_3(TestCase):
    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=False)  # disable exceptions

    def test_opcode_slices_3(self):
        res = self.i.execute(
            f'INCLUDE "{abspath("opcodes/contracts/slices.tz")}"')
        self.assertTrue(res['success'])

        res = self.i.execute(
            'RUN (Pair 0xe009ab79e8b84ef0e55c43a9a857214d8761e67b75ba63500a5694fb2ffe174acc2de22d01ccb7259342437f05e1987949f0ad82e9f32e9a0b79cb252d7f7b8236ad728893f4e7150733eefdbeda254970f9fcd92c6228c178e1a923e5600758eb83f2a05edd0be7625657901f2ba81eaf145d003dbef78e33f43a32a3788bdf0501000000085341554349535345 "spsig1PPUFZucuAQybs5wsqsNQ68QNgFaBnVKMFaoZZfi1BtNnuCAWnmL9wVy5HfHkR6AeodjVGxpBVVSYcJKyMURn6K1yknYLm") "sppk7dBPqMPjDjXgKbb5f7V3PuKUrA4Zuwc3c3H7XqQerqPUWbK7Hna"'
        )
        self.assertEqual(False, res['success'])
class OpcodeTestreverse_loop_179(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_reverse_loop_179(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/reverse_loop.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN {} {""}')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('{}')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
Exemple #12
0
class OpcodeTestdipn_250(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_dipn_250(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/dipn.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN (Pair (Pair (Pair (Pair 1 2) 3) 4) 5) 0')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('6')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
Exemple #13
0
class OpcodeTestlist_map_block_6(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_list_map_block_6(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/list_map_block.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN { 1 ; 1 ; 1 ; 1 } {0}')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('{ 1 ; 2 ; 3 ; 4 }')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
Exemple #14
0
class OpcodeTestxor_69(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_xor_69(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/xor.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN (Right (Pair 1 0)) None')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('(Some (Right 1))')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
class OpcodeTestadd_delta_timestamp_213(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_add_delta_timestamp_213(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/add_delta_timestamp.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN (Pair -100 100) None')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('(Some "1970-01-01T00:00:00Z")')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
class OpcodeTestset_size_152(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_set_size_152(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/set_size.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN { 1 ; 2 ; 3 ; 4 ; 5 ; 6 } 111')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('6')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
Exemple #17
0
class OpcodeTestself_with_default_entrypoint_257(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_self_with_default_entrypoint_257(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/self_with_default_entrypoint.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN Unit Unit')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('Unit')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
Exemple #18
0
class OpcodeTestmacro_compare_271(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_macro_compare_271(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/macro_compare.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN (Pair 2370000 2370000) {}')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('{ True ; False ; False ; True ; True }')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
class OpcodeTestcontract_239(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_contract_239(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/contract.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN "tz1cxcwwnzENRdhe2Kb8ZdTrdNy4bFNyScx5" Unit')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('Unit')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
Exemple #20
0
class OpcodeTestmap_mem_nat_102(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_map_mem_nat_102(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/map_mem_nat.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN 2 (Pair { Elt 1 4 ; Elt 2 11 } None)')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('(Pair { Elt 1 4 ; Elt 2 11 } (Some True))')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
class OpcodeTestassert_cmple_285(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_assert_cmple_285(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/assert_cmple.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN (Pair -1 0) Unit')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('Unit')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
class OpcodeTestset_car_203(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_set_car_203(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/set_car.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN "" (Pair "hello" 0)')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('(Pair "" 0)')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
Exemple #23
0
class OpcodeTestbig_map_mem_string_120(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_big_map_mem_string_120(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/big_map_mem_string.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN "bar" (Pair {} None)')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('(Pair 0 (Some False))')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
class OpcodeTestmap_caddaadr_291(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_map_caddaadr_291(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/map_caddaadr.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN Unit (Pair (Pair 1 (Pair 2 (Pair (Pair (Pair 3 0) 4) 5))) 6)')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('(Pair (Pair 1 (Pair 2 (Pair (Pair (Pair 3 1000000) 4) 5))) 6)')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
class OpcodeTestnot_binary_57(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_not_binary_57(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/not_binary.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN (Left 7) None')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('(Some -8)')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
class OpcodeTestleft_right_177(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_left_right_177(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/left_right.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN (Left True) (Left "X")')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('(Right True)')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
Exemple #27
0
class OpcodeTestmax_in_list_268(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_max_in_list_268(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/max_in_list.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN { -10 ; -1 ; -20 ; -100 } None')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('(Some -1)')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
class OpcodeTestconcat_hello_168(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_concat_hello_168(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/concat_hello.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN { "test1" ; "test2" } {}')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('{ "Hello test1" ; "Hello test2" }')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
Exemple #29
0
class OpcodeTestlist_concat_134(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_list_concat_134(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/list_concat.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN { "d" ; "e" ; "f" } "abc"')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('"abcdef"')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)
class OpcodeTestdig_eq_251(TestCase):

    def setUp(self):
        self.maxDiff = None
        self.i = Interpreter(debug=True)
        
    def test_opcode_dig_eq_251(self):
        res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/dig_eq.tz")}"')
        self.assertTrue(res['success'])
        
        res = self.i.execute('RUN (Pair 17 (Pair 16 (Pair 15 (Pair 14 (Pair 13 (Pair 12 (Pair 11 (Pair 10 (Pair 9 (Pair 8 (Pair 7 (Pair 6 (Pair 5 (Pair 4 (Pair 3 (Pair 2 1)))))))))))))))) Unit')
        self.assertTrue(res['success'])
        
        exp_val_expr = michelson_to_micheline('Unit')
        exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
        self.assertEqual(exp_val, res['result']['storage']._val)