mirror of
https://github.com/corda/corda.git
synced 2025-03-11 15:04:14 +00:00
Vault: return a list of unconsumed states rather than a sequence. The lazy evaluation captures some extra context, serialization of which in flows results in errors.
This commit is contained in:
parent
f44dd969ce
commit
6f3ed327a0
@ -38,7 +38,7 @@ val DEFAULT_SESSION_ID = 0L
|
|||||||
* Active means they haven't been consumed yet (or we don't know about it).
|
* Active means they haven't been consumed yet (or we don't know about it).
|
||||||
* Relevant means they contain at least one of our pubkeys.
|
* Relevant means they contain at least one of our pubkeys.
|
||||||
*/
|
*/
|
||||||
class Vault(val states: Iterable<StateAndRef<ContractState>>) {
|
class Vault(val states: List<StateAndRef<ContractState>>) {
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
inline fun <reified T : ContractState> statesOfType() = states.filter { it.state.data is T } as List<StateAndRef<T>>
|
inline fun <reified T : ContractState> statesOfType() = states.filter { it.state.data is T } as List<StateAndRef<T>>
|
||||||
|
|
||||||
@ -151,14 +151,12 @@ interface VaultService {
|
|||||||
* Possibly update the vault by marking as spent states that these transactions consume, and adding any relevant
|
* Possibly update the vault by marking as spent states that these transactions consume, and adding any relevant
|
||||||
* new states that they create. You should only insert transactions that have been successfully verified here!
|
* new states that they create. You should only insert transactions that have been successfully verified here!
|
||||||
*
|
*
|
||||||
* Returns the new vault that resulted from applying the transactions (note: it may quickly become out of date).
|
|
||||||
*
|
|
||||||
* TODO: Consider if there's a good way to enforce the must-be-verified requirement in the type system.
|
* TODO: Consider if there's a good way to enforce the must-be-verified requirement in the type system.
|
||||||
*/
|
*/
|
||||||
fun notifyAll(txns: Iterable<WireTransaction>): Vault
|
fun notifyAll(txns: Iterable<WireTransaction>)
|
||||||
|
|
||||||
/** Same as notifyAll but with a single transaction. */
|
/** Same as notifyAll but with a single transaction. */
|
||||||
fun notify(tx: WireTransaction): Vault = notifyAll(listOf(tx))
|
fun notify(tx: WireTransaction) = notifyAll(listOf(tx))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provide a [Future] for when a [StateRef] is consumed, which can be very useful in building tests.
|
* Provide a [Future] for when a [StateRef] is consumed, which can be very useful in building tests.
|
||||||
|
@ -52,7 +52,7 @@ class CordaRPCOpsImpl(
|
|||||||
override fun vaultAndUpdates(): Pair<List<StateAndRef<ContractState>>, Observable<Vault.Update>> {
|
override fun vaultAndUpdates(): Pair<List<StateAndRef<ContractState>>, Observable<Vault.Update>> {
|
||||||
return databaseTransaction(database) {
|
return databaseTransaction(database) {
|
||||||
val (vault, updates) = services.vaultService.track()
|
val (vault, updates) = services.vaultService.track()
|
||||||
Pair(vault.states.toList(), updates)
|
Pair(vault.states, updates)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,13 +103,13 @@ class NodeVaultService(private val services: ServiceHub) : SingletonSerializeAsT
|
|||||||
// For use during publishing only.
|
// For use during publishing only.
|
||||||
val updatesPublisher: rx.Observer<Vault.Update> get() = _updatesPublisher.bufferUntilDatabaseCommit().tee(_rawUpdatesPublisher)
|
val updatesPublisher: rx.Observer<Vault.Update> get() = _updatesPublisher.bufferUntilDatabaseCommit().tee(_rawUpdatesPublisher)
|
||||||
|
|
||||||
fun allUnconsumedStates(): Iterable<StateAndRef<ContractState>> {
|
fun allUnconsumedStates(): List<StateAndRef<ContractState>> {
|
||||||
// Order by txhash for if and when transaction storage has some caching.
|
// Ideally we'd map this transform onto a sequence, but we can't have a lazy list here, since accessing it
|
||||||
// Map to StateRef and then to StateAndRef. Use Sequence to avoid conversion to ArrayList that Iterable.map() performs.
|
// from a flow might end up trying to serialize the captured context - vault internal state or db context.
|
||||||
return unconsumedStates.asSequence().map {
|
return unconsumedStates.map {
|
||||||
val storedTx = services.storageService.validatedTransactions.getTransaction(it.txhash) ?: throw Error("Found transaction hash ${it.txhash} in unconsumed contract states that is not in transaction storage.")
|
val storedTx = services.storageService.validatedTransactions.getTransaction(it.txhash) ?: throw Error("Found transaction hash ${it.txhash} in unconsumed contract states that is not in transaction storage.")
|
||||||
StateAndRef(storedTx.tx.outputs[it.index], it)
|
StateAndRef(storedTx.tx.outputs[it.index], it)
|
||||||
}.asIterable()
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun recordUpdate(update: Vault.Update): Vault.Update {
|
fun recordUpdate(update: Vault.Update): Vault.Update {
|
||||||
@ -169,7 +169,7 @@ class NodeVaultService(private val services: ServiceHub) : SingletonSerializeAsT
|
|||||||
override val linearHeads: Map<UniqueIdentifier, StateAndRef<LinearState>>
|
override val linearHeads: Map<UniqueIdentifier, StateAndRef<LinearState>>
|
||||||
get() = currentVault.states.filterStatesOfType<LinearState>().associateBy { it.state.data.linearId }.mapValues { it.value }
|
get() = currentVault.states.filterStatesOfType<LinearState>().associateBy { it.state.data.linearId }.mapValues { it.value }
|
||||||
|
|
||||||
override fun notifyAll(txns: Iterable<WireTransaction>): Vault {
|
override fun notifyAll(txns: Iterable<WireTransaction>) {
|
||||||
val ourKeys = services.keyManagementService.keys.keys
|
val ourKeys = services.keyManagementService.keys.keys
|
||||||
val netDelta = txns.fold(Vault.NoUpdate) { netDelta, txn -> netDelta + makeUpdate(txn, netDelta, ourKeys) }
|
val netDelta = txns.fold(Vault.NoUpdate) { netDelta, txn -> netDelta + makeUpdate(txn, netDelta, ourKeys) }
|
||||||
if (netDelta != Vault.NoUpdate) {
|
if (netDelta != Vault.NoUpdate) {
|
||||||
@ -179,7 +179,6 @@ class NodeVaultService(private val services: ServiceHub) : SingletonSerializeAsT
|
|||||||
updatesPublisher.onNext(netDelta)
|
updatesPublisher.onNext(netDelta)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return currentVault
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun addNoteToTransaction(txnId: SecureHash, noteText: String) {
|
override fun addNoteToTransaction(txnId: SecureHash, noteText: String) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user