【问题标题】:State's Unique Identify in CordaCorda 中的州唯一标识
【发布时间】:2019-02-01 15:56:34
【问题描述】:

我想在数据库中创建具有唯一 ID 的状态。有我的州代码

data class SampleState(
    val partyA: Party,
    val partyB: Party,
    val value: Int,
    val id: String,
    override val linearId: UniqueIdentifier = UniqueIdentifier(id),
    val properties: LCProperties = LCProperties("ABC"))    : LinearState {...}

当我提交两个相似的 SampleState 时,数据库中有两个不同的 State,具有两个不同的 linearId。那么,有谁能告诉我,如何确保数据库中 SampleState 对象的“id”是唯一的? 我在 Flows 和 Contracts 中使用相同的代码来捕获这种情况,例如

  val results = builder {

        val quantityIndex = SampleSchemaV1.PersistentSample::id.equal(id);

        val customCriteria1 = QueryCriteria.VaultCustomQueryCriteria(quantityIndex)

        val criteria = generalCriteria.and(customCriteria1);

        serviceHub.vaultService.queryBy<SampleState>(criteria)
    }
    if(results.states.count() > 0)
        throw IllegalArgumentException("id $id is exist")

但是,即使在 1 秒内(提交事务 1,1 秒后,提交事务 2),它也不能在几乎相似的时间内处理两个提交样本状态事务

【问题讨论】:

    标签: corda


    【解决方案1】:

    在您的州代码中,就是这一行:

    override val linearId: UniqueIdentifier = UniqueIdentifier(id)
    

    这将为您创建一个唯一的 ID。您传递给 UniqueIdentifier 的 id 将生成的唯一 id 绑定到您的 id。但是,所有相等和比较都仅基于唯一 ID。

    查看源代码中的 UniqueIdentifier.kt,您会发现这是底层代码:

    data class UniqueIdentifier(val externalId: String? = null, val id: UUID = UUID.randomUUID()) : Comparable<UniqueIdentifier> {
        override fun toString(): String = if (externalId != null) "${externalId}_$id" else id.toString()
    

    This 是一篇关于 Java 的 randomUUID 在确保 id 唯一性方面有多出色的好帖子

    您还可以阅读更多关于 UniqueIdentifier here

    【讨论】:

    • 那么,在 Corda 中,有什么办法可以确保数据库中存储两个相似的状态?例如,在上面的示例代码中,我不想存储两个具有相似 PartyA、PartyB、value 和 id 的 SampleState。
    • 你能给他们相同的外部ID吗?他们的 id 仍然是唯一的,但您可以根据这个相同的外部 ID 进行查询。
    • @CaisManai 即使您将相同的 UUID 传递给两个不同的线性 ID 状态,Corda 也没有强制执行这些唯一性的约束,这意味着您可以在保险库中有多个未使用的状态,所有状态都相同类型和线性 ID。
    猜你喜欢
    • 2022-11-16
    • 1970-01-01
    • 2019-02-13
    • 1970-01-01
    • 2011-08-17
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    相关资源
    最近更新 更多