def get_com(self, com_id): company_address = addresser.get_company_address(com_id) state_entries = self._context.get_state(addresses=[company_address], timeout=self._timeout) if state_entries: container = company_pb2.CompanyContainer() container.ParseFromString(state_entries[0].data) for company in container.entries: if company.id == com_id: return company return None
def create_com(self, com_id, name, timestamp): company_address = addresser.get_company_address(com_id) company = company_pb2.Company(id=com_id, name=name) container = company_pb2.CompanyContainer() state_entries = self._context.get_state(addresses=[company_address], timeout=self._timeout) if state_entries: container.ParseFromString(state_entries[0].data) container.entries.extend([company]) data = container.SerializeToString() updated_state = {company_address: data} self._context.set_state(updated_state, timeout=self._timeout)
def update_com(self, com_id, establish, address, timestamp): company_address = addresser.get_company_address(com_id) update = company_pb2.Company.Update(timestamp=timestamp, establish=establish, address=address) container = company_pb2.CompanyContainer() state_entries = self._context.get_state(addresses=[company_address], timeout=self._timeout) if state_entries: container.ParseFromString(state_entries[0].data) for company in container.entries: if company.id == com_id: company.updates.extend([update]) data = container.SerializeToString() updated_state = {company_address: data} self._context.set_state(updated_state, timeout=self._timeout)
def make_create_com_transaction(transaction_signer, batch_signer, com_id, name, timestamp): user_address = addresser.get_user_address( transaction_signer.get_public_key().as_hex()) com_address = addresser.get_company_address(com_id) inputs = [user_address, com_address] outputs = [com_address] action = payload_pb2.CreateCom(id=com_id, name=name) payload = payload_pb2.SimpleSupplyPayload( action=payload_pb2.SimpleSupplyPayload.CREATE_COM, create_com=action, timestamp=timestamp) LOGGER.info(payload) payload_bytes = payload.SerializeToString() return _make_batch(payload_bytes=payload_bytes, inputs=inputs, outputs=outputs, transaction_signer=transaction_signer, batch_signer=batch_signer)