def test_daemon_brandnew(self):
        self._callback_event = multiprocessing.Event()
        self._deployed_event = multiprocessing.Event()
        p = multiprocessing.Process(target=self.run_node_client_daemon)
        p.start()

        self._deployed_event.wait()
        try:
            self.daemon_brandnew_test_logic()
        finally:
            p.terminate()
            p.join()
        get_all_fee_reports()
示例#2
0
    def test_basic_test(self):
        myWeb3 = MyWeb3(_TEST_CONFIG)
        other_user = myWeb3.get_accounts()[3]

        hodl_saver = HodlSaver(_TEST_CONFIG)
        all_events = hodl_saver.get_all_events()
        deposit_balance_event_hdr = all_events.DepositBalance.createFilter(
            fromBlock='latest')
        withdraw_balance_event_hdr = all_events.WithdrawBalance.createFilter(
            fromBlock='latest')
        PAYMENT_VALUE = 1000

        now_balance = hodl_saver.get_balance()
        hodl_saver.deposit_balance(
            100, **{
                'value': convert_to_wei(PAYMENT_VALUE, 'wei'),
                'from': other_user
            })
        deposit_balance_event = deposit_balance_event_hdr.get_new_entries()
        self.check_event_deposit_balance(
            other_user, 100, convert_to_wei(PAYMENT_VALUE, 'wei'),
            now_balance + convert_to_wei(PAYMENT_VALUE, 'wei'),
            deposit_balance_event)
        now_balance = hodl_saver.get_balance()

        hodl_saver.deposit_balance(
            200, **{
                'value': convert_to_wei(50000, 'wei'),
                'from': other_user
            })
        deposit_balance_event = deposit_balance_event_hdr.get_new_entries()
        self.check_event_deposit_balance(
            other_user, 200, convert_to_wei(50000, 'wei'),
            now_balance + convert_to_wei(50000, 'wei'), deposit_balance_event)
        now_balance = hodl_saver.get_balance()

        hodl_saver.withdraw_balance(199, **{'from': self._hodl_owner})
        withdraw_balance_event = withdraw_balance_event_hdr.get_new_entries()
        self.check_event_withdraw_balance(other_user, 200, 199, False,
                                          withdraw_balance_event)
        self.assertEqual(now_balance, hodl_saver.get_balance(),
                         'balance should be the same')

        hodl_saver.withdraw_balance(201, **{'from': self._hodl_owner})
        withdraw_balance_event = withdraw_balance_event_hdr.get_new_entries()
        self.check_event_withdraw_balance(other_user, 200, 201, True,
                                          withdraw_balance_event)
        self.assertEqual(0, hodl_saver.get_balance(),
                         'balance should be the same')
        get_all_fee_reports()
示例#3
0
    def test_single_event(self):
        myWeb3 = MyWeb3(_TEST_CONFIG)
        hodl_oracle = HodlOracle(_TEST_CONFIG)
        PAYMENT_VALUE = 10000
        oracle_wallet = OracleWallet(_TEST_CONFIG)
        before_wallet_balance = oracle_wallet.get_balance()
        hodl_oracle.deposit(**{
            'value': convert_to_wei(PAYMENT_VALUE, 'wei'),
            'from': self._hodl_owner
        })
        hodl_oracle.trigger(**{
            'from': self._hodl_owner
        })

        oracle_fee_wallet = OracleFeeWallet(_TEST_CONFIG)
        oracle_fee_wallet.payback(**{
            'from': self._oracle_owner
        })

        after_wallet_balance = oracle_wallet.get_balance()
        self.assertEqual(before_wallet_balance + convert_to_wei(PAYMENT_VALUE, 'wei'),
                         after_wallet_balance,
                         'balance should be the same')

        other_user = myWeb3.get_accounts()[3]
        before_wallet_balance = oracle_wallet.get_balance()
        before_account_balance = myWeb3.get_address_balance(other_user)
        oracle_wallet.withdraw(other_user, **{
            'from': self._oracle_owner
        })
        after_account_balance = myWeb3.get_address_balance(other_user)
        after_wallet_balance = oracle_wallet.get_balance()

        self.assertEqual(before_wallet_balance - PAYMENT_VALUE,
                         after_wallet_balance,
                         'balance should be the same')
        self.assertEqual(before_account_balance + before_wallet_balance,
                         after_account_balance,
                         'balance should be the same')
        get_all_fee_reports()
    def test_single_late_event(self):
        self._callback_event.clear()
        TEST_TIME = 60
        private_daemon = OracleNodeClient(config_path=_TEST_CONFIG,
                                          to_oracle_node_callback_objs=[self],
                                          wait_time=1)
        private_daemon.link_exception(froce_die)
        private_daemon.start()

        hodl_oracle = HodlOracle(_TEST_CONFIG)
        hodl_oracle.deposit(value=convert_to_wei(20000, 'wei'), **{
            'from': self._hodl_owner
        })

        oracle_fee_wallet = OracleFeeWallet(_TEST_CONFIG)
        before_balance = oracle_fee_wallet.get_balance(hodl_oracle.get_address())
        # use oracle_node to trigger event
        node = OracleCore(_TEST_CONFIG)
        node.query_sent_node(TEST_TIME,
                             hodl_oracle.get_address(),
                             'json(https://api.kraken.com/0/public/Ticker)["error"][0]',
                             **{
                                 'from': self._oracle_owner
                             })
        self._start_time = time.time()

        self._callback_event.wait()

        self.assertNotEqual(self._finish_time, 0, 'SHould not equal 0')
        self.assertTrue((self._finish_time - self._start_time) > TEST_TIME,
                        'callback should wait {0} - {1}'.format(self._finish_time, self._start_time))

        private_daemon.kill()
        self.assertEqual(self._tested, True, 'Should be tested')
        after_balance = oracle_fee_wallet.get_balance(hodl_oracle.get_address())
        self.assertEqual(before_balance, after_balance + 20000, 'Should be the same')
        get_all_fee_reports()
