|
| 1 | +# Copyright 2024 Hathor Labs |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import re |
| 16 | +import sys |
| 17 | +from argparse import ArgumentParser, FileType |
| 18 | + |
| 19 | +from hathor.cli.run_node import RunNode |
| 20 | + |
| 21 | + |
| 22 | +class RunFromLogs(RunNode): |
| 23 | + def start_manager(self) -> None: |
| 24 | + pass |
| 25 | + |
| 26 | + def register_signal_handlers(self) -> None: |
| 27 | + pass |
| 28 | + |
| 29 | + @classmethod |
| 30 | + def create_parser(cls) -> ArgumentParser: |
| 31 | + parser = super().create_parser() |
| 32 | + parser.add_argument('--log-input', type=FileType('r', encoding='UTF-8'), default=sys.stdin, nargs='?', |
| 33 | + help='Where to read logs from, defaults to stdin.') |
| 34 | + return parser |
| 35 | + |
| 36 | + def prepare(self, *, register_resources: bool = True) -> None: |
| 37 | + super().prepare(register_resources=False) |
| 38 | + |
| 39 | + def run(self) -> None: |
| 40 | + from hathor.transaction.base_transaction import tx_or_block_from_bytes |
| 41 | + |
| 42 | + pattern = r'new (tx|block) .*bytes=([^ ]*) ' |
| 43 | + pattern = r'new (tx|block) .*bytes=([^ ]*) ' |
| 44 | + compiled_pattern = re.compile(pattern) |
| 45 | + |
| 46 | + while True: |
| 47 | + line_with_break = self._args.log_input.readline() |
| 48 | + if not line_with_break: |
| 49 | + break |
| 50 | + line = line_with_break.strip() |
| 51 | + |
| 52 | + matches = compiled_pattern.findall(line) |
| 53 | + if len(matches) == 0: |
| 54 | + continue |
| 55 | + |
| 56 | + assert len(matches) == 1 |
| 57 | + _, vertex_bytes_hex = matches[0] |
| 58 | + |
| 59 | + vertex_bytes = bytes.fromhex(vertex_bytes_hex) |
| 60 | + vertex = tx_or_block_from_bytes(vertex_bytes) |
| 61 | + self.manager.on_new_tx(vertex) |
| 62 | + |
| 63 | + |
| 64 | +def main(): |
| 65 | + RunFromLogs().run() |
0 commit comments