예제 #1
0
    def pre_alloc(self, val_expr, type_expr, copy=False, network=None):
        def alloc_selector(val_node, type_node, res, type_path):
            prim = type_node['prim']
            if prim in ['list', 'set']:
                return res
            if prim in ['pair', 'or']:
                return {'prim': val_node['prim'], 'args': res}
            elif prim == 'option' and val_node['prim'] == 'Some':
                return {'prim': val_node['prim'], 'args': res}
            elif prim == 'map':
                return list(map(make_elt, res))
            elif prim == 'big_map':
                if isinstance(val_node, list):
                    return self._pre_alloc(val_node, type_node)
                elif copy:
                    return self._pre_copy(val_node, type_node, network=network)
                else:
                    return self._pre_remove(val_node,
                                            type_node,
                                            network=network)

            return val_node

        val_expr = parse_expression(val_expr, type_expr, alloc_selector)
        return StackItem.parse(val_expr=val_expr, type_expr=type_expr)
예제 #2
0
    def test_opcode_get_big_map_value_0(self):
        res = self.i.execute(
            f'INCLUDE "{abspath("opcodes/contracts/get_big_map_value.tz")}"')
        self.assertTrue(res['success'])

        res = self.i.execute('RUN "hello" (Pair { Elt "hello" "hi" } None)')
        self.assertTrue(res['success'])

        exp_val_expr = michelson_to_micheline('(Pair 0 (Some "hi"))')
        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': '0',
            'key_type': {
                'prim': 'string'
            },
            'value_type': {
                'prim': 'string'
            }
        }, {
            'action': 'update',
            'big_map': '0',
            'key_hash':
            'exprtsjEVVZk3Gm82U9wEs8kvwRiQwUT7zipJwvCeFMNsApe2tQ15s',
            'key': {
                'string': 'hello'
            },
            'value': {
                'string': 'hi'
            }
        }]
        self.assertCountEqual(big_map_diff, res['result']['big_map_diff'])
예제 #3
0
def parse_micheline(val_expr, type_expr, schema: Schema, bin_root='0'):
    def flatten_pair(args) -> Pair:
        res = list()
        for arg in args:
            if isinstance(arg, Pair):
                res.extend(flatten_pair(arg))
            else:
                res.append(arg)
        return Pair(res)

    def decode_selector(val_node, type_node, val, type_path):
        bin_type = schema.bin_types[type_path]
        if bin_type == 'map':
            return dict(val)
        elif bin_type == 'big_map':
            return dict(val) if isinstance(val_node, list) else val
        elif bin_type == 'option':
            return val[0] if val is not None else None
        elif bin_type == 'pair':
            return flatten_pair(val)
        elif bin_type == 'tuple':
            return list(flatten_pair(val))
        elif bin_type == 'keypair':
            return tuple(flatten_pair(val))
        elif bin_type == 'namedtuple':
            names = list(
                map(lambda x: schema.bin_names[x],
                    schema.metadata[type_path]['args']))
            return dict(zip(names, flatten_pair(val)))
        elif bin_type in ['or', 'router', 'enum']:
            arg_path = type_path + {
                'Left': '0',
                'Right': '1'
            }[val_node['prim']]
            if schema.bin_types[arg_path] == 'option':
                arg_path += '0'
            is_leaf = schema.metadata[arg_path]['prim'] != 'or'
            res = {schema.bin_names[arg_path]: val[0]} if is_leaf else val[0]
            return next(iter(res)) if bin_type == 'enum' else res
        elif bin_type == 'unit':
            return None
        elif bin_type == 'lambda':
            return micheline_to_michelson(val)
        elif bin_type == 'timestamp':
            return dispatch_core_map(val_node, {'string': str, 'int': int})
        elif bin_type == 'bytes':
            return val.hex()
        elif bin_type == 'mutez':
            return Decimal(val) / 10**6
        else:
            return val

    if type_expr['prim'] in ['storage', 'parameter']:
        type_expr = type_expr['args'][0]

    for idx in bin_root[1:]:
        type_expr = type_expr['args'][int(idx)]

    return parse_expression(val_expr, type_expr, decode_selector, bin_root)
