Skip to content

Commit

Permalink
Merge branch 'master' into smart-contract
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
  • Loading branch information
anna.shunko committed Nov 29, 2018
2 parents 2220284 + 8950f96 commit 5310132
Show file tree
Hide file tree
Showing 46 changed files with 569 additions and 429 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Smart contract: loading contract to JVM
- Smart contract: simple contact validation

## Changed
- Core: chain synchronization improved (request for chain of blocks instead of last block only)
- Core: time synchronization improved (based on time-synchronized nodes)


## [1.3.0] - 2018-11-16
## Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import io.openfuture.chain.consensus.service.EpochService
import io.openfuture.chain.core.component.NodeKeyHolder
import io.openfuture.chain.core.service.GenesisBlockService
import io.openfuture.chain.core.service.MainBlockService
import io.openfuture.chain.core.sync.SyncState
import io.openfuture.chain.core.sync.SyncState.SyncStatusType.SYNCHRONIZED
import io.openfuture.chain.core.sync.SyncManager
import io.openfuture.chain.core.sync.SyncStatus.SYNCHRONIZED
import io.openfuture.chain.network.component.time.ClockSynchronizer
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
Expand All @@ -23,7 +24,8 @@ class BlockProductionScheduler(
private val genesisBlockService: GenesisBlockService,
private val pendingBlockHandler: PendingBlockHandler,
private val consensusProperties: ConsensusProperties,
private val syncState: SyncState
private val syncManager: SyncManager,
private val clockSynchronizer: ClockSynchronizer
) {

companion object {
Expand All @@ -41,11 +43,14 @@ class BlockProductionScheduler(

private fun proceedProductionLoop() {
try {
if (SYNCHRONIZED != syncState.getNodeStatus()) {
log.warn("Node is not synchronized")
if (SYNCHRONIZED != clockSynchronizer.getStatus() || SYNCHRONIZED != syncManager.getStatus()) {
log.debug("----------------Clock is ${clockSynchronizer.getStatus()}----------------")
log.debug("----------------Ledger is ${syncManager.getStatus()}----------------")
return
}

val slotOwner = epochService.getCurrentSlotOwner()
log.debug("CONSENSUS: Slot owner ${slotOwner.id}")
if (genesisBlockService.isGenesisBlockRequired()) {
val genesisBlock = genesisBlockService.create()
genesisBlockService.add(genesisBlock)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import io.openfuture.chain.network.message.consensus.BlockApprovalMessage
import io.openfuture.chain.network.message.consensus.PendingBlockMessage
import io.openfuture.chain.network.service.NetworkApiService
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component

@Component
Expand All @@ -22,6 +24,10 @@ class DefaultPendingBlockHandler(
private val networkService: NetworkApiService
) : PendingBlockHandler {

companion object {
private val log: Logger = LoggerFactory.getLogger(DefaultPendingBlockHandler::class.java)
}

private val pendingBlocks: MutableSet<PendingBlockMessage> = mutableSetOf()
private val prepareVotes: MutableMap<String, Delegate> = mutableMapOf()
private val commits: MutableMap<String, MutableList<Delegate>> = mutableMapOf()
Expand Down Expand Up @@ -103,7 +109,10 @@ class DefaultPendingBlockHandler(
blockCommits.add(delegate)
networkService.broadcast(message)
if (blockCommits.size > (delegates.size / 3 * 2) && !blockAddedFlag) {
pendingBlocks.find { it.hash == message.hash }?.let { mainBlockService.add(it) }
pendingBlocks.find { it.hash == message.hash }?.let {
log.debug("CONSENSUS: Saving main block ${it.hash}")
mainBlockService.add(it)
}
blockAddedFlag = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@ package io.openfuture.chain.core.aspect

import io.openfuture.chain.core.annotation.BlockchainSynchronized
import io.openfuture.chain.core.exception.SynchronizationException
import io.openfuture.chain.core.sync.SyncState
import io.openfuture.chain.core.sync.SyncState.SyncStatusType.SYNCHRONIZED
import io.openfuture.chain.core.sync.SyncManager
import io.openfuture.chain.core.sync.SyncStatus.SYNCHRONIZED
import io.openfuture.chain.network.component.time.ClockSynchronizer
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.springframework.stereotype.Component


@Aspect
@Component
class BlockchainSynchronizationAspect(
private val syncStatus: SyncState
class NodeSyncAspect(
private val syncManager: SyncManager,
private val clockSynchronizer: ClockSynchronizer
) {

@Before("@annotation(annotation)")
fun annotated(annotation: BlockchainSynchronized) {
if (syncStatus.getChainStatus() != SYNCHRONIZED) {
throw SynchronizationException("Application is not synchronized! Current sync status: ${syncStatus.getChainStatus()}!")
if (SYNCHRONIZED != syncManager.getStatus() || SYNCHRONIZED != clockSynchronizer.getStatus()) {
throw SynchronizationException("Application is not synchronized!")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ interface BlockRepository<Entity : Block> : BaseRepository<Entity> {

fun findOneByHash(hash: String): Entity?

fun findOneByHashAndHeight(hash: String, height: Long): Entity?

fun findFirstByOrderByHeightDesc(): Entity?

fun findFirstByHeightLessThanOrderByHeightDesc(height: Long): Entity?
Expand All @@ -39,6 +41,8 @@ interface BlockRepository<Entity : Block> : BaseRepository<Entity> {
@Query(value = "Select HEIGHT From BLOCKS Order By HEIGHT Desc Limit 1", nativeQuery = true)
fun getCurrentHeight(): Long

fun findTop30ByHeightGreaterThanOrderByHeightDesc(height: Long): List<Entity>

}

@Repository
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/io/openfuture/chain/core/service/Services.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ interface BlockService {
fun getAvgProductionTime(): Long

fun getCurrentHeight(): Long

fun isExists(hash: String, height: Long): Boolean

fun getCarcassForBlockSync(hash: String): List<Block>

}

interface GenesisBlockService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ class DefaultBlockService(
@Transactional(readOnly = true)
override fun isExists(hash: String): Boolean = repository.findOneByHash(hash)?.let { true } ?: false

@Transactional(readOnly = true)
override fun isExists(hash: String, height: Long): Boolean =
repository.findOneByHashAndHeight(hash, height)?.let { true } ?: false

@Transactional(readOnly = true)
override fun getCurrentHeight(): Long = repository.getCurrentHeight()

@Transactional(readOnly = true)
override fun getCarcassForBlockSync(hash: String): List<Block> {
val startBlock = repository.findOneByHash(hash) ?: return emptyList()
return repository.findTop30ByHeightGreaterThanOrderByHeightDesc(startBlock.height)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import io.openfuture.chain.core.service.DefaultDelegateService
import io.openfuture.chain.core.service.GenesisBlockService
import io.openfuture.chain.core.service.WalletService
import io.openfuture.chain.core.sync.BlockchainLock
import io.openfuture.chain.core.sync.SyncState
import io.openfuture.chain.core.sync.SyncState.SyncStatusType.NOT_SYNCHRONIZED
import io.openfuture.chain.core.sync.SyncManager
import io.openfuture.chain.crypto.util.SignatureUtils
import io.openfuture.chain.network.message.sync.GenesisBlockMessage
import io.openfuture.chain.rpc.domain.base.PageRequest
Expand All @@ -29,7 +28,7 @@ class DefaultGenesisBlockService(
delegateService: DefaultDelegateService,
repository: GenesisBlockRepository,
private val keyHolder: NodeKeyHolder,
private val syncStatus: SyncState,
private val syncManager: SyncManager,
private val consensusProperties: ConsensusProperties
) : BaseBlockService<GenesisBlock>(repository, blockService, walletService, delegateService), GenesisBlockService {

Expand Down Expand Up @@ -82,6 +81,7 @@ class DefaultGenesisBlockService(
}

@Transactional
@Synchronized
override fun add(message: GenesisBlockMessage) {
if (null != repository.findOneByHash(message.hash)) {
return
Expand All @@ -91,7 +91,7 @@ class DefaultGenesisBlockService(
val block = GenesisBlock.of(message, delegates)

if (!isSync(block)) {
syncStatus.setChainStatus(NOT_SYNCHRONIZED)
syncManager.outOfSync()
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import io.openfuture.chain.core.model.entity.transaction.unconfirmed.Unconfirmed
import io.openfuture.chain.core.repository.MainBlockRepository
import io.openfuture.chain.core.service.*
import io.openfuture.chain.core.sync.BlockchainLock
import io.openfuture.chain.core.sync.SyncState
import io.openfuture.chain.core.sync.SyncState.SyncStatusType.NOT_SYNCHRONIZED
import io.openfuture.chain.core.sync.SyncManager
import io.openfuture.chain.crypto.util.HashUtils
import io.openfuture.chain.crypto.util.SignatureUtils
import io.openfuture.chain.network.component.time.Clock
Expand Down Expand Up @@ -46,7 +45,7 @@ class DefaultMainBlockService(
private val transferTransactionService: TransferTransactionService,
private val rewardTransactionService: RewardTransactionService,
private val consensusProperties: ConsensusProperties,
private val syncStatus: SyncState,
private val syncManager: SyncManager,
private val throughput: TransactionThroughput
) : BaseBlockService<MainBlock>(repository, blockService, walletService, delegateService), MainBlockService {

Expand Down Expand Up @@ -118,7 +117,7 @@ class DefaultMainBlockService(
BlockchainLock.readLock.lock()
try {
if (!isSync(MainBlock.of(message))) {
syncStatus.setChainStatus(NOT_SYNCHRONIZED)
syncManager.outOfSync()
return false
}

Expand All @@ -134,6 +133,7 @@ class DefaultMainBlockService(
}

@Transactional
@Synchronized
override fun add(message: BaseMainBlockMessage) {
BlockchainLock.writeLock.lock()
try {
Expand All @@ -143,7 +143,7 @@ class DefaultMainBlockService(

val block = MainBlock.of(message)
if (!isSync(block)) {
syncStatus.setChainStatus(NOT_SYNCHRONIZED)
syncManager.outOfSync()
return
}

Expand Down

This file was deleted.

Loading

0 comments on commit 5310132

Please sign in to comment.