Fix bugs handling client UTXO requests

Allow strings for ints - Electrum command line doesn't convert
Don't blow away hash168s from the DB
This commit is contained in:
Neil Booth 2016-11-03 11:40:17 +09:00
parent 07515c6a0d
commit 892e9524e5
3 changed files with 16 additions and 7 deletions

View File

@ -767,7 +767,8 @@ class BlockProcessor(LoggedClass):
'''Returns the hash168 for a UTXO.'''
hash168 = None
if 0 <= index <= 65535:
hash168 = self.utxo_cache(tx_hash, struct.pack('<H', index))
idx_packed = struct.pack('<H', index)
hash168 = self.utxo_cache.hash168(tx_hash, idx_packed)
if hash168 == NO_CACHE_ENTRY:
hash168 = None
return hash168

View File

@ -109,7 +109,7 @@ class UTXOCache(LoggedClass):
return value
# Oh well. Find and remove it from the DB.
hash168 = self.hash168(prev_hash, idx_packed)
hash168 = self.hash168(prev_hash, idx_packed, True)
if not hash168:
return NO_CACHE_ENTRY
@ -144,7 +144,7 @@ class UTXOCache(LoggedClass):
raise Exception('could not resolve UTXO key collision')
def hash168(self, tx_hash, idx_packed):
def hash168(self, tx_hash, idx_packed, delete=False):
'''Return the hash168 paid to by the given TXO.
Refers to the database. Returns None if not found (which is
@ -158,7 +158,8 @@ class UTXOCache(LoggedClass):
return None
if len(data) == 25:
self.cache_delete(key)
if delete:
self.cache_delete(key)
return data[:21]
assert len(data) % 25 == 0
@ -168,7 +169,8 @@ class UTXOCache(LoggedClass):
(tx_num, ) = struct.unpack('<I', data[n+21:n+25])
my_hash, height = self.parent.get_tx_hash(tx_num)
if my_hash == tx_hash:
self.cache_write(key, data[:n] + data[n+25:])
if delete:
self.cache_write(key, data[:n] + data[n+25:])
return data[n:n+21]
raise Exception('could not resolve hash168 collision')

View File

@ -143,8 +143,14 @@ class JSONRPC(asyncio.Protocol, LoggedClass):
@classmethod
def non_negative_integer_from_param(cls, param):
if isinstance(param, int) and param >= 0:
return param
try:
param = int(param)
except ValueError:
pass
else:
if param >= 0:
return param
raise RPCError('param should be a non-negative integer: {}'
.format(param))