Skip to content

Commit

Permalink
some adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
jansegre committed May 24, 2023
1 parent ffc704c commit fec1d60
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
36 changes: 19 additions & 17 deletions hathor/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,8 @@ def on_new_tx(self, tx: BaseTransaction, *, conn: Optional[HathorProtocol] = Non
tx.storage = self.tx_storage

try:
metadata = tx.get_metadata()
with self.tx_storage.allow_partially_validated_context():
metadata = tx.get_metadata()
except TransactionDoesNotExist:
if not fails_silently:
raise InvalidNewTransaction('missing parent')
Expand Down Expand Up @@ -1054,23 +1055,24 @@ def on_new_tx(self, tx: BaseTransaction, *, conn: Optional[HathorProtocol] = Non
self.log_new_object(tx, 'new {} partially accepted while syncing checkpoints', quiet=quiet)
else:
assert self.tx_storage.indexes.deps is not None
if isinstance(tx, Block) and not tx.has_basic_block_parent():
if not fails_silently:
raise InvalidNewTransaction('block parent needs to be at least basic-valid')
self.log.warn('on_new_tx(): block parent needs to be at least basic-valid', tx=tx.hash_hex)
return False
if not tx.validate_basic():
if not fails_silently:
raise InvalidNewTransaction('basic validation failed')
self.log.warn('on_new_tx(): basic validation failed', tx=tx.hash_hex)
return False
with self.tx_storage.allow_partially_validated_context():
if isinstance(tx, Block) and not tx.has_basic_block_parent():
if not fails_silently:
raise InvalidNewTransaction('block parent needs to be at least basic-valid')
self.log.warn('on_new_tx(): block parent needs to be at least basic-valid', tx=tx.hash_hex)
return False
if not tx.validate_basic():
if not fails_silently:
raise InvalidNewTransaction('basic validation failed')
self.log.warn('on_new_tx(): basic validation failed', tx=tx.hash_hex)
return False

# The method below adds the tx as a child of the parents
# This needs to be called right before the save because we were adding the children
# in the tx parents even if the tx was invalid (failing the verifications above)
# then I would have a children that was not in the storage
self.tx_storage.save_transaction(tx)
self.tx_storage.indexes.deps.add_tx(tx)
# The method below adds the tx as a child of the parents
# This needs to be called right before the save because we were adding the children
# in the tx parents even if the tx was invalid (failing the verifications above)
# then I would have a children that was not in the storage
self.tx_storage.save_transaction(tx)
self.tx_storage.indexes.deps.add_tx(tx)
self.log_new_object(tx, 'new {} partially accepted', quiet=quiet)

if self.tx_storage.indexes.deps is not None:
Expand Down
5 changes: 3 additions & 2 deletions hathor/p2p/node_sync_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,9 @@ def run_sync_transactions(self) -> None:
if block is None:
block = BLOCK_GENESIS
else:
while not block.get_metadata().validation.is_valid():
block = block.get_block_parent()
with self.tx_storage.allow_partially_validated_context():
while not block.get_metadata().validation.is_valid():
block = block.get_block_parent()
assert block.hash is not None
block_hash = block.hash
block_height = block.get_metadata().get_soft_height()
Expand Down
8 changes: 8 additions & 0 deletions tests/p2p/test_sync_enabled.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ class SyncV1RandomSimulatorTestCase(unittest.SyncV1Params, BaseRandomSimulatorTe
class SyncV2RandomSimulatorTestCase(unittest.SyncV2Params, BaseRandomSimulatorTestCase):
__test__ = True

# XXX: disabling this for sync-v2 since sync enable/disable is not implemented
def test_new_node_disabled(self):
pass

# XXX: disabling this for sync-v2 since sync enable/disable is not implemented
def test_sync_rotate(self):
pass


# sync-bridge should behave like sync-v2
class SyncBridgeRandomSimulatorTestCase(unittest.SyncBridgeParams, SyncV2RandomSimulatorTestCase):
Expand Down
4 changes: 4 additions & 0 deletions tests/p2p/test_sync_rate_limiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class SyncV1RandomSimulatorTestCase(unittest.SyncV1Params, BaseRandomSimulatorTe
class SyncV2RandomSimulatorTestCase(unittest.SyncV2Params, BaseRandomSimulatorTestCase):
__test__ = True

# XXX: disable because sync-v2 does not have send_tips
def test_sync_rate_limiter(self):
pass


# sync-bridge should behave like sync-v2
class SyncBridgeRandomSimulatorTestCase(unittest.SyncBridgeParams, SyncV2RandomSimulatorTestCase):
Expand Down
6 changes: 4 additions & 2 deletions tests/resources/transaction/test_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from hathor.transaction import Transaction
from hathor.transaction.resources import TransactionResource
from hathor.transaction.token_creation_tx import TokenCreationTransaction
from hathor.transaction.transaction_metadata import ValidationState
from tests import unittest
from tests.resources.base_resource import StubSite, _BaseResourceTest
from tests.utils import add_blocks_unlock_reward, add_new_blocks, add_new_transactions
Expand Down Expand Up @@ -513,8 +514,9 @@ def test_partially_validated_not_found(self):
'2dc703120a77192fc16eda9ed22e1b88ac40200000218def416095b08602003d3c40fb04737e1a2a848cfd2592490a71cd'
'0248b9e7d6a626f45dec86975b00f4dd53f84f1f0091125250b044e49023fbbd0f74f6093cdd2226fdff3e09a1000002be')
tx = Transaction.create_from_struct(bytes.fromhex(tx_hex), self.manager.tx_storage)
tx.mark_partially_validated()
self.manager.tx_storage.save_transaction(tx)
tx.set_validation(ValidationState.BASIC)
with self.manager.tx_storage.allow_partially_validated_context():
self.manager.tx_storage.save_transaction(tx)

response = yield self.web.get("transaction", {b'id': bytes(tx.hash_hex, 'utf-8')})
data = response.json_value()
Expand Down

0 comments on commit fec1d60

Please sign in to comment.