Skip to content

Commit

Permalink
Remove unused or redundant code.
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinTArthur committed Jun 15, 2020
1 parent c070573 commit 9ae83ca
Show file tree
Hide file tree
Showing 28 changed files with 69 additions and 193 deletions.
4 changes: 1 addition & 3 deletions contrib/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import argparse
import asyncio
import sys

from electrumx import Env
from electrumx.server.db import DB
Expand Down Expand Up @@ -106,8 +105,7 @@ def main():
parser.add_argument('scripts', nargs='*', default=[], type=str,
help='hex scripts to query')
args = parser.parse_args()
loop = asyncio.get_event_loop()
loop.run_until_complete(query(args))
asyncio.run(query(args))

if __name__ == '__main__':
main()
58 changes: 20 additions & 38 deletions electrumx/lib/coins.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from typing import Sequence

import electrumx.lib.util as util
from electrumx.lib.hash import Base58, hash160, double_sha256, hash_to_hex_str
from electrumx.lib.hash import Base58, double_sha256, hash_to_hex_str
from electrumx.lib.hash import HASHX_LEN, hex_str_to_hash
from electrumx.lib.script import (_match_ops, Script, ScriptError,
ScriptPubKey, OpCodes)
Expand All @@ -63,7 +63,7 @@ class CoinError(Exception):
'''Exception raised for coin-related errors.'''


class Coin(object):
class Coin:
'''Base class of coin hierarchy.'''

REORG_LIMIT = 200
Expand Down Expand Up @@ -173,23 +173,6 @@ def address_to_hashX(cls, address):
'''Return a hashX given a coin address.'''
return cls.hashX_from_script(cls.pay_to_address_script(address))

@classmethod
def P2PKH_address_from_hash160(cls, hash160):
'''Return a P2PKH address given a public key.'''
assert len(hash160) == 20
return cls.ENCODE_CHECK(cls.P2PKH_VERBYTE + hash160)

@classmethod
def P2PKH_address_from_pubkey(cls, pubkey):
'''Return a coin address given a public key.'''
return cls.P2PKH_address_from_hash160(hash160(pubkey))

@classmethod
def P2SH_address_from_hash160(cls, hash160):
'''Return a coin address given a hash160.'''
assert len(hash160) == 20
return cls.ENCODE_CHECK(cls.P2SH_VERBYTES[0] + hash160)

@classmethod
def hash160_to_P2PKH_script(cls, hash160):
return ScriptPubKey.P2PKH_script(hash160)
Expand Down Expand Up @@ -278,7 +261,7 @@ def warn_old_client_on_tx_broadcast(cls, _client_ver):
return False


class AuxPowMixin(object):
class AuxPowMixin:
STATIC_BLOCK_HEADERS = False
DESERIALIZER = lib_tx.DeserializerAuxPow
SESSIONCLS = AuxPoWElectrumX
Expand All @@ -300,7 +283,7 @@ def block_header(cls, block, height):
return deserializer.read_header(cls.BASIC_HEADER_SIZE)


class EquihashMixin(object):
class EquihashMixin:
STATIC_BLOCK_HEADERS = False
BASIC_HEADER_SIZE = 140 # Excluding Equihash solution
DESERIALIZER = lib_tx.DeserializerEquihash
Expand All @@ -315,7 +298,7 @@ def block_header(cls, block, height):
return deserializer.read_header(cls.BASIC_HEADER_SIZE)


class ScryptMixin(object):
class ScryptMixin:

DESERIALIZER = lib_tx.DeserializerTxTime
HEADER_HASH = None
Expand All @@ -335,7 +318,7 @@ def header_hash(cls, header):
return cls.HEADER_HASH(header)


class KomodoMixin(object):
class KomodoMixin:
P2PKH_VERBYTE = bytes.fromhex("3C")
P2SH_VERBYTES = (bytes.fromhex("55"),)
WIF_BYTE = bytes.fromhex("BC")
Expand All @@ -344,15 +327,15 @@ class KomodoMixin(object):
DESERIALIZER = lib_tx.DeserializerZcash


