Skip to content

Commit b8854d1

Browse files
authored
Bug fix: Shouldn't roll back the genesis block (#89)
1 parent 9b256d8 commit b8854d1

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jacoco {
2020

2121
group = "org.vechain"
2222

23-
val projectVersion = System.getenv("PROJECT_VERSION") ?: "6.3.0"
23+
val projectVersion = System.getenv("PROJECT_VERSION") ?: "6.3.1"
2424
version = projectVersion
2525

2626
val isSnapshot = version.toString().endsWith("SNAPSHOT")

src/main/kotlin/org.vechain.indexer/BlockIndexer.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,14 @@ open class BlockIndexer(
8888
/**
8989
* Rolls back to a safe state to ensure data integrity.
9090
*
91+
* Only rolls back if the block number is greater than zero.
92+
*
9193
* @param blockNumber The block number to roll back to.
9294
*/
9395
protected open fun rollbackToSafeState(blockNumber: Long) {
94-
rollback(blockNumber)
96+
if (blockNumber > 0) {
97+
rollback(blockNumber)
98+
}
9599
}
96100

97101
/**

src/test/kotlin/org/vechain/indexer/BlockIndexerTest.kt

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,8 @@ internal class BlockIndexerTest {
205205

206206
indexer.initialise()
207207

208-
verify(exactly = 2) {
209-
processor.getLastSyncedBlock()
210-
} // Verify the rollback is performed once
211-
verify(exactly = 1) { processor.rollback(0L) }
208+
verify(exactly = 2) { processor.getLastSyncedBlock() }
209+
verify(exactly = 0) { processor.rollback(any()) }
212210

213211
expect {
214212
// Verify the status is INITIALISED
@@ -221,6 +219,29 @@ internal class BlockIndexerTest {
221219
}
222220
}
223221

222+
@Test
223+
fun `Should rollback to startBlock when startBlock is greater than zero and no last synced block`() =
224+
runBlocking {
225+
indexer =
226+
BlockIndexer(
227+
name = "TestBlockIndexer",
228+
thorClient = thorClient,
229+
processor = processor,
230+
startBlock = 50L,
231+
eventProcessor = null,
232+
pruner = null,
233+
prunerInterval = 10000L,
234+
syncLoggerInterval = 1L,
235+
inspectionClauses = null,
236+
dependsOn = null,
237+
)
238+
every { processor.getLastSyncedBlock() } returns null
239+
240+
indexer.initialise()
241+
242+
verify(exactly = 1) { processor.rollback(50L) }
243+
}
244+
224245
@Test
225246
fun `Previous block should be null if last synced block doesn't match current block - 1`() {
226247
every { processor.getLastSyncedBlock() } returns

0 commit comments

Comments
 (0)