Skip to content

Commit 3bc19fb

Browse files
BROKEN COMMIT: FIX OR REVERT!
1 parent 9da47cc commit 3bc19fb

File tree

7 files changed

+292
-71
lines changed

7 files changed

+292
-71
lines changed

onixlabs-corda-identity-framework-contract/src/main/kotlin/io/onixlabs/corda/identityframework/contract/accounts/Account.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ open class Account(
5353
* @return Returns a persistent state entity.
5454
*/
5555
override fun generateMappedObject(schema: MappedSchema): PersistentState = when (schema) {
56-
is AccountSchemaV1 -> AccountEntity.fromAccount(this)
56+
is AccountSchemaV1 -> AccountEntity(this)
5757
else -> throw IllegalArgumentException("Unrecognised schema: $schema.")
5858
}
5959

onixlabs-corda-identity-framework-contract/src/main/kotlin/io/onixlabs/corda/identityframework/contract/accounts/AccountSchema.kt

Lines changed: 35 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,22 @@ package io.onixlabs.corda.identityframework.contract.accounts
1818

1919
import io.onixlabs.corda.identityframework.contract.claims.AbstractClaim
2020
import net.corda.core.crypto.NullKeys.NULL_PARTY
21-
import net.corda.core.crypto.SecureHash
2221
import net.corda.core.identity.AbstractParty
23-
import net.corda.core.schemas.MappedSchema
24-
import net.corda.core.schemas.PersistentState
25-
import net.corda.core.schemas.StatePersistable
22+
import net.corda.core.schemas.*
23+
import java.io.Serializable
2624
import java.util.*
2725
import javax.persistence.*
26+
import kotlin.jvm.Transient
2827

2928
object AccountSchema {
3029

3130
object AccountSchemaV1 :
3231
MappedSchema(AccountSchema.javaClass, 1, listOf(AccountEntity::class.java, AccountClaim::class.java)) {
33-
override val migrationResource: String = "account-schema.changelog-master"
32+
override val migrationResource = super.migrationResource
3433
}
3534

3635
@Entity
37-
@Table(
38-
name = "onixlabs_account_states",
39-
indexes = [
40-
Index(name = "account_id_index", columnList = "linear_id"),
41-
Index(name = "owner_index", columnList = "owner")
42-
]
43-
)
36+
@Table(name = "onixlabs_account_states")
4437
class AccountEntity(
4538
@Column(name = "linear_id", nullable = false)
4639
val linearId: UUID = UUID.randomUUID(),
@@ -51,58 +44,23 @@ object AccountSchema {
5144
@Column(name = "owner", nullable = false)
5245
val owner: AbstractParty = NULL_PARTY,
5346

54-
@OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.PERSIST])
47+
@OneToMany(cascade = [CascadeType.PERSIST])
5548
@JoinColumns(
56-
JoinColumn(name = "transaction_id", referencedColumnName = "transaction_id"),
57-
JoinColumn(name = "output_index", referencedColumnName = "output_index")
49+
JoinColumn(name = "output_index", referencedColumnName = "output_index"),
50+
JoinColumn(name = "transaction_id", referencedColumnName = "transaction_id")
5851
)
59-
@OrderColumn
60-
val claims: MutableSet<AccountClaim> = mutableSetOf()
52+
val claims: List<AccountClaim> = emptyList()
6153
) : PersistentState() {
62-
63-
companion object {
64-
fun fromAccount(account: Account): AccountEntity {
65-
val accountEntity = AccountEntity(
66-
linearId = account.linearId.id,
67-
externalId = account.linearId.externalId,
68-
owner = account.owner
69-
)
70-
71-
mapClaimsToAccountClaims(accountEntity, account.claims)
72-
73-
return accountEntity
74-
}
75-
76-
private fun mapClaimsToAccountClaims(accountEntity: AccountEntity, claims: Set<AbstractClaim<*>>) {
77-
claims.forEach {
78-
val hash = with(it) { SecureHash.sha256("$property$value${value.javaClass}$javaClass") }
79-
80-
val claim = AccountClaim(
81-
property = it.property,
82-
value = it.value.toString(),
83-
hash = hash.toString(),
84-
account = accountEntity
85-
)
86-
87-
accountEntity.claims.add(claim)
88-
}
89-
}
90-
}
54+
constructor(account: Account) : this(
55+
account.linearId.id,
56+
account.linearId.externalId,
57+
account.owner,
58+
account.claims.map { AccountClaim(it) })
9159
}
9260

9361
@Entity
94-
@Table(
95-
name = "onixlabs_account_claims",
96-
indexes = [
97-
Index(name = "account_claim_index", columnList = "hash", unique = false)
98-
]
99-
)
62+
@Table(name = "onixlabs_account_claims")
10063
class AccountClaim(
101-
@Id
102-
@GeneratedValue
103-
@Column(name = "id", unique = true, nullable = false)
104-
val id: UUID = UUID.randomUUID(),
105-
10664
@Column(name = "property", nullable = false)
10765
val property: String = "",
10866

@@ -112,11 +70,25 @@ object AccountSchema {
11270
@Column(name = "hash", nullable = false)
11371
val hash: String = "",
11472

115-
@ManyToOne(fetch = FetchType.LAZY)
116-
@JoinColumns(
117-
JoinColumn(name = "transaction_id", referencedColumnName = "transaction_id"),
118-
JoinColumn(name = "output_index", referencedColumnName = "output_index")
73+
@EmbeddedId
74+
override val compositeKey: Key
75+
) : IndirectStatePersistable<Key> {
76+
constructor(claim: AbstractClaim<*>) : this(
77+
//UUID.randomUUID(),
78+
claim.property,
79+
claim.value.toString(),
80+
claim.computeHash().toString(),
81+
Key()
11982
)
120-
val account: AccountEntity = AccountEntity()
121-
) : StatePersistable
83+
}
84+
85+
class Key(override val stateRef: PersistentStateRef? = null) : DirectStatePersistable, Serializable {
86+
override fun equals(other: Any?): Boolean {
87+
return this === other || (other is Key && other.stateRef == stateRef)
88+
}
89+
90+
override fun hashCode(): Int {
91+
return Objects.hash(stateRef)
92+
}
93+
}
12294
}

onixlabs-corda-identity-framework-workflow/src/test/kotlin/io/onixlabs/corda/identityframework/workflow/FlowTest.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,20 @@
1717
package io.onixlabs.corda.identityframework.workflow
1818

1919
import io.onixlabs.corda.identityframework.contract.accounts.Account
20+
import io.onixlabs.corda.identityframework.contract.claims.Claim
2021
import io.onixlabs.corda.identityframework.contract.claims.CordaClaim
21-
import net.corda.core.contracts.TypeOnlyCommandData
2222
import net.corda.core.contracts.UniqueIdentifier
23-
import net.corda.core.crypto.NullKeys.NULL_PARTY
2423
import net.corda.core.identity.CordaX500Name
2524
import net.corda.core.identity.Party
2625
import net.corda.testing.common.internal.testNetworkParameters
27-
import net.corda.testing.contracts.DummyContract
2826
import net.corda.testing.core.singleIdentity
29-
import net.corda.testing.internal.vault.DummyLinearContract
3027
import net.corda.testing.node.MockNetwork
3128
import net.corda.testing.node.MockNetworkParameters
3229
import net.corda.testing.node.StartedMockNode
3330
import net.corda.testing.node.internal.cordappsForPackages
3431
import org.junit.jupiter.api.AfterAll
3532
import org.junit.jupiter.api.BeforeAll
3633
import org.junit.jupiter.api.TestInstance
37-
import java.math.BigDecimal
3834
import java.time.Instant
3935

4036
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@@ -48,9 +44,19 @@ abstract class FlowTest {
4844
protected val CLAIM_2 by lazy { CordaClaim(partyB, "Number", 123, LINEAR_ID_2) }
4945
protected val CLAIM_3 by lazy { CordaClaim(partyC, "Time", Instant.now(), LINEAR_ID_3) }
5046

51-
protected val ACCOUNT_1_FOR_PARTY_A by lazy { Account(partyA) }
47+
protected val ACCOUNT_CLAIMS by lazy {
48+
setOf(
49+
Claim("string", "abc"),
50+
Claim("integer", 123),
51+
Claim("decimal", (123.45).toBigDecimal()),
52+
Claim("boolean", true),
53+
Claim("instant", Instant.MIN)
54+
)
55+
}
56+
57+
protected val ACCOUNT_1_FOR_PARTY_A by lazy { Account(partyA, ACCOUNT_CLAIMS) }
5258
protected val ACCOUNT_2_FOR_PARTY_A by lazy { Account(partyA) }
53-
protected val ACCOUNT_1_FOR_PARTY_B by lazy { Account(partyB) }
59+
protected val ACCOUNT_1_FOR_PARTY_B by lazy { Account(partyB, ACCOUNT_CLAIMS) }
5460
protected val ACCOUNT_2_FOR_PARTY_B by lazy { Account(partyB) }
5561

5662
private lateinit var _network: MockNetwork

onixlabs-corda-identity-framework-workflow/src/test/kotlin/io/onixlabs/corda/identityframework/workflow/accounts/AmendAccountFlowTests.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class AmendAccountFlowTests : FlowTest() {
7171
?: fail("Failed to find a recorded account.")
7272

7373
assertEquals(account, recordedAccount)
74+
assertEquals(5, recordedAccount.claims.size)
75+
assert(recordedAccount.claims.containsAll(ACCOUNT_CLAIMS))
7476
}
7577
}
7678
}

onixlabs-corda-identity-framework-workflow/src/test/kotlin/io/onixlabs/corda/identityframework/workflow/accounts/IssueAccountFlowTests.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class IssueAccountFlowTests : FlowTest() {
6767
?: fail("Failed to find a recorded account.")
6868

6969
assertEquals(account, recordedAccount)
70+
assertEquals(5, recordedAccount.claims.size)
71+
assert(recordedAccount.claims.containsAll(ACCOUNT_CLAIMS))
7072
}
7173
}
7274
}

onixlabs-corda-identity-framework-workflow/src/test/kotlin/io/onixlabs/corda/identityframework/workflow/accounts/PublishAccountFlowTests.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class PublishAccountFlowTests : FlowTest() {
6666
?: fail("Failed to find a recorded account.")
6767

6868
assertEquals(account, recordedAccount)
69+
assertEquals(5, recordedAccount.state.data.claims.size)
70+
assert(recordedAccount.state.data.claims.containsAll(ACCOUNT_CLAIMS))
6971
}
7072
}
7173
}

0 commit comments

Comments
 (0)