Skip to content

Commit 115036e

Browse files
Renamed TransactionResolution to StatePosition.
1 parent 7213566 commit 115036e

File tree

8 files changed

+99
-56
lines changed

8 files changed

+99
-56
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=onixlabs-corda-core
22
group=io.onixlabs
3-
version=4.0.0-rc3
3+
version=4.0.0-rc4
44
onixlabs.development.jarsign.keystore=../lib/onixlabs.development.pkcs12
55
onixlabs.development.jarsign.password=5891f47942424d2acbe108691fdb5ba258712fca7e4762be4327241ebf3dbfa3

onixlabs-corda-core-contract/src/main/kotlin/io/onixlabs/corda/core/contract/AbstractPluralResolvable.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,10 @@ abstract class AbstractPluralResolvable<T> : PluralResolvable<T> where T : Contr
6161
* Resolves a [ContractState] using a [LedgerTransaction] instance.
6262
*
6363
* @param transaction The [LedgerTransaction] instance to use to resolve the state.
64-
* @param resolution The transaction resolution method to use to resolve the [ContractState] instance.
64+
* @param position The position of the [ContractState] instances to resolve in the transaction.
6565
* @return Returns a list of resolved [ContractState] elements, or an empty list if no matching state is found.
6666
*/
67-
override fun resolve(transaction: LedgerTransaction, resolution: TransactionResolution): List<StateAndRef<T>> {
68-
val states = when (resolution) {
69-
TransactionResolution.INPUT -> transaction.inRefsOfType(contractStateType)
70-
TransactionResolution.OUTPUT -> transaction.outRefsOfType(contractStateType)
71-
TransactionResolution.REFERENCE -> transaction.referenceInputRefsOfType(contractStateType)
72-
}
73-
74-
return states.filter { isPointingTo(it) }
67+
override fun resolve(transaction: LedgerTransaction, position: StatePosition): List<StateAndRef<T>> {
68+
return position.getStateAndRefs(transaction, contractStateType).filter { isPointingTo(it) }
7569
}
7670
}

onixlabs-corda-core-contract/src/main/kotlin/io/onixlabs/corda/core/contract/AbstractSingularResolvable.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,10 @@ abstract class AbstractSingularResolvable<T> : SingularResolvable<T> where T : C
6161
* Resolves a [ContractState] using a [LedgerTransaction] instance.
6262
*
6363
* @param transaction The [LedgerTransaction] instance to use to resolve the state.
64-
* @param resolution The transaction resolution method to use to resolve the [ContractState] instance.
64+
* @param position The position of the [ContractState] instance to resolve in the transaction.
6565
* @return Returns the resolved [ContractState], or null if no matching state is found.
6666
*/
67-
override fun resolve(transaction: LedgerTransaction, resolution: TransactionResolution): StateAndRef<T>? {
68-
val states = when (resolution) {
69-
TransactionResolution.INPUT -> transaction.inRefsOfType(contractStateType)
70-
TransactionResolution.OUTPUT -> transaction.outRefsOfType(contractStateType)
71-
TransactionResolution.REFERENCE -> transaction.referenceInputRefsOfType(contractStateType)
72-
}
73-
74-
return states.singleOrNull { isPointingTo(it) }
67+
override fun resolve(transaction: LedgerTransaction, position: StatePosition): StateAndRef<T>? {
68+
return position.getStateAndRefs(transaction, contractStateType).singleOrNull { isPointingTo(it) }
7569
}
7670
}

onixlabs-corda-core-contract/src/main/kotlin/io/onixlabs/corda/core/contract/Extensions.SingularResolvable.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ fun <T : ContractState> SingularResolvable<T>.resolveOrThrow(
5252
* Resolves a [ContractState] using a [LedgerTransaction] instance.
5353
*
5454
* @param transaction The [LedgerTransaction] instance to use to resolve the state.
55-
* @param resolution The transaction resolution method to use to resolve the [ContractState] instance.
55+
* @param position The position of the [ContractState] instance to resolve in the transaction.
5656
* @param message The exception message to throw if the state cannot be resolved.
5757
* @return Returns the resolved [ContractState], or throws an exception if the state cannot be resolved.
5858
*/
5959
fun <T : ContractState> SingularResolvable<T>.resolveOrThrow(
6060
transaction: LedgerTransaction,
61-
resolution: TransactionResolution,
61+
position: StatePosition,
6262
message: () -> String = { MESSAGE }
63-
): StateAndRef<T> = resolve(transaction, resolution) ?: throw IllegalArgumentException(message())
63+
): StateAndRef<T> = resolve(transaction, position) ?: throw IllegalArgumentException(message())

