Mercurial > public > bitcaviar-plus
changeset 19:905b6fdc5e1b
handle invalid magic bytes
author | Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com> |
---|---|
date | Wed, 10 Nov 2021 11:37:44 +0100 |
parents | d1f527020d54 |
children | b3eb78c5d03b |
files | Dockerfile README.md src/bitcaviar_plus/block.py src/bitcaviar_plus/errors.py tests/test_app.py |
diffstat | 5 files changed, 50 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Dockerfile Wed Nov 10 11:37:44 2021 +0100 @@ -0,0 +1,16 @@ +# syntax=docker/dockerfile:1 + +# Install Python image +FROM python:3.8-slim-buster + +# Create working directory and install dependencies +WORKDIR /app +COPY requirements.txt requirements.txt +RUN pip3 install -r requirements.txt + +# Copy files +COPY src/bitcaviar_plus bitcaviar_plus/ +COPY tests/test_app.py . + +# Run script +CMD ["python3", "-u", "test_app.py"] \ No newline at end of file
--- a/README.md Thu Nov 04 10:31:29 2021 +0100 +++ b/README.md Wed Nov 10 11:37:44 2021 +0100 @@ -1,3 +1,4 @@ +<!--suppress HtmlDeprecatedAttribute, HtmlRequiredAltAttribute --> <p align="center"> <img src="https://user-images.githubusercontent.com/66180929/136657868-0ec6971b-ed76-43f3-9689-f643e8893706.png" /> </p>
--- a/src/bitcaviar_plus/block.py Thu Nov 04 10:31:29 2021 +0100 +++ b/src/bitcaviar_plus/block.py Wed Nov 10 11:37:44 2021 +0100 @@ -1,6 +1,7 @@ from bitcaviar_plus.block_structure import * from bitcaviar_plus.helpers import __get_var_int from bitcaviar_plus.helpers import __compute_hash +from bitcaviar_plus.errors import InvalidMagicBytes """ Deserialize methods @@ -16,6 +17,10 @@ block = Block() block.magic_number = f.read(4).hex() + + if block.magic_number != 'f9beb4d9': + raise InvalidMagicBytes(block.magic_number) + block.size = f.read(4)[::-1].hex() block_header, block.id = __deserialize_header(f) block.transaction_count = __get_var_int(f)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/bitcaviar_plus/errors.py Wed Nov 10 11:37:44 2021 +0100 @@ -0,0 +1,9 @@ +# noinspection PyCompatibility +class InvalidMagicBytes(Exception): + """ + Exception when magic bytes are different from 'f9beb4d9' + """ + + def __init__(self, magic_bytes): + self.message = 'Invalid magic bytes: {}'.format(magic_bytes) + super().__init__(self.message)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_app.py Wed Nov 10 11:37:44 2021 +0100 @@ -0,0 +1,19 @@ +import os +from bitcaviar_plus.block import deserialize_block +from bitcaviar_plus.errors import InvalidMagicBytes + + +def parse_genesis_block(): + blk_path = '/bitcoin-node/.bitcoin/blocks/blk00355.dat' + + with open(blk_path, 'rb') as f: + file_size = os.path.getsize(blk_path) + while f.tell() < file_size: + try: + block = deserialize_block(f) + except InvalidMagicBytes as e: + print(e) + + +if __name__ == '__main__': + parse_genesis_block()