def from_snapshot(cls, snapshot_data, env, executing_on_head=False): state = State(env=env) if "state_root" in snapshot_data: state.trie.root_hash = parse_as_bin(snapshot_data["state_root"]) else: raise Exception("Must specify either alloc or state root parameter") for k, default in STATE_DEFAULTS.items(): default = copy.copy(default) v = snapshot_data[k] if k in snapshot_data else None if is_numeric(default): setattr(state, k, parse_as_int(v) if k in snapshot_data else default) elif is_string(default): setattr(state, k, parse_as_bin(v) if k in snapshot_data else default) elif k == "prev_headers": if k in snapshot_data: headers = [dict_to_prev_header(h) for h in v] else: headers = default setattr(state, k, headers) elif k == "recent_uncles": if k in snapshot_data: uncles = {} for height, _uncles in v.items(): uncles[int(height)] = [] for uncle in _uncles: uncles[int(height)].append(parse_as_bin(uncle)) else: uncles = default setattr(state, k, uncles) if executing_on_head: state.executing_on_head = True state.commit() state.changed = {} return state
def to_snapshot(self, root_only=False, no_prevblocks=False): snapshot = {} if root_only: # Smaller snapshot format that only includes the state root # (requires original DB to re-initialize) snapshot["state_root"] = "0x" + encode_hex(self.trie.root_hash) else: # "Full" snapshot snapshot["alloc"] = self.to_dict() # Save non-state-root variables for k, default in STATE_DEFAULTS.items(): default = copy.copy(default) v = getattr(self, k) if is_numeric(default): snapshot[k] = str(v) elif isinstance(default, (str, bytes)): snapshot[k] = "0x" + encode_hex(v) elif k == "prev_headers" and not no_prevblocks: snapshot[k] = [ prev_header_to_dict(h) for h in v[:self.config["PREV_HEADER_DEPTH"]] ] elif k == "recent_uncles" and not no_prevblocks: snapshot[k] = { str(n): ["0x" + encode_hex(h) for h in headers] for n, headers in v.items() } return snapshot
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 is_numeric(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 from_snapshot(cls, snapshot_data, env, executing_on_head=False): state = State(env=env) if "alloc" in snapshot_data: for addr, data in snapshot_data["alloc"].items(): if len(addr) == 40: addr = decode_hex(addr) assert len(addr) == 20 if "wei" in data: state.set_balance(addr, parse_as_int(data["wei"])) if "balance" in data: state.set_balance(addr, parse_as_int(data["balance"])) if "code" in data: state.set_code(addr, parse_as_bin(data["code"])) if "nonce" in data: state.set_nonce(addr, parse_as_int(data["nonce"])) if "storage" in data: for k, v in data["storage"].items(): state.set_storage_data( addr, big_endian_to_int(parse_as_bin(k)), big_endian_to_int(parse_as_bin(v)), ) elif "state_root" in snapshot_data: state.trie.root_hash = parse_as_bin(snapshot_data["state_root"]) else: raise Exception( "Must specify either alloc or state root parameter") for k, default in STATE_DEFAULTS.items(): default = copy.copy(default) v = snapshot_data[k] if k in snapshot_data else None if is_numeric(default): setattr(state, k, parse_as_int(v) if k in snapshot_data else default) elif is_string(default): setattr(state, k, parse_as_bin(v) if k in snapshot_data else default) elif k == "prev_headers": if k in snapshot_data: headers = [dict_to_prev_header(h) for h in v] else: headers = default setattr(state, k, headers) elif k == "recent_uncles": if k in snapshot_data: uncles = {} for height, _uncles in v.items(): uncles[int(height)] = [] for uncle in _uncles: uncles[int(height)].append(parse_as_bin(uncle)) else: uncles = default setattr(state, k, uncles) if executing_on_head: state.executing_on_head = True state.commit() state.changed = {} return state
def to_snapshot(self, no_prevblocks=False): snapshot = dict() snapshot["state_root"] = "0x" + encode_hex(self.trie.root_hash) # Save non-state-root variables for k, default in STATE_DEFAULTS.items(): default = copy.copy(default) v = getattr(self, k) if is_numeric(default): snapshot[k] = str(v) elif isinstance(default, (str, bytes)): snapshot[k] = "0x" + encode_hex(v) elif k == "prev_headers" and not no_prevblocks: snapshot[k] = [ prev_header_to_dict(h) for h in v[:self.config["PREV_HEADER_DEPTH"]] ] return snapshot
def from_snapshot(cls, snapshot_data, env): state = State(env=env) if "state_root" in snapshot_data: state.trie.root_hash = parse_as_bin(snapshot_data["state_root"]) else: raise Exception( "Must specify either alloc or state root parameter") for k, default in STATE_DEFAULTS.items(): default = copy.copy(default) v = snapshot_data[k] if k in snapshot_data else None if is_numeric(default): setattr(state, k, parse_as_int(v) if k in snapshot_data else default) elif is_string(default): setattr(state, k, parse_as_bin(v) if k in snapshot_data else default) elif k == "prev_headers": if k in snapshot_data: headers = [dict_to_prev_header(h) for h in v] else: headers = default setattr(state, k, headers) state.commit() return state
def snapshot_form(val): if is_numeric(val): return str(val) elif is_string(val): return "0x" + encode_hex(val)
def quantity_encoder(i): """Encode integer quantity `data`.""" assert is_numeric(i) return hex(i)