Пример #1
0
    def test_withdraw_unstake(self, stake, unstakes_info, amount,
                              expected_unstakes_info):
        expected_total_unstake = sum(unstake
                                     for unstake, _ in expected_unstakes_info)

        stake_part = StakePart(stake=stake, unstakes_info=unstakes_info)
        stake_part.normalize(block_height=500,
                             revision=Revision.FIX_UNSTAKE_BUG.value)

        # withdraw() assumes that amount is less than total_stake
        assert stake_part.total_unstake > amount
        stake_part.withdraw_unstake(amount)

        assert stake_part.stake == stake
        assert stake_part.unstakes_info == expected_unstakes_info
        assert stake_part.total_unstake == expected_total_unstake
Пример #2
0
    def test_stake_part_set_unstake_update(self):
        part = StakePart()
        part.set_complete(True)

        stake = 100
        block_height = 10
        part.add_stake(100)
        unstake = stake
        part.set_unstake(block_height, unstake)

        self.assertEqual(0, part.stake)
        self.assertEqual(stake, part.unstake)
        self.assertEqual(block_height, part.unstake_block_height)
        self.assertTrue(
            part.is_set(BasePartState.DIRTY | BasePartState.COMPLETE))

        block_height += block_height
        unstake = 10
        part.set_unstake(block_height, unstake)
        self.assertEqual(stake - unstake, part.stake)
        self.assertEqual(unstake, part.unstake)
        self.assertEqual(block_height, part.unstake_block_height)
        self.assertTrue(
            part.is_set(BasePartState.DIRTY | BasePartState.COMPLETE))

        refund_unstake = part.normalize(block_height + 1, Revision.IISS.value)
        self.assertEqual(unstake, refund_unstake)
Пример #3
0
    def test_stake_part_update(self):
        stake: int = 10
        unstake: int = 0
        unstake_block_height: int = 0
        part1 = StakePart(stake, unstake, unstake_block_height)
        part1.normalize(0, Revision.IISS.value)
        self.assertEqual(stake, part1.stake)
        self.assertEqual(unstake, part1.unstake)
        self.assertEqual(unstake_block_height, part1.unstake_block_height)

        stake: int = 5
        unstake: int = 5
        unstake_block_height: int = 3
        part2 = StakePart(stake, unstake, unstake_block_height)
        part2.normalize(unstake_block_height + 1, Revision.IISS.value)
        self.assertEqual(stake, part2.stake)
        self.assertEqual(0, part2.unstake)
        self.assertEqual(0, part2.unstake_block_height)
Пример #4
0
    def test_normalize(self, revision, block_height, unstakes_info,
                       expected_expired_unstake, expected_unstakes_info,
                       expected_dirty):
        stake = random.randint(0, 100)

        stake_part = StakePart(stake=stake,
                               unstakes_info=copy.deepcopy(unstakes_info))
        expired_unstake = stake_part.normalize(block_height, revision)
        assert expired_unstake == expected_expired_unstake
        assert stake_part.unstakes_info == expected_unstakes_info
        assert stake_part.is_dirty() == expected_dirty
Пример #5
0
 def test_stake_part_update_multiple_unstake(self):
     stake: int = 5
     block_height = 3
     unstake_info1 = [10, 1]
     unstake_info2 = [20, 2]
     unstake_info3 = [40, 3]
     unstake_info4 = [30, 5]
     unstake_info5 = [100, 5]
     unstakes_info = [
         unstake_info1, unstake_info2, unstake_info3, unstake_info4,
         unstake_info5
     ]
     part = StakePart(stake=stake, unstakes_info=unstakes_info)
     part.set_complete(True)
     part.normalize(block_height, Revision.MULTIPLE_UNSTAKE.value)
     self.assertEqual(stake, part.stake)
     self.assertEqual(0, part.unstake)
     self.assertEqual(0, part.unstake_block_height)
     expected_info = [[40, 3], [30, 5], [100, 5]]
     self.assertEqual(expected_info, part.unstakes_info)
Пример #6
0
    def test_stake_part_on_rev_8(self, block_height, params, expected):
        revision = 8
        stake_part = StakePart(**params)
        expired_unstake: int = stake_part.normalize(block_height=block_height,
                                                    revision=revision)

        assert expired_unstake == expected["expired_unstake"]
        assert stake_part.stake == expected["stake"]
        assert stake_part.total_stake == expected["stake"] + expected["unstake"]
        assert stake_part.unstake == expected["unstake"]
        assert stake_part.total_unstake == expected["unstake"]
        assert stake_part.unstake_block_height == expected[
            "unstake_block_height"]
Пример #7
0
    def test_set_unstakes_info(self, unstakes_info, new_total_unstake,
                               unstake_block_height, expected_unstakes_info):
        old_total_unsake: int = sum(
            info[0] for info in unstakes_info) if unstakes_info else 0
        assert new_total_unstake != old_total_unsake

        stake = random.randint(0, 100)
        slot_max = 3
        revision = Revision.FIX_UNSTAKE_BUG.value
        block_height = 500

        stake_part = StakePart(stake=stake, unstakes_info=unstakes_info)
        stake_part.normalize(block_height, revision)

        unstake_delta: int = new_total_unstake - old_total_unsake
        stake_part.set_unstakes_info(unstake_block_height, new_total_unstake,
                                     slot_max)
        assert stake_part.is_dirty()
        assert stake_part.stake == stake - unstake_delta
        assert stake_part.unstakes_info == expected_unstakes_info
        assert stake_part.total_unstake == new_total_unstake
        assert stake_part.total_stake == stake_part.stake + stake_part.total_unstake
Пример #8
0
    def test_stake_part_from_bytes_to_bytes(self):
        part1 = StakePart()
        part1.normalize(0, Revision.IISS.value)
        data = part1.to_bytes(Revision.IISS.value)
        self.assertTrue(isinstance(data, bytes))
        self.assertEqual(5, len(data))

        part2 = StakePart.from_bytes(data)
        part2.normalize(0, Revision.IISS.value)
        self.assertEqual(part1.stake, part2.stake)
        self.assertEqual(part1.unstake, part2.unstake)
        self.assertEqual(part1.unstake_block_height,
                         part2.unstake_block_height)

        part3 = StakePart(10, 20, 30)
        part3.normalize(0, Revision.IISS.value)
        part4 = StakePart.from_bytes(part3.to_bytes(Revision.IISS.value))
        part4.normalize(0, Revision.IISS.value)
        self.assertEqual(part3.stake, part4.stake)
        self.assertEqual(part3.unstake, part4.unstake)
        self.assertEqual(part3.unstake_block_height,
                         part4.unstake_block_height)
Пример #9
0
    def test_delegation_part_equal(self):
        part1 = StakePart()
        part1.normalize(0, Revision.IISS.value)
        part2 = StakePart()
        part2.normalize(1, Revision.IISS.value)
        self.assertEqual(part1, part2)

        offset = 100
        part1.add_stake(offset)

        part3 = StakePart(stake=offset)
        part3.normalize(100, Revision.IISS.value)
        self.assertEqual(part1, part3)
Пример #10
0
    def test_delegation_part_equal(self):
        part1 = StakePart()
        part1.normalize(0)
        part2 = StakePart()
        part2.normalize(1)
        self.assertEqual(part1, part2)

        offset = 100
        part1.add_stake(offset)

        part3 = StakePart(stake=offset)
        part3.normalize(100)
        self.assertEqual(part1, part3)