@@ -5,6 +5,7 @@ import scorex.account.Account
5
5
import scorex .block .Block
6
6
import scorex .block .Block .BlockId
7
7
import scorex .consensus .ConsensusModule
8
+ import scorex .crypto .encode .Base58
8
9
import scorex .transaction .BlockStorage ._
9
10
import scorex .transaction .History .BlockchainScore
10
11
import scorex .transaction .{BlockChain , TransactionModule }
@@ -24,6 +25,15 @@ class StoredBlockchain(db: MVStore)
24
25
case class BlockchainPersistence (database : MVStore ) {
25
26
val blocks : MVMap [Int , Array [Byte ]] = database.openMap(" blocks" )
26
27
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
+
27
37
val scoreMap : MVMap [Int , BigInt ] = database.openMap(" score" )
28
38
29
39
// if there are some uncommited changes from last run, discard'em
@@ -33,21 +43,23 @@ class StoredBlockchain(db: MVStore)
33
43
blocks.put(height, block.bytes)
34
44
scoreMap.put(height, score() + block.consensusModule.blockScore(block)(block.transactionModule))
35
45
signatures.put(height, block.uniqueId)
46
+ signaturesReverse.put(block.uniqueId, height)
36
47
}
37
48
38
49
def readBlock (height : Int ): Option [Block ] =
39
50
Try (Option (blocks.get(height))).toOption.flatten.flatMap(b => Block .parseBytes(b).toOption)
40
51
41
52
def deleteBlock (height : Int ): Unit = {
42
53
blocks.remove(height)
43
- signatures.remove(height)
54
+ val vOpt = Option (signatures.remove(height))
55
+ vOpt.map(v => signaturesReverse.remove(v))
44
56
}
45
57
46
58
def contains (id : BlockId ): Boolean = signatures.exists(_._2.sameElements(id))
47
59
48
60
def height (): Int = signatures.size()
49
61
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))
51
63
52
64
def score (): BlockchainScore = if (height() > 0 ) scoreMap.get(height()) else 0
53
65
0 commit comments