userAG
userAG

Reputation: 99

How to generate unique id from oracle corda service

I have a corda state say Employee and I need to generate the Employee Id. Anyone please let me know how to generate this through corda oracle services?

Upvotes: 1

Views: 119

Answers (1)

Joel
Joel

Reputation: 23140

The Oracle could have an atomic counter that is incremented each time an employee ID is allocated.

Where should you put this counter?

  • You can't put it inside a flow, as flows are short-lived and do not share state
  • Instead, you need to put the counter inside a service. A Corda service is a long-lived object that is instantiated when a node starts, and allows state to be shared between flows

You could define the service as follows:

@CordaService
class Oracle(val services: ServiceHub) : SingletonSerializeAsToken() {
    private val counter = AtomicInteger(0)

    val employeeId
        get() = counter.getAndIncrement()
}

And you would retrieve the employee ID in a flow using:

serviceHub.cordaService(Oracle::class.java).employeeId

Upvotes: 1

Related Questions