Turn Verifier into an abstract base class that is specialised by BasicVerifier and DeterministicVerifier.

This commit is contained in:
Chris Rankin 2019-08-01 17:14:18 +01:00
parent 378635475a
commit cccc5992ae
4 changed files with 21 additions and 11 deletions

View File

@ -29,12 +29,12 @@ fun LedgerTransaction.prepareVerify(extraAttachments: List<Attachment>) = this.i
* Because we create a separate [LedgerTransaction] onto which we need to perform verification, it becomes important we don't verify the
* wrong object instance. This class helps avoid that.
*/
open class Verifier(val ltx: LedgerTransaction, protected val transactionClassLoader: ClassLoader) : AutoCloseable {
abstract class Verifier(val ltx: LedgerTransaction, protected val transactionClassLoader: ClassLoader) : AutoCloseable {
private val inputStates: List<TransactionState<*>> = ltx.inputs.map { it.state }
private val allStates: List<TransactionState<*>> = inputStates + ltx.references.map { it.state } + ltx.outputs
companion object {
private val logger = contextLogger()
val logger = contextLogger()
}
/**
@ -344,13 +344,25 @@ open class Verifier(val ltx: LedgerTransaction, protected val transactionClassLo
}
}
/**
* Placeholder function for the contract verification logic.
*/
abstract fun verifyContracts()
/**
* Placeholder function so that the [Verifier] can release any resources.
*/
abstract override fun close()
}
class BasicVerifier(ltx: LedgerTransaction, transactionClassLoader: ClassLoader) : Verifier(ltx, transactionClassLoader) {
/**
* Check the transaction is contract-valid by running the verify() for each input and output state contract.
* If any contract fails to verify, the whole transaction is considered to be invalid.
*
* Note: Reference states are not verified.
*/
open fun verifyContracts() {
override fun verifyContracts() {
try {
ContractVerifier(transactionClassLoader).apply(ltx)
} catch (e: TransactionVerificationException.ContractRejection) {
@ -359,9 +371,6 @@ open class Verifier(val ltx: LedgerTransaction, protected val transactionClassLo
}
}
/**
* Placeholder function so that the [Verifier] can release any resources.
*/
override fun close() {}
}

View File

@ -123,7 +123,7 @@ private constructor(
serializedInputs = protect(serializedInputs),
serializedReferences = protect(serializedReferences),
isAttachmentTrusted = isAttachmentTrusted,
verifierFactory = ::Verifier
verifierFactory = ::BasicVerifier
)
}
}
@ -653,7 +653,7 @@ private constructor(
serializedInputs = null,
serializedReferences = null,
isAttachmentTrusted = { it.isUploaderTrusted() },
verifierFactory = ::Verifier
verifierFactory = ::BasicVerifier
)
@Deprecated("LedgerTransaction should not be created directly, use WireTransaction.toLedgerTransaction instead.")
@ -682,7 +682,7 @@ private constructor(
serializedInputs = null,
serializedReferences = null,
isAttachmentTrusted = { it.isUploaderTrusted() },
verifierFactory = ::Verifier
verifierFactory = ::BasicVerifier
)
@Deprecated("LedgerTransactions should not be created directly, use WireTransaction.toLedgerTransaction instead.")

View File

@ -2,8 +2,8 @@ package net.corda.node.internal
import net.corda.core.contracts.*
import net.corda.core.cordapp.CordappProvider
import net.corda.core.internal.BasicVerifier
import net.corda.core.internal.SerializedStateAndRef
import net.corda.core.internal.Verifier
import net.corda.core.node.NetworkParameters
import net.corda.core.node.ServicesForResolution
import net.corda.core.node.services.AttachmentStorage
@ -88,7 +88,7 @@ data class ServicesForResolutionImpl(
// Specialise the LedgerTransaction here so that
// contracts are verified inside the DJVM!
return ltx.specialise { tx, cl ->
(cl as? URLClassLoader)?.run { DeterministicVerifier(tx, cl, createSandbox(cordaSource, cl)) } ?: Verifier(tx, cl)
(cl as? URLClassLoader)?.run { DeterministicVerifier(tx, cl, createSandbox(cordaSource, cl)) } ?: BasicVerifier(tx, cl)
}
}

View File

@ -43,6 +43,7 @@ class DeterministicVerifier(
ExecutionSummary(result.costs),
this
)
logger.error("Error validating transaction ${ltx.id}.", sandboxEx)
throw DeterministicVerificationException(ltx.id, sandboxEx.message ?: "", sandboxEx)
}
}