def read_from_file(cls, ledger_config, basename, data_dir='./data'): filename = putils.build_file_name(basename, data_dir, '.pdo') logger.debug('load contract information from %s', filename) if os.path.exists(filename) is not True: raise FileNotFoundError(errno.ENOENT, "contract data file does not exist", filename) try: with open(filename, "r") as contract_file: contract_info = json.load(contract_file) except Exception as e: logger.warn('load contract information file failed; %s', str(e)) raise Exception("invalid contract file; {}".format(filename)) try: code_info = contract_info['contract_code'] code = ContractCode(code_info['Code'], code_info['Name'], code_info['Nonce']) except KeyError as ke: logger.error('invalid contract data file; missing %s', str(ke)) raise Exception("invalid contract file; {}".format(filename)) except Exception as e: logger.error('error occurred retreiving contract code; %s', str(e)) raise Exception("invalid contract file; {}".format(filename)) ## need to handle the case where the contract has been registered ## but the initial state has not been committed try: contract_id = contract_info['contract_id'] current_state_hash = ContractState.get_current_state_hash( ledger_config, contract_id) except Exception as e: logger.error('error occurred retreiving contract state hash; %s', str(e)) raise Exception('invalid contract file; {}'.format(filename)) try: state = ContractState.read_from_cache(contract_id, current_state_hash, data_dir=data_dir) if state is None: state = ContractState.get_from_ledger(ledger_config, contract_id, current_state_hash) state.save_to_cache(data_dir=data_dir) except Exception as e: logger.error('error occurred retreiving contract state; %s', str(e)) raise Exception("invalid contract file; {}".format(filename)) obj = cls(code, state, contract_info['contract_id'], contract_info['creator_id']) for enclave in contract_info['enclaves_info']: obj.set_state_encryption_key( enclave['contract_enclave_id'], enclave['encrypted_contract_state_encryption_key']) return obj
def read_from_file(cls, ledger_config, basename, data_dir=None): filename = putils.build_file_name(basename, data_dir, cls.__path__, cls.__extension__) logger.debug('load contract information from %s', filename) if os.path.exists(filename) is not True: raise FileNotFoundError(errno.ENOENT, "contract data file does not exist", filename) try: with open(filename, "r") as contract_file: contract_info = json.load(contract_file) except Exception as e: logger.warn('load contract information file failed; %s', str(e)) raise Exception("invalid contract file; {}".format(filename)) try: code_info = contract_info['contract_code'] comp_report = None if not code_info['CompilationReport'].get( 'CompilerVerifyingKey') is None: comp_report = ContractCompilationReport.init_from_dict( code_info['CompilationReport']) code = ContractCode(code_info['Code'], code_info['Name'], code_info['Nonce'], compilation_report=comp_report) except KeyError as ke: logger.error('invalid contract data file; missing %s', str(ke)) raise Exception("invalid contract file; {}".format(filename)) except Exception as e: logger.error('error occurred retreiving contract code; %s', str(e)) raise Exception("invalid contract file; {}".format(filename)) ## need to handle the case where the contract has been registered ## but the initial state has not been committed try: contract_id = contract_info['contract_id'] current_state_hash = ContractState.get_current_state_hash( ledger_config, contract_id) except Exception as e: logger.error('error occurred retreiving contract state hash; %s', str(e)) raise Exception('invalid contract file; {}'.format(filename)) try: state = ContractState.read_from_cache(contract_id, current_state_hash, data_dir=data_dir) if state is None: state = ContractState.get_from_ledger(ledger_config, contract_id, current_state_hash) state.save_to_cache(data_dir=data_dir) except Exception as e: logger.error('error occurred retreiving contract state; %s', str(e)) raise Exception("invalid contract file; {}".format(filename)) extra_data = contract_info.get('extra_data', {}) obj = cls(code, state, contract_info['contract_id'], contract_info['creator_id'], extra_data=extra_data) for enclave in contract_info['enclaves_info']: obj.set_state_encryption_key( enclave['contract_enclave_id'], enclave['encrypted_contract_state_encryption_key']) obj.set_replication_parameters(contract_info['num_provable_replicas'], contract_info['availability_duration']) return obj