Skip to content

Commit

Permalink
Merge pull request #116 from onflow/transaction-result-protobuf-schema
Browse files Browse the repository at this point in the history
#106/ TransactionResult type differs from protobuf schema
  • Loading branch information
lealobanov authored Oct 8, 2024
2 parents 170bf2c + b1796ae commit 86b1d98
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 14 deletions.
19 changes: 17 additions & 2 deletions sdk/src/main/kotlin/org/onflow/flow/sdk/models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,25 @@ data class FlowTransactionResult(
val status: FlowTransactionStatus,
val statusCode: Int,
val errorMessage: String,
val events: List<FlowEvent>
val events: List<FlowEvent>,
val blockId: FlowId,
val blockHeight: Long,
val transactionId: FlowId,
val collectionId: FlowId,
val computationUsage: Long
) : Serializable {
companion object {
@JvmStatic
fun of(value: Access.TransactionResultResponse): FlowTransactionResult = FlowTransactionResult(
status = FlowTransactionStatus.of(value.statusValue),
statusCode = value.statusCode,
errorMessage = value.errorMessage,
events = value.eventsList.map { FlowEvent.of(it) }
events = value.eventsList.map { FlowEvent.of(it) },
blockId = FlowId.of(value.blockId.toByteArray()),
blockHeight = value.blockHeight,
transactionId = FlowId.of(value.transactionId.toByteArray()),
collectionId = FlowId.of(value.collectionId.toByteArray()),
computationUsage = value.computationUsage
)
}

Expand All @@ -284,6 +294,11 @@ data class FlowTransactionResult(
.setStatus(TransactionOuterClass.TransactionStatus.valueOf(status.name))
.setStatusCode(statusCode)
.setErrorMessage(errorMessage)
.setBlockId(blockId.byteStringValue)
.setBlockHeight(blockHeight)
.setTransactionId(transactionId.byteStringValue)
.setCollectionId(collectionId.byteStringValue)
.setComputationUsage(computationUsage)
.addAllEvents(events.map { it.builder().build() })

@JvmOverloads
Expand Down
32 changes: 26 additions & 6 deletions sdk/src/test/kotlin/org/onflow/flow/sdk/FlowAccessApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,37 @@ class FlowAccessApiTest {
@Test
fun `Test getTransactionResultsByBlockId with multiple results`() {
val flowAccessApi = mock(FlowAccessApi::class.java)
val blockId = FlowId("01")

val transactionResult1 = FlowTransactionResult(FlowTransactionStatus.SEALED, 1, "message1", emptyList())
val flowId = FlowId("01")

val transactionResult2 = FlowTransactionResult(FlowTransactionStatus.SEALED, 2, "message2", emptyList())
val transactionResult1 = FlowTransactionResult(
FlowTransactionStatus.SEALED,
1,
"message1",
emptyList(),
flowId,
1L,
flowId,
flowId,
1
)

val transactionResult2 = FlowTransactionResult(
FlowTransactionStatus.SEALED,
2,
"message2",
emptyList(),
flowId,
1L,
flowId,
flowId,
1
)

val transactions = listOf(transactionResult1, transactionResult2)

`when`(flowAccessApi.getTransactionResultsByBlockId(blockId)).thenReturn(FlowAccessApi.AccessApiCallResponse.Success(transactions))
`when`(flowAccessApi.getTransactionResultsByBlockId(flowId)).thenReturn(FlowAccessApi.AccessApiCallResponse.Success(transactions))

val result = flowAccessApi.getTransactionResultsByBlockId(blockId)
val result = flowAccessApi.getTransactionResultsByBlockId(flowId)

assertEquals(FlowAccessApi.AccessApiCallResponse.Success(transactions), result)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,19 @@ class AsyncFlowAccessApiImplTest {
@Test
fun `test getTransactionResultById`() {
val flowId = FlowId.of("id".toByteArray())
val flowTransactionResult = FlowTransactionResult(FlowTransactionStatus.SEALED, 1, "message", emptyList())
val transactionResultResponse = Access.TransactionResultResponse.newBuilder().setStatus(TransactionOuterClass.TransactionStatus.SEALED).setStatusCode(1).setErrorMessage("message").setBlockId(ByteString.copyFromUtf8("id")).build()
val flowTransactionResult = FlowTransactionResult(FlowTransactionStatus.SEALED, 1, "message", emptyList(), flowId, 1L, flowId, flowId, 1L)

val transactionResultResponse = Access.TransactionResultResponse.newBuilder()
.setStatus(TransactionOuterClass.TransactionStatus.SEALED)
.setStatusCode(1)
.setErrorMessage("message")
.setBlockId(ByteString.copyFromUtf8("id"))
.setBlockHeight(1L)
.setTransactionId(ByteString.copyFromUtf8("id"))
.setCollectionId(ByteString.copyFromUtf8("id"))
.setComputationUsage(1L)
.build()

`when`(api.getTransactionResult(any())).thenReturn(setupFutureMock(transactionResultResponse))

val result = asyncFlowAccessApi.getTransactionResultById(flowId).get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,18 @@ class FlowAccessApiImplTest {
@Test
fun `Test getTransactionResultById`() {
val flowId = FlowId.of("id".toByteArray())
val flowTransactionResult = FlowTransactionResult(FlowTransactionStatus.SEALED, 1, "message", emptyList())
val response = Access.TransactionResultResponse.newBuilder().setStatus(TransactionOuterClass.TransactionStatus.SEALED).setStatusCode(1).setErrorMessage("message").setBlockId(ByteString.copyFromUtf8("id")).build()
val flowTransactionResult = FlowTransactionResult(FlowTransactionStatus.SEALED, 1, "message", emptyList(), flowId, 1L, flowId, flowId, 1L)

val response = Access.TransactionResultResponse.newBuilder()
.setStatus(TransactionOuterClass.TransactionStatus.SEALED)
.setStatusCode(1)
.setErrorMessage("message")
.setBlockId(ByteString.copyFromUtf8("id"))
.setBlockHeight(1L)
.setTransactionId(ByteString.copyFromUtf8("id"))
.setCollectionId(ByteString.copyFromUtf8("id"))
.setComputationUsage(1L)
.build()

`when`(mockApi.getTransactionResult(any())).thenReturn(response)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.onflow.flow.sdk.models

import com.google.protobuf.ByteString
import org.onflow.flow.sdk.*
import org.onflow.flow.sdk.cadence.CompositeAttribute
import org.onflow.flow.sdk.cadence.CompositeValue
Expand All @@ -26,6 +27,11 @@ class FlowTransactionResultTest {
.setStatus(TransactionOuterClass.TransactionStatus.EXECUTED)
.setStatusCode(statusCode)
.setErrorMessage(errorMessage)
.setBlockId(ByteString.copyFromUtf8("blockId"))
.setBlockHeight(1L)
.setTransactionId(ByteString.copyFromUtf8("transactionId"))
.setCollectionId(ByteString.copyFromUtf8("collectionId"))
.setComputationUsage(1L)
.addAllEvents(events.map { it.builder().build() })

val flowTransactionResult = FlowTransactionResult.of(responseBuilder.build())
Expand All @@ -42,7 +48,19 @@ class FlowTransactionResultTest {
val invalidStatusCode = 1
val errorMessage = "Error message"

val flowTransactionResult = FlowTransactionResult(status, invalidStatusCode, errorMessage, emptyList())
val flowId = FlowId("0x01")

val flowTransactionResult = FlowTransactionResult(
status,
invalidStatusCode,
errorMessage,
emptyList(),
flowId,
1L,
flowId,
flowId,
1L
)

assertThrows<FlowException> { flowTransactionResult.throwOnError() }
}
Expand All @@ -57,11 +75,18 @@ class FlowTransactionResultTest {
val event2 = FlowEvent("type2", FlowId("0x2234"), 0, 0, FlowEventPayload(eventField2))
val event3 = FlowEvent("sub-type1", FlowId("0x3234"), 0, 0, FlowEventPayload(eventField3))

val flowId = FlowId("0x01")

val flowTransactionResult = FlowTransactionResult(
FlowTransactionStatus.SEALED,
0,
"",
listOf(event1, event2, event3)
listOf(event1, event2, event3),
flowId,
1L,
flowId,
flowId,
1L
)

// Events of a specific type
Expand Down

0 comments on commit 86b1d98

Please sign in to comment.