Refactor get_matches and filter_valid_matches to not be static

This commit is contained in:
Sergi Delgado Segura
2019-11-08 18:21:42 +00:00
parent e65b02c473
commit 4233c9d5cc

View File

@@ -12,7 +12,6 @@ from cryptography.hazmat.primitives.serialization import load_pem_private_key
from pisa.logger import Logger from pisa.logger import Logger
from pisa.cleaner import Cleaner from pisa.cleaner import Cleaner
from pisa.responder import Responder from pisa.responder import Responder
from pisa.appointment import Appointment
from pisa.cryptographer import Cryptographer from pisa.cryptographer import Cryptographer
from pisa.block_processor import BlockProcessor from pisa.block_processor import BlockProcessor
from pisa.utils.zmq_subscriber import ZMQHandler from pisa.utils.zmq_subscriber import ZMQHandler
@@ -41,6 +40,10 @@ class Watcher:
secret_key_pem = key_file.read().encode("utf-8") secret_key_pem = key_file.read().encode("utf-8")
self.signing_key = load_pem_private_key(secret_key_pem, password=None, backend=default_backend()) self.signing_key = load_pem_private_key(secret_key_pem, password=None, backend=default_backend())
@staticmethod
def compute_locator(tx_id):
return sha256(unhexlify(tx_id)).hexdigest()
def sign_appointment(self, appointment): def sign_appointment(self, appointment):
data = appointment.serialize() data = appointment.serialize()
return self.signing_key.sign(data, ec.ECDSA(hashes.SHA256())) return self.signing_key.sign(data, ec.ECDSA(hashes.SHA256()))
@@ -119,8 +122,7 @@ class Watcher:
expired_appointments, self.appointments, self.locator_uuid_map, self.db_manager expired_appointments, self.appointments, self.locator_uuid_map, self.db_manager
) )
matches = self.get_matches(txids, self.locator_uuid_map) filtered_matches = self.filter_valid_matches(self.get_matches(txids))
filtered_matches = self.filter_valid_matches(matches, self.locator_uuid_map, self.appointments)
for uuid, filtered_match in filtered_matches.items(): for uuid, filtered_match in filtered_matches.items():
# Errors decrypting the Blob will result in a None justice_txid # Errors decrypting the Blob will result in a None justice_txid
@@ -156,17 +158,11 @@ class Watcher:
logger.info("No more pending appointments, going back to sleep") logger.info("No more pending appointments, going back to sleep")
@staticmethod def get_matches(self, txids):
def compute_locator(tx_id):
return sha256(unhexlify(tx_id)).hexdigest()
# DISCUSS: 36-who-should-check-appointment-trigger
@staticmethod
def get_matches(txids, locator_uuid_map):
potential_locators = {Watcher.compute_locator(txid): txid for txid in txids} potential_locators = {Watcher.compute_locator(txid): txid for txid in txids}
# Check is any of the tx_ids in the received block is an actual match # Check is any of the tx_ids in the received block is an actual match
intersection = set(locator_uuid_map.keys()).intersection(potential_locators.keys()) intersection = set(self.locator_uuid_map.keys()).intersection(potential_locators.keys())
matches = {locator: potential_locators[locator] for locator in intersection} matches = {locator: potential_locators[locator] for locator in intersection}
if len(matches) > 0: if len(matches) > 0:
@@ -177,18 +173,16 @@ class Watcher:
return matches return matches
@staticmethod def filter_valid_matches(self, matches):
# NOTCOVERED
def filter_valid_matches(matches, locator_uuid_map, appointments):
filtered_matches = {} filtered_matches = {}
for locator, dispute_txid in matches.items(): for locator, dispute_txid in matches.items():
for uuid in locator_uuid_map[locator]: for uuid in self.locator_uuid_map[locator]:
justice_rawtx = Cryptographer.decrypt(appointments[uuid].encrypted_blob, dispute_txid) justice_rawtx = Cryptographer.decrypt(self.appointments[uuid].encrypted_blob, dispute_txid)
justice_tx = BlockProcessor.decode_raw_transaction(justice_rawtx) justice_tx = BlockProcessor.decode_raw_transaction(justice_rawtx)
if justice_rawtx is not None: if justice_tx is not None:
justice_txid = justice_tx.get("txid") justice_txid = justice_tx.get("txid")
valid_match = True valid_match = True