当前位置: 首页>>代码示例>>Python>>正文


Python math.clip函数代码示例

本文整理汇总了Python中util.math.clip函数的典型用法代码示例。如果您正苦于以下问题:Python clip函数的具体用法?Python clip怎么用?Python clip使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了clip函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_work


#.........这里部分代码省略.........
                block_target=self.current_work.value['bits'].target,
                desired_timestamp=int(time.time() + 0.5),
                desired_target=desired_share_target,
                ref_merkle_link=dict(branch=[], index=0),
                desired_other_transaction_hashes_and_fees=zip(tx_hashes, self.current_work.value['transaction_fees']),
                net=self.node.net,
                known_txs=tx_map,
                base_subsidy=self.node.net.PARENT.SUBSIDY_FUNC(self.current_work.value['height']),
            )
		    
		    
       
        
        
        packed_gentx = bitcoin_data.tx_type.pack(gentx)
        other_transactions = [tx_map[tx_hash] for tx_hash in other_transaction_hashes]
        
        mm_later = [(dict(aux_work, target=aux_work['target'] if aux_work['target'] == 'p2pool' else share_info['bits'].target), index, hashes) for aux_work, index, hashes in mm_later]
        
        if desired_pseudoshare_target is None:
            target = 2**256-1
            local_hash_rate = self._estimate_local_hash_rate()
            if local_hash_rate is not None:
                target = min(target,
                    bitcoin_data.average_attempts_to_target(local_hash_rate * 1)) # limit to 1 share response every second by modulating pseudoshare difficulty
        else:
            target = desired_pseudoshare_target
           
       # target = max(target, share_info['bits'].target)
        
        for aux_work, index, hashes in mm_later:
            target = max(target, aux_work['target'])
         
        target = math.clip(target, self.node.net.PARENT.SANE_TARGET_RANGE)
      
        
        getwork_time = time.time()
        lp_count = self.new_work_event.times
        merkle_link = bitcoin_data.calculate_merkle_link([None] + other_transaction_hashes, 0)
        
        print 'New work for worker! Difficulty: %.06f Share difficulty: %.08f Total block value: %.6f %s including %i transactions' % (
            bitcoin_data.target_to_difficulty(target),
            bitcoin_data.target_to_difficulty(share_info['bits'].target),
            self.current_work.value['subsidy']*1e-8, self.node.net.PARENT.SYMBOL,
            len(self.current_work.value['transactions']),
        )
        
        ba = dict(
            version=min(self.current_work.value['version'], 2),
            previous_block=self.current_work.value['previous_block'],
            merkle_link=merkle_link,
            coinb1=packed_gentx[:-self.COINBASE_NONCE_LENGTH-4],
            coinb2=packed_gentx[-4:],
            timestamp=self.current_work.value['time'],
            bits=self.current_work.value['bits'],
            share_target=target,
        )
        
        received_header_hashes = set()
        
        def got_response(header, user, coinbase_nonce):
            assert len(coinbase_nonce) == self.COINBASE_NONCE_LENGTH
            new_packed_gentx = packed_gentx[:-self.COINBASE_NONCE_LENGTH-4] + coinbase_nonce + packed_gentx[-4:] if coinbase_nonce != '\0'*self.COINBASE_NONCE_LENGTH else packed_gentx
            new_gentx = bitcoin_data.tx_type.unpack(new_packed_gentx) if coinbase_nonce != '\0'*self.COINBASE_NONCE_LENGTH else gentx
            
           
开发者ID:cqtenq,项目名称:p2pool-neoscrypt,代码行数:65,代码来源:work.py

