Remove hash_to_str

This commit is contained in:
Neil Booth 2018-07-14 21:48:58 +08:00
parent 3c3a1b7017
commit 73a46df17f
10 changed files with 49 additions and 51 deletions

View File

@ -39,7 +39,7 @@ from functools import partial
import base64
import electrumx.lib.util as util
from electrumx.lib.hash import Base58, hash160, double_sha256, hash_to_str
from electrumx.lib.hash import Base58, hash160, double_sha256, hash_to_hex_str
from electrumx.lib.hash import HASHX_LEN
from electrumx.lib.script import ScriptPubKey, OpCodes
import electrumx.lib.tx as lib_tx
@ -121,7 +121,7 @@ class Coin(object):
Return the block less its unspendable coinbase.
'''
header = cls.block_header(block, 0)
header_hex_hash = hash_to_str(cls.header_hash(header))
header_hex_hash = hash_to_hex_str(cls.header_hash(header))
if header_hex_hash != cls.GENESIS_HASH:
raise CoinError('genesis block has hash {} expected {}'
.format(header_hex_hash, cls.GENESIS_HASH))
@ -293,8 +293,8 @@ class Coin(object):
return {
'block_height': height,
'version': version,
'prev_block_hash': hash_to_str(header[4:36]),
'merkle_root': hash_to_str(header[36:68]),
'prev_block_hash': hash_to_hex_str(header[4:36]),
'merkle_root': hash_to_hex_str(header[36:68]),
'timestamp': timestamp,
'bits': bits,
'nonce': nonce,
@ -330,11 +330,11 @@ class EquihashMixin(object):
return {
'block_height': height,
'version': version,
'prev_block_hash': hash_to_str(header[4:36]),
'merkle_root': hash_to_str(header[36:68]),
'prev_block_hash': hash_to_hex_str(header[4:36]),
'merkle_root': hash_to_hex_str(header[36:68]),
'timestamp': timestamp,
'bits': bits,
'nonce': hash_to_str(header[108:140]),
'nonce': hash_to_hex_str(header[108:140]),
}
@classmethod
@ -477,13 +477,13 @@ class BitcoinGold(EquihashMixin, BitcoinMixin, Coin):
h = dict(
block_height=height,
version=struct.unpack('<I', header[:4])[0],
prev_block_hash=hash_to_str(header[4:36]),
merkle_root=hash_to_str(header[36:68]),
prev_block_hash=hash_to_hex_str(header[4:36]),
merkle_root=hash_to_hex_str(header[36:68]),
timestamp=struct.unpack('<I', header[100:104])[0],
reserved=hash_to_str(header[72:100]),
reserved=hash_to_hex_str(header[72:100]),
bits=struct.unpack('<I', header[104:108])[0],
nonce=hash_to_str(header[108:140]),
solution=hash_to_str(header[140:])
nonce=hash_to_hex_str(header[108:140]),
solution=hash_to_hex_str(header[140:])
)
return h
@ -936,9 +936,9 @@ class FairCoin(Coin):
return {
'block_height': height,
'version': version,
'prev_block_hash': hash_to_str(header[4:36]),
'merkle_root': hash_to_str(header[36:68]),
'payload_hash': hash_to_str(header[68:100]),
'prev_block_hash': hash_to_hex_str(header[4:36]),
'merkle_root': hash_to_hex_str(header[36:68]),
'payload_hash': hash_to_hex_str(header[68:100]),
'timestamp': timestamp,
'creatorId': creatorId,
}
@ -1000,12 +1000,12 @@ class SnowGem(EquihashMixin, Coin):
return {
'block_height': height,
'version': version,
'prev_block_hash': hash_to_str(header[4:36]),
'merkle_root': hash_to_str(header[36:68]),
'hash_reserved': hash_to_str(header[68:100]),
'prev_block_hash': hash_to_hex_str(header[4:36]),
'merkle_root': hash_to_hex_str(header[36:68]),
'hash_reserved': hash_to_hex_str(header[68:100]),
'timestamp': timestamp,
'bits': bits,
'nonce': hash_to_str(header[108:140]),
'nonce': hash_to_hex_str(header[108:140]),
'n_solution': base64.b64encode(lib_tx.Deserializer(
header, start=140)._read_varbytes()).decode('utf8')
}
@ -1717,8 +1717,8 @@ class Xuez(Coin):
return {
'block_height': height,
'version': version,
'prev_block_hash': hash_to_str(header[4:36]),
'merkle_root': hash_to_str(header[36:68]),
'prev_block_hash': hash_to_hex_str(header[4:36]),
'merkle_root': hash_to_hex_str(header[36:68]),
'timestamp': timestamp,
'bits': bits,
'nonce': nonce,
@ -1727,12 +1727,12 @@ class Xuez(Coin):
return {
'block_height': height,
'version': version,
'prev_block_hash': hash_to_str(header[4:36]),
'merkle_root': hash_to_str(header[36:68]),
'prev_block_hash': hash_to_hex_str(header[4:36]),
'merkle_root': hash_to_hex_str(header[36:68]),
'timestamp': timestamp,
'bits': bits,
'nonce': nonce,
'nAccumulatorCheckpoint': hash_to_str(header[80:112]),
'nAccumulatorCheckpoint': hash_to_hex_str(header[80:112]),
}

View File

@ -75,9 +75,6 @@ def hash_to_hex_str(x):
return bytes(reversed(x)).hex()
hash_to_str = hash_to_hex_str # Temporary
def hex_str_to_hash(x):
'''Convert a displayed hex string to a binary hash.'''
return bytes(reversed(hex_to_bytes(x)))

View File

@ -30,7 +30,7 @@
from collections import namedtuple
from electrumx.lib.hash import double_sha256, hash_to_str
from electrumx.lib.hash import double_sha256, hash_to_hex_str
from electrumx.lib.util import (
cachedproperty, unpack_int32_from, unpack_int64_from,
unpack_uint16_from, unpack_uint32_from, unpack_uint64_from
@ -60,7 +60,7 @@ class TxInput(namedtuple("TxInput", "prev_hash prev_idx script sequence")):
def __str__(self):
script = self.script.hex()
prev_hash = hash_to_str(self.prev_hash)
prev_hash = hash_to_hex_str(self.prev_hash)
return ("Input({}, {:d}, script={}, sequence={:d})"
.format(prev_hash, self.prev_idx, script, self.sequence))
@ -428,7 +428,7 @@ class TxInputDcr(namedtuple("TxInput", "prev_hash prev_idx tree sequence")):
self.prev_idx == TxInputDcr.MINUS_1)
def __str__(self):
prev_hash = hash_to_str(self.prev_hash)
prev_hash = hash_to_hex_str(self.prev_hash)
return ("Input({}, {:d}, tree={}, sequence={:d})"
.format(prev_hash, self.prev_idx, self.tree, self.sequence))

View File

@ -16,7 +16,7 @@ import time
from functools import partial
from electrumx.server.daemon import DaemonError
from electrumx.lib.hash import hash_to_str, HASHX_LEN
from electrumx.lib.hash import hash_to_hex_str, HASHX_LEN
from electrumx.lib.util import chunks, formatted_time, class_logger
import electrumx.server.db
@ -286,7 +286,7 @@ class BlockProcessor(electrumx.server.db.DB):
hashes = await self.reorg_hashes(count)
# Reverse and convert to hex strings.
hashes = [hash_to_str(hash) for hash in reversed(hashes)]
hashes = [hash_to_hex_str(hash) for hash in reversed(hashes)]
for hex_hashes in chunks(hashes, 50):
blocks = await self.daemon.raw_blocks(hex_hashes)
await self.controller.run_in_executor(self.backup_blocks, blocks)
@ -311,7 +311,7 @@ class BlockProcessor(electrumx.server.db.DB):
count = 1
while start > 0:
hashes = self.fs_block_hashes(start, count)
hex_hashes = [hash_to_str(hash) for hash in hashes]
hex_hashes = [hash_to_hex_str(hash) for hash in hashes]
d_hex_hashes = await self.daemon.block_hex_hashes(start, count)
n = diff_pos(hex_hashes, d_hex_hashes)
if n > 0:
@ -574,8 +574,9 @@ class BlockProcessor(electrumx.server.db.DB):
header_hash = coin.header_hash(block.header)
if header_hash != self.tip:
raise ChainError('backup block {} not tip {} at height {:,d}'
.format(hash_to_str(header_hash),
hash_to_str(self.tip), self.height))
.format(hash_to_hex_str(header_hash),
hash_to_hex_str(self.tip),
self.height))
self.tip = coin.header_prevhash(block.header)
self.backup_txs(block.transactions)
self.height -= 1
@ -718,7 +719,7 @@ class BlockProcessor(electrumx.server.db.DB):
return hashX + tx_num_packed + utxo_value_packed
raise ChainError('UTXO {} / {:,d} not found in "h" table'
.format(hash_to_str(tx_hash), tx_idx))
.format(hash_to_hex_str(tx_hash), tx_idx))
def flush_utxos(self, batch):
'''Flush the cached DB writes and UTXO set to the batch.'''

View File

@ -17,7 +17,7 @@ from bisect import bisect_right
from collections import namedtuple
import electrumx.lib.util as util
from electrumx.lib.hash import hash_to_str, HASHX_LEN
from electrumx.lib.hash import hash_to_hex_str, HASHX_LEN
from electrumx.server.storage import db_class
from electrumx.server.history import History
@ -133,7 +133,7 @@ class DB(object):
self.logger.info('coin: {}'.format(self.coin.NAME))
self.logger.info('network: {}'.format(self.coin.NET))
self.logger.info('height: {:,d}'.format(self.db_height))
self.logger.info('tip: {}'.format(hash_to_str(self.db_tip)))
self.logger.info('tip: {}'.format(hash_to_hex_str(self.db_tip)))
self.logger.info('tx count: {:,d}'.format(self.db_tx_count))
if self.first_sync:
self.logger.info('sync time so far: {}'
@ -380,7 +380,7 @@ class DB(object):
db_value = self.utxo_db.get(key)
if not db_value:
raise self.DBError('UTXO {} / {:,d} in one table only'
.format(hash_to_str(tx_hash), tx_idx))
.format(hash_to_hex_str(tx_hash), tx_idx))
value, = unpack('<Q', db_value)
return hashX, value

View File

@ -16,7 +16,7 @@ from functools import partial
from struct import pack, unpack
import electrumx.lib.util as util
from electrumx.lib.hash import hash_to_str, HASHX_LEN
from electrumx.lib.hash import hash_to_hex_str, HASHX_LEN
class History(object):
@ -230,8 +230,8 @@ class History(object):
if nrows > 4:
self.logger.info('hashX {} is large: {:,d} entries across '
'{:,d} rows'
.format(hash_to_str(hashX), len(full_hist) // 4,
nrows))
.format(hash_to_hex_str(hashX),
len(full_hist) // 4, nrows))
# Find what history needs to be written, and what keys need to
# be deleted. Start by assuming all keys are to be deleted,

View File

@ -12,7 +12,7 @@ import itertools
import time
from collections import defaultdict
from electrumx.lib.hash import hash_to_str, hex_str_to_hash
from electrumx.lib.hash import hash_to_hex_str, hex_str_to_hash
from electrumx.lib.util import class_logger
from electrumx.server.daemon import DaemonError
from electrumx.server.db import UTXO
@ -230,7 +230,7 @@ class MemPool(object):
for txout in tx.outputs]
# Convert the tx inputs to ([prev_hex_hash, prev_idx) pairs
txin_pairs = [(hash_to_str(txin.prev_hash), txin.prev_idx)
txin_pairs = [(hash_to_hex_str(txin.prev_hash), txin.prev_idx)
for txin in tx.inputs]
pending.append((tx_hash, txin_pairs, txout_pairs, tx_size))
@ -309,7 +309,7 @@ class MemPool(object):
continue
tx_fee = item[2]
tx = deserializer(raw_tx).read_tx()
unconfirmed = any(hash_to_str(txin.prev_hash) in self.txs
unconfirmed = any(hash_to_hex_str(txin.prev_hash) in self.txs
for txin in tx.inputs)
result.append((hex_hash, tx_fee, unconfirmed))
return result

View File

@ -15,7 +15,7 @@ from functools import partial
from aiorpcx import ServerSession, JSONRPCAutoDetect, RPCError
from electrumx.lib.hash import sha256, hash_to_str
from electrumx.lib.hash import sha256, hash_to_hex_str
import electrumx.lib.util as util
from electrumx.server.daemon import DaemonError
@ -248,7 +248,7 @@ class ElectrumX(SessionBase):
history = await self.controller.get_history(hashX)
mempool = await self.controller.mempool_transactions(hashX)
status = ''.join('{}:{:d}:'.format(hash_to_str(tx_hash), height)
status = ''.join('{}:{:d}:'.format(hash_to_hex_str(tx_hash), height)
for tx_hash, height in history)
status += ''.join('{}:{:d}:'.format(hex_hash, -unconfirmed)
for hex_hash, tx_fee, unconfirmed in mempool)

View File

@ -17,7 +17,7 @@ import sys
from electrumx import Env
from electrumx.server.db import DB
from electrumx.lib.hash import hash_to_str
from electrumx.lib.hash import hash_to_hex_str
def count_entries(hist_db, utxo_db):
@ -58,11 +58,11 @@ def main():
for n, (tx_hash, height) in enumerate(bp.get_history(hashX, limit)):
print('History #{:d}: hash: {} height: {:d}'
.format(n + 1, hash_to_str(tx_hash), height))
.format(n + 1, hash_to_hex_str(tx_hash), height))
n = None
for n, utxo in enumerate(bp.get_utxos(hashX, limit)):
print('UTXOs #{:d}: hash: {} pos: {:d} height: {:d} value: {:d}'
.format(n + 1, hash_to_str(utxo.tx_hash),
.format(n + 1, hash_to_hex_str(utxo.tx_hash),
utxo.tx_pos, utxo.height, utxo.value))
if n is None:
print('No UTXOs')

View File

@ -6,7 +6,7 @@ from os import environ, urandom
from struct import pack
import random
from electrumx.lib.hash import hash_to_str, HASHX_LEN
from electrumx.lib.hash import HASHX_LEN
from electrumx.server.env import Env
from electrumx.server.db import DB