Esempio n. 1
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
Esempio n. 2
0
MOCK_ADDRESS = constants.ZERO_ADDRESS
DEFAULT_INITIAL_BALANCE = to_wei(10000, 'ether')

GENESIS_PARAMS = {
    'parent_hash': constants.GENESIS_PARENT_HASH,
    'uncles_hash': constants.EMPTY_UNCLE_HASH,
    'coinbase': constants.ZERO_ADDRESS,
    'transaction_root': constants.BLANK_ROOT_HASH,
    'receipt_root': constants.BLANK_ROOT_HASH,
    'difficulty': constants.GENESIS_DIFFICULTY,
    'block_number': constants.GENESIS_BLOCK_NUMBER,
    'gas_limit': constants.GENESIS_GAS_LIMIT,
    'extra_data': constants.GENESIS_EXTRA_DATA,
    'nonce': constants.GENESIS_NONCE
}

GENESIS_STATE = {
    MOCK_ADDRESS: {
        "balance": DEFAULT_INITIAL_BALANCE,
        "nonce": 0,
        "code": b'',
        "storage": {}
    }
}

chain = MainnetChain.from_genesis(AtomicDB(), GENESIS_PARAMS, GENESIS_STATE)

mock_address_balance = chain.get_vm().state.get_balance(MOCK_ADDRESS)

print("The balance of address {} is {} wei".format(encode_hex(MOCK_ADDRESS),
                                                   mock_address_balance))