示例2: get_work

    def get_work(self, pubkey, desired_share_target, desired_pseudoshare_target):
        if (self.node.p2p_node is None or len(self.node.p2p_node.peers) == 0) and self.node.net.PERSIST:
            raise jsonrpc.Error_for_code(-12345)(u'p2pool is not connected to any peers')
        if self.node.best_share_var.value is None and self.node.net.PERSIST:
            raise jsonrpc.Error_for_code(-12345)(u'p2pool is downloading shares')
        if time.time() > self.current_work.value['last_update'] + 60:
            raise jsonrpc.Error_for_code(-12345)(u'lost contact with bitcoind')
        
        if self.merged_work.value:
            tree, size = bitcoin_data.make_auxpow_tree(self.merged_work.value)
            mm_hashes = [self.merged_work.value.get(tree.get(i), dict(hash=0))['hash'] for i in xrange(size)]
            mm_data = '\xfa\xbemm' + bitcoin_data.aux_pow_coinbase_type.pack(dict(
                merkle_root=bitcoin_data.merkle_hash(mm_hashes),
                size=size,
                nonce=0,
            ))
            mm_later = [(aux_work, mm_hashes.index(aux_work['hash']), mm_hashes) for chain_id, aux_work in self.merged_work.value.iteritems()]
        else:
            mm_data = ''
            mm_later = []
        
        tx_hashes = [bitcoin_data.hash256(bitcoin_data.tx_type.pack(tx)) for tx in self.current_work.value['transactions']]
        tx_map = dict(zip(tx_hashes, self.current_work.value['transactions']))
        txn_timestamp = self.current_work.value['txn_timestamp']
        
        #print
        #print txn_timestamp
        #print
        
        if self.node.best_share_var.value is None:
            share_type = p2pool_data.Share
        else:
            previous_share = self.node.tracker.items[self.node.best_share_var.value]
            previous_share_type = type(previous_share)
            
            if previous_share_type.SUCCESSOR is None or self.node.tracker.get_height(previous_share.hash) < self.node.net.CHAIN_LENGTH:
                share_type = previous_share_type
            else:
                successor_type = previous_share_type.SUCCESSOR
                
                counts = p2pool_data.get_desired_version_counts(self.node.tracker,
                    self.node.tracker.get_nth_parent_hash(previous_share.hash, self.node.net.CHAIN_LENGTH*9//10), self.node.net.CHAIN_LENGTH//10)
                # Share -> NewShare only valid if 85% of hashes in [net.CHAIN_LENGTH*9//10, net.CHAIN_LENGTH] for new version
                if counts.get(successor_type.VERSION, 0) > sum(counts.itervalues())*95//100:
                    share_type = successor_type
                else:
                    share_type = previous_share_type
        
        if True:
            subsidy = self.node.net.PARENT.SUBSIDY_FUNC(self.current_work.value['bits'].target)
            desired_timestamp = int(time.time() + 0.5)

            share_info, gentx, other_transaction_hashes, get_share = share_type.generate_transaction(
                tracker=self.node.tracker,
                share_data=dict(
                    previous_share_hash=self.node.best_share_var.value,
                    coinbase=(script.create_push_script([
                        self.current_work.value['height'],
                        ] + ([mm_data] if mm_data else []) + [
                    ]) + self.current_work.value['coinbaseflags'])[:100],
                    nonce=random.randrange(2**32),
                    pubkey=pubkey,
                    subsidy=self.current_work.value['subsidy'],
                    donation=math.perfect_round(65535*self.donation_percentage/100),
                    stale_info=(lambda (orphans, doas), total, (orphans_recorded_in_chain, doas_recorded_in_chain):
                        'orphan' if orphans > orphans_recorded_in_chain else
                        'doa' if doas > doas_recorded_in_chain else
                        None
                    )(*self.get_stale_counts()),
                    desired_version=14,
                ),
                block_target=self.current_work.value['bits'].target,
                desired_timestamp=desired_timestamp if txn_timestamp < desired_timestamp else txn_timestamp + 1,
                desired_target=desired_share_target,
                ref_merkle_link=dict(branch=[], index=0),
                desired_other_transaction_hashes_and_fees=zip(tx_hashes, self.current_work.value['transaction_fees']),
                net=self.node.net,
                known_txs=tx_map,
                base_subsidy=subsidy
            )
        
        packed_gentx = bitcoin_data.tx_type.pack(gentx)
        other_transactions = [tx_map[tx_hash] for tx_hash in other_transaction_hashes]
        
        mm_later = [(dict(aux_work, target=aux_work['target'] if aux_work['target'] != 'p2pool' else share_info['bits'].target), index, hashes) for aux_work, index, hashes in mm_later]
        
        if desired_pseudoshare_target is None:
            target = 2**256-1
            if len(self.recent_shares_ts_work) == 50:
                hash_rate = sum(work for ts, work in self.recent_shares_ts_work[1:])//(self.recent_shares_ts_work[-1][0] - self.recent_shares_ts_work[0][0])
                if hash_rate:
                    target = min(target, int(2**256/hash_rate))
        else:
            target = desired_pseudoshare_target
        target = max(target, share_info['bits'].target)
        for aux_work, index, hashes in mm_later:
            target = max(target, aux_work['target'])
        target = math.clip(target, self.node.net.PARENT.SANE_TARGET_RANGE)
        
        getwork_time = time.time()
#.........这里部分代码省略.........
开发者ID:yuriell,项目名称:p2pool,代码行数:101,代码来源:work.py

示例3: get_work


#.........这里部分代码省略.........
                desired_target=desired_share_target,
                ref_merkle_link=dict(branch=[], index=0),
                desired_other_transaction_hashes=tx_hashes,
                net=self.node.net,
                known_txs=tx_map,
            )

        transactions = [gentx] + [tx_map[tx_hash] for tx_hash in other_transaction_hashes]

        mm_later = [
            (
                dict(
                    aux_work, target=aux_work["target"] if aux_work["target"] != "p2pool" else share_info["bits"].target
                ),
                index,
                hashes,
            )
            for aux_work, index, hashes in mm_later
        ]

        if desired_pseudoshare_target is None:
            target = 2 ** 256 - 1
            if len(self.recent_shares_ts_work) == 50:
                hash_rate = sum(work for ts, work in self.recent_shares_ts_work[1:]) // (
                    self.recent_shares_ts_work[-1][0] - self.recent_shares_ts_work[0][0]
                )
                if hash_rate:
                    target = min(target, int(2 ** 256 / hash_rate))
        else:
            target = desired_pseudoshare_target
        target = max(target, share_info["bits"].target)
        for aux_work, index, hashes in mm_later:
            target = max(target, aux_work["target"])
        target = math.clip(target, self.node.net.PARENT.SANE_TARGET_RANGE)

        getwork_time = time.time()
        lp_count = self.new_work_event.times
        merkle_link = bitcoin_data.calculate_merkle_link(
            [bitcoin_data.hash256(bitcoin_data.tx_type.pack(tx)) for tx in transactions], 0
        )

        print "New work for worker! Difficulty: %.06f Share difficulty: %.06f Total block value: %.6f %s including %i transactions" % (
            bitcoin_data.target_to_difficulty(target),
            bitcoin_data.target_to_difficulty(share_info["bits"].target),
            self.current_work.value["subsidy"] * 1e-8,
            self.node.net.PARENT.SYMBOL,
            len(self.current_work.value["transactions"]),
        )

        ba = bitcoin_getwork.BlockAttempt(
            version=min(self.current_work.value["version"], 2),
            previous_block=self.current_work.value["previous_block"],
            merkle_root=bitcoin_data.check_merkle_link(
                bitcoin_data.hash256(bitcoin_data.tx_type.pack(transactions[0])), merkle_link
            ),
            timestamp=self.current_work.value["time"],
            bits=self.current_work.value["bits"],
            share_target=target,
        )

        received_header_hashes = set()

        def got_response(header, request):
            header_hash = bitcoin_data.hash256(bitcoin_data.block_header_type.pack(header))
            pow_hash = self.node.net.PARENT.POW_FUNC(bitcoin_data.block_header_type.pack(header))
            try:
