diff --git a/src/sakia/data/processors/blockchain.py b/src/sakia/data/processors/blockchain.py index bd8380092b15a2bafdbdd3158ecf7ac733a8ff45..e6823cb71ecf717cfcbb48af1cda4e9803920891 100644 --- a/src/sakia/data/processors/blockchain.py +++ b/src/sakia/data/processors/blockchain.py @@ -286,7 +286,14 @@ class BlockchainProcessor: 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'] @@ -341,11 +348,6 @@ class BlockchainProcessor: 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 @@ -363,23 +365,17 @@ class BlockchainProcessor: 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)) diff --git a/tests/technical/test_blockchain_service.py b/tests/technical/test_blockchain_service.py index ba8a872a117e4e42d9be60ddaade91a79ea60431..5801121d95075105fe1463adb557f6821b0deb0f 100644 --- a/tests/technical/test_blockchain_service.py +++ b/tests/technical/test_blockchain_service.py @@ -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() \ No newline at end of file diff --git a/tests/technical/test_documents_service.py b/tests/technical/test_documents_service.py index 3718f05084d84b7b0ce9bbacd64619ed1065fb47..d841ce61fa0fce173f603ea859913dcc2044f81d 100644 --- a/tests/technical/test_documents_service.py +++ b/tests/technical/test_documents_service.py @@ -4,14 +4,14 @@ from sakia.data.processors import ConnectionsProcessor @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) diff --git a/tests/technical/test_identities_service.py b/tests/technical/test_identities_service.py index f9842c6bfe1133e976343a87c46e050c80bd0fda..95cecd14b45d841396d4c8171dad61b2141b755f 100644 --- a/tests/technical/test_identities_service.py +++ b/tests/technical/test_identities_service.py @@ -3,64 +3,3 @@ from duniterpy.documents import Certification, BlockUID 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() diff --git a/tests/technical/test_sources_service.py b/tests/technical/test_sources_service.py index c1ad6a8facd7c7f084864ecabad3c6574cd891f2..f9258e5bd9227a7c3c242ae603705425c0fb9aa9 100644 --- a/tests/technical/test_sources_service.py +++ b/tests/technical/test_sources_service.py @@ -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() @@ -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()