Пример #1
0
    def from_genesis(cls,
                     base_db: AtomicDatabaseAPI,
                     genesis_params: Dict[str, HeaderParams],
                     genesis_state: AccountState=None) -> 'BaseChain':
        genesis_vm_class = cls.get_vm_class_for_block_number(BlockNumber(0))

        pre_genesis_header = BlockHeader(difficulty=0, block_number=-1, gas_limit=0)
        chain_context = ChainContext(cls.chain_id)
        state = genesis_vm_class.build_state(base_db, pre_genesis_header, chain_context)

        if genesis_state is None:
            genesis_state = {}

        # mutation
        apply_state_dict(state, genesis_state)
        state.persist()

        if 'state_root' not in genesis_params:
            # If the genesis state_root was not specified, use the value
            # computed from the initialized state database.
            genesis_params = assoc(genesis_params, 'state_root', state.state_root)
        elif genesis_params['state_root'] != state.state_root:
            # If the genesis state_root was specified, validate that it matches
            # the computed state from the initialized state database.
            raise ValidationError(
                "The provided genesis state root does not match the computed "
                f"genesis state root.  Got {state.state_root!r}.  "
                f"Expected {genesis_params['state_root']!r}"
            )

        genesis_header = BlockHeader(**genesis_params)
        return cls.from_genesis_header(base_db, genesis_header)
Пример #2
0
def initialize_vm_and_state(state_test):
    account_state = decode_account_state(state_test["pre"])
    # print(account_state)

    base_db = AtomicDB()
    chain = MainnetChain(base_db)

    pre_genesis_header = BlockHeader(difficulty=0,
                                     block_number=-1,
                                     gas_limit=0)
    chain_context = ChainContext(MAINNET_CHAIN_ID)
    state = IstanbulVM.build_state(base_db, pre_genesis_header, chain_context)

    # apply custom state
    apply_state_dict(state, account_state)
    state.persist()

    # print("initial state", encode_hex(state.make_state_root()))

    current_block_params = decode_current_block_params(state_test["env"])
    current_block_header = BlockHeader(**current_block_params)

    # vm = chain.get_vm()
    vm = IstanbulVM(
        header=pre_genesis_header,
        chaindb=chain.chaindb,
        chain_context=chain_context,
        consensus_context=chain.consensus_context,
    )

    return vm, state, current_block_header
Пример #3
0
    def from_genesis(cls,
                     base_db: BaseAtomicDB,
                     genesis_params: Dict[str, HeaderParams],
                     genesis_state: AccountState = None) -> 'BaseChain':
        """
        Initializes the Chain from a genesis state.
        """
        genesis_vm_class = cls.get_vm_class_for_block_number(BlockNumber(0))

        account_db = genesis_vm_class.get_state_class().get_account_db_class()(
            base_db,
            BLANK_ROOT_HASH,
        )

        if genesis_state is None:
            genesis_state = {}

        # mutation
        apply_state_dict(account_db, genesis_state)
        account_db.persist()

        if 'state_root' not in genesis_params:
            # If the genesis state_root was not specified, use the value
            # computed from the initialized state database.
            genesis_params = assoc(genesis_params, 'state_root',
                                   account_db.state_root)
        elif genesis_params['state_root'] != account_db.state_root:
            # If the genesis state_root was specified, validate that it matches
            # the computed state from the initialized state database.
            raise ValidationError(
                "The provided genesis state root does not match the computed "
                "genesis state root.  Got {0}.  Expected {1}".format(
                    account_db.state_root,
                    genesis_params['state_root'],
                ))

        genesis_header = BlockHeader(**genesis_params)
        return cls.from_genesis_header(base_db, genesis_header)
Пример #4
0
def calc_state_root(state_dict: AccountState,
                    state_class: Type[BaseState]) -> bytes:
    state = state_class(AtomicDB(), None, BLANK_ROOT_HASH)
    apply_state_dict(state, state_dict)
    return state.state_root
Пример #5
0
def calc_state_root(state: AccountState,
                    account_db_class: Type[BaseAccountDB]) -> bytes:
    account_db = account_db_class(MemoryDB())
    apply_state_dict(account_db, state)
    return account_db.state_root