Use deserialized block header in info object (#1536)

* Change block info struct to hold deserialized block header

* Delete bitcoin util module
This commit is contained in:
Jonathan Zernik 2021-10-07 20:54:14 -07:00 committed by GitHub
parent 831c96d47e
commit 391fe7474c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 32 deletions

View file

@ -24,6 +24,7 @@ import logging
import os
import requests
from bitcoin.core import CBlockHeader
from squeaknode.bitcoin.bitcoin_client import BitcoinClient
from squeaknode.bitcoin.block_info import BlockInfo
@ -89,7 +90,7 @@ class BitcoinCoreClient(BitcoinClient):
logger.debug("Got block_hash: {}".format(block_hash))
return bytes.fromhex(block_hash)
def get_block_header(self, block_hash: bytes, verbose: bool = False) -> bytes:
def get_block_header(self, block_hash: bytes, verbose: bool = False) -> CBlockHeader:
payload = {
"method": "getblockheader",
"params": [block_hash.hex(), verbose],
@ -99,7 +100,8 @@ class BitcoinCoreClient(BitcoinClient):
json_response = self.make_request(payload)
result = json_response["result"]
logger.debug("Got block_header: {}".format(result))
return bytes.fromhex(result)
header_bytes = bytes.fromhex(result)
return CBlockHeader.deserialize(header_bytes)
def make_request(self, payload: dict) -> dict:
try:

View file

@ -21,9 +21,11 @@
# SOFTWARE.
from typing import NamedTuple
from bitcoin.core import CBlockHeader
class BlockInfo(NamedTuple):
"""Class for getting block info from blockchain."""
block_height: int
block_hash: bytes
block_header: bytes
block_header: CBlockHeader

View file

@ -1,26 +0,0 @@
# MIT License
#
# Copyright (c) 2020 Jonathan Zernik
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from bitcoin.core import CBlockHeader
def parse_block_header(header_bytes: bytes) -> CBlockHeader:
return CBlockHeader.deserialize(header_bytes)

View file

@ -30,7 +30,6 @@ from squeak.core import CSqueak
from squeak.core.signing import CSigningKey
from squeaknode.bitcoin.bitcoin_client import BitcoinClient
from squeaknode.bitcoin.util import parse_block_header
from squeaknode.core.exception import InvoiceSubscriptionError
from squeaknode.core.lightning_address import LightningAddressHostPort
from squeaknode.core.offer import Offer
@ -125,7 +124,7 @@ class SqueakCore:
squeak.nBlockHeight)
if squeak.hashBlock != block_info.block_hash:
raise Exception("Block hash incorrect.")
return parse_block_header(block_info.block_header)
return block_info.block_header
def get_decrypted_content(self, squeak: CSqueak, secret_key: bytes) -> str:
"""Checks if the secret key is valid for the given squeak and returns

View file

@ -21,6 +21,7 @@
# SOFTWARE.
import mock
import pytest
from bitcoin.core import CBlockHeader
from requests import HTTPError
from requests.exceptions import ConnectionError
from requests.exceptions import RequestException
@ -66,10 +67,15 @@ def block_header_str():
@pytest.fixture
def block_header(block_header_str):
def block_header_bytes(block_header_str):
yield bytes.fromhex(block_header_str)
@pytest.fixture
def block_header(block_header_bytes):
yield CBlockHeader.deserialize(block_header_bytes)
@pytest.fixture
def block_info(block_count, block_hash, block_header):
yield BlockInfo(