Search code examples
blockchaincorda

Corda totalStatesAvailable value in vaultQuery


I am trying to get consumed states using below queryCriteria. And I know that only consumed states will be present in vault for this particular State class

    var criteria: QueryCriteria = QueryCriteria.VaultQueryCriteria(
         externalIds = listOf(carAccountId)
     ).and(
        QueryCriteria.VaultQueryCriteria(
            Vault.StateStatus.CONSUMED,
            timeCondition = betweenRecordedTime(
                startDate?.toInstant(ZoneOffset.UTC),
                endDate?.toInstant(ZoneOffset.UTC)
            )
        )
    )

But the surprising fact is, in the Vault.Page outcome. I can see totalStatesAvailable as 4 whereas final states list output = 2. I am expecting value = 2 on both, because I issued a state and consume in same flow. (similarly another state instance created, hence total 2)

And the statesMetadata field, I can see 4 states data details there, where 2 expected states has a duplicate (recorded time, createdtime, ref all are same)

For unconsumed cases the outcome is as expected (totalStatesAvailable = Query states list output).

Please let me know what am I missing here.


Solution

  • I found out the reason for this, while giving the participants for the states to be created in flow, our scenario is having two Corda account ids which can be same or different according to the business scenario.

    But the code was doing .distinct() after creating public keys using RequestKeyForAccount() which will be different for same accountInfo (duplicate)

        val publicKeysOfParticipants =
            gatherKeys(car1OwnerAccount, car2OwnerAccount, car1Account, car2Account)
    

    //gatherKeys() calls the RequestKeyForAccount()

        val participants = gatherParticipants(publicKeysOfParticipants)
    

    Here the publicKeysOfParticipants will be anyway 4 different ones even if there is one duplicate accountInfo. that is .distinct() should have applied before getting the Publickey from the Anonymous party creation.

    But I am not yet clear the logic of having totalStatesAvailable will be double based on given query. but the reason is this. So I am assuming that, Corda is considering or creating states (all these are consumed states) based on Anonymous party present in participants list.

    (here both accounts (car1OwnerAccount, car2OwnerAccount) are in same node and both are two anonymous parties based on same account ID)

    And the query is by externalIds, for that kind of query we get this issue.