Skip to content

Commit

Permalink
Merge branch 'improve-sync-temporary-blocks' into 'master'
Browse files Browse the repository at this point in the history
Improve sync temporary blocks

See merge request open-platform/chain!377
  • Loading branch information
george-bisiarin committed Sep 6, 2019
2 parents e106528 + c8c1279 commit f7e511b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ class ChainSynchronizer(
}

private fun initSync(message: GenesisBlockMessage) {
syncSession.clearTemporaryBlocks()

val lastLocalGenesisBlock = blockManager.getLastGenesisBlock()
val delegates = lastLocalGenesisBlock.getPayload().activeDelegates
try {
Expand Down
19 changes: 8 additions & 11 deletions src/main/kotlin/io/openfuture/chain/core/sync/SyncSession.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ class SyncSession(
}

val temporaryBlocks = createTemporaryBlocks(epochBlocks)
val lastTemporaryBlock = temporaryBlocks.last()

val lastSavedTemporaryBlock = temporaryBlockService.getByHeightIn(listOf(lastTemporaryBlock.height))
if (null == lastSavedTemporaryBlock.firstOrNull { it.block == lastTemporaryBlock.block }) {
try {
temporaryBlockService.save(temporaryBlocks)
} catch (e: Exception) {
log.warn("Blocks till height ${epochBlocks.last().height} already saved, skiping...")
}

completed = null != epochBlocks.firstOrNull { it.hash == currentGenesisBlock.hash }
Expand All @@ -92,6 +92,10 @@ class SyncSession(
epochAdded = 0
}

fun clearTemporaryBlocks(){
temporaryBlockService.deleteAll()
}

private fun createTemporaryBlocks(blocks: List<Block>): List<TemporaryBlock> =
blocks.map { TemporaryBlock(it.height, ByteUtils.toHexString(SerializationUtils.serialize(it))) }

Expand All @@ -106,14 +110,7 @@ class SyncSession(
}

for (index in 1 until chain.size) {
if (!mainBlockValidator.verify(
chain[index],
chain[index - 1],
chain[index] as MainBlock,
false,
pipeline
)
) {
if (!mainBlockValidator.verify(chain[index], chain[index - 1], chain[index] as MainBlock, false, pipeline)) {
return false
}
}
Expand Down

0 comments on commit f7e511b

Please sign in to comment.