예제 #1
0
    def test_short_on_gnt_closure_time(self):
        self.sci.get_eth_balance.return_value = denoms.ether
        self.sci.get_gnt_balance.return_value = 0
        self.sci.get_gntb_balance.return_value = 4 * denoms.ether
        self.pp.CLOSURE_TIME_DELAY = 0
        ts1 = 1000
        ts2 = 2000

        p1, scip1 = make_awaiting_payment(value=1 * denoms.ether, ts=ts1)
        p2, scip2 = make_awaiting_payment(value=2 * denoms.ether, ts=ts2)
        p5, scip5 = make_awaiting_payment(value=5 * denoms.ether, ts=ts2)
        self.pp.add(p1)
        self.pp.add(p2)
        self.pp.add(p5)

        with freeze_time(timestamp_to_datetime(10000)):
            self.pp.sendout(0)
            self._assert_batch_transfer_called_with([scip1], ts1)
            self.sci.batch_transfer.reset_mock()

        self.sci.get_gntb_balance.return_value = 10 * denoms.ether
        with freeze_time(timestamp_to_datetime(10000)):
            self.pp.sendout(0)
            self._assert_batch_transfer_called_with([scip2, scip5], ts2)
            self.sci.batch_transfer.reset_mock()
예제 #2
0
    def test_payment_timestamp(self):
        self.sci.get_eth_balance.return_value = denoms.ether

        ts = 7000000
        p = Payment.create(subtask="p1", payee=urandom(20), value=1)
        with freeze_time(timestamp_to_datetime(ts)):
            self.pp.add(p)
        self.assertEqual(ts, p.processed_ts)

        new_ts = 900000
        with freeze_time(timestamp_to_datetime(new_ts)):
            self.pp.add(p)
        self.assertEqual(ts, p.processed_ts)
예제 #3
0
 def __init__(self, task_header):
     self.uuid = task_header.task_id
     self.interval_start_date =\
         timestamp_to_datetime(task_header.last_checking)\
         .replace(hour=0, minute=0, second=0, microsecond=0)
     self.deadline = task_header.deadline
     self.min_version = task_header.min_version
     self.max_price = task_header.max_price
     self.requesting_trust = None
     self.unsupport_reasons = None
예제 #4
0
    def test_batch_transfer_throws(self):
        self.sci.get_eth_balance.return_value = 1000 * denoms.ether
        self.sci.get_gnt_balance.return_value = 0
        self.sci.get_gntb_balance.return_value = 1000 * denoms.ether
        self.pp.CLOSURE_TIME_DELAY = 0

        ts = 100000
        p, scip = make_awaiting_payment(value=1, ts=ts)
        self.pp.add(p)
        self.sci.batch_transfer.side_effect = Exception

        with freeze_time(timestamp_to_datetime(ts)):
            with self.assertRaises(Exception):
                self.pp.sendout(0)
            self._assert_batch_transfer_called_with([scip], ts)
            self.sci.batch_transfer.reset_mock()

        self.sci.batch_transfer.side_effect = None
        with freeze_time(timestamp_to_datetime(ts)):
            self.pp.sendout(0)
            self._assert_batch_transfer_called_with([scip], ts)
예제 #5
0
    def test_batch_transfer(self):
        deadline = PAYMENT_MAX_DELAY
        self.pp.CLOSURE_TIME_DELAY = 0
        self.sci.get_eth_balance.return_value = denoms.ether
        self.sci.get_gnt_balance.return_value = 0
        self.sci.get_gntb_balance.return_value = 1000 * denoms.ether

        assert not self.pp.sendout()
        self.sci.batch_transfer.assert_not_called()

        ts1 = 1230000
        ts2 = ts1 + 2 * deadline
        p1, scip1 = make_awaiting_payment(ts=ts1)
        p2, scip2 = make_awaiting_payment(ts=ts2)
        self.pp.add(p1)
        self.pp.add(p2)

        with freeze_time(timestamp_to_datetime(ts1 + deadline - 1)):
            assert not self.pp.sendout()
            self.sci.batch_transfer.assert_not_called()
        with freeze_time(timestamp_to_datetime(ts1 + deadline + 1)):
            assert self.pp.sendout()
            self._assert_batch_transfer_called_with(
                [scip1],
                ts1,
            )
            self.sci.batch_transfer.reset_mock()

        with freeze_time(timestamp_to_datetime(ts2 + deadline - 1)):
            assert not self.pp.sendout()
            self.sci.batch_transfer.assert_not_called()
        with freeze_time(timestamp_to_datetime(ts2 + deadline + 1)):
            assert self.pp.sendout()
            self._assert_batch_transfer_called_with(
                [scip2],
                ts2,
            )
            self.sci.batch_transfer.reset_mock()
