From: Forrest Voight Date: Mon, 24 Jun 2013 02:06:40 +0000 (-0400) Subject: added some checks to bitcoin target util functions X-Git-Tag: 13.0~19 X-Git-Url: https://git.novaco.in/?p=p2pool.git;a=commitdiff_plain;h=ac13bde7f9370979f46226b1f80677af4f710233 added some checks to bitcoin target util functions --- diff --git a/p2pool/bitcoin/data.py b/p2pool/bitcoin/data.py index de6d405..8e4fa52 100644 --- a/p2pool/bitcoin/data.py +++ b/p2pool/bitcoin/data.py @@ -2,6 +2,7 @@ from __future__ import division import hashlib import random +import warnings import p2pool from p2pool.util import math, pack @@ -214,13 +215,18 @@ def check_merkle_link(tip_hash, link): # targets def target_to_average_attempts(target): + assert 0 <= target and isinstance(target, (int, long)), target + if target >= 2**256: warnings.warn('target >= 2**256!') return 2**256//(target + 1) def target_to_difficulty(target): + assert 0 <= target and isinstance(target, (int, long)), target + if target >= 2**256: warnings.warn('target >= 2**256!') return (0xffff0000 * 2**(256-64) + 1)/(target + 1) def difficulty_to_target(difficulty): - return (0xffff0000 * 2**(256-64) + 1)/difficulty - 1 + assert difficulty >= 0 + return min(int((0xffff0000 * 2**(256-64) + 1)/difficulty - 1 + 0.5), 2**256-1) # human addresses diff --git a/p2pool/data.py b/p2pool/data.py index 1793043..8881623 100644 --- a/p2pool/data.py +++ b/p2pool/data.py @@ -514,7 +514,7 @@ class OkayTracker(forest.Tracker): best = best_share.previous_hash timestamp_cutoff = min(int(time.time()), best_share.timestamp) - 3600 - target_cutoff = 2**256//(self.net.SHARE_PERIOD*best_tail_score[1] + 1) * 2 if best_tail_score[1] is not None else 2**256-1 + 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