Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): Add load_from_logs cli command to load blocks and transactions as they are found in a log dump #1034

Merged
merged 1 commit into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions hathor/cli/load_from_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2024 Hathor Labs
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re
import sys
from argparse import ArgumentParser, FileType

from hathor.cli.run_node import RunNode


class LoadFromLogs(RunNode):
def start_manager(self) -> None:
pass

def register_signal_handlers(self) -> None:
pass

@classmethod
def create_parser(cls) -> ArgumentParser:
parser = super().create_parser()
parser.add_argument('--log-dump', type=FileType('r', encoding='UTF-8'), default=sys.stdin, nargs='?',
help='Where to read logs from, defaults to stdin.')
return parser

def prepare(self, *, register_resources: bool = True) -> None:
super().prepare(register_resources=False)

def run(self) -> None:
from hathor.transaction.base_transaction import tx_or_block_from_bytes

pattern = r'new (tx|block) .*bytes=([^ ]*) '
pattern = r'new (tx|block) .*bytes=([^ ]*) '
compiled_pattern = re.compile(pattern)

while True:
line_with_break = self._args.log_dump.readline()
if not line_with_break:
break
line = line_with_break.strip()

matches = compiled_pattern.findall(line)
if len(matches) == 0:
continue

assert len(matches) == 1
_, vertex_bytes_hex = matches[0]

vertex_bytes = bytes.fromhex(vertex_bytes_hex)
vertex = tx_or_block_from_bytes(vertex_bytes)
self.manager.on_new_tx(vertex)


def main():
LoadFromLogs().run()
2 changes: 2 additions & 0 deletions hathor/cli/main.py
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ def __init__(self) -> None:
db_export,
db_import,
generate_valid_words,
load_from_logs,
merged_mining,
mining,
multisig_address,
@@ -91,6 +92,7 @@ def __init__(self) -> None:
self.add_cmd('dev', 'x-export', db_export, 'EXPERIMENTAL: Export database to a simple format.')
self.add_cmd('dev', 'x-import', db_import, 'EXPERIMENTAL: Import database from exported format.')
self.add_cmd('dev', 'replay-logs', replay_logs, 'EXPERIMENTAL: re-play json logs as console printted')
self.add_cmd('dev', 'load-from-logs', load_from_logs, 'Load vertices as they are found in a log dump')

def add_cmd(self, group: str, cmd: str, module: ModuleType, short_description: Optional[str] = None) -> None:
self.command_list[cmd] = module