예제 #6
0
    def test_short_on_eth(self):
        self.sci.get_eth_balance.return_value = self.sci.GAS_PRICE * \
            (self.sci.GAS_BATCH_PAYMENT_BASE + 2 * self.sci.GAS_PER_PAYMENT)
        self.sci.get_gnt_balance.return_value = 0
        self.sci.get_gntb_balance.return_value = 1000 * denoms.ether
        self.pp.CLOSURE_TIME_DELAY = 0

        p1, scip1 = make_awaiting_payment(value=1, ts=1)
        p2, scip2 = make_awaiting_payment(value=2, ts=2)
        p5, scip5 = make_awaiting_payment(value=5, ts=3)
        self.pp.add(p1)
        self.pp.add(p2)
        self.pp.add(p5)

        with freeze_time(timestamp_to_datetime(10000)):
            self.pp.sendout(0)
            self._assert_batch_transfer_called_with([scip1, scip2], 2)
            self.sci.batch_transfer.reset_mock()

        self.sci.get_eth_balance.return_value = denoms.ether
        with freeze_time(timestamp_to_datetime(10000)):
            self.pp.sendout(0)
            self._assert_batch_transfer_called_with([scip5], 3)
            self.sci.batch_transfer.reset_mock()
예제 #7
0
    def test_sorted_payments(self):
        self.sci.get_eth_balance.return_value = 1000 * denoms.ether
        self.sci.get_gnt_balance.return_value = 0
        self.sci.get_gntb_balance.return_value = 1000 * denoms.ether
        self.pp.CLOSURE_TIME_DELAY = 0

        p1, _ = make_awaiting_payment(value=1, ts=300000)
        p2, scip2 = make_awaiting_payment(value=2, ts=200000)
        p3, scip3 = make_awaiting_payment(value=3, ts=100000)
        self.pp.add(p1)
        self.pp.add(p2)
        self.pp.add(p3)

        with freeze_time(timestamp_to_datetime(200000)):
            self.pp.sendout(0)
            self._assert_batch_transfer_called_with([scip3, scip2], 200000)
예제 #8
0
    def test_block_gas_limit(self):
        self.sci.get_eth_balance.return_value = denoms.ether
        self.sci.get_gnt_balance.return_value = 0
        self.sci.get_gntb_balance.return_value = 1000 * denoms.ether
        self.sci.get_latest_block.return_value.gas_limit = \
            (self.sci.GAS_BATCH_PAYMENT_BASE + self.sci.GAS_PER_PAYMENT) /\
            self.pp.BLOCK_GAS_LIMIT_RATIO
        self.pp.CLOSURE_TIME_DELAY = 0

        p1, scip1 = make_awaiting_payment(value=1, ts=1)
        p2, _ = make_awaiting_payment(value=2, ts=2)
        self.pp.add(p1)
        self.pp.add(p2)

        with freeze_time(timestamp_to_datetime(10000)):
            self.pp.sendout(0)
            self._assert_batch_transfer_called_with([scip1], 1)
            self.sci.batch_transfer.reset_mock()
예제 #9
0
    def test_closure_time(self):
        self.sci.get_eth_balance.return_value = denoms.ether
        self.sci.get_gnt_balance.return_value = 0
        self.sci.get_gntb_balance.return_value = 1000 * denoms.ether

        p1, scip1 = make_awaiting_payment()
        p2, scip2 = make_awaiting_payment()
        p5, scip5 = make_awaiting_payment()
        with freeze_time(timestamp_to_datetime(1000000)):
            self.pp.add(p1)
        with freeze_time(timestamp_to_datetime(2000000)):
            self.pp.add(p2)
        with freeze_time(timestamp_to_datetime(5000000)):
            self.pp.add(p5)

        closure_time = 2000000
        time_value = closure_time + self.pp.CLOSURE_TIME_DELAY
        with freeze_time(timestamp_to_datetime(time_value)):
            self.pp.sendout(0)
            self._assert_batch_transfer_called_with([scip1, scip2],
                                                    closure_time)
            self.sci.batch_transfer.reset_mock()

        closure_time = 4000000
        time_value = closure_time + self.pp.CLOSURE_TIME_DELAY
        with freeze_time(timestamp_to_datetime(time_value)):
            self.pp.sendout(0)
            self.sci.batch_transfer.assert_not_called()
            self.sci.batch_transfer.reset_mock()

        closure_time = 5000000
        time_value = closure_time + self.pp.CLOSURE_TIME_DELAY
        with freeze_time(timestamp_to_datetime(time_value)):
            self.pp.sendout(0)
            self._assert_batch_transfer_called_with([scip5], closure_time)
            self.sci.batch_transfer.reset_mock()
예제 #10
0
 def test_datetime_to_timestamp(self):
     ts = get_timestamp_utc()
     assert ts
     dt = timestamp_to_datetime(ts)
     assert datetime_to_timestamp(dt) == ts
예제 #11
0
 def test_datetime_to_timestamp(self):
     ts = get_timestamp_utc()
     assert ts
     dt = timestamp_to_datetime(ts)
     assert round(datetime_to_timestamp(dt), 5) == round(ts, 5)