Browse Source

Make HASHX_LEN a global, not a coin property

patch-2
Neil Booth 7 years ago
parent
commit
77051f83a4
  1. 13
      lib/coins.py
  2. 1
      lib/hash.py
  3. 4
      server/block_processor.py
  4. 4
      server/controller.py
  5. 4
      server/db.py
  6. 6
      tests/server/test_compaction.py

13
lib/coins.py

@ -39,7 +39,7 @@ from functools import partial
import base64
import lib.util as util
from lib.hash import Base58, hash160, double_sha256, hash_to_str
from lib.hash import Base58, hash160, double_sha256, hash_to_str, HASHX_LEN
from lib.script import ScriptPubKey, OpCodes
import lib.tx as lib_tx
from server.block_processor import BlockProcessor
@ -63,7 +63,6 @@ class Coin(object):
RPC_URL_REGEX = re.compile('.+@(\[[0-9a-fA-F:]+\]|[^:]+)(:[0-9]+)?')
VALUE_PER_COIN = 100000000
CHUNK_SIZE = 2016
HASHX_LEN = 11
BASIC_HEADER_SIZE = 80
STATIC_BLOCK_HEADERS = True
SESSIONCLS = ElectrumX
@ -135,7 +134,7 @@ class Coin(object):
'''
if script and script[0] == OP_RETURN:
return None
return sha256(script).digest()[:cls.HASHX_LEN]
return sha256(script).digest()[:HASHX_LEN]
@util.cachedproperty
def address_handlers(cls):
@ -1660,7 +1659,7 @@ class Xuez(Coin):
GENESIS_HASH = ('000000e1febc39965b055e8e0117179a'
'4d18e24e7aaa0c69864c4054b4f29445')
TX_COUNT = 30000
TX_COUNT_HEIGHT = 15000
TX_PER_BLOCK = 1
@ -1679,7 +1678,7 @@ class Xuez(Coin):
version, = struct.unpack('<I', header[:4])
import xevan_hash
if version == 1 :
return xevan_hash.getPoWHash(header[:80])
else:
@ -1698,7 +1697,7 @@ class Xuez(Coin):
'timestamp': timestamp,
'bits': bits,
'nonce': nonce,
}
}
else:
return {
'block_height': height,
@ -1709,4 +1708,4 @@ class Xuez(Coin):
'bits': bits,
'nonce': nonce,
'nAccumulatorCheckpoint': hash_to_str(header[80:112]),
}
}

1
lib/hash.py

@ -35,6 +35,7 @@ _sha256 = hashlib.sha256
_sha512 = hashlib.sha512
_new_hash = hashlib.new
_new_hmac = hmac.new
HASHX_LEN = 11
def sha256(x):

4
server/block_processor.py

@ -18,7 +18,7 @@ from collections import defaultdict
from functools import partial
from server.daemon import DaemonError
from lib.hash import hash_to_str
from lib.hash import hash_to_str, HASHX_LEN
from lib.util import chunks, formatted_time
import server.db
@ -606,7 +606,7 @@ class BlockProcessor(server.db.DB):
spend_utxo = self.spend_utxo
script_hashX = self.coin.hashX_from_script
touched = self.touched
undo_entry_len = 12 + self.coin.HASHX_LEN
undo_entry_len = 12 + HASHX_LEN
for tx, tx_hash in reversed(txs):
for idx, txout in enumerate(tx.outputs):

4
server/controller.py

@ -20,7 +20,7 @@ from functools import partial
import pylru
from aiorpcx import RPCError, TaskSet, _version
from lib.hash import double_sha256, hash_to_str, hex_str_to_hash
from lib.hash import double_sha256, hash_to_str, hex_str_to_hash, HASHX_LEN
from lib.peer import Peer
from lib.server_base import ServerBase
import lib.util as util
@ -633,7 +633,7 @@ class Controller(ServerBase):
try:
bin_hash = hex_str_to_hash(scripthash)
if len(bin_hash) == 32:
return bin_hash[:self.coin.HASHX_LEN]
return bin_hash[:HASHX_LEN]
except Exception:
pass
raise RPCError(BAD_REQUEST, f'{scripthash} is not a valid script hash')

4
server/db.py

@ -18,7 +18,7 @@ from bisect import bisect_left, bisect_right
from collections import namedtuple
import lib.util as util
from lib.hash import hash_to_str
from lib.hash import hash_to_str, HASHX_LEN
from server.storage import db_class
@ -609,7 +609,7 @@ class DB(object):
hist_map = {}
hist_list = []
key_len = self.coin.HASHX_LEN + 2
key_len = HASHX_LEN + 2
write_size = 0
for key, hist in self.hist_db.iterator(prefix=prefix):
# Ignore non-history entries

6
tests/server/test_compaction.py

@ -6,7 +6,7 @@ from os import environ, urandom
from struct import pack
import random
from lib.hash import hash_to_str
from lib.hash import hash_to_str, HASHX_LEN
from server.env import Env
from server.db import DB
@ -14,7 +14,7 @@ from server.db import DB
def create_histories(db, hashX_count=100):
'''Creates a bunch of random transaction histories, and write them
to disk in a series of small flushes.'''
hashXs = [urandom(db.coin.HASHX_LEN) for n in range(hashX_count)]
hashXs = [urandom(HASHX_LEN) for n in range(hashX_count)]
mk_array = lambda : array.array('I')
histories = {hashX : mk_array() for hashX in hashXs}
this_history = defaultdict(mk_array)
@ -42,7 +42,7 @@ def check_hashX_compaction(db):
db.max_hist_row_entries = 40
row_size = db.max_hist_row_entries * 4
full_hist = array.array('I', range(100)).tobytes()
hashX = urandom(db.coin.HASHX_LEN)
hashX = urandom(HASHX_LEN)
pairs = ((1, 20), (26, 50), (56, 30))
cum = 0

Loading…
Cancel
Save