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