moved getwork checks to testcases
[p2pool.git] / p2pool / bitcoin / getwork.py
1 '''
2 Representation of a getwork request/reply
3 '''
4
5 from __future__ import division
6
7 from . import data as bitcoin_data
8 from . import sha256
9 from p2pool.util import pack
10
11 def _swap4(s):
12     if len(s) % 4:
13         raise ValueError()
14     return ''.join(s[x:x+4][::-1] for x in xrange(0, len(s), 4))
15
16 class BlockAttempt(object):
17     def __init__(self, version, previous_block, merkle_root, timestamp, bits, share_target):
18         self.version, self.previous_block, self.merkle_root, self.timestamp, self.bits, self.share_target = version, previous_block, merkle_root, timestamp, bits, share_target
19     
20     def __hash__(self):
21         return hash((self.version, self.previous_block, self.merkle_root, self.timestamp, self.bits, self.share_target))
22     
23     def __eq__(self, other):
24         if not isinstance(other, BlockAttempt):
25             raise ValueError('comparisons only valid with other BlockAttempts')
26         return self.__dict__ == other.__dict__
27     
28     def __ne__(self, other):
29         return not (self == other)
30     
31     def __repr__(self):
32         return 'BlockAttempt(%s)' % (', '.join('%s=%r' % (k, v) for k, v in self.__dict__.iteritems()),)
33     
34     def getwork(self, **extra):
35         if 'data' in extra or 'hash1' in extra or 'target' in extra or 'midstate' in extra:
36             raise ValueError()
37         
38         block_data = bitcoin_data.block_header_type.pack(dict(
39             version=self.version,
40             previous_block=self.previous_block,
41             merkle_root=self.merkle_root,
42             timestamp=self.timestamp,
43             bits=self.bits,
44             nonce=0,
45         ))
46         
47         getwork = {
48             'data': _swap4(block_data).encode('hex') + '000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000',
49             'hash1': '00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000',
50             'target': pack.IntType(256).pack(self.share_target).encode('hex'),
51             'midstate': _swap4(sha256.process(sha256.initial_state, block_data[:64])).encode('hex'),
52         }
53         
54         getwork = dict(getwork)
55         getwork.update(extra)
56         
57         return getwork
58     
59     @classmethod
60     def from_getwork(cls, getwork):
61         attrs = decode_data(getwork['data'])
62         
63         return cls(
64             version=attrs['version'],
65             previous_block=attrs['previous_block'],
66             merkle_root=attrs['merkle_root'],
67             timestamp=attrs['timestamp'],
68             bits=attrs['bits'],
69             share_target=pack.IntType(256).unpack(getwork['target'].decode('hex')),
70         )
71     
72     def update(self, **kwargs):
73         d = self.__dict__.copy()
74         d.update(kwargs)
75         return self.__class__(**d)
76
77 def decode_data(data):
78     return bitcoin_data.block_header_type.unpack(_swap4(data.decode('hex'))[:80])