def make_create_item_transaction(transaction_signer, batch_signer, item_id, latitude, longitude, timestamp): fprint('=== makde create item transaction ===') agent_address = addresser.get_agent_address( transaction_signer.get_public_key().as_hex()) fprint(f'=== itemid: {item_id}') item_address = addresser.get_item_address(item_id) inputs = [agent_address, item_address] outputs = [item_address] action = payload_pb2.CreateItemAction(item_id=item_id, latitude=latitude, longitude=longitude) payload = payload_pb2.SupplyPayload( action=payload_pb2.SupplyPayload.CREATE_ITEM, create_item=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 set_item(self, public_key, item_id, latitude, longitude, timestamp): address = addresser.get_item_address(item_id) fprint(address) owner = item_pb2.Item.Owner( public_key=public_key, timestamp=timestamp ) location = item_pb2.Item.Location( latitude=latitude, longitude=longitude, timestamp=timestamp ) item = item_pb2.Item( item_id=item_id, locations=[location], owners=[owner] ) container = item_pb2.ItemContainer() state_entries = self._context.get_state(addresses=[address], timeout=self._timeout) fprint('==== state_entries: {}'.format(state_entries)) if state_entries: container.ParseFromString(state_entries[0].data) container.entries.extend([item]) data = container.SerializeToString() fprint('data: {}'.format(data)) updated_state = {} updated_state[address] = data self._context.set_state(updated_state, timeout=self._timeout)
def tranfer_item(self, received_agent, item_id, timestamp): fprint(f'{received_agent}, {item_id}, {timestamp}') fprint('start share item in state 1') item_address = addresser.get_item_address(item_id) received_agent_address = addresser.get_agent_address(received_agent) shared_owner = item_pb2.Item.Owner( public_key=received_agent_address, timestamp=timestamp ) container = item_pb2.ItemContainer() state_entries = self._context.get_state(addresses=[item_address], timeout=self._timeout) fprint('start update document in state 2') if state_entries: container.ParseFromString(state_entries[0].data) for item in container.entries: if item.item_id == item_id: item.owners.extend([shared_owner]) fprint('start update document in state 3') data = container.SerializeToString() updated_state = {} updated_state[item_address] = data self._context.set_state(updated_state, timeout=self._timeout) fprint('start update document in state 4') pass
def update_item(self, item_id, latitude, longitude, timestamp): location = item_pb2.Item.Location( latitude=latitude, longitude=longitude, timestamp=timestamp ) fprint('start update item in state 1') address = addresser.get_item_address(item_id) container = item_pb2.ItemContainer() state_entries = self._context.get_state(addresses=[address], timeout=self._timeout) fprint('start update item in state 2') if state_entries: container.ParseFromString(state_entries[0].data) for item in container.entries: fprint('=== item.item_id ===') fprint(item.item_id) fprint(item_id) if item.item_id == item_id: item.locations.extend([location]) fprint('start update item in state 3') data = container.SerializeToString() updated_state = {} updated_state[address] = data self._context.set_state(updated_state, timeout=self._timeout) fprint('start update item in state 4')
def get_item(self, item_id): address = addresser.get_item_address(item_id) try: state_entries = self._context.get_state( addresses=[address], timeout=self._timeout) if state_entries: container = item_pb2.ItemContainer() container.ParseFromString(state_entries[0].data) for item in container.entries: if item.item_id == item_id: return item except: return None
def make_tranfer_item_transaction(transaction_signer, batch_signer, item_id, received_agent, timestamp): agent_address = addresser.get_agent_address( transaction_signer.get_public_key().as_hex()) received_agent_address = addresser.get_agent_address(received_agent) item_address = addresser.get_item_address(item_id) inputs = [agent_address, item_address, received_agent_address] outputs = [item_address] action = payload_pb2.TranferItemAction( item_id=item_id, received_agent=received_agent_address) payload = payload_pb2.SupplyPayload( action=payload_pb2.SupplyPayload.TRANFER_ITEM, tranfer_item=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_update_item_transaction(transaction_signer, batch_signer, item_id, latitude, longitude, timestamp): agent_address = addresser.get_agent_address( transaction_signer.get_public_key().as_hex()) item_address = addresser.get_item_address(item_id) inputs = [agent_address, item_address] outputs = [item_address] action = payload_pb2.UpdateItemAction(item_id=item_id, latitude=latitude, longitude=longitude) payload = payload_pb2.SupplyPayload( action=payload_pb2.SupplyPayload.UPDATE_ITEM, update_item=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)