NVC protocol v. 60011 support
[p2pool.git] / p2pool / bitcoin / data.py
1 from __future__ import division
2
3 import hashlib
4 import random
5 import warnings
6
7 import p2pool
8 from p2pool.util import math, pack
9
10 def hash256(data):
11     return pack.IntType(256).unpack(hashlib.sha256(hashlib.sha256(data).digest()).digest())
12
13 def hash160(data):
14     return pack.IntType(160).unpack(hashlib.new('ripemd160', hashlib.sha256(data).digest()).digest())
15
16 def scrypt(data):
17     return pack.IntType(256).unpack(__import__('ltc_scrypt').getPoWHash(data))
18
19 class ChecksummedType(pack.Type):
20     def __init__(self, inner, checksum_func=lambda data: hashlib.sha256(hashlib.sha256(data).digest()).digest()[:4]):
21         self.inner = inner
22         self.checksum_func = checksum_func
23     
24     def read(self, file):
25         obj, file = self.inner.read(file)
26         data = self.inner.pack(obj)
27         
28         calculated_checksum = self.checksum_func(data)
29         checksum, file = pack.read(file, len(calculated_checksum))
30         if checksum != calculated_checksum:
31             raise ValueError('invalid checksum')
32         
33         return obj, file
34     
35     def write(self, file, item):
36         data = self.inner.pack(item)
37         return (file, data), self.checksum_func(data)
38
39 class FloatingInteger(object):
40     __slots__ = ['bits', '_target']
41     
42     @classmethod
43     def from_target_upper_bound(cls, target):
44         n = math.natural_to_string(target)
45         if n and ord(n[0]) >= 128:
46             n = '\x00' + n
47         bits2 = (chr(len(n)) + (n + 3*chr(0))[:3])[::-1]
48         bits = pack.IntType(32).unpack(bits2)
49         return cls(bits)
50     
51     def __init__(self, bits, target=None):
52         self.bits = bits
53         self._target = None
54         if target is not None and self.target != target:
55             raise ValueError('target does not match')
56     
57     @property
58     def target(self):
59         res = self._target
60         if res is None:
61             res = self._target = math.shift_left(self.bits & 0x00ffffff, 8 * ((self.bits >> 24) - 3))
62         return res
63     
64     def __hash__(self):
65         return hash(self.bits)
66     
67     def __eq__(self, other):
68         return self.bits == other.bits
69     
70     def __ne__(self, other):
71         return not (self == other)
72     
73     def __cmp__(self, other):
74         assert False
75     
76     def __repr__(self):
77         return 'FloatingInteger(bits=%s, target=%s)' % (hex(self.bits), hex(self.target))
78
79 class FloatingIntegerType(pack.Type):
80     _inner = pack.IntType(32)
81     
82     def read(self, file):
83         bits, file = self._inner.read(file)
84         return FloatingInteger(bits), file
85     
86     def write(self, file, item):
87         return self._inner.write(file, item.bits)
88
89 address_type = pack.ComposedType([
90     ('services', pack.IntType(64)),
91     ('address', pack.IPV6AddressType()),
92     ('port', pack.IntType(16, 'big')),
93 ])
94
95 tx_type = pack.ComposedType([
96     ('version', pack.IntType(32)),
97     ('timestamp', pack.IntType(32)), # txn timestamp
98     ('tx_ins', pack.ListType(pack.ComposedType([
99         ('previous_output', pack.PossiblyNoneType(dict(hash=0, index=2**32 - 1), pack.ComposedType([
100             ('hash', pack.IntType(256)),
101             ('index', pack.IntType(32)),
102         ]))),
103         ('script', pack.VarStrType()),
104         ('sequence', pack.PossiblyNoneType(2**32 - 1, pack.IntType(32))),
105     ]))),
106     ('tx_outs', pack.ListType(pack.ComposedType([
107         ('value', pack.IntType(64)),
108         ('script', pack.VarStrType()),
109     ]))),
110     ('lock_time', pack.IntType(32)),
111 ])
112
113 merkle_link_type = pack.ComposedType([
114     ('branch', pack.ListType(pack.IntType(256))),
115     ('index', pack.IntType(32)),
116 ])
117
118 merkle_tx_type = pack.ComposedType([
119     ('tx', tx_type),
120     ('block_hash', pack.IntType(256)),
121     ('merkle_link', merkle_link_type),
122 ])
123
124 block_header_type = pack.ComposedType([
125     ('version', pack.IntType(32)),
126     ('previous_block', pack.PossiblyNoneType(0, pack.IntType(256))),
127     ('merkle_root', pack.IntType(256)),
128     ('timestamp', pack.IntType(32)),
129     ('bits', FloatingIntegerType()),
130     ('nonce', pack.IntType(32)),
131 ])
132
133 block_type = pack.ComposedType([
134     ('header', block_header_type),
135     ('txs', pack.ListType(tx_type)),
136     ('signature', pack.VarStrType()), # header signature field
137 ])
138
139 # merged mining
140
141 aux_pow_type = pack.ComposedType([
142     ('merkle_tx', merkle_tx_type),
143     ('merkle_link', merkle_link_type),
144     ('parent_block_header', block_header_type),
145 ])
146
147 aux_pow_coinbase_type = pack.ComposedType([
148     ('merkle_root', pack.IntType(256, 'big')),
149     ('size', pack.IntType(32)),
150     ('nonce', pack.IntType(32)),
151 ])
152
153 def make_auxpow_tree(chain_ids):
154     for size in (2**i for i in xrange(31)):
155         if size < len(chain_ids):
156             continue
157         res = {}
158         for chain_id in chain_ids:
159             pos = (1103515245 * chain_id + 1103515245 * 12345 + 12345) % size
160             if pos in res:
161                 break
162             res[pos] = chain_id
163         else:
164             return res, size
165     raise AssertionError()
166
167 # merkle trees
168
169 merkle_record_type = pack.ComposedType([
170     ('left', pack.IntType(256)),
171     ('right', pack.IntType(256)),
172 ])
173
174 def merkle_hash(hashes):
175     if not hashes:
176         return 0
177     hash_list = list(hashes)
178     while len(hash_list) > 1:
179         hash_list = [hash256(merkle_record_type.pack(dict(left=left, right=right)))
180             for left, right in zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])]
181     return hash_list[0]
182
183 def calculate_merkle_link(hashes, index):
184     # XXX optimize this
185     
186     hash_list = [(lambda _h=h: _h, i == index, []) for i, h in enumerate(hashes)]
187     
188     while len(hash_list) > 1:
189         hash_list = [
190             (
191                 lambda _left=left, _right=right: hash256(merkle_record_type.pack(dict(left=_left(), right=_right()))),
192                 left_f or right_f,
193                 (left_l if left_f else right_l) + [dict(side=1, hash=right) if left_f else dict(side=0, hash=left)],
194             )
195             for (left, left_f, left_l), (right, right_f, right_l) in
196                 zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])
197         ]
198     
199     res = [x['hash']() for x in hash_list[0][2]]
200     
201     assert hash_list[0][1]
202     if p2pool.DEBUG:
203         new_hashes = [random.randrange(2**256) if x is None else x
204             for x in hashes]
205         assert check_merkle_link(new_hashes[index], dict(branch=res, index=index)) == merkle_hash(new_hashes)
206     assert index == sum(k*2**i for i, k in enumerate([1-x['side'] for x in hash_list[0][2]]))
207     
208     return dict(branch=res, index=index)
209
210 def check_merkle_link(tip_hash, link):
211     if link['index'] >= 2**len(link['branch']):
212         raise ValueError('index too large')
213     return reduce(lambda c, (i, h): hash256(merkle_record_type.pack(
214         dict(left=h, right=c) if (link['index'] >> i) & 1 else
215         dict(left=c, right=h)
216     )), enumerate(link['branch']), tip_hash)
217
218 # targets
219
220 def target_to_average_attempts(target):
221     assert 0 <= target and isinstance(target, (int, long)), target
222     if target >= 2**256: warnings.warn('target >= 2**256!')
223     return 2**256//(target + 1)
224
225 def average_attempts_to_target(average_attempts):
226     assert average_attempts > 0
227     return min(int(2**256/average_attempts - 1 + 0.5), 2**256-1)
228
229 def target_to_difficulty(target):
230     assert 0 <= target and isinstance(target, (int, long)), target
231     if target >= 2**256: warnings.warn('target >= 2**256!')
232     return (0xffff0000 * 2**(256-64) + 1)/(target + 1)
233
234 def difficulty_to_target(difficulty):
235     assert difficulty >= 0
236     if difficulty == 0: return 2**256-1
237     return min(int((0xffff0000 * 2**(256-64) + 1)/difficulty - 1 + 0.5), 2**256-1)
238
239 # human addresses
240
241 base58_alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
242
243 def base58_encode(bindata):
244     bindata2 = bindata.lstrip(chr(0))
245     return base58_alphabet[0]*(len(bindata) - len(bindata2)) + math.natural_to_string(math.string_to_natural(bindata2), base58_alphabet)
246
247 def base58_decode(b58data):
248     b58data2 = b58data.lstrip(base58_alphabet[0])
249     return chr(0)*(len(b58data) - len(b58data2)) + math.natural_to_string(math.string_to_natural(b58data2, base58_alphabet))
250
251 human_address_type = ChecksummedType(pack.ComposedType([
252     ('version', pack.IntType(8)),
253     ('pubkey_hash', pack.IntType(160)),
254 ]))
255
256 def pubkey_hash_to_address(pubkey_hash, net):
257     return base58_encode(human_address_type.pack(dict(version=net.ADDRESS_VERSION, pubkey_hash=pubkey_hash)))
258
259 def pubkey_to_address(pubkey, net):
260     return pubkey_hash_to_address(hash160(pubkey), net)
261
262 def address_to_pubkey_hash(address, net):
263     x = human_address_type.unpack(base58_decode(address))
264     if x['version'] != net.ADDRESS_VERSION:
265         raise ValueError('address not for this net!')
266     return x['pubkey_hash']
267
268 def address_to_script(address, net):
269     x = human_address_type.unpack(base58_decode(address))
270
271     print x['pubkey_hash']
272
273     if x['version'] != net.ADDRESS_VERSION:
274         raise ValueError('address not for this net!')
275     return '\x76\xa9' + ('\x14' + pack.IntType(160).pack(x['pubkey_hash'])) + '\x88\xac'
276
277
278 # transactions
279
280 def pubkey_to_script2(pubkey):
281     assert len(pubkey) <= 75
282     return (chr(len(pubkey)) + pubkey) + '\xac'
283
284 def pubkey_hash_to_script2(pubkey_hash):
285     return '\x76\xa9' + ('\x14' + pack.IntType(160).pack(pubkey_hash)) + '\x88\xac'
286
287 def script2_to_address(script2, net):
288     try:
289         pubkey = script2[1:-1]
290         script2_test = pubkey_to_script2(pubkey)
291     except:
292         pass
293     else:
294         if script2_test == script2:
295             return pubkey_to_address(pubkey, net)
296     
297     try:
298         pubkey_hash = pack.IntType(160).unpack(script2[3:-2])
299         script2_test2 = pubkey_hash_to_script2(pubkey_hash)
300     except:
301         pass
302     else:
303         if script2_test2 == script2:
304             return pubkey_hash_to_address(pubkey_hash, net)
305
306 def script2_to_human(script2, net):
307     try:
308         pubkey = script2[1:-1]
309         script2_test = pubkey_to_script2(pubkey)
310     except:
311         pass
312     else:
313         if script2_test == script2:
314             return 'Pubkey. Address: %s' % (pubkey_to_address(pubkey, net),)
315     
316     try:
317         pubkey_hash = pack.IntType(160).unpack(script2[3:-2])
318         script2_test2 = pubkey_hash_to_script2(pubkey_hash)
319     except:
320         pass
321     else:
322         if script2_test2 == script2:
323             return 'Address. Address: %s' % (pubkey_hash_to_address(pubkey_hash, net),)
324     
325     return 'Unknown. Script: %s'  % (script2.encode('hex'),)