Skip to content

Commit 1d6a210

Browse files
committed
server: Disable migration
1 parent 2e3b418 commit 1d6a210

File tree

1 file changed

+5
-118
lines changed

1 file changed

+5
-118
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,18 @@
11
package com.xsn.explorer.migrations
22

3-
import anorm.SQL
4-
import com.alexitc.playsonify.core.FutureOr.Implicits.FutureOps
53
import com.xsn.explorer.data.anorm.AnormPostgresDataHandler
6-
import com.xsn.explorer.data.anorm.dao.{BlockPostgresDAO, BlockRewardPostgresDAO}
7-
import com.xsn.explorer.data.async.BlockFutureDataHandler
84
import com.xsn.explorer.executors.DatabaseExecutionContext
9-
import com.xsn.explorer.models.{BlockExtractionMethod, BlockRewards, rpc}
10-
import com.xsn.explorer.models.persisted.Block
11-
import com.xsn.explorer.models.values.{Blockhash, Height}
12-
import com.xsn.explorer.services.{BlockService, XSNService}
135
import javax.inject.Inject
14-
import org.scalactic.{Bad, Good}
156
import org.slf4j.LoggerFactory
167
import play.api.db.Database
178

18-
import scala.concurrent.{ExecutionContext, Future}
19-
import scala.util.{Failure, Success}
9+
import scala.concurrent.ExecutionContext
2010

21-
class MigrationRunner @Inject()(
22-
xsnService: XSNService,
23-
blockService: BlockService,
24-
blockDataHandler: BlockFutureDataHandler,
25-
db: MigrationRunner.DatabaseOperations
26-
)(implicit ec: ExecutionContext) {
11+
class MigrationRunner @Inject()(db: MigrationRunner.DatabaseOperations)(implicit ec: ExecutionContext) {
2712

2813
private val logger = LoggerFactory.getLogger(this.getClass)
2914

30-
def run() = {
31-
val targetBlock = blockDataHandler.getLatestBlock().toFutureOr
32-
33-
targetBlock.map { targetBlock =>
34-
logger.info(s"Migrating Block Rewards from block 0 to block ${targetBlock.height}")
35-
36-
val startingState = Future.successful(Good(())).toFutureOr
37-
val finalState = (0 to targetBlock.height.int).foldLeft(startingState) {
38-
case (state, height) =>
39-
for {
40-
_ <- state
41-
block <- blockDataHandler.getBy(Height(height)).toFutureOr
42-
rpcBlock <- xsnService.getFullBlock(block.hash).toFutureOr
43-
_ <- block.extractionMethod match {
44-
case BlockExtractionMethod.TrustlessProofOfStake =>
45-
recalculateExtractionMethod(block, rpcBlock).toFutureOr
46-
case _ => Future.successful(Good(())).toFutureOr
47-
}
48-
_ <- db.getReward(block.hash).toFutureOr.flatMap {
49-
case None => storeBlockReward(rpcBlock).toFutureOr
50-
case _ => Future.successful(Good(())).toFutureOr
51-
}
52-
_ = logProgress(height, targetBlock.height.int)
53-
} yield ()
54-
}
55-
56-
finalState.toFuture.onComplete {
57-
case Success(Good(_)) => logger.info("Block rewards successfully migrated")
58-
case Success(Bad(error)) => logger.info(s"Block reward migration failed due to ${error.toString}")
59-
case Failure(error) => logger.info(s"Block reward migration failed due to ${error.toString}")
60-
}
61-
}
62-
}
15+
def run() = {}
6316

6417
private def logProgress(migratedHeight: Int, targetHeight: Int) = {
6518
val percentage: Int = 100 * migratedHeight / targetHeight
@@ -69,76 +22,10 @@ class MigrationRunner @Inject()(
6922
logger.info(s"migrated block at height ${migratedHeight}, ${percentage}% done")
7023
}
7124
}
72-
73-
private def recalculateExtractionMethod(block: Block, rpcBlock: rpc.Block[_]) = {
74-
val result = for {
75-
extractionMethod <- blockService.extractionMethod(rpcBlock).toFutureOr
76-
_ <- db.updateExtractionMethod(block.hash, extractionMethod).toFutureOr
77-
} yield Good(())
78-
79-
result.future
80-
}
81-
82-
private def storeBlockReward(rpcBlock: rpc.Block[_]) = {
83-
val result = for {
84-
extractionMethod <- blockService.extractionMethod(rpcBlock).toFutureOr
85-
reward <- blockService
86-
.getBlockRewards(rpcBlock, extractionMethod)
87-
.map {
88-
case Good(reward) => Good(Some(reward))
89-
case Bad(_) => Good(None)
90-
}
91-
.toFutureOr
92-
_ = reward.foreach(r => db.insertBlockRewards(rpcBlock.hash, r))
93-
} yield Good(())
94-
95-
result.future
96-
}
9725
}
9826

9927
object MigrationRunner {
100-
private class DatabaseOperations @Inject()(
101-
override val database: Database,
102-
blockPostgresDAO: BlockPostgresDAO,
103-
blockRewardPostgresDAO: BlockRewardPostgresDAO
104-
)(
105-
implicit dbEC: DatabaseExecutionContext
106-
) extends AnormPostgresDataHandler {
107-
108-
def updateExtractionMethod(blockhash: Blockhash, extractionMethod: BlockExtractionMethod) = {
109-
Future {
110-
withConnection { implicit conn =>
111-
SQL(
112-
"""
113-
|UPDATE blocks
114-
|SET extraction_method = {extraction_method}::BLOCK_EXTRACTION_METHOD_TYPE
115-
|WHERE blockhash = {blockhash}
116-
""".stripMargin
117-
).on(
118-
'blockhash -> blockhash.toBytesBE.toArray,
119-
'extraction_method -> extractionMethod.entryName
120-
)
121-
.execute
122-
123-
Good(())
124-
}
125-
}
126-
}
12728

128-
def getReward(blockhash: Blockhash) = {
129-
Future {
130-
withConnection { implicit conn =>
131-
Good(blockRewardPostgresDAO.getBy(blockhash))
132-
}
133-
}
134-
}
135-
136-
def insertBlockRewards(blockhash: Blockhash, reward: BlockRewards) = {
137-
Future {
138-
withConnection { implicit conn =>
139-
Good(blockRewardPostgresDAO.upsert(blockhash, reward))
140-
}
141-
}
142-
}
143-
}
29+
class DatabaseOperations @Inject()(override val database: Database)(implicit dbEC: DatabaseExecutionContext)
30+
extends AnormPostgresDataHandler {}
14431
}

0 commit comments

Comments
 (0)