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

Add GetProtocolStateSnapshotByBlockID and GetProtocolStateSnapshotByHeight to the SDK #141

Prev Previous commit
Next Next commit
Lint
lealobanov committed Nov 3, 2024
commit 81934027c861d51562137ff4cd21b5677df84250
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ package org.onflow.examples.kotlin.getNetworkParams
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.onflow.examples.kotlin.getProtocolState.GetProtocolStateAccessAPIConnector
import org.onflow.flow.common.test.FlowEmulatorProjectTest
import org.onflow.flow.common.test.FlowTestClient
import org.onflow.flow.sdk.FlowAccessApi
Original file line number Diff line number Diff line change
@@ -30,7 +30,6 @@ internal class GetProtocolStateAccessAPIConnectorTest {

@Test
fun `Can get protocol state snapshot by blockId`() {

block = when (val response = accessAPI.getLatestBlock()) {
is FlowAccessApi.AccessApiCallResponse.Success -> response.data
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable)
@@ -42,7 +41,6 @@ internal class GetProtocolStateAccessAPIConnectorTest {

@Test
fun `Can get protocol state snapshot by height`() {

block = when (val response = accessAPI.getLatestBlock()) {
is FlowAccessApi.AccessApiCallResponse.Success -> response.data
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable)
Original file line number Diff line number Diff line change
@@ -454,14 +454,28 @@ class AsyncFlowAccessApiImpl(

override fun getProtocolStateSnapshotByHeight(height: Long): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowSnapshot>> =
handleApiCall(
apiCall = { api.getProtocolStateSnapshotByHeight(Access.GetProtocolStateSnapshotByHeightRequest.newBuilder().setBlockHeight(height).build()) },
apiCall = {
api.getProtocolStateSnapshotByHeight(
Access.GetProtocolStateSnapshotByHeightRequest
.newBuilder()
.setBlockHeight(height)
.build()
)
},
transform = { FlowSnapshot(it.serializedSnapshot.toByteArray()) },
errorMessage = "Failed to get protocol state snapshot by height"
)

override fun getProtocolStateSnapshotByBlockId(blockId: FlowId): CompletableFuture<FlowAccessApi.AccessApiCallResponse<FlowSnapshot>> =
handleApiCall(
apiCall = { api.getProtocolStateSnapshotByBlockID(Access.GetProtocolStateSnapshotByBlockIDRequest.newBuilder().setBlockId(blockId.byteStringValue).build()) },
apiCall = {
api.getProtocolStateSnapshotByBlockID(
Access.GetProtocolStateSnapshotByBlockIDRequest
.newBuilder()
.setBlockId(blockId.byteStringValue)
.build()
)
},
transform = { FlowSnapshot(it.serializedSnapshot.toByteArray()) },
errorMessage = "Failed to get protocol state snapshot by block ID"
)
Original file line number Diff line number Diff line change
@@ -581,7 +581,14 @@ class FlowAccessApiImplTest {
fun `Test getProtocolStateSnapshotByBlockId`() {
val mockFlowSnapshot = FlowSnapshot("test_serialized_snapshot".toByteArray())

`when`(mockApi.getProtocolStateSnapshotByBlockID(Access.GetProtocolStateSnapshotByBlockIDRequest.newBuilder().setBlockId(blockId.byteStringValue).build())).thenReturn(AsyncFlowAccessApiImplTest.MockResponseFactory.protocolStateSnapshotResponse())
`when`(
mockApi.getProtocolStateSnapshotByBlockID(
Access.GetProtocolStateSnapshotByBlockIDRequest
.newBuilder()
.setBlockId(blockId.byteStringValue)
.build()
)
).thenReturn(AsyncFlowAccessApiImplTest.MockResponseFactory.protocolStateSnapshotResponse())

val result = flowAccessApiImpl.getProtocolStateSnapshotByBlockId(blockId)
assertResultSuccess(result) { assertEquals(mockFlowSnapshot, it) }
@@ -591,7 +598,14 @@ class FlowAccessApiImplTest {
fun `Test getProtocolStateSnapshotByHeight`() {
val mockFlowSnapshot = FlowSnapshot("test_serialized_snapshot".toByteArray())

`when`(mockApi.getProtocolStateSnapshotByHeight(Access.GetProtocolStateSnapshotByHeightRequest.newBuilder().setBlockHeight(HEIGHT).build())).thenReturn(AsyncFlowAccessApiImplTest.MockResponseFactory.protocolStateSnapshotResponse())
`when`(
mockApi.getProtocolStateSnapshotByHeight(
Access.GetProtocolStateSnapshotByHeightRequest
.newBuilder()
.setBlockHeight(HEIGHT)
.build()
)
).thenReturn(AsyncFlowAccessApiImplTest.MockResponseFactory.protocolStateSnapshotResponse())

val result = flowAccessApiImpl.getProtocolStateSnapshotByHeight(HEIGHT)
assertResultSuccess(result) { assertEquals(mockFlowSnapshot, it) }