NVC/PPC protocol changes support
[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     ('timestamp', 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     ('signature', pack.VarStrType()),
135 ])
136
137 # merged mining
138
139 aux_pow_type = pack.ComposedType([
140     ('merkle_tx', merkle_tx_type),
141     ('merkle_link', merkle_link_type),
142     ('parent_block_header', block_header_type),
143 ])
144
145 aux_pow_coinbase_type = pack.ComposedType([
146     ('merkle_root', pack.IntType(256, 'big')),
147     ('size', pack.IntType(32)),
148     ('nonce', pack.IntType(32)),
149 ])
150
151 def make_auxpow_tree(chain_ids):
152     for size in (2**i for i in xrange(31)):
153         if size < len(chain_ids):
154             continue
155         res = {}
156         for chain_id in chain_ids:
157             pos = (1103515245 * chain_id + 1103515245 * 12345 + 12345) % size
158             if pos in res:
159                 break
160             res[pos] = chain_id
161         else:
162             return res, size
163     raise AssertionError()
164
165 # merkle trees
166
167 merkle_record_type = pack.ComposedType([
168     ('left', pack.IntType(256)),
169     ('right', pack.IntType(256)),
170 ])
171
172 def merkle_hash(hashes):
173     if not hashes:
174         return 0
175     hash_list = list(hashes)
176     while len(hash_list) > 1:
177         hash_list = [hash256(merkle_record_type.pack(dict(left=left, right=right)))
178             for left, right in zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])]
179     return hash_list[0]
180
181 def calculate_merkle_link(hashes, index):
182     # XXX optimize this
183     
184     hash_list = [(lambda _h=h: _h, i == index, []) for i, h in enumerate(hashes)]
185     
186     while len(hash_list) > 1:
187         hash_list = [
188             (
189                 lambda _left=left, _right=right: hash256(merkle_record_type.pack(dict(left=_left(), right=_right()))),
190                 left_f or right_f,
191                 (left_l if left_f else right_l) + [dict(side=1, hash=right) if left_f else dict(side=0, hash=left)],
192             )
193             for (left, left_f, left_l), (right, right_f, right_l) in
194                 zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])
195         ]
196     
197     res = [x['hash']() for x in hash_list[0][2]]
198     
199     assert hash_list[0][1]
200     if p2pool.DEBUG:
201         new_hashes = [random.randrange(2**256) if x is None else x
202             for x in hashes]
203         assert check_merkle_link(new_hashes[index], dict(branch=res, index=index)) == merkle_hash(new_hashes)
204     assert index == sum(k*2**i for i, k in enumerate([1-x['side'] for x in hash_list[0][2]]))
205     
206     return dict(branch=res, index=index)
207
208 def check_merkle_link(tip_hash, link):
209     if link['index'] >= 2**len(link['branch']):
210         raise ValueError('index too large')
211     return reduce(lambda c, (i, h): hash256(merkle_record_type.pack(
212         dict(left=h, right=c) if (link['index'] >> i) & 1 else
213         dict(left=c, right=h)
214     )), enumerate(link['branch']), tip_hash)
215
216 # targets
217
218 def target_to_average_attempts(target):
219     return 2**256//(target + 1)
220
221 def target_to_difficulty(target):
222     return (0xffff0000 * 2**(256-64) + 1)/(target + 1)
223
224 def difficulty_to_target(difficulty):
225     return (0xffff0000 * 2**(256-64) + 1)/difficulty - 1
226
227 # human addresses
228
229 base58_alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
230
231 def base58_encode(bindata):
232     bindata2 = bindata.lstrip(chr(0))
233     return base58_alphabet[0]*(len(bindata) - len(bindata2)) + math.natural_to_string(math.string_to_natural(bindata2), base58_alphabet)
234
235 def base58_decode(b58data):
236     b58data2 = b58data.lstrip(base58_alphabet[0])
237     return chr(0)*(len(b58data) - len(b58data2)) + math.natural_to_string(math.string_to_natural(b58data2, base58_alphabet))
238
239 human_address_type = ChecksummedType(pack.ComposedType([
240     ('version', pack.IntType(8)),
241     ('pubkey_hash', pack.IntType(160)),
242 ]))
243
244 def pubkey_hash_to_address(pubkey_hash, net):
245     return base58_encode(human_address_type.pack(dict(version=net.ADDRESS_VERSION, pubkey_hash=pubkey_hash)))
246
247 def pubkey_to_address(pubkey, net):
248     return pubkey_hash_to_address(hash160(pubkey), net)
249
250 def address_to_pubkey_hash(address, net):
251     x = human_address_type.unpack(base58_decode(address))
252     if x['version'] != net.ADDRESS_VERSION:
253         raise ValueError('address not for this net!')
254     return x['pubkey_hash']
255
256 # transactions
257
258 def pubkey_to_script2(pubkey):
259     assert len(pubkey) <= 75
260     return (chr(len(pubkey)) + pubkey) + '\xac'
261
262 def pubkey_hash_to_script2(pubkey_hash):
263     return '\x76\xa9' + ('\x14' + pack.IntType(160).pack(pubkey_hash)) + '\x88\xac'
264
265 def script2_to_address(script2, net):
266     try:
267         pubkey = script2[1:-1]
268         script2_test = pubkey_to_script2(pubkey)
269     except:
270         pass
271     else:
272         if script2_test == script2:
273             return pubkey_to_address(pubkey, net)
274     
275     try:
276         pubkey_hash = pack.IntType(160).unpack(script2[3:-2])
277         script2_test2 = pubkey_hash_to_script2(pubkey_hash)
278     except:
279         pass
280     else:
281         if script2_test2 == script2:
282             return pubkey_hash_to_address(pubkey_hash, net)
283
284 def script2_to_human(script2, net):
285     try:
286         pubkey = script2[1:-1]
287         script2_test = pubkey_to_script2(pubkey)
288     except:
289         pass
290     else:
291         if script2_test == script2:
292             return 'Pubkey. Address: %s' % (pubkey_to_address(pubkey, net),)
293     
294     try:
295         pubkey_hash = pack.IntType(160).unpack(script2[3:-2])
296         script2_test2 = pubkey_hash_to_script2(pubkey_hash)
297     except:
298         pass
299     else:
300         if script2_test2 == script2:
301             return 'Address. Address: %s' % (pubkey_hash_to_address(pubkey_hash, net),)
302     
303     return 'Unknown. Script: %s'  % (script2.encode('hex'),)