def create_role(new_role, state): role_container = role_state_pb2.RoleAttributesContainer() role = role_container.role_attributes.add() role.role_id = new_role.role_id role.name = new_role.name role.metadata = new_role.metadata entries_to_set = { addresser.make_role_attributes_address(new_role.role_id): role_container.SerializeToString() } pubkeys_by_address = {} for admin in list(new_role.admins): admin_address = addresser.make_role_admins_address( role_id=new_role.role_id, user_id=admin) if admin_address in pubkeys_by_address: pubkeys_by_address[admin_address].append(admin) else: pubkeys_by_address[admin_address] = [admin] for owner in list(new_role.owners): owner_address = addresser.make_role_owners_address( role_id=new_role.role_id, user_id=owner) if owner_address in pubkeys_by_address: pubkeys_by_address[owner_address].append(owner) else: pubkeys_by_address[owner_address] = [owner] state_returns = state_accessor.get_state( state, [ addresser.make_role_admins_address(role_id=new_role.role_id, user_id=a) for a in new_role.admins ] + [ addresser.make_role_owners_address(role_id=new_role.role_id, user_id=o) for o in new_role.owners ], ) for addr, pubkeys in pubkeys_by_address.items(): try: state_entry = state_accessor.get_state_entry(state_returns, addr) container = role_state_pb2.RoleRelationshipContainer() container.ParseFromString(state_entry.data) except KeyError: container = role_state_pb2.RoleRelationshipContainer() message_accessor.add_role_rel_to_container(container, new_role.role_id, pubkeys) entries_to_set[addr] = container.SerializeToString() state_accessor.set_state(state, entries_to_set)
def _role_already_exists(state_return, role_id): if not state_return: return False role_attr_container = role_state_pb2.RoleAttributesContainer() role_attr_container.ParseFromString(state_return[0].data) return message_accessor.is_in_role_attributes_container( container=role_attr_container, identifier=role_id)
def get(self, role_id): container = role_state_pb2.RoleAttributesContainer() address = addresser.make_role_attributes_address(role_id=role_id) container.ParseFromString(self.client.get_address(address=address)) items = list(container.role_attributes) if len(items) == 0: return None elif len(items) > 1: LOGGER.warning( "role container for %s at address %s has more than one role record", role_id, address, ) return items[0]
def test_state(self): """Grab the entire blockchain state and deserialize it""" subtree = addresser.family.namespace for item in client.list_state(subtree=subtree)["data"]: address_type = item["address_type"] = addresser.address_is( item["address"]) if address_type == addresser.AddressSpace.USER: content = user_state_pb2.UserContainer() content.ParseFromString(b64decode(item["data"])) elif address_type == addresser.AddressSpace.PROPOSALS: content = proposal_state_pb2.ProposalsContainer() content.ParseFromString(b64decode(item["data"])) elif address_type == addresser.AddressSpace.SYSADMIN_ATTRIBUTES: content = "SYSADMIN_ATTRIBUTES" elif address_type == addresser.AddressSpace.SYSADMIN_MEMBERS: content = "SYSADMIN_MEMBERS" elif address_type == addresser.AddressSpace.SYSADMIN_OWNERS: content = "SYSADMIN_OWNERS" elif address_type == addresser.AddressSpace.SYSADMIN_ADMINS: content = "SYSADMIN_ADMINS" elif address_type == addresser.AddressSpace.ROLES_ATTRIBUTES: content = role_state_pb2.RoleAttributesContainer() content.ParseFromString(b64decode(item["data"])) elif address_type == addresser.AddressSpace.ROLES_MEMBERS: content = role_state_pb2.RoleRelationshipContainer() content.ParseFromString(b64decode(item["data"])) elif address_type == addresser.AddressSpace.ROLES_OWNERS: content = role_state_pb2.RoleRelationshipContainer() content.ParseFromString(b64decode(item["data"])) elif address_type == addresser.AddressSpace.ROLES_ADMINS: content = role_state_pb2.RoleRelationshipContainer() content.ParseFromString(b64decode(item["data"])) elif address_type == addresser.AddressSpace.ROLES_TASKS: content = role_state_pb2.RoleRelationshipContainer() content.ParseFromString(b64decode(item["data"])) elif address_type == addresser.AddressSpace.TASKS_ATTRIBUTES: content = task_state_pb2.TaskAttributesContainer() content.ParseFromString(b64decode(item["data"])) elif address_type == addresser.AddressSpace.TASKS_OWNERS: content = task_state_pb2.TaskRelationshipContainer() content.ParseFromString(b64decode(item["data"])) elif address_type == addresser.AddressSpace.TASKS_ADMINS: content = task_state_pb2.TaskRelationshipContainer() content.ParseFromString(b64decode(item["data"])) else: content = "ERROR: unknown type: {}".format(address_type) LOGGER.debug("%-80s%-30s%s", item["address"], address_type, content)