Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #207 from FigureTechnologies/piercetrey-figure/add…
Browse files Browse the repository at this point in the history
…-initial-height-query

add initial height query to block fetcher (by querying genesis)
  • Loading branch information
celloman authored Dec 28, 2023
2 parents 6990f2c + 1169338 commit 9b08356
Show file tree
Hide file tree
Showing 9 changed files with 810 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,7 @@ components:
- "chain_id"
- "initial_height"
- "consensus_params"
- "validators"
# - "validators" # for some reason this schema has this as required, but it comes back as empty from the chain... maybe this is just out of date?
- "app_hash"
properties:
genesis_time:
Expand Down Expand Up @@ -2938,11 +2938,19 @@ components:
evidence:
type: object
required:
- "max_age"
- "max_age_num_blocks"
- "max_age_duration"
- "max_bytes"
properties:
max_age:
max_age_num_blocks: # Manually updated these properties to reflect the current properties here in cosmos/tendermint
type: string
example: "100000"
max_age_duration:
type: string
example: "172800000000000"
max_bytes:
type: string
example: "1048576"
validator:
type: object
required:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ open class BlockFetchException(m: String) : Exception(m)
interface BlockFetcher {
suspend fun getBlocksMeta(min: Long, max: Long): List<BlockMeta>?
suspend fun getCurrentHeight(): Long?
suspend fun getInitialHeight(): Long
suspend fun getBlock(height: Long): BlockData
suspend fun getBlockResults(height: Long): BlockResultsResponse?
suspend fun getBlocks(heights: List<Long>, concurrency: Int = DEFAULT_CONCURRENCY, context: CoroutineContext = EmptyCoroutineContext): Flow<BlockData>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class TendermintBlockFetcher(
?: throw BlockFetchException("failed to fetch current block height")
}

override suspend fun getInitialHeight(): Long {
return tendermintServiceClient.genesis().result.genesis.initialHeight
}

override suspend fun getBlock(height: Long): BlockData {
log.trace("getBlock($height)")
val block = tendermintServiceClient.block(height).result?.block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import tech.figure.eventstream.stream.models.ABCIInfoResponse
import tech.figure.eventstream.stream.models.BlockResponse
import tech.figure.eventstream.stream.models.BlockResultsResponse
import tech.figure.eventstream.stream.models.BlockchainResponse
import tech.figure.eventstream.stream.models.GenesisResponse

/**
* A client designed to interact with the Tendermint RPC API.
*/
interface TendermintServiceClient {
suspend fun abciInfo(): ABCIInfoResponse
suspend fun genesis(): GenesisResponse
suspend fun block(height: Long?): BlockResponse
suspend fun blockResults(height: Long?): BlockResultsResponse
suspend fun blockchain(minHeight: Long?, maxHeight: Long?): BlockchainResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import tech.figure.eventstream.stream.models.ABCIInfoResponse
import tech.figure.eventstream.stream.models.BlockResponse
import tech.figure.eventstream.stream.models.BlockResultsResponse
import tech.figure.eventstream.stream.models.BlockchainResponse
import tech.figure.eventstream.stream.models.GenesisResponse

/**
* An OpenAPI generated client designed to interact with the Tendermint RPC API.
Expand All @@ -30,6 +31,8 @@ class TendermintServiceOpenApiClient(

override suspend fun abciInfo(): ABCIInfoResponse = abciApi.abciInfo()

override suspend fun genesis(): GenesisResponse = infoApi.genesis()

override suspend fun block(height: Long?): BlockResponse = infoApi.block(height)

override suspend fun blockResults(height: Long?): BlockResultsResponse = infoApi.blockResults(height)
Expand Down
22 changes: 22 additions & 0 deletions es-core/src/test/kotlin/tech/figure/eventstream/StreamTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.assertThrows
import tech.figure.eventstream.mocks.MockEventStreamService
import tech.figure.eventstream.stream.clients.TendermintServiceClient
import tech.figure.eventstream.stream.models.GenesisResponse

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class StreamTests : TestBase() {
Expand Down Expand Up @@ -153,6 +154,27 @@ class StreamTests : TestBase() {
}
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun testGenesisResponse() {
val expectedInitialHeight = 10000L

val tm = ServiceMocker.Builder()
.doFor("genesis") {
templates.readAs(
GenesisResponse::class.java,
"genesis/success.json",
mapOf("initial_height" to expectedInitialHeight),
)
}
.build(MockTendermintServiceClient::class.java)

dispatcherProvider.runBlockingTest {
val initialHeight = tm.genesis().result.genesis.initialHeight
assert(initialHeight == expectedInitialHeight) { "expected: $expectedInitialHeight received: $initialHeight" }
}
}

// duped
@OptIn(ExperimentalCoroutinesApi::class)
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import tech.figure.eventstream.utils.Template
import io.reactivex.Flowable
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.asFlow
import tech.figure.eventstream.stream.models.GenesisResponse
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.coroutines.CoroutineContext

Expand Down Expand Up @@ -90,6 +91,11 @@ fun mockBlockFetcher(template: Template, currentHeight: Long? = null): BlockFetc
return response!!.result!!.response.lastBlockHeight!!
}

override suspend fun getInitialHeight(): Long {
val response = template.readAs(GenesisResponse::class.java, "genesis/success.json")
return response!!.result.genesis.initialHeight
}

override suspend fun getBlock(height: Long): BlockData {
val block = template.readAs(BlockResponse::class.java, "block/$height.json")
val results = template.readAs(BlockResultsResponse::class.java, "block_results/$height.json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import tech.figure.eventstream.stream.models.BlockResultsResponse
import tech.figure.eventstream.stream.models.BlockchainResponse
import tech.figure.eventstream.stream.clients.TendermintServiceClient
import tech.figure.eventstream.stream.models.ABCIInfoResponse
import tech.figure.eventstream.stream.models.GenesisResponse

class MockTendermintServiceClient(mocker: ServiceMock) : TendermintServiceClient, ServiceMock by mocker {

override suspend fun abciInfo() =
respondWith<ABCIInfoResponse>("abciInfo")

override suspend fun genesis() = respondWith<GenesisResponse>("genesis")

override suspend fun block(height: Long?) =
respondWith<BlockResponse>("block", height)

Expand Down
Loading

0 comments on commit 9b08356

Please sign in to comment.