ENT-10122: Add consuming transaction id to vault states table.

This commit is contained in:
Adel El-Beik 2023-07-17 17:58:31 +01:00
parent 6af6af4a80
commit 669d6590af
4 changed files with 34 additions and 6 deletions

View File

@ -68,7 +68,7 @@ class Vault<out T : ContractState>(val states: Iterable<StateAndRef<T>>) {
* other transactions observed, then the changes are observed "net" of those.
*/
@CordaSerializable
data class Update<U : ContractState> @JvmOverloads constructor(
data class Update<U : ContractState> constructor(
val consumed: Set<StateAndRef<U>>,
val produced: Set<StateAndRef<U>>,
val flowId: UUID? = null,
@ -78,8 +78,20 @@ class Vault<out T : ContractState>(val states: Iterable<StateAndRef<T>>) {
* differently.
*/
val type: UpdateType = UpdateType.GENERAL,
val references: Set<StateAndRef<U>> = emptySet()
val references: Set<StateAndRef<U>> = emptySet(),
val consumingTxIds: Map<StateRef, SecureHash> = emptyMap()
) {
@JvmOverloads constructor( consumed: Set<StateAndRef<U>>,
produced: Set<StateAndRef<U>>,
flowId: UUID? = null,
/**
* Specifies the type of update, currently supported types are general and, contract upgrade and notary change.
* Notary change transactions only modify the notary field on states, and potentially need to be handled
* differently.
*/
type: UpdateType = UpdateType.GENERAL,
references: Set<StateAndRef<U>> = emptySet()) : this(consumed, produced, flowId, type, references, consumingTxIds = emptyMap())
/** Checks whether the update contains a state of the specified type. */
inline fun <reified T : ContractState> containsType() = consumed.any { it.state.data is T } || produced.any { it.state.data is T } || references.any { it.state.data is T }
@ -105,7 +117,7 @@ class Vault<out T : ContractState>(val states: Iterable<StateAndRef<T>>) {
val combinedConsumed = consumed + (rhs.consumed - produced)
// The ordering below matters to preserve ordering of consumed/produced Sets when they are insertion order dependent implementations.
val combinedProduced = produced.filter { it !in rhs.consumed }.toSet() + rhs.produced
return copy(consumed = combinedConsumed, produced = combinedProduced, references = references + rhs.references)
return copy(consumed = combinedConsumed, produced = combinedProduced, references = references + rhs.references, consumingTxIds = consumingTxIds + rhs.consumingTxIds)
}
override fun toString(): String {
@ -138,6 +150,16 @@ class Vault<out T : ContractState>(val states: Iterable<StateAndRef<T>>) {
return Update(consumed, produced, flowId, type, references)
}
/** Additional copy method to maintain backwards compatibility. */
fun copy(
consumed: Set<StateAndRef<U>>,
produced: Set<StateAndRef<U>>,
flowId: UUID? = null,
type: UpdateType = UpdateType.GENERAL,
references: Set<StateAndRef<U>> = emptySet()
): Update<U> {
return Update(consumed, produced, flowId, type, references, consumingTxIds)
}
}
@CordaSerializable

View File

@ -231,6 +231,7 @@ class NodeVaultService(
if (stateStatus != Vault.StateStatus.CONSUMED) {
stateStatus = Vault.StateStatus.CONSUMED
consumedTime = clock.instant()
consumingTxId = update.consumingTxIds[stateRef]?.toString() ?: ""
// remove lock (if held)
if (lockId != null) {
lockId = null
@ -370,8 +371,8 @@ class NodeVaultService(
}
}
}
return Vault.Update(consumedStates.toSet(), ourNewStates.toSet(), references = newReferenceStateAndRefs.toSet())
val consumedTxIds = consumedStates.associate { Pair(it.ref, tx.id) }
return Vault.Update(consumedStates.toSet(), ourNewStates.toSet(), references = newReferenceStateAndRefs.toSet(), consumingTxIds = consumedTxIds)
}
fun resolveAndMakeUpdate(tx: CoreTransaction): Vault.Update<ContractState>? {

View File

@ -91,7 +91,11 @@ object VaultSchemaV1 : MappedSchema(
/** associated constraint type data (if any) */
@Column(name = "constraint_data", length = MAX_CONSTRAINT_DATA_SIZE, nullable = true)
@Type(type = "corda-wrapper-binary")
var constraintData: ByteArray? = null
var constraintData: ByteArray? = null,
/** consuming transaction */
@Column(name = "consuming_tx_id", length = 144, nullable = false)
var consumingTxId: String = ""
) : PersistentState()
@Entity

View File

@ -13,4 +13,5 @@
<include file="migration/vault-schema.changelog-v8.xml"/>
<include file="migration/vault-schema.changelog-v11.xml"/>
<include file="migration/vault-schema.changelog-v13.xml"/>
<include file="migration/vault-schema.changelog-v14.xml"/>
</databaseChangeLog>