moved getwork checks to testcases
authorForrest Voight <forrest@forre.st>
Fri, 26 Oct 2012 04:53:52 +0000 (00:53 -0400)
committerForrest Voight <forrest@forre.st>
Sun, 28 Oct 2012 06:08:20 +0000 (02:08 -0400)
p2pool/bitcoin/getwork.py
p2pool/test/bitcoin/test_getwork.py

index 529a163..721b8ba 100644 (file)
@@ -31,7 +31,7 @@ class BlockAttempt(object):
     def __repr__(self):
         return 'BlockAttempt(%s)' % (', '.join('%s=%r' % (k, v) for k, v in self.__dict__.iteritems()),)
     
-    def getwork(self, _check=False, **extra):
+    def getwork(self, **extra):
         if 'data' in extra or 'hash1' in extra or 'target' in extra or 'midstate' in extra:
             raise ValueError()
         
@@ -51,21 +51,16 @@ class BlockAttempt(object):
             'midstate': _swap4(sha256.process(sha256.initial_state, block_data[:64])).encode('hex'),
         }
         
-        if _check:
-            self_check = self.__class__.from_getwork(getwork, _check=False)
-            if self_check != self:
-                raise AssertionError('failed check - input invalid or implementation error')
-        
         getwork = dict(getwork)
         getwork.update(extra)
         
         return getwork
     
     @classmethod
-    def from_getwork(cls, getwork, _check=True):
+    def from_getwork(cls, getwork):
         attrs = decode_data(getwork['data'])
         