class BitcoinMixin(object):
class BitcoinMixin:
SHORTNAME = "BTC"
NET = "mainnet"
XPUB_VERBYTES = bytes.fromhex("0488b21e")
XPRV_VERBYTES = bytes.fromhex("0488ade4")
RPC_PORT = 8332


class NameMixin(object):
class NameMixin:
DATA_PUSH_MULTIPLE = -2

@classmethod
Expand Down Expand Up @@ -524,7 +507,7 @@ def name_hashX_from_script(cls, script):
return super().hashX_from_script(name_index_script)


class PrimeChainPowMixin(object):
class PrimeChainPowMixin:
STATIC_BLOCK_HEADERS = False
DESERIALIZER = lib_tx.DeserializerPrimecoin

Expand Down Expand Up @@ -772,7 +755,7 @@ def hashX_from_script(cls, script):
return super().hashX_from_script(address_script)


class BitcoinTestnetMixin(object):
class BitcoinTestnetMixin:
SHORTNAME = "XTN"
NET = "testnet"
XPUB_VERBYTES = bytes.fromhex("043587cf")
Expand Down Expand Up @@ -1052,7 +1035,7 @@ class GravityCoin(Coin):
P2PKH_VERBYTE = bytes.fromhex("28")
P2SH_VERBYTES = (bytes.fromhex("0a"),)
WIF_BYTE = bytes.fromhex("d2")
GENESIS_HASH = ('322bad477efb4b33fa4b1f0b2861eaf543c61068da9898a95062fdb02ada486f')
GENESIS_HASH = '322bad477efb4b33fa4b1f0b2861eaf543c61068da9898a95062fdb02ada486f'
TX_COUNT = 446050
TX_COUNT_HEIGHT = 547346
TX_PER_BLOCK = 2
Expand Down Expand Up @@ -1569,8 +1552,8 @@ def header_hash(cls, header):
if cls.header_prevhash(header) == bytes(32):
return double_sha256(header)
else:
if (header[0] == 4 and header[2] >= 1):
if (len(header) < 144 or header[143] < 3):
if header[0] == 4 and header[2] >= 1:
if len(header) < 144 or header[143] < 3:
return verushash.verushash_v2b(header)
else:
return verushash.verushash_v2b1(header)
Expand Down Expand Up @@ -2737,7 +2720,7 @@ class Pivx(Coin):
NET = "mainnet"
XPUB_VERBYTES = bytes.fromhex("022D2533")
XPRV_VERBYTES = bytes.fromhex("0221312B")
GENESIS_HASH = ('0000041e482b9b9691d98eefb48473405c0b8ec31b76df3797c74a78680ef818')
GENESIS_HASH = '0000041e482b9b9691d98eefb48473405c0b8ec31b76df3797c74a78680ef818'
P2PKH_VERBYTE = bytes.fromhex("1e")
P2SH_VERBYTE = bytes.fromhex("0d")
WIF_BYTE = bytes.fromhex("d4")
Expand All @@ -2753,7 +2736,7 @@ class Pivx(Coin):
@classmethod
def static_header_len(cls, height):
'''Given a header height return its length.'''
if (height >= cls.ZEROCOIN_START_HEIGHT):
if height >= cls.ZEROCOIN_START_HEIGHT:
return cls.ZEROCOIN_HEADER
else:
return cls.BASIC_HEADER_SIZE
Expand All @@ -2773,7 +2756,7 @@ class PivxTestnet(Pivx):
NET = "testnet"
XPUB_VERBYTES = bytes.fromhex("3a8061a0")
XPRV_VERBYTES = bytes.fromhex("3a805837")
GENESIS_HASH = ('0000041e482b9b9691d98eefb48473405c0b8ec31b76df3797c74a78680ef818')
GENESIS_HASH = '0000041e482b9b9691d98eefb48473405c0b8ec31b76df3797c74a78680ef818'
P2PKH_VERBYTE = bytes.fromhex("8B")
P2SH_VERBYTE = bytes.fromhex("13")
WIF_BYTE = bytes.fromhex("EF")
Expand Down Expand Up @@ -2801,7 +2784,6 @@ class Bitg(Coin):
RPC_PORT = 9332
REORG_LIMIT = 1000
SESSIONCLS = DashElectrumX
DAEMON = daemon.DashDaemon

