Exemple #1
0
    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
Exemple #2
0
    def import_contract(cls, config, contract_file, contract_name):
        """create a new contract pdo object and save it
        """

        try:
            contract_info = json.load(contract_file)
        except:
            logger.warn('failed to retrieve contract information')
            return None

        try:
            code_info = contract_info['contract_code']
            code = pdo_contract_code(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())
        except Exception as e:
            logger.error('error occurred retreiving contract code; %s', str(e))
            raise Exception("invalid contract file; {}".format(contract_name))

        ## need to handle the case where the contract has been registered
        ## but the initial state has not been committed

        ledger_config = config.get('Sawtooth', {})
        try:
            contract_id = contract_info['contract_id']
            current_state_hash = pdo_contract_state.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(contract_name))

        try:
            path_config = config.get('ContentPaths')
            state_root = os.path.realpath(
                path_config.get('State',
                                os.path.join(os.environ['HOME'], '.toxaway')))

            state = pdo_contract_state.read_from_cache(contract_id,
                                                       current_state_hash,
                                                       data_dir=state_root)
            if state is None:
                state = pdo_contract_state.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(contract_name))

        extra_data = contract_info.get('extra_data', {})
        extra_data['name'] = contract_name
        extra_data['update-enclave'] = 'random'
        extra_data['invoke-enclave'] = 'random'

        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.save(config)
        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