onixlabs-corda-core-contract/src/main/kotlin/io/onixlabs/corda/core/contract/PluralResolvable.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ interface PluralResolvable<T> where T : ContractState {
4949
* Resolves a [ContractState] using a [LedgerTransaction] instance.
5050
*
5151
* @param transaction The [LedgerTransaction] instance to use to resolve the state.
52-
* @param resolution The transaction resolution method to use to resolve the [ContractState] instance.
52+
* @param position The position of the [ContractState] instances to resolve in the transaction.
5353
* @return Returns a list of resolved [ContractState] elements, or an empty list if no matching state is found.
5454
*/
55-
fun resolve(transaction: LedgerTransaction, resolution: TransactionResolution): List<StateAndRef<T>>
55+
fun resolve(transaction: LedgerTransaction, position: StatePosition): List<StateAndRef<T>>
5656

5757
/**
5858
* Determines whether this [PluralResolvable] is pointing to the specified [StateAndRef] instance.

onixlabs-corda-core-contract/src/main/kotlin/io/onixlabs/corda/core/contract/SingularResolvable.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ interface SingularResolvable<T> where T : ContractState {
4949
* Resolves a [ContractState] using a [LedgerTransaction] instance.
5050
*
5151
* @param transaction The [LedgerTransaction] instance to use to resolve the state.
52-
* @param resolution The transaction resolution method to use to resolve the [ContractState] instance.
52+
* @param position The position of the [ContractState] instance to resolve in the transaction.
5353
* @return Returns the resolved [ContractState], or null if no matching state is found.
5454
*/
55-
fun resolve(transaction: LedgerTransaction, resolution: TransactionResolution): StateAndRef<T>?
55+
fun resolve(transaction: LedgerTransaction, position: StatePosition): StateAndRef<T>?
5656

5757
/**
5858
* Determines whether this [SingularResolvable] is pointing to the specified [StateAndRef] instance.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2020-2022 ONIXLabs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.onixlabs.corda.core.contract
18+
19+
import io.onixlabs.corda.core.contract.StatePosition.*
20+
import net.corda.core.contracts.ContractState
21+
import net.corda.core.contracts.StateAndRef
22+
import net.corda.core.serialization.CordaSerializable
23+
import net.corda.core.transactions.LedgerTransaction
24+
25+
/**
26+
* Determines the position of a state within a transaction.
27+
*
28+
* @property INPUT The state is an input state in the transaction.
29+
* @property OUTPUT The state is an output state in the transaction.
30+
* @property REFERENCE The state is a reference state in the transaction.
31+
*/
32+
@CordaSerializable
33+
enum class StatePosition {
34+
INPUT,
35+
OUTPUT,
36+
REFERENCE;
37+
38+
/**
39+
* Obtains a [List] of [StateAndRef] of type [T] from the specified [LedgerTransaction].
40+
*
41+
* @param T The underlying [ContractState] type to obtain from the transaction.
42+
* @param transaction The [LedgerTransaction] from which to obtain [StateAndRef] instances of type [T].
43+
* @param type The [ContractState] type to obtain from the transaction.
44+
* @return Returns a [List] of [StateAndRef] of type [T] from the specified [LedgerTransaction].
45+
*/
46+
fun <T : ContractState> getStateAndRefs(transaction: LedgerTransaction, type: Class<T>): List<StateAndRef<T>> = when (this) {
47+
INPUT -> transaction.inRefsOfType(type)
48+
OUTPUT -> transaction.outRefsOfType(type)
49+
REFERENCE -> transaction.referenceInputRefsOfType(type)
50+
}
51+
52+
/**
53+
* Obtains a [List] of [StateAndRef] of type [T] from the specified [LedgerTransaction].
54+
*
55+
* @param T The underlying [ContractState] type to obtain from the transaction.
56+
* @param transaction The [LedgerTransaction] from which to obtain instances of type [T].
57+
* @return Returns a [List] of [StateAndRef] of type [T] from the specified [LedgerTransaction].
58+
*/
59+
inline fun <reified T : ContractState> getStateAndRefs(transaction: LedgerTransaction): List<StateAndRef<T>> {
60+
return getStateAndRefs(transaction, T::class.java)
61+
}
62+
63+
/**
64+
* Obtains a [List] of type [T] from the specified [LedgerTransaction].
65+
*
66+
* @param T The underlying [ContractState] type to obtain from the transaction.
67+
* @param transaction The [LedgerTransaction] from which to obtain instances of type [T].
68+
* @param type The [ContractState] type to obtain from the transaction.
69+
* @return Returns a [List] of type [T] from the specified [LedgerTransaction].
70+
*/
71+
fun <T : ContractState> getStates(transaction: LedgerTransaction, type: Class<T>): List<T> {
72+
return getStateAndRefs(transaction, type).map { it.state.data }
73+
}
74+
75+
/**
76+
* Obtains a [List] of type [T] from the specified [LedgerTransaction].
77+
*
78+
* @param T The underlying [ContractState] type to obtain from the transaction.
79+
* @param transaction The [LedgerTransaction] from which to obtain instances of type [T].
80+
* @return Returns a [List] of type [T] from the specified [LedgerTransaction].
81+
*/
82+
inline fun <reified T : ContractState> getStates(transaction: LedgerTransaction): List<T> {
83+
return getStates(transaction, T::class.java)
84+
}
85+
}

onixlabs-corda-core-contract/src/main/kotlin/io/onixlabs/corda/core/contract/TransactionResolution.kt

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)