Skip to content

Commit

Permalink
🔥 Tear down AtomicalsCoin to AtomicalsCoinMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed May 12, 2024
1 parent 5419c7d commit ad2d510
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 39 deletions.
23 changes: 3 additions & 20 deletions electrumx/lib/coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,30 +283,13 @@ def bucket_estimatefee_block_target(cls, n: int) -> int:
return n


class AtomicalsCoin(Coin):
class AtomicalsCoinMixin:
ATOMICALS_ACTIVATION_HEIGHT: int
ATOMICALS_ACTIVATION_HEIGHT_DMINT: int
ATOMICALS_ACTIVATION_HEIGHT_COMMITZ: int
ATOMICALS_ACTIVATION_HEIGHT_DENSITY: int
ATOMICALS_ACTIVATION_HEIGHT_DFT_BITWORK_ROLLOVER: int

@classmethod
def lookup_coin_class(cls, name, net):
"""Return a coin class given name and network.
Raise an exception if unrecognised."""
req_attrs = ('TX_COUNT', 'TX_COUNT_HEIGHT', 'TX_PER_BLOCK')
for coin in util.subclasses(AtomicalsCoin):
if coin.NAME.lower() == name.lower() and coin.NET.lower() == net.lower():
missing = [
attr
for attr in req_attrs
if not hasattr(coin, attr)
]
if missing:
raise CoinError(f'coin {name} missing {missing} attributes')
return coin
raise CoinError(f'unknown coin {name} and network {net} combination')


class AuxPowMixin:
STATIC_BLOCK_HEADERS = False
Expand Down Expand Up @@ -654,7 +637,7 @@ def warn_old_client_on_tx_broadcast(cls, client_ver):
return False


class Bitcoin(BitcoinMixin, AtomicalsCoin):
class Bitcoin(BitcoinMixin, AtomicalsCoinMixin, Coin):
NAME = "Bitcoin"
DESERIALIZER = lib_tx.DeserializerSegWit
MEMPOOL_HISTOGRAM_REFRESH_SECS = 120
Expand Down Expand Up @@ -938,7 +921,7 @@ class BitcoinSVRegtest(BitcoinSVTestnet):
GENESIS_ACTIVATION = 10_000


class BitcoinTestnet(BitcoinTestnetMixin, AtomicalsCoin):
class BitcoinTestnet(BitcoinTestnetMixin, AtomicalsCoinMixin, Coin):
'''Bitcoin Testnet for Core bitcoind >= 0.13.1.'''
NAME = "Bitcoin"
DESERIALIZER = lib_tx.DeserializerSegWit
Expand Down
6 changes: 3 additions & 3 deletions electrumx/server/block_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import asyncio
import os
import time
from typing import Sequence, Tuple, List, Callable, Optional, TYPE_CHECKING, Type
from typing import Sequence, Tuple, List, Callable, Optional, TYPE_CHECKING, Type, Union

from aiorpcx import run_in_thread, CancelledError

Expand Down Expand Up @@ -78,7 +78,7 @@
import copy

if TYPE_CHECKING:
from electrumx.lib.coins import AtomicalsCoin
from electrumx.lib.coins import Coin, AtomicalsCoinMixin
from electrumx.server.env import Env
from electrumx.server.controller import Notifications

Expand All @@ -101,7 +101,7 @@ class Prefetcher:
def __init__(
self,
daemon: 'Daemon',
coin: Type['AtomicalsCoin'],
coin: Type[Union['Coin', 'AtomicalsCoinMixin']],
blocks_event: asyncio.Event,
*,
polling_delay_secs,
Expand Down
8 changes: 5 additions & 3 deletions electrumx/server/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import time
from calendar import timegm
from struct import pack
from typing import TYPE_CHECKING, Type
from typing import TYPE_CHECKING, Type, Union

import aiohttp
from aiorpcx import JSONRPC
Expand All @@ -25,7 +25,7 @@
unpack_le_uint16_from)

if TYPE_CHECKING:
from electrumx.lib.coins import AtomicalsCoin
from electrumx.lib.coins import Coin, AtomicalsCoinMixin


class DaemonError(Exception):
Expand All @@ -48,7 +48,7 @@ class Daemon:

