Skip to content

Commit

Permalink
synchronizer: disconnect from server if cannot deserialize txn
Browse files Browse the repository at this point in the history
  • Loading branch information
SomberNight authored and wakiyamap committed Feb 16, 2019
1 parent fdccbaa commit c67a436
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
22 changes: 13 additions & 9 deletions electrum_mona/synchronizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
from .address_synchronizer import AddressSynchronizer


class SynchronizerFailure(Exception): pass


def history_status(h):
if not h:
return None
Expand Down Expand Up @@ -194,18 +197,19 @@ async def _get_transaction(self, tx_hash, *, allow_server_not_finding_tx=False):
raise
tx = Transaction(result)
try:
tx.deserialize()
except Exception:
self.print_msg("cannot deserialize transaction, skipping", tx_hash)
return
tx.deserialize() # see if raises
except Exception as e:
# possible scenarios:
# 1: server is sending garbage
# 2: there is a bug in the deserialization code
# 3: there was a segwit-like upgrade that changed the tx structure
# that we don't know about
raise SynchronizerFailure(f"cannot deserialize transaction {tx_hash}") from e
if tx_hash != tx.txid():
self.print_error("received tx does not match expected txid ({} != {})"
.format(tx_hash, tx.txid()))
return
raise SynchronizerFailure(f"received tx does not match expected txid ({tx_hash} != {tx.txid()})")
tx_height = self.requested_tx.pop(tx_hash)
self.wallet.receive_tx_callback(tx_hash, tx, tx_height)
self.print_error("received tx %s height: %d bytes: %d" %
(tx_hash, tx_height, len(tx.raw)))
self.print_error(f"received tx {tx_hash} height: {tx_height} bytes: {len(tx.raw)}")
# callbacks
self.wallet.network.trigger_callback('new_transaction', self.wallet, tx)

Expand Down
8 changes: 4 additions & 4 deletions electrum_mona/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def read_bytes(self, length):
self.read_cursor += length
return result
except IndexError:
raise SerializationError("attempt to read past end of buffer")
raise SerializationError("attempt to read past end of buffer") from None

def can_read_more(self) -> bool:
if not self.input:
Expand Down Expand Up @@ -159,8 +159,8 @@ def read_compact_size(self):
elif size == 255:
size = self._read_num('<Q')
return size
except IndexError:
raise SerializationError("attempt to read past end of buffer")
except IndexError as e:
raise SerializationError("attempt to read past end of buffer") from e

def write_compact_size(self, size):
if size < 0:
Expand All @@ -182,7 +182,7 @@ def _read_num(self, format):
(i,) = struct.unpack_from(format, self.input, self.read_cursor)
self.read_cursor += struct.calcsize(format)
except Exception as e:
raise SerializationError(e)
raise SerializationError(e) from e
return i

def _write_num(self, format, num):
Expand Down

0 comments on commit c67a436

Please sign in to comment.