-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from hyperschwartz/jschwartz/add-event-streams
More event stream changes
- Loading branch information
Showing
12 changed files
with
147 additions
and
20 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
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
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
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
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
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
83 changes: 83 additions & 0 deletions
83
server/src/main/kotlin/io/provenance/invoice/services/EventHandlerService.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,83 @@ | ||
package io.provenance.invoice.services | ||
|
||
import com.google.common.util.concurrent.FutureCallback | ||
import com.google.common.util.concurrent.Futures | ||
import io.provenance.invoice.AssetProtos | ||
import io.provenance.invoice.AssetProtos.Asset | ||
import io.provenance.invoice.config.provenance.ObjectStore | ||
import io.provenance.invoice.repository.InvoiceRepository | ||
import io.provenance.invoice.util.eventstream.external.StreamEvent | ||
import io.provenance.invoice.util.extension.checkNotNull | ||
import io.provenance.invoice.util.extension.parseUuid | ||
import io.provenance.invoice.util.extension.unpackInvoice | ||
import io.provenance.invoice.util.validation.InvoiceValidator | ||
import io.provenance.scope.encryption.domain.inputstream.DIMEInputStream | ||
import io.provenance.scope.sdk.extensions.resultHash | ||
import mu.KLogging | ||
import org.springframework.stereotype.Service | ||
import java.util.concurrent.Executors | ||
|
||
@Service | ||
class EventHandlerService( | ||
private val objectStore: ObjectStore, | ||
private val invoiceRepository: InvoiceRepository, | ||
) { | ||
|
||
private companion object : KLogging() { | ||
private const val PAYABLE_REGISTRATION_KEY: String = "PAYABLE_REGISTERED" | ||
private const val ORACLE_APPROVED_KEY: String = "ORACLE_APPROVED" | ||
private const val PAYMENT_MADE_KEY: String = "PAYMENT_MADE" | ||
} | ||
|
||
fun handleEvent(event: StreamEvent) { | ||
val eventKeys = event.attributes.map { it.key } | ||
try { | ||
when { | ||
PAYABLE_REGISTRATION_KEY in eventKeys -> handleInvoiceRegisteredEvent(event) | ||
ORACLE_APPROVED_KEY in eventKeys -> handleOracleApprovedEvent(event) | ||
PAYMENT_MADE_KEY in eventKeys -> handlePaymentMadeEvent(event) | ||
} | ||
} catch (e: Exception) { | ||
logger.error("Failed to process event with hash [${event.txHash}] and type [${event.eventType}] at height [${event.height}]", e) | ||
} | ||
} | ||
|
||
private fun handleInvoiceRegisteredEvent(event: StreamEvent) { | ||
val invoiceUuid = event.attributeValue(PAYABLE_REGISTRATION_KEY) | ||
.also { logger.info("Handling invoice registration event for invoice uuid [$it]") } | ||
.parseUuid() | ||
val invoiceDto = invoiceRepository.findDtoByUuid(invoiceUuid) | ||
val assetHash = invoiceDto.writeRecordRequest.record.resultHash() | ||
val getFuture = objectStore.osClient.get( | ||
hash = assetHash, | ||
publicKey = objectStore.oracleCredentials.public, | ||
) | ||
Futures.addCallback(getFuture, object: FutureCallback<DIMEInputStream> { | ||
override fun onSuccess(result: DIMEInputStream?) { | ||
result.checkNotNull { "Null DIMEInputStream received from object store query for invoice [$invoiceUuid]" } | ||
.getDecryptedPayload(objectStore.keyRef).use { signatureStream -> | ||
val messageBytes = signatureStream.readAllBytes() | ||
val targetInvoice = Asset.parseFrom(messageBytes).unpackInvoice() | ||
InvoiceValidator.validateInvoice(targetInvoice) | ||
logger.info("Successfully validated invoice from object store [${targetInvoice.invoiceUuid.value}]") | ||
} | ||
} | ||
override fun onFailure(t: Throwable) { | ||
logger.error("Failed to receive invoice [$invoiceUuid] DIME stream from object store") | ||
} | ||
}, Executors.newSingleThreadExecutor()) | ||
} | ||
|
||
private fun handleOracleApprovedEvent(event: StreamEvent) { | ||
logger.info("Handling oracle approved event") | ||
} | ||
|
||
private fun handlePaymentMadeEvent(event: StreamEvent) { | ||
logger.info("Handling payment made event") | ||
} | ||
|
||
private fun StreamEvent.attributeValueOrNull(key: String): String? = attributes.singleOrNull { it.key == key }?.value | ||
|
||
private fun StreamEvent.attributeValue(key: String): String = attributeValueOrNull(key) | ||
.checkNotNull { "Unable to find stream attribute with key [$key]" } | ||
} |
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
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
4 changes: 4 additions & 0 deletions
4
server/src/main/kotlin/io/provenance/invoice/util/extension/StringExtensions.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,6 +1,10 @@ | ||
package io.provenance.invoice.util.extension | ||
|
||
import java.time.OffsetDateTime | ||
import java.util.UUID | ||
|
||
fun String.toOffsetDateTime(): OffsetDateTime = OffsetDateTime.parse(this) | ||
fun String.toOffsetDateTimeOrNull(): OffsetDateTime? = tryOrNull(::toOffsetDateTime) | ||
|
||
fun String.parseUuidOrNull(): UUID? = tryOrNull { UUID.fromString(this) } | ||
fun String.parseUuid(): UUID = parseUuidOrNull().checkNotNull { "Unable to parse value [$this] to a valid UUID" } |
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
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