Example #1
0
 def test_entrypoints_to_tree(self):
     contract = Contract(
         storage=10,
         storage_type=t.Int(),
         entrypoints={
             "add":
             Entrypoint(
                 FunctionPrototype(t.Int(), t.Int()),
                 [
                     Instr("PUSH", [t.Int(), 1], {}),
                 ],
             ),
             "sub":
             Entrypoint(
                 FunctionPrototype(t.Int(), t.Int()),
                 [
                     Instr("PUSH", [t.Int(), 2], {}),
                 ],
             ),
             "div":
             Entrypoint(
                 FunctionPrototype(t.Int(), t.Int()),
                 [
                     Instr("PUSH", [t.Int(), 3], {}),
                 ],
             ),
         },
         instructions=[],
     )
     entrypoints = [
         contract.entrypoints[name]
         for name in sorted(contract.entrypoints.keys())
     ]
     entrypoint_tree = EntrypointTree()
     contract_body = entrypoint_tree.list_to_tree(entrypoints)
     expected_result = Instr(
         "IF_LEFT",
         [
             [
                 Instr(
                     "IF_LEFT",
                     [
                         contract.entrypoints["add"].instructions,
                         contract.entrypoints["div"].instructions,
                     ],
                     {},
                 )
             ],
             contract.entrypoints["sub"].instructions,
         ],
         {},
     )
     self.assertEqual(contract_body, expected_result)
Example #2
0
 def test_compile_param(self):
     b = CompilerBackend()
     compiled_parameter = b.compile_type(Or(t.Int(), t.Int()))
     expected_parameter = {
         "prim": "or",
         "args": [
             {
                 "prim": "int",
             },
             {
                 "prim": "int",
             },
         ],
     }
     self.assertEqual(compiled_parameter, expected_parameter)
Example #3
0
 def test_make_contract_param(self):
     contract = Contract(
         storage=10,
         storage_type=t.Int(),
         entrypoints={
             "add": Entrypoint(
                 FunctionPrototype(t.Int(), t.Int()),
                 [],
             ),
             "sub": Entrypoint(
                 FunctionPrototype(t.Int(), t.Int()),
                 [],
             ),
             "div": Entrypoint(
                 FunctionPrototype(t.Int(), t.Int()),
                 [],
             ),
         },
         instructions=[],
     )
     self.assertEqual(
         Left(Left(1)),
         contract.make_contract_param("add", 1),
     )
     self.assertEqual(
         Left(Right(1)),
         contract.make_contract_param("div", 1),
     )
     self.assertEqual(
         Right(1),
         contract.make_contract_param("sub", 1),
     )
Example #4
0
 def test_get_contract_body(self):
     contract = Contract(
         storage=10,
         storage_type=t.Int(),
         entrypoints={
             "add":
             Entrypoint(
                 FunctionPrototype(t.Int(), t.Int()),
                 [
                     Instr("PUSH", [t.Int(), 1], {}),
                 ],
             ),
             "sub":
             Entrypoint(
                 FunctionPrototype(t.Int(), t.Int()),
                 [
                     Instr("PUSH", [t.Int(), 2], {}),
                 ],
             ),
             "div":
             Entrypoint(
                 FunctionPrototype(t.Int(), t.Int()),
                 [
                     Instr("PUSH", [t.Int(), 3], {}),
                 ],
             ),
         },
         instructions=[],
     )
     contract_body = contract.get_contract_body()
     sorted_entrypoints = [
         contract.entrypoints[name]
         for name in sorted(contract.entrypoints.keys())
     ]
     entrypoint_tree = EntrypointTree()
     expected_result = entrypoint_tree.list_to_tree(sorted_entrypoints)
     self.assertEqual(contract_body, expected_result)
Example #5
0
 def test_contract_parameter_type(self):
     contract = Contract(
         storage=10,
         storage_type=t.Int(),
         entrypoints={
             "add": Entrypoint(
                 FunctionPrototype(t.Int(), t.Int()),
                 [],
             ),
             "sub": Entrypoint(
                 FunctionPrototype(t.Int(), t.Int()),
                 [],
             ),
             "div": Entrypoint(
                 FunctionPrototype(t.Int(), t.Int()),
                 [],
             ),
         },
         instructions=[],
     )
     self.assertEqual(
         Or(Or(t.Int(), t.Int()), t.Int()),
         contract.get_parameter_type(),
     )