Пример #1
0
    def test_timestamp(self):
        contract_a = make_contract("", "uint256")
        vm = create_evm_vm([contract_a], False, False)

        arbsys = contract_templates.get_arbsys()
        arbsys_abi = ContractABI(arbsys)

        arbinfo = contract_templates.get_info_contract()
        arbinfo_abi = ContractABI(arbinfo)
        output_handler = create_output_handler([contract_a, arbinfo_abi])
        inbox = value.Tuple([])
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple(
                        [0, address,
                         arbsys_abi.timestampUpperBound(0, 0)]), 0)),
        )
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple(
                        [0, address,
                         arbsys_abi.currentMessageTimestamp(1, 0)]),
                    0,
                    543,
                )),
        )
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple([
                        CALL_TX_TYPE, address,
                        arbsys_abi.call_timestampUpperBound()
                    ]),
                    34,
                )),
        )

        vm.env.messages = inbox
        run_until_block(vm, self)
        self.assertEqual(len(vm.logs), 3)
        parsed_out0 = output_handler(vm.logs[0])
        parsed_out1 = output_handler(vm.logs[1])
        parsed_out2 = output_handler(vm.logs[2])

        self.assertIsInstance(parsed_out0, EVMReturn)
        self.assertIsInstance(parsed_out1, EVMReturn)
        self.assertIsInstance(parsed_out2, EVMReturn)

        self.assertEqual(parsed_out0.output_values[0], 73657336)
        self.assertEqual(parsed_out1.output_values[0], 543)
        self.assertEqual(parsed_out2.output_values[0], 73657336)
Пример #2
0
    def test_eth(self):
        contract_a = make_contract("", "uint256")
        vm = create_evm_vm([contract_a], False, False)

        arbsys = contract_templates.get_arbsys()
        arbsys_abi = ContractABI(arbsys)

        arbinfo = contract_templates.get_info_contract()
        arbinfo_abi = ContractABI(arbinfo)
        output_handler = create_output_handler([contract_a, arbinfo_abi])
        inbox = value.Tuple([])
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple(
                        [1, 2345, value.Tuple([address, 100000])]
                    )  # type  # sender
                )
            ),
        )
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple(
                        [
                            CALL_TX_TYPE,
                            address,
                            arbinfo_abi.call_getBalance(address_string),
                        ]
                    )  # type  # sender
                )
            ),
        )
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple(
                        [
                            0,
                            address,
                            arbsys_abi.withdrawEth(0, 0, dest_address_string, 150000),
                        ]
                    )  # type  # sender
                )
            ),
        )
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple(
                        [
                            0,
                            address,
                            arbsys_abi.withdrawEth(1, 0, dest_address_string, 50000),
                        ]
                    )  # type  # sender
                )
            ),
        )
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple(
                        [
                            CALL_TX_TYPE,
                            address,
                            arbinfo_abi.call_getBalance(address_string),
                        ]
                    )  # type  # sender
                )
            ),
        )

        vm.env.messages = inbox
        run_until_block(vm, self)
        self.assertEqual(len(vm.logs), 5)
        parsed_out0 = output_handler(vm.logs[0])
        parsed_out1 = output_handler(vm.logs[1])
        parsed_out2 = output_handler(vm.logs[2])
        parsed_out3 = output_handler(vm.logs[3])
        parsed_out4 = output_handler(vm.logs[4])

        self.assertIsInstance(parsed_out0, EVMStop)
        self.assertIsInstance(parsed_out1, EVMReturn)
        self.assertIsInstance(parsed_out2, EVMRevert)
        self.assertIsInstance(parsed_out3, EVMStop)
        self.assertIsInstance(parsed_out4, EVMReturn)

        self.assertEqual(parsed_out1.output_values[0], 100000)
        self.assertEqual(parsed_out4.output_values[0], 50000)

        self.assertEqual(len(vm.sent_messages), 1)
        self.assertEqual(
            vm.sent_messages[0],
            value.Tuple([1, address, value.Tuple([dest_address, 50000])]),
        )
