def format_message(self, msg, kwargs, highlight, level): if getattr(self, 'log_json', False): message = dict() message['event'] = '{}.{}'.format(self.name, msg.lower().replace(' ', '_')) message['level'] = logging.getLevelName(level) try: message.update(kwargs) try: msg = json.dumps(message, cls=_LogJSONEncoder) except TypeError: # Invalid value. With our custom encoder this can only happen with non-string # dict keys (see: https://bugs.python.org/issue18820). message = _stringify_dict_keys(message) msg = json.dumps(message, cls=_LogJSONEncoder) except UnicodeDecodeError: message.update({ k: v if isnumeric(v) or isinstance(v, (float, complex)) else repr(v) for k, v in kwargs.items() }) msg = json.dumps(message, cls=_LogJSONEncoder) else: msg = "{}{} {}{}".format( bcolors.WARNING if highlight else "", msg, " ".join("{}={!s}".format(k, v) for k, v in kwargs.items()), bcolors.ENDC if highlight else "") return msg
def format_message(self, msg, kwargs, highlight, level): if getattr(self, 'log_json', False): message = dict() message['event'] = '{}.{}'.format(self.name, msg.lower().replace(' ', '_')) message['level'] = logging.getLevelName(level) try: message.update(kwargs) try: msg = json.dumps(message, cls=_LogJSONEncoder) except TypeError: # Invalid value. With our custom encoder this can only happen with non-string # dict keys (see: https://bugs.python.org/issue18820). message = _stringify_dict_keys(message) msg = json.dumps(message, cls=_LogJSONEncoder) except UnicodeDecodeError: message.update({ k: v if isnumeric(v) or isinstance(v, (float, complex)) else repr(v) for k, v in kwargs.items() }) msg = json.dumps(message, cls=_LogJSONEncoder) else: msg = "{}{} {}{}".format( bcolors.WARNING if highlight else "", msg, " ".join("{}={!s}".format(k, v) for k, v in kwargs.items()), bcolors.ENDC if highlight else "" ) return msg
def mine(block_number, difficulty, mining_hash, start_nonce=0, rounds=1000): assert utils.isnumeric(start_nonce) cache = get_cache(block_number) nonce = start_nonce target = utils.zpad(utils.int_to_big_endian(2**256 // (difficulty or 1)), 32) for i in range(1, rounds + 1): bin_nonce = utils.zpad(utils.int_to_big_endian((nonce + i) & TT64M1), 8) o = hashimoto_light(block_number, cache, mining_hash, bin_nonce) if o["result"] <= target: log.debug("nonce found") assert len(bin_nonce) == 8 assert len(o["mix digest"]) == 32 return bin_nonce, o["mix digest"] return None, None
def format_message(self, msg, kwargs, highlight): if getattr(self, 'log_json', False): message = { k: v if isnumeric(v) or isinstance(v, (float, complex)) else repr(v) for k, v in kwargs.items() } message['event'] = "{}.{}".format(self.name, msg) msg = json.dumps(message) else: msg = "{}{} {}{}".format( bcolors.WARNING if highlight else "", msg, " ".join("{}={!s}".format(k, v) for k, v in kwargs.items()), bcolors.ENDC if highlight else "") return msg
def format_message(self, msg, kwargs, highlight): if getattr(self, 'log_json', False): message = { k: v if isnumeric(v) or isinstance(v, (float, complex)) else repr(v) for k, v in kwargs.items() } message['event'] = "{}.{}".format(self.name, msg) msg = json.dumps(message) else: msg = "{}{} {}{}".format( bcolors.WARNING if highlight else "", msg, " ".join("{}={!s}".format(k, v) for k, v in kwargs.items()), bcolors.ENDC if highlight else "" ) return msg
def encode_single(typ, arg): base, sub, _ = typ # Unsigned integers: uint<sz> if base == 'uint': sub = int(sub) i = decint(arg) if not 0 <= i < 2**sub: raise ValueOutOfBounds(repr(arg)) return zpad(encode_int(i), 32) # bool: int<sz> elif base == 'bool': assert isinstance(arg, bool) return zpad(encode_int(int(arg)), 32) # Signed integers: int<sz> elif base == 'int': sub = int(sub) i = decint(arg) if not -2**(sub - 1) <= i < 2**sub: raise ValueOutOfBounds(repr(arg)) return zpad(encode_int(i % 2**sub), 32) # Unsigned reals: ureal<high>x<low> elif base == 'ureal': high, low = [int(x) for x in sub.split('x')] if not 0 <= arg < 2**high: raise ValueOutOfBounds(repr(arg)) return zpad(encode_int(arg * 2**low), 32) # Signed reals: real<high>x<low> elif base == 'real': high, low = [int(x) for x in sub.split('x')] if not -2**(high - 1) <= arg < 2**(high - 1): raise ValueOutOfBounds(repr(arg)) return zpad(encode_int((arg % 2**high) * 2**low), 32) # Strings elif base == 'string' or base == 'bytes': if not is_string(arg): raise EncodingError("Expecting string: %r" % arg) # Fixed length: string<sz> if len(sub): assert int(sub) <= 32 assert len(arg) <= int(sub) return arg + b'\x00' * (32 - len(arg)) # Variable length: string else: return zpad(encode_int(len(arg)), 32) + \ arg + \ b'\x00' * (utils.ceil32(len(arg)) - len(arg)) # Hashes: hash<sz> elif base == 'hash': if not (int(sub) and int(sub) <= 32): raise EncodingError("too long: %r" % arg) if isnumeric(arg): return zpad(encode_int(arg), 32) elif len(arg) == len(sub): return zpad(arg, 32) elif len(arg) == len(sub) * 2: return zpad(decode_hex(arg), 32) else: raise EncodingError("Could not parse hash: %r" % arg) # Addresses: address (== hash160) elif base == 'address': assert sub == '' if isnumeric(arg): return zpad(encode_int(arg), 32) elif len(arg) == 20: return zpad(arg, 32) elif len(arg) == 40: return zpad(decode_hex(arg), 32) else: raise EncodingError("Could not parse address: %r" % arg) raise EncodingError("Unhandled type: %r %r" % (base, sub))
def encode_single(typ, arg): # pylint: disable=too-many-return-statements,too-many-branches,too-many-statements,too-many-locals ''' Encode `arg` as `typ`. `arg` will be encoded in a best effort manner, were necessary the function will try to correctly define the underlying binary representation (ie. decoding a hex-encoded address/hash). Args: typ (Tuple[(str, int, list)]): A 3-tuple defining the `arg` type. The first element defines the type name. The second element defines the type length in bits. The third element defines if it's an array type. Together the first and second defines the elementary type, the third element must be present but is ignored. Valid type names are: - uint - int - bool - ufixed - fixed - string - bytes - hash - address arg (object): The object to be encoded, it must be a python object compatible with the `typ`. Raises: ValueError: when an invalid `typ` is supplied. ValueOutOfBounds: when `arg` cannot be encoded as `typ` because of the binary contraints. Note: This function don't work with array types, for that use the `enc` function. ''' base, sub, _ = typ if base == 'uint': sub = int(sub) if not (0 < sub <= 256 and sub % 8 == 0): raise ValueError( 'invalid unsigned integer bit length {}'.format(sub)) try: i = decint(arg, signed=False) except EncodingError: # arg is larger than 2**256 raise ValueOutOfBounds(repr(arg)) if not 0 <= i < 2**sub: raise ValueOutOfBounds(repr(arg)) value_encoded = int_to_big_endian(i) return zpad(value_encoded, 32) if base == 'int': sub = int(sub) bits = sub - 1 if not (0 < sub <= 256 and sub % 8 == 0): raise ValueError('invalid integer bit length {}'.format(sub)) try: i = decint(arg, signed=True) except EncodingError: # arg is larger than 2**255 raise ValueOutOfBounds(repr(arg)) if not -2**bits <= i < 2**bits: raise ValueOutOfBounds(repr(arg)) value = i % 2**sub # convert negative to "equivalent" positive value_encoded = int_to_big_endian(value) return zpad(value_encoded, 32) if base == 'bool': if arg is True: value_encoded = int_to_big_endian(1) elif arg is False: value_encoded = int_to_big_endian(0) else: raise ValueError('%r is not bool' % arg) return zpad(value_encoded, 32) if base == 'ufixed': sub = str(sub) # pylint: disable=redefined-variable-type high_str, low_str = sub.split('x') high = int(high_str) low = int(low_str) if not (0 < high + low <= 256 and high % 8 == 0 and low % 8 == 0): raise ValueError('invalid unsigned fixed length {}'.format(sub)) if not 0 <= arg < 2**high: raise ValueOutOfBounds(repr(arg)) float_point = arg * 2**low fixed_point = int(float_point) return zpad(int_to_big_endian(fixed_point), 32) if base == 'fixed': sub = str(sub) # pylint: disable=redefined-variable-type high_str, low_str = sub.split('x') high = int(high_str) low = int(low_str) bits = high - 1 if not (0 < high + low <= 256 and high % 8 == 0 and low % 8 == 0): raise ValueError('invalid unsigned fixed length {}'.format(sub)) if not -2**bits <= arg < 2**bits: raise ValueOutOfBounds(repr(arg)) float_point = arg * 2**low fixed_point = int(float_point) value = fixed_point % 2**256 return zpad(int_to_big_endian(value), 32) if base == 'string': if isinstance(arg, utils.unicode): arg = arg.encode('utf8') else: try: arg.decode('utf8') except UnicodeDecodeError: raise ValueError('string must be utf8 encoded') if len(sub): # fixed length if not 0 <= len(arg) <= int(sub): raise ValueError('invalid string length {}'.format(sub)) if not 0 <= int(sub) <= 32: raise ValueError('invalid string length {}'.format(sub)) return rzpad(arg, 32) if not 0 <= len(arg) < TT256: raise Exception('Integer invalid or out of range: %r' % arg) length_encoded = zpad(int_to_big_endian(len(arg)), 32) value_encoded = rzpad(arg, utils.ceil32(len(arg))) return length_encoded + value_encoded if base == 'bytes': if not is_string(arg): raise EncodingError('Expecting string: %r' % arg) arg = utils.to_string(arg) # py2: force unicode into str if len(sub): # fixed length if not 0 <= len(arg) <= int(sub): raise ValueError('string must be utf8 encoded') if not 0 <= int(sub) <= 32: raise ValueError('string must be utf8 encoded') return rzpad(arg, 32) if not 0 <= len(arg) < TT256: raise Exception('Integer invalid or out of range: %r' % arg) length_encoded = zpad(int_to_big_endian(len(arg)), 32) value_encoded = rzpad(arg, utils.ceil32(len(arg))) return length_encoded + value_encoded if base == 'hash': if not (int(sub) and int(sub) <= 32): raise EncodingError('too long: %r' % arg) if isnumeric(arg): return zpad(encode_int(arg), 32) if len(arg) == int(sub): return zpad(arg, 32) if len(arg) == int(sub) * 2: return zpad(decode_hex(arg), 32) raise EncodingError('Could not parse hash: %r' % arg) if base == 'address': assert sub == '' if isnumeric(arg): return zpad(encode_int(arg), 32) if len(arg) == 20: return zpad(arg, 32) if len(arg) == 40: return zpad(decode_hex(arg), 32) if len(arg) == 42 and arg[:2] == '0x': return zpad(decode_hex(arg[2:]), 32) raise EncodingError('Could not parse address: %r' % arg) raise EncodingError('Unhandled type: %r %r' % (base, sub))