-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding participants to audience keys * first pass * updates * final * cleanup * cleanup / documentation * PR comments * more pr comments
- Loading branch information
1 parent
f89a38d
commit 3e99626
Showing
32 changed files
with
690 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 1 addition & 5 deletions
6
models/src/main/kotlin/io/provenance/api/models/cee/ContractConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
package io.provenance.api.models.cee | ||
|
||
import java.util.UUID | ||
|
||
data class ContractConfig( | ||
val contractName: String, | ||
val scopeUuid: UUID, | ||
val sessionUuid: UUID?, | ||
val scopeSpecificationName: String, | ||
val parserConfig: ParserConfig?, | ||
val parserConfig: ParserConfig? = null, | ||
) |
13 changes: 13 additions & 0 deletions
13
models/src/main/kotlin/io/provenance/api/models/cee/approve/ApproveContractBatchRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.provenance.api.models.cee.approve | ||
|
||
import io.provenance.api.models.account.AccountInfo | ||
import io.provenance.api.models.eos.ObjectStoreConfig | ||
import io.provenance.api.models.p8e.ProvenanceConfig | ||
|
||
class ApproveContractBatchRequest( | ||
val account: AccountInfo = AccountInfo(), | ||
val client: ObjectStoreConfig, | ||
val provenanceConfig: ProvenanceConfig, | ||
val approvals: List<EnvelopeApproval>, | ||
val chunkSize: Int = 25, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
models/src/main/kotlin/io/provenance/api/models/cee/approve/EnvelopeApproval.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.provenance.api.models.cee.approve | ||
|
||
import java.time.OffsetDateTime | ||
|
||
data class EnvelopeApproval( | ||
val envelope: ByteArray, | ||
val expiration: OffsetDateTime = OffsetDateTime.now().plusHours(1), | ||
) |
13 changes: 13 additions & 0 deletions
13
models/src/main/kotlin/io/provenance/api/models/cee/execute/ExecuteContractBatchRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.provenance.api.models.cee.execute | ||
|
||
import io.provenance.api.models.account.Participant | ||
import io.provenance.api.models.p8e.PermissionInfo | ||
|
||
data class ExecuteContractBatchRequest( | ||
val config: ExecuteContractConfig, | ||
val records: Map<String, Any>, | ||
val participants: List<Participant> = emptyList(), | ||
val permissions: PermissionInfo?, | ||
val chunkSize: Int = 25, | ||
val scopes: List<ScopeInfo> = emptyList(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
models/src/main/kotlin/io/provenance/api/models/cee/execute/ScopeInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.provenance.api.models.cee.execute | ||
|
||
import java.util.UUID | ||
|
||
data class ScopeInfo( | ||
val scopeUuid: UUID, | ||
val sessionUuid: UUID? = null, | ||
) |
10 changes: 10 additions & 0 deletions
10
models/src/main/kotlin/io/provenance/api/models/cee/reject/RejectContractBatchRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.provenance.api.models.cee.reject | ||
|
||
import io.provenance.api.models.account.AccountInfo | ||
import io.provenance.api.models.eos.ObjectStoreConfig | ||
|
||
data class RejectContractBatchRequest( | ||
val account: AccountInfo = AccountInfo(), | ||
val client: ObjectStoreConfig, | ||
val rejection: List<ByteArray>, | ||
) |
6 changes: 6 additions & 0 deletions
6
models/src/main/kotlin/io/provenance/api/models/cee/submit/EnvelopeSubmission.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.provenance.api.models.cee.submit | ||
|
||
data class EnvelopeSubmission( | ||
val envelope: ByteArray, | ||
val state: ByteArray | ||
) |
11 changes: 11 additions & 0 deletions
11
...n/kotlin/io/provenance/api/models/cee/submit/SubmitContractBatchExecutionResultRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.provenance.api.models.cee.submit | ||
|
||
import io.provenance.api.models.account.AccountInfo | ||
import io.provenance.api.models.p8e.ProvenanceConfig | ||
|
||
data class SubmitContractBatchExecutionResultRequest( | ||
val account: AccountInfo = AccountInfo(), | ||
val provenance: ProvenanceConfig, | ||
val submission: List<EnvelopeSubmission>, | ||
val chunkSize: Int = 25, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
models/src/main/kotlin/io/provenance/api/models/p8e/ProvenanceConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
package io.provenance.api.models.p8e | ||
|
||
import cosmos.tx.v1beta1.ServiceOuterClass | ||
|
||
data class ProvenanceConfig( | ||
val chainId: String, | ||
val nodeEndpoint: String, | ||
val gasAdjustment: Double? = 1.5, | ||
val broadcastMode: ServiceOuterClass.BroadcastMode = ServiceOuterClass.BroadcastMode.BROADCAST_MODE_SYNC | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...main/kotlin/io/provenance/api/domain/usecase/cee/approve/ApproveContractBatchExecution.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package io.provenance.api.domain.usecase.cee.approve | ||
|
||
import com.google.protobuf.Any | ||
import cosmos.authz.v1beta1.Tx | ||
import cosmos.tx.v1beta1.TxOuterClass | ||
import io.provenance.api.domain.provenance.Provenance | ||
import io.provenance.api.domain.usecase.AbstractUseCase | ||
import io.provenance.api.domain.usecase.cee.approve.models.ApproveContractBatchRequestWrapper | ||
import io.provenance.api.domain.usecase.cee.common.client.CreateClient | ||
import io.provenance.api.domain.usecase.cee.common.client.model.CreateClientRequest | ||
import io.provenance.api.domain.usecase.provenance.account.GetSigner | ||
import io.provenance.api.domain.usecase.provenance.account.models.GetSignerRequest | ||
import io.provenance.api.frameworks.provenance.exceptions.ContractExecutionBatchException | ||
import io.provenance.scope.contract.proto.Envelopes | ||
import io.provenance.scope.sdk.FragmentResult | ||
import mu.KotlinLogging | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class ApproveContractBatchExecution( | ||
private val createClient: CreateClient, | ||
private val provenance: Provenance, | ||
private val getSigner: GetSigner, | ||
) : AbstractUseCase<ApproveContractBatchRequestWrapper, Unit>() { | ||
private val log = KotlinLogging.logger { } | ||
|
||
override suspend fun execute(args: ApproveContractBatchRequestWrapper) { | ||
val errors = mutableListOf<Throwable>() | ||
val executionResults = mutableListOf<Pair<Envelopes.EnvelopeState, List<Tx.MsgGrant>>>() | ||
val signer = getSigner.execute(GetSignerRequest(args.uuid, args.request.account)) | ||
val client = createClient.execute(CreateClientRequest(args.uuid, args.request.account, args.request.client)) | ||
|
||
args.request.approvals.forEach { | ||
val envelope = Envelopes.Envelope.newBuilder().mergeFrom(it.envelope).build() | ||
val result = client.execute(envelope) | ||
|
||
if (result is FragmentResult) { | ||
client.approveScopeUpdate(result.envelopeState, it.expiration).let { grant -> | ||
executionResults.add(Pair(result.envelopeState, grant)) | ||
} | ||
} | ||
} | ||
|
||
val chunked = executionResults.chunked(args.request.chunkSize) | ||
chunked.forEachIndexed { index, it -> | ||
runCatching { | ||
val messages = it.flatMap { grantList -> grantList.second.map { grant -> Any.pack(grant, "") } } | ||
val txBody = TxOuterClass.TxBody.newBuilder().addAllMessages(messages).build() | ||
val broadcast = provenance.executeTransaction(args.request.provenanceConfig, txBody, signer) | ||
|
||
it.forEach { executions -> | ||
client.respondWithApproval(executions.first, broadcast.txhash) | ||
} | ||
}.fold( | ||
onSuccess = { | ||
log.info("Successfully processed batch $index of ${chunked.size}") | ||
}, | ||
onFailure = { | ||
errors.add(it) | ||
} | ||
) | ||
} | ||
|
||
if (errors.any()) { | ||
throw ContractExecutionBatchException(errors.joinToString(limit = 20) { it.message.toString() }) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...io/provenance/api/domain/usecase/cee/approve/models/ApproveContractBatchRequestWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.provenance.api.domain.usecase.cee.approve.models | ||
|
||
import io.provenance.api.models.cee.approve.ApproveContractBatchRequest | ||
import java.util.UUID | ||
|
||
data class ApproveContractBatchRequestWrapper( | ||
val uuid: UUID, | ||
val request: ApproveContractBatchRequest | ||
) |
105 changes: 105 additions & 0 deletions
105
service/src/main/kotlin/io/provenance/api/domain/usecase/cee/common/ContractUtilities.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package io.provenance.api.domain.usecase.cee.common | ||
|
||
import com.google.protobuf.Message | ||
import io.provenance.api.domain.cee.ContractParser | ||
import io.provenance.api.domain.cee.ContractService | ||
import io.provenance.api.domain.provenance.Provenance | ||
import io.provenance.api.domain.usecase.cee.common.client.CreateClient | ||
import io.provenance.api.domain.usecase.cee.common.client.model.CreateClientRequest | ||
import io.provenance.api.domain.usecase.common.originator.EntityManager | ||
import io.provenance.api.domain.usecase.common.originator.models.KeyManagementConfigWrapper | ||
import io.provenance.api.models.account.Participant | ||
import io.provenance.api.models.cee.ParserConfig | ||
import io.provenance.api.models.cee.execute.ExecuteContractConfig | ||
import io.provenance.api.models.cee.execute.ScopeInfo | ||
import io.provenance.api.models.p8e.PermissionInfo | ||
import io.provenance.client.protobuf.extensions.isSet | ||
import io.provenance.metadata.v1.ScopeResponse | ||
import io.provenance.scope.contract.annotations.Input | ||
import io.provenance.scope.contract.spec.P8eContract | ||
import io.provenance.scope.encryption.util.toJavaPublicKey | ||
import io.provenance.scope.sdk.Client | ||
import io.provenance.scope.sdk.Session | ||
import java.util.UUID | ||
import kotlin.reflect.KType | ||
import kotlin.reflect.full.functions | ||
import mu.KotlinLogging | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class ContractUtilities( | ||
private val contractService: ContractService, | ||
private val provenanceService: Provenance, | ||
private val contractParser: ContractParser, | ||
private val createClient: CreateClient, | ||
private val entityManager: EntityManager | ||
) { | ||
private val log = KotlinLogging.logger { } | ||
|
||
suspend fun createClient(uuid: UUID, permissions: PermissionInfo?, participants: List<Participant>, config: ExecuteContractConfig): Client { | ||
val audiences = entityManager.hydrateKeys(permissions, participants) | ||
return createClient.execute(CreateClientRequest(uuid, config.account, config.client, audiences)) | ||
} | ||
|
||
suspend fun createSession(uuid: UUID, permissions: PermissionInfo?, participants: List<Participant>, config: ExecuteContractConfig, records: Map<String, Any>, scopes: List<ScopeInfo>): List<Session> { | ||
val audiences = entityManager.hydrateKeys(permissions, participants) | ||
val client = createClient.execute(CreateClientRequest(uuid, config.account, config.client, audiences)) | ||
val contract = contractService.getContract(config.contract.contractName) | ||
val parsedRecords = getRecords(contractParser, records, contract, config.contract.parserConfig) | ||
|
||
val participantsMap = participants.associate { | ||
it.partyType to entityManager.getEntity(KeyManagementConfigWrapper(it.uuid, config.account.keyManagementConfig)) | ||
} | ||
|
||
return scopes.map { | ||
val scope = provenanceService.getScope(config.provenanceConfig, it.scopeUuid) | ||
val scopeToUse: ScopeResponse? = if (scope.scope.scope.isSet() && !scope.scope.scope.scopeId.isEmpty) scope else null | ||
contractService.setupContract( | ||
client, | ||
contract, | ||
parsedRecords, | ||
it.scopeUuid, | ||
it.sessionUuid, | ||
participantsMap, | ||
scopeToUse, | ||
config.contract.scopeSpecificationName, | ||
audiences.map { it.encryptionKey.toJavaPublicKey() }.toSet() | ||
) | ||
} | ||
} | ||
|
||
@Suppress("TooGenericExceptionCaught") | ||
fun getRecords(contractParser: ContractParser, records: Map<String, Any>, contract: Class<out P8eContract>, parserConfig: ParserConfig?): Map<String, Message> { | ||
val contractRecords = mutableMapOf<String, Message>() | ||
|
||
try { | ||
contract.kotlin.functions.forEach { func -> | ||
func.parameters.forEach { param -> | ||
(param.annotations.firstOrNull { it is Input } as? Input)?.let { input -> | ||
val parameterClass = Class.forName(param.type.toClassNameString()) | ||
records.getOrDefault(input.name, null)?.let { | ||
|
||
val record = when (val parser = parserConfig?.name?.let { name -> contractParser.getParser(name) }) { | ||
null -> { | ||
contractParser.parseInput(it, parameterClass) | ||
} | ||
else -> { | ||
parser.parse(it, parameterClass, parserConfig.descriptors) | ||
} | ||
} | ||
|
||
contractRecords[input.name] = record | ||
} | ||
} | ||
} | ||
} | ||
} catch (ex: Exception) { | ||
log.error("Failed to get inputs for contract ${contract.simpleName}") | ||
throw ex | ||
} | ||
|
||
return contractRecords | ||
} | ||
|
||
private fun KType?.toClassNameString(): String? = this?.classifier?.toString()?.drop("class ".length) | ||
} |
Oops, something went wrong.