Пример #3
0
    def test_transfer_eth_from_contract(self):
        contract_address_string = "0x0b55929f4095f677C9Ec1F4810C3E59CCD6D33C7"
        contract_address = eth_utils.to_int(hexstr=contract_address_string)
        evm_code = make_evm_transfer_code(dest_address_string, 2000)
        contract_a = make_contract(evm_code, "address",
                                   contract_address_string)

        arbinfo = contract_templates.get_info_contract()
        arbinfo_abi = ContractABI(arbinfo)

        vm = create_evm_vm([contract_a], False, False)
        output_handler = create_output_handler([contract_a, arbinfo_abi])
        inbox = value.Tuple([])
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple(
                        [1, 2345,
                         value.Tuple([contract_address,
                                      2000])])  # type  # sender
                )),
        )
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple([0, address,
                                 contract_a.testMethod(0,
                                                       0)])  # type  # sender
                )),
        )
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple([
                        CALL_TX_TYPE,
                        address,
                        arbinfo_abi.call_getBalance(dest_address_string),
                    ])  # type  # sender
                )),
        )
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple([
                        CALL_TX_TYPE,
                        address,
                        arbinfo_abi.call_getBalance(contract_address_string),
                    ])  # type  # sender
                )),
        )
        vm.env.messages = inbox
        run_until_block(vm, self)
        self.assertEqual(len(vm.logs), 4)
        parsed_outs = [output_handler(log) for log in vm.logs]
        self.assertIsInstance(parsed_outs[0], EVMStop)
        self.assertIsInstance(parsed_outs[1], EVMReturn)
        self.assertIsInstance(parsed_outs[2], EVMReturn)
        self.assertIsInstance(parsed_outs[3], EVMReturn)
        self.assertEqual(
            eth_utils.to_int(hexstr=parsed_outs[1].output_values[0]), 1)
        dest_balance = parsed_outs[2].output_values[0]
        contract_balance = parsed_outs[3].output_values[0]
        self.assertEqual(dest_balance, 2000)
        self.assertEqual(contract_balance, 0)
Пример #4
0
    def test_insuffient_balance(self):
        contract_a = make_contract("", "uint256")
        vm = create_evm_vm([contract_a], False, False)
        arbinfo = contract_templates.get_info_contract()
        arbinfo_abi = ContractABI(arbinfo)
        output_handler = create_output_handler([contract_a, arbinfo_abi])
        inbox = value.Tuple([])
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple([1, 2345,
                                 value.Tuple([address,
                                              5000])])  # type  # sender
                )),
        )
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple([
                        CALL_TX_TYPE,
                        address,
                        arbinfo_abi.call_getBalance(address_string),
                    ])  # type  # sender
                )),
        )
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple([
                        CALL_TX_TYPE,
                        address,
                        arbinfo_abi.call_getBalance(dest_address_string),
                    ])  # type  # sender
                )),
        )
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple([
                        0,
                        address,
                        value.Tuple(
                            [dest_address, 0, 10000,
                             bytestack_frombytes(b"")]),
                    ])  # type  # sender
                )),
        )
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple([
                        CALL_TX_TYPE,
                        address,
                        arbinfo_abi.call_getBalance(address_string),
                    ])  # type  # sender
                )),
        )
        inbox = messagestack.addMessage(
            inbox,
            value.Tuple(
                make_msg_val(
                    value.Tuple([
                        CALL_TX_TYPE,
                        address,
                        arbinfo_abi.call_getBalance(dest_address_string),
                    ])  # type  # sender
                )),
        )

        vm.env.messages = inbox
        run_until_block(vm, self)
        self.assertEqual(len(vm.logs), 6)
        parsed_outs = [output_handler(log) for log in vm.logs]

        self.assertIsInstance(parsed_outs[0], EVMStop)
        self.assertIsInstance(parsed_outs[1], EVMReturn)
        self.assertIsInstance(parsed_outs[2], EVMReturn)
        self.assertIsInstance(parsed_outs[3], EVMRevert)
        self.assertIsInstance(parsed_outs[4], EVMReturn)
        self.assertIsInstance(parsed_outs[5], EVMReturn)
        self.assertEqual(len(vm.sent_messages), 0)

        self.assertEqual(parsed_outs[1].output_values[0],
                         parsed_outs[4].output_values[0])
        self.assertEqual(parsed_outs[2].output_values[0],
                         parsed_outs[5].output_values[0])