def create_record(self, record_id, record_type, properties_dict): payload = _make_tt_payload( action=TTPayload.CREATE_RECORD, create_record=CreateRecordAction( record_id=record_id, record_type=record_type, properties=[ _make_property_value(name, value) for name, value in properties_dict.items() ])) record_address = addressing.make_record_address(record_id) record_type_address = addressing.make_record_type_address(record_type) property_address_range = \ addressing.make_property_address_range(record_id) inputs = [ record_address, record_type_address, property_address_range, self.signer_address, ] return self._create_transaction(payload, inputs=inputs, outputs=[ record_address, property_address_range, ])
def answer_proposal(self, record_id, receiving_agent, role, response): payload = _make_tt_payload(action=TTPayload.ANSWER_PROPOSAL, answer_proposal=AnswerProposalAction( record_id=record_id, receiving_agent=receiving_agent, role=role, response=response)) proposal_address = addressing.make_proposal_address( record_id, receiving_agent) record_address = addressing.make_record_address(record_id) property_address_range = addressing.make_property_address_range( record_id) return self._create_transaction( payload, inputs=[ proposal_address, record_address, property_address_range, addressing.RECORD_TYPE_ADDRESS_RANGE, ], outputs=[ proposal_address, record_address, property_address_range, ], )
def revoke_reporter(self, record_id, reporter_id, properties): payload = _make_tt_payload(action=TTPayload.REVOKE_REPORTER, revoke_reporter=RevokeReporterAction( record_id=record_id, reporter_id=reporter_id, properties=properties)) record_address = addressing.make_record_address(record_id) proposal_address = addressing.make_proposal_address( record_id, reporter_id) property_addresses = [ addressing.make_property_address(record_id, property_name) for property_name in properties ] return self._create_transaction( payload, inputs=[ record_address, proposal_address, *property_addresses, ], outputs=[ proposal_address, *property_addresses, ], )
def create_proposal(self, record_id, receiving_agent, role, properties=None): if properties is None: properties = [] payload = _make_tt_payload(action=TTPayload.CREATE_PROPOSAL, create_proposal=CreateProposalAction( record_id=record_id, receiving_agent=receiving_agent, role=role, properties=properties)) proposal_address = addressing.make_proposal_address( record_id, receiving_agent) receiving_address = addressing.make_agent_address(receiving_agent) record_address = addressing.make_record_address(record_id) return self._create_transaction( payload, inputs=[ proposal_address, record_address, receiving_address, self.signer_address, ], outputs=[proposal_address], )
def finalize_record(self, record_id): payload = _make_tt_payload( action=TTPayload.FINALIZE_RECORD, finalize_record=FinalizeRecordAction(record_id=record_id)) record_address = addressing.make_record_address(record_id) return self._create_transaction(payload, [record_address], [record_address])
def _get_record(state, record_id): ''' Return record, record_container, record_address ''' record_address = addressing.make_record_address(record_id) record_container = _get_container(state, record_address) try: record = next(record for record in record_container.entries if record.record_id == record_id) except StopIteration: raise InvalidTransaction('Record does not exist') return record, record_container, record_address
def update_properties(self, record_id, properties_dict): payload = _make_tt_payload( action=TTPayload.UPDATE_PROPERTIES, update_properties=UpdatePropertiesAction( record_id=record_id, properties=[ _make_property_value(name, value) for name, value in properties_dict.items() ])) record_address = addressing.make_record_address(record_id) property_address_range = \ addressing.make_property_address_range(record_id) inputs = [ record_address, property_address_range, ] return self._create_transaction(payload, inputs=inputs, outputs=[property_address_range])
def _create_record(payload, signer, timestamp, state): ''' * Check that the signer is registered. * Check that the record doesn't already exist. * Check that the record type exists. * Check that the required properties have been provided. * Create the record (and record container if needed). * Create the associated properties. * Post the provided property values. ''' # Check that the signer is registered. _verify_agent(state, signer) # Check that the record doesn't already exist record_id = payload.record_id if not record_id: raise InvalidTransaction('Record id cannot be empty string') record_address = addressing.make_record_address(record_id) record_container = _get_container(state, record_address) if any(rec.record_id == record_id for rec in record_container.entries): raise InvalidTransaction('Record {} already exists'.format(record_id)) # Check that the record type exists. type_name = payload.record_type record_type, _, _ = _get_record_type(state, type_name) type_schemata = {prop.name: prop for prop in record_type.properties} required_properties = { name: prop for name, prop in type_schemata.items() if prop.required } provided_properties = {prop.name: prop for prop in payload.properties} # Make sure the required properties are all provided for name in required_properties: if name not in provided_properties: raise InvalidTransaction( 'Required property {} not provided'.format(name)) # Make sure the provided properties have the right type for provided_name in provided_properties: required_type = type_schemata[provided_name].data_type provided_type = provided_properties[provided_name].data_type if required_type != provided_type: raise InvalidTransaction( 'Value provided for {} is the wrong type'.format( provided_name)) # Create the record record = Record( record_id=record_id, record_type=type_name, final=False, ) record.owners.extend( [Record.AssociatedAgent( agent_id=signer, timestamp=timestamp, )]) record.custodians.extend( [Record.AssociatedAgent( agent_id=signer, timestamp=timestamp, )]) record_container.entries.extend([record]) record_container.entries.sort(key=lambda rec: rec.record_id) _set_container(state, record_address, record_container) # Create the associated properties for property_name, prop in type_schemata.items(): _make_new_property( state=state, record_id=record_id, property_name=property_name, data_type=prop.data_type, signer=signer, ) _make_new_property_page( state=state, timestamp=timestamp, record_id=record_id, property_name=property_name, value=(None if property_name not in provided_properties else provided_properties[property_name]), page_number=1, )