1 from __future__ import division
6 from twisted.internet import defer
8 from . import base58, skiplists
9 from p2pool.util import bases, math, expiring_dict, memoize, dicts
12 class EarlyEnd(Exception):
15 class LateEnd(Exception):
18 def read((data, pos), length):
19 data2 = data[pos:pos + length]
20 if len(data2) != length:
22 return data2, (data, pos + length)
24 def size((data, pos)):
25 return len(data) - pos
28 # the same data can have only one unpacked representation, but multiple packed binary representations
31 rval = getattr(self, '_hash', None)
34 rval = self._hash = hash((type(self), frozenset(self.__dict__.items())))
40 def __eq__(self, other):
41 return type(other) is type(self) and other.__dict__ == self.__dict__
43 def __ne__(self, other):
44 return not (self == other)
46 def _unpack(self, data):
47 obj, (data2, pos) = self.read((data, 0))
57 f = self.write(None, obj)
67 def unpack(self, data):
68 obj = self._unpack(data)
71 data2 = self._pack(obj)
73 if self._unpack(data2) != obj:
74 raise AssertionError()
79 data = self._pack(obj)
82 if self._unpack(data) != obj:
83 raise AssertionError((self._unpack(data), obj))
87 _backing = expiring_dict.ExpiringDict(100)
88 pack2 = memoize.memoize_with_backing(_backing, [unpack])(pack2)
89 unpack = memoize.memoize_with_backing(_backing)(unpack) # doesn't have an inverse
92 return self.pack2(dicts.immutify(obj))
95 def pack_base58(self, obj):
96 return base58.base58_encode(self.pack(obj))
98 def unpack_base58(self, base58_data):
99 return self.unpack(base58.base58_decode(base58_data))
102 def hash160(self, obj):
103 return ShortHashType().unpack(hashlib.new('ripemd160', hashlib.sha256(self.pack(obj)).digest()).digest())
105 def hash256(self, obj):
106 return HashType().unpack(hashlib.sha256(hashlib.sha256(self.pack(obj)).digest()).digest())
108 def scrypt(self, obj):
110 return HashType().unpack(ltc_scrypt.getPoWHash(self.pack(obj)))
112 class VarIntType(Type):
113 # redundancy doesn't matter here because bitcoin and p2pool both reencode before hashing
114 def read(self, file):
115 data, file = read(file, 1)
120 desc, length = '<H', 2
122 desc, length = '<I', 4
124 desc, length = '<Q', 8
126 raise AssertionError()
127 data, file = read(file, length)
128 return struct.unpack(desc, data)[0], file
130 def write(self, file, item):
132 file = file, struct.pack('<B', item)
134 file = file, struct.pack('<BH', 0xfd, item)
135 elif item <= 0xffffffff:
136 file = file, struct.pack('<BI', 0xfe, item)
137 elif item <= 0xffffffffffffffff:
138 file = file, struct.pack('<BQ', 0xff, item)
140 raise ValueError('int too large for varint')
143 class VarStrType(Type):
144 _inner_size = VarIntType()
146 def read(self, file):
147 length, file = self._inner_size.read(file)
148 return read(file, length)
150 def write(self, file, item):
151 return self._inner_size.write(file, len(item)), item
153 class FixedStrType(Type):
154 def __init__(self, length):
157 def read(self, file):
158 return read(file, self.length)
160 def write(self, file, item):
161 if len(item) != self.length:
162 raise ValueError('incorrect length item!')
165 class EnumType(Type):
166 def __init__(self, inner, values):
168 self.values = dicts.frozendict(values)
171 for k, v in values.iteritems():
173 raise ValueError('duplicate value in values')
175 self.keys = dicts.frozendict(keys)
177 def read(self, file):
178 data, file = self.inner.read(file)
179 if data not in self.keys:
180 raise ValueError('enum data (%r) not in values (%r)' % (data, self.values))
181 return self.keys[data], file
183 def write(self, file, item):
184 if item not in self.values:
185 raise ValueError('enum item (%r) not in values (%r)' % (item, self.values))
186 return self.inner.write(file, self.values[item])
188 class HashType(Type):
189 def read(self, file):
190 data, file = read(file, 256//8)
191 return int(data[::-1].encode('hex'), 16), file
193 def write(self, file, item):
194 if not 0 <= item < 2**256:
195 raise ValueError('invalid hash value - %r' % (item,))
196 if item != 0 and item < 2**160:
197 print 'Very low hash value - maybe you meant to use ShortHashType? %x' % (item,)
198 return file, ('%064x' % (item,)).decode('hex')[::-1]
200 class ShortHashType(Type):
201 def read(self, file):
202 data, file = read(file, 160//8)
203 return int(data[::-1].encode('hex'), 16), file
205 def write(self, file, item):
206 if not 0 <= item < 2**160:
207 raise ValueError('invalid hash value - %r' % (item,))
208 return file, ('%040x' % (item,)).decode('hex')[::-1]
210 class ListType(Type):
211 _inner_size = VarIntType()
213 def __init__(self, type):
216 def read(self, file):
217 length, file = self._inner_size.read(file)
219 for i in xrange(length):
220 item, file = self.type.read(file)
224 def write(self, file, item):
225 file = self._inner_size.write(file, len(item))
227 file = self.type.write(file, subitem)
230 class StructType(Type):
231 def __init__(self, desc):
233 self.length = struct.calcsize(self.desc)
235 def read(self, file):
236 data, file = read(file, self.length)
237 res, = struct.unpack(self.desc, data)
240 def write(self, file, item):
241 data = struct.pack(self.desc, item)
242 if struct.unpack(self.desc, data)[0] != item:
243 # special test because struct doesn't error on some overflows
244 raise ValueError('''item didn't survive pack cycle (%r)''' % (item,))
247 class IPV6AddressType(Type):
248 def read(self, file):
249 data, file = read(file, 16)
250 if data[:12] != '00000000000000000000ffff'.decode('hex'):
251 raise ValueError('ipv6 addresses not supported yet')
252 return '.'.join(str(ord(x)) for x in data[12:]), file
254 def write(self, file, item):
255 bits = map(int, item.split('.'))
257 raise ValueError('invalid address: %r' % (bits,))
258 data = '00000000000000000000ffff'.decode('hex') + ''.join(chr(x) for x in bits)
259 assert len(data) == 16, len(data)
264 def get_record(fields):
265 fields = tuple(sorted(fields))
268 if fields not in _record_types:
269 class _Record(object):
272 return repr(dict(self))
273 def __getitem__(self, key):
274 return getattr(self, key)
275 def __setitem__(self, key, value):
276 setattr(self, key, value)
278 # for field in self.__slots__:
279 # yield field, getattr(self, field)
281 return self.__slots__
282 def __eq__(self, other):
283 if isinstance(other, dict):
284 return dict(self) == other
285 elif isinstance(other, _Record):
286 return all(self[k] == other[k] for k in self.keys())
288 def __ne__(self, other):
289 return not (self == other)
290 _record_types[fields] = _Record
291 return _record_types[fields]()
293 class ComposedType(Type):
294 def __init__(self, fields):
295 self.fields = tuple(fields)
297 def read(self, file):
298 item = get_record(k for k, v in self.fields)
299 for key, type_ in self.fields:
300 item[key], file = type_.read(file)
303 def write(self, file, item):
304 for key, type_ in self.fields:
305 file = type_.write(file, item[key])
308 class ChecksummedType(Type):
309 def __init__(self, inner):
312 def read(self, file):
313 obj, file = self.inner.read(file)
314 data = self.inner.pack(obj)
316 checksum, file = read(file, 4)
317 if checksum != hashlib.sha256(hashlib.sha256(data).digest()).digest()[:4]:
318 raise ValueError('invalid checksum')
322 def write(self, file, item):
323 data = self.inner.pack(item)
324 return (file, data), hashlib.sha256(hashlib.sha256(data).digest()).digest()[:4]
326 class FloatingInteger(object):
327 __slots__ = ['_bits']
330 def from_target_upper_bound(cls, target):
331 n = bases.natural_to_string(target)
332 if n and ord(n[0]) >= 128:
334 bits2 = (chr(len(n)) + (n + 3*chr(0))[:3])[::-1]
335 bits = struct.unpack('<I', bits2)[0]
338 def __init__(self, bits):
343 return math.shift_left(self._bits & 0x00ffffff, 8 * ((self._bits >> 24) - 3))
346 return hash(self._value)
348 def __cmp__(self, other):
349 if isinstance(other, FloatingInteger):
350 return cmp(self._value, other._value)
351 elif isinstance(other, (int, long)):
352 return cmp(self._value, other)
354 raise NotImplementedError(other)
360 return 'FloatingInteger(bits=%s (%x))' % (hex(self._bits), self)
362 def __add__(self, other):
363 if isinstance(other, (int, long)):
364 return self._value + other
365 raise NotImplementedError()
367 def __mul__(self, other):
368 if isinstance(other, (int, long)):
369 return self._value * other
370 raise NotImplementedError()
372 def __truediv__(self, other):
373 if isinstance(other, (int, long)):
374 return self._value / other
375 raise NotImplementedError()
376 def __floordiv__(self, other):
377 if isinstance(other, (int, long)):
378 return self._value // other
379 raise NotImplementedError()
380 __div__ = __truediv__
381 def __rtruediv__(self, other):
382 if isinstance(other, (int, long)):
383 return other / self._value
384 raise NotImplementedError()
385 def __rfloordiv__(self, other):
386 if isinstance(other, (int, long)):
387 return other // self._value
388 raise NotImplementedError()
389 __rdiv__ = __rtruediv__
391 class FloatingIntegerType(Type):
392 _inner = StructType('<I')
394 def read(self, file):
395 bits, file = self._inner.read(file)
396 return FloatingInteger(bits), file
398 def write(self, file, item):
399 return self._inner.write(file, item._bits)
401 class PossiblyNoneType(Type):
402 def __init__(self, none_value, inner):
403 self.none_value = none_value
406 def read(self, file):
407 value, file = self.inner.read(file)
408 return None if value == self.none_value else value, file
410 def write(self, file, item):
411 if item == self.none_value:
412 raise ValueError('none_value used')
413 return self.inner.write(file, self.none_value if item is None else item)
415 address_type = ComposedType([
416 ('services', StructType('<Q')),
417 ('address', IPV6AddressType()),
418 ('port', StructType('>H')),
421 tx_type = ComposedType([
422 ('version', StructType('<I')),
423 ('tx_ins', ListType(ComposedType([
424 ('previous_output', PossiblyNoneType(dicts.frozendict(hash=0, index=2**32 - 1), ComposedType([
425 ('hash', HashType()),
426 ('index', StructType('<I')),
428 ('script', VarStrType()),
429 ('sequence', PossiblyNoneType(2**32 - 1, StructType('<I'))),
431 ('tx_outs', ListType(ComposedType([
432 ('value', StructType('<Q')),
433 ('script', VarStrType()),
435 ('lock_time', StructType('<I')),
438 merkle_branch_type = ListType(HashType())
440 merkle_tx_type = ComposedType([
442 ('block_hash', HashType()),
443 ('merkle_branch', merkle_branch_type),
444 ('index', StructType('<i')),
447 block_header_type = ComposedType([
448 ('version', StructType('<I')),
449 ('previous_block', PossiblyNoneType(0, HashType())),
450 ('merkle_root', HashType()),
451 ('timestamp', StructType('<I')),
452 ('target', FloatingIntegerType()),
453 ('nonce', StructType('<I')),
456 block_type = ComposedType([
457 ('header', block_header_type),
458 ('txs', ListType(tx_type)),
461 aux_pow_type = ComposedType([
462 ('merkle_tx', merkle_tx_type),
463 ('merkle_branch', merkle_branch_type),
464 ('index', StructType('<i')),
465 ('parent_block_header', block_header_type),
469 merkle_record_type = ComposedType([
470 ('left', HashType()),
471 ('right', HashType()),
474 def merkle_hash(tx_list):
477 hash_list = map(tx_type.hash256, tx_list)
478 while len(hash_list) > 1:
479 hash_list = [merkle_record_type.hash256(dict(left=left, right=left if right is None else right))
480 for left, right in zip(hash_list[::2], hash_list[1::2] + [None])]
483 def calculate_merkle_branch(txs, index):
486 hash_list = [(tx_type.hash256(tx), i == index, []) for i, tx in enumerate(txs)]
488 while len(hash_list) > 1:
491 merkle_record_type.hash256(dict(left=left, right=right)),
493 (left_l if left_f else right_l) + [dict(side=1, hash=right) if left_f else dict(side=0, hash=left)],
495 for (left, left_f, left_l), (right, right_f, right_l) in
496 zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])
499 res = [x['hash'] for x in hash_list[0][2]]
501 assert hash_list[0][1]
502 assert check_merkle_branch(txs[index], index, res) == hash_list[0][0]
503 assert index == sum(k*2**i for i, k in enumerate([1-x['side'] for x in hash_list[0][2]]))
507 def check_merkle_branch(tx, index, merkle_branch):
508 return reduce(lambda c, (i, h): merkle_record_type.hash256(
509 dict(left=h, right=c) if 2**i & index else
510 dict(left=c, right=h)
511 ), enumerate(merkle_branch), tx_type.hash256(tx))
513 def target_to_average_attempts(target):
514 return 2**256//(target + 1)
516 def target_to_difficulty(target):
517 return (0xffff0000 * 2**(256-64) + 1)/(target + 1)
521 def tx_get_sigop_count(tx):
522 return sum(script.get_sigop_count(txin['script']) for txin in tx['tx_ins']) + sum(script.get_sigop_count(txout['script']) for txout in tx['tx_outs'])
526 human_address_type = ChecksummedType(ComposedType([
527 ('version', StructType('<B')),
528 ('pubkey_hash', ShortHashType()),
531 pubkey_type = FixedStrType(65)
533 def pubkey_hash_to_address(pubkey_hash, net):
534 return human_address_type.pack_base58(dict(version=net.BITCOIN_ADDRESS_VERSION, pubkey_hash=pubkey_hash))
536 def pubkey_to_address(pubkey, net):
537 return pubkey_hash_to_address(pubkey_type.hash160(pubkey), net)
539 def address_to_pubkey_hash(address, net):
540 x = human_address_type.unpack_base58(address)
541 if x['version'] != net.BITCOIN_ADDRESS_VERSION:
542 raise ValueError('address not for this net!')
543 return x['pubkey_hash']
547 def pubkey_to_script2(pubkey):
548 return ('\x41' + pubkey_type.pack(pubkey)) + '\xac'
550 def pubkey_hash_to_script2(pubkey_hash):
551 return '\x76\xa9' + ('\x14' + ShortHashType().pack(pubkey_hash)) + '\x88\xac'
553 def script2_to_human(script2, net):
555 pubkey = script2[1:-1]
556 script2_test = pubkey_to_script2(pubkey)
560 if script2_test == script2:
561 return 'Pubkey. Address: %s' % (pubkey_to_address(pubkey, net),)
564 pubkey_hash = ShortHashType().unpack(script2[3:-2])
565 script2_test2 = pubkey_hash_to_script2(pubkey_hash)
569 if script2_test2 == script2:
570 return 'Address. Address: %s' % (pubkey_hash_to_address(pubkey_hash, net),)
572 return 'Unknown. Script: %s' % (script2.encode('hex'),)