@classmethod
def header_hash(cls, header):
Expand Down Expand Up @@ -3252,7 +3234,7 @@ class ECCoin(Coin):
XPRV_VERBYTES = bytes.fromhex("0488ade4")
P2PKH_VERBYTE = bytes.fromhex("21")
P2SH_VERBYTES = (bytes.fromhex("08"),)
GENESIS_HASH = ('a60ac43c88dbc44b826cf315352a8a7b373d2af8b6e1c4c4a0638859c5e9ecd1')
GENESIS_HASH = 'a60ac43c88dbc44b826cf315352a8a7b373d2af8b6e1c4c4a0638859c5e9ecd1'
TX_COUNT = 4661197
TX_COUNT_HEIGHT = 2114846
TX_PER_BLOCK = 10
Expand Down Expand Up @@ -3395,7 +3377,7 @@ class GravityZeroCoin(ScryptMixin, Coin):
NET = "mainnet"
P2PKH_VERBYTE = bytes.fromhex("26")
WIF_BYTE = bytes.fromhex("26")
GENESIS_HASH = ('0000028bfbf9ccaed8f28b3ca6b3ffe6b65e29490ab0e4430679bf41cc7c164f')
GENESIS_HASH = '0000028bfbf9ccaed8f28b3ca6b3ffe6b65e29490ab0e4430679bf41cc7c164f'
DAEMON = daemon.FakeEstimateLegacyRPCDaemon
TX_COUNT = 100
TX_COUNT_HEIGHT = 747635
Expand All @@ -3414,7 +3396,7 @@ class Simplicity(Coin):
P2PKH_VERBYTE = bytes.fromhex("12")
P2SH_VERBYTE = bytes.fromhex("3b")
WIF_BYTE = bytes.fromhex("5d")
GENESIS_HASH = ('f4bbfc518aa3622dbeb8d2818a606b82c2b8b1ac2f28553ebdb6fc04d7abaccf')
GENESIS_HASH = 'f4bbfc518aa3622dbeb8d2818a606b82c2b8b1ac2f28553ebdb6fc04d7abaccf'
RPC_PORT = 11958
TX_COUNT = 1726548
TX_COUNT_HEIGHT = 1040000
Expand Down Expand Up @@ -3443,7 +3425,7 @@ class Myce(Coin):
P2PKH_VERBYTE = bytes.fromhex("32")
P2SH_VERBYTE = bytes.fromhex("55")
WIF_BYTE = bytes.fromhex("99")
GENESIS_HASH = ('0000c74cc66c72cb1a327c5c1d4893ae5276aa50be49fb23cec21df1a2f20d87')
GENESIS_HASH = '0000c74cc66c72cb1a327c5c1d4893ae5276aa50be49fb23cec21df1a2f20d87'
RPC_PORT = 23512
TX_COUNT = 1568977
TX_COUNT_HEIGHT = 774450
Expand Down
2 changes: 1 addition & 1 deletion electrumx/lib/env_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from electrumx.lib.util import class_logger


class EnvBase(object):
class EnvBase:
'''Wraps environment configuration.'''

class Error(Exception):
Expand Down
20 changes: 1 addition & 19 deletions electrumx/lib/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from electrumx.lib.util import bytes_to_int, int_to_bytes, hex_to_bytes

_sha256 = hashlib.sha256
_sha512 = hashlib.sha512
_new_hash = hashlib.new
_hmac_digest = hmac.digest
HASHX_LEN = 11
Expand All @@ -43,28 +42,11 @@ def sha256(x):
return _sha256(x).digest()


def ripemd160(x):
'''Simple wrapper of hashlib ripemd160.'''
return _new_hash('ripemd160', x).digest()


def double_sha256(x):
'''SHA-256 of SHA-256, as used extensively in bitcoin.'''
return sha256(sha256(x))


