Skip to content

Commit 635b1c9

Browse files
committed
Performance update: reverse index for block height
1 parent 43482d3 commit 635b1c9

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

scorex-consensus/src/main/scala/scorex/consensus/nxt/NxtLikeConsensusModule.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class NxtLikeConsensusModule(AvgDelay: Duration = 5.seconds)
140140
}
141141
}
142142

143-
private def calcTarget(prevBlock: Block,
143+
protected def calcTarget(prevBlock: Block,
144144
timestamp: Long,
145145
effBalance: Long)(implicit transactionModule: TransactionModule[_]): BigInt = {
146146
val prevBlockData = consensusBlockData(prevBlock)

scorex-transaction/src/main/scala/scorex/transaction/state/database/blockchain/StoredBlockchain.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import scorex.account.Account
55
import scorex.block.Block
66
import scorex.block.Block.BlockId
77
import scorex.consensus.ConsensusModule
8+
import scorex.crypto.encode.Base58
89
import scorex.transaction.BlockStorage._
910
import scorex.transaction.History.BlockchainScore
1011
import scorex.transaction.{BlockChain, TransactionModule}
@@ -24,6 +25,15 @@ class StoredBlockchain(db: MVStore)
2425
case class BlockchainPersistence(database: MVStore) {
2526
val blocks: MVMap[Int, Array[Byte]] = database.openMap("blocks")
2627
val signatures: MVMap[Int, BlockId] = database.openMap("signatures")
28+
val signaturesReverse: MVMap[BlockId, Int] = database.openMap("signaturesReverse")
29+
30+
//TOOD remove when no blockchains without signaturesReverse remains
31+
if (signaturesReverse.size() != signatures.size()) {
32+
signaturesReverse.clear()
33+
signatures.keySet().foreach(k => signaturesReverse.put(signatures.get(k), k))
34+
database.commit()
35+
}
36+
2737
val scoreMap: MVMap[Int, BigInt] = database.openMap("score")
2838

2939
//if there are some uncommited changes from last run, discard'em
@@ -33,21 +43,23 @@ class StoredBlockchain(db: MVStore)
3343
blocks.put(height, block.bytes)
3444
scoreMap.put(height, score() + block.consensusModule.blockScore(block)(block.transactionModule))
3545
signatures.put(height, block.uniqueId)
46+
signaturesReverse.put(block.uniqueId, height)
3647
}
3748

3849
def readBlock(height: Int): Option[Block] =
3950
Try(Option(blocks.get(height))).toOption.flatten.flatMap(b => Block.parseBytes(b).toOption)
4051

4152
def deleteBlock(height: Int): Unit = {
4253
blocks.remove(height)
43-
signatures.remove(height)
54+
val vOpt = Option(signatures.remove(height))
55+
vOpt.map(v => signaturesReverse.remove(v))
4456
}
4557

4658
def contains(id: BlockId): Boolean = signatures.exists(_._2.sameElements(id))
4759

4860
def height(): Int = signatures.size()
4961

50-
def heightOf(id: BlockId): Option[Int] = signatures.find(_._2.sameElements(id)).map(_._1)
62+
def heightOf(id: BlockId): Option[Int] = Option(signaturesReverse.get(id))
5163

5264
def score(): BlockchainScore = if (height() > 0) scoreMap.get(height()) else 0
5365

0 commit comments

Comments
 (0)