About Labels
Date | Responsible | Changes |
---|---|---|
July 10, 2024 | @Luis Fidelis | Initial version |
This document describes the concept of labels and helps users leveraging its use in their use cases.
What is a label?
Label - meta.labels
- is a property of records that allows users to attach a label or tag anything in ledger with a descriptive name. Some examples of the usage of labels in ledger:
- preferred accounts: marking certain wallets or anchors as preferred.
- organizing records: marking wallets as saving accounts, cards, loans, etc.
The regular custom
fields can be used to accomplish many of those use cases as well and they are preferable in most situations. Labels can be an alternative when it makes sense in same ways you can use tags in many apps to organize and group.
Labels vs Custom
Even though custom
is applicable for most of the cases, labels can have uniqueness constraints defined via policies in ledger.
Let's dive into the example of marking a preferred account in the system.
An account could be marked as preferred by having a preferred property in custom
,
i.e.
but this basic approach is very limited in several main ways.
- The first issue is that you can have only one preferred account, sometimes you want to have a preferred per symbol, this becomes difficult to manage with
custom
. - The second issue is related to security, marking something as preferred requires you to have full access to manage that record in ledger, since you need to update it to set a value.
- The last main issue is related to data consistency but it overlaps a bit with the security aspect. The issue is related to removing the preferred field from the record that was a preferred previously. This can either cause multiple records being marked as preferred or requires someone to have access to both the new and the currently preferred records. Often this is very difficult to implement while keeping everything secure and protected.
Managing record labels
Users can manage labels of a record within a proof. Proofs with labels can be attached to records when they are created - POST /{record}s
e.g. POST /anchors
- or with a separate call to
POST /{record}s/{id}/proofs
e.g. POST /anchors/anchor-handle/proofs
.
A proof can push, pull and make complex changes in a record labels list.
Below is extensive list of expressions the ledger supports for changing record labels:
The simple array expression from above which replaces the labels
array in record completely can be also expressed as complex expression. So the following 2 have the same meaning:
Some examples:
Managing labels with policies
This section uses labels policies to define which labels are valid for a record as well as their uniqueness criteria. Please, read About Labels Policies for keeping up to date with this concept.
Given the policy above, let’s look at an example when we have two account anchors on the same wallet:
First, we label the svgs.100100.min
anchor as preferred
:
After that, someone else marks the svgs.200200.tesla
anchor as preferred
:
Because the labels policy doesn’t allow preferred
to be assigned to multiple anchors with the same wallet and symbol, the ledger will actually move the preferred
label from svgs.100100.mint
to svgs.200200.tesla
.
Reassigning labels automatically prevents bugs because of incorrect implementations and makes it simpler to connect to the system because there are fewer operations that external systems need to perform.
This behavior makes the feature easy to use to end users, and makes labels the way to go in complex situations like that. In this example, users don’t have to worry about what is marked as preferred
currently, they only need to state what they want as final state. Because of policies, we also get a strict data model, without duplicates and invalid states.
Automatic removal of unique label
Since the initiator appends a proof with labels
operation to a record, this proof will correspond to the labels mutation on the same record. On the other hand, the record that looses a label due to unique
constraint will have a proof signed by ledger system signer which will instruct the removal of this label. An example of a proof added by the ledger for example above.
Querying by labels
Labels can also be useful for querying, ledger allows users to query labeled records by using label names:
MongoDB Query Language works by doing contains operation on arrays if you pass a single value, see more info here.
Notifying about changed record labels
Changing the labels of the record fires also <record>-updated
event where the new state of record labels will be delivered to the listener in the record meta of the payload. Also the last proof which caused the change in labels will be included in this payload.
For the anchor svgs.200200.tesla
above when the preferred
label is applied the following event payload will be delivered to the listener who registered the effect for anchor-updated
Also for the anchor svgs.100100.mint
the anchor-updated
event will be delivered where the preferred
label is removed: