mirror of
https://github.com/corda/corda.git
synced 2025-02-20 17:33:15 +00:00
Make NotaryFlow idempotent
Alternatively, we could make the underlying UniquenessProviders idempotent.
This commit is contained in:
parent
021bcb0628
commit
159ca9884f
@ -133,9 +133,18 @@ object NotaryFlow {
|
||||
try {
|
||||
uniquenessProvider.commit(tx.inputs, tx.id, otherSide)
|
||||
} catch (e: UniquenessException) {
|
||||
val conflictData = e.error.serialize()
|
||||
val signedConflict = SignedData(conflictData, sign(conflictData.bytes))
|
||||
throw NotaryException(NotaryError.Conflict(tx, signedConflict))
|
||||
// Allow re-committing the transaction to make the NotaryFlow idempotent. Alternatively, we could make
|
||||
// the underlying UniquenessProviders idempotent.
|
||||
val conflicts = tx.inputs.filterIndexed { i, stateRef ->
|
||||
val consumingTx = e.error.stateHistory[stateRef]
|
||||
consumingTx != null && consumingTx != UniquenessProvider.ConsumingTx(tx.id, i, otherSide)
|
||||
}
|
||||
if (conflicts.isNotEmpty()) {
|
||||
// TODO: Create a new UniquenessException that only contains the conflicts filtered above.
|
||||
val conflictData = e.error.serialize()
|
||||
val signedConflict = SignedData(conflictData, sign(conflictData.bytes))
|
||||
throw NotaryException(NotaryError.Conflict(tx, signedConflict))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,7 +85,7 @@ class NotaryServiceTests {
|
||||
assertThat(ex.error).isInstanceOf(NotaryError.TimestampInvalid::class.java)
|
||||
}
|
||||
|
||||
@Test fun `should report conflict for a duplicate transaction`() {
|
||||
@Test fun `should sign identical transaction multiple times (signing is idempotent)`() {
|
||||
val stx = run {
|
||||
val inputState = issueState(clientNode)
|
||||
val tx = TransactionType.General.Builder(notaryNode.info.notaryIdentity).withItems(inputState)
|
||||
@ -93,8 +93,32 @@ class NotaryServiceTests {
|
||||
tx.toSignedTransaction(false)
|
||||
}
|
||||
|
||||
val firstAttempt = NotaryFlow.Client(stx)
|
||||
val secondAttempt = NotaryFlow.Client(stx)
|
||||
clientNode.services.startFlow(firstAttempt)
|
||||
val future = clientNode.services.startFlow(secondAttempt)
|
||||
|
||||
net.runNetwork()
|
||||
|
||||
future.resultFuture.getOrThrow()
|
||||
}
|
||||
|
||||
@Test fun `should report conflict when inputs are reused accross transactions`() {
|
||||
val inputState = issueState(clientNode)
|
||||
val stx = run {
|
||||
val tx = TransactionType.General.Builder(notaryNode.info.notaryIdentity).withItems(inputState)
|
||||
tx.signWith(clientNode.keyPair!!)
|
||||
tx.toSignedTransaction(false)
|
||||
}
|
||||
val stx2 = run {
|
||||
val tx = TransactionType.General.Builder(notaryNode.info.notaryIdentity).withItems(inputState)
|
||||
tx.addInputState(issueState(clientNode))
|
||||
tx.signWith(clientNode.keyPair!!)
|
||||
tx.toSignedTransaction(false)
|
||||
}
|
||||
|
||||
val firstSpend = NotaryFlow.Client(stx)
|
||||
val secondSpend = NotaryFlow.Client(stx)
|
||||
val secondSpend = NotaryFlow.Client(stx2) // Double spend the inputState in a second transaction.
|
||||
clientNode.services.startFlow(firstSpend)
|
||||
val future = clientNode.services.startFlow(secondSpend)
|
||||
|
||||
@ -102,11 +126,10 @@ class NotaryServiceTests {
|
||||
|
||||
val ex = assertFailsWith(NotaryException::class) { future.resultFuture.getOrThrow() }
|
||||
val notaryError = ex.error as NotaryError.Conflict
|
||||
assertEquals(notaryError.tx, stx.tx)
|
||||
assertEquals(notaryError.tx, stx2.tx)
|
||||
notaryError.conflict.verified()
|
||||
}
|
||||
|
||||
|
||||
private fun runNotaryClient(stx: SignedTransaction): ListenableFuture<DigitalSignature.WithKey> {
|
||||
val flow = NotaryFlow.Client(stx)
|
||||
val future = clientNode.services.startFlow(flow).resultFuture
|
||||
|
Loading…
x
Reference in New Issue
Block a user