Example #1
0
    def set_record(self, public_key, seq, ts, 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(seq=seq,
                                              ts=ts,
                                              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)
Example #2
0
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)
Example #3
0
 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)
Example #4
0
 def update_record(self, device, seq, ts, record_id, timestamp):
     location = record_pb2.Record.Location(seq=seq,
                                           ts=ts,
                                           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 make_update_record_transaction(transaction_signer, batch_signer, seq, ts,
                                   device, ddata, dsize, dhash, 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): Updated latitude of the location
        longitude (int): Updated longitude of the location
        record_id (str): Unique ID of the record
        timestamp (int): Unix UTC timestamp of when the record is updated

    Returns:
        batch_pb2.Batch: The transaction wrapped in a batch
    """
    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,
                                            seq=seq,
                                            ts=ts,
                                            device=device,
                                            ddata=ddata,
                                            dsize=dsize,
                                            dhash=dhash,
                                            timestamp=timestamp)

    payload = payload_pb2.SimpleSupplyPayload(
        action=payload_pb2.SimpleSupplyPayload.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)
Example #6
0
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)
Example #7
0
    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