-        ba = cls(
+        return cls(
             version=attrs['version'],
             previous_block=attrs['previous_block'],
             merkle_root=attrs['merkle_root'],
@@ -73,16 +68,6 @@ class BlockAttempt(object):
             bits=attrs['bits'],
             share_target=pack.IntType(256).unpack(getwork['target'].decode('hex')),
         )
-        
-        if _check:
-            extra = dict(getwork)
-            del extra['data'], extra['hash1'], extra['target']
-            extra.pop('midstate', None)
-            getwork_check = ba.getwork(_check=False, **extra)
-            if getwork_check != getwork and dict((k, v) for k, v in getwork_check.iteritems() if k != 'midstate') != getwork:
-                raise AssertionError('failed check - input invalid or implementation error')
-        
-        return ba
     
     def update(self, **kwargs):
         d = self.__dict__.copy()
index 029b87b..58776f9 100644 (file)
@@ -4,52 +4,66 @@ from p2pool.bitcoin import getwork, data as bitcoin_data
 
 class Test(unittest.TestCase):
     def test_all(self):
-        getwork.BlockAttempt.from_getwork({
-            'target': '0000000000000000000000000000000000000000000000f2b944000000000000',
-            'midstate': '5982f893102dec03e374b472647c4f19b1b6d21ae4b2ac624f3d2f41b9719404',
-            'hash1': '00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000',
-            'data': '0000000163930d52a5ffca79b29b95a659a302cd4e1654194780499000002274000000002e133d9e51f45bc0886d05252038e421e82bff18b67dc14b90d9c3c2f422cd5c4dd4598e1a44b9f200000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000'
-    }, _check=100)
-        getwork.BlockAttempt.from_getwork({
-            'midstate' : 'f4a9b048c0cb9791bc94b13ee0eec21e713963d524fd140b58bb754dd7b0955f',
-            'data' : '000000019a1d7342fb62090bda686b22d90f9f73d0f5c418b9c980cd0000011a00000000680b07c8a2f97ecd831f951806857e09f98a3b81cdef1fa71982934fef8dc3444e18585d1a0abbcf00000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000',
-            'hash1' : '00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000',
-            'target' : '0000000000000000000000000000000000000000000000cfbb0a000000000000',
-            'extrathing': 'hi!',
-        })
-        getwork.BlockAttempt.from_getwork({
-            'data' : '000000019a1d7342fb62090bda686b22d90f9f73d0f5c418b9c980cd0000011a00000000680b07c8a2f97ecd831f951806857e09f98a3b81cdef1fa71982934fef8dc3444e18585d1a0abbcf00000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000',
-            'hash1' : '00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000',
-            'target' : '0000000000000000000000000000000000000000000000cfbb0a000000000000',
-            'extrathing': 'hi!',
-        })
-        ba = getwork.BlockAttempt(
-            1,
-            0x148135e10208db85abb62754341a392eab1f186aab077a831cf7,
-            0x534ea08be1ab529f484369344b6d5423ef5a0767db9b3ebb4e182bbb67962520,
-            1305759879,
-            bitcoin_data.FloatingInteger.from_target_upper_bound(0x44b9f20000000000000000000000000000000000000000000000),
-            0x44b9f20000000000000000000000000000000000000000000000,
-        )
-        ba.getwork(100)
-        ba = getwork.BlockAttempt(
-            1,
-            0x148135e10208db85abb62754341a392eab1f186aab077a831cf7,
-            0x534ea08be1ab529f484369344b6d5423ef5a0767db9b3ebb4e182bbb67962520,
-            1305759879,
-            bitcoin_data.FloatingInteger.from_target_upper_bound(0x44b9f20000000000000000000000000000000000000000000000),
-            432*2**230,
-        )
-        ba.getwork(100)
-        ba = getwork.BlockAttempt(
-            1,
-            0x148135e10208db85abb62754341a392eab1f186aab077a831cf7,
-            0x534ea08be1ab529f484369344b6d5423ef5a0767db9b3ebb4e182bbb67962520,
-            1305759879,
-            bitcoin_data.FloatingInteger.from_target_upper_bound(0x44b9f20000000000000000000000000000000000000000000000),
-            7*2**240,
-        )
-        ba.getwork(100)
-        ba.getwork()
-        ba.getwork(_check=100)
-        ba.update(previous_block=ba.previous_block - 10).getwork(_check=100, ident="hi")
+        cases = [
+            {
+                'target': '0000000000000000000000000000000000000000000000f2b944000000000000',
+                'midstate': '5982f893102dec03e374b472647c4f19b1b6d21ae4b2ac624f3d2f41b9719404',
+                'hash1': '00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000',
+                'data': '0000000163930d52a5ffca79b29b95a659a302cd4e1654194780499000002274000000002e133d9e51f45bc0886d05252038e421e82bff18b67dc14b90d9c3c2f422cd5c4dd4598e1a44b9f200000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000'
+            },
+            {
+                'midstate' : 'f4a9b048c0cb9791bc94b13ee0eec21e713963d524fd140b58bb754dd7b0955f',
+                'data' : '000000019a1d7342fb62090bda686b22d90f9f73d0f5c418b9c980cd0000011a00000000680b07c8a2f97ecd831f951806857e09f98a3b81cdef1fa71982934fef8dc3444e18585d1a0abbcf00000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000',
+                'hash1' : '00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000',
+                'target' : '0000000000000000000000000000000000000000000000cfbb0a000000000000',
+                'extrathing': 'hi!',
+            },
+            {
+                'data' : '000000019a1d7342fb62090bda686b22d90f9f73d0f5c418b9c980cd0000011a00000000680b07c8a2f97ecd831f951806857e09f98a3b81cdef1fa71982934fef8dc3444e18585d1a0abbcf00000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000',
+                'hash1' : '00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000',
+                'target' : '0000000000000000000000000000000000000000000000cfbb0a000000000000',
+                'extrathing': 'hi!',
+            },
+        ]
+        for case in cases:
+            ba = getwork.BlockAttempt.from_getwork(case)
+            
+            extra = dict(case)
+            del extra['data'], extra['hash1'], extra['target']
+            extra.pop('midstate', None)
+            
+            getwork_check = ba.getwork(**extra)
+            assert getwork_check == case or dict((k, v) for k, v in getwork_check.iteritems() if k != 'midstate') == case
+        
+        case2s = [
+            getwork.BlockAttempt(
+                1,
+                0x148135e10208db85abb62754341a392eab1f186aab077a831cf7,
+                0x534ea08be1ab529f484369344b6d5423ef5a0767db9b3ebb4e182bbb67962520,
+                1305759879,
+                bitcoin_data.FloatingInteger.from_target_upper_bound(0x44b9f20000000000000000000000000000000000000000000000),
+                0x44b9f20000000000000000000000000000000000000000000000,
+            ),
+            getwork.BlockAttempt(
+                1,
+                0x148135e10208db85abb62754341a392eab1f186aab077a831cf7,
+                0x534ea08be1ab529f484369344b6d5423ef5a0767db9b3ebb4e182bbb67962520,
+                1305759879,
+                bitcoin_data.FloatingInteger.from_target_upper_bound(0x44b9f20000000000000000000000000000000000000000000000),
+                432*2**230,
+            ),
+            getwork.BlockAttempt(
+                1,
+                0x148135e10208db85abb62754341a392eab1f186aab077a831cf7,
+                0x534ea08be1ab529f484369344b6d5423ef5a0767db9b3ebb4e182bbb67962520,
+                1305759879,
+                bitcoin_data.FloatingInteger.from_target_upper_bound(0x44b9f20000000000000000000000000000000000000000000000),
+                7*2**240,
+            )
+        ]
+        for case2 in case2s:
+            assert getwork.BlockAttempt.from_getwork(case2.getwork()) == case2
+            assert getwork.BlockAttempt.from_getwork(case2.getwork(ident='hi')) == case2
+            case2 = case2.update(previous_block=case2.previous_block - 10)
+            assert getwork.BlockAttempt.from_getwork(case2.getwork()) == case2
+            assert getwork.BlockAttempt.from_getwork(case2.getwork(ident='hi')) == case2