1
1
package io .iohk .ethereum .db .storage
2
2
3
+ import java .math .BigInteger
4
+
3
5
import io .iohk .ethereum .db .dataSource .{DataSource , DataSourceBatchUpdate }
4
6
import io .iohk .ethereum .db .storage .AppStateStorage ._
5
7
@@ -8,18 +10,15 @@ import io.iohk.ethereum.db.storage.AppStateStorage._
8
10
* Key: see AppStateStorage.Keys
9
11
* Value: stored string value
10
12
*/
11
- class AppStateStorage (val dataSource : DataSource ) extends TransactionalKeyValueStorage [Key , Value ]{
12
- type T = AppStateStorage
13
+ class AppStateStorage (val dataSource : DataSource ) extends TransactionalKeyValueStorage [Key , Value ] {
13
14
14
15
val namespace : IndexedSeq [Byte ] = Namespaces .AppStateNamespace
15
- def keySerializer : Key => IndexedSeq [Byte ] = _.name. getBytes
16
+ def keySerializer : Key => IndexedSeq [Byte ] = _.getBytes
16
17
def valueSerializer : String => IndexedSeq [Byte ] = _.getBytes
17
18
def valueDeserializer : IndexedSeq [Byte ] => String = (valueBytes : IndexedSeq [Byte ]) => new String (valueBytes.toArray)
18
19
19
- protected def apply (dataSource : DataSource ): AppStateStorage = new AppStateStorage (dataSource)
20
-
21
20
def getBestBlockNumber (): BigInt =
22
- BigInt (get( Keys .BestBlockNumber ).getOrElse( " 0 " ) )
21
+ getBigInt( Keys .BestBlockNumber )
23
22
24
23
def putBestBlockNumber (bestBlockNumber : BigInt ): DataSourceBatchUpdate =
25
24
put(Keys .BestBlockNumber , bestBlockNumber.toString)
@@ -31,27 +30,48 @@ class AppStateStorage(val dataSource: DataSource) extends TransactionalKeyValueS
31
30
put(Keys .FastSyncDone , true .toString)
32
31
33
32
def getEstimatedHighestBlock (): BigInt =
34
- BigInt (get( Keys .EstimatedHighestBlock ).getOrElse( " 0 " ) )
33
+ getBigInt( Keys .EstimatedHighestBlock )
35
34
36
35
def putEstimatedHighestBlock (n : BigInt ): DataSourceBatchUpdate =
37
36
put(Keys .EstimatedHighestBlock , n.toString)
38
37
39
38
def getSyncStartingBlock (): BigInt =
40
- BigInt (get( Keys .SyncStartingBlock ).getOrElse( " 0 " ) )
39
+ getBigInt( Keys .SyncStartingBlock )
41
40
42
41
def putSyncStartingBlock (n : BigInt ): DataSourceBatchUpdate =
43
42
put(Keys .SyncStartingBlock , n.toString)
43
+
44
+ private def getBigInt (key : Key ): BigInt = {
45
+ get(key).map(BigInt (_)).getOrElse(BigInt (BigInteger .ZERO ))
46
+ }
47
+
48
+ /**
49
+ * It is safe to return zero in case of not having any checkpoint block,
50
+ * because we assume that genesis block is a kinda stable checkpoint block (without real checkpoint)
51
+ *
52
+ * @return Latest CheckpointBlock Number
53
+ */
54
+ def getLatestCheckpointBlockNumber (): BigInt =
55
+ getBigInt(Keys .LatestCheckpointBlockNumber )
56
+
57
+ def removeLatestCheckpointBlockNumber (): DataSourceBatchUpdate = {
58
+ update(toRemove = Seq (Keys .LatestCheckpointBlockNumber ), toUpsert = Nil )
59
+ }
60
+
61
+ def putLatestCheckpointBlockNumber (latestCheckpointBlockNumber : BigInt ): DataSourceBatchUpdate = {
62
+ update(Nil , Seq (Keys .LatestCheckpointBlockNumber -> latestCheckpointBlockNumber.toString))
63
+ }
44
64
}
45
65
46
66
object AppStateStorage {
67
+ type Key = String
47
68
type Value = String
48
69
49
- case class Key private (name : String )
50
-
51
70
object Keys {
52
- val BestBlockNumber = Key (" BestBlockNumber" )
53
- val FastSyncDone = Key (" FastSyncDone" )
54
- val EstimatedHighestBlock = Key (" EstimatedHighestBlock" )
55
- val SyncStartingBlock = Key (" SyncStartingBlock" )
71
+ val BestBlockNumber = " BestBlockNumber"
72
+ val FastSyncDone = " FastSyncDone"
73
+ val EstimatedHighestBlock = " EstimatedHighestBlock"
74
+ val SyncStartingBlock = " SyncStartingBlock"
75
+ val LatestCheckpointBlockNumber = " LatestCheckpointBlockNumber"
56
76
}
57
77
}
0 commit comments