asdadsadsadsad

This commit is contained in:
Thomas Zarebczan 2018-11-16 17:55:56 -05:00
parent dd611c8b27
commit 857047ba23

View file

@ -520,65 +520,78 @@ def can_connect(header: dict) -> Optional[Blockchain]:
return None return None
# see src/arith_uint256.cpp in lbrycrd # see src/arith_uint256.cpp in lbrycrd
class ArithUint256(object): class ArithUint256:
def __init__(self, value): # https://github.com/bitcoin/bitcoin/blob/master/src/arith_uint256.cpp
__slots__ = '_value', '_compact'
def __init__(self, value: int) -> None:
self._value = value self._value = value
self._compact: Optional[int] = None
def __str__(self):
return hex(self._value)
@staticmethod
def fromCompact(nCompact):
"""Convert a compact representation into its value"""
nSize = nCompact >> 24
# the lower 23 bits
nWord = nCompact & 0x007fffff
if nSize <= 3:
return nWord >> 8 * (3 - nSize)
else:
return nWord << 8 * (nSize - 3)
@classmethod @classmethod
def SetCompact(cls, nCompact): def from_compact(cls, compact) -> 'ArithUint256':
return cls(ArithUint256.fromCompact(nCompact)) size = compact >> 24
word = compact & 0x007fffff
if size <= 3:
return cls(word >> 8 * (3 - size))
else:
return cls(word << 8 * (size - 3))
def bits(self): @property
"""Returns the position of the highest bit set plus one.""" def value(self) -> int:
bn = bin(self._value)[2:] return self._value
for i, d in enumerate(bn):
@property
def compact(self) -> int:
if self._compact is None:
self._compact = self._calculate_compact()
return self._compact
@property
def negative(self) -> int:
return self._calculate_compact(negative=True)
@property
def bits(self) -> int:
""" Returns the position of the highest bit set plus one. """
bits = bin(self._value)[2:]
for i, d in enumerate(bits):
if d: if d:
return (len(bn) - i) + 1 return (len(bits) - i) + 1
return 0 return 0
def GetLow64(self): @property
def low64(self) -> int:
return self._value & 0xffffffffffffffff return self._value & 0xffffffffffffffff
def GetCompact(self): def _calculate_compact(self, negative=False) -> int:
"""Convert a value into its compact representation""" size = (self.bits + 7) // 8
nSize = (self.bits() + 7) // 8 if size <= 3:
nCompact = 0 compact = self.low64 << 8 * (3 - size)
if nSize <= 3:
nCompact = self.GetLow64() << 8 * (3 - nSize)
else: else:
bn = ArithUint256(self._value >> 8 * (nSize - 3)) compact = ArithUint256(self._value >> 8 * (size - 3)).low64
nCompact = bn.GetLow64()
# The 0x00800000 bit denotes the sign. # The 0x00800000 bit denotes the sign.
# Thus, if it is already set, divide the mantissa by 256 and increase the exponent. # Thus, if it is already set, divide the mantissa by 256 and increase the exponent.
if nCompact & 0x00800000: if compact & 0x00800000:
nCompact >>= 8 compact >>= 8
nSize += 1 size += 1
assert (nCompact & ~0x007fffff) == 0 assert (compact & ~0x007fffff) == 0
assert nSize < 256 assert size < 256
nCompact |= nSize << 24 compact |= size << 24
return nCompact if negative and compact & 0x007fffff:
compact |= 0x00800000
return compact
def __mul__(self, x): def __mul__(self, x):
# Take the mod because we are limited to an unsigned 256 bit number # Take the mod because we are limited to an unsigned 256 bit number
return ArithUint256((self._value * x) % 2 ** 256) return ArithUint256((self._value * x) % 2 ** 256)
def __idiv__(self, x): def __truediv__(self, x):
self._value = (self._value // x) return ArithUint256(int(self._value / x))
return self
def __gt__(self, x): def __gt__(self, other):
return self._value > x return self._value > other
def __lt__(self, other):
return self._value < other