From 37c3c9566fbdd2cfbc3d0d3959da9470a5e6ecc4 Mon Sep 17 00:00:00 2001 From: Moul <moul@moul.re> Date: Fri, 19 Jun 2020 16:54:51 +0200 Subject: [PATCH] [mod] Move HeadBlock() to blockchain_tools.py --- silkaj/blockchain_tools.py | 16 ++++++++++++++++ silkaj/cert.py | 4 ++-- silkaj/commands.py | 2 +- silkaj/money.py | 3 ++- silkaj/network_tools.py | 18 +----------------- silkaj/tx.py | 3 ++- tests/test_unit_tx.py | 4 +++- 7 files changed, 27 insertions(+), 23 deletions(-) diff --git a/silkaj/blockchain_tools.py b/silkaj/blockchain_tools.py index 032504a2..9321e0ab 100644 --- a/silkaj/blockchain_tools.py +++ b/silkaj/blockchain_tools.py @@ -33,3 +33,19 @@ class BlockchainParams(object): async def get_params(self): client = ClientInstance().client return await client(blockchain.parameters) + + +class HeadBlock(object): + __instance = None + + def __new__(cls): + if HeadBlock.__instance is None: + HeadBlock.__instance = object.__new__(cls) + return HeadBlock.__instance + + def __init__(self): + self.head_block = self.get_head() + + async def get_head(self): + client = ClientInstance().client + return await client(blockchain.current) diff --git a/silkaj/cert.py b/silkaj/cert.py index fc5682b4..a5a09589 100644 --- a/silkaj/cert.py +++ b/silkaj/cert.py @@ -25,8 +25,8 @@ from duniterpy.documents import BlockUID, block_uid, Identity, Certification from silkaj.auth import auth_method from silkaj.tools import message_exit, coroutine from silkaj.tui import convert_time -from silkaj.network_tools import ClientInstance, HeadBlock -from silkaj.blockchain_tools import BlockchainParams +from silkaj.network_tools import ClientInstance +from silkaj.blockchain_tools import BlockchainParams, HeadBlock from silkaj.license import license_approval from silkaj.wot import is_member, get_informations_for_identity from silkaj.constants import SUCCESS_EXIT_STATUS diff --git a/silkaj/commands.py b/silkaj/commands.py index 6de07d44..fdaa8717 100644 --- a/silkaj/commands.py +++ b/silkaj/commands.py @@ -33,8 +33,8 @@ from silkaj.network_tools import ( best_endpoint_address, EndPoint, ClientInstance, - HeadBlock, ) +from silkaj.blockchain_tools import HeadBlock from silkaj.tools import CurrencySymbol from silkaj.tui import convert_time from silkaj.constants import ASYNC_SLEEP diff --git a/silkaj/money.py b/silkaj/money.py index 57088b29..f9540b6d 100644 --- a/silkaj/money.py +++ b/silkaj/money.py @@ -18,7 +18,8 @@ along with Silkaj. If not, see <https://www.gnu.org/licenses/>. from click import command, argument, pass_context, echo from tabulate import tabulate -from silkaj.network_tools import ClientInstance, HeadBlock +from silkaj.network_tools import ClientInstance +from silkaj.blockchain_tools import HeadBlock from silkaj.tools import CurrencySymbol, message_exit, coroutine from silkaj.auth import auth_method from silkaj.wot import check_public_key diff --git a/silkaj/network_tools.py b/silkaj/network_tools.py index 8b014be5..89701d5e 100644 --- a/silkaj/network_tools.py +++ b/silkaj/network_tools.py @@ -22,7 +22,7 @@ import logging from sys import exit, stderr from asyncio import sleep from duniterpy.api.client import Client -from duniterpy.api.bma import blockchain, network +from duniterpy.api.bma import network from silkaj.constants import ( G1_DEFAULT_ENDPOINT, @@ -234,19 +234,3 @@ def check_port(port): print("Wrong port number", file=stderr) return False return True - - -class HeadBlock(object): - __instance = None - - def __new__(cls): - if HeadBlock.__instance is None: - HeadBlock.__instance = object.__new__(cls) - return HeadBlock.__instance - - def __init__(self): - self.head_block = self.get_head() - - async def get_head(self): - client = ClientInstance().client - return await client(blockchain.current) diff --git a/silkaj/tx.py b/silkaj/tx.py index 30636665..84f6ba5e 100644 --- a/silkaj/tx.py +++ b/silkaj/tx.py @@ -22,7 +22,8 @@ from tabulate import tabulate from click import command, option, FloatRange from silkaj.cli_tools import MutuallyExclusiveOption -from silkaj.network_tools import ClientInstance, HeadBlock +from silkaj.network_tools import ClientInstance +from silkaj.blockchain_tools import HeadBlock from silkaj.crypto_tools import check_public_key from silkaj.tools import message_exit, CurrencySymbol, coroutine from silkaj.auth import auth_method diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py index 11df8bf8..46f6ce74 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -447,7 +447,9 @@ async def test_generate_transaction_document( monkeypatch, ): # patch Head_block - monkeypatch.setattr("silkaj.network_tools.HeadBlock.get_head", patched.head_block) + monkeypatch.setattr( + "silkaj.blockchain_tools.HeadBlock.get_head", patched.head_block + ) assert result == await generate_transaction_document( issuers, -- GitLab