Skip to content

Commit f7e511b

Browse files
Merge branch 'improve-sync-temporary-blocks' into 'master'
Improve sync temporary blocks See merge request open-platform/chain!377
2 parents e106528 + c8c1279 commit f7e511b

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

src/main/kotlin/io/openfuture/chain/core/sync/ChainSynchronizer.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ class ChainSynchronizer(
151151
}
152152

153153
private fun initSync(message: GenesisBlockMessage) {
154+
syncSession.clearTemporaryBlocks()
155+
154156
val lastLocalGenesisBlock = blockManager.getLastGenesisBlock()
155157
val delegates = lastLocalGenesisBlock.getPayload().activeDelegates
156158
try {

src/main/kotlin/io/openfuture/chain/core/sync/SyncSession.kt

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ class SyncSession(
6666
}
6767

6868
val temporaryBlocks = createTemporaryBlocks(epochBlocks)
69-
val lastTemporaryBlock = temporaryBlocks.last()
7069

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

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

95+
fun clearTemporaryBlocks(){
96+
temporaryBlockService.deleteAll()
97+
}
98+
9599
private fun createTemporaryBlocks(blocks: List<Block>): List<TemporaryBlock> =
96100
blocks.map { TemporaryBlock(it.height, ByteUtils.toHexString(SerializationUtils.serialize(it))) }
97101

@@ -106,14 +110,7 @@ class SyncSession(
106110
}
107111

108112
for (index in 1 until chain.size) {
109-
if (!mainBlockValidator.verify(
110-
chain[index],
111-
chain[index - 1],
112-
chain[index] as MainBlock,
113-
false,
114-
pipeline
115-
)
116-
) {
113+
if (!mainBlockValidator.verify(chain[index], chain[index - 1], chain[index] as MainBlock, false, pipeline)) {
117114
return false
118115
}
119116
}

0 commit comments

Comments
 (0)