mirror of
https://github.com/corda/corda.git
synced 2024-12-28 16:58:55 +00:00
Refactor NotaryFlow.Client so it's easier to override its behaviour
This commit is contained in:
parent
dca8699e7c
commit
e1edce18f0
@ -47,8 +47,18 @@ class NotaryFlow {
|
|||||||
@Suspendable
|
@Suspendable
|
||||||
@Throws(NotaryException::class)
|
@Throws(NotaryException::class)
|
||||||
override fun call(): List<TransactionSignature> {
|
override fun call(): List<TransactionSignature> {
|
||||||
|
val notaryParty = checkTransaction()
|
||||||
progressTracker.currentStep = REQUESTING
|
progressTracker.currentStep = REQUESTING
|
||||||
|
val response = notarise(notaryParty)
|
||||||
|
progressTracker.currentStep = VALIDATING
|
||||||
|
return validateResponse(response, notaryParty)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks that the transaction specifies a valid notary, and verifies that it contains all required signatures
|
||||||
|
* apart from the notary's.
|
||||||
|
*/
|
||||||
|
protected fun checkTransaction(): Party {
|
||||||
val notaryParty = stx.notary ?: throw IllegalStateException("Transaction does not specify a Notary")
|
val notaryParty = stx.notary ?: throw IllegalStateException("Transaction does not specify a Notary")
|
||||||
check(serviceHub.networkMapCache.isNotary(notaryParty)) { "$notaryParty is not a notary on the network" }
|
check(serviceHub.networkMapCache.isNotary(notaryParty)) { "$notaryParty is not a notary on the network" }
|
||||||
check(stx.inputs.all { stateRef -> serviceHub.loadState(stateRef).notary == notaryParty }) {
|
check(stx.inputs.all { stateRef -> serviceHub.loadState(stateRef).notary == notaryParty }) {
|
||||||
@ -60,19 +70,18 @@ class NotaryFlow {
|
|||||||
} catch (ex: SignatureException) {
|
} catch (ex: SignatureException) {
|
||||||
throw NotaryException(NotaryError.TransactionInvalid(ex))
|
throw NotaryException(NotaryError.TransactionInvalid(ex))
|
||||||
}
|
}
|
||||||
|
return notaryParty
|
||||||
|
}
|
||||||
|
|
||||||
val response = try {
|
@Throws(NotaryException::class)
|
||||||
|
@Suspendable
|
||||||
|
protected fun notarise(notaryParty: Party): UntrustworthyData<List<TransactionSignature>> {
|
||||||
|
return try {
|
||||||
val session = initiateFlow(notaryParty)
|
val session = initiateFlow(notaryParty)
|
||||||
if (serviceHub.networkMapCache.isValidatingNotary(notaryParty)) {
|
if (serviceHub.networkMapCache.isValidatingNotary(notaryParty)) {
|
||||||
subFlow(SendTransactionWithRetry(session, stx))
|
sendAndReceiveValidating(session)
|
||||||
session.receive<List<TransactionSignature>>()
|
|
||||||
} else {
|
} else {
|
||||||
val tx: Any = if (stx.isNotaryChangeTransaction()) {
|
sendAndReceiveNonValidating(notaryParty, session)
|
||||||
stx.notaryChangeTx
|
|
||||||
} else {
|
|
||||||
stx.buildFilteredTransaction(Predicate { it is StateRef || it is TimeWindow || it == notaryParty })
|
|
||||||
}
|
|
||||||
session.sendAndReceiveWithRetry(tx)
|
|
||||||
}
|
}
|
||||||
} catch (e: NotaryException) {
|
} catch (e: NotaryException) {
|
||||||
if (e.error is NotaryError.Conflict) {
|
if (e.error is NotaryError.Conflict) {
|
||||||
@ -80,7 +89,25 @@ class NotaryFlow {
|
|||||||
}
|
}
|
||||||
throw e
|
throw e
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suspendable
|
||||||
|
protected open fun sendAndReceiveValidating(session: FlowSession): UntrustworthyData<List<TransactionSignature>> {
|
||||||
|
subFlow(SendTransactionWithRetry(session, stx))
|
||||||
|
return session.receive()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suspendable
|
||||||
|
protected open fun sendAndReceiveNonValidating(notaryParty: Party, session: FlowSession): UntrustworthyData<List<TransactionSignature>> {
|
||||||
|
val tx: Any = if (stx.isNotaryChangeTransaction()) {
|
||||||
|
stx.notaryChangeTx // Notary change transactions do not support filtering
|
||||||
|
} else {
|
||||||
|
stx.buildFilteredTransaction(Predicate { it is StateRef || it is TimeWindow || it == notaryParty })
|
||||||
|
}
|
||||||
|
return session.sendAndReceiveWithRetry(tx)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun validateResponse(response: UntrustworthyData<List<TransactionSignature>>, notaryParty: Party): List<TransactionSignature> {
|
||||||
return response.unwrap { signatures ->
|
return response.unwrap { signatures ->
|
||||||
signatures.forEach { validateSignature(it, stx.id, notaryParty) }
|
signatures.forEach { validateSignature(it, stx.id, notaryParty) }
|
||||||
signatures
|
signatures
|
||||||
|
Loading…
Reference in New Issue
Block a user