mirror of
https://github.com/corda/corda.git
synced 2025-06-22 17:09:00 +00:00
CORDA-654 Remove key constants from NodeTestUtils (#2205)
* DUMMY_NOTARY was hiding in a couple more places
This commit is contained in:
@ -4,6 +4,8 @@ import net.corda.core.contracts.ContractState
|
||||
import net.corda.core.contracts.StateAndRef
|
||||
import net.corda.core.contracts.TransactionState
|
||||
import net.corda.core.crypto.SecureHash
|
||||
import net.corda.core.identity.Party
|
||||
import net.corda.core.internal.uncheckedCast
|
||||
import net.corda.core.transactions.TransactionBuilder
|
||||
import net.corda.core.transactions.WireTransaction
|
||||
import java.io.InputStream
|
||||
@ -14,7 +16,7 @@ import java.io.InputStream
|
||||
*/
|
||||
interface OutputStateLookup {
|
||||
/**
|
||||
* Retrieves an output previously defined by [TransactionDSLInterpreter._output] with a label passed in.
|
||||
* Retrieves an output previously defined by [TransactionDSLInterpreter.output] with a label passed in.
|
||||
* @param clazz The class object holding the type of the output state expected.
|
||||
* @param label The label of the to-be-retrieved output state.
|
||||
* @return The output [StateAndRef].
|
||||
@ -90,7 +92,7 @@ interface LedgerDSLInterpreter<out T : TransactionDSLInterpreter> : Verifies, Ou
|
||||
* @return The final [WireTransaction] of the built transaction.
|
||||
*/
|
||||
fun _transaction(transactionLabel: String?, transactionBuilder: TransactionBuilder,
|
||||
dsl: TransactionDSL<T>.() -> EnforceVerifyOrFail): WireTransaction
|
||||
dsl: T.() -> EnforceVerifyOrFail): WireTransaction
|
||||
|
||||
/**
|
||||
* Creates and adds a transaction to the ledger that will not be verified by [verifies].
|
||||
@ -100,13 +102,13 @@ interface LedgerDSLInterpreter<out T : TransactionDSLInterpreter> : Verifies, Ou
|
||||
* @return The final [WireTransaction] of the built transaction.
|
||||
*/
|
||||
fun _unverifiedTransaction(transactionLabel: String?, transactionBuilder: TransactionBuilder,
|
||||
dsl: TransactionDSL<T>.() -> Unit): WireTransaction
|
||||
dsl: T.() -> Unit): WireTransaction
|
||||
|
||||
/**
|
||||
* Creates a local scoped copy of the ledger.
|
||||
* @param dsl The ledger DSL to be interpreted using the copy.
|
||||
*/
|
||||
fun tweak(dsl: LedgerDSL<T, LedgerDSLInterpreter<T>>.() -> Unit)
|
||||
fun _tweak(dsl: LedgerDSLInterpreter<T>.() -> Unit)
|
||||
|
||||
/**
|
||||
* Adds an attachment to the ledger.
|
||||
@ -123,24 +125,27 @@ interface LedgerDSLInterpreter<out T : TransactionDSLInterpreter> : Verifies, Ou
|
||||
* functionality then first add your primitive to [LedgerDSLInterpreter] and then add the convenience defaults/extension
|
||||
* methods here.
|
||||
*/
|
||||
class LedgerDSL<out T : TransactionDSLInterpreter, out L : LedgerDSLInterpreter<T>>(val interpreter: L) :
|
||||
class LedgerDSL<out T : TransactionDSLInterpreter, out L : LedgerDSLInterpreter<T>>(val interpreter: L, private val notary: Party) :
|
||||
LedgerDSLInterpreter<TransactionDSLInterpreter> by interpreter {
|
||||
|
||||
/**
|
||||
* Creates and adds a transaction to the ledger.
|
||||
*/
|
||||
@JvmOverloads
|
||||
fun transaction(label: String? = null, transactionBuilder: TransactionBuilder = TransactionBuilder(notary = DUMMY_NOTARY),
|
||||
fun transaction(label: String? = null, transactionBuilder: TransactionBuilder = TransactionBuilder(notary = notary),
|
||||
dsl: TransactionDSL<TransactionDSLInterpreter>.() -> EnforceVerifyOrFail) =
|
||||
_transaction(label, transactionBuilder, dsl)
|
||||
_transaction(label, transactionBuilder) { TransactionDSL(this, notary).dsl() }
|
||||
|
||||
/**
|
||||
* Creates and adds a transaction to the ledger that will not be verified by [verifies].
|
||||
*/
|
||||
@JvmOverloads
|
||||
fun unverifiedTransaction(label: String? = null, transactionBuilder: TransactionBuilder = TransactionBuilder(notary = DUMMY_NOTARY),
|
||||
fun unverifiedTransaction(label: String? = null, transactionBuilder: TransactionBuilder = TransactionBuilder(notary = notary),
|
||||
dsl: TransactionDSL<TransactionDSLInterpreter>.() -> Unit) =
|
||||
_unverifiedTransaction(label, transactionBuilder, dsl)
|
||||
_unverifiedTransaction(label, transactionBuilder) { TransactionDSL(this, notary).dsl() }
|
||||
|
||||
/** Creates a local scoped copy of the ledger. */
|
||||
fun tweak(dsl: LedgerDSL<T, L>.() -> Unit) = _tweak { LedgerDSL<T, L>(uncheckedCast(this), notary).dsl() }
|
||||
|
||||
/**
|
||||
* Retrieves an output previously defined by [TransactionDSLInterpreter._output] with a label passed in.
|
||||
|
@ -133,9 +133,7 @@ data class TestTransactionDSLInterpreter private constructor(
|
||||
transactionBuilder.setTimeWindow(data)
|
||||
}
|
||||
|
||||
override fun tweak(
|
||||
dsl: TransactionDSL<TransactionDSLInterpreter>.() -> EnforceVerifyOrFail
|
||||
) = dsl(TransactionDSL(copy()))
|
||||
override fun _tweak(dsl: TransactionDSLInterpreter.() -> EnforceVerifyOrFail) = copy().dsl()
|
||||
|
||||
override fun _attachment(contractClassName: ContractClassName) {
|
||||
(services.cordappProvider as MockCordappProvider).addMockCordapp(contractClassName, services.attachments as MockAttachmentStorage)
|
||||
@ -205,11 +203,9 @@ data class TestLedgerDSLInterpreter private constructor(
|
||||
|
||||
private fun <R> interpretTransactionDsl(
|
||||
transactionBuilder: TransactionBuilder,
|
||||
dsl: TransactionDSL<TestTransactionDSLInterpreter>.() -> R
|
||||
dsl: TestTransactionDSLInterpreter.() -> R
|
||||
): TestTransactionDSLInterpreter {
|
||||
val transactionInterpreter = TestTransactionDSLInterpreter(this, transactionBuilder)
|
||||
dsl(TransactionDSL(transactionInterpreter))
|
||||
return transactionInterpreter
|
||||
return TestTransactionDSLInterpreter(this, transactionBuilder).apply { dsl() }
|
||||
}
|
||||
|
||||
fun transactionName(transactionHash: SecureHash): String? {
|
||||
@ -227,7 +223,7 @@ data class TestLedgerDSLInterpreter private constructor(
|
||||
private fun <R> recordTransactionWithTransactionMap(
|
||||
transactionLabel: String?,
|
||||
transactionBuilder: TransactionBuilder,
|
||||
dsl: TransactionDSL<TestTransactionDSLInterpreter>.() -> R,
|
||||
dsl: TestTransactionDSLInterpreter.() -> R,
|
||||
transactionMap: HashMap<SecureHash, WireTransactionWithLocation> = HashMap(),
|
||||
/** If set to true, will add dummy components to [transactionBuilder] to make it valid. */
|
||||
fillTransaction: Boolean = false
|
||||
@ -267,19 +263,17 @@ data class TestLedgerDSLInterpreter private constructor(
|
||||
override fun _transaction(
|
||||
transactionLabel: String?,
|
||||
transactionBuilder: TransactionBuilder,
|
||||
dsl: TransactionDSL<TestTransactionDSLInterpreter>.() -> EnforceVerifyOrFail
|
||||
dsl: TestTransactionDSLInterpreter.() -> EnforceVerifyOrFail
|
||||
) = recordTransactionWithTransactionMap(transactionLabel, transactionBuilder, dsl, transactionWithLocations)
|
||||
|
||||
override fun _unverifiedTransaction(
|
||||
transactionLabel: String?,
|
||||
transactionBuilder: TransactionBuilder,
|
||||
dsl: TransactionDSL<TestTransactionDSLInterpreter>.() -> Unit
|
||||
dsl: TestTransactionDSLInterpreter.() -> Unit
|
||||
) = recordTransactionWithTransactionMap(transactionLabel, transactionBuilder, dsl, nonVerifiedTransactionWithLocations, fillTransaction = true)
|
||||
|
||||
override fun tweak(
|
||||
dsl: LedgerDSL<TestTransactionDSLInterpreter,
|
||||
LedgerDSLInterpreter<TestTransactionDSLInterpreter>>.() -> Unit) =
|
||||
dsl(LedgerDSL(copy()))
|
||||
override fun _tweak(dsl: LedgerDSLInterpreter<TestTransactionDSLInterpreter>.() -> Unit) =
|
||||
copy().dsl()
|
||||
|
||||
override fun attachment(attachment: InputStream): SecureHash {
|
||||
return services.attachments.importAttachment(attachment)
|
||||
|
@ -65,7 +65,7 @@ interface TransactionDSLInterpreter : Verifies, OutputStateLookup {
|
||||
* Creates a local scoped copy of the transaction.
|
||||
* @param dsl The transaction DSL to be interpreted using the copy.
|
||||
*/
|
||||
fun tweak(dsl: TransactionDSL<TransactionDSLInterpreter>.() -> EnforceVerifyOrFail): EnforceVerifyOrFail
|
||||
fun _tweak(dsl: TransactionDSLInterpreter.() -> EnforceVerifyOrFail): EnforceVerifyOrFail
|
||||
|
||||
/**
|
||||
* Attaches an attachment containing the named contract to the transaction
|
||||
@ -74,7 +74,7 @@ interface TransactionDSLInterpreter : Verifies, OutputStateLookup {
|
||||
fun _attachment(contractClassName: ContractClassName)
|
||||
}
|
||||
|
||||
class TransactionDSL<out T : TransactionDSLInterpreter>(interpreter: T) : TransactionDSLInterpreter by interpreter {
|
||||
class TransactionDSL<out T : TransactionDSLInterpreter>(interpreter: T, private val notary: Party) : TransactionDSLInterpreter by interpreter {
|
||||
/**
|
||||
* Looks up the output label and adds the found state as an input.
|
||||
* @param stateLabel The label of the output state specified when calling [TransactionDSLInterpreter.output] and friends.
|
||||
@ -87,7 +87,7 @@ class TransactionDSL<out T : TransactionDSLInterpreter>(interpreter: T) : Transa
|
||||
* @param state The state to be added.
|
||||
*/
|
||||
fun input(contractClassName: ContractClassName, state: ContractState) {
|
||||
val transaction = ledgerInterpreter._unverifiedTransaction(null, TransactionBuilder(notary = DUMMY_NOTARY)) {
|
||||
val transaction = ledgerInterpreter._unverifiedTransaction(null, TransactionBuilder(notary)) {
|
||||
output(contractClassName, null, DUMMY_NOTARY, null, AlwaysAcceptAttachmentConstraint, state)
|
||||
}
|
||||
input(transaction.outRef<ContractState>(0).ref)
|
||||
@ -103,13 +103,13 @@ class TransactionDSL<out T : TransactionDSLInterpreter>(interpreter: T) : Transa
|
||||
* Adds a labelled output to the transaction.
|
||||
*/
|
||||
fun output(contractClassName: ContractClassName, label: String, encumbrance: Int, contractState: ContractState) =
|
||||
output(contractClassName, label, DUMMY_NOTARY, encumbrance, AutomaticHashConstraint, contractState)
|
||||
output(contractClassName, label, notary, encumbrance, AutomaticHashConstraint, contractState)
|
||||
|
||||
/**
|
||||
* Adds a labelled output to the transaction.
|
||||
*/
|
||||
fun output(contractClassName: ContractClassName, label: String, contractState: ContractState) =
|
||||
output(contractClassName, label, DUMMY_NOTARY, null, AutomaticHashConstraint, contractState)
|
||||
output(contractClassName, label, notary, null, AutomaticHashConstraint, contractState)
|
||||
|
||||
/**
|
||||
* Adds an output to the transaction.
|
||||
@ -121,13 +121,13 @@ class TransactionDSL<out T : TransactionDSLInterpreter>(interpreter: T) : Transa
|
||||
* Adds an output to the transaction.
|
||||
*/
|
||||
fun output(contractClassName: ContractClassName, encumbrance: Int, contractState: ContractState) =
|
||||
output(contractClassName, null, DUMMY_NOTARY, encumbrance, AutomaticHashConstraint, contractState)
|
||||
output(contractClassName, null, notary, encumbrance, AutomaticHashConstraint, contractState)
|
||||
|
||||
/**
|
||||
* Adds an output to the transaction.
|
||||
*/
|
||||
fun output(contractClassName: ContractClassName, contractState: ContractState) =
|
||||
output(contractClassName, null, DUMMY_NOTARY, null, AutomaticHashConstraint, contractState)
|
||||
output(contractClassName, null, notary, null, AutomaticHashConstraint, contractState)
|
||||
|
||||
/**
|
||||
* Adds a command to the transaction.
|
||||
@ -143,6 +143,10 @@ class TransactionDSL<out T : TransactionDSLInterpreter>(interpreter: T) : Transa
|
||||
fun timeWindow(time: Instant, tolerance: Duration = 30.seconds) =
|
||||
timeWindow(TimeWindow.withTolerance(time, tolerance))
|
||||
|
||||
/** Creates a local scoped copy of the transaction. */
|
||||
fun tweak(dsl: TransactionDSL<TransactionDSLInterpreter>.() -> EnforceVerifyOrFail) =
|
||||
_tweak { TransactionDSL(this, notary).dsl() }
|
||||
|
||||
/**
|
||||
* @see TransactionDSLInterpreter._attachment
|
||||
*/
|
||||
|
Reference in New Issue
Block a user