Initial commit
[stratum-mining.git] / lib / extranonce_counter.py
diff --git a/lib/extranonce_counter.py b/lib/extranonce_counter.py
new file mode 100644 (file)
index 0000000..19254f4
--- /dev/null
@@ -0,0 +1,25 @@
+import struct
+
+class ExtranonceCounter(object):
+    '''Implementation of a counter producing
+       unique extranonce across all pool instances.
+       This is just dumb "quick&dirty" solution,
+       but it can be changed at any time without breaking anything.'''       
+
+    def __init__(self, instance_id):
+        if instance_id < 0 or instance_id > 31:
+            raise Exception("Current ExtranonceCounter implementation needs an instance_id in <0, 31>.")
+        
+        # Last 5 most-significant bits represents instance_id
+        # The rest is just an iterator of jobs.
+        self.counter = instance_id << 27
+        self.size = struct.calcsize('>L')
+        
+    def get_size(self):
+        '''Return expected size of generated extranonce in bytes'''
+        return self.size
+    
+    def get_new_bin(self):
+        self.counter += 1
+        return struct.pack('>L', self.counter)
+        
\ No newline at end of file