protocol change scheduled for mar 4 and feb 26 for litecoin 0.9
authorForrest Voight <forrest@forre.st>
Fri, 17 Feb 2012 18:57:28 +0000 (13:57 -0500)
committerForrest Voight <forrest@forre.st>
Sun, 19 Feb 2012 06:48:27 +0000 (01:48 -0500)
p2pool/bitcoin/data.py
p2pool/data.py
p2pool/main.py
p2pool/networks.py
p2pool/test/test_data.py [new file with mode: 0644]
p2pool/util/pack.py

index 8f53d2a..ad95ff3 100644 (file)
@@ -209,6 +209,9 @@ def target_to_average_attempts(target):
 def target_to_difficulty(target):
     return (0xffff0000 * 2**(256-64) + 1)/(target + 1)
 
+def difficulty_to_target(difficulty):
+    return (0xffff0000 * 2**(256-64) + 1)/difficulty - 1
+
 # tx
 
 def tx_get_sigop_count(tx):
index dce21ec..49707c8 100644 (file)
@@ -1,16 +1,38 @@
 from __future__ import division
 
+import hashlib
+import os
 import random
 import time
-import os
 
 from twisted.python import log
 
 import p2pool
 from p2pool import skiplists
-from p2pool.bitcoin import data as bitcoin_data, script
+from p2pool.bitcoin import data as bitcoin_data, script, sha256
 from p2pool.util import math, forest, pack
 