예제 #4
0
    def test_opcode_update_big_map_5(self):
        res = self.i.execute(
            f'INCLUDE "{abspath("opcodes/contracts/update_big_map.tz")}"')
        self.assertTrue(res['success'])

        res = self.i.execute(
            'RUN { Elt "3" (Some "three") } (Pair { Elt "1" "one" ; Elt "2" "two" } Unit)'
        )
        self.assertTrue(res['success'])

        exp_val_expr = michelson_to_micheline('(Pair 0 Unit)')
        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': '0',
            'key_type': {
                'prim': 'string'
            },
            'value_type': {
                'prim': 'string'
            }
        }, {
            'action': 'update',
            'big_map': '0',
            'key_hash':
            'exprvHK69NiDmfXXw2Gx5x9meBfRp3CBMZ1QjM3UdqoNfUzK3nSpnL',
            'key': {
                'string': '1'
            },
            'value': {
                'string': 'one'
            }
        }, {
            'action': 'update',
            'big_map': '0',
            'key_hash':
            'exprtaPfHX9A3HW7vujsuarwHDXwJYu9hJZvuqUCyoeHLRLPXyDQjW',
            'key': {
                'string': '2'
            },
            'value': {
                'string': 'two'
            }
        }, {
            'action': 'update',
            'big_map': '0',
            'key_hash':
            'exprtzN2y9qBiaf7A14AbS1SveJWXpMdJQztXFgiGzG5yu43tem2he',
            'key': {
                'string': '3'
            },
            'value': {
                'string': 'three'
            }
        }]
        self.assertCountEqual(big_map_diff, res['result']['big_map_diff'])
예제 #5
0
 def __init__(self, val=False, val_expr=None, type_expr=None, **kwargs):
     assert_type(val, bool)
     super(Bool,
           self).__init__(val=val if val_expr is None else parse_expression(
               val_expr, type_expr),
                          val_expr=val_expr or {'prim': str(val)},
                          type_expr=type_expr or {'prim': self.prim},
                          **kwargs)
예제 #6
0
 def __init__(self, val=b'', type_expr=None, val_expr=None, **kwargs):
     assert_type(val, bytes)
     super(Bytes,
           self).__init__(val=val if val_expr is None else parse_expression(
               val_expr, type_expr),
                          val_expr=val_expr or {'bytes': val.hex()},
                          type_expr=type_expr or {'prim': self.prim},
                          **kwargs)
예제 #7
0
 def __init__(self, val='', val_expr=None, type_expr=None, **kwargs):
     assert isinstance(val, str)
     super(String,
           self).__init__(val=val if val_expr is None else parse_expression(
               val_expr, type_expr),
                          val_expr=val_expr or {'string': val},
                          type_expr=type_expr or {'prim': self.prim},
                          **kwargs)
예제 #8
0
 def __init__(self, val=0, val_expr=None, type_expr=None, **kwargs):
     assert_type(val, int)
     assert isinstance(val, int)
     super(Int,
           self).__init__(val=val if val_expr is None else parse_expression(
               val_expr, type_expr),
                          val_expr=val_expr or {'int': str(val)},
                          type_expr=type_expr or {'prim': self.prim},
                          **kwargs)
예제 #9
0
    def test_opcode_big_map_magic_10(self):
        res = self.i.execute(
            f'INCLUDE "{abspath("opcodes/contracts/big_map_magic.tz")}"')
        self.assertTrue(res['success'])

        res = self.i.execute(
            'RUN (Right (Left (Left (Pair { Elt "3" "three" } { Elt "4" "four" })))) (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':
            'exprttiwE7dpYJ8Xjp28uMZzcC3Bwh4xAEF7GT7FutVwVoskMZYExx',
            'key': {
                'string': '4'
            },
            'value': {
                'string': 'four'
            }
        }, {
            'action': 'alloc',
            'big_map': '0',
            'key_type': {
                'prim': 'string'
            },
            'value_type': {
                'prim': 'string'
            }
        }, {
            'action': 'update',
            'big_map': '0',
            'key_hash':
            'exprtzN2y9qBiaf7A14AbS1SveJWXpMdJQztXFgiGzG5yu43tem2he',
            'key': {
                'string': '3'
            },
            'value': {
                'string': 'three'
            }
        }]
        self.assertCountEqual(big_map_diff, res['result']['big_map_diff'])
