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