def __init__(
self,
coin: Type['AtomicalsCoin'],
coin: Type[Union['Coin', 'AtomicalsCoinMixin']],
url,
*,
max_workqueue=10,
Expand Down Expand Up @@ -307,12 +307,14 @@ async def height(self):
'''Query the daemon for its current height.'''
self._height = await self._send_single('getblockcount')
return self._height
# return self.coin.ATOMICALS_ACTIVATION_HEIGHT - 1

def cached_height(self):
'''Return the cached daemon height.
If the daemon has not been queried yet this returns None.'''
return self._height
# return self.coin.ATOMICALS_ACTIVATION_HEIGHT - 1


class DashDaemon(Daemon):
Expand Down
10 changes: 5 additions & 5 deletions electrumx/server/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

import re
from ipaddress import IPv4Address, IPv6Address
from typing import Type
from typing import Type, Union

from aiorpcx import Service, ServicePart
from electrumx.lib.coins import AtomicalsCoin
from electrumx.lib.coins import Coin, AtomicalsCoinMixin
from electrumx.lib.env_base import EnvBase


Expand All @@ -32,7 +32,7 @@ class Env(EnvBase):
SSL_PROTOCOLS = {'ssl', 'wss'}
KNOWN_PROTOCOLS = {'ssl', 'tcp', 'ws', 'wss', 'rpc', 'http'}

coin: Type[AtomicalsCoin]
coin: Type[Union['Coin', 'AtomicalsCoinMixin']]

def __init__(self, coin=None):
super().__init__()
Expand All @@ -47,12 +47,12 @@ def __init__(self, coin=None):
self.daemon_url = self.required('DAEMON_URL')
self.daemon_proxy_url = self.default('DAEMON_PROXY_URL', None)
if coin is not None:
assert issubclass(coin, AtomicalsCoin)
assert issubclass(coin, Coin)
self.coin = coin
else:
coin_name = self.required('COIN').strip()
network = self.default('NET', 'mainnet').strip()
self.coin = AtomicalsCoin.lookup_coin_class(coin_name, network)
self.coin = Coin.lookup_coin_class(coin_name, network)

# Peer discovery

Expand Down
6 changes: 3 additions & 3 deletions electrumx/server/mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from abc import ABC, abstractmethod
from asyncio import Lock
from collections import defaultdict
from typing import Sequence, Tuple, TYPE_CHECKING, Type, Dict
from typing import Sequence, Tuple, TYPE_CHECKING, Type, Dict, Union
import math

import attr
Expand All @@ -27,7 +27,7 @@
from electrumx.lib.hash import hash_to_hex_str, HASHX_LEN, double_sha256

if TYPE_CHECKING:
from electrumx.lib.coins import AtomicalsCoin
from electrumx.lib.coins import Coin, AtomicalsCoinMixin


@attr.s(slots=True)
Expand Down Expand Up @@ -112,7 +112,7 @@ class MemPool:

def __init__(
self,
coin: Type['AtomicalsCoin'],
coin: Type[Union['Coin', 'AtomicalsCoinMixin']],
api: MemPoolAPI,
*,
refresh_secs=5.0,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

import pytest

from electrumx.lib.coins import AtomicalsCoin
from electrumx.lib.coins import Coin
from electrumx.lib.hash import hex_str_to_hash

BLOCKS_DIR = os.path.join(
Expand All @@ -43,7 +43,7 @@
for name in os.listdir(BLOCKS_DIR):
try:
name_parts = name.split("_")
coin = AtomicalsCoin.lookup_coin_class(name_parts[0], name_parts[1])
coin = Coin.lookup_coin_class(name_parts[0], name_parts[1])
with open(os.path.join(BLOCKS_DIR, name)) as f:
blocks.append((coin, json.load(f)))
except Exception as e:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import pytest

from electrumx.lib.coins import AtomicalsCoin, Namecoin
from electrumx.lib.coins import Coin, Namecoin
from electrumx.lib.hash import hash_to_hex_str
from electrumx.lib.script import OpCodes, Script

Expand All @@ -25,7 +25,7 @@
for name in os.listdir(TRANSACTION_DIR):
try:
name_parts = name.split("_")
coinFound = AtomicalsCoin.lookup_coin_class(name_parts[0], name_parts[1])
coinFound = Coin.lookup_coin_class(name_parts[0], name_parts[1])
with open(os.path.join(TRANSACTION_DIR, name)) as f:
transactions.append((coinFound, json.load(f)))
except Exception as e:
Expand Down Expand Up @@ -74,4 +74,4 @@ def test_transaction(transaction_details):
normalized_name_op_script.append(OpCodes.OP_2DROP)
normalized_name_op_script.append(OpCodes.OP_DROP)
normalized_name_op_script.append(OpCodes.OP_RETURN)
assert coin.name_hashX_from_script(tx_pks) == AtomicalsCoin.hashX_from_script(normalized_name_op_script)
assert coin.name_hashX_from_script(tx_pks) == Coin.hashX_from_script(normalized_name_op_script)

0 comments on commit ad2d510

Please sign in to comment.