Searching the chain can i find if something has been consumed and what it was consumed by?

丶灬走出姿态 提交于 2021-01-29 17:30:54

问题


I am trying to pass out information to a node but that node may not have been on the network when it was initially sent out. So to work around this I am using transaction references, the transaction uses "addReferenceState" to reference previous states, and then the service hub to have the ability to recursively run back through the information.

So I have this method that's all fine and works.

private fun findInChain(infoState: StateAndRef<InfoState>): StateAndRef<InfoState> {
    val stx = serviceHub.validatedTransactions.getTransaction(state.ref.txhash)!!
    if (infoState.state.data.id == id) {
        return state
    } else {
        val prvRef =
            stx.tx.references.single()
        val prvState = serviceHub.validatedTransactions.getTransaction(prvRef.txhash)!!.tx.outRefsOfType<InfoState>()
                .single()
        return findInChain(prvState)
    }
}

However I now have a new requirement that any one of those states could be consumed as an input to update the pice of information.

So the new requirement means I first of all need to check if a state has been consumed before sending it back and secondly need to find the transaction that consumed it ...

private fun findInChain(infoState: StateAndRef<InfoState>): StateAndRef<InfoState> {
    val stx = serviceHub.validatedTransactions.getTransaction(state.ref.txhash)!!
    if (infoState.state.data.id == id) {
        
        // 1st Check if the state is consumed

        // 2nd: If the state is consumed find what consumed it

        return state
    } else {
        val prvRef =
            stx.tx.references.single()
        val prvState = serviceHub.validatedTransactions.getTransaction(prvRef.txhash)!!.tx.outRefsOfType<InfoState>()
                .single()
        return findInChain(prvState)
    }
}

Thanks for any help in advance


回答1:


I would need to understand your use case to have a better answer. I believe you are using the reference state incorrectly. ReferenceStates are used in situations where you want to have access to a state's data in a transaction without consuming it. You could think of it as something that stores master data and you can refer to it in different transactions without necessarily consuming it.

As per your requirement, you could do a vaultQuery with Vault.StateStatus.ALL criteria, which would fetch the state irrespective it's consumed to not. You could then check the status to identify if it's consumed. Or just fetch the Consumed/ Unconsumed and check based on the number of results returned. Either way you like.

Though one could walk the trnx-chain in backward direction, I am not aware of a direct API which can give you which transaction consumed a state. You might perhaps have to build a custom solution for this.



来源:https://stackoverflow.com/questions/63688600/searching-the-chain-can-i-find-if-something-has-been-consumed-and-what-it-was-co

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!