def set_record(self, public_key, latitude, longitude, record_id, timestamp): """Creates a new record in state Args: public_key (str): The public key of the agent creating the record latitude (int): Initial latitude of the record longitude (int): Initial latitude of the record record_id (str): Unique ID of the record timestamp (int): Unix UTC timestamp of when the agent was created """ address = addresser.get_record_address(record_id) owner = record_pb2.Record.Owner(agent_id=public_key, timestamp=timestamp) location = record_pb2.Record.Location(latitude=latitude, longitude=longitude, timestamp=timestamp) record = record_pb2.Record(record_id=record_id, owners=[owner], locations=[location]) container = record_pb2.RecordContainer() state_entries = self._context.get_state(addresses=[address], timeout=self._timeout) if state_entries: container.ParseFromString(state_entries[0].data) container.entries.extend([record]) data = container.SerializeToString() updated_state = {} updated_state[address] = data self._context.set_state(updated_state, timeout=self._timeout)
def make_transfer_record_transaction(transaction_signer, batch_signer, receiving_agent, record_id, timestamp): sending_agent_address = addresser.get_agent_address( transaction_signer.get_public_key().as_hex()) receiving_agent_address = addresser.get_agent_address(receiving_agent) record_address = addresser.get_record_address(record_id) inputs = [sending_agent_address, receiving_agent_address, record_address] outputs = [record_address] action = payload_pb2.TransferRecordAction( record_id=record_id, receiving_agent=receiving_agent) payload = payload_pb2.ScPayload( action=payload_pb2.ScPayload.TRANSFER_RECORD, transfer_record=action, timestamp=timestamp) payload_bytes = payload.SerializeToString() return _make_batch( payload_bytes=payload_bytes, inputs=inputs, outputs=outputs, transaction_signer=transaction_signer, batch_signer=batch_signer)
def make_finalize_record_transaction(transaction_signer, batch_signer, record_id, status, remarks, timestamp): agent_address = addresser.get_agent_address( transaction_signer.get_public_key().as_hex()) record_address = addresser.get_record_address(record_id) inputs = [agent_address, record_address] outputs = [record_address] action = payload_pb2.FinalizeRecordAction( record_id=record_id, status=status, remarks=remarks) payload = payload_pb2.ScPayload( action=payload_pb2.ScPayload.FINALIZE_RECORD, finalize_record=action, timestamp=timestamp) payload_bytes = payload.SerializeToString() return _make_batch( payload_bytes=payload_bytes, inputs=inputs, outputs=outputs, transaction_signer=transaction_signer, batch_signer=batch_signer)
def make_create_record_transaction(transaction_signer, batch_signer, record_id, description, price, quantity, units, parent_id, other_fields, remarks, new, timestamp): inputs = [ addresser.get_agent_address( transaction_signer.get_public_key().as_hex()), addresser.get_record_address(record_id) ] outputs = [addresser.get_record_address(record_id)] action = payload_pb2.CreateRecordAction( record_id=record_id, description=description, price=price, quantity=quantity, units=units, parent_id=parent_id, other_fields=other_fields, remarks=remarks, new=new ) payload = payload_pb2.ScPayload( action=payload_pb2.ScPayload.CREATE_RECORD, create_record=action, timestamp=timestamp) payload_bytes = payload.SerializeToString() return _make_batch( payload_bytes=payload_bytes, inputs=inputs, outputs=outputs, transaction_signer=transaction_signer, batch_signer=batch_signer)
def make_create_record_transaction(transaction_signer, batch_signer, latitude, longitude, record_id, timestamp): """Make a CreateRecordAction transaction and wrap it in a batch Args: transaction_signer (sawtooth_signing.Signer): The transaction key pair batch_signer (sawtooth_signing.Signer): The batch key pair latitude (int): Initial latitude of the record longitude (int): Initial latitude of the record record_id (str): Unique ID of the record timestamp (int): Unix UTC timestamp of when the agent is created Returns: batch_pb2.Batch: The transaction wrapped in a batch """ inputs = [ addresser.get_agent_address( transaction_signer.get_public_key().as_hex()), addresser.get_record_address(record_id) ] outputs = [addresser.get_record_address(record_id)] action = payload_pb2.CreateRecordAction(record_id=record_id, latitude=latitude, longitude=longitude) payload = payload_pb2.SimpleSupplyPayload( action=payload_pb2.SimpleSupplyPayload.CREATE_RECORD, create_record=action, timestamp=timestamp) payload_bytes = payload.SerializeToString() return _make_batch(payload_bytes=payload_bytes, inputs=inputs, outputs=outputs, transaction_signer=transaction_signer, batch_signer=batch_signer)
def transfer_record(self, receiving_agent, record_id, timestamp): owner = record_pb2.Record.Owner(agent_id=receiving_agent, timestamp=timestamp) address = addresser.get_record_address(record_id) container = record_pb2.RecordContainer() state_entries = self._context.get_state(addresses=[address], timeout=self._timeout) if state_entries: container.ParseFromString(state_entries[0].data) for record in container.entries: if record.record_id == record_id: record.owners.extend([owner]) data = container.SerializeToString() updated_state = {} updated_state[address] = data self._context.set_state(updated_state, timeout=self._timeout)
def update_record(self, latitude, longitude, record_id, timestamp): location = record_pb2.Record.Location(latitude=latitude, longitude=longitude, timestamp=timestamp) address = addresser.get_record_address(record_id) container = record_pb2.RecordContainer() state_entries = self._context.get_state(addresses=[address], timeout=self._timeout) if state_entries: container.ParseFromString(state_entries[0].data) for record in container.entries: if record.record_id == record_id: record.locations.extend([location]) data = container.SerializeToString() updated_state = {} updated_state[address] = data self._context.set_state(updated_state, timeout=self._timeout)
def get_record(self, record_id): """Gets the record associated with the record_id Args: record_id (str): The id of the record Returns: record_pb2.Record: Record with the provided record_id """ address = addresser.get_record_address(record_id) state_entries = self._context.get_state(addresses=[address], timeout=self._timeout) if state_entries: container = record_pb2.RecordContainer() container.ParseFromString(state_entries[0].data) for record in container.entries: if record.record_id == record_id: return record return None
def make_update_record_transaction(transaction_signer, batch_signer, record_id, description, price, quantity, units, parent_id, other_fields, timestamp): agent_address = addresser.get_agent_address( transaction_signer.get_public_key().as_hex()) record_address = addresser.get_record_address(record_id) inputs = [agent_address, record_address] outputs = [record_address] action = payload_pb2.UpdateRecordAction( record_id=record_id, description=description, price=price, quantity=quantity, units=units, parent_id=parent_id, other_fields=other_fields) payload = payload_pb2.ScPayload( action=payload_pb2.ScPayload.UPDATE_RECORD, update_record=action, timestamp=timestamp) payload_bytes = payload.SerializeToString() return _make_batch( payload_bytes=payload_bytes, inputs=inputs, outputs=outputs, transaction_signer=transaction_signer, batch_signer=batch_signer)
def make_transfer_record_transaction(transaction_signer, batch_signer, receiving_agent, record_id, timestamp): """Make a CreateRecordAction transaction and wrap it in a batch Args: transaction_signer (sawtooth_signing.Signer): The transaction key pair batch_signer (sawtooth_signing.Signer): The batch key pair receiving_agent (str): Public key of the agent receiving the record record_id (str): Unique ID of the record timestamp (int): Unix UTC timestamp of when the record is transferred Returns: batch_pb2.Batch: The transaction wrapped in a batch """ sending_agent_address = addresser.get_agent_address( transaction_signer.get_public_key().as_hex()) receiving_agent_address = addresser.get_agent_address(receiving_agent) record_address = addresser.get_record_address(record_id) inputs = [sending_agent_address, receiving_agent_address, record_address] outputs = [record_address] action = payload_pb2.TransferRecordAction(record_id=record_id, receiving_agent=receiving_agent) payload = payload_pb2.SimpleSupplyPayload( action=payload_pb2.SimpleSupplyPayload.TRANSFER_RECORD, transfer_record=action, timestamp=timestamp) payload_bytes = payload.SerializeToString() return _make_batch(payload_bytes=payload_bytes, inputs=inputs, outputs=outputs, transaction_signer=transaction_signer, batch_signer=batch_signer)