@@ -62,7 +62,7 @@ trait Blockchain {
62
62
63
63
def getLatestCheckpointBlockNumber (): BigInt
64
64
65
- def removeBlock (hash : ByteString , withState : Boolean ): Unit
65
+ def removeBlock (hash : ByteString ): Unit
66
66
67
67
def saveBestKnownBlocks (bestBlockNumber : BigInt , latestCheckpointNumber : Option [BigInt ] = None ): Unit
68
68
@@ -77,15 +77,13 @@ class BlockchainImpl(
77
77
protected val transactionMappingStorage : TransactionMappingStorage ,
78
78
protected val appStateStorage : AppStateStorage ,
79
79
protected val stateStorage : StateStorage ,
80
- blockchainReader : BlockchainReader ,
81
- blockchainMetadata : BlockchainMetadata
80
+ blockchainReader : BlockchainReader
82
81
) extends Blockchain
83
82
with Logger {
84
83
85
84
override def getChainWeightByHash (blockhash : ByteString ): Option [ChainWeight ] = chainWeightStorage.get(blockhash)
86
85
87
- override def getLatestCheckpointBlockNumber (): BigInt =
88
- blockchainMetadata.bestKnownBlockAndLatestCheckpoint.get().latestCheckpointNumber
86
+ override def getLatestCheckpointBlockNumber (): BigInt = appStateStorage.getLatestCheckpointBlockNumber()
89
87
90
88
override def getAccountStorageAt (
91
89
rootHash : ByteString ,
@@ -128,22 +126,6 @@ class BlockchainImpl(
128
126
129
127
def getReadOnlyMptStorage (): MptStorage = stateStorage.getReadOnlyStorage
130
128
131
- private def persistBestBlocksData (): Unit = {
132
- val currentBestBlockNumber = blockchainReader.getBestBlockNumber()
133
- val currentBestCheckpointNumber = getLatestCheckpointBlockNumber()
134
- log.debug(
135
- " Persisting app info data into database. Persisted block number is {}. " +
136
- " Persisted checkpoint number is {}" ,
137
- currentBestBlockNumber,
138
- currentBestCheckpointNumber
139
- )
140
-
141
- appStateStorage
142
- .putBestBlockNumber(currentBestBlockNumber)
143
- .and(appStateStorage.putLatestCheckpointBlockNumber(currentBestCheckpointNumber))
144
- .commit()
145
- }
146
-
147
129
override def saveBestKnownBlocks (bestBlockNumber : BigInt , latestCheckpointNumber : Option [BigInt ] = None ): Unit =
148
130
latestCheckpointNumber match {
149
131
case Some (number) =>
@@ -153,28 +135,29 @@ class BlockchainImpl(
153
135
}
154
136
155
137
private def saveBestKnownBlock (bestBlockNumber : BigInt ): Unit =
156
- blockchainMetadata.bestKnownBlockAndLatestCheckpoint.updateAndGet(_.copy( bestBlockNumber = bestBlockNumber) )
138
+ appStateStorage.putBestBlockNumber( bestBlockNumber).commit( )
157
139
158
140
private def saveBestKnownBlockAndLatestCheckpointNumber (number : BigInt , latestCheckpointNumber : BigInt ): Unit =
159
- blockchainMetadata.bestKnownBlockAndLatestCheckpoint.set(
160
- BestBlockLatestCheckpointNumbers (number, latestCheckpointNumber)
161
- )
141
+ appStateStorage
142
+ .putBestBlockNumber(number)
143
+ .and(appStateStorage.putLatestCheckpointBlockNumber(latestCheckpointNumber))
144
+ .commit()
162
145
163
146
private def removeBlockNumberMapping (number : BigInt ): DataSourceBatchUpdate =
164
147
blockNumberMappingStorage.remove(number)
165
148
166
- override def removeBlock (blockHash : ByteString , withState : Boolean ): Unit = {
149
+ override def removeBlock (blockHash : ByteString ): Unit = {
167
150
val maybeBlock = blockchainReader.getBlockByHash(blockHash)
168
151
169
152
maybeBlock match {
170
- case Some (block) => removeBlock(block, withState )
153
+ case Some (block) => removeBlock(block)
171
154
case None =>
172
155
log.warn(s " Attempted removing block with hash ${ByteStringUtils .hash2string(blockHash)} that we don't have " )
173
156
}
174
157
}
175
158
176
159
// scalastyle:off method.length
177
- private def removeBlock (block : Block , withState : Boolean ): Unit = {
160
+ private def removeBlock (block : Block ): Unit = {
178
161
val blockHash = block.hash
179
162
180
163
log.debug(s " Trying to remove block ${block.idTag}" )
@@ -229,17 +212,12 @@ class BlockchainImpl(
229
212
.and(latestCheckpointNumberUpdates)
230
213
.commit()
231
214
232
- saveBestKnownBlocks(newBestBlockNumber, Some (newLatestCheckpointNumber))
233
215
log.debug(
234
216
" Removed block with hash {}. New best block number - {}, new best checkpoint block number - {}" ,
235
217
ByteStringUtils .hash2string(blockHash),
236
218
newBestBlockNumber,
237
219
newLatestCheckpointNumber
238
220
)
239
-
240
- // not transactional part
241
- if (withState)
242
- stateStorage.onBlockRollback(block.number, bestBlockNumber)(() => persistBestBlocksData())
243
221
}
244
222
// scalastyle:on method.length
245
223
@@ -288,8 +266,7 @@ trait BlockchainStorages {
288
266
object BlockchainImpl {
289
267
def apply (
290
268
storages : BlockchainStorages ,
291
- blockchainReader : BlockchainReader ,
292
- metadata : BlockchainMetadata
269
+ blockchainReader : BlockchainReader
293
270
): BlockchainImpl =
294
271
new BlockchainImpl (
295
272
blockHeadersStorage = storages.blockHeadersStorage,
@@ -300,7 +277,6 @@ object BlockchainImpl {
300
277
transactionMappingStorage = storages.transactionMappingStorage,
301
278
appStateStorage = storages.appStateStorage,
302
279
stateStorage = storages.stateStorage,
303
- blockchainReader = blockchainReader,
304
- blockchainMetadata = metadata
280
+ blockchainReader = blockchainReader
305
281
)
306
282
}
0 commit comments