Пример #1
0
    def test_account_add_block_invalid_precomputed_work(
            self, pocketable_block_factory):
        account = Account(**ACCOUNT_KWARGS)
        account.receive_block(
            pocketable_block_factory(account_id=ACCOUNT_ID, amount=1000)
        )

        # Precompute work
        account.precomputed_work = PrecomputedWork(
            work=solve_work(
                account.blocks[-1].block_hash, difficulty=TEST_DIFFICULTY
            ),
            difficulty=TEST_DIFFICULTY
        )

        # Use an arbitrarily high difficulty to make the precomputed work
        # invalid
        account.precomputed_work.difficulty = "f"*16

        # Precomputed work is discarded silently since it was invalid
        # at the time the block was received
        block = account.receive_block(
            pocketable_block_factory(account_id=ACCOUNT_ID, amount=1000)
        )
        assert not block.work
        assert not account.precomputed_work
Пример #2
0
    def test_account_receive_legacy_multiple(self, legacy_pocketable_block_factory):
        """
        Receive two legacy receive blocks to start an account blockchain
        """
        account = Account(**ACCOUNT_KWARGS)

        # Create two fake blocks to receive 10000 and 20000 raw respectively
        link_block_a = legacy_pocketable_block_factory(
            account_id=ACCOUNT_ID, amount=10000)
        link_block_b = legacy_pocketable_block_factory(
            account_id=ACCOUNT_ID, amount=20000)

        account.receive_block(link_block_a)
        account.receive_block(link_block_b)

        assert account.balance == 30000

        first_block, second_block = account.blocks

        # Blockchain forms a linked list
        assert first_block.next == second_block
        assert second_block.prev == first_block

        assert second_block.block_type == "state"
        assert second_block.tx_type == "receive"
        assert second_block.previous == first_block.block_hash
        assert second_block.amount == 20000
        assert second_block.balance == 30000
        assert second_block.link == link_block_b.block_hash
Пример #3
0
    def test_account_receive_legacy_first(
            self, legacy_pocketable_block_factory):
        """
        Receive a legacy receive block to start an account blockchain
        """
        account = Account(**ACCOUNT_KWARGS)

        # Create a fake block to receive 10000 raw
        link_block = legacy_pocketable_block_factory(
            account_id=ACCOUNT_ID, amount=10000)

        account.receive_block(link_block)

        assert account.balance == 10000

        first_block = account.blocks[0]

        # Check the receiving block
        assert first_block.link == first_block.link_block.block_hash
        assert first_block.amount == 10000
        assert first_block.block_type == "state"
        assert first_block.tx_type == "open"

        assert first_block.prev is None
        assert first_block.next is None
Пример #4
0
    def test_account_receive_first(self, pocketable_block_factory):
        """
        Receive the first state block on an account and check that it's
        an open block
        """
        account = Account(**ACCOUNT_KWARGS)

        assert account.balance == 0

        # Create a fake block to receive 10000 raw
        link_block = pocketable_block_factory(account_id=ACCOUNT_ID, amount=10000)

        account.receive_block(link_block)
        first_block = account.blocks[0]

        assert account.balance == 10000

        # Check the receiving block
        assert first_block.link == first_block.link_block.block_hash
        assert first_block.amount == 10000
        assert first_block.block_type == "state"
        assert first_block.tx_type == "open"

        # Check the sending block
        assert first_block.link_block.link_as_account == ACCOUNT_ID

        assert first_block.prev is None
        assert first_block.next is None
Пример #5
0
    def test_account_receive_duplicate(self, pocketable_block_factory):
        """
        Try receiving the same block twice
        """
        account = Account(**ACCOUNT_KWARGS)

        link_block = pocketable_block_factory(account_id=ACCOUNT_ID, amount=10000)

        account.receive_block(link_block)
        assert account.balance == 10000

        # Second attempt will do nothing
        assert account.receive_block(link_block) is None
        assert account.balance == 10000
Пример #6
0
    def test_account_send_legacy_block(self, legacy_pocketable_block_factory):
        """
        Receive NANO from a legacy block and then spend it
        """
        account = Account(**ACCOUNT_KWARGS)

        account.receive_block(
            legacy_pocketable_block_factory(account_id=ACCOUNT_ID, amount=10000)
        )

        first_block = account.blocks[0]

        send_block = account.send(account_id=ACCOUNT_ID_B, amount=6000)

        assert account.balance == 4000

        assert send_block.balance == 4000
        # Since we're sending, the amount is negative
        assert send_block.amount == -6000
        assert send_block.previous == first_block.block_hash
Пример #7
0
    def test_account_add_block_first_non_open(self, pocketable_block_factory):
        """
        Try adding a non-open block as the first block to an account
        """
        account = Account(**ACCOUNT_KWARGS)

        for _ in range(0, 2):
            account.receive_block(
                pocketable_block_factory(account_id=ACCOUNT_ID, amount=1000)
            )

        block_b = account.blocks[1]

        # Recreate account
        account = Account(**ACCOUNT_KWARGS)

        with pytest.raises(InvalidAccountBlock) as exc:
            account.add_block(block_b)

        assert "First block for an account has to be an 'open' block" in str(exc.value)
Пример #8
0
    def test_account_legacy_block_disallowed_after_state(
            self, pocketable_block_factory, legacy_receive_block_factory):
        """
        Add a state block and then try adding a legacy receive block
        """
        account = Account(**ACCOUNT_KWARGS)

        account.receive_block(
            pocketable_block_factory(account_id=ACCOUNT_ID, amount=10000)
        )

        with pytest.raises(InvalidAccountBlock) as exc:
            # Once a state block is added to a chain, only state blocks are
            # allowed
            account.add_block(
                legacy_receive_block_factory(
                    prev_block=account.blocks[0],
                    private_key=PRIVATE_KEY,
                    amount=20000
                )
            )

        assert "State block can't be followed by a legacy block" in str(exc.value)
Пример #9
0
    def test_account_add_block_precomputed_work(
            self, pocketable_block_factory):
        account = Account(**ACCOUNT_KWARGS)

        account.receive_block(
            pocketable_block_factory(account_id=ACCOUNT_ID, amount=1000)
        )

        # Precompute work
        account.precomputed_work = PrecomputedWork(
            work=solve_work(
                account.blocks[-1].block_hash, difficulty=TEST_DIFFICULTY
            ),
            difficulty=TEST_DIFFICULTY
        )

        block = account.receive_block(
            pocketable_block_factory(account_id=ACCOUNT_ID, amount=1000)
        )

        # The precomputed work is used
        assert block.work
        assert not account.precomputed_work
Пример #10
0
    def test_account_add_block_wrong_order(self, pocketable_block_factory):
        """
        Try adding blocks to an account in wrong order
        """
        account = Account(**ACCOUNT_KWARGS)

        for _ in range(0, 3):
            account.receive_block(
                pocketable_block_factory(account_id=ACCOUNT_ID, amount=1000)
            )

        block_a = account.blocks[0]
        block_c = account.blocks[2]

        # Recreate account
        account = Account(**ACCOUNT_KWARGS)
        account.add_block(block_a)

        # Block added in wrong order
        with pytest.raises(InvalidAccountBlock) as exc:
            account.add_block(block_c)

        assert "isn't a successor to the current head" in str(exc.value)