Skip to content

Commit e749918

Browse files
committed
Add retry mechanism to getLatestBlock
1 parent 4e3652c commit e749918

File tree

5 files changed

+37
-38
lines changed

5 files changed

+37
-38
lines changed

kotlin-example/src/test/kotlin/org/onflow/examples/kotlin/AccessAPIConnectorTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import java.math.BigDecimal
1515

1616
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json")
1717
internal class AccessAPIConnectorTest {
18+
1819
// user key pairs using all supported signing algorithms
1920
private val userKeyPairs = arrayOf(
2021
Crypto.generateKeyPair(SignatureAlgorithm.ECDSA_P256),
@@ -62,6 +63,7 @@ internal class AccessAPIConnectorTest {
6263
accessAPIConnector.transferTokens(sender, to, amount)
6364
}
6465

66+
6567
@Test
6668
fun `Can transfer tokens to other account`() {
6769
val amount = BigDecimal("10.00000001")

kotlin-example/src/test/kotlin/org/onflow/examples/kotlin/getAccountBalance/GetAccountBalanceAccessAPIConnectorTest.kt

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package org.onflow.examples.kotlin.getAccountBalance
33
import org.junit.jupiter.api.Assertions
44
import org.junit.jupiter.api.BeforeEach
55
import org.junit.jupiter.api.Test
6+
import org.onflow.examples.kotlin.getTransaction.GetTransactionAccessAPIConnectorTest
67
import org.onflow.flow.common.test.FlowEmulatorProjectTest
78
import org.onflow.flow.common.test.FlowServiceAccountCredentials
89
import org.onflow.flow.common.test.FlowTestClient
910
import org.onflow.flow.common.test.TestAccount
1011
import org.onflow.flow.sdk.FlowAccessApi
12+
import org.onflow.flow.sdk.FlowBlock
1113

1214
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json")
1315
internal class GetAccountBalanceAccessAPIConnectorTest {
@@ -18,10 +20,12 @@ internal class GetAccountBalanceAccessAPIConnectorTest {
1820
lateinit var accessAPI: FlowAccessApi
1921

2022
private lateinit var balanceAPIConnector: GetAccountBalanceAccessAPIConnector
23+
private lateinit var latestBlock: FlowBlock
2124

2225
@BeforeEach
2326
fun setup() {
2427
balanceAPIConnector = GetAccountBalanceAccessAPIConnector(accessAPI)
28+
latestBlock = GetTransactionAccessAPIConnectorTest.fetchLatestBlockWithRetries(accessAPI)
2529
}
2630

2731
@Test
@@ -36,17 +40,10 @@ internal class GetAccountBalanceAccessAPIConnectorTest {
3640
@Test
3741
fun `Can fetch account balance at a specific block height`() {
3842
val address = serviceAccount.flowAddress
39-
val latestBlock = accessAPI.getLatestBlock(true) // Fetch the latest sealed block
43+
val balanceAtHeight = balanceAPIConnector.getBalanceAtBlockHeight(address, latestBlock.height)
4044

41-
when (latestBlock) {
42-
is FlowAccessApi.AccessApiCallResponse.Success -> {
43-
val balanceAtHeight = balanceAPIConnector.getBalanceAtBlockHeight(address, latestBlock.data.height)
44-
45-
Assertions.assertNotNull(balanceAtHeight, "Balance at specific block height should not be null")
46-
Assertions.assertTrue(balanceAtHeight >= 0, "Balance at specific block height should be non-negative")
47-
}
48-
is FlowAccessApi.AccessApiCallResponse.Error -> Assertions.fail("Failed to retrieve the latest block: ${latestBlock.message}")
49-
}
45+
Assertions.assertNotNull(balanceAtHeight, "Balance at specific block height should not be null")
46+
Assertions.assertTrue(balanceAtHeight >= 0, "Balance at specific block height should be non-negative")
5047
}
5148

5249
@Test
@@ -56,18 +53,11 @@ internal class GetAccountBalanceAccessAPIConnectorTest {
5653
// Fetch balance at latest block
5754
val balanceAtLatest = balanceAPIConnector.getBalanceAtLatestBlock(address)
5855

59-
// Fetch latest block height
60-
val latestBlock = accessAPI.getLatestBlock(true)
61-
when (latestBlock) {
62-
is FlowAccessApi.AccessApiCallResponse.Success -> {
63-
val blockHeight = latestBlock.data.height
56+
val blockHeight = latestBlock.height
6457

65-
// Fetch balance at the same block height
66-
val balanceAtHeight = balanceAPIConnector.getBalanceAtBlockHeight(address, blockHeight)
58+
// Fetch balance at the same block height
59+
val balanceAtHeight = balanceAPIConnector.getBalanceAtBlockHeight(address, blockHeight)
6760

68-
Assertions.assertEquals(balanceAtLatest, balanceAtHeight, "Balance at latest block and specific block height should match")
69-
}
70-
is FlowAccessApi.AccessApiCallResponse.Error -> Assertions.fail("Failed to retrieve the latest block: ${latestBlock.message}")
71-
}
61+
Assertions.assertEquals(balanceAtLatest, balanceAtHeight, "Balance at latest block and specific block height should match")
7262
}
7363
}

kotlin-example/src/test/kotlin/org/onflow/examples/kotlin/getCollection/GetCollectionAccessAPIConnectorTest.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.junit.jupiter.api.Assertions.*
44
import org.junit.jupiter.api.BeforeEach
55
import org.junit.jupiter.api.Test
66
import org.onflow.examples.kotlin.AccessAPIConnector
7+
import org.onflow.examples.kotlin.getTransaction.GetTransactionAccessAPIConnectorTest
78
import org.onflow.flow.common.test.FlowEmulatorProjectTest
89
import org.onflow.flow.common.test.FlowServiceAccountCredentials
910
import org.onflow.flow.common.test.FlowTestClient
@@ -23,6 +24,7 @@ class GetCollectionAccessAPIConnectorTest {
2324
private lateinit var accessAPIConnector: AccessAPIConnector
2425

2526
private lateinit var collectionId: FlowId
27+
lateinit var block: FlowBlock
2628

2729
@BeforeEach
2830
fun setup() {
@@ -36,10 +38,7 @@ class GetCollectionAccessAPIConnectorTest {
3638
publicKey
3739
)
3840

39-
val block = when (val response = accessAPI.getLatestBlock()) {
40-
is FlowAccessApi.AccessApiCallResponse.Success -> response.data
41-
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable)
42-
}
41+
block = GetTransactionAccessAPIConnectorTest.fetchLatestBlockWithRetries(accessAPI)
4342
collectionId = block.collectionGuarantees.first().id
4443
}
4544

kotlin-example/src/test/kotlin/org/onflow/examples/kotlin/getProtocolState/GetProtocolStateAccessAPIConnectorTest.kt

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.onflow.examples.kotlin.getProtocolState
33
import org.junit.jupiter.api.Assertions.*
44
import org.junit.jupiter.api.BeforeEach
55
import org.junit.jupiter.api.Test
6+
import org.onflow.examples.kotlin.getTransaction.GetTransactionAccessAPIConnectorTest
67
import org.onflow.flow.common.test.FlowEmulatorProjectTest
78
import org.onflow.flow.common.test.FlowTestClient
89
import org.onflow.flow.sdk.FlowAccessApi
@@ -20,6 +21,7 @@ internal class GetProtocolStateAccessAPIConnectorTest {
2021
@BeforeEach
2122
fun setup() {
2223
protocolStateConnector = GetProtocolStateAccessAPIConnector(accessAPI)
24+
block = GetTransactionAccessAPIConnectorTest.fetchLatestBlockWithRetries(accessAPI)
2325
}
2426

2527
@Test
@@ -30,21 +32,12 @@ internal class GetProtocolStateAccessAPIConnectorTest {
3032

3133
@Test
3234
fun `Can get protocol state snapshot by blockId`() {
33-
block = when (val response = accessAPI.getLatestBlock()) {
34-
is FlowAccessApi.AccessApiCallResponse.Success -> response.data
35-
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable)
36-
}
37-
3835
val latestSnapshot: FlowSnapshot = protocolStateConnector.getProtocolStateSnapshotByBlockId(block.id)
3936
assertNotNull(latestSnapshot, ("Snapshot should not be null"))
4037
}
4138

4239
@Test
4340
fun `Can get protocol state snapshot by height`() {
44-
block = when (val response = accessAPI.getLatestBlock()) {
45-
is FlowAccessApi.AccessApiCallResponse.Success -> response.data
46-
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable)
47-
}
4841

4942
val latestSnapshot: FlowSnapshot = protocolStateConnector.getProtocolStateSnapshotByHeight(block.height)
5043
assertNotNull(latestSnapshot, ("Snapshot should not be null"))

kotlin-example/src/test/kotlin/org/onflow/examples/kotlin/getTransaction/GetTransactionAccessAPIConnectorTest.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.onflow.examples.kotlin.getTransaction
22

3+
import kotlinx.coroutines.delay
4+
import kotlinx.coroutines.runBlocking
35
import org.junit.jupiter.api.Assertions.*
46
import org.junit.jupiter.api.BeforeEach
57
import org.junit.jupiter.api.Test
68
import org.onflow.examples.kotlin.AccessAPIConnector
9+
import org.onflow.examples.kotlin.AccessAPIConnectorTest
710
import org.onflow.flow.common.test.FlowEmulatorProjectTest
811
import org.onflow.flow.common.test.FlowServiceAccountCredentials
912
import org.onflow.flow.common.test.FlowTestClient
@@ -37,10 +40,7 @@ internal class GetTransactionAccessAPIConnectorTest {
3740
publicKey
3841
)
3942

40-
block = when (val response = accessAPI.getLatestBlock()) {
41-
is FlowAccessApi.AccessApiCallResponse.Success -> response.data
42-
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable)
43-
}
43+
block = fetchLatestBlockWithRetries(accessAPI)
4444
}
4545

4646
@Test
@@ -66,4 +66,19 @@ internal class GetTransactionAccessAPIConnectorTest {
6666
assertNotNull(transactionResult, "Transaction result should not be null")
6767
assertTrue(transactionResult.status === FlowTransactionStatus.SEALED, "Transaction should be sealed")
6868
}
69+
70+
companion object {
71+
fun fetchLatestBlockWithRetries(accessAPI : FlowAccessApi, retries: Int = 5, delayMillis: Long = 500): FlowBlock {
72+
repeat(retries) { attempt ->
73+
when (val response = accessAPI.getLatestBlock()) {
74+
is FlowAccessApi.AccessApiCallResponse.Success -> return response.data
75+
is FlowAccessApi.AccessApiCallResponse.Error -> {
76+
println("Attempt ${attempt + 1} failed: ${response.message}. Retrying...")
77+
runBlocking { delay(delayMillis) }
78+
}
79+
}
80+
}
81+
throw Exception("Failed to retrieve the latest block after $retries attempts.")
82+
}
83+
}
6984
}

0 commit comments

Comments
 (0)