问题
When I tried to use below codes to retrieve the state by linearId, I got 2 records returned, one is the consumed one, the other is the unconsumed one. The initial linearId was passed in from web api.
val linearId: UniqueIdentifier = UniqueIdentifier(null, UUID.fromString(legalContractState.legalContract.linearId))
val linearIds = listOf(linearId)
val linearStateCriteria = QueryCriteria.LinearStateQueryCriteria(linearId = listOf(linearIds.first(), linearIds.last()))
val states = serviceHub.vaultQueryService.queryBy(LegalContractState::class.java, linearStateCriteria).states
val inputState: StateAndRef<LegalContractState> = serviceHub.vaultQueryService.queryBy(LegalContractState::class.java, linearStateCriteria).states.single()
But from a sample code on the vault api page, it says this will return an unconsumed state based on a linearId, I also checked the data in H2 database VAULT_STATES table, there are 2 records, one has a CONSUMED_TIMESTAMP and its STATE_STATUS is 1, but the other one CONSUMED_TIMESTAMP IS null and STATE_STATUS is 0. This is one unshared state which means only stored in my database, and I executed one update for it, so ideally have one consumed state and one new output state in db. So now I am not sure what's wrong here.
Query for unconsumed linear states for given linear ids:
val linearIds = issuedStates.states.map { it.state.data.linearId }.toList()
val criteria = LinearStateQueryCriteria(linearId = listOf(linearIds.first(), linearIds.last()))
val results = vaultQuerySvc.queryBy<LinearState>(criteria)
回答1:
This is a bug that will be fixed in release M14. See https://github.com/corda/corda/issues/949.
来源:https://stackoverflow.com/questions/45144809/servicehub-vaultqueryservice-queryby-returns-all-related-states-by-default