mirror of
https://github.com/corda/corda.git
synced 2024-12-27 08:22:35 +00:00
Merge remote-tracking branch 'open/master'
This commit is contained in:
commit
4f85026dfc
@ -22,7 +22,7 @@ buildscript {
|
||||
ext.capsule_version = '1.0.1'
|
||||
|
||||
ext.asm_version = '0.5.3'
|
||||
ext.artemis_version = '1.5.3'
|
||||
ext.artemis_version = '2.1.0'
|
||||
ext.jackson_version = '2.8.5'
|
||||
ext.jetty_version = '9.3.9.v20160517'
|
||||
ext.jersey_version = '2.25'
|
||||
@ -44,7 +44,7 @@ buildscript {
|
||||
ext.hibernate_version = '5.2.6.Final'
|
||||
ext.h2_version = '1.4.194'
|
||||
ext.rxjava_version = '1.2.4'
|
||||
ext.requery_version = '1.2.1'
|
||||
ext.requery_version = '1.3.1'
|
||||
ext.dokka_version = '0.9.14'
|
||||
ext.eddsa_version = '0.2.0'
|
||||
|
||||
|
@ -5,7 +5,6 @@ import com.esotericsoftware.kryo.Serializer
|
||||
import com.esotericsoftware.kryo.io.Input
|
||||
import com.esotericsoftware.kryo.io.Output
|
||||
import com.esotericsoftware.kryo.pool.KryoPool
|
||||
import com.google.common.base.Stopwatch
|
||||
import com.google.common.net.HostAndPort
|
||||
import com.google.common.util.concurrent.Futures
|
||||
import net.corda.client.rpc.internal.RPCClient
|
||||
@ -17,7 +16,6 @@ import net.corda.node.services.messaging.RPCServerConfiguration
|
||||
import net.corda.nodeapi.RPCApi
|
||||
import net.corda.nodeapi.RPCKryo
|
||||
import net.corda.testing.*
|
||||
import org.apache.activemq.artemis.ArtemisConstants
|
||||
import org.apache.activemq.artemis.api.core.SimpleString
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
@ -28,8 +26,6 @@ import rx.subjects.UnicastSubject
|
||||
import java.time.Duration
|
||||
import java.util.concurrent.*
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import kotlin.concurrent.thread
|
||||
import kotlin.test.fail
|
||||
|
||||
class RPCStabilityTests {
|
||||
|
||||
@ -218,65 +214,27 @@ class RPCStabilityTests {
|
||||
|
||||
@Test
|
||||
fun `client reconnects to rebooted server`() {
|
||||
// TODO: Remove multiple trials when we fix the Artemis bug (which should have its own test(s)).
|
||||
if (ArtemisConstants::class.java.`package`.implementationVersion == "1.5.3") {
|
||||
// The test fails maybe 1 in 100 times, so to stay green until we upgrade Artemis, retry if it fails:
|
||||
for (i in (1..3)) {
|
||||
try {
|
||||
`client reconnects to rebooted server`(1)
|
||||
} catch (e: TimeoutException) {
|
||||
continue
|
||||
}
|
||||
return
|
||||
}
|
||||
fail("Test failed 3 times, which is vanishingly unlikely unless something has changed.")
|
||||
} else {
|
||||
// We've upgraded Artemis so make the test fail reliably, in the 2.1.0 case that takes 25 trials:
|
||||
`client reconnects to rebooted server`(25)
|
||||
}
|
||||
}
|
||||
|
||||
private fun `client reconnects to rebooted server`(trials: Int) {
|
||||
rpcDriver {
|
||||
val coreBurner = thread {
|
||||
while (!Thread.interrupted()) {
|
||||
// Spin.
|
||||
}
|
||||
val ops = object : ReconnectOps {
|
||||
override val protocolVersion = 0
|
||||
override fun ping() = "pong"
|
||||
}
|
||||
try {
|
||||
val ops = object : ReconnectOps {
|
||||
override val protocolVersion = 0
|
||||
override fun ping() = "pong"
|
||||
}
|
||||
var serverFollower = shutdownManager.follower()
|
||||
val serverPort = startRpcServer<ReconnectOps>(ops = ops).getOrThrow().broker.hostAndPort!!
|
||||
serverFollower.unfollow()
|
||||
val clientFollower = shutdownManager.follower()
|
||||
val client = startRpcClient<ReconnectOps>(serverPort).getOrThrow()
|
||||
clientFollower.unfollow()
|
||||
assertEquals("pong", client.ping())
|
||||
val background = Executors.newSingleThreadExecutor()
|
||||
(1..trials).forEach {
|
||||
System.err.println("Start trial $it of $trials.")
|
||||
serverFollower.shutdown()
|
||||
serverFollower = shutdownManager.follower()
|
||||
startRpcServer<ReconnectOps>(ops = ops, customPort = serverPort).getOrThrow()
|
||||
serverFollower.unfollow()
|
||||
val stopwatch = Stopwatch.createStarted()
|
||||
val pingFuture = background.submit(Callable {
|
||||
client.ping() // Would also hang in foreground, we need it in background so we can timeout.
|
||||
})
|
||||
assertEquals("pong", pingFuture.getOrThrow(10.seconds))
|
||||
System.err.println("Took ${stopwatch.elapsed(TimeUnit.MILLISECONDS)} millis.")
|
||||
}
|
||||
background.shutdown() // No point in the hanging case.
|
||||
clientFollower.shutdown() // Driver would do this after the current server, causing 'legit' failover hang.
|
||||
} finally {
|
||||
with(coreBurner) {
|
||||
interrupt()
|
||||
join()
|
||||
}
|
||||
val serverFollower = shutdownManager.follower()
|
||||
val serverPort = startRpcServer<ReconnectOps>(ops = ops).getOrThrow().broker.hostAndPort!!
|
||||
serverFollower.unfollow()
|
||||
// Set retry interval to 1s to reduce test duration
|
||||
val clientConfiguration = RPCClientConfiguration.default.copy(connectionRetryInterval = 1.seconds)
|
||||
val clientFollower = shutdownManager.follower()
|
||||
val client = startRpcClient<ReconnectOps>(serverPort, configuration = clientConfiguration).getOrThrow()
|
||||
clientFollower.unfollow()
|
||||
assertEquals("pong", client.ping())
|
||||
serverFollower.shutdown()
|
||||
startRpcServer<ReconnectOps>(ops = ops, customPort = serverPort).getOrThrow()
|
||||
val pingFuture = future {
|
||||
client.ping()
|
||||
}
|
||||
assertEquals("pong", pingFuture.getOrThrow(10.seconds))
|
||||
clientFollower.shutdown() // Driver would do this after the new server, causing hang.
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,22 +4,26 @@ import com.google.common.hash.Hashing
|
||||
import com.google.common.hash.HashingInputStream
|
||||
import net.corda.client.rpc.CordaRPCConnection
|
||||
import net.corda.client.rpc.notUsed
|
||||
import net.corda.contracts.asset.Cash
|
||||
import net.corda.core.contracts.DOLLARS
|
||||
import net.corda.core.contracts.POUNDS
|
||||
import net.corda.core.contracts.SWISS_FRANCS
|
||||
import net.corda.core.crypto.SecureHash
|
||||
import net.corda.core.getOrThrow
|
||||
import net.corda.core.identity.Party
|
||||
import net.corda.core.messaging.CordaRPCOps
|
||||
import net.corda.core.messaging.StateMachineUpdate
|
||||
import net.corda.core.messaging.startFlow
|
||||
import net.corda.core.messaging.startTrackedFlow
|
||||
import net.corda.core.messaging.*
|
||||
import net.corda.core.node.services.Vault
|
||||
import net.corda.core.node.services.vault.PageSpecification
|
||||
import net.corda.core.node.services.vault.QueryCriteria
|
||||
import net.corda.core.node.services.vault.Sort
|
||||
import net.corda.core.node.services.vault.SortAttribute
|
||||
import net.corda.core.seconds
|
||||
import net.corda.core.serialization.OpaqueBytes
|
||||
import net.corda.core.sizedInputStreamAndHash
|
||||
import net.corda.core.utilities.DUMMY_NOTARY
|
||||
import net.corda.core.utilities.loggerFor
|
||||
import net.corda.flows.CashIssueFlow
|
||||
import net.corda.flows.CashPaymentFlow
|
||||
import net.corda.nodeapi.User
|
||||
import net.corda.smoketesting.NodeConfig
|
||||
import net.corda.smoketesting.NodeProcess
|
||||
@ -156,6 +160,54 @@ class StandaloneCordaRPClientTest {
|
||||
assertEquals(629.POUNDS, cashBalance[Currency.getInstance("GBP")])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test vault track by`() {
|
||||
val (vault, vaultUpdates) = rpcProxy.vaultTrackBy<Cash.State>()
|
||||
assertEquals(0, vault.totalStatesAvailable)
|
||||
|
||||
var updateCount = 0
|
||||
vaultUpdates.subscribe { update ->
|
||||
log.info("Vault>> FlowId=${update.flowId}")
|
||||
++updateCount
|
||||
}
|
||||
|
||||
// Now issue some cash
|
||||
rpcProxy.startFlow(::CashIssueFlow, 629.POUNDS, OpaqueBytes.of(0), notaryIdentity, notaryIdentity)
|
||||
.returnValue.getOrThrow(timeout)
|
||||
assertNotEquals(0, updateCount)
|
||||
|
||||
// Check that this cash exists in the vault
|
||||
val cashBalance = rpcProxy.getCashBalances()
|
||||
log.info("Cash Balances: $cashBalance")
|
||||
assertEquals(1, cashBalance.size)
|
||||
assertEquals(629.POUNDS, cashBalance[Currency.getInstance("GBP")])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test vault query by`() {
|
||||
// Now issue some cash
|
||||
rpcProxy.startFlow(::CashIssueFlow, 629.POUNDS, OpaqueBytes.of(0), notaryIdentity, notaryIdentity)
|
||||
.returnValue.getOrThrow(timeout)
|
||||
|
||||
val criteria = QueryCriteria.VaultQueryCriteria(status = Vault.StateStatus.ALL)
|
||||
val paging = PageSpecification(0, 10)
|
||||
val sorting = Sort(setOf(Sort.SortColumn(SortAttribute.Standard(Sort.VaultStateAttribute.RECORDED_TIME), Sort.Direction.DESC)))
|
||||
|
||||
val queryResults = rpcProxy.vaultQueryBy<Cash.State>(criteria, paging, sorting)
|
||||
assertEquals(1, queryResults.totalStatesAvailable)
|
||||
assertEquals(queryResults.states.first().state.data.amount.quantity, 629.POUNDS.quantity)
|
||||
|
||||
rpcProxy.startFlow(::CashPaymentFlow, 100.POUNDS, notaryIdentity).returnValue.getOrThrow()
|
||||
|
||||
val moreResults = rpcProxy.vaultQueryBy<Cash.State>(criteria, paging, sorting)
|
||||
assertEquals(3, moreResults.totalStatesAvailable) // 629 - 100 + 100
|
||||
|
||||
// Check that this cash exists in the vault
|
||||
val cashBalance = rpcProxy.getCashBalances()
|
||||
log.info("Cash Balances: $cashBalance")
|
||||
assertEquals(1, cashBalance.size)
|
||||
assertEquals(629.POUNDS, cashBalance[Currency.getInstance("GBP")])
|
||||
}
|
||||
|
||||
private fun fetchNotaryIdentity(): Party {
|
||||
val (nodeInfo, nodeUpdates) = rpcProxy.networkMapUpdates()
|
||||
|
@ -13,7 +13,7 @@
|
||||
|
||||
<Appenders>
|
||||
<Console name="Console-Appender" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%highlight{%level{length=1} %d{HH:mm:ss} [%t] %c{2}.%M - %msg%n}{INFO=white,WARN=red,FATAL=bright red}" />
|
||||
<PatternLayout pattern="%highlight{%level{length=1} %date{HH:mm:ss} [%t] %c{2}.%method - %msg%n}{INFO=white,WARN=red,FATAL=bright red}" />
|
||||
</Console>
|
||||
|
||||
<!-- Required for printBasicInfo -->
|
||||
@ -25,9 +25,9 @@
|
||||
those that are older than 60 days, but keep the most recent 10 GB -->
|
||||
<RollingFile name="RollingFile-Appender"
|
||||
fileName="${sys:log-path}/${log-name}.log"
|
||||
filePattern="${archive}/${log-name}.%d{yyyy-MM-dd}-%i.log.gz">
|
||||
filePattern="${archive}/${log-name}.%date{yyyy-MM-dd}-%i.log.gz">
|
||||
|
||||
<PatternLayout pattern="[%-5level] %d{ISO8601}{GMT+0} [%t] %c{2}.%M - %msg%n"/>
|
||||
<PatternLayout pattern="[%-5level] %date{ISO8601}{GMT+0} [%t] %c{2}.%method - %msg%n"/>
|
||||
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy/>
|
||||
|
@ -5,7 +5,7 @@
|
||||
</Properties>
|
||||
<Appenders>
|
||||
<Console name="Console-Appender" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="[%-5level] %d{HH:mm:ss.SSS} [%t] %c{2}.%M - %msg%n" />
|
||||
<PatternLayout pattern="[%-5level] %date{HH:mm:ss.SSS} [%t] %c{2}.%method - %msg%n" />
|
||||
</Console>
|
||||
<!-- Required for printBasicInfo -->
|
||||
<Console name="Console-Appender-Println" target="SYSTEM_OUT">
|
||||
|
@ -1,4 +1,5 @@
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'kotlin-jpa'
|
||||
apply plugin: 'net.corda.plugins.quasar-utils'
|
||||
apply plugin: 'net.corda.plugins.publish-utils'
|
||||
|
||||
|
@ -0,0 +1,60 @@
|
||||
package net.corda.core.contracts
|
||||
|
||||
import net.corda.core.contracts.clauses.Clause
|
||||
import net.corda.core.contracts.clauses.FilterOn
|
||||
import net.corda.core.contracts.clauses.verifyClause
|
||||
import net.corda.core.crypto.SecureHash
|
||||
import net.corda.core.crypto.containsAny
|
||||
import net.corda.core.identity.AbstractParty
|
||||
import net.corda.core.schemas.*
|
||||
import java.security.PublicKey
|
||||
import java.time.Instant
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneOffset
|
||||
|
||||
class DummyLinearContract : Contract {
|
||||
override val legalContractReference: SecureHash = SecureHash.sha256("Test")
|
||||
|
||||
val clause: Clause<State, CommandData, Unit> = LinearState.ClauseVerifier()
|
||||
override fun verify(tx: TransactionForContract) = verifyClause(tx,
|
||||
FilterOn(clause, { states -> states.filterIsInstance<State>() }),
|
||||
emptyList())
|
||||
|
||||
data class State(
|
||||
override val linearId: UniqueIdentifier = UniqueIdentifier(),
|
||||
override val contract: Contract = DummyLinearContract(),
|
||||
override val participants: List<AbstractParty> = listOf(),
|
||||
val linearString: String = "ABC",
|
||||
val linearNumber: Long = 123L,
|
||||
val linearTimestamp: Instant = LocalDateTime.now().toInstant(ZoneOffset.UTC),
|
||||
val linearBoolean: Boolean = true,
|
||||
val nonce: SecureHash = SecureHash.randomSHA256()) : LinearState, QueryableState {
|
||||
|
||||
override fun isRelevant(ourKeys: Set<PublicKey>): Boolean {
|
||||
return participants.any { it.owningKey.containsAny(ourKeys) }
|
||||
}
|
||||
|
||||
override fun supportedSchemas(): Iterable<MappedSchema> = listOf(DummyLinearStateSchemaV1, DummyLinearStateSchemaV2)
|
||||
|
||||
override fun generateMappedObject(schema: MappedSchema): PersistentState {
|
||||
return when (schema) {
|
||||
is DummyLinearStateSchemaV1 -> DummyLinearStateSchemaV1.PersistentDummyLinearState(
|
||||
externalId = linearId.externalId,
|
||||
uuid = linearId.id,
|
||||
linearString = linearString,
|
||||
linearNumber = linearNumber,
|
||||
linearTimestamp = linearTimestamp,
|
||||
linearBoolean = linearBoolean
|
||||
)
|
||||
is DummyLinearStateSchemaV2 -> DummyLinearStateSchemaV2.PersistentDummyLinearState(
|
||||
uid = linearId,
|
||||
linearString = linearString,
|
||||
linearNumber = linearNumber,
|
||||
linearTimestamp = linearTimestamp,
|
||||
linearBoolean = linearBoolean
|
||||
)
|
||||
else -> throw IllegalArgumentException("Unrecognised schema $schema")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -587,7 +587,7 @@ object Crypto {
|
||||
* @throws IllegalArgumentException if the requested signature scheme is not supported.
|
||||
* @throws UnsupportedOperationException if deterministic key generation is not supported for this particular scheme.
|
||||
*/
|
||||
fun deterministicKeyPair(signatureScheme: SignatureScheme, privateKey: PrivateKey, seed: ByteArray): KeyPair {
|
||||
fun deriveKeyPair(signatureScheme: SignatureScheme, privateKey: PrivateKey, seed: ByteArray): KeyPair {
|
||||
require(isSupportedSignatureScheme(signatureScheme)) { "Unsupported key/algorithm for schemeCodeName: ${signatureScheme.schemeCodeName}" }
|
||||
when (signatureScheme) {
|
||||
ECDSA_SECP256R1_SHA256, ECDSA_SECP256K1_SHA256 -> return deriveKeyPairECDSA(signatureScheme.algSpec as ECParameterSpec, privateKey, seed)
|
||||
@ -605,8 +605,8 @@ object Crypto {
|
||||
* @throws IllegalArgumentException if the requested signature scheme is not supported.
|
||||
* @throws UnsupportedOperationException if deterministic key generation is not supported for this particular scheme.
|
||||
*/
|
||||
fun deterministicKeyPair(privateKey: PrivateKey, seed: ByteArray): KeyPair {
|
||||
return deterministicKeyPair(findSignatureScheme(privateKey), privateKey, seed)
|
||||
fun deriveKeyPair(privateKey: PrivateKey, seed: ByteArray): KeyPair {
|
||||
return deriveKeyPair(findSignatureScheme(privateKey), privateKey, seed)
|
||||
}
|
||||
|
||||
// Given the domain parameters, this routine deterministically generates an ECDSA key pair
|
||||
|
@ -83,11 +83,31 @@ interface CordaRPCOps : RPCOps {
|
||||
* It is the responsibility of the Client to request further pages and/or specify a more suitable [PageSpecification].
|
||||
*/
|
||||
// DOCSTART VaultQueryByAPI
|
||||
fun <T : ContractState> vaultQueryBy(criteria: QueryCriteria = QueryCriteria.VaultQueryCriteria(),
|
||||
paging: PageSpecification = PageSpecification(),
|
||||
sorting: Sort = Sort(emptySet())): Vault.Page<T>
|
||||
@RPCReturnsObservables
|
||||
fun <T : ContractState> vaultQueryBy(criteria: QueryCriteria,
|
||||
paging: PageSpecification,
|
||||
sorting: Sort,
|
||||
contractType: Class<out T>): Vault.Page<T>
|
||||
// DOCEND VaultQueryByAPI
|
||||
|
||||
// Note: cannot apply @JvmOverloads to interfaces nor interface implementations
|
||||
// Java Helpers
|
||||
|
||||
// DOCSTART VaultQueryAPIHelpers
|
||||
fun <T : ContractState> vaultQuery(contractType: Class<out T>): Vault.Page<T> {
|
||||
return vaultQueryBy(QueryCriteria.VaultQueryCriteria(), PageSpecification(), Sort(emptySet()), contractType)
|
||||
}
|
||||
fun <T : ContractState> vaultQueryByCriteria(criteria: QueryCriteria, contractType: Class<out T>): Vault.Page<T> {
|
||||
return vaultQueryBy(criteria, PageSpecification(), Sort(emptySet()), contractType)
|
||||
}
|
||||
fun <T : ContractState> vaultQueryByWithPagingSpec(contractType: Class<out T>, criteria: QueryCriteria, paging: PageSpecification): Vault.Page<T> {
|
||||
return vaultQueryBy(criteria, paging, Sort(emptySet()), contractType)
|
||||
}
|
||||
fun <T : ContractState> vaultQueryByWithSorting(contractType: Class<out T>, criteria: QueryCriteria, sorting: Sort): Vault.Page<T> {
|
||||
return vaultQueryBy(criteria, PageSpecification(), sorting, contractType)
|
||||
}
|
||||
// DOCEND VaultQueryAPIHelpers
|
||||
|
||||
/**
|
||||
* Returns a snapshot (as per queryBy) and an observable of future updates to the vault for the given query criteria.
|
||||
*
|
||||
@ -102,30 +122,36 @@ interface CordaRPCOps : RPCOps {
|
||||
*/
|
||||
// DOCSTART VaultTrackByAPI
|
||||
@RPCReturnsObservables
|
||||
fun <T : ContractState> vaultTrackBy(criteria: QueryCriteria = QueryCriteria.VaultQueryCriteria(),
|
||||
paging: PageSpecification = PageSpecification(),
|
||||
sorting: Sort = Sort(emptySet())): Vault.PageAndUpdates<T>
|
||||
fun <T : ContractState> vaultTrackBy(criteria: QueryCriteria,
|
||||
paging: PageSpecification,
|
||||
sorting: Sort,
|
||||
contractType: Class<out T>): Vault.PageAndUpdates<T>
|
||||
// DOCEND VaultTrackByAPI
|
||||
|
||||
// Note: cannot apply @JvmOverloads to interfaces nor interface implementations
|
||||
// Java Helpers
|
||||
|
||||
// DOCSTART VaultQueryAPIJavaHelpers
|
||||
fun <T : ContractState> vaultQueryByCriteria(criteria: QueryCriteria): Vault.Page<T> = vaultQueryBy(criteria = criteria)
|
||||
fun <T : ContractState> vaultQueryByWithPagingSpec(criteria: QueryCriteria, paging: PageSpecification): Vault.Page<T> = vaultQueryBy(criteria, paging = paging)
|
||||
fun <T : ContractState> vaultQueryByWithSorting(criteria: QueryCriteria, sorting: Sort): Vault.Page<T> = vaultQueryBy(criteria, sorting = sorting)
|
||||
|
||||
fun <T : ContractState> vaultTrackByCriteria(criteria: QueryCriteria): Vault.PageAndUpdates<T> = vaultTrackBy(criteria = criteria)
|
||||
fun <T : ContractState> vaultTrackByWithPagingSpec(criteria: QueryCriteria, paging: PageSpecification): Vault.PageAndUpdates<T> = vaultTrackBy(criteria, paging = paging)
|
||||
fun <T : ContractState> vaultTrackByWithSorting(criteria: QueryCriteria, sorting: Sort): Vault.PageAndUpdates<T> = vaultTrackBy(criteria, sorting = sorting)
|
||||
// DOCEND VaultQueryAPIJavaHelpers
|
||||
// DOCSTART VaultTrackAPIHelpers
|
||||
fun <T : ContractState> vaultTrack(contractType: Class<out T>): Vault.PageAndUpdates<T> {
|
||||
return vaultTrackBy(QueryCriteria.VaultQueryCriteria(), PageSpecification(), Sort(emptySet()), contractType)
|
||||
}
|
||||
fun <T : ContractState> vaultTrackByCriteria(contractType: Class<out T>, criteria: QueryCriteria): Vault.PageAndUpdates<T> {
|
||||
return vaultTrackBy(criteria, PageSpecification(), Sort(emptySet()), contractType)
|
||||
}
|
||||
fun <T : ContractState> vaultTrackByWithPagingSpec(contractType: Class<out T>, criteria: QueryCriteria, paging: PageSpecification): Vault.PageAndUpdates<T> {
|
||||
return vaultTrackBy(criteria, paging, Sort(emptySet()), contractType)
|
||||
}
|
||||
fun <T : ContractState> vaultTrackByWithSorting(contractType: Class<out T>, criteria: QueryCriteria, sorting: Sort): Vault.PageAndUpdates<T> {
|
||||
return vaultTrackBy(criteria, PageSpecification(), sorting, contractType)
|
||||
}
|
||||
// DOCEND VaultTrackAPIHelpers
|
||||
|
||||
/**
|
||||
* Returns a pair of head states in the vault and an observable of future updates to the vault.
|
||||
*/
|
||||
@RPCReturnsObservables
|
||||
// TODO: Remove this from the interface
|
||||
// @Deprecated("This function will be removed in a future milestone", ReplaceWith("vaultTrackBy(QueryCriteria())"))
|
||||
@Deprecated("This function will be removed in a future milestone", ReplaceWith("vaultTrackBy(QueryCriteria())"))
|
||||
fun vaultAndUpdates(): Pair<List<StateAndRef<ContractState>>, Observable<Vault.Update>>
|
||||
|
||||
/**
|
||||
@ -258,6 +284,18 @@ interface CordaRPCOps : RPCOps {
|
||||
fun registeredFlows(): List<String>
|
||||
}
|
||||
|
||||
inline fun <reified T : ContractState> CordaRPCOps.vaultQueryBy(criteria: QueryCriteria = QueryCriteria.VaultQueryCriteria(),
|
||||
paging: PageSpecification = PageSpecification(),
|
||||
sorting: Sort = Sort(emptySet())): Vault.Page<T> {
|
||||
return vaultQueryBy(criteria, paging, sorting, T::class.java)
|
||||
}
|
||||
|
||||
inline fun <reified T : ContractState> CordaRPCOps.vaultTrackBy(criteria: QueryCriteria = QueryCriteria.VaultQueryCriteria(),
|
||||
paging: PageSpecification = PageSpecification(),
|
||||
sorting: Sort = Sort(emptySet())): Vault.PageAndUpdates<T> {
|
||||
return vaultTrackBy(criteria, paging, sorting, T::class.java)
|
||||
}
|
||||
|
||||
/**
|
||||
* These allow type safe invocations of flows from Kotlin, e.g.:
|
||||
*
|
||||
|
@ -1,8 +1,12 @@
|
||||
package net.corda.core.node
|
||||
|
||||
import net.corda.core.messaging.CordaRPCOps
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.core.serialization.SerializationCustomization
|
||||
import java.util.function.Function
|
||||
import net.corda.core.schemas.QueryableState
|
||||
import net.corda.core.contracts.ContractState
|
||||
import net.corda.core.node.services.VaultQueryService
|
||||
|
||||
/**
|
||||
* Implement this interface on a class advertised in a META-INF/services/net.corda.core.node.CordaPluginRegistry file
|
||||
@ -46,4 +50,13 @@ abstract class CordaPluginRegistry {
|
||||
* @return true if you register types, otherwise you will be filtered out of the list of plugins considered in future.
|
||||
*/
|
||||
open fun customizeSerialization(custom: SerializationCustomization): Boolean = false
|
||||
|
||||
/**
|
||||
* Optionally, custom schemas to be used for contract state persistence and vault custom querying
|
||||
*
|
||||
* For example, if you implement the [QueryableState] interface on a new [ContractState]
|
||||
* it needs to be registered here if you wish to perform custom queries on schema entity attributes using the
|
||||
* [VaultQueryService] API
|
||||
*/
|
||||
open val requiredSchemas: Set<MappedSchema> get() = emptySet()
|
||||
}
|
@ -38,6 +38,7 @@ interface ServicesForResolution {
|
||||
*/
|
||||
interface ServiceHub : ServicesForResolution {
|
||||
val vaultService: VaultService
|
||||
val vaultQueryService: VaultQueryService
|
||||
val keyManagementService: KeyManagementService
|
||||
override val storageService: StorageService
|
||||
val networkMapCache: NetworkMapCache
|
||||
|
@ -2,13 +2,11 @@ package net.corda.core.node.services
|
||||
|
||||
import net.corda.core.contracts.PartyAndReference
|
||||
import net.corda.core.identity.*
|
||||
import net.corda.core.node.NodeInfo
|
||||
import org.bouncycastle.asn1.x500.X500Name
|
||||
import org.bouncycastle.cert.X509CertificateHolder
|
||||
import java.security.InvalidAlgorithmParameterException
|
||||
import java.security.PublicKey
|
||||
import java.security.cert.CertPath
|
||||
import java.security.cert.CertificateExpiredException
|
||||
import java.security.cert.CertificateNotYetValidException
|
||||
import java.security.cert.*
|
||||
|
||||
/**
|
||||
* An identity service maintains a directory of parties by their associated distinguished name/public keys and thus
|
||||
@ -16,6 +14,10 @@ import java.security.cert.CertificateNotYetValidException
|
||||
* identities back to the well known identity (i.e. the identity in the network map) of a party.
|
||||
*/
|
||||
interface IdentityService {
|
||||
val trustRoot: X509Certificate
|
||||
val trustRootHolder: X509CertificateHolder
|
||||
val caCertStore: CertStore
|
||||
|
||||
/**
|
||||
* Verify and then store a well known identity.
|
||||
*
|
||||
|
@ -3,6 +3,7 @@ package net.corda.core.node.services
|
||||
import co.paralleluniverse.fibers.Suspendable
|
||||
import com.google.common.util.concurrent.ListenableFuture
|
||||
import net.corda.core.contracts.*
|
||||
import net.corda.core.node.services.vault.QueryCriteria
|
||||
import net.corda.core.crypto.CompositeKey
|
||||
import net.corda.core.crypto.DigitalSignature
|
||||
import net.corda.core.crypto.SecureHash
|
||||
@ -12,7 +13,6 @@ import net.corda.core.identity.AbstractParty
|
||||
import net.corda.core.identity.Party
|
||||
import net.corda.core.identity.PartyAndCertificate
|
||||
import net.corda.core.node.services.vault.PageSpecification
|
||||
import net.corda.core.node.services.vault.QueryCriteria
|
||||
import net.corda.core.node.services.vault.Sort
|
||||
import net.corda.core.serialization.CordaSerializable
|
||||
import net.corda.core.serialization.OpaqueBytes
|
||||
@ -22,6 +22,7 @@ import net.corda.core.transactions.TransactionBuilder
|
||||
import net.corda.core.transactions.WireTransaction
|
||||
import org.bouncycastle.cert.X509CertificateHolder
|
||||
import rx.Observable
|
||||
import rx.subjects.PublishSubject
|
||||
import java.io.InputStream
|
||||
import java.security.PublicKey
|
||||
import java.security.cert.CertPath
|
||||
@ -68,6 +69,15 @@ class Vault<out T : ContractState>(val states: Iterable<StateAndRef<T>>) {
|
||||
/** 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 }
|
||||
|
||||
/** Checks whether the update contains a state of the specified type and state status */
|
||||
fun <T : ContractState> containsType(clazz: Class<T>, status: StateStatus) =
|
||||
when(status) {
|
||||
StateStatus.UNCONSUMED -> produced.any { clazz.isAssignableFrom(it.state.data.javaClass) }
|
||||
StateStatus.CONSUMED -> consumed.any { clazz.isAssignableFrom(it.state.data.javaClass) }
|
||||
else -> consumed.any { clazz.isAssignableFrom(it.state.data.javaClass) }
|
||||
|| produced.any { clazz.isAssignableFrom(it.state.data.javaClass) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Combine two updates into a single update with the combined inputs and outputs of the two updates but net
|
||||
* any outputs of the left-hand-side (this) that are consumed by the inputs of the right-hand-side (rhs).
|
||||
@ -98,6 +108,7 @@ class Vault<out T : ContractState>(val states: Iterable<StateAndRef<T>>) {
|
||||
val NoUpdate = Update(emptySet(), emptySet())
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
enum class StateStatus {
|
||||
UNCONSUMED, CONSUMED, ALL
|
||||
}
|
||||
@ -114,9 +125,10 @@ class Vault<out T : ContractState>(val states: Iterable<StateAndRef<T>>) {
|
||||
*/
|
||||
@CordaSerializable
|
||||
data class Page<out T : ContractState>(val states: List<StateAndRef<T>>,
|
||||
val statesMetadata: List<Vault.StateMetadata>,
|
||||
val statesMetadata: List<StateMetadata>,
|
||||
val pageable: PageSpecification,
|
||||
val totalStatesAvailable: Long)
|
||||
val totalStatesAvailable: Int,
|
||||
val stateTypes: StateStatus)
|
||||
|
||||
@CordaSerializable
|
||||
data class StateMetadata(val ref: StateRef,
|
||||
@ -130,7 +142,7 @@ class Vault<out T : ContractState>(val states: Iterable<StateAndRef<T>>) {
|
||||
val lockUpdateTime: Instant?)
|
||||
|
||||
@CordaSerializable
|
||||
data class PageAndUpdates<out T : ContractState> (val current: Vault.Page<T>, val future: Observable<Vault.Update>? = null)
|
||||
data class PageAndUpdates<out T : ContractState> (val current: Vault.Page<T>, val future: Observable<Vault.Update>)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -160,6 +172,11 @@ interface VaultService {
|
||||
*/
|
||||
val updates: Observable<Vault.Update>
|
||||
|
||||
/**
|
||||
* Enable creation of observables of updates.
|
||||
*/
|
||||
val updatesPublisher: PublishSubject<Vault.Update>
|
||||
|
||||
/**
|
||||
* Returns a map of how much cash we have in each currency, ignoring details like issuer. Note: currencies for
|
||||
* which we have no cash evaluate to null (not present in map), not 0.
|
||||
@ -171,57 +188,14 @@ interface VaultService {
|
||||
* first subscriber is registered so as to avoid racing with early updates.
|
||||
*/
|
||||
// TODO: Remove this from the interface
|
||||
// @Deprecated("This function will be removed in a future milestone", ReplaceWith("trackBy(QueryCriteria())"))
|
||||
@Deprecated("This function will be removed in a future milestone", ReplaceWith("trackBy(QueryCriteria())"))
|
||||
fun track(): Pair<Vault<ContractState>, Observable<Vault.Update>>
|
||||
|
||||
// DOCSTART VaultQueryAPI
|
||||
/**
|
||||
* Generic vault query function which takes a [QueryCriteria] object to define filters,
|
||||
* optional [PageSpecification] and optional [Sort] modification criteria (default unsorted),
|
||||
* and returns a [Vault.Page] object containing the following:
|
||||
* 1. states as a List of <StateAndRef> (page number and size defined by [PageSpecification])
|
||||
* 2. states metadata as a List of [Vault.StateMetadata] held in the Vault States table.
|
||||
* 3. the [PageSpecification] used in the query
|
||||
* 4. a total number of results available (for subsequent paging if necessary)
|
||||
*
|
||||
* Note: a default [PageSpecification] is applied to the query returning the 1st page (indexed from 0) with up to 200 entries.
|
||||
* It is the responsibility of the Client to request further pages and/or specify a more suitable [PageSpecification].
|
||||
*/
|
||||
fun <T : ContractState> queryBy(criteria: QueryCriteria = QueryCriteria.VaultQueryCriteria(),
|
||||
paging: PageSpecification = PageSpecification(),
|
||||
sorting: Sort = Sort(emptySet())): Vault.Page<T>
|
||||
/**
|
||||
* Generic vault query function which takes a [QueryCriteria] object to define filters,
|
||||
* optional [PageSpecification] and optional [Sort] modification criteria (default unsorted),
|
||||
* and returns a [Vault.PageAndUpdates] object containing
|
||||
* 1) a snapshot as a [Vault.Page] (described previously in [queryBy])
|
||||
* 2) an [Observable] of [Vault.Update]
|
||||
*
|
||||
* Notes: the snapshot part of the query adheres to the same behaviour as the [queryBy] function.
|
||||
* the [QueryCriteria] applies to both snapshot and deltas (streaming updates).
|
||||
*/
|
||||
fun <T : ContractState> trackBy(criteria: QueryCriteria = QueryCriteria.VaultQueryCriteria(),
|
||||
paging: PageSpecification = PageSpecification(),
|
||||
sorting: Sort = Sort(emptySet())): Vault.PageAndUpdates<T>
|
||||
// DOCEND VaultQueryAPI
|
||||
|
||||
// Note: cannot apply @JvmOverloads to interfaces nor interface implementations
|
||||
// Java Helpers
|
||||
fun <T : ContractState> queryBy(): Vault.Page<T> = queryBy()
|
||||
fun <T : ContractState> queryBy(criteria: QueryCriteria): Vault.Page<T> = queryBy(criteria)
|
||||
fun <T : ContractState> queryBy(criteria: QueryCriteria, paging: PageSpecification): Vault.Page<T> = queryBy(criteria, paging)
|
||||
fun <T : ContractState> queryBy(criteria: QueryCriteria, sorting: Sort): Vault.Page<T> = queryBy(criteria, sorting)
|
||||
|
||||
fun <T : ContractState> trackBy(): Vault.PageAndUpdates<T> = trackBy()
|
||||
fun <T : ContractState> trackBy(criteria: QueryCriteria): Vault.PageAndUpdates<T> = trackBy(criteria)
|
||||
fun <T : ContractState> trackBy(criteria: QueryCriteria, paging: PageSpecification): Vault.PageAndUpdates<T> = trackBy(criteria, paging)
|
||||
fun <T : ContractState> trackBy(criteria: QueryCriteria, sorting: Sort): Vault.PageAndUpdates<T> = trackBy(criteria, Sort(emptySet()))
|
||||
|
||||
/**
|
||||
* Return unconsumed [ContractState]s for a given set of [StateRef]s
|
||||
*/
|
||||
// TODO: Remove this from the interface
|
||||
// @Deprecated("This function will be removed in a future milestone", ReplaceWith("queryBy(VaultQueryCriteria(stateRefs = listOf(<StateRef>)))"))
|
||||
@Deprecated("This function will be removed in a future milestone", ReplaceWith("queryBy(VaultQueryCriteria(stateRefs = listOf(<StateRef>)))"))
|
||||
fun statesForRefs(refs: List<StateRef>): Map<StateRef, TransactionState<*>?>
|
||||
|
||||
/**
|
||||
@ -300,7 +274,7 @@ interface VaultService {
|
||||
* Optionally may specify whether to include [StateRef] that have been marked as soft locked (default is true)
|
||||
*/
|
||||
// TODO: Remove this from the interface
|
||||
// @Deprecated("This function will be removed in a future milestone", ReplaceWith("queryBy(QueryCriteria())"))
|
||||
@Deprecated("This function will be removed in a future milestone", ReplaceWith("queryBy(QueryCriteria())"))
|
||||
fun <T : ContractState> states(clazzes: Set<Class<T>>, statuses: EnumSet<Vault.StateStatus>, includeSoftLockedStates: Boolean = true): Iterable<StateAndRef<T>>
|
||||
// DOCEND VaultStatesQuery
|
||||
|
||||
@ -347,18 +321,18 @@ interface VaultService {
|
||||
}
|
||||
|
||||
// TODO: Remove this from the interface
|
||||
// @Deprecated("This function will be removed in a future milestone", ReplaceWith("queryBy(VaultQueryCriteria())"))
|
||||
@Deprecated("This function will be removed in a future milestone", ReplaceWith("queryBy(VaultQueryCriteria())"))
|
||||
inline fun <reified T : ContractState> VaultService.unconsumedStates(includeSoftLockedStates: Boolean = true): Iterable<StateAndRef<T>> =
|
||||
states(setOf(T::class.java), EnumSet.of(Vault.StateStatus.UNCONSUMED), includeSoftLockedStates)
|
||||
|
||||
// TODO: Remove this from the interface
|
||||
// @Deprecated("This function will be removed in a future milestone", ReplaceWith("queryBy(VaultQueryCriteria(status = Vault.StateStatus.CONSUMED))"))
|
||||
@Deprecated("This function will be removed in a future milestone", ReplaceWith("queryBy(VaultQueryCriteria(status = Vault.StateStatus.CONSUMED))"))
|
||||
inline fun <reified T : ContractState> VaultService.consumedStates(): Iterable<StateAndRef<T>> =
|
||||
states(setOf(T::class.java), EnumSet.of(Vault.StateStatus.CONSUMED))
|
||||
|
||||
/** Returns the [linearState] heads only when the type of the state would be considered an 'instanceof' the given type. */
|
||||
// TODO: Remove this from the interface
|
||||
// @Deprecated("This function will be removed in a future milestone", ReplaceWith("queryBy(LinearStateQueryCriteria(linearId = listOf(<UniqueIdentifier>)))"))
|
||||
@Deprecated("This function will be removed in a future milestone", ReplaceWith("queryBy(LinearStateQueryCriteria(linearId = listOf(<UniqueIdentifier>)))"))
|
||||
inline fun <reified T : LinearState> VaultService.linearHeadsOfType() =
|
||||
states(setOf(T::class.java), EnumSet.of(Vault.StateStatus.UNCONSUMED))
|
||||
.associateBy { it.state.data.linearId }.mapValues { it.value }
|
||||
@ -367,6 +341,126 @@ class StatesNotAvailableException(override val message: String?, override val ca
|
||||
override fun toString() = "Soft locking error: $message"
|
||||
}
|
||||
|
||||
interface VaultQueryService {
|
||||
|
||||
// DOCSTART VaultQueryAPI
|
||||
/**
|
||||
* Generic vault query function which takes a [QueryCriteria] object to define filters,
|
||||
* optional [PageSpecification] and optional [Sort] modification criteria (default unsorted),
|
||||
* and returns a [Vault.Page] object containing the following:
|
||||
* 1. states as a List of <StateAndRef> (page number and size defined by [PageSpecification])
|
||||
* 2. states metadata as a List of [Vault.StateMetadata] held in the Vault States table.
|
||||
* 3. the [PageSpecification] used in the query
|
||||
* 4. a total number of results available (for subsequent paging if necessary)
|
||||
*
|
||||
* @throws VaultQueryException if the query cannot be executed for any reason
|
||||
* (missing criteria or parsing error, invalid operator, unsupported query, underlying database error)
|
||||
*
|
||||
* Note: a default [PageSpecification] is applied to the query returning the 1st page (indexed from 0) with up to 200 entries.
|
||||
* It is the responsibility of the Client to request further pages and/or specify a more suitable [PageSpecification].
|
||||
* Note2: you can also annotate entity fields with JPA OrderBy annotation to achieve the same effect as explicit sorting
|
||||
*/
|
||||
@Throws(VaultQueryException::class)
|
||||
fun <T : ContractState> _queryBy(criteria: QueryCriteria,
|
||||
paging: PageSpecification,
|
||||
sorting: Sort,
|
||||
contractType: Class<out T>): Vault.Page<T>
|
||||
/**
|
||||
* Generic vault query function which takes a [QueryCriteria] object to define filters,
|
||||
* optional [PageSpecification] and optional [Sort] modification criteria (default unsorted),
|
||||
* and returns a [Vault.PageAndUpdates] object containing
|
||||
* 1) a snapshot as a [Vault.Page] (described previously in [queryBy])
|
||||
* 2) an [Observable] of [Vault.Update]
|
||||
*
|
||||
* @throws VaultQueryException if the query cannot be executed for any reason
|
||||
*
|
||||
* Notes: the snapshot part of the query adheres to the same behaviour as the [queryBy] function.
|
||||
* the [QueryCriteria] applies to both snapshot and deltas (streaming updates).
|
||||
*/
|
||||
@Throws(VaultQueryException::class)
|
||||
fun <T : ContractState> _trackBy(criteria: QueryCriteria,
|
||||
paging: PageSpecification,
|
||||
sorting: Sort,
|
||||
contractType: Class<out T>): Vault.PageAndUpdates<T>
|
||||
// DOCEND VaultQueryAPI
|
||||
|
||||
// Note: cannot apply @JvmOverloads to interfaces nor interface implementations
|
||||
// Java Helpers
|
||||
fun <T : ContractState> queryBy(contractType: Class<out T>): Vault.Page<T> {
|
||||
return _queryBy(QueryCriteria.VaultQueryCriteria(), PageSpecification(), Sort(emptySet()), contractType)
|
||||
}
|
||||
fun <T : ContractState> queryBy(contractType: Class<out T>, criteria: QueryCriteria): Vault.Page<T> {
|
||||
return _queryBy(criteria, PageSpecification(), Sort(emptySet()), contractType)
|
||||
}
|
||||
fun <T : ContractState> queryBy(contractType: Class<out T>, criteria: QueryCriteria, paging: PageSpecification): Vault.Page<T> {
|
||||
return _queryBy(criteria, paging, Sort(emptySet()), contractType)
|
||||
}
|
||||
fun <T : ContractState> queryBy(contractType: Class<out T>, criteria: QueryCriteria, sorting: Sort): Vault.Page<T> {
|
||||
return _queryBy(criteria, PageSpecification(), sorting, contractType)
|
||||
}
|
||||
fun <T : ContractState> queryBy(contractType: Class<out T>, criteria: QueryCriteria, paging: PageSpecification, sorting: Sort): Vault.Page<T> {
|
||||
return _queryBy(criteria, paging, sorting, contractType)
|
||||
}
|
||||
|
||||
fun <T : ContractState> trackBy(contractType: Class<out T>): Vault.Page<T> {
|
||||
return _queryBy(QueryCriteria.VaultQueryCriteria(), PageSpecification(), Sort(emptySet()), contractType)
|
||||
}
|
||||
fun <T : ContractState> trackBy(contractType: Class<out T>, criteria: QueryCriteria): Vault.PageAndUpdates<T> {
|
||||
return _trackBy(criteria, PageSpecification(), Sort(emptySet()), contractType)
|
||||
}
|
||||
fun <T : ContractState> trackBy(contractType: Class<out T>, criteria: QueryCriteria, paging: PageSpecification): Vault.PageAndUpdates<T> {
|
||||
return _trackBy(criteria, paging, Sort(emptySet()), contractType)
|
||||
}
|
||||
fun <T : ContractState> trackBy(contractType: Class<out T>, criteria: QueryCriteria, sorting: Sort): Vault.PageAndUpdates<T> {
|
||||
return _trackBy(criteria, PageSpecification(), sorting, contractType)
|
||||
}
|
||||
fun <T : ContractState> trackBy(contractType: Class<out T>, criteria: QueryCriteria, paging: PageSpecification, sorting: Sort): Vault.PageAndUpdates<T> {
|
||||
return _trackBy(criteria, paging, sorting, contractType)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T : ContractState> VaultQueryService.queryBy(): Vault.Page<T> {
|
||||
return _queryBy(QueryCriteria.VaultQueryCriteria(), PageSpecification(), Sort(emptySet()), T::class.java)
|
||||
}
|
||||
|
||||
inline fun <reified T : ContractState> VaultQueryService.queryBy(criteria: QueryCriteria): Vault.Page<T> {
|
||||
return _queryBy(criteria, PageSpecification(), Sort(emptySet()), T::class.java)
|
||||
}
|
||||
|
||||
inline fun <reified T : ContractState> VaultQueryService.queryBy(criteria: QueryCriteria, paging: PageSpecification): Vault.Page<T> {
|
||||
return _queryBy(criteria, paging, Sort(emptySet()), T::class.java)
|
||||
}
|
||||
|
||||
inline fun <reified T : ContractState> VaultQueryService.queryBy(criteria: QueryCriteria, sorting: Sort): Vault.Page<T> {
|
||||
return _queryBy(criteria, PageSpecification(), sorting, T::class.java)
|
||||
}
|
||||
|
||||
inline fun <reified T : ContractState> VaultQueryService.queryBy(criteria: QueryCriteria, paging: PageSpecification, sorting: Sort): Vault.Page<T> {
|
||||
return _queryBy(criteria, paging, sorting, T::class.java)
|
||||
}
|
||||
|
||||
inline fun <reified T : ContractState> VaultQueryService.trackBy(): Vault.PageAndUpdates<T> {
|
||||
return _trackBy(QueryCriteria.VaultQueryCriteria(), PageSpecification(), Sort(emptySet()), T::class.java)
|
||||
}
|
||||
|
||||
inline fun <reified T : ContractState> VaultQueryService.trackBy(criteria: QueryCriteria): Vault.PageAndUpdates<T> {
|
||||
return _trackBy(criteria, PageSpecification(), Sort(emptySet()), T::class.java)
|
||||
}
|
||||
|
||||
inline fun <reified T : ContractState> VaultQueryService.trackBy(criteria: QueryCriteria, paging: PageSpecification): Vault.PageAndUpdates<T> {
|
||||
return _trackBy(criteria, paging, Sort(emptySet()), T::class.java)
|
||||
}
|
||||
|
||||
inline fun <reified T : ContractState> VaultQueryService.trackBy(criteria: QueryCriteria, sorting: Sort): Vault.PageAndUpdates<T> {
|
||||
return _trackBy(criteria, PageSpecification(), sorting, T::class.java)
|
||||
}
|
||||
|
||||
inline fun <reified T : ContractState> VaultQueryService.trackBy(criteria: QueryCriteria, paging: PageSpecification, sorting: Sort): Vault.PageAndUpdates<T> {
|
||||
return _trackBy(criteria, paging, sorting, T::class.java)
|
||||
}
|
||||
|
||||
class VaultQueryException(description: String) : FlowException("$description")
|
||||
|
||||
/**
|
||||
* The KMS is responsible for storing and using private keys to sign things. An implementation of this may, for example,
|
||||
* call out to a hardware security module that enforces various auditing and frequency-of-use requirements.
|
||||
|
@ -3,14 +3,17 @@ package net.corda.core.node.services.vault
|
||||
import net.corda.core.contracts.ContractState
|
||||
import net.corda.core.contracts.StateRef
|
||||
import net.corda.core.contracts.UniqueIdentifier
|
||||
import net.corda.core.identity.AbstractParty
|
||||
import net.corda.core.node.services.Vault
|
||||
import net.corda.core.node.services.vault.QueryCriteria.AndComposition
|
||||
import net.corda.core.node.services.vault.QueryCriteria.OrComposition
|
||||
import net.corda.core.schemas.PersistentState
|
||||
import net.corda.core.serialization.CordaSerializable
|
||||
import net.corda.core.serialization.OpaqueBytes
|
||||
import org.bouncycastle.asn1.x500.X500Name
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
import javax.persistence.criteria.Predicate
|
||||
|
||||
/**
|
||||
* Indexing assumptions:
|
||||
@ -18,27 +21,39 @@ import java.util.*
|
||||
*/
|
||||
@CordaSerializable
|
||||
sealed class QueryCriteria {
|
||||
abstract fun visit(parser: IQueryCriteriaParser): Collection<Predicate>
|
||||
|
||||
@CordaSerializable
|
||||
data class TimeCondition(val type: TimeInstantType, val predicate: ColumnPredicate<Instant>)
|
||||
|
||||
/**
|
||||
* VaultQueryCriteria: provides query by attributes defined in [VaultSchema.VaultStates]
|
||||
*/
|
||||
data class VaultQueryCriteria @JvmOverloads constructor (
|
||||
val status: Vault.StateStatus = Vault.StateStatus.UNCONSUMED,
|
||||
val stateRefs: List<StateRef>? = null,
|
||||
val contractStateTypes: Set<Class<out ContractState>>? = null,
|
||||
val stateRefs: List<StateRef>? = null,
|
||||
val notaryName: List<X500Name>? = null,
|
||||
val includeSoftlockedStates: Boolean? = true,
|
||||
val timeCondition: Logical<TimeInstantType, Array<Instant>>? = null,
|
||||
val participantIdentities: List<X500Name>? = null) : QueryCriteria()
|
||||
val includeSoftlockedStates: Boolean = true,
|
||||
val timeCondition: TimeCondition? = null) : QueryCriteria() {
|
||||
|
||||
override fun visit(parser: IQueryCriteriaParser): Collection<Predicate> {
|
||||
return parser.parseCriteria(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* LinearStateQueryCriteria: provides query by attributes defined in [VaultSchema.VaultLinearState]
|
||||
*/
|
||||
data class LinearStateQueryCriteria @JvmOverloads constructor(
|
||||
val participants: List<AbstractParty>? = null,
|
||||
val linearId: List<UniqueIdentifier>? = null,
|
||||
val latestOnly: Boolean? = true,
|
||||
val dealRef: List<String>? = null,
|
||||
val dealPartyName: List<X500Name>? = null) : QueryCriteria()
|
||||
val dealRef: List<String>? = null) : QueryCriteria() {
|
||||
|
||||
override fun visit(parser: IQueryCriteriaParser): Collection<Predicate> {
|
||||
return parser.parseCriteria(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* FungibleStateQueryCriteria: provides query by attributes defined in [VaultSchema.VaultFungibleState]
|
||||
@ -48,13 +63,16 @@ sealed class QueryCriteria {
|
||||
* [Commodity] as used in [CommodityContract] state
|
||||
*/
|
||||
data class FungibleAssetQueryCriteria @JvmOverloads constructor(
|
||||
val ownerIdentity: List<X500Name>? = null,
|
||||
val quantity: Logical<*,Long>? = null,
|
||||
val tokenType: List<Class<out Any>>? = null,
|
||||
val tokenValue: List<String>? = null,
|
||||
val issuerPartyName: List<X500Name>? = null,
|
||||
val issuerRef: List<OpaqueBytes>? = null,
|
||||
val exitKeyIdentity: List<String>? = null) : QueryCriteria()
|
||||
val participants: List<AbstractParty>? = null,
|
||||
val owner: List<AbstractParty>? = null,
|
||||
val quantity: ColumnPredicate<Long>? = null,
|
||||
val issuerPartyName: List<AbstractParty>? = null,
|
||||
val issuerRef: List<OpaqueBytes>? = null) : QueryCriteria() {
|
||||
|
||||
override fun visit(parser: IQueryCriteriaParser): Collection<Predicate> {
|
||||
return parser.parseCriteria(this)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* VaultCustomQueryCriteria: provides query by custom attributes defined in a contracts
|
||||
@ -62,16 +80,28 @@ sealed class QueryCriteria {
|
||||
* (see Persistence documentation for more information)
|
||||
*
|
||||
* Params
|
||||
* [indexExpression] refers to a (composable) JPA Query like WHERE expression clauses of the form:
|
||||
* [JPA entityAttributeName] [Operand] [Value]
|
||||
* [expression] refers to a (composable) type safe [CriteriaExpression]
|
||||
*
|
||||
* Refer to [CommercialPaper.State] for a concrete example.
|
||||
*/
|
||||
data class VaultCustomQueryCriteria<L,R>(val indexExpression: Logical<L,R>? = null) : QueryCriteria()
|
||||
data class VaultCustomQueryCriteria<L : PersistentState>(val expression: CriteriaExpression<L, Boolean>) : QueryCriteria() {
|
||||
override fun visit(parser: IQueryCriteriaParser): Collection<Predicate> {
|
||||
return parser.parseCriteria(this)
|
||||
}
|
||||
}
|
||||
|
||||
// enable composition of [QueryCriteria]
|
||||
data class AndComposition(val a: QueryCriteria, val b: QueryCriteria): QueryCriteria()
|
||||
data class OrComposition(val a: QueryCriteria, val b: QueryCriteria): QueryCriteria()
|
||||
data class AndComposition(val a: QueryCriteria, val b: QueryCriteria): QueryCriteria() {
|
||||
override fun visit(parser: IQueryCriteriaParser): Collection<Predicate> {
|
||||
return parser.parseAnd(this.a, this.b)
|
||||
}
|
||||
}
|
||||
|
||||
data class OrComposition(val a: QueryCriteria, val b: QueryCriteria): QueryCriteria() {
|
||||
override fun visit(parser: IQueryCriteriaParser): Collection<Predicate> {
|
||||
return parser.parseOr(this.a, this.b)
|
||||
}
|
||||
}
|
||||
|
||||
// timestamps stored in the vault states table [VaultSchema.VaultStates]
|
||||
@CordaSerializable
|
||||
@ -81,5 +111,15 @@ sealed class QueryCriteria {
|
||||
}
|
||||
}
|
||||
|
||||
interface IQueryCriteriaParser {
|
||||
fun parseCriteria(criteria: QueryCriteria.FungibleAssetQueryCriteria): Collection<Predicate>
|
||||
fun parseCriteria(criteria: QueryCriteria.LinearStateQueryCriteria): Collection<Predicate>
|
||||
fun <L: PersistentState> parseCriteria(criteria: QueryCriteria.VaultCustomQueryCriteria<L>): Collection<Predicate>
|
||||
fun parseCriteria(criteria: QueryCriteria.VaultQueryCriteria): Collection<Predicate>
|
||||
fun parseOr(left: QueryCriteria, right: QueryCriteria): Collection<Predicate>
|
||||
fun parseAnd(left: QueryCriteria, right: QueryCriteria): Collection<Predicate>
|
||||
fun parse(criteria: QueryCriteria, sorting: Sort? = null) : Collection<Predicate>
|
||||
}
|
||||
|
||||
infix fun QueryCriteria.and(criteria: QueryCriteria): QueryCriteria = AndComposition(this, criteria)
|
||||
infix fun QueryCriteria.or(criteria: QueryCriteria): QueryCriteria = OrComposition(this, criteria)
|
||||
|
@ -1,61 +1,93 @@
|
||||
package net.corda.core.node.services.vault
|
||||
|
||||
import net.corda.core.schemas.PersistentState
|
||||
import net.corda.core.serialization.CordaSerializable
|
||||
import java.lang.reflect.Field
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.javaField
|
||||
|
||||
@CordaSerializable
|
||||
enum class Operator {
|
||||
enum class BinaryLogicalOperator {
|
||||
AND,
|
||||
OR,
|
||||
OR
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
enum class EqualityComparisonOperator {
|
||||
EQUAL,
|
||||
NOT_EQUAL,
|
||||
NOT_EQUAL
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
enum class BinaryComparisonOperator {
|
||||
LESS_THAN,
|
||||
LESS_THAN_OR_EQUAL,
|
||||
GREATER_THAN,
|
||||
GREATER_THAN_OR_EQUAL,
|
||||
IN,
|
||||
NOT_IN,
|
||||
LIKE,
|
||||
NOT_LIKE,
|
||||
BETWEEN,
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
enum class NullOperator {
|
||||
IS_NULL,
|
||||
NOT_NULL
|
||||
}
|
||||
|
||||
interface Condition<L, R> {
|
||||
val leftOperand: L
|
||||
val operator: Operator
|
||||
val rightOperand: R
|
||||
}
|
||||
|
||||
interface AndOr<out Q> {
|
||||
infix fun <V> and(condition: Condition<V, *>): Q
|
||||
infix fun <V> or(condition: Condition<V, *>): Q
|
||||
@CordaSerializable
|
||||
enum class LikenessOperator {
|
||||
LIKE,
|
||||
NOT_LIKE
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
sealed class Logical<L, R> : Condition<L, R>, AndOr<Logical<*, *>>
|
||||
|
||||
class LogicalExpression<L, R>(leftOperand: L,
|
||||
operator: Operator,
|
||||
rightOperand: R? = null) : Logical<L, R>() {
|
||||
init {
|
||||
if (rightOperand == null) {
|
||||
check(operator in setOf(Operator.NOT_NULL, Operator.IS_NULL),
|
||||
{ "Must use a unary operator (${Operator.IS_NULL}, ${Operator.NOT_NULL}) if right operand is null"} )
|
||||
}
|
||||
else {
|
||||
check(operator !in setOf(Operator.NOT_NULL, Operator.IS_NULL),
|
||||
{ "Cannot use a unary operator (${Operator.IS_NULL}, ${Operator.NOT_NULL}) if right operand is not null"} )
|
||||
}
|
||||
}
|
||||
override fun <V> and(condition: Condition<V, *>): Logical<*, *> = LogicalExpression(this, Operator.AND, condition)
|
||||
override fun <V> or(condition: Condition<V, *>): Logical<*, *> = LogicalExpression(this, Operator.OR, condition)
|
||||
|
||||
override val operator: Operator = operator
|
||||
override val rightOperand: R = rightOperand as R
|
||||
override val leftOperand: L = leftOperand
|
||||
enum class CollectionOperator {
|
||||
IN,
|
||||
NOT_IN
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
sealed class CriteriaExpression<O, out T> {
|
||||
data class BinaryLogical<O>(val left: CriteriaExpression<O, Boolean>, val right: CriteriaExpression<O, Boolean>, val operator: BinaryLogicalOperator) : CriteriaExpression<O, Boolean>()
|
||||
data class Not<O>(val expression: CriteriaExpression<O, Boolean>) : CriteriaExpression<O, Boolean>()
|
||||
data class ColumnPredicateExpression<O, C>(val column: Column<O, C>, val predicate: ColumnPredicate<C>) : CriteriaExpression<O, Boolean>()
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
sealed class Column<O, out C> {
|
||||
data class Java<O, out C>(val field: Field) : Column<O, C>()
|
||||
data class Kotlin<O, out C>(val property: KProperty1<O, C?>) : Column<O, C>()
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
sealed class ColumnPredicate<C> {
|
||||
data class EqualityComparison<C>(val operator: EqualityComparisonOperator, val rightLiteral: C) : ColumnPredicate<C>()
|
||||
data class BinaryComparison<C : Comparable<C>>(val operator: BinaryComparisonOperator, val rightLiteral: C) : ColumnPredicate<C>()
|
||||
data class Likeness(val operator: LikenessOperator, val rightLiteral: String) : ColumnPredicate<String>()
|
||||
data class CollectionExpression<C>(val operator: CollectionOperator, val rightLiteral: Collection<C>) : ColumnPredicate<C>()
|
||||
data class Between<C : Comparable<C>>(val rightFromLiteral: C, val rightToLiteral: C) : ColumnPredicate<C>()
|
||||
data class NullExpression<C>(val operator: NullOperator) : ColumnPredicate<C>()
|
||||
}
|
||||
|
||||
fun <O, R> resolveEnclosingObjectFromExpression(expression: CriteriaExpression<O, R>): Class<O> {
|
||||
return when (expression) {
|
||||
is CriteriaExpression.BinaryLogical -> resolveEnclosingObjectFromExpression(expression.left)
|
||||
is CriteriaExpression.Not -> resolveEnclosingObjectFromExpression(expression.expression)
|
||||
is CriteriaExpression.ColumnPredicateExpression<O, *> -> resolveEnclosingObjectFromColumn(expression.column)
|
||||
}
|
||||
}
|
||||
|
||||
fun <O, C> resolveEnclosingObjectFromColumn(column: Column<O, C>): Class<O> {
|
||||
return when (column) {
|
||||
is Column.Java -> column.field.declaringClass as Class<O>
|
||||
is Column.Kotlin -> column.property.javaField!!.declaringClass as Class<O>
|
||||
}
|
||||
}
|
||||
|
||||
fun <O, C> getColumnName(column: Column<O, C>): String {
|
||||
return when (column) {
|
||||
is Column.Java -> column.field.name
|
||||
is Column.Kotlin -> column.property.name
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pagination and Ordering
|
||||
@ -70,21 +102,21 @@ class LogicalExpression<L, R>(leftOperand: L,
|
||||
* paging and sorting capability:
|
||||
* https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/PagingAndSortingRepository.html
|
||||
*/
|
||||
val DEFAULT_PAGE_NUM = 0L
|
||||
val DEFAULT_PAGE_SIZE = 200L
|
||||
val DEFAULT_PAGE_NUM = 0
|
||||
val DEFAULT_PAGE_SIZE = 200
|
||||
|
||||
/**
|
||||
* Note: this maximum size will be configurable in future (to allow for large JVM heap sized node configurations)
|
||||
* Use [PageSpecification] to correctly handle a number of bounded pages of [MAX_PAGE_SIZE].
|
||||
*/
|
||||
val MAX_PAGE_SIZE = 512L
|
||||
val MAX_PAGE_SIZE = 512
|
||||
|
||||
/**
|
||||
* PageSpecification allows specification of a page number (starting from 0 as default) and page size (defaulting to
|
||||
* [DEFAULT_PAGE_SIZE] with a maximum page size of [DEFAULT_PAGE_SIZE]
|
||||
* [DEFAULT_PAGE_SIZE] with a maximum page size of [MAX_PAGE_SIZE]
|
||||
*/
|
||||
@CordaSerializable
|
||||
data class PageSpecification(val pageNumber: Long = DEFAULT_PAGE_NUM, val pageSize: Long = DEFAULT_PAGE_SIZE)
|
||||
data class PageSpecification(val pageNumber: Int = DEFAULT_PAGE_NUM, val pageSize: Int = DEFAULT_PAGE_SIZE)
|
||||
|
||||
/**
|
||||
* Sort allows specification of a set of entity attribute names and their associated directionality
|
||||
@ -97,17 +129,106 @@ data class Sort(val columns: Collection<SortColumn>) {
|
||||
ASC,
|
||||
DESC
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
enum class NullHandling {
|
||||
NULLS_FIRST,
|
||||
NULLS_LAST
|
||||
interface Attribute
|
||||
|
||||
enum class VaultStateAttribute(val columnName: String) : Attribute {
|
||||
/** Vault States */
|
||||
NOTARY_NAME("notaryName"),
|
||||
CONTRACT_TYPE("contractStateClassName"),
|
||||
STATE_STATUS("stateStatus"),
|
||||
RECORDED_TIME("recordedTime"),
|
||||
CONSUMED_TIME("consumedTime"),
|
||||
LOCK_ID("lockId"),
|
||||
}
|
||||
/**
|
||||
* [columnName] should reference a persisted entity attribute name as defined by the associated mapped schema
|
||||
* (for example, [VaultSchema.VaultStates::txId.name])
|
||||
*/
|
||||
|
||||
enum class LinearStateAttribute(val columnName: String) : Attribute {
|
||||
/** Vault Linear States */
|
||||
UUID("uuid"),
|
||||
EXTERNAL_ID("externalId"),
|
||||
DEAL_REFERENCE("dealReference"),
|
||||
}
|
||||
|
||||
enum class FungibleStateAttribute(val columnName: String) : Attribute {
|
||||
/** Vault Fungible States */
|
||||
QUANTITY("quantity"),
|
||||
ISSUER_REF("issuerRef")
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
data class SortColumn(val columnName: String, val direction: Sort.Direction = Sort.Direction.ASC,
|
||||
val nullHandling: Sort.NullHandling = if (direction == Sort.Direction.ASC) Sort.NullHandling.NULLS_LAST else Sort.NullHandling.NULLS_FIRST)
|
||||
data class SortColumn(
|
||||
val sortAttribute: SortAttribute,
|
||||
val direction: Sort.Direction = Sort.Direction.ASC)
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
sealed class SortAttribute {
|
||||
/**
|
||||
* [sortAttribute] refers to common table attributes defined in the node schemas:
|
||||
* VaultState, VaultLinearStates, VaultFungibleStates
|
||||
*/
|
||||
data class Standard(val attribute: Sort.Attribute) : SortAttribute()
|
||||
|
||||
/**
|
||||
* [entityStateClass] should reference a persistent state entity
|
||||
* [entityStateColumnName] should reference an entity attribute name as defined by the associated mapped schema
|
||||
* (for example, [CashSchemaV1.PersistentCashState::currency.name])
|
||||
*/
|
||||
data class Custom(val entityStateClass: Class<out PersistentState>,
|
||||
val entityStateColumnName: String) : SortAttribute()
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
object Builder {
|
||||
|
||||
fun <R : Comparable<R>> compare(operator: BinaryComparisonOperator, value: R) = ColumnPredicate.BinaryComparison(operator, value)
|
||||
|
||||
fun <O, R> KProperty1<O, R?>.predicate(predicate: ColumnPredicate<R>) = CriteriaExpression.ColumnPredicateExpression(Column.Kotlin(this), predicate)
|
||||
fun <R> Field.predicate(predicate: ColumnPredicate<R>) = CriteriaExpression.ColumnPredicateExpression(Column.Java<Any, R>(this), predicate)
|
||||
|
||||
fun <O, R : Comparable<R>> KProperty1<O, R?>.comparePredicate(operator: BinaryComparisonOperator, value: R) = predicate(compare(operator, value))
|
||||
fun <R : Comparable<R>> Field.comparePredicate(operator: BinaryComparisonOperator, value: R) = predicate(compare(operator, value))
|
||||
|
||||
fun <O, R> KProperty1<O, R?>.equal(value: R) = predicate(ColumnPredicate.EqualityComparison(EqualityComparisonOperator.EQUAL, value))
|
||||
fun <O, R> KProperty1<O, R?>.notEqual(value: R) = predicate(ColumnPredicate.EqualityComparison(EqualityComparisonOperator.NOT_EQUAL, value))
|
||||
fun <O, R : Comparable<R>> KProperty1<O, R?>.lessThan(value: R) = comparePredicate(BinaryComparisonOperator.LESS_THAN, value)
|
||||
fun <O, R : Comparable<R>> KProperty1<O, R?>.lessThanOrEqual(value: R) = comparePredicate(BinaryComparisonOperator.LESS_THAN_OR_EQUAL, value)
|
||||
fun <O, R : Comparable<R>> KProperty1<O, R?>.greaterThan(value: R) = comparePredicate(BinaryComparisonOperator.GREATER_THAN, value)
|
||||
fun <O, R : Comparable<R>> KProperty1<O, R?>.greaterThanOrEqual(value: R) = comparePredicate(BinaryComparisonOperator.GREATER_THAN_OR_EQUAL, value)
|
||||
fun <O, R : Comparable<R>> KProperty1<O, R?>.between(from: R, to: R) = predicate(ColumnPredicate.Between(from, to))
|
||||
fun <O, R : Comparable<R>> KProperty1<O, R?>.`in`(collection: Collection<R>) = predicate(ColumnPredicate.CollectionExpression(CollectionOperator.IN, collection))
|
||||
fun <O, R : Comparable<R>> KProperty1<O, R?>.notIn(collection: Collection<R>) = predicate(ColumnPredicate.CollectionExpression(CollectionOperator.NOT_IN, collection))
|
||||
|
||||
fun <R> Field.equal(value: R) = predicate(ColumnPredicate.EqualityComparison(EqualityComparisonOperator.EQUAL, value))
|
||||
fun <R> Field.notEqual(value: R) = predicate(ColumnPredicate.EqualityComparison(EqualityComparisonOperator.NOT_EQUAL, value))
|
||||
fun <R : Comparable<R>> Field.lessThan(value: R) = comparePredicate(BinaryComparisonOperator.LESS_THAN, value)
|
||||
fun <R : Comparable<R>> Field.lessThanOrEqual(value: R) = comparePredicate(BinaryComparisonOperator.LESS_THAN_OR_EQUAL, value)
|
||||
fun <R : Comparable<R>> Field.greaterThan(value: R) = comparePredicate(BinaryComparisonOperator.GREATER_THAN, value)
|
||||
fun <R : Comparable<R>> Field.greaterThanOrEqual(value: R) = comparePredicate(BinaryComparisonOperator.GREATER_THAN_OR_EQUAL, value)
|
||||
fun <R : Comparable<R>> Field.between(from: R, to: R) = predicate(ColumnPredicate.Between(from, to))
|
||||
fun <R : Comparable<R>> Field.`in`(collection: Collection<R>) = predicate(ColumnPredicate.CollectionExpression(CollectionOperator.IN, collection))
|
||||
fun <R : Comparable<R>> Field.notIn(collection: Collection<R>) = predicate(ColumnPredicate.CollectionExpression(CollectionOperator.NOT_IN, collection))
|
||||
|
||||
fun <R> equal(value: R) = ColumnPredicate.EqualityComparison(EqualityComparisonOperator.EQUAL, value)
|
||||
fun <R> notEqual(value: R) = ColumnPredicate.EqualityComparison(EqualityComparisonOperator.NOT_EQUAL, value)
|
||||
fun <R : Comparable<R>> lessThan(value: R) = compare(BinaryComparisonOperator.LESS_THAN, value)
|
||||
fun <R : Comparable<R>> lessThanOrEqual(value: R) = compare(BinaryComparisonOperator.LESS_THAN_OR_EQUAL, value)
|
||||
fun <R : Comparable<R>> greaterThan(value: R) = compare(BinaryComparisonOperator.GREATER_THAN, value)
|
||||
fun <R : Comparable<R>> greaterThanOrEqual(value: R) = compare(BinaryComparisonOperator.GREATER_THAN_OR_EQUAL, value)
|
||||
fun <R : Comparable<R>> between(from: R, to: R) = ColumnPredicate.Between(from, to)
|
||||
fun <R : Comparable<R>> `in`(collection: Collection<R>) = ColumnPredicate.CollectionExpression(CollectionOperator.IN, collection)
|
||||
fun <R : Comparable<R>> notIn(collection: Collection<R>) = ColumnPredicate.CollectionExpression(CollectionOperator.NOT_IN, collection)
|
||||
|
||||
fun <O> KProperty1<O, String?>.like(string: String) = predicate(ColumnPredicate.Likeness(LikenessOperator.LIKE, string))
|
||||
fun Field.like(string: String) = predicate(ColumnPredicate.Likeness(LikenessOperator.LIKE, string))
|
||||
fun <O> KProperty1<O, String?>.notLike(string: String) = predicate(ColumnPredicate.Likeness(LikenessOperator.NOT_LIKE, string))
|
||||
fun Field.notLike(string: String) = predicate(ColumnPredicate.Likeness(LikenessOperator.NOT_LIKE, string))
|
||||
|
||||
fun <O, R> KProperty1<O, R?>.isNull() = predicate(ColumnPredicate.NullExpression(NullOperator.IS_NULL))
|
||||
fun Field.isNull() = predicate(ColumnPredicate.NullExpression<Any>(NullOperator.IS_NULL))
|
||||
fun <O, R> KProperty1<O, R?>.notNull() = predicate(ColumnPredicate.NullExpression(NullOperator.NOT_NULL))
|
||||
fun Field.notNull() = predicate(ColumnPredicate.NullExpression<Any>(NullOperator.NOT_NULL))
|
||||
}
|
||||
|
||||
inline fun <A> builder(block: Builder.() -> A) = block(Builder)
|
||||
|
96
core/src/main/kotlin/net/corda/core/schemas/CommonSchema.kt
Normal file
96
core/src/main/kotlin/net/corda/core/schemas/CommonSchema.kt
Normal file
@ -0,0 +1,96 @@
|
||||
package net.corda.node.services.vault.schemas.jpa
|
||||
|
||||
import net.corda.core.contracts.UniqueIdentifier
|
||||
import net.corda.core.crypto.toBase58String
|
||||
import net.corda.core.identity.AbstractParty
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.core.schemas.PersistentState
|
||||
import net.corda.core.schemas.StatePersistable
|
||||
import java.util.*
|
||||
import javax.persistence.*
|
||||
|
||||
/**
|
||||
* JPA representation of the common schema entities
|
||||
*/
|
||||
object CommonSchema
|
||||
|
||||
/**
|
||||
* First version of the Vault ORM schema
|
||||
*/
|
||||
object CommonSchemaV1 : MappedSchema(schemaFamily = CommonSchema.javaClass, version = 1, mappedTypes = listOf(Party::class.java)) {
|
||||
|
||||
@MappedSuperclass
|
||||
open class LinearState(
|
||||
/**
|
||||
* Represents a [LinearState] [UniqueIdentifier]
|
||||
*/
|
||||
@Column(name = "external_id")
|
||||
var externalId: String?,
|
||||
|
||||
@Column(name = "uuid", nullable = false)
|
||||
var uuid: UUID
|
||||
|
||||
) : PersistentState() {
|
||||
constructor(uid: UniqueIdentifier) : this(externalId = uid.externalId, uuid = uid.id)
|
||||
}
|
||||
|
||||
@MappedSuperclass
|
||||
open class FungibleState(
|
||||
/** [ContractState] attributes */
|
||||
@OneToMany(cascade = arrayOf(CascadeType.ALL))
|
||||
var participants: Set<CommonSchemaV1.Party>,
|
||||
|
||||
/** [OwnableState] attributes */
|
||||
@OneToOne(cascade = arrayOf(CascadeType.ALL))
|
||||
var ownerKey: CommonSchemaV1.Party,
|
||||
|
||||
/** [FungibleAsset] attributes
|
||||
*
|
||||
* Note: the underlying Product being issued must be modelled into the
|
||||
* custom contract itself (eg. see currency in Cash contract state)
|
||||
*/
|
||||
|
||||
/** Amount attributes */
|
||||
@Column(name = "quantity")
|
||||
var quantity: Long,
|
||||
|
||||
/** Issuer attributes */
|
||||
@OneToOne(cascade = arrayOf(CascadeType.ALL))
|
||||
var issuerParty: CommonSchemaV1.Party,
|
||||
|
||||
@Column(name = "issuer_reference")
|
||||
var issuerRef: ByteArray
|
||||
) : PersistentState() {
|
||||
constructor(_participants: Set<AbstractParty>, _ownerKey: AbstractParty, _quantity: Long, _issuerParty: AbstractParty, _issuerRef: ByteArray)
|
||||
: this(participants = _participants.map { CommonSchemaV1.Party(it) }.toSet(),
|
||||
ownerKey = CommonSchemaV1.Party(_ownerKey),
|
||||
quantity = _quantity,
|
||||
issuerParty = CommonSchemaV1.Party(_issuerParty),
|
||||
issuerRef = _issuerRef)
|
||||
}
|
||||
|
||||
/**
|
||||
* Party entity (to be replaced by referencing final Identity Schema)
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "vault_party",
|
||||
indexes = arrayOf(Index(name = "party_name_idx", columnList = "party_name")))
|
||||
class Party(
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "party_id")
|
||||
var id: Int,
|
||||
|
||||
/**
|
||||
* [Party] attributes
|
||||
*/
|
||||
@Column(name = "party_name")
|
||||
var name: String,
|
||||
|
||||
@Column(name = "party_key", length = 65535) // TODO What is the upper limit on size of CompositeKey?)
|
||||
var key: String
|
||||
) {
|
||||
constructor(party: net.corda.core.identity.AbstractParty)
|
||||
: this(0, party.nameOrNull()?.toString() ?: party.toString(), party.owningKey.toBase58String())
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package net.corda.core.schemas
|
||||
|
||||
/**
|
||||
* An object used to fully qualify the [DummyDealStateSchema] family name (i.e. independent of version).
|
||||
*/
|
||||
object DummyDealStateSchema
|
||||
|
||||
/**
|
||||
* First version of a cash contract ORM schema that maps all fields of the [DummyDealState] contract state as it stood
|
||||
* at the time of writing.
|
||||
*/
|
||||
object DummyDealStateSchemaV1 : net.corda.core.schemas.MappedSchema(schemaFamily = net.corda.core.schemas.DummyDealStateSchema.javaClass, version = 1, mappedTypes = listOf(net.corda.core.schemas.DummyDealStateSchemaV1.PersistentDummyDealState::class.java)) {
|
||||
@javax.persistence.Entity
|
||||
@javax.persistence.Table(name = "dummy_deal_states")
|
||||
class PersistentDummyDealState(
|
||||
|
||||
@javax.persistence.Column(name = "deal_reference")
|
||||
var dealReference: String,
|
||||
|
||||
/** parent attributes */
|
||||
@javax.persistence.Transient
|
||||
val uid: net.corda.core.contracts.UniqueIdentifier
|
||||
|
||||
) : net.corda.node.services.vault.schemas.jpa.CommonSchemaV1.LinearState(uid = uid)
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package net.corda.core.schemas
|
||||
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
import javax.persistence.Column
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.Index
|
||||
import javax.persistence.Table
|
||||
|
||||
/**
|
||||
* An object used to fully qualify the [DummyLinearStateSchema] family name (i.e. independent of version).
|
||||
*/
|
||||
object DummyLinearStateSchema
|
||||
|
||||
/**
|
||||
* First version of a cash contract ORM schema that maps all fields of the [DummyLinearState] contract state as it stood
|
||||
* at the time of writing.
|
||||
*/
|
||||
object DummyLinearStateSchemaV1 : MappedSchema(schemaFamily = DummyLinearStateSchema.javaClass, version = 1, mappedTypes = listOf(PersistentDummyLinearState::class.java)) {
|
||||
@Entity
|
||||
@Table(name = "dummy_linear_states",
|
||||
indexes = arrayOf(Index(name = "external_id_idx", columnList = "external_id"),
|
||||
Index(name = "uuid_idx", columnList = "uuid")))
|
||||
class PersistentDummyLinearState(
|
||||
/**
|
||||
* UniqueIdentifier
|
||||
*/
|
||||
@Column(name = "external_id")
|
||||
var externalId: String?,
|
||||
|
||||
@Column(name = "uuid", nullable = false)
|
||||
var uuid: UUID,
|
||||
|
||||
/**
|
||||
* Dummy attributes
|
||||
*/
|
||||
@Column(name = "linear_string")
|
||||
var linearString: String,
|
||||
|
||||
@Column(name = "linear_number")
|
||||
var linearNumber: Long,
|
||||
|
||||
@Column(name = "linear_timestamp")
|
||||
var linearTimestamp: Instant,
|
||||
|
||||
@Column(name = "linear_boolean")
|
||||
var linearBoolean: Boolean
|
||||
) : PersistentState()
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package net.corda.core.schemas
|
||||
|
||||
import net.corda.node.services.vault.schemas.jpa.CommonSchemaV1
|
||||
import javax.persistence.Column
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.Table
|
||||
|
||||
/**
|
||||
* Second version of a cash contract ORM schema that extends the common
|
||||
* [VaultLinearState] abstract schema
|
||||
*/
|
||||
object DummyLinearStateSchemaV2 : net.corda.core.schemas.MappedSchema(schemaFamily = DummyLinearStateSchema.javaClass, version = 2,
|
||||
mappedTypes = listOf(PersistentDummyLinearState::class.java)) {
|
||||
@Entity
|
||||
@Table(name = "dummy_linear_states_v2")
|
||||
class PersistentDummyLinearState(
|
||||
@Column(name = "linear_string") var linearString: String,
|
||||
|
||||
@Column(name = "linear_number") var linearNumber: Long,
|
||||
|
||||
@Column(name = "linear_timestamp") var linearTimestamp: java.time.Instant,
|
||||
|
||||
@Column(name = "linear_boolean") var linearBoolean: Boolean,
|
||||
|
||||
/** parent attributes */
|
||||
@Transient
|
||||
val uid: net.corda.core.contracts.UniqueIdentifier
|
||||
) : CommonSchemaV1.LinearState(uid = uid)
|
||||
}
|
@ -49,7 +49,7 @@ open class MappedSchema(schemaFamily: Class<*>,
|
||||
* A super class for all mapped states exported to a schema that ensures the [StateRef] appears on the database row. The
|
||||
* [StateRef] will be set to the correct value by the framework (there's no need to set during mapping generation by the state itself).
|
||||
*/
|
||||
@MappedSuperclass open class PersistentState(@EmbeddedId var stateRef: PersistentStateRef? = null) : Persistable
|
||||
@MappedSuperclass open class PersistentState(@EmbeddedId var stateRef: PersistentStateRef? = null) : StatePersistable
|
||||
|
||||
/**
|
||||
* Embedded [StateRef] representation used in state mapping.
|
||||
@ -64,3 +64,8 @@ data class PersistentStateRef(
|
||||
) : Serializable {
|
||||
constructor(stateRef: StateRef) : this(stateRef.txhash.bytes.toHexString(), stateRef.index)
|
||||
}
|
||||
|
||||
/**
|
||||
* Marker interface to denote a persistable Corda state entity that will always have a transaction id and index
|
||||
*/
|
||||
interface StatePersistable : Persistable
|
@ -4,6 +4,7 @@ import io.requery.Key
|
||||
import io.requery.Persistable
|
||||
import io.requery.Superclass
|
||||
import net.corda.core.contracts.StateRef
|
||||
import net.corda.core.schemas.StatePersistable
|
||||
|
||||
import javax.persistence.Column
|
||||
|
||||
@ -14,7 +15,7 @@ object Requery {
|
||||
*/
|
||||
// TODO: this interface will supercede the existing [PersistentState] interface defined in PersistentTypes.kt
|
||||
// once we cut-over all existing Hibernate ContractState persistence to Requery
|
||||
@Superclass interface PersistentState : Persistable {
|
||||
@Superclass interface PersistentState : StatePersistable {
|
||||
@get:Key
|
||||
@get:Column(name = "transaction_id", length = 64)
|
||||
var txId: String
|
||||
|
@ -73,6 +73,7 @@ object DefaultKryoCustomizer {
|
||||
|
||||
noReferencesWithin<WireTransaction>()
|
||||
|
||||
register(sun.security.ec.ECPublicKeyImpl::class.java, PublicKeySerializer)
|
||||
register(EdDSAPublicKey::class.java, Ed25519PublicKeySerializer)
|
||||
register(EdDSAPrivateKey::class.java, Ed25519PrivateKeySerializer)
|
||||
|
||||
|
@ -662,7 +662,7 @@ class CryptoUtilsTest {
|
||||
@Test
|
||||
fun `ECDSA secp256R1 deterministic key generation`() {
|
||||
val (priv, pub) = Crypto.generateKeyPair(Crypto.ECDSA_SECP256R1_SHA256)
|
||||
val (dpriv, dpub) = Crypto.deterministicKeyPair(priv, "seed-1".toByteArray())
|
||||
val (dpriv, dpub) = Crypto.deriveKeyPair(priv, "seed-1".toByteArray())
|
||||
|
||||
// Check scheme.
|
||||
assertEquals(priv.algorithm, dpriv.algorithm)
|
||||
@ -687,15 +687,15 @@ class CryptoUtilsTest {
|
||||
assertNotEquals(pub, dpub)
|
||||
|
||||
// A new keyPair is always generated per different seed.
|
||||
val (dpriv2, dpub2) = Crypto.deterministicKeyPair(priv, "seed-2".toByteArray())
|
||||
val (dpriv2, dpub2) = Crypto.deriveKeyPair(priv, "seed-2".toByteArray())
|
||||
assertNotEquals(dpriv, dpriv2)
|
||||
assertNotEquals(dpub, dpub2)
|
||||
|
||||
// Check if the same input always produces the same output (i.e. deterministically generated).
|
||||
val (dpriv_1, dpub_1) = Crypto.deterministicKeyPair(priv, "seed-1".toByteArray())
|
||||
val (dpriv_1, dpub_1) = Crypto.deriveKeyPair(priv, "seed-1".toByteArray())
|
||||
assertEquals(dpriv, dpriv_1)
|
||||
assertEquals(dpub, dpub_1)
|
||||
val (dpriv_2, dpub_2) = Crypto.deterministicKeyPair(priv, "seed-2".toByteArray())
|
||||
val (dpriv_2, dpub_2) = Crypto.deriveKeyPair(priv, "seed-2".toByteArray())
|
||||
assertEquals(dpriv2, dpriv_2)
|
||||
assertEquals(dpub2, dpub_2)
|
||||
}
|
||||
@ -703,7 +703,7 @@ class CryptoUtilsTest {
|
||||
@Test
|
||||
fun `ECDSA secp256K1 deterministic key generation`() {
|
||||
val (priv, pub) = Crypto.generateKeyPair(Crypto.ECDSA_SECP256K1_SHA256)
|
||||
val (dpriv, dpub) = Crypto.deterministicKeyPair(priv, "seed-1".toByteArray())
|
||||
val (dpriv, dpub) = Crypto.deriveKeyPair(priv, "seed-1".toByteArray())
|
||||
|
||||
// Check scheme.
|
||||
assertEquals(priv.algorithm, dpriv.algorithm)
|
||||
@ -728,15 +728,15 @@ class CryptoUtilsTest {
|
||||
assertNotEquals(pub, dpub)
|
||||
|
||||
// A new keyPair is always generated per different seed.
|
||||
val (dpriv2, dpub2) = Crypto.deterministicKeyPair(priv, "seed-2".toByteArray())
|
||||
val (dpriv2, dpub2) = Crypto.deriveKeyPair(priv, "seed-2".toByteArray())
|
||||
assertNotEquals(dpriv, dpriv2)
|
||||
assertNotEquals(dpub, dpub2)
|
||||
|
||||
// Check if the same input always produces the same output (i.e. deterministically generated).
|
||||
val (dpriv_1, dpub_1) = Crypto.deterministicKeyPair(priv, "seed-1".toByteArray())
|
||||
val (dpriv_1, dpub_1) = Crypto.deriveKeyPair(priv, "seed-1".toByteArray())
|
||||
assertEquals(dpriv, dpriv_1)
|
||||
assertEquals(dpub, dpub_1)
|
||||
val (dpriv_2, dpub_2) = Crypto.deterministicKeyPair(priv, "seed-2".toByteArray())
|
||||
val (dpriv_2, dpub_2) = Crypto.deriveKeyPair(priv, "seed-2".toByteArray())
|
||||
assertEquals(dpriv2, dpriv_2)
|
||||
assertEquals(dpub2, dpub_2)
|
||||
}
|
||||
@ -744,7 +744,7 @@ class CryptoUtilsTest {
|
||||
@Test
|
||||
fun `EdDSA ed25519 deterministic key generation`() {
|
||||
val (priv, pub) = Crypto.generateKeyPair(Crypto.EDDSA_ED25519_SHA512)
|
||||
val (dpriv, dpub) = Crypto.deterministicKeyPair(priv, "seed-1".toByteArray())
|
||||
val (dpriv, dpub) = Crypto.deriveKeyPair(priv, "seed-1".toByteArray())
|
||||
|
||||
// Check scheme.
|
||||
assertEquals(priv.algorithm, dpriv.algorithm)
|
||||
@ -769,15 +769,15 @@ class CryptoUtilsTest {
|
||||
assertNotEquals(pub, dpub)
|
||||
|
||||
// A new keyPair is always generated per different seed.
|
||||
val (dpriv2, dpub2) = Crypto.deterministicKeyPair(priv, "seed-2".toByteArray())
|
||||
val (dpriv2, dpub2) = Crypto.deriveKeyPair(priv, "seed-2".toByteArray())
|
||||
assertNotEquals(dpriv, dpriv2)
|
||||
assertNotEquals(dpub, dpub2)
|
||||
|
||||
// Check if the same input always produces the same output (i.e. deterministically generated).
|
||||
val (dpriv_1, dpub_1) = Crypto.deterministicKeyPair(priv, "seed-1".toByteArray())
|
||||
val (dpriv_1, dpub_1) = Crypto.deriveKeyPair(priv, "seed-1".toByteArray())
|
||||
assertEquals(dpriv, dpriv_1)
|
||||
assertEquals(dpub, dpub_1)
|
||||
val (dpriv_2, dpub_2) = Crypto.deterministicKeyPair(priv, "seed-2".toByteArray())
|
||||
val (dpriv_2, dpub_2) = Crypto.deriveKeyPair(priv, "seed-2".toByteArray())
|
||||
assertEquals(dpriv2, dpriv_2)
|
||||
assertEquals(dpub2, dpub_2)
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ import net.corda.node.internal.InitiatedFlowFactory
|
||||
import net.corda.node.services.config.NodeConfiguration
|
||||
import net.corda.node.services.network.NetworkMapService
|
||||
import net.corda.node.services.persistence.NodeAttachmentService
|
||||
import net.corda.node.services.persistence.schemas.AttachmentEntity
|
||||
import net.corda.node.services.persistence.schemas.requery.AttachmentEntity
|
||||
import net.corda.node.services.statemachine.SessionInit
|
||||
import net.corda.node.utilities.transaction
|
||||
import net.corda.testing.node.MockNetwork
|
||||
|
@ -9,9 +9,13 @@ API: Flows
|
||||
|
||||
.. note:: Before reading this page, you should be familiar with the key concepts of :doc:`key-concepts-flows`.
|
||||
|
||||
.. contents::
|
||||
|
||||
An example flow
|
||||
---------------
|
||||
Let's imagine a flow for agreeing a basic ledger update between Alice and Bob. This flow will have two sides:
|
||||
Before we discuss the API offered by the flow, let's consider what a standard flow may look like.
|
||||
|
||||
Imagine a flow for agreeing a basic ledger update between Alice and Bob. This flow will have two sides:
|
||||
|
||||
* An ``Initiator`` side, that will initiate the request to update the ledger
|
||||
* A ``Responder`` side, that will respond to the request to update the ledger
|
||||
@ -76,23 +80,42 @@ To respond to these actions, the responder takes the following steps:
|
||||
|
||||
FlowLogic
|
||||
---------
|
||||
In practice, a flow is implemented as one or more communicating ``FlowLogic`` subclasses. Each ``FlowLogic`` subclass
|
||||
must override ``FlowLogic.call()``, which describes the actions it will take as part of the flow.
|
||||
In practice, a flow is implemented as one or more communicating ``FlowLogic`` subclasses. The ``FlowLogic``
|
||||
subclass's constructor can take any number of arguments of any type. The generic of ``FlowLogic`` (e.g.
|
||||
``FlowLogic<SignedTransaction>``) indicates the flow's return type.
|
||||
|
||||
So in the example above, we would have an ``Initiator`` ``FlowLogic`` subclass and a ``Responder`` ``FlowLogic``
|
||||
subclass. The actions of the initiator's side of the flow would be defined in ``Initiator.call``, and the actions
|
||||
of the responder's side of the flow would be defined in ``Responder.call``.
|
||||
.. container:: codeset
|
||||
|
||||
.. sourcecode:: kotlin
|
||||
|
||||
class Initiator(val arg1: Boolean,
|
||||
val arg2: Int,
|
||||
val counterparty: Party): FlowLogic<SignedTransaction>() { }
|
||||
|
||||
class Responder(val otherParty: Party) : FlowLogic<Unit>() { }
|
||||
|
||||
.. sourcecode:: java
|
||||
|
||||
public static class Initiator extends FlowLogic<SignedTransaction> {
|
||||
private final boolean arg1;
|
||||
private final int arg2;
|
||||
private final Party counterparty;
|
||||
|
||||
public Initiator(boolean arg1, int arg2, Party counterparty) {
|
||||
this.arg1 = arg1;
|
||||
this.arg2 = arg2;
|
||||
this.counterparty = counterparty;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Responder extends FlowLogic<Void> { }
|
||||
|
||||
FlowLogic annotations
|
||||
^^^^^^^^^^^^^^^^^^^^^
|
||||
---------------------
|
||||
Any flow that you wish to start either directly via RPC or as a subflow must be annotated with the
|
||||
``@InitiatingFlow`` annotation. Additionally, if you wish to start the flow via RPC, you must annotate it with the
|
||||
``@StartableByRPC`` annotation.
|
||||
|
||||
Any flow that responds to a message from another flow must be annotated with the ``@InitiatedBy`` annotation.
|
||||
``@InitiatedBy`` takes the class of the flow it is responding to as its single parameter.
|
||||
|
||||
So in our example, we would have:
|
||||
``@StartableByRPC`` annotation:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
@ -100,74 +123,145 @@ So in our example, we would have:
|
||||
|
||||
@InitiatingFlow
|
||||
@StartableByRPC
|
||||
class Initiator(): FlowLogic<Unit>() {
|
||||
|
||||
...
|
||||
|
||||
@InitiatedBy(Initiator::class)
|
||||
class Responder(val otherParty: Party) : FlowLogic<Unit>() {
|
||||
class Initiator(): FlowLogic<Unit>() { }
|
||||
|
||||
.. sourcecode:: java
|
||||
|
||||
@InitiatingFlow
|
||||
@StartableByRPC
|
||||
public static class Initiator extends FlowLogic<Unit> {
|
||||
public static class Initiator extends FlowLogic<Unit> { }
|
||||
|
||||
...
|
||||
Meanwhile, any flow that responds to a message from another flow must be annotated with the ``@InitiatedBy`` annotation.
|
||||
``@InitiatedBy`` takes the class of the flow it is responding to as its single parameter:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. sourcecode:: kotlin
|
||||
|
||||
@InitiatedBy(Initiator::class)
|
||||
class Responder(val otherParty: Party) : FlowLogic<Unit>() { }
|
||||
|
||||
.. sourcecode:: java
|
||||
|
||||
@InitiatedBy(Initiator.class)
|
||||
public static class Responder extends FlowLogic<Void> {
|
||||
public static class Responder extends FlowLogic<Void> { }
|
||||
|
||||
Additionally, any flow that is started by a ``SchedulableState`` must be annotated with the ``@SchedulableFlow``
|
||||
annotation.
|
||||
|
||||
ServiceHub
|
||||
----------
|
||||
Within ``FlowLogic.call``, the flow developer has access to the node's ``ServiceHub``, which provides access to the
|
||||
various services the node provides. See :doc:`api-service-hub` for information about the services the ``ServiceHub``
|
||||
offers.
|
||||
Call
|
||||
----
|
||||
Each ``FlowLogic`` subclass must override ``FlowLogic.call()``, which describes the actions it will take as part of
|
||||
the flow. For example, the actions of the initiator's side of the flow would be defined in ``Initiator.call``, and the
|
||||
actions of the responder's side of the flow would be defined in ``Responder.call``.
|
||||
|
||||
Some common tasks performed using the ``ServiceHub`` are:
|
||||
|
||||
* Looking up your own identity or the identity of a counterparty using the ``networkMapCache``
|
||||
* Identifying the providers of a given service (e.g. a notary service) using the ``networkMapCache``
|
||||
* Retrieving states to use in a transaction using the ``vaultService``
|
||||
* Retrieving attachments and past transactions to use in a transaction using the ``storageService``
|
||||
* Creating a timestamp using the ``clock``
|
||||
* Signing a transaction using the ``keyManagementService``
|
||||
|
||||
Common flow tasks
|
||||
-----------------
|
||||
There are a number of common tasks that you will need to perform within ``FlowLogic.call`` in order to agree ledger
|
||||
updates. This section details the API for the most common tasks.
|
||||
|
||||
Retrieving information about other nodes
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
We use the network map to retrieve information about other nodes on the network:
|
||||
In order for nodes to be able to run multiple flows concurrently, and to allow flows to survive node upgrades and
|
||||
restarts, flows need to be checkpointable and serializable to disk. This is achieved by marking ``FlowLogic.call()``,
|
||||
as well as any function invoked from within ``FlowLogic.call()``, with an ``@Suspendable`` annotation.
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. sourcecode:: kotlin
|
||||
|
||||
val networkMap = serviceHub.networkMapCache
|
||||
|
||||
val allNodes = networkMap.partyNodes
|
||||
val allNotaryNodes = networkMap.notaryNodes
|
||||
val randomNotaryNode = networkMap.getAnyNotary()
|
||||
|
||||
val alice = networkMap.getNodeByLegalName(X500Name("CN=Alice,O=Alice,L=London,C=GB"))
|
||||
val bob = networkMap.getNodeByLegalIdentityKey(bobsKey)
|
||||
class Initiator(val counterparty: Party): FlowLogic<Unit>() {
|
||||
@Suspendable
|
||||
override fun call() { }
|
||||
}
|
||||
|
||||
.. sourcecode:: java
|
||||
|
||||
final NetworkMapCache networkMap = getServiceHub().getNetworkMapCache();
|
||||
public static class InitiatorFlow extends FlowLogic<Void> {
|
||||
private final Party counterparty;
|
||||
|
||||
final List<NodeInfo> allNodes = networkMap.getPartyNodes();
|
||||
final List<NodeInfo> allNotaryNodes = networkMap.getNotaryNodes();
|
||||
final Party randomNotaryNode = networkMap.getAnyNotary(null);
|
||||
public Initiator(Party counterparty) {
|
||||
this.counterparty = counterparty;
|
||||
}
|
||||
|
||||
final NodeInfo alice = networkMap.getNodeByLegalName(new X500Name("CN=Alice,O=Alice,L=London,C=GB"));
|
||||
final NodeInfo bob = networkMap.getNodeByLegalIdentityKey(bobsKey);
|
||||
@Suspendable
|
||||
@Override
|
||||
public Void call() throws FlowException { }
|
||||
|
||||
}
|
||||
|
||||
ServiceHub
|
||||
----------
|
||||
Within ``FlowLogic.call``, the flow developer has access to the node's ``ServiceHub``, which provides access to the
|
||||
various services the node provides. We will use the ``ServiceHub`` extensively in the examples that follow. You can
|
||||
also see :doc:`api-service-hub` for information about the services the ``ServiceHub`` offers.
|
||||
|
||||
Common flow tasks
|
||||
-----------------
|
||||
There are a number of common tasks that you will need to perform within ``FlowLogic.call`` in order to agree ledger
|
||||
updates. This section details the API for common tasks.
|
||||
|
||||
Transaction building
|
||||
^^^^^^^^^^^^^^^^^^^^
|
||||
The majority of the work performed during a flow will be to build, verify and sign a transaction. We cover this in
|
||||
:doc:`api-transactions`.
|
||||
|
||||
Retrieving information about other nodes
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
We can retrieve information about other nodes on the network and the services they offer using
|
||||
``ServiceHub.networkMapCache``.
|
||||
|
||||
Notaries
|
||||
~~~~~~~~
|
||||
Remember that a transaction generally needs a notary to:
|
||||
|
||||
* Prevent double-spends if the transaction has inputs
|
||||
* Serve as a timestamping authority if the transaction has a time-window
|
||||
|
||||
There are several ways to retrieve a notary from the network map:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 1
|
||||
:end-before: DOCEND 1
|
||||
:dedent: 12
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 1
|
||||
:end-before: DOCEND 1
|
||||
:dedent: 12
|
||||
|
||||
Specific counterparties
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
We can also use the network map to retrieve a specific counterparty:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 2
|
||||
:end-before: DOCEND 2
|
||||
:dedent: 12
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 2
|
||||
:end-before: DOCEND 2
|
||||
:dedent: 12
|
||||
|
||||
Specific services
|
||||
~~~~~~~~~~~~~~~~~
|
||||
Finally, we can use the map to identify nodes providing a specific service (e.g. a regulator or an oracle):
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 3
|
||||
:end-before: DOCEND 3
|
||||
:dedent: 12
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 3
|
||||
:end-before: DOCEND 3
|
||||
:dedent: 12
|
||||
|
||||
Communication between parties
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -180,50 +274,121 @@ Communication between parties
|
||||
* ``sendAndReceive(receiveType: Class<R>, otherParty: Party, payload: Any)``
|
||||
* Sends the ``payload`` object to the ``otherParty``, and receives an object of type ``receiveType`` back
|
||||
|
||||
Each ``FlowLogic`` subclass can be annotated to respond to messages from a given *counterparty* flow using the
|
||||
``@InitiatedBy`` annotation. When a node first receives a message from a given ``FlowLogic.call()`` invocation, it
|
||||
responds as follows:
|
||||
|
||||
* The node checks whether they have a ``FlowLogic`` subclass that is registered to respond to the ``FlowLogic`` that
|
||||
is sending the message:
|
||||
|
||||
a. If yes, the node starts an instance of this ``FlowLogic`` by invoking ``FlowLogic.call()``
|
||||
b. Otherwise, the node ignores the message
|
||||
|
||||
* The counterparty steps through their ``FlowLogic.call()`` method until they encounter a call to ``receive()``, at
|
||||
which point they process the message from the initiator
|
||||
|
||||
Upon calling ``receive()``/``sendAndReceive()``, the ``FlowLogic`` is suspended until it receives a response.
|
||||
|
||||
UntrustworthyData
|
||||
~~~~~~~~~~~~~~~~~
|
||||
``send()`` and ``sendAndReceive()`` return a payload wrapped in an ``UntrustworthyData`` instance. This is a
|
||||
reminder that any data received off the wire is untrustworthy and must be verified.
|
||||
|
||||
We verify the ``UntrustworthyData`` and retrieve its payload by calling ``unwrap``:
|
||||
Send
|
||||
~~~~
|
||||
We can send arbitrary data to a counterparty:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. sourcecode:: kotlin
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 4
|
||||
:end-before: DOCEND 4
|
||||
:dedent: 12
|
||||
|
||||
val partSignedTx = receive<SignedTransaction>(otherParty).unwrap { partSignedTx ->
|
||||
val wireTx = partSignedTx.verifySignatures(keyPair.public, notaryPubKey)
|
||||
wireTx.toLedgerTransaction(serviceHub).verify()
|
||||
partSignedTx
|
||||
}
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 4
|
||||
:end-before: DOCEND 4
|
||||
:dedent: 12
|
||||
|
||||
.. sourcecode:: java
|
||||
If this is the first ``send``, the counterparty will either:
|
||||
|
||||
final SignedTransaction partSignedTx = receive(SignedTransaction.class, otherParty)
|
||||
.unwrap(tx -> {
|
||||
try {
|
||||
final WireTransaction wireTx = tx.verifySignatures(keyPair.getPublic(), notaryPubKey);
|
||||
wireTx.toLedgerTransaction(getServiceHub()).verify();
|
||||
} catch (SignatureException ex) {
|
||||
throw new FlowException(tx.getId() + " failed signature checks", ex);
|
||||
}
|
||||
return tx;
|
||||
});
|
||||
1. Ignore the message if they are not registered to respond to messages from this flow.
|
||||
2. Start the flow they have registered to respond to this flow, and run the flow until the first call to ``receive``,
|
||||
at which point they process the message. In other words, we are assuming that the counterparty is registered to
|
||||
respond to this flow, and has a corresponding ``receive`` call.
|
||||
|
||||
Receive
|
||||
~~~~~~~
|
||||
We can also wait to receive arbitrary data of a specific type from a counterparty. Again, this implies a corresponding
|
||||
``send`` call in the counterparty's flow. A few scenarios:
|
||||
|
||||
* We never receive a message back. In the current design, the flow is paused until the node's owner kills the flow.
|
||||
* Instead of sending a message back, the counterparty throws a ``FlowException``. This exception is propagated back
|
||||
to us, and we can use the error message to establish what happened.
|
||||
* We receive a message back, but it's of the wrong type. In this case, a ``FlowException`` is thrown.
|
||||
* We receive back a message of the correct type. All is good.
|
||||
|
||||
Upon calling ``receive`` (or ``sendAndReceive``), the ``FlowLogic`` is suspended until it receives a response.
|
||||
|
||||
We receive the data wrapped in an ``UntrustworthyData`` instance. This is a reminder that the data we receive may not
|
||||
be what it appears to be! We must unwrap the ``UntrustworthyData`` using a lambda:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 5
|
||||
:end-before: DOCEND 5
|
||||
:dedent: 12
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 5
|
||||
:end-before: DOCEND 5
|
||||
:dedent: 12
|
||||
|
||||
We're not limited to sending to and receiving from a single counterparty. A flow can send messages to as many parties
|
||||
as it likes, and each party can invoke a different response flow:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 6
|
||||
:end-before: DOCEND 6
|
||||
:dedent: 12
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 6
|
||||
:end-before: DOCEND 6
|
||||
:dedent: 12
|
||||
|
||||
SendAndReceive
|
||||
~~~~~~~~~~~~~~
|
||||
We can also use a single call to send data to a counterparty and wait to receive data of a specific type back. The
|
||||
type of data sent doesn't need to match the type of the data received back:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 7
|
||||
:end-before: DOCEND 7
|
||||
:dedent: 12
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 7
|
||||
:end-before: DOCEND 7
|
||||
:dedent: 12
|
||||
|
||||
Counterparty response
|
||||
~~~~~~~~~~~~~~~~~~~~~
|
||||
Suppose we're now on the ``Responder`` side of the flow. We just received the following series of messages from the
|
||||
``Initiator``:
|
||||
|
||||
1. They sent us an ``Any`` instance
|
||||
2. They waited to receive an ``Integer`` instance back
|
||||
3. They sent a ``String`` instance and waited to receive a ``Boolean`` instance back
|
||||
|
||||
Our side of the flow must mirror these calls. We could do this as follows:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 8
|
||||
:end-before: DOCEND 8
|
||||
:dedent: 12
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 8
|
||||
:end-before: DOCEND 8
|
||||
:dedent: 12
|
||||
|
||||
Subflows
|
||||
--------
|
||||
@ -236,27 +401,118 @@ Corda provides a number of built-in flows that should be used for handling commo
|
||||
* ``NotaryChangeFlow``, which should be used to change a state's notary
|
||||
|
||||
These flows are designed to be used as building blocks in your own flows. You invoke them by calling
|
||||
``FlowLogic.subFlow`` from within your flow's ``call`` method. Here is an example from ``TwoPartyDealFlow.kt``:
|
||||
``FlowLogic.subFlow`` from within your flow's ``call`` method. Let's look at three very common examples.
|
||||
|
||||
FinalityFlow
|
||||
^^^^^^^^^^^^
|
||||
``FinalityFlow`` allows us to notarise the transaction and get it recorded in the vault of the participants of all
|
||||
the transaction's states:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../core/src/main/kotlin/net/corda/flows/TwoPartyDealFlow.kt
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 1
|
||||
:end-before: DOCEND 1
|
||||
:start-after: DOCSTART 9
|
||||
:end-before: DOCEND 9
|
||||
:dedent: 12
|
||||
|
||||
In this example, we are starting a ``CollectSignaturesFlow``, passing in a partially signed transaction, and
|
||||
receiving back a fully-signed version of the same transaction.
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 9
|
||||
:end-before: DOCEND 9
|
||||
:dedent: 12
|
||||
|
||||
Subflows in our example flow
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
In practice, many of the actions in our example flow would be automated using subflows:
|
||||
We can also choose to send the transaction to additional parties who aren't one of the state's participants:
|
||||
|
||||
* Parts 2-4 of ``Initiator.call`` should be automated by invoking ``CollectSignaturesFlow``
|
||||
* Part 5 of ``Initiator.call`` should be automated by invoking ``FinalityFlow``
|
||||
* Part 1 of ``Responder.call`` should be automated by invoking ``SignTransactionFlow``
|
||||
* Part 2 of ``Responder.call`` will be handled automatically when the counterparty invokes ``FinalityFlow``
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 10
|
||||
:end-before: DOCEND 10
|
||||
:dedent: 12
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 10
|
||||
:end-before: DOCEND 10
|
||||
:dedent: 12
|
||||
|
||||
Only one party has to call ``FinalityFlow`` for a given transaction to be recorded by all participants. It does
|
||||
**not** need to be called by each participant individually.
|
||||
|
||||
CollectSignaturesFlow/SignTransactionFlow
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
The list of parties who need to sign a transaction is dictated by the transaction's commands. Once we've signed a
|
||||
transaction ourselves, we can automatically gather the signatures of the other required signers using
|
||||
``CollectSignaturesFlow``:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 15
|
||||
:end-before: DOCEND 15
|
||||
:dedent: 12
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 15
|
||||
:end-before: DOCEND 15
|
||||
:dedent: 12
|
||||
|
||||
Each required signer will need to respond by invoking its own ``SignTransactionFlow`` subclass to check the
|
||||
transaction and provide their signature if they are satisfied:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 16
|
||||
:end-before: DOCEND 16
|
||||
:dedent: 12
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 16
|
||||
:end-before: DOCEND 16
|
||||
:dedent: 12
|
||||
|
||||
ResolveTransactionsFlow
|
||||
^^^^^^^^^^^^^^^^^^^^^^^
|
||||
Verifying a transaction will also verify every transaction in the transaction's dependency chain. So if we receive a
|
||||
transaction from a counterparty and it has any dependencies, we'd need to download all of these dependencies
|
||||
using``ResolveTransactionsFlow`` before verifying it:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 13
|
||||
:end-before: DOCEND 13
|
||||
:dedent: 12
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 13
|
||||
:end-before: DOCEND 13
|
||||
:dedent: 12
|
||||
|
||||
We can also resolve a `StateRef` dependency chain:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 14
|
||||
:end-before: DOCEND 14
|
||||
:dedent: 12
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 14
|
||||
:end-before: DOCEND 14
|
||||
:dedent: 12
|
||||
|
||||
FlowException
|
||||
-------------
|
||||
@ -283,18 +539,38 @@ There are many scenarios in which throwing a ``FlowException`` would be appropri
|
||||
* The transaction does not match the parameters of the deal as discussed
|
||||
* You are reneging on a deal
|
||||
|
||||
Suspending flows
|
||||
----------------
|
||||
In order for nodes to be able to run multiple flows concurrently, and to allow flows to survive node upgrades and
|
||||
restarts, flows need to be checkpointable and serializable to disk.
|
||||
ProgressTracker
|
||||
---------------
|
||||
We can give our flow a progress tracker. This allows us to see the flow's progress visually in our node's CRaSH shell.
|
||||
|
||||
This is achieved by marking any function invoked from within ``FlowLogic.call()`` with an ``@Suspendable`` annotation.
|
||||
|
||||
We can see an example in ``CollectSignaturesFlow``:
|
||||
To provide a progress tracker, we have to override ``FlowLogic.progressTracker`` in our flow:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../core/src/main/kotlin/net/corda/flows/CollectSignaturesFlow.kt
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 1
|
||||
:end-before: DOCEND 1
|
||||
:start-after: DOCSTART 17
|
||||
:end-before: DOCEND 17
|
||||
:dedent: 8
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 17
|
||||
:end-before: DOCEND 17
|
||||
:dedent: 8
|
||||
|
||||
We then update the progress tracker's current step as we progress through the flow as follows:
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART 18
|
||||
:end-before: DOCEND 18
|
||||
:dedent: 12
|
||||
|
||||
.. literalinclude:: ../../docs/source/example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
||||
:start-after: DOCSTART 18
|
||||
:end-before: DOCEND 18
|
||||
:dedent: 12
|
@ -6,8 +6,9 @@ This section describes the APIs that are available for the development of CorDap
|
||||
* :doc:`api-states`
|
||||
* :doc:`api-persistence`
|
||||
* :doc:`api-contracts`
|
||||
* :doc:`api-vault`
|
||||
* :doc:`api-transactions`
|
||||
* :doc:`api-flows`
|
||||
* :doc:`api-core-types`
|
||||
|
||||
Before reading this page, you should be familiar with the key concepts of Corda: :doc:`key-concepts`.
|
||||
Before reading this page, you should be familiar with the :doc:`key concepts of Corda <key-concepts>`.
|
@ -1,5 +1,5 @@
|
||||
Vault Query
|
||||
===========
|
||||
API: Vault
|
||||
==========
|
||||
|
||||
Corda has been architected from the ground up to encourage usage of industry standard, proven query frameworks and libraries for accessing RDBMS backed transactional stores (including the Vault).
|
||||
|
||||
@ -9,7 +9,7 @@ Corda provides a number of flexible query mechanisms for accessing the Vault:
|
||||
- custom JPA_/JPQL_ queries
|
||||
- custom 3rd party Data Access frameworks such as `Spring Data <http://projects.spring.io/spring-data>`_
|
||||
|
||||
The majority of query requirements can be satified by using the Vault Query API, which is exposed via the ``VaultService`` for use directly by flows:
|
||||
The majority of query requirements can be satisfied by using the Vault Query API, which is exposed via the ``VaultQueryService`` for use directly by flows:
|
||||
|
||||
.. literalinclude:: ../../core/src/main/kotlin/net/corda/core/node/services/Services.kt
|
||||
:language: kotlin
|
||||
@ -28,63 +28,80 @@ and via ``CordaRPCOps`` for use by RPC client applications:
|
||||
:start-after: DOCSTART VaultTrackByAPI
|
||||
:end-before: DOCEND VaultTrackByAPI
|
||||
|
||||
Java helper methods are also provided with default values for arguments:
|
||||
Helper methods are also provided with default values for arguments:
|
||||
|
||||
.. literalinclude:: ../../core/src/main/kotlin/net/corda/core/messaging/CordaRPCOps.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART VaultQueryAPIJavaHelpers
|
||||
:end-before: DOCEND VaultQueryAPIJavaHelpers
|
||||
:start-after: DOCSTART VaultQueryAPIHelpers
|
||||
:end-before: DOCEND VaultQueryAPIHelpers
|
||||
|
||||
.. literalinclude:: ../../core/src/main/kotlin/net/corda/core/messaging/CordaRPCOps.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART VaultTrackAPIHelpers
|
||||
:end-before: DOCEND VaultTrackAPIHelpers
|
||||
|
||||
The API provides both static (snapshot) and dynamic (snapshot with streaming updates) methods for a defined set of filter criteria.
|
||||
|
||||
- Use ``queryBy`` to obtain a only current snapshot of data (for a given ``QueryCriteria``)
|
||||
- Use ``trackBy`` to obtain a both a current snapshot and a future stream of updates (for a given ``QueryCriteria``)
|
||||
|
||||
.. note:: Streaming updates are only filtered based on contract type and state status (UNCONSUMED, CONSUMED, ALL)
|
||||
|
||||
Simple pagination (page number and size) and sorting (directional ordering, null handling, custom property sort) is also specifiable.
|
||||
Defaults are defined for Paging (pageNumber = 0, pageSize = 200) and Sorting (direction = ASC, nullHandling = NULLS_LAST).
|
||||
Simple pagination (page number and size) and sorting (directional ordering using standard or custom property attributes) is also specifiable.
|
||||
Defaults are defined for Paging (pageNumber = 0, pageSize = 200) and Sorting (direction = ASC).
|
||||
|
||||
The ``QueryCriteria`` interface provides a flexible mechanism for specifying different filtering criteria, including and/or composition and a rich set of logical operators. There are four implementations of this interface which can be chained together to define advanced filters.
|
||||
The ``QueryCriteria`` interface provides a flexible mechanism for specifying different filtering criteria, including and/or composition and a rich set of operators to include: binary logical (AND, OR), comparison (LESS_THAN, LESS_THAN_OR_EQUAL, GREATER_THAN, GREATER_THAN_OR_EQUAL), equality (EQUAL, NOT_EQUAL), likeness (LIKE, NOT_LIKE), nullability (IS_NULL, NOT_NULL), and collection based (IN, NOT_IN).
|
||||
|
||||
There are four implementations of this interface which can be chained together to define advanced filters.
|
||||
|
||||
1. ``VaultQueryCriteria`` provides filterable criteria on attributes within the Vault states table: status (UNCONSUMED, CONSUMED), state reference(s), contract state type(s), notaries, soft locked states, timestamps (RECORDED, CONSUMED).
|
||||
|
||||
.. note:: Sensible defaults are defined for frequently used attributes (status = UNCONSUMED, includeSoftlockedStates = true).
|
||||
|
||||
2. ``FungibleAssetQueryCriteria`` provides filterable criteria on attributes defined in the Corda Core ``FungibleAsset`` contract state interface, used to represent assets that are fungible, countable and issued by a specific party (eg. ``Cash.State`` and ``CommodityContract.State`` in the Corda finance module).
|
||||
2. ``FungibleAssetQueryCriteria`` provides filterable criteria on attributes defined in the Corda Core ``FungibleAsset`` contract state interface, used to represent assets that are fungible, countable and issued by a specific party (eg. ``Cash.State`` and ``CommodityContract.State`` in the Corda finance module). Filterable attributes include: participants(s), owner(s), quantity, issuer party(s) and issuer reference(s).
|
||||
|
||||
.. note:: Contract states that extend the ``FungibleAsset`` interface now automatically persist associated state attributes.
|
||||
.. note:: All contract states that extend the ``FungibleAsset`` now automatically persist that interfaces common state attributes to the **vault_fungible_states** table.
|
||||
|
||||
3. ``LinearStateQueryCriteria`` provides filterable criteria on attributes defined in the Corda Core ``LinearState`` and ``DealState`` contract state interfaces, used to represent entities that continuously supercede themselves, all of which share the same *linearId* (eg. trade entity states such as the ``IRSState`` defined in the SIMM valuation demo)
|
||||
3. ``LinearStateQueryCriteria`` provides filterable criteria on attributes defined in the Corda Core ``LinearState`` and ``DealState`` contract state interfaces, used to represent entities that continuously supercede themselves, all of which share the same *linearId* (eg. trade entity states such as the ``IRSState`` defined in the SIMM valuation demo). Filterable attributes include: participant(s), linearId(s), dealRef(s).
|
||||
|
||||
.. note:: Contract states that extend the ``LinearState`` or ``DealState`` interfaces now automatically persist associated state attributes.
|
||||
.. note:: All contract states that extend ``LinearState`` or ``DealState`` now automatically persist those interfaces common state attributes to the **vault_linear_states** table.
|
||||
|
||||
4. ``VaultCustomQueryCriteria`` provides the means to specify one or many arbitrary expressions on attributes defined by a custom contract state that implements its own schema as described in the Persistence_ documentation and associated examples.
|
||||
Custom criteria expressions are expressed as JPA Query like WHERE clauses as follows: [JPA entityAttributeName] [Operand] [Value]
|
||||
4. ``VaultCustomQueryCriteria`` provides the means to specify one or many arbitrary expressions on attributes defined by a custom contract state that implements its own schema as described in the api-persistence_ documentation and associated examples. Custom criteria expressions are expressed using one of several type-safe ``CriteriaExpression``: BinaryLogical, Not, ColumnPredicateExpression. The ColumnPredicateExpression allows for specification arbitrary criteria using the previously enumerated operator types. Furthermore, a rich DSL is provided to enable simple construction of custom criteria using any combination of ``ColumnPredicate``.
|
||||
|
||||
An example is illustrated here:
|
||||
.. note:: It is a requirement to register any custom contract schemas to be used in Vault Custom queries in the associated `CordaPluginRegistry` configuration for the respective CorDapp using the ``requiredSchemas`` configuration field (which specifies a set of `MappedSchema`)
|
||||
|
||||
An example is illustrated here:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART VaultQueryExample16
|
||||
:end-before: DOCEND VaultQueryExample16
|
||||
:start-after: DOCSTART VaultQueryExample20
|
||||
:end-before: DOCEND VaultQueryExample20
|
||||
|
||||
All ``QueryCriteria`` implementations are composable using ``and`` and ``or`` operators, as also illustrated above.
|
||||
|
||||
Additional notes
|
||||
|
||||
.. note:: Custom contract states that implement the ``Queryable`` interface may now extend the ``FungiblePersistentState``, ``LinearPersistentState`` or ``DealPersistentState`` classes when implementing their ``MappedSchema``. Previously, all custom contracts extended the root ``PersistentState`` class and defined repeated mappings of ``FungibleAsset``, ``LinearState`` and ``DealState`` attributes.
|
||||
.. note:: Custom contract states that implement the ``Queryable`` interface may now extend common schemas types ``FungiblePersistentState`` or, ``LinearPersistentState``. Previously, all custom contracts extended the root ``PersistentState`` class and defined repeated mappings of ``FungibleAsset`` and ``LinearState`` attributes. See ``SampleCashSchemaV2`` and ``DummyLinearStateSchemaV2`` as examples.
|
||||
|
||||
Examples of these ``QueryCriteria`` objects are presented below for Kotlin and Java.
|
||||
|
||||
The Vault Query API leverages the rich semantics of the underlying Requery_ persistence framework adopted by Corda.
|
||||
.. note:: When specifying the Contract Type as a parameterised type to the QueryCriteria in Kotlin, queries now include all concrete implementations of that type if this is an interface. Previously, it was only possible to query on Concrete types (or the universe of all Contract States).
|
||||
|
||||
.. _Requery: https://github.com/requery/requery/wiki
|
||||
.. _Persistence: https://docs.corda.net/persistence.html
|
||||
The Vault Query API leverages the rich semantics of the underlying JPA Hibernate_ based Persistence_ framework adopted by Corda.
|
||||
|
||||
.. _Hibernate: https://docs.jboss.org/hibernate/jpa/2.1/api/
|
||||
.. _Persistence: https://docs.corda.net/api-persistence.html
|
||||
|
||||
.. note:: Permissioning at the database level will be enforced at a later date to ensure authenticated, role-based, read-only access to underlying Corda tables.
|
||||
|
||||
.. note:: API's now provide ease of use calling semantics from both Java and Kotlin.
|
||||
.. note:: API's now provide ease of use calling semantics from both Java and Kotlin. However, it should be noted that Java custom queries are significantly more verbose due to the use of reflection fields to reference schema attribute types.
|
||||
|
||||
.. note:: Current queries by ``Party`` specify only a party name as the underlying identity framework is being re-designed. In future it may be possible to query on specific elements of a parties identity such as a ``CompositeKey`` hierarchy (parent and child nodes, weightings).
|
||||
An example of a custom query in Java is illustrated here:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/java/net/corda/node/services/vault/VaultQueryJavaTests.java
|
||||
:language: java
|
||||
:start-after: DOCSTART VaultJavaQueryExample3
|
||||
:end-before: DOCEND VaultJavaQueryExample3
|
||||
|
||||
.. note:: Current queries by ``Party`` specify the ``AbstractParty`` which may be concrete or anonymous. In the later case, where an anonymous party does not have an associated X500Name, then no query results will ever be produced. For performance reasons, queries do not use PublicKey as search criteria. Ongoing design work on identity manangement is likely to enhance identity based queries (including composite key criteria selection).
|
||||
|
||||
Example usage
|
||||
-------------
|
||||
@ -94,7 +111,7 @@ Kotlin
|
||||
|
||||
**General snapshot queries using** ``VaultQueryCriteria``
|
||||
|
||||
Query for all unconsumed states:
|
||||
Query for all unconsumed states (simplest query possible):
|
||||
|
||||
.. literalinclude:: ../../node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt
|
||||
:language: kotlin
|
||||
@ -122,6 +139,8 @@ Query for unconsumed states for a given notary:
|
||||
:start-after: DOCSTART VaultQueryExample4
|
||||
:end-before: DOCEND VaultQueryExample4
|
||||
|
||||
.. note:: We are using the notaries X500Name as our search identifier.
|
||||
|
||||
Query for unconsumed states for a given set of participants:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt
|
||||
@ -136,13 +155,17 @@ Query for unconsumed states recorded between two time intervals:
|
||||
:start-after: DOCSTART VaultQueryExample6
|
||||
:end-before: DOCEND VaultQueryExample6
|
||||
|
||||
Query for all states with pagination specification:
|
||||
.. note:: This example illustrates usage of a Between ColumnPredicate.
|
||||
|
||||
Query for all states with pagination specification (10 results per page):
|
||||
|
||||
.. literalinclude:: ../../node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART VaultQueryExample7
|
||||
:end-before: DOCEND VaultQueryExample7
|
||||
|
||||
.. note:: The result set metadata field `totalStatesAvailable` allows you to further paginate accordingly.
|
||||
|
||||
**LinearState and DealState queries using** ``LinearStateQueryCriteria``
|
||||
|
||||
Query for unconsumed linear states for given linear ids:
|
||||
@ -152,12 +175,12 @@ Query for unconsumed linear states for given linear ids:
|
||||
:start-after: DOCSTART VaultQueryExample8
|
||||
:end-before: DOCEND VaultQueryExample8
|
||||
|
||||
.. note:: This example was previously executed using the deprecated extension method:
|
||||
This example was previously executed using the deprecated extension method:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART VaultDeprecatedQueryExample1
|
||||
:end-before: DOCEND VaultDeprecatedQueryExample1
|
||||
.. literalinclude:: ../../node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART VaultDeprecatedQueryExample1
|
||||
:end-before: DOCEND VaultDeprecatedQueryExample1
|
||||
|
||||
Query for all linear states associated with a linear id:
|
||||
|
||||
@ -166,12 +189,12 @@ Query for all linear states associated with a linear id:
|
||||
:start-after: DOCSTART VaultQueryExample9
|
||||
:end-before: DOCEND VaultQueryExample9
|
||||
|
||||
.. note:: This example was previously executed using the deprecated method:
|
||||
This example was previously executed using the deprecated method:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART VaultDeprecatedQueryExample2
|
||||
:end-before: DOCEND VaultDeprecatedQueryExample2
|
||||
.. literalinclude:: ../../node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART VaultDeprecatedQueryExample2
|
||||
:end-before: DOCEND VaultDeprecatedQueryExample2
|
||||
|
||||
Query for unconsumed deal states with deals references:
|
||||
|
||||
@ -196,13 +219,15 @@ Query for fungible assets for a given currency:
|
||||
:start-after: DOCSTART VaultQueryExample12
|
||||
:end-before: DOCEND VaultQueryExample12
|
||||
|
||||
Query for fungible assets for a given currency and minimum quantity:
|
||||
Query for fungible assets for a minimum quantity:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART VaultQueryExample13
|
||||
:end-before: DOCEND VaultQueryExample13
|
||||
|
||||
.. note:: This example uses the builder DSL.
|
||||
|
||||
Query for fungible assets for a specifc issuer party:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt
|
||||
@ -210,45 +235,84 @@ Query for fungible assets for a specifc issuer party:
|
||||
:start-after: DOCSTART VaultQueryExample14
|
||||
:end-before: DOCEND VaultQueryExample14
|
||||
|
||||
Query for consumed fungible assets with a specific exit key:
|
||||
**Dynamic queries** (also using ``VaultQueryCriteria``) are an extension to the snapshot queries by returning an additional ``QueryResults`` return type in the form of an ``Observable<Vault.Update>``. Refer to `ReactiveX Observable <http://reactivex.io/documentation/observable.html>`_ for a detailed understanding and usage of this type.
|
||||
|
||||
Track unconsumed cash states:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART VaultQueryExample15
|
||||
:end-before: DOCEND VaultQueryExample15
|
||||
|
||||
Track unconsumed linear states:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART VaultQueryExample16
|
||||
:end-before: DOCEND VaultQueryExample16
|
||||
|
||||
.. note:: This will return both Deal and Linear states.
|
||||
|
||||
Track unconsumed deal states:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/kotlin/net/corda/node/services/vault/VaultQueryTests.kt
|
||||
:language: kotlin
|
||||
:start-after: DOCSTART VaultQueryExample17
|
||||
:end-before: DOCEND VaultQueryExample17
|
||||
|
||||
.. note:: This will return only Deal states.
|
||||
|
||||
Java examples
|
||||
^^^^^^^^^^^^^
|
||||
|
||||
Query for all consumed contract states:
|
||||
Query for all unconsumed linear states:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/java/net/corda/node/services/vault/VaultQueryJavaTests.java
|
||||
:language: java
|
||||
:start-after: DOCSTART VaultJavaQueryExample0
|
||||
:end-before: DOCEND VaultJavaQueryExample0
|
||||
|
||||
This example was previously executed using the deprecated method:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/java/net/corda/node/services/vault/VaultQueryJavaTests.java
|
||||
:language: java
|
||||
:start-after: DOCSTART VaultDeprecatedJavaQueryExample0
|
||||
:end-before: DOCEND VaultDeprecatedJavaQueryExample0
|
||||
|
||||
Query for all consumed cash states:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/java/net/corda/node/services/vault/VaultQueryJavaTests.java
|
||||
:language: java
|
||||
:start-after: DOCSTART VaultJavaQueryExample1
|
||||
:end-before: DOCEND VaultJavaQueryExample1
|
||||
|
||||
.. note:: This example was previously executed using the deprecated method:
|
||||
This example was previously executed using the deprecated method:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/java/net/corda/node/services/vault/VaultQueryJavaTests.java
|
||||
:language: java
|
||||
:start-after: DOCSTART VaultDeprecatedJavaQueryExample1
|
||||
:end-before: DOCEND VaultDeprecatedJavaQueryExample1
|
||||
.. literalinclude:: ../../node/src/test/java/net/corda/node/services/vault/VaultQueryJavaTests.java
|
||||
:language: java
|
||||
:start-after: DOCSTART VaultDeprecatedJavaQueryExample1
|
||||
:end-before: DOCEND VaultDeprecatedJavaQueryExample1
|
||||
|
||||
Query for all deal states:
|
||||
Query for consumed deal states or linear ids, specify a paging specification and sort by unique identifier:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/java/net/corda/node/services/vault/VaultQueryJavaTests.java
|
||||
:language: java
|
||||
:start-after: DOCSTART VaultJavaQueryExample2
|
||||
:end-before: DOCEND VaultJavaQueryExample2
|
||||
|
||||
.. note:: This example was previously executed using the deprecated method:
|
||||
Track unconsumed cash states:
|
||||
|
||||
.. literalinclude:: ../../node/src/test/java/net/corda/node/services/vault/VaultQueryJavaTests.java
|
||||
:language: java
|
||||
:start-after: DOCSTART VaultDeprecatedJavaQueryExample2
|
||||
:end-before: DOCEND VaultDeprecatedJavaQueryExample2
|
||||
.. literalinclude:: ../../node/src/test/java/net/corda/node/services/vault/VaultQueryJavaTests.java
|
||||
:language: java
|
||||
:start-after: DOCSTART VaultJavaQueryExample4
|
||||
:end-before: DOCEND VaultJavaQueryExample4
|
||||
|
||||
**Dynamic queries** (also using ``VaultQueryCriteria``) are an extension to the snapshot queries by returning an additional ``QueryResults`` return type in the form of an ``Observable<Vault.Update>``. Refer to `ReactiveX Observable <http://reactivex.io/documentation/observable.html>`_ for a detailed understanding and usage of this type.
|
||||
Track unconsumed deal states or linear states (with snapshot including specification of paging and sorting by unique identifier):
|
||||
|
||||
.. literalinclude:: ../../node/src/test/java/net/corda/node/services/vault/VaultQueryJavaTests.java
|
||||
:language: java
|
||||
:start-after: DOCSTART VaultJavaQueryExample4
|
||||
:end-before: DOCEND VaultJavaQueryExample4
|
||||
|
||||
Other use case scenarios
|
||||
------------------------
|
@ -7,4 +7,5 @@ Building a CorDapp
|
||||
cordapp-overview
|
||||
writing-cordapps
|
||||
api-index
|
||||
flow-cookbook
|
||||
cheat-sheet
|
@ -7,25 +7,84 @@ from the previous milestone release.
|
||||
UNRELEASED
|
||||
----------
|
||||
|
||||
* A new RPC has been added to support fuzzy matching of X.500 names, for instance, to translate from user input to
|
||||
an unambiguous identity by searching the network map.
|
||||
|
||||
* The node driver has moved to net.corda.testing.driver in the test-utils module
|
||||
|
||||
* Enable certificate validation in most scenarios (will be enforced in all cases in an upcoming milestone)
|
||||
|
||||
* Added DER encoded format for CompositeKey so they can be used in X.509 certificates
|
||||
|
||||
* Corrected several tests which made assumptions about counterparty keys, which are invalid when confidential identities
|
||||
are used
|
||||
|
||||
Milestone 13
|
||||
------------
|
||||
----------
|
||||
Special thank you to `Frederic Dalibard <https://github.com/FredericDalibard>`_, for his contribution which adds
|
||||
support for more currencies to the DemoBench and Explorer tools.
|
||||
|
||||
* Web API related collections ``CordaPluginRegistry.webApis`` and ``CordaPluginRegistry.staticServeDirs`` moved to
|
||||
``net.corda.webserver.services.WebServerPluginRegistry`` in ``webserver`` module.
|
||||
Classes serving Web API should now extend ``WebServerPluginRegistry`` instead of ``CordaPluginRegistry``
|
||||
and they should be registered in ``resources/META-INF/services/net.corda.webserver.services.WebServerPluginRegistry``.
|
||||
* A new Vault Query service:
|
||||
|
||||
* Implemented using JPA and Hibernate, this new service provides the ability to specify advanced queries using
|
||||
criteria specification sets for both vault attributes and custom contract specific attributes. In addition, new
|
||||
queries provide sorting and pagination capabilities.
|
||||
The new API provides two function variants which are exposed for usage within Flows and by RPC clients:
|
||||
- ``queryBy()`` for point-in-time snapshot queries
|
||||
(replaces several existing VaultService functions and a number of Kotlin-only extension functions)
|
||||
- ``trackBy()`` for snapshot and streaming updates
|
||||
(replaces the VaultService ``track()`` function and the RPC ``vaultAndUpdates()`` function)
|
||||
Existing VaultService API methods will be maintained as deprecated until the following milestone release.
|
||||
|
||||
* The NodeSchema service has been enhanced to automatically generate mapped objects for any ContractState objects
|
||||
that extend FungibleAsset or LinearState, such that common attributes of those parent states are persisted to
|
||||
two new vault tables: vault_fungible_states and vault_linear_states (and thus queryable using the new Vault Query
|
||||
service API).
|
||||
Similarly, two new common JPA superclass schemas (``CommonSchemaV1.FungibleState`` and
|
||||
``CommonSchemaV1.LinearState``) mirror the associated FungibleAsset and LinearState interface states to enable
|
||||
CorDapp developers to create new custom schemas by extension (rather than duplication of common attribute mappings)
|
||||
|
||||
* A new configurable field ``requiredSchemas`` has been added to the CordaPluginRegistry to enable CorDapps to
|
||||
register custom contract state schemas they wish to query using the new Vault Query service API (using the
|
||||
``VaultCustomQueryCriteria``).
|
||||
|
||||
* See :doc:`vault-query` for full details and code samples of using the new Vault Query service.
|
||||
|
||||
* Identity and cryptography related changes:
|
||||
|
||||
* Enable certificate validation in most scenarios (will be enforced in all cases in an upcoming milestone).
|
||||
|
||||
* Added DER encoded format for CompositeKey so they can be used in X.509 certificates.
|
||||
|
||||
* Corrected several tests which made assumptions about counterparty keys, which are invalid when confidential
|
||||
identities are used.
|
||||
|
||||
* A new RPC has been added to support fuzzy matching of X.500 names, for instance, to translate from user input to
|
||||
an unambiguous identity by searching the network map.
|
||||
|
||||
* A function for deterministic key derivation ``Crypto.deriveKeyPair(privateKey: PrivateKey, seed: ByteArray)``
|
||||
has been implemented to support deterministic ``KeyPair`` derivation using an existing private key and a seed
|
||||
as inputs. This operation is based on the HKDF scheme and it's a variant of the hardened parent-private ->
|
||||
child-private key derivation function of the BIP32 protocol, but it doesn't utilize extension chain codes.
|
||||
Currently, this function supports the following schemes: ECDSA secp256r1 (NIST P-256), ECDSA secp256k1 and
|
||||
EdDSA ed25519.
|
||||
|
||||
* A new ``ClassWhitelist`` implementation, ``AllButBlacklisted`` is used internally to blacklist classes/interfaces,
|
||||
which are not expected to be serialised during checkpoints, such as ``Thread``, ``Connection`` and ``HashSet``.
|
||||
This implementation supports inheritance and if a superclass or superinterface of a class is blacklisted, so is
|
||||
the class itself. An ``IllegalStateException`` informs the user if a class is blacklisted and such an exception is
|
||||
returned before checking for ``@CordaSerializable``; thus, blacklisting precedes annotation checking.
|
||||
|
||||
* ``TimeWindow`` has a new 5th factory method ``TimeWindow.fromStartAndDuration(fromTime: Instant, duration: Duration)``
|
||||
which takes a start-time and a period-of-validity (after this start-time) as inputs.
|
||||
|
||||
* The node driver has moved to net.corda.testing.driver in the test-utils module.
|
||||
|
||||
* Web API related collections ``CordaPluginRegistry.webApis`` and ``CordaPluginRegistry.staticServeDirs`` moved to
|
||||
``net.corda.webserver.services.WebServerPluginRegistry`` in ``webserver`` module.
|
||||
Classes serving Web API should now extend ``WebServerPluginRegistry`` instead of ``CordaPluginRegistry``
|
||||
and they should be registered in ``resources/META-INF/services/net.corda.webserver.services.WebServerPluginRegistry``.
|
||||
|
||||
* Added a flag to the driver that allows the running of started nodes in-process, allowing easier debugging.
|
||||
To enable use `driver(startNodesInProcess = true) { .. }`, or `startNode(startInSameProcess = true, ..)`
|
||||
to specify for individual nodes.
|
||||
|
||||
* Dependencies changes:
|
||||
* Upgraded Kotlin to v1.1.2.
|
||||
* Upgraded Dokka to v0.9.14.
|
||||
* Upgraded Gradle Plugins to 0.12.4.
|
||||
* Upgraded Apache ActiveMQ Artemis to v2.1.0.
|
||||
* Upgraded Netty to v4.1.9.Final.
|
||||
* Upgraded BouncyCastle to v1.57.
|
||||
* Upgraded Requery to v1.3.1.
|
||||
|
||||
Milestone 12
|
||||
------------
|
||||
|
@ -134,6 +134,10 @@ path to the node's base directory.
|
||||
does not exist, a developer keystore will be used if ``devMode`` is true. The node will exit if ``devMode`` is false
|
||||
and keystore does not exist.
|
||||
|
||||
:detectPublicIp: This flag toggles the auto IP detection behaviour, it is enabled by default. On startup the node will
|
||||
attempt to discover its externally visible IP address first by looking for any public addresses on its network
|
||||
interfaces, and then by sending an IP discovery request to the network map service. Set to ``false`` to disable.
|
||||
|
||||
:certificateSigningService: Certificate Signing Server address. It is used by the certificate signing request utility to
|
||||
obtain SSL certificate. (See :doc:`permissioning` for more information.)
|
||||
|
||||
|
@ -0,0 +1,498 @@
|
||||
package net.corda.docs;
|
||||
|
||||
import co.paralleluniverse.fibers.Suspendable;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import net.corda.contracts.asset.Cash;
|
||||
import net.corda.core.contracts.*;
|
||||
import net.corda.core.contracts.TransactionType.General;
|
||||
import net.corda.core.contracts.TransactionType.NotaryChange;
|
||||
import net.corda.core.crypto.SecureHash;
|
||||
import net.corda.core.flows.*;
|
||||
import net.corda.core.identity.Party;
|
||||
import net.corda.core.node.services.ServiceType;
|
||||
import net.corda.core.node.services.Vault;
|
||||
import net.corda.core.node.services.Vault.Page;
|
||||
import net.corda.core.node.services.vault.QueryCriteria.VaultQueryCriteria;
|
||||
import net.corda.core.transactions.LedgerTransaction;
|
||||
import net.corda.core.transactions.SignedTransaction;
|
||||
import net.corda.core.transactions.TransactionBuilder;
|
||||
import net.corda.core.transactions.WireTransaction;
|
||||
import net.corda.core.utilities.ProgressTracker;
|
||||
import net.corda.core.utilities.ProgressTracker.Step;
|
||||
import net.corda.core.utilities.UntrustworthyData;
|
||||
import net.corda.flows.CollectSignaturesFlow;
|
||||
import net.corda.flows.FinalityFlow;
|
||||
import net.corda.flows.ResolveTransactionsFlow;
|
||||
import net.corda.flows.SignTransactionFlow;
|
||||
import org.bouncycastle.asn1.x500.X500Name;
|
||||
|
||||
import java.security.PublicKey;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static net.corda.core.contracts.ContractsDSL.requireThat;
|
||||
import static net.corda.core.utilities.TestConstants.getDUMMY_PUBKEY_1;
|
||||
|
||||
// We group our two flows inside a singleton object to indicate that they work
|
||||
// together.
|
||||
public class FlowCookbookJava {
|
||||
// ``InitiatorFlow`` is our first flow, and will communicate with
|
||||
// ``ResponderFlow``, below.
|
||||
// We mark ``InitiatorFlow`` as an ``InitiatingFlow``, allowing it to be
|
||||
// started directly by the node.
|
||||
@InitiatingFlow
|
||||
// We also mark ``InitiatorFlow`` as ``StartableByRPC``, allowing the
|
||||
// node's owner to start the flow via RPC.
|
||||
@StartableByRPC
|
||||
// Every flow must subclass ``FlowLogic``. The generic indicates the
|
||||
// flow's return type.
|
||||
public static class InitiatorFlow extends FlowLogic<Void> {
|
||||
|
||||
private final boolean arg1;
|
||||
private final int arg2;
|
||||
private final Party counterparty;
|
||||
|
||||
public InitiatorFlow(boolean arg1, int arg2, Party counterparty) {
|
||||
this.arg1 = arg1;
|
||||
this.arg2 = arg2;
|
||||
this.counterparty = counterparty;
|
||||
}
|
||||
|
||||
/*----------------------------------
|
||||
* WIRING UP THE PROGRESS TRACKER *
|
||||
----------------------------------*/
|
||||
// Giving our flow a progress tracker allows us to see the flow's
|
||||
// progress visually in our node's CRaSH shell.
|
||||
// DOCSTART 17
|
||||
private static final Step ID_OTHER_NODES = new Step("Identifying other nodes on the network.");
|
||||
private static final Step SENDING_AND_RECEIVING_DATA = new Step("Sending data between parties.");
|
||||
private static final Step EXTRACTING_VAULT_STATES = new Step("Extracting states from the vault.");
|
||||
private static final Step OTHER_TX_COMPONENTS = new Step("Gathering a transaction's other components.");
|
||||
private static final Step TX_BUILDING = new Step("Building a transaction.");
|
||||
private static final Step TX_SIGNING = new Step("Signing a transaction.");
|
||||
private static final Step TX_VERIFICATION = new Step("Verifying a transaction.");
|
||||
private static final Step SIGS_GATHERING = new Step("Gathering a transaction's signatures.") {
|
||||
// Wiring up a child progress tracker allows us to see the
|
||||
// subflow's progress steps in our flow's progress tracker.
|
||||
@Override public ProgressTracker childProgressTracker() {
|
||||
return CollectSignaturesFlow.Companion.tracker();
|
||||
}
|
||||
};
|
||||
private static final Step FINALISATION = new Step("Finalising a transaction.") {
|
||||
@Override public ProgressTracker childProgressTracker() {
|
||||
return FinalityFlow.Companion.tracker();
|
||||
}
|
||||
};
|
||||
|
||||
private final ProgressTracker progressTracker = new ProgressTracker(
|
||||
ID_OTHER_NODES,
|
||||
SENDING_AND_RECEIVING_DATA,
|
||||
EXTRACTING_VAULT_STATES,
|
||||
OTHER_TX_COMPONENTS,
|
||||
TX_BUILDING,
|
||||
TX_SIGNING,
|
||||
TX_VERIFICATION,
|
||||
SIGS_GATHERING,
|
||||
FINALISATION
|
||||
);
|
||||
// DOCEND 17
|
||||
|
||||
@Suspendable
|
||||
@Override
|
||||
public Void call() throws FlowException {
|
||||
// We'll be using a dummy public key for demonstration purposes.
|
||||
// These are built in to Corda, and are generally used for writing
|
||||
// tests.
|
||||
PublicKey dummyPubKey = getDUMMY_PUBKEY_1();
|
||||
|
||||
/*---------------------------
|
||||
* IDENTIFYING OTHER NODES *
|
||||
---------------------------*/
|
||||
// DOCSTART 18
|
||||
progressTracker.setCurrentStep(ID_OTHER_NODES);
|
||||
// DOCEND 18
|
||||
|
||||
// A transaction generally needs a notary:
|
||||
// - To prevent double-spends if the transaction has inputs
|
||||
// - To serve as a timestamping authority if the transaction has a time-window
|
||||
// We retrieve a notary from the network map.
|
||||
// DOCSTART 1
|
||||
Party specificNotary = getServiceHub().getNetworkMapCache().getNotary(new X500Name("CN=Notary Service,O=R3,OU=corda,L=London,C=UK"));
|
||||
Party anyNotary = getServiceHub().getNetworkMapCache().getAnyNotary(null);
|
||||
// Unlike the first two methods, ``getNotaryNodes`` returns a
|
||||
// ``List<NodeInfo>``. We have to extract the notary identity of
|
||||
// the node we want.
|
||||
Party firstNotary = getServiceHub().getNetworkMapCache().getNotaryNodes().get(0).getNotaryIdentity();
|
||||
// DOCEND 1
|
||||
|
||||
// We may also need to identify a specific counterparty.
|
||||
// Again, we do so using the network map.
|
||||
// DOCSTART 2
|
||||
Party namedCounterparty = getServiceHub().getNetworkMapCache().getNodeByLegalName(new X500Name("CN=NodeA,O=NodeA,L=London,C=UK")).getLegalIdentity();
|
||||
Party keyedCounterparty = getServiceHub().getNetworkMapCache().getNodeByLegalIdentityKey(dummyPubKey).getLegalIdentity();
|
||||
Party firstCounterparty = getServiceHub().getNetworkMapCache().getPartyNodes().get(0).getLegalIdentity();
|
||||
// DOCEND 2
|
||||
|
||||
// Finally, we can use the map to identify nodes providing a
|
||||
// specific service (e.g. a regulator or an oracle).
|
||||
// DOCSTART 3
|
||||
Party regulator = getServiceHub().getNetworkMapCache().getNodesWithService(ServiceType.Companion.getRegulator()).get(0).getLegalIdentity();
|
||||
// DOCEND 3
|
||||
|
||||
/*------------------------------
|
||||
* SENDING AND RECEIVING DATA *
|
||||
------------------------------*/
|
||||
progressTracker.setCurrentStep(SENDING_AND_RECEIVING_DATA);
|
||||
|
||||
// We can send arbitrary data to a counterparty.
|
||||
// If this is the first ``send``, the counterparty will either:
|
||||
// 1. Ignore the message if they are not registered to respond
|
||||
// to messages from this flow.
|
||||
// 2. Start the flow they have registered to respond to this flow,
|
||||
// and run the flow until the first call to ``receive``, at
|
||||
// which point they process the message.
|
||||
// In other words, we are assuming that the counterparty is
|
||||
// registered to respond to this flow, and has a corresponding
|
||||
// ``receive`` call.
|
||||
// DOCSTART 4
|
||||
send(counterparty, new Object());
|
||||
// DOCEND 4
|
||||
|
||||
// We can wait to receive arbitrary data of a specific type from a
|
||||
// counterparty. Again, this implies a corresponding ``send`` call
|
||||
// in the counterparty's flow. A few scenarios:
|
||||
// - We never receive a message back. In the current design, the
|
||||
// flow is paused until the node's owner kills the flow.
|
||||
// - Instead of sending a message back, the counterparty throws a
|
||||
// ``FlowException``. This exception is propagated back to us,
|
||||
// and we can use the error message to establish what happened.
|
||||
// - We receive a message back, but it's of the wrong type. In
|
||||
// this case, a ``FlowException`` is thrown.
|
||||
// - We receive back a message of the correct type. All is good.
|
||||
//
|
||||
// Upon calling ``receive()`` (or ``sendAndReceive()``), the
|
||||
// ``FlowLogic`` is suspended until it receives a response.
|
||||
//
|
||||
// We receive the data wrapped in an ``UntrustworthyData``
|
||||
// instance. This is a reminder that the data we receive may not
|
||||
// be what it appears to be! We must unwrap the
|
||||
// ``UntrustworthyData`` using a lambda.
|
||||
// DOCSTART 5
|
||||
UntrustworthyData<Integer> packet1 = receive(Integer.class, counterparty);
|
||||
Integer integer = packet1.unwrap(data -> {
|
||||
// Perform checking on the object received.
|
||||
// T O D O: Check the received object.
|
||||
// Return the object.
|
||||
return data;
|
||||
});
|
||||
// DOCEND 5
|
||||
|
||||
// We can also use a single call to send data to a counterparty
|
||||
// and wait to receive data of a specific type back. The type of
|
||||
// data sent doesn't need to match the type of the data received
|
||||
// back.
|
||||
// DOCSTART 7
|
||||
UntrustworthyData<Boolean> packet2 = sendAndReceive(Boolean.class, counterparty, "You can send and receive any class!");
|
||||
Boolean bool = packet2.unwrap(data -> {
|
||||
// Perform checking on the object received.
|
||||
// T O D O: Check the received object.
|
||||
// Return the object.
|
||||
return data;
|
||||
});
|
||||
// DOCEND 7
|
||||
|
||||
// We're not limited to sending to and receiving from a single
|
||||
// counterparty. A flow can send messages to as many parties as it
|
||||
// likes, and each party can invoke a different response flow.
|
||||
// DOCSTART 6
|
||||
send(regulator, new Object());
|
||||
UntrustworthyData<Object> packet3 = receive(Object.class, regulator);
|
||||
// DOCEND 6
|
||||
|
||||
/*------------------------------------
|
||||
* EXTRACTING STATES FROM THE VAULT *
|
||||
------------------------------------*/
|
||||
progressTracker.setCurrentStep(EXTRACTING_VAULT_STATES);
|
||||
|
||||
// Let's assume there are already some ``DummyState``s in our
|
||||
// node's vault, stored there as a result of running past flows,
|
||||
// and we want to consume them in a transaction. There are many
|
||||
// ways to extract these states from our vault.
|
||||
|
||||
// For example, we would extract any unconsumed ``DummyState``s
|
||||
// from our vault as follows:
|
||||
VaultQueryCriteria criteria = new VaultQueryCriteria(Vault.StateStatus.UNCONSUMED);
|
||||
Page<DummyState> results = getServiceHub().getVaultQueryService().queryBy(DummyState.class, criteria);
|
||||
List<StateAndRef<DummyState>> dummyStates = results.getStates();
|
||||
|
||||
// For a full list of the available ways of extracting states from
|
||||
// the vault, see the Vault Query docs page.
|
||||
|
||||
// When building a transaction, input states are passed in as
|
||||
// ``StateRef`` instances, which pair the hash of the transaction
|
||||
// that generated the state with the state's index in the outputs
|
||||
// of that transaction.
|
||||
StateRef ourStateRef = new StateRef(SecureHash.sha256("DummyTransactionHash"), 0);
|
||||
// A ``StateAndRef`` pairs a ``StateRef`` with the state it points to.
|
||||
StateAndRef ourStateAndRef = getServiceHub().toStateAndRef(ourStateRef);
|
||||
|
||||
/*------------------------------------------
|
||||
* GATHERING OTHER TRANSACTION COMPONENTS *
|
||||
------------------------------------------*/
|
||||
progressTracker.setCurrentStep(OTHER_TX_COMPONENTS);
|
||||
|
||||
// Output states are constructed from scratch.
|
||||
DummyState ourOutput = new DummyState();
|
||||
// Or as copies of other states with some properties changed.
|
||||
DummyState ourOtherOutput = ourOutput.copy(77);
|
||||
|
||||
// Commands pair a ``CommandData`` instance with a list of
|
||||
// public keys. To be valid, the transaction requires a signature
|
||||
// matching every public key in all of the transaction's commands.
|
||||
CommandData commandData = new DummyContract.Commands.Create();
|
||||
PublicKey ourPubKey = getServiceHub().getLegalIdentityKey();
|
||||
PublicKey counterpartyPubKey = counterparty.getOwningKey();
|
||||
List<PublicKey> requiredSigners = ImmutableList.of(ourPubKey, counterpartyPubKey);
|
||||
Command ourCommand = new Command(commandData, requiredSigners);
|
||||
|
||||
// ``CommandData`` can either be:
|
||||
// 1. Of type ``TypeOnlyCommandData``, in which case it only
|
||||
// serves to attach signers to the transaction and possibly
|
||||
// fork the contract's verification logic.
|
||||
TypeOnlyCommandData typeOnlyCommandData = new DummyContract.Commands.Create();
|
||||
// 2. Include additional data which can be used by the contract
|
||||
// during verification, alongside fulfilling the roles above
|
||||
CommandData commandDataWithData = new Cash.Commands.Issue(12345678);
|
||||
|
||||
// Attachments are identified by their hash.
|
||||
// The attachment with the corresponding hash must have been
|
||||
// uploaded ahead of time via the node's RPC interface.
|
||||
SecureHash ourAttachment = SecureHash.sha256("DummyAttachment");
|
||||
|
||||
// Time windows can have a start and end time, or be open at either end.
|
||||
TimeWindow ourTimeWindow = TimeWindow.between(Instant.MIN, Instant.MAX);
|
||||
TimeWindow ourAfter = TimeWindow.fromOnly(Instant.MIN);
|
||||
TimeWindow ourBefore = TimeWindow.untilOnly(Instant.MAX);
|
||||
|
||||
/*------------------------
|
||||
* TRANSACTION BUILDING *
|
||||
------------------------*/
|
||||
progressTracker.setCurrentStep(TX_BUILDING);
|
||||
|
||||
// There are two types of transaction (notary-change and general),
|
||||
// and therefore two types of transaction builder:
|
||||
TransactionBuilder notaryChangeTxBuilder = new TransactionBuilder(NotaryChange.INSTANCE, specificNotary);
|
||||
TransactionBuilder regTxBuilder = new TransactionBuilder(General.INSTANCE, specificNotary);
|
||||
|
||||
// We add items to the transaction builder using ``TransactionBuilder.withItems``:
|
||||
regTxBuilder.withItems(
|
||||
// Inputs, as ``StateRef``s that reference to the outputs of previous transactions
|
||||
ourStateRef,
|
||||
// Outputs, as ``ContractState``s
|
||||
ourOutput,
|
||||
// Commands, as ``Command``s
|
||||
ourCommand
|
||||
);
|
||||
|
||||
// We can also add items using methods for the individual components:
|
||||
regTxBuilder.addInputState(ourStateAndRef);
|
||||
regTxBuilder.addOutputState(ourOutput);
|
||||
regTxBuilder.addCommand(ourCommand);
|
||||
regTxBuilder.addAttachment(ourAttachment);
|
||||
regTxBuilder.addTimeWindow(ourTimeWindow);
|
||||
|
||||
/*-----------------------
|
||||
* TRANSACTION SIGNING *
|
||||
-----------------------*/
|
||||
progressTracker.setCurrentStep(TX_SIGNING);
|
||||
|
||||
// We finalise the transaction by signing it,
|
||||
// converting it into a ``SignedTransaction``.
|
||||
SignedTransaction onceSignedTx = getServiceHub().signInitialTransaction(regTxBuilder);
|
||||
|
||||
// If instead this was a ``SignedTransaction`` that we'd received
|
||||
// from a counterparty and we needed to sign it, we would add our
|
||||
// signature using:
|
||||
SignedTransaction twiceSignedTx = getServiceHub().addSignature(onceSignedTx, dummyPubKey);
|
||||
|
||||
/*----------------------------
|
||||
* TRANSACTION VERIFICATION *
|
||||
----------------------------*/
|
||||
progressTracker.setCurrentStep(TX_VERIFICATION);
|
||||
|
||||
// Verifying a transaction will also verify every transaction in
|
||||
// the transaction's dependency chain. So if this was a
|
||||
// transaction we'd received from a counterparty and it had any
|
||||
// dependencies, we'd need to download all of these dependencies
|
||||
// using``ResolveTransactionsFlow`` before verifying it.
|
||||
// DOCSTART 13
|
||||
subFlow(new ResolveTransactionsFlow(twiceSignedTx, counterparty));
|
||||
// DOCEND 13
|
||||
|
||||
// We can also resolve a `StateRef` dependency chain.
|
||||
// DOCSTART 14
|
||||
subFlow(new ResolveTransactionsFlow(ImmutableSet.of(ourStateRef.getTxhash()), counterparty));
|
||||
// DOCEND 14
|
||||
|
||||
// We verify a transaction using the following one-liner:
|
||||
twiceSignedTx.getTx().toLedgerTransaction(getServiceHub()).verify();
|
||||
|
||||
// Let's break that down...
|
||||
|
||||
// A ``SignedTransaction`` is a pairing of a ``WireTransaction``
|
||||
// with signatures over this ``WireTransaction``. We don't verify
|
||||
// a signed transaction per se, but rather the ``WireTransaction``
|
||||
// it contains.
|
||||
WireTransaction wireTx = twiceSignedTx.getTx();
|
||||
// Before we can verify the transaction, we need the
|
||||
// ``ServiceHub`` to use our node's local storage to resolve the
|
||||
// transaction's inputs and attachments into actual objects,
|
||||
// rather than just references. We do this by converting the
|
||||
// ``WireTransaction`` into a ``LedgerTransaction``.
|
||||
LedgerTransaction ledgerTx = wireTx.toLedgerTransaction(getServiceHub());
|
||||
// We can now verify the transaction.
|
||||
ledgerTx.verify();
|
||||
|
||||
// We'll often want to perform our own additional verification
|
||||
// too. Just because a transaction is valid based on the contract
|
||||
// rules and requires our signature doesn't mean we have to
|
||||
// sign it! We need to make sure the transaction represents an
|
||||
// agreement we actually want to enter into.
|
||||
DummyState outputState = (DummyState) wireTx.getOutputs().get(0).getData();
|
||||
if (outputState.getMagicNumber() != 777) {
|
||||
throw new FlowException("We expected a magic number of 777.");
|
||||
}
|
||||
|
||||
// Of course, if you are not a required signer on the transaction,
|
||||
// you have no power to decide whether it is valid or not. If it
|
||||
// requires signatures from all the required signers and is
|
||||
// contractually valid, it's a valid ledger update.
|
||||
|
||||
/*------------------------
|
||||
* GATHERING SIGNATURES *
|
||||
------------------------*/
|
||||
progressTracker.setCurrentStep(SIGS_GATHERING);
|
||||
|
||||
// The list of parties who need to sign a transaction is dictated
|
||||
// by the transaction's commands. Once we've signed a transaction
|
||||
// ourselves, we can automatically gather the signatures of the
|
||||
// other required signers using ``CollectSignaturesFlow``.
|
||||
// The responder flow will need to call ``SignTransactionFlow``.
|
||||
// DOCSTART 15
|
||||
SignedTransaction fullySignedTx = subFlow(new CollectSignaturesFlow(twiceSignedTx, SIGS_GATHERING.childProgressTracker()));
|
||||
// DOCEND 15
|
||||
|
||||
/*------------------------------
|
||||
* FINALISING THE TRANSACTION *
|
||||
------------------------------*/
|
||||
progressTracker.setCurrentStep(FINALISATION);
|
||||
|
||||
// We notarise the transaction and get it recorded in the vault of
|
||||
// the participants of all the transaction's states.
|
||||
// DOCSTART 9
|
||||
SignedTransaction notarisedTx1 = subFlow(new FinalityFlow(fullySignedTx, FINALISATION.childProgressTracker())).get(0);
|
||||
// DOCEND 9
|
||||
// We can also choose to send it to additional parties who aren't one
|
||||
// of the state's participants.
|
||||
// DOCSTART 10
|
||||
Set<Party> additionalParties = ImmutableSet.of(regulator, regulator);
|
||||
SignedTransaction notarisedTx2 = subFlow(new FinalityFlow(ImmutableList.of(fullySignedTx), additionalParties, FINALISATION.childProgressTracker())).get(0);
|
||||
// DOCEND 10
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ``ResponderFlow`` is our second flow, and will communicate with
|
||||
// ``InitiatorFlow``.
|
||||
// We mark ``ResponderFlow`` as an ``InitiatedByFlow``, meaning that it
|
||||
// can only be started in response to a message from its initiating flow.
|
||||
// That's ``InitiatorFlow`` in this case.
|
||||
// Each node also has several flow pairs registered by default - see
|
||||
// ``AbstractNode.installCoreFlows``.
|
||||
@InitiatedBy(InitiatorFlow.class)
|
||||
public static class ResponderFlow extends FlowLogic<Void> {
|
||||
|
||||
private final Party counterparty;
|
||||
|
||||
public ResponderFlow(Party counterparty) {
|
||||
this.counterparty = counterparty;
|
||||
}
|
||||
|
||||
private static final Step RECEIVING_AND_SENDING_DATA = new Step("Sending data between parties.");
|
||||
private static final Step SIGNING = new Step("Responding to CollectSignaturesFlow.");
|
||||
private static final Step FINALISATION = new Step("Finalising a transaction.");
|
||||
|
||||
private final ProgressTracker progressTracker = new ProgressTracker(
|
||||
RECEIVING_AND_SENDING_DATA,
|
||||
SIGNING,
|
||||
FINALISATION
|
||||
);
|
||||
|
||||
@Suspendable
|
||||
@Override
|
||||
public Void call() throws FlowException {
|
||||
// The ``ResponderFlow` has all the same APIs available. It looks
|
||||
// up network information, sends and receives data, and constructs
|
||||
// transactions in exactly the same way.
|
||||
|
||||
/*------------------------------
|
||||
* SENDING AND RECEIVING DATA *
|
||||
-----------------------------*/
|
||||
progressTracker.setCurrentStep(RECEIVING_AND_SENDING_DATA);
|
||||
|
||||
// We need to respond to the messages sent by the initiator:
|
||||
// 1. They sent us an ``Any`` instance
|
||||
// 2. They waited to receive an ``Integer`` instance back
|
||||
// 3. They sent a ``String`` instance and waited to receive a
|
||||
// ``Boolean`` instance back
|
||||
// Our side of the flow must mirror these calls.
|
||||
// DOCSTART 8
|
||||
Object obj = receive(Object.class, counterparty).unwrap(data -> data);
|
||||
String string = sendAndReceive(String.class, counterparty, 99).unwrap(data -> data);
|
||||
send(counterparty, true);
|
||||
// DOCEND 8
|
||||
|
||||
/*-----------------------------------------
|
||||
* RESPONDING TO COLLECT_SIGNATURES_FLOW *
|
||||
-----------------------------------------*/
|
||||
progressTracker.setCurrentStep(SIGNING);
|
||||
|
||||
// The responder will often need to respond to a call to
|
||||
// ``CollectSignaturesFlow``. It does so my invoking its own
|
||||
// ``SignTransactionFlow`` subclass.
|
||||
// DOCSTART 16
|
||||
class SignTxFlow extends SignTransactionFlow {
|
||||
private SignTxFlow(Party otherParty, ProgressTracker progressTracker) {
|
||||
super(otherParty, progressTracker);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void checkTransaction(SignedTransaction stx) {
|
||||
requireThat(require -> {
|
||||
// Any additional checking we see fit...
|
||||
DummyState outputState = (DummyState) stx.getTx().getOutputs().get(0).getData();
|
||||
assert (outputState.getMagicNumber() == 777);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
subFlow(new SignTxFlow(counterparty, SignTransactionFlow.Companion.tracker()));
|
||||
// DOCEND 16
|
||||
|
||||
/*------------------------------
|
||||
* FINALISING THE TRANSACTION *
|
||||
------------------------------*/
|
||||
progressTracker.setCurrentStep(FINALISATION);
|
||||
|
||||
// Nothing to do here! As long as some other party calls
|
||||
// ``FinalityFlow``, the recording of the transaction on our node
|
||||
// we be handled automatically.
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,474 @@
|
||||
package net.corda.docs
|
||||
|
||||
import co.paralleluniverse.fibers.Suspendable
|
||||
import net.corda.contracts.asset.Cash
|
||||
import net.corda.core.contracts.*
|
||||
import net.corda.core.contracts.TransactionType.General
|
||||
import net.corda.core.contracts.TransactionType.NotaryChange
|
||||
import net.corda.core.crypto.SecureHash
|
||||
import net.corda.core.flows.*
|
||||
import net.corda.core.identity.Party
|
||||
import net.corda.core.node.services.ServiceType
|
||||
import net.corda.core.node.services.Vault.Page
|
||||
import net.corda.core.node.services.queryBy
|
||||
import net.corda.core.node.services.vault.QueryCriteria.VaultQueryCriteria
|
||||
import net.corda.core.transactions.LedgerTransaction
|
||||
import net.corda.core.transactions.SignedTransaction
|
||||
import net.corda.core.transactions.TransactionBuilder
|
||||
import net.corda.core.transactions.WireTransaction
|
||||
import net.corda.core.utilities.DUMMY_PUBKEY_1
|
||||
import net.corda.core.utilities.ProgressTracker
|
||||
import net.corda.core.utilities.ProgressTracker.Step
|
||||
import net.corda.core.utilities.UntrustworthyData
|
||||
import net.corda.core.utilities.unwrap
|
||||
import net.corda.flows.CollectSignaturesFlow
|
||||
import net.corda.flows.FinalityFlow
|
||||
import net.corda.flows.ResolveTransactionsFlow
|
||||
import net.corda.flows.SignTransactionFlow
|
||||
import org.bouncycastle.asn1.x500.X500Name
|
||||
import java.security.PublicKey
|
||||
import java.time.Instant
|
||||
|
||||
// We group our two flows inside a singleton object to indicate that they work
|
||||
// together.
|
||||
object FlowCookbook {
|
||||
// ``InitiatorFlow`` is our first flow, and will communicate with
|
||||
// ``ResponderFlow``, below.
|
||||
// We mark ``InitiatorFlow`` as an ``InitiatingFlow``, allowing it to be
|
||||
// started directly by the node.
|
||||
@InitiatingFlow
|
||||
// We also mark ``InitiatorFlow`` as ``StartableByRPC``, allowing the
|
||||
// node's owner to start the flow via RPC.
|
||||
@StartableByRPC
|
||||
// Every flow must subclass ``FlowLogic``. The generic indicates the
|
||||
// flow's return type.
|
||||
class InitiatorFlow(val arg1: Boolean, val arg2: Int, val counterparty: Party) : FlowLogic<Unit>() {
|
||||
|
||||
/**---------------------------------
|
||||
* WIRING UP THE PROGRESS TRACKER *
|
||||
---------------------------------**/
|
||||
// Giving our flow a progress tracker allows us to see the flow's
|
||||
// progress visually in our node's CRaSH shell.
|
||||
// DOCSTART 17
|
||||
companion object {
|
||||
object ID_OTHER_NODES : Step("Identifying other nodes on the network.")
|
||||
object SENDING_AND_RECEIVING_DATA : Step("Sending data between parties.")
|
||||
object EXTRACTING_VAULT_STATES : Step("Extracting states from the vault.")
|
||||
object OTHER_TX_COMPONENTS : Step("Gathering a transaction's other components.")
|
||||
object TX_BUILDING : Step("Building a transaction.")
|
||||
object TX_SIGNING : Step("Signing a transaction.")
|
||||
object TX_VERIFICATION : Step("Verifying a transaction.")
|
||||
object SIGS_GATHERING : Step("Gathering a transaction's signatures.") {
|
||||
// Wiring up a child progress tracker allows us to see the
|
||||
// subflow's progress steps in our flow's progress tracker.
|
||||
override fun childProgressTracker() = CollectSignaturesFlow.tracker()
|
||||
}
|
||||
object FINALISATION : Step("Finalising a transaction.") {
|
||||
override fun childProgressTracker() = FinalityFlow.tracker()
|
||||
}
|
||||
|
||||
fun tracker() = ProgressTracker(
|
||||
ID_OTHER_NODES,
|
||||
SENDING_AND_RECEIVING_DATA,
|
||||
EXTRACTING_VAULT_STATES,
|
||||
OTHER_TX_COMPONENTS,
|
||||
TX_BUILDING,
|
||||
TX_SIGNING,
|
||||
TX_VERIFICATION,
|
||||
SIGS_GATHERING,
|
||||
FINALISATION
|
||||
)
|
||||
}
|
||||
// DOCEND 17
|
||||
|
||||
override val progressTracker: ProgressTracker = tracker()
|
||||
|
||||
@Suspendable
|
||||
override fun call() {
|
||||
// We'll be using a dummy public key for demonstration purposes.
|
||||
// These are built in to Corda, and are generally used for writing
|
||||
// tests.
|
||||
val dummyPubKey: PublicKey = DUMMY_PUBKEY_1
|
||||
|
||||
/**--------------------------
|
||||
* IDENTIFYING OTHER NODES *
|
||||
--------------------------**/
|
||||
// DOCSTART 18
|
||||
progressTracker.currentStep = ID_OTHER_NODES
|
||||
// DOCEND 18
|
||||
|
||||
// A transaction generally needs a notary:
|
||||
// - To prevent double-spends if the transaction has inputs
|
||||
// - To serve as a timestamping authority if the transaction has a time-window
|
||||
// We retrieve the notary from the network map.
|
||||
// DOCSTART 1
|
||||
val specificNotary: Party? = serviceHub.networkMapCache.getNotary(X500Name("CN=Notary Service,O=R3,OU=corda,L=London,C=UK"))
|
||||
val anyNotary: Party? = serviceHub.networkMapCache.getAnyNotary()
|
||||
// Unlike the first two methods, ``getNotaryNodes`` returns a
|
||||
// ``List<NodeInfo>``. We have to extract the notary identity of
|
||||
// the node we want.
|
||||
val firstNotary: Party = serviceHub.networkMapCache.notaryNodes[0].notaryIdentity
|
||||
// DOCEND 1
|
||||
|
||||
// We may also need to identify a specific counterparty. Again, we
|
||||
// do so using the network map.
|
||||
// DOCSTART 2
|
||||
val namedCounterparty: Party? = serviceHub.networkMapCache.getNodeByLegalName(X500Name("CN=NodeA,O=NodeA,L=London,C=UK"))?.legalIdentity
|
||||
val keyedCounterparty: Party? = serviceHub.networkMapCache.getNodeByLegalIdentityKey(dummyPubKey)?.legalIdentity
|
||||
val firstCounterparty: Party = serviceHub.networkMapCache.partyNodes[0].legalIdentity
|
||||
// DOCEND 2
|
||||
|
||||
// Finally, we can use the map to identify nodes providing a
|
||||
// specific service (e.g. a regulator or an oracle).
|
||||
// DOCSTART 3
|
||||
val regulator: Party = serviceHub.networkMapCache.getNodesWithService(ServiceType.regulator)[0].legalIdentity
|
||||
// DOCEND 3
|
||||
|
||||
/**-----------------------------
|
||||
* SENDING AND RECEIVING DATA *
|
||||
-----------------------------**/
|
||||
progressTracker.currentStep = SENDING_AND_RECEIVING_DATA
|
||||
|
||||
// We can send arbitrary data to a counterparty.
|
||||
// If this is the first ``send``, the counterparty will either:
|
||||
// 1. Ignore the message if they are not registered to respond
|
||||
// to messages from this flow.
|
||||
// 2. Start the flow they have registered to respond to this flow,
|
||||
// and run the flow until the first call to ``receive``, at
|
||||
// which point they process the message.
|
||||
// In other words, we are assuming that the counterparty is
|
||||
// registered to respond to this flow, and has a corresponding
|
||||
// ``receive`` call.
|
||||
// DOCSTART 4
|
||||
send(counterparty, Any())
|
||||
// DOCEND 4
|
||||
|
||||
// We can wait to receive arbitrary data of a specific type from a
|
||||
// counterparty. Again, this implies a corresponding ``send`` call
|
||||
// in the counterparty's flow. A few scenarios:
|
||||
// - We never receive a message back. In the current design, the
|
||||
// flow is paused until the node's owner kills the flow.
|
||||
// - Instead of sending a message back, the counterparty throws a
|
||||
// ``FlowException``. This exception is propagated back to us,
|
||||
// and we can use the error message to establish what happened.
|
||||
// - We receive a message back, but it's of the wrong type. In
|
||||
// this case, a ``FlowException`` is thrown.
|
||||
// - We receive back a message of the correct type. All is good.
|
||||
//
|
||||
// Upon calling ``receive()`` (or ``sendAndReceive()``), the
|
||||
// ``FlowLogic`` is suspended until it receives a response.
|
||||
//
|
||||
// We receive the data wrapped in an ``UntrustworthyData``
|
||||
// instance. This is a reminder that the data we receive may not
|
||||
// be what it appears to be! We must unwrap the
|
||||
// ``UntrustworthyData`` using a lambda.
|
||||
// DOCSTART 5
|
||||
val packet1: UntrustworthyData<Int> = receive<Int>(counterparty)
|
||||
val int: Int = packet1.unwrap { data ->
|
||||
// Perform checking on the object received.
|
||||
// T O D O: Check the received object.
|
||||
// Return the object.
|
||||
data
|
||||
}
|
||||
// DOCEND 5
|
||||
|
||||
// We can also use a single call to send data to a counterparty
|
||||
// and wait to receive data of a specific type back. The type of
|
||||
// data sent doesn't need to match the type of the data received
|
||||
// back.
|
||||
// DOCSTART 7
|
||||
val packet2: UntrustworthyData<Boolean> = sendAndReceive<Boolean>(counterparty, "You can send and receive any class!")
|
||||
val boolean: Boolean = packet2.unwrap { data ->
|
||||
// Perform checking on the object received.
|
||||
// T O D O: Check the received object.
|
||||
// Return the object.
|
||||
data
|
||||
}
|
||||
// DOCEND 7
|
||||
|
||||
// We're not limited to sending to and receiving from a single
|
||||
// counterparty. A flow can send messages to as many parties as it
|
||||
// likes, and each party can invoke a different response flow.
|
||||
// DOCSTART 6
|
||||
send(regulator, Any())
|
||||
val packet3: UntrustworthyData<Any> = receive<Any>(regulator)
|
||||
// DOCEND 6
|
||||
|
||||
/**-----------------------------------
|
||||
* EXTRACTING STATES FROM THE VAULT *
|
||||
-----------------------------------**/
|
||||
progressTracker.currentStep = EXTRACTING_VAULT_STATES
|
||||
|
||||
// Let's assume there are already some ``DummyState``s in our
|
||||
// node's vault, stored there as a result of running past flows,
|
||||
// and we want to consume them in a transaction. There are many
|
||||
// ways to extract these states from our vault.
|
||||
|
||||
// For example, we would extract any unconsumed ``DummyState``s
|
||||
// from our vault as follows:
|
||||
val criteria: VaultQueryCriteria = VaultQueryCriteria() // default is UNCONSUMED
|
||||
val results: Page<DummyState> = serviceHub.vaultQueryService.queryBy<DummyState>(criteria)
|
||||
val dummyStates: List<StateAndRef<DummyState>> = results.states
|
||||
|
||||
// For a full list of the available ways of extracting states from
|
||||
// the vault, see the Vault Query docs page.
|
||||
|
||||
// When building a transaction, input states are passed in as
|
||||
// ``StateRef`` instances, which pair the hash of the transaction
|
||||
// that generated the state with the state's index in the outputs
|
||||
// of that transaction.
|
||||
val ourStateRef: StateRef = StateRef(SecureHash.sha256("DummyTransactionHash"), 0)
|
||||
// A ``StateAndRef`` pairs a ``StateRef`` with the state it points to.
|
||||
val ourStateAndRef: StateAndRef<DummyState> = serviceHub.toStateAndRef<DummyState>(ourStateRef)
|
||||
|
||||
/**-----------------------------------------
|
||||
* GATHERING OTHER TRANSACTION COMPONENTS *
|
||||
-----------------------------------------**/
|
||||
progressTracker.currentStep = OTHER_TX_COMPONENTS
|
||||
|
||||
// Output states are constructed from scratch.
|
||||
val ourOutput: DummyState = DummyState()
|
||||
// Or as copies of other states with some properties changed.
|
||||
val ourOtherOutput: DummyState = ourOutput.copy(magicNumber = 77)
|
||||
|
||||
// Commands pair a ``CommandData`` instance with a list of
|
||||
// public keys. To be valid, the transaction requires a signature
|
||||
// matching every public key in all of the transaction's commands.
|
||||
val commandData: CommandData = DummyContract.Commands.Create()
|
||||
val ourPubKey: PublicKey = serviceHub.legalIdentityKey
|
||||
val counterpartyPubKey: PublicKey = counterparty.owningKey
|
||||
val requiredSigners: List<PublicKey> = listOf(ourPubKey, counterpartyPubKey)
|
||||
val ourCommand: Command = Command(commandData, requiredSigners)
|
||||
|
||||
// ``CommandData`` can either be:
|
||||
// 1. Of type ``TypeOnlyCommandData``, in which case it only
|
||||
// serves to attach signers to the transaction and possibly
|
||||
// fork the contract's verification logic.
|
||||
val typeOnlyCommandData: TypeOnlyCommandData = DummyContract.Commands.Create()
|
||||
// 2. Include additional data which can be used by the contract
|
||||
// during verification, alongside fulfilling the roles above
|
||||
val commandDataWithData: CommandData = Cash.Commands.Issue(nonce = 12345678)
|
||||
|
||||
// Attachments are identified by their hash.
|
||||
// The attachment with the corresponding hash must have been
|
||||
// uploaded ahead of time via the node's RPC interface.
|
||||
val ourAttachment: SecureHash = SecureHash.sha256("DummyAttachment")
|
||||
|
||||
// Time windows can have a start and end time, or be open at either end.
|
||||
val ourTimeWindow: TimeWindow = TimeWindow.between(Instant.MIN, Instant.MAX)
|
||||
val ourAfter: TimeWindow = TimeWindow.fromOnly(Instant.MIN)
|
||||
val ourBefore: TimeWindow = TimeWindow.untilOnly(Instant.MAX)
|
||||
|
||||
/**-----------------------
|
||||
* TRANSACTION BUILDING *
|
||||
-----------------------**/
|
||||
progressTracker.currentStep = TX_BUILDING
|
||||
|
||||
// There are two types of transaction (notary-change and general),
|
||||
// and therefore two types of transaction builder:
|
||||
val notaryChangeTxBuilder: TransactionBuilder = TransactionBuilder(NotaryChange, specificNotary)
|
||||
val regTxBuilder: TransactionBuilder = TransactionBuilder(General, specificNotary)
|
||||
|
||||
// We add items to the transaction builder using ``TransactionBuilder.withItems``:
|
||||
regTxBuilder.withItems(
|
||||
// Inputs, as ``StateRef``s that reference to the outputs of previous transactions
|
||||
ourStateRef,
|
||||
// Outputs, as ``ContractState``s
|
||||
ourOutput,
|
||||
// Commands, as ``Command``s
|
||||
ourCommand
|
||||
)
|
||||
|
||||
// We can also add items using methods for the individual components:
|
||||
regTxBuilder.addInputState(ourStateAndRef)
|
||||
regTxBuilder.addOutputState(ourOutput)
|
||||
regTxBuilder.addCommand(ourCommand)
|
||||
regTxBuilder.addAttachment(ourAttachment)
|
||||
regTxBuilder.addTimeWindow(ourTimeWindow)
|
||||
|
||||
/**----------------------
|
||||
* TRANSACTION SIGNING *
|
||||
----------------------**/
|
||||
progressTracker.currentStep = TX_SIGNING
|
||||
|
||||
// We finalise the transaction by signing it, converting it into a
|
||||
// ``SignedTransaction``.
|
||||
val onceSignedTx: SignedTransaction = serviceHub.signInitialTransaction(regTxBuilder)
|
||||
|
||||
// If instead this was a ``SignedTransaction`` that we'd received
|
||||
// from a counterparty and we needed to sign it, we would add our
|
||||
// signature using:
|
||||
val twiceSignedTx: SignedTransaction = serviceHub.addSignature(onceSignedTx, dummyPubKey)
|
||||
|
||||
// In practice, however, the process of gathering every signature
|
||||
// but the first can be automated using ``CollectSignaturesFlow``.
|
||||
// See the "Gathering Signatures" section below.
|
||||
|
||||
/**---------------------------
|
||||
* TRANSACTION VERIFICATION *
|
||||
---------------------------**/
|
||||
progressTracker.currentStep = TX_VERIFICATION
|
||||
|
||||
// Verifying a transaction will also verify every transaction in
|
||||
// the transaction's dependency chain. So if this was a
|
||||
// transaction we'd received from a counterparty and it had any
|
||||
// dependencies, we'd need to download all of these dependencies
|
||||
// using``ResolveTransactionsFlow`` before verifying it.
|
||||
// DOCSTART 13
|
||||
subFlow(ResolveTransactionsFlow(twiceSignedTx, counterparty))
|
||||
// DOCEND 13
|
||||
|
||||
// We can also resolve a `StateRef` dependency chain.
|
||||
// DOCSTART 14
|
||||
subFlow(ResolveTransactionsFlow(setOf(ourStateRef.txhash), counterparty))
|
||||
// DOCEND 14
|
||||
|
||||
// We verify a transaction using the following one-liner:
|
||||
twiceSignedTx.tx.toLedgerTransaction(serviceHub).verify()
|
||||
|
||||
// Let's break that down...
|
||||
|
||||
// A ``SignedTransaction`` is a pairing of a ``WireTransaction``
|
||||
// with signatures over this ``WireTransaction``. We don't verify
|
||||
// a signed transaction per se, but rather the ``WireTransaction``
|
||||
// it contains.
|
||||
val wireTx: WireTransaction = twiceSignedTx.tx
|
||||
// Before we can verify the transaction, we need the
|
||||
// ``ServiceHub`` to use our node's local storage to resolve the
|
||||
// transaction's inputs and attachments into actual objects,
|
||||
// rather than just references. We do this by converting the
|
||||
// ``WireTransaction`` into a ``LedgerTransaction``.
|
||||
val ledgerTx: LedgerTransaction = wireTx.toLedgerTransaction(serviceHub)
|
||||
// We can now verify the transaction.
|
||||
ledgerTx.verify()
|
||||
|
||||
// We'll often want to perform our own additional verification
|
||||
// too. Just because a transaction is valid based on the contract
|
||||
// rules and requires our signature doesn't mean we have to
|
||||
// sign it! We need to make sure the transaction represents an
|
||||
// agreement we actually want to enter into.
|
||||
val outputState: DummyState = wireTx.outputs.single().data as DummyState
|
||||
if (outputState.magicNumber == 777) {
|
||||
// ``FlowException`` is a special exception type. It will be
|
||||
// propagated back to any counterparty flows waiting for a
|
||||
// message from this flow, notifying them that the flow has
|
||||
// failed.
|
||||
throw FlowException("We expected a magic number of 777.")
|
||||
}
|
||||
|
||||
// Of course, if you are not a required signer on the transaction,
|
||||
// you have no power to decide whether it is valid or not. If it
|
||||
// requires signatures from all the required signers and is
|
||||
// contractually valid, it's a valid ledger update.
|
||||
|
||||
/**-----------------------
|
||||
* GATHERING SIGNATURES *
|
||||
-----------------------**/
|
||||
progressTracker.currentStep = SIGS_GATHERING
|
||||
|
||||
// The list of parties who need to sign a transaction is dictated
|
||||
// by the transaction's commands. Once we've signed a transaction
|
||||
// ourselves, we can automatically gather the signatures of the
|
||||
// other required signers using ``CollectSignaturesFlow``.
|
||||
// The responder flow will need to call ``SignTransactionFlow``.
|
||||
// DOCSTART 15
|
||||
val fullySignedTx: SignedTransaction = subFlow(CollectSignaturesFlow(twiceSignedTx, SIGS_GATHERING.childProgressTracker()))
|
||||
// DOCEND 15
|
||||
|
||||
/**-----------------------------
|
||||
* FINALISING THE TRANSACTION *
|
||||
-----------------------------**/
|
||||
progressTracker.currentStep = FINALISATION
|
||||
|
||||
// We notarise the transaction and get it recorded in the vault of
|
||||
// the participants of all the transaction's states.
|
||||
// DOCSTART 9
|
||||
val notarisedTx1: SignedTransaction = subFlow(FinalityFlow(fullySignedTx, FINALISATION.childProgressTracker())).single()
|
||||
// DOCEND 9
|
||||
// We can also choose to send it to additional parties who aren't one
|
||||
// of the state's participants.
|
||||
// DOCSTART 10
|
||||
val additionalParties: Set<Party> = setOf(regulator)
|
||||
val notarisedTx2: SignedTransaction = subFlow(FinalityFlow(listOf(fullySignedTx), additionalParties, FINALISATION.childProgressTracker())).single()
|
||||
// DOCEND 10
|
||||
}
|
||||
}
|
||||
|
||||
// ``ResponderFlow`` is our second flow, and will communicate with
|
||||
// ``InitiatorFlow``.
|
||||
// We mark ``ResponderFlow`` as an ``InitiatedByFlow``, meaning that it
|
||||
// can only be started in response to a message from its initiating flow.
|
||||
// That's ``InitiatorFlow`` in this case.
|
||||
// Each node also has several flow pairs registered by default - see
|
||||
// ``AbstractNode.installCoreFlows``.
|
||||
@InitiatedBy(InitiatorFlow::class)
|
||||
class ResponderFlow(val counterparty: Party) : FlowLogic<Unit>() {
|
||||
|
||||
companion object {
|
||||
object RECEIVING_AND_SENDING_DATA : Step("Sending data between parties.")
|
||||
object SIGNING : Step("Responding to CollectSignaturesFlow.")
|
||||
object FINALISATION : Step("Finalising a transaction.")
|
||||
|
||||
fun tracker() = ProgressTracker(
|
||||
RECEIVING_AND_SENDING_DATA,
|
||||
SIGNING,
|
||||
FINALISATION
|
||||
)
|
||||
}
|
||||
|
||||
override val progressTracker: ProgressTracker = tracker()
|
||||
|
||||
@Suspendable
|
||||
override fun call() {
|
||||
// The ``ResponderFlow` has all the same APIs available. It looks
|
||||
// up network information, sends and receives data, and constructs
|
||||
// transactions in exactly the same way.
|
||||
|
||||
/**-----------------------------
|
||||
* SENDING AND RECEIVING DATA *
|
||||
-----------------------------**/
|
||||
progressTracker.currentStep = RECEIVING_AND_SENDING_DATA
|
||||
|
||||
// We need to respond to the messages sent by the initiator:
|
||||
// 1. They sent us an ``Any`` instance
|
||||
// 2. They waited to receive an ``Integer`` instance back
|
||||
// 3. They sent a ``String`` instance and waited to receive a
|
||||
// ``Boolean`` instance back
|
||||
// Our side of the flow must mirror these calls.
|
||||
// DOCSTART 8
|
||||
val any: Any = receive<Any>(counterparty).unwrap { data -> data }
|
||||
val string: String = sendAndReceive<String>(counterparty, 99).unwrap { data -> data }
|
||||
send(counterparty, true)
|
||||
// DOCEND 8
|
||||
|
||||
/**----------------------------------------
|
||||
* RESPONDING TO COLLECT_SIGNATURES_FLOW *
|
||||
----------------------------------------**/
|
||||
progressTracker.currentStep = SIGNING
|
||||
|
||||
// The responder will often need to respond to a call to
|
||||
// ``CollectSignaturesFlow``. It does so my invoking its own
|
||||
// ``SignTransactionFlow`` subclass.
|
||||
// DOCSTART 16
|
||||
val signTransactionFlow: SignTransactionFlow = object : SignTransactionFlow(counterparty) {
|
||||
override fun checkTransaction(stx: SignedTransaction) = requireThat {
|
||||
// Any additional checking we see fit...
|
||||
val outputState = stx.tx.outputs.single().data as DummyState
|
||||
assert(outputState.magicNumber == 777)
|
||||
}
|
||||
}
|
||||
|
||||
subFlow(signTransactionFlow)
|
||||
// DOCEND 16
|
||||
|
||||
/**-----------------------------
|
||||
* FINALISING THE TRANSACTION *
|
||||
-----------------------------**/
|
||||
progressTracker.currentStep = FINALISATION
|
||||
|
||||
// Nothing to do here! As long as some other party calls
|
||||
// ``FinalityFlow``, the recording of the transaction on our node
|
||||
// we be handled automatically.
|
||||
}
|
||||
}
|
||||
}
|
@ -2,11 +2,14 @@ package net.corda.docs
|
||||
|
||||
import net.corda.core.contracts.LinearState
|
||||
import net.corda.core.contracts.StateAndRef
|
||||
import net.corda.core.contracts.StateRef
|
||||
import net.corda.core.contracts.UniqueIdentifier
|
||||
import net.corda.core.getOrThrow
|
||||
import net.corda.core.node.ServiceHub
|
||||
import net.corda.core.node.services.ServiceInfo
|
||||
import net.corda.core.node.services.linearHeadsOfType
|
||||
import net.corda.core.node.services.Vault
|
||||
import net.corda.core.node.services.queryBy
|
||||
import net.corda.core.node.services.vault.QueryCriteria
|
||||
import net.corda.core.node.services.vault.and
|
||||
import net.corda.core.toFuture
|
||||
import net.corda.core.utilities.DUMMY_NOTARY
|
||||
import net.corda.core.utilities.DUMMY_NOTARY_KEY
|
||||
@ -26,10 +29,10 @@ class WorkflowTransactionBuildTutorialTest {
|
||||
lateinit var nodeB: MockNetwork.MockNode
|
||||
|
||||
// Helper method to locate the latest Vault version of a LinearState from a possibly out of date StateRef
|
||||
private inline fun <reified T : LinearState> ServiceHub.latest(ref: StateRef): StateAndRef<T> {
|
||||
val linearHeads = vaultService.linearHeadsOfType<T>()
|
||||
val original = storageService.validatedTransactions.getTransaction(ref.txhash)!!.tx.outRef<T>(ref.index)
|
||||
return linearHeads[original.state.data.linearId]!!
|
||||
private inline fun <reified T : LinearState> ServiceHub.latest(ref: UniqueIdentifier): StateAndRef<T> {
|
||||
val linearHeads = vaultQueryService.queryBy<T>(QueryCriteria.LinearStateQueryCriteria(linearId = listOf(ref))
|
||||
.and(QueryCriteria.VaultQueryCriteria(status = Vault.StateStatus.UNCONSUMED)))
|
||||
return linearHeads.states.single()
|
||||
}
|
||||
|
||||
@Before
|
||||
@ -59,14 +62,15 @@ class WorkflowTransactionBuildTutorialTest {
|
||||
val flow1 = nodeA.services.startFlow(SubmitTradeApprovalFlow("1234", nodeB.info.legalIdentity))
|
||||
// Wait for the flow to finish
|
||||
val proposalRef = flow1.resultFuture.getOrThrow()
|
||||
val proposalLinearId = proposalRef.state.data.linearId
|
||||
// Wait for NodeB to include it's copy in the vault
|
||||
nodeBVaultUpdate.get()
|
||||
// Fetch the latest copy of the state from both nodes
|
||||
val latestFromA = nodeA.database.transaction {
|
||||
nodeA.services.latest<TradeApprovalContract.State>(proposalRef.ref)
|
||||
nodeA.services.latest<TradeApprovalContract.State>(proposalLinearId)
|
||||
}
|
||||
val latestFromB = nodeB.database.transaction {
|
||||
nodeB.services.latest<TradeApprovalContract.State>(proposalRef.ref)
|
||||
nodeB.services.latest<TradeApprovalContract.State>(proposalLinearId)
|
||||
}
|
||||
// Confirm the state as as expected
|
||||
assertEquals(WorkflowState.NEW, proposalRef.state.data.state)
|
||||
@ -87,10 +91,10 @@ class WorkflowTransactionBuildTutorialTest {
|
||||
secondNodeBVaultUpdate.get()
|
||||
// Fetch the latest copies from the vault
|
||||
val finalFromA = nodeA.database.transaction {
|
||||
nodeA.services.latest<TradeApprovalContract.State>(proposalRef.ref)
|
||||
nodeA.services.latest<TradeApprovalContract.State>(proposalLinearId)
|
||||
}
|
||||
val finalFromB = nodeB.database.transaction {
|
||||
nodeB.services.latest<TradeApprovalContract.State>(proposalRef.ref)
|
||||
nodeB.services.latest<TradeApprovalContract.State>(proposalLinearId)
|
||||
}
|
||||
// Confirm the state is as expected
|
||||
assertEquals(WorkflowState.APPROVED, completedRef.state.data.state)
|
||||
|
18
docs/source/flow-cookbook.rst
Normal file
18
docs/source/flow-cookbook.rst
Normal file
@ -0,0 +1,18 @@
|
||||
.. highlight:: kotlin
|
||||
.. raw:: html
|
||||
|
||||
<script type="text/javascript" src="_static/jquery.js"></script>
|
||||
<script type="text/javascript" src="_static/codesets.js"></script>
|
||||
|
||||
Flow cookbook
|
||||
=============
|
||||
|
||||
This flow showcases how to use Corda's API, in both Java and Kotlin.
|
||||
|
||||
.. container:: codeset
|
||||
|
||||
.. literalinclude:: example-code/src/main/kotlin/net/corda/docs/FlowCookbook.kt
|
||||
:language: kotlin
|
||||
|
||||
.. literalinclude:: example-code/src/main/java/net/corda/docs/FlowCookbookJava.java
|
||||
:language: java
|
@ -6,9 +6,48 @@ Here are release notes for each snapshot release from M9 onwards.
|
||||
Unreleased
|
||||
----------
|
||||
|
||||
Certificate checks have been enabled for much of the identity service, with additional checks coming targetted at M13.
|
||||
These are part of the confidential identities work, and ensure that parties are actually who they claim to be by checking
|
||||
their certificate path back to the network trust root (certificate authority).
|
||||
Milestone 13
|
||||
------------
|
||||
|
||||
Following our first public beta in M12, this release continues the work on API stability and user friendliness. Apart
|
||||
from bug fixes and code refactoring, there are also significant improvements in the Vault Query and the
|
||||
Identity Service (for more detailed information about what has changed, see :doc:`changelog`).
|
||||
More specifically:
|
||||
|
||||
The long awaited new **Vault Query** service makes its debut in this release and provides advanced vault query
|
||||
capabilities using criteria specifications (see ``QueryCriteria``), sorting, and pagination. Criteria specifications
|
||||
enable selective filtering with and/or composition using multiple operator primitives on standard attributes stored in
|
||||
Corda internal vault tables (eg. vault_states, vault_fungible_states, vault_linear_states), and also on custom contract
|
||||
state schemas defined by CorDapp developers when modelling new contract types. Custom queries are specifiable using a
|
||||
simple but sophisticated builder DSL (see ``QueryCriteriaUtils``). The new Vault Query service is usable by flows and by
|
||||
RPC clients alike via two simple API functions: ``queryBy()`` and ``trackBy()``. The former provides point-in-time
|
||||
snapshot queries whilst the later supplements the snapshot with dynamic streaming of updates.
|
||||
See :doc:`vault-query` for full details.
|
||||
|
||||
We have written a comprehensive Hello, World! tutorial, showing developers how to build a CorDapp from start
|
||||
to finish. The tutorial shows how the core elements of a CorDapp - states, contracts and flows - fit together
|
||||
to allow your node to handle new business processes. It also explains how you can use our contract and
|
||||
flow testing frameworks to massively reduce CorDapp development time.
|
||||
|
||||
Certificate checks have been enabled for much of the identity service. These are part of the confidential (anonymous)
|
||||
identities work, and ensure that parties are actually who they claim to be by checking their certificate path back to
|
||||
the network trust root (certificate authority).
|
||||
|
||||
To deal with anonymized keys, we've also implemented a deterministic key derivation function that combines logic
|
||||
from the HMAC-based Extract-and-Expand Key Derivation Function (HKDF) protocol and the BIP32 hardened
|
||||
parent-private-key -> child-private-key scheme. This function currently supports the following algorithms:
|
||||
ECDSA secp256K1, ECDSA secpR1 (NIST P-256) and EdDSA ed25519. We are now very close to fully supporting anonymous
|
||||
identities so as to increase privacy even against validating notaries.
|
||||
|
||||
We have further tightened the set of objects which Corda will attempt to serialise from the stack during flow
|
||||
checkpointing. As flows are arbitrary code in which it is convenient to do many things, we ended up pulling in a lot of
|
||||
objects that didn't make sense to put in a checkpoint, such as ``Thread`` and ``Connection``. To minimize serialization
|
||||
cost and increase security by not allowing certain classes to be serialized, we now support class blacklisting
|
||||
that will return an ``IllegalStateException`` if such a class is encountered during a checkpoint. Blacklisting supports
|
||||
superclass and superinterface inheritance and always precedes ``@CordaSerializable`` annotation checking.
|
||||
|
||||
We've also started working on improving user experience when searching, by adding a new RPC to support fuzzy matching
|
||||
of X.500 names.
|
||||
|
||||
Milestone 12 - First Public Beta
|
||||
--------------------------------
|
||||
|
@ -32,7 +32,7 @@ To run from the command line in Unix:
|
||||
|
||||
1. Run ``./gradlew samples:trader-demo:deployNodes`` to create a set of configs and installs under ``samples/trader-demo/build/nodes``
|
||||
2. Run ``./samples/trader-demo/build/nodes/runnodes`` to open up four new terminals with the four nodes
|
||||
3. Run ``./gradlew samples:trader-demo:runBuyer`` to instruct the buyer node to request issuance of some cash from the Bank of Corda node.
|
||||
3. Run ``./gradlew samples:trader-demo:runBuyer`` to instruct the buyer node to request issuance of some cash from the Bank of Corda node
|
||||
4. Run ``./gradlew samples:trader-demo:runSeller`` to trigger the transaction. If you entered ``flow watch``
|
||||
you can see flows running on both sides of transaction. Additionally you should see final trade information displayed
|
||||
to your terminal.
|
||||
@ -41,7 +41,7 @@ To run from the command line in Windows:
|
||||
|
||||
1. Run ``gradlew samples:trader-demo:deployNodes`` to create a set of configs and installs under ``samples\trader-demo\build\nodes``
|
||||
2. Run ``samples\trader-demo\build\nodes\runnodes`` to open up four new terminals with the four nodes
|
||||
3. Run ``gradlew samples:trader-demo:runBuyer`` to instruct the buyer node to request issuance of some cash from the Bank of Corda node.
|
||||
3. Run ``gradlew samples:trader-demo:runBuyer`` to instruct the buyer node to request issuance of some cash from the Bank of Corda node
|
||||
4. Run ``gradlew samples:trader-demo:runSeller`` to trigger the transaction. If you entered ``flow watch``
|
||||
you can see flows running on both sides of transaction. Additionally you should see final trade information displayed
|
||||
to your terminal.
|
||||
@ -57,16 +57,10 @@ on a simulated clock passes.
|
||||
|
||||
To run from the command line in Unix:
|
||||
|
||||
1. Run ``./gradlew samples:irs-demo:deployNodes`` to install configs and a command line tool under ``samples/irs-demo/build``.
|
||||
1. Run ``./gradlew samples:irs-demo:deployNodes`` to install configs and a command line tool under ``samples/irs-demo/build``
|
||||
2. Run ``./gradlew samples:irs-demo:installDist``
|
||||
3. Move to the ``samples/irs-demo/build`` directory
|
||||
4. Run ``./nodes/runnodes`` to open up three new terminals with the three nodes (you may have to install xterm).
|
||||
5. Run ``./install/irs-demo/bin/irs-demo --role UploadRates``. You should see a
|
||||
message be printed to the first node (the notary/oracle/network map node) saying that it has accepted the new
|
||||
interest rates
|
||||
6. Now run ``./install/irs-demo/bin/irs-demo --role Trade 1``. The number is a trade ID. If you enter in node's terminal
|
||||
``flow watch`` you should see lots of activity as the nodes set up the deal, notarise it, get it signed by the oracle, and so on
|
||||
7. Now run ``./install/irs-demo/bin/irs-demo --role Date 2017-12-12`` to roll the simulated clock forward and see some fixings take place
|
||||
|
||||
To run from the command line in Windows:
|
||||
|
||||
@ -74,21 +68,15 @@ To run from the command line in Windows:
|
||||
2. Run ``gradlew samples:irs-demo:installDist``
|
||||
3. Move to the ``samples\irs-demo\build`` directory
|
||||
4. Run ``nodes\runnodes`` to open up three new terminals with the three nodes.
|
||||
5. Run ``install\irs-demo\bin\irs-demo --role UploadRates``. You should see a
|
||||
message be printed to the first node (the notary/oracle/network map node) saying that it has accepted the new
|
||||
interest rates
|
||||
6. Now run ``install\irs-demo\bin\irs-demo --role Trade 1``. The number is a trade ID. If you enter in node's terminal
|
||||
``flow watch`` you should see lots of activity as the nodes set up the deal, notarise it, get it signed by the oracle, and so on
|
||||
7. Now run ``install\irs-demo\bin\irs-demo --role Date 2017-12-12`` to roll the simulated clock forward and see some fixings take place
|
||||
|
||||
This demo also has a web app. To use this, run nodes and upload rates, then navigate to
|
||||
This demo also has a web app. To use this, run nodes and then navigate to
|
||||
http://localhost:10007/web/irsdemo and http://localhost:10010/web/irsdemo to see each node's view of the ledger.
|
||||
|
||||
To use the web app, click the "Create Deal" button, fill in the form, then click the "Submit" button. You can then
|
||||
use the time controls at the top left of the home page to run the fixings. Click any individual trade in the blotter to view it.
|
||||
|
||||
.. note:: The IRS web UI currently has a bug when changing the clock time where it may show no numbers or apply fixings inconsistently.
|
||||
The issues will be addressed in M13 milestone release. Meanwhile, you can take a look at a simpler oracle example https://github.com/corda/oracle-example
|
||||
The issues will be addressed in a future milestone release. Meanwhile, you can take a look at a simpler oracle example https://github.com/corda/oracle-example
|
||||
|
||||
Attachment demo
|
||||
---------------
|
||||
|
@ -46,7 +46,7 @@ Note the following:
|
||||
* the vault performs fungible state spending (and in future, fungible state optimisation management including merging, splitting and re-issuance)
|
||||
* vault extensions represent additional custom plugin code a developer may write to query specific custom contract state attributes.
|
||||
* customer "Off Ledger" (private store) represents internal organisational data that may be joined with the vault data to perform additional reporting or processing
|
||||
* a :doc:`vault-query` API is exposed to developers using standard Corda RPC and CorDapp plugin mechanisms
|
||||
* a :doc:`vault query API <api-vault>` is exposed to developers using standard Corda RPC and CorDapp plugin mechanisms
|
||||
* a vault update API is internally used by transaction recording flows.
|
||||
* the vault database schemas are directly accessible via JDBC for customer joins and queries
|
||||
|
||||
|
@ -30,6 +30,19 @@ configurations.testCompile {
|
||||
exclude group: 'javassist', module: 'javassist'
|
||||
}
|
||||
|
||||
configurations {
|
||||
testArtifacts.extendsFrom testRuntime
|
||||
}
|
||||
|
||||
task testJar(type: Jar) {
|
||||
classifier "tests"
|
||||
from sourceSets.test.output
|
||||
}
|
||||
|
||||
artifacts {
|
||||
testArtifacts testJar
|
||||
}
|
||||
|
||||
jar {
|
||||
baseName 'corda-finance'
|
||||
}
|
||||
|
@ -84,6 +84,7 @@ class CommercialPaper : Contract {
|
||||
// DOCSTART VaultIndexedQueryCriteria
|
||||
/** Object Relational Mapping support. */
|
||||
override fun supportedSchemas(): Iterable<MappedSchema> = listOf(CommercialPaperSchemaV1)
|
||||
/** Additional used schemas would be added here (eg. CommercialPaperV2, ...) */
|
||||
|
||||
/** Object Relational Mapping support. */
|
||||
override fun generateMappedObject(schema: MappedSchema): PersistentState {
|
||||
@ -98,6 +99,7 @@ class CommercialPaper : Contract {
|
||||
faceValueIssuerParty = this.faceValue.token.issuer.party.owningKey.toBase58String(),
|
||||
faceValueIssuerRef = this.faceValue.token.issuer.reference.bytes
|
||||
)
|
||||
/** Additional schema mappings would be added here (eg. CommercialPaperV2, ...) */
|
||||
else -> throw IllegalArgumentException("Unrecognised schema $schema")
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,15 @@
|
||||
package net.corda.contracts.testing
|
||||
package net.corda.contracts
|
||||
|
||||
import net.corda.contracts.DealState
|
||||
import net.corda.core.contracts.Contract
|
||||
import net.corda.core.contracts.TransactionForContract
|
||||
import net.corda.core.contracts.UniqueIdentifier
|
||||
import net.corda.core.crypto.SecureHash
|
||||
import net.corda.core.crypto.containsAny
|
||||
import net.corda.core.crypto.*
|
||||
import net.corda.core.identity.AbstractParty
|
||||
import net.corda.core.identity.Party
|
||||
import net.corda.core.schemas.DummyDealStateSchemaV1
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.core.schemas.PersistentState
|
||||
import net.corda.core.schemas.QueryableState
|
||||
import net.corda.core.transactions.TransactionBuilder
|
||||
import java.security.PublicKey
|
||||
|
||||
@ -20,7 +22,8 @@ class DummyDealContract : Contract {
|
||||
override val contract: Contract = DummyDealContract(),
|
||||
override val participants: List<AbstractParty> = listOf(),
|
||||
override val linearId: UniqueIdentifier = UniqueIdentifier(),
|
||||
override val ref: String) : DealState {
|
||||
override val ref: String) : DealState, QueryableState
|
||||
{
|
||||
override fun isRelevant(ourKeys: Set<PublicKey>): Boolean {
|
||||
return participants.any { it.owningKey.containsAny(ourKeys) }
|
||||
}
|
||||
@ -28,5 +31,17 @@ class DummyDealContract : Contract {
|
||||
override fun generateAgreement(notary: Party): TransactionBuilder {
|
||||
throw UnsupportedOperationException("not implemented")
|
||||
}
|
||||
|
||||
override fun supportedSchemas(): Iterable<MappedSchema> = listOf(DummyDealStateSchemaV1)
|
||||
|
||||
override fun generateMappedObject(schema: MappedSchema): PersistentState {
|
||||
return when (schema) {
|
||||
is DummyDealStateSchemaV1 -> DummyDealStateSchemaV1.PersistentDummyDealState(
|
||||
uid = linearId,
|
||||
dealReference = ref
|
||||
)
|
||||
else -> throw IllegalArgumentException("Unrecognised schema $schema")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -410,7 +410,7 @@ interface DealState : LinearState {
|
||||
}
|
||||
|
||||
// TODO: Remove this from the interface
|
||||
// @Deprecated("This function will be removed in a future milestone", ReplaceWith("queryBy(LinearStateQueryCriteria(dealPartyName = listOf(<String>)))"))
|
||||
@Deprecated("This function will be removed in a future milestone", ReplaceWith("queryBy(LinearStateQueryCriteria(dealPartyName = listOf(<String>)))"))
|
||||
inline fun <reified T : DealState> VaultService.dealsWith(party: AbstractParty) = linearHeadsOfType<T>().values.filter {
|
||||
it.state.data.participants.any { it == party }
|
||||
}
|
||||
|
@ -116,12 +116,14 @@ class Cash : OnLedgerAsset<Currency, Cash.Commands, Cash.State>() {
|
||||
issuerParty = this.amount.token.issuer.party.owningKey.toBase58String(),
|
||||
issuerRef = this.amount.token.issuer.reference.bytes
|
||||
)
|
||||
/** Additional schema mappings would be added here (eg. CashSchemaV2, CashSchemaV3, ...) */
|
||||
else -> throw IllegalArgumentException("Unrecognised schema $schema")
|
||||
}
|
||||
}
|
||||
|
||||
/** Object Relational Mapping support. */
|
||||
override fun supportedSchemas(): Iterable<MappedSchema> = listOf(CashSchemaV1)
|
||||
/** Additional used schemas would be added here (eg. CashSchemaV2, CashSchemaV3, ...) */
|
||||
}
|
||||
// DOCEND 1
|
||||
|
||||
|
@ -1,30 +0,0 @@
|
||||
package net.corda.contracts.testing
|
||||
|
||||
import net.corda.core.contracts.*
|
||||
import net.corda.core.contracts.clauses.Clause
|
||||
import net.corda.core.contracts.clauses.FilterOn
|
||||
import net.corda.core.contracts.clauses.verifyClause
|
||||
import net.corda.core.crypto.SecureHash
|
||||
import net.corda.core.crypto.containsAny
|
||||
import net.corda.core.identity.AbstractParty
|
||||
import java.security.PublicKey
|
||||
|
||||
class DummyLinearContract : Contract {
|
||||
override val legalContractReference: SecureHash = SecureHash.sha256("Test")
|
||||
|
||||
val clause: Clause<State, CommandData, Unit> = LinearState.ClauseVerifier()
|
||||
override fun verify(tx: TransactionForContract) = verifyClause(tx,
|
||||
FilterOn(clause, { states -> states.filterIsInstance<State>() }),
|
||||
emptyList())
|
||||
|
||||
data class State(
|
||||
override val linearId: UniqueIdentifier = UniqueIdentifier(),
|
||||
override val contract: Contract = DummyLinearContract(),
|
||||
override val participants: List<AbstractParty> = listOf(),
|
||||
val nonce: SecureHash = SecureHash.randomSHA256()) : LinearState {
|
||||
|
||||
override fun isRelevant(ourKeys: Set<PublicKey>): Boolean {
|
||||
return participants.any { it.owningKey.containsAny(ourKeys) }
|
||||
}
|
||||
}
|
||||
}
|
@ -2,10 +2,10 @@
|
||||
|
||||
package net.corda.contracts.testing
|
||||
|
||||
import net.corda.contracts.Commodity
|
||||
import net.corda.contracts.DealState
|
||||
import net.corda.contracts.asset.Cash
|
||||
import net.corda.contracts.asset.DUMMY_CASH_ISSUER
|
||||
import net.corda.contracts.asset.DUMMY_CASH_ISSUER_KEY
|
||||
import net.corda.contracts.DummyDealContract
|
||||
import net.corda.contracts.asset.*
|
||||
import net.corda.core.contracts.*
|
||||
import net.corda.core.identity.AbstractParty
|
||||
import net.corda.core.identity.AnonymousParty
|
||||
@ -14,22 +14,25 @@ import net.corda.core.node.ServiceHub
|
||||
import net.corda.core.node.services.Vault
|
||||
import net.corda.core.serialization.OpaqueBytes
|
||||
import net.corda.core.transactions.SignedTransaction
|
||||
import net.corda.core.utilities.CHARLIE
|
||||
import net.corda.core.utilities.DUMMY_NOTARY
|
||||
import net.corda.core.utilities.DUMMY_NOTARY_KEY
|
||||
import java.security.KeyPair
|
||||
import java.security.PublicKey
|
||||
import java.time.Instant
|
||||
import java.time.Instant.now
|
||||
import java.util.*
|
||||
|
||||
@JvmOverloads
|
||||
fun ServiceHub.fillWithSomeTestDeals(dealIds: List<String>,
|
||||
participants: List<AbstractParty> = emptyList()) : Vault<DealState> {
|
||||
val freshKey = keyManagementService.freshKey()
|
||||
val recipient = AnonymousParty(freshKey)
|
||||
val myKey: PublicKey = myInfo.legalIdentity.owningKey
|
||||
val me = AnonymousParty(myKey)
|
||||
|
||||
val transactions: List<SignedTransaction> = dealIds.map {
|
||||
// Issue a deal state
|
||||
val dummyIssue = TransactionType.General.Builder(notary = DUMMY_NOTARY).apply {
|
||||
addOutputState(DummyDealContract.State(ref = it, participants = participants.plus(recipient)))
|
||||
addOutputState(DummyDealContract.State(ref = it, participants = participants.plus(me)))
|
||||
signWith(DUMMY_NOTARY_KEY)
|
||||
}
|
||||
return@map signInitialTransaction(dummyIssue)
|
||||
@ -47,15 +50,24 @@ fun ServiceHub.fillWithSomeTestDeals(dealIds: List<String>,
|
||||
|
||||
@JvmOverloads
|
||||
fun ServiceHub.fillWithSomeTestLinearStates(numberToCreate: Int,
|
||||
uid: UniqueIdentifier = UniqueIdentifier(),
|
||||
participants: List<AbstractParty> = emptyList()) : Vault<LinearState> {
|
||||
val freshKey = keyManagementService.freshKey()
|
||||
val recipient = AnonymousParty(freshKey)
|
||||
externalId: String? = null,
|
||||
participants: List<AbstractParty> = emptyList(),
|
||||
linearString: String = "",
|
||||
linearNumber: Long = 0L,
|
||||
linearBoolean: Boolean = false,
|
||||
linearTimestamp: Instant = now()) : Vault<LinearState> {
|
||||
val myKey: PublicKey = myInfo.legalIdentity.owningKey
|
||||
val me = AnonymousParty(myKey)
|
||||
|
||||
val transactions: List<SignedTransaction> = (1..numberToCreate).map {
|
||||
// Issue a Linear state
|
||||
val dummyIssue = TransactionType.General.Builder(notary = DUMMY_NOTARY).apply {
|
||||
addOutputState(DummyLinearContract.State(linearId = uid, participants = participants.plus(recipient)))
|
||||
addOutputState(DummyLinearContract.State(linearId = UniqueIdentifier(externalId),
|
||||
participants = participants.plus(me),
|
||||
linearString = linearString,
|
||||
linearNumber = linearNumber,
|
||||
linearBoolean = linearBoolean,
|
||||
linearTimestamp = linearTimestamp))
|
||||
signWith(DUMMY_NOTARY_KEY)
|
||||
}
|
||||
|
||||
@ -116,6 +128,27 @@ fun ServiceHub.fillWithSomeTestCash(howMuch: Amount<Currency>,
|
||||
return Vault(states)
|
||||
}
|
||||
|
||||
// TODO: need to make all FungibleAsset commands (issue, move, exit) generic
|
||||
fun ServiceHub.fillWithSomeTestCommodity(amount: Amount<Commodity>,
|
||||
outputNotary: Party = DUMMY_NOTARY,
|
||||
ref: OpaqueBytes = OpaqueBytes(ByteArray(1, { 1 })),
|
||||
ownedBy: AbstractParty? = null,
|
||||
issuedBy: PartyAndReference = DUMMY_OBLIGATION_ISSUER.ref(1),
|
||||
issuerKey: KeyPair = DUMMY_OBLIGATION_ISSUER_KEY): Vault<CommodityContract.State> {
|
||||
val myKey: PublicKey = ownedBy?.owningKey ?: myInfo.legalIdentity.owningKey
|
||||
val me = AnonymousParty(myKey)
|
||||
|
||||
val commodity = CommodityContract()
|
||||
val issuance = TransactionType.General.Builder(null as Party?)
|
||||
commodity.generateIssue(issuance, Amount(amount.quantity, Issued(issuedBy.copy(reference = ref), amount.token)), me, outputNotary)
|
||||
issuance.signWith(issuerKey)
|
||||
val transaction = issuance.toSignedTransaction(true)
|
||||
|
||||
recordTransactions(transaction)
|
||||
|
||||
return Vault(setOf(transaction.tx.outRef<CommodityContract.State>(0)))
|
||||
}
|
||||
|
||||
fun calculateRandomlySizedAmounts(howMuch: Amount<Currency>, min: Int, max: Int, rng: Random): LongArray {
|
||||
val numSlots = min + Math.floor(rng.nextDouble() * (max - min)).toInt()
|
||||
val baseSize = howMuch.quantity / numSlots
|
||||
@ -143,3 +176,58 @@ fun calculateRandomlySizedAmounts(howMuch: Amount<Currency>, min: Int, max: Int,
|
||||
|
||||
return amounts
|
||||
}
|
||||
|
||||
fun <T : LinearState> ServiceHub.consume(states: List<StateAndRef<T>>) {
|
||||
// Create a txn consuming different contract types
|
||||
states.forEach {
|
||||
val consumedTx = TransactionType.General.Builder(notary = DUMMY_NOTARY).apply {
|
||||
addInputState(it)
|
||||
signWith(DUMMY_NOTARY_KEY)
|
||||
}.toSignedTransaction()
|
||||
|
||||
recordTransactions(consumedTx)
|
||||
}
|
||||
}
|
||||
|
||||
fun <T : LinearState> ServiceHub.consumeAndProduce(stateAndRef: StateAndRef<T>): StateAndRef<T> {
|
||||
// Create a txn consuming different contract types
|
||||
val consumedTx = TransactionType.General.Builder(notary = DUMMY_NOTARY).apply {
|
||||
addInputState(stateAndRef)
|
||||
signWith(DUMMY_NOTARY_KEY)
|
||||
}.toSignedTransaction()
|
||||
|
||||
recordTransactions(consumedTx)
|
||||
|
||||
// Create a txn consuming different contract types
|
||||
val producedTx = TransactionType.General.Builder(notary = DUMMY_NOTARY).apply {
|
||||
addOutputState(DummyLinearContract.State(linearId = stateAndRef.state.data.linearId,
|
||||
participants = stateAndRef.state.data.participants))
|
||||
signWith(DUMMY_NOTARY_KEY)
|
||||
}.toSignedTransaction()
|
||||
|
||||
recordTransactions(producedTx)
|
||||
|
||||
return producedTx.tx.outRef<T>(0)
|
||||
}
|
||||
|
||||
fun <T : LinearState> ServiceHub.consumeAndProduce(states: List<StateAndRef<T>>) {
|
||||
states.forEach {
|
||||
consumeAndProduce(it)
|
||||
}
|
||||
}
|
||||
|
||||
fun ServiceHub.consumeDeals(dealStates: List<StateAndRef<DealState>>) = consume(dealStates)
|
||||
fun ServiceHub.consumeLinearStates(linearStates: List<StateAndRef<LinearState>>) = consume(linearStates)
|
||||
fun ServiceHub.evolveLinearStates(linearStates: List<StateAndRef<LinearState>>) = consumeAndProduce(linearStates)
|
||||
fun ServiceHub.evolveLinearState(linearState: StateAndRef<LinearState>) : StateAndRef<LinearState> = consumeAndProduce(linearState)
|
||||
|
||||
@JvmOverloads
|
||||
fun ServiceHub.consumeCash(amount: Amount<Currency>, to: Party = CHARLIE) {
|
||||
// A tx that spends our money.
|
||||
val spendTX = TransactionType.General.Builder(DUMMY_NOTARY).apply {
|
||||
vaultService.generateSpend(this, amount, to)
|
||||
signWith(DUMMY_NOTARY_KEY)
|
||||
}.toSignedTransaction(checkSufficientSignatures = false)
|
||||
|
||||
recordTransactions(spendTX)
|
||||
}
|
||||
|
@ -2,10 +2,8 @@ package net.corda.schemas
|
||||
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.core.schemas.PersistentState
|
||||
import javax.persistence.Column
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.Index
|
||||
import javax.persistence.Table
|
||||
import net.corda.node.services.vault.schemas.jpa.CommonSchemaV1
|
||||
import javax.persistence.*
|
||||
|
||||
/**
|
||||
* An object used to fully qualify the [CashSchema] family name (i.e. independent of version).
|
||||
@ -18,7 +16,7 @@ object CashSchema
|
||||
*/
|
||||
object CashSchemaV1 : MappedSchema(schemaFamily = CashSchema.javaClass, version = 1, mappedTypes = listOf(PersistentCashState::class.java)) {
|
||||
@Entity
|
||||
@Table(name = "cash_states",
|
||||
@Table(name = "contract_cash_states",
|
||||
indexes = arrayOf(Index(name = "ccy_code_idx", columnList = "ccy_code"),
|
||||
Index(name = "pennies_idx", columnList = "pennies")))
|
||||
class PersistentCashState(
|
||||
|
@ -20,9 +20,9 @@ object CommercialPaperSchema
|
||||
object CommercialPaperSchemaV1 : MappedSchema(schemaFamily = CommercialPaperSchema.javaClass, version = 1, mappedTypes = listOf(PersistentCommercialPaperState::class.java)) {
|
||||
@Entity
|
||||
@Table(name = "cp_states",
|
||||
indexes = arrayOf(Index(name = "ccy_code_idx", columnList = "ccy_code"),
|
||||
Index(name = "maturity_idx", columnList = "maturity_instant"),
|
||||
Index(name = "face_value_idx", columnList = "face_value")))
|
||||
indexes = arrayOf(Index(name = "ccy_code_index", columnList = "ccy_code"),
|
||||
Index(name = "maturity_index", columnList = "maturity_instant"),
|
||||
Index(name = "face_value_index", columnList = "face_value")))
|
||||
class PersistentCommercialPaperState(
|
||||
@Column(name = "issuance_key")
|
||||
var issuanceParty: String,
|
||||
|
@ -0,0 +1,132 @@
|
||||
package net.corda.contracts.asset
|
||||
|
||||
import net.corda.contracts.clause.AbstractConserveAmount
|
||||
import net.corda.contracts.clause.AbstractIssue
|
||||
import net.corda.contracts.clause.NoZeroSizedOutputs
|
||||
import net.corda.core.contracts.*
|
||||
import net.corda.core.contracts.clauses.AllOf
|
||||
import net.corda.core.contracts.clauses.FirstOf
|
||||
import net.corda.core.contracts.clauses.GroupClauseVerifier
|
||||
import net.corda.core.contracts.clauses.verifyClause
|
||||
import net.corda.core.crypto.*
|
||||
import net.corda.core.identity.AbstractParty
|
||||
import net.corda.core.identity.Party
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.core.schemas.PersistentState
|
||||
import net.corda.core.schemas.QueryableState
|
||||
import net.corda.core.serialization.CordaSerializable
|
||||
import net.corda.core.transactions.TransactionBuilder
|
||||
import net.corda.core.utilities.Emoji
|
||||
import net.corda.schemas.SampleCashSchemaV1
|
||||
import net.corda.schemas.SampleCashSchemaV2
|
||||
import net.corda.schemas.SampleCashSchemaV3
|
||||
import java.util.*
|
||||
|
||||
class DummyFungibleContract : OnLedgerAsset<Currency, DummyFungibleContract.Commands, DummyFungibleContract.State>() {
|
||||
override val legalContractReference: SecureHash = SecureHash.sha256("https://www.big-book-of-banking-law.gov/cash-claims.html")
|
||||
|
||||
override fun extractCommands(commands: Collection<AuthenticatedObject<CommandData>>): List<AuthenticatedObject<DummyFungibleContract.Commands>>
|
||||
= commands.select<DummyFungibleContract.Commands>()
|
||||
|
||||
interface Clauses {
|
||||
class Group : GroupClauseVerifier<State, Commands, Issued<Currency>>(AllOf<State, Commands, Issued<Currency>>(
|
||||
NoZeroSizedOutputs<State, Commands, Currency>(),
|
||||
FirstOf<State, Commands, Issued<Currency>>(
|
||||
Issue(),
|
||||
ConserveAmount())
|
||||
)
|
||||
) {
|
||||
override fun groupStates(tx: TransactionForContract): List<TransactionForContract.InOutGroup<State, Issued<Currency>>>
|
||||
= tx.groupStates<State, Issued<Currency>> { it.amount.token }
|
||||
}
|
||||
|
||||
class Issue : AbstractIssue<State, Commands, Currency>(
|
||||
sum = { sumCash() },
|
||||
sumOrZero = { sumCashOrZero(it) }
|
||||
) {
|
||||
override val requiredCommands: Set<Class<out CommandData>> = setOf(Commands.Issue::class.java)
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
class ConserveAmount : AbstractConserveAmount<State, Commands, Currency>()
|
||||
}
|
||||
|
||||
data class State(
|
||||
override val amount: Amount<Issued<Currency>>,
|
||||
|
||||
override val owner: AbstractParty
|
||||
) : FungibleAsset<Currency>, QueryableState {
|
||||
constructor(deposit: PartyAndReference, amount: Amount<Currency>, owner: AbstractParty)
|
||||
: this(Amount(amount.quantity, Issued(deposit, amount.token)), owner)
|
||||
|
||||
override val exitKeys = setOf(owner.owningKey, amount.token.issuer.party.owningKey)
|
||||
override val contract = CASH_PROGRAM_ID
|
||||
override val participants = listOf(owner)
|
||||
|
||||
override fun move(newAmount: Amount<Issued<Currency>>, newOwner: AbstractParty): FungibleAsset<Currency>
|
||||
= copy(amount = amount.copy(newAmount.quantity), owner = newOwner)
|
||||
|
||||
override fun toString() = "${Emoji.bagOfCash}Cash($amount at ${amount.token.issuer} owned by $owner)"
|
||||
|
||||
override fun withNewOwner(newOwner: AbstractParty) = Pair(Commands.Move(), copy(owner = newOwner))
|
||||
|
||||
/** Object Relational Mapping support. */
|
||||
override fun generateMappedObject(schema: MappedSchema): PersistentState {
|
||||
return when (schema) {
|
||||
is SampleCashSchemaV1 -> SampleCashSchemaV1.PersistentCashState(
|
||||
owner = this.owner.owningKey.toBase58String(),
|
||||
pennies = this.amount.quantity,
|
||||
currency = this.amount.token.product.currencyCode,
|
||||
issuerParty = this.amount.token.issuer.party.owningKey.toBase58String(),
|
||||
issuerRef = this.amount.token.issuer.reference.bytes
|
||||
)
|
||||
is SampleCashSchemaV2 -> SampleCashSchemaV2.PersistentCashState(
|
||||
_participants = this.participants.toSet(),
|
||||
_owner = this.owner,
|
||||
_quantity = this.amount.quantity,
|
||||
currency = this.amount.token.product.currencyCode,
|
||||
_issuerParty = this.amount.token.issuer.party,
|
||||
_issuerRef = this.amount.token.issuer.reference.bytes
|
||||
)
|
||||
is SampleCashSchemaV3 -> SampleCashSchemaV3.PersistentCashState(
|
||||
_participants = this.participants.toSet(),
|
||||
_owner = this.owner,
|
||||
_quantity = this.amount.quantity,
|
||||
_currency = this.amount.token.product.currencyCode,
|
||||
_issuerParty = this.amount.token.issuer.party,
|
||||
_issuerRef = this.amount.token.issuer.reference.bytes
|
||||
)
|
||||
else -> throw IllegalArgumentException("Unrecognised schema $schema")
|
||||
}
|
||||
}
|
||||
|
||||
/** Object Relational Mapping support. */
|
||||
override fun supportedSchemas(): Iterable<MappedSchema> = listOf(SampleCashSchemaV1, SampleCashSchemaV2, SampleCashSchemaV3)
|
||||
}
|
||||
|
||||
interface Commands : FungibleAsset.Commands {
|
||||
|
||||
data class Move(override val contractHash: SecureHash? = null) : FungibleAsset.Commands.Move, Commands
|
||||
|
||||
data class Issue(override val nonce: Long = newSecureRandom().nextLong()) : FungibleAsset.Commands.Issue, Commands
|
||||
|
||||
data class Exit(override val amount: Amount<Issued<Currency>>) : Commands, FungibleAsset.Commands.Exit<Currency>
|
||||
}
|
||||
|
||||
fun generateIssue(tx: TransactionBuilder, tokenDef: Issued<Currency>, pennies: Long, owner: AbstractParty, notary: Party)
|
||||
= generateIssue(tx, Amount(pennies, tokenDef), owner, notary)
|
||||
|
||||
fun generateIssue(tx: TransactionBuilder, amount: Amount<Issued<Currency>>, owner: AbstractParty, notary: Party)
|
||||
= generateIssue(tx, TransactionState(State(amount, owner), notary), generateIssueCommand())
|
||||
|
||||
override fun deriveState(txState: TransactionState<State>, amount: Amount<Issued<Currency>>, owner: AbstractParty)
|
||||
= txState.copy(data = txState.data.copy(amount = amount, owner = owner))
|
||||
|
||||
override fun generateExitCommand(amount: Amount<Issued<Currency>>) = Commands.Exit(amount)
|
||||
override fun generateIssueCommand() = Commands.Issue()
|
||||
override fun generateMoveCommand() = Commands.Move()
|
||||
|
||||
override fun verify(tx: TransactionForContract)
|
||||
= verifyClause(tx, Clauses.Group(), extractCommands(tx.commands))
|
||||
}
|
||||
|
@ -0,0 +1,38 @@
|
||||
package net.corda.schemas
|
||||
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.core.schemas.PersistentState
|
||||
import net.corda.node.services.vault.schemas.jpa.CommonSchemaV1
|
||||
import javax.persistence.*
|
||||
|
||||
/**
|
||||
* An object used to fully qualify the [CashSchema] family name (i.e. independent of version).
|
||||
*/
|
||||
object CashSchema
|
||||
|
||||
/**
|
||||
* First version of a cash contract ORM schema that maps all fields of the [Cash] contract state as it stood
|
||||
* at the time of writing.
|
||||
*/
|
||||
object SampleCashSchemaV1 : MappedSchema(schemaFamily = CashSchema.javaClass, version = 1, mappedTypes = listOf(PersistentCashState::class.java)) {
|
||||
@Entity
|
||||
@Table(name = "contract_cash_states",
|
||||
indexes = arrayOf(Index(name = "ccy_code_idx", columnList = "ccy_code"),
|
||||
Index(name = "pennies_idx", columnList = "pennies")))
|
||||
class PersistentCashState(
|
||||
@Column(name = "owner_key")
|
||||
var owner: String,
|
||||
|
||||
@Column(name = "pennies")
|
||||
var pennies: Long,
|
||||
|
||||
@Column(name = "ccy_code", length = 3)
|
||||
var currency: String,
|
||||
|
||||
@Column(name = "issuer_key")
|
||||
var issuerParty: String,
|
||||
|
||||
@Column(name = "issuer_ref")
|
||||
var issuerRef: ByteArray
|
||||
) : PersistentState()
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package net.corda.schemas
|
||||
|
||||
import net.corda.core.identity.AbstractParty
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.node.services.vault.schemas.jpa.CommonSchemaV1
|
||||
import javax.persistence.Column
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.Index
|
||||
import javax.persistence.Table
|
||||
|
||||
/**
|
||||
* Second version of a cash contract ORM schema that extends the common
|
||||
* [VaultFungibleState] abstract schema
|
||||
*/
|
||||
object SampleCashSchemaV2 : MappedSchema(schemaFamily = CashSchema.javaClass, version = 2,
|
||||
mappedTypes = listOf(PersistentCashState::class.java, CommonSchemaV1.Party::class.java)) {
|
||||
@Entity
|
||||
@Table(name = "cash_states_v2",
|
||||
indexes = arrayOf(Index(name = "ccy_code_idx2", columnList = "ccy_code")))
|
||||
class PersistentCashState (
|
||||
/** product type */
|
||||
@Column(name = "ccy_code", length = 3)
|
||||
var currency: String,
|
||||
|
||||
/** parent attributes */
|
||||
@Transient
|
||||
val _participants: Set<AbstractParty>,
|
||||
@Transient
|
||||
val _owner: AbstractParty,
|
||||
@Transient
|
||||
val _quantity: Long,
|
||||
@Transient
|
||||
val _issuerParty: AbstractParty,
|
||||
@Transient
|
||||
val _issuerRef: ByteArray
|
||||
) : CommonSchemaV1.FungibleState(_participants, _owner, _quantity, _issuerParty, _issuerRef)
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package net.corda.schemas
|
||||
|
||||
import net.corda.core.identity.AbstractParty
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.core.schemas.PersistentState
|
||||
import net.corda.node.services.vault.schemas.jpa.CommonSchemaV1
|
||||
import javax.persistence.*
|
||||
|
||||
/**
|
||||
* First version of a cash contract ORM schema that maps all fields of the [Cash] contract state as it stood
|
||||
* at the time of writing.
|
||||
*/
|
||||
object SampleCashSchemaV3 : MappedSchema(schemaFamily = CashSchema.javaClass, version = 3,
|
||||
mappedTypes = listOf(PersistentCashState::class.java, CommonSchemaV1.Party::class.java)) {
|
||||
@Entity
|
||||
@Table(name = "cash_states_v3")
|
||||
class PersistentCashState(
|
||||
/** [ContractState] attributes */
|
||||
@OneToMany(cascade = arrayOf(CascadeType.ALL))
|
||||
var participants: Set<CommonSchemaV1.Party>,
|
||||
|
||||
@OneToOne(cascade = arrayOf(CascadeType.ALL))
|
||||
var owner: CommonSchemaV1.Party,
|
||||
|
||||
@Column(name = "pennies")
|
||||
var pennies: Long,
|
||||
|
||||
@Column(name = "ccy_code", length = 3)
|
||||
var currency: String,
|
||||
|
||||
@OneToOne(cascade = arrayOf(CascadeType.ALL))
|
||||
var issuerParty: CommonSchemaV1.Party,
|
||||
|
||||
@Column(name = "issuer_ref")
|
||||
var issuerRef: ByteArray
|
||||
) : PersistentState() {
|
||||
constructor(_participants: Set<AbstractParty>, _owner: AbstractParty, _quantity: Long, _currency: String, _issuerParty: AbstractParty, _issuerRef: ByteArray)
|
||||
: this(participants = _participants.map { CommonSchemaV1.Party(it) }.toSet(),
|
||||
owner = CommonSchemaV1.Party(_owner),
|
||||
pennies = _quantity,
|
||||
currency = _currency,
|
||||
issuerParty = CommonSchemaV1.Party(_issuerParty),
|
||||
issuerRef = _issuerRef)
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package net.corda.schemas
|
||||
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.core.schemas.PersistentState
|
||||
import java.time.Instant
|
||||
import javax.persistence.Column
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.Index
|
||||
import javax.persistence.Table
|
||||
|
||||
/**
|
||||
* An object used to fully qualify the [CommercialPaperSchema] family name (i.e. independent of version).
|
||||
*/
|
||||
object CommercialPaperSchema
|
||||
|
||||
/**
|
||||
* First version of a commercial paper contract ORM schema that maps all fields of the [CommercialPaper] contract state
|
||||
* as it stood at the time of writing.
|
||||
*/
|
||||
object SampleCommercialPaperSchemaV1 : MappedSchema(schemaFamily = CommercialPaperSchema.javaClass, version = 1, mappedTypes = listOf(PersistentCommercialPaperState::class.java)) {
|
||||
@Entity
|
||||
@Table(name = "cp_states",
|
||||
indexes = arrayOf(Index(name = "ccy_code_index", columnList = "ccy_code"),
|
||||
Index(name = "maturity_index", columnList = "maturity_instant"),
|
||||
Index(name = "face_value_index", columnList = "face_value")))
|
||||
class PersistentCommercialPaperState(
|
||||
@Column(name = "issuance_key")
|
||||
var issuanceParty: String,
|
||||
|
||||
@Column(name = "issuance_ref")
|
||||
var issuanceRef: ByteArray,
|
||||
|
||||
@Column(name = "owner_key")
|
||||
var owner: String,
|
||||
|
||||
@Column(name = "maturity_instant")
|
||||
var maturity: Instant,
|
||||
|
||||
@Column(name = "face_value")
|
||||
var faceValue: Long,
|
||||
|
||||
@Column(name = "ccy_code", length = 3)
|
||||
var currency: String,
|
||||
|
||||
@Column(name = "face_value_issuer_key")
|
||||
var faceValueIssuerParty: String,
|
||||
|
||||
@Column(name = "face_value_issuer_ref")
|
||||
var faceValueIssuerRef: ByteArray
|
||||
) : PersistentState()
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package net.corda.schemas
|
||||
|
||||
import net.corda.core.crypto.toBase58String
|
||||
import net.corda.core.identity.AbstractParty
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.node.services.vault.schemas.jpa.CommonSchemaV1
|
||||
import java.security.PublicKey
|
||||
import java.time.Instant
|
||||
import javax.persistence.Column
|
||||
import javax.persistence.Entity
|
||||
import javax.persistence.Index
|
||||
import javax.persistence.Table
|
||||
|
||||
/**
|
||||
* Second version of a cash contract ORM schema that extends the common
|
||||
* [VaultFungibleState] abstract schema
|
||||
*/
|
||||
object SampleCommercialPaperSchemaV2 : MappedSchema(schemaFamily = CommercialPaperSchema.javaClass, version = 1,
|
||||
mappedTypes = listOf(PersistentCommercialPaperState::class.java, CommonSchemaV1.Party::class.java)) {
|
||||
@Entity
|
||||
@Table(name = "cp_states_v2",
|
||||
indexes = arrayOf(Index(name = "ccy_code_index2", columnList = "ccy_code"),
|
||||
Index(name = "maturity_index2", columnList = "maturity_instant")))
|
||||
class PersistentCommercialPaperState(
|
||||
@Column(name = "maturity_instant")
|
||||
var maturity: Instant,
|
||||
|
||||
@Column(name = "ccy_code", length = 3)
|
||||
var currency: String,
|
||||
|
||||
@Column(name = "face_value_issuer_key")
|
||||
var faceValueIssuerParty: String,
|
||||
|
||||
@Column(name = "face_value_issuer_ref")
|
||||
var faceValueIssuerRef: ByteArray,
|
||||
|
||||
/** parent attributes */
|
||||
@Transient
|
||||
val _participants: Set<AbstractParty>,
|
||||
@Transient
|
||||
val _owner: AbstractParty,
|
||||
@Transient
|
||||
// face value
|
||||
val _quantity: Long,
|
||||
@Transient
|
||||
val _issuerParty: AbstractParty,
|
||||
@Transient
|
||||
val _issuerRef: ByteArray
|
||||
) : CommonSchemaV1.FungibleState(_participants, _owner, _quantity, _issuerParty, _issuerRef)
|
||||
}
|
@ -65,10 +65,14 @@ object RPCApi {
|
||||
val RPC_SERVER_QUEUE_NAME = "rpc.server"
|
||||
val RPC_CLIENT_QUEUE_NAME_PREFIX = "rpc.client"
|
||||
val RPC_CLIENT_BINDING_REMOVALS = "rpc.clientqueueremovals"
|
||||
val RPC_CLIENT_BINDING_ADDITIONS = "rpc.clientqueueadditions"
|
||||
|
||||
val RPC_CLIENT_BINDING_REMOVAL_FILTER_EXPRESSION =
|
||||
"${ManagementHelper.HDR_NOTIFICATION_TYPE} = '${CoreNotificationType.BINDING_REMOVED.name}' AND " +
|
||||
"${ManagementHelper.HDR_ROUTING_NAME} LIKE '$RPC_CLIENT_QUEUE_NAME_PREFIX.%'"
|
||||
val RPC_CLIENT_BINDING_ADDITION_FILTER_EXPRESSION =
|
||||
"${ManagementHelper.HDR_NOTIFICATION_TYPE} = '${CoreNotificationType.BINDING_ADDED.name}' AND " +
|
||||
"${ManagementHelper.HDR_ROUTING_NAME} LIKE '$RPC_CLIENT_QUEUE_NAME_PREFIX.%'"
|
||||
|
||||
data class RpcRequestId(val toLong: Long)
|
||||
data class ObservableId(val toLong: Long)
|
||||
|
@ -1,4 +1,4 @@
|
||||
package net.corda.node.services.persistence.schemas
|
||||
package net.corda.node.services.persistence.schemas.requery
|
||||
|
||||
import io.requery.*
|
||||
import net.corda.core.crypto.SecureHash
|
@ -1,4 +1,4 @@
|
||||
package net.corda.node.services.vault.schemas
|
||||
package net.corda.node.services.vault.schemas.requery
|
||||
|
||||
import io.requery.*
|
||||
import net.corda.core.node.services.Vault
|
||||
@ -74,20 +74,4 @@ object VaultSchema {
|
||||
@get:Column(name = "lock_timestamp", nullable = true)
|
||||
var lockUpdateTime: Instant?
|
||||
}
|
||||
|
||||
/**
|
||||
* The following entity is for illustration purposes only as used by VaultQueryTests
|
||||
*/
|
||||
@Table(name = "vault_linear_states")
|
||||
@Entity(model = "vault")
|
||||
interface VaultLinearState : Persistable {
|
||||
|
||||
@get:Index("external_id_index")
|
||||
@get:Column(name = "external_id")
|
||||
var externalId: String
|
||||
|
||||
@get:Index("uuid_index")
|
||||
@get:Column(name = "uuid", unique = true, nullable = false)
|
||||
var uuid: UUID
|
||||
}
|
||||
}
|
@ -2,15 +2,13 @@ package net.corda.node.services.vault.schemas
|
||||
|
||||
import io.requery.Persistable
|
||||
import io.requery.TransactionIsolation
|
||||
import io.requery.kotlin.`in`
|
||||
import io.requery.kotlin.eq
|
||||
import io.requery.kotlin.invoke
|
||||
import io.requery.kotlin.isNull
|
||||
import io.requery.kotlin.*
|
||||
import io.requery.query.RowExpression
|
||||
import io.requery.rx.KotlinRxEntityStore
|
||||
import io.requery.sql.*
|
||||
import io.requery.sql.platform.Generic
|
||||
import net.corda.core.contracts.*
|
||||
import net.corda.core.contracts.TimeWindow
|
||||
import net.corda.core.crypto.CompositeKey
|
||||
import net.corda.core.crypto.SecureHash
|
||||
import net.corda.core.crypto.generateKeyPair
|
||||
@ -23,10 +21,8 @@ import net.corda.core.schemas.requery.converters.VaultStateStatusConverter
|
||||
import net.corda.core.serialization.deserialize
|
||||
import net.corda.core.serialization.serialize
|
||||
import net.corda.core.transactions.LedgerTransaction
|
||||
import net.corda.core.utilities.ALICE
|
||||
import net.corda.core.utilities.BOB
|
||||
import net.corda.core.utilities.DUMMY_NOTARY
|
||||
import net.corda.core.utilities.DUMMY_NOTARY_KEY
|
||||
import net.corda.core.utilities.*
|
||||
import net.corda.node.services.vault.schemas.requery.*
|
||||
import org.h2.jdbcx.JdbcDataSource
|
||||
import org.junit.After
|
||||
import org.junit.Assert
|
||||
@ -34,7 +30,6 @@ import org.junit.Before
|
||||
import org.junit.Test
|
||||
import rx.Observable
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
import java.util.concurrent.CountDownLatch
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.test.assertEquals
|
||||
@ -56,7 +51,7 @@ class VaultSchemaTest {
|
||||
fun setup() {
|
||||
val dataSource = JdbcDataSource()
|
||||
dataSource.setURL("jdbc:h2:mem:vault_persistence;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1")
|
||||
val configuration = KotlinConfiguration(dataSource = dataSource, model = Models.VAULT, mapping = setupCustomMapping())
|
||||
val configuration = KotlinConfiguration(dataSource = dataSource, model = Models.VAULT, mapping = setupCustomMapping(), useDefaultLogging = true)
|
||||
instance = KotlinEntityDataStore<Persistable>(configuration)
|
||||
oinstance = KotlinRxEntityStore(KotlinEntityDataStore<Persistable>(configuration))
|
||||
val tables = SchemaModifier(configuration)
|
||||
@ -102,7 +97,10 @@ class VaultSchemaTest {
|
||||
}
|
||||
|
||||
private fun setupDummyData() {
|
||||
// dummy Transaction
|
||||
// dummy Transaction comprised of 3 different Contract State types
|
||||
// 1. SingleOwnerState
|
||||
// 2. MultiOwnerState
|
||||
// 3. VaultNoopState
|
||||
val notary: Party = DUMMY_NOTARY
|
||||
val inState1 = TransactionState(DummyContract.SingleOwnerState(0, ALICE), notary)
|
||||
val inState2 = TransactionState(DummyContract.MultiOwnerState(0,
|
||||
@ -287,6 +285,24 @@ class VaultSchemaTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDistinctContractStateTypes() {
|
||||
val txn = createTxnWithTwoStateTypes()
|
||||
dummyStatesInsert(txn)
|
||||
|
||||
data.invoke {
|
||||
transaction!!.inputs.forEach {
|
||||
val stateEntity = createStateEntity(it)
|
||||
insert(stateEntity)
|
||||
}
|
||||
|
||||
val query = select(VaultSchema.VaultStates::contractStateClassName).distinct()
|
||||
val results = query.get()
|
||||
|
||||
Assert.assertSame(3, results.count())
|
||||
}
|
||||
}
|
||||
|
||||
private fun createStateEntity(stateAndRef: StateAndRef<*>, idx: Int? = null, txHash: String? = null): VaultStatesEntity {
|
||||
val stateRef = stateAndRef.ref
|
||||
val state = stateAndRef.state
|
||||
|
@ -1,4 +1,7 @@
|
||||
apply plugin: 'kotlin'
|
||||
// Java Persistence API support: create no-arg constructor
|
||||
// see: http://stackoverflow.com/questions/32038177/kotlin-with-jpa-default-constructor-hell
|
||||
apply plugin: 'kotlin-jpa'
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'net.corda.plugins.quasar-utils'
|
||||
apply plugin: 'net.corda.plugins.publish-utils'
|
||||
@ -137,6 +140,9 @@ dependencies {
|
||||
testCompile project(':test-utils')
|
||||
testCompile project(':client:jfx')
|
||||
|
||||
// sample test schemas
|
||||
testCompile project(path: ':finance', configuration: 'testArtifacts')
|
||||
|
||||
// For H2 database support in persistence
|
||||
compile "com.h2database:h2:$h2_version"
|
||||
|
||||
|
@ -11,12 +11,12 @@ import net.corda.testing.driver.driver
|
||||
import net.corda.node.internal.NodeStartup
|
||||
import net.corda.node.services.startFlowPermission
|
||||
import net.corda.nodeapi.User
|
||||
import net.corda.testing.ProjectStructure.projectRootDir
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.Assertions.assertThatThrownBy
|
||||
import org.junit.Test
|
||||
import java.io.*
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class BootTests {
|
||||
@ -33,7 +33,7 @@ class BootTests {
|
||||
|
||||
@Test
|
||||
fun `double node start doesn't write into log file`() {
|
||||
val logConfigFile = Paths.get("..", "config", "dev", "log4j2.xml").toAbsolutePath()
|
||||
val logConfigFile = projectRootDir / "config" / "dev" / "log4j2.xml"
|
||||
assertThat(logConfigFile).isRegularFile()
|
||||
driver(isDebug = true, systemProperties = mapOf("log4j.configurationFile" to logConfigFile.toString())) {
|
||||
val alice = startNode(ALICE.name).get()
|
||||
|
@ -1,71 +1,81 @@
|
||||
package net.corda.node.services
|
||||
|
||||
import com.google.common.util.concurrent.Futures
|
||||
import com.google.common.util.concurrent.ListenableFuture
|
||||
import com.google.common.net.HostAndPort
|
||||
import com.nhaarman.mockito_kotlin.whenever
|
||||
import net.corda.core.*
|
||||
import net.corda.core.contracts.*
|
||||
import net.corda.core.crypto.CompositeKey
|
||||
import net.corda.core.crypto.SecureHash
|
||||
import net.corda.core.crypto.appendToCommonName
|
||||
import net.corda.core.identity.Party
|
||||
import net.corda.core.node.services.ServiceInfo
|
||||
import net.corda.core.utilities.ALICE
|
||||
import net.corda.core.utilities.DUMMY_NOTARY
|
||||
import net.corda.flows.NotaryError
|
||||
import net.corda.flows.NotaryException
|
||||
import net.corda.flows.NotaryFlow
|
||||
import net.corda.node.internal.AbstractNode
|
||||
import net.corda.node.services.network.NetworkMapService
|
||||
import net.corda.node.services.transactions.BFTNonValidatingNotaryService
|
||||
import net.corda.node.services.transactions.minClusterSize
|
||||
import net.corda.node.services.transactions.minCorrectReplicas
|
||||
import net.corda.node.utilities.ServiceIdentityGenerator
|
||||
import net.corda.node.utilities.transaction
|
||||
import net.corda.testing.node.NodeBasedTest
|
||||
import net.corda.testing.node.MockNetwork
|
||||
import org.bouncycastle.asn1.x500.X500Name
|
||||
import org.junit.Ignore
|
||||
import org.junit.After
|
||||
import org.junit.Test
|
||||
import java.nio.file.Files
|
||||
import kotlin.test.*
|
||||
|
||||
class BFTNotaryServiceTests : NodeBasedTest() {
|
||||
class BFTNotaryServiceTests {
|
||||
companion object {
|
||||
private val clusterName = X500Name("CN=BFT,O=R3,OU=corda,L=Zurich,C=CH")
|
||||
private val serviceType = BFTNonValidatingNotaryService.type
|
||||
}
|
||||
|
||||
private fun bftNotaryCluster(clusterSize: Int): ListenableFuture<Party> {
|
||||
private val mockNet = MockNetwork()
|
||||
private val node = mockNet.createNode(advertisedServices = ServiceInfo(NetworkMapService.type))
|
||||
@After
|
||||
fun stopNodes() {
|
||||
mockNet.stopNodes()
|
||||
}
|
||||
|
||||
private fun bftNotaryCluster(clusterSize: Int): Party {
|
||||
Files.deleteIfExists("config" / "currentView") // XXX: Make config object warn if this exists?
|
||||
val replicaIds = (0 until clusterSize)
|
||||
val replicaNames = replicaIds.map { DUMMY_NOTARY.name.appendToCommonName(" $it") }
|
||||
val party = ServiceIdentityGenerator.generateToDisk(
|
||||
replicaNames.map { baseDirectory(it) },
|
||||
replicaIds.map { mockNet.baseDirectory(mockNet.nextNodeId + it) },
|
||||
serviceType.id,
|
||||
clusterName)
|
||||
val advertisedServices = setOf(ServiceInfo(serviceType, clusterName))
|
||||
val config = mapOf("notaryClusterAddresses" to replicaIds.map { "localhost:${11000 + it * 10}" })
|
||||
return Futures.allAsList(replicaIds.map {
|
||||
startNode(
|
||||
replicaNames[it],
|
||||
advertisedServices = advertisedServices,
|
||||
configOverrides = mapOf("bftReplicaId" to it) + config
|
||||
)
|
||||
}).map { party }
|
||||
val bftNotaryService = ServiceInfo(serviceType, clusterName)
|
||||
val notaryClusterAddresses = replicaIds.map { HostAndPort.fromParts("localhost", 11000 + it * 10) }
|
||||
replicaIds.forEach { replicaId ->
|
||||
mockNet.createNode(
|
||||
node.info.address,
|
||||
advertisedServices = bftNotaryService,
|
||||
configOverrides = {
|
||||
whenever(it.bftReplicaId).thenReturn(replicaId)
|
||||
whenever(it.notaryClusterAddresses).thenReturn(notaryClusterAddresses)
|
||||
})
|
||||
}
|
||||
return party
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Under investigation due to failure on TC build server")
|
||||
fun `detect double spend 1 faulty`() {
|
||||
detectDoubleSpend(1)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Under investigation due to failure on TC build server")
|
||||
fun `detect double spend 2 faulty`() {
|
||||
detectDoubleSpend(2)
|
||||
}
|
||||
|
||||
private fun detectDoubleSpend(faultyReplicas: Int) {
|
||||
val clusterSize = minClusterSize(faultyReplicas)
|
||||
val aliceFuture = startNode(ALICE.name)
|
||||
val notary = bftNotaryCluster(clusterSize).getOrThrow()
|
||||
aliceFuture.getOrThrow().run {
|
||||
val notary = bftNotaryCluster(clusterSize)
|
||||
node.run {
|
||||
val issueTx = signInitialTransaction(notary) {
|
||||
addOutputState(DummyContract.SingleOwnerState(owner = info.legalIdentity))
|
||||
}
|
||||
@ -80,6 +90,7 @@ class BFTNotaryServiceTests : NodeBasedTest() {
|
||||
assertEquals(spendTxs.size, spendTxs.map { it.id }.distinct().size)
|
||||
val flows = spendTxs.map { NotaryFlow.Client(it) }
|
||||
val stateMachines = flows.map { services.startFlow(it) }
|
||||
mockNet.runNetwork()
|
||||
val results = stateMachines.map { ErrorOr.catch { it.resultFuture.getOrThrow() } }
|
||||
val successfulIndex = results.mapIndexedNotNull { index, result ->
|
||||
if (result.error == null) {
|
||||
|
@ -1,15 +1,14 @@
|
||||
package net.corda.services.messaging
|
||||
|
||||
import com.google.common.util.concurrent.ListenableFuture
|
||||
import com.nhaarman.mockito_kotlin.whenever
|
||||
import net.corda.core.crypto.X509Utilities
|
||||
import net.corda.core.crypto.cert
|
||||
import net.corda.core.getOrThrow
|
||||
import net.corda.core.node.NodeInfo
|
||||
import net.corda.core.random63BitValue
|
||||
import net.corda.core.seconds
|
||||
import net.corda.core.utilities.BOB
|
||||
import net.corda.core.utilities.DUMMY_BANK_A
|
||||
import net.corda.core.utilities.DUMMY_BANK_B
|
||||
import net.corda.core.utilities.getTestPartyAndCertificate
|
||||
import net.corda.core.utilities.*
|
||||
import net.corda.node.internal.NetworkMapInfo
|
||||
import net.corda.node.services.config.configureWithDevSSLCertificate
|
||||
import net.corda.node.services.messaging.sendRequest
|
||||
@ -18,14 +17,14 @@ import net.corda.node.services.network.NetworkMapService.RegistrationRequest
|
||||
import net.corda.node.services.network.NodeRegistration
|
||||
import net.corda.node.utilities.AddOrRemove
|
||||
import net.corda.testing.MOCK_VERSION_INFO
|
||||
import net.corda.testing.TestNodeConfiguration
|
||||
import net.corda.testing.node.NodeBasedTest
|
||||
import net.corda.testing.node.SimpleNode
|
||||
import net.corda.testing.testNodeConfiguration
|
||||
import org.assertj.core.api.Assertions.assertThatExceptionOfType
|
||||
import org.assertj.core.api.Assertions.assertThatThrownBy
|
||||
import org.bouncycastle.asn1.x500.X500Name
|
||||
import org.bouncycastle.cert.X509CertificateHolder
|
||||
import org.junit.Test
|
||||
import java.security.cert.X509Certificate
|
||||
import java.time.Instant
|
||||
import java.util.concurrent.TimeoutException
|
||||
|
||||
@ -46,7 +45,7 @@ class P2PSecurityTest : NodeBasedTest() {
|
||||
|
||||
@Test
|
||||
fun `register with the network map service using a legal name different from the TLS CN`() {
|
||||
startSimpleNode(DUMMY_BANK_A.name).use {
|
||||
startSimpleNode(DUMMY_BANK_A.name, DUMMY_CA.certificate.cert).use {
|
||||
// Register with the network map using a different legal name
|
||||
val response = it.registerWithNetworkMap(DUMMY_BANK_B.name)
|
||||
// We don't expect a response because the network map's host verification will prevent a connection back
|
||||
@ -58,11 +57,12 @@ class P2PSecurityTest : NodeBasedTest() {
|
||||
}
|
||||
|
||||
private fun startSimpleNode(legalName: X500Name,
|
||||
trustRoot: X509CertificateHolder? = null): SimpleNode {
|
||||
val config = TestNodeConfiguration(
|
||||
trustRoot: X509Certificate): SimpleNode {
|
||||
val config = testNodeConfiguration(
|
||||
baseDirectory = baseDirectory(legalName),
|
||||
myLegalName = legalName,
|
||||
networkMapService = NetworkMapInfo(networkMapNode.configuration.p2pAddress, networkMapNode.info.legalIdentity.name))
|
||||
myLegalName = legalName).also {
|
||||
whenever(it.networkMapService).thenReturn(NetworkMapInfo(networkMapNode.configuration.p2pAddress, networkMapNode.info.legalIdentity.name))
|
||||
}
|
||||
config.configureWithDevSSLCertificate() // This creates the node's TLS cert with the CN as the legal name
|
||||
return SimpleNode(config, trustRoot = trustRoot).apply { start() }
|
||||
}
|
||||
|
@ -29,9 +29,9 @@ import net.corda.core.utilities.getTestPartyAndCertificate
|
||||
import net.corda.flows.*
|
||||
import net.corda.node.services.*
|
||||
import net.corda.node.services.api.*
|
||||
import net.corda.node.services.config.FullNodeConfiguration
|
||||
import net.corda.node.services.config.NodeConfiguration
|
||||
import net.corda.node.services.config.configureWithDevSSLCertificate
|
||||
import net.corda.node.services.database.HibernateConfiguration
|
||||
import net.corda.node.services.events.NodeSchedulerService
|
||||
import net.corda.node.services.events.ScheduledActivityObserver
|
||||
import net.corda.node.services.identity.InMemoryIdentityService
|
||||
@ -51,6 +51,7 @@ import net.corda.node.services.statemachine.StateMachineManager
|
||||
import net.corda.node.services.statemachine.flowVersionAndInitiatingClass
|
||||
import net.corda.node.services.transactions.*
|
||||
import net.corda.node.services.vault.CashBalanceAsMetricsObserver
|
||||
import net.corda.node.services.vault.HibernateVaultQueryImpl
|
||||
import net.corda.node.services.vault.NodeVaultService
|
||||
import net.corda.node.services.vault.VaultSoftLockManager
|
||||
import net.corda.node.utilities.AddOrRemove.ADD
|
||||
@ -63,6 +64,7 @@ import org.jetbrains.exposed.sql.Database
|
||||
import org.slf4j.Logger
|
||||
import rx.Observable
|
||||
import java.io.IOException
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
import java.lang.reflect.Modifier.*
|
||||
import java.net.JarURLConnection
|
||||
import java.net.URI
|
||||
@ -121,6 +123,7 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
|
||||
override val networkMapCache: NetworkMapCacheInternal get() = netMapCache
|
||||
override val storageService: TxWritableStorageService get() = storage
|
||||
override val vaultService: VaultService get() = vault
|
||||
override val vaultQueryService: VaultQueryService get() = vaultQuery
|
||||
override val keyManagementService: KeyManagementService get() = keyManagement
|
||||
override val identityService: IdentityService get() = identity
|
||||
override val schedulerService: SchedulerService get() = scheduler
|
||||
@ -164,6 +167,7 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
|
||||
lateinit var checkpointStorage: CheckpointStorage
|
||||
lateinit var smm: StateMachineManager
|
||||
lateinit var vault: VaultService
|
||||
lateinit var vaultQuery: VaultQueryService
|
||||
lateinit var keyManagement: KeyManagementService
|
||||
var inNodeNetworkMapService: NetworkMapService? = null
|
||||
lateinit var txVerifierService: TransactionVerifierService
|
||||
@ -216,7 +220,8 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
|
||||
|
||||
// Do all of this in a database transaction so anything that might need a connection has one.
|
||||
initialiseDatabasePersistence {
|
||||
val tokenizableServices = makeServices()
|
||||
val keyStoreWrapper = KeyStoreWrapper(configuration.trustStoreFile, configuration.trustStorePassword)
|
||||
val tokenizableServices = makeServices(keyStoreWrapper)
|
||||
|
||||
smm = StateMachineManager(services,
|
||||
checkpointStorage,
|
||||
@ -269,6 +274,8 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
|
||||
return this
|
||||
}
|
||||
|
||||
private class ServiceInstantiationException(cause: Throwable?) : Exception(cause)
|
||||
|
||||
private fun installCordaServices(scanResult: ScanResult) {
|
||||
fun getServiceType(clazz: Class<*>): ServiceType? {
|
||||
return try {
|
||||
@ -296,6 +303,8 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
|
||||
} catch (e: NoSuchMethodException) {
|
||||
log.error("${it.name}, as a Corda service, must have a constructor with a single parameter " +
|
||||
"of type ${PluginServiceHub::class.java.name}")
|
||||
} catch (e: ServiceInstantiationException) {
|
||||
log.error("Corda service ${it.name} failed to instantiate", e.cause)
|
||||
} catch (e: Exception) {
|
||||
log.error("Unable to install Corda service ${it.name}", e)
|
||||
}
|
||||
@ -306,13 +315,17 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
|
||||
* Use this method to install your Corda services in your tests. This is automatically done by the node when it
|
||||
* starts up for all classes it finds which are annotated with [CordaService].
|
||||
*/
|
||||
fun <T : SerializeAsToken> installCordaService(clazz: Class<T>): T {
|
||||
clazz.requireAnnotation<CordaService>()
|
||||
val ctor = clazz.getDeclaredConstructor(PluginServiceHub::class.java).apply { isAccessible = true }
|
||||
val service = ctor.newInstance(services)
|
||||
cordappServices.putInstance(clazz, service)
|
||||
fun <T : SerializeAsToken> installCordaService(serviceClass: Class<T>): T {
|
||||
serviceClass.requireAnnotation<CordaService>()
|
||||
val constructor = serviceClass.getDeclaredConstructor(PluginServiceHub::class.java).apply { isAccessible = true }
|
||||
val service = try {
|
||||
constructor.newInstance(services)
|
||||
} catch (e: InvocationTargetException) {
|
||||
throw ServiceInstantiationException(e.cause)
|
||||
}
|
||||
cordappServices.putInstance(serviceClass, service)
|
||||
smm.tokenizableServices += service
|
||||
log.info("Installed ${clazz.name} Corda service")
|
||||
log.info("Installed ${serviceClass.name} Corda service")
|
||||
return service
|
||||
}
|
||||
|
||||
@ -439,7 +452,8 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
|
||||
* Builds node internal, advertised, and plugin services.
|
||||
* Returns a list of tokenizable services to be added to the serialisation context.
|
||||
*/
|
||||
private fun makeServices(): MutableList<Any> {
|
||||
private fun makeServices(keyStoreWrapper: KeyStoreWrapper): MutableList<Any> {
|
||||
val keyStore = keyStoreWrapper.keyStore
|
||||
val storageServices = initialiseStorageService(configuration.baseDirectory)
|
||||
storage = storageServices.first
|
||||
checkpointStorage = storageServices.second
|
||||
@ -447,11 +461,14 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
|
||||
network = makeMessagingService()
|
||||
schemas = makeSchemaService()
|
||||
vault = makeVaultService(configuration.dataSourceProperties)
|
||||
vaultQuery = makeVaultQueryService(schemas)
|
||||
txVerifierService = makeTransactionVerifierService()
|
||||
auditService = DummyAuditService()
|
||||
|
||||
info = makeInfo()
|
||||
identity = makeIdentityService()
|
||||
identity = makeIdentityService(keyStore.getCertificate(X509Utilities.CORDA_ROOT_CA)!! as X509Certificate,
|
||||
keyStoreWrapper.certificateAndKeyPair(X509Utilities.CORDA_CLIENT_CA),
|
||||
info.legalIdentityAndCert)
|
||||
// Place the long term identity key in the KMS. Eventually, this is likely going to be separated again because
|
||||
// the KMS is meant for derived temporary keys used in transactions, and we're not supposed to sign things with
|
||||
// the identity key. But the infrastructure to make that easy isn't here yet.
|
||||
@ -525,7 +542,7 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
|
||||
VaultSoftLockManager(vault, smm)
|
||||
CashBalanceAsMetricsObserver(services, database)
|
||||
ScheduledActivityObserver(services)
|
||||
HibernateObserver(vault.rawUpdates, schemas)
|
||||
HibernateObserver(vault.rawUpdates, HibernateConfiguration(schemas))
|
||||
}
|
||||
|
||||
private fun makeInfo(): NodeInfo {
|
||||
@ -668,7 +685,7 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
|
||||
ValidatingNotaryService.type -> ValidatingNotaryService(timeWindowChecker, uniquenessProvider)
|
||||
RaftNonValidatingNotaryService.type -> RaftNonValidatingNotaryService(timeWindowChecker, uniquenessProvider as RaftUniquenessProvider)
|
||||
RaftValidatingNotaryService.type -> RaftValidatingNotaryService(timeWindowChecker, uniquenessProvider as RaftUniquenessProvider)
|
||||
BFTNonValidatingNotaryService.type -> with(configuration as FullNodeConfiguration) {
|
||||
BFTNonValidatingNotaryService.type -> with(configuration) {
|
||||
val replicaId = bftReplicaId ?: throw IllegalArgumentException("bftReplicaId value must be specified in the configuration")
|
||||
BFTSMaRtConfig(notaryClusterAddresses).use { config ->
|
||||
BFTNonValidatingNotaryService(config, services, timeWindowChecker, replicaId, database).also {
|
||||
@ -687,10 +704,13 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
|
||||
|
||||
protected abstract fun makeUniquenessProvider(type: ServiceType): UniquenessProvider
|
||||
|
||||
protected open fun makeIdentityService(): IdentityService {
|
||||
val keyStore = KeyStoreUtilities.loadKeyStore(configuration.trustStoreFile, configuration.trustStorePassword)
|
||||
val trustRoot = keyStore.getCertificate(X509Utilities.CORDA_ROOT_CA) as? X509Certificate
|
||||
val service = InMemoryIdentityService(setOf(info.legalIdentityAndCert), trustRoot = trustRoot)
|
||||
protected open fun makeIdentityService(trustRoot: X509Certificate,
|
||||
clientCa: CertificateAndKeyPair?,
|
||||
legalIdentity: PartyAndCertificate): IdentityService {
|
||||
val caCertificates: Array<X509Certificate> = listOf(legalIdentity.certificate.cert, clientCa?.certificate?.cert)
|
||||
.filterNotNull()
|
||||
.toTypedArray()
|
||||
val service = InMemoryIdentityService(setOf(info.legalIdentityAndCert), trustRoot = trustRoot, caCertificates = *caCertificates)
|
||||
services.networkMapCache.partyNodes.forEach { service.registerIdentity(it.legalIdentityAndCert) }
|
||||
netMapCache.changed.subscribe { mapChange ->
|
||||
// TODO how should we handle network map removal
|
||||
@ -704,7 +724,9 @@ abstract class AbstractNode(open val configuration: NodeConfiguration,
|
||||
// TODO: sort out ordering of open & protected modifiers of functions in this class.
|
||||
protected open fun makeVaultService(dataSourceProperties: Properties): VaultService = NodeVaultService(services, dataSourceProperties)
|
||||
|
||||
protected open fun makeSchemaService(): SchemaService = NodeSchemaService()
|
||||
protected open fun makeVaultQueryService(schemas: SchemaService): VaultQueryService = HibernateVaultQueryImpl(HibernateConfiguration(schemas), vault.updatesPublisher)
|
||||
|
||||
protected open fun makeSchemaService(): SchemaService = NodeSchemaService(pluginRegistries.flatMap { it.requiredSchemas }.toSet())
|
||||
|
||||
protected abstract fun makeTransactionVerifierService(): TransactionVerifierService
|
||||
|
||||
|
@ -14,6 +14,7 @@ import net.corda.core.node.NodeInfo
|
||||
import net.corda.core.node.services.NetworkMapCache
|
||||
import net.corda.core.node.services.StateMachineTransactionMapping
|
||||
import net.corda.core.node.services.Vault
|
||||
import net.corda.core.node.services.queryBy
|
||||
import net.corda.core.node.services.vault.PageSpecification
|
||||
import net.corda.core.node.services.vault.QueryCriteria
|
||||
import net.corda.core.node.services.vault.Sort
|
||||
@ -57,18 +58,20 @@ class CordaRPCOpsImpl(
|
||||
|
||||
override fun <T : ContractState> vaultQueryBy(criteria: QueryCriteria,
|
||||
paging: PageSpecification,
|
||||
sorting: Sort): Vault.Page<T> {
|
||||
sorting: Sort,
|
||||
contractType: Class<out T>): Vault.Page<T> {
|
||||
return database.transaction {
|
||||
services.vaultService.queryBy<T>(criteria, paging, sorting)
|
||||
services.vaultQueryService._queryBy(criteria, paging, sorting, contractType)
|
||||
}
|
||||
}
|
||||
|
||||
@RPCReturnsObservables
|
||||
override fun <T : ContractState> vaultTrackBy(criteria: QueryCriteria,
|
||||
paging: PageSpecification,
|
||||
sorting: Sort): Vault.PageAndUpdates<T> {
|
||||
sorting: Sort,
|
||||
contractType: Class<out T>): Vault.PageAndUpdates<T> {
|
||||
return database.transaction {
|
||||
services.vaultService.trackBy<T>(criteria, paging, sorting)
|
||||
services.vaultQueryService._trackBy<T>(criteria, paging, sorting, contractType)
|
||||
}
|
||||
}
|
||||
|
||||
@ -194,3 +197,4 @@ class CordaRPCOpsImpl(
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -40,6 +40,7 @@ import net.corda.nodeapi.ConnectionDirection
|
||||
import net.corda.nodeapi.internal.ShutdownHook
|
||||
import net.corda.nodeapi.internal.addShutdownHook
|
||||
import org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException
|
||||
import org.apache.activemq.artemis.api.core.RoutingType
|
||||
import org.apache.activemq.artemis.api.core.client.ActiveMQClient
|
||||
import org.apache.activemq.artemis.api.core.client.ClientMessage
|
||||
import org.bouncycastle.asn1.x500.X500Name
|
||||
@ -171,8 +172,11 @@ open class Node(override val configuration: FullNodeConfiguration,
|
||||
if (relay != null) {
|
||||
HostAndPort.fromParts(relay.relayHost, relay.remoteInboundPort)
|
||||
} else {
|
||||
val publicHost = tryDetectIfNotPublicHost(p2pAddress.host)
|
||||
val useHost = publicHost ?: p2pAddress.host
|
||||
val useHost = if (detectPublicIp) {
|
||||
tryDetectIfNotPublicHost(p2pAddress.host) ?: p2pAddress.host
|
||||
} else {
|
||||
p2pAddress.host
|
||||
}
|
||||
HostAndPort.fromParts(useHost, p2pAddress.port)
|
||||
}
|
||||
}
|
||||
@ -227,7 +231,7 @@ open class Node(override val configuration: FullNodeConfiguration,
|
||||
session.start()
|
||||
|
||||
val queueName = "$IP_REQUEST_PREFIX$requestId"
|
||||
session.createQueue(queueName, queueName, false)
|
||||
session.createQueue(queueName, RoutingType.MULTICAST, queueName, false)
|
||||
|
||||
val consumer = session.createConsumer(queueName)
|
||||
val artemisMessage: ClientMessage = consumer.receive(10.seconds.toMillis()) ?:
|
||||
|
@ -316,11 +316,11 @@ open class NodeStartup(val args: Array<String>) {
|
||||
messages += "Kind of like a regular database but\nwith emojis, colours and ascii art. ${Emoji.coolGuy}"
|
||||
val (msg1, msg2) = messages.randomOrNull()!!.split('\n')
|
||||
|
||||
println(Ansi.ansi().fgBrightRed().a("""
|
||||
______ __
|
||||
/ ____/ _________/ /___ _
|
||||
/ / __ / ___/ __ / __ `/ """).fgBrightBlue().a(msg1).newline().fgBrightRed().a(
|
||||
"/ /___ /_/ / / / /_/ / /_/ / ").fgBrightBlue().a(msg2).newline().fgBrightRed().a(
|
||||
println(Ansi.ansi().newline().fgBrightRed().a(
|
||||
""" ______ __""").newline().a(
|
||||
""" / ____/ _________/ /___ _""").newline().a(
|
||||
""" / / __ / ___/ __ / __ `/ """).fgBrightBlue().a(msg1).newline().fgBrightRed().a(
|
||||
"""/ /___ /_/ / / / /_/ / /_/ / """).fgBrightBlue().a(msg2).newline().fgBrightRed().a(
|
||||
"""\____/ /_/ \__,_/\__,_/""").reset().newline().newline().fgBrightDefault().bold().
|
||||
a("--- ${versionInfo.vendor} ${versionInfo.releaseVersion} (${versionInfo.revision.take(7)}) -----------------------------------------------").
|
||||
newline().
|
||||
|
@ -1,8 +1,8 @@
|
||||
package net.corda.node.services.api
|
||||
|
||||
import net.corda.core.contracts.ContractState
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.core.schemas.PersistentState
|
||||
import net.corda.core.schemas.QueryableState
|
||||
|
||||
//DOCSTART SchemaService
|
||||
/**
|
||||
@ -23,12 +23,12 @@ interface SchemaService {
|
||||
* Given a state, select schemas to map it to that are supported by [generateMappedObject] and that are configured
|
||||
* for this node.
|
||||
*/
|
||||
fun selectSchemas(state: QueryableState): Iterable<MappedSchema>
|
||||
fun selectSchemas(state: ContractState): Iterable<MappedSchema>
|
||||
|
||||
/**
|
||||
* Map a state to a [PersistentState] for the given schema, either via direct support from the state
|
||||
* or via custom logic in this service.
|
||||
*/
|
||||
fun generateMappedObject(state: QueryableState, schema: MappedSchema): PersistentState
|
||||
fun generateMappedObject(state: ContractState, schema: MappedSchema): PersistentState
|
||||
}
|
||||
//DOCEND SchemaService
|
||||
|
@ -26,6 +26,9 @@ interface NodeConfiguration : NodeSSLConfiguration {
|
||||
val certificateChainCheckPolicies: List<CertChainPolicyConfig>
|
||||
val verifierType: VerifierType
|
||||
val messageRedeliveryDelaySeconds: Int
|
||||
val bftReplicaId: Int?
|
||||
val notaryNodeAddress: HostAndPort?
|
||||
val notaryClusterAddresses: List<HostAndPort>
|
||||
}
|
||||
|
||||
data class FullNodeConfiguration(
|
||||
@ -54,12 +57,13 @@ data class FullNodeConfiguration(
|
||||
// Instead this should be a Boolean indicating whether that broker is an internal one started by the node or an external one
|
||||
val messagingServerAddress: HostAndPort?,
|
||||
val extraAdvertisedServiceIds: List<String>,
|
||||
val bftReplicaId: Int?,
|
||||
val notaryNodeAddress: HostAndPort?,
|
||||
val notaryClusterAddresses: List<HostAndPort>,
|
||||
override val bftReplicaId: Int?,
|
||||
override val notaryNodeAddress: HostAndPort?,
|
||||
override val notaryClusterAddresses: List<HostAndPort>,
|
||||
override val certificateChainCheckPolicies: List<CertChainPolicyConfig>,
|
||||
override val devMode: Boolean = false,
|
||||
val useTestClock: Boolean = false
|
||||
val useTestClock: Boolean = false,
|
||||
val detectPublicIp: Boolean = true
|
||||
) : NodeConfiguration {
|
||||
/** This is not retrieved from the config file but rather from a command line argument. */
|
||||
@Suppress("DEPRECATION")
|
||||
|
@ -0,0 +1,119 @@
|
||||
package net.corda.node.services.database
|
||||
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.core.utilities.debug
|
||||
import net.corda.core.utilities.loggerFor
|
||||
import net.corda.node.services.api.SchemaService
|
||||
import org.hibernate.SessionFactory
|
||||
import org.hibernate.boot.MetadataSources
|
||||
import org.hibernate.boot.model.naming.Identifier
|
||||
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
|
||||
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder
|
||||
import org.hibernate.cfg.Configuration
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider
|
||||
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment
|
||||
import org.hibernate.service.UnknownUnwrapTypeException
|
||||
import org.jetbrains.exposed.sql.transactions.TransactionManager
|
||||
import java.sql.Connection
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
class HibernateConfiguration(val schemaService: SchemaService, val useDefaultLogging: Boolean = false) {
|
||||
constructor(schemaService: SchemaService) : this(schemaService, false)
|
||||
|
||||
companion object {
|
||||
val logger = loggerFor<HibernateConfiguration>()
|
||||
}
|
||||
|
||||
// TODO: make this a guava cache or similar to limit ability for this to grow forever.
|
||||
val sessionFactories = ConcurrentHashMap<MappedSchema, SessionFactory>()
|
||||
|
||||
init {
|
||||
schemaService.schemaOptions.map { it.key }.forEach { mappedSchema ->
|
||||
sessionFactories.computeIfAbsent(mappedSchema, { makeSessionFactoryForSchema(mappedSchema) })
|
||||
}
|
||||
}
|
||||
|
||||
fun sessionFactoryForRegisteredSchemas(): SessionFactory {
|
||||
return sessionFactoryForSchemas(*schemaService.schemaOptions.map { it.key }.toTypedArray())
|
||||
}
|
||||
|
||||
fun sessionFactoryForSchema(schema: MappedSchema): SessionFactory {
|
||||
return sessionFactories.computeIfAbsent(schema, { sessionFactoryForSchemas(schema) })
|
||||
}
|
||||
|
||||
fun sessionFactoryForSchemas(vararg schemas: MappedSchema): SessionFactory {
|
||||
return makeSessionFactoryForSchemas(schemas.iterator())
|
||||
}
|
||||
|
||||
private fun makeSessionFactoryForSchema(schema: MappedSchema): SessionFactory {
|
||||
return makeSessionFactoryForSchemas(setOf(schema).iterator())
|
||||
}
|
||||
|
||||
private fun makeSessionFactoryForSchemas(schemas: Iterator<MappedSchema>): SessionFactory {
|
||||
logger.info("Creating session factory for schemas: $schemas")
|
||||
val serviceRegistry = BootstrapServiceRegistryBuilder().build()
|
||||
val metadataSources = MetadataSources(serviceRegistry)
|
||||
// We set a connection provider as the auto schema generation requires it. The auto schema generation will not
|
||||
// necessarily remain and would likely be replaced by something like Liquibase. For now it is very convenient though.
|
||||
// TODO: replace auto schema generation as it isn't intended for production use, according to Hibernate docs.
|
||||
val config = Configuration(metadataSources).setProperty("hibernate.connection.provider_class", HibernateConfiguration.NodeDatabaseConnectionProvider::class.java.name)
|
||||
.setProperty("hibernate.hbm2ddl.auto", "update")
|
||||
.setProperty("hibernate.show_sql", "$useDefaultLogging")
|
||||
.setProperty("hibernate.format_sql", "$useDefaultLogging")
|
||||
schemas.forEach { schema ->
|
||||
// TODO: require mechanism to set schemaOptions (databaseSchema, tablePrefix) which are not global to session
|
||||
schema.mappedTypes.forEach { config.addAnnotatedClass(it) }
|
||||
}
|
||||
val sessionFactory = buildSessionFactory(config, metadataSources, "")
|
||||
logger.info("Created session factory for schemas: $schemas")
|
||||
return sessionFactory
|
||||
}
|
||||
|
||||
private fun buildSessionFactory(config: Configuration, metadataSources: MetadataSources, tablePrefix: String): SessionFactory {
|
||||
config.standardServiceRegistryBuilder.applySettings(config.properties)
|
||||
val metadata = metadataSources.getMetadataBuilder(config.standardServiceRegistryBuilder.build()).run {
|
||||
applyPhysicalNamingStrategy(object : PhysicalNamingStrategyStandardImpl() {
|
||||
override fun toPhysicalTableName(name: Identifier?, context: JdbcEnvironment?): Identifier {
|
||||
val default = super.toPhysicalTableName(name, context)
|
||||
return Identifier.toIdentifier(tablePrefix + default.text, default.isQuoted)
|
||||
}
|
||||
})
|
||||
build()
|
||||
}
|
||||
|
||||
return metadata.sessionFactoryBuilder.run {
|
||||
allowOutOfTransactionUpdateOperations(true)
|
||||
applySecondLevelCacheSupport(false)
|
||||
applyQueryCacheSupport(false)
|
||||
enableReleaseResourcesOnCloseEnabled(true)
|
||||
build()
|
||||
}
|
||||
}
|
||||
|
||||
// Supply Hibernate with connections from our underlying Exposed database integration. Only used
|
||||
// during schema creation / update.
|
||||
class NodeDatabaseConnectionProvider : ConnectionProvider {
|
||||
override fun closeConnection(conn: Connection) {
|
||||
val tx = TransactionManager.current()
|
||||
tx.commit()
|
||||
tx.close()
|
||||
}
|
||||
|
||||
override fun supportsAggressiveRelease(): Boolean = true
|
||||
|
||||
override fun getConnection(): Connection {
|
||||
val tx = TransactionManager.manager.newTransaction(Connection.TRANSACTION_REPEATABLE_READ)
|
||||
return tx.connection
|
||||
}
|
||||
|
||||
override fun <T : Any?> unwrap(unwrapType: Class<T>): T {
|
||||
try {
|
||||
return unwrapType.cast(this)
|
||||
} catch(e: ClassCastException) {
|
||||
throw UnknownUnwrapTypeException(unwrapType)
|
||||
}
|
||||
}
|
||||
|
||||
override fun isUnwrappableAs(unwrapType: Class<*>?): Boolean = (unwrapType == NodeDatabaseConnectionProvider::class.java)
|
||||
}
|
||||
}
|
@ -31,22 +31,30 @@ import kotlin.collections.ArrayList
|
||||
* @param certPaths initial set of certificate paths for the service, typically only used for unit tests.
|
||||
*/
|
||||
@ThreadSafe
|
||||
class InMemoryIdentityService(identities: Iterable<PartyAndCertificate>,
|
||||
class InMemoryIdentityService(identities: Iterable<PartyAndCertificate> = emptySet(),
|
||||
certPaths: Map<AnonymousParty, CertPath> = emptyMap(),
|
||||
val trustRoot: X509Certificate?) : SingletonSerializeAsToken(), IdentityService {
|
||||
override val trustRoot: X509Certificate,
|
||||
vararg caCertificates: X509Certificate) : SingletonSerializeAsToken(), IdentityService {
|
||||
constructor(identities: Iterable<PartyAndCertificate> = emptySet(),
|
||||
certPaths: Map<AnonymousParty, CertPath> = emptyMap(),
|
||||
trustRoot: X509CertificateHolder?) : this(identities, certPaths, trustRoot?.cert)
|
||||
trustRoot: X509CertificateHolder) : this(identities, certPaths, trustRoot.cert)
|
||||
companion object {
|
||||
private val log = loggerFor<InMemoryIdentityService>()
|
||||
}
|
||||
|
||||
private val trustAnchor: TrustAnchor? = trustRoot?.let { cert -> TrustAnchor(cert, null) }
|
||||
/**
|
||||
* Certificate store for certificate authority and intermediary certificates.
|
||||
*/
|
||||
override val caCertStore: CertStore
|
||||
override val trustRootHolder = X509CertificateHolder(trustRoot.encoded)
|
||||
private val trustAnchor: TrustAnchor = TrustAnchor(trustRoot, null)
|
||||
private val keyToParties = ConcurrentHashMap<PublicKey, PartyAndCertificate>()
|
||||
private val principalToParties = ConcurrentHashMap<X500Name, PartyAndCertificate>()
|
||||
private val partyToPath = ConcurrentHashMap<AbstractParty, CertPath>()
|
||||
|
||||
init {
|
||||
val caCertificatesWithRoot: Set<X509Certificate> = caCertificates.toSet() + trustRoot
|
||||
caCertStore = CertStore.getInstance("Collection", CollectionCertStoreParameters(caCertificatesWithRoot))
|
||||
keyToParties.putAll(identities.associateBy { it.owningKey } )
|
||||
principalToParties.putAll(identities.associateBy { it.name })
|
||||
partyToPath.putAll(certPaths)
|
||||
@ -57,7 +65,7 @@ class InMemoryIdentityService(identities: Iterable<PartyAndCertificate>,
|
||||
override fun registerIdentity(party: PartyAndCertificate) {
|
||||
require(party.certPath.certificates.isNotEmpty()) { "Certificate path must contain at least one certificate" }
|
||||
// Validate the chain first, before we do anything clever with it
|
||||
if (trustRoot != null) validateCertificatePath(party.party, party.certPath)
|
||||
validateCertificatePath(party.party, party.certPath)
|
||||
|
||||
log.trace { "Registering identity $party" }
|
||||
require(Arrays.equals(party.certificate.subjectPublicKeyInfo.encoded, party.owningKey.encoded)) { "Party certificate must end with party's public key" }
|
||||
@ -122,7 +130,7 @@ class InMemoryIdentityService(identities: Iterable<PartyAndCertificate>,
|
||||
val fullParty = certificateFromParty(party) ?: throw IllegalArgumentException("Unknown identity ${party.name}")
|
||||
require(path.certificates.isNotEmpty()) { "Certificate path must contain at least one certificate" }
|
||||
// Validate the chain first, before we do anything clever with it
|
||||
if (trustRoot != null) validateCertificatePath(anonymousParty, path)
|
||||
validateCertificatePath(anonymousParty, path)
|
||||
val subjectCertificate = path.certificates.first()
|
||||
require(subjectCertificate is X509Certificate && subjectCertificate.subject == fullParty.name) { "Subject of the transaction certificate must match the well known identity" }
|
||||
|
||||
|
@ -30,12 +30,12 @@ import org.apache.activemq.artemis.core.config.Configuration
|
||||
import org.apache.activemq.artemis.core.config.CoreQueueConfiguration
|
||||
import org.apache.activemq.artemis.core.config.impl.ConfigurationImpl
|
||||
import org.apache.activemq.artemis.core.config.impl.SecurityConfiguration
|
||||
import org.apache.activemq.artemis.core.message.impl.CoreMessage
|
||||
import org.apache.activemq.artemis.core.remoting.impl.netty.*
|
||||
import org.apache.activemq.artemis.core.security.Role
|
||||
import org.apache.activemq.artemis.core.server.ActiveMQServer
|
||||
import org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl
|
||||
import org.apache.activemq.artemis.core.server.impl.RoutingContextImpl
|
||||
import org.apache.activemq.artemis.core.server.impl.ServerMessageImpl
|
||||
import org.apache.activemq.artemis.core.settings.impl.AddressFullMessagePolicy
|
||||
import org.apache.activemq.artemis.core.settings.impl.AddressSettings
|
||||
import org.apache.activemq.artemis.spi.core.remoting.*
|
||||
@ -200,6 +200,12 @@ class ArtemisMessagingServer(override val config: NodeConfiguration,
|
||||
address = NOTIFICATIONS_ADDRESS,
|
||||
filter = RPCApi.RPC_CLIENT_BINDING_REMOVAL_FILTER_EXPRESSION,
|
||||
durable = false
|
||||
),
|
||||
queueConfig(
|
||||
name = RPCApi.RPC_CLIENT_BINDING_ADDITIONS,
|
||||
address = NOTIFICATIONS_ADDRESS,
|
||||
filter = RPCApi.RPC_CLIENT_BINDING_ADDITION_FILTER_EXPRESSION,
|
||||
durable = false
|
||||
)
|
||||
)
|
||||
addressesSettings = mapOf(
|
||||
@ -445,7 +451,7 @@ class ArtemisMessagingServer(override val config: NodeConfiguration,
|
||||
}
|
||||
|
||||
fun sendResponse(remoteAddress: String?) {
|
||||
val responseMessage = ServerMessageImpl(random63BitValue(), 0).apply {
|
||||
val responseMessage = CoreMessage(random63BitValue(), 0).apply {
|
||||
putStringProperty(ipDetectResponseProperty, remoteAddress)
|
||||
}
|
||||
val routingContext = RoutingContextImpl(null)
|
||||
|
@ -32,6 +32,7 @@ import net.corda.nodeapi.VerifierApi.VERIFICATION_REQUESTS_QUEUE_NAME
|
||||
import net.corda.nodeapi.VerifierApi.VERIFICATION_RESPONSES_QUEUE_NAME_PREFIX
|
||||
import org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException
|
||||
import org.apache.activemq.artemis.api.core.Message.*
|
||||
import org.apache.activemq.artemis.api.core.RoutingType
|
||||
import org.apache.activemq.artemis.api.core.SimpleString
|
||||
import org.apache.activemq.artemis.api.core.client.*
|
||||
import org.apache.activemq.artemis.api.core.client.ActiveMQClient.DEFAULT_ACK_BATCH_SIZE
|
||||
@ -513,7 +514,7 @@ class NodeMessagingClient(override val config: NodeConfiguration,
|
||||
val queueQuery = session!!.queueQuery(SimpleString(queueName))
|
||||
if (!queueQuery.isExists) {
|
||||
log.info("Create fresh queue $queueName bound on same address")
|
||||
session!!.createQueue(queueName, queueName, true)
|
||||
session!!.createQueue(queueName, RoutingType.MULTICAST, queueName, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -42,8 +42,8 @@ import rx.Subscription
|
||||
import java.lang.reflect.InvocationTargetException
|
||||
import java.lang.reflect.Method
|
||||
import java.time.Duration
|
||||
import java.util.*
|
||||
import java.util.concurrent.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
data class RPCServerConfiguration(
|
||||
/** The number of threads to use for handling RPC requests */
|
||||
@ -91,14 +91,22 @@ class RPCServer(
|
||||
STARTED,
|
||||
FINISHED
|
||||
}
|
||||
|
||||
private sealed class BufferOrNone {
|
||||
data class Buffer(val container: MutableCollection<MessageAndContext>) : BufferOrNone()
|
||||
object None : BufferOrNone()
|
||||
}
|
||||
|
||||
private data class MessageAndContext(val message: RPCApi.ServerToClient.RpcReply, val context: ObservableContext)
|
||||
|
||||
private val lifeCycle = LifeCycle(State.UNSTARTED)
|
||||
// The methodname->Method map to use for dispatching.
|
||||
/** The methodname->Method map to use for dispatching. */
|
||||
private val methodTable: Map<String, Method>
|
||||
// The observable subscription mapping.
|
||||
/** The observable subscription mapping. */
|
||||
private val observableMap = createObservableSubscriptionMap()
|
||||
// A mapping from client addresses to IDs of associated Observables
|
||||
/** A mapping from client addresses to IDs of associated Observables */
|
||||
private val clientAddressToObservables = Multimaps.synchronizedSetMultimap(HashMultimap.create<SimpleString, RPCApi.ObservableId>())
|
||||
// The scheduled reaper handle.
|
||||
/** The scheduled reaper handle. */
|
||||
private var reaperScheduledFuture: ScheduledFuture<*>? = null
|
||||
|
||||
private var observationSendExecutor: ExecutorService? = null
|
||||
@ -113,13 +121,16 @@ class RPCServer(
|
||||
ArtemisProducer(sessionFactory, session, session.createProducer())
|
||||
}
|
||||
private var clientBindingRemovalConsumer: ClientConsumer? = null
|
||||
private var clientBindingAdditionConsumer: ClientConsumer? = null
|
||||
private var serverControl: ActiveMQServerControl? = null
|
||||
|
||||
private val responseMessageBuffer = ConcurrentHashMap<SimpleString, BufferOrNone>()
|
||||
|
||||
init {
|
||||
val groupedMethods = ops.javaClass.declaredMethods.groupBy { it.name }
|
||||
groupedMethods.forEach { name, methods ->
|
||||
if (methods.size > 1) {
|
||||
throw IllegalArgumentException("Encountered more than one method called ${name} on ${ops.javaClass.name}")
|
||||
throw IllegalArgumentException("Encountered more than one method called $name on ${ops.javaClass.name}")
|
||||
}
|
||||
}
|
||||
methodTable = groupedMethods.mapValues { it.value.single() }
|
||||
@ -155,17 +166,8 @@ class RPCServer(
|
||||
rpcConfiguration.reapInterval.toMillis(),
|
||||
TimeUnit.MILLISECONDS
|
||||
)
|
||||
val sessions = ArrayList<ClientSession>()
|
||||
for (i in 1 .. rpcConfiguration.consumerPoolSize) {
|
||||
val sessionFactory = serverLocator.createSessionFactory()
|
||||
val session = sessionFactory.createSession(rpcServerUsername, rpcServerPassword, false, true, true, false, DEFAULT_ACK_BATCH_SIZE)
|
||||
val consumer = session.createConsumer(RPCApi.RPC_SERVER_QUEUE_NAME)
|
||||
consumer.setMessageHandler(this@RPCServer::clientArtemisMessageHandler)
|
||||
sessionAndConsumers.add(ArtemisConsumer(sessionFactory, session, consumer))
|
||||
sessions.add(session)
|
||||
}
|
||||
clientBindingRemovalConsumer = sessionAndConsumers[0].session.createConsumer(RPCApi.RPC_CLIENT_BINDING_REMOVALS)
|
||||
clientBindingRemovalConsumer!!.setMessageHandler(this::bindingRemovalArtemisMessageHandler)
|
||||
val sessions = createConsumerSessions()
|
||||
createNotificationConsumers()
|
||||
serverControl = activeMqServerControl
|
||||
lifeCycle.transition(State.UNSTARTED, State.STARTED)
|
||||
// We delay the consumer session start because Artemis starts delivering messages immediately, so we need to be
|
||||
@ -179,6 +181,26 @@ class RPCServer(
|
||||
}
|
||||
}
|
||||
|
||||
private fun createConsumerSessions(): ArrayList<ClientSession> {
|
||||
val sessions = ArrayList<ClientSession>()
|
||||
for (i in 1..rpcConfiguration.consumerPoolSize) {
|
||||
val sessionFactory = serverLocator.createSessionFactory()
|
||||
val session = sessionFactory.createSession(rpcServerUsername, rpcServerPassword, false, true, true, false, DEFAULT_ACK_BATCH_SIZE)
|
||||
val consumer = session.createConsumer(RPCApi.RPC_SERVER_QUEUE_NAME)
|
||||
consumer.setMessageHandler(this@RPCServer::clientArtemisMessageHandler)
|
||||
sessionAndConsumers.add(ArtemisConsumer(sessionFactory, session, consumer))
|
||||
sessions.add(session)
|
||||
}
|
||||
return sessions
|
||||
}
|
||||
|
||||
private fun createNotificationConsumers() {
|
||||
clientBindingRemovalConsumer = sessionAndConsumers[0].session.createConsumer(RPCApi.RPC_CLIENT_BINDING_REMOVALS)
|
||||
clientBindingRemovalConsumer!!.setMessageHandler(this::bindingRemovalArtemisMessageHandler)
|
||||
clientBindingAdditionConsumer = sessionAndConsumers[0].session.createConsumer(RPCApi.RPC_CLIENT_BINDING_ADDITIONS)
|
||||
clientBindingAdditionConsumer!!.setMessageHandler(this::bindingAdditionArtemisMessageHandler)
|
||||
}
|
||||
|
||||
fun close() {
|
||||
reaperScheduledFuture?.cancel(false)
|
||||
rpcExecutor?.shutdownNow()
|
||||
@ -203,12 +225,38 @@ class RPCServer(
|
||||
invalidateClient(SimpleString(clientAddress))
|
||||
}
|
||||
|
||||
private fun bindingAdditionArtemisMessageHandler(artemisMessage: ClientMessage) {
|
||||
lifeCycle.requireState(State.STARTED)
|
||||
val notificationType = artemisMessage.getStringProperty(ManagementHelper.HDR_NOTIFICATION_TYPE)
|
||||
require(notificationType == CoreNotificationType.BINDING_ADDED.name)
|
||||
val clientAddress = SimpleString(artemisMessage.getStringProperty(ManagementHelper.HDR_ROUTING_NAME))
|
||||
log.debug("RPC client queue created on address $clientAddress")
|
||||
|
||||
val buffer = stopBuffering(clientAddress)
|
||||
buffer?.let { drainBuffer(it) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables message buffering for [clientAddress] and returns the existing buffer
|
||||
* or `null` if no requests were ever received.
|
||||
*/
|
||||
private fun stopBuffering(clientAddress: SimpleString): BufferOrNone.Buffer? {
|
||||
return responseMessageBuffer.put(clientAddress, BufferOrNone.None) as? BufferOrNone.Buffer
|
||||
}
|
||||
|
||||
private fun drainBuffer(buffer: BufferOrNone.Buffer) {
|
||||
buffer.container.forEach {
|
||||
it.context.sendMessage(it.message)
|
||||
}
|
||||
}
|
||||
|
||||
// Note that this function operates on the *current* view of client observables. During invalidation further
|
||||
// Observables may be serialised and thus registered.
|
||||
private fun invalidateClient(clientAddress: SimpleString) {
|
||||
lifeCycle.requireState(State.STARTED)
|
||||
val observableIds = clientAddressToObservables.removeAll(clientAddress)
|
||||
observableMap.invalidateAll(observableIds)
|
||||
responseMessageBuffer.remove(clientAddress)
|
||||
}
|
||||
|
||||
private fun clientArtemisMessageHandler(artemisMessage: ClientMessage) {
|
||||
@ -221,17 +269,7 @@ class RPCServer(
|
||||
currentUser = getUser(artemisMessage)
|
||||
)
|
||||
rpcExecutor!!.submit {
|
||||
val result = ErrorOr.catch {
|
||||
try {
|
||||
CURRENT_RPC_CONTEXT.set(rpcContext)
|
||||
log.debug { "Calling ${clientToServer.methodName}" }
|
||||
val method = methodTable[clientToServer.methodName] ?:
|
||||
throw RPCException("Received RPC for unknown method ${clientToServer.methodName} - possible client/server version skew?")
|
||||
method.invoke(ops, *clientToServer.arguments.toTypedArray())
|
||||
} finally {
|
||||
CURRENT_RPC_CONTEXT.remove()
|
||||
}
|
||||
}
|
||||
val result = invokeRpc(rpcContext, clientToServer.methodName, clientToServer.arguments)
|
||||
val resultWithExceptionUnwrapped = result.mapError {
|
||||
if (it is InvocationTargetException) {
|
||||
it.cause ?: RPCException("Caught InvocationTargetException without cause")
|
||||
@ -239,21 +277,7 @@ class RPCServer(
|
||||
it
|
||||
}
|
||||
}
|
||||
val reply = RPCApi.ServerToClient.RpcReply(
|
||||
id = clientToServer.id,
|
||||
result = resultWithExceptionUnwrapped
|
||||
)
|
||||
val observableContext = ObservableContext(
|
||||
clientToServer.id,
|
||||
observableMap,
|
||||
clientAddressToObservables,
|
||||
clientToServer.clientAddress,
|
||||
serverControl!!,
|
||||
sessionAndProducerPool,
|
||||
observationSendExecutor!!,
|
||||
kryoPool
|
||||
)
|
||||
observableContext.sendMessage(reply)
|
||||
sendReply(clientToServer.id, clientToServer.clientAddress, resultWithExceptionUnwrapped)
|
||||
}
|
||||
}
|
||||
is RPCApi.ClientToServer.ObservablesClosed -> {
|
||||
@ -263,6 +287,62 @@ class RPCServer(
|
||||
artemisMessage.acknowledge()
|
||||
}
|
||||
|
||||
private fun invokeRpc(rpcContext: RpcContext, methodName: String, arguments: List<Any?>): ErrorOr<Any> {
|
||||
return ErrorOr.catch {
|
||||
try {
|
||||
CURRENT_RPC_CONTEXT.set(rpcContext)
|
||||
log.debug { "Calling $methodName" }
|
||||
val method = methodTable[methodName] ?:
|
||||
throw RPCException("Received RPC for unknown method $methodName - possible client/server version skew?")
|
||||
method.invoke(ops, *arguments.toTypedArray())
|
||||
} finally {
|
||||
CURRENT_RPC_CONTEXT.remove()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendReply(requestId: RPCApi.RpcRequestId, clientAddress: SimpleString, resultWithExceptionUnwrapped: ErrorOr<Any>) {
|
||||
val reply = RPCApi.ServerToClient.RpcReply(
|
||||
id = requestId,
|
||||
result = resultWithExceptionUnwrapped
|
||||
)
|
||||
val observableContext = ObservableContext(
|
||||
requestId,
|
||||
observableMap,
|
||||
clientAddressToObservables,
|
||||
clientAddress,
|
||||
serverControl!!,
|
||||
sessionAndProducerPool,
|
||||
observationSendExecutor!!,
|
||||
kryoPool
|
||||
)
|
||||
|
||||
val buffered = bufferIfQueueNotBound(clientAddress, reply, observableContext)
|
||||
if (!buffered) observableContext.sendMessage(reply)
|
||||
}
|
||||
|
||||
/**
|
||||
* Buffer the message if the queue at [clientAddress] is not yet bound.
|
||||
*
|
||||
* This can happen after server restart when the client consumer session initiates failover,
|
||||
* but the client queue is not yet set up. We buffer the messages and flush the buffer only once
|
||||
* we receive a notification that the client queue bindings were added.
|
||||
*/
|
||||
private fun bufferIfQueueNotBound(clientAddress: SimpleString, message: RPCApi.ServerToClient.RpcReply, context: ObservableContext): Boolean {
|
||||
val clientBuffer = responseMessageBuffer.compute(clientAddress, { _, value ->
|
||||
when (value) {
|
||||
null -> BufferOrNone.Buffer(ArrayList<MessageAndContext>()).apply {
|
||||
container.add(MessageAndContext(message, context))
|
||||
}
|
||||
is BufferOrNone.Buffer -> value.apply {
|
||||
container.add(MessageAndContext(message, context))
|
||||
}
|
||||
is BufferOrNone.None -> value
|
||||
}
|
||||
})
|
||||
return clientBuffer is BufferOrNone.Buffer
|
||||
}
|
||||
|
||||
private fun reapSubscriptions() {
|
||||
observableMap.cleanUp()
|
||||
}
|
||||
|
@ -21,8 +21,8 @@ import net.corda.core.serialization.SerializeAsTokenContext
|
||||
import net.corda.core.utilities.loggerFor
|
||||
import net.corda.node.services.api.AcceptsFileUpload
|
||||
import net.corda.node.services.database.RequeryConfiguration
|
||||
import net.corda.node.services.persistence.schemas.AttachmentEntity
|
||||
import net.corda.node.services.persistence.schemas.Models
|
||||
import net.corda.node.services.persistence.schemas.requery.AttachmentEntity
|
||||
import net.corda.node.services.persistence.schemas.requery.Models
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.io.FilterInputStream
|
||||
import java.io.IOException
|
||||
|
@ -6,144 +6,48 @@ import net.corda.core.contracts.StateRef
|
||||
import net.corda.core.node.services.Vault
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.core.schemas.PersistentStateRef
|
||||
import net.corda.core.schemas.QueryableState
|
||||
import net.corda.core.utilities.debug
|
||||
import net.corda.core.utilities.loggerFor
|
||||
import net.corda.node.services.api.SchemaService
|
||||
import net.corda.node.services.database.HibernateConfiguration
|
||||
import org.hibernate.FlushMode
|
||||
import org.hibernate.SessionFactory
|
||||
import org.hibernate.boot.MetadataSources
|
||||
import org.hibernate.boot.model.naming.Identifier
|
||||
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
|
||||
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder
|
||||
import org.hibernate.cfg.Configuration
|
||||
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider
|
||||
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment
|
||||
import org.hibernate.service.UnknownUnwrapTypeException
|
||||
import org.jetbrains.exposed.sql.transactions.TransactionManager
|
||||
import rx.Observable
|
||||
import java.sql.Connection
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
/**
|
||||
* A vault observer that extracts Object Relational Mappings for contract states that support it, and persists them with Hibernate.
|
||||
*/
|
||||
// TODO: Manage version evolution of the schemas via additional tooling.
|
||||
class HibernateObserver(vaultUpdates: Observable<Vault.Update>, val schemaService: SchemaService) {
|
||||
class HibernateObserver(vaultUpdates: Observable<Vault.Update>, val config: HibernateConfiguration) {
|
||||
|
||||
companion object {
|
||||
val logger = loggerFor<HibernateObserver>()
|
||||
}
|
||||
|
||||
// TODO: make this a guava cache or similar to limit ability for this to grow forever.
|
||||
val sessionFactories = ConcurrentHashMap<MappedSchema, SessionFactory>()
|
||||
|
||||
init {
|
||||
schemaService.schemaOptions.map { it.key }.forEach {
|
||||
makeSessionFactoryForSchema(it)
|
||||
}
|
||||
vaultUpdates.subscribe { persist(it.produced) }
|
||||
}
|
||||
|
||||
private fun sessionFactoryForSchema(schema: MappedSchema): SessionFactory {
|
||||
return sessionFactories.computeIfAbsent(schema, { makeSessionFactoryForSchema(it) })
|
||||
}
|
||||
|
||||
private fun makeSessionFactoryForSchema(schema: MappedSchema): SessionFactory {
|
||||
logger.info("Creating session factory for schema $schema")
|
||||
val serviceRegistry = BootstrapServiceRegistryBuilder().build()
|
||||
val metadataSources = MetadataSources(serviceRegistry)
|
||||
// We set a connection provider as the auto schema generation requires it. The auto schema generation will not
|
||||
// necessarily remain and would likely be replaced by something like Liquibase. For now it is very convenient though.
|
||||
// TODO: replace auto schema generation as it isn't intended for production use, according to Hibernate docs.
|
||||
val config = Configuration(metadataSources).setProperty("hibernate.connection.provider_class", NodeDatabaseConnectionProvider::class.java.name)
|
||||
.setProperty("hibernate.hbm2ddl.auto", "update")
|
||||
.setProperty("hibernate.show_sql", "false")
|
||||
.setProperty("hibernate.format_sql", "true")
|
||||
val options = schemaService.schemaOptions[schema]
|
||||
val databaseSchema = options?.databaseSchema
|
||||
if (databaseSchema != null) {
|
||||
logger.debug { "Database schema = $databaseSchema" }
|
||||
config.setProperty("hibernate.default_schema", databaseSchema)
|
||||
}
|
||||
val tablePrefix = options?.tablePrefix ?: "contract_" // We always have this as the default for aesthetic reasons.
|
||||
logger.debug { "Table prefix = $tablePrefix" }
|
||||
schema.mappedTypes.forEach { config.addAnnotatedClass(it) }
|
||||
val sessionFactory = buildSessionFactory(config, metadataSources, tablePrefix)
|
||||
logger.info("Created session factory for schema $schema")
|
||||
return sessionFactory
|
||||
}
|
||||
|
||||
private fun persist(produced: Set<StateAndRef<ContractState>>) {
|
||||
produced.forEach { persistState(it) }
|
||||
}
|
||||
|
||||
private fun persistState(stateAndRef: StateAndRef<ContractState>) {
|
||||
val state = stateAndRef.state.data
|
||||
if (state is QueryableState) {
|
||||
logger.debug { "Asked to persist state ${stateAndRef.ref}" }
|
||||
schemaService.selectSchemas(state).forEach { persistStateWithSchema(state, stateAndRef.ref, it) }
|
||||
}
|
||||
logger.debug { "Asked to persist state ${stateAndRef.ref}" }
|
||||
config.schemaService.selectSchemas(state).forEach { persistStateWithSchema(state, stateAndRef.ref, it) }
|
||||
}
|
||||
|
||||
private fun persistStateWithSchema(state: QueryableState, stateRef: StateRef, schema: MappedSchema) {
|
||||
val sessionFactory = sessionFactoryForSchema(schema)
|
||||
fun persistStateWithSchema(state: ContractState, stateRef: StateRef, schema: MappedSchema) {
|
||||
val sessionFactory = config.sessionFactoryForSchema(schema)
|
||||
val session = sessionFactory.withOptions().
|
||||
connection(TransactionManager.current().connection).
|
||||
flushMode(FlushMode.MANUAL).
|
||||
openSession()
|
||||
session.use {
|
||||
val mappedObject = schemaService.generateMappedObject(state, schema)
|
||||
val mappedObject = config.schemaService.generateMappedObject(state, schema)
|
||||
mappedObject.stateRef = PersistentStateRef(stateRef)
|
||||
it.persist(mappedObject)
|
||||
it.flush()
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildSessionFactory(config: Configuration, metadataSources: MetadataSources, tablePrefix: String): SessionFactory {
|
||||
config.standardServiceRegistryBuilder.applySettings(config.properties)
|
||||
val metadata = metadataSources.getMetadataBuilder(config.standardServiceRegistryBuilder.build()).run {
|
||||
applyPhysicalNamingStrategy(object : PhysicalNamingStrategyStandardImpl() {
|
||||
override fun toPhysicalTableName(name: Identifier?, context: JdbcEnvironment?): Identifier {
|
||||
val default = super.toPhysicalTableName(name, context)
|
||||
return Identifier.toIdentifier(tablePrefix + default.text, default.isQuoted)
|
||||
}
|
||||
})
|
||||
build()
|
||||
}
|
||||
|
||||
return metadata.sessionFactoryBuilder.run {
|
||||
allowOutOfTransactionUpdateOperations(true)
|
||||
applySecondLevelCacheSupport(false)
|
||||
applyQueryCacheSupport(false)
|
||||
enableReleaseResourcesOnCloseEnabled(true)
|
||||
build()
|
||||
}
|
||||
}
|
||||
|
||||
// Supply Hibernate with connections from our underlying Exposed database integration. Only used
|
||||
// during schema creation / update.
|
||||
class NodeDatabaseConnectionProvider : ConnectionProvider {
|
||||
override fun closeConnection(conn: Connection) {
|
||||
val tx = TransactionManager.current()
|
||||
tx.commit()
|
||||
tx.close()
|
||||
}
|
||||
|
||||
override fun supportsAggressiveRelease(): Boolean = true
|
||||
|
||||
override fun getConnection(): Connection {
|
||||
val tx = TransactionManager.manager.newTransaction(Connection.TRANSACTION_REPEATABLE_READ)
|
||||
return tx.connection
|
||||
}
|
||||
|
||||
override fun <T : Any?> unwrap(unwrapType: Class<T>): T {
|
||||
try {
|
||||
return unwrapType.cast(this)
|
||||
} catch(e: ClassCastException) {
|
||||
throw UnknownUnwrapTypeException(unwrapType)
|
||||
}
|
||||
}
|
||||
|
||||
override fun isUnwrappableAs(unwrapType: Class<*>?): Boolean = (unwrapType == NodeDatabaseConnectionProvider::class.java)
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,16 @@
|
||||
package net.corda.node.services.schema
|
||||
|
||||
import net.corda.contracts.DealState
|
||||
import net.corda.core.contracts.ContractState
|
||||
import net.corda.core.contracts.FungibleAsset
|
||||
import net.corda.core.contracts.LinearState
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.core.schemas.PersistentState
|
||||
import net.corda.core.schemas.QueryableState
|
||||
import net.corda.core.serialization.SingletonSerializeAsToken
|
||||
import net.corda.node.services.api.SchemaService
|
||||
import net.corda.node.services.vault.schemas.jpa.CommonSchemaV1
|
||||
import net.corda.node.services.vault.schemas.jpa.VaultSchemaV1
|
||||
import net.corda.schemas.CashSchemaV1
|
||||
|
||||
/**
|
||||
@ -15,21 +21,46 @@ import net.corda.schemas.CashSchemaV1
|
||||
* TODO: support plugins for schema version upgrading or custom mapping not supported by original [QueryableState].
|
||||
* TODO: create whitelisted tables when a CorDapp is first installed
|
||||
*/
|
||||
class NodeSchemaService : SchemaService, SingletonSerializeAsToken() {
|
||||
class NodeSchemaService(customSchemas: Set<MappedSchema> = emptySet()) : SchemaService, SingletonSerializeAsToken() {
|
||||
|
||||
// Currently does not support configuring schema options.
|
||||
|
||||
// Whitelisted tables are those required by internal Corda services
|
||||
// For example, cash is used by the vault for coin selection
|
||||
// This whitelist will grow as we add further functionality (eg. other fungible assets)
|
||||
override val schemaOptions: Map<MappedSchema, SchemaService.SchemaOptions> = mapOf(Pair(CashSchemaV1, SchemaService.SchemaOptions()))
|
||||
// Required schemas are those used by internal Corda services
|
||||
// For example, cash is used by the vault for coin selection (but will be extracted as a standalone CorDapp in future)
|
||||
val requiredSchemas: Map<MappedSchema, SchemaService.SchemaOptions> =
|
||||
mapOf(Pair(CashSchemaV1, SchemaService.SchemaOptions()),
|
||||
Pair(CommonSchemaV1, SchemaService.SchemaOptions()),
|
||||
Pair(VaultSchemaV1, SchemaService.SchemaOptions()))
|
||||
|
||||
override val schemaOptions: Map<MappedSchema, SchemaService.SchemaOptions> = requiredSchemas.plus(customSchemas.map {
|
||||
mappedSchema -> Pair(mappedSchema, SchemaService.SchemaOptions())
|
||||
})
|
||||
|
||||
// Currently returns all schemas supported by the state, with no filtering or enrichment.
|
||||
override fun selectSchemas(state: QueryableState): Iterable<MappedSchema> {
|
||||
return state.supportedSchemas()
|
||||
override fun selectSchemas(state: ContractState): Iterable<MappedSchema> {
|
||||
val schemas = mutableSetOf<MappedSchema>()
|
||||
if (state is QueryableState)
|
||||
schemas += state.supportedSchemas()
|
||||
if (state is LinearState)
|
||||
schemas += VaultSchemaV1 // VaultLinearStates
|
||||
// TODO: DealState to be deprecated (collapsed into LinearState)
|
||||
if (state is DealState)
|
||||
schemas += VaultSchemaV1 // VaultLinearStates
|
||||
if (state is FungibleAsset<*>)
|
||||
schemas += VaultSchemaV1 // VaultFungibleStates
|
||||
|
||||
return schemas
|
||||
}
|
||||
|
||||
// Because schema is always one supported by the state, just delegate.
|
||||
override fun generateMappedObject(state: QueryableState, schema: MappedSchema): PersistentState {
|
||||
return state.generateMappedObject(schema)
|
||||
override fun generateMappedObject(state: ContractState, schema: MappedSchema): PersistentState {
|
||||
// TODO: DealState to be deprecated (collapsed into LinearState)
|
||||
if ((schema is VaultSchemaV1) && (state is DealState))
|
||||
return VaultSchemaV1.VaultLinearStates(state.linearId, state.ref, state.participants)
|
||||
if ((schema is VaultSchemaV1) && (state is LinearState))
|
||||
return VaultSchemaV1.VaultLinearStates(state.linearId, "", state.participants)
|
||||
if ((schema is VaultSchemaV1) && (state is FungibleAsset<*>))
|
||||
return VaultSchemaV1.VaultFungibleStates(state.owner, state.amount.quantity, state.amount.token.issuer.party, state.amount.token.issuer.reference, state.participants)
|
||||
return (state as QueryableState).generateMappedObject(schema)
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,368 @@
|
||||
package net.corda.node.services.vault
|
||||
|
||||
import net.corda.core.contracts.ContractState
|
||||
import net.corda.core.contracts.StateRef
|
||||
import net.corda.core.contracts.UniqueIdentifier
|
||||
import net.corda.core.identity.AbstractParty
|
||||
import net.corda.core.node.services.Vault
|
||||
import net.corda.core.node.services.VaultQueryException
|
||||
import net.corda.core.node.services.vault.*
|
||||
import net.corda.core.schemas.PersistentState
|
||||
import net.corda.core.schemas.PersistentStateRef
|
||||
import net.corda.core.serialization.OpaqueBytes
|
||||
import net.corda.core.serialization.toHexString
|
||||
import net.corda.core.utilities.loggerFor
|
||||
import net.corda.core.utilities.trace
|
||||
import net.corda.node.services.vault.schemas.jpa.CommonSchemaV1
|
||||
import net.corda.node.services.vault.schemas.jpa.VaultSchemaV1
|
||||
import org.bouncycastle.asn1.x500.X500Name
|
||||
import java.util.*
|
||||
import javax.persistence.Tuple
|
||||
import javax.persistence.criteria.*
|
||||
|
||||
|
||||
class HibernateQueryCriteriaParser(val contractType: Class<out ContractState>,
|
||||
val contractTypeMappings: Map<String, List<String>>,
|
||||
val criteriaBuilder: CriteriaBuilder,
|
||||
val criteriaQuery: CriteriaQuery<Tuple>,
|
||||
val vaultStates: Root<VaultSchemaV1.VaultStates>) : IQueryCriteriaParser {
|
||||
private companion object {
|
||||
val log = loggerFor<HibernateQueryCriteriaParser>()
|
||||
}
|
||||
|
||||
// incrementally build list of join predicates
|
||||
private val joinPredicates = mutableListOf<Predicate>()
|
||||
// incrementally build list of root entities (for later use in Sort parsing)
|
||||
private val rootEntities = mutableMapOf<Class<out PersistentState>, Root<*>>()
|
||||
|
||||
var stateTypes: Vault.StateStatus = Vault.StateStatus.UNCONSUMED
|
||||
|
||||
override fun parseCriteria(criteria: QueryCriteria.VaultQueryCriteria) : Collection<Predicate> {
|
||||
log.trace { "Parsing VaultQueryCriteria: $criteria" }
|
||||
val predicateSet = mutableSetOf<Predicate>()
|
||||
|
||||
// state status
|
||||
stateTypes = criteria.status
|
||||
if (criteria.status == Vault.StateStatus.ALL)
|
||||
predicateSet.add(vaultStates.get<Vault.StateStatus>("stateStatus").`in`(setOf(Vault.StateStatus.UNCONSUMED, Vault.StateStatus.CONSUMED)))
|
||||
else
|
||||
predicateSet.add(criteriaBuilder.equal(vaultStates.get<Vault.StateStatus>("stateStatus"), criteria.status))
|
||||
|
||||
// contract State Types
|
||||
val combinedContractTypeTypes = criteria.contractStateTypes?.plus(contractType) ?: setOf(contractType)
|
||||
combinedContractTypeTypes.filter { it.name != ContractState::class.java.name }.let {
|
||||
val interfaces = it.flatMap { contractTypeMappings[it.name] ?: emptyList() }
|
||||
val concrete = it.filter { !it.isInterface }.map { it.name }
|
||||
val all = interfaces.plus(concrete)
|
||||
if (all.isNotEmpty())
|
||||
predicateSet.add(criteriaBuilder.and(vaultStates.get<String>("contractStateClassName").`in`(all)))
|
||||
}
|
||||
|
||||
// soft locking
|
||||
if (!criteria.includeSoftlockedStates)
|
||||
predicateSet.add(criteriaBuilder.and(vaultStates.get<String>("lockId").isNull))
|
||||
|
||||
// notary names
|
||||
criteria.notaryName?.let {
|
||||
val notaryNames = (criteria.notaryName as List<X500Name>).map { it.toString() }
|
||||
predicateSet.add(criteriaBuilder.and(vaultStates.get<String>("notaryName").`in`(notaryNames)))
|
||||
}
|
||||
|
||||
// state references
|
||||
criteria.stateRefs?.let {
|
||||
val persistentStateRefs = (criteria.stateRefs as List<StateRef>).map { PersistentStateRef(it.txhash.bytes.toHexString(), it.index) }
|
||||
val compositeKey = vaultStates.get<PersistentStateRef>("stateRef")
|
||||
predicateSet.add(criteriaBuilder.and(compositeKey.`in`(persistentStateRefs)))
|
||||
}
|
||||
|
||||
// time constraints (recorded, consumed)
|
||||
criteria.timeCondition?.let {
|
||||
val timeCondition = criteria.timeCondition
|
||||
val timeInstantType = timeCondition!!.type
|
||||
val timeColumn = when (timeInstantType) {
|
||||
QueryCriteria.TimeInstantType.RECORDED -> Column.Kotlin(VaultSchemaV1.VaultStates::recordedTime)
|
||||
QueryCriteria.TimeInstantType.CONSUMED -> Column.Kotlin(VaultSchemaV1.VaultStates::consumedTime)
|
||||
}
|
||||
val expression = CriteriaExpression.ColumnPredicateExpression(timeColumn, timeCondition.predicate)
|
||||
predicateSet.add(expressionToPredicate(vaultStates, expression))
|
||||
}
|
||||
return predicateSet
|
||||
}
|
||||
|
||||
private fun columnPredicateToPredicate(column: Path<out Any?>, columnPredicate: ColumnPredicate<*>): Predicate {
|
||||
return when (columnPredicate) {
|
||||
is ColumnPredicate.EqualityComparison -> {
|
||||
val literal = columnPredicate.rightLiteral
|
||||
when (columnPredicate.operator) {
|
||||
EqualityComparisonOperator.EQUAL -> criteriaBuilder.equal(column, literal)
|
||||
EqualityComparisonOperator.NOT_EQUAL -> criteriaBuilder.notEqual(column, literal)
|
||||
}
|
||||
}
|
||||
is ColumnPredicate.BinaryComparison -> {
|
||||
column as Path<Comparable<Any?>?>
|
||||
val literal = columnPredicate.rightLiteral as Comparable<Any?>?
|
||||
when (columnPredicate.operator) {
|
||||
BinaryComparisonOperator.GREATER_THAN -> criteriaBuilder.greaterThan(column, literal)
|
||||
BinaryComparisonOperator.GREATER_THAN_OR_EQUAL -> criteriaBuilder.greaterThanOrEqualTo(column, literal)
|
||||
BinaryComparisonOperator.LESS_THAN -> criteriaBuilder.lessThan(column, literal)
|
||||
BinaryComparisonOperator.LESS_THAN_OR_EQUAL -> criteriaBuilder.lessThanOrEqualTo(column, literal)
|
||||
}
|
||||
}
|
||||
is ColumnPredicate.Likeness -> {
|
||||
column as Path<String?>
|
||||
when (columnPredicate.operator) {
|
||||
LikenessOperator.LIKE -> criteriaBuilder.like(column, columnPredicate.rightLiteral)
|
||||
LikenessOperator.NOT_LIKE -> criteriaBuilder.notLike(column, columnPredicate.rightLiteral)
|
||||
}
|
||||
}
|
||||
is ColumnPredicate.CollectionExpression -> {
|
||||
when (columnPredicate.operator) {
|
||||
CollectionOperator.IN -> column.`in`(columnPredicate.rightLiteral)
|
||||
CollectionOperator.NOT_IN -> criteriaBuilder.not(column.`in`(columnPredicate.rightLiteral))
|
||||
}
|
||||
}
|
||||
is ColumnPredicate.Between -> {
|
||||
column as Path<Comparable<Any?>?>
|
||||
val fromLiteral = columnPredicate.rightFromLiteral as Comparable<Any?>?
|
||||
val toLiteral = columnPredicate.rightToLiteral as Comparable<Any?>?
|
||||
criteriaBuilder.between(column, fromLiteral, toLiteral)
|
||||
}
|
||||
is ColumnPredicate.NullExpression -> {
|
||||
when (columnPredicate.operator) {
|
||||
NullOperator.IS_NULL -> criteriaBuilder.isNull(column)
|
||||
NullOperator.NOT_NULL -> criteriaBuilder.isNotNull(column)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return : Expression<Boolean> -> : Predicate
|
||||
*/
|
||||
private fun <O, R> expressionToExpression(root: Root<O>, expression: CriteriaExpression<O, R>): Expression<R> {
|
||||
return when (expression) {
|
||||
is CriteriaExpression.BinaryLogical -> {
|
||||
val leftPredicate = expressionToExpression(root, expression.left)
|
||||
val rightPredicate = expressionToExpression(root, expression.right)
|
||||
when (expression.operator) {
|
||||
BinaryLogicalOperator.AND -> criteriaBuilder.and(leftPredicate, rightPredicate) as Expression<R>
|
||||
BinaryLogicalOperator.OR -> criteriaBuilder.or(leftPredicate, rightPredicate) as Expression<R>
|
||||
}
|
||||
}
|
||||
is CriteriaExpression.Not -> criteriaBuilder.not(expressionToExpression(root, expression.expression)) as Expression<R>
|
||||
is CriteriaExpression.ColumnPredicateExpression<O, *> -> {
|
||||
val column = root.get<Any?>(getColumnName(expression.column))
|
||||
columnPredicateToPredicate(column, expression.predicate) as Expression<R>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun <O> expressionToPredicate(root: Root<O>, expression: CriteriaExpression<O, Boolean>): Predicate {
|
||||
return expressionToExpression(root, expression) as Predicate
|
||||
}
|
||||
|
||||
override fun parseCriteria(criteria: QueryCriteria.FungibleAssetQueryCriteria) : Collection<Predicate> {
|
||||
log.trace { "Parsing FungibleAssetQueryCriteria: $criteria" }
|
||||
|
||||
var predicateSet = mutableSetOf<Predicate>()
|
||||
|
||||
val vaultFungibleStates = criteriaQuery.from(VaultSchemaV1.VaultFungibleStates::class.java)
|
||||
rootEntities.putIfAbsent(VaultSchemaV1.VaultFungibleStates::class.java, vaultFungibleStates)
|
||||
|
||||
val joinPredicate = criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), vaultFungibleStates.get<PersistentStateRef>("stateRef"))
|
||||
predicateSet.add(joinPredicate)
|
||||
|
||||
// owner
|
||||
criteria.owner?.let {
|
||||
val ownerKeys = criteria.owner as List<AbstractParty>
|
||||
val joinFungibleStateToParty = vaultFungibleStates.join<VaultSchemaV1.VaultFungibleStates, CommonSchemaV1.Party>("issuerParty")
|
||||
val owners = ownerKeys.map { it.nameOrNull()?.toString() ?: it.toString()}
|
||||
predicateSet.add(criteriaBuilder.and(joinFungibleStateToParty.get<CommonSchemaV1.Party>("name").`in`(owners)))
|
||||
}
|
||||
|
||||
// quantity
|
||||
criteria.quantity?.let {
|
||||
predicateSet.add(columnPredicateToPredicate(vaultFungibleStates.get<Long>("quantity"), it))
|
||||
}
|
||||
|
||||
// issuer party
|
||||
criteria.issuerPartyName?.let {
|
||||
val issuerParties = criteria.issuerPartyName as List<AbstractParty>
|
||||
val joinFungibleStateToParty = vaultFungibleStates.join<VaultSchemaV1.VaultFungibleStates, CommonSchemaV1.Party>("issuerParty")
|
||||
val dealPartyKeys = issuerParties.map { it.nameOrNull().toString() }
|
||||
predicateSet.add(criteriaBuilder.equal(joinFungibleStateToParty.get<CommonSchemaV1.Party>("name"), dealPartyKeys))
|
||||
}
|
||||
|
||||
// issuer reference
|
||||
criteria.issuerRef?.let {
|
||||
val issuerRefs = (criteria.issuerRef as List<OpaqueBytes>).map { it.bytes }
|
||||
predicateSet.add(criteriaBuilder.and(vaultFungibleStates.get<ByteArray>("issuerRef").`in`(issuerRefs)))
|
||||
}
|
||||
|
||||
// participants
|
||||
criteria.participants?.let {
|
||||
val participants = criteria.participants as List<AbstractParty>
|
||||
val joinFungibleStateToParty = vaultFungibleStates.join<VaultSchemaV1.VaultFungibleStates, CommonSchemaV1.Party>("participants")
|
||||
val participantKeys = participants.map { it.nameOrNull().toString() }
|
||||
predicateSet.add(criteriaBuilder.and(joinFungibleStateToParty.get<CommonSchemaV1.Party>("name").`in`(participantKeys)))
|
||||
criteriaQuery.distinct(true)
|
||||
}
|
||||
return predicateSet
|
||||
}
|
||||
|
||||
override fun parseCriteria(criteria: QueryCriteria.LinearStateQueryCriteria) : Collection<Predicate> {
|
||||
log.trace { "Parsing LinearStateQueryCriteria: $criteria" }
|
||||
|
||||
val predicateSet = mutableSetOf<Predicate>()
|
||||
|
||||
val vaultLinearStates = criteriaQuery.from(VaultSchemaV1.VaultLinearStates::class.java)
|
||||
rootEntities.putIfAbsent(VaultSchemaV1.VaultLinearStates::class.java, vaultLinearStates)
|
||||
val joinPredicate = criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), vaultLinearStates.get<PersistentStateRef>("stateRef"))
|
||||
joinPredicates.add(joinPredicate)
|
||||
|
||||
// linear ids
|
||||
criteria.linearId?.let {
|
||||
val uniqueIdentifiers = criteria.linearId as List<UniqueIdentifier>
|
||||
val externalIds = uniqueIdentifiers.mapNotNull { it.externalId }
|
||||
if (externalIds.isNotEmpty())
|
||||
predicateSet.add(criteriaBuilder.and(vaultLinearStates.get<String>("externalId").`in`(externalIds)))
|
||||
predicateSet.add(criteriaBuilder.and(vaultLinearStates.get<UUID>("uuid").`in`(uniqueIdentifiers.map { it.id })))
|
||||
}
|
||||
|
||||
// deal refs
|
||||
criteria.dealRef?.let {
|
||||
val dealRefs = criteria.dealRef as List<String>
|
||||
predicateSet.add(criteriaBuilder.and(vaultLinearStates.get<String>("dealReference").`in`(dealRefs)))
|
||||
}
|
||||
|
||||
// deal participants
|
||||
criteria.participants?.let {
|
||||
val participants = criteria.participants as List<AbstractParty>
|
||||
val joinLinearStateToParty = vaultLinearStates.join<VaultSchemaV1.VaultLinearStates, CommonSchemaV1.Party>("participants")
|
||||
val participantKeys = participants.map { it.nameOrNull().toString() }
|
||||
predicateSet.add(criteriaBuilder.and(joinLinearStateToParty.get<CommonSchemaV1.Party>("name").`in`(participantKeys)))
|
||||
criteriaQuery.distinct(true)
|
||||
}
|
||||
return predicateSet
|
||||
}
|
||||
|
||||
override fun <L : PersistentState> parseCriteria(criteria: QueryCriteria.VaultCustomQueryCriteria<L>): Collection<Predicate> {
|
||||
log.trace { "Parsing VaultCustomQueryCriteria: $criteria" }
|
||||
|
||||
val predicateSet = mutableSetOf<Predicate>()
|
||||
val entityClass = resolveEnclosingObjectFromExpression(criteria.expression)
|
||||
|
||||
try {
|
||||
val entityRoot = criteriaQuery.from(entityClass)
|
||||
rootEntities.putIfAbsent(entityClass, entityRoot)
|
||||
val joinPredicate = criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), entityRoot.get<PersistentStateRef>("stateRef"))
|
||||
joinPredicates.add(joinPredicate)
|
||||
|
||||
predicateSet.add(expressionToPredicate(entityRoot, criteria.expression))
|
||||
}
|
||||
catch (e: Exception) {
|
||||
e.message?.let { message ->
|
||||
if (message.contains("Not an entity"))
|
||||
throw VaultQueryException("""
|
||||
Please register the entity '${entityClass.name.substringBefore('$')}' class in your CorDapp's CordaPluginRegistry configuration (requiredSchemas attribute)
|
||||
and ensure you have declared (in supportedSchemas()) and mapped (in generateMappedObject()) the schema in the associated contract state's QueryableState interface implementation.
|
||||
See https://docs.corda.net/persistence.html?highlight=persistence for more information""")
|
||||
}
|
||||
throw VaultQueryException("Parsing error: ${e.message}")
|
||||
}
|
||||
return predicateSet
|
||||
}
|
||||
|
||||
override fun parseOr(left: QueryCriteria, right: QueryCriteria): Collection<Predicate> {
|
||||
log.trace { "Parsing OR QueryCriteria composition: $left OR $right" }
|
||||
|
||||
var predicateSet = mutableSetOf<Predicate>()
|
||||
val leftPredicates = parse(left)
|
||||
val rightPredicates = parse(right)
|
||||
|
||||
val orPredicate = criteriaBuilder.or(*leftPredicates.toTypedArray(), *rightPredicates.toTypedArray())
|
||||
predicateSet.add(orPredicate)
|
||||
|
||||
return predicateSet
|
||||
}
|
||||
|
||||
override fun parseAnd(left: QueryCriteria, right: QueryCriteria): Collection<Predicate> {
|
||||
log.trace { "Parsing AND QueryCriteria composition: $left AND $right" }
|
||||
|
||||
var predicateSet = mutableSetOf<Predicate>()
|
||||
val leftPredicates = parse(left)
|
||||
val rightPredicates = parse(right)
|
||||
|
||||
val andPredicate = criteriaBuilder.and(criteriaBuilder.and(*leftPredicates.toTypedArray(), *rightPredicates.toTypedArray()))
|
||||
predicateSet.add(andPredicate)
|
||||
|
||||
return predicateSet
|
||||
}
|
||||
|
||||
override fun parse(criteria: QueryCriteria, sorting: Sort?): Collection<Predicate> {
|
||||
val predicateSet = criteria.visit(this)
|
||||
|
||||
sorting?.let {
|
||||
if (sorting.columns.isNotEmpty())
|
||||
parse(sorting)
|
||||
}
|
||||
|
||||
val selections = listOf(vaultStates).plus(rootEntities.map { it.value })
|
||||
criteriaQuery.multiselect(selections)
|
||||
val combinedPredicates = joinPredicates.plus(predicateSet)
|
||||
criteriaQuery.where(*combinedPredicates.toTypedArray())
|
||||
|
||||
return predicateSet
|
||||
}
|
||||
|
||||
private fun parse(sorting: Sort) {
|
||||
log.trace { "Parsing sorting specification: $sorting" }
|
||||
|
||||
var orderCriteria = mutableListOf<Order>()
|
||||
|
||||
sorting.columns.map { (sortAttribute, direction) ->
|
||||
val (entityStateClass, entityStateColumnName) =
|
||||
when(sortAttribute) {
|
||||
is SortAttribute.Standard -> parse(sortAttribute.attribute)
|
||||
is SortAttribute.Custom -> Pair(sortAttribute.entityStateClass, sortAttribute.entityStateColumnName)
|
||||
}
|
||||
val sortEntityRoot =
|
||||
rootEntities.getOrElse(entityStateClass) {
|
||||
// scenario where sorting on attributes not parsed as criteria
|
||||
val entityRoot = criteriaQuery.from(entityStateClass)
|
||||
rootEntities.put(entityStateClass, entityRoot)
|
||||
val joinPredicate = criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), entityRoot.get<PersistentStateRef>("stateRef"))
|
||||
joinPredicates.add(joinPredicate)
|
||||
entityRoot
|
||||
}
|
||||
when (direction) {
|
||||
Sort.Direction.ASC -> {
|
||||
orderCriteria.add(criteriaBuilder.asc(sortEntityRoot.get<String>(entityStateColumnName)))
|
||||
}
|
||||
Sort.Direction.DESC ->
|
||||
orderCriteria.add(criteriaBuilder.desc(sortEntityRoot.get<String>(entityStateColumnName)))
|
||||
}
|
||||
}
|
||||
if (orderCriteria.isNotEmpty()) {
|
||||
criteriaQuery.orderBy(orderCriteria)
|
||||
criteriaQuery.where(*joinPredicates.toTypedArray())
|
||||
}
|
||||
}
|
||||
|
||||
private fun parse(sortAttribute: Sort.Attribute): Pair<Class<out PersistentState>, String> {
|
||||
val entityClassAndColumnName : Pair<Class<out PersistentState>, String> =
|
||||
when(sortAttribute) {
|
||||
is Sort.VaultStateAttribute -> {
|
||||
Pair(VaultSchemaV1.VaultStates::class.java, sortAttribute.columnName)
|
||||
}
|
||||
is Sort.LinearStateAttribute -> {
|
||||
Pair(VaultSchemaV1.VaultLinearStates::class.java, sortAttribute.columnName)
|
||||
}
|
||||
is Sort.FungibleStateAttribute -> {
|
||||
Pair(VaultSchemaV1.VaultFungibleStates::class.java, sortAttribute.columnName)
|
||||
}
|
||||
else -> throw VaultQueryException("Invalid sort attribute: $sortAttribute")
|
||||
}
|
||||
return entityClassAndColumnName
|
||||
}
|
||||
}
|
@ -0,0 +1,148 @@
|
||||
package net.corda.node.services.vault
|
||||
|
||||
import net.corda.core.ThreadBox
|
||||
import net.corda.core.bufferUntilSubscribed
|
||||
import net.corda.core.contracts.ContractState
|
||||
import net.corda.core.contracts.StateAndRef
|
||||
import net.corda.core.contracts.StateRef
|
||||
import net.corda.core.contracts.TransactionState
|
||||
import net.corda.core.crypto.SecureHash
|
||||
import net.corda.core.node.services.Vault
|
||||
import net.corda.core.node.services.VaultQueryException
|
||||
import net.corda.core.node.services.VaultQueryService
|
||||
import net.corda.core.node.services.vault.MAX_PAGE_SIZE
|
||||
import net.corda.core.node.services.vault.PageSpecification
|
||||
import net.corda.core.node.services.vault.QueryCriteria
|
||||
import net.corda.core.node.services.vault.Sort
|
||||
import net.corda.core.serialization.SingletonSerializeAsToken
|
||||
import net.corda.core.serialization.deserialize
|
||||
import net.corda.core.serialization.storageKryo
|
||||
import net.corda.core.utilities.loggerFor
|
||||
import net.corda.node.services.database.HibernateConfiguration
|
||||
import net.corda.node.services.vault.schemas.jpa.VaultSchemaV1
|
||||
import net.corda.node.utilities.wrapWithDatabaseTransaction
|
||||
import org.jetbrains.exposed.sql.transactions.TransactionManager
|
||||
import rx.subjects.PublishSubject
|
||||
import java.lang.Exception
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.Tuple
|
||||
|
||||
|
||||
class HibernateVaultQueryImpl(hibernateConfig: HibernateConfiguration,
|
||||
val updatesPublisher: PublishSubject<Vault.Update>) : SingletonSerializeAsToken(), VaultQueryService {
|
||||
|
||||
companion object {
|
||||
val log = loggerFor<HibernateVaultQueryImpl>()
|
||||
}
|
||||
|
||||
private val sessionFactory = hibernateConfig.sessionFactoryForRegisteredSchemas()
|
||||
private val criteriaBuilder = sessionFactory.criteriaBuilder
|
||||
|
||||
@Throws(VaultQueryException::class)
|
||||
override fun <T : ContractState> _queryBy(criteria: QueryCriteria, paging: PageSpecification, sorting: Sort, contractType: Class<out T>): Vault.Page<T> {
|
||||
log.info("Vault Query for contract type: $contractType, criteria: $criteria, pagination: $paging, sorting: $sorting")
|
||||
|
||||
val session = sessionFactory.withOptions().
|
||||
connection(TransactionManager.current().connection).
|
||||
openSession()
|
||||
|
||||
session.use {
|
||||
val criteriaQuery = criteriaBuilder.createQuery(Tuple::class.java)
|
||||
val queryRootVaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
|
||||
val contractTypeMappings = resolveUniqueContractStateTypes(session)
|
||||
// TODO: revisit (use single instance of parser for all queries)
|
||||
val criteriaParser = HibernateQueryCriteriaParser(contractType, contractTypeMappings, criteriaBuilder, criteriaQuery, queryRootVaultStates)
|
||||
|
||||
try {
|
||||
// parse criteria and build where predicates
|
||||
criteriaParser.parse(criteria, sorting)
|
||||
|
||||
// prepare query for execution
|
||||
val query = session.createQuery(criteriaQuery)
|
||||
|
||||
// pagination
|
||||
if (paging.pageNumber < 0) throw VaultQueryException("Page specification: invalid page number ${paging.pageNumber} [page numbers start from 0]")
|
||||
if (paging.pageSize < 0 || paging.pageSize > MAX_PAGE_SIZE) throw VaultQueryException("Page specification: invalid page size ${paging.pageSize} [maximum page size is ${MAX_PAGE_SIZE}]")
|
||||
|
||||
// count total results available
|
||||
val countQuery = criteriaBuilder.createQuery(Long::class.java)
|
||||
countQuery.select(criteriaBuilder.count(countQuery.from(VaultSchemaV1.VaultStates::class.java)))
|
||||
val totalStates = session.createQuery(countQuery).singleResult.toInt()
|
||||
|
||||
if ((paging.pageNumber != 0) && (paging.pageSize * paging.pageNumber >= totalStates))
|
||||
throw VaultQueryException("Requested more results than available [${paging.pageSize} * ${paging.pageNumber} >= ${totalStates}]")
|
||||
|
||||
query.firstResult = paging.pageNumber * paging.pageSize
|
||||
query.maxResults = paging.pageSize
|
||||
|
||||
// execution
|
||||
val results = query.resultList
|
||||
val statesAndRefs: MutableList<StateAndRef<*>> = mutableListOf()
|
||||
val statesMeta: MutableList<Vault.StateMetadata> = mutableListOf()
|
||||
|
||||
results.asSequence()
|
||||
.forEach { it ->
|
||||
val it = it[0] as VaultSchemaV1.VaultStates
|
||||
val stateRef = StateRef(SecureHash.parse(it.stateRef!!.txId!!), it.stateRef!!.index!!)
|
||||
val state = it.contractState.deserialize<TransactionState<T>>(storageKryo())
|
||||
statesMeta.add(Vault.StateMetadata(stateRef, it.contractStateClassName, it.recordedTime, it.consumedTime, it.stateStatus, it.notaryName, it.notaryKey, it.lockId, it.lockUpdateTime))
|
||||
statesAndRefs.add(StateAndRef(state, stateRef))
|
||||
}
|
||||
|
||||
return Vault.Page(states = statesAndRefs, statesMetadata = statesMeta, pageable = paging, stateTypes = criteriaParser.stateTypes, totalStatesAvailable = totalStates) as Vault.Page<T>
|
||||
|
||||
} catch (e: Exception) {
|
||||
log.error(e.message)
|
||||
throw e.cause ?: e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val mutex = ThreadBox ({ updatesPublisher })
|
||||
|
||||
@Throws(VaultQueryException::class)
|
||||
override fun <T : ContractState> _trackBy(criteria: QueryCriteria, paging: PageSpecification, sorting: Sort, contractType: Class<out T>): Vault.PageAndUpdates<T> {
|
||||
return mutex.locked {
|
||||
val snapshotResults = _queryBy<T>(criteria, paging, sorting, contractType)
|
||||
Vault.PageAndUpdates(snapshotResults,
|
||||
updatesPublisher.bufferUntilSubscribed()
|
||||
.filter { it.containsType(contractType, snapshotResults.stateTypes) } )
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maintain a list of contract state interfaces to concrete types stored in the vault
|
||||
* for usage in generic queries of type queryBy<LinearState> or queryBy<FungibleState<*>>
|
||||
*/
|
||||
fun resolveUniqueContractStateTypes(session: EntityManager) : Map<String, List<String>> {
|
||||
val criteria = criteriaBuilder.createQuery(String::class.java)
|
||||
val vaultStates = criteria.from(VaultSchemaV1.VaultStates::class.java)
|
||||
criteria.select(vaultStates.get("contractStateClassName")).distinct(true)
|
||||
val query = session.createQuery(criteria)
|
||||
val results = query.resultList
|
||||
val distinctTypes = results.map { it }
|
||||
|
||||
val contractInterfaceToConcreteTypes = mutableMapOf<String, MutableList<String>>()
|
||||
distinctTypes.forEach { it ->
|
||||
val concreteType = Class.forName(it) as Class<ContractState>
|
||||
val contractInterfaces = deriveContractInterfaces(concreteType)
|
||||
contractInterfaces.map {
|
||||
val contractInterface = contractInterfaceToConcreteTypes.getOrPut(it.name, { mutableListOf() })
|
||||
contractInterface.add(concreteType.name)
|
||||
}
|
||||
}
|
||||
return contractInterfaceToConcreteTypes
|
||||
}
|
||||
|
||||
private fun <T: ContractState> deriveContractInterfaces(clazz: Class<T>): Set<Class<T>> {
|
||||
val myInterfaces: MutableSet<Class<T>> = mutableSetOf()
|
||||
clazz.interfaces.forEach {
|
||||
if (!it.equals(ContractState::class.java)) {
|
||||
myInterfaces.add(it as Class<T>)
|
||||
myInterfaces.addAll(deriveContractInterfaces(it))
|
||||
}
|
||||
}
|
||||
return myInterfaces
|
||||
}
|
||||
}
|
@ -20,13 +20,7 @@ import net.corda.core.crypto.toBase58String
|
||||
import net.corda.core.identity.AbstractParty
|
||||
import net.corda.core.identity.Party
|
||||
import net.corda.core.node.ServiceHub
|
||||
import net.corda.core.node.services.StatesNotAvailableException
|
||||
import net.corda.core.node.services.Vault
|
||||
import net.corda.core.node.services.VaultService
|
||||
import net.corda.core.node.services.unconsumedStates
|
||||
import net.corda.core.node.services.vault.PageSpecification
|
||||
import net.corda.core.node.services.vault.QueryCriteria
|
||||
import net.corda.core.node.services.vault.Sort
|
||||
import net.corda.core.node.services.*
|
||||
import net.corda.core.serialization.*
|
||||
import net.corda.core.tee
|
||||
import net.corda.core.transactions.TransactionBuilder
|
||||
@ -35,7 +29,7 @@ import net.corda.core.utilities.loggerFor
|
||||
import net.corda.core.utilities.trace
|
||||
import net.corda.node.services.database.RequeryConfiguration
|
||||
import net.corda.node.services.statemachine.FlowStateMachineImpl
|
||||
import net.corda.node.services.vault.schemas.*
|
||||
import net.corda.node.services.vault.schemas.requery.*
|
||||
import net.corda.node.utilities.bufferUntilDatabaseCommit
|
||||
import net.corda.node.utilities.wrapWithDatabaseTransaction
|
||||
import rx.Observable
|
||||
@ -173,6 +167,9 @@ class NodeVaultService(private val services: ServiceHub, dataSourceProperties: P
|
||||
override val updates: Observable<Vault.Update>
|
||||
get() = mutex.locked { _updatesInDbTx }
|
||||
|
||||
override val updatesPublisher: PublishSubject<Vault.Update>
|
||||
get() = mutex.locked { _updatesPublisher }
|
||||
|
||||
override fun track(): Pair<Vault<ContractState>, Observable<Vault.Update>> {
|
||||
return mutex.locked {
|
||||
Pair(Vault(unconsumedStates<ContractState>()), _updatesPublisher.bufferUntilSubscribed().wrapWithDatabaseTransaction())
|
||||
@ -222,26 +219,6 @@ class NodeVaultService(private val services: ServiceHub, dataSourceProperties: P
|
||||
return stateAndRefs.associateBy({ it.ref }, { it.state })
|
||||
}
|
||||
|
||||
override fun <T : ContractState> queryBy(criteria: QueryCriteria, paging: PageSpecification, sorting: Sort): Vault.Page<T> {
|
||||
|
||||
TODO("Under construction")
|
||||
|
||||
// If [VaultQueryCriteria.PageSpecification] specified
|
||||
// must return (CloseableIterator) result.get().iterator(skip, take)
|
||||
// where
|
||||
// skip = Max[(pageNumber - 1),0] * pageSize
|
||||
// take = pageSize
|
||||
}
|
||||
|
||||
override fun <T : ContractState> trackBy(criteria: QueryCriteria, paging: PageSpecification, sorting: Sort): Vault.PageAndUpdates<T> {
|
||||
TODO("Under construction")
|
||||
|
||||
// return mutex.locked {
|
||||
// Vault.PageAndUpdates(queryBy(criteria),
|
||||
// _updatesPublisher.bufferUntilSubscribed().wrapWithDatabaseTransaction())
|
||||
// }
|
||||
}
|
||||
|
||||
override fun notifyAll(txns: Iterable<WireTransaction>) {
|
||||
val ourKeys = services.keyManagementService.keys
|
||||
val netDelta = txns.fold(Vault.NoUpdate) { netDelta, txn -> netDelta + makeUpdate(txn, ourKeys) }
|
||||
|
@ -0,0 +1,134 @@
|
||||
package net.corda.node.services.vault.schemas.jpa
|
||||
|
||||
import net.corda.core.contracts.UniqueIdentifier
|
||||
import net.corda.core.identity.AbstractParty
|
||||
import net.corda.core.identity.AnonymousParty
|
||||
import net.corda.core.node.services.Vault
|
||||
import net.corda.core.schemas.MappedSchema
|
||||
import net.corda.core.schemas.PersistentState
|
||||
import net.corda.core.serialization.OpaqueBytes
|
||||
import java.security.PublicKey
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
import javax.persistence.*
|
||||
|
||||
/**
|
||||
* JPA representation of the core Vault Schema
|
||||
*/
|
||||
object VaultSchema
|
||||
|
||||
/**
|
||||
* First version of the Vault ORM schema
|
||||
*/
|
||||
object VaultSchemaV1 : MappedSchema(schemaFamily = VaultSchema.javaClass, version = 1,
|
||||
mappedTypes = listOf(VaultStates::class.java, VaultLinearStates::class.java, VaultFungibleStates::class.java, CommonSchemaV1.Party::class.java)) {
|
||||
@Entity
|
||||
@Table(name = "vault_states",
|
||||
indexes = arrayOf(Index(name = "state_status_idx", columnList = "state_status")))
|
||||
class VaultStates(
|
||||
/** refers to the notary a state is attached to */
|
||||
@Column(name = "notary_name")
|
||||
var notaryName: String,
|
||||
|
||||
@Column(name = "notary_key", length = 65535) // TODO What is the upper limit on size of CompositeKey?
|
||||
var notaryKey: String,
|
||||
|
||||
/** references a concrete ContractState that is [QueryableState] and has a [MappedSchema] */
|
||||
@Column(name = "contract_state_class_name")
|
||||
var contractStateClassName: String,
|
||||
|
||||
/** refers to serialized transaction Contract State */
|
||||
// TODO: define contract state size maximum size and adjust length accordingly
|
||||
@Column(name = "contract_state", length = 100000)
|
||||
var contractState: ByteArray,
|
||||
|
||||
/** state lifecycle: unconsumed, consumed */
|
||||
@Column(name = "state_status")
|
||||
var stateStatus: Vault.StateStatus,
|
||||
|
||||
/** refers to timestamp recorded upon entering UNCONSUMED state */
|
||||
@Column(name = "recorded_timestamp")
|
||||
var recordedTime: Instant,
|
||||
|
||||
/** refers to timestamp recorded upon entering CONSUMED state */
|
||||
@Column(name = "consumed_timestamp", nullable = true)
|
||||
var consumedTime: Instant?,
|
||||
|
||||
/** used to denote a state has been soft locked (to prevent double spend)
|
||||
* will contain a temporary unique [UUID] obtained from a flow session */
|
||||
@Column(name = "lock_id", nullable = true)
|
||||
var lockId: String,
|
||||
|
||||
/** refers to the last time a lock was taken (reserved) or updated (released, re-reserved) */
|
||||
@Column(name = "lock_timestamp", nullable = true)
|
||||
var lockUpdateTime: Instant?
|
||||
) : PersistentState()
|
||||
|
||||
@Entity
|
||||
@Table(name = "vault_linear_states",
|
||||
indexes = arrayOf(Index(name = "external_id_index", columnList = "external_id"),
|
||||
Index(name = "uuid_index", columnList = "uuid"),
|
||||
Index(name = "deal_reference_index", columnList = "deal_reference")))
|
||||
class VaultLinearStates(
|
||||
/** [ContractState] attributes */
|
||||
@OneToMany(cascade = arrayOf(CascadeType.ALL))
|
||||
var participants: Set<CommonSchemaV1.Party>,
|
||||
|
||||
/**
|
||||
* Represents a [LinearState] [UniqueIdentifier]
|
||||
*/
|
||||
@Column(name = "external_id")
|
||||
var externalId: String?,
|
||||
|
||||
@Column(name = "uuid", nullable = false)
|
||||
var uuid: UUID,
|
||||
|
||||
// TODO: DealState to be deprecated (collapsed into LinearState)
|
||||
|
||||
/** Deal State attributes **/
|
||||
@Column(name = "deal_reference")
|
||||
var dealReference: String
|
||||
) : PersistentState() {
|
||||
constructor(uid: UniqueIdentifier, _dealReference: String, _participants: List<AbstractParty>) :
|
||||
this(externalId = uid.externalId,
|
||||
uuid = uid.id,
|
||||
dealReference = _dealReference,
|
||||
participants = _participants.map{ CommonSchemaV1.Party(it) }.toSet() )
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Table(name = "vault_fungible_states")
|
||||
class VaultFungibleStates(
|
||||
/** [ContractState] attributes */
|
||||
@OneToMany(cascade = arrayOf(CascadeType.ALL))
|
||||
var participants: Set<CommonSchemaV1.Party>,
|
||||
|
||||
/** [OwnableState] attributes */
|
||||
@OneToOne(cascade = arrayOf(CascadeType.ALL))
|
||||
var owner: CommonSchemaV1.Party,
|
||||
|
||||
/** [FungibleAsset] attributes
|
||||
*
|
||||
* Note: the underlying Product being issued must be modelled into the
|
||||
* custom contract itself (eg. see currency in Cash contract state)
|
||||
*/
|
||||
|
||||
/** Amount attributes */
|
||||
@Column(name = "quantity")
|
||||
var quantity: Long,
|
||||
|
||||
/** Issuer attributes */
|
||||
@OneToOne(cascade = arrayOf(CascadeType.ALL))
|
||||
var issuerParty: CommonSchemaV1.Party,
|
||||
|
||||
@Column(name = "issuer_reference")
|
||||
var issuerRef: ByteArray
|
||||
) : PersistentState() {
|
||||
constructor(_owner: AbstractParty, _quantity: Long, _issuerParty: AbstractParty, _issuerRef: OpaqueBytes, _participants: List<AbstractParty>) :
|
||||
this(owner = CommonSchemaV1.Party(_owner),
|
||||
quantity = _quantity,
|
||||
issuerParty = CommonSchemaV1.Party(_issuerParty),
|
||||
issuerRef = _issuerRef.bytes,
|
||||
participants = _participants.map { CommonSchemaV1.Party(it) }.toSet())
|
||||
}
|
||||
}
|
@ -1,86 +1,88 @@
|
||||
package net.corda.node.services.vault;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import kotlin.Pair;
|
||||
import net.corda.contracts.DealState;
|
||||
import net.corda.contracts.asset.Cash;
|
||||
import com.google.common.collect.*;
|
||||
import kotlin.*;
|
||||
import net.corda.contracts.*;
|
||||
import net.corda.contracts.asset.*;
|
||||
import net.corda.core.contracts.*;
|
||||
import net.corda.core.crypto.SecureHash;
|
||||
import net.corda.core.node.services.Vault;
|
||||
import net.corda.core.node.services.VaultService;
|
||||
import net.corda.core.node.services.vault.PageSpecification;
|
||||
import net.corda.core.node.services.vault.QueryCriteria;
|
||||
import net.corda.core.node.services.vault.QueryCriteria.LinearStateQueryCriteria;
|
||||
import net.corda.core.node.services.vault.QueryCriteria.VaultQueryCriteria;
|
||||
import net.corda.core.node.services.vault.Sort;
|
||||
import net.corda.core.serialization.OpaqueBytes;
|
||||
import net.corda.core.transactions.SignedTransaction;
|
||||
import net.corda.core.transactions.WireTransaction;
|
||||
import net.corda.node.services.vault.schemas.VaultLinearStateEntity;
|
||||
import net.corda.testing.node.MockServices;
|
||||
import org.bouncycastle.asn1.x500.X500Name;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.exposed.sql.Database;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import net.corda.core.crypto.*;
|
||||
import net.corda.core.identity.*;
|
||||
import net.corda.core.node.services.*;
|
||||
import net.corda.core.node.services.vault.*;
|
||||
import net.corda.core.node.services.vault.QueryCriteria.*;
|
||||
import net.corda.core.schemas.*;
|
||||
import net.corda.core.serialization.*;
|
||||
import net.corda.core.transactions.*;
|
||||
import net.corda.node.services.database.*;
|
||||
import net.corda.node.services.schema.*;
|
||||
import net.corda.schemas.*;
|
||||
import net.corda.testing.node.*;
|
||||
import org.jetbrains.annotations.*;
|
||||
import org.jetbrains.exposed.sql.*;
|
||||
import org.junit.*;
|
||||
import rx.Observable;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
import java.util.stream.*;
|
||||
|
||||
import static net.corda.contracts.asset.CashKt.getDUMMY_CASH_ISSUER;
|
||||
import static net.corda.contracts.asset.CashKt.getDUMMY_CASH_ISSUER_KEY;
|
||||
import static net.corda.contracts.asset.CashKt.*;
|
||||
import static net.corda.contracts.testing.VaultFiller.*;
|
||||
import static net.corda.core.node.services.vault.QueryCriteriaKt.and;
|
||||
import static net.corda.core.node.services.vault.QueryCriteriaUtilsKt.getMAX_PAGE_SIZE;
|
||||
import static net.corda.core.utilities.TestConstants.getDUMMY_NOTARY;
|
||||
import static net.corda.node.utilities.DatabaseSupportKt.configureDatabase;
|
||||
import static net.corda.core.node.services.vault.QueryCriteriaKt.*;
|
||||
import static net.corda.core.node.services.vault.QueryCriteriaUtilsKt.*;
|
||||
import static net.corda.core.utilities.TestConstants.*;
|
||||
import static net.corda.node.utilities.DatabaseSupportKt.*;
|
||||
import static net.corda.node.utilities.DatabaseSupportKt.transaction;
|
||||
import static net.corda.testing.CoreTestUtils.getMEGA_CORP;
|
||||
import static net.corda.testing.node.MockServicesKt.makeTestDataSourceProperties;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static net.corda.testing.CoreTestUtils.*;
|
||||
import static net.corda.testing.node.MockServicesKt.*;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
@Ignore
|
||||
public class VaultQueryJavaTests {
|
||||
|
||||
private MockServices services;
|
||||
private VaultService vaultSvc;
|
||||
VaultService vaultSvc;
|
||||
private VaultQueryService vaultQuerySvc;
|
||||
private Closeable dataSource;
|
||||
private Database database;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
Properties dataSourceProps = makeTestDataSourceProperties(SecureHash.randomSHA256().toString());
|
||||
Pair<Closeable, Database> dataSourceAndDatabase = configureDatabase(dataSourceProps);
|
||||
dataSource = dataSourceAndDatabase.getFirst();
|
||||
database = dataSourceAndDatabase.getSecond();
|
||||
|
||||
transaction(database, statement -> services = new MockServices() {
|
||||
@NotNull
|
||||
@Override
|
||||
public VaultService getVaultService() {
|
||||
return makeVaultService(dataSourceProps);
|
||||
}
|
||||
Set<MappedSchema> customSchemas = new HashSet<>(Arrays.asList(DummyLinearStateSchemaV1.INSTANCE));
|
||||
HibernateConfiguration hibernateConfig = new HibernateConfiguration(new NodeSchemaService(customSchemas));
|
||||
transaction(database,
|
||||
statement -> { services = new MockServices(getMEGA_CORP_KEY()) {
|
||||
@NotNull
|
||||
@Override
|
||||
public VaultService getVaultService() {
|
||||
return makeVaultService(dataSourceProps, hibernateConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recordTransactions(@NotNull Iterable<SignedTransaction> txs) {
|
||||
for (SignedTransaction stx : txs ) {
|
||||
getStorageService().getValidatedTransactions().addTransaction(stx);
|
||||
}
|
||||
@Override
|
||||
public VaultQueryService getVaultQueryService() {
|
||||
return new HibernateVaultQueryImpl(hibernateConfig, getVaultService().getUpdatesPublisher());
|
||||
}
|
||||
|
||||
Stream<WireTransaction> wtxn = StreamSupport.stream(txs.spliterator(), false).map(txn -> txn.getTx());
|
||||
getVaultService().notifyAll(wtxn.collect(Collectors.toList()));
|
||||
}
|
||||
@Override
|
||||
public void recordTransactions(@NotNull Iterable<SignedTransaction> txs) {
|
||||
for (SignedTransaction stx : txs) {
|
||||
getStorageService().getValidatedTransactions().addTransaction(stx);
|
||||
}
|
||||
|
||||
Stream<WireTransaction> wtxn = StreamSupport.stream(txs.spliterator(), false).map(txn -> txn.getTx());
|
||||
getVaultService().notifyAll(wtxn.collect(Collectors.toList()));
|
||||
}
|
||||
};
|
||||
vaultSvc = services.getVaultService();
|
||||
vaultQuerySvc = services.getVaultQueryService();
|
||||
|
||||
return services;
|
||||
});
|
||||
|
||||
vaultSvc = services.getVaultService();
|
||||
}
|
||||
|
||||
@After
|
||||
@ -97,8 +99,27 @@ public class VaultQueryJavaTests {
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void consumedStates() {
|
||||
public void unconsumedLinearStates() throws VaultQueryException {
|
||||
transaction(database, tx -> {
|
||||
|
||||
fillWithSomeTestLinearStates(services, 3);
|
||||
|
||||
// DOCSTART VaultJavaQueryExample0
|
||||
Vault.Page<LinearState> results = vaultQuerySvc.queryBy(LinearState.class);
|
||||
// DOCEND VaultJavaQueryExample0
|
||||
|
||||
assertThat(results.getStates()).hasSize(3);
|
||||
|
||||
return tx;
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consumedCashStates() {
|
||||
transaction(database, tx -> {
|
||||
|
||||
Amount<Currency> amount = new Amount<>(100, Currency.getInstance("USD"));
|
||||
|
||||
fillWithSomeTestCash(services,
|
||||
new Amount<>(100, Currency.getInstance("USD")),
|
||||
getDUMMY_NOTARY(),
|
||||
@ -110,13 +131,11 @@ public class VaultQueryJavaTests {
|
||||
getDUMMY_CASH_ISSUER(),
|
||||
getDUMMY_CASH_ISSUER_KEY() );
|
||||
|
||||
// DOCSTART VaultJavaQueryExample1
|
||||
@SuppressWarnings("unchecked")
|
||||
Set<Class<ContractState>> contractStateTypes = new HashSet(Collections.singletonList(Cash.State.class));
|
||||
Vault.StateStatus status = Vault.StateStatus.CONSUMED;
|
||||
consumeCash(services, amount);
|
||||
|
||||
VaultQueryCriteria criteria = new VaultQueryCriteria(status, null, contractStateTypes);
|
||||
Vault.Page<ContractState> results = vaultSvc.queryBy(criteria);
|
||||
// DOCSTART VaultJavaQueryExample1
|
||||
VaultQueryCriteria criteria = new VaultQueryCriteria(Vault.StateStatus.CONSUMED);
|
||||
Vault.Page<Cash.State> results = vaultQuerySvc.queryBy(Cash.State.class, criteria);
|
||||
// DOCEND VaultJavaQueryExample1
|
||||
|
||||
assertThat(results.getStates()).hasSize(3);
|
||||
@ -126,32 +145,38 @@ public class VaultQueryJavaTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consumedDealStatesPagedSorted() {
|
||||
public void consumedDealStatesPagedSorted() throws VaultQueryException {
|
||||
transaction(database, tx -> {
|
||||
|
||||
UniqueIdentifier uid = new UniqueIdentifier();
|
||||
fillWithSomeTestLinearStates(services, 10, uid);
|
||||
Vault<LinearState> states = fillWithSomeTestLinearStates(services, 10, null);
|
||||
StateAndRef<LinearState> linearState = states.getStates().iterator().next();
|
||||
UniqueIdentifier uid = linearState.component1().getData().getLinearId();
|
||||
|
||||
List<String> dealIds = Arrays.asList("123", "456", "789");
|
||||
fillWithSomeTestDeals(services, dealIds);
|
||||
Vault<DealState> dealStates = fillWithSomeTestDeals(services, dealIds);
|
||||
|
||||
// consume states
|
||||
consumeDeals(services, (List<? extends StateAndRef<? extends DealState>>) dealStates.getStates());
|
||||
consumeLinearStates(services, Arrays.asList(linearState));
|
||||
|
||||
// DOCSTART VaultJavaQueryExample2
|
||||
Vault.StateStatus status = Vault.StateStatus.CONSUMED;
|
||||
@SuppressWarnings("unchecked")
|
||||
Set<Class<ContractState>> contractStateTypes = new HashSet(Collections.singletonList(Cash.State.class));
|
||||
Set<Class<LinearState>> contractStateTypes = new HashSet(Collections.singletonList(LinearState.class));
|
||||
|
||||
QueryCriteria vaultCriteria = new VaultQueryCriteria(status, null, contractStateTypes);
|
||||
QueryCriteria vaultCriteria = new VaultQueryCriteria(status, contractStateTypes);
|
||||
|
||||
List<UniqueIdentifier> linearIds = Arrays.asList(uid);
|
||||
List<X500Name> dealPartyNames = Arrays.asList(getMEGA_CORP().getName());
|
||||
QueryCriteria dealCriteriaAll = new LinearStateQueryCriteria(linearIds, false, dealIds, dealPartyNames);
|
||||
QueryCriteria linearCriteriaAll = new LinearStateQueryCriteria(null, linearIds);
|
||||
QueryCriteria dealCriteriaAll = new LinearStateQueryCriteria(null, null, dealIds);
|
||||
|
||||
QueryCriteria compositeCriteria = and(dealCriteriaAll, vaultCriteria);
|
||||
QueryCriteria compositeCriteria1 = or(dealCriteriaAll, linearCriteriaAll);
|
||||
QueryCriteria compositeCriteria2 = and(vaultCriteria, compositeCriteria1);
|
||||
|
||||
PageSpecification pageSpec = new PageSpecification(0, getMAX_PAGE_SIZE());
|
||||
Sort.SortColumn sortByUid = new Sort.SortColumn(VaultLinearStateEntity.UUID.getName(), Sort.Direction.DESC, Sort.NullHandling.NULLS_LAST);
|
||||
Sort.SortColumn sortByUid = new Sort.SortColumn(new SortAttribute.Standard(Sort.LinearStateAttribute.UUID), Sort.Direction.DESC);
|
||||
Sort sorting = new Sort(ImmutableSet.of(sortByUid));
|
||||
Vault.Page<ContractState> results = vaultSvc.queryBy(compositeCriteria, pageSpec, sorting);
|
||||
Vault.Page<LinearState> results = vaultQuerySvc.queryBy(LinearState.class, compositeCriteria2, pageSpec, sorting);
|
||||
// DOCEND VaultJavaQueryExample2
|
||||
|
||||
assertThat(results.getStates()).hasSize(4);
|
||||
@ -160,13 +185,52 @@ public class VaultQueryJavaTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customQueryForCashStatesWithAmountOfCurrencyGreaterOrEqualThanQuantity() {
|
||||
transaction(database, tx -> {
|
||||
|
||||
Amount<Currency> pounds = new Amount<>(100, Currency.getInstance("GBP"));
|
||||
Amount<Currency> dollars100 = new Amount<>(100, Currency.getInstance("USD"));
|
||||
Amount<Currency> dollars10 = new Amount<>(10, Currency.getInstance("USD"));
|
||||
Amount<Currency> dollars1 = new Amount<>(1, Currency.getInstance("USD"));
|
||||
|
||||
fillWithSomeTestCash(services, pounds, getDUMMY_NOTARY(), 1, 1, new Random(0L), new OpaqueBytes("1".getBytes()), null, getDUMMY_CASH_ISSUER(), getDUMMY_CASH_ISSUER_KEY());
|
||||
fillWithSomeTestCash(services, dollars100, getDUMMY_NOTARY(), 1, 1, new Random(0L), new OpaqueBytes("1".getBytes()), null, getDUMMY_CASH_ISSUER(), getDUMMY_CASH_ISSUER_KEY());
|
||||
fillWithSomeTestCash(services, dollars10, getDUMMY_NOTARY(), 1, 1, new Random(0L), new OpaqueBytes("1".getBytes()), null, getDUMMY_CASH_ISSUER(), getDUMMY_CASH_ISSUER_KEY());
|
||||
fillWithSomeTestCash(services, dollars1, getDUMMY_NOTARY(), 1, 1, new Random(0L), new OpaqueBytes("1".getBytes()), null, getDUMMY_CASH_ISSUER(), getDUMMY_CASH_ISSUER_KEY());
|
||||
|
||||
try {
|
||||
// DOCSTART VaultJavaQueryExample3
|
||||
QueryCriteria generalCriteria = new VaultQueryCriteria(Vault.StateStatus.ALL);
|
||||
|
||||
Field attributeCurrency = CashSchemaV1.PersistentCashState.class.getDeclaredField("currency");
|
||||
Field attributeQuantity = CashSchemaV1.PersistentCashState.class.getDeclaredField("pennies");
|
||||
|
||||
CriteriaExpression currencyIndex = Builder.INSTANCE.equal(attributeCurrency, "USD");
|
||||
CriteriaExpression quantityIndex = Builder.INSTANCE.greaterThanOrEqual(attributeQuantity, 10L);
|
||||
|
||||
QueryCriteria customCriteria2 = new VaultCustomQueryCriteria(quantityIndex);
|
||||
QueryCriteria customCriteria1 = new VaultCustomQueryCriteria(currencyIndex);
|
||||
|
||||
|
||||
QueryCriteria criteria = QueryCriteriaKt.and(QueryCriteriaKt.and(generalCriteria, customCriteria1), customCriteria2);
|
||||
Vault.Page<ContractState> results = vaultQuerySvc.queryBy(Cash.State.class, criteria);
|
||||
// DOCEND VaultJavaQueryExample3
|
||||
|
||||
assertThat(results.getStates()).hasSize(2);
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return tx;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamic trackBy() tests
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void trackCashStates() {
|
||||
|
||||
transaction(database, tx -> {
|
||||
fillWithSomeTestCash(services,
|
||||
new Amount<>(100, Currency.getInstance("USD")),
|
||||
@ -179,17 +243,17 @@ public class VaultQueryJavaTests {
|
||||
getDUMMY_CASH_ISSUER(),
|
||||
getDUMMY_CASH_ISSUER_KEY() );
|
||||
|
||||
// DOCSTART VaultJavaQueryExample1
|
||||
// DOCSTART VaultJavaQueryExample4
|
||||
@SuppressWarnings("unchecked")
|
||||
Set<Class<ContractState>> contractStateTypes = new HashSet(Collections.singletonList(Cash.State.class));
|
||||
|
||||
VaultQueryCriteria criteria = new VaultQueryCriteria(Vault.StateStatus.UNCONSUMED, null, contractStateTypes);
|
||||
Vault.PageAndUpdates<ContractState> results = vaultSvc.trackBy(criteria);
|
||||
VaultQueryCriteria criteria = new VaultQueryCriteria(Vault.StateStatus.UNCONSUMED, contractStateTypes);
|
||||
Vault.PageAndUpdates<ContractState> results = vaultQuerySvc.trackBy(ContractState.class, criteria);
|
||||
|
||||
Vault.Page<ContractState> snapshot = results.getCurrent();
|
||||
Observable<Vault.Update> updates = results.getFuture();
|
||||
|
||||
// DOCEND VaultJavaQueryExample1
|
||||
// DOCEND VaultJavaQueryExample4
|
||||
assertThat(snapshot.getStates()).hasSize(3);
|
||||
|
||||
return tx;
|
||||
@ -200,31 +264,36 @@ public class VaultQueryJavaTests {
|
||||
public void trackDealStatesPagedSorted() {
|
||||
transaction(database, tx -> {
|
||||
|
||||
UniqueIdentifier uid = new UniqueIdentifier();
|
||||
fillWithSomeTestLinearStates(services, 10, uid);
|
||||
Vault<LinearState> states = fillWithSomeTestLinearStates(services, 10, null);
|
||||
UniqueIdentifier uid = states.getStates().iterator().next().component1().getData().getLinearId();
|
||||
|
||||
List<String> dealIds = Arrays.asList("123", "456", "789");
|
||||
fillWithSomeTestDeals(services, dealIds);
|
||||
|
||||
// DOCSTART VaultJavaQueryExample2
|
||||
// DOCSTART VaultJavaQueryExample5
|
||||
@SuppressWarnings("unchecked")
|
||||
Set<Class<ContractState>> contractStateTypes = new HashSet(Collections.singletonList(DealState.class));
|
||||
QueryCriteria vaultCriteria = new VaultQueryCriteria(Vault.StateStatus.UNCONSUMED, null, contractStateTypes);
|
||||
Set<Class<ContractState>> contractStateTypes = new HashSet(Arrays.asList(DealState.class, LinearState.class));
|
||||
QueryCriteria vaultCriteria = new VaultQueryCriteria(Vault.StateStatus.UNCONSUMED, contractStateTypes);
|
||||
|
||||
List<UniqueIdentifier> linearIds = Arrays.asList(uid);
|
||||
List<X500Name> dealPartyNames = Arrays.asList(getMEGA_CORP().getName());
|
||||
QueryCriteria dealCriteriaAll = new LinearStateQueryCriteria(linearIds, false, dealIds, dealPartyNames);
|
||||
List<AbstractParty> dealParty = Arrays.asList(getMEGA_CORP());
|
||||
QueryCriteria dealCriteria = new LinearStateQueryCriteria(dealParty, null, dealIds);
|
||||
|
||||
QueryCriteria compositeCriteria = and(dealCriteriaAll, vaultCriteria);
|
||||
QueryCriteria linearCriteria = new LinearStateQueryCriteria(dealParty, linearIds, null);
|
||||
|
||||
|
||||
QueryCriteria dealOrLinearIdCriteria = or(dealCriteria, linearCriteria);
|
||||
|
||||
QueryCriteria compositeCriteria = and(dealOrLinearIdCriteria, vaultCriteria);
|
||||
|
||||
PageSpecification pageSpec = new PageSpecification(0, getMAX_PAGE_SIZE());
|
||||
Sort.SortColumn sortByUid = new Sort.SortColumn(VaultLinearStateEntity.UUID.getName(), Sort.Direction.DESC, Sort.NullHandling.NULLS_LAST);
|
||||
Sort.SortColumn sortByUid = new Sort.SortColumn(new SortAttribute.Standard(Sort.LinearStateAttribute.UUID), Sort.Direction.DESC);
|
||||
Sort sorting = new Sort(ImmutableSet.of(sortByUid));
|
||||
Vault.PageAndUpdates<ContractState> results = vaultSvc.trackBy(compositeCriteria, pageSpec, sorting);
|
||||
Vault.PageAndUpdates<ContractState> results = vaultQuerySvc.trackBy(ContractState.class, compositeCriteria, pageSpec, sorting);
|
||||
|
||||
Vault.Page<ContractState> snapshot = results.getCurrent();
|
||||
Observable<Vault.Update> updates = results.getFuture();
|
||||
// DOCEND VaultJavaQueryExample2
|
||||
// DOCEND VaultJavaQueryExample5
|
||||
|
||||
assertThat(snapshot.getStates()).hasSize(4);
|
||||
|
||||
@ -239,6 +308,7 @@ public class VaultQueryJavaTests {
|
||||
@Test
|
||||
public void consumedStatesDeprecated() {
|
||||
transaction(database, tx -> {
|
||||
Amount<Currency> amount = new Amount<>(100, Currency.getInstance("USD"));
|
||||
fillWithSomeTestCash(services,
|
||||
new Amount<>(100, Currency.getInstance("USD")),
|
||||
getDUMMY_NOTARY(),
|
||||
@ -250,6 +320,8 @@ public class VaultQueryJavaTests {
|
||||
getDUMMY_CASH_ISSUER(),
|
||||
getDUMMY_CASH_ISSUER_KEY() );
|
||||
|
||||
consumeCash(services, amount);
|
||||
|
||||
// DOCSTART VaultDeprecatedJavaQueryExample1
|
||||
@SuppressWarnings("unchecked")
|
||||
Set<Class<ContractState>> contractStateTypes = new HashSet(Collections.singletonList(Cash.State.class));
|
||||
@ -269,24 +341,21 @@ public class VaultQueryJavaTests {
|
||||
public void consumedStatesForLinearIdDeprecated() {
|
||||
transaction(database, tx -> {
|
||||
|
||||
UniqueIdentifier trackUid = new UniqueIdentifier();
|
||||
fillWithSomeTestLinearStates(services, 1, trackUid);
|
||||
fillWithSomeTestLinearStates(services, 4, new UniqueIdentifier());
|
||||
Vault<LinearState> linearStates = fillWithSomeTestLinearStates(services, 4,null);
|
||||
UniqueIdentifier trackUid = linearStates.getStates().iterator().next().component1().getData().getLinearId();
|
||||
|
||||
// DOCSTART VaultDeprecatedJavaQueryExample2
|
||||
consumeLinearStates(services, (List<? extends StateAndRef<? extends LinearState>>) linearStates.getStates());
|
||||
|
||||
// DOCSTART VaultDeprecatedJavaQueryExample0
|
||||
@SuppressWarnings("unchecked")
|
||||
Set<Class<ContractState>> contractStateTypes = new HashSet(Collections.singletonList(LinearState.class));
|
||||
Set<Class<LinearState>> contractStateTypes = new HashSet(Collections.singletonList(DummyLinearContract.State.class));
|
||||
EnumSet<Vault.StateStatus> status = EnumSet.of(Vault.StateStatus.CONSUMED);
|
||||
|
||||
// WARNING! unfortunately cannot use inlined reified Kotlin extension methods.
|
||||
Iterable<StateAndRef<ContractState>> results = vaultSvc.states(contractStateTypes, status, true);
|
||||
|
||||
Stream<StateAndRef<ContractState>> trackedLinearState = StreamSupport.stream(results.spliterator(), false).filter(
|
||||
state -> ((LinearState) state.component1().getData()).getLinearId() == trackUid);
|
||||
// DOCEND VaultDeprecatedJavaQueryExample2
|
||||
Iterable<StateAndRef<LinearState>> results = vaultSvc.states(contractStateTypes, status, true);
|
||||
// DOCEND VaultDeprecatedJavaQueryExample0
|
||||
|
||||
assertThat(results).hasSize(4);
|
||||
assertThat(trackedLinearState).hasSize(1);
|
||||
|
||||
return tx;
|
||||
});
|
||||
|
@ -7,8 +7,7 @@ import net.corda.core.crypto.isFulfilledBy
|
||||
import net.corda.core.crypto.keys
|
||||
import net.corda.core.flows.FlowLogic
|
||||
import net.corda.core.flows.StateMachineRunId
|
||||
import net.corda.core.messaging.StateMachineUpdate
|
||||
import net.corda.core.messaging.startFlow
|
||||
import net.corda.core.messaging.*
|
||||
import net.corda.core.node.services.ServiceInfo
|
||||
import net.corda.core.node.services.Vault
|
||||
import net.corda.core.node.services.unconsumedStates
|
||||
@ -50,10 +49,11 @@ class CordaRPCOpsImplTest {
|
||||
lateinit var mockNet: MockNetwork
|
||||
lateinit var aliceNode: MockNode
|
||||
lateinit var notaryNode: MockNode
|
||||
lateinit var rpc: CordaRPCOpsImpl
|
||||
lateinit var rpc: CordaRPCOps
|
||||
lateinit var stateMachineUpdates: Observable<StateMachineUpdate>
|
||||
lateinit var transactions: Observable<SignedTransaction>
|
||||
lateinit var vaultUpdates: Observable<Vault.Update>
|
||||
lateinit var vaultUpdates: Observable<Vault.Update> // TODO: deprecated
|
||||
lateinit var vaultTrackCash: Observable<Vault.Update>
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
@ -71,6 +71,7 @@ class CordaRPCOpsImplTest {
|
||||
stateMachineUpdates = rpc.stateMachinesAndUpdates().second
|
||||
transactions = rpc.verifiedTransactions().second
|
||||
vaultUpdates = rpc.vaultAndUpdates().second
|
||||
vaultTrackCash = rpc.vaultTrackBy<Cash.State>().future
|
||||
}
|
||||
}
|
||||
|
||||
@ -93,6 +94,10 @@ class CordaRPCOpsImplTest {
|
||||
Issued(aliceNode.info.legalIdentity.ref(ref), GBP)),
|
||||
recipient)
|
||||
|
||||
// Query vault via RPC
|
||||
val cash = rpc.vaultQueryBy<Cash.State>()
|
||||
assertEquals(expectedState, cash.states.first().state.data)
|
||||
|
||||
var issueSmId: StateMachineRunId? = null
|
||||
stateMachineUpdates.expectEvents {
|
||||
sequence(
|
||||
@ -112,12 +117,20 @@ class CordaRPCOpsImplTest {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: deprecated
|
||||
vaultUpdates.expectEvents {
|
||||
expect { update ->
|
||||
val actual = update.produced.single().state.data
|
||||
assertEquals(expectedState, actual)
|
||||
}
|
||||
}
|
||||
|
||||
vaultTrackCash.expectEvents {
|
||||
expect { update ->
|
||||
val actual = update.produced.single().state.data
|
||||
assertEquals(expectedState, actual)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -180,6 +193,7 @@ class CordaRPCOpsImplTest {
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: deprecated
|
||||
vaultUpdates.expectEvents {
|
||||
sequence(
|
||||
// ISSUE
|
||||
@ -194,6 +208,21 @@ class CordaRPCOpsImplTest {
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
vaultTrackCash.expectEvents {
|
||||
sequence(
|
||||
// ISSUE
|
||||
expect { update ->
|
||||
require(update.consumed.isEmpty()) { update.consumed.size }
|
||||
require(update.produced.size == 1) { update.produced.size }
|
||||
},
|
||||
// MOVE
|
||||
expect { update ->
|
||||
require(update.consumed.size == 1) { update.consumed.size }
|
||||
require(update.produced.size == 1) { update.produced.size }
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -12,7 +12,7 @@ import net.corda.node.services.config.NodeConfiguration
|
||||
import net.corda.node.services.database.RequeryConfiguration
|
||||
import net.corda.node.services.network.NetworkMapService
|
||||
import net.corda.node.services.persistence.NodeAttachmentService
|
||||
import net.corda.node.services.persistence.schemas.AttachmentEntity
|
||||
import net.corda.node.services.persistence.schemas.requery.AttachmentEntity
|
||||
import net.corda.node.services.transactions.SimpleNotaryService
|
||||
import net.corda.testing.node.MockNetwork
|
||||
import net.corda.node.utilities.transaction
|
||||
|
@ -22,6 +22,7 @@ import java.time.Clock
|
||||
|
||||
open class MockServiceHubInternal(
|
||||
val customVault: VaultService? = null,
|
||||
val customVaultQuery: VaultQueryService? = null,
|
||||
val keyManagement: KeyManagementService? = null,
|
||||
val network: MessagingService? = null,
|
||||
val identity: IdentityService? = MOCK_IDENTITY_SERVICE,
|
||||
@ -32,6 +33,8 @@ open class MockServiceHubInternal(
|
||||
val schemas: SchemaService? = NodeSchemaService(),
|
||||
val customTransactionVerifierService: TransactionVerifierService? = InMemoryTransactionVerifierService(2)
|
||||
) : ServiceHubInternal() {
|
||||
override val vaultQueryService: VaultQueryService
|
||||
get() = customVaultQuery ?: throw UnsupportedOperationException()
|
||||
override val transactionVerifierService: TransactionVerifierService
|
||||
get() = customTransactionVerifierService ?: throw UnsupportedOperationException()
|
||||
override val vaultService: VaultService
|
||||
|
@ -1,21 +1,45 @@
|
||||
package net.corda.node.services.config
|
||||
|
||||
import com.google.common.net.HostAndPort
|
||||
import net.corda.core.crypto.commonName
|
||||
import net.corda.core.utilities.ALICE
|
||||
import net.corda.nodeapi.User
|
||||
import net.corda.testing.testConfiguration
|
||||
import net.corda.testing.node.makeTestDataSourceProperties
|
||||
import org.assertj.core.api.Assertions.assertThatThrownBy
|
||||
import org.bouncycastle.asn1.x500.X500Name
|
||||
import org.junit.Test
|
||||
import java.net.URL
|
||||
import java.nio.file.Paths
|
||||
|
||||
class FullNodeConfigurationTest {
|
||||
@Test
|
||||
fun `Artemis special characters not permitted in RPC usernames`() {
|
||||
fun configWithRPCUsername(username: String): FullNodeConfiguration {
|
||||
return testConfiguration(Paths.get("."), ALICE.name, 0).copy(
|
||||
rpcUsers = listOf(User(username, "pass", emptySet())))
|
||||
}
|
||||
val testConfiguration = FullNodeConfiguration(
|
||||
basedir = Paths.get("."),
|
||||
myLegalName = ALICE.name,
|
||||
networkMapService = null,
|
||||
emailAddress = "",
|
||||
keyStorePassword = "cordacadevpass",
|
||||
trustStorePassword = "trustpass",
|
||||
dataSourceProperties = makeTestDataSourceProperties(ALICE.name.commonName),
|
||||
certificateSigningService = URL("http://localhost"),
|
||||
rpcUsers = emptyList(),
|
||||
verifierType = VerifierType.InMemory,
|
||||
useHTTPS = false,
|
||||
p2pAddress = HostAndPort.fromParts("localhost", 0),
|
||||
rpcAddress = HostAndPort.fromParts("localhost", 1),
|
||||
messagingServerAddress = null,
|
||||
extraAdvertisedServiceIds = emptyList(),
|
||||
bftReplicaId = null,
|
||||
notaryNodeAddress = null,
|
||||
notaryClusterAddresses = emptyList(),
|
||||
certificateChainCheckPolicies = emptyList(),
|
||||
devMode = true,
|
||||
relay = null
|
||||
)
|
||||
|
||||
fun configWithRPCUsername(username: String) {
|
||||
testConfiguration.copy(rpcUsers = listOf(User(username, "pass", emptySet())))
|
||||
}
|
||||
assertThatThrownBy { configWithRPCUsername("user.1") }.hasMessageContaining(".")
|
||||
assertThatThrownBy { configWithRPCUsername("user*1") }.hasMessageContaining("*")
|
||||
assertThatThrownBy { configWithRPCUsername("user#1") }.hasMessageContaining("#")
|
||||
|
@ -0,0 +1,698 @@
|
||||
package net.corda.node.services.database
|
||||
|
||||
import net.corda.contracts.asset.Cash
|
||||
import net.corda.contracts.asset.DummyFungibleContract
|
||||
import net.corda.contracts.testing.consumeCash
|
||||
import net.corda.contracts.testing.fillWithSomeTestCash
|
||||
import net.corda.contracts.testing.fillWithSomeTestDeals
|
||||
import net.corda.contracts.testing.fillWithSomeTestLinearStates
|
||||
import net.corda.core.contracts.*
|
||||
import net.corda.core.crypto.toBase58String
|
||||
import net.corda.core.node.services.Vault
|
||||
import net.corda.core.node.services.VaultService
|
||||
import net.corda.core.schemas.DummyLinearStateSchemaV1
|
||||
import net.corda.core.schemas.DummyLinearStateSchemaV2
|
||||
import net.corda.core.schemas.PersistentStateRef
|
||||
import net.corda.core.serialization.deserialize
|
||||
import net.corda.core.serialization.storageKryo
|
||||
import net.corda.core.transactions.SignedTransaction
|
||||
import net.corda.core.utilities.ALICE
|
||||
import net.corda.core.utilities.BOB
|
||||
import net.corda.core.utilities.BOB_KEY
|
||||
import net.corda.core.utilities.DUMMY_NOTARY
|
||||
import net.corda.node.services.schema.HibernateObserver
|
||||
import net.corda.node.services.schema.NodeSchemaService
|
||||
import net.corda.node.services.vault.NodeVaultService
|
||||
import net.corda.node.services.vault.schemas.jpa.CommonSchemaV1
|
||||
import net.corda.node.services.vault.schemas.jpa.VaultSchemaV1
|
||||
import net.corda.node.utilities.configureDatabase
|
||||
import net.corda.node.utilities.transaction
|
||||
import net.corda.schemas.CashSchemaV1
|
||||
import net.corda.schemas.SampleCashSchemaV2
|
||||
import net.corda.schemas.SampleCashSchemaV3
|
||||
import net.corda.testing.BOB_PUBKEY
|
||||
import net.corda.testing.node.MockServices
|
||||
import net.corda.testing.node.makeTestDataSourceProperties
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.hibernate.SessionFactory
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import java.io.Closeable
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
import javax.persistence.EntityManager
|
||||
import javax.persistence.Tuple
|
||||
import javax.persistence.criteria.CriteriaBuilder
|
||||
|
||||
class HibernateConfigurationTest {
|
||||
|
||||
lateinit var services: MockServices
|
||||
lateinit var dataSource: Closeable
|
||||
lateinit var database: Database
|
||||
val vault: VaultService get() = services.vaultService
|
||||
|
||||
// Hibernate configuration objects
|
||||
lateinit var hibernateConfig: HibernateConfiguration
|
||||
lateinit var hibernatePersister: HibernateObserver
|
||||
lateinit var sessionFactory: SessionFactory
|
||||
lateinit var entityManager: EntityManager
|
||||
lateinit var criteriaBuilder: CriteriaBuilder
|
||||
|
||||
// test States
|
||||
lateinit var cashStates: List<StateAndRef<Cash.State>>
|
||||
|
||||
@Before
|
||||
fun setUp() {
|
||||
val dataSourceProps = makeTestDataSourceProperties()
|
||||
val dataSourceAndDatabase = configureDatabase(dataSourceProps)
|
||||
val customSchemas = setOf(VaultSchemaV1, CashSchemaV1, SampleCashSchemaV2, SampleCashSchemaV3)
|
||||
|
||||
dataSource = dataSourceAndDatabase.first
|
||||
database = dataSourceAndDatabase.second
|
||||
database.transaction {
|
||||
|
||||
hibernateConfig = HibernateConfiguration(NodeSchemaService(customSchemas))
|
||||
|
||||
services = object : MockServices(BOB_KEY) {
|
||||
override val vaultService: VaultService get() {
|
||||
val vaultService = NodeVaultService(this, dataSourceProps)
|
||||
hibernatePersister = HibernateObserver(vaultService.rawUpdates, hibernateConfig)
|
||||
return vaultService
|
||||
}
|
||||
|
||||
override fun recordTransactions(txs: Iterable<SignedTransaction>) {
|
||||
for (stx in txs) {
|
||||
storageService.validatedTransactions.addTransaction(stx)
|
||||
}
|
||||
// Refactored to use notifyAll() as we have no other unit test for that method with multiple transactions.
|
||||
vaultService.notifyAll(txs.map { it.tx })
|
||||
}
|
||||
}
|
||||
}
|
||||
setUpDb()
|
||||
|
||||
sessionFactory = hibernateConfig.sessionFactoryForSchemas(*customSchemas.toTypedArray())
|
||||
entityManager = sessionFactory.createEntityManager()
|
||||
criteriaBuilder = sessionFactory.criteriaBuilder
|
||||
}
|
||||
|
||||
@After
|
||||
fun cleanUp() {
|
||||
dataSource.close()
|
||||
}
|
||||
|
||||
private fun setUpDb() {
|
||||
database.transaction {
|
||||
cashStates = services.fillWithSomeTestCash(100.DOLLARS, DUMMY_NOTARY, 10, 10, Random(0L)).states.toList()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `count rows`() {
|
||||
// structure query
|
||||
val countQuery = criteriaBuilder.createQuery(Long::class.java)
|
||||
countQuery.select(criteriaBuilder.count(countQuery.from(VaultSchemaV1.VaultStates::class.java)))
|
||||
|
||||
// execute query
|
||||
val countResult = entityManager.createQuery(countQuery).singleResult
|
||||
|
||||
assertThat(countResult).isEqualTo(10)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `consumed states`() {
|
||||
database.transaction {
|
||||
services.consumeCash(50.DOLLARS)
|
||||
}
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
criteriaQuery.where(criteriaBuilder.equal(
|
||||
vaultStates.get<Vault.StateStatus>("stateStatus"), Vault.StateStatus.CONSUMED))
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
assertThat(queryResults.size).isEqualTo(6)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `select by composite primary key`() {
|
||||
val issuedStates =
|
||||
database.transaction {
|
||||
services.fillWithSomeTestLinearStates(8)
|
||||
services.fillWithSomeTestLinearStates(2)
|
||||
}
|
||||
val persistentStateRefs = issuedStates.states.map { PersistentStateRef(it.ref) }.toList()
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
val compositeKey = vaultStates.get<PersistentStateRef>("stateRef")
|
||||
criteriaQuery.where(criteriaBuilder.and(compositeKey.`in`(persistentStateRefs)))
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
|
||||
assertThat(queryResults).hasSize(2)
|
||||
assertThat(queryResults.first().stateRef?.txId).isEqualTo(issuedStates.states.first().ref.txhash.toString())
|
||||
assertThat(queryResults.first().stateRef?.index).isEqualTo(issuedStates.states.first().ref.index)
|
||||
assertThat(queryResults.last().stateRef?.txId).isEqualTo(issuedStates.states.last().ref.txhash.toString())
|
||||
assertThat(queryResults.last().stateRef?.index).isEqualTo(issuedStates.states.last().ref.index)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `distinct contract types`() {
|
||||
database.transaction {
|
||||
// add 2 more contract types
|
||||
services.fillWithSomeTestLinearStates(10)
|
||||
services.fillWithSomeTestDeals(listOf("123", "456", "789"))
|
||||
}
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(String::class.java)
|
||||
val vaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
criteriaQuery.select(vaultStates.get("contractStateClassName")).distinct(true)
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
Assertions.assertThat(queryResults.size).isEqualTo(3)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `with sorting`() {
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
|
||||
// order by DESC
|
||||
criteriaQuery.orderBy(criteriaBuilder.desc(vaultStates.get<Instant>("recordedTime")))
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
queryResults.map { println(it.recordedTime) }
|
||||
|
||||
// order by ASC
|
||||
criteriaQuery.orderBy(criteriaBuilder.asc(vaultStates.get<Instant>("recordedTime")))
|
||||
val queryResultsAsc = entityManager.createQuery(criteriaQuery).resultList
|
||||
queryResultsAsc.map { println(it.recordedTime) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `with pagination`() {
|
||||
// add 100 additional cash entries
|
||||
database.transaction {
|
||||
services.fillWithSomeTestCash(1000.POUNDS, DUMMY_NOTARY, 100, 100, Random(0L))
|
||||
}
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(VaultSchemaV1.VaultStates::class.java)
|
||||
criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
|
||||
// set pagination
|
||||
val query = entityManager.createQuery(criteriaQuery)
|
||||
query.firstResult = 10
|
||||
query.maxResults = 15
|
||||
|
||||
// execute query
|
||||
val queryResults = query.resultList
|
||||
Assertions.assertThat(queryResults.size).isEqualTo(15)
|
||||
|
||||
// try towards end
|
||||
query.firstResult = 100
|
||||
query.maxResults = 15
|
||||
|
||||
val lastQueryResults = query.resultList
|
||||
|
||||
Assertions.assertThat(lastQueryResults.size).isEqualTo(10)
|
||||
}
|
||||
|
||||
/**
|
||||
* VaultLinearState is a concrete table, extendible by any Contract extending a LinearState
|
||||
*/
|
||||
@Test
|
||||
fun `select by composite primary key on LinearStates`() {
|
||||
database.transaction {
|
||||
services.fillWithSomeTestLinearStates(10)
|
||||
}
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(VaultSchemaV1.VaultStates::class.java)
|
||||
|
||||
val vaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultLinearStates = criteriaQuery.from(VaultSchemaV1.VaultLinearStates::class.java)
|
||||
|
||||
criteriaQuery.select(vaultStates)
|
||||
criteriaQuery.where(criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), vaultLinearStates.get<PersistentStateRef>("stateRef")))
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
assertThat(queryResults).hasSize(10)
|
||||
}
|
||||
|
||||
/**
|
||||
* VaultFungibleState is an abstract entity, which should be extended any Contract extending a FungibleAsset
|
||||
*/
|
||||
|
||||
/**
|
||||
* CashSchemaV1 = original Cash schema (extending PersistentState)
|
||||
*/
|
||||
@Test
|
||||
fun `count CashStates`() {
|
||||
// structure query
|
||||
val countQuery = criteriaBuilder.createQuery(Long::class.java)
|
||||
countQuery.select(criteriaBuilder.count(countQuery.from(CashSchemaV1.PersistentCashState::class.java)))
|
||||
|
||||
// execute query
|
||||
val countResult = entityManager.createQuery(countQuery).singleResult
|
||||
|
||||
assertThat(countResult).isEqualTo(10)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `select by composite primary key on CashStates`() {
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
vaultStates.join<VaultSchemaV1.VaultStates, CashSchemaV1.PersistentCashState>("stateRef")
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
assertThat(queryResults).hasSize(10)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `select and join by composite primary key on CashStates`() {
|
||||
database.transaction {
|
||||
services.fillWithSomeTestLinearStates(5)
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultCashStates = criteriaQuery.from(CashSchemaV1.PersistentCashState::class.java)
|
||||
|
||||
criteriaQuery.select(vaultStates)
|
||||
criteriaQuery.where(criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), vaultCashStates.get<PersistentStateRef>("stateRef")))
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
assertThat(queryResults).hasSize(10)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* CashSchemaV2 = optimised Cash schema (extending FungibleState)
|
||||
*/
|
||||
@Test
|
||||
fun `count CashStates in V2`() {
|
||||
database.transaction {
|
||||
// persist cash states explicitly with V2 schema
|
||||
cashStates.forEach {
|
||||
val cashState = it.state.data
|
||||
val dummyFungibleState = DummyFungibleContract.State(cashState.amount, cashState.owner)
|
||||
hibernatePersister.persistStateWithSchema(dummyFungibleState, it.ref, SampleCashSchemaV2)
|
||||
}
|
||||
}
|
||||
|
||||
// structure query
|
||||
val countQuery = criteriaBuilder.createQuery(Long::class.java)
|
||||
countQuery.select(criteriaBuilder.count(countQuery.from(SampleCashSchemaV2.PersistentCashState::class.java)))
|
||||
|
||||
// execute query
|
||||
val countResult = entityManager.createQuery(countQuery).singleResult
|
||||
|
||||
assertThat(countResult).isEqualTo(10)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `select by composite primary key on CashStates in V2`() {
|
||||
database.transaction {
|
||||
services.fillWithSomeTestLinearStates(5)
|
||||
|
||||
// persist cash states explicitly with V2 schema
|
||||
cashStates.forEach {
|
||||
val cashState = it.state.data
|
||||
val dummyFungibleState = DummyFungibleContract.State(cashState.amount, cashState.owner)
|
||||
hibernatePersister.persistStateWithSchema(dummyFungibleState, it.ref, SampleCashSchemaV2)
|
||||
}
|
||||
}
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultCashStates = criteriaQuery.from(SampleCashSchemaV2.PersistentCashState::class.java)
|
||||
|
||||
criteriaQuery.select(vaultStates)
|
||||
criteriaQuery.where(criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), vaultCashStates.get<PersistentStateRef>("stateRef")))
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
assertThat(queryResults).hasSize(10)
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a 3-way join between:
|
||||
* - VaultStates
|
||||
* - VaultLinearStates
|
||||
* - a concrete LinearState implementation (eg. DummyLinearState)
|
||||
*/
|
||||
|
||||
/**
|
||||
* DummyLinearStateV1 = original DummyLinearState schema (extending PersistentState)
|
||||
*/
|
||||
@Test
|
||||
fun `select by composite primary between VaultStates, VaultLinearStates and DummyLinearStates`() {
|
||||
database.transaction {
|
||||
services.fillWithSomeTestLinearStates(8)
|
||||
services.fillWithSomeTestDeals(listOf("123", "456", "789"))
|
||||
services.fillWithSomeTestLinearStates(2)
|
||||
}
|
||||
|
||||
val sessionFactory = hibernateConfig.sessionFactoryForSchemas(VaultSchemaV1, DummyLinearStateSchemaV1)
|
||||
val criteriaBuilder = sessionFactory.criteriaBuilder
|
||||
val entityManager = sessionFactory.createEntityManager()
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultLinearStates = criteriaQuery.from(VaultSchemaV1.VaultLinearStates::class.java)
|
||||
val dummyLinearStates = criteriaQuery.from(DummyLinearStateSchemaV1.PersistentDummyLinearState::class.java)
|
||||
|
||||
criteriaQuery.select(vaultStates)
|
||||
val joinPredicate1 = criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), vaultLinearStates.get<PersistentStateRef>("stateRef"))
|
||||
val joinPredicate2 = criteriaBuilder.and(criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), dummyLinearStates.get<PersistentStateRef>("stateRef")))
|
||||
criteriaQuery.where(joinPredicate1, joinPredicate2)
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
assertThat(queryResults).hasSize(10)
|
||||
}
|
||||
|
||||
/**
|
||||
* DummyLinearSchemaV2 = optimised DummyLinear schema (extending LinearState)
|
||||
*/
|
||||
|
||||
@Test
|
||||
fun `three way join by composite primary between VaultStates, VaultLinearStates and DummyLinearStates`() {
|
||||
database.transaction {
|
||||
services.fillWithSomeTestLinearStates(8)
|
||||
services.fillWithSomeTestDeals(listOf("123", "456", "789"))
|
||||
services.fillWithSomeTestLinearStates(2)
|
||||
}
|
||||
|
||||
val sessionFactory = hibernateConfig.sessionFactoryForSchemas(VaultSchemaV1, DummyLinearStateSchemaV2)
|
||||
val criteriaBuilder = sessionFactory.criteriaBuilder
|
||||
val entityManager = sessionFactory.createEntityManager()
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultLinearStates = criteriaQuery.from(VaultSchemaV1.VaultLinearStates::class.java)
|
||||
val dummyLinearStates = criteriaQuery.from(DummyLinearStateSchemaV2.PersistentDummyLinearState::class.java)
|
||||
|
||||
criteriaQuery.select(vaultStates)
|
||||
val joinPredicate1 = criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), vaultLinearStates.get<PersistentStateRef>("stateRef"))
|
||||
val joinPredicate2 = criteriaBuilder.and(criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), dummyLinearStates.get<PersistentStateRef>("stateRef")))
|
||||
criteriaQuery.where(joinPredicate1, joinPredicate2)
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
assertThat(queryResults).hasSize(10)
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a OneToOne table mapping
|
||||
*/
|
||||
@Test
|
||||
fun `select fungible states by owner party`() {
|
||||
database.transaction {
|
||||
// persist original cash states explicitly with V3 schema
|
||||
cashStates.forEach {
|
||||
val cashState = it.state.data
|
||||
val dummyFungibleState = DummyFungibleContract.State(cashState.amount, cashState.owner)
|
||||
hibernatePersister.persistStateWithSchema(dummyFungibleState, it.ref, SampleCashSchemaV3)
|
||||
}
|
||||
}
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(SampleCashSchemaV3.PersistentCashState::class.java)
|
||||
criteriaQuery.from(SampleCashSchemaV3.PersistentCashState::class.java)
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
assertThat(queryResults).hasSize(10)
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Query by Party (OneToOne table mapping)
|
||||
*/
|
||||
@Test
|
||||
fun `query fungible states by owner party`() {
|
||||
database.transaction {
|
||||
// persist original cash states explicitly with V3 schema
|
||||
cashStates.forEach {
|
||||
val cashState = it.state.data
|
||||
val dummyFungibleState = DummyFungibleContract.State(cashState.amount, cashState.owner)
|
||||
hibernatePersister.persistStateWithSchema(dummyFungibleState, it.ref, SampleCashSchemaV3)
|
||||
}
|
||||
|
||||
services.fillWithSomeTestCash(100.DOLLARS, DUMMY_NOTARY, 2, 2, Random(0L), ownedBy = ALICE)
|
||||
val cashStates = services.fillWithSomeTestCash(100.DOLLARS, DUMMY_NOTARY, 2, 2, Random(0L),
|
||||
issuedBy = BOB.ref(0), issuerKey = BOB_KEY, ownedBy = (BOB)).states
|
||||
// persist additional cash states explicitly with V3 schema
|
||||
cashStates.forEach {
|
||||
val cashState = it.state.data
|
||||
val dummyFungibleState = DummyFungibleContract.State(cashState.amount, cashState.owner)
|
||||
hibernatePersister.persistStateWithSchema(dummyFungibleState, it.ref, SampleCashSchemaV3)
|
||||
}
|
||||
}
|
||||
|
||||
val sessionFactory = hibernateConfig.sessionFactoryForSchemas(VaultSchemaV1, CommonSchemaV1, SampleCashSchemaV3)
|
||||
val criteriaBuilder = sessionFactory.criteriaBuilder
|
||||
val entityManager = sessionFactory.createEntityManager()
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(VaultSchemaV1.VaultStates::class.java)
|
||||
|
||||
// select
|
||||
val vaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
criteriaQuery.select(vaultStates)
|
||||
|
||||
// search predicate
|
||||
val cashStatesSchema = criteriaQuery.from(SampleCashSchemaV3.PersistentCashState::class.java)
|
||||
|
||||
val joinCashToParty = cashStatesSchema.join<SampleCashSchemaV3.PersistentCashState,CommonSchemaV1.Party>("owner")
|
||||
val queryOwnerKey = BOB_PUBKEY.toBase58String()
|
||||
criteriaQuery.where(criteriaBuilder.equal(joinCashToParty.get<CommonSchemaV1.Party>("key"), queryOwnerKey))
|
||||
|
||||
val joinVaultStatesToCash = criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), cashStatesSchema.get<PersistentStateRef>("stateRef"))
|
||||
criteriaQuery.where(joinVaultStatesToCash)
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
|
||||
queryResults.forEach {
|
||||
val contractState = it.contractState.deserialize<TransactionState<ContractState>>(storageKryo())
|
||||
val cashState = contractState.data as Cash.State
|
||||
println("${it.stateRef} with owner: ${cashState.owner.owningKey.toBase58String()}") }
|
||||
|
||||
assertThat(queryResults).hasSize(12)
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a OneToMany table mapping
|
||||
*/
|
||||
@Test
|
||||
fun `select fungible states by participants`() {
|
||||
database.transaction {
|
||||
// persist cash states explicitly with V2 schema
|
||||
cashStates.forEach {
|
||||
val cashState = it.state.data
|
||||
val dummyFungibleState = DummyFungibleContract.State(cashState.amount, cashState.owner)
|
||||
hibernatePersister.persistStateWithSchema(dummyFungibleState, it.ref, SampleCashSchemaV3)
|
||||
}
|
||||
}
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(SampleCashSchemaV3.PersistentCashState::class.java)
|
||||
criteriaQuery.from(SampleCashSchemaV3.PersistentCashState::class.java)
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
|
||||
assertThat(queryResults).hasSize(10)
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Query by participants (OneToMany table mapping)
|
||||
*/
|
||||
@Test
|
||||
fun `query fungible states by participants`() {
|
||||
val firstCashState =
|
||||
database.transaction {
|
||||
// persist original cash states explicitly with V3 schema
|
||||
cashStates.forEach {
|
||||
val cashState = it.state.data
|
||||
val dummyFungibleState = DummyFungibleContract.State(cashState.amount, cashState.owner)
|
||||
hibernatePersister.persistStateWithSchema(dummyFungibleState, it.ref, SampleCashSchemaV3)
|
||||
}
|
||||
|
||||
val moreCash = services.fillWithSomeTestCash(100.DOLLARS, DUMMY_NOTARY, 2, 2, Random(0L),
|
||||
issuedBy = BOB.ref(0), issuerKey = BOB_KEY, ownedBy = BOB).states
|
||||
// persist additional cash states explicitly with V3 schema
|
||||
moreCash.forEach {
|
||||
val cashState = it.state.data
|
||||
val dummyFungibleState = DummyFungibleContract.State(cashState.amount, cashState.owner)
|
||||
hibernatePersister.persistStateWithSchema(dummyFungibleState, it.ref, SampleCashSchemaV3)
|
||||
}
|
||||
|
||||
val cashStates = services.fillWithSomeTestCash(100.DOLLARS, DUMMY_NOTARY, 2, 2, Random(0L), ownedBy = (ALICE)).states
|
||||
// persist additional cash states explicitly with V3 schema
|
||||
cashStates.forEach {
|
||||
val cashState = it.state.data
|
||||
val dummyFungibleState = DummyFungibleContract.State(cashState.amount, cashState.owner)
|
||||
hibernatePersister.persistStateWithSchema(dummyFungibleState, it.ref, SampleCashSchemaV3)
|
||||
}
|
||||
cashStates.first()
|
||||
}
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(VaultSchemaV1.VaultStates::class.java)
|
||||
|
||||
// select
|
||||
val vaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
criteriaQuery.select(vaultStates)
|
||||
|
||||
// search predicate
|
||||
val cashStatesSchema = criteriaQuery.from(SampleCashSchemaV3.PersistentCashState::class.java)
|
||||
|
||||
val joinCashToParty = cashStatesSchema.join<SampleCashSchemaV3.PersistentCashState, CommonSchemaV1.Party>("participants")
|
||||
val queryParticipantKeys = firstCashState.state.data.participants.map { it.owningKey.toBase58String() }
|
||||
criteriaQuery.where(criteriaBuilder.equal(joinCashToParty.get<CommonSchemaV1.Party>("key"), queryParticipantKeys))
|
||||
|
||||
val joinVaultStatesToCash = criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), cashStatesSchema.get<PersistentStateRef>("stateRef"))
|
||||
criteriaQuery.where(joinVaultStatesToCash)
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
queryResults.forEach {
|
||||
val contractState = it.contractState.deserialize<TransactionState<ContractState>>(storageKryo())
|
||||
val cashState = contractState.data as Cash.State
|
||||
println("${it.stateRef} with owner ${cashState.owner.owningKey.toBase58String()} and participants ${cashState.participants.map { it.owningKey.toBase58String() }}")
|
||||
}
|
||||
|
||||
assertThat(queryResults).hasSize(12)
|
||||
}
|
||||
|
||||
/**
|
||||
* Query with sorting on Common table attribute
|
||||
*/
|
||||
@Test
|
||||
fun `with sorting on attribute from common table`() {
|
||||
|
||||
database.transaction {
|
||||
services.fillWithSomeTestLinearStates(1, externalId = "111")
|
||||
services.fillWithSomeTestLinearStates(2, externalId = "222")
|
||||
services.fillWithSomeTestLinearStates(3, externalId = "333")
|
||||
}
|
||||
|
||||
val sessionFactory = hibernateConfig.sessionFactoryForSchemas(VaultSchemaV1, DummyLinearStateSchemaV2)
|
||||
val criteriaBuilder = sessionFactory.criteriaBuilder
|
||||
val entityManager = sessionFactory.createEntityManager()
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(Tuple::class.java)
|
||||
val vaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultLinearStates = criteriaQuery.from(VaultSchemaV1.VaultLinearStates::class.java)
|
||||
|
||||
// join
|
||||
criteriaQuery.multiselect(vaultStates, vaultLinearStates)
|
||||
val joinPredicate = criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), vaultLinearStates.get<PersistentStateRef>("stateRef"))
|
||||
criteriaQuery.where(joinPredicate)
|
||||
|
||||
// order by DESC
|
||||
criteriaQuery.orderBy(criteriaBuilder.desc(vaultLinearStates.get<String>("externalId")))
|
||||
criteriaQuery.orderBy(criteriaBuilder.desc(vaultLinearStates.get<UUID>("uuid")))
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
queryResults.map {
|
||||
val vaultState = it[0] as VaultSchemaV1.VaultStates
|
||||
val vaultLinearState = it[1] as VaultSchemaV1.VaultLinearStates
|
||||
println("${vaultState.stateRef} : ${vaultLinearState.externalId} ${vaultLinearState.uuid}")
|
||||
}
|
||||
|
||||
// order by ASC
|
||||
criteriaQuery.orderBy(criteriaBuilder.asc(vaultLinearStates.get<String>("externalId")))
|
||||
criteriaQuery.orderBy(criteriaBuilder.asc(vaultLinearStates.get<UUID>("uuid")))
|
||||
|
||||
// execute query
|
||||
val queryResultsAsc = entityManager.createQuery(criteriaQuery).resultList
|
||||
queryResultsAsc.map {
|
||||
val vaultState = it[0] as VaultSchemaV1.VaultStates
|
||||
val vaultLinearState = it[1] as VaultSchemaV1.VaultLinearStates
|
||||
println("${vaultState.stateRef} : ${vaultLinearState.externalId} ${vaultLinearState.uuid}")
|
||||
}
|
||||
|
||||
assertThat(queryResults).hasSize(6)
|
||||
}
|
||||
|
||||
/**
|
||||
* Query with sorting on Custom table attribute
|
||||
*/
|
||||
@Test
|
||||
fun `with sorting on attribute from custom table`() {
|
||||
|
||||
database.transaction {
|
||||
services.fillWithSomeTestLinearStates(1, externalId = "111")
|
||||
services.fillWithSomeTestLinearStates(2, externalId = "222")
|
||||
services.fillWithSomeTestLinearStates(3, externalId = "333")
|
||||
}
|
||||
|
||||
val sessionFactory = hibernateConfig.sessionFactoryForSchemas(VaultSchemaV1, DummyLinearStateSchemaV1)
|
||||
val criteriaBuilder = sessionFactory.criteriaBuilder
|
||||
val entityManager = sessionFactory.createEntityManager()
|
||||
|
||||
// structure query
|
||||
val criteriaQuery = criteriaBuilder.createQuery(Tuple::class.java)
|
||||
val vaultStates = criteriaQuery.from(VaultSchemaV1.VaultStates::class.java)
|
||||
val vaultLinearStates = criteriaQuery.from(VaultSchemaV1.VaultLinearStates::class.java)
|
||||
val dummyLinearStates = criteriaQuery.from(DummyLinearStateSchemaV1.PersistentDummyLinearState::class.java)
|
||||
|
||||
// join
|
||||
criteriaQuery.multiselect(vaultStates, vaultLinearStates, dummyLinearStates)
|
||||
val joinPredicate1 = criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), vaultLinearStates.get<PersistentStateRef>("stateRef"))
|
||||
val joinPredicate2 = criteriaBuilder.and(criteriaBuilder.equal(vaultStates.get<PersistentStateRef>("stateRef"), dummyLinearStates.get<PersistentStateRef>("stateRef")))
|
||||
criteriaQuery.where(joinPredicate1, joinPredicate2)
|
||||
|
||||
// order by DESC
|
||||
criteriaQuery.orderBy(criteriaBuilder.desc(dummyLinearStates.get<String>("externalId")))
|
||||
criteriaQuery.orderBy(criteriaBuilder.desc(dummyLinearStates.get<UUID>("uuid")))
|
||||
|
||||
// execute query
|
||||
val queryResults = entityManager.createQuery(criteriaQuery).resultList
|
||||
queryResults.map {
|
||||
val vaultState = it[0] as VaultSchemaV1.VaultStates
|
||||
val _vaultLinearStates = it[1] as VaultSchemaV1.VaultLinearStates
|
||||
val _dummyLinearStates = it[2] as DummyLinearStateSchemaV1.PersistentDummyLinearState
|
||||
println("${vaultState.stateRef} : [${_dummyLinearStates.externalId} ${_dummyLinearStates.uuid}] : [${_vaultLinearStates.externalId} ${_vaultLinearStates.uuid}]")
|
||||
}
|
||||
|
||||
// order by ASC
|
||||
criteriaQuery.orderBy(criteriaBuilder.asc(dummyLinearStates.get<String>("externalId")))
|
||||
criteriaQuery.orderBy(criteriaBuilder.asc(dummyLinearStates.get<UUID>("uuid")))
|
||||
|
||||
// execute query
|
||||
val queryResultsAsc = entityManager.createQuery(criteriaQuery).resultList
|
||||
queryResultsAsc.map {
|
||||
val vaultState = it[0] as VaultSchemaV1.VaultStates
|
||||
val _vaultLinearStates = it[1] as VaultSchemaV1.VaultLinearStates
|
||||
val _dummyLinearStates = it[2] as DummyLinearStateSchemaV1.PersistentDummyLinearState
|
||||
println("${vaultState.stateRef} : [${_dummyLinearStates.externalId} ${_dummyLinearStates.uuid}] : [${_vaultLinearStates.externalId} ${_vaultLinearStates.uuid}]")
|
||||
}
|
||||
|
||||
assertThat(queryResults).hasSize(6)
|
||||
}
|
||||
|
||||
}
|
@ -19,10 +19,10 @@ import net.corda.core.transactions.WireTransaction
|
||||
import net.corda.core.utilities.DUMMY_NOTARY
|
||||
import net.corda.core.utilities.DUMMY_PUBKEY_1
|
||||
import net.corda.node.services.persistence.DBTransactionStorage
|
||||
import net.corda.node.services.vault.schemas.Models
|
||||
import net.corda.node.services.vault.schemas.VaultCashBalancesEntity
|
||||
import net.corda.node.services.vault.schemas.VaultSchema
|
||||
import net.corda.node.services.vault.schemas.VaultStatesEntity
|
||||
import net.corda.node.services.vault.schemas.requery.Models
|
||||
import net.corda.node.services.vault.schemas.requery.VaultCashBalancesEntity
|
||||
import net.corda.node.services.vault.schemas.requery.VaultSchema
|
||||
import net.corda.node.services.vault.schemas.requery.VaultStatesEntity
|
||||
import net.corda.node.utilities.configureDatabase
|
||||
import net.corda.node.utilities.transaction
|
||||
import net.corda.testing.node.makeTestDataSourceProperties
|
||||
@ -30,6 +30,7 @@ import org.assertj.core.api.Assertions
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import java.io.Closeable
|
||||
@ -123,6 +124,55 @@ class RequeryConfigurationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `bounded iteration`() {
|
||||
// insert 100 entities
|
||||
database.transaction {
|
||||
requerySession.withTransaction {
|
||||
(1..100)
|
||||
.map { newTransaction(it) }
|
||||
.forEach { insert(createVaultStateEntity(it)) }
|
||||
}
|
||||
}
|
||||
|
||||
// query entities 41..45
|
||||
database.transaction {
|
||||
requerySession.withTransaction {
|
||||
// Note: cannot specify a limit explicitly when using iterator skip & take
|
||||
val query = select(VaultSchema.VaultStates::class)
|
||||
val count = query.get().count()
|
||||
Assertions.assertThat(count).isEqualTo(100)
|
||||
val result = query.get().iterator(40, 5)
|
||||
Assertions.assertThat(result.asSequence().count()).isEqualTo(5)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test calling an arbitrary JDBC native query`() {
|
||||
val txn = newTransaction()
|
||||
|
||||
database.transaction {
|
||||
transactionStorage.addTransaction(txn)
|
||||
requerySession.withTransaction {
|
||||
insert(createVaultStateEntity(txn))
|
||||
}
|
||||
}
|
||||
|
||||
val dataSourceProperties = makeTestDataSourceProperties()
|
||||
val nativeQuery = "SELECT v.transaction_id, v.output_index FROM vault_states v WHERE v.state_status = 0"
|
||||
|
||||
database.transaction {
|
||||
val configuration = RequeryConfiguration(dataSourceProperties, true)
|
||||
val jdbcSession = configuration.jdbcSession()
|
||||
val prepStatement = jdbcSession.prepareStatement(nativeQuery)
|
||||
val rs = prepStatement.executeQuery()
|
||||
assertTrue(rs.next())
|
||||
assertEquals(rs.getString(1), txn.tx.inputs[0].txhash.toString())
|
||||
assertEquals(rs.getInt(2), txn.tx.inputs[0].index)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createVaultStateEntity(txn: SignedTransaction): VaultStatesEntity {
|
||||
val txnState = txn.tx.inputs[0]
|
||||
val state = VaultStatesEntity().apply {
|
||||
@ -158,9 +208,9 @@ class RequeryConfigurationTest {
|
||||
}
|
||||
}
|
||||
|
||||
private fun newTransaction(): SignedTransaction {
|
||||
private fun newTransaction(index: Int = 0): SignedTransaction {
|
||||
val wtx = WireTransaction(
|
||||
inputs = listOf(StateRef(SecureHash.randomSHA256(), 0)),
|
||||
inputs = listOf(StateRef(SecureHash.randomSHA256(), index)),
|
||||
attachments = emptyList(),
|
||||
outputs = emptyList(),
|
||||
commands = emptyList(),
|
||||
|
@ -22,10 +22,10 @@ import net.corda.node.utilities.AffinityExecutor.ServiceAffinityExecutor
|
||||
import net.corda.node.utilities.configureDatabase
|
||||
import net.corda.node.utilities.transaction
|
||||
import net.corda.testing.MOCK_VERSION_INFO
|
||||
import net.corda.testing.TestNodeConfiguration
|
||||
import net.corda.testing.freeLocalHostAndPort
|
||||
import net.corda.testing.freePort
|
||||
import net.corda.testing.node.makeTestDataSourceProperties
|
||||
import net.corda.testing.testNodeConfiguration
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.Assertions.assertThatThrownBy
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
@ -70,10 +70,9 @@ class ArtemisMessagingTests {
|
||||
fun setUp() {
|
||||
val baseDirectory = temporaryFolder.root.toPath()
|
||||
userService = RPCUserServiceImpl(emptyList())
|
||||
config = TestNodeConfiguration(
|
||||
config = testNodeConfiguration(
|
||||
baseDirectory = baseDirectory,
|
||||
myLegalName = ALICE.name,
|
||||
networkMapService = null)
|
||||
myLegalName = ALICE.name)
|
||||
LogHelper.setLevel(PersistentUniquenessProvider::class)
|
||||
val dataSourceAndDatabase = configureDatabase(makeTestDataSourceProperties())
|
||||
dataSource = dataSourceAndDatabase.first
|
||||
|
@ -11,7 +11,7 @@ import net.corda.core.utilities.LogHelper
|
||||
import net.corda.core.write
|
||||
import net.corda.core.writeLines
|
||||
import net.corda.node.services.database.RequeryConfiguration
|
||||
import net.corda.node.services.persistence.schemas.AttachmentEntity
|
||||
import net.corda.node.services.persistence.schemas.requery.AttachmentEntity
|
||||
import net.corda.node.services.transactions.PersistentUniquenessProvider
|
||||
import net.corda.node.utilities.configureDatabase
|
||||
import net.corda.node.utilities.transaction
|
||||
|
@ -1,9 +1,6 @@
|
||||
package net.corda.node.services.schema
|
||||
|
||||
import net.corda.core.contracts.Contract
|
||||
import net.corda.core.contracts.StateAndRef
|
||||
import net.corda.core.contracts.StateRef
|
||||
import net.corda.core.contracts.TransactionState
|
||||
import net.corda.core.contracts.*
|
||||
import net.corda.core.crypto.CompositeKey
|
||||
import net.corda.core.crypto.SecureHash
|
||||
import net.corda.core.identity.AbstractParty
|
||||
@ -13,6 +10,7 @@ import net.corda.core.schemas.PersistentState
|
||||
import net.corda.core.schemas.QueryableState
|
||||
import net.corda.core.utilities.LogHelper
|
||||
import net.corda.node.services.api.SchemaService
|
||||
import net.corda.node.services.database.HibernateConfiguration
|
||||
import net.corda.node.services.schema.HibernateObserver
|
||||
import net.corda.node.utilities.configureDatabase
|
||||
import net.corda.node.utilities.transaction
|
||||
@ -99,9 +97,9 @@ class HibernateObserverTests {
|
||||
val schemaService = object : SchemaService {
|
||||
override val schemaOptions: Map<MappedSchema, SchemaService.SchemaOptions> = emptyMap()
|
||||
|
||||
override fun selectSchemas(state: QueryableState): Iterable<MappedSchema> = setOf(testSchema)
|
||||
override fun selectSchemas(state: ContractState): Iterable<MappedSchema> = setOf(testSchema)
|
||||
|
||||
override fun generateMappedObject(state: QueryableState, schema: MappedSchema): PersistentState {
|
||||
override fun generateMappedObject(state: ContractState, schema: MappedSchema): PersistentState {
|
||||
val parent = Parent()
|
||||
parent.children.add(Child())
|
||||
parent.children.add(Child())
|
||||
@ -110,14 +108,14 @@ class HibernateObserverTests {
|
||||
}
|
||||
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
val observer = HibernateObserver(rawUpdatesPublisher, schemaService)
|
||||
val observer = HibernateObserver(rawUpdatesPublisher, HibernateConfiguration(schemaService))
|
||||
database.transaction {
|
||||
rawUpdatesPublisher.onNext(Vault.Update(emptySet(), setOf(StateAndRef(TransactionState(TestState(), MEGA_CORP), StateRef(SecureHash.sha256("dummy"), 0)))))
|
||||
val parentRowCountResult = TransactionManager.current().connection.prepareStatement("select count(*) from contract_Parents").executeQuery()
|
||||
val parentRowCountResult = TransactionManager.current().connection.prepareStatement("select count(*) from Parents").executeQuery()
|
||||
parentRowCountResult.next()
|
||||
val parentRows = parentRowCountResult.getInt(1)
|
||||
parentRowCountResult.close()
|
||||
val childrenRowCountResult = TransactionManager.current().connection.prepareStatement("select count(*) from contract_Children").executeQuery()
|
||||
val childrenRowCountResult = TransactionManager.current().connection.prepareStatement("select count(*) from Children").executeQuery()
|
||||
childrenRowCountResult.next()
|
||||
val childrenRows = childrenRowCountResult.getInt(1)
|
||||
childrenRowCountResult.close()
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,5 +1,6 @@
|
||||
package net.corda.node.services.vault
|
||||
|
||||
import net.corda.contracts.DummyDealContract
|
||||
import net.corda.contracts.asset.Cash
|
||||
import net.corda.contracts.asset.DUMMY_CASH_ISSUER
|
||||
import net.corda.contracts.testing.*
|
||||
|
@ -7,8 +7,8 @@ import net.corda.core.crypto.*
|
||||
import net.corda.core.exists
|
||||
import net.corda.core.toTypedArray
|
||||
import net.corda.core.utilities.ALICE
|
||||
import net.corda.testing.TestNodeConfiguration
|
||||
import net.corda.testing.getTestX509Name
|
||||
import net.corda.testing.testNodeConfiguration
|
||||
import org.bouncycastle.cert.X509CertificateHolder
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
@ -38,10 +38,9 @@ class NetworkRegistrationHelperTest {
|
||||
on { retrieveCertificates(eq(id)) }.then { certs }
|
||||
}
|
||||
|
||||
val config = TestNodeConfiguration(
|
||||
val config = testNodeConfiguration(
|
||||
baseDirectory = tempFolder.root.toPath(),
|
||||
myLegalName = ALICE.name,
|
||||
networkMapService = null)
|
||||
myLegalName = ALICE.name)
|
||||
|
||||
assertFalse(config.nodeKeystore.exists())
|
||||
assertFalse(config.sslKeystore.exists())
|
||||
|
@ -112,3 +112,7 @@ publishing {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
jar {
|
||||
from sourceSets.test.output
|
||||
}
|
@ -27,6 +27,7 @@ import net.corda.core.transactions.FilteredTransaction
|
||||
import net.corda.core.utilities.ProgressTracker
|
||||
import net.corda.core.utilities.unwrap
|
||||
import net.corda.irs.flows.RatesFixFlow
|
||||
import org.apache.commons.io.IOUtils
|
||||
import java.math.BigDecimal
|
||||
import java.security.PublicKey
|
||||
import java.time.LocalDate
|
||||
@ -90,7 +91,12 @@ object NodeInterestRates {
|
||||
services.myInfo.serviceIdentities(type).first(),
|
||||
services.myInfo.serviceIdentities(type).first().owningKey.keys.first { services.keyManagementService.keys.contains(it) },
|
||||
services
|
||||
)
|
||||
) {
|
||||
// Set some default fixes to the Oracle, so we can smoothly run the IRS Demo without uploading fixes.
|
||||
// This is required to avoid a situation where the runnodes version of the demo isn't in a good state
|
||||
// upon startup.
|
||||
addDefaultFixes()
|
||||
}
|
||||
// DOCEND 3
|
||||
|
||||
companion object {
|
||||
@ -179,6 +185,10 @@ object NodeInterestRates {
|
||||
fun uploadFixes(s: String) {
|
||||
knownFixes = parseFile(s)
|
||||
}
|
||||
|
||||
private fun addDefaultFixes() {
|
||||
knownFixes = parseFile(IOUtils.toString(Thread.currentThread().contextClassLoader.getResourceAsStream("net/corda/irs/simulation/example.rates.txt"), Charsets.UTF_8.name()))
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: can we split into two? Fix not available (retryable/transient) and unknown (permanent)
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"fixedLeg": {
|
||||
"fixedRatePayer": "8Kqd4oWdx4KQAVcA8RDJXNvzFMvBkqWTZPhRtg9dSpNs6T6eZ4cGJWA7FWK",
|
||||
"fixedRatePayer": "8Kqd4oWdx4KQGHGR7xcgpFf9JmP6HiXqTf85NpSgdSu431EGEhujA6ePaFD",
|
||||
"notional": "$25000000",
|
||||
"paymentFrequency": "SemiAnnual",
|
||||
"effectiveDate": "2016-03-11",
|
||||
@ -22,7 +22,7 @@
|
||||
"interestPeriodAdjustment": "Adjusted"
|
||||
},
|
||||
"floatingLeg": {
|
||||
"floatingRatePayer": "8Kqd4oWdx4KQAVc3Si48msuQrMJPGpA3TnGGuWTqXyoshjL25wzzdxtGyjq",
|
||||
"floatingRatePayer": "8Kqd4oWdx4KQGHGJSFTX4kdZukmHohBRN3gvPekticL4eHTdmbJTVZNZJUj",
|
||||
"notional": {
|
||||
"quantity": 2500000000,
|
||||
"token": "USD"
|
||||
|
@ -20,11 +20,11 @@ import net.corda.node.services.network.NetworkMapService
|
||||
import net.corda.node.services.statemachine.StateMachineManager
|
||||
import net.corda.node.services.transactions.SimpleNotaryService
|
||||
import net.corda.node.utilities.transaction
|
||||
import net.corda.testing.TestNodeConfiguration
|
||||
import net.corda.testing.node.InMemoryMessagingNetwork
|
||||
import net.corda.testing.node.MockNetwork
|
||||
import net.corda.testing.node.TestClock
|
||||
import net.corda.testing.node.setTo
|
||||
import net.corda.testing.testNodeConfiguration
|
||||
import org.bouncycastle.asn1.x500.X500Name
|
||||
import rx.Observable
|
||||
import rx.subjects.PublishSubject
|
||||
@ -71,10 +71,9 @@ abstract class Simulation(val networkSendManuallyPumped: Boolean,
|
||||
val letter = 'A' + counter
|
||||
val (city, country) = bankLocations[counter++ % bankLocations.size]
|
||||
|
||||
val cfg = TestNodeConfiguration(
|
||||
val cfg = testNodeConfiguration(
|
||||
baseDirectory = config.baseDirectory,
|
||||
myLegalName = X500Name("CN=Bank $letter,O=Bank $letter,L=$city,C=$country"),
|
||||
networkMapService = null)
|
||||
myLegalName = X500Name("CN=Bank $letter,O=Bank $letter,L=$city,C=$country"))
|
||||
return SimulatedNode(cfg, network, networkMapAddr, advertisedServices, id, overrideServices, entropyRoot)
|
||||
}
|
||||
|
||||
@ -93,10 +92,9 @@ abstract class Simulation(val networkSendManuallyPumped: Boolean,
|
||||
advertisedServices: Set<ServiceInfo>, id: Int, overrideServices: Map<ServiceInfo, KeyPair>?,
|
||||
entropyRoot: BigInteger): MockNetwork.MockNode {
|
||||
require(advertisedServices.containsType(NetworkMapService.type))
|
||||
val cfg = TestNodeConfiguration(
|
||||
val cfg = testNodeConfiguration(
|
||||
baseDirectory = config.baseDirectory,
|
||||
myLegalName = DUMMY_MAP.name,
|
||||
networkMapService = null)
|
||||
myLegalName = DUMMY_MAP.name)
|
||||
return object : SimulatedNode(cfg, network, networkMapAddr, advertisedServices, id, overrideServices, entropyRoot) {}
|
||||
}
|
||||
}
|
||||
@ -106,10 +104,9 @@ abstract class Simulation(val networkSendManuallyPumped: Boolean,
|
||||
advertisedServices: Set<ServiceInfo>, id: Int, overrideServices: Map<ServiceInfo, KeyPair>?,
|
||||
entropyRoot: BigInteger): MockNetwork.MockNode {
|
||||
require(advertisedServices.containsType(SimpleNotaryService.type))
|
||||
val cfg = TestNodeConfiguration(
|
||||
val cfg = testNodeConfiguration(
|
||||
baseDirectory = config.baseDirectory,
|
||||
myLegalName = DUMMY_NOTARY.name,
|
||||
networkMapService = null)
|
||||
myLegalName = DUMMY_NOTARY.name)
|
||||
return SimulatedNode(cfg, network, networkMapAddr, advertisedServices, id, overrideServices, entropyRoot)
|
||||
}
|
||||
}
|
||||
@ -122,10 +119,9 @@ abstract class Simulation(val networkSendManuallyPumped: Boolean,
|
||||
advertisedServices: Set<ServiceInfo>, id: Int, overrideServices: Map<ServiceInfo, KeyPair>?,
|
||||
entropyRoot: BigInteger): MockNetwork.MockNode {
|
||||
require(advertisedServices.containsType(NodeInterestRates.Oracle.type))
|
||||
val cfg = TestNodeConfiguration(
|
||||
val cfg = testNodeConfiguration(
|
||||
baseDirectory = config.baseDirectory,
|
||||
myLegalName = RATES_SERVICE_NAME,
|
||||
networkMapService = null)
|
||||
myLegalName = RATES_SERVICE_NAME)
|
||||
return object : SimulatedNode(cfg, network, networkMapAddr, advertisedServices, id, overrideServices, entropyRoot) {
|
||||
override fun start(): MockNetwork.MockNode {
|
||||
super.start()
|
||||
@ -146,10 +142,9 @@ abstract class Simulation(val networkSendManuallyPumped: Boolean,
|
||||
override fun create(config: NodeConfiguration, network: MockNetwork, networkMapAddr: SingleMessageRecipient?,
|
||||
advertisedServices: Set<ServiceInfo>, id: Int, overrideServices: Map<ServiceInfo, KeyPair>?,
|
||||
entropyRoot: BigInteger): MockNetwork.MockNode {
|
||||
val cfg = TestNodeConfiguration(
|
||||
val cfg = testNodeConfiguration(
|
||||
baseDirectory = config.baseDirectory,
|
||||
myLegalName = DUMMY_REGULATOR.name,
|
||||
networkMapService = null)
|
||||
myLegalName = DUMMY_REGULATOR.name)
|
||||
return object : SimulatedNode(cfg, network, networkMapAddr, advertisedServices, id, overrideServices, entropyRoot) {
|
||||
// TODO: Regulatory nodes don't actually exist properly, this is a last minute demo request.
|
||||
// So we just fire a message at a node that doesn't know how to handle it, and it'll ignore it.
|
||||
|
@ -297,7 +297,7 @@ object SimmFlow {
|
||||
@Suspendable
|
||||
private fun updatePortfolio(portfolio: Portfolio) {
|
||||
logger.info("Handshake finished, awaiting Simm update")
|
||||
send(replyToParty, Ack) // Hack to state that this party is ready
|
||||
send(replyToParty, Ack) // Hack to state that this party is ready.
|
||||
subFlow(object : StateRevisionFlow.Receiver<PortfolioState.Update>(replyToParty) {
|
||||
override fun verifyProposal(proposal: Proposal<PortfolioState.Update>) {
|
||||
super.verifyProposal(proposal)
|
||||
@ -320,5 +320,6 @@ object SimmFlow {
|
||||
}
|
||||
}
|
||||
|
||||
@CordaSerializable
|
||||
private object Ack
|
||||
}
|
||||
|
@ -13,7 +13,7 @@
|
||||
<Console name="Console-Appender" target="SYSTEM_OUT">
|
||||
<PatternLayout>
|
||||
<pattern>
|
||||
[%-5level] %d{HH:mm:ss.SSS} [%t] %c{2}.%M - %msg%n
|
||||
[%-5level] %date{HH:mm:ss.SSS} [%t] %c{2}.%method - %msg%n
|
||||
</pattern>>
|
||||
</PatternLayout>
|
||||
</Console>
|
||||
@ -27,9 +27,9 @@
|
||||
those that are older than 60 days, but keep the most recent 10 GB -->
|
||||
<RollingFile name="RollingFile-Appender"
|
||||
fileName="${log-path}/${log-name}.log"
|
||||
filePattern="${archive}/${log-name}.%d{yyyy-MM-dd}-%i.log.gz">
|
||||
filePattern="${archive}/${log-name}.%date{yyyy-MM-dd}-%i.log.gz">
|
||||
|
||||
<PatternLayout pattern="[%-5level] %d{ISO8601}{GMT+0} [%t] %c{2} - %msg%n"/>
|
||||
<PatternLayout pattern="[%-5level] %date{ISO8601}{GMT+0} [%t] %c{2} - %msg%n"/>
|
||||
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy/>
|
||||
@ -61,4 +61,4 @@
|
||||
</Logger>
|
||||
</Loggers>
|
||||
|
||||
</Configuration>
|
||||
</Configuration>
|
||||
|
@ -13,9 +13,9 @@ import net.corda.node.internal.NodeStartup
|
||||
import net.corda.node.services.api.RegulatorService
|
||||
import net.corda.node.services.transactions.SimpleNotaryService
|
||||
import net.corda.nodeapi.ArtemisMessagingComponent
|
||||
import net.corda.testing.ProjectStructure.projectRootDir
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.Test
|
||||
import java.nio.file.Paths
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.ScheduledExecutorService
|
||||
|
||||
@ -70,7 +70,7 @@ class DriverTests {
|
||||
@Test
|
||||
fun `debug mode enables debug logging level`() {
|
||||
// Make sure we're using the log4j2 config which writes to the log file
|
||||
val logConfigFile = Paths.get("..", "config", "dev", "log4j2.xml").toAbsolutePath()
|
||||
val logConfigFile = projectRootDir / "config" / "dev" / "log4j2.xml"
|
||||
assertThat(logConfigFile).isRegularFile()
|
||||
driver(isDebug = true, systemProperties = mapOf("log4j.configurationFile" to logConfigFile.toString())) {
|
||||
val baseDirectory = startNode(DUMMY_BANK_A.name).getOrThrow().configuration.baseDirectory
|
||||
|
@ -4,12 +4,14 @@
|
||||
package net.corda.testing
|
||||
|
||||
import com.google.common.net.HostAndPort
|
||||
import com.nhaarman.mockito_kotlin.spy
|
||||
import com.nhaarman.mockito_kotlin.whenever
|
||||
import net.corda.core.contracts.StateRef
|
||||
import net.corda.core.crypto.SecureHash
|
||||
import net.corda.core.crypto.X509Utilities
|
||||
import net.corda.core.crypto.commonName
|
||||
import net.corda.core.crypto.generateKeyPair
|
||||
import net.corda.core.identity.AnonymousParty
|
||||
import net.corda.core.crypto.*
|
||||
import net.corda.core.identity.Party
|
||||
import net.corda.core.identity.PartyAndCertificate
|
||||
import net.corda.core.node.ServiceHub
|
||||
@ -18,10 +20,8 @@ import net.corda.core.node.services.IdentityService
|
||||
import net.corda.core.serialization.OpaqueBytes
|
||||
import net.corda.core.transactions.TransactionBuilder
|
||||
import net.corda.core.utilities.*
|
||||
import net.corda.node.internal.NetworkMapInfo
|
||||
import net.corda.node.services.config.*
|
||||
import net.corda.node.services.identity.InMemoryIdentityService
|
||||
import net.corda.nodeapi.User
|
||||
import net.corda.nodeapi.config.SSLConfiguration
|
||||
import net.corda.testing.node.MockServices
|
||||
import net.corda.testing.node.makeTestDataSourceProperties
|
||||
@ -33,8 +33,6 @@ import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.security.KeyPair
|
||||
import java.security.PublicKey
|
||||
import java.security.cert.CertPath
|
||||
import java.util.*
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
/**
|
||||
@ -89,7 +87,7 @@ val BIG_CORP_PARTY_REF = BIG_CORP.ref(OpaqueBytes.of(1)).reference
|
||||
val ALL_TEST_KEYS: List<KeyPair> get() = listOf(MEGA_CORP_KEY, MINI_CORP_KEY, ALICE_KEY, BOB_KEY, DUMMY_NOTARY_KEY)
|
||||
|
||||
val MOCK_IDENTITIES = listOf(MEGA_CORP_IDENTITY, MINI_CORP_IDENTITY, DUMMY_NOTARY_IDENTITY)
|
||||
val MOCK_IDENTITY_SERVICE: IdentityService get() = InMemoryIdentityService(MOCK_IDENTITIES, emptyMap(), DUMMY_CA.certificate)
|
||||
val MOCK_IDENTITY_SERVICE: IdentityService get() = InMemoryIdentityService(MOCK_IDENTITIES, emptyMap(), DUMMY_CA.certificate.cert)
|
||||
|
||||
val MOCK_VERSION_INFO = VersionInfo(1, "Mock release", "Mock revision", "Mock Vendor")
|
||||
|
||||
@ -147,48 +145,26 @@ fun getFreeLocalPorts(hostName: String, numberToAlloc: Int): List<HostAndPort> {
|
||||
dsl: TransactionDSL<TransactionDSLInterpreter>.() -> EnforceVerifyOrFail
|
||||
) = ledger { this.transaction(transactionLabel, transactionBuilder, dsl) }
|
||||
|
||||
// TODO Replace this with testConfiguration
|
||||
data class TestNodeConfiguration(
|
||||
override val baseDirectory: Path,
|
||||
override val myLegalName: X500Name,
|
||||
override val networkMapService: NetworkMapInfo?,
|
||||
override val minimumPlatformVersion: Int = 1,
|
||||
override val keyStorePassword: String = "cordacadevpass",
|
||||
override val trustStorePassword: String = "trustpass",
|
||||
override val rpcUsers: List<User> = emptyList(),
|
||||
override val dataSourceProperties: Properties = makeTestDataSourceProperties(myLegalName.commonName),
|
||||
override val emailAddress: String = "",
|
||||
override val exportJMXto: String = "",
|
||||
override val devMode: Boolean = true,
|
||||
override val certificateSigningService: URL = URL("http://localhost"),
|
||||
override val certificateChainCheckPolicies: List<CertChainPolicyConfig> = emptyList(),
|
||||
override val verifierType: VerifierType = VerifierType.InMemory,
|
||||
override val messageRedeliveryDelaySeconds: Int = 5) : NodeConfiguration {
|
||||
}
|
||||
|
||||
fun testConfiguration(baseDirectory: Path, legalName: X500Name, basePort: Int): FullNodeConfiguration {
|
||||
return FullNodeConfiguration(
|
||||
basedir = baseDirectory,
|
||||
myLegalName = legalName,
|
||||
networkMapService = null,
|
||||
emailAddress = "",
|
||||
keyStorePassword = "cordacadevpass",
|
||||
trustStorePassword = "trustpass",
|
||||
dataSourceProperties = makeTestDataSourceProperties(legalName.commonName),
|
||||
certificateSigningService = URL("http://localhost"),
|
||||
rpcUsers = emptyList(),
|
||||
verifierType = VerifierType.InMemory,
|
||||
useHTTPS = false,
|
||||
p2pAddress = HostAndPort.fromParts("localhost", basePort),
|
||||
rpcAddress = HostAndPort.fromParts("localhost", basePort + 1),
|
||||
messagingServerAddress = null,
|
||||
extraAdvertisedServiceIds = emptyList(),
|
||||
bftReplicaId = null,
|
||||
notaryNodeAddress = null,
|
||||
notaryClusterAddresses = emptyList(),
|
||||
certificateChainCheckPolicies = emptyList(),
|
||||
devMode = true,
|
||||
relay = null)
|
||||
fun testNodeConfiguration(
|
||||
baseDirectory: Path,
|
||||
myLegalName: X500Name): NodeConfiguration {
|
||||
abstract class MockableNodeConfiguration : NodeConfiguration // Otherwise Mockito is defeated by val getters.
|
||||
val nc = spy<MockableNodeConfiguration>()
|
||||
whenever(nc.baseDirectory).thenReturn(baseDirectory)
|
||||
whenever(nc.myLegalName).thenReturn(myLegalName)
|
||||
whenever(nc.minimumPlatformVersion).thenReturn(1)
|
||||
whenever(nc.keyStorePassword).thenReturn("cordacadevpass")
|
||||
whenever(nc.trustStorePassword).thenReturn("trustpass")
|
||||
whenever(nc.rpcUsers).thenReturn(emptyList())
|
||||
whenever(nc.dataSourceProperties).thenReturn(makeTestDataSourceProperties(myLegalName.commonName))
|
||||
whenever(nc.emailAddress).thenReturn("")
|
||||
whenever(nc.exportJMXto).thenReturn("")
|
||||
whenever(nc.devMode).thenReturn(true)
|
||||
whenever(nc.certificateSigningService).thenReturn(URL("http://localhost"))
|
||||
whenever(nc.certificateChainCheckPolicies).thenReturn(emptyList())
|
||||
whenever(nc.verifierType).thenReturn(VerifierType.InMemory)
|
||||
whenever(nc.messageRedeliveryDelaySeconds).thenReturn(5)
|
||||
return nc
|
||||
}
|
||||
|
||||
@JvmOverloads
|
||||
|
@ -0,0 +1,16 @@
|
||||
package net.corda.testing
|
||||
|
||||
import net.corda.core.div
|
||||
import net.corda.core.isDirectory
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
|
||||
object ProjectStructure {
|
||||
val projectRootDir: Path = run {
|
||||
var dir = Paths.get(javaClass.getResource("/").toURI())
|
||||
while (!(dir / ".git").isDirectory()) {
|
||||
dir = dir.parent
|
||||
}
|
||||
dir
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user