예제 #10
0
    def test_opcode_big_map_magic_12(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 (Left (Pair { Pair "foo" "bar" } { Pair "gaz" "baz" }) ))) (Right Unit)'
        )
        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':
            'exprtft4mfpxnyufwZf17PQxf57VAyrwNM5mNwqCMkVRTfb5pALQpz',
            'key': {
                'string': 'gaz'
            },
            'value': {
                'string': 'baz'
            }
        }, {
            'action': 'alloc',
            'big_map': '0',
            'key_type': {
                'prim': 'string'
            },
            'value_type': {
                'prim': 'string'
            }
        }, {
            'action': 'update',
            'big_map': '0',
            'key_hash':
            'expruTFUPVsqkuD5iwLMJuzoyGSFABnxLo7CZrgnS1czt1WbTwpVrJ',
            'key': {
                'string': 'foo'
            },
            'value': {
                'string': 'bar'
            }
        }]
        self.assertCountEqual(big_map_diff, res['result']['big_map_diff'])
예제 #11
0
 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)
예제 #12
0
 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)
예제 #13
0
 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)
예제 #14
0
 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)
예제 #15
0
 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)
예제 #16
0
 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)
예제 #17
0
 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)
예제 #18
0
 def test_opcode_or_binary_52(self):
     res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/or_binary.tz")}"')
     self.assertTrue(res['success'])
     
     res = self.i.execute('RUN (Pair 15 4) None')
     self.assertTrue(res['success'])
     
     exp_val_expr = michelson_to_micheline('(Some 15)')
     exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
     self.assertEqual(exp_val, res['result']['storage']._val)
예제 #19
0
 def test_opcode_list_size_143(self):
     res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/list_size.tz")}"')
     self.assertTrue(res['success'])
     
     res = self.i.execute('RUN { 1 } 111')
     self.assertTrue(res['success'])
     
     exp_val_expr = michelson_to_micheline('1')
     exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
     self.assertEqual(exp_val, res['result']['storage']._val)
예제 #20
0
 def test_opcode_abs_187(self):
     res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/abs.tz")}"')
     self.assertTrue(res['success'])
     
     res = self.i.execute('RUN 12039123919239192312931 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)
예제 #21
0
 def test_opcode_map_mem_nat_100(self):
     res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/map_mem_nat.tz")}"')
     self.assertTrue(res['success'])
     
     res = self.i.execute('RUN 1 (Pair { Elt 1 0 } None)')
     self.assertTrue(res['success'])
     
     exp_val_expr = michelson_to_micheline('(Pair { Elt 1 0 } (Some True))')
     exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
     self.assertEqual(exp_val, res['result']['storage']._val)
예제 #22
0
 def test_opcode_contains_all_160(self):
     res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/contains_all.tz")}"')
     self.assertTrue(res['success'])
     
     res = self.i.execute('RUN (Pair {} {}) None')
     self.assertTrue(res['success'])
     
     exp_val_expr = michelson_to_micheline('(Some True)')
     exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
     self.assertEqual(exp_val, res['result']['storage']._val)
 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)
예제 #24
0
 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)
예제 #25
0
 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)
예제 #26
0
 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)
예제 #27
0
 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)
예제 #28
0
 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)
예제 #29
0
 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)
예제 #30
0
 def test_opcode_and_logical_1_46(self):
     res = self.i.execute(f'INCLUDE "{abspath("opcodes/contracts/and_logical_1.tz")}"')
     self.assertTrue(res['success'])
     
     res = self.i.execute('RUN (Pair True False) False')
     self.assertTrue(res['success'])
     
     exp_val_expr = michelson_to_micheline('False')
     exp_val = parse_expression(exp_val_expr, res['result']['storage'].type_expr)
     self.assertEqual(exp_val, res['result']['storage']._val)