예제 #1
0
def _handle_role_state_set(create_role, state):
    role_container = role_state_pb2.RoleAttributesContainer()
    role = role_container.role_attributes.add()
    role.role_id = create_role.role_id
    role.name = create_role.name
    role.metadata = create_role.metadata

    entries_to_set = [
        StateEntry(address=addresser.make_role_attributes_address(
            create_role.role_id),
                   data=role_container.SerializeToString())
    ]

    pubkeys_by_address = {}

    for admin in list(create_role.admins):
        admin_address = addresser.make_role_admins_address(
            role_id=create_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(create_role.owners):
        owner_address = addresser.make_role_owners_address(
            role_id=create_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 = get_state(state, [
        addresser.make_role_admins_address(role_id=create_role.role_id,
                                           user_id=a)
        for a in create_role.admins
    ] + [
        addresser.make_role_owners_address(role_id=create_role.role_id,
                                           user_id=o)
        for o in create_role.owners
    ])

    for addr, pubkeys in pubkeys_by_address.items():
        try:
            state_entry = get_state_entry(state_returns, addr)
            container = role_state_pb2.RoleRelationshipContainer()
            container.ParseFromString(state_entry.data)
        except KeyError:
            container = role_state_pb2.RoleRelationshipContainer()

        _add_role_rel_to_container(container, create_role.role_id, pubkeys)

        entries_to_set.append(
            StateEntry(address=addr, data=container.SerializeToString()))
    set_state(state, entries_to_set)
def handle_confirm_add(state_entries,
                       header,
                       confirm,
                       role_rel_address,
                       state,
                       rel_type='user_id'):
    proposal_address = addresser.make_proposal_address(
        object_id=confirm.role_id, related_id=getattr(confirm, rel_type))

    proposal_entry = get_state_entry(state_entries, proposal_address)
    proposal_container = return_prop_container(proposal_entry)
    proposal = get_prop_from_container(proposal_container,
                                       proposal_id=confirm.proposal_id)

    proposal.status = proposal_state_pb2.Proposal.CONFIRMED
    proposal.closer = header.signer_pubkey
    proposal.close_reason = confirm.reason

    address_values = [
        StateEntry(address=proposal_address,
                   data=proposal_container.SerializeToString())
    ]

    try:
        role_rel_entry = get_state_entry(state_entries, role_rel_address)
        role_rel_container = return_role_rel_container(role_rel_entry)
    except KeyError:
        role_rel_container = role_state_pb2.RoleRelationshipContainer()

    try:
        role_rel = get_role_rel(role_rel_container, confirm.role_id)

    except KeyError:
        role_rel = role_rel_container.relationships.add()
        role_rel.role_id = confirm.role_id

    role_rel.identifiers.append(getattr(confirm, rel_type))

    address_values.append(
        StateEntry(address=role_rel_address,
                   data=role_rel_container.SerializeToString()))

    set_state(state, address_values)
예제 #3
0
def return_role_rel_container(entry):
    role_rel_container = role_state_pb2.RoleRelationshipContainer()
    role_rel_container.ParseFromString(entry.data)
    return role_rel_container