开发者ID:hua002,项目名称:p2pool,代码行数:67,代码来源:work.py

示例4: get_work


#.........这里部分代码省略.........
                ref_merkle_link=dict(branch=[], index=0),
                desired_other_transaction_hashes_and_fees=zip(tx_hashes, self.current_work.value["transaction_fees"]),
                net=self.node.net,
                known_txs=tx_map,
                base_subsidy=self.node.net.PARENT.SUBSIDY_FUNC(self.current_work.value["height"]),
            )

        packed_gentx = bitcoin_data.tx_type.pack(gentx)
        other_transactions = [tx_map[tx_hash] for tx_hash in other_transaction_hashes]

        mm_later = [
            (
                dict(
                    aux_work, target=aux_work["target"] if aux_work["target"] != "p2pool" else share_info["bits"].target
                ),
                index,
                hashes,
            )
            for aux_work, index, hashes in mm_later
        ]

        if desired_pseudoshare_target is None:
            target = 2 ** 256 - 1
            local_hash_rate = self._estimate_local_hash_rate()
            if local_hash_rate is not None:
                target = min(
                    target, bitcoin_data.average_attempts_to_target(local_hash_rate * 3)
                )  # limit to 1 share response every 3 seconds by modulating pseudoshare difficulty
        else:
            target = desired_pseudoshare_target
        target = max(target, share_info["bits"].target)
        for aux_work, index, hashes in mm_later:
            target = max(target, aux_work["target"])
        target = math.clip(target, self.node.net.PARENT.SANE_TARGET_RANGE)

        getwork_time = time.time()
        lp_count = self.new_work_event.times
        merkle_link = bitcoin_data.calculate_merkle_link([None] + other_transaction_hashes, 0)

        print "New work for worker! Difficulty: %.09f Share difficulty: %.09f Total block value: %.6f %s including %i transactions" % (
            bitcoin_data.target_to_difficulty(target),
            bitcoin_data.target_to_difficulty(share_info["bits"].target),
            self.current_work.value["subsidy"] * 1e-8,
            self.node.net.PARENT.SYMBOL,
            len(self.current_work.value["transactions"]),
        )

        ba = dict(
            version=min(self.current_work.value["version"], 2),
            previous_block=self.current_work.value["previous_block"],
            merkle_link=merkle_link,
            coinb1=packed_gentx[: -self.COINBASE_NONCE_LENGTH - 4],
            coinb2=packed_gentx[-4:],
            timestamp=self.current_work.value["time"],
            bits=self.current_work.value["bits"],
            share_target=target,
        )

        received_header_hashes = set()

        def got_response(header, user, coinbase_nonce):
            assert len(coinbase_nonce) == self.COINBASE_NONCE_LENGTH
            new_packed_gentx = (
                packed_gentx[: -self.COINBASE_NONCE_LENGTH - 4] + coinbase_nonce + packed_gentx[-4:]
                if coinbase_nonce != "\0" * self.COINBASE_NONCE_LENGTH
                else packed_gentx
开发者ID:Cloudsy,项目名称:p2pool-Rewardcoin,代码行数:67,代码来源:work.py

示例5: get_work

 def get_work(self, pubkey_hash, desired_share_target, desired_pseudoshare_target):
     if len(self.p2p_node.peers) == 0 and self.net.PERSIST:
         raise jsonrpc.Error_for_code(-12345)(u'p2pool is not connected to any peers')
     if self.best_share_var.value is None and self.net.PERSIST:
         raise jsonrpc.Error_for_code(-12345)(u'p2pool is downloading shares')
     if time.time() > self.current_work.value['last_update'] + 60:
         raise jsonrpc.Error_for_code(-12345)(u'lost contact with bitcoind')
     
     if self.merged_work.value:
         tree, size = bitcoin_data.make_auxpow_tree(self.merged_work.value)
         mm_hashes = [self.merged_work.value.get(tree.get(i), dict(hash=0))['hash'] for i in xrange(size)]
         mm_data = '\xfa\xbemm' + bitcoin_data.aux_pow_coinbase_type.pack(dict(
             merkle_root=bitcoin_data.merkle_hash(mm_hashes),
             size=size,
             nonce=0,
         ))
         mm_later = [(aux_work, mm_hashes.index(aux_work['hash']), mm_hashes) for chain_id, aux_work in self.merged_work.value.iteritems()]
     else:
         mm_data = ''
         mm_later = []
     
     tx_hashes = [bitcoin_data.hash256(bitcoin_data.tx_type.pack(tx)) for tx in self.current_work.value['transactions']]
     tx_map = dict(zip(tx_hashes, self.current_work.value['transactions']))
     
     if True:
         share_info, gentx, other_transaction_hashes, get_share = p2pool_data.Share.generate_transaction(
             tracker=self.tracker,
             share_data=dict(
                 previous_share_hash=self.best_share_var.value,
                 coinbase=(script.create_push_script([
                     self.current_work.value['height'],
                     ] + ([mm_data] if mm_data else []) + [
                 ]) + self.current_work.value['coinbaseflags'])[:100],
                 nonce=random.randrange(2**32),
                 pubkey_hash=pubkey_hash,
                 subsidy=self.current_work.value['subsidy'],
                 donation=math.perfect_round(65535*self.donation_percentage/100),
                 stale_info=(lambda (orphans, doas), total, (orphans_recorded_in_chain, doas_recorded_in_chain):
                     'orphan' if orphans > orphans_recorded_in_chain else
                     'doa' if doas > doas_recorded_in_chain else
                     None
                 )(*self.get_stale_counts()),
                 desired_version=5,
             ),
             block_target=self.current_work.value['bits'].target,
             desired_timestamp=int(time.time() + 0.5),
             desired_target=desired_share_target,
             ref_merkle_link=dict(branch=[], index=0),
             other_transaction_hashes=tx_hashes,
             net=self.net,
         )
     
     transactions = [gentx] + [tx_map[tx_hash] for tx_hash in other_transaction_hashes]
     
     mm_later = [(dict(aux_work, target=aux_work['target'] if aux_work['target'] != 'p2pool' else share_info['bits'].target), index, hashes) for aux_work, index, hashes in mm_later]
     
     if desired_pseudoshare_target is None:
         target = 2**256-1
         if len(self.recent_shares_ts_work) == 50:
             hash_rate = sum(work for ts, work in self.recent_shares_ts_work[1:])//(self.recent_shares_ts_work[-1][0] - self.recent_shares_ts_work[0][0])
             if hash_rate:
                 target = min(target, int(2**256/hash_rate))
     else:
         target = desired_pseudoshare_target
     target = max(target, share_info['bits'].target)
     for aux_work, index, hashes in mm_later:
         target = max(target, aux_work['target'])
     target = math.clip(target, self.net.PARENT.SANE_TARGET_RANGE)
     
     getwork_time = time.time()
     lp_count = self.new_work_event.times
     merkle_link = bitcoin_data.calculate_merkle_link([bitcoin_data.hash256(bitcoin_data.tx_type.pack(tx)) for tx in transactions], 0)
     
     print 'New work for worker! Difficulty: %.06f Share difficulty: %.06f Total block value: %.6f %s including %i transactions' % (
         bitcoin_data.target_to_difficulty(target),
         bitcoin_data.target_to_difficulty(share_info['bits'].target),
         self.current_work.value['subsidy']*1e-8, self.net.PARENT.SYMBOL,
         len(self.current_work.value['transactions']),
     )
     
     ba = bitcoin_getwork.BlockAttempt(
         version=min(self.current_work.value['version'], 2),
         previous_block=self.current_work.value['previous_block'],
         merkle_root=bitcoin_data.check_merkle_link(bitcoin_data.hash256(bitcoin_data.tx_type.pack(transactions[0])), merkle_link),
         timestamp=self.current_work.value['time'],
         bits=self.current_work.value['bits'],
         share_target=target,
     )
     
     received_header_hashes = set()
     
     def got_response(header, request):
         header_hash = bitcoin_data.hash256(bitcoin_data.block_header_type.pack(header))
         pow_hash = self.net.PARENT.POW_FUNC(bitcoin_data.block_header_type.pack(header))
         try:
             if pow_hash <= header['bits'].target or p2pool.DEBUG:
                 self.submit_block(dict(header=header, txs=transactions), ignore_failure=False)
                 if pow_hash <= header['bits'].target:
                     print
                     print 'GOT BLOCK FROM MINER! Passing to bitcoind! %s%064x' % (self.net.PARENT.BLOCK_EXPLORER_URL_PREFIX, header_hash)
#.........这里部分代码省略.........
开发者ID:alaudidae,项目名称:p2pool,代码行数:101,代码来源:work.py


注:本文中的util.math.clip函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。