def hmac_sha512(key, msg):
'''Use SHA-512 to provide an HMAC.'''
return _hmac_digest(key, msg, _sha512)


def hash160(x):
'''RIPEMD-160 of SHA-256.
Used to make bitcoin addresses from pubkeys.'''
return ripemd160(sha256(x))


def hash_to_hex_str(x):
'''Convert a big-endian binary hash to displayed hex string.
Expand All @@ -82,7 +64,7 @@ class Base58Error(Exception):
'''Exception used for Base58 errors.'''


class Base58(object):
class Base58:
'''Class providing base 58 functionality.'''

chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
Expand Down
4 changes: 2 additions & 2 deletions electrumx/lib/merkle.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from electrumx.lib.hash import double_sha256


class Merkle(object):
class Merkle:
'''Perform merkle tree calculations on binary hashes using a given hash
function.
Expand Down Expand Up @@ -157,7 +157,7 @@ def branch_and_root_from_level(self, level, leaf_hashes, index,
return leaf_branch + level_branch, root


class MerkleCache(object):
class MerkleCache:
'''A cache to calculate merkle branches efficiently.'''

def __init__(self, merkle, source_func):
Expand Down
2 changes: 1 addition & 1 deletion electrumx/lib/peer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from electrumx.lib.util import cachedproperty, protocol_tuple, version_string


class Peer(object):
class Peer:

# Protocol version
ATTRS = ('host', 'features',
Expand Down
40 changes: 2 additions & 38 deletions electrumx/lib/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
# and warranty status of this software.

'''Script-related classes and functions.'''


from collections import namedtuple

from electrumx.lib.enum import Enumeration
from electrumx.lib.util import unpack_le_uint16_from, unpack_le_uint32_from, \
pack_le_uint16, pack_le_uint32
Expand Down Expand Up @@ -99,7 +95,7 @@ def _match_ops(ops, pattern):
return True


class ScriptPubKey(object):
class ScriptPubKey:
'''A class for handling a tx output script that gives conditions
necessary for spending.
'''
Expand All @@ -109,38 +105,6 @@ class ScriptPubKey(object):
TO_P2SH_OPS = (OpCodes.OP_HASH160, -1, OpCodes.OP_EQUAL)
TO_PUBKEY_OPS = (-1, OpCodes.OP_CHECKSIG)

PayToHandlers = namedtuple('PayToHandlers', 'address script_hash pubkey '
'unspendable strange')

@classmethod
def pay_to(cls, handlers, script):
'''Parse a script, invoke the appropriate handler and
return the result.
One of the following handlers is invoked:
handlers.address(hash160)
handlers.script_hash(hash160)
handlers.pubkey(pubkey)
handlers.unspendable()
handlers.strange(script)
'''
try:
ops = Script.get_ops(script)
except ScriptError:
return handlers.unspendable()

match = _match_ops

if match(ops, cls.TO_ADDRESS_OPS):
return handlers.address(ops[2][-1])
if match(ops, cls.TO_P2SH_OPS):
return handlers.script_hash(ops[1][-1])
if match(ops, cls.TO_PUBKEY_OPS):
return handlers.pubkey(ops[0][-1])
if ops and ops[0] == OpCodes.OP_RETURN:
return handlers.unspendable()
return handlers.strange(script)

@classmethod
def P2SH_script(cls, hash160):
return (bytes((OpCodes.OP_HASH160,))
Expand All @@ -154,7 +118,7 @@ def P2PKH_script(cls, hash160):
+ bytes((OpCodes.OP_EQUALVERIFY, OpCodes.OP_CHECKSIG)))


class Script(object):
class Script:

@classmethod
def get_ops(cls, script):
Expand Down
2 changes: 1 addition & 1 deletion electrumx/lib/tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def serialize(self):
))


class Deserializer(object):
class Deserializer:
'''Deserializes blocks into transactions.
External entry points are read_tx(), read_tx_and_hash(),
Expand Down
Loading

0 comments on commit 9ae83ca

Please sign in to comment.