setup.py
[p2pool.git] / conv.py
1 '''
2 Representation of a getwork request/reply
3 '''
4
5 from __future__ import division
6
7 import struct
8
9 import sha256
10
11 def bits_to_target(bits):
12     return (bits & 0x00ffffff) * 2 ** (8 * ((bits >> 24) - 3))
13
14 def target_to_attempts(target):
15     return (2**257 + target + 1)//(2*target + 2)
16
17 def reverse_chunks(s, l):
18     return ''.join(reversed([s[x:x+l] for x in xrange(0, len(s), l)]))
19
20 class BlockAttempt(object):
21     def __init__(self, version, previous_block, merkle_root, timestamp, bits):
22         self.version, self.previous_block, self.merkle_root, self.timestamp, self.bits = version, previous_block, merkle_root, timestamp, bits
23     
24     def __repr__(self):
25         return '<BlockAttempt %s>' % (' '.join('%s=%s' % (k, hex(v))) for k, v in self.__dict__.iteritems())
26     
27     def __eq__(self, other):
28         if not isinstance(other, BlockAttempt):
29             raise ValueError('comparisons only valid with other BlockAttempts')
30         return self.__dict__ == other.__dict__
31     
32     def __ne__(self, other):
33         return not (self == other)
34     
35     def __repr__(self):
36         return 'BlockAttempt(%s)' % (', '.join('%s=%r' % (k, v) for k, v in self.__dict__.iteritems()),)
37     
38     def getwork(self, target_multiplier=1, _check=2):
39         target = bits_to_target(self.bits) * target_multiplier
40         
41         previous_block2 = reverse_chunks('%064x' % self.previous_block, 8).decode('hex')
42         merkle_root2 = reverse_chunks('%064x' % self.merkle_root, 8).decode('hex')
43         data = struct.pack('>I32s32sIII', self.version, previous_block2, merkle_root2, self.timestamp, self.bits, 0).encode('hex') + '000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000'
44         
45         previous_block3 = ('%064x' % self.previous_block).decode('hex')[::-1]
46         merkle_root3 = ('%064x' % self.merkle_root).decode('hex')[::-1]
47         data2 = struct.pack('<I32s32s', self.version, previous_block3, merkle_root3)
48         
49         getwork = {
50             'data': data,
51             'hash1': '00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000',
52             'target': ('%064x' % (target,)).decode('hex')[::-1].encode('hex'),
53             'midstate': reverse_chunks(sha256.process(data2[:64])[::-1], 4).encode('hex'),
54         }
55         
56         if _check:
57             self2 = self.__class__.from_getwork(getwork, _check=_check - 1, _check_multiplier=target_multiplier)
58             if self2 != self:
59                 raise ValueError('failed check - input invalid or implementation error')
60         
61         return getwork
62     
63     @classmethod
64     def from_getwork(cls, getwork, _check=2, _check_multiplier=1):
65         attrs = decode_data(getwork['data'])
66         attrs.pop('nonce')
67         
68         ba = cls(**attrs)
69         
70         if _check:
71             getwork2 = ba.getwork(_check_multiplier, _check=_check - 1)
72             if getwork2 != getwork:
73                 raise ValueError('failed check - input invalid or implementation error')
74         
75         return ba
76
77 def decode_data(data):
78     version, previous_block, merkle_root, timestamp, bits, nonce = struct.unpack('>I32s32sIII', data[:160].decode('hex'))
79     previous_block = int(reverse_chunks(previous_block.encode('hex'), 8), 16)
80     merkle_root = int(reverse_chunks(merkle_root.encode('hex'), 8), 16)
81     return dict(version=version, previous_block=previous_block, merkle_root=merkle_root, timestamp=timestamp, bits=bits, nonce=nonce)
82
83 if __name__ == '__main__':
84     ba = BlockAttempt(
85         1,
86         0x000000000000148135e10208db85abb62754341a392eab1f186aab077a831cf7,
87         0x534ea08be1ab529f484369344b6d5423ef5a0767db9b3ebb4e182bbb67962520,
88         1305759879,
89         440711666,
90     )
91     ba.getwork(1, 100)
92     ba.getwork(10, 100)
93     ba.from_getwork({
94         'target': '0000000000000000000000000000000000000000000000f2b944000000000000',
95         'midstate': '5982f893102dec03e374b472647c4f19b1b6d21ae4b2ac624f3d2f41b9719404',
96         'hash1': '00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000',
97         'data': '0000000163930d52a5ffca79b29b95a659a302cd4e1654194780499000002274000000002e133d9e51f45bc0886d05252038e421e82bff18b67dc14b90d9c3c2f422cd5c4dd4598e1a44b9f200000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000'
98 }, _check=100)