from __future__ import division
-TRANSITION_TIME = 1322804365
-
-import itertools
+import hashlib
+import os
import random
+import sys
import time
-import os
from twisted.python import log
import p2pool
-from p2pool import skiplists
-from p2pool.bitcoin import data as bitcoin_data, script, networks
-from p2pool.util import memoize, expiring_dict, math
-
-
-merkle_branch_type = bitcoin_data.ListType(bitcoin_data.ComposedType([
- ('side', bitcoin_data.StructType('<B')), # enum?
- ('hash', bitcoin_data.HashType()),
-]))
-
+from p2pool.bitcoin import data as bitcoin_data, script, sha256
+from p2pool.util import math, forest, pack
-share_data_type = bitcoin_data.ComposedType([
- ('previous_share_hash', bitcoin_data.PossiblyNoneType(0, bitcoin_data.HashType())),
- ('target', bitcoin_data.FloatingIntegerType()),
- ('nonce', bitcoin_data.VarStrType()),
-])
-
-
-coinbase_type = bitcoin_data.ComposedType([
- ('identifier', bitcoin_data.FixedStrType(8)),
- ('share_data', share_data_type),
-])
+# hashlink
-share_info_type = bitcoin_data.ComposedType([
- ('share_data', share_data_type),
- ('new_script', bitcoin_data.VarStrType()),
- ('subsidy', bitcoin_data.StructType('<Q')),
+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)
-share1a_type = bitcoin_data.ComposedType([
- ('header', bitcoin_data.block_header_type),
- ('share_info', share_info_type),
- ('merkle_branch', merkle_branch_type),
-])
+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())
-share1b_type = bitcoin_data.ComposedType([
- ('header', bitcoin_data.block_header_type),
- ('share_info', share_info_type),
- ('other_txs', bitcoin_data.ListType(bitcoin_data.tx_type)),
-])
+# shares
-new_share_data_type = bitcoin_data.ComposedType([
- ('previous_share_hash', bitcoin_data.PossiblyNoneType(0, bitcoin_data.HashType())),
- ('coinbase', bitcoin_data.VarStrType()),
- ('nonce', bitcoin_data.VarStrType()),
- ('new_script', bitcoin_data.VarStrType()),
- ('subsidy', bitcoin_data.StructType('<Q')),
- ('donation', bitcoin_data.StructType('<H')),
- ('stale_frac', bitcoin_data.StructType('<B')),
+share_type = pack.ComposedType([
+ ('type', pack.VarIntType()),
+ ('contents', pack.VarStrType()),
])
-new_share_info_type = bitcoin_data.ComposedType([
- ('new_share_data', new_share_data_type),
- ('target', bitcoin_data.FloatingIntegerType()),
- ('timestamp', bitcoin_data.StructType('<I')),
-])
-
-new_share1a_type = bitcoin_data.ComposedType([
- ('header', bitcoin_data.block_header_type),
- ('share_info', new_share_info_type),
- ('merkle_branch', bitcoin_data.merkle_branch_type),
-])
-
-new_share1b_type = bitcoin_data.ComposedType([
- ('header', bitcoin_data.block_header_type),
- ('share_info', new_share_info_type),
- ('other_txs', bitcoin_data.ListType(bitcoin_data.tx_type)),
-])
-
-# type:
-# 0: new_share1a
-# 1: new_share1b
-
-new_share_type = bitcoin_data.ComposedType([
- ('type', bitcoin_data.VarIntType()),
- ('contents', bitcoin_data.VarStrType()),
-])
-
-def calculate_merkle_branch(txs, index):
- hash_list = [(bitcoin_data.tx_type.hash256(tx), i == index, []) for i, tx in enumerate(txs)]
-
- while len(hash_list) > 1:
- hash_list = [
- (
- bitcoin_data.merkle_record_type.hash256(dict(left=left, right=right)),
- left_f or right_f,
- (left_l if left_f else right_l) + [dict(side=1, hash=right) if left_f else dict(side=0, hash=left)],
- )
- for (left, left_f, left_l), (right, right_f, right_l) in
- zip(hash_list[::2], hash_list[1::2] + [hash_list[::2][-1]])
- ]
-
- assert hash_list[0][1]
- assert check_merkle_branch(txs[index], hash_list[0][2]) == hash_list[0][0]
-
- return hash_list[0][2]
-
-def check_merkle_branch(tx, branch):
- hash_ = bitcoin_data.tx_type.hash256(tx)
- for step in branch:
- if not step['side']:
- hash_ = bitcoin_data.merkle_record_type.hash256(dict(left=step['hash'], right=hash_))
- else:
- hash_ = bitcoin_data.merkle_record_type.hash256(dict(left=hash_, right=step['hash']))
- return hash_
+def load_share(share, net, peer_addr):
+ assert peer_addr is None or isinstance(peer_addr, tuple)
+ if share['type'] < Share.VERSION:
+ from p2pool import p2p
+ raise p2p.PeerMisbehavingError('sent an obsolete share')
+ elif share['type'] == Share.VERSION:
+ return Share(net, peer_addr, Share.share_type.unpack(share['contents']))
+ elif share['type'] == NewShare.VERSION:
+ return NewShare(net, peer_addr, NewShare.share_type.unpack(share['contents']))
+ else:
+ raise ValueError('unknown share type: %r' % (share['type'],))
-def share_info_to_gentx(share_info, block_target, tracker, net):
- return generate_transaction(
- tracker=tracker,
- previous_share_hash=share_info['share_data']['previous_share_hash'],
- new_script=share_info['new_script'],
- subsidy=share_info['subsidy'],
- nonce=share_info['share_data']['nonce'],
- block_target=block_target,
- net=net,
- )[1]
+DONATION_SCRIPT = '4104ffd03de44a6e11b9917f3a29f9443283d9871c9d743ef30d5eddcd37094b64d1b3d8090496b53256786bf5c82932ec23c3b74d9f05a6f95a8b5529352656664bac'.decode('hex')
-class Share(object):
- donation = int(0.005*65535 + 0.5)
+class NewShare(object):
+ VERSION = 13
+ VOTING_VERSION = 13
+ SUCCESSOR = None
+
+ small_block_header_type = pack.ComposedType([
+ ('version', pack.VarIntType()),
+ ('previous_block', pack.PossiblyNoneType(0, pack.IntType(256))),
+ ('timestamp', pack.IntType(32)),
+ ('bits', bitcoin_data.FloatingIntegerType()),
+ ('nonce', pack.IntType(32)),
+ ])
+
+ share_info_type = pack.ComposedType([
+ ('share_data', 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.EnumType(pack.IntType(8), dict((k, {0: None, 253: 'orphan', 254: 'doa'}.get(k, 'unk%i' % (k,))) for k in xrange(256)))),
+ ('desired_version', pack.VarIntType()),
+ ])),
+ ('new_transaction_hashes', pack.ListType(pack.IntType(256))),
+ ('transaction_hash_refs', pack.ListType(pack.VarIntType(), 2)), # pairs of share_count, tx_count
+ ('far_share_hash', pack.PossiblyNoneType(0, pack.IntType(256))),
+ ('max_bits', bitcoin_data.FloatingIntegerType()),
+ ('bits', bitcoin_data.FloatingIntegerType()),
+ ('timestamp', pack.IntType(32)),
+ ])
+
+ share_type = pack.ComposedType([
+ ('min_header', small_block_header_type),
+ ('share_info', share_info_type),
+ ('ref_merkle_link', pack.ComposedType([
+ ('branch', pack.ListType(pack.IntType(256))),
+ ('index', pack.IntType(0)),
+ ])),
+ ('last_txout_nonce', pack.IntType(32)),
+ ('hash_link', hash_link_type),
+ ('merkle_link', pack.ComposedType([
+ ('branch', pack.ListType(pack.IntType(256))),
+ ('index', pack.IntType(0)), # it will always be 0
+ ])),
+ ])
+
+ ref_type = pack.ComposedType([
+ ('identifier', pack.FixedStrType(64//8)),
+ ('share_info', share_info_type),
+ ])
+
+ gentx_before_refhash = pack.VarStrType().pack(DONATION_SCRIPT) + pack.IntType(64).pack(0) + pack.VarStrType().pack('\x24' + pack.IntType(256).pack(0) + pack.IntType(32).pack(0))[:2]
@classmethod
- def from_share1a(cls, share1a, net):
- return cls(net, **share1a)
+ def generate_transaction(cls, tracker, share_data, block_target, desired_timestamp, desired_target, ref_merkle_link, desired_other_transaction_hashes_and_fees, net, known_txs=None, last_txout_nonce=0, base_subsidy=None):
+ previous_share = tracker.items[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, integer=True)
+ pre_target = 2**256//(net.SHARE_PERIOD*attempts_per_second) - 1 if attempts_per_second else 2**256-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, (net.MIN_TARGET, 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)))
+
+ new_transaction_hashes = []
+ new_transaction_size = 0
+ transaction_hash_refs = []
+ other_transaction_hashes = []
+
+ past_shares = list(tracker.get_chain(share_data['previous_share_hash'], min(height, 100)))
+ tx_hash_to_this = {}
+ for i, share in enumerate(past_shares):
+ for j, tx_hash in enumerate(share.new_transaction_hashes):
+ if tx_hash not in tx_hash_to_this:
+ tx_hash_to_this[tx_hash] = [1+i, j] # share_count, tx_count
+ for tx_hash, fee in desired_other_transaction_hashes_and_fees:
+ if tx_hash in tx_hash_to_this:
+ this = tx_hash_to_this[tx_hash]
+ else:
+ if known_txs is not None:
+ this_size = bitcoin_data.tx_type.packed_size(known_txs[tx_hash])
+ if new_transaction_size + this_size > 50000: # only allow 50 kB of new txns/share
+ break
+ new_transaction_size += this_size
+ new_transaction_hashes.append(tx_hash)
+ this = [0, len(new_transaction_hashes)-1]
+ transaction_hash_refs.extend(this)
+ other_transaction_hashes.append(tx_hash)
+
+ included_transactions = set(other_transaction_hashes)
+ removed_fees = [fee for tx_hash, fee in desired_other_transaction_hashes_and_fees if tx_hash not in included_transactions]
+ definite_fees = sum(0 if fee is None else fee for tx_hash, fee in desired_other_transaction_hashes_and_fees if tx_hash in included_transactions)
+ if None not in removed_fees:
+ share_data = dict(share_data, subsidy=share_data['subsidy'] - sum(removed_fees))
+ else:
+ assert base_subsidy is not None
+ share_data = dict(share_data, subsidy=base_subsidy + definite_fees)
+
+ weights, total_weight, donation_weight = tracker.get_cumulative_weights(previous_share.share_data['previous_share_hash'] if previous_share is not None else None,
+ min(height, net.REAL_CHAIN_LENGTH-1),
+ 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,
+ far_share_hash=None if last is None and height < 99 else tracker.get_nth_parent_hash(share_data['previous_share_hash'], 99),
+ 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,
+ new_transaction_hashes=new_transaction_hashes,
+ transaction_hash_refs=transaction_hash_refs,
+ )
+
+ gentx = dict(
+ version=1,
+ tx_ins=[dict(
+ previous_output=None,
+ sequence=None,
+ script=share_data['coinbase'],
+ )],
+ tx_outs=[dict(value=amounts[script], script=script) for script in dests if amounts[script] or script == DONATION_SCRIPT] + [dict(
+ value=0,
+ script='\x24' + cls.get_ref_hash(net, share_info, ref_merkle_link) + pack.IntType(32).pack(last_txout_nonce),
+ )],
+ lock_time=0,
+ )
+
+ def get_share(header, last_txout_nonce=last_txout_nonce):
+ min_header = dict(header); del min_header['merkle_root']
+ share = cls(net, None, dict(
+ min_header=min_header,
+ share_info=share_info,
+ ref_merkle_link=dict(branch=[], index=0),
+ last_txout_nonce=last_txout_nonce,
+ hash_link=prefix_to_hash_link(bitcoin_data.tx_type.pack(gentx)[:-32-4-4], cls.gentx_before_refhash),
+ merkle_link=bitcoin_data.calculate_merkle_link([None] + other_transaction_hashes, 0),
+ ))
+ assert share.header == header # checks merkle_root
+ return share
+
+ return share_info, gentx, other_transaction_hashes, get_share
@classmethod
- def from_share1b(cls, share1b, net):
- return cls(net, **share1b)
+ def get_ref_hash(cls, net, share_info, ref_merkle_link):
+ return pack.IntType(256).pack(bitcoin_data.check_merkle_link(bitcoin_data.hash256(cls.ref_type.pack(dict(
+ identifier=net.IDENTIFIER,
+ share_info=share_info,
+ ))), ref_merkle_link))
- __slots__ = 'header previous_block share_info merkle_branch other_txs timestamp share_data new_script subsidy previous_hash previous_share_hash target nonce pow_hash header_hash hash time_seen peer'.split(' ')
+ __slots__ = 'net peer_addr contents min_header share_info hash_link merkle_link hash share_data max_target target timestamp previous_hash new_script desired_version gentx_hash header pow_hash header_hash new_transaction_hashes time_seen'.split(' ')
- def __init__(self, net, header, share_info, merkle_branch=None, other_txs=None):
- if merkle_branch is None and other_txs is None:
- raise ValueError('need either merkle_branch or other_txs')
- if other_txs is not None:
- new_merkle_branch = calculate_merkle_branch([dict(version=0, tx_ins=[], tx_outs=[], lock_time=0)] + other_txs, 0)
- if merkle_branch is not None:
- if merke_branch != new_merkle_branch:
- raise ValueError('invalid merkle_branch and other_txs')
- merkle_branch = new_merkle_branch
-
- if len(merkle_branch) > 16:
- raise ValueError('merkle_branch too long!')
+ def __init__(self, net, peer_addr, contents):
+ self.net = net
+ self.peer_addr = peer_addr
+ self.contents = contents
- self.header = header
- self.previous_block = header['previous_block']
- self.share_info = share_info
- self.merkle_branch = merkle_branch
- self.other_txs = other_txs
+ self.min_header = contents['min_header']
+ self.share_info = contents['share_info']
+ self.hash_link = contents['hash_link']
+ self.merkle_link = contents['merkle_link']
- self.timestamp = self.header['timestamp']
+ if not (2 <= len(self.share_info['share_data']['coinbase']) <= 100):
+ raise ValueError('''bad coinbase size! %i bytes''' % (len(self.share_info['share_data']['coinbase']),))
- self.share_data = self.share_info['share_data']
- self.new_script = self.share_info['new_script']
- self.subsidy = self.share_info['subsidy']
+ if len(self.merkle_link['branch']) > 16:
+ raise ValueError('merkle branch too long!')
- if len(self.new_script) > 100:
- raise ValueError('new_script too long!')
+ assert not self.hash_link['extra_data'], repr(self.hash_link['extra_data'])
- self.previous_hash = self.previous_share_hash = self.share_data['previous_share_hash']
- self.target = self.share_data['target']
- self.nonce = self.share_data['nonce']
+ 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'])
+ self.desired_version = self.share_data['desired_version']
- if len(self.nonce) > 100:
- raise ValueError('nonce too long!')
+ n = set()
+ for share_count, tx_count in self.iter_transaction_hash_refs():
+ assert share_count < 110
+ if share_count == 0:
+ n.add(tx_count)
+ assert n == set(range(len(self.share_info['new_transaction_hashes'])))
- self.pow_hash = net.BITCOIN_POW_FUNC(header)
- self.header_hash = bitcoin_data.block_header_type.hash256(header)
+ self.gentx_hash = check_hash_link(
+ self.hash_link,
+ self.get_ref_hash(net, self.share_info, contents['ref_merkle_link']) + pack.IntType(32).pack(self.contents['last_txout_nonce']) + pack.IntType(32).pack(0),
+ self.gentx_before_refhash,
+ )
+ merkle_root = bitcoin_data.check_merkle_link(self.gentx_hash, self.merkle_link)
+ self.header = dict(self.min_header, merkle_root=merkle_root)
+ self.pow_hash = net.PARENT.POW_FUNC(bitcoin_data.block_header_type.pack(self.header))
+ self.hash = self.header_hash = bitcoin_data.hash256(bitcoin_data.block_header_type.pack(self.header))
- if net.BITCOIN_POW_FUNC == bitcoin_data.block_header_type.scrypt:
- # compatibility hack
- self.hash = share1a_type.scrypt(self.as_share1a())
- else:
- self.hash = share1a_type.hash256(self.as_share1a())
+ if self.target > net.MAX_TARGET:
+ from p2pool import p2p
+ raise p2p.PeerMisbehavingError('share target invalid')
if self.pow_hash > self.target:
- print 'hash %x' % self.pow_hash
- print 'targ %x' % self.target
- raise ValueError('not enough work!')
+ from p2pool import p2p
+ raise p2p.PeerMisbehavingError('share PoW invalid')
- if script.get_sigop_count(self.new_script) > 1:
- raise ValueError('too many sigops!')
+ self.new_transaction_hashes = self.share_info['new_transaction_hashes']
# XXX eww
self.time_seen = time.time()
- self.peer = None
- def as_block(self, tracker, net):
- if self.other_txs is None:
- raise ValueError('share does not contain all txs')
-
- gentx = share_info_to_gentx(self.share_info, self.header['target'], tracker, net)
-
- return dict(header=self.header, txs=[gentx] + self.other_txs)
+ def __repr__(self):
+ return 'Share' + repr((self.net, self.peer_addr, self.contents))
- def as_share1a(self):
- return dict(header=self.header, share_info=self.share_info, merkle_branch=self.merkle_branch)
+ def as_share(self):
+ return dict(type=self.VERSION, contents=self.share_type.pack(self.contents))
- def as_share1b(self):
- if self.other_txs is None:
- raise ValueError('share does not contain all txs')
-
- return dict(header=self.header, share_info=self.share_info, other_txs=self.other_txs)
+ def iter_transaction_hash_refs(self):
+ return zip(self.share_info['transaction_hash_refs'][::2], self.share_info['transaction_hash_refs'][1::2])
- def check(self, tracker, now, net):
- if self.header['timestamp'] >= TRANSITION_TIME:
- raise AssertionError('transitioning...')
- import time
- if self.previous_share_hash is not None:
- if self.header['timestamp'] <= math.median((s.timestamp for s in itertools.islice(tracker.get_chain_to_root(self.previous_share_hash), 11)), use_float=False):
- raise ValueError('share from too far in the past!')
+ def check(self, tracker):
+ from p2pool import p2p
+ if self.share_data['previous_share_hash'] is not None:
+ previous_share = tracker.items[self.share_data['previous_share_hash']]
+ if type(self) is type(previous_share):
+ pass
+ elif type(self) is type(previous_share).SUCCESSOR:
+ if tracker.get_height(previous_share.hash) < self.net.CHAIN_LENGTH:
+ from p2pool import p2p
+ raise p2p.PeerMisbehavingError('switch without enough history')
+
+ # switch only valid if 85% of hashes in [self.net.CHAIN_LENGTH*9//10, self.net.CHAIN_LENGTH] for new version
+ counts = get_desired_version_counts(tracker,
+ tracker.get_nth_parent_hash(previous_share.hash, self.net.CHAIN_LENGTH*9//10), self.net.CHAIN_LENGTH//10)
+ if counts.get(self.VERSION, 0) < sum(counts.itervalues())*85//100:
+ raise p2p.PeerMisbehavingError('switch without enough hash power upgraded')
+ else:
+ raise p2p.PeerMisbehavingError('''%s can't follow %s''' % (type(self).__name__, type(previous_share).__name__))
- if self.header['timestamp'] > now + 2*60*60:
- raise ValueError('share from too far in the future!')
+ other_tx_hashes = [tracker.items[tracker.get_nth_parent_hash(self.hash, share_count)].share_info['new_transaction_hashes'][tx_count] for share_count, tx_count in self.iter_transaction_hash_refs()]
- gentx = share_info_to_gentx(self.share_info, self.header['target'], tracker, net)
+ share_info, gentx, other_tx_hashes2, get_share = self.generate_transaction(tracker, self.share_info['share_data'], self.header['bits'].target, self.share_info['timestamp'], self.share_info['bits'].target, self.contents['ref_merkle_link'], [(h, None) for h in other_tx_hashes], self.net, last_txout_nonce=self.contents['last_txout_nonce'])
+ assert other_tx_hashes2 == other_tx_hashes
+ if share_info != self.share_info:
+ raise ValueError('share_info invalid')
+ if bitcoin_data.hash256(bitcoin_data.tx_type.pack(gentx)) != self.gentx_hash:
+ raise ValueError('''gentx doesn't match hash_link''')
- if len(gentx['tx_ins'][0]['script']) > 100:
- raise ValueError('coinbase too large! %i bytes' % (len(gentx['tx_ins'][0]['script']),))
+ if bitcoin_data.calculate_merkle_link([None] + other_tx_hashes, 0) != self.merkle_link:
+ raise ValueError('merkle_link and other_tx_hashes do not match')
- if check_merkle_branch(gentx, self.merkle_branch) != self.header['merkle_root']:
- raise ValueError('''gentx doesn't match header via merkle_branch''')
+ return gentx # only used by as_block
+
+ def get_other_tx_hashes(self, tracker):
+ parents_needed = max(share_count for share_count, tx_count in self.iter_transaction_hash_refs()) if self.share_info['transaction_hash_refs'] else 0
+ parents = tracker.get_height(self.hash) - 1
+ if parents < parents_needed:
+ return None
+ last_shares = list(tracker.get_chain(self.hash, parents_needed + 1))
+ return [last_shares[share_count].share_info['new_transaction_hashes'][tx_count] for share_count, tx_count in self.iter_transaction_hash_refs()]
+
+ def _get_other_txs(self, tracker, known_txs):
+ other_tx_hashes = self.get_other_tx_hashes(tracker)
+ if other_tx_hashes is None:
+ return None # not all parents present
+
+ if not all(tx_hash in known_txs for tx_hash in other_tx_hashes):
+ return None # not all txs present
+
+ return [known_txs[tx_hash] for tx_hash in other_tx_hashes]
+
+ def should_punish_reason(self, previous_block, bits, tracker, known_txs):
+ if (self.header['previous_block'], self.header['bits']) != (previous_block, bits) and self.header_hash != previous_block and self.peer_addr is not None:
+ return True, 'Block-stale detected! %x < %x' % (self.header['previous_block'], previous_block)
+
+ if self.pow_hash <= self.header['bits'].target:
+ return -1, 'block solution'
- if self.other_txs is not None:
- if bitcoin_data.merkle_hash([gentx] + self.other_txs) != self.header['merkle_root']:
- raise ValueError('''gentx doesn't match header via other_txs''')
+ other_txs = self._get_other_txs(tracker, known_txs)
+ if other_txs is None:
+ if self.time_seen != 0: # ignore if loaded from ShareStore
+ return True, 'not all txs present'
+ else:
+ all_txs_size = sum(bitcoin_data.tx_type.packed_size(tx) for tx in other_txs)
+ if all_txs_size > 1000000:
+ return True, 'txs over block size limit'
- if len(bitcoin_data.block_type.pack(dict(header=self.header, txs=[gentx] + self.other_txs))) > 1000000 - 1000:
- raise ValueError('block size too large')
+ new_txs_size = sum(bitcoin_data.tx_type.packed_size(known_txs[tx_hash]) for tx_hash in self.share_info['new_transaction_hashes'])
+ if new_txs_size > 50000:
+ return True, 'new txs over limit'
+
+ return False, None
- def __repr__(self):
- return '<Share %s>' % (' '.join('%s=%r' % (k, getattr(self, k)) for k in self.__slots__),)
+ def as_block(self, tracker, known_txs):
+ other_txs = self._get_other_txs(tracker, known_txs)
+ if other_txs is None:
+ return None # not all txs present
+ return dict(header=self.header, txs=[self.check(tracker)] + other_txs)
-class NewShare(Share):
- __slots__ = 'header previous_block share_info merkle_branch other_txs timestamp share_data new_script subsidy previous_hash previous_share_hash target nonce pow_hash header_hash hash time_seen peer donation'.split(' ')
+class Share(object):
+ VERSION = 9
+ VOTING_VERSION = 11
+ SUCCESSOR = NewShare
+
+ small_block_header_type = pack.ComposedType([
+ ('version', pack.VarIntType()),
+ ('previous_block', pack.PossiblyNoneType(0, pack.IntType(256))),
+ ('timestamp', pack.IntType(32)),
+ ('bits', bitcoin_data.FloatingIntegerType()),
+ ('nonce', pack.IntType(32)),
+ ])
+
+ share_info_type = pack.ComposedType([
+ ('share_data', 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.EnumType(pack.IntType(8), dict((k, {0: None, 253: 'orphan', 254: 'doa'}.get(k, 'unk%i' % (k,))) for k in xrange(256)))),
+ ('desired_version', pack.VarIntType()),
+ ])),
+ ('new_transaction_hashes', pack.ListType(pack.IntType(256))),
+ ('transaction_hash_refs', pack.ListType(pack.VarIntType(), 2)), # pairs of share_count, tx_count
+ ('far_share_hash', pack.PossiblyNoneType(0, pack.IntType(256))),
+ ('max_bits', bitcoin_data.FloatingIntegerType()),
+ ('bits', bitcoin_data.FloatingIntegerType()),
+ ('timestamp', pack.IntType(32)),
+ ])
+
+ share_type = pack.ComposedType([
+ ('min_header', small_block_header_type),
+ ('share_info', share_info_type),
+ ('ref_merkle_link', pack.ComposedType([
+ ('branch', pack.ListType(pack.IntType(256))),
+ ('index', pack.IntType(0)),
+ ])),
+ ('last_txout_nonce', pack.IntType(32)),
+ ('hash_link', hash_link_type),
+ ('merkle_link', pack.ComposedType([
+ ('branch', pack.ListType(pack.IntType(256))),
+ ('index', pack.IntType(0)), # it will always be 0
+ ])),
+ ])
+
+ ref_type = pack.ComposedType([
+ ('identifier', pack.FixedStrType(64//8)),
+ ('share_info', share_info_type),
+ ])
+
+ gentx_before_refhash = pack.VarStrType().pack(DONATION_SCRIPT) + pack.IntType(64).pack(0) + pack.VarStrType().pack('\x24' + pack.IntType(256).pack(0) + pack.IntType(32).pack(0))[:2]
@classmethod
- def from_share(cls, share, net):
- if share['type'] == 0:
- res = cls.from_share1a(new_share1a_type.unpack(share['contents']), net)
- if not (res.pow_hash > res.header['target']):
- raise ValueError('invalid share type')
- return res
- elif share['type'] == 1:
- res = cls.from_share1b(new_share1b_type.unpack(share['contents']), net)
- if not (res.pow_hash <= res.header['target']):
- raise ValueError('invalid share type')
- return res
+ def generate_transaction(cls, tracker, share_data, block_target, desired_timestamp, desired_target, ref_merkle_link, desired_other_transaction_hashes_and_fees, net, known_txs=None, last_txout_nonce=0, base_subsidy=None):
+ previous_share = tracker.items[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:
- raise ValueError('unknown share type: %r' % (share['type'],))
-
- def as_share(self):
- if self.pow_hash > self.header['target']: # new_share1a
- return dict(type=0, contents=new_share1a_type.pack(self.as_share1a()))
- elif self.pow_hash <= self.header['target']: # new_share1b
- return dict(type=1, contents=new_share1b_type.pack(self.as_share1b()))
+ attempts_per_second = get_pool_attempts_per_second(tracker, share_data['previous_share_hash'], net.TARGET_LOOKBEHIND, min_work=True, integer=True)
+ pre_target = 2**256//(net.SHARE_PERIOD*attempts_per_second) - 1 if attempts_per_second else 2**256-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, (net.MIN_TARGET, 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)))
+
+ new_transaction_hashes = []
+ new_transaction_size = 0
+ transaction_hash_refs = []
+ other_transaction_hashes = []
+
+ past_shares = list(tracker.get_chain(share_data['previous_share_hash'], min(height, 100)))
+ tx_hash_to_this = {}
+ for i, share in enumerate(past_shares):
+ for j, tx_hash in enumerate(share.new_transaction_hashes):
+ if tx_hash not in tx_hash_to_this:
+ tx_hash_to_this[tx_hash] = [1+i, j] # share_count, tx_count
+ for tx_hash, fee in desired_other_transaction_hashes_and_fees:
+ if tx_hash in tx_hash_to_this:
+ this = tx_hash_to_this[tx_hash]
+ else:
+ if known_txs is not None:
+ this_size = bitcoin_data.tx_type.packed_size(known_txs[tx_hash])
+ if new_transaction_size + this_size > 50000: # only allow 50 kB of new txns/share
+ break
+ new_transaction_size += this_size
+ new_transaction_hashes.append(tx_hash)
+ this = [0, len(new_transaction_hashes)-1]
+ transaction_hash_refs.extend(this)
+ other_transaction_hashes.append(tx_hash)
+
+ included_transactions = set(other_transaction_hashes)
+ removed_fees = [fee for tx_hash, fee in desired_other_transaction_hashes_and_fees if tx_hash not in included_transactions]
+ definite_fees = sum(0 if fee is None else fee for tx_hash, fee in desired_other_transaction_hashes_and_fees if tx_hash in included_transactions)
+ if None not in removed_fees:
+ share_data = dict(share_data, subsidy=share_data['subsidy'] - sum(removed_fees))
else:
- raise AssertionError()
+ assert base_subsidy is not None
+ share_data = dict(share_data, subsidy=base_subsidy + definite_fees)
+
+ 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,
+ far_share_hash=None if last is None and height < 99 else tracker.get_nth_parent_hash(share_data['previous_share_hash'], 99),
+ 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,
+ new_transaction_hashes=new_transaction_hashes,
+ transaction_hash_refs=transaction_hash_refs,
+ )
+
+ gentx = dict(
+ version=1,
+ tx_ins=[dict(
+ previous_output=None,
+ sequence=None,
+ script=share_data['coinbase'],
+ )],
+ tx_outs=[dict(value=amounts[script], script=script) for script in dests if amounts[script] or script == DONATION_SCRIPT] + [dict(
+ value=0,
+ script='\x24' + cls.get_ref_hash(net, share_info, ref_merkle_link) + pack.IntType(32).pack(last_txout_nonce),
+ )],
+ lock_time=0,
+ )
+
+ def get_share(header, last_txout_nonce=last_txout_nonce):
+ min_header = dict(header); del min_header['merkle_root']
+ share = cls(net, None, dict(
+ min_header=min_header,
+ share_info=share_info,
+ ref_merkle_link=dict(branch=[], index=0),
+ last_txout_nonce=last_txout_nonce,
+ hash_link=prefix_to_hash_link(bitcoin_data.tx_type.pack(gentx)[:-32-4-4], cls.gentx_before_refhash),
+ merkle_link=bitcoin_data.calculate_merkle_link([None] + other_transaction_hashes, 0),
+ ))
+ assert share.header == header # checks merkle_root
+ return share
+
+ return share_info, gentx, other_transaction_hashes, get_share
- def __init__(self, net, header, share_info, merkle_branch=None, other_txs=None):
- if merkle_branch is None and other_txs is None:
- raise ValueError('need either merkle_branch or other_txs')
- if other_txs is not None:
- new_merkle_branch = bitcoin_data.calculate_merkle_branch([dict(version=0, tx_ins=[], tx_outs=[], lock_time=0)] + other_txs, 0)
- if merkle_branch is not None:
- if merke_branch != new_merkle_branch:
- raise ValueError('invalid merkle_branch and other_txs')
- merkle_branch = new_merkle_branch
-
- if len(merkle_branch) > 16:
- raise ValueError('merkle_branch too long!')
-
- self.header = header
- self.previous_block = header['previous_block']
- self.share_info = share_info
- self.merkle_branch = merkle_branch
- self.other_txs = other_txs
-
- self.share_data = self.share_info['new_share_data']
- self.target = self.share_info['target']
- self.timestamp = self.share_info['timestamp']
+ @classmethod
+ def get_ref_hash(cls, net, share_info, ref_merkle_link):
+ return pack.IntType(256).pack(bitcoin_data.check_merkle_link(bitcoin_data.hash256(cls.ref_type.pack(dict(
+ identifier=net.IDENTIFIER,
+ share_info=share_info,
+ ))), ref_merkle_link))
+
+ __slots__ = 'net peer_addr contents min_header share_info hash_link merkle_link hash share_data max_target target timestamp previous_hash new_script desired_version gentx_hash header pow_hash header_hash new_transaction_hashes time_seen'.split(' ')
+
+ def __init__(self, net, peer_addr, contents):
+ self.net = net
+ self.peer_addr = peer_addr
+ self.contents = contents
+
+ self.min_header = contents['min_header']
+ self.share_info = contents['share_info']
+ self.hash_link = contents['hash_link']
+ self.merkle_link = contents['merkle_link']
- self.new_script = self.share_data['new_script']
- self.subsidy = self.share_data['subsidy']
- self.donation = self.share_data['donation']
+ if not (2 <= len(self.share_info['share_data']['coinbase']) <= 100):
+ raise ValueError('''bad coinbase size! %i bytes''' % (len(self.share_info['share_data']['coinbase']),))
- if len(self.new_script) > 100:
- raise ValueError('new_script too long!')
+ if len(self.merkle_link['branch']) > 16:
+ raise ValueError('merkle branch too long!')
- self.previous_hash = self.previous_share_hash = self.share_data['previous_share_hash']
- self.nonce = self.share_data['nonce']
+ assert not self.hash_link['extra_data'], repr(self.hash_link['extra_data'])
- if len(self.nonce) > 100:
- raise ValueError('nonce too long!')
+ 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'])
+ self.desired_version = self.share_data['desired_version']
- if len(self.share_data['coinbase']) > 100:
- raise ValueError('''coinbase too large! %i bytes''' % (len(self.share_data['coinbase']),))
+ n = set()
+ for share_count, tx_count in self.iter_transaction_hash_refs():
+ assert share_count < 110
+ if share_count == 0:
+ n.add(tx_count)
+ assert n == set(range(len(self.share_info['new_transaction_hashes'])))
- self.pow_hash = net.BITCOIN_POW_FUNC(header)
- self.header_hash = bitcoin_data.block_header_type.hash256(header)
+ self.gentx_hash = check_hash_link(
+ self.hash_link,
+ self.get_ref_hash(net, self.share_info, contents['ref_merkle_link']) + pack.IntType(32).pack(self.contents['last_txout_nonce']) + pack.IntType(32).pack(0),
+ self.gentx_before_refhash,
+ )
+ merkle_root = bitcoin_data.check_merkle_link(self.gentx_hash, self.merkle_link)
+ self.header = dict(self.min_header, merkle_root=merkle_root)
+ self.pow_hash = net.PARENT.POW_FUNC(bitcoin_data.block_header_type.pack(self.header))
+ self.hash = self.header_hash = bitcoin_data.hash256(bitcoin_data.block_header_type.pack(self.header))
- self.hash = new_share1a_type.hash256(self.as_share1a())
+ if self.target > net.MAX_TARGET:
+ from p2pool import p2p
+ raise p2p.PeerMisbehavingError('share target invalid')
if self.pow_hash > self.target:
- print 'hash %x' % self.pow_hash
- print 'targ %x' % self.target
- raise ValueError('not enough work!')
+ from p2pool import p2p
+ raise p2p.PeerMisbehavingError('share PoW invalid')
- if script.get_sigop_count(self.new_script) > 1:
- raise ValueError('too many sigops!')
+ self.new_transaction_hashes = self.share_info['new_transaction_hashes']
# XXX eww
self.time_seen = time.time()
- self.peer = None
- def check(self, tracker, now, net):
- if self.header['timestamp'] < TRANSITION_TIME:
- raise AssertionError('transitioning...')
+ def __repr__(self):
+ return 'Share' + repr((self.net, self.peer_addr, self.contents))
+
+ def as_share(self):
+ return dict(type=self.VERSION, contents=self.share_type.pack(self.contents))
+
+ def iter_transaction_hash_refs(self):
+ return zip(self.share_info['transaction_hash_refs'][::2], self.share_info['transaction_hash_refs'][1::2])
+
+ def check(self, tracker):
+ from p2pool import p2p
+ if self.share_data['previous_share_hash'] is not None:
+ previous_share = tracker.items[self.share_data['previous_share_hash']]
+ if type(self) is type(previous_share):
+ pass
+ elif type(self) is type(previous_share).SUCCESSOR:
+ if tracker.get_height(previous_share.hash) < self.net.CHAIN_LENGTH:
+ from p2pool import p2p
+ raise p2p.PeerMisbehavingError('switch without enough history')
+
+ # switch only valid if 85% of hashes in [self.net.CHAIN_LENGTH*9//10, self.net.CHAIN_LENGTH] for new version
+ counts = get_desired_version_counts(tracker,
+ tracker.get_nth_parent_hash(previous_share.hash, self.net.CHAIN_LENGTH*9//10), self.net.CHAIN_LENGTH//10)
+ if counts.get(self.VERSION, 0) < sum(counts.itervalues())*85//100:
+ raise p2p.PeerMisbehavingError('switch without enough hash power upgraded')
+ else:
+ raise p2p.PeerMisbehavingError('''%s can't follow %s''' % (type(self).__name__, type(previous_share).__name__))
+
+ other_tx_hashes = [tracker.items[tracker.get_nth_parent_hash(self.hash, share_count)].share_info['new_transaction_hashes'][tx_count] for share_count, tx_count in self.iter_transaction_hash_refs()]
- share_info, gentx = new_generate_transaction(tracker, self.share_info['new_share_data'], self.header['target'], self.share_info['timestamp'], net)
+ share_info, gentx, other_tx_hashes2, get_share = self.generate_transaction(tracker, self.share_info['share_data'], self.header['bits'].target, self.share_info['timestamp'], self.share_info['bits'].target, self.contents['ref_merkle_link'], [(h, None) for h in other_tx_hashes], self.net, last_txout_nonce=self.contents['last_txout_nonce'])
+ assert other_tx_hashes2 == other_tx_hashes
if share_info != self.share_info:
- raise ValueError('share difficulty invalid')
+ raise ValueError('share_info invalid')
+ if bitcoin_data.hash256(bitcoin_data.tx_type.pack(gentx)) != self.gentx_hash:
+ raise ValueError('''gentx doesn't match hash_link''')
- if bitcoin_data.check_merkle_branch(gentx, 0, self.merkle_branch) != self.header['merkle_root']:
- raise ValueError('''gentx doesn't match header via merkle_branch''')
+ if bitcoin_data.calculate_merkle_link([None] + other_tx_hashes, 0) != self.merkle_link:
+ raise ValueError('merkle_link and other_tx_hashes do not match')
+
+ return gentx # only used by as_block
+
+ def get_other_tx_hashes(self, tracker):
+ parents_needed = max(share_count for share_count, tx_count in self.iter_transaction_hash_refs()) if self.share_info['transaction_hash_refs'] else 0
+ parents = tracker.get_height(self.hash) - 1
+ if parents < parents_needed:
+ return None
+ last_shares = list(tracker.get_chain(self.hash, parents_needed + 1))
+ return [last_shares[share_count].share_info['new_transaction_hashes'][tx_count] for share_count, tx_count in self.iter_transaction_hash_refs()]
- def as_block(self, tracker, net):
- if self.other_txs is None:
- raise ValueError('share does not contain all txs')
+ def _get_other_txs(self, tracker, known_txs):
+ other_tx_hashes = self.get_other_tx_hashes(tracker)
+ if other_tx_hashes is None:
+ return None # not all parents present
- share_info, gentx = new_generate_transaction(tracker, self.share_info['new_share_data'], self.header['target'], self.share_info['timestamp'], net)
- assert share_info == self.share_info
+ if not all(tx_hash in known_txs for tx_hash in other_tx_hashes):
+ return None # not all txs present
- return dict(header=self.header, txs=[gentx] + self.other_txs)
-
-def get_pool_attempts_per_second(tracker, previous_share_hash, net, dist=None):
- if dist is None:
- dist = net.TARGET_LOOKBEHIND
- near = tracker.shares[previous_share_hash]
- far = tracker.shares[tracker.get_nth_parent_hash(previous_share_hash, dist - 1)]
- attempts = tracker.get_work(near.hash) - tracker.get_work(far.hash)
- time = near.timestamp - far.timestamp
- if time == 0:
- time = 1
- return attempts//time
-
-def generate_transaction(tracker, previous_share_hash, new_script, subsidy, nonce, block_target, net):
- donation = int(0.005*65535 + 0.5)
+ return [known_txs[tx_hash] for tx_hash in other_tx_hashes]
- height, last = tracker.get_height_and_last(previous_share_hash)
- assert height >= net.CHAIN_LENGTH or last is None
- if height < net.TARGET_LOOKBEHIND:
- target = bitcoin_data.FloatingInteger.from_target_upper_bound(net.MAX_TARGET)
- else:
- attempts_per_second = get_pool_attempts_per_second(tracker, previous_share_hash, net)
- previous_share = tracker.shares[previous_share_hash] if previous_share_hash is not None else None
- pre_target = 2**256//(net.SHARE_PERIOD*attempts_per_second) - 1
- pre_target2 = math.clip(pre_target, (previous_share.target*9//10, previous_share.target*11//10))
- pre_target3 = math.clip(pre_target2, (0, net.MAX_TARGET))
- target = bitcoin_data.FloatingInteger.from_target_upper_bound(pre_target3)
-
- attempts_to_block = bitcoin_data.target_to_average_attempts(block_target)
- max_att = net.SPREAD * attempts_to_block
-
- this_att = min(bitcoin_data.target_to_average_attempts(target), max_att)
- other_weights, other_total_weight, other_donation_weight = tracker.get_cumulative_weights(previous_share_hash, min(height, net.CHAIN_LENGTH), 65535*max(0, max_att - this_att))
- assert other_total_weight == sum(other_weights.itervalues()) + other_donation_weight, (other_total_weight, sum(other_weights.itervalues()) + other_donation_weight)
- weights, total_weight, donation_weight = math.add_dicts([{new_script: this_att*(65535-donation)}, other_weights]), this_att*65535 + other_total_weight, this_att*donation + other_donation_weight
- assert total_weight == sum(weights.itervalues()) + donation_weight, (total_weight, sum(weights.itervalues()) + donation_weight)
-
- total_weight -= donation_weight # ignore donation
-
- if net.SCRIPT:
- amounts = dict((script, subsidy*(396*weight)//(400*total_weight)) for (script, weight) in weights.iteritems())
- amounts[new_script] = amounts.get(new_script, 0) + subsidy*2//400
- amounts[net.SCRIPT] = amounts.get(net.SCRIPT, 0) + subsidy*2//400
- amounts[net.SCRIPT] = amounts.get(net.SCRIPT, 0) + subsidy - sum(amounts.itervalues()) # collect any extra
- else:
- amounts = dict((script, subsidy*(398*weight)//(400*total_weight)) for (script, weight) in weights.iteritems())
- amounts[new_script] = amounts.get(new_script, 0) + subsidy*2//400
- amounts[new_script] = amounts.get(new_script, 0) + subsidy - sum(amounts.itervalues()) # collect any extra
-
- if sum(amounts.itervalues()) != subsidy:
- raise ValueError()
- if any(x < 0 for x in amounts.itervalues()):
- raise ValueError()
-
- pre_dests = sorted(amounts.iterkeys(), key=lambda script: (amounts[script], script))
- pre_dests = pre_dests[-4000:] # block length limit, unlikely to ever be hit
-
- dests = sorted(pre_dests, key=lambda script: (script == new_script, script))
- assert dests[-1] == new_script
-
- share_data = dict(
- previous_share_hash=previous_share_hash,
- nonce=nonce,
- target=target,
- )
-
- share_info = dict(
- share_data=share_data,
- new_script=new_script,
- subsidy=subsidy,
- )
-
- return share_info, dict(
- version=1,
- tx_ins=[dict(
- previous_output=None,
- sequence=None,
- script=coinbase_type.pack(dict(
- identifier=net.IDENTIFIER,
- share_data=share_data,
- )),
- )],
- tx_outs=[dict(value=amounts[script], script=script) for script in dests if amounts[script]],
- lock_time=0,
- )
+ def should_punish_reason(self, previous_block, bits, tracker, known_txs):
+ if (self.header['previous_block'], self.header['bits']) != (previous_block, bits) and self.header_hash != previous_block and self.peer_addr is not None:
+ return True, 'Block-stale detected! %x < %x' % (self.header['previous_block'], previous_block)
+
+ if self.pow_hash <= self.header['bits'].target:
+ return -1, 'block solution'
+
+ other_txs = self._get_other_txs(tracker, known_txs)
+ if other_txs is None:
+ if self.time_seen != 0: # ignore if loaded from ShareStore
+ return True, 'not all txs present'
+ else:
+ all_txs_size = sum(bitcoin_data.tx_type.packed_size(tx) for tx in other_txs)
+ if all_txs_size > 1000000:
+ return True, 'txs over block size limit'
+
+ new_txs_size = sum(bitcoin_data.tx_type.packed_size(known_txs[tx_hash]) for tx_hash in self.share_info['new_transaction_hashes'])
+ if new_txs_size > 50000:
+ return True, 'new txs over limit'
+
+ return False, None
+
+ def as_block(self, tracker, known_txs):
+ other_txs = self._get_other_txs(tracker, known_txs)
+ if other_txs is None:
+ return None # not all txs present
+ return dict(header=self.header, txs=[self.check(tracker)] + other_txs)
+
-def new_generate_transaction(tracker, new_share_data, block_target, desired_timestamp, net):
- previous_share_hash = new_share_data['previous_share_hash']
- new_script = new_share_data['new_script']
- subsidy = new_share_data['subsidy']
- donation = new_share_data['donation']
- assert 0 <= donation <= 65535
-
- if len(new_share_data['coinbase']) > 100:
- raise ValueError('coinbase too long!')
-
- previous_share = tracker.shares[previous_share_hash] if previous_share_hash is not None else None
-
- height, last = tracker.get_height_and_last(previous_share_hash)
- assert height >= net.CHAIN_LENGTH or last is None
- if height < net.TARGET_LOOKBEHIND:
- target = bitcoin_data.FloatingInteger.from_target_upper_bound(net.MAX_TARGET)
- else:
- attempts_per_second = get_pool_attempts_per_second(tracker, previous_share_hash, net)
- pre_target = 2**256//(net.SHARE_PERIOD*attempts_per_second) - 1
- pre_target2 = math.clip(pre_target, (previous_share.target*9//10, previous_share.target*11//10))
- pre_target3 = math.clip(pre_target2, (0, net.MAX_TARGET))
- target = bitcoin_data.FloatingInteger.from_target_upper_bound(pre_target3)
-
- attempts_to_block = bitcoin_data.target_to_average_attempts(block_target)
- max_att = net.SPREAD * attempts_to_block
-
- this_att = min(bitcoin_data.target_to_average_attempts(target), max_att)
- other_weights, other_total_weight, other_donation_weight = tracker.get_cumulative_weights(previous_share_hash, min(height, getattr(net, 'CHAIN_LENGTH2', net.CHAIN_LENGTH)), 65535*max(0, max_att - this_att))
- assert other_total_weight == sum(other_weights.itervalues()) + other_donation_weight, (other_total_weight, sum(other_weights.itervalues()) + other_donation_weight)
- weights, total_weight, donation_weight = math.add_dicts([{new_script: this_att*(65535-donation)}, other_weights]), this_att*65535 + other_total_weight, this_att*donation + other_donation_weight
- assert total_weight == sum(weights.itervalues()) + donation_weight, (total_weight, sum(weights.itervalues()) + donation_weight)
-
- SCRIPT = '4104ffd03de44a6e11b9917f3a29f9443283d9871c9d743ef30d5eddcd37094b64d1b3d8090496b53256786bf5c82932ec23c3b74d9f05a6f95a8b5529352656664bac'.decode('hex')
-
- # 1 satoshi is always donated so that a list of p2pool generated blocks can be easily found by looking at the donation address
- amounts = dict((script, (subsidy-1)*(199*weight)//(200*total_weight)) for (script, weight) in weights.iteritems())
- amounts[new_script] = amounts.get(new_script, 0) + (subsidy-1)//200
- amounts[SCRIPT] = amounts.get(SCRIPT, 0) + (subsidy-1)*(199*donation_weight)//(200*total_weight)
- amounts[SCRIPT] = amounts.get(SCRIPT, 0) + subsidy - sum(amounts.itervalues()) # collect any extra satoshis :P
-
- if sum(amounts.itervalues()) != subsidy:
- raise ValueError()
- if any(x < 0 for x in amounts.itervalues()):
- raise ValueError()
-
- dests = sorted(amounts.iterkeys(), key=lambda script: (amounts[script], script))
- dests = dests[-4000:] # block length limit, unlikely to ever be hit
-
- share_info = dict(
- new_share_data=new_share_data,
- target=target,
- timestamp=math.clip(desired_timestamp, (previous_share.timestamp - 60, previous_share.timestamp + 60)) if previous_share is not None else desired_timestamp,
- )
-
- return share_info, dict(
- version=1,
- tx_ins=[dict(
- previous_output=None,
- sequence=None,
- script=new_share_data['coinbase'].ljust(2, '\x00'),
- )],
- tx_outs=[dict(value=0, script='\x20' + bitcoin_data.HashType().pack(new_share_info_type.hash256(share_info)))] + [dict(value=amounts[script], script=script) for script in dests if amounts[script]],
- lock_time=0,
- )
+class WeightsSkipList(forest.TrackerSkipList):
+ # share_count, weights, total_weight
+
+ def get_delta(self, element):
+ from p2pool.bitcoin import data as bitcoin_data
+ share = self.tracker.items[element]
+ att = bitcoin_data.target_to_average_attempts(share.target)
+ return 1, {share.new_script: att*(65535-share.share_data['donation'])}, att*65535, att*share.share_data['donation']
+
+ def combine_deltas(self, (share_count1, weights1, total_weight1, total_donation_weight1), (share_count2, weights2, total_weight2, total_donation_weight2)):
+ return share_count1 + share_count2, math.add_dicts(weights1, weights2), total_weight1 + total_weight2, total_donation_weight1 + total_donation_weight2
+
+ def initial_solution(self, start, (max_shares, desired_weight)):
+ assert desired_weight % 65535 == 0, divmod(desired_weight, 65535)
+ return 0, None, 0, 0
+
+ def apply_delta(self, (share_count1, weights_list, total_weight1, total_donation_weight1), (share_count2, weights2, total_weight2, total_donation_weight2), (max_shares, desired_weight)):
+ if total_weight1 + total_weight2 > desired_weight and share_count2 == 1:
+ assert (desired_weight - total_weight1) % 65535 == 0
+ script, = weights2.iterkeys()
+ new_weights = {script: (desired_weight - total_weight1)//65535*weights2[script]//(total_weight2//65535)}
+ return share_count1 + share_count2, (weights_list, new_weights), desired_weight, total_donation_weight1 + (desired_weight - total_weight1)//65535*total_donation_weight2//(total_weight2//65535)
+ return share_count1 + share_count2, (weights_list, weights2), total_weight1 + total_weight2, total_donation_weight1 + total_donation_weight2
+
+ def judge(self, (share_count, weights_list, total_weight, total_donation_weight), (max_shares, desired_weight)):
+ if share_count > max_shares or total_weight > desired_weight:
+ return 1
+ elif share_count == max_shares or total_weight == desired_weight:
+ return 0
+ else:
+ return -1
+
+ def finalize(self, (share_count, weights_list, total_weight, total_donation_weight), (max_shares, desired_weight)):
+ assert share_count <= max_shares and total_weight <= desired_weight
+ assert share_count == max_shares or total_weight == desired_weight
+ return math.add_dicts(*math.flatten_linked_list(weights_list)), total_weight, total_donation_weight
-class OkayTracker(bitcoin_data.Tracker):
+class OkayTracker(forest.Tracker):
def __init__(self, net):
- bitcoin_data.Tracker.__init__(self)
+ forest.Tracker.__init__(self, delta_type=forest.get_attributedelta_type(dict(forest.AttributeDelta.attrs,
+ work=lambda share: bitcoin_data.target_to_average_attempts(share.target),
+ min_work=lambda share: bitcoin_data.target_to_average_attempts(share.max_target),
+ )))
self.net = net
- self.verified = bitcoin_data.Tracker()
- self.verified.get_nth_parent_hash = self.get_nth_parent_hash # self is a superset of self.verified
-
- self.get_cumulative_weights = skiplists.WeightsSkipList(self)
-
- def add(self, share, known_verified=False):
- bitcoin_data.Tracker.add(self, share)
- if known_verified:
- self.verified.add(share)
+ self.verified = forest.SubsetTracker(delta_type=forest.get_attributedelta_type(dict(forest.AttributeDelta.attrs,
+ work=lambda share: bitcoin_data.target_to_average_attempts(share.target),
+ )), subset_of=self)
+ self.get_cumulative_weights = WeightsSkipList(self)
- def attempt_verify(self, share, now):
- if share.hash in self.verified.shares:
+ def attempt_verify(self, share):
+ if share.hash in self.verified.items:
return True
height, last = self.get_height_and_last(share.hash)
if height < self.net.CHAIN_LENGTH + 1 and last is not None:
raise AssertionError()
try:
- share.check(self, now, self.net)
+ share.check(self)
except:
log.err(None, 'Share check failed:')
return False
self.verified.add(share)
return True
- def think(self, ht, previous_block, now):
+ def think(self, block_rel_height_func, previous_block, bits, known_txs):
desired = set()
# O(len(self.heads))
for head in set(self.heads) - set(self.verified.heads):
head_height, last = self.get_height_and_last(head)
- for share in itertools.islice(self.get_chain_known(head), None if last is None else min(5, max(0, head_height - self.net.CHAIN_LENGTH))):
- if self.attempt_verify(share, now):
+ for share in self.get_chain(head, head_height if last is None else min(5, max(0, head_height - self.net.CHAIN_LENGTH))):
+ if self.attempt_verify(share):
break
if share.hash in self.heads:
bads.add(share.hash)
else:
if last is not None:
- desired.add((self.shares[random.choice(list(self.reverse_shares[last]))].peer, last))
+ desired.add((
+ self.items[random.choice(list(self.reverse[last]))].peer_addr,
+ last,
+ max(x.timestamp for x in self.get_chain(head, min(head_height, 5))),
+ min(x.target for x in self.get_chain(head, min(head_height, 5))),
+ ))
for bad in bads:
- assert bad not in self.verified.shares
+ assert bad not in self.verified.items
assert bad in self.heads
if p2pool.DEBUG:
print "BAD", bad
can = max(last_height - 1 - self.net.CHAIN_LENGTH, 0) if last_last_hash is not None else last_height
get = min(want, can)
#print 'Z', head_height, last_hash is None, last_height, last_last_hash is None, want, can, get
- for share in itertools.islice(self.get_chain_known(last_hash), get):
- if not self.attempt_verify(share, now):
+ for share in self.get_chain(last_hash, get):
+ if not self.attempt_verify(share):
break
if head_height < self.net.CHAIN_LENGTH and last_last_hash is not None:
- desired.add((self.verified.shares[random.choice(list(self.verified.reverse_shares[last_hash]))].peer, last_last_hash))
+ desired.add((
+ self.items[random.choice(list(self.verified.reverse[last_hash]))].peer_addr,
+ last_last_hash,
+ max(x.timestamp for x in self.get_chain(head, min(head_height, 5))),
+ min(x.target for x in self.get_chain(head, min(head_height, 5))),
+ ))
# decide best tree
- best_tail = max(self.verified.tails, key=lambda h: self.score(max(self.verified.tails[h], key=self.verified.get_height), ht)) if self.verified.tails else None
+ decorated_tails = sorted((self.score(max(self.verified.tails[tail_hash], key=self.verified.get_work), block_rel_height_func), tail_hash) for tail_hash in self.verified.tails)
+ if p2pool.DEBUG:
+ print len(decorated_tails), 'tails:'
+ for score, tail_hash in decorated_tails:
+ print format_hash(tail_hash), score
+ best_tail_score, best_tail = decorated_tails[-1] if decorated_tails else (None, None)
+
# decide best verified head
- scores = sorted(self.verified.tails.get(best_tail, []), key=lambda h: (
+ decorated_heads = sorted(((
self.verified.get_work(self.verified.get_nth_parent_hash(h, min(5, self.verified.get_height(h)))),
- #self.verified.shares[h].peer is None,
- ht.get_min_height(self.verified.shares[h].previous_block),
- -self.verified.shares[h].time_seen
- ))
-
-
+ #self.items[h].peer_addr is None,
+ -self.items[h].should_punish_reason(previous_block, bits, self, known_txs)[0],
+ -self.items[h].time_seen,
+ ), h) for h in self.verified.tails.get(best_tail, []))
if p2pool.DEBUG:
- print len(self.verified.tails), "chain tails and", len(self.verified.tails.get(best_tail, [])), 'chain heads. Top 10 tails:'
- if len(scores) > 10:
- print ' ...'
- for h in scores[-10:]:
- print ' ', format_hash(h), format_hash(self.verified.shares[h].previous_hash), (
- self.verified.get_work(self.verified.get_nth_parent_hash(h, min(5, self.verified.get_height(h)))),
- self.verified.shares[h].peer is None,
- ht.get_min_height(self.verified.shares[h].previous_block),
- -self.verified.shares[h].time_seen
- )
-
- # eat away at heads
- if scores:
- for i in xrange(1000):
- to_remove = set()
- for share_hash, tail in self.heads.iteritems():
- if share_hash in scores[-5:]:
- #print 1
- continue
- if self.shares[share_hash].time_seen > time.time() - 300:
- #print 2
- continue
- if share_hash not in self.verified.shares and max(self.shares[after_tail_hash].time_seen for after_tail_hash in self.reverse_shares.get(tail)) > time.time() - 120: # XXX stupid
- #print 3
- continue
- to_remove.add(share_hash)
- if not to_remove:
- break
- for share_hash in to_remove:
- self.remove(share_hash)
- if share_hash in self.verified.shares:
- self.verified.remove(share_hash)
- #print "_________", to_remove
-
- # drop tails
- for i in xrange(1000):
- to_remove = set()
- for tail, heads in self.tails.iteritems():
- if min(self.get_height(head) for head in heads) < 2*self.net.CHAIN_LENGTH + 10:
- continue
- for aftertail in self.reverse_shares.get(tail, set()):
- if len(self.reverse_shares[self.shares[aftertail].previous_hash]) > 1: # XXX
- print "raw"
- continue
- to_remove.add(aftertail)
- if not to_remove:
- break
- # if removed from this, it must be removed from verified
- #start = time.time()
- for aftertail in to_remove:
- if self.shares[aftertail].previous_hash not in self.tails:
- print "erk", aftertail, self.shares[aftertail].previous_hash
- continue
- self.remove(aftertail)
- if aftertail in self.verified.shares:
- self.verified.remove(aftertail)
- #end = time.time()
- #print "removed! %i %f" % (len(to_remove), (end - start)/len(to_remove))
-
- best = scores[-1] if scores else None
+ print len(decorated_heads), 'heads. Top 10:'
+ for score, head_hash in decorated_heads[-10:]:
+ print ' ', format_hash(head_hash), format_hash(self.items[head_hash].previous_hash), score
+ best_head_score, best = decorated_heads[-1] if decorated_heads else (None, None)
if best is not None:
- best_share = self.verified.shares[best]
- if ht.get_min_height(best_share.header['previous_block']) < ht.get_min_height(previous_block) and best_share.header_hash != previous_block and best_share.peer is not None:
- if p2pool.DEBUG:
- print 'Stale detected! %x < %x' % (best_share.header['previous_block'], previous_block)
+ best_share = self.items[best]
+ punish, punish_reason = best_share.should_punish_reason(previous_block, bits, self, known_txs)
+ if punish > 0:
+ print 'Punishing share for %r! Jumping from %s to %s!' % (punish_reason, format_hash(best), format_hash(best_share.previous_hash))
best = best_share.previous_hash
+
+ timestamp_cutoff = min(int(time.time()), best_share.timestamp) - 3600
+ target_cutoff = int(2**256//(self.net.SHARE_PERIOD*best_tail_score[1] + 1) * 2 + .5) if best_tail_score[1] is not None else 2**256-1
+ else:
+ timestamp_cutoff = int(time.time()) - 24*60*60
+ target_cutoff = 2**256-1
- return best, desired
-
- @memoize.memoize_with_backing(expiring_dict.ExpiringDict(5, get_touches=False))
- def score(self, share_hash, ht):
- head_height, last = self.verified.get_height_and_last(share_hash)
- score2 = 0
- attempts = 0
- max_height = 0
- share2_hash = self.verified.get_nth_parent_hash(share_hash, min(self.net.CHAIN_LENGTH//2, head_height//2)) if last is not None else share_hash
- for share in reversed(list(itertools.islice(self.verified.get_chain_known(share2_hash), self.net.CHAIN_LENGTH))):
- max_height = max(max_height, ht.get_min_height(share.header['previous_block']))
- attempts += bitcoin_data.target_to_average_attempts(share.target)
- this_score = attempts//(ht.get_highest_height() - max_height + 1)
- if this_score > score2:
- score2 = this_score
- return min(head_height, self.net.CHAIN_LENGTH), score2
+ if p2pool.DEBUG:
+ print 'Desire %i shares. Cutoff: %s old diff>%.2f' % (len(desired), math.format_dt(time.time() - timestamp_cutoff), bitcoin_data.target_to_difficulty(target_cutoff))
+ for peer_addr, hash, ts, targ in desired:
+ print ' ', None if peer_addr is None else '%s:%i' % peer_addr, format_hash(hash), math.format_dt(time.time() - ts), bitcoin_data.target_to_difficulty(targ), ts >= timestamp_cutoff, targ <= target_cutoff
+
+ return best, [(peer_addr, hash) for peer_addr, hash, ts, targ in desired if ts >= timestamp_cutoff], decorated_heads
+
+ def score(self, share_hash, block_rel_height_func):
+ # returns approximate lower bound on chain's hashrate in the last self.net.CHAIN_LENGTH*15//16*self.net.SHARE_PERIOD time
+
+ head_height = self.verified.get_height(share_hash)
+ if head_height < self.net.CHAIN_LENGTH:
+ return head_height, None
+
+ end_point = self.verified.get_nth_parent_hash(share_hash, self.net.CHAIN_LENGTH*15//16)
+
+ block_height = max(block_rel_height_func(share.header['previous_block']) for share in
+ self.verified.get_chain(end_point, self.net.CHAIN_LENGTH//16))
+
+ return self.net.CHAIN_LENGTH, self.verified.get_delta(share_hash, end_point).work/((0 - block_height + 1)*self.net.PARENT.BLOCK_PERIOD)
+
+def get_pool_attempts_per_second(tracker, previous_share_hash, dist, min_work=False, integer=False):
+ assert dist >= 2
+ near = tracker.items[previous_share_hash]
+ far = tracker.items[tracker.get_nth_parent_hash(previous_share_hash, dist - 1)]
+ attempts = tracker.get_delta(near.hash, far.hash).work if not min_work else tracker.get_delta(near.hash, far.hash).min_work
+ time = near.timestamp - far.timestamp
+ if time <= 0:
+ time = 1
+ if integer:
+ return attempts//time
+ return attempts/time
+
+def get_average_stale_prop(tracker, share_hash, lookbehind):
+ stales = sum(1 for share in tracker.get_chain(share_hash, lookbehind) if share.share_data['stale_info'] is not None)
+ return stales/(lookbehind + stales)
+
+def get_stale_counts(tracker, share_hash, lookbehind, rates=False):
+ res = {}
+ for share in tracker.get_chain(share_hash, lookbehind - 1):
+ res['good'] = res.get('good', 0) + bitcoin_data.target_to_average_attempts(share.target)
+ s = share.share_data['stale_info']
+ if s is not None:
+ res[s] = res.get(s, 0) + bitcoin_data.target_to_average_attempts(share.target)
+ if rates:
+ dt = tracker.items[share_hash].timestamp - tracker.items[tracker.get_nth_parent_hash(share_hash, lookbehind - 1)].timestamp
+ res = dict((k, v/dt) for k, v in res.iteritems())
+ return res
+
+def get_user_stale_props(tracker, share_hash, lookbehind):
+ res = {}
+ for share in tracker.get_chain(share_hash, lookbehind - 1):
+ stale, total = res.get(share.share_data['pubkey_hash'], (0, 0))
+ total += 1
+ if share.share_data['stale_info'] is not None:
+ stale += 1
+ total += 1
+ res[share.share_data['pubkey_hash']] = stale, total
+ return dict((pubkey_hash, stale/total) for pubkey_hash, (stale, total) in res.iteritems())
+
+def get_expected_payouts(tracker, best_share_hash, block_target, subsidy, net):
+ weights, total_weight, donation_weight = tracker.get_cumulative_weights(best_share_hash, min(tracker.get_height(best_share_hash), net.REAL_CHAIN_LENGTH), 65535*net.SPREAD*bitcoin_data.target_to_average_attempts(block_target))
+ res = dict((script, subsidy*weight//total_weight) for script, weight in weights.iteritems())
+ res[DONATION_SCRIPT] = res.get(DONATION_SCRIPT, 0) + subsidy - sum(res.itervalues())
+ return res
+
+def get_desired_version_counts(tracker, best_share_hash, dist):
+ res = {}
+ for share in tracker.get_chain(best_share_hash, dist):
+ res[share.desired_version] = res.get(share.desired_version, 0) + bitcoin_data.target_to_average_attempts(share.target)
+ return res
+
+def get_warnings(tracker, best_share, net, bitcoind_warning, bitcoind_work_value):
+ res = []
+
+ desired_version_counts = get_desired_version_counts(tracker, best_share,
+ min(net.CHAIN_LENGTH, 60*60//net.SHARE_PERIOD, tracker.get_height(best_share)))
+ majority_desired_version = max(desired_version_counts, key=lambda k: desired_version_counts[k])
+ if majority_desired_version > (Share.SUCCESSOR if Share.SUCCESSOR is not None else Share).VOTING_VERSION and desired_version_counts[majority_desired_version] > sum(desired_version_counts.itervalues())/2:
+ res.append('A MAJORITY OF SHARES CONTAIN A VOTE FOR AN UNSUPPORTED SHARE IMPLEMENTATION! (v%i with %i%% support)\n'
+ 'An upgrade is likely necessary. Check http://p2pool.forre.st/ for more information.' % (
+ majority_desired_version, 100*desired_version_counts[majority_desired_version]/sum(desired_version_counts.itervalues())))
+
+ if bitcoind_warning is not None:
+ if 'This is a pre-release test build' not in bitcoind_warning:
+ res.append('(from bitcoind) %s' % (bitcoind_warning,))
+
+ if time.time() > bitcoind_work_value['last_update'] + 60:
+ res.append('''LOST CONTACT WITH BITCOIND for %s! Check that it isn't frozen or dead!''' % (math.format_dt(time.time() - bitcoind_work_value['last_update']),))
+
+ return res
def format_hash(x):
if x is None:
self.filename = os.path.basename(os.path.abspath(prefix))
self.net = net
self.known = None # will be filename -> set of share hashes, set of verified hashes
+ self.known_desired = None
def get_shares(self):
if self.known is not None:
type_id_str, data_hex = line.strip().split(' ')
type_id = int(type_id_str)
if type_id == 0:
- share = Share.from_share1a(share1a_type.unpack(data_hex.decode('hex')), self.net)
- yield 'share', share
- share_hashes.add(share.hash)
+ pass
elif type_id == 1:
- share = Share.from_share1b(share1b_type.unpack(data_hex.decode('hex')), self.net)
- yield 'share', share
- share_hashes.add(share.hash)
+ pass
elif type_id == 2:
verified_hash = int(data_hex, 16)
yield 'verified_hash', verified_hash
verified_hashes.add(verified_hash)
elif type_id == 5:
- share = NewShare.from_share(new_share_type.unpack(data_hex.decode('hex')), self.net)
+ raw_share = share_type.unpack(data_hex.decode('hex'))
+ if raw_share['type'] in [0, 1, 2, 3, 4, 5, 6, 7, 8]:
+ continue
+ share = load_share(raw_share, self.net, None)
yield 'share', share
share_hashes.add(share.hash)
else:
raise NotImplementedError("share type %i" % (type_id,))
except Exception:
- log.err(None, "Error while reading saved shares, continuing where left off:")
+ log.err(None, "HARMLESS error while reading saved shares, continuing where left off:")
self.known = known
+ self.known_desired = dict((k, (set(a), set(b))) for k, (a, b) in known.iteritems())
def _add_line(self, line):
filenames, next = self.get_filenames_and_next()
return filename
def add_share(self, share):
- if isinstance(share, NewShare):
- type_id, data = 5, new_share_type.pack(share.as_share())
- elif share.pow_hash <= share.header['target']:
- type_id, data = 1, share1b_type.pack(share.as_share1b())
+ for filename, (share_hashes, verified_hashes) in self.known.iteritems():
+ if share.hash in share_hashes:
+ break
else:
- type_id, data = 0, share1a_type.pack(share.as_share1a())
- filename = self._add_line("%i %s" % (type_id, data.encode('hex')))
- share_hashes, verified_hashes = self.known.setdefault(filename, (set(), set()))
+ filename = self._add_line("%i %s" % (5, share_type.pack(share.as_share()).encode('hex')))
+ share_hashes, verified_hashes = self.known.setdefault(filename, (set(), set()))
+ share_hashes.add(share.hash)
+ share_hashes, verified_hashes = self.known_desired.setdefault(filename, (set(), set()))
share_hashes.add(share.hash)
def add_verified_hash(self, share_hash):
- filename = self._add_line("%i %x" % (2, share_hash))
- share_hashes, verified_hashes = self.known.setdefault(filename, (set(), set()))
+ for filename, (share_hashes, verified_hashes) in self.known.iteritems():
+ if share_hash in verified_hashes:
+ break
+ else:
+ filename = self._add_line("%i %x" % (2, share_hash))
+ share_hashes, verified_hashes = self.known.setdefault(filename, (set(), set()))
+ verified_hashes.add(share_hash)
+ share_hashes, verified_hashes = self.known_desired.setdefault(filename, (set(), set()))
verified_hashes.add(share_hash)
def get_filenames_and_next(self):
return [os.path.join(self.dirname, self.filename + str(suffix)) for suffix in suffixes], os.path.join(self.dirname, self.filename + (str(suffixes[-1] + 1) if suffixes else str(0)))
def forget_share(self, share_hash):
- for filename, (share_hashes, verified_hashes) in self.known.iteritems():
+ for filename, (share_hashes, verified_hashes) in self.known_desired.iteritems():
if share_hash in share_hashes:
share_hashes.remove(share_hash)
self.check_remove()
def forget_verified_share(self, share_hash):
- for filename, (share_hashes, verified_hashes) in self.known.iteritems():
+ for filename, (share_hashes, verified_hashes) in self.known_desired.iteritems():
if share_hash in verified_hashes:
verified_hashes.remove(share_hash)
self.check_remove()
def check_remove(self):
to_remove = set()
- for filename, (share_hashes, verified_hashes) in self.known.iteritems():
+ for filename, (share_hashes, verified_hashes) in self.known_desired.iteritems():
#print filename, len(share_hashes) + len(verified_hashes)
if not share_hashes and not verified_hashes:
to_remove.add(filename)
for filename in to_remove:
self.known.pop(filename)
+ self.known_desired.pop(filename)
os.remove(filename)
print "REMOVED", filename
-
-class BitcoinMainnet(networks.BitcoinMainnet):
- SHARE_PERIOD = 10 # seconds
- CHAIN_LENGTH = 24*60*60//5 # shares
- CHAIN_LENGTH2 = 24*60*60//10//2 # shares
- TARGET_LOOKBEHIND = 200 # shares
- SPREAD = 3 # blocks
- SCRIPT = '4104ffd03de44a6e11b9917f3a29f9443283d9871c9d743ef30d5eddcd37094b64d1b3d8090496b53256786bf5c82932ec23c3b74d9f05a6f95a8b5529352656664bac'.decode('hex')
- IDENTIFIER = 'fc70035c7a81bc6f'.decode('hex')
- PREFIX = '2472ef181efcd37b'.decode('hex')
- NAME = 'bitcoin'
- P2P_PORT = 9333
- MAX_TARGET = 2**256//2**32 - 1
- PERSIST = False
- WORKER_PORT = 9332
-
-class BitcoinTestnet(networks.BitcoinTestnet):
- SHARE_PERIOD = 1 # seconds
- CHAIN_LENGTH = 24*60*60//5 # shares
- CHAIN_LENGTH2 = 24*60*60//10//2 # shares
- TARGET_LOOKBEHIND = 200 # shares
- SPREAD = 3 # blocks
- SCRIPT = '410403ad3dee8ab3d8a9ce5dd2abfbe7364ccd9413df1d279bf1a207849310465b0956e5904b1155ecd17574778f9949589ebfd4fb33ce837c241474a225cf08d85dac'.decode('hex')
- IDENTIFIER = '5fc2be2d4f0d6bfb'.decode('hex')
- PREFIX = '3f6057a15036f441'.decode('hex')
- NAME = 'bitcoin_testnet'
- P2P_PORT = 19333
- MAX_TARGET = 2**256//2**32 - 1
- PERSIST = False
- WORKER_PORT = 19332
-
-class NamecoinMainnet(networks.NamecoinMainnet):
- SHARE_PERIOD = 10 # seconds
- CHAIN_LENGTH = 24*60*60//10 # shares
- TARGET_LOOKBEHIND = 3600//10 # shares
- SPREAD = 3 # blocks
- SCRIPT = '41043da5beb73f8f18cede1a41b0ed953123f1342b8e0216ab5bf71ed3e024201b4017f472bddb6041f17978d89ed8f8ed84f9e726b0bca80cacf96347c7153e8df0ac'.decode('hex')
- IDENTIFIER = 'd5b1192062c4c454'.decode('hex')
- PREFIX = 'b56f3d0fb24fc982'.decode('hex')
- NAME = 'namecoin'
- P2P_PORT = 9334
- MAX_TARGET = 2**256//2**32 - 1
- PERSIST = True
- WORKER_PORT = 9331
-
-class NamecoinTestnet(networks.NamecoinTestnet):
- SHARE_PERIOD = 1 # seconds
- CHAIN_LENGTH = 24*60*60//5 # shares
- TARGET_LOOKBEHIND = 200 # shares
- SPREAD = 3 # blocks
- SCRIPT = '410403ad3dee8ab3d8a9ce5dd2abfbe7364ccd9413df1d279bf1a207849310465b0956e5904b1155ecd17574778f9949589ebfd4fb33ce837c241474a225cf08d85dac'.decode('hex')
- IDENTIFIER = '8dd303d014a01a60'.decode('hex')
- PREFIX = '4d6581d24f51acbf'.decode('hex')
- NAME = 'namecoin_testnet'
- P2P_PORT = 19334
- MAX_TARGET = 2**256//2**20 - 1
- PERSIST = False
- WORKER_PORT = 19331
-
-class IxcoinMainnet(networks.IxcoinMainnet):
- SHARE_PERIOD = 10 # seconds
- CHAIN_LENGTH = 24*60*60//10 # shares
- TARGET_LOOKBEHIND = 3600//10 # shares
- SPREAD = 3 # blocks
- SCRIPT = '41043da5beb73f8f18cede1a41b0ed953123f1342b8e0216ab5bf71ed3e024201b4017f472bddb6041f17978d89ed8f8ed84f9e726b0bca80cacf96347c7153e8df0ac'.decode('hex')
- IDENTIFIER = '27b564116e2a2666'.decode('hex')
- PREFIX = '9dd6c4a619401f2f'.decode('hex')
- NAME = 'ixcoin'
- P2P_PORT = 9335
- MAX_TARGET = 2**256//2**32 - 1
- PERSIST = True
- WORKER_PORT = 9330
-
-class IxcoinTestnet(networks.IxcoinTestnet):
- SHARE_PERIOD = 1 # seconds
- CHAIN_LENGTH = 24*60*60//5 # shares
- TARGET_LOOKBEHIND = 200 # shares
- SPREAD = 3 # blocks
- SCRIPT = '410403ad3dee8ab3d8a9ce5dd2abfbe7364ccd9413df1d279bf1a207849310465b0956e5904b1155ecd17574778f9949589ebfd4fb33ce837c241474a225cf08d85dac'.decode('hex')
- IDENTIFIER = '7430cbeb01249e44'.decode('hex')
- PREFIX = '7cfffda946709c1f'.decode('hex')
- NAME = 'ixcoin_testnet'
- P2P_PORT = 19335
- MAX_TARGET = 2**256//2**20 - 1
- PERSIST = False
- WORKER_PORT = 19330
-
-class I0coinMainnet(networks.I0coinMainnet):
- SHARE_PERIOD = 10 # seconds
- CHAIN_LENGTH = 24*60*60//10 # shares
- TARGET_LOOKBEHIND = 3600//10 # shares
- SPREAD = 3 # blocks
- SCRIPT = '410403ad3dee8ab3d8a9ce5dd2abfbe7364ccd9413df1d279bf1a207849310465b0956e5904b1155ecd17574778f9949589ebfd4fb33ce837c241474a225cf08d85dac'.decode('hex')
- IDENTIFIER = 'b32e3f10c2ff221b'.decode('hex')
- PREFIX = '6155537ed977a3b5'.decode('hex')
- NAME = 'i0coin'
- P2P_PORT = 9336
- MAX_TARGET = 2**256//2**32 - 1
- PERSIST = False
- WORKER_PORT = 9329
-
-class I0coinTestnet(networks.I0coinTestnet):
- SHARE_PERIOD = 1 # seconds
- CHAIN_LENGTH = 24*60*60//5 # shares
- TARGET_LOOKBEHIND = 200 # shares
- SPREAD = 3 # blocks
- SCRIPT = '410403ad3dee8ab3d8a9ce5dd2abfbe7364ccd9413df1d279bf1a207849310465b0956e5904b1155ecd17574778f9949589ebfd4fb33ce837c241474a225cf08d85dac'.decode('hex')
- IDENTIFIER = '7712c1a8181b5f2e'.decode('hex')
- PREFIX = '792d2e7d770fbe68'.decode('hex')
- NAME = 'i0coin_testnet'
- P2P_PORT = 19336
- MAX_TARGET = 2**256//2**20 - 1
- PERSIST = False
- WORKER_PORT = 19329
-
-class SolidcoinMainnet(networks.SolidcoinMainnet):
- SHARE_PERIOD = 10
- CHAIN_LENGTH = 24*60*60//10 # shares
- TARGET_LOOKBEHIND = 3600//10 # shares
- SPREAD = 3 # blocks
- SCRIPT = bitcoin_data.pubkey_hash_to_script2(bitcoin_data.address_to_pubkey_hash('sMKZ1yxHETxPYKh4Z2anWnwZDJZU7ztroy', networks.SolidcoinMainnet))
- IDENTIFIER = '9cc9c421cca258cd'.decode('hex')
- PREFIX = 'c059125b8070f00a'.decode('hex')
- NAME = 'solidcoin'
- P2P_PORT = 9337
- MAX_TARGET = 2**256//2**32 - 1
- PERSIST = True
- WORKER_PORT = 9328
-
-class LitecoinMainnet(networks.LitecoinMainnet):
- SHARE_PERIOD = 10 # seconds
- CHAIN_LENGTH = 24*60*60//5 # shares
- CHAIN_LENGTH2 = 24*60*60//10//2 # shares
- TARGET_LOOKBEHIND = 200 # shares
- SPREAD = 12 # blocks
- SCRIPT = None # no fee
- IDENTIFIER = 'e037d5b8c6923410'.decode('hex')
- PREFIX = '7208c1a53ef629b0'.decode('hex')
- NAME = 'litecoin'
- P2P_PORT = 9338
- MAX_TARGET = 2**256//2**20 - 1
- PERSIST = True
- WORKER_PORT = 9327
-
-class LitecoinTestnet(networks.LitecoinTestnet):
- SHARE_PERIOD = 1 # seconds
- CHAIN_LENGTH = 24*60*60//5 # shares
- TARGET_LOOKBEHIND = 200 # shares
- SPREAD = 12 # blocks
- SCRIPT = None # no fee
- IDENTIFIER = 'cca5e24ec6408b1e'.decode('hex')
- PREFIX = 'ad9614f6466a39cf'.decode('hex')
- NAME = 'litecoin_testnet'
- P2P_PORT = 19338
- MAX_TARGET = 2**256//2**17 - 1
- PERSIST = False
- WORKER_PORT = 19327
-
-nets = dict((net.NAME, net) for net in set([BitcoinMainnet, BitcoinTestnet, NamecoinMainnet, NamecoinTestnet, IxcoinMainnet, IxcoinTestnet, I0coinMainnet, I0coinTestnet, SolidcoinMainnet, LitecoinMainnet, LitecoinTestnet]))