Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#106/ TransactionResult type differs from protobuf schema #116

Merged
merged 10 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions java-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This package contains runnable code examples that use the [Flow JVM SDK](https:/
- [Get accounts](#get-accounts)
- [Get events](#get-events)
- [Get collection](#get-collection)
- [Get execution data](#get-execution-data)
- [Get network parameters](#get-network-parameters)
- [Get transactions](#get-transactions)
- [Sending transactions](#sending-transactions)
Expand Down Expand Up @@ -65,6 +66,10 @@ Below is a list of all Java code examples currently supported in this repo:

[Get collections by ID.](src/main/java/org/onflow/examples/java/getCollection/GetCollectionAccessAPIConnector.java)

#### Get Execution Data

[Get execution data by block ID.](src/main/java/org/onflow/examples/java/getExecutionData/GetExecutionDataAccessAPIConnector.java)

#### Get Network Parameters

[Get the current network parameters.](src/main/java/org/onflow/examples/java/getNetworkParams/GetNetworkParametersAccessAPIConnector.java)
Expand Down
14 changes: 12 additions & 2 deletions kotlin-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This package contains runnable code examples that use the [Flow JVM SDK](https:/
- [Get accounts](#get-accounts)
- [Get events](#get-events)
- [Get collection](#get-collection)
- [Get execution data](#get-execution-data)
- [Get network parameters](#get-network-parameters)
- [Get transactions](#get-transactions)
- [Sending transactions](#sending-transactions)
Expand All @@ -20,6 +21,7 @@ This package contains runnable code examples that use the [Flow JVM SDK](https:/
- [Deploy contract](#deploy-contract)
- [Transaction signing](#transaction-signing)
- [Verifying signatures](#verifying-signatures)
- [Streaming events and execution data](#streaming-events-and-execution-data)

## Running the emulator with the Flow CLI

Expand Down Expand Up @@ -65,6 +67,10 @@ Below is a list of all Kotlin code examples currently supported in this repo:

[Get collections by ID.](src/main/kotlin/org/onflow/examples/kotlin/getCollection/GetCollectionAccessAPIConnector.kt)

#### Get Execution Data

[Get execution data by block ID.](src/main/kotlin/org/onflow/examples/kotlin/getExecutionData/GetExecutionDataAccessAPIConnector.kt)

#### Get Network Parameters

[Get the current network parameters.](src/main/kotlin/org/onflow/examples/kotlin/getNetworkParams/GetNetworkParametersAccessAPIConnector.kt)
Expand Down Expand Up @@ -120,6 +126,10 @@ Below is a list of all Kotlin code examples currently supported in this repo:
- Signing an arbitrary user message and verifying it using the public keys on an account, respecting the weights of each key.
- Signing an arbitrary user message and verifying it using the public keys on an account. Return success if any public key on the account can sign the message.

#### Unsupported Features
#### Streaming Events and Execution Data

[Utilizing the Access API subscription endpoints to stream event and execution data.](src/main/kotlin/org/onflow/examples/kotlin/streaming)

The JVM SDK code examples currently do not support the Access API subscription endpoints (streaming events and execution data), which depend on the Execution Data API (not supported in Flow Emulator). We intend to add these examples as soon as support for these methods is released on the Flow Emulator.
- Streaming events.
- Streaming events and reconnecting in the event of a failure.
- Streaming execution data.
14 changes: 12 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
Comment on lines +270 to +275
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Update the builder method to include new properties

The new properties blockId, blockHeight, transactionId, collectionId, and computationUsage have been added to the FlowTransactionResult data class. However, the builder method has not been updated to include these new properties when building the Access.TransactionResultResponse. This omission can lead to inconsistencies when serializing the object back to its protobuf representation.

Apply this diff to update the builder method:

 @JvmOverloads
 fun builder(builder: Access.TransactionResultResponse.Builder = Access.TransactionResultResponse.newBuilder()): Access.TransactionResultResponse.Builder = builder
     .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() })

Committable suggestion was skipped due to low confidence.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lealobanov what do you think of this suggestion? Seems maybe reasonable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes will do, thanks!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been updated

) : 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 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")
franklywatson marked this conversation as resolved.
Show resolved Hide resolved

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,7 +162,7 @@ class AsyncFlowAccessApiImplTest {
@Test
fun `test getTransactionResultById`() {
val flowId = FlowId.of("id".toByteArray())
val flowTransactionResult = FlowTransactionResult(FlowTransactionStatus.SEALED, 1, "message", emptyList())
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")).build()
`when`(api.getTransactionResult(any())).thenReturn(setupFutureMock(transactionResultResponse))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class FlowAccessApiImplTest {
@Test
fun `Test getTransactionResultById`() {
val flowId = FlowId.of("id".toByteArray())
val flowTransactionResult = FlowTransactionResult(FlowTransactionStatus.SEALED, 1, "message", emptyList())
val flowTransactionResult = FlowTransactionResult(FlowTransactionStatus.SEALED, 1, "message", emptyList(), flowId,1L, flowId, flowId,1L)
lealobanov marked this conversation as resolved.
Show resolved Hide resolved
val response = Access.TransactionResultResponse.newBuilder().setStatus(TransactionOuterClass.TransactionStatus.SEALED).setStatusCode(1).setErrorMessage("message").setBlockId(ByteString.copyFromUtf8("id")).build()

`when`(mockApi.getTransactionResult(any())).thenReturn(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,19 @@ class FlowTransactionResultTest {
val invalidStatusCode = 1
val errorMessage = "Error message"

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

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

assertThrows<FlowException> { flowTransactionResult.throwOnError() }
}
Expand All @@ -57,11 +69,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("0x1")

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
Loading