def apply_create_message( cls, state: StateAPI, message: MessageAPI, transaction_context: TransactionContextAPI) -> ComputationAPI: computation = cls.apply_message(state, message, transaction_context) if computation.is_error: return computation else: contract_code = computation.output if contract_code: contract_code_gas_fee = len(contract_code) * GAS_CODEDEPOSIT try: computation.consume_gas( contract_code_gas_fee, reason="Write contract code for CREATE", ) except OutOfGas: computation.output = b'' else: cls.logger.debug2( "SETTING CODE: %s -> length: %s | hash: %s", encode_hex(message.storage_address), len(contract_code), encode_hex(keccak(contract_code))) state.set_code(message.storage_address, contract_code) return computation
def apply_state_dict(state: StateAPI, state_dict: AccountState) -> None: for account, account_data in state_dict.items(): state.set_balance(account, account_data["balance"]) state.set_nonce(account, account_data["nonce"]) state.set_code(account, account_data["code"]) for slot, value in account_data["storage"].items(): state.set_storage(account, slot, value)
def setup_state(desired_state: AccountState, state: StateAPI) -> None: for account, account_data in desired_state.items(): for slot, value in account_data['storage'].items(): state.set_storage(account, slot, value) nonce = account_data['nonce'] code = account_data['code'] balance = account_data['balance'] state.set_nonce(account, nonce) state.set_code(account, code) state.set_balance(account, balance) state.persist()
def apply_create_message( cls, state: StateAPI, message: MessageAPI, transaction_context: TransactionContextAPI) -> ComputationAPI: snapshot = state.snapshot() # EIP161 nonce incrementation state.increment_nonce(message.storage_address) computation = cls.apply_message(state, message, transaction_context) if computation.is_error: state.revert(snapshot) return computation else: contract_code = computation.output if contract_code and len(contract_code) >= EIP170_CODE_SIZE_LIMIT: computation.error = OutOfGas( f"Contract code size exceeds EIP170 limit of {EIP170_CODE_SIZE_LIMIT}." f" Got code of size: {len(contract_code)}") state.revert(snapshot) elif contract_code: contract_code_gas_cost = len( contract_code) * constants.GAS_CODEDEPOSIT try: computation.consume_gas( contract_code_gas_cost, reason="Write contract code for CREATE", ) except OutOfGas as err: # Different from Frontier: reverts state on gas failure while # writing contract code. computation.error = err state.revert(snapshot) else: if cls.logger: cls.logger.debug2( "SETTING CODE: %s -> length: %s | hash: %s", encode_hex(message.storage_address), len(contract_code), encode_hex(keccak(contract_code))) state.set_code(message.storage_address, contract_code) state.commit(snapshot) else: state.commit(snapshot) return computation
def apply_create_message( cls, state: StateAPI, message: MessageAPI, transaction_context: TransactionContextAPI) -> ComputationAPI: snapshot = state.snapshot() # EIP161 nonce incrementation state.increment_nonce(message.storage_address) computation = cls.apply_message(state, message, transaction_context) if computation.is_error: state.revert(snapshot) return computation else: contract_code = computation.output if contract_code: try: cls.validate_contract_code(contract_code) contract_code_gas_cost = len( contract_code) * constants.GAS_CODEDEPOSIT computation.consume_gas( contract_code_gas_cost, reason="Write contract code for CREATE", ) except VMError as err: # Different from Frontier: reverts state on gas failure while # writing contract code. computation.error = err state.revert(snapshot) cls.logger.debug2(f"VMError setting contract code: {err}") else: if cls.logger: cls.logger.debug2( "SETTING CODE: %s -> length: %s | hash: %s", encode_hex(message.storage_address), len(contract_code), encode_hex(keccak(contract_code))) state.set_code(message.storage_address, contract_code) state.commit(snapshot) else: state.commit(snapshot) return computation