From a235a6b38bd83553c25c05312015283be1522c60 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Mon, 21 Aug 2017 13:22:35 +0200 Subject: [PATCH] all: docs, logging --- src/protobuf.py | 18 ++++++++++++++++++ src/trezor/loop.py | 9 ++++++--- src/trezor/wire/__init__.py | 8 ++++++++ src/trezor/wire/codec_v1.py | 2 +- src/trezor/wire/codec_v2.py | 3 +++ 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/protobuf.py b/src/protobuf.py index 7c0d166323..8174874954 100644 --- a/src/protobuf.py +++ b/src/protobuf.py @@ -1,6 +1,24 @@ ''' Extremely minimal streaming codec for a subset of protobuf. Supports uint32, bytes, string, embedded message and repeated fields. + +For de-sererializing (loading) protobuf types, object with `AsyncReader` +interface is required: + + class AsyncReader: + async def areadinto(self, buffer): + """ + Reads `len(buffer)` bytes into `buffer`, or raises `EOFError`. + """ + +For serializing (dumping) protobuf types, object with `AsyncWriter` interface is +required: + + class AsyncWriter: + async def awrite(self, buffer): + """ + Writes all bytes from `buffer`, or raises `EOFError`. + """ ''' from micropython import const diff --git a/src/trezor/loop.py b/src/trezor/loop.py index 88826788a3..56be3f98a2 100644 --- a/src/trezor/loop.py +++ b/src/trezor/loop.py @@ -120,16 +120,19 @@ def _step_task(task, value): else: result = task.send(value) except StopIteration as e: - log.debug(__name__, '%s finished', task) + if __debug__: + log.debug(__name__, 'finish: %s', task) except Exception as e: - log.exception(__name__, e) + if __debug__: + log.exception(__name__, e) else: if isinstance(result, Syscall): result.handle(task) elif result is None: schedule_task(task) else: - log.error(__name__, '%s is unknown syscall', result) + if __debug__: + log.error(__name__, 'unknown syscall: %s', result) if after_step_hook: after_step_hook() diff --git a/src/trezor/wire/__init__.py b/src/trezor/wire/__init__.py index 7c5785136e..d39ce22177 100644 --- a/src/trezor/wire/__init__.py +++ b/src/trezor/wire/__init__.py @@ -46,6 +46,10 @@ class Context: ''' reader = self.getreader() + if __debug__: + log.debug(__name__, '%s:%x read: %s', + self.iface.iface_num(), self.sid, types) + await reader.aopen() # wait for the message header # if we got a message with unexpected type, raise the reader via @@ -63,6 +67,10 @@ class Context: ''' writer = self.getwriter() + if __debug__: + log.debug(__name__, '%s:%x write: %s', + self.iface.iface_num(), self.sid, msg) + # get the message size counter = protobuf.CountingWriter() await protobuf.dump_message(counter, msg) diff --git a/src/trezor/wire/codec_v1.py b/src/trezor/wire/codec_v1.py index 488a81a0af..01f1553197 100644 --- a/src/trezor/wire/codec_v1.py +++ b/src/trezor/wire/codec_v1.py @@ -100,7 +100,7 @@ class Writer: self.ofs = 0 def __repr__(self): - return '' % (self.type, self.size) + return '' % (self.type, self.size) def setheader(self, mtype, msize): ''' diff --git a/src/trezor/wire/codec_v2.py b/src/trezor/wire/codec_v2.py index 31176e49b0..afe88032c0 100644 --- a/src/trezor/wire/codec_v2.py +++ b/src/trezor/wire/codec_v2.py @@ -124,6 +124,9 @@ class Writer: self.ofs = 0 self.seq = 0 + def __repr__(self): + return '' % (self.type, self.size) + def setheader(self, mtype, msize): ''' Reset the writer state and load the message header with passed type and