Skip to content

Commit

Permalink
Log all raw data fetched from the ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
ptzianos committed Sep 2, 2019
1 parent 9f344a0 commit 9226bac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
10 changes: 6 additions & 4 deletions carbon-ledger/src/carbon/ledger/connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class LedgerConnector(ABC):
def __init__(self, protocol: ProtocolParser):
self._protocol = protocol

def fetch(self, address: str) -> Block:
def fetch(self, address: str, log: Logger) -> Block:
raise NotImplemented()


Expand All @@ -50,7 +50,7 @@ def __init__(self, node_address: str = 'https://nodes.thetangle.org',
super().__init__(*args, **kwargs)
self._iota_api = Iota(node_address)

def fetch(self, address: str) -> Block:
def fetch(self, address: str, log: Logger) -> Block:
"""Fetches a block from IOTA with an address.
Since IOTA does not support exactly blocks, what is fetched is all the
Expand All @@ -74,7 +74,8 @@ def get_signature_string(t: 'Transaction') -> str:
raise IOTAConnector.NoDataFetched()
raw_data = ''.join(map(get_signature_string, transactions))
block = self._protocol.parse_headers(address=address,
raw_data=raw_data)
raw_data=raw_data,
log=log)
return block


Expand Down Expand Up @@ -150,7 +151,8 @@ def _fetch(self, address: str, latest: bool = False) -> None:
"""Fetch a block from the ledger and update the state of the stream."""
try:
# Fetch block from the ledger
self._address_index[address] = self._connector.fetch(address)
self._address_index[address] = self._connector.fetch(address,
self._logger)
# Add block to the registry
self._logger.info(f'Fetched block with address {address}')
except Exception as e:
Expand Down
21 changes: 14 additions & 7 deletions carbon-ledger/src/carbon/ledger/protocols.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import ABC
from datetime import datetime
from enum import Enum
from logging import Logger
from typing import List, Type, TYPE_CHECKING

if TYPE_CHECKING:
Expand Down Expand Up @@ -28,35 +29,41 @@ class InvalidData(Exception):
pass

@staticmethod
def parse_headers(address: str, raw_data: str) -> 'Block':
def parse_headers(address: str, raw_data: str, log: Logger) -> 'Block':
raise NotImplemented()

@staticmethod
def parse_data(block: 'Block') -> List['Packet']:
def parse_data(block: 'Block', log: Logger) -> List['Packet']:
raise NotImplemented()


class HermesPlaintextParser(ProtocolParser):
@staticmethod
def parse_headers(address: str, raw_data: str) -> 'Block':
def parse_headers(address: str, raw_data: str, log: Logger) -> 'Block':
from carbon.ledger.connectors import Block
fields = (raw_data
.replace('next_address:', '')
.replace('previous_address:', '')
.split('::')) # type: List[str]
log.debug(f'Header of block with address {address} '
f'is : {"::".join(fields[:3])}')
if len(fields) < 4:
raise ProtocolParser.InvalidData()
return Block(address=address, next_link=fields[1], previous_link=fields[2],
data={'samples': fields[3:]}, metadata={'digest': fields[0]})
return Block(address=address,
next_link=fields[1],
previous_link=fields[2],
data={'samples': fields[3:]},
metadata={'digest': fields[0]})

@staticmethod
def parse_data(block: 'Block') -> None:
def parse_data(block: 'Block', log: Logger) -> None:
from carbon.ledger.data import Packet
for sample in block.data['samples']:
tags, timestamp, data = sample.split(' ')
tags = tags.split(';')
block.samples.append(Packet(sample, tags[0], tags[1:],
epoch_to_datetime(timestamp), data, block))
epoch_to_datetime(timestamp),
data, block))


def get_protocol_parser(protocol_id: str) -> Type[ProtocolParser]:
Expand Down

0 comments on commit 9226bac

Please sign in to comment.