Counter¶
This example describes the mechanics of counters associated with a specific controller, implemented in a resource model.
CounterId resource (application)¶
CounterId resource represents a counter tied to a specific controller that is used to initialise other counters. In a sense, it can be seen as a counter of counters. CounterId resource can be seen as an atomic counter (in a broad sense) application. For that reason, we describe it in the application terms.
CounterId application interface¶
- Init: creates a new
CounterIdresource with a zero value. - Update: updates an existing
CounterIdresource from value \(n\) to value \(n + 1\).
CounterId application logic constraints¶
- The quantity of each
CounterIdresource must be the same, set to a non-zero constant value \(q\). - Each
CounterIdresource must have the controller's public key in the label. - A signature from the controller authorising the transaction must be provided (verified with the key stored in the label).
- Init case: The value (representing the current count) must be set to 0. The transaction must be balanced by a consumed ephemeral resource of the same kind.
- Update case: The value of the created resource must be set to \(n + 1\) where \(n\) is the value of the consumed resource of the same kind.
Counter application¶
Counter resource represents a simple counter.
Counter application interface¶
- Init: a new counter is initialised from the current counterId resource (the counterId value goes to the counter resource label). When a new counter is created, the
CounterIdresource is updated to ensure there are no counters that have the same label. - Update: An existing
Counterresource can be updated from value \(n\) to value \(n + 1\).
Counter application logic constraints¶
- The quantity of each counterId resource must be the same, set to a non-zero constant value \(q\).
- Each
Counterresource must have the controller's public key in the label and a value of theCounterIdresource from the moment theCounterresource was initialised. - Init case:
- The label and the value are set to the value of the
CounterIdresource updated in the same transaction function. The transaction must be balanced by a consumed ephemeral resource of the same kind. - The resource kind of theCounterIdresource must be verified to ensure that this is indeed theCounterIdresource's value is used to derive theCounterresource label. - Update case: - The value of the created resource must be set to \(n + 1\) where \(n\) is the value of the consumed resource of the same kind.
In this simple version, updating the counter requires no special permissions but the counter resource logic can be modified to be more complex and have more restricted counter update rules.