示例#5
0
    def test_single_event(self):
        self.to_oracle_node_data = []
        self.show_event_data = []
        self.sent_event_data = []
        example_daemon = HodlOracleClient(config_path=_TEST_CONFIG,
                                          sent_callback_objs=[self],
                                          show_callback_objs=[self],
                                          wait_time=1)
        example_daemon.start()

        node_daemon = OracleNodeClient(config_path=_TEST_CONFIG,
                                       to_oracle_node_callback_objs=[self],
                                       wait_time=1)
        node_daemon.start()

        hodl_oracle = HodlOracle(_TEST_CONFIG)
        # self.assertEqual(0, hodl_oracle.get_lastest_query_id(), 'There is no query id')

        hodl_oracle.deposit(**{
            'value': convert_to_wei(20000, 'wei'),
            'from': self._hodl_owner
        })
        hodl_oracle.trigger(**{'from': self._hodl_owner})
        hodl_oracle_queryid = hodl_oracle.get_lastest_query_id(
            **{'from': self._hodl_owner})
        gevent.sleep(5)

        for idx, test_list in enumerate([
                self.to_oracle_node_data, self.sent_event_data,
                self.show_event_data
        ]):
            self.assertEqual(
                len(test_list), 1,
                '{0} has more than one entry {1}'.format(test_list, idx))

        # check queryid
        for idx, test_list in enumerate([
                self.to_oracle_node_data, self.sent_event_data,
                self.show_event_data
        ]):
            self.assertEqual(
                test_list[0]['queryId'], hodl_oracle_queryid,
                'query id {0} != {1}, {2}'.format(test_list[0]['queryId'],
                                                  hodl_oracle_queryid, idx))

        # check resquest
        self.assertEqual(self.to_oracle_node_data[0]['request'],
                         self.sent_event_data[0]['request'],
                         'two request should be the same')

        # check response
        self.assertRegex(self.show_event_data[0]['response'], '^(\d+\.?\d+)$')

        self.assertEqual(
            convert_to_hex(self.show_event_data[0]['hash']),
            convert_to_hex(
                Web3.sha3(text=self.show_event_data[0]['response'])))

        # check the result is correct
        example_daemon.kill()
        node_daemon.kill()
        get_all_fee_reports()
示例#6
0
 def setUpClass(cls):
     cls._hodl_owner = ConfigHandler(_TEST_CONFIG).get_hodl_owner()
     cls._oracle_owner = ConfigHandler(_TEST_CONFIG).get_oracle_owner()
     MyDeployer(_TEST_CONFIG).deploy()
     get_all_fee_reports()
示例#7
0
    def test_behavior(self):
        myWeb3 = MyWeb3(_TEST_CONFIG)
        other_user = myWeb3.get_accounts()[3]
        payment_value = convert_to_wei(1000, 'wei')

        hodl_saver = HodlSaver(_TEST_CONFIG)
        now_balance = hodl_saver.get_balance()

        eth_price = get_eth_price()
        hodl_saver.deposit_balance(
            int(eth_price * 2), **{
                'value': payment_value,
                'from': other_user
            })
        new_balance = hodl_saver.get_balance()
        self.assertEqual(new_balance, now_balance + payment_value,
                         'Should be the same')
        now_balance = new_balance

        node_daemon = OracleNodeClient(config_path=_TEST_CONFIG, wait_time=1)
        node_daemon.start()

        test_example = HodlOracle(_TEST_CONFIG)
        test_example.deposit(**{
            'value': convert_to_wei(20000, 'wei'),
            'from': self._hodl_owner
        })
        oracle_fee_wallet = OracleFeeWallet(_TEST_CONFIG)
        self.assertEqual(
            oracle_fee_wallet.get_balance(test_example.get_address()),
            convert_to_wei(20000, 'wei'), 'should be the same in balance')
        test_example.trigger(**{'from': self._hodl_owner})

        new_balance = hodl_saver.get_balance()
        self.assertEqual(new_balance, now_balance, 'Should be the same')
        now_balance = new_balance

        eth_price = get_eth_price()
        hodl_saver.deposit_balance(
            int(eth_price / 2), **{
                'value': payment_value,
                'from': other_user
            })
        new_balance = hodl_saver.get_balance()
        self.assertEqual(new_balance, now_balance + payment_value,
                         'Should be the same')
        now_balance = new_balance
        test_example.deposit(**{
            'value': convert_to_wei(20000, 'wei'),
            'from': self._hodl_owner
        })
        test_example.trigger(**{'from': self._hodl_owner})

        for _ in range(15):
            gevent.sleep(2)
            new_balance = hodl_saver.get_balance()
            if new_balance != now_balance:
                break

        new_balance = hodl_saver.get_balance()
        self.assertEqual(new_balance, 0, 'Should be the same')

        node_daemon.kill()
        self.assertEqual(
            oracle_fee_wallet.get_balance(test_example.get_address()),
            convert_to_wei(0, 'wei'), 'should be the same in balance')
        get_all_fee_reports()