def init_state(env, pre, is_qkc_state, qkc_env=None):
    # Setup env
    db = InMemoryDb()
    state_env = Env(config=konfig)
    state_env.db = db
    state = State(
        env=state_env,
        block_prevhash=decode_hex(remove_0x_head(env["previousHash"])),
        prev_headers=[
            mk_fake_header(i)
            for i in range(
                parse_int_or_hex(env["currentNumber"]) - 1,
                max(-1, parse_int_or_hex(env["currentNumber"]) - 257),
                -1,
            )
        ],
        block_number=parse_int_or_hex(env["currentNumber"]),
        block_coinbase=decode_hex(remove_0x_head(env["currentCoinbase"])),
        block_difficulty=parse_int_or_hex(env["currentDifficulty"]),
        gas_limit=parse_int_or_hex(env["currentGasLimit"]),
        timestamp=parse_int_or_hex(env["currentTimestamp"]),
        qkc_config=qkc_env.quark_chain_config,
        # If testing QuarkChain states, should not use mock account
        use_mock_evm_account=not is_qkc_state,
    )

    seen_token_ids = set()
    # Fill up pre
    for address, h in list(pre.items()):
        assert len(address) in (40, 42)
        address = decode_hex(remove_0x_head(address))
        state.set_nonce(address, parse_int_or_hex(h["nonce"]))
        if is_qkc_state and "balances" in h:
            # In QuarkChain state tests, can either specify balance map or single balance
            for token_id, balance in h["balances"].items():
                parsed_token_id = parse_int_or_hex(token_id)
                state.set_token_balance(
                    address, parsed_token_id, parse_int_or_hex(balance)
                )
                seen_token_ids.add(parsed_token_id)
        else:
            state.set_balance(address, parse_int_or_hex(h["balance"]))
        state.set_code(address, decode_hex(remove_0x_head(h["code"])))
        for k, v in h["storage"].items():
            state.set_storage_data(
                address,
                big_endian_to_int(decode_hex(k[2:])),
                big_endian_to_int(decode_hex(v[2:])),
            )

    # Update allowed token IDs
    if seen_token_ids:
        state.qkc_config._allowed_token_ids = seen_token_ids

    state.commit(allow_empties=True)
    return state
Example #2
0
 def __init__(
     self,
     root=BLANK_ROOT,
     env=Env(),
     qkc_config=None,
     executing_on_head=False,
     db=None,
     use_mock_evm_account=False,
     **kwargs
 ):
     if db is None:
         db = env.db
     self.env = env
     self.__db = db
     self.trie = SecureTrie(Trie(self.db, root))
     for k, v in STATE_DEFAULTS.items():
         setattr(self, k, kwargs.get(k, copy.copy(v)))
     self.journal = []
     self.cache = {}
     self.log_listeners = []
     self.deletes = []
     self.changed = {}
     self.executing_on_head = executing_on_head
     self.qkc_config = qkc_config
     self.sender_disallow_map = dict()  # type: Dict[bytes, int]
     self.shard_config = ShardConfig(ChainConfig())
     self.use_mock_evm_account = use_mock_evm_account
def init_state(env, pre):
    # Setup env
    db = InMemoryDb()
    stateEnv = Env(config=konfig)
    stateEnv.db = db
    state = State(
        env=stateEnv,
        block_prevhash=decode_hex(remove_0x_head(env["previousHash"])),
        prev_headers=[
            mk_fake_header(i) for i in range(
                parse_int_or_hex(env["currentNumber"]) - 1,
                max(-1,
                    parse_int_or_hex(env["currentNumber"]) - 257),
                -1,
            )
        ],
        block_number=parse_int_or_hex(env["currentNumber"]),
        block_coinbase=decode_hex(remove_0x_head(env["currentCoinbase"])),
        block_difficulty=parse_int_or_hex(env["currentDifficulty"]),
        gas_limit=parse_int_or_hex(env["currentGasLimit"]),
        timestamp=parse_int_or_hex(env["currentTimestamp"]),
    )

    # Fill up pre
    for address, h in list(pre.items()):
        assert len(address) in (40, 42)
        address = decode_hex(remove_0x_head(address))
        assert set(h.keys()) == set(["code", "nonce", "balance", "storage"])
        state.set_nonce(address, parse_int_or_hex(h["nonce"]))
        state.set_balance(address, parse_int_or_hex(h["balance"]))
        state.set_code(address, decode_hex(remove_0x_head(h["code"])))
        for k, v in h["storage"].items():
            state.set_storage_data(
                address,
                big_endian_to_int(decode_hex(k[2:])),
                big_endian_to_int(decode_hex(v[2:])),
            )

    state.commit(allow_empties=True)
    # state.commit()
    return state
Example #4
0
 def ephemeral_clone(self):
     snapshot = self.to_snapshot(root_only=True, no_prevblocks=True)
     env2 = Env(OverlayDb(self.db), self.env.config)
     s = State.from_snapshot(snapshot, env2)
     for param in STATE_DEFAULTS:
         setattr(s, param, getattr(self, param))
     s.recent_uncles = self.recent_uncles
     s.prev_headers = self.prev_headers
     for acct in self.cache.values():
         assert not acct.touched or not acct.deleted
     s.journal = copy.copy(self.journal)
     s.cache = {}
     return s
Example #5
0
 def ephemeral_clone(self):
     snapshot = self.to_snapshot(no_prevblocks=True)
     env2 = Env(OverlayDb(self.db), self.env.config)
     s = State.from_snapshot(snapshot, env2)
     for param in STATE_DEFAULTS:
         setattr(s, param, getattr(self, param))
     s.prev_headers = self.prev_headers
     for acct in self.cache.values():
         assert not acct.touched or not acct.deleted
     s.journal = copy.copy(self.journal)
     s.cache = {}
     s.qkc_config = self.qkc_config
     s.sender_disallow_map = self.sender_disallow_map
     return s
Example #6
0
 def __init__(
     self, root=BLANK_ROOT, env=Env(), executing_on_head=False, db=None, **kwargs
 ):
     if db is None:
         db = env.db
     self.env = env
     self.__db = db
     self.trie = SecureTrie(Trie(self.db, root))
     for k, v in STATE_DEFAULTS.items():
         setattr(self, k, kwargs.get(k, copy.copy(v)))
     self.journal = []
     self.cache = {}
     self.log_listeners = []
     self.deletes = []
     self.changed = {}
     self.executing_on_head = executing_on_head