1
1
package com .xsn .explorer .migrations
2
2
3
- import anorm .SQL
4
- import com .alexitc .playsonify .core .FutureOr .Implicits .FutureOps
5
3
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
8
4
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 }
13
5
import javax .inject .Inject
14
- import org .scalactic .{Bad , Good }
15
6
import org .slf4j .LoggerFactory
16
7
import play .api .db .Database
17
8
18
- import scala .concurrent .{ExecutionContext , Future }
19
- import scala .util .{Failure , Success }
9
+ import scala .concurrent .ExecutionContext
20
10
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 ) {
27
12
28
13
private val logger = LoggerFactory .getLogger(this .getClass)
29
14
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 () = {}
63
16
64
17
private def logProgress (migratedHeight : Int , targetHeight : Int ) = {
65
18
val percentage : Int = 100 * migratedHeight / targetHeight
@@ -69,76 +22,10 @@ class MigrationRunner @Inject()(
69
22
logger.info(s " migrated block at height ${migratedHeight}, ${percentage}% done " )
70
23
}
71
24
}
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
- }
97
25
}
98
26
99
27
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
- }
127
28
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 {}
144
31
}
0 commit comments