A simple counter is a resource holding some number. The value can be initiated when a new instance of the counter is created or incremented by one.
Resource structure
- label: a unique counter identifier. In the example code it is generated randomly
- value: holds the counter state. The init value is 1. The ephemeral counter value is 0.
- quantity: 1
In this example, the nullifier key and nullifier key commitment are set to random values. In a real system they would be set to the keypair that belongs to the actor responsible for updating the counter.
Transaction functions
- Init: Mints a new counter with unique counter id and value 1. Balanced with an ephemeral resource.
- Increment: Increments the value of the counter by one. All other fields stay the same.
Ephemeral resources are only consumed to mint new counters. No ephemeral resources can be created
The logic constraints
consumed.label == created.label
consumed.quantity == 1
created.quantity == 1
if consumed.is_ephemeral = True:
created.value == 1
created.value == consumed.value + 1