+# hashlink
+
+hash_link_type = pack.ComposedType([
+    ('state', pack.FixedStrType(32)),
+    ('extra_data', pack.FixedStrType(0)), # bit of a hack, but since the donation script is at the end, const_ending is long enough to always make this empty
+    ('length', pack.VarIntType()),
+])
+
+def prefix_to_hash_link(prefix, const_ending=''):
+    assert prefix.endswith(const_ending), (prefix, const_ending)
+    x = sha256.sha256(prefix)
+    return dict(state=x.state, extra_data=x.buf[:max(0, len(x.buf)-len(const_ending))], length=x.length//8)
+
+def check_hash_link(hash_link, data, const_ending=''):
+    extra_length = hash_link['length'] % (512//8)
+    assert len(hash_link['extra_data']) == max(0, extra_length - len(const_ending))
+    extra = (hash_link['extra_data'] + const_ending)[len(hash_link['extra_data']) + len(const_ending) - extra_length:]
+    assert len(extra) == extra_length
+    return pack.IntType(256).unpack(hashlib.sha256(sha256.sha256(data, (hash_link['state'], extra, 8*hash_link['length'])).digest()).digest())
+
+# shares
 
 share_data_type = pack.ComposedType([
     ('previous_share_hash', pack.PossiblyNoneType(0, pack.IntType(256))),
@@ -40,9 +62,53 @@ share1b_type = pack.ComposedType([
     ('other_txs', pack.ListType(bitcoin_data.tx_type)),
 ])
 
+# new
+
+small_block_header_type = pack.ComposedType([
+    ('version', pack.VarIntType()), # XXX must be constrained to 32 bits
+    ('previous_block', pack.PossiblyNoneType(0, pack.IntType(256))),
+    ('timestamp', pack.IntType(32)),
+    ('bits', bitcoin_data.FloatingIntegerType()),
+    ('nonce', pack.IntType(32)),
+])
+
+new_share_data_type = pack.ComposedType([
+    ('previous_share_hash', pack.PossiblyNoneType(0, pack.IntType(256))),
+    ('coinbase', pack.VarStrType()),
+    ('nonce', pack.IntType(32)),
+    ('pubkey_hash', pack.IntType(160)),
+    ('subsidy', pack.IntType(64)),
+    ('donation', pack.IntType(16)),
+    ('stale_info', pack.IntType(8)), # 0 nothing, 253 orphan, 254 doa
+])
+
+new_share_info_type = pack.ComposedType([
+    ('share_data', new_share_data_type),
+    ('max_bits', bitcoin_data.FloatingIntegerType()),
+    ('bits', bitcoin_data.FloatingIntegerType()),
+    ('timestamp', pack.IntType(32)),
+])
+
+new_share1a_type = pack.ComposedType([
+    ('min_header', small_block_header_type),
+    ('share_info', new_share_info_type),
+    ('hash_link', hash_link_type),
+    ('merkle_branch', bitcoin_data.merkle_branch_type),
+])
+
+new_share1b_type = pack.ComposedType([
+    ('min_header', small_block_header_type),
+    ('share_info', new_share_info_type),
+    ('hash_link', hash_link_type),
+    ('other_txs', pack.ListType(bitcoin_data.tx_type)),
+])
+
+
 # type:
 # 0: share1a
 # 1: share1b
+# 2: new_share1a
+# 3: new_share1b
 
 share_type = pack.ComposedType([
     ('type', pack.VarIntType()),
@@ -65,6 +131,17 @@ class Share(object):
             if not (res.pow_hash <= res.header['bits'].target):
                 raise ValueError('invalid share type')
             return res
+        elif share['type'] == 2:
+            res = NewShare(net, **new_share1a_type.unpack(share['contents']))
+            if not (res.pow_hash > res.header['bits'].target):
+                raise ValueError('invalid share type')
+            return res
+        elif share['type'] == 3:
+            share1b = new_share1b_type.unpack(share['contents'])
+            res = NewShare(net, merkle_branch=bitcoin_data.calculate_merkle_branch([0] + [bitcoin_data.hash256(bitcoin_data.tx_type.pack(x)) for x in share1b['other_txs']], 0), **share1b)
+            if not (res.pow_hash <= res.header['bits'].target):
+                raise ValueError('invalid share type')
+            return res
         else:
             raise ValueError('unknown share type: %r' % (share['type'],))
     
@@ -84,6 +161,8 @@ class Share(object):
         self.share_data = self.share_info['share_data']
         self.target = self.max_target = self.share_info['bits'].target
         self.timestamp = self.share_info['timestamp']
+        if self.timestamp > net.SWITCH_TIME + 60*60:
+            raise ValueError('old share an hour after switch time')
         
         if len(self.share_data['new_script']) > 100:
             raise ValueError('new_script too long!')
@@ -234,6 +313,159 @@ def generate_transaction(tracker, share_data, block_target, desired_timestamp, n
         lock_time=0,
     )
 
+ref_type = pack.ComposedType([
+    ('identifier', pack.FixedStrType(64//8)),
+    ('share_info', new_share_info_type),
+])
+
+gentx_before_refhash = pack.VarStrType().pack(DONATION_SCRIPT) + pack.IntType(64).pack(0) + pack.VarStrType().pack('\x20' + pack.IntType(256).pack(0))[:2]
+
+def new_generate_transaction(tracker, share_data, block_target, desired_timestamp, desired_target, net):
+    previous_share = tracker.shares[share_data['previous_share_hash']] if share_data['previous_share_hash'] is not None else None
+    
+    height, last = tracker.get_height_and_last(share_data['previous_share_hash'])
+    assert height >= net.REAL_CHAIN_LENGTH or last is None
+    if height < net.TARGET_LOOKBEHIND:
+        pre_target3 = net.MAX_TARGET
+    else:
+        attempts_per_second = get_pool_attempts_per_second(tracker, share_data['previous_share_hash'], net.TARGET_LOOKBEHIND, min_work=True)
+        pre_target = 2**256//(net.SHARE_PERIOD*attempts_per_second) - 1
+        pre_target2 = math.clip(pre_target, (previous_share.max_target*9//10, previous_share.max_target*11//10))
+        pre_target3 = math.clip(pre_target2, (0, net.MAX_TARGET))
+    max_bits = bitcoin_data.FloatingInteger.from_target_upper_bound(pre_target3)
+    bits = bitcoin_data.FloatingInteger.from_target_upper_bound(math.clip(desired_target, (pre_target3//10, pre_target3)))
+    
+    weights, total_weight, donation_weight = tracker.get_cumulative_weights(share_data['previous_share_hash'],
+        min(height, net.REAL_CHAIN_LENGTH),
+        65535*net.SPREAD*bitcoin_data.target_to_average_attempts(block_target),
+    )
+    assert total_weight == sum(weights.itervalues()) + donation_weight, (total_weight, sum(weights.itervalues()) + donation_weight)
+    
+    amounts = dict((script, share_data['subsidy']*(199*weight)//(200*total_weight)) for script, weight in weights.iteritems()) # 99.5% goes according to weights prior to this share
+    this_script = bitcoin_data.pubkey_hash_to_script2(share_data['pubkey_hash'])
+    amounts[this_script] = amounts.get(this_script, 0) + share_data['subsidy']//200 # 0.5% goes to block finder
+    amounts[DONATION_SCRIPT] = amounts.get(DONATION_SCRIPT, 0) + share_data['subsidy'] - sum(amounts.itervalues()) # all that's left over is the donation weight and some extra satoshis due to rounding
+    
+    if sum(amounts.itervalues()) != share_data['subsidy'] or any(x < 0 for x in amounts.itervalues()):
+        raise ValueError()
+    
+    dests = sorted(amounts.iterkeys(), key=lambda script: (script == DONATION_SCRIPT, amounts[script], script))[-4000:] # block length limit, unlikely to ever be hit
+    
+    share_info = dict(
+        share_data=share_data,
+        max_bits=max_bits,
+        bits=bits,
+        timestamp=math.clip(desired_timestamp, (
+            (previous_share.timestamp + net.SHARE_PERIOD) - (net.SHARE_PERIOD - 1), # = previous_share.timestamp + 1
+            (previous_share.timestamp + net.SHARE_PERIOD) + (net.SHARE_PERIOD - 1),
+        )) if previous_share is not None else desired_timestamp,
+    )
+    
+    return share_info, dict(
+        version=1,
+        tx_ins=[dict(
+            previous_output=None,
+            sequence=None,
+            script=share_data['coinbase'].ljust(2, '\x00'),
+        )],
+        tx_outs=[dict(value=amounts[script], script=script) for script in dests if amounts[script]] + [dict(
+            value=0,
+            script='\x20' + pack.IntType(256).pack(bitcoin_data.hash256(ref_type.pack(dict(
+                identifier=net.IDENTIFIER,
+                share_info=share_info,
+            )))),
+        )],
+        lock_time=0,
+    )
+
+class NewShare(object):
+    __slots__ = 'net min_header share_info hash_link merkle_branch other_txs hash share_data max_target target timestamp previous_hash new_script gentx_hash header pow_hash header_hash time_seen peer'.split(' ')
+    
+    def __init__(self, net, min_header, share_info, hash_link, merkle_branch, other_txs=None):
+        if len(share_info['share_data']['coinbase']) > 100:
+            raise ValueError('''coinbase too large! %i bytes''' % (len(self.share_data['coinbase']),))
+        
+        if len(merkle_branch) > 16:
+            raise ValueError('merkle_branch too long!')
+        
+        if p2pool.DEBUG and other_txs is not None and bitcoin_data.calculate_merkle_branch([0] + [bitcoin_data.hash256(bitcoin_data.tx_type.pack(x)) for x in other_txs], 0) != merkle_branch:
+            raise ValueError('merkle_branch and other_txs do not match')
+        
+        assert not hash_link['extra_data'], repr(hash_link['extra_data'])
+        
+        self.net = net
+        self.min_header = min_header
+        self.share_info = share_info
+        self.hash_link = hash_link
+        self.merkle_branch = merkle_branch
+        self.other_txs = other_txs
+        
+        self.hash = bitcoin_data.hash256(share_type.pack(self.as_share(block_hint=other_txs is not None)))
+        self.share_data = self.share_info['share_data']
+        self.max_target = self.share_info['max_bits'].target
+        self.target = self.share_info['bits'].target
+        self.timestamp = self.share_info['timestamp']
+        self.previous_hash = self.share_data['previous_share_hash']
+        self.new_script = bitcoin_data.pubkey_hash_to_script2(self.share_data['pubkey_hash'])
+        if self.timestamp < net.SWITCH_TIME - 60*60:
+            raise ValueError('new share an hour before switch time')
+        
+        self.gentx_hash = check_hash_link(
+            hash_link,
+            pack.IntType(256).pack(bitcoin_data.hash256(ref_type.pack(dict(
+                identifier=net.IDENTIFIER,
+                share_info=share_info,
+            )))) + pack.IntType(32).pack(0),
+            gentx_before_refhash,
+        )
+        merkle_root = bitcoin_data.check_merkle_branch(self.gentx_hash, 0, merkle_branch)
+        self.header = dict(min_header, merkle_root=merkle_root)
+        self.pow_hash = net.PARENT.POW_FUNC(bitcoin_data.block_header_type.pack(self.header))
+        self.header_hash = bitcoin_data.hash256(bitcoin_data.block_header_type.pack(self.header))
+        
+        if self.pow_hash > self.target:
+            print 'hash %x' % self.pow_hash
+            print 'targ %x' % self.target
+            raise ValueError('not enough work!')
+        
+        if other_txs is not None and not self.pow_hash <= self.header['bits'].target:
+            raise ValueError('other_txs provided when not a block solution')
+        if other_txs is None and self.pow_hash <= self.header['bits'].target:
+            raise ValueError('other_txs not provided when a block solution')
+        
+        # XXX eww
+        self.time_seen = time.time()
+        self.peer = None
+    
+    def __repr__(self):
+        return '<Share %s>' % (' '.join('%s=%r' % (k, getattr(self, k)) for k in self.__slots__),)
+    
+    def check(self, tracker):
+        share_info, gentx = new_generate_transaction(tracker, self.share_info['share_data'], self.header['bits'].target, self.share_info['timestamp'], self.share_info['bits'].target, self.net)
+        if share_info != self.share_info:
+            raise ValueError('share difficulty invalid')
+        if bitcoin_data.hash256(bitcoin_data.tx_type.pack(gentx)) != self.gentx_hash:
+            raise ValueError('''gentx doesn't match hash_link''')
+    
+    def as_share(self, block_hint=None):
+        if block_hint is None:
+            block_hint = self.pow_hash <= self.header['bits'].target
+        if not block_hint: # share1a
+            return dict(type=2, contents=new_share1a_type.pack(dict(min_header=self.min_header, share_info=self.share_info, hash_link=self.hash_link, merkle_branch=self.merkle_branch)))
+        else: # share1b
+            if self.other_txs is None:
+                raise ValueError('share does not contain all txs')
+            return dict(type=3, contents=new_share1b_type.pack(dict(min_header=self.min_header, share_info=self.share_info, hash_link=self.hash_link, other_txs=self.other_txs)))
+    
+    def as_block(self, tracker):
+        if self.other_txs is None:
+            raise ValueError('share does not contain all txs')
+        
+        share_info, gentx = new_generate_transaction(tracker, self.share_info['share_data'], self.header['bits'].target, self.share_info['timestamp'], self.share_info['bits'].target, self.net)
+        assert share_info == self.share_info
+        
+        return dict(header=self.header, txs=[gentx] + self.other_txs)
+
 class OkayTracker(forest.Tracker):
     def __init__(self, net, my_share_hashes, my_doa_share_hashes):
         forest.Tracker.__init__(self, delta_type=forest.get_attributedelta_type(dict(forest.AttributeDelta.attrs,
index 6c1eb66..28fe2ac 100644 (file)
@@ -457,21 +457,25 @@ def main(args, net, datadir_path, merged_urls, worker_endpoint):
                 self.new_work_event = current_work.changed
                 self.recent_shares_ts_work = []
             
-            def _get_payout_pubkey_hash_from_username(self, user):
-                if user is None:
-                    return None
+            def preprocess_request(self, request):
+                user = request.getUser() if request.getUser() is not None else ''
+                pubkey_hash = my_pubkey_hash
+                max_target = 2**256 - 1
+                if '/' in user:
+                    user, min_diff_str = user.rsplit('/', 1)
+                    try:
+                        max_target = bitcoin_data.difficulty_to_target(float(min_diff_str))
+                    except:
+                        pass
                 try:
-                    return bitcoin_data.address_to_pubkey_hash(user, net.PARENT)
+                    pubkey_hash = bitcoin_data.address_to_pubkey_hash(user, net.PARENT)
                 except: # XXX blah
-                    return None
-            
-            def preprocess_request(self, request):
-                payout_pubkey_hash = self._get_payout_pubkey_hash_from_username(request.getUser())
-                if payout_pubkey_hash is None or random.uniform(0, 100) < args.worker_fee:
-                    payout_pubkey_hash = my_pubkey_hash
-                return payout_pubkey_hash,
+                    pass
+                if random.uniform(0, 100) < args.worker_fee:
+                    pubkey_hash = my_pubkey_hash
+                return pubkey_hash, max_target
             
-            def get_work(self, pubkey_hash):
+            def get_work(self, pubkey_hash, max_target):
                 if len(p2p_node.peers) == 0 and net.PERSIST:
                     raise jsonrpc.Error(-12345, u'p2pool is not connected to any peers')
                 if current_work.value['best_share_hash'] is None and net.PERSIST:
@@ -492,25 +496,49 @@ def main(args, net, datadir_path, merged_urls, worker_endpoint):
                     mm_data = ''
                     mm_later = []
                 
-                share_info, generate_tx = p2pool_data.generate_transaction(
-                    tracker=tracker,
-                    share_data=dict(
-                        previous_share_hash=current_work.value['best_share_hash'],
-                        coinbase=(mm_data + current_work.value['coinbaseflags'])[:100],
-                        nonce=struct.pack('<Q', random.randrange(2**64)),
-                        new_script=bitcoin_data.pubkey_hash_to_script2(pubkey_hash),
-                        subsidy=current_work2.value['subsidy'],
-                        donation=math.perfect_round(65535*args.donation_percentage/100),
-                        stale_info=(lambda (orphans, doas), total, (orphans_recorded_in_chain, doas_recorded_in_chain):
-                            253 if orphans > orphans_recorded_in_chain else
-                            254 if doas > doas_recorded_in_chain else
-                            0
-                        )(*get_stale_counts()),
-                    ),
-                    block_target=current_work.value['bits'].target,
-                    desired_timestamp=int(time.time() - current_work2.value['clock_offset']),
-                    net=net,
-                )
+                new = time.time() > net.SWITCH_TIME
+                
+                if new:
+                    share_info, generate_tx = p2pool_data.new_generate_transaction(
+                        tracker=tracker,
+                        share_data=dict(
+                            previous_share_hash=current_work.value['best_share_hash'],
+                            coinbase=(mm_data + current_work.value['coinbaseflags'])[:100],
+                            nonce=random.randrange(2**32),
+                            pubkey_hash=pubkey_hash,
+                            subsidy=current_work2.value['subsidy'],
+                            donation=math.perfect_round(65535*args.donation_percentage/100),
+                            stale_info=(lambda (orphans, doas), total, (orphans_recorded_in_chain, doas_recorded_in_chain):
+                                253 if orphans > orphans_recorded_in_chain else
+                                254 if doas > doas_recorded_in_chain else
+                                0
+                            )(*get_stale_counts()),
+                        ),
+                        block_target=current_work.value['bits'].target,
+                        desired_timestamp=int(time.time() - current_work2.value['clock_offset']),
+                        desired_target=max_target,
+                        net=net,
+                    )
+                else:
+                    share_info, generate_tx = p2pool_data.generate_transaction(
+                        tracker=tracker,
+                        share_data=dict(
+                            previous_share_hash=current_work.value['best_share_hash'],
+                            coinbase=(mm_data + current_work.value['coinbaseflags'])[:100],
+                            nonce=struct.pack('<Q', random.randrange(2**64)),
+                            new_script=bitcoin_data.pubkey_hash_to_script2(pubkey_hash),
+                            subsidy=current_work2.value['subsidy'],
+                            donation=math.perfect_round(65535*args.donation_percentage/100),
+                            stale_info=(lambda (orphans, doas), total, (orphans_recorded_in_chain, doas_recorded_in_chain):
+                                253 if orphans > orphans_recorded_in_chain else
+                                254 if doas > doas_recorded_in_chain else
+                                0
+                            )(*get_stale_counts()),
+                        ),
+                        block_target=current_work.value['bits'].target,
+                        desired_timestamp=int(time.time() - current_work2.value['clock_offset']),
+                        net=net,
+                    )
                 
                 target = net.PARENT.SANE_MAX_TARGET
                 if len(self.recent_shares_ts_work) == 50:
@@ -521,7 +549,8 @@ def main(args, net, datadir_path, merged_urls, worker_endpoint):
                     target = max(target, aux_work['target'])
                 
                 transactions = [generate_tx] + list(current_work2.value['transactions'])
-                merkle_root = bitcoin_data.check_merkle_branch(bitcoin_data.hash256(bitcoin_data.tx_type.pack(generate_tx)), 0, current_work2.value['merkle_branch'])
+                packed_generate_tx = bitcoin_data.tx_type.pack(generate_tx)
+                merkle_root = bitcoin_data.check_merkle_branch(bitcoin_data.hash256(packed_generate_tx), 0, current_work2.value['merkle_branch'])
                 
                 getwork_time = time.time()
                 merkle_branch = current_work2.value['merkle_branch']
@@ -598,7 +627,12 @@ def main(args, net, datadir_path, merged_urls, worker_endpoint):
                             log.err(None, 'Error while processing merged mining POW:')
                     
                     if pow_hash <= share_info['bits'].target:
-                        share = p2pool_data.Share(net, header, share_info, merkle_branch=merkle_branch, other_txs=transactions[1:] if pow_hash <= header['bits'].target else None)
+                        if new:
+                            min_header = dict(header);del min_header['merkle_root']
+                            hash_link = p2pool_data.prefix_to_hash_link(packed_generate_tx[:-32-4], p2pool_data.gentx_before_refhash)
+                            share = p2pool_data.NewShare(net, min_header, share_info, hash_link=hash_link, merkle_branch=merkle_branch, other_txs=transactions[1:] if pow_hash <= header['bits'].target else None)
+                        else:
+                            share = p2pool_data.Share(net, header, share_info, merkle_branch=merkle_branch, other_txs=transactions[1:] if pow_hash <= header['bits'].target else None)
                         print 'GOT SHARE! %s %s prev %s age %.2fs%s' % (
                             request.getUser(),
                             p2pool_data.format_hash(share.hash),
@@ -664,7 +698,10 @@ def main(args, net, datadir_path, merged_urls, worker_endpoint):
         
         def get_current_txouts():
             share = tracker.shares[current_work.value['best_share_hash']]
-            share_info, gentx = p2pool_data.generate_transaction(tracker, share.share_info['share_data'], share.header['bits'].target, share.share_info['timestamp'], share.net)
+            if isinstance(share, p2pool_data.NewShare):
+                share_info, gentx = p2pool_data.new_generate_transaction(tracker, share.share_info['share_data'], share.header['bits'].target, share.share_info['timestamp'], share.share_info['bits'].target, share.net)
+            else:
+                share_info, gentx = p2pool_data.generate_transaction(tracker, share.share_info['share_data'], share.header['bits'].target, share.share_info['timestamp'], share.net)
             return dict((out['script'], out['value']) for out in gentx['tx_outs'])
         
         def get_current_scaled_txouts(scale, trunc=0):
index ca27b8c..ba2b876 100644 (file)
@@ -22,6 +22,7 @@ nets = dict(
         PERSIST=True,
         WORKER_PORT=9332,
         BOOTSTRAP_ADDRS='74.220.242.6:9334 93.97.192.93 66.90.73.83 67.83.108.0 219.84.64.174 24.167.17.248 109.74.195.142 83.211.86.49 89.78.212.44 94.23.34.145 168.7.116.243 72.14.191.28 94.174.40.189:9344'.split(' '),
+        SWITCH_TIME=1330837200, # Sun Mar  4 00:00:00 2012 EST
     ),
     bitcoin_testnet=math.Object(
         PARENT=networks.nets['bitcoin_testnet'],
@@ -37,6 +38,7 @@ nets = dict(
         PERSIST=False,
         WORKER_PORT=19332,
         BOOTSTRAP_ADDRS='72.14.191.28'.split(' '),
+        SWITCH_TIME=1329638400, # Sun Feb 19 03:00:00 2012 EST
     ),
     
     litecoin=math.Object(
@@ -53,6 +55,7 @@ nets = dict(
         PERSIST=True,
         WORKER_PORT=9327,
         BOOTSTRAP_ADDRS='76.26.53.101 124.205.120.178 190.195.79.161 173.167.113.73 82.161.65.210 67.83.108.0 78.101.67.239 78.100.161.252 87.58.117.233 78.100.162.223 216.239.45.4 78.101.131.221 72.14.191.28 97.81.163.217 69.126.183.240 219.84.64.174 78.101.119.27 89.211.228.244 178.152.122.30 172.16.0.3 76.26.53.101:51319'.split(' '),
+        SWITCH_TIME=1330232400, # Sun Feb 26 00:00:00 2012 EST
     ),
     litecoin_testnet=math.Object(
         PARENT=networks.nets['litecoin_testnet'],
@@ -68,6 +71,7 @@ nets = dict(
         PERSIST=False,
         WORKER_PORT=19327,
         BOOTSTRAP_ADDRS='72.14.191.28'.split(' '),
+        SWITCH_TIME=1329627600, # Sun Feb 19 00:00:00 2012 EST
     ),
 )
 for net_name, net in nets.iteritems():
diff --git a/p2pool/test/test_data.py b/p2pool/test/test_data.py
new file mode 100644 (file)
index 0000000..784dc13
--- /dev/null
@@ -0,0 +1,30 @@
+import random
+import unittest
+
+from p2pool import data
+from p2pool.bitcoin import data as bitcoin_data
+
+def random_bytes(length):
+    return ''.join(chr(random.randrange(2**8)) for i in xrange(length))
+
+class Test(unittest.TestCase):
+    def test_hashlink1(self):
+        for i in xrange(100):
+            d = random_bytes(random.randrange(2048))
+            x = data.prefix_to_hash_link(d)
+            assert data.check_hash_link(x, '') == bitcoin_data.hash256(d)
+    
+    def test_hashlink2(self):
+        for i in xrange(100):
+            d = random_bytes(random.randrange(2048))
+            d2 = random_bytes(random.randrange(2048))
+            x = data.prefix_to_hash_link(d)
+            assert data.check_hash_link(x, d2) == bitcoin_data.hash256(d + d2)
+    
+    def test_hashlink3(self):
+        for i in xrange(100):
+            d = random_bytes(random.randrange(2048))
+            d2 = random_bytes(random.randrange(200))
+            d3 = random_bytes(random.randrange(2048))
+            x = data.prefix_to_hash_link(d + d2, d2)
+            assert data.check_hash_link(x, d3, d2) == bitcoin_data.hash256(d + d2 + d3)
index 10fe3f6..5e7cfbd 100644 (file)
@@ -281,3 +281,15 @@ class PossiblyNoneType(Type):
         if item == self.none_value:
             raise ValueError('none_value used')
         return self.inner.write(file, self.none_value if item is None else item)
+
+class FixedStrType(Type):
+    def __init__(self, length):
+        self.length = length
+    
+    def read(self, file):
+        return read(file, self.length)
+    
+    def write(self, file, item):
+        if len(item) != self.length:
+            raise ValueError('incorrect length item!')
+        return file, item