Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Insoleet committed Aug 8, 2018
1 parent 2964ea7 commit 01d58e4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 121 deletions.
30 changes: 13 additions & 17 deletions src/sakia/data/processors/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,14 @@ async def initialize_blockchain(self, currency):
except errors.DuniterError as e:
if e.ucode != errors.NO_CURRENT_BLOCK:
raise
await self.refresh_dividend_data(currency, blockchain)

try:
self._repo.insert(blockchain)
except sqlite3.IntegrityError:
self._repo.update(blockchain)

async def refresh_dividend_data(self, currency, blockchain):
self._logger.debug("Requesting blocks with dividend")
with_ud = await self._bma_connector.get(currency, bma.blockchain.ud)
blocks_with_ud = with_ud['result']['blocks']
Expand Down Expand Up @@ -341,11 +348,6 @@ async def initialize_blockchain(self, currency):
if e.ucode != errors.NO_CURRENT_BLOCK:
raise

try:
self._repo.insert(blockchain)
except sqlite3.IntegrityError:
self._repo.update(blockchain)

async def handle_new_blocks(self, currency, network_blockstamp):
"""
Initialize blockchain for a given currency if no source exists locally
Expand All @@ -363,23 +365,17 @@ async def handle_new_blocks(self, currency, network_blockstamp):
blockchain.current_buid = block.blockUID
blockchain.median_time = block.mediantime
blockchain.current_members_count = block.members_count
if block.ud:
blockchain.current_mass += (block.ud * 10**block.unit_base) * block.members_count
if block.ud and blockchain.last_ud_time + blockchain.parameters.dt_reeval >= block.mediantime:
blockchain.previous_mass = blockchain.last_mass
blockchain.previous_members_count = blockchain.last_members_count
blockchain.previous_ud = blockchain.last_ud
blockchain.previous_ud_base = blockchain.last_ud_base
blockchain.previous_ud_time = blockchain.last_ud_time
blockchain.last_members_count = block.members_count
blockchain.last_ud = block.ud
blockchain.last_ud_base = block.unit_base
blockchain.last_ud_time = block.mediantime

if blockchain.last_ud_time + blockchain.parameters.dt <= blockchain.median_time:
await self.refresh_dividend_data(currency, blockchain)

self._repo.update(blockchain)

except errors.DuniterError as e:
if e.ucode != errors.NO_CURRENT_BLOCK:
raise


def remove_blockchain(self, currency):
self._repo.drop(self._repo.get_one(currency=currency))

19 changes: 2 additions & 17 deletions tests/technical/test_blockchain_service.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
from sakia.data.entities import Identity
from duniterpy.documents.certification import Certification
import pytest

import pytest
import time

@pytest.mark.asyncio
async def test_new_block_with_ud(application_with_one_connection, fake_server_with_blockchain):
previous_ud = application_with_one_connection.blockchain_service.previous_ud()
fake_server_with_blockchain.forge.forge_block()
fake_server_with_blockchain.forge.generate_dividend()
fake_server_with_blockchain.forge.forge_block()
fake_server_with_blockchain.forge.generate_dividend()
fake_server_with_blockchain.forge.forge_block()
new_blocks = fake_server_with_blockchain.forge.blocks[-3:]
application_with_one_connection.blockchain_service.handle_new_blocks(
new_blocks)
previous_ud_after_parse = application_with_one_connection.blockchain_service.previous_ud()
assert previous_ud_after_parse > previous_ud
await fake_server_with_blockchain.close()
6 changes: 3 additions & 3 deletions tests/technical/test_documents_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

@pytest.mark.asyncio
async def test_send_more_than_40_sources(application_with_one_connection, fake_server_with_blockchain, bob, alice):
start = fake_server_with_blockchain.forge.blocks[-1].number + 1
for i in range(0, 60):
fake_server_with_blockchain.forge.generate_dividend()
fake_server_with_blockchain.forge.forge_block()

new_blocks = fake_server_with_blockchain.forge.blocks[-60:]
end = fake_server_with_blockchain.forge.blocks[-1].number
connections = ConnectionsProcessor.instanciate(application_with_one_connection).connections()
changed_tx, new_tx, new_ud = await application_with_one_connection.transactions_service.handle_new_blocks(connections,
new_blocks)
start, end)

await application_with_one_connection.sources_service.refresh_sources_of_pubkey(bob.key.pubkey)
amount_before_send = application_with_one_connection.sources_service.amount(bob.key.pubkey)
Expand Down
61 changes: 0 additions & 61 deletions tests/technical/test_identities_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,3 @@

import pytest


@pytest.mark.asyncio
async def test_new_block_with_certs(application_with_one_connection, fake_server_with_blockchain, bob, alice):
certs_before_send = application_with_one_connection.identities_service.certifications_sent(
bob.key.pubkey)
alice_user_identity = fake_server_with_blockchain.forge.user_identities[bob.key.pubkey]
alice_identity = Identity(currency=fake_server_with_blockchain.forge.currency,
pubkey=alice.key.pubkey,
uid=alice.uid,
blockstamp=alice_user_identity.blockstamp,
signature=alice_user_identity.signature)
bob_connection = application_with_one_connection.db.connections_repo.get_one(pubkey=bob.key.pubkey)
await application_with_one_connection.documents_service.certify(bob_connection,
bob.salt,
bob.password, alice_identity)
certs_after_send = application_with_one_connection.identities_service.certifications_sent(
bob.key.pubkey)
assert len(certs_after_send) == len(certs_before_send) + 1
assert certs_after_send[0].written_on == -1
assert isinstance(fake_server_with_blockchain.forge.pool[0], Certification)
fake_server_with_blockchain.forge.forge_block()
fake_server_with_blockchain.forge.forge_block()
fake_server_with_blockchain.forge.forge_block()
new_blocks = fake_server_with_blockchain.forge.blocks[-3:]
await application_with_one_connection.identities_service.handle_new_blocks(
new_blocks)
certs_after_parse = application_with_one_connection.identities_service.certifications_sent(
bob.key.pubkey)
assert len(certs_after_parse) == len(certs_after_send)
assert certs_after_parse[0].written_on == fake_server_with_blockchain.forge.blocks[-3].number
await fake_server_with_blockchain.close()


@pytest.mark.asyncio
async def test_new_block_with_idty(application_with_one_connection, john, fake_server_with_blockchain):
john_identity = Identity(currency=fake_server_with_blockchain.forge.currency,
pubkey=john.key.pubkey,
uid=john.uid,
blockstamp=john.blockstamp,
signature=john.identity().signatures[0])
john_connection = Connection(currency="test_currency",
pubkey=john.key.pubkey, uid=john.uid,
scrypt_N=4096, scrypt_r=4, scrypt_p=2,
blockstamp=john.blockstamp)
application_with_one_connection.db.connections_repo.insert(john_connection)
application_with_one_connection.db.identities_repo.insert(john_identity)
application_with_one_connection.instanciate_services()

john_found = application_with_one_connection.db.identities_repo.get_one(pubkey=john_identity.pubkey)
assert john_found.written is False
fake_server_with_blockchain.forge.forge_block()
fake_server_with_blockchain.forge.push(john.identity())
fake_server_with_blockchain.forge.push(john.join(BlockUID.empty()))
fake_server_with_blockchain.forge.forge_block()
fake_server_with_blockchain.forge.forge_block()
new_blocks = fake_server_with_blockchain.forge.blocks[-3:]
await application_with_one_connection.identities_service.handle_new_blocks(
new_blocks)
john_found = application_with_one_connection.db.identities_repo.get_one(pubkey=john_identity.pubkey)
assert john_found.written is True
await fake_server_with_blockchain.close()
30 changes: 7 additions & 23 deletions tests/technical/test_sources_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ async def test_receive_source(application_with_one_connection, fake_server_with_
amount = application_with_one_connection.sources_service.amount(bob.key.pubkey)
fake_server_with_blockchain.forge.push(alice.send_money(150, fake_server_with_blockchain.forge.user_identities[alice.key.pubkey].sources, bob,
fake_server_with_blockchain.forge.blocks[-1].blockUID, "Test receive"))
start = fake_server_with_blockchain.forge.blocks[-1].number + 1
fake_server_with_blockchain.forge.forge_block()
fake_server_with_blockchain.forge.forge_block()
fake_server_with_blockchain.forge.forge_block()
new_blocks = fake_server_with_blockchain.forge.blocks[-3:]
end = fake_server_with_blockchain.forge.blocks[-1].number + 1
connections = ConnectionsProcessor.instanciate(application_with_one_connection).connections()
await application_with_one_connection.transactions_service.handle_new_blocks(connections, new_blocks)
await application_with_one_connection.transactions_service.handle_new_blocks(connections, start, end)
await application_with_one_connection.sources_service.refresh_sources(connections)
assert amount + 150 == application_with_one_connection.sources_service.amount(bob.key.pubkey)
await fake_server_with_blockchain.close()
Expand All @@ -24,33 +25,16 @@ async def test_send_source(application_with_one_connection, fake_server_with_blo
amount = application_with_one_connection.sources_service.amount(bob.key.pubkey)
fake_server_with_blockchain.forge.push(bob.send_money(150, fake_server_with_blockchain.forge.user_identities[bob.key.pubkey].sources, alice,
fake_server_with_blockchain.forge.blocks[-1].blockUID, "Test receive"))
fake_server_with_blockchain.forge.forge_block()
fake_server_with_blockchain.forge.forge_block()
fake_server_with_blockchain.forge.forge_block()
new_blocks = fake_server_with_blockchain.forge.blocks[-3:]
connections = ConnectionsProcessor.instanciate(application_with_one_connection).connections()
await application_with_one_connection.transactions_service.handle_new_blocks(connections, new_blocks)
await application_with_one_connection.sources_service.refresh_sources(connections)
assert amount - 150 == application_with_one_connection.sources_service.amount(bob.key.pubkey)
await fake_server_with_blockchain.close()

start = fake_server_with_blockchain.forge.blocks[-1].number + 1

@pytest.mark.asyncio
async def test_destruction(application_with_one_connection, fake_server_with_blockchain, bob, alice):
amount = application_with_one_connection.sources_service.amount(bob.key.pubkey)
fake_server_with_blockchain.forge.push(bob.send_money(amount - 80, fake_server_with_blockchain.forge.user_identities[bob.key.pubkey].sources, alice,
fake_server_with_blockchain.forge.blocks[-1].blockUID, "Test receive"))
fake_server_with_blockchain.forge.forge_block()
fake_server_with_blockchain.forge.forge_block()
fake_server_with_blockchain.forge.forge_block()
new_blocks = fake_server_with_blockchain.forge.blocks[-3:]
end = fake_server_with_blockchain.forge.blocks[-1].number + 1
connections = ConnectionsProcessor.instanciate(application_with_one_connection).connections()
await application_with_one_connection.transactions_service.handle_new_blocks(connections,
new_blocks)
await application_with_one_connection.transactions_service.handle_new_blocks(connections, start, end)
await application_with_one_connection.sources_service.refresh_sources(connections)
assert 0 == application_with_one_connection.sources_service.amount(bob.key.pubkey)
tx_after_parse = application_with_one_connection.transactions_service.transfers(bob.key.pubkey)
assert "Too low balance" in [t.comment for t in tx_after_parse]
assert amount - 150 == application_with_one_connection.sources_service.amount(bob.key.pubkey)
await fake_server_with_blockchain.close()


Expand Down

0 comments on commit 01d58